]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/sqlite3/shell.c
Fix multiple vulnerabilities in sqlite3.
[FreeBSD/FreeBSD.git] / contrib / sqlite3 / shell.c
1 /* DO NOT EDIT!
2 ** This file is automatically generated by the script in the canonical
3 ** SQLite source tree at tool/mkshellc.tcl.  That script combines source
4 ** code from various constituent source files of SQLite into this single
5 ** "shell.c" file used to implement the SQLite command-line shell.
6 **
7 ** Most of the code found below comes from the "src/shell.c.in" file in
8 ** the canonical SQLite source tree.  That main file contains "INCLUDE"
9 ** lines that specify other files in the canonical source tree that are
10 ** inserted to getnerate this complete program source file.
11 **
12 ** The code from multiple files is combined into this single "shell.c"
13 ** source file to help make the command-line program easier to compile.
14 **
15 ** To modify this program, get a copy of the canonical SQLite source tree,
16 ** edit the src/shell.c.in" and/or some of the other files that are included
17 ** by "src/shell.c.in", then rerun the tool/mkshellc.tcl script.
18 */
19 /*
20 ** 2001 September 15
21 **
22 ** The author disclaims copyright to this source code.  In place of
23 ** a legal notice, here is a blessing:
24 **
25 **    May you do good and not evil.
26 **    May you find forgiveness for yourself and forgive others.
27 **    May you share freely, never taking more than you give.
28 **
29 *************************************************************************
30 ** This file contains code to implement the "sqlite" command line
31 ** utility for accessing SQLite databases.
32 */
33 #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
34 /* This needs to come before any includes for MSVC compiler */
35 #define _CRT_SECURE_NO_WARNINGS
36 #endif
37
38 /*
39 ** Determine if we are dealing with WinRT, which provides only a subset of
40 ** the full Win32 API.
41 */
42 #if !defined(SQLITE_OS_WINRT)
43 # define SQLITE_OS_WINRT 0
44 #endif
45
46 /*
47 ** Warning pragmas copied from msvc.h in the core.
48 */
49 #if defined(_MSC_VER)
50 #pragma warning(disable : 4054)
51 #pragma warning(disable : 4055)
52 #pragma warning(disable : 4100)
53 #pragma warning(disable : 4127)
54 #pragma warning(disable : 4130)
55 #pragma warning(disable : 4152)
56 #pragma warning(disable : 4189)
57 #pragma warning(disable : 4206)
58 #pragma warning(disable : 4210)
59 #pragma warning(disable : 4232)
60 #pragma warning(disable : 4244)
61 #pragma warning(disable : 4305)
62 #pragma warning(disable : 4306)
63 #pragma warning(disable : 4702)
64 #pragma warning(disable : 4706)
65 #endif /* defined(_MSC_VER) */
66
67 /*
68 ** No support for loadable extensions in VxWorks.
69 */
70 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
71 # define SQLITE_OMIT_LOAD_EXTENSION 1
72 #endif
73
74 /*
75 ** Enable large-file support for fopen() and friends on unix.
76 */
77 #ifndef SQLITE_DISABLE_LFS
78 # define _LARGE_FILE       1
79 # ifndef _FILE_OFFSET_BITS
80 #   define _FILE_OFFSET_BITS 64
81 # endif
82 # define _LARGEFILE_SOURCE 1
83 #endif
84
85 #include <stdlib.h>
86 #include <string.h>
87 #include <stdio.h>
88 #include <assert.h>
89 #include "sqlite3.h"
90 typedef sqlite3_int64 i64;
91 typedef sqlite3_uint64 u64;
92 typedef unsigned char u8;
93 #if SQLITE_USER_AUTHENTICATION
94 # include "sqlite3userauth.h"
95 #endif
96 #include <ctype.h>
97 #include <stdarg.h>
98
99 #if !defined(_WIN32) && !defined(WIN32)
100 # include <signal.h>
101 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
102 #  include <pwd.h>
103 # endif
104 #endif
105 #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
106 # include <unistd.h>
107 # include <dirent.h>
108 # define GETPID getpid
109 # if defined(__MINGW32__)
110 #  define DIRENT dirent
111 #  ifndef S_ISLNK
112 #   define S_ISLNK(mode) (0)
113 #  endif
114 # endif
115 #else
116 # define GETPID (int)GetCurrentProcessId
117 #endif
118 #include <sys/types.h>
119 #include <sys/stat.h>
120
121 #if HAVE_READLINE
122 # include <readline/readline.h>
123 # include <readline/history.h>
124 #endif
125
126 #if HAVE_EDITLINE
127 # include <editline/readline.h>
128 #endif
129
130 #if HAVE_EDITLINE || HAVE_READLINE
131
132 # define shell_add_history(X) add_history(X)
133 # define shell_read_history(X) read_history(X)
134 # define shell_write_history(X) write_history(X)
135 # define shell_stifle_history(X) stifle_history(X)
136 # define shell_readline(X) readline(X)
137
138 #elif HAVE_LINENOISE
139
140 # include "linenoise.h"
141 # define shell_add_history(X) linenoiseHistoryAdd(X)
142 # define shell_read_history(X) linenoiseHistoryLoad(X)
143 # define shell_write_history(X) linenoiseHistorySave(X)
144 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
145 # define shell_readline(X) linenoise(X)
146
147 #else
148
149 # define shell_read_history(X)
150 # define shell_write_history(X)
151 # define shell_stifle_history(X)
152
153 # define SHELL_USE_LOCAL_GETLINE 1
154 #endif
155
156
157 #if defined(_WIN32) || defined(WIN32)
158 # if SQLITE_OS_WINRT
159 #  define SQLITE_OMIT_POPEN 1
160 # else
161 #  include <io.h>
162 #  include <fcntl.h>
163 #  define isatty(h) _isatty(h)
164 #  ifndef access
165 #   define access(f,m) _access((f),(m))
166 #  endif
167 #  ifndef unlink
168 #   define unlink _unlink
169 #  endif
170 #  ifndef strdup
171 #   define strdup _strdup
172 #  endif
173 #  undef popen
174 #  define popen _popen
175 #  undef pclose
176 #  define pclose _pclose
177 # endif
178 #else
179  /* Make sure isatty() has a prototype. */
180  extern int isatty(int);
181
182 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
183   /* popen and pclose are not C89 functions and so are
184   ** sometimes omitted from the <stdio.h> header */
185    extern FILE *popen(const char*,const char*);
186    extern int pclose(FILE*);
187 # else
188 #  define SQLITE_OMIT_POPEN 1
189 # endif
190 #endif
191
192 #if defined(_WIN32_WCE)
193 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
194  * thus we always assume that we have a console. That can be
195  * overridden with the -batch command line option.
196  */
197 #define isatty(x) 1
198 #endif
199
200 /* ctype macros that work with signed characters */
201 #define IsSpace(X)  isspace((unsigned char)X)
202 #define IsDigit(X)  isdigit((unsigned char)X)
203 #define ToLower(X)  (char)tolower((unsigned char)X)
204
205 #if defined(_WIN32) || defined(WIN32)
206 #if SQLITE_OS_WINRT
207 #include <intrin.h>
208 #endif
209 #include <windows.h>
210
211 /* string conversion routines only needed on Win32 */
212 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
213 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
214 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
215 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
216 #endif
217
218 /* On Windows, we normally run with output mode of TEXT so that \n characters
219 ** are automatically translated into \r\n.  However, this behavior needs
220 ** to be disabled in some cases (ex: when generating CSV output and when
221 ** rendering quoted strings that contain \n characters).  The following
222 ** routines take care of that.
223 */
224 #if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
225 static void setBinaryMode(FILE *file, int isOutput){
226   if( isOutput ) fflush(file);
227   _setmode(_fileno(file), _O_BINARY);
228 }
229 static void setTextMode(FILE *file, int isOutput){
230   if( isOutput ) fflush(file);
231   _setmode(_fileno(file), _O_TEXT);
232 }
233 #else
234 # define setBinaryMode(X,Y)
235 # define setTextMode(X,Y)
236 #endif
237
238
239 /* True if the timer is enabled */
240 static int enableTimer = 0;
241
242 /* Return the current wall-clock time */
243 static sqlite3_int64 timeOfDay(void){
244   static sqlite3_vfs *clockVfs = 0;
245   sqlite3_int64 t;
246   if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
247   if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
248     clockVfs->xCurrentTimeInt64(clockVfs, &t);
249   }else{
250     double r;
251     clockVfs->xCurrentTime(clockVfs, &r);
252     t = (sqlite3_int64)(r*86400000.0);
253   }
254   return t;
255 }
256
257 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
258 #include <sys/time.h>
259 #include <sys/resource.h>
260
261 /* VxWorks does not support getrusage() as far as we can determine */
262 #if defined(_WRS_KERNEL) || defined(__RTP__)
263 struct rusage {
264   struct timeval ru_utime; /* user CPU time used */
265   struct timeval ru_stime; /* system CPU time used */
266 };
267 #define getrusage(A,B) memset(B,0,sizeof(*B))
268 #endif
269
270 /* Saved resource information for the beginning of an operation */
271 static struct rusage sBegin;  /* CPU time at start */
272 static sqlite3_int64 iBegin;  /* Wall-clock time at start */
273
274 /*
275 ** Begin timing an operation
276 */
277 static void beginTimer(void){
278   if( enableTimer ){
279     getrusage(RUSAGE_SELF, &sBegin);
280     iBegin = timeOfDay();
281   }
282 }
283
284 /* Return the difference of two time_structs in seconds */
285 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
286   return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
287          (double)(pEnd->tv_sec - pStart->tv_sec);
288 }
289
290 /*
291 ** Print the timing results.
292 */
293 static void endTimer(void){
294   if( enableTimer ){
295     sqlite3_int64 iEnd = timeOfDay();
296     struct rusage sEnd;
297     getrusage(RUSAGE_SELF, &sEnd);
298     printf("Run Time: real %.3f user %f sys %f\n",
299        (iEnd - iBegin)*0.001,
300        timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
301        timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
302   }
303 }
304
305 #define BEGIN_TIMER beginTimer()
306 #define END_TIMER endTimer()
307 #define HAS_TIMER 1
308
309 #elif (defined(_WIN32) || defined(WIN32))
310
311 /* Saved resource information for the beginning of an operation */
312 static HANDLE hProcess;
313 static FILETIME ftKernelBegin;
314 static FILETIME ftUserBegin;
315 static sqlite3_int64 ftWallBegin;
316 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
317                                     LPFILETIME, LPFILETIME);
318 static GETPROCTIMES getProcessTimesAddr = NULL;
319
320 /*
321 ** Check to see if we have timer support.  Return 1 if necessary
322 ** support found (or found previously).
323 */
324 static int hasTimer(void){
325   if( getProcessTimesAddr ){
326     return 1;
327   } else {
328 #if !SQLITE_OS_WINRT
329     /* GetProcessTimes() isn't supported in WIN95 and some other Windows
330     ** versions. See if the version we are running on has it, and if it
331     ** does, save off a pointer to it and the current process handle.
332     */
333     hProcess = GetCurrentProcess();
334     if( hProcess ){
335       HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
336       if( NULL != hinstLib ){
337         getProcessTimesAddr =
338             (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
339         if( NULL != getProcessTimesAddr ){
340           return 1;
341         }
342         FreeLibrary(hinstLib);
343       }
344     }
345 #endif
346   }
347   return 0;
348 }
349
350 /*
351 ** Begin timing an operation
352 */
353 static void beginTimer(void){
354   if( enableTimer && getProcessTimesAddr ){
355     FILETIME ftCreation, ftExit;
356     getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
357                         &ftKernelBegin,&ftUserBegin);
358     ftWallBegin = timeOfDay();
359   }
360 }
361
362 /* Return the difference of two FILETIME structs in seconds */
363 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
364   sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
365   sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
366   return (double) ((i64End - i64Start) / 10000000.0);
367 }
368
369 /*
370 ** Print the timing results.
371 */
372 static void endTimer(void){
373   if( enableTimer && getProcessTimesAddr){
374     FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
375     sqlite3_int64 ftWallEnd = timeOfDay();
376     getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
377     printf("Run Time: real %.3f user %f sys %f\n",
378        (ftWallEnd - ftWallBegin)*0.001,
379        timeDiff(&ftUserBegin, &ftUserEnd),
380        timeDiff(&ftKernelBegin, &ftKernelEnd));
381   }
382 }
383
384 #define BEGIN_TIMER beginTimer()
385 #define END_TIMER endTimer()
386 #define HAS_TIMER hasTimer()
387
388 #else
389 #define BEGIN_TIMER
390 #define END_TIMER
391 #define HAS_TIMER 0
392 #endif
393
394 /*
395 ** Used to prevent warnings about unused parameters
396 */
397 #define UNUSED_PARAMETER(x) (void)(x)
398
399 /*
400 ** Number of elements in an array
401 */
402 #define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
403
404 /*
405 ** If the following flag is set, then command execution stops
406 ** at an error if we are not interactive.
407 */
408 static int bail_on_error = 0;
409
410 /*
411 ** Threat stdin as an interactive input if the following variable
412 ** is true.  Otherwise, assume stdin is connected to a file or pipe.
413 */
414 static int stdin_is_interactive = 1;
415
416 /*
417 ** On Windows systems we have to know if standard output is a console
418 ** in order to translate UTF-8 into MBCS.  The following variable is
419 ** true if translation is required.
420 */
421 static int stdout_is_console = 1;
422
423 /*
424 ** The following is the open SQLite database.  We make a pointer
425 ** to this database a static variable so that it can be accessed
426 ** by the SIGINT handler to interrupt database processing.
427 */
428 static sqlite3 *globalDb = 0;
429
430 /*
431 ** True if an interrupt (Control-C) has been received.
432 */
433 static volatile int seenInterrupt = 0;
434
435 #ifdef SQLITE_DEBUG
436 /*
437 ** Out-of-memory simulator variables
438 */
439 static unsigned int oomCounter = 0;    /* Simulate OOM when equals 1 */
440 static unsigned int oomRepeat = 0;     /* Number of OOMs in a row */
441 static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
442 #endif /* SQLITE_DEBUG */
443
444 /*
445 ** This is the name of our program. It is set in main(), used
446 ** in a number of other places, mostly for error messages.
447 */
448 static char *Argv0;
449
450 /*
451 ** Prompt strings. Initialized in main. Settable with
452 **   .prompt main continue
453 */
454 static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
455 static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
456
457 /*
458 ** Render output like fprintf().  Except, if the output is going to the
459 ** console and if this is running on a Windows machine, translate the
460 ** output from UTF-8 into MBCS.
461 */
462 #if defined(_WIN32) || defined(WIN32)
463 void utf8_printf(FILE *out, const char *zFormat, ...){
464   va_list ap;
465   va_start(ap, zFormat);
466   if( stdout_is_console && (out==stdout || out==stderr) ){
467     char *z1 = sqlite3_vmprintf(zFormat, ap);
468     char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
469     sqlite3_free(z1);
470     fputs(z2, out);
471     sqlite3_free(z2);
472   }else{
473     vfprintf(out, zFormat, ap);
474   }
475   va_end(ap);
476 }
477 #elif !defined(utf8_printf)
478 # define utf8_printf fprintf
479 #endif
480
481 /*
482 ** Render output like fprintf().  This should not be used on anything that
483 ** includes string formatting (e.g. "%s").
484 */
485 #if !defined(raw_printf)
486 # define raw_printf fprintf
487 #endif
488
489 /* Indicate out-of-memory and exit. */
490 static void shell_out_of_memory(void){
491   raw_printf(stderr,"Error: out of memory\n");
492   exit(1);
493 }
494
495 #ifdef SQLITE_DEBUG
496 /* This routine is called when a simulated OOM occurs.  It is broken
497 ** out as a separate routine to make it easy to set a breakpoint on
498 ** the OOM
499 */
500 void shellOomFault(void){
501   if( oomRepeat>0 ){
502     oomRepeat--;
503   }else{
504     oomCounter--;
505   }
506 }
507 #endif /* SQLITE_DEBUG */
508
509 #ifdef SQLITE_DEBUG
510 /* This routine is a replacement malloc() that is used to simulate
511 ** Out-Of-Memory (OOM) errors for testing purposes.
512 */
513 static void *oomMalloc(int nByte){
514   if( oomCounter ){
515     if( oomCounter==1 ){
516       shellOomFault();
517       return 0;
518     }else{
519       oomCounter--;
520     }
521   }
522   return defaultMalloc(nByte);
523 }
524 #endif /* SQLITE_DEBUG */
525
526 #ifdef SQLITE_DEBUG
527 /* Register the OOM simulator.  This must occur before any memory
528 ** allocations */
529 static void registerOomSimulator(void){
530   sqlite3_mem_methods mem;
531   sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
532   defaultMalloc = mem.xMalloc;
533   mem.xMalloc = oomMalloc;
534   sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
535 }
536 #endif
537
538 /*
539 ** Write I/O traces to the following stream.
540 */
541 #ifdef SQLITE_ENABLE_IOTRACE
542 static FILE *iotrace = 0;
543 #endif
544
545 /*
546 ** This routine works like printf in that its first argument is a
547 ** format string and subsequent arguments are values to be substituted
548 ** in place of % fields.  The result of formatting this string
549 ** is written to iotrace.
550 */
551 #ifdef SQLITE_ENABLE_IOTRACE
552 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
553   va_list ap;
554   char *z;
555   if( iotrace==0 ) return;
556   va_start(ap, zFormat);
557   z = sqlite3_vmprintf(zFormat, ap);
558   va_end(ap);
559   utf8_printf(iotrace, "%s", z);
560   sqlite3_free(z);
561 }
562 #endif
563
564 /*
565 ** Output string zUtf to stream pOut as w characters.  If w is negative,
566 ** then right-justify the text.  W is the width in UTF-8 characters, not
567 ** in bytes.  This is different from the %*.*s specification in printf
568 ** since with %*.*s the width is measured in bytes, not characters.
569 */
570 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
571   int i;
572   int n;
573   int aw = w<0 ? -w : w;
574   char zBuf[1000];
575   if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
576   for(i=n=0; zUtf[i]; i++){
577     if( (zUtf[i]&0xc0)!=0x80 ){
578       n++;
579       if( n==aw ){
580         do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
581         break;
582       }
583     }
584   }
585   if( n>=aw ){
586     utf8_printf(pOut, "%.*s", i, zUtf);
587   }else if( w<0 ){
588     utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
589   }else{
590     utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
591   }
592 }
593
594
595 /*
596 ** Determines if a string is a number of not.
597 */
598 static int isNumber(const char *z, int *realnum){
599   if( *z=='-' || *z=='+' ) z++;
600   if( !IsDigit(*z) ){
601     return 0;
602   }
603   z++;
604   if( realnum ) *realnum = 0;
605   while( IsDigit(*z) ){ z++; }
606   if( *z=='.' ){
607     z++;
608     if( !IsDigit(*z) ) return 0;
609     while( IsDigit(*z) ){ z++; }
610     if( realnum ) *realnum = 1;
611   }
612   if( *z=='e' || *z=='E' ){
613     z++;
614     if( *z=='+' || *z=='-' ) z++;
615     if( !IsDigit(*z) ) return 0;
616     while( IsDigit(*z) ){ z++; }
617     if( realnum ) *realnum = 1;
618   }
619   return *z==0;
620 }
621
622 /*
623 ** Compute a string length that is limited to what can be stored in
624 ** lower 30 bits of a 32-bit signed integer.
625 */
626 static int strlen30(const char *z){
627   const char *z2 = z;
628   while( *z2 ){ z2++; }
629   return 0x3fffffff & (int)(z2 - z);
630 }
631
632 /*
633 ** Return the length of a string in characters.  Multibyte UTF8 characters
634 ** count as a single character.
635 */
636 static int strlenChar(const char *z){
637   int n = 0;
638   while( *z ){
639     if( (0xc0&*(z++))!=0x80 ) n++;
640   }
641   return n;
642 }
643
644 /*
645 ** This routine reads a line of text from FILE in, stores
646 ** the text in memory obtained from malloc() and returns a pointer
647 ** to the text.  NULL is returned at end of file, or if malloc()
648 ** fails.
649 **
650 ** If zLine is not NULL then it is a malloced buffer returned from
651 ** a previous call to this routine that may be reused.
652 */
653 static char *local_getline(char *zLine, FILE *in){
654   int nLine = zLine==0 ? 0 : 100;
655   int n = 0;
656
657   while( 1 ){
658     if( n+100>nLine ){
659       nLine = nLine*2 + 100;
660       zLine = realloc(zLine, nLine);
661       if( zLine==0 ) shell_out_of_memory();
662     }
663     if( fgets(&zLine[n], nLine - n, in)==0 ){
664       if( n==0 ){
665         free(zLine);
666         return 0;
667       }
668       zLine[n] = 0;
669       break;
670     }
671     while( zLine[n] ) n++;
672     if( n>0 && zLine[n-1]=='\n' ){
673       n--;
674       if( n>0 && zLine[n-1]=='\r' ) n--;
675       zLine[n] = 0;
676       break;
677     }
678   }
679 #if defined(_WIN32) || defined(WIN32)
680   /* For interactive input on Windows systems, translate the
681   ** multi-byte characterset characters into UTF-8. */
682   if( stdin_is_interactive && in==stdin ){
683     char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
684     if( zTrans ){
685       int nTrans = strlen30(zTrans)+1;
686       if( nTrans>nLine ){
687         zLine = realloc(zLine, nTrans);
688         if( zLine==0 ) shell_out_of_memory();
689       }
690       memcpy(zLine, zTrans, nTrans);
691       sqlite3_free(zTrans);
692     }
693   }
694 #endif /* defined(_WIN32) || defined(WIN32) */
695   return zLine;
696 }
697
698 /*
699 ** Retrieve a single line of input text.
700 **
701 ** If in==0 then read from standard input and prompt before each line.
702 ** If isContinuation is true, then a continuation prompt is appropriate.
703 ** If isContinuation is zero, then the main prompt should be used.
704 **
705 ** If zPrior is not NULL then it is a buffer from a prior call to this
706 ** routine that can be reused.
707 **
708 ** The result is stored in space obtained from malloc() and must either
709 ** be freed by the caller or else passed back into this routine via the
710 ** zPrior argument for reuse.
711 */
712 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
713   char *zPrompt;
714   char *zResult;
715   if( in!=0 ){
716     zResult = local_getline(zPrior, in);
717   }else{
718     zPrompt = isContinuation ? continuePrompt : mainPrompt;
719 #if SHELL_USE_LOCAL_GETLINE
720     printf("%s", zPrompt);
721     fflush(stdout);
722     zResult = local_getline(zPrior, stdin);
723 #else
724     free(zPrior);
725     zResult = shell_readline(zPrompt);
726     if( zResult && *zResult ) shell_add_history(zResult);
727 #endif
728   }
729   return zResult;
730 }
731
732
733 /*
734 ** Return the value of a hexadecimal digit.  Return -1 if the input
735 ** is not a hex digit.
736 */
737 static int hexDigitValue(char c){
738   if( c>='0' && c<='9' ) return c - '0';
739   if( c>='a' && c<='f' ) return c - 'a' + 10;
740   if( c>='A' && c<='F' ) return c - 'A' + 10;
741   return -1;
742 }
743
744 /*
745 ** Interpret zArg as an integer value, possibly with suffixes.
746 */
747 static sqlite3_int64 integerValue(const char *zArg){
748   sqlite3_int64 v = 0;
749   static const struct { char *zSuffix; int iMult; } aMult[] = {
750     { "KiB", 1024 },
751     { "MiB", 1024*1024 },
752     { "GiB", 1024*1024*1024 },
753     { "KB",  1000 },
754     { "MB",  1000000 },
755     { "GB",  1000000000 },
756     { "K",   1000 },
757     { "M",   1000000 },
758     { "G",   1000000000 },
759   };
760   int i;
761   int isNeg = 0;
762   if( zArg[0]=='-' ){
763     isNeg = 1;
764     zArg++;
765   }else if( zArg[0]=='+' ){
766     zArg++;
767   }
768   if( zArg[0]=='0' && zArg[1]=='x' ){
769     int x;
770     zArg += 2;
771     while( (x = hexDigitValue(zArg[0]))>=0 ){
772       v = (v<<4) + x;
773       zArg++;
774     }
775   }else{
776     while( IsDigit(zArg[0]) ){
777       v = v*10 + zArg[0] - '0';
778       zArg++;
779     }
780   }
781   for(i=0; i<ArraySize(aMult); i++){
782     if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
783       v *= aMult[i].iMult;
784       break;
785     }
786   }
787   return isNeg? -v : v;
788 }
789
790 /*
791 ** A variable length string to which one can append text.
792 */
793 typedef struct ShellText ShellText;
794 struct ShellText {
795   char *z;
796   int n;
797   int nAlloc;
798 };
799
800 /*
801 ** Initialize and destroy a ShellText object
802 */
803 static void initText(ShellText *p){
804   memset(p, 0, sizeof(*p));
805 }
806 static void freeText(ShellText *p){
807   free(p->z);
808   initText(p);
809 }
810
811 /* zIn is either a pointer to a NULL-terminated string in memory obtained
812 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
813 ** added to zIn, and the result returned in memory obtained from malloc().
814 ** zIn, if it was not NULL, is freed.
815 **
816 ** If the third argument, quote, is not '\0', then it is used as a
817 ** quote character for zAppend.
818 */
819 static void appendText(ShellText *p, char const *zAppend, char quote){
820   int len;
821   int i;
822   int nAppend = strlen30(zAppend);
823
824   len = nAppend+p->n+1;
825   if( quote ){
826     len += 2;
827     for(i=0; i<nAppend; i++){
828       if( zAppend[i]==quote ) len++;
829     }
830   }
831
832   if( p->n+len>=p->nAlloc ){
833     p->nAlloc = p->nAlloc*2 + len + 20;
834     p->z = realloc(p->z, p->nAlloc);
835     if( p->z==0 ) shell_out_of_memory();
836   }
837
838   if( quote ){
839     char *zCsr = p->z+p->n;
840     *zCsr++ = quote;
841     for(i=0; i<nAppend; i++){
842       *zCsr++ = zAppend[i];
843       if( zAppend[i]==quote ) *zCsr++ = quote;
844     }
845     *zCsr++ = quote;
846     p->n = (int)(zCsr - p->z);
847     *zCsr = '\0';
848   }else{
849     memcpy(p->z+p->n, zAppend, nAppend);
850     p->n += nAppend;
851     p->z[p->n] = '\0';
852   }
853 }
854
855 /*
856 ** Attempt to determine if identifier zName needs to be quoted, either
857 ** because it contains non-alphanumeric characters, or because it is an
858 ** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
859 ** that quoting is required.
860 **
861 ** Return '"' if quoting is required.  Return 0 if no quoting is required.
862 */
863 static char quoteChar(const char *zName){
864   int i;
865   if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
866   for(i=0; zName[i]; i++){
867     if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
868   }
869   return sqlite3_keyword_check(zName, i) ? '"' : 0;
870 }
871
872 /*
873 ** Construct a fake object name and column list to describe the structure
874 ** of the view, virtual table, or table valued function zSchema.zName.
875 */
876 static char *shellFakeSchema(
877   sqlite3 *db,            /* The database connection containing the vtab */
878   const char *zSchema,    /* Schema of the database holding the vtab */
879   const char *zName       /* The name of the virtual table */
880 ){
881   sqlite3_stmt *pStmt = 0;
882   char *zSql;
883   ShellText s;
884   char cQuote;
885   char *zDiv = "(";
886   int nRow = 0;
887
888   zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
889                          zSchema ? zSchema : "main", zName);
890   sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
891   sqlite3_free(zSql);
892   initText(&s);
893   if( zSchema ){
894     cQuote = quoteChar(zSchema);
895     if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
896     appendText(&s, zSchema, cQuote);
897     appendText(&s, ".", 0);
898   }
899   cQuote = quoteChar(zName);
900   appendText(&s, zName, cQuote);
901   while( sqlite3_step(pStmt)==SQLITE_ROW ){
902     const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
903     nRow++;
904     appendText(&s, zDiv, 0);
905     zDiv = ",";
906     cQuote = quoteChar(zCol);
907     appendText(&s, zCol, cQuote);
908   }
909   appendText(&s, ")", 0);
910   sqlite3_finalize(pStmt);
911   if( nRow==0 ){
912     freeText(&s);
913     s.z = 0;
914   }
915   return s.z;
916 }
917
918 /*
919 ** SQL function:  shell_module_schema(X)
920 **
921 ** Return a fake schema for the table-valued function or eponymous virtual
922 ** table X.
923 */
924 static void shellModuleSchema(
925   sqlite3_context *pCtx,
926   int nVal,
927   sqlite3_value **apVal
928 ){
929   const char *zName = (const char*)sqlite3_value_text(apVal[0]);
930   char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
931   UNUSED_PARAMETER(nVal);
932   if( zFake ){
933     sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
934                         -1, sqlite3_free);
935     free(zFake);
936   }
937 }
938
939 /*
940 ** SQL function:  shell_add_schema(S,X)
941 **
942 ** Add the schema name X to the CREATE statement in S and return the result.
943 ** Examples:
944 **
945 **    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
946 **
947 ** Also works on
948 **
949 **    CREATE INDEX
950 **    CREATE UNIQUE INDEX
951 **    CREATE VIEW
952 **    CREATE TRIGGER
953 **    CREATE VIRTUAL TABLE
954 **
955 ** This UDF is used by the .schema command to insert the schema name of
956 ** attached databases into the middle of the sqlite_master.sql field.
957 */
958 static void shellAddSchemaName(
959   sqlite3_context *pCtx,
960   int nVal,
961   sqlite3_value **apVal
962 ){
963   static const char *aPrefix[] = {
964      "TABLE",
965      "INDEX",
966      "UNIQUE INDEX",
967      "VIEW",
968      "TRIGGER",
969      "VIRTUAL TABLE"
970   };
971   int i = 0;
972   const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
973   const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
974   const char *zName = (const char*)sqlite3_value_text(apVal[2]);
975   sqlite3 *db = sqlite3_context_db_handle(pCtx);
976   UNUSED_PARAMETER(nVal);
977   if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
978     for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
979       int n = strlen30(aPrefix[i]);
980       if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
981         char *z = 0;
982         char *zFake = 0;
983         if( zSchema ){
984           char cQuote = quoteChar(zSchema);
985           if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
986             z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
987           }else{
988             z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
989           }
990         }
991         if( zName
992          && aPrefix[i][0]=='V'
993          && (zFake = shellFakeSchema(db, zSchema, zName))!=0
994         ){
995           if( z==0 ){
996             z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
997           }else{
998             z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
999           }
1000           free(zFake);
1001         }
1002         if( z ){
1003           sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1004           return;
1005         }
1006       }
1007     }
1008   }
1009   sqlite3_result_value(pCtx, apVal[0]);
1010 }
1011
1012 /*
1013 ** The source code for several run-time loadable extensions is inserted
1014 ** below by the ../tool/mkshellc.tcl script.  Before processing that included
1015 ** code, we need to override some macros to make the included program code
1016 ** work here in the middle of this regular program.
1017 */
1018 #define SQLITE_EXTENSION_INIT1
1019 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
1020
1021 #if defined(_WIN32) && defined(_MSC_VER)
1022 /************************* Begin test_windirent.h ******************/
1023 /*
1024 ** 2015 November 30
1025 **
1026 ** The author disclaims copyright to this source code.  In place of
1027 ** a legal notice, here is a blessing:
1028 **
1029 **    May you do good and not evil.
1030 **    May you find forgiveness for yourself and forgive others.
1031 **    May you share freely, never taking more than you give.
1032 **
1033 *************************************************************************
1034 ** This file contains declarations for most of the opendir() family of
1035 ** POSIX functions on Win32 using the MSVCRT.
1036 */
1037
1038 #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
1039 #define SQLITE_WINDIRENT_H
1040
1041 /*
1042 ** We need several data types from the Windows SDK header.
1043 */
1044
1045 #ifndef WIN32_LEAN_AND_MEAN
1046 #define WIN32_LEAN_AND_MEAN
1047 #endif
1048
1049 #include "windows.h"
1050
1051 /*
1052 ** We need several support functions from the SQLite core.
1053 */
1054
1055 /* #include "sqlite3.h" */
1056
1057 /*
1058 ** We need several things from the ANSI and MSVCRT headers.
1059 */
1060
1061 #include <stdio.h>
1062 #include <stdlib.h>
1063 #include <errno.h>
1064 #include <io.h>
1065 #include <limits.h>
1066 #include <sys/types.h>
1067 #include <sys/stat.h>
1068
1069 /*
1070 ** We may need several defines that should have been in "sys/stat.h".
1071 */
1072
1073 #ifndef S_ISREG
1074 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1075 #endif
1076
1077 #ifndef S_ISDIR
1078 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1079 #endif
1080
1081 #ifndef S_ISLNK
1082 #define S_ISLNK(mode) (0)
1083 #endif
1084
1085 /*
1086 ** We may need to provide the "mode_t" type.
1087 */
1088
1089 #ifndef MODE_T_DEFINED
1090   #define MODE_T_DEFINED
1091   typedef unsigned short mode_t;
1092 #endif
1093
1094 /*
1095 ** We may need to provide the "ino_t" type.
1096 */
1097
1098 #ifndef INO_T_DEFINED
1099   #define INO_T_DEFINED
1100   typedef unsigned short ino_t;
1101 #endif
1102
1103 /*
1104 ** We need to define "NAME_MAX" if it was not present in "limits.h".
1105 */
1106
1107 #ifndef NAME_MAX
1108 #  ifdef FILENAME_MAX
1109 #    define NAME_MAX (FILENAME_MAX)
1110 #  else
1111 #    define NAME_MAX (260)
1112 #  endif
1113 #endif
1114
1115 /*
1116 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1117 */
1118
1119 #ifndef NULL_INTPTR_T
1120 #  define NULL_INTPTR_T ((intptr_t)(0))
1121 #endif
1122
1123 #ifndef BAD_INTPTR_T
1124 #  define BAD_INTPTR_T ((intptr_t)(-1))
1125 #endif
1126
1127 /*
1128 ** We need to provide the necessary structures and related types.
1129 */
1130
1131 #ifndef DIRENT_DEFINED
1132 #define DIRENT_DEFINED
1133 typedef struct DIRENT DIRENT;
1134 typedef DIRENT *LPDIRENT;
1135 struct DIRENT {
1136   ino_t d_ino;               /* Sequence number, do not use. */
1137   unsigned d_attributes;     /* Win32 file attributes. */
1138   char d_name[NAME_MAX + 1]; /* Name within the directory. */
1139 };
1140 #endif
1141
1142 #ifndef DIR_DEFINED
1143 #define DIR_DEFINED
1144 typedef struct DIR DIR;
1145 typedef DIR *LPDIR;
1146 struct DIR {
1147   intptr_t d_handle; /* Value returned by "_findfirst". */
1148   DIRENT d_first;    /* DIRENT constructed based on "_findfirst". */
1149   DIRENT d_next;     /* DIRENT constructed based on "_findnext". */
1150 };
1151 #endif
1152
1153 /*
1154 ** Provide a macro, for use by the implementation, to determine if a
1155 ** particular directory entry should be skipped over when searching for
1156 ** the next directory entry that should be returned by the readdir() or
1157 ** readdir_r() functions.
1158 */
1159
1160 #ifndef is_filtered
1161 #  define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1162 #endif
1163
1164 /*
1165 ** Provide the function prototype for the POSIX compatiable getenv()
1166 ** function.  This function is not thread-safe.
1167 */
1168
1169 extern const char *windirent_getenv(const char *name);
1170
1171 /*
1172 ** Finally, we can provide the function prototypes for the opendir(),
1173 ** readdir(), readdir_r(), and closedir() POSIX functions.
1174 */
1175
1176 extern LPDIR opendir(const char *dirname);
1177 extern LPDIRENT readdir(LPDIR dirp);
1178 extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1179 extern INT closedir(LPDIR dirp);
1180
1181 #endif /* defined(WIN32) && defined(_MSC_VER) */
1182
1183 /************************* End test_windirent.h ********************/
1184 /************************* Begin test_windirent.c ******************/
1185 /*
1186 ** 2015 November 30
1187 **
1188 ** The author disclaims copyright to this source code.  In place of
1189 ** a legal notice, here is a blessing:
1190 **
1191 **    May you do good and not evil.
1192 **    May you find forgiveness for yourself and forgive others.
1193 **    May you share freely, never taking more than you give.
1194 **
1195 *************************************************************************
1196 ** This file contains code to implement most of the opendir() family of
1197 ** POSIX functions on Win32 using the MSVCRT.
1198 */
1199
1200 #if defined(_WIN32) && defined(_MSC_VER)
1201 /* #include "test_windirent.h" */
1202
1203 /*
1204 ** Implementation of the POSIX getenv() function using the Win32 API.
1205 ** This function is not thread-safe.
1206 */
1207 const char *windirent_getenv(
1208   const char *name
1209 ){
1210   static char value[32768]; /* Maximum length, per MSDN */
1211   DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1212   DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1213
1214   memset(value, 0, sizeof(value));
1215   dwRet = GetEnvironmentVariableA(name, value, dwSize);
1216   if( dwRet==0 || dwRet>dwSize ){
1217     /*
1218     ** The function call to GetEnvironmentVariableA() failed -OR-
1219     ** the buffer is not large enough.  Either way, return NULL.
1220     */
1221     return 0;
1222   }else{
1223     /*
1224     ** The function call to GetEnvironmentVariableA() succeeded
1225     ** -AND- the buffer contains the entire value.
1226     */
1227     return value;
1228   }
1229 }
1230
1231 /*
1232 ** Implementation of the POSIX opendir() function using the MSVCRT.
1233 */
1234 LPDIR opendir(
1235   const char *dirname
1236 ){
1237   struct _finddata_t data;
1238   LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1239   SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1240
1241   if( dirp==NULL ) return NULL;
1242   memset(dirp, 0, sizeof(DIR));
1243
1244   /* TODO: Remove this if Unix-style root paths are not used. */
1245   if( sqlite3_stricmp(dirname, "/")==0 ){
1246     dirname = windirent_getenv("SystemDrive");
1247   }
1248
1249   memset(&data, 0, sizeof(struct _finddata_t));
1250   _snprintf(data.name, namesize, "%s\\*", dirname);
1251   dirp->d_handle = _findfirst(data.name, &data);
1252
1253   if( dirp->d_handle==BAD_INTPTR_T ){
1254     closedir(dirp);
1255     return NULL;
1256   }
1257
1258   /* TODO: Remove this block to allow hidden and/or system files. */
1259   if( is_filtered(data) ){
1260 next:
1261
1262     memset(&data, 0, sizeof(struct _finddata_t));
1263     if( _findnext(dirp->d_handle, &data)==-1 ){
1264       closedir(dirp);
1265       return NULL;
1266     }
1267
1268     /* TODO: Remove this block to allow hidden and/or system files. */
1269     if( is_filtered(data) ) goto next;
1270   }
1271
1272   dirp->d_first.d_attributes = data.attrib;
1273   strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1274   dirp->d_first.d_name[NAME_MAX] = '\0';
1275
1276   return dirp;
1277 }
1278
1279 /*
1280 ** Implementation of the POSIX readdir() function using the MSVCRT.
1281 */
1282 LPDIRENT readdir(
1283   LPDIR dirp
1284 ){
1285   struct _finddata_t data;
1286
1287   if( dirp==NULL ) return NULL;
1288
1289   if( dirp->d_first.d_ino==0 ){
1290     dirp->d_first.d_ino++;
1291     dirp->d_next.d_ino++;
1292
1293     return &dirp->d_first;
1294   }
1295
1296 next:
1297
1298   memset(&data, 0, sizeof(struct _finddata_t));
1299   if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1300
1301   /* TODO: Remove this block to allow hidden and/or system files. */
1302   if( is_filtered(data) ) goto next;
1303
1304   dirp->d_next.d_ino++;
1305   dirp->d_next.d_attributes = data.attrib;
1306   strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1307   dirp->d_next.d_name[NAME_MAX] = '\0';
1308
1309   return &dirp->d_next;
1310 }
1311
1312 /*
1313 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
1314 */
1315 INT readdir_r(
1316   LPDIR dirp,
1317   LPDIRENT entry,
1318   LPDIRENT *result
1319 ){
1320   struct _finddata_t data;
1321
1322   if( dirp==NULL ) return EBADF;
1323
1324   if( dirp->d_first.d_ino==0 ){
1325     dirp->d_first.d_ino++;
1326     dirp->d_next.d_ino++;
1327
1328     entry->d_ino = dirp->d_first.d_ino;
1329     entry->d_attributes = dirp->d_first.d_attributes;
1330     strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1331     entry->d_name[NAME_MAX] = '\0';
1332
1333     *result = entry;
1334     return 0;
1335   }
1336
1337 next:
1338
1339   memset(&data, 0, sizeof(struct _finddata_t));
1340   if( _findnext(dirp->d_handle, &data)==-1 ){
1341     *result = NULL;
1342     return ENOENT;
1343   }
1344
1345   /* TODO: Remove this block to allow hidden and/or system files. */
1346   if( is_filtered(data) ) goto next;
1347
1348   entry->d_ino = (ino_t)-1; /* not available */
1349   entry->d_attributes = data.attrib;
1350   strncpy(entry->d_name, data.name, NAME_MAX);
1351   entry->d_name[NAME_MAX] = '\0';
1352
1353   *result = entry;
1354   return 0;
1355 }
1356
1357 /*
1358 ** Implementation of the POSIX closedir() function using the MSVCRT.
1359 */
1360 INT closedir(
1361   LPDIR dirp
1362 ){
1363   INT result = 0;
1364
1365   if( dirp==NULL ) return EINVAL;
1366
1367   if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1368     result = _findclose(dirp->d_handle);
1369   }
1370
1371   sqlite3_free(dirp);
1372   return result;
1373 }
1374
1375 #endif /* defined(WIN32) && defined(_MSC_VER) */
1376
1377 /************************* End test_windirent.c ********************/
1378 #define dirent DIRENT
1379 #endif
1380 /************************* Begin ../ext/misc/shathree.c ******************/
1381 /*
1382 ** 2017-03-08
1383 **
1384 ** The author disclaims copyright to this source code.  In place of
1385 ** a legal notice, here is a blessing:
1386 **
1387 **    May you do good and not evil.
1388 **    May you find forgiveness for yourself and forgive others.
1389 **    May you share freely, never taking more than you give.
1390 **
1391 ******************************************************************************
1392 **
1393 ** This SQLite extension implements functions that compute SHA3 hashes.
1394 ** Two SQL functions are implemented:
1395 **
1396 **     sha3(X,SIZE)
1397 **     sha3_query(Y,SIZE)
1398 **
1399 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1400 ** X is NULL.
1401 **
1402 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1403 ** and returns a hash of their results.
1404 **
1405 ** The SIZE argument is optional.  If omitted, the SHA3-256 hash algorithm
1406 ** is used.  If SIZE is included it must be one of the integers 224, 256,
1407 ** 384, or 512, to determine SHA3 hash variant that is computed.
1408 */
1409 /* #include "sqlite3ext.h" */
1410 SQLITE_EXTENSION_INIT1
1411 #include <assert.h>
1412 #include <string.h>
1413 #include <stdarg.h>
1414 /* typedef sqlite3_uint64 u64; */
1415
1416 /******************************************************************************
1417 ** The Hash Engine
1418 */
1419 /*
1420 ** Macros to determine whether the machine is big or little endian,
1421 ** and whether or not that determination is run-time or compile-time.
1422 **
1423 ** For best performance, an attempt is made to guess at the byte-order
1424 ** using C-preprocessor macros.  If that is unsuccessful, or if
1425 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1426 ** at run-time.
1427 */
1428 #ifndef SHA3_BYTEORDER
1429 # if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
1430      defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
1431      defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
1432      defined(__arm__)
1433 #   define SHA3_BYTEORDER    1234
1434 # elif defined(sparc)    || defined(__ppc__)
1435 #   define SHA3_BYTEORDER    4321
1436 # else
1437 #   define SHA3_BYTEORDER 0
1438 # endif
1439 #endif
1440
1441
1442 /*
1443 ** State structure for a SHA3 hash in progress
1444 */
1445 typedef struct SHA3Context SHA3Context;
1446 struct SHA3Context {
1447   union {
1448     u64 s[25];                /* Keccak state. 5x5 lines of 64 bits each */
1449     unsigned char x[1600];    /* ... or 1600 bytes */
1450   } u;
1451   unsigned nRate;        /* Bytes of input accepted per Keccak iteration */
1452   unsigned nLoaded;      /* Input bytes loaded into u.x[] so far this cycle */
1453   unsigned ixMask;       /* Insert next input into u.x[nLoaded^ixMask]. */
1454 };
1455
1456 /*
1457 ** A single step of the Keccak mixing function for a 1600-bit state
1458 */
1459 static void KeccakF1600Step(SHA3Context *p){
1460   int i;
1461   u64 b0, b1, b2, b3, b4;
1462   u64 c0, c1, c2, c3, c4;
1463   u64 d0, d1, d2, d3, d4;
1464   static const u64 RC[] = {
1465     0x0000000000000001ULL,  0x0000000000008082ULL,
1466     0x800000000000808aULL,  0x8000000080008000ULL,
1467     0x000000000000808bULL,  0x0000000080000001ULL,
1468     0x8000000080008081ULL,  0x8000000000008009ULL,
1469     0x000000000000008aULL,  0x0000000000000088ULL,
1470     0x0000000080008009ULL,  0x000000008000000aULL,
1471     0x000000008000808bULL,  0x800000000000008bULL,
1472     0x8000000000008089ULL,  0x8000000000008003ULL,
1473     0x8000000000008002ULL,  0x8000000000000080ULL,
1474     0x000000000000800aULL,  0x800000008000000aULL,
1475     0x8000000080008081ULL,  0x8000000000008080ULL,
1476     0x0000000080000001ULL,  0x8000000080008008ULL
1477   };
1478 # define a00 (p->u.s[0])
1479 # define a01 (p->u.s[1])
1480 # define a02 (p->u.s[2])
1481 # define a03 (p->u.s[3])
1482 # define a04 (p->u.s[4])
1483 # define a10 (p->u.s[5])
1484 # define a11 (p->u.s[6])
1485 # define a12 (p->u.s[7])
1486 # define a13 (p->u.s[8])
1487 # define a14 (p->u.s[9])
1488 # define a20 (p->u.s[10])
1489 # define a21 (p->u.s[11])
1490 # define a22 (p->u.s[12])
1491 # define a23 (p->u.s[13])
1492 # define a24 (p->u.s[14])
1493 # define a30 (p->u.s[15])
1494 # define a31 (p->u.s[16])
1495 # define a32 (p->u.s[17])
1496 # define a33 (p->u.s[18])
1497 # define a34 (p->u.s[19])
1498 # define a40 (p->u.s[20])
1499 # define a41 (p->u.s[21])
1500 # define a42 (p->u.s[22])
1501 # define a43 (p->u.s[23])
1502 # define a44 (p->u.s[24])
1503 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
1504
1505   for(i=0; i<24; i+=4){
1506     c0 = a00^a10^a20^a30^a40;
1507     c1 = a01^a11^a21^a31^a41;
1508     c2 = a02^a12^a22^a32^a42;
1509     c3 = a03^a13^a23^a33^a43;
1510     c4 = a04^a14^a24^a34^a44;
1511     d0 = c4^ROL64(c1, 1);
1512     d1 = c0^ROL64(c2, 1);
1513     d2 = c1^ROL64(c3, 1);
1514     d3 = c2^ROL64(c4, 1);
1515     d4 = c3^ROL64(c0, 1);
1516
1517     b0 = (a00^d0);
1518     b1 = ROL64((a11^d1), 44);
1519     b2 = ROL64((a22^d2), 43);
1520     b3 = ROL64((a33^d3), 21);
1521     b4 = ROL64((a44^d4), 14);
1522     a00 =   b0 ^((~b1)&  b2 );
1523     a00 ^= RC[i];
1524     a11 =   b1 ^((~b2)&  b3 );
1525     a22 =   b2 ^((~b3)&  b4 );
1526     a33 =   b3 ^((~b4)&  b0 );
1527     a44 =   b4 ^((~b0)&  b1 );
1528
1529     b2 = ROL64((a20^d0), 3);
1530     b3 = ROL64((a31^d1), 45);
1531     b4 = ROL64((a42^d2), 61);
1532     b0 = ROL64((a03^d3), 28);
1533     b1 = ROL64((a14^d4), 20);
1534     a20 =   b0 ^((~b1)&  b2 );
1535     a31 =   b1 ^((~b2)&  b3 );
1536     a42 =   b2 ^((~b3)&  b4 );
1537     a03 =   b3 ^((~b4)&  b0 );
1538     a14 =   b4 ^((~b0)&  b1 );
1539
1540     b4 = ROL64((a40^d0), 18);
1541     b0 = ROL64((a01^d1), 1);
1542     b1 = ROL64((a12^d2), 6);
1543     b2 = ROL64((a23^d3), 25);
1544     b3 = ROL64((a34^d4), 8);
1545     a40 =   b0 ^((~b1)&  b2 );
1546     a01 =   b1 ^((~b2)&  b3 );
1547     a12 =   b2 ^((~b3)&  b4 );
1548     a23 =   b3 ^((~b4)&  b0 );
1549     a34 =   b4 ^((~b0)&  b1 );
1550
1551     b1 = ROL64((a10^d0), 36);
1552     b2 = ROL64((a21^d1), 10);
1553     b3 = ROL64((a32^d2), 15);
1554     b4 = ROL64((a43^d3), 56);
1555     b0 = ROL64((a04^d4), 27);
1556     a10 =   b0 ^((~b1)&  b2 );
1557     a21 =   b1 ^((~b2)&  b3 );
1558     a32 =   b2 ^((~b3)&  b4 );
1559     a43 =   b3 ^((~b4)&  b0 );
1560     a04 =   b4 ^((~b0)&  b1 );
1561
1562     b3 = ROL64((a30^d0), 41);
1563     b4 = ROL64((a41^d1), 2);
1564     b0 = ROL64((a02^d2), 62);
1565     b1 = ROL64((a13^d3), 55);
1566     b2 = ROL64((a24^d4), 39);
1567     a30 =   b0 ^((~b1)&  b2 );
1568     a41 =   b1 ^((~b2)&  b3 );
1569     a02 =   b2 ^((~b3)&  b4 );
1570     a13 =   b3 ^((~b4)&  b0 );
1571     a24 =   b4 ^((~b0)&  b1 );
1572
1573     c0 = a00^a20^a40^a10^a30;
1574     c1 = a11^a31^a01^a21^a41;
1575     c2 = a22^a42^a12^a32^a02;
1576     c3 = a33^a03^a23^a43^a13;
1577     c4 = a44^a14^a34^a04^a24;
1578     d0 = c4^ROL64(c1, 1);
1579     d1 = c0^ROL64(c2, 1);
1580     d2 = c1^ROL64(c3, 1);
1581     d3 = c2^ROL64(c4, 1);
1582     d4 = c3^ROL64(c0, 1);
1583
1584     b0 = (a00^d0);
1585     b1 = ROL64((a31^d1), 44);
1586     b2 = ROL64((a12^d2), 43);
1587     b3 = ROL64((a43^d3), 21);
1588     b4 = ROL64((a24^d4), 14);
1589     a00 =   b0 ^((~b1)&  b2 );
1590     a00 ^= RC[i+1];
1591     a31 =   b1 ^((~b2)&  b3 );
1592     a12 =   b2 ^((~b3)&  b4 );
1593     a43 =   b3 ^((~b4)&  b0 );
1594     a24 =   b4 ^((~b0)&  b1 );
1595
1596     b2 = ROL64((a40^d0), 3);
1597     b3 = ROL64((a21^d1), 45);
1598     b4 = ROL64((a02^d2), 61);
1599     b0 = ROL64((a33^d3), 28);
1600     b1 = ROL64((a14^d4), 20);
1601     a40 =   b0 ^((~b1)&  b2 );
1602     a21 =   b1 ^((~b2)&  b3 );
1603     a02 =   b2 ^((~b3)&  b4 );
1604     a33 =   b3 ^((~b4)&  b0 );
1605     a14 =   b4 ^((~b0)&  b1 );
1606
1607     b4 = ROL64((a30^d0), 18);
1608     b0 = ROL64((a11^d1), 1);
1609     b1 = ROL64((a42^d2), 6);
1610     b2 = ROL64((a23^d3), 25);
1611     b3 = ROL64((a04^d4), 8);
1612     a30 =   b0 ^((~b1)&  b2 );
1613     a11 =   b1 ^((~b2)&  b3 );
1614     a42 =   b2 ^((~b3)&  b4 );
1615     a23 =   b3 ^((~b4)&  b0 );
1616     a04 =   b4 ^((~b0)&  b1 );
1617
1618     b1 = ROL64((a20^d0), 36);
1619     b2 = ROL64((a01^d1), 10);
1620     b3 = ROL64((a32^d2), 15);
1621     b4 = ROL64((a13^d3), 56);
1622     b0 = ROL64((a44^d4), 27);
1623     a20 =   b0 ^((~b1)&  b2 );
1624     a01 =   b1 ^((~b2)&  b3 );
1625     a32 =   b2 ^((~b3)&  b4 );
1626     a13 =   b3 ^((~b4)&  b0 );
1627     a44 =   b4 ^((~b0)&  b1 );
1628
1629     b3 = ROL64((a10^d0), 41);
1630     b4 = ROL64((a41^d1), 2);
1631     b0 = ROL64((a22^d2), 62);
1632     b1 = ROL64((a03^d3), 55);
1633     b2 = ROL64((a34^d4), 39);
1634     a10 =   b0 ^((~b1)&  b2 );
1635     a41 =   b1 ^((~b2)&  b3 );
1636     a22 =   b2 ^((~b3)&  b4 );
1637     a03 =   b3 ^((~b4)&  b0 );
1638     a34 =   b4 ^((~b0)&  b1 );
1639
1640     c0 = a00^a40^a30^a20^a10;
1641     c1 = a31^a21^a11^a01^a41;
1642     c2 = a12^a02^a42^a32^a22;
1643     c3 = a43^a33^a23^a13^a03;
1644     c4 = a24^a14^a04^a44^a34;
1645     d0 = c4^ROL64(c1, 1);
1646     d1 = c0^ROL64(c2, 1);
1647     d2 = c1^ROL64(c3, 1);
1648     d3 = c2^ROL64(c4, 1);
1649     d4 = c3^ROL64(c0, 1);
1650
1651     b0 = (a00^d0);
1652     b1 = ROL64((a21^d1), 44);
1653     b2 = ROL64((a42^d2), 43);
1654     b3 = ROL64((a13^d3), 21);
1655     b4 = ROL64((a34^d4), 14);
1656     a00 =   b0 ^((~b1)&  b2 );
1657     a00 ^= RC[i+2];
1658     a21 =   b1 ^((~b2)&  b3 );
1659     a42 =   b2 ^((~b3)&  b4 );
1660     a13 =   b3 ^((~b4)&  b0 );
1661     a34 =   b4 ^((~b0)&  b1 );
1662
1663     b2 = ROL64((a30^d0), 3);
1664     b3 = ROL64((a01^d1), 45);
1665     b4 = ROL64((a22^d2), 61);
1666     b0 = ROL64((a43^d3), 28);
1667     b1 = ROL64((a14^d4), 20);
1668     a30 =   b0 ^((~b1)&  b2 );
1669     a01 =   b1 ^((~b2)&  b3 );
1670     a22 =   b2 ^((~b3)&  b4 );
1671     a43 =   b3 ^((~b4)&  b0 );
1672     a14 =   b4 ^((~b0)&  b1 );
1673
1674     b4 = ROL64((a10^d0), 18);
1675     b0 = ROL64((a31^d1), 1);
1676     b1 = ROL64((a02^d2), 6);
1677     b2 = ROL64((a23^d3), 25);
1678     b3 = ROL64((a44^d4), 8);
1679     a10 =   b0 ^((~b1)&  b2 );
1680     a31 =   b1 ^((~b2)&  b3 );
1681     a02 =   b2 ^((~b3)&  b4 );
1682     a23 =   b3 ^((~b4)&  b0 );
1683     a44 =   b4 ^((~b0)&  b1 );
1684
1685     b1 = ROL64((a40^d0), 36);
1686     b2 = ROL64((a11^d1), 10);
1687     b3 = ROL64((a32^d2), 15);
1688     b4 = ROL64((a03^d3), 56);
1689     b0 = ROL64((a24^d4), 27);
1690     a40 =   b0 ^((~b1)&  b2 );
1691     a11 =   b1 ^((~b2)&  b3 );
1692     a32 =   b2 ^((~b3)&  b4 );
1693     a03 =   b3 ^((~b4)&  b0 );
1694     a24 =   b4 ^((~b0)&  b1 );
1695
1696     b3 = ROL64((a20^d0), 41);
1697     b4 = ROL64((a41^d1), 2);
1698     b0 = ROL64((a12^d2), 62);
1699     b1 = ROL64((a33^d3), 55);
1700     b2 = ROL64((a04^d4), 39);
1701     a20 =   b0 ^((~b1)&  b2 );
1702     a41 =   b1 ^((~b2)&  b3 );
1703     a12 =   b2 ^((~b3)&  b4 );
1704     a33 =   b3 ^((~b4)&  b0 );
1705     a04 =   b4 ^((~b0)&  b1 );
1706
1707     c0 = a00^a30^a10^a40^a20;
1708     c1 = a21^a01^a31^a11^a41;
1709     c2 = a42^a22^a02^a32^a12;
1710     c3 = a13^a43^a23^a03^a33;
1711     c4 = a34^a14^a44^a24^a04;
1712     d0 = c4^ROL64(c1, 1);
1713     d1 = c0^ROL64(c2, 1);
1714     d2 = c1^ROL64(c3, 1);
1715     d3 = c2^ROL64(c4, 1);
1716     d4 = c3^ROL64(c0, 1);
1717
1718     b0 = (a00^d0);
1719     b1 = ROL64((a01^d1), 44);
1720     b2 = ROL64((a02^d2), 43);
1721     b3 = ROL64((a03^d3), 21);
1722     b4 = ROL64((a04^d4), 14);
1723     a00 =   b0 ^((~b1)&  b2 );
1724     a00 ^= RC[i+3];
1725     a01 =   b1 ^((~b2)&  b3 );
1726     a02 =   b2 ^((~b3)&  b4 );
1727     a03 =   b3 ^((~b4)&  b0 );
1728     a04 =   b4 ^((~b0)&  b1 );
1729
1730     b2 = ROL64((a10^d0), 3);
1731     b3 = ROL64((a11^d1), 45);
1732     b4 = ROL64((a12^d2), 61);
1733     b0 = ROL64((a13^d3), 28);
1734     b1 = ROL64((a14^d4), 20);
1735     a10 =   b0 ^((~b1)&  b2 );
1736     a11 =   b1 ^((~b2)&  b3 );
1737     a12 =   b2 ^((~b3)&  b4 );
1738     a13 =   b3 ^((~b4)&  b0 );
1739     a14 =   b4 ^((~b0)&  b1 );
1740
1741     b4 = ROL64((a20^d0), 18);
1742     b0 = ROL64((a21^d1), 1);
1743     b1 = ROL64((a22^d2), 6);
1744     b2 = ROL64((a23^d3), 25);
1745     b3 = ROL64((a24^d4), 8);
1746     a20 =   b0 ^((~b1)&  b2 );
1747     a21 =   b1 ^((~b2)&  b3 );
1748     a22 =   b2 ^((~b3)&  b4 );
1749     a23 =   b3 ^((~b4)&  b0 );
1750     a24 =   b4 ^((~b0)&  b1 );
1751
1752     b1 = ROL64((a30^d0), 36);
1753     b2 = ROL64((a31^d1), 10);
1754     b3 = ROL64((a32^d2), 15);
1755     b4 = ROL64((a33^d3), 56);
1756     b0 = ROL64((a34^d4), 27);
1757     a30 =   b0 ^((~b1)&  b2 );
1758     a31 =   b1 ^((~b2)&  b3 );
1759     a32 =   b2 ^((~b3)&  b4 );
1760     a33 =   b3 ^((~b4)&  b0 );
1761     a34 =   b4 ^((~b0)&  b1 );
1762
1763     b3 = ROL64((a40^d0), 41);
1764     b4 = ROL64((a41^d1), 2);
1765     b0 = ROL64((a42^d2), 62);
1766     b1 = ROL64((a43^d3), 55);
1767     b2 = ROL64((a44^d4), 39);
1768     a40 =   b0 ^((~b1)&  b2 );
1769     a41 =   b1 ^((~b2)&  b3 );
1770     a42 =   b2 ^((~b3)&  b4 );
1771     a43 =   b3 ^((~b4)&  b0 );
1772     a44 =   b4 ^((~b0)&  b1 );
1773   }
1774 }
1775
1776 /*
1777 ** Initialize a new hash.  iSize determines the size of the hash
1778 ** in bits and should be one of 224, 256, 384, or 512.  Or iSize
1779 ** can be zero to use the default hash size of 256 bits.
1780 */
1781 static void SHA3Init(SHA3Context *p, int iSize){
1782   memset(p, 0, sizeof(*p));
1783   if( iSize>=128 && iSize<=512 ){
1784     p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1785   }else{
1786     p->nRate = (1600 - 2*256)/8;
1787   }
1788 #if SHA3_BYTEORDER==1234
1789   /* Known to be little-endian at compile-time. No-op */
1790 #elif SHA3_BYTEORDER==4321
1791   p->ixMask = 7;  /* Big-endian */
1792 #else
1793   {
1794     static unsigned int one = 1;
1795     if( 1==*(unsigned char*)&one ){
1796       /* Little endian.  No byte swapping. */
1797       p->ixMask = 0;
1798     }else{
1799       /* Big endian.  Byte swap. */
1800       p->ixMask = 7;
1801     }
1802   }
1803 #endif
1804 }
1805
1806 /*
1807 ** Make consecutive calls to the SHA3Update function to add new content
1808 ** to the hash
1809 */
1810 static void SHA3Update(
1811   SHA3Context *p,
1812   const unsigned char *aData,
1813   unsigned int nData
1814 ){
1815   unsigned int i = 0;
1816 #if SHA3_BYTEORDER==1234
1817   if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1818     for(; i+7<nData; i+=8){
1819       p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1820       p->nLoaded += 8;
1821       if( p->nLoaded>=p->nRate ){
1822         KeccakF1600Step(p);
1823         p->nLoaded = 0;
1824       }
1825     }
1826   }
1827 #endif
1828   for(; i<nData; i++){
1829 #if SHA3_BYTEORDER==1234
1830     p->u.x[p->nLoaded] ^= aData[i];
1831 #elif SHA3_BYTEORDER==4321
1832     p->u.x[p->nLoaded^0x07] ^= aData[i];
1833 #else
1834     p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1835 #endif
1836     p->nLoaded++;
1837     if( p->nLoaded==p->nRate ){
1838       KeccakF1600Step(p);
1839       p->nLoaded = 0;
1840     }
1841   }
1842 }
1843
1844 /*
1845 ** After all content has been added, invoke SHA3Final() to compute
1846 ** the final hash.  The function returns a pointer to the binary
1847 ** hash value.
1848 */
1849 static unsigned char *SHA3Final(SHA3Context *p){
1850   unsigned int i;
1851   if( p->nLoaded==p->nRate-1 ){
1852     const unsigned char c1 = 0x86;
1853     SHA3Update(p, &c1, 1);
1854   }else{
1855     const unsigned char c2 = 0x06;
1856     const unsigned char c3 = 0x80;
1857     SHA3Update(p, &c2, 1);
1858     p->nLoaded = p->nRate - 1;
1859     SHA3Update(p, &c3, 1);
1860   }
1861   for(i=0; i<p->nRate; i++){
1862     p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1863   }
1864   return &p->u.x[p->nRate];
1865 }
1866 /* End of the hashing logic
1867 *****************************************************************************/
1868
1869 /*
1870 ** Implementation of the sha3(X,SIZE) function.
1871 **
1872 ** Return a BLOB which is the SIZE-bit SHA3 hash of X.  The default
1873 ** size is 256.  If X is a BLOB, it is hashed as is.  
1874 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1875 ** and the string is hashed without the trailing 0x00 terminator.  The hash
1876 ** of a NULL value is NULL.
1877 */
1878 static void sha3Func(
1879   sqlite3_context *context,
1880   int argc,
1881   sqlite3_value **argv
1882 ){
1883   SHA3Context cx;
1884   int eType = sqlite3_value_type(argv[0]);
1885   int nByte = sqlite3_value_bytes(argv[0]);
1886   int iSize;
1887   if( argc==1 ){
1888     iSize = 256;
1889   }else{
1890     iSize = sqlite3_value_int(argv[1]);
1891     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1892       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1893                                     "384 512", -1);
1894       return;
1895     }
1896   }
1897   if( eType==SQLITE_NULL ) return;
1898   SHA3Init(&cx, iSize);
1899   if( eType==SQLITE_BLOB ){
1900     SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1901   }else{
1902     SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1903   }
1904   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1905 }
1906
1907 /* Compute a string using sqlite3_vsnprintf() with a maximum length
1908 ** of 50 bytes and add it to the hash.
1909 */
1910 static void hash_step_vformat(
1911   SHA3Context *p,                 /* Add content to this context */
1912   const char *zFormat,
1913   ...
1914 ){
1915   va_list ap;
1916   int n;
1917   char zBuf[50];
1918   va_start(ap, zFormat);
1919   sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1920   va_end(ap);
1921   n = (int)strlen(zBuf);
1922   SHA3Update(p, (unsigned char*)zBuf, n);
1923 }
1924
1925 /*
1926 ** Implementation of the sha3_query(SQL,SIZE) function.
1927 **
1928 ** This function compiles and runs the SQL statement(s) given in the
1929 ** argument. The results are hashed using a SIZE-bit SHA3.  The default
1930 ** size is 256.
1931 **
1932 ** The format of the byte stream that is hashed is summarized as follows:
1933 **
1934 **       S<n>:<sql>
1935 **       R
1936 **       N
1937 **       I<int>
1938 **       F<ieee-float>
1939 **       B<size>:<bytes>
1940 **       T<size>:<text>
1941 **
1942 ** <sql> is the original SQL text for each statement run and <n> is
1943 ** the size of that text.  The SQL text is UTF-8.  A single R character
1944 ** occurs before the start of each row.  N means a NULL value.
1945 ** I mean an 8-byte little-endian integer <int>.  F is a floating point
1946 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1947 ** B means blobs of <size> bytes.  T means text rendered as <size>
1948 ** bytes of UTF-8.  The <n> and <size> values are expressed as an ASCII
1949 ** text integers.
1950 **
1951 ** For each SQL statement in the X input, there is one S segment.  Each
1952 ** S segment is followed by zero or more R segments, one for each row in the
1953 ** result set.  After each R, there are one or more N, I, F, B, or T segments,
1954 ** one for each column in the result set.  Segments are concatentated directly
1955 ** with no delimiters of any kind.
1956 */
1957 static void sha3QueryFunc(
1958   sqlite3_context *context,
1959   int argc,
1960   sqlite3_value **argv
1961 ){
1962   sqlite3 *db = sqlite3_context_db_handle(context);
1963   const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1964   sqlite3_stmt *pStmt = 0;
1965   int nCol;                   /* Number of columns in the result set */
1966   int i;                      /* Loop counter */
1967   int rc;
1968   int n;
1969   const char *z;
1970   SHA3Context cx;
1971   int iSize;
1972
1973   if( argc==1 ){
1974     iSize = 256;
1975   }else{
1976     iSize = sqlite3_value_int(argv[1]);
1977     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1978       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1979                                     "384 512", -1);
1980       return;
1981     }
1982   }
1983   if( zSql==0 ) return;
1984   SHA3Init(&cx, iSize);
1985   while( zSql[0] ){
1986     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
1987     if( rc ){
1988       char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
1989                                    zSql, sqlite3_errmsg(db));
1990       sqlite3_finalize(pStmt);
1991       sqlite3_result_error(context, zMsg, -1);
1992       sqlite3_free(zMsg);
1993       return;
1994     }
1995     if( !sqlite3_stmt_readonly(pStmt) ){
1996       char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
1997       sqlite3_finalize(pStmt);
1998       sqlite3_result_error(context, zMsg, -1);
1999       sqlite3_free(zMsg);
2000       return;
2001     }
2002     nCol = sqlite3_column_count(pStmt);
2003     z = sqlite3_sql(pStmt);
2004     n = (int)strlen(z);
2005     hash_step_vformat(&cx,"S%d:",n);
2006     SHA3Update(&cx,(unsigned char*)z,n);
2007
2008     /* Compute a hash over the result of the query */
2009     while( SQLITE_ROW==sqlite3_step(pStmt) ){
2010       SHA3Update(&cx,(const unsigned char*)"R",1);
2011       for(i=0; i<nCol; i++){
2012         switch( sqlite3_column_type(pStmt,i) ){
2013           case SQLITE_NULL: {
2014             SHA3Update(&cx, (const unsigned char*)"N",1);
2015             break;
2016           }
2017           case SQLITE_INTEGER: {
2018             sqlite3_uint64 u;
2019             int j;
2020             unsigned char x[9];
2021             sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
2022             memcpy(&u, &v, 8);
2023             for(j=8; j>=1; j--){
2024               x[j] = u & 0xff;
2025               u >>= 8;
2026             }
2027             x[0] = 'I';
2028             SHA3Update(&cx, x, 9);
2029             break;
2030           }
2031           case SQLITE_FLOAT: {
2032             sqlite3_uint64 u;
2033             int j;
2034             unsigned char x[9];
2035             double r = sqlite3_column_double(pStmt,i);
2036             memcpy(&u, &r, 8);
2037             for(j=8; j>=1; j--){
2038               x[j] = u & 0xff;
2039               u >>= 8;
2040             }
2041             x[0] = 'F';
2042             SHA3Update(&cx,x,9);
2043             break;
2044           }
2045           case SQLITE_TEXT: {
2046             int n2 = sqlite3_column_bytes(pStmt, i);
2047             const unsigned char *z2 = sqlite3_column_text(pStmt, i);
2048             hash_step_vformat(&cx,"T%d:",n2);
2049             SHA3Update(&cx, z2, n2);
2050             break;
2051           }
2052           case SQLITE_BLOB: {
2053             int n2 = sqlite3_column_bytes(pStmt, i);
2054             const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
2055             hash_step_vformat(&cx,"B%d:",n2);
2056             SHA3Update(&cx, z2, n2);
2057             break;
2058           }
2059         }
2060       }
2061     }
2062     sqlite3_finalize(pStmt);
2063   }
2064   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
2065 }
2066
2067
2068 #ifdef _WIN32
2069
2070 #endif
2071 int sqlite3_shathree_init(
2072   sqlite3 *db,
2073   char **pzErrMsg,
2074   const sqlite3_api_routines *pApi
2075 ){
2076   int rc = SQLITE_OK;
2077   SQLITE_EXTENSION_INIT2(pApi);
2078   (void)pzErrMsg;  /* Unused parameter */
2079   rc = sqlite3_create_function(db, "sha3", 1,
2080                       SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2081                       0, sha3Func, 0, 0);
2082   if( rc==SQLITE_OK ){
2083     rc = sqlite3_create_function(db, "sha3", 2,
2084                       SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2085                       0, sha3Func, 0, 0);
2086   }
2087   if( rc==SQLITE_OK ){
2088     rc = sqlite3_create_function(db, "sha3_query", 1,
2089                       SQLITE_UTF8 | SQLITE_DIRECTONLY,
2090                       0, sha3QueryFunc, 0, 0);
2091   }
2092   if( rc==SQLITE_OK ){
2093     rc = sqlite3_create_function(db, "sha3_query", 2,
2094                       SQLITE_UTF8 | SQLITE_DIRECTONLY,
2095                       0, sha3QueryFunc, 0, 0);
2096   }
2097   return rc;
2098 }
2099
2100 /************************* End ../ext/misc/shathree.c ********************/
2101 /************************* Begin ../ext/misc/fileio.c ******************/
2102 /*
2103 ** 2014-06-13
2104 **
2105 ** The author disclaims copyright to this source code.  In place of
2106 ** a legal notice, here is a blessing:
2107 **
2108 **    May you do good and not evil.
2109 **    May you find forgiveness for yourself and forgive others.
2110 **    May you share freely, never taking more than you give.
2111 **
2112 ******************************************************************************
2113 **
2114 ** This SQLite extension implements SQL functions readfile() and
2115 ** writefile(), and eponymous virtual type "fsdir".
2116 **
2117 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2118 **
2119 **   If neither of the optional arguments is present, then this UDF
2120 **   function writes blob DATA to file FILE. If successful, the number
2121 **   of bytes written is returned. If an error occurs, NULL is returned.
2122 **
2123 **   If the first option argument - MODE - is present, then it must
2124 **   be passed an integer value that corresponds to a POSIX mode
2125 **   value (file type + permissions, as returned in the stat.st_mode
2126 **   field by the stat() system call). Three types of files may
2127 **   be written/created:
2128 **
2129 **     regular files:  (mode & 0170000)==0100000
2130 **     symbolic links: (mode & 0170000)==0120000
2131 **     directories:    (mode & 0170000)==0040000
2132 **
2133 **   For a directory, the DATA is ignored. For a symbolic link, it is
2134 **   interpreted as text and used as the target of the link. For a
2135 **   regular file, it is interpreted as a blob and written into the
2136 **   named file. Regardless of the type of file, its permissions are
2137 **   set to (mode & 0777) before returning.
2138 **
2139 **   If the optional MTIME argument is present, then it is interpreted
2140 **   as an integer - the number of seconds since the unix epoch. The
2141 **   modification-time of the target file is set to this value before
2142 **   returning.
2143 **
2144 **   If three or more arguments are passed to this function and an
2145 **   error is encountered, an exception is raised.
2146 **
2147 ** READFILE(FILE):
2148 **
2149 **   Read and return the contents of file FILE (type blob) from disk.
2150 **
2151 ** FSDIR:
2152 **
2153 **   Used as follows:
2154 **
2155 **     SELECT * FROM fsdir($path [, $dir]);
2156 **
2157 **   Parameter $path is an absolute or relative pathname. If the file that it
2158 **   refers to does not exist, it is an error. If the path refers to a regular
2159 **   file or symbolic link, it returns a single row. Or, if the path refers
2160 **   to a directory, it returns one row for the directory, and one row for each
2161 **   file within the hierarchy rooted at $path.
2162 **
2163 **   Each row has the following columns:
2164 **
2165 **     name:  Path to file or directory (text value).
2166 **     mode:  Value of stat.st_mode for directory entry (an integer).
2167 **     mtime: Value of stat.st_mtime for directory entry (an integer).
2168 **     data:  For a regular file, a blob containing the file data. For a
2169 **            symlink, a text value containing the text of the link. For a
2170 **            directory, NULL.
2171 **
2172 **   If a non-NULL value is specified for the optional $dir parameter and
2173 **   $path is a relative path, then $path is interpreted relative to $dir. 
2174 **   And the paths returned in the "name" column of the table are also 
2175 **   relative to directory $dir.
2176 */
2177 /* #include "sqlite3ext.h" */
2178 SQLITE_EXTENSION_INIT1
2179 #include <stdio.h>
2180 #include <string.h>
2181 #include <assert.h>
2182
2183 #include <sys/types.h>
2184 #include <sys/stat.h>
2185 #include <fcntl.h>
2186 #if !defined(_WIN32) && !defined(WIN32)
2187 #  include <unistd.h>
2188 #  include <dirent.h>
2189 #  include <utime.h>
2190 #  include <sys/time.h>
2191 #else
2192 #  include "windows.h"
2193 #  include <io.h>
2194 #  include <direct.h>
2195 /* #  include "test_windirent.h" */
2196 #  define dirent DIRENT
2197 #  ifndef chmod
2198 #    define chmod _chmod
2199 #  endif
2200 #  ifndef stat
2201 #    define stat _stat
2202 #  endif
2203 #  define mkdir(path,mode) _mkdir(path)
2204 #  define lstat(path,buf) stat(path,buf)
2205 #endif
2206 #include <time.h>
2207 #include <errno.h>
2208
2209
2210 /*
2211 ** Structure of the fsdir() table-valued function
2212 */
2213                  /*    0    1    2     3    4           5             */
2214 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2215 #define FSDIR_COLUMN_NAME     0     /* Name of the file */
2216 #define FSDIR_COLUMN_MODE     1     /* Access mode */
2217 #define FSDIR_COLUMN_MTIME    2     /* Last modification time */
2218 #define FSDIR_COLUMN_DATA     3     /* File content */
2219 #define FSDIR_COLUMN_PATH     4     /* Path to top of search */
2220 #define FSDIR_COLUMN_DIR      5     /* Path is relative to this directory */
2221
2222
2223 /*
2224 ** Set the result stored by context ctx to a blob containing the 
2225 ** contents of file zName.  Or, leave the result unchanged (NULL)
2226 ** if the file does not exist or is unreadable.
2227 **
2228 ** If the file exceeds the SQLite blob size limit, through an
2229 ** SQLITE_TOOBIG error.
2230 **
2231 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
2232 ** off of disk.
2233 */
2234 static void readFileContents(sqlite3_context *ctx, const char *zName){
2235   FILE *in;
2236   sqlite3_int64 nIn;
2237   void *pBuf;
2238   sqlite3 *db;
2239   int mxBlob;
2240
2241   in = fopen(zName, "rb");
2242   if( in==0 ){
2243     /* File does not exist or is unreadable. Leave the result set to NULL. */
2244     return;
2245   }
2246   fseek(in, 0, SEEK_END);
2247   nIn = ftell(in);
2248   rewind(in);
2249   db = sqlite3_context_db_handle(ctx);
2250   mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
2251   if( nIn>mxBlob ){
2252     sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2253     fclose(in);
2254     return;
2255   }
2256   pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
2257   if( pBuf==0 ){
2258     sqlite3_result_error_nomem(ctx);
2259     fclose(in);
2260     return;
2261   }
2262   if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
2263     sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2264   }else{
2265     sqlite3_result_error_code(ctx, SQLITE_IOERR);
2266     sqlite3_free(pBuf);
2267   }
2268   fclose(in);
2269 }
2270
2271 /*
2272 ** Implementation of the "readfile(X)" SQL function.  The entire content
2273 ** of the file named X is read and returned as a BLOB.  NULL is returned
2274 ** if the file does not exist or is unreadable.
2275 */
2276 static void readfileFunc(
2277   sqlite3_context *context,
2278   int argc,
2279   sqlite3_value **argv
2280 ){
2281   const char *zName;
2282   (void)(argc);  /* Unused parameter */
2283   zName = (const char*)sqlite3_value_text(argv[0]);
2284   if( zName==0 ) return;
2285   readFileContents(context, zName);
2286 }
2287
2288 /*
2289 ** Set the error message contained in context ctx to the results of
2290 ** vprintf(zFmt, ...).
2291 */
2292 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2293   char *zMsg = 0;
2294   va_list ap;
2295   va_start(ap, zFmt);
2296   zMsg = sqlite3_vmprintf(zFmt, ap);
2297   sqlite3_result_error(ctx, zMsg, -1);
2298   sqlite3_free(zMsg);
2299   va_end(ap);
2300 }
2301
2302 #if defined(_WIN32)
2303 /*
2304 ** This function is designed to convert a Win32 FILETIME structure into the
2305 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2306 */
2307 static sqlite3_uint64 fileTimeToUnixTime(
2308   LPFILETIME pFileTime
2309 ){
2310   SYSTEMTIME epochSystemTime;
2311   ULARGE_INTEGER epochIntervals;
2312   FILETIME epochFileTime;
2313   ULARGE_INTEGER fileIntervals;
2314
2315   memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2316   epochSystemTime.wYear = 1970;
2317   epochSystemTime.wMonth = 1;
2318   epochSystemTime.wDay = 1;
2319   SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2320   epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2321   epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2322
2323   fileIntervals.LowPart = pFileTime->dwLowDateTime;
2324   fileIntervals.HighPart = pFileTime->dwHighDateTime;
2325
2326   return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2327 }
2328
2329 /*
2330 ** This function attempts to normalize the time values found in the stat()
2331 ** buffer to UTC.  This is necessary on Win32, where the runtime library
2332 ** appears to return these values as local times.
2333 */
2334 static void statTimesToUtc(
2335   const char *zPath,
2336   struct stat *pStatBuf
2337 ){
2338   HANDLE hFindFile;
2339   WIN32_FIND_DATAW fd;
2340   LPWSTR zUnicodeName;
2341   extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2342   zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2343   if( zUnicodeName ){
2344     memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2345     hFindFile = FindFirstFileW(zUnicodeName, &fd);
2346     if( hFindFile!=NULL ){
2347       pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2348       pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2349       pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2350       FindClose(hFindFile);
2351     }
2352     sqlite3_free(zUnicodeName);
2353   }
2354 }
2355 #endif
2356
2357 /*
2358 ** This function is used in place of stat().  On Windows, special handling
2359 ** is required in order for the included time to be returned as UTC.  On all
2360 ** other systems, this function simply calls stat().
2361 */
2362 static int fileStat(
2363   const char *zPath,
2364   struct stat *pStatBuf
2365 ){
2366 #if defined(_WIN32)
2367   int rc = stat(zPath, pStatBuf);
2368   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2369   return rc;
2370 #else
2371   return stat(zPath, pStatBuf);
2372 #endif
2373 }
2374
2375 /*
2376 ** This function is used in place of lstat().  On Windows, special handling
2377 ** is required in order for the included time to be returned as UTC.  On all
2378 ** other systems, this function simply calls lstat().
2379 */
2380 static int fileLinkStat(
2381   const char *zPath,
2382   struct stat *pStatBuf
2383 ){
2384 #if defined(_WIN32)
2385   int rc = lstat(zPath, pStatBuf);
2386   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2387   return rc;
2388 #else
2389   return lstat(zPath, pStatBuf);
2390 #endif
2391 }
2392
2393 /*
2394 ** Argument zFile is the name of a file that will be created and/or written
2395 ** by SQL function writefile(). This function ensures that the directory
2396 ** zFile will be written to exists, creating it if required. The permissions
2397 ** for any path components created by this function are set in accordance
2398 ** with the current umask.
2399 **
2400 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2401 ** SQLITE_OK is returned if the directory is successfully created, or
2402 ** SQLITE_ERROR otherwise.
2403 */
2404 static int makeDirectory(
2405   const char *zFile
2406 ){
2407   char *zCopy = sqlite3_mprintf("%s", zFile);
2408   int rc = SQLITE_OK;
2409
2410   if( zCopy==0 ){
2411     rc = SQLITE_NOMEM;
2412   }else{
2413     int nCopy = (int)strlen(zCopy);
2414     int i = 1;
2415
2416     while( rc==SQLITE_OK ){
2417       struct stat sStat;
2418       int rc2;
2419
2420       for(; zCopy[i]!='/' && i<nCopy; i++);
2421       if( i==nCopy ) break;
2422       zCopy[i] = '\0';
2423
2424       rc2 = fileStat(zCopy, &sStat);
2425       if( rc2!=0 ){
2426         if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2427       }else{
2428         if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2429       }
2430       zCopy[i] = '/';
2431       i++;
2432     }
2433
2434     sqlite3_free(zCopy);
2435   }
2436
2437   return rc;
2438 }
2439
2440 /*
2441 ** This function does the work for the writefile() UDF. Refer to 
2442 ** header comments at the top of this file for details.
2443 */
2444 static int writeFile(
2445   sqlite3_context *pCtx,          /* Context to return bytes written in */
2446   const char *zFile,              /* File to write */
2447   sqlite3_value *pData,           /* Data to write */
2448   mode_t mode,                    /* MODE parameter passed to writefile() */
2449   sqlite3_int64 mtime             /* MTIME parameter (or -1 to not set time) */
2450 ){
2451 #if !defined(_WIN32) && !defined(WIN32)
2452   if( S_ISLNK(mode) ){
2453     const char *zTo = (const char*)sqlite3_value_text(pData);
2454     if( symlink(zTo, zFile)<0 ) return 1;
2455   }else
2456 #endif
2457   {
2458     if( S_ISDIR(mode) ){
2459       if( mkdir(zFile, mode) ){
2460         /* The mkdir() call to create the directory failed. This might not
2461         ** be an error though - if there is already a directory at the same
2462         ** path and either the permissions already match or can be changed
2463         ** to do so using chmod(), it is not an error.  */
2464         struct stat sStat;
2465         if( errno!=EEXIST
2466          || 0!=fileStat(zFile, &sStat)
2467          || !S_ISDIR(sStat.st_mode)
2468          || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2469         ){
2470           return 1;
2471         }
2472       }
2473     }else{
2474       sqlite3_int64 nWrite = 0;
2475       const char *z;
2476       int rc = 0;
2477       FILE *out = fopen(zFile, "wb");
2478       if( out==0 ) return 1;
2479       z = (const char*)sqlite3_value_blob(pData);
2480       if( z ){
2481         sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2482         nWrite = sqlite3_value_bytes(pData);
2483         if( nWrite!=n ){
2484           rc = 1;
2485         }
2486       }
2487       fclose(out);
2488       if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2489         rc = 1;
2490       }
2491       if( rc ) return 2;
2492       sqlite3_result_int64(pCtx, nWrite);
2493     }
2494   }
2495
2496   if( mtime>=0 ){
2497 #if defined(_WIN32)
2498 #if !SQLITE_OS_WINRT
2499     /* Windows */
2500     FILETIME lastAccess;
2501     FILETIME lastWrite;
2502     SYSTEMTIME currentTime;
2503     LONGLONG intervals;
2504     HANDLE hFile;
2505     LPWSTR zUnicodeName;
2506     extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2507
2508     GetSystemTime(&currentTime);
2509     SystemTimeToFileTime(&currentTime, &lastAccess);
2510     intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2511     lastWrite.dwLowDateTime = (DWORD)intervals;
2512     lastWrite.dwHighDateTime = intervals >> 32;
2513     zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2514     if( zUnicodeName==0 ){
2515       return 1;
2516     }
2517     hFile = CreateFileW(
2518       zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2519       FILE_FLAG_BACKUP_SEMANTICS, NULL
2520     );
2521     sqlite3_free(zUnicodeName);
2522     if( hFile!=INVALID_HANDLE_VALUE ){
2523       BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2524       CloseHandle(hFile);
2525       return !bResult;
2526     }else{
2527       return 1;
2528     }
2529 #endif
2530 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2531     /* Recent unix */
2532     struct timespec times[2];
2533     times[0].tv_nsec = times[1].tv_nsec = 0;
2534     times[0].tv_sec = time(0);
2535     times[1].tv_sec = mtime;
2536     if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2537       return 1;
2538     }
2539 #else
2540     /* Legacy unix */
2541     struct timeval times[2];
2542     times[0].tv_usec = times[1].tv_usec = 0;
2543     times[0].tv_sec = time(0);
2544     times[1].tv_sec = mtime;
2545     if( utimes(zFile, times) ){
2546       return 1;
2547     }
2548 #endif
2549   }
2550
2551   return 0;
2552 }
2553
2554 /*
2555 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.  
2556 ** Refer to header comments at the top of this file for details.
2557 */
2558 static void writefileFunc(
2559   sqlite3_context *context,
2560   int argc,
2561   sqlite3_value **argv
2562 ){
2563   const char *zFile;
2564   mode_t mode = 0;
2565   int res;
2566   sqlite3_int64 mtime = -1;
2567
2568   if( argc<2 || argc>4 ){
2569     sqlite3_result_error(context, 
2570         "wrong number of arguments to function writefile()", -1
2571     );
2572     return;
2573   }
2574
2575   zFile = (const char*)sqlite3_value_text(argv[0]);
2576   if( zFile==0 ) return;
2577   if( argc>=3 ){
2578     mode = (mode_t)sqlite3_value_int(argv[2]);
2579   }
2580   if( argc==4 ){
2581     mtime = sqlite3_value_int64(argv[3]);
2582   }
2583
2584   res = writeFile(context, zFile, argv[1], mode, mtime);
2585   if( res==1 && errno==ENOENT ){
2586     if( makeDirectory(zFile)==SQLITE_OK ){
2587       res = writeFile(context, zFile, argv[1], mode, mtime);
2588     }
2589   }
2590
2591   if( argc>2 && res!=0 ){
2592     if( S_ISLNK(mode) ){
2593       ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2594     }else if( S_ISDIR(mode) ){
2595       ctxErrorMsg(context, "failed to create directory: %s", zFile);
2596     }else{
2597       ctxErrorMsg(context, "failed to write file: %s", zFile);
2598     }
2599   }
2600 }
2601
2602 /*
2603 ** SQL function:   lsmode(MODE)
2604 **
2605 ** Given a numberic st_mode from stat(), convert it into a human-readable
2606 ** text string in the style of "ls -l".
2607 */
2608 static void lsModeFunc(
2609   sqlite3_context *context,
2610   int argc,
2611   sqlite3_value **argv
2612 ){
2613   int i;
2614   int iMode = sqlite3_value_int(argv[0]);
2615   char z[16];
2616   (void)argc;
2617   if( S_ISLNK(iMode) ){
2618     z[0] = 'l';
2619   }else if( S_ISREG(iMode) ){
2620     z[0] = '-';
2621   }else if( S_ISDIR(iMode) ){
2622     z[0] = 'd';
2623   }else{
2624     z[0] = '?';
2625   }
2626   for(i=0; i<3; i++){
2627     int m = (iMode >> ((2-i)*3));
2628     char *a = &z[1 + i*3];
2629     a[0] = (m & 0x4) ? 'r' : '-';
2630     a[1] = (m & 0x2) ? 'w' : '-';
2631     a[2] = (m & 0x1) ? 'x' : '-';
2632   }
2633   z[10] = '\0';
2634   sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2635 }
2636
2637 #ifndef SQLITE_OMIT_VIRTUALTABLE
2638
2639 /* 
2640 ** Cursor type for recursively iterating through a directory structure.
2641 */
2642 typedef struct fsdir_cursor fsdir_cursor;
2643 typedef struct FsdirLevel FsdirLevel;
2644
2645 struct FsdirLevel {
2646   DIR *pDir;                 /* From opendir() */
2647   char *zDir;                /* Name of directory (nul-terminated) */
2648 };
2649
2650 struct fsdir_cursor {
2651   sqlite3_vtab_cursor base;  /* Base class - must be first */
2652
2653   int nLvl;                  /* Number of entries in aLvl[] array */
2654   int iLvl;                  /* Index of current entry */
2655   FsdirLevel *aLvl;          /* Hierarchy of directories being traversed */
2656
2657   const char *zBase;
2658   int nBase;
2659
2660   struct stat sStat;         /* Current lstat() results */
2661   char *zPath;               /* Path to current entry */
2662   sqlite3_int64 iRowid;      /* Current rowid */
2663 };
2664
2665 typedef struct fsdir_tab fsdir_tab;
2666 struct fsdir_tab {
2667   sqlite3_vtab base;         /* Base class - must be first */
2668 };
2669
2670 /*
2671 ** Construct a new fsdir virtual table object.
2672 */
2673 static int fsdirConnect(
2674   sqlite3 *db,
2675   void *pAux,
2676   int argc, const char *const*argv,
2677   sqlite3_vtab **ppVtab,
2678   char **pzErr
2679 ){
2680   fsdir_tab *pNew = 0;
2681   int rc;
2682   (void)pAux;
2683   (void)argc;
2684   (void)argv;
2685   (void)pzErr;
2686   rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2687   if( rc==SQLITE_OK ){
2688     pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2689     if( pNew==0 ) return SQLITE_NOMEM;
2690     memset(pNew, 0, sizeof(*pNew));
2691     sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
2692   }
2693   *ppVtab = (sqlite3_vtab*)pNew;
2694   return rc;
2695 }
2696
2697 /*
2698 ** This method is the destructor for fsdir vtab objects.
2699 */
2700 static int fsdirDisconnect(sqlite3_vtab *pVtab){
2701   sqlite3_free(pVtab);
2702   return SQLITE_OK;
2703 }
2704
2705 /*
2706 ** Constructor for a new fsdir_cursor object.
2707 */
2708 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2709   fsdir_cursor *pCur;
2710   (void)p;
2711   pCur = sqlite3_malloc( sizeof(*pCur) );
2712   if( pCur==0 ) return SQLITE_NOMEM;
2713   memset(pCur, 0, sizeof(*pCur));
2714   pCur->iLvl = -1;
2715   *ppCursor = &pCur->base;
2716   return SQLITE_OK;
2717 }
2718
2719 /*
2720 ** Reset a cursor back to the state it was in when first returned
2721 ** by fsdirOpen().
2722 */
2723 static void fsdirResetCursor(fsdir_cursor *pCur){
2724   int i;
2725   for(i=0; i<=pCur->iLvl; i++){
2726     FsdirLevel *pLvl = &pCur->aLvl[i];
2727     if( pLvl->pDir ) closedir(pLvl->pDir);
2728     sqlite3_free(pLvl->zDir);
2729   }
2730   sqlite3_free(pCur->zPath);
2731   sqlite3_free(pCur->aLvl);
2732   pCur->aLvl = 0;
2733   pCur->zPath = 0;
2734   pCur->zBase = 0;
2735   pCur->nBase = 0;
2736   pCur->nLvl = 0;
2737   pCur->iLvl = -1;
2738   pCur->iRowid = 1;
2739 }
2740
2741 /*
2742 ** Destructor for an fsdir_cursor.
2743 */
2744 static int fsdirClose(sqlite3_vtab_cursor *cur){
2745   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2746
2747   fsdirResetCursor(pCur);
2748   sqlite3_free(pCur);
2749   return SQLITE_OK;
2750 }
2751
2752 /*
2753 ** Set the error message for the virtual table associated with cursor
2754 ** pCur to the results of vprintf(zFmt, ...).
2755 */
2756 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2757   va_list ap;
2758   va_start(ap, zFmt);
2759   pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2760   va_end(ap);
2761 }
2762
2763
2764 /*
2765 ** Advance an fsdir_cursor to its next row of output.
2766 */
2767 static int fsdirNext(sqlite3_vtab_cursor *cur){
2768   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2769   mode_t m = pCur->sStat.st_mode;
2770
2771   pCur->iRowid++;
2772   if( S_ISDIR(m) ){
2773     /* Descend into this directory */
2774     int iNew = pCur->iLvl + 1;
2775     FsdirLevel *pLvl;
2776     if( iNew>=pCur->nLvl ){
2777       int nNew = iNew+1;
2778       sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
2779       FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
2780       if( aNew==0 ) return SQLITE_NOMEM;
2781       memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2782       pCur->aLvl = aNew;
2783       pCur->nLvl = nNew;
2784     }
2785     pCur->iLvl = iNew;
2786     pLvl = &pCur->aLvl[iNew];
2787     
2788     pLvl->zDir = pCur->zPath;
2789     pCur->zPath = 0;
2790     pLvl->pDir = opendir(pLvl->zDir);
2791     if( pLvl->pDir==0 ){
2792       fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2793       return SQLITE_ERROR;
2794     }
2795   }
2796
2797   while( pCur->iLvl>=0 ){
2798     FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2799     struct dirent *pEntry = readdir(pLvl->pDir);
2800     if( pEntry ){
2801       if( pEntry->d_name[0]=='.' ){
2802        if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2803        if( pEntry->d_name[1]=='\0' ) continue;
2804       }
2805       sqlite3_free(pCur->zPath);
2806       pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2807       if( pCur->zPath==0 ) return SQLITE_NOMEM;
2808       if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2809         fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2810         return SQLITE_ERROR;
2811       }
2812       return SQLITE_OK;
2813     }
2814     closedir(pLvl->pDir);
2815     sqlite3_free(pLvl->zDir);
2816     pLvl->pDir = 0;
2817     pLvl->zDir = 0;
2818     pCur->iLvl--;
2819   }
2820
2821   /* EOF */
2822   sqlite3_free(pCur->zPath);
2823   pCur->zPath = 0;
2824   return SQLITE_OK;
2825 }
2826
2827 /*
2828 ** Return values of columns for the row at which the series_cursor
2829 ** is currently pointing.
2830 */
2831 static int fsdirColumn(
2832   sqlite3_vtab_cursor *cur,   /* The cursor */
2833   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
2834   int i                       /* Which column to return */
2835 ){
2836   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2837   switch( i ){
2838     case FSDIR_COLUMN_NAME: {
2839       sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2840       break;
2841     }
2842
2843     case FSDIR_COLUMN_MODE:
2844       sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2845       break;
2846
2847     case FSDIR_COLUMN_MTIME:
2848       sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2849       break;
2850
2851     case FSDIR_COLUMN_DATA: {
2852       mode_t m = pCur->sStat.st_mode;
2853       if( S_ISDIR(m) ){
2854         sqlite3_result_null(ctx);
2855 #if !defined(_WIN32) && !defined(WIN32)
2856       }else if( S_ISLNK(m) ){
2857         char aStatic[64];
2858         char *aBuf = aStatic;
2859         sqlite3_int64 nBuf = 64;
2860         int n;
2861
2862         while( 1 ){
2863           n = readlink(pCur->zPath, aBuf, nBuf);
2864           if( n<nBuf ) break;
2865           if( aBuf!=aStatic ) sqlite3_free(aBuf);
2866           nBuf = nBuf*2;
2867           aBuf = sqlite3_malloc64(nBuf);
2868           if( aBuf==0 ){
2869             sqlite3_result_error_nomem(ctx);
2870             return SQLITE_NOMEM;
2871           }
2872         }
2873
2874         sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2875         if( aBuf!=aStatic ) sqlite3_free(aBuf);
2876 #endif
2877       }else{
2878         readFileContents(ctx, pCur->zPath);
2879       }
2880     }
2881     case FSDIR_COLUMN_PATH:
2882     default: {
2883       /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2884       ** always return their values as NULL */
2885       break;
2886     }
2887   }
2888   return SQLITE_OK;
2889 }
2890
2891 /*
2892 ** Return the rowid for the current row. In this implementation, the
2893 ** first row returned is assigned rowid value 1, and each subsequent
2894 ** row a value 1 more than that of the previous.
2895 */
2896 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2897   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2898   *pRowid = pCur->iRowid;
2899   return SQLITE_OK;
2900 }
2901
2902 /*
2903 ** Return TRUE if the cursor has been moved off of the last
2904 ** row of output.
2905 */
2906 static int fsdirEof(sqlite3_vtab_cursor *cur){
2907   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2908   return (pCur->zPath==0);
2909 }
2910
2911 /*
2912 ** xFilter callback.
2913 **
2914 ** idxNum==1   PATH parameter only
2915 ** idxNum==2   Both PATH and DIR supplied
2916 */
2917 static int fsdirFilter(
2918   sqlite3_vtab_cursor *cur, 
2919   int idxNum, const char *idxStr,
2920   int argc, sqlite3_value **argv
2921 ){
2922   const char *zDir = 0;
2923   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2924   (void)idxStr;
2925   fsdirResetCursor(pCur);
2926
2927   if( idxNum==0 ){
2928     fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2929     return SQLITE_ERROR;
2930   }
2931
2932   assert( argc==idxNum && (argc==1 || argc==2) );
2933   zDir = (const char*)sqlite3_value_text(argv[0]);
2934   if( zDir==0 ){
2935     fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2936     return SQLITE_ERROR;
2937   }
2938   if( argc==2 ){
2939     pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2940   }
2941   if( pCur->zBase ){
2942     pCur->nBase = (int)strlen(pCur->zBase)+1;
2943     pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2944   }else{
2945     pCur->zPath = sqlite3_mprintf("%s", zDir);
2946   }
2947
2948   if( pCur->zPath==0 ){
2949     return SQLITE_NOMEM;
2950   }
2951   if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2952     fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2953     return SQLITE_ERROR;
2954   }
2955
2956   return SQLITE_OK;
2957 }
2958
2959 /*
2960 ** SQLite will invoke this method one or more times while planning a query
2961 ** that uses the generate_series virtual table.  This routine needs to create
2962 ** a query plan for each invocation and compute an estimated cost for that
2963 ** plan.
2964 **
2965 ** In this implementation idxNum is used to represent the
2966 ** query plan.  idxStr is unused.
2967 **
2968 ** The query plan is represented by values of idxNum:
2969 **
2970 **  (1)  The path value is supplied by argv[0]
2971 **  (2)  Path is in argv[0] and dir is in argv[1]
2972 */
2973 static int fsdirBestIndex(
2974   sqlite3_vtab *tab,
2975   sqlite3_index_info *pIdxInfo
2976 ){
2977   int i;                 /* Loop over constraints */
2978   int idxPath = -1;      /* Index in pIdxInfo->aConstraint of PATH= */
2979   int idxDir = -1;       /* Index in pIdxInfo->aConstraint of DIR= */
2980   int seenPath = 0;      /* True if an unusable PATH= constraint is seen */
2981   int seenDir = 0;       /* True if an unusable DIR= constraint is seen */
2982   const struct sqlite3_index_constraint *pConstraint;
2983
2984   (void)tab;
2985   pConstraint = pIdxInfo->aConstraint;
2986   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2987     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2988     switch( pConstraint->iColumn ){
2989       case FSDIR_COLUMN_PATH: {
2990         if( pConstraint->usable ){
2991           idxPath = i;
2992           seenPath = 0;
2993         }else if( idxPath<0 ){
2994           seenPath = 1;
2995         }
2996         break;
2997       }
2998       case FSDIR_COLUMN_DIR: {
2999         if( pConstraint->usable ){
3000           idxDir = i;
3001           seenDir = 0;
3002         }else if( idxDir<0 ){
3003           seenDir = 1;
3004         }
3005         break;
3006       }
3007     } 
3008   }
3009   if( seenPath || seenDir ){
3010     /* If input parameters are unusable, disallow this plan */
3011     return SQLITE_CONSTRAINT;
3012   }
3013
3014   if( idxPath<0 ){
3015     pIdxInfo->idxNum = 0;
3016     /* The pIdxInfo->estimatedCost should have been initialized to a huge
3017     ** number.  Leave it unchanged. */
3018     pIdxInfo->estimatedRows = 0x7fffffff;
3019   }else{
3020     pIdxInfo->aConstraintUsage[idxPath].omit = 1;
3021     pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
3022     if( idxDir>=0 ){
3023       pIdxInfo->aConstraintUsage[idxDir].omit = 1;
3024       pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
3025       pIdxInfo->idxNum = 2;
3026       pIdxInfo->estimatedCost = 10.0;
3027     }else{
3028       pIdxInfo->idxNum = 1;
3029       pIdxInfo->estimatedCost = 100.0;
3030     }
3031   }
3032
3033   return SQLITE_OK;
3034 }
3035
3036 /*
3037 ** Register the "fsdir" virtual table.
3038 */
3039 static int fsdirRegister(sqlite3 *db){
3040   static sqlite3_module fsdirModule = {
3041     0,                         /* iVersion */
3042     0,                         /* xCreate */
3043     fsdirConnect,              /* xConnect */
3044     fsdirBestIndex,            /* xBestIndex */
3045     fsdirDisconnect,           /* xDisconnect */
3046     0,                         /* xDestroy */
3047     fsdirOpen,                 /* xOpen - open a cursor */
3048     fsdirClose,                /* xClose - close a cursor */
3049     fsdirFilter,               /* xFilter - configure scan constraints */
3050     fsdirNext,                 /* xNext - advance a cursor */
3051     fsdirEof,                  /* xEof - check for end of scan */
3052     fsdirColumn,               /* xColumn - read data */
3053     fsdirRowid,                /* xRowid - read data */
3054     0,                         /* xUpdate */
3055     0,                         /* xBegin */
3056     0,                         /* xSync */
3057     0,                         /* xCommit */
3058     0,                         /* xRollback */
3059     0,                         /* xFindMethod */
3060     0,                         /* xRename */
3061     0,                         /* xSavepoint */
3062     0,                         /* xRelease */
3063     0,                         /* xRollbackTo */
3064     0,                         /* xShadowName */
3065   };
3066
3067   int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
3068   return rc;
3069 }
3070 #else         /* SQLITE_OMIT_VIRTUALTABLE */
3071 # define fsdirRegister(x) SQLITE_OK
3072 #endif
3073
3074 #ifdef _WIN32
3075
3076 #endif
3077 int sqlite3_fileio_init(
3078   sqlite3 *db, 
3079   char **pzErrMsg, 
3080   const sqlite3_api_routines *pApi
3081 ){
3082   int rc = SQLITE_OK;
3083   SQLITE_EXTENSION_INIT2(pApi);
3084   (void)pzErrMsg;  /* Unused parameter */
3085   rc = sqlite3_create_function(db, "readfile", 1, 
3086                                SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3087                                readfileFunc, 0, 0);
3088   if( rc==SQLITE_OK ){
3089     rc = sqlite3_create_function(db, "writefile", -1,
3090                                  SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3091                                  writefileFunc, 0, 0);
3092   }
3093   if( rc==SQLITE_OK ){
3094     rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
3095                                  lsModeFunc, 0, 0);
3096   }
3097   if( rc==SQLITE_OK ){
3098     rc = fsdirRegister(db);
3099   }
3100   return rc;
3101 }
3102
3103 /************************* End ../ext/misc/fileio.c ********************/
3104 /************************* Begin ../ext/misc/completion.c ******************/
3105 /*
3106 ** 2017-07-10
3107 **
3108 ** The author disclaims copyright to this source code.  In place of
3109 ** a legal notice, here is a blessing:
3110 **
3111 **    May you do good and not evil.
3112 **    May you find forgiveness for yourself and forgive others.
3113 **    May you share freely, never taking more than you give.
3114 **
3115 *************************************************************************
3116 **
3117 ** This file implements an eponymous virtual table that returns suggested
3118 ** completions for a partial SQL input.
3119 **
3120 ** Suggested usage:
3121 **
3122 **     SELECT DISTINCT candidate COLLATE nocase
3123 **       FROM completion($prefix,$wholeline)
3124 **      ORDER BY 1;
3125 **
3126 ** The two query parameters are optional.  $prefix is the text of the
3127 ** current word being typed and that is to be completed.  $wholeline is
3128 ** the complete input line, used for context.
3129 **
3130 ** The raw completion() table might return the same candidate multiple
3131 ** times, for example if the same column name is used to two or more
3132 ** tables.  And the candidates are returned in an arbitrary order.  Hence,
3133 ** the DISTINCT and ORDER BY are recommended.
3134 **
3135 ** This virtual table operates at the speed of human typing, and so there
3136 ** is no attempt to make it fast.  Even a slow implementation will be much
3137 ** faster than any human can type.
3138 **
3139 */
3140 /* #include "sqlite3ext.h" */
3141 SQLITE_EXTENSION_INIT1
3142 #include <assert.h>
3143 #include <string.h>
3144 #include <ctype.h>
3145
3146 #ifndef SQLITE_OMIT_VIRTUALTABLE
3147
3148 /* completion_vtab is a subclass of sqlite3_vtab which will
3149 ** serve as the underlying representation of a completion virtual table
3150 */
3151 typedef struct completion_vtab completion_vtab;
3152 struct completion_vtab {
3153   sqlite3_vtab base;  /* Base class - must be first */
3154   sqlite3 *db;        /* Database connection for this completion vtab */
3155 };
3156
3157 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3158 ** serve as the underlying representation of a cursor that scans
3159 ** over rows of the result
3160 */
3161 typedef struct completion_cursor completion_cursor;
3162 struct completion_cursor {
3163   sqlite3_vtab_cursor base;  /* Base class - must be first */
3164   sqlite3 *db;               /* Database connection for this cursor */
3165   int nPrefix, nLine;        /* Number of bytes in zPrefix and zLine */
3166   char *zPrefix;             /* The prefix for the word we want to complete */
3167   char *zLine;               /* The whole that we want to complete */
3168   const char *zCurrentRow;   /* Current output row */
3169   int szRow;                 /* Length of the zCurrentRow string */
3170   sqlite3_stmt *pStmt;       /* Current statement */
3171   sqlite3_int64 iRowid;      /* The rowid */
3172   int ePhase;                /* Current phase */
3173   int j;                     /* inter-phase counter */
3174 };
3175
3176 /* Values for ePhase:
3177 */
3178 #define COMPLETION_FIRST_PHASE   1
3179 #define COMPLETION_KEYWORDS      1
3180 #define COMPLETION_PRAGMAS       2
3181 #define COMPLETION_FUNCTIONS     3
3182 #define COMPLETION_COLLATIONS    4
3183 #define COMPLETION_INDEXES       5
3184 #define COMPLETION_TRIGGERS      6
3185 #define COMPLETION_DATABASES     7
3186 #define COMPLETION_TABLES        8    /* Also VIEWs and TRIGGERs */
3187 #define COMPLETION_COLUMNS       9
3188 #define COMPLETION_MODULES       10
3189 #define COMPLETION_EOF           11
3190
3191 /*
3192 ** The completionConnect() method is invoked to create a new
3193 ** completion_vtab that describes the completion virtual table.
3194 **
3195 ** Think of this routine as the constructor for completion_vtab objects.
3196 **
3197 ** All this routine needs to do is:
3198 **
3199 **    (1) Allocate the completion_vtab object and initialize all fields.
3200 **
3201 **    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3202 **        result set of queries against completion will look like.
3203 */
3204 static int completionConnect(
3205   sqlite3 *db,
3206   void *pAux,
3207   int argc, const char *const*argv,
3208   sqlite3_vtab **ppVtab,
3209   char **pzErr
3210 ){
3211   completion_vtab *pNew;
3212   int rc;
3213
3214   (void)(pAux);    /* Unused parameter */
3215   (void)(argc);    /* Unused parameter */
3216   (void)(argv);    /* Unused parameter */
3217   (void)(pzErr);   /* Unused parameter */
3218
3219 /* Column numbers */
3220 #define COMPLETION_COLUMN_CANDIDATE 0  /* Suggested completion of the input */
3221 #define COMPLETION_COLUMN_PREFIX    1  /* Prefix of the word to be completed */
3222 #define COMPLETION_COLUMN_WHOLELINE 2  /* Entire line seen so far */
3223 #define COMPLETION_COLUMN_PHASE     3  /* ePhase - used for debugging only */
3224
3225   sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
3226   rc = sqlite3_declare_vtab(db,
3227       "CREATE TABLE x("
3228       "  candidate TEXT,"
3229       "  prefix TEXT HIDDEN,"
3230       "  wholeline TEXT HIDDEN,"
3231       "  phase INT HIDDEN"        /* Used for debugging only */
3232       ")");
3233   if( rc==SQLITE_OK ){
3234     pNew = sqlite3_malloc( sizeof(*pNew) );
3235     *ppVtab = (sqlite3_vtab*)pNew;
3236     if( pNew==0 ) return SQLITE_NOMEM;
3237     memset(pNew, 0, sizeof(*pNew));
3238     pNew->db = db;
3239   }
3240   return rc;
3241 }
3242
3243 /*
3244 ** This method is the destructor for completion_cursor objects.
3245 */
3246 static int completionDisconnect(sqlite3_vtab *pVtab){
3247   sqlite3_free(pVtab);
3248   return SQLITE_OK;
3249 }
3250
3251 /*
3252 ** Constructor for a new completion_cursor object.
3253 */
3254 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3255   completion_cursor *pCur;
3256   pCur = sqlite3_malloc( sizeof(*pCur) );
3257   if( pCur==0 ) return SQLITE_NOMEM;
3258   memset(pCur, 0, sizeof(*pCur));
3259   pCur->db = ((completion_vtab*)p)->db;
3260   *ppCursor = &pCur->base;
3261   return SQLITE_OK;
3262 }
3263
3264 /*
3265 ** Reset the completion_cursor.
3266 */
3267 static void completionCursorReset(completion_cursor *pCur){
3268   sqlite3_free(pCur->zPrefix);   pCur->zPrefix = 0;  pCur->nPrefix = 0;
3269   sqlite3_free(pCur->zLine);     pCur->zLine = 0;    pCur->nLine = 0;
3270   sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3271   pCur->j = 0;
3272 }
3273
3274 /*
3275 ** Destructor for a completion_cursor.
3276 */
3277 static int completionClose(sqlite3_vtab_cursor *cur){
3278   completionCursorReset((completion_cursor*)cur);
3279   sqlite3_free(cur);
3280   return SQLITE_OK;
3281 }
3282
3283 /*
3284 ** Advance a completion_cursor to its next row of output.
3285 **
3286 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3287 ** record the current state of the scan.  This routine sets ->zCurrentRow
3288 ** to the current row of output and then returns.  If no more rows remain,
3289 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3290 ** table that has reached the end of its scan.
3291 **
3292 ** The current implementation just lists potential identifiers and
3293 ** keywords and filters them by zPrefix.  Future enhancements should
3294 ** take zLine into account to try to restrict the set of identifiers and
3295 ** keywords based on what would be legal at the current point of input.
3296 */
3297 static int completionNext(sqlite3_vtab_cursor *cur){
3298   completion_cursor *pCur = (completion_cursor*)cur;
3299   int eNextPhase = 0;  /* Next phase to try if current phase reaches end */
3300   int iCol = -1;       /* If >=0, step pCur->pStmt and use the i-th column */
3301   pCur->iRowid++;
3302   while( pCur->ePhase!=COMPLETION_EOF ){
3303     switch( pCur->ePhase ){
3304       case COMPLETION_KEYWORDS: {
3305         if( pCur->j >= sqlite3_keyword_count() ){
3306           pCur->zCurrentRow = 0;
3307           pCur->ePhase = COMPLETION_DATABASES;
3308         }else{
3309           sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3310         }
3311         iCol = -1;
3312         break;
3313       }
3314       case COMPLETION_DATABASES: {
3315         if( pCur->pStmt==0 ){
3316           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3317                              &pCur->pStmt, 0);
3318         }
3319         iCol = 1;
3320         eNextPhase = COMPLETION_TABLES;
3321         break;
3322       }
3323       case COMPLETION_TABLES: {
3324         if( pCur->pStmt==0 ){
3325           sqlite3_stmt *pS2;
3326           char *zSql = 0;
3327           const char *zSep = "";
3328           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3329           while( sqlite3_step(pS2)==SQLITE_ROW ){
3330             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3331             zSql = sqlite3_mprintf(
3332                "%z%s"
3333                "SELECT name FROM \"%w\".sqlite_master",
3334                zSql, zSep, zDb
3335             );
3336             if( zSql==0 ) return SQLITE_NOMEM;
3337             zSep = " UNION ";
3338           }
3339           sqlite3_finalize(pS2);
3340           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3341           sqlite3_free(zSql);
3342         }
3343         iCol = 0;
3344         eNextPhase = COMPLETION_COLUMNS;
3345         break;
3346       }
3347       case COMPLETION_COLUMNS: {
3348         if( pCur->pStmt==0 ){
3349           sqlite3_stmt *pS2;
3350           char *zSql = 0;
3351           const char *zSep = "";
3352           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3353           while( sqlite3_step(pS2)==SQLITE_ROW ){
3354             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3355             zSql = sqlite3_mprintf(
3356                "%z%s"
3357                "SELECT pti.name FROM \"%w\".sqlite_master AS sm"
3358                        " JOIN pragma_table_info(sm.name,%Q) AS pti"
3359                " WHERE sm.type='table'",
3360                zSql, zSep, zDb, zDb
3361             );
3362             if( zSql==0 ) return SQLITE_NOMEM;
3363             zSep = " UNION ";
3364           }
3365           sqlite3_finalize(pS2);
3366           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3367           sqlite3_free(zSql);
3368         }
3369         iCol = 0;
3370         eNextPhase = COMPLETION_EOF;
3371         break;
3372       }
3373     }
3374     if( iCol<0 ){
3375       /* This case is when the phase presets zCurrentRow */
3376       if( pCur->zCurrentRow==0 ) continue;
3377     }else{
3378       if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3379         /* Extract the next row of content */
3380         pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3381         pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3382       }else{
3383         /* When all rows are finished, advance to the next phase */
3384         sqlite3_finalize(pCur->pStmt);
3385         pCur->pStmt = 0;
3386         pCur->ePhase = eNextPhase;
3387         continue;
3388       }
3389     }
3390     if( pCur->nPrefix==0 ) break;
3391     if( pCur->nPrefix<=pCur->szRow
3392      && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3393     ){
3394       break;
3395     }
3396   }
3397
3398   return SQLITE_OK;
3399 }
3400
3401 /*
3402 ** Return values of columns for the row at which the completion_cursor
3403 ** is currently pointing.
3404 */
3405 static int completionColumn(
3406   sqlite3_vtab_cursor *cur,   /* The cursor */
3407   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
3408   int i                       /* Which column to return */
3409 ){
3410   completion_cursor *pCur = (completion_cursor*)cur;
3411   switch( i ){
3412     case COMPLETION_COLUMN_CANDIDATE: {
3413       sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3414       break;
3415     }
3416     case COMPLETION_COLUMN_PREFIX: {
3417       sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3418       break;
3419     }
3420     case COMPLETION_COLUMN_WHOLELINE: {
3421       sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3422       break;
3423     }
3424     case COMPLETION_COLUMN_PHASE: {
3425       sqlite3_result_int(ctx, pCur->ePhase);
3426       break;
3427     }
3428   }
3429   return SQLITE_OK;
3430 }
3431
3432 /*
3433 ** Return the rowid for the current row.  In this implementation, the
3434 ** rowid is the same as the output value.
3435 */
3436 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3437   completion_cursor *pCur = (completion_cursor*)cur;
3438   *pRowid = pCur->iRowid;
3439   return SQLITE_OK;
3440 }
3441
3442 /*
3443 ** Return TRUE if the cursor has been moved off of the last
3444 ** row of output.
3445 */
3446 static int completionEof(sqlite3_vtab_cursor *cur){
3447   completion_cursor *pCur = (completion_cursor*)cur;
3448   return pCur->ePhase >= COMPLETION_EOF;
3449 }
3450
3451 /*
3452 ** This method is called to "rewind" the completion_cursor object back
3453 ** to the first row of output.  This method is always called at least
3454 ** once prior to any call to completionColumn() or completionRowid() or 
3455 ** completionEof().
3456 */
3457 static int completionFilter(
3458   sqlite3_vtab_cursor *pVtabCursor, 
3459   int idxNum, const char *idxStr,
3460   int argc, sqlite3_value **argv
3461 ){
3462   completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3463   int iArg = 0;
3464   (void)(idxStr);   /* Unused parameter */
3465   (void)(argc);     /* Unused parameter */
3466   completionCursorReset(pCur);
3467   if( idxNum & 1 ){
3468     pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3469     if( pCur->nPrefix>0 ){
3470       pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3471       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3472     }
3473     iArg = 1;
3474   }
3475   if( idxNum & 2 ){
3476     pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3477     if( pCur->nLine>0 ){
3478       pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3479       if( pCur->zLine==0 ) return SQLITE_NOMEM;
3480     }
3481   }
3482   if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3483     int i = pCur->nLine;
3484     while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3485       i--;
3486     }
3487     pCur->nPrefix = pCur->nLine - i;
3488     if( pCur->nPrefix>0 ){
3489       pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3490       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3491     }
3492   }
3493   pCur->iRowid = 0;
3494   pCur->ePhase = COMPLETION_FIRST_PHASE;
3495   return completionNext(pVtabCursor);
3496 }
3497
3498 /*
3499 ** SQLite will invoke this method one or more times while planning a query
3500 ** that uses the completion virtual table.  This routine needs to create
3501 ** a query plan for each invocation and compute an estimated cost for that
3502 ** plan.
3503 **
3504 ** There are two hidden parameters that act as arguments to the table-valued
3505 ** function:  "prefix" and "wholeline".  Bit 0 of idxNum is set if "prefix"
3506 ** is available and bit 1 is set if "wholeline" is available.
3507 */
3508 static int completionBestIndex(
3509   sqlite3_vtab *tab,
3510   sqlite3_index_info *pIdxInfo
3511 ){
3512   int i;                 /* Loop over constraints */
3513   int idxNum = 0;        /* The query plan bitmask */
3514   int prefixIdx = -1;    /* Index of the start= constraint, or -1 if none */
3515   int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3516   int nArg = 0;          /* Number of arguments that completeFilter() expects */
3517   const struct sqlite3_index_constraint *pConstraint;
3518
3519   (void)(tab);    /* Unused parameter */
3520   pConstraint = pIdxInfo->aConstraint;
3521   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3522     if( pConstraint->usable==0 ) continue;
3523     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3524     switch( pConstraint->iColumn ){
3525       case COMPLETION_COLUMN_PREFIX:
3526         prefixIdx = i;
3527         idxNum |= 1;
3528         break;
3529       case COMPLETION_COLUMN_WHOLELINE:
3530         wholelineIdx = i;
3531         idxNum |= 2;
3532         break;
3533     }
3534   }
3535   if( prefixIdx>=0 ){
3536     pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3537     pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3538   }
3539   if( wholelineIdx>=0 ){
3540     pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3541     pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3542   }
3543   pIdxInfo->idxNum = idxNum;
3544   pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3545   pIdxInfo->estimatedRows = 500 - 100*nArg;
3546   return SQLITE_OK;
3547 }
3548
3549 /*
3550 ** This following structure defines all the methods for the 
3551 ** completion virtual table.
3552 */
3553 static sqlite3_module completionModule = {
3554   0,                         /* iVersion */
3555   0,                         /* xCreate */
3556   completionConnect,         /* xConnect */
3557   completionBestIndex,       /* xBestIndex */
3558   completionDisconnect,      /* xDisconnect */
3559   0,                         /* xDestroy */
3560   completionOpen,            /* xOpen - open a cursor */
3561   completionClose,           /* xClose - close a cursor */
3562   completionFilter,          /* xFilter - configure scan constraints */
3563   completionNext,            /* xNext - advance a cursor */
3564   completionEof,             /* xEof - check for end of scan */
3565   completionColumn,          /* xColumn - read data */
3566   completionRowid,           /* xRowid - read data */
3567   0,                         /* xUpdate */
3568   0,                         /* xBegin */
3569   0,                         /* xSync */
3570   0,                         /* xCommit */
3571   0,                         /* xRollback */
3572   0,                         /* xFindMethod */
3573   0,                         /* xRename */
3574   0,                         /* xSavepoint */
3575   0,                         /* xRelease */
3576   0,                         /* xRollbackTo */
3577   0                          /* xShadowName */
3578 };
3579
3580 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3581
3582 int sqlite3CompletionVtabInit(sqlite3 *db){
3583   int rc = SQLITE_OK;
3584 #ifndef SQLITE_OMIT_VIRTUALTABLE
3585   rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3586 #endif
3587   return rc;
3588 }
3589
3590 #ifdef _WIN32
3591
3592 #endif
3593 int sqlite3_completion_init(
3594   sqlite3 *db, 
3595   char **pzErrMsg, 
3596   const sqlite3_api_routines *pApi
3597 ){
3598   int rc = SQLITE_OK;
3599   SQLITE_EXTENSION_INIT2(pApi);
3600   (void)(pzErrMsg);  /* Unused parameter */
3601 #ifndef SQLITE_OMIT_VIRTUALTABLE
3602   rc = sqlite3CompletionVtabInit(db);
3603 #endif
3604   return rc;
3605 }
3606
3607 /************************* End ../ext/misc/completion.c ********************/
3608 /************************* Begin ../ext/misc/appendvfs.c ******************/
3609 /*
3610 ** 2017-10-20
3611 **
3612 ** The author disclaims copyright to this source code.  In place of
3613 ** a legal notice, here is a blessing:
3614 **
3615 **    May you do good and not evil.
3616 **    May you find forgiveness for yourself and forgive others.
3617 **    May you share freely, never taking more than you give.
3618 **
3619 ******************************************************************************
3620 **
3621 ** This file implements a VFS shim that allows an SQLite database to be
3622 ** appended onto the end of some other file, such as an executable.
3623 **
3624 ** A special record must appear at the end of the file that identifies the
3625 ** file as an appended database and provides an offset to page 1.  For
3626 ** best performance page 1 should be located at a disk page boundary, though
3627 ** that is not required.
3628 **
3629 ** When opening a database using this VFS, the connection might treat
3630 ** the file as an ordinary SQLite database, or it might treat is as a
3631 ** database appended onto some other file.  Here are the rules:
3632 **
3633 **  (1)  When opening a new empty file, that file is treated as an ordinary
3634 **       database.
3635 **
3636 **  (2)  When opening a file that begins with the standard SQLite prefix
3637 **       string "SQLite format 3", that file is treated as an ordinary
3638 **       database.
3639 **
3640 **  (3)  When opening a file that ends with the appendvfs trailer string
3641 **       "Start-Of-SQLite3-NNNNNNNN" that file is treated as an appended
3642 **       database.
3643 **
3644 **  (4)  If none of the above apply and the SQLITE_OPEN_CREATE flag is
3645 **       set, then a new database is appended to the already existing file.
3646 **
3647 **  (5)  Otherwise, SQLITE_CANTOPEN is returned.
3648 **
3649 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3650 ** the file containing the database is limited to 1GB.  This VFS will refuse
3651 ** to read or write past the 1GB mark.  This restriction might be lifted in
3652 ** future versions.  For now, if you need a large database, then keep the
3653 ** database in a separate file.
3654 **
3655 ** If the file being opened is not an appended database, then this shim is
3656 ** a pass-through into the default underlying VFS.
3657 **/
3658 /* #include "sqlite3ext.h" */
3659 SQLITE_EXTENSION_INIT1
3660 #include <string.h>
3661 #include <assert.h>
3662
3663 /* The append mark at the end of the database is:
3664 **
3665 **     Start-Of-SQLite3-NNNNNNNN
3666 **     123456789 123456789 12345
3667 **
3668 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3669 ** the offset to page 1.
3670 */
3671 #define APND_MARK_PREFIX     "Start-Of-SQLite3-"
3672 #define APND_MARK_PREFIX_SZ  17
3673 #define APND_MARK_SIZE       25
3674
3675 /*
3676 ** Maximum size of the combined prefix + database + append-mark.  This
3677 ** must be less than 0x40000000 to avoid locking issues on Windows.
3678 */
3679 #define APND_MAX_SIZE  (65536*15259)
3680
3681 /*
3682 ** Forward declaration of objects used by this utility
3683 */
3684 typedef struct sqlite3_vfs ApndVfs;
3685 typedef struct ApndFile ApndFile;
3686
3687 /* Access to a lower-level VFS that (might) implement dynamic loading,
3688 ** access to randomness, etc.
3689 */
3690 #define ORIGVFS(p)  ((sqlite3_vfs*)((p)->pAppData))
3691 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3692
3693 /* An open file */
3694 struct ApndFile {
3695   sqlite3_file base;              /* IO methods */
3696   sqlite3_int64 iPgOne;           /* File offset to page 1 */
3697   sqlite3_int64 iMark;            /* Start of the append-mark */
3698 };
3699
3700 /*
3701 ** Methods for ApndFile
3702 */
3703 static int apndClose(sqlite3_file*);
3704 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3705 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3706 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3707 static int apndSync(sqlite3_file*, int flags);
3708 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3709 static int apndLock(sqlite3_file*, int);
3710 static int apndUnlock(sqlite3_file*, int);
3711 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3712 static int apndFileControl(sqlite3_file*, int op, void *pArg);
3713 static int apndSectorSize(sqlite3_file*);
3714 static int apndDeviceCharacteristics(sqlite3_file*);
3715 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3716 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3717 static void apndShmBarrier(sqlite3_file*);
3718 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3719 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3720 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3721
3722 /*
3723 ** Methods for ApndVfs
3724 */
3725 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3726 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3727 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3728 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3729 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3730 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3731 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3732 static void apndDlClose(sqlite3_vfs*, void*);
3733 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3734 static int apndSleep(sqlite3_vfs*, int microseconds);
3735 static int apndCurrentTime(sqlite3_vfs*, double*);
3736 static int apndGetLastError(sqlite3_vfs*, int, char *);
3737 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3738 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3739 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3740 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3741
3742 static sqlite3_vfs apnd_vfs = {
3743   3,                            /* iVersion (set when registered) */
3744   0,                            /* szOsFile (set when registered) */
3745   1024,                         /* mxPathname */
3746   0,                            /* pNext */
3747   "apndvfs",                    /* zName */
3748   0,                            /* pAppData (set when registered) */ 
3749   apndOpen,                     /* xOpen */
3750   apndDelete,                   /* xDelete */
3751   apndAccess,                   /* xAccess */
3752   apndFullPathname,             /* xFullPathname */
3753   apndDlOpen,                   /* xDlOpen */
3754   apndDlError,                  /* xDlError */
3755   apndDlSym,                    /* xDlSym */
3756   apndDlClose,                  /* xDlClose */
3757   apndRandomness,               /* xRandomness */
3758   apndSleep,                    /* xSleep */
3759   apndCurrentTime,              /* xCurrentTime */
3760   apndGetLastError,             /* xGetLastError */
3761   apndCurrentTimeInt64,         /* xCurrentTimeInt64 */
3762   apndSetSystemCall,            /* xSetSystemCall */
3763   apndGetSystemCall,            /* xGetSystemCall */
3764   apndNextSystemCall            /* xNextSystemCall */
3765 };
3766
3767 static const sqlite3_io_methods apnd_io_methods = {
3768   3,                              /* iVersion */
3769   apndClose,                      /* xClose */
3770   apndRead,                       /* xRead */
3771   apndWrite,                      /* xWrite */
3772   apndTruncate,                   /* xTruncate */
3773   apndSync,                       /* xSync */
3774   apndFileSize,                   /* xFileSize */
3775   apndLock,                       /* xLock */
3776   apndUnlock,                     /* xUnlock */
3777   apndCheckReservedLock,          /* xCheckReservedLock */
3778   apndFileControl,                /* xFileControl */
3779   apndSectorSize,                 /* xSectorSize */
3780   apndDeviceCharacteristics,      /* xDeviceCharacteristics */
3781   apndShmMap,                     /* xShmMap */
3782   apndShmLock,                    /* xShmLock */
3783   apndShmBarrier,                 /* xShmBarrier */
3784   apndShmUnmap,                   /* xShmUnmap */
3785   apndFetch,                      /* xFetch */
3786   apndUnfetch                     /* xUnfetch */
3787 };
3788
3789
3790
3791 /*
3792 ** Close an apnd-file.
3793 */
3794 static int apndClose(sqlite3_file *pFile){
3795   pFile = ORIGFILE(pFile);
3796   return pFile->pMethods->xClose(pFile);
3797 }
3798
3799 /*
3800 ** Read data from an apnd-file.
3801 */
3802 static int apndRead(
3803   sqlite3_file *pFile, 
3804   void *zBuf, 
3805   int iAmt, 
3806   sqlite_int64 iOfst
3807 ){
3808   ApndFile *p = (ApndFile *)pFile;
3809   pFile = ORIGFILE(pFile);
3810   return pFile->pMethods->xRead(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3811 }
3812
3813 /*
3814 ** Add the append-mark onto the end of the file.
3815 */
3816 static int apndWriteMark(ApndFile *p, sqlite3_file *pFile){
3817   int i;
3818   unsigned char a[APND_MARK_SIZE];
3819   memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3820   for(i=0; i<8; i++){
3821     a[APND_MARK_PREFIX_SZ+i] = (p->iPgOne >> (56 - i*8)) & 0xff;
3822   }
3823   return pFile->pMethods->xWrite(pFile, a, APND_MARK_SIZE, p->iMark);
3824 }
3825
3826 /*
3827 ** Write data to an apnd-file.
3828 */
3829 static int apndWrite(
3830   sqlite3_file *pFile,
3831   const void *zBuf,
3832   int iAmt,
3833   sqlite_int64 iOfst
3834 ){
3835   int rc;
3836   ApndFile *p = (ApndFile *)pFile;
3837   pFile = ORIGFILE(pFile);
3838   if( iOfst+iAmt>=APND_MAX_SIZE ) return SQLITE_FULL;
3839   rc = pFile->pMethods->xWrite(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3840   if( rc==SQLITE_OK &&  iOfst + iAmt + p->iPgOne > p->iMark ){
3841     sqlite3_int64 sz = 0;
3842     rc = pFile->pMethods->xFileSize(pFile, &sz);
3843     if( rc==SQLITE_OK ){
3844       p->iMark = sz - APND_MARK_SIZE;
3845       if( iOfst + iAmt + p->iPgOne > p->iMark ){
3846         p->iMark = p->iPgOne + iOfst + iAmt;
3847         rc = apndWriteMark(p, pFile);
3848       }
3849     }
3850   }
3851   return rc;
3852 }
3853
3854 /*
3855 ** Truncate an apnd-file.
3856 */
3857 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3858   int rc;
3859   ApndFile *p = (ApndFile *)pFile;
3860   pFile = ORIGFILE(pFile);
3861   rc = pFile->pMethods->xTruncate(pFile, size+p->iPgOne+APND_MARK_SIZE);
3862   if( rc==SQLITE_OK ){
3863     p->iMark = p->iPgOne+size;
3864     rc = apndWriteMark(p, pFile);
3865   }
3866   return rc;
3867 }
3868
3869 /*
3870 ** Sync an apnd-file.
3871 */
3872 static int apndSync(sqlite3_file *pFile, int flags){
3873   pFile = ORIGFILE(pFile);
3874   return pFile->pMethods->xSync(pFile, flags);
3875 }
3876
3877 /*
3878 ** Return the current file-size of an apnd-file.
3879 */
3880 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3881   ApndFile *p = (ApndFile *)pFile;
3882   int rc;
3883   pFile = ORIGFILE(p);
3884   rc = pFile->pMethods->xFileSize(pFile, pSize);
3885   if( rc==SQLITE_OK && p->iPgOne ){
3886     *pSize -= p->iPgOne + APND_MARK_SIZE;
3887   }
3888   return rc;
3889 }
3890
3891 /*
3892 ** Lock an apnd-file.
3893 */
3894 static int apndLock(sqlite3_file *pFile, int eLock){
3895   pFile = ORIGFILE(pFile);
3896   return pFile->pMethods->xLock(pFile, eLock);
3897 }
3898
3899 /*
3900 ** Unlock an apnd-file.
3901 */
3902 static int apndUnlock(sqlite3_file *pFile, int eLock){
3903   pFile = ORIGFILE(pFile);
3904   return pFile->pMethods->xUnlock(pFile, eLock);
3905 }
3906
3907 /*
3908 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
3909 */
3910 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3911   pFile = ORIGFILE(pFile);
3912   return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3913 }
3914
3915 /*
3916 ** File control method. For custom operations on an apnd-file.
3917 */
3918 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
3919   ApndFile *p = (ApndFile *)pFile;
3920   int rc;
3921   pFile = ORIGFILE(pFile);
3922   rc = pFile->pMethods->xFileControl(pFile, op, pArg);
3923   if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3924     *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", p->iPgOne, *(char**)pArg);
3925   }
3926   return rc;
3927 }
3928
3929 /*
3930 ** Return the sector-size in bytes for an apnd-file.
3931 */
3932 static int apndSectorSize(sqlite3_file *pFile){
3933   pFile = ORIGFILE(pFile);
3934   return pFile->pMethods->xSectorSize(pFile);
3935 }
3936
3937 /*
3938 ** Return the device characteristic flags supported by an apnd-file.
3939 */
3940 static int apndDeviceCharacteristics(sqlite3_file *pFile){
3941   pFile = ORIGFILE(pFile);
3942   return pFile->pMethods->xDeviceCharacteristics(pFile);
3943 }
3944
3945 /* Create a shared memory file mapping */
3946 static int apndShmMap(
3947   sqlite3_file *pFile,
3948   int iPg,
3949   int pgsz,
3950   int bExtend,
3951   void volatile **pp
3952 ){
3953   pFile = ORIGFILE(pFile);
3954   return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
3955 }
3956
3957 /* Perform locking on a shared-memory segment */
3958 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
3959   pFile = ORIGFILE(pFile);
3960   return pFile->pMethods->xShmLock(pFile,offset,n,flags);
3961 }
3962
3963 /* Memory barrier operation on shared memory */
3964 static void apndShmBarrier(sqlite3_file *pFile){
3965   pFile = ORIGFILE(pFile);
3966   pFile->pMethods->xShmBarrier(pFile);
3967 }
3968
3969 /* Unmap a shared memory segment */
3970 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
3971   pFile = ORIGFILE(pFile);
3972   return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
3973 }
3974
3975 /* Fetch a page of a memory-mapped file */
3976 static int apndFetch(
3977   sqlite3_file *pFile,
3978   sqlite3_int64 iOfst,
3979   int iAmt,
3980   void **pp
3981 ){
3982   ApndFile *p = (ApndFile *)pFile;
3983   pFile = ORIGFILE(pFile);
3984   return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
3985 }
3986
3987 /* Release a memory-mapped page */
3988 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
3989   ApndFile *p = (ApndFile *)pFile;
3990   pFile = ORIGFILE(pFile);
3991   return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
3992 }
3993
3994 /*
3995 ** Check to see if the file is an ordinary SQLite database file.
3996 */
3997 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
3998   int rc;
3999   char zHdr[16];
4000   static const char aSqliteHdr[] = "SQLite format 3";
4001   if( sz<512 ) return 0;
4002   rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0);
4003   if( rc ) return 0;
4004   return memcmp(zHdr, aSqliteHdr, sizeof(zHdr))==0;
4005 }
4006
4007 /*
4008 ** Try to read the append-mark off the end of a file.  Return the
4009 ** start of the appended database if the append-mark is present.  If
4010 ** there is no append-mark, return -1;
4011 */
4012 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
4013   int rc, i;
4014   sqlite3_int64 iMark;
4015   unsigned char a[APND_MARK_SIZE];
4016
4017   if( sz<=APND_MARK_SIZE ) return -1;
4018   rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
4019   if( rc ) return -1;
4020   if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
4021   iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ]&0x7f))<<56;
4022   for(i=1; i<8; i++){    
4023     iMark += (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<(56-8*i);
4024   }
4025   return iMark;
4026 }
4027
4028 /*
4029 ** Open an apnd file handle.
4030 */
4031 static int apndOpen(
4032   sqlite3_vfs *pVfs,
4033   const char *zName,
4034   sqlite3_file *pFile,
4035   int flags,
4036   int *pOutFlags
4037 ){
4038   ApndFile *p;
4039   sqlite3_file *pSubFile;
4040   sqlite3_vfs *pSubVfs;
4041   int rc;
4042   sqlite3_int64 sz;
4043   pSubVfs = ORIGVFS(pVfs);
4044   if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
4045     return pSubVfs->xOpen(pSubVfs, zName, pFile, flags, pOutFlags);
4046   }
4047   p = (ApndFile*)pFile;
4048   memset(p, 0, sizeof(*p));
4049   pSubFile = ORIGFILE(pFile);
4050   p->base.pMethods = &apnd_io_methods;
4051   rc = pSubVfs->xOpen(pSubVfs, zName, pSubFile, flags, pOutFlags);
4052   if( rc ) goto apnd_open_done;
4053   rc = pSubFile->pMethods->xFileSize(pSubFile, &sz);
4054   if( rc ){
4055     pSubFile->pMethods->xClose(pSubFile);
4056     goto apnd_open_done;
4057   }
4058   if( apndIsOrdinaryDatabaseFile(sz, pSubFile) ){
4059     memmove(pFile, pSubFile, pSubVfs->szOsFile);
4060     return SQLITE_OK;
4061   }
4062   p->iMark = 0;
4063   p->iPgOne = apndReadMark(sz, pFile);
4064   if( p->iPgOne>0 ){
4065     return SQLITE_OK;
4066   }
4067   if( (flags & SQLITE_OPEN_CREATE)==0 ){
4068     pSubFile->pMethods->xClose(pSubFile);
4069     rc = SQLITE_CANTOPEN;
4070   }
4071   p->iPgOne = (sz+0xfff) & ~(sqlite3_int64)0xfff;
4072 apnd_open_done:
4073   if( rc ) pFile->pMethods = 0;
4074   return rc;
4075 }
4076
4077 /*
4078 ** All other VFS methods are pass-thrus.
4079 */
4080 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
4081   return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
4082 }
4083 static int apndAccess(
4084   sqlite3_vfs *pVfs, 
4085   const char *zPath, 
4086   int flags, 
4087   int *pResOut
4088 ){
4089   return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
4090 }
4091 static int apndFullPathname(
4092   sqlite3_vfs *pVfs, 
4093   const char *zPath, 
4094   int nOut, 
4095   char *zOut
4096 ){
4097   return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
4098 }
4099 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
4100   return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
4101 }
4102 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
4103   ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
4104 }
4105 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
4106   return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
4107 }
4108 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
4109   ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
4110 }
4111 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4112   return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4113 }
4114 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4115   return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4116 }
4117 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4118   return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4119 }
4120 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4121   return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4122 }
4123 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4124   return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4125 }
4126 static int apndSetSystemCall(
4127   sqlite3_vfs *pVfs,
4128   const char *zName,
4129   sqlite3_syscall_ptr pCall
4130 ){
4131   return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4132 }
4133 static sqlite3_syscall_ptr apndGetSystemCall(
4134   sqlite3_vfs *pVfs,
4135   const char *zName
4136 ){
4137   return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4138 }
4139 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4140   return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4141 }
4142
4143   
4144 #ifdef _WIN32
4145
4146 #endif
4147 /* 
4148 ** This routine is called when the extension is loaded.
4149 ** Register the new VFS.
4150 */
4151 int sqlite3_appendvfs_init(
4152   sqlite3 *db, 
4153   char **pzErrMsg, 
4154   const sqlite3_api_routines *pApi
4155 ){
4156   int rc = SQLITE_OK;
4157   sqlite3_vfs *pOrig;
4158   SQLITE_EXTENSION_INIT2(pApi);
4159   (void)pzErrMsg;
4160   (void)db;
4161   pOrig = sqlite3_vfs_find(0);
4162   apnd_vfs.iVersion = pOrig->iVersion;
4163   apnd_vfs.pAppData = pOrig;
4164   apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4165   rc = sqlite3_vfs_register(&apnd_vfs, 0);
4166 #ifdef APPENDVFS_TEST
4167   if( rc==SQLITE_OK ){
4168     rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4169   }
4170 #endif
4171   if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4172   return rc;
4173 }
4174
4175 /************************* End ../ext/misc/appendvfs.c ********************/
4176 /************************* Begin ../ext/misc/memtrace.c ******************/
4177 /*
4178 ** 2019-01-21
4179 **
4180 ** The author disclaims copyright to this source code.  In place of
4181 ** a legal notice, here is a blessing:
4182 **
4183 **    May you do good and not evil.
4184 **    May you find forgiveness for yourself and forgive others.
4185 **    May you share freely, never taking more than you give.
4186 **
4187 *************************************************************************
4188 **
4189 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
4190 ** mechanism to add a tracing layer on top of SQLite.  If this extension
4191 ** is registered prior to sqlite3_initialize(), it will cause all memory
4192 ** allocation activities to be logged on standard output, or to some other
4193 ** FILE specified by the initializer.
4194 **
4195 ** This file needs to be compiled into the application that uses it.
4196 **
4197 ** This extension is used to implement the --memtrace option of the
4198 ** command-line shell.
4199 */
4200 #include <assert.h>
4201 #include <string.h>
4202 #include <stdio.h>
4203
4204 /* The original memory allocation routines */
4205 static sqlite3_mem_methods memtraceBase;
4206 static FILE *memtraceOut;
4207
4208 /* Methods that trace memory allocations */
4209 static void *memtraceMalloc(int n){
4210   if( memtraceOut ){
4211     fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n", 
4212             memtraceBase.xRoundup(n));
4213   }
4214   return memtraceBase.xMalloc(n);
4215 }
4216 static void memtraceFree(void *p){
4217   if( p==0 ) return;
4218   if( memtraceOut ){
4219     fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
4220   }
4221   memtraceBase.xFree(p);
4222 }
4223 static void *memtraceRealloc(void *p, int n){
4224   if( p==0 ) return memtraceMalloc(n);
4225   if( n==0 ){
4226     memtraceFree(p);
4227     return 0;
4228   }
4229   if( memtraceOut ){
4230     fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
4231             memtraceBase.xSize(p), memtraceBase.xRoundup(n));
4232   }
4233   return memtraceBase.xRealloc(p, n);
4234 }
4235 static int memtraceSize(void *p){
4236   return memtraceBase.xSize(p);
4237 }
4238 static int memtraceRoundup(int n){
4239   return memtraceBase.xRoundup(n);
4240 }
4241 static int memtraceInit(void *p){
4242   return memtraceBase.xInit(p);
4243 }
4244 static void memtraceShutdown(void *p){
4245   memtraceBase.xShutdown(p);
4246 }
4247
4248 /* The substitute memory allocator */
4249 static sqlite3_mem_methods ersaztMethods = {
4250   memtraceMalloc,
4251   memtraceFree,
4252   memtraceRealloc,
4253   memtraceSize,
4254   memtraceRoundup,
4255   memtraceInit,
4256   memtraceShutdown,
4257   0
4258 };
4259
4260 /* Begin tracing memory allocations to out. */
4261 int sqlite3MemTraceActivate(FILE *out){
4262   int rc = SQLITE_OK;
4263   if( memtraceBase.xMalloc==0 ){
4264     rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
4265     if( rc==SQLITE_OK ){
4266       rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
4267     }
4268   }
4269   memtraceOut = out;
4270   return rc;
4271 }
4272
4273 /* Deactivate memory tracing */
4274 int sqlite3MemTraceDeactivate(void){
4275   int rc = SQLITE_OK;
4276   if( memtraceBase.xMalloc!=0 ){
4277     rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
4278     if( rc==SQLITE_OK ){
4279       memset(&memtraceBase, 0, sizeof(memtraceBase));
4280     }
4281   }
4282   memtraceOut = 0;
4283   return rc;
4284 }
4285
4286 /************************* End ../ext/misc/memtrace.c ********************/
4287 /************************* Begin ../ext/misc/uint.c ******************/
4288 /*
4289 ** 2020-04-14
4290 **
4291 ** The author disclaims copyright to this source code.  In place of
4292 ** a legal notice, here is a blessing:
4293 **
4294 **    May you do good and not evil.
4295 **    May you find forgiveness for yourself and forgive others.
4296 **    May you share freely, never taking more than you give.
4297 **
4298 ******************************************************************************
4299 **
4300 ** This SQLite extension implements the UINT collating sequence.
4301 **
4302 ** UINT works like BINARY for text, except that embedded strings
4303 ** of digits compare in numeric order.
4304 **
4305 **     *   Leading zeros are handled properly, in the sense that
4306 **         they do not mess of the maginitude comparison of embedded
4307 **         strings of digits.  "x00123y" is equal to "x123y".
4308 **
4309 **     *   Only unsigned integers are recognized.  Plus and minus
4310 **         signs are ignored.  Decimal points and exponential notation
4311 **         are ignored.
4312 **
4313 **     *   Embedded integers can be of arbitrary length.  Comparison
4314 **         is *not* limited integers that can be expressed as a
4315 **         64-bit machine integer.
4316 */
4317 /* #include "sqlite3ext.h" */
4318 SQLITE_EXTENSION_INIT1
4319 #include <assert.h>
4320 #include <string.h>
4321 #include <ctype.h>
4322
4323 /*
4324 ** Compare text in lexicographic order, except strings of digits
4325 ** compare in numeric order.
4326 */
4327 static int uintCollFunc(
4328   void *notUsed,
4329   int nKey1, const void *pKey1,
4330   int nKey2, const void *pKey2
4331 ){
4332   const unsigned char *zA = (const unsigned char*)pKey1;
4333   const unsigned char *zB = (const unsigned char*)pKey2;
4334   int i=0, j=0, x;
4335   (void)notUsed;
4336   while( i<nKey1 && j<nKey2 ){
4337     x = zA[i] - zB[j];
4338     if( isdigit(zA[i]) ){
4339       int k;
4340       if( !isdigit(zB[j]) ) return x;
4341       while( i<nKey1 && zA[i]=='0' ){ i++; }
4342       while( j<nKey2 && zB[j]=='0' ){ j++; }
4343       k = 0;
4344       while( i+k<nKey1 && isdigit(zA[i+k])
4345              && j+k<nKey2 && isdigit(zB[j+k]) ){
4346         k++;
4347       }
4348       if( i+k<nKey1 && isdigit(zA[i+k]) ){
4349         return +1;
4350       }else if( j+k<nKey2 && isdigit(zB[j+k]) ){
4351         return -1;
4352       }else{
4353         x = memcmp(zA+i, zB+j, k);
4354         if( x ) return x;
4355         i += k;
4356         j += k;
4357       }
4358     }else if( x ){
4359       return x;
4360     }else{
4361       i++;
4362       j++;
4363     }
4364   }
4365   return (nKey1 - i) - (nKey2 - j);
4366 }
4367
4368 #ifdef _WIN32
4369
4370 #endif
4371 int sqlite3_uint_init(
4372   sqlite3 *db, 
4373   char **pzErrMsg, 
4374   const sqlite3_api_routines *pApi
4375 ){
4376   SQLITE_EXTENSION_INIT2(pApi);
4377   (void)pzErrMsg;  /* Unused parameter */
4378   return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc);
4379 }
4380
4381 /************************* End ../ext/misc/uint.c ********************/
4382 #ifdef SQLITE_HAVE_ZLIB
4383 /************************* Begin ../ext/misc/zipfile.c ******************/
4384 /*
4385 ** 2017-12-26
4386 **
4387 ** The author disclaims copyright to this source code.  In place of
4388 ** a legal notice, here is a blessing:
4389 **
4390 **    May you do good and not evil.
4391 **    May you find forgiveness for yourself and forgive others.
4392 **    May you share freely, never taking more than you give.
4393 **
4394 ******************************************************************************
4395 **
4396 ** This file implements a virtual table for reading and writing ZIP archive
4397 ** files.
4398 **
4399 ** Usage example:
4400 **
4401 **     SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
4402 **
4403 ** Current limitations:
4404 **
4405 **    *  No support for encryption
4406 **    *  No support for ZIP archives spanning multiple files
4407 **    *  No support for zip64 extensions
4408 **    *  Only the "inflate/deflate" (zlib) compression method is supported
4409 */
4410 /* #include "sqlite3ext.h" */
4411 SQLITE_EXTENSION_INIT1
4412 #include <stdio.h>
4413 #include <string.h>
4414 #include <assert.h>
4415
4416 #include <zlib.h>
4417
4418 #ifndef SQLITE_OMIT_VIRTUALTABLE
4419
4420 #ifndef SQLITE_AMALGAMATION
4421
4422 /* typedef sqlite3_int64 i64; */
4423 /* typedef unsigned char u8; */
4424 typedef unsigned short u16;
4425 typedef unsigned long u32;
4426 #define MIN(a,b) ((a)<(b) ? (a) : (b))
4427
4428 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
4429 # define ALWAYS(X)      (1)
4430 # define NEVER(X)       (0)
4431 #elif !defined(NDEBUG)
4432 # define ALWAYS(X)      ((X)?1:(assert(0),0))
4433 # define NEVER(X)       ((X)?(assert(0),1):0)
4434 #else
4435 # define ALWAYS(X)      (X)
4436 # define NEVER(X)       (X)
4437 #endif
4438
4439 #endif   /* SQLITE_AMALGAMATION */
4440
4441 /*
4442 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
4443 **
4444 ** In some ways it would be better to obtain these values from system 
4445 ** header files. But, the dependency is undesirable and (a) these
4446 ** have been stable for decades, (b) the values are part of POSIX and
4447 ** are also made explicit in [man stat], and (c) are part of the 
4448 ** file format for zip archives.
4449 */
4450 #ifndef S_IFDIR
4451 # define S_IFDIR 0040000
4452 #endif
4453 #ifndef S_IFREG
4454 # define S_IFREG 0100000
4455 #endif
4456 #ifndef S_IFLNK
4457 # define S_IFLNK 0120000
4458 #endif
4459
4460 static const char ZIPFILE_SCHEMA[] = 
4461   "CREATE TABLE y("
4462     "name PRIMARY KEY,"  /* 0: Name of file in zip archive */
4463     "mode,"              /* 1: POSIX mode for file */
4464     "mtime,"             /* 2: Last modification time (secs since 1970)*/
4465     "sz,"                /* 3: Size of object */
4466     "rawdata,"           /* 4: Raw data */
4467     "data,"              /* 5: Uncompressed data */
4468     "method,"            /* 6: Compression method (integer) */
4469     "z HIDDEN"           /* 7: Name of zip file */
4470   ") WITHOUT ROWID;";
4471
4472 #define ZIPFILE_F_COLUMN_IDX 7    /* Index of column "file" in the above */
4473 #define ZIPFILE_BUFFER_SIZE (64*1024)
4474
4475
4476 /*
4477 ** Magic numbers used to read and write zip files.
4478 **
4479 ** ZIPFILE_NEWENTRY_MADEBY:
4480 **   Use this value for the "version-made-by" field in new zip file
4481 **   entries. The upper byte indicates "unix", and the lower byte 
4482 **   indicates that the zip file matches pkzip specification 3.0. 
4483 **   This is what info-zip seems to do.
4484 **
4485 ** ZIPFILE_NEWENTRY_REQUIRED:
4486 **   Value for "version-required-to-extract" field of new entries.
4487 **   Version 2.0 is required to support folders and deflate compression.
4488 **
4489 ** ZIPFILE_NEWENTRY_FLAGS:
4490 **   Value for "general-purpose-bit-flags" field of new entries. Bit
4491 **   11 means "utf-8 filename and comment".
4492 **
4493 ** ZIPFILE_SIGNATURE_CDS:
4494 **   First 4 bytes of a valid CDS record.
4495 **
4496 ** ZIPFILE_SIGNATURE_LFH:
4497 **   First 4 bytes of a valid LFH record.
4498 **
4499 ** ZIPFILE_SIGNATURE_EOCD
4500 **   First 4 bytes of a valid EOCD record.
4501 */
4502 #define ZIPFILE_EXTRA_TIMESTAMP   0x5455
4503 #define ZIPFILE_NEWENTRY_MADEBY   ((3<<8) + 30)
4504 #define ZIPFILE_NEWENTRY_REQUIRED 20
4505 #define ZIPFILE_NEWENTRY_FLAGS    0x800
4506 #define ZIPFILE_SIGNATURE_CDS     0x02014b50
4507 #define ZIPFILE_SIGNATURE_LFH     0x04034b50
4508 #define ZIPFILE_SIGNATURE_EOCD    0x06054b50
4509
4510 /*
4511 ** The sizes of the fixed-size part of each of the three main data 
4512 ** structures in a zip archive.
4513 */
4514 #define ZIPFILE_LFH_FIXED_SZ      30
4515 #define ZIPFILE_EOCD_FIXED_SZ     22
4516 #define ZIPFILE_CDS_FIXED_SZ      46
4517
4518 /*
4519 *** 4.3.16  End of central directory record:
4520 ***
4521 ***   end of central dir signature    4 bytes  (0x06054b50)
4522 ***   number of this disk             2 bytes
4523 ***   number of the disk with the
4524 ***   start of the central directory  2 bytes
4525 ***   total number of entries in the
4526 ***   central directory on this disk  2 bytes
4527 ***   total number of entries in
4528 ***   the central directory           2 bytes
4529 ***   size of the central directory   4 bytes
4530 ***   offset of start of central
4531 ***   directory with respect to
4532 ***   the starting disk number        4 bytes
4533 ***   .ZIP file comment length        2 bytes
4534 ***   .ZIP file comment       (variable size)
4535 */
4536 typedef struct ZipfileEOCD ZipfileEOCD;
4537 struct ZipfileEOCD {
4538   u16 iDisk;
4539   u16 iFirstDisk;
4540   u16 nEntry;
4541   u16 nEntryTotal;
4542   u32 nSize;
4543   u32 iOffset;
4544 };
4545
4546 /*
4547 *** 4.3.12  Central directory structure:
4548 ***
4549 *** ...
4550 ***
4551 ***   central file header signature   4 bytes  (0x02014b50)
4552 ***   version made by                 2 bytes
4553 ***   version needed to extract       2 bytes
4554 ***   general purpose bit flag        2 bytes
4555 ***   compression method              2 bytes
4556 ***   last mod file time              2 bytes
4557 ***   last mod file date              2 bytes
4558 ***   crc-32                          4 bytes
4559 ***   compressed size                 4 bytes
4560 ***   uncompressed size               4 bytes
4561 ***   file name length                2 bytes
4562 ***   extra field length              2 bytes
4563 ***   file comment length             2 bytes
4564 ***   disk number start               2 bytes
4565 ***   internal file attributes        2 bytes
4566 ***   external file attributes        4 bytes
4567 ***   relative offset of local header 4 bytes
4568 */
4569 typedef struct ZipfileCDS ZipfileCDS;
4570 struct ZipfileCDS {
4571   u16 iVersionMadeBy;
4572   u16 iVersionExtract;
4573   u16 flags;
4574   u16 iCompression;
4575   u16 mTime;
4576   u16 mDate;
4577   u32 crc32;
4578   u32 szCompressed;
4579   u32 szUncompressed;
4580   u16 nFile;
4581   u16 nExtra;
4582   u16 nComment;
4583   u16 iDiskStart;
4584   u16 iInternalAttr;
4585   u32 iExternalAttr;
4586   u32 iOffset;
4587   char *zFile;                    /* Filename (sqlite3_malloc()) */
4588 };
4589
4590 /*
4591 *** 4.3.7  Local file header:
4592 ***
4593 ***   local file header signature     4 bytes  (0x04034b50)
4594 ***   version needed to extract       2 bytes
4595 ***   general purpose bit flag        2 bytes
4596 ***   compression method              2 bytes
4597 ***   last mod file time              2 bytes
4598 ***   last mod file date              2 bytes
4599 ***   crc-32                          4 bytes
4600 ***   compressed size                 4 bytes
4601 ***   uncompressed size               4 bytes
4602 ***   file name length                2 bytes
4603 ***   extra field length              2 bytes
4604 ***   
4605 */
4606 typedef struct ZipfileLFH ZipfileLFH;
4607 struct ZipfileLFH {
4608   u16 iVersionExtract;
4609   u16 flags;
4610   u16 iCompression;
4611   u16 mTime;
4612   u16 mDate;
4613   u32 crc32;
4614   u32 szCompressed;
4615   u32 szUncompressed;
4616   u16 nFile;
4617   u16 nExtra;
4618 };
4619
4620 typedef struct ZipfileEntry ZipfileEntry;
4621 struct ZipfileEntry {
4622   ZipfileCDS cds;            /* Parsed CDS record */
4623   u32 mUnixTime;             /* Modification time, in UNIX format */
4624   u8 *aExtra;                /* cds.nExtra+cds.nComment bytes of extra data */
4625   i64 iDataOff;              /* Offset to data in file (if aData==0) */
4626   u8 *aData;                 /* cds.szCompressed bytes of compressed data */
4627   ZipfileEntry *pNext;       /* Next element in in-memory CDS */
4628 };
4629
4630 /* 
4631 ** Cursor type for zipfile tables.
4632 */
4633 typedef struct ZipfileCsr ZipfileCsr;
4634 struct ZipfileCsr {
4635   sqlite3_vtab_cursor base;  /* Base class - must be first */
4636   i64 iId;                   /* Cursor ID */
4637   u8 bEof;                   /* True when at EOF */
4638   u8 bNoop;                  /* If next xNext() call is no-op */
4639
4640   /* Used outside of write transactions */
4641   FILE *pFile;               /* Zip file */
4642   i64 iNextOff;              /* Offset of next record in central directory */
4643   ZipfileEOCD eocd;          /* Parse of central directory record */
4644
4645   ZipfileEntry *pFreeEntry;  /* Free this list when cursor is closed or reset */
4646   ZipfileEntry *pCurrent;    /* Current entry */
4647   ZipfileCsr *pCsrNext;      /* Next cursor on same virtual table */
4648 };
4649
4650 typedef struct ZipfileTab ZipfileTab;
4651 struct ZipfileTab {
4652   sqlite3_vtab base;         /* Base class - must be first */
4653   char *zFile;               /* Zip file this table accesses (may be NULL) */
4654   sqlite3 *db;               /* Host database connection */
4655   u8 *aBuffer;               /* Temporary buffer used for various tasks */
4656
4657   ZipfileCsr *pCsrList;      /* List of cursors */
4658   i64 iNextCsrid;
4659
4660   /* The following are used by write transactions only */
4661   ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
4662   ZipfileEntry *pLastEntry;  /* Last element in pFirstEntry list */
4663   FILE *pWriteFd;            /* File handle open on zip archive */
4664   i64 szCurrent;             /* Current size of zip archive */
4665   i64 szOrig;                /* Size of archive at start of transaction */
4666 };
4667
4668 /*
4669 ** Set the error message contained in context ctx to the results of
4670 ** vprintf(zFmt, ...).
4671 */
4672 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
4673   char *zMsg = 0;
4674   va_list ap;
4675   va_start(ap, zFmt);
4676   zMsg = sqlite3_vmprintf(zFmt, ap);
4677   sqlite3_result_error(ctx, zMsg, -1);
4678   sqlite3_free(zMsg);
4679   va_end(ap);
4680 }
4681
4682 /*
4683 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
4684 ** is not quoted, do nothing.
4685 */
4686 static void zipfileDequote(char *zIn){
4687   char q = zIn[0];
4688   if( q=='"' || q=='\'' || q=='`' || q=='[' ){
4689     int iIn = 1;
4690     int iOut = 0;
4691     if( q=='[' ) q = ']';
4692     while( ALWAYS(zIn[iIn]) ){
4693       char c = zIn[iIn++];
4694       if( c==q && zIn[iIn++]!=q ) break;
4695       zIn[iOut++] = c;
4696     }
4697     zIn[iOut] = '\0';
4698   }
4699 }
4700
4701 /*
4702 ** Construct a new ZipfileTab virtual table object.
4703 ** 
4704 **   argv[0]   -> module name  ("zipfile")
4705 **   argv[1]   -> database name
4706 **   argv[2]   -> table name
4707 **   argv[...] -> "column name" and other module argument fields.
4708 */
4709 static int zipfileConnect(
4710   sqlite3 *db,
4711   void *pAux,
4712   int argc, const char *const*argv,
4713   sqlite3_vtab **ppVtab,
4714   char **pzErr
4715 ){
4716   int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
4717   int nFile = 0;
4718   const char *zFile = 0;
4719   ZipfileTab *pNew = 0;
4720   int rc;
4721
4722   /* If the table name is not "zipfile", require that the argument be
4723   ** specified. This stops zipfile tables from being created as:
4724   **
4725   **   CREATE VIRTUAL TABLE zzz USING zipfile();
4726   **
4727   ** It does not prevent:
4728   **
4729   **   CREATE VIRTUAL TABLE zipfile USING zipfile();
4730   */
4731   assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
4732   if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
4733     *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
4734     return SQLITE_ERROR;
4735   }
4736
4737   if( argc>3 ){
4738     zFile = argv[3];
4739     nFile = (int)strlen(zFile)+1;
4740   }
4741
4742   rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
4743   if( rc==SQLITE_OK ){
4744     pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
4745     if( pNew==0 ) return SQLITE_NOMEM;
4746     memset(pNew, 0, nByte+nFile);
4747     pNew->db = db;
4748     pNew->aBuffer = (u8*)&pNew[1];
4749     if( zFile ){
4750       pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
4751       memcpy(pNew->zFile, zFile, nFile);
4752       zipfileDequote(pNew->zFile);
4753     }
4754   }
4755   sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
4756   *ppVtab = (sqlite3_vtab*)pNew;
4757   return rc;
4758 }
4759
4760 /*
4761 ** Free the ZipfileEntry structure indicated by the only argument.
4762 */
4763 static void zipfileEntryFree(ZipfileEntry *p){
4764   if( p ){
4765     sqlite3_free(p->cds.zFile);
4766     sqlite3_free(p);
4767   }
4768 }
4769
4770 /*
4771 ** Release resources that should be freed at the end of a write 
4772 ** transaction.
4773 */
4774 static void zipfileCleanupTransaction(ZipfileTab *pTab){
4775   ZipfileEntry *pEntry;
4776   ZipfileEntry *pNext;
4777
4778   if( pTab->pWriteFd ){
4779     fclose(pTab->pWriteFd);
4780     pTab->pWriteFd = 0;
4781   }
4782   for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
4783     pNext = pEntry->pNext;
4784     zipfileEntryFree(pEntry);
4785   }
4786   pTab->pFirstEntry = 0;
4787   pTab->pLastEntry = 0;
4788   pTab->szCurrent = 0;
4789   pTab->szOrig = 0;
4790 }
4791
4792 /*
4793 ** This method is the destructor for zipfile vtab objects.
4794 */
4795 static int zipfileDisconnect(sqlite3_vtab *pVtab){
4796   zipfileCleanupTransaction((ZipfileTab*)pVtab);
4797   sqlite3_free(pVtab);
4798   return SQLITE_OK;
4799 }
4800
4801 /*
4802 ** Constructor for a new ZipfileCsr object.
4803 */
4804 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
4805   ZipfileTab *pTab = (ZipfileTab*)p;
4806   ZipfileCsr *pCsr;
4807   pCsr = sqlite3_malloc(sizeof(*pCsr));
4808   *ppCsr = (sqlite3_vtab_cursor*)pCsr;
4809   if( pCsr==0 ){
4810     return SQLITE_NOMEM;
4811   }
4812   memset(pCsr, 0, sizeof(*pCsr));
4813   pCsr->iId = ++pTab->iNextCsrid;
4814   pCsr->pCsrNext = pTab->pCsrList;
4815   pTab->pCsrList = pCsr;
4816   return SQLITE_OK;
4817 }
4818
4819 /*
4820 ** Reset a cursor back to the state it was in when first returned
4821 ** by zipfileOpen().
4822 */
4823 static void zipfileResetCursor(ZipfileCsr *pCsr){
4824   ZipfileEntry *p;
4825   ZipfileEntry *pNext;
4826
4827   pCsr->bEof = 0;
4828   if( pCsr->pFile ){
4829     fclose(pCsr->pFile);
4830     pCsr->pFile = 0;
4831     zipfileEntryFree(pCsr->pCurrent);
4832     pCsr->pCurrent = 0;
4833   }
4834
4835   for(p=pCsr->pFreeEntry; p; p=pNext){
4836     pNext = p->pNext;
4837     zipfileEntryFree(p);
4838   }
4839 }
4840
4841 /*
4842 ** Destructor for an ZipfileCsr.
4843 */
4844 static int zipfileClose(sqlite3_vtab_cursor *cur){
4845   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4846   ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
4847   ZipfileCsr **pp;
4848   zipfileResetCursor(pCsr);
4849
4850   /* Remove this cursor from the ZipfileTab.pCsrList list. */
4851   for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
4852   *pp = pCsr->pCsrNext;
4853
4854   sqlite3_free(pCsr);
4855   return SQLITE_OK;
4856 }
4857
4858 /*
4859 ** Set the error message for the virtual table associated with cursor
4860 ** pCsr to the results of vprintf(zFmt, ...).
4861 */
4862 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
4863   va_list ap;
4864   va_start(ap, zFmt);
4865   sqlite3_free(pTab->base.zErrMsg);
4866   pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
4867   va_end(ap);
4868 }
4869 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
4870   va_list ap;
4871   va_start(ap, zFmt);
4872   sqlite3_free(pCsr->base.pVtab->zErrMsg);
4873   pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
4874   va_end(ap);
4875 }
4876
4877 /*
4878 ** Read nRead bytes of data from offset iOff of file pFile into buffer
4879 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
4880 ** otherwise. 
4881 **
4882 ** If an error does occur, output variable (*pzErrmsg) may be set to point
4883 ** to an English language error message. It is the responsibility of the
4884 ** caller to eventually free this buffer using
4885 ** sqlite3_free().
4886 */
4887 static int zipfileReadData(
4888   FILE *pFile,                    /* Read from this file */
4889   u8 *aRead,                      /* Read into this buffer */
4890   int nRead,                      /* Number of bytes to read */
4891   i64 iOff,                       /* Offset to read from */
4892   char **pzErrmsg                 /* OUT: Error message (from sqlite3_malloc) */
4893 ){
4894   size_t n;
4895   fseek(pFile, (long)iOff, SEEK_SET);
4896   n = fread(aRead, 1, nRead, pFile);
4897   if( (int)n!=nRead ){
4898     *pzErrmsg = sqlite3_mprintf("error in fread()");
4899     return SQLITE_ERROR;
4900   }
4901   return SQLITE_OK;
4902 }
4903
4904 static int zipfileAppendData(
4905   ZipfileTab *pTab,
4906   const u8 *aWrite,
4907   int nWrite
4908 ){
4909   size_t n;
4910   fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
4911   n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
4912   if( (int)n!=nWrite ){
4913     pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
4914     return SQLITE_ERROR;
4915   }
4916   pTab->szCurrent += nWrite;
4917   return SQLITE_OK;
4918 }
4919
4920 /*
4921 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
4922 */
4923 static u16 zipfileGetU16(const u8 *aBuf){
4924   return (aBuf[1] << 8) + aBuf[0];
4925 }
4926
4927 /*
4928 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
4929 */
4930 static u32 zipfileGetU32(const u8 *aBuf){
4931   return ((u32)(aBuf[3]) << 24)
4932        + ((u32)(aBuf[2]) << 16)
4933        + ((u32)(aBuf[1]) <<  8)
4934        + ((u32)(aBuf[0]) <<  0);
4935 }
4936
4937 /*
4938 ** Write a 16-bit little endiate integer into buffer aBuf.
4939 */
4940 static void zipfilePutU16(u8 *aBuf, u16 val){
4941   aBuf[0] = val & 0xFF;
4942   aBuf[1] = (val>>8) & 0xFF;
4943 }
4944
4945 /*
4946 ** Write a 32-bit little endiate integer into buffer aBuf.
4947 */
4948 static void zipfilePutU32(u8 *aBuf, u32 val){
4949   aBuf[0] = val & 0xFF;
4950   aBuf[1] = (val>>8) & 0xFF;
4951   aBuf[2] = (val>>16) & 0xFF;
4952   aBuf[3] = (val>>24) & 0xFF;
4953 }
4954
4955 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
4956 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
4957
4958 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
4959 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
4960
4961 /*
4962 ** Magic numbers used to read CDS records.
4963 */
4964 #define ZIPFILE_CDS_NFILE_OFF        28
4965 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
4966
4967 /*
4968 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
4969 ** if the record is not well-formed, or SQLITE_OK otherwise.
4970 */
4971 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
4972   u8 *aRead = aBuf;
4973   u32 sig = zipfileRead32(aRead);
4974   int rc = SQLITE_OK;
4975   if( sig!=ZIPFILE_SIGNATURE_CDS ){
4976     rc = SQLITE_ERROR;
4977   }else{
4978     pCDS->iVersionMadeBy = zipfileRead16(aRead);
4979     pCDS->iVersionExtract = zipfileRead16(aRead);
4980     pCDS->flags = zipfileRead16(aRead);
4981     pCDS->iCompression = zipfileRead16(aRead);
4982     pCDS->mTime = zipfileRead16(aRead);
4983     pCDS->mDate = zipfileRead16(aRead);
4984     pCDS->crc32 = zipfileRead32(aRead);
4985     pCDS->szCompressed = zipfileRead32(aRead);
4986     pCDS->szUncompressed = zipfileRead32(aRead);
4987     assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
4988     pCDS->nFile = zipfileRead16(aRead);
4989     pCDS->nExtra = zipfileRead16(aRead);
4990     pCDS->nComment = zipfileRead16(aRead);
4991     pCDS->iDiskStart = zipfileRead16(aRead);
4992     pCDS->iInternalAttr = zipfileRead16(aRead);
4993     pCDS->iExternalAttr = zipfileRead32(aRead);
4994     pCDS->iOffset = zipfileRead32(aRead);
4995     assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
4996   }
4997
4998   return rc;
4999 }
5000
5001 /*
5002 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
5003 ** if the record is not well-formed, or SQLITE_OK otherwise.
5004 */
5005 static int zipfileReadLFH(
5006   u8 *aBuffer,
5007   ZipfileLFH *pLFH
5008 ){
5009   u8 *aRead = aBuffer;
5010   int rc = SQLITE_OK;
5011
5012   u32 sig = zipfileRead32(aRead);
5013   if( sig!=ZIPFILE_SIGNATURE_LFH ){
5014     rc = SQLITE_ERROR;
5015   }else{
5016     pLFH->iVersionExtract = zipfileRead16(aRead);
5017     pLFH->flags = zipfileRead16(aRead);
5018     pLFH->iCompression = zipfileRead16(aRead);
5019     pLFH->mTime = zipfileRead16(aRead);
5020     pLFH->mDate = zipfileRead16(aRead);
5021     pLFH->crc32 = zipfileRead32(aRead);
5022     pLFH->szCompressed = zipfileRead32(aRead);
5023     pLFH->szUncompressed = zipfileRead32(aRead);
5024     pLFH->nFile = zipfileRead16(aRead);
5025     pLFH->nExtra = zipfileRead16(aRead);
5026   }
5027   return rc;
5028 }
5029
5030
5031 /*
5032 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
5033 ** Scan through this buffer to find an "extra-timestamp" field. If one
5034 ** exists, extract the 32-bit modification-timestamp from it and store
5035 ** the value in output parameter *pmTime.
5036 **
5037 ** Zero is returned if no extra-timestamp record could be found (and so
5038 ** *pmTime is left unchanged), or non-zero otherwise.
5039 **
5040 ** The general format of an extra field is:
5041 **
5042 **   Header ID    2 bytes
5043 **   Data Size    2 bytes
5044 **   Data         N bytes
5045 */
5046 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
5047   int ret = 0;
5048   u8 *p = aExtra;
5049   u8 *pEnd = &aExtra[nExtra];
5050
5051   while( p<pEnd ){
5052     u16 id = zipfileRead16(p);
5053     u16 nByte = zipfileRead16(p);
5054
5055     switch( id ){
5056       case ZIPFILE_EXTRA_TIMESTAMP: {
5057         u8 b = p[0];
5058         if( b & 0x01 ){     /* 0x01 -> modtime is present */
5059           *pmTime = zipfileGetU32(&p[1]);
5060           ret = 1;
5061         }
5062         break;
5063       }
5064     }
5065
5066     p += nByte;
5067   }
5068   return ret;
5069 }
5070
5071 /*
5072 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
5073 ** fields of the CDS structure passed as the only argument to a 32-bit
5074 ** UNIX seconds-since-the-epoch timestamp. Return the result.
5075 **
5076 ** "Standard" MS-DOS time format:
5077 **
5078 **   File modification time:
5079 **     Bits 00-04: seconds divided by 2
5080 **     Bits 05-10: minute
5081 **     Bits 11-15: hour
5082 **   File modification date:
5083 **     Bits 00-04: day
5084 **     Bits 05-08: month (1-12)
5085 **     Bits 09-15: years from 1980 
5086 **
5087 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
5088 */
5089 static u32 zipfileMtime(ZipfileCDS *pCDS){
5090   int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
5091   int M = ((pCDS->mDate >> 5) & 0x0F);
5092   int D = (pCDS->mDate & 0x1F);
5093   int B = -13;
5094
5095   int sec = (pCDS->mTime & 0x1F)*2;
5096   int min = (pCDS->mTime >> 5) & 0x3F;
5097   int hr = (pCDS->mTime >> 11) & 0x1F;
5098   i64 JD;
5099
5100   /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
5101
5102   /* Calculate the JD in seconds for noon on the day in question */
5103   if( M<3 ){
5104     Y = Y-1;
5105     M = M+12;
5106   }
5107   JD = (i64)(24*60*60) * (
5108       (int)(365.25 * (Y + 4716))
5109     + (int)(30.6001 * (M + 1))
5110     + D + B - 1524
5111   );
5112
5113   /* Correct the JD for the time within the day */
5114   JD += (hr-12) * 3600 + min * 60 + sec;
5115
5116   /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
5117   return (u32)(JD - (i64)(24405875) * 24*60*6);
5118 }
5119
5120 /*
5121 ** The opposite of zipfileMtime(). This function populates the mTime and
5122 ** mDate fields of the CDS structure passed as the first argument according
5123 ** to the UNIX timestamp value passed as the second.
5124 */
5125 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
5126   /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
5127   i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
5128
5129   int A, B, C, D, E;
5130   int yr, mon, day;
5131   int hr, min, sec;
5132
5133   A = (int)((JD - 1867216.25)/36524.25);
5134   A = (int)(JD + 1 + A - (A/4));
5135   B = A + 1524;
5136   C = (int)((B - 122.1)/365.25);
5137   D = (36525*(C&32767))/100;
5138   E = (int)((B-D)/30.6001);
5139
5140   day = B - D - (int)(30.6001*E);
5141   mon = (E<14 ? E-1 : E-13);
5142   yr = mon>2 ? C-4716 : C-4715;
5143
5144   hr = (mUnixTime % (24*60*60)) / (60*60);
5145   min = (mUnixTime % (60*60)) / 60;
5146   sec = (mUnixTime % 60);
5147
5148   if( yr>=1980 ){
5149     pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
5150     pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
5151   }else{
5152     pCds->mDate = pCds->mTime = 0;
5153   }
5154
5155   assert( mUnixTime<315507600 
5156        || mUnixTime==zipfileMtime(pCds) 
5157        || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds)) 
5158        /* || (mUnixTime % 2) */
5159   );
5160 }
5161
5162 /*
5163 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
5164 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
5165 ** then pFile is a file-handle open on a zip file. In either case, this
5166 ** function creates a ZipfileEntry object based on the zip archive entry
5167 ** for which the CDS record is at offset iOff.
5168 **
5169 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
5170 ** the new object. Otherwise, an SQLite error code is returned and the
5171 ** final value of (*ppEntry) undefined.
5172 */
5173 static int zipfileGetEntry(
5174   ZipfileTab *pTab,               /* Store any error message here */
5175   const u8 *aBlob,                /* Pointer to in-memory file image */
5176   int nBlob,                      /* Size of aBlob[] in bytes */
5177   FILE *pFile,                    /* If aBlob==0, read from this file */
5178   i64 iOff,                       /* Offset of CDS record */
5179   ZipfileEntry **ppEntry          /* OUT: Pointer to new object */
5180 ){
5181   u8 *aRead;
5182   char **pzErr = &pTab->base.zErrMsg;
5183   int rc = SQLITE_OK;
5184
5185   if( aBlob==0 ){
5186     aRead = pTab->aBuffer;
5187     rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
5188   }else{
5189     aRead = (u8*)&aBlob[iOff];
5190   }
5191
5192   if( rc==SQLITE_OK ){
5193     sqlite3_int64 nAlloc;
5194     ZipfileEntry *pNew;
5195
5196     int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
5197     int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
5198     nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
5199
5200     nAlloc = sizeof(ZipfileEntry) + nExtra;
5201     if( aBlob ){
5202       nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
5203     }
5204
5205     pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
5206     if( pNew==0 ){
5207       rc = SQLITE_NOMEM;
5208     }else{
5209       memset(pNew, 0, sizeof(ZipfileEntry));
5210       rc = zipfileReadCDS(aRead, &pNew->cds);
5211       if( rc!=SQLITE_OK ){
5212         *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
5213       }else if( aBlob==0 ){
5214         rc = zipfileReadData(
5215             pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
5216         );
5217       }else{
5218         aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
5219       }
5220     }
5221
5222     if( rc==SQLITE_OK ){
5223       u32 *pt = &pNew->mUnixTime;
5224       pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead); 
5225       pNew->aExtra = (u8*)&pNew[1];
5226       memcpy(pNew->aExtra, &aRead[nFile], nExtra);
5227       if( pNew->cds.zFile==0 ){
5228         rc = SQLITE_NOMEM;
5229       }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
5230         pNew->mUnixTime = zipfileMtime(&pNew->cds);
5231       }
5232     }
5233
5234     if( rc==SQLITE_OK ){
5235       static const int szFix = ZIPFILE_LFH_FIXED_SZ;
5236       ZipfileLFH lfh;
5237       if( pFile ){
5238         rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
5239       }else{
5240         aRead = (u8*)&aBlob[pNew->cds.iOffset];
5241       }
5242
5243       rc = zipfileReadLFH(aRead, &lfh);
5244       if( rc==SQLITE_OK ){
5245         pNew->iDataOff =  pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
5246         pNew->iDataOff += lfh.nFile + lfh.nExtra;
5247         if( aBlob && pNew->cds.szCompressed ){
5248           pNew->aData = &pNew->aExtra[nExtra];
5249           memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
5250         }
5251       }else{
5252         *pzErr = sqlite3_mprintf("failed to read LFH at offset %d", 
5253             (int)pNew->cds.iOffset
5254         );
5255       }
5256     }
5257
5258     if( rc!=SQLITE_OK ){
5259       zipfileEntryFree(pNew);
5260     }else{
5261       *ppEntry = pNew;
5262     }
5263   }
5264
5265   return rc;
5266 }
5267
5268 /*
5269 ** Advance an ZipfileCsr to its next row of output.
5270 */
5271 static int zipfileNext(sqlite3_vtab_cursor *cur){
5272   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5273   int rc = SQLITE_OK;
5274
5275   if( pCsr->pFile ){
5276     i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
5277     zipfileEntryFree(pCsr->pCurrent);
5278     pCsr->pCurrent = 0;
5279     if( pCsr->iNextOff>=iEof ){
5280       pCsr->bEof = 1;
5281     }else{
5282       ZipfileEntry *p = 0;
5283       ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
5284       rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
5285       if( rc==SQLITE_OK ){
5286         pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
5287         pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
5288       }
5289       pCsr->pCurrent = p;
5290     }
5291   }else{
5292     if( !pCsr->bNoop ){
5293       pCsr->pCurrent = pCsr->pCurrent->pNext;
5294     }
5295     if( pCsr->pCurrent==0 ){
5296       pCsr->bEof = 1;
5297     }
5298   }
5299
5300   pCsr->bNoop = 0;
5301   return rc;
5302 }
5303
5304 static void zipfileFree(void *p) { 
5305   sqlite3_free(p); 
5306 }
5307
5308 /*
5309 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
5310 ** size is nOut bytes. This function uncompresses the data and sets the
5311 ** return value in context pCtx to the result (a blob).
5312 **
5313 ** If an error occurs, an error code is left in pCtx instead.
5314 */
5315 static void zipfileInflate(
5316   sqlite3_context *pCtx,          /* Store result here */
5317   const u8 *aIn,                  /* Compressed data */
5318   int nIn,                        /* Size of buffer aIn[] in bytes */
5319   int nOut                        /* Expected output size */
5320 ){
5321   u8 *aRes = sqlite3_malloc(nOut);
5322   if( aRes==0 ){
5323     sqlite3_result_error_nomem(pCtx);
5324   }else{
5325     int err;
5326     z_stream str;
5327     memset(&str, 0, sizeof(str));
5328
5329     str.next_in = (Byte*)aIn;
5330     str.avail_in = nIn;
5331     str.next_out = (Byte*)aRes;
5332     str.avail_out = nOut;
5333
5334     err = inflateInit2(&str, -15);
5335     if( err!=Z_OK ){
5336       zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
5337     }else{
5338       err = inflate(&str, Z_NO_FLUSH);
5339       if( err!=Z_STREAM_END ){
5340         zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
5341       }else{
5342         sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
5343         aRes = 0;
5344       }
5345     }
5346     sqlite3_free(aRes);
5347     inflateEnd(&str);
5348   }
5349 }
5350
5351 /*
5352 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
5353 ** compresses it and sets (*ppOut) to point to a buffer containing the
5354 ** compressed data. The caller is responsible for eventually calling
5355 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut) 
5356 ** is set to the size of buffer (*ppOut) in bytes.
5357 **
5358 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
5359 ** code is returned and an error message left in virtual-table handle
5360 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
5361 ** case.
5362 */
5363 static int zipfileDeflate(
5364   const u8 *aIn, int nIn,         /* Input */
5365   u8 **ppOut, int *pnOut,         /* Output */
5366   char **pzErr                    /* OUT: Error message */
5367 ){
5368   int rc = SQLITE_OK;
5369   sqlite3_int64 nAlloc;
5370   z_stream str;
5371   u8 *aOut;
5372
5373   memset(&str, 0, sizeof(str));
5374   str.next_in = (Bytef*)aIn;
5375   str.avail_in = nIn;
5376   deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
5377
5378   nAlloc = deflateBound(&str, nIn);
5379   aOut = (u8*)sqlite3_malloc64(nAlloc);
5380   if( aOut==0 ){
5381     rc = SQLITE_NOMEM;
5382   }else{
5383     int res;
5384     str.next_out = aOut;
5385     str.avail_out = nAlloc;
5386     res = deflate(&str, Z_FINISH);
5387     if( res==Z_STREAM_END ){
5388       *ppOut = aOut;
5389       *pnOut = (int)str.total_out;
5390     }else{
5391       sqlite3_free(aOut);
5392       *pzErr = sqlite3_mprintf("zipfile: deflate() error");
5393       rc = SQLITE_ERROR;
5394     }
5395     deflateEnd(&str);
5396   }
5397
5398   return rc;
5399 }
5400
5401
5402 /*
5403 ** Return values of columns for the row at which the series_cursor
5404 ** is currently pointing.
5405 */
5406 static int zipfileColumn(
5407   sqlite3_vtab_cursor *cur,   /* The cursor */
5408   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
5409   int i                       /* Which column to return */
5410 ){
5411   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5412   ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
5413   int rc = SQLITE_OK;
5414   switch( i ){
5415     case 0:   /* name */
5416       sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
5417       break;
5418     case 1:   /* mode */
5419       /* TODO: Whether or not the following is correct surely depends on
5420       ** the platform on which the archive was created.  */
5421       sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
5422       break;
5423     case 2: { /* mtime */
5424       sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
5425       break;
5426     }
5427     case 3: { /* sz */
5428       if( sqlite3_vtab_nochange(ctx)==0 ){
5429         sqlite3_result_int64(ctx, pCDS->szUncompressed);
5430       }
5431       break;
5432     }
5433     case 4:   /* rawdata */
5434       if( sqlite3_vtab_nochange(ctx) ) break;
5435     case 5: { /* data */
5436       if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
5437         int sz = pCDS->szCompressed;
5438         int szFinal = pCDS->szUncompressed;
5439         if( szFinal>0 ){
5440           u8 *aBuf;
5441           u8 *aFree = 0;
5442           if( pCsr->pCurrent->aData ){
5443             aBuf = pCsr->pCurrent->aData;
5444           }else{
5445             aBuf = aFree = sqlite3_malloc64(sz);
5446             if( aBuf==0 ){
5447               rc = SQLITE_NOMEM;
5448             }else{
5449               FILE *pFile = pCsr->pFile;
5450               if( pFile==0 ){
5451                 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
5452               }
5453               rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
5454                   &pCsr->base.pVtab->zErrMsg
5455               );
5456             }
5457           }
5458           if( rc==SQLITE_OK ){
5459             if( i==5 && pCDS->iCompression ){
5460               zipfileInflate(ctx, aBuf, sz, szFinal);
5461             }else{
5462               sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
5463             }
5464           }
5465           sqlite3_free(aFree);
5466         }else{
5467           /* Figure out if this is a directory or a zero-sized file. Consider
5468           ** it to be a directory either if the mode suggests so, or if
5469           ** the final character in the name is '/'.  */
5470           u32 mode = pCDS->iExternalAttr >> 16;
5471           if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
5472             sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
5473           }
5474         }
5475       }
5476       break;
5477     }
5478     case 6:   /* method */
5479       sqlite3_result_int(ctx, pCDS->iCompression);
5480       break;
5481     default:  /* z */
5482       assert( i==7 );
5483       sqlite3_result_int64(ctx, pCsr->iId);
5484       break;
5485   }
5486
5487   return rc;
5488 }
5489
5490 /*
5491 ** Return TRUE if the cursor is at EOF.
5492 */
5493 static int zipfileEof(sqlite3_vtab_cursor *cur){
5494   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5495   return pCsr->bEof;
5496 }
5497
5498 /*
5499 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
5500 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
5501 ** is guaranteed to be a file-handle open on a zip file.
5502 **
5503 ** This function attempts to locate the EOCD record within the zip archive
5504 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
5505 ** returned if successful. Otherwise, an SQLite error code is returned and
5506 ** an English language error message may be left in virtual-table pTab.
5507 */
5508 static int zipfileReadEOCD(
5509   ZipfileTab *pTab,               /* Return errors here */
5510   const u8 *aBlob,                /* Pointer to in-memory file image */
5511   int nBlob,                      /* Size of aBlob[] in bytes */
5512   FILE *pFile,                    /* Read from this file if aBlob==0 */
5513   ZipfileEOCD *pEOCD              /* Object to populate */
5514 ){
5515   u8 *aRead = pTab->aBuffer;      /* Temporary buffer */
5516   int nRead;                      /* Bytes to read from file */
5517   int rc = SQLITE_OK;
5518
5519   if( aBlob==0 ){
5520     i64 iOff;                     /* Offset to read from */
5521     i64 szFile;                   /* Total size of file in bytes */
5522     fseek(pFile, 0, SEEK_END);
5523     szFile = (i64)ftell(pFile);
5524     if( szFile==0 ){
5525       memset(pEOCD, 0, sizeof(ZipfileEOCD));
5526       return SQLITE_OK;
5527     }
5528     nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
5529     iOff = szFile - nRead;
5530     rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
5531   }else{
5532     nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
5533     aRead = (u8*)&aBlob[nBlob-nRead];
5534   }
5535
5536   if( rc==SQLITE_OK ){
5537     int i;
5538
5539     /* Scan backwards looking for the signature bytes */
5540     for(i=nRead-20; i>=0; i--){
5541       if( aRead[i]==0x50 && aRead[i+1]==0x4b 
5542        && aRead[i+2]==0x05 && aRead[i+3]==0x06 
5543       ){
5544         break;
5545       }
5546     }
5547     if( i<0 ){
5548       pTab->base.zErrMsg = sqlite3_mprintf(
5549           "cannot find end of central directory record"
5550       );
5551       return SQLITE_ERROR;
5552     }
5553
5554     aRead += i+4;
5555     pEOCD->iDisk = zipfileRead16(aRead);
5556     pEOCD->iFirstDisk = zipfileRead16(aRead);
5557     pEOCD->nEntry = zipfileRead16(aRead);
5558     pEOCD->nEntryTotal = zipfileRead16(aRead);
5559     pEOCD->nSize = zipfileRead32(aRead);
5560     pEOCD->iOffset = zipfileRead32(aRead);
5561   }
5562
5563   return rc;
5564 }
5565
5566 /*
5567 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry 
5568 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
5569 ** to the end of the list. Otherwise, it is added to the list immediately
5570 ** before pBefore (which is guaranteed to be a part of said list).
5571 */
5572 static void zipfileAddEntry(
5573   ZipfileTab *pTab, 
5574   ZipfileEntry *pBefore, 
5575   ZipfileEntry *pNew
5576 ){
5577   assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
5578   assert( pNew->pNext==0 );
5579   if( pBefore==0 ){
5580     if( pTab->pFirstEntry==0 ){
5581       pTab->pFirstEntry = pTab->pLastEntry = pNew;
5582     }else{
5583       assert( pTab->pLastEntry->pNext==0 );
5584       pTab->pLastEntry->pNext = pNew;
5585       pTab->pLastEntry = pNew;
5586     }
5587   }else{
5588     ZipfileEntry **pp;
5589     for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
5590     pNew->pNext = pBefore;
5591     *pp = pNew;
5592   }
5593 }
5594
5595 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
5596   ZipfileEOCD eocd;
5597   int rc;
5598   int i;
5599   i64 iOff;
5600
5601   rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
5602   iOff = eocd.iOffset;
5603   for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
5604     ZipfileEntry *pNew = 0;
5605     rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
5606
5607     if( rc==SQLITE_OK ){
5608       zipfileAddEntry(pTab, 0, pNew);
5609       iOff += ZIPFILE_CDS_FIXED_SZ;
5610       iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
5611     }
5612   }
5613   return rc;
5614 }
5615
5616 /*
5617 ** xFilter callback.
5618 */
5619 static int zipfileFilter(
5620   sqlite3_vtab_cursor *cur, 
5621   int idxNum, const char *idxStr,
5622   int argc, sqlite3_value **argv
5623 ){
5624   ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
5625   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5626   const char *zFile = 0;          /* Zip file to scan */
5627   int rc = SQLITE_OK;             /* Return Code */
5628   int bInMemory = 0;              /* True for an in-memory zipfile */
5629
5630   zipfileResetCursor(pCsr);
5631
5632   if( pTab->zFile ){
5633     zFile = pTab->zFile;
5634   }else if( idxNum==0 ){
5635     zipfileCursorErr(pCsr, "zipfile() function requires an argument");
5636     return SQLITE_ERROR;
5637   }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
5638     const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
5639     int nBlob = sqlite3_value_bytes(argv[0]);
5640     assert( pTab->pFirstEntry==0 );
5641     rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
5642     pCsr->pFreeEntry = pTab->pFirstEntry;
5643     pTab->pFirstEntry = pTab->pLastEntry = 0;
5644     if( rc!=SQLITE_OK ) return rc;
5645     bInMemory = 1;
5646   }else{
5647     zFile = (const char*)sqlite3_value_text(argv[0]);
5648   }
5649
5650   if( 0==pTab->pWriteFd && 0==bInMemory ){
5651     pCsr->pFile = fopen(zFile, "rb");
5652     if( pCsr->pFile==0 ){
5653       zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
5654       rc = SQLITE_ERROR;
5655     }else{
5656       rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
5657       if( rc==SQLITE_OK ){
5658         if( pCsr->eocd.nEntry==0 ){
5659           pCsr->bEof = 1;
5660         }else{
5661           pCsr->iNextOff = pCsr->eocd.iOffset;
5662           rc = zipfileNext(cur);
5663         }
5664       }
5665     }
5666   }else{
5667     pCsr->bNoop = 1;
5668     pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
5669     rc = zipfileNext(cur);
5670   }
5671
5672   return rc;
5673 }
5674
5675 /*
5676 ** xBestIndex callback.
5677 */
5678 static int zipfileBestIndex(
5679   sqlite3_vtab *tab,
5680   sqlite3_index_info *pIdxInfo
5681 ){
5682   int i;
5683   int idx = -1;
5684   int unusable = 0;
5685
5686   for(i=0; i<pIdxInfo->nConstraint; i++){
5687     const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
5688     if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
5689     if( pCons->usable==0 ){
5690       unusable = 1;
5691     }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5692       idx = i;
5693     }
5694   }
5695   pIdxInfo->estimatedCost = 1000.0;
5696   if( idx>=0 ){
5697     pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
5698     pIdxInfo->aConstraintUsage[idx].omit = 1;
5699     pIdxInfo->idxNum = 1;
5700   }else if( unusable ){
5701     return SQLITE_CONSTRAINT;
5702   }
5703   return SQLITE_OK;
5704 }
5705
5706 static ZipfileEntry *zipfileNewEntry(const char *zPath){
5707   ZipfileEntry *pNew;
5708   pNew = sqlite3_malloc(sizeof(ZipfileEntry));
5709   if( pNew ){
5710     memset(pNew, 0, sizeof(ZipfileEntry));
5711     pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
5712     if( pNew->cds.zFile==0 ){
5713       sqlite3_free(pNew);
5714       pNew = 0;
5715     }
5716   }
5717   return pNew;
5718 }
5719
5720 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
5721   ZipfileCDS *pCds = &pEntry->cds;
5722   u8 *a = aBuf;
5723
5724   pCds->nExtra = 9;
5725
5726   /* Write the LFH itself */
5727   zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
5728   zipfileWrite16(a, pCds->iVersionExtract);
5729   zipfileWrite16(a, pCds->flags);
5730   zipfileWrite16(a, pCds->iCompression);
5731   zipfileWrite16(a, pCds->mTime);
5732   zipfileWrite16(a, pCds->mDate);
5733   zipfileWrite32(a, pCds->crc32);
5734   zipfileWrite32(a, pCds->szCompressed);
5735   zipfileWrite32(a, pCds->szUncompressed);
5736   zipfileWrite16(a, (u16)pCds->nFile);
5737   zipfileWrite16(a, pCds->nExtra);
5738   assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
5739
5740   /* Add the file name */
5741   memcpy(a, pCds->zFile, (int)pCds->nFile);
5742   a += (int)pCds->nFile;
5743
5744   /* The "extra" data */
5745   zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5746   zipfileWrite16(a, 5);
5747   *a++ = 0x01;
5748   zipfileWrite32(a, pEntry->mUnixTime);
5749
5750   return a-aBuf;
5751 }
5752
5753 static int zipfileAppendEntry(
5754   ZipfileTab *pTab,
5755   ZipfileEntry *pEntry,
5756   const u8 *pData,
5757   int nData
5758 ){
5759   u8 *aBuf = pTab->aBuffer;
5760   int nBuf;
5761   int rc;
5762
5763   nBuf = zipfileSerializeLFH(pEntry, aBuf);
5764   rc = zipfileAppendData(pTab, aBuf, nBuf);
5765   if( rc==SQLITE_OK ){
5766     pEntry->iDataOff = pTab->szCurrent;
5767     rc = zipfileAppendData(pTab, pData, nData);
5768   }
5769
5770   return rc;
5771 }
5772
5773 static int zipfileGetMode(
5774   sqlite3_value *pVal, 
5775   int bIsDir,                     /* If true, default to directory */
5776   u32 *pMode,                     /* OUT: Mode value */
5777   char **pzErr                    /* OUT: Error message */
5778 ){
5779   const char *z = (const char*)sqlite3_value_text(pVal);
5780   u32 mode = 0;
5781   if( z==0 ){
5782     mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
5783   }else if( z[0]>='0' && z[0]<='9' ){
5784     mode = (unsigned int)sqlite3_value_int(pVal);
5785   }else{
5786     const char zTemplate[11] = "-rwxrwxrwx";
5787     int i;
5788     if( strlen(z)!=10 ) goto parse_error;
5789     switch( z[0] ){
5790       case '-': mode |= S_IFREG; break;
5791       case 'd': mode |= S_IFDIR; break;
5792       case 'l': mode |= S_IFLNK; break;
5793       default: goto parse_error;
5794     }
5795     for(i=1; i<10; i++){
5796       if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
5797       else if( z[i]!='-' ) goto parse_error;
5798     }
5799   }
5800   if( ((mode & S_IFDIR)==0)==bIsDir ){
5801     /* The "mode" attribute is a directory, but data has been specified.
5802     ** Or vice-versa - no data but "mode" is a file or symlink.  */
5803     *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
5804     return SQLITE_CONSTRAINT;
5805   }
5806   *pMode = mode;
5807   return SQLITE_OK;
5808
5809  parse_error:
5810   *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
5811   return SQLITE_ERROR;
5812 }
5813
5814 /*
5815 ** Both (const char*) arguments point to nul-terminated strings. Argument
5816 ** nB is the value of strlen(zB). This function returns 0 if the strings are
5817 ** identical, ignoring any trailing '/' character in either path.  */
5818 static int zipfileComparePath(const char *zA, const char *zB, int nB){
5819   int nA = (int)strlen(zA);
5820   if( nA>0 && zA[nA-1]=='/' ) nA--;
5821   if( nB>0 && zB[nB-1]=='/' ) nB--;
5822   if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
5823   return 1;
5824 }
5825
5826 static int zipfileBegin(sqlite3_vtab *pVtab){
5827   ZipfileTab *pTab = (ZipfileTab*)pVtab;
5828   int rc = SQLITE_OK;
5829
5830   assert( pTab->pWriteFd==0 );
5831   if( pTab->zFile==0 || pTab->zFile[0]==0 ){
5832     pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename");
5833     return SQLITE_ERROR;
5834   }
5835
5836   /* Open a write fd on the file. Also load the entire central directory
5837   ** structure into memory. During the transaction any new file data is 
5838   ** appended to the archive file, but the central directory is accumulated
5839   ** in main-memory until the transaction is committed.  */
5840   pTab->pWriteFd = fopen(pTab->zFile, "ab+");
5841   if( pTab->pWriteFd==0 ){
5842     pTab->base.zErrMsg = sqlite3_mprintf(
5843         "zipfile: failed to open file %s for writing", pTab->zFile
5844         );
5845     rc = SQLITE_ERROR;
5846   }else{
5847     fseek(pTab->pWriteFd, 0, SEEK_END);
5848     pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
5849     rc = zipfileLoadDirectory(pTab, 0, 0);
5850   }
5851
5852   if( rc!=SQLITE_OK ){
5853     zipfileCleanupTransaction(pTab);
5854   }
5855
5856   return rc;
5857 }
5858
5859 /*
5860 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
5861 ** time(2)).
5862 */
5863 static u32 zipfileTime(void){
5864   sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
5865   u32 ret;
5866   if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
5867     i64 ms;
5868     pVfs->xCurrentTimeInt64(pVfs, &ms);
5869     ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
5870   }else{
5871     double day;
5872     pVfs->xCurrentTime(pVfs, &day);
5873     ret = (u32)((day - 2440587.5) * 86400);
5874   }
5875   return ret;
5876 }
5877
5878 /*
5879 ** Return a 32-bit timestamp in UNIX epoch format.
5880 **
5881 ** If the value passed as the only argument is either NULL or an SQL NULL,
5882 ** return the current time. Otherwise, return the value stored in (*pVal)
5883 ** cast to a 32-bit unsigned integer.
5884 */
5885 static u32 zipfileGetTime(sqlite3_value *pVal){
5886   if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
5887     return zipfileTime();
5888   }
5889   return (u32)sqlite3_value_int64(pVal);
5890 }
5891
5892 /*
5893 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
5894 ** linked list.  Remove it from the list and free the object.
5895 */
5896 static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
5897   if( pOld ){
5898     ZipfileEntry **pp;
5899     for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
5900     *pp = (*pp)->pNext;
5901     zipfileEntryFree(pOld);
5902   }
5903 }
5904
5905 /*
5906 ** xUpdate method.
5907 */
5908 static int zipfileUpdate(
5909   sqlite3_vtab *pVtab, 
5910   int nVal, 
5911   sqlite3_value **apVal, 
5912   sqlite_int64 *pRowid
5913 ){
5914   ZipfileTab *pTab = (ZipfileTab*)pVtab;
5915   int rc = SQLITE_OK;             /* Return Code */
5916   ZipfileEntry *pNew = 0;         /* New in-memory CDS entry */
5917
5918   u32 mode = 0;                   /* Mode for new entry */
5919   u32 mTime = 0;                  /* Modification time for new entry */
5920   i64 sz = 0;                     /* Uncompressed size */
5921   const char *zPath = 0;          /* Path for new entry */
5922   int nPath = 0;                  /* strlen(zPath) */
5923   const u8 *pData = 0;            /* Pointer to buffer containing content */
5924   int nData = 0;                  /* Size of pData buffer in bytes */
5925   int iMethod = 0;                /* Compression method for new entry */
5926   u8 *pFree = 0;                  /* Free this */
5927   char *zFree = 0;                /* Also free this */
5928   ZipfileEntry *pOld = 0;
5929   ZipfileEntry *pOld2 = 0;
5930   int bUpdate = 0;                /* True for an update that modifies "name" */
5931   int bIsDir = 0;
5932   u32 iCrc32 = 0;
5933
5934   if( pTab->pWriteFd==0 ){
5935     rc = zipfileBegin(pVtab);
5936     if( rc!=SQLITE_OK ) return rc;
5937   }
5938
5939   /* If this is a DELETE or UPDATE, find the archive entry to delete. */
5940   if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
5941     const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
5942     int nDelete = (int)strlen(zDelete);
5943     if( nVal>1 ){
5944       const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
5945       if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
5946         bUpdate = 1;
5947       }
5948     }
5949     for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
5950       if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
5951         break;
5952       }
5953       assert( pOld->pNext );
5954     }
5955   }
5956
5957   if( nVal>1 ){
5958     /* Check that "sz" and "rawdata" are both NULL: */
5959     if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
5960       zipfileTableErr(pTab, "sz must be NULL");
5961       rc = SQLITE_CONSTRAINT;
5962     }
5963     if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
5964       zipfileTableErr(pTab, "rawdata must be NULL"); 
5965       rc = SQLITE_CONSTRAINT;
5966     }
5967
5968     if( rc==SQLITE_OK ){
5969       if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
5970         /* data=NULL. A directory */
5971         bIsDir = 1;
5972       }else{
5973         /* Value specified for "data", and possibly "method". This must be
5974         ** a regular file or a symlink. */
5975         const u8 *aIn = sqlite3_value_blob(apVal[7]);
5976         int nIn = sqlite3_value_bytes(apVal[7]);
5977         int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
5978
5979         iMethod = sqlite3_value_int(apVal[8]);
5980         sz = nIn;
5981         pData = aIn;
5982         nData = nIn;
5983         if( iMethod!=0 && iMethod!=8 ){
5984           zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
5985           rc = SQLITE_CONSTRAINT;
5986         }else{
5987           if( bAuto || iMethod ){
5988             int nCmp;
5989             rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
5990             if( rc==SQLITE_OK ){
5991               if( iMethod || nCmp<nIn ){
5992                 iMethod = 8;
5993                 pData = pFree;
5994                 nData = nCmp;
5995               }
5996             }
5997           }
5998           iCrc32 = crc32(0, aIn, nIn);
5999         }
6000       }
6001     }
6002
6003     if( rc==SQLITE_OK ){
6004       rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
6005     }
6006
6007     if( rc==SQLITE_OK ){
6008       zPath = (const char*)sqlite3_value_text(apVal[2]);
6009       if( zPath==0 ) zPath = "";
6010       nPath = (int)strlen(zPath);
6011       mTime = zipfileGetTime(apVal[4]);
6012     }
6013
6014     if( rc==SQLITE_OK && bIsDir ){
6015       /* For a directory, check that the last character in the path is a
6016       ** '/'. This appears to be required for compatibility with info-zip
6017       ** (the unzip command on unix). It does not create directories
6018       ** otherwise.  */
6019       if( nPath<=0 || zPath[nPath-1]!='/' ){
6020         zFree = sqlite3_mprintf("%s/", zPath);
6021         zPath = (const char*)zFree;
6022         if( zFree==0 ){
6023           rc = SQLITE_NOMEM;
6024           nPath = 0;
6025         }else{
6026           nPath = (int)strlen(zPath);
6027         }
6028       }
6029     }
6030
6031     /* Check that we're not inserting a duplicate entry -OR- updating an
6032     ** entry with a path, thereby making it into a duplicate. */
6033     if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
6034       ZipfileEntry *p;
6035       for(p=pTab->pFirstEntry; p; p=p->pNext){
6036         if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
6037           switch( sqlite3_vtab_on_conflict(pTab->db) ){
6038             case SQLITE_IGNORE: {
6039               goto zipfile_update_done;
6040             }
6041             case SQLITE_REPLACE: {
6042               pOld2 = p;
6043               break;
6044             }
6045             default: {
6046               zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
6047               rc = SQLITE_CONSTRAINT;
6048               break;
6049             }
6050           }
6051           break;
6052         }
6053       }
6054     }
6055
6056     if( rc==SQLITE_OK ){
6057       /* Create the new CDS record. */
6058       pNew = zipfileNewEntry(zPath);
6059       if( pNew==0 ){
6060         rc = SQLITE_NOMEM;
6061       }else{
6062         pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
6063         pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
6064         pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
6065         pNew->cds.iCompression = (u16)iMethod;
6066         zipfileMtimeToDos(&pNew->cds, mTime);
6067         pNew->cds.crc32 = iCrc32;
6068         pNew->cds.szCompressed = nData;
6069         pNew->cds.szUncompressed = (u32)sz;
6070         pNew->cds.iExternalAttr = (mode<<16);
6071         pNew->cds.iOffset = (u32)pTab->szCurrent;
6072         pNew->cds.nFile = (u16)nPath;
6073         pNew->mUnixTime = (u32)mTime;
6074         rc = zipfileAppendEntry(pTab, pNew, pData, nData);
6075         zipfileAddEntry(pTab, pOld, pNew);
6076       }
6077     }
6078   }
6079
6080   if( rc==SQLITE_OK && (pOld || pOld2) ){
6081     ZipfileCsr *pCsr;
6082     for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
6083       if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
6084         pCsr->pCurrent = pCsr->pCurrent->pNext;
6085         pCsr->bNoop = 1;
6086       }
6087     }
6088
6089     zipfileRemoveEntryFromList(pTab, pOld);
6090     zipfileRemoveEntryFromList(pTab, pOld2);
6091   }
6092
6093 zipfile_update_done:
6094   sqlite3_free(pFree);
6095   sqlite3_free(zFree);
6096   return rc;
6097 }
6098
6099 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
6100   u8 *a = aBuf;
6101   zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
6102   zipfileWrite16(a, p->iDisk);
6103   zipfileWrite16(a, p->iFirstDisk);
6104   zipfileWrite16(a, p->nEntry);
6105   zipfileWrite16(a, p->nEntryTotal);
6106   zipfileWrite32(a, p->nSize);
6107   zipfileWrite32(a, p->iOffset);
6108   zipfileWrite16(a, 0);        /* Size of trailing comment in bytes*/
6109
6110   return a-aBuf;
6111 }
6112
6113 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
6114   int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
6115   assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
6116   return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
6117 }
6118
6119 /*
6120 ** Serialize the CDS structure into buffer aBuf[]. Return the number
6121 ** of bytes written.
6122 */
6123 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
6124   u8 *a = aBuf;
6125   ZipfileCDS *pCDS = &pEntry->cds;
6126
6127   if( pEntry->aExtra==0 ){
6128     pCDS->nExtra = 9;
6129   }
6130
6131   zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
6132   zipfileWrite16(a, pCDS->iVersionMadeBy);
6133   zipfileWrite16(a, pCDS->iVersionExtract);
6134   zipfileWrite16(a, pCDS->flags);
6135   zipfileWrite16(a, pCDS->iCompression);
6136   zipfileWrite16(a, pCDS->mTime);
6137   zipfileWrite16(a, pCDS->mDate);
6138   zipfileWrite32(a, pCDS->crc32);
6139   zipfileWrite32(a, pCDS->szCompressed);
6140   zipfileWrite32(a, pCDS->szUncompressed);
6141   assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
6142   zipfileWrite16(a, pCDS->nFile);
6143   zipfileWrite16(a, pCDS->nExtra);
6144   zipfileWrite16(a, pCDS->nComment);
6145   zipfileWrite16(a, pCDS->iDiskStart);
6146   zipfileWrite16(a, pCDS->iInternalAttr);
6147   zipfileWrite32(a, pCDS->iExternalAttr);
6148   zipfileWrite32(a, pCDS->iOffset);
6149
6150   memcpy(a, pCDS->zFile, pCDS->nFile);
6151   a += pCDS->nFile;
6152
6153   if( pEntry->aExtra ){
6154     int n = (int)pCDS->nExtra + (int)pCDS->nComment;
6155     memcpy(a, pEntry->aExtra, n);
6156     a += n;
6157   }else{
6158     assert( pCDS->nExtra==9 );
6159     zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
6160     zipfileWrite16(a, 5);
6161     *a++ = 0x01;
6162     zipfileWrite32(a, pEntry->mUnixTime);
6163   }
6164
6165   return a-aBuf;
6166 }
6167
6168 static int zipfileCommit(sqlite3_vtab *pVtab){
6169   ZipfileTab *pTab = (ZipfileTab*)pVtab;
6170   int rc = SQLITE_OK;
6171   if( pTab->pWriteFd ){
6172     i64 iOffset = pTab->szCurrent;
6173     ZipfileEntry *p;
6174     ZipfileEOCD eocd;
6175     int nEntry = 0;
6176
6177     /* Write out all entries */
6178     for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
6179       int n = zipfileSerializeCDS(p, pTab->aBuffer);
6180       rc = zipfileAppendData(pTab, pTab->aBuffer, n);
6181       nEntry++;
6182     }
6183
6184     /* Write out the EOCD record */
6185     eocd.iDisk = 0;
6186     eocd.iFirstDisk = 0;
6187     eocd.nEntry = (u16)nEntry;
6188     eocd.nEntryTotal = (u16)nEntry;
6189     eocd.nSize = (u32)(pTab->szCurrent - iOffset);
6190     eocd.iOffset = (u32)iOffset;
6191     rc = zipfileAppendEOCD(pTab, &eocd);
6192
6193     zipfileCleanupTransaction(pTab);
6194   }
6195   return rc;
6196 }
6197
6198 static int zipfileRollback(sqlite3_vtab *pVtab){
6199   return zipfileCommit(pVtab);
6200 }
6201
6202 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
6203   ZipfileCsr *pCsr;
6204   for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
6205     if( iId==pCsr->iId ) break;
6206   }
6207   return pCsr;
6208 }
6209
6210 static void zipfileFunctionCds(
6211   sqlite3_context *context,
6212   int argc,
6213   sqlite3_value **argv
6214 ){
6215   ZipfileCsr *pCsr;
6216   ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
6217   assert( argc>0 );
6218
6219   pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
6220   if( pCsr ){
6221     ZipfileCDS *p = &pCsr->pCurrent->cds;
6222     char *zRes = sqlite3_mprintf("{"
6223         "\"version-made-by\" : %u, "
6224         "\"version-to-extract\" : %u, "
6225         "\"flags\" : %u, "
6226         "\"compression\" : %u, "
6227         "\"time\" : %u, "
6228         "\"date\" : %u, "
6229         "\"crc32\" : %u, "
6230         "\"compressed-size\" : %u, "
6231         "\"uncompressed-size\" : %u, "
6232         "\"file-name-length\" : %u, "
6233         "\"extra-field-length\" : %u, "
6234         "\"file-comment-length\" : %u, "
6235         "\"disk-number-start\" : %u, "
6236         "\"internal-attr\" : %u, "
6237         "\"external-attr\" : %u, "
6238         "\"offset\" : %u }",
6239         (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
6240         (u32)p->flags, (u32)p->iCompression,
6241         (u32)p->mTime, (u32)p->mDate,
6242         (u32)p->crc32, (u32)p->szCompressed,
6243         (u32)p->szUncompressed, (u32)p->nFile,
6244         (u32)p->nExtra, (u32)p->nComment,
6245         (u32)p->iDiskStart, (u32)p->iInternalAttr,
6246         (u32)p->iExternalAttr, (u32)p->iOffset
6247     );
6248
6249     if( zRes==0 ){
6250       sqlite3_result_error_nomem(context);
6251     }else{
6252       sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
6253       sqlite3_free(zRes);
6254     }
6255   }
6256 }
6257
6258 /*
6259 ** xFindFunction method.
6260 */
6261 static int zipfileFindFunction(
6262   sqlite3_vtab *pVtab,            /* Virtual table handle */
6263   int nArg,                       /* Number of SQL function arguments */
6264   const char *zName,              /* Name of SQL function */
6265   void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
6266   void **ppArg                    /* OUT: User data for *pxFunc */
6267 ){
6268   if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
6269     *pxFunc = zipfileFunctionCds;
6270     *ppArg = (void*)pVtab;
6271     return 1;
6272   }
6273   return 0;
6274 }
6275
6276 typedef struct ZipfileBuffer ZipfileBuffer;
6277 struct ZipfileBuffer {
6278   u8 *a;                          /* Pointer to buffer */
6279   int n;                          /* Size of buffer in bytes */
6280   int nAlloc;                     /* Byte allocated at a[] */
6281 };
6282
6283 typedef struct ZipfileCtx ZipfileCtx;
6284 struct ZipfileCtx {
6285   int nEntry;
6286   ZipfileBuffer body;
6287   ZipfileBuffer cds;
6288 };
6289
6290 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
6291   if( pBuf->n+nByte>pBuf->nAlloc ){
6292     u8 *aNew;
6293     sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
6294     int nReq = pBuf->n + nByte;
6295
6296     while( nNew<nReq ) nNew = nNew*2;
6297     aNew = sqlite3_realloc64(pBuf->a, nNew);
6298     if( aNew==0 ) return SQLITE_NOMEM;
6299     pBuf->a = aNew;
6300     pBuf->nAlloc = (int)nNew;
6301   }
6302   return SQLITE_OK;
6303 }
6304
6305 /*
6306 ** xStep() callback for the zipfile() aggregate. This can be called in
6307 ** any of the following ways:
6308 **
6309 **   SELECT zipfile(name,data) ...
6310 **   SELECT zipfile(name,mode,mtime,data) ...
6311 **   SELECT zipfile(name,mode,mtime,data,method) ...
6312 */
6313 void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
6314   ZipfileCtx *p;                  /* Aggregate function context */
6315   ZipfileEntry e;                 /* New entry to add to zip archive */
6316
6317   sqlite3_value *pName = 0;
6318   sqlite3_value *pMode = 0;
6319   sqlite3_value *pMtime = 0;
6320   sqlite3_value *pData = 0;
6321   sqlite3_value *pMethod = 0;
6322
6323   int bIsDir = 0;
6324   u32 mode;
6325   int rc = SQLITE_OK;
6326   char *zErr = 0;
6327
6328   int iMethod = -1;               /* Compression method to use (0 or 8) */
6329
6330   const u8 *aData = 0;            /* Possibly compressed data for new entry */
6331   int nData = 0;                  /* Size of aData[] in bytes */
6332   int szUncompressed = 0;         /* Size of data before compression */
6333   u8 *aFree = 0;                  /* Free this before returning */
6334   u32 iCrc32 = 0;                 /* crc32 of uncompressed data */
6335
6336   char *zName = 0;                /* Path (name) of new entry */
6337   int nName = 0;                  /* Size of zName in bytes */
6338   char *zFree = 0;                /* Free this before returning */
6339   int nByte;
6340
6341   memset(&e, 0, sizeof(e));
6342   p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
6343   if( p==0 ) return;
6344
6345   /* Martial the arguments into stack variables */
6346   if( nVal!=2 && nVal!=4 && nVal!=5 ){
6347     zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
6348     rc = SQLITE_ERROR;
6349     goto zipfile_step_out;
6350   }
6351   pName = apVal[0];
6352   if( nVal==2 ){
6353     pData = apVal[1];
6354   }else{
6355     pMode = apVal[1];
6356     pMtime = apVal[2];
6357     pData = apVal[3];
6358     if( nVal==5 ){
6359       pMethod = apVal[4];
6360     }
6361   }
6362
6363   /* Check that the 'name' parameter looks ok. */
6364   zName = (char*)sqlite3_value_text(pName);
6365   nName = sqlite3_value_bytes(pName);
6366   if( zName==0 ){
6367     zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
6368     rc = SQLITE_ERROR;
6369     goto zipfile_step_out;
6370   }
6371
6372   /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
6373   ** deflate compression) or NULL (choose automatically).  */
6374   if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
6375     iMethod = (int)sqlite3_value_int64(pMethod);
6376     if( iMethod!=0 && iMethod!=8 ){
6377       zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
6378       rc = SQLITE_ERROR;
6379       goto zipfile_step_out;
6380     }
6381   }
6382
6383   /* Now inspect the data. If this is NULL, then the new entry must be a
6384   ** directory.  Otherwise, figure out whether or not the data should
6385   ** be deflated or simply stored in the zip archive. */
6386   if( sqlite3_value_type(pData)==SQLITE_NULL ){
6387     bIsDir = 1;
6388     iMethod = 0;
6389   }else{
6390     aData = sqlite3_value_blob(pData);
6391     szUncompressed = nData = sqlite3_value_bytes(pData);
6392     iCrc32 = crc32(0, aData, nData);
6393     if( iMethod<0 || iMethod==8 ){
6394       int nOut = 0;
6395       rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
6396       if( rc!=SQLITE_OK ){
6397         goto zipfile_step_out;
6398       }
6399       if( iMethod==8 || nOut<nData ){
6400         aData = aFree;
6401         nData = nOut;
6402         iMethod = 8;
6403       }else{
6404         iMethod = 0;
6405       }
6406     }
6407   }
6408
6409   /* Decode the "mode" argument. */
6410   rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
6411   if( rc ) goto zipfile_step_out;
6412
6413   /* Decode the "mtime" argument. */
6414   e.mUnixTime = zipfileGetTime(pMtime);
6415
6416   /* If this is a directory entry, ensure that there is exactly one '/'
6417   ** at the end of the path. Or, if this is not a directory and the path
6418   ** ends in '/' it is an error. */
6419   if( bIsDir==0 ){
6420     if( nName>0 && zName[nName-1]=='/' ){
6421       zErr = sqlite3_mprintf("non-directory name must not end with /");
6422       rc = SQLITE_ERROR;
6423       goto zipfile_step_out;
6424     }
6425   }else{
6426     if( nName==0 || zName[nName-1]!='/' ){
6427       zName = zFree = sqlite3_mprintf("%s/", zName);
6428       if( zName==0 ){
6429         rc = SQLITE_NOMEM;
6430         goto zipfile_step_out;
6431       }
6432       nName = (int)strlen(zName);
6433     }else{
6434       while( nName>1 && zName[nName-2]=='/' ) nName--;
6435     }
6436   }
6437
6438   /* Assemble the ZipfileEntry object for the new zip archive entry */
6439   e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
6440   e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
6441   e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
6442   e.cds.iCompression = (u16)iMethod;
6443   zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
6444   e.cds.crc32 = iCrc32;
6445   e.cds.szCompressed = nData;
6446   e.cds.szUncompressed = szUncompressed;
6447   e.cds.iExternalAttr = (mode<<16);
6448   e.cds.iOffset = p->body.n;
6449   e.cds.nFile = (u16)nName;
6450   e.cds.zFile = zName;
6451
6452   /* Append the LFH to the body of the new archive */
6453   nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
6454   if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
6455   p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
6456
6457   /* Append the data to the body of the new archive */
6458   if( nData>0 ){
6459     if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
6460     memcpy(&p->body.a[p->body.n], aData, nData);
6461     p->body.n += nData;
6462   }
6463
6464   /* Append the CDS record to the directory of the new archive */
6465   nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
6466   if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
6467   p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
6468
6469   /* Increment the count of entries in the archive */
6470   p->nEntry++;
6471
6472  zipfile_step_out:
6473   sqlite3_free(aFree);
6474   sqlite3_free(zFree);
6475   if( rc ){
6476     if( zErr ){
6477       sqlite3_result_error(pCtx, zErr, -1);
6478     }else{
6479       sqlite3_result_error_code(pCtx, rc);
6480     }
6481   }
6482   sqlite3_free(zErr);
6483 }
6484
6485 /*
6486 ** xFinalize() callback for zipfile aggregate function.
6487 */
6488 void zipfileFinal(sqlite3_context *pCtx){
6489   ZipfileCtx *p;
6490   ZipfileEOCD eocd;
6491   sqlite3_int64 nZip;
6492   u8 *aZip;
6493
6494   p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
6495   if( p==0 ) return;
6496   if( p->nEntry>0 ){
6497     memset(&eocd, 0, sizeof(eocd));
6498     eocd.nEntry = (u16)p->nEntry;
6499     eocd.nEntryTotal = (u16)p->nEntry;
6500     eocd.nSize = p->cds.n;
6501     eocd.iOffset = p->body.n;
6502
6503     nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
6504     aZip = (u8*)sqlite3_malloc64(nZip);
6505     if( aZip==0 ){
6506       sqlite3_result_error_nomem(pCtx);
6507     }else{
6508       memcpy(aZip, p->body.a, p->body.n);
6509       memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
6510       zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
6511       sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
6512     }
6513   }
6514
6515   sqlite3_free(p->body.a);
6516   sqlite3_free(p->cds.a);
6517 }
6518
6519
6520 /*
6521 ** Register the "zipfile" virtual table.
6522 */
6523 static int zipfileRegister(sqlite3 *db){
6524   static sqlite3_module zipfileModule = {
6525     1,                         /* iVersion */
6526     zipfileConnect,            /* xCreate */
6527     zipfileConnect,            /* xConnect */
6528     zipfileBestIndex,          /* xBestIndex */
6529     zipfileDisconnect,         /* xDisconnect */
6530     zipfileDisconnect,         /* xDestroy */
6531     zipfileOpen,               /* xOpen - open a cursor */
6532     zipfileClose,              /* xClose - close a cursor */
6533     zipfileFilter,             /* xFilter - configure scan constraints */
6534     zipfileNext,               /* xNext - advance a cursor */
6535     zipfileEof,                /* xEof - check for end of scan */
6536     zipfileColumn,             /* xColumn - read data */
6537     0,                         /* xRowid - read data */
6538     zipfileUpdate,             /* xUpdate */
6539     zipfileBegin,              /* xBegin */
6540     0,                         /* xSync */
6541     zipfileCommit,             /* xCommit */
6542     zipfileRollback,           /* xRollback */
6543     zipfileFindFunction,       /* xFindMethod */
6544     0,                         /* xRename */
6545   };
6546
6547   int rc = sqlite3_create_module(db, "zipfile"  , &zipfileModule, 0);
6548   if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
6549   if( rc==SQLITE_OK ){
6550     rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0, 
6551         zipfileStep, zipfileFinal
6552     );
6553   }
6554   return rc;
6555 }
6556 #else         /* SQLITE_OMIT_VIRTUALTABLE */
6557 # define zipfileRegister(x) SQLITE_OK
6558 #endif
6559
6560 #ifdef _WIN32
6561
6562 #endif
6563 int sqlite3_zipfile_init(
6564   sqlite3 *db, 
6565   char **pzErrMsg, 
6566   const sqlite3_api_routines *pApi
6567 ){
6568   SQLITE_EXTENSION_INIT2(pApi);
6569   (void)pzErrMsg;  /* Unused parameter */
6570   return zipfileRegister(db);
6571 }
6572
6573 /************************* End ../ext/misc/zipfile.c ********************/
6574 /************************* Begin ../ext/misc/sqlar.c ******************/
6575 /*
6576 ** 2017-12-17
6577 **
6578 ** The author disclaims copyright to this source code.  In place of
6579 ** a legal notice, here is a blessing:
6580 **
6581 **    May you do good and not evil.
6582 **    May you find forgiveness for yourself and forgive others.
6583 **    May you share freely, never taking more than you give.
6584 **
6585 ******************************************************************************
6586 **
6587 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
6588 ** for working with sqlar archives and used by the shell tool's built-in
6589 ** sqlar support.
6590 */
6591 /* #include "sqlite3ext.h" */
6592 SQLITE_EXTENSION_INIT1
6593 #include <zlib.h>
6594 #include <assert.h>
6595
6596 /*
6597 ** Implementation of the "sqlar_compress(X)" SQL function.
6598 **
6599 ** If the type of X is SQLITE_BLOB, and compressing that blob using
6600 ** zlib utility function compress() yields a smaller blob, return the
6601 ** compressed blob. Otherwise, return a copy of X.
6602 **
6603 ** SQLar uses the "zlib format" for compressed content.  The zlib format
6604 ** contains a two-byte identification header and a four-byte checksum at
6605 ** the end.  This is different from ZIP which uses the raw deflate format.
6606 **
6607 ** Future enhancements to SQLar might add support for new compression formats.
6608 ** If so, those new formats will be identified by alternative headers in the
6609 ** compressed data.
6610 */
6611 static void sqlarCompressFunc(
6612   sqlite3_context *context,
6613   int argc,
6614   sqlite3_value **argv
6615 ){
6616   assert( argc==1 );
6617   if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
6618     const Bytef *pData = sqlite3_value_blob(argv[0]);
6619     uLong nData = sqlite3_value_bytes(argv[0]);
6620     uLongf nOut = compressBound(nData);
6621     Bytef *pOut;
6622
6623     pOut = (Bytef*)sqlite3_malloc(nOut);
6624     if( pOut==0 ){
6625       sqlite3_result_error_nomem(context);
6626       return;
6627     }else{
6628       if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
6629         sqlite3_result_error(context, "error in compress()", -1);
6630       }else if( nOut<nData ){
6631         sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
6632       }else{
6633         sqlite3_result_value(context, argv[0]);
6634       }
6635       sqlite3_free(pOut);
6636     }
6637   }else{
6638     sqlite3_result_value(context, argv[0]);
6639   }
6640 }
6641
6642 /*
6643 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
6644 **
6645 ** Parameter SZ is interpreted as an integer. If it is less than or
6646 ** equal to zero, then this function returns a copy of X. Or, if
6647 ** SZ is equal to the size of X when interpreted as a blob, also
6648 ** return a copy of X. Otherwise, decompress blob X using zlib
6649 ** utility function uncompress() and return the results (another
6650 ** blob).
6651 */
6652 static void sqlarUncompressFunc(
6653   sqlite3_context *context,
6654   int argc,
6655   sqlite3_value **argv
6656 ){
6657   uLong nData;
6658   uLongf sz;
6659
6660   assert( argc==2 );
6661   sz = sqlite3_value_int(argv[1]);
6662
6663   if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
6664     sqlite3_result_value(context, argv[0]);
6665   }else{
6666     const Bytef *pData= sqlite3_value_blob(argv[0]);
6667     Bytef *pOut = sqlite3_malloc(sz);
6668     if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
6669       sqlite3_result_error(context, "error in uncompress()", -1);
6670     }else{
6671       sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
6672     }
6673     sqlite3_free(pOut);
6674   }
6675 }
6676
6677
6678 #ifdef _WIN32
6679
6680 #endif
6681 int sqlite3_sqlar_init(
6682   sqlite3 *db, 
6683   char **pzErrMsg, 
6684   const sqlite3_api_routines *pApi
6685 ){
6686   int rc = SQLITE_OK;
6687   SQLITE_EXTENSION_INIT2(pApi);
6688   (void)pzErrMsg;  /* Unused parameter */
6689   rc = sqlite3_create_function(db, "sqlar_compress", 1, 
6690                                SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
6691                                sqlarCompressFunc, 0, 0);
6692   if( rc==SQLITE_OK ){
6693     rc = sqlite3_create_function(db, "sqlar_uncompress", 2,
6694                                  SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
6695                                  sqlarUncompressFunc, 0, 0);
6696   }
6697   return rc;
6698 }
6699
6700 /************************* End ../ext/misc/sqlar.c ********************/
6701 #endif
6702 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
6703 /*
6704 ** 2017 April 07
6705 **
6706 ** The author disclaims copyright to this source code.  In place of
6707 ** a legal notice, here is a blessing:
6708 **
6709 **    May you do good and not evil.
6710 **    May you find forgiveness for yourself and forgive others.
6711 **    May you share freely, never taking more than you give.
6712 **
6713 *************************************************************************
6714 */
6715 #if !defined(SQLITEEXPERT_H)
6716 #define SQLITEEXPERT_H 1
6717 /* #include "sqlite3.h" */
6718
6719 typedef struct sqlite3expert sqlite3expert;
6720
6721 /*
6722 ** Create a new sqlite3expert object.
6723 **
6724 ** If successful, a pointer to the new object is returned and (*pzErr) set
6725 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
6726 ** an English-language error message. In this case it is the responsibility
6727 ** of the caller to eventually free the error message buffer using
6728 ** sqlite3_free().
6729 */
6730 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
6731
6732 /*
6733 ** Configure an sqlite3expert object.
6734 **
6735 ** EXPERT_CONFIG_SAMPLE:
6736 **   By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
6737 **   each candidate index. This involves scanning and sorting the entire
6738 **   contents of each user database table once for each candidate index
6739 **   associated with the table. For large databases, this can be 
6740 **   prohibitively slow. This option allows the sqlite3expert object to
6741 **   be configured so that sqlite_stat1 data is instead generated based on a
6742 **   subset of each table, or so that no sqlite_stat1 data is used at all.
6743 **
6744 **   A single integer argument is passed to this option. If the value is less
6745 **   than or equal to zero, then no sqlite_stat1 data is generated or used by
6746 **   the analysis - indexes are recommended based on the database schema only.
6747 **   Or, if the value is 100 or greater, complete sqlite_stat1 data is
6748 **   generated for each candidate index (this is the default). Finally, if the
6749 **   value falls between 0 and 100, then it represents the percentage of user
6750 **   table rows that should be considered when generating sqlite_stat1 data.
6751 **
6752 **   Examples:
6753 **
6754 **     // Do not generate any sqlite_stat1 data
6755 **     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
6756 **
6757 **     // Generate sqlite_stat1 data based on 10% of the rows in each table.
6758 **     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
6759 */
6760 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
6761
6762 #define EXPERT_CONFIG_SAMPLE 1    /* int */
6763
6764 /*
6765 ** Specify zero or more SQL statements to be included in the analysis.
6766 **
6767 ** Buffer zSql must contain zero or more complete SQL statements. This
6768 ** function parses all statements contained in the buffer and adds them
6769 ** to the internal list of statements to analyze. If successful, SQLITE_OK
6770 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
6771 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
6772 ** may be set to point to an English language error message. In this case
6773 ** the caller is responsible for eventually freeing the error message buffer
6774 ** using sqlite3_free().
6775 **
6776 ** If an error does occur while processing one of the statements in the
6777 ** buffer passed as the second argument, none of the statements in the
6778 ** buffer are added to the analysis.
6779 **
6780 ** This function must be called before sqlite3_expert_analyze(). If a call
6781 ** to this function is made on an sqlite3expert object that has already
6782 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
6783 ** immediately and no statements are added to the analysis.
6784 */
6785 int sqlite3_expert_sql(
6786   sqlite3expert *p,               /* From a successful sqlite3_expert_new() */
6787   const char *zSql,               /* SQL statement(s) to add */
6788   char **pzErr                    /* OUT: Error message (if any) */
6789 );
6790
6791
6792 /*
6793 ** This function is called after the sqlite3expert object has been configured
6794 ** with all SQL statements using sqlite3_expert_sql() to actually perform
6795 ** the analysis. Once this function has been called, it is not possible to
6796 ** add further SQL statements to the analysis.
6797 **
6798 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
6799 ** an error occurs, an SQLite error code is returned and (*pzErr) set to 
6800 ** point to a buffer containing an English language error message. In this
6801 ** case it is the responsibility of the caller to eventually free the buffer
6802 ** using sqlite3_free().
6803 **
6804 ** If an error does occur within this function, the sqlite3expert object
6805 ** is no longer useful for any purpose. At that point it is no longer
6806 ** possible to add further SQL statements to the object or to re-attempt
6807 ** the analysis. The sqlite3expert object must still be freed using a call
6808 ** sqlite3_expert_destroy().
6809 */
6810 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
6811
6812 /*
6813 ** Return the total number of statements loaded using sqlite3_expert_sql().
6814 ** The total number of SQL statements may be different from the total number
6815 ** to calls to sqlite3_expert_sql().
6816 */
6817 int sqlite3_expert_count(sqlite3expert*);
6818
6819 /*
6820 ** Return a component of the report.
6821 **
6822 ** This function is called after sqlite3_expert_analyze() to extract the
6823 ** results of the analysis. Each call to this function returns either a
6824 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
6825 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
6826 ** #define constants defined below.
6827 **
6828 ** For some EXPERT_REPORT_* parameters, the buffer returned contains 
6829 ** information relating to a specific SQL statement. In these cases that
6830 ** SQL statement is identified by the value passed as the second argument.
6831 ** SQL statements are numbered from 0 in the order in which they are parsed.
6832 ** If an out-of-range value (less than zero or equal to or greater than the
6833 ** value returned by sqlite3_expert_count()) is passed as the second argument
6834 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
6835 **
6836 ** EXPERT_REPORT_SQL:
6837 **   Return the text of SQL statement iStmt.
6838 **
6839 ** EXPERT_REPORT_INDEXES:
6840 **   Return a buffer containing the CREATE INDEX statements for all recommended
6841 **   indexes for statement iStmt. If there are no new recommeded indexes, NULL 
6842 **   is returned.
6843 **
6844 ** EXPERT_REPORT_PLAN:
6845 **   Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
6846 **   iStmt after the proposed indexes have been added to the database schema.
6847 **
6848 ** EXPERT_REPORT_CANDIDATES:
6849 **   Return a pointer to a buffer containing the CREATE INDEX statements 
6850 **   for all indexes that were tested (for all SQL statements). The iStmt
6851 **   parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
6852 */
6853 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
6854
6855 /*
6856 ** Values for the third argument passed to sqlite3_expert_report().
6857 */
6858 #define EXPERT_REPORT_SQL        1
6859 #define EXPERT_REPORT_INDEXES    2
6860 #define EXPERT_REPORT_PLAN       3
6861 #define EXPERT_REPORT_CANDIDATES 4
6862
6863 /*
6864 ** Free an (sqlite3expert*) handle and all associated resources. There 
6865 ** should be one call to this function for each successful call to 
6866 ** sqlite3-expert_new().
6867 */
6868 void sqlite3_expert_destroy(sqlite3expert*);
6869
6870 #endif  /* !defined(SQLITEEXPERT_H) */
6871
6872 /************************* End ../ext/expert/sqlite3expert.h ********************/
6873 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
6874 /*
6875 ** 2017 April 09
6876 **
6877 ** The author disclaims copyright to this source code.  In place of
6878 ** a legal notice, here is a blessing:
6879 **
6880 **    May you do good and not evil.
6881 **    May you find forgiveness for yourself and forgive others.
6882 **    May you share freely, never taking more than you give.
6883 **
6884 *************************************************************************
6885 */
6886 /* #include "sqlite3expert.h" */
6887 #include <assert.h>
6888 #include <string.h>
6889 #include <stdio.h>
6890
6891 #ifndef SQLITE_OMIT_VIRTUALTABLE 
6892
6893 /* typedef sqlite3_int64 i64; */
6894 /* typedef sqlite3_uint64 u64; */
6895
6896 typedef struct IdxColumn IdxColumn;
6897 typedef struct IdxConstraint IdxConstraint;
6898 typedef struct IdxScan IdxScan;
6899 typedef struct IdxStatement IdxStatement;
6900 typedef struct IdxTable IdxTable;
6901 typedef struct IdxWrite IdxWrite;
6902
6903 #define STRLEN  (int)strlen
6904
6905 /*
6906 ** A temp table name that we assume no user database will actually use.
6907 ** If this assumption proves incorrect triggers on the table with the
6908 ** conflicting name will be ignored.
6909 */
6910 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
6911
6912 /*
6913 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
6914 ** any other type of single-ended range constraint on a column).
6915 **
6916 ** pLink:
6917 **   Used to temporarily link IdxConstraint objects into lists while
6918 **   creating candidate indexes.
6919 */
6920 struct IdxConstraint {
6921   char *zColl;                    /* Collation sequence */
6922   int bRange;                     /* True for range, false for eq */
6923   int iCol;                       /* Constrained table column */
6924   int bFlag;                      /* Used by idxFindCompatible() */
6925   int bDesc;                      /* True if ORDER BY <expr> DESC */
6926   IdxConstraint *pNext;           /* Next constraint in pEq or pRange list */
6927   IdxConstraint *pLink;           /* See above */
6928 };
6929
6930 /*
6931 ** A single scan of a single table.
6932 */
6933 struct IdxScan {
6934   IdxTable *pTab;                 /* Associated table object */
6935   int iDb;                        /* Database containing table zTable */
6936   i64 covering;                   /* Mask of columns required for cov. index */
6937   IdxConstraint *pOrder;          /* ORDER BY columns */
6938   IdxConstraint *pEq;             /* List of == constraints */
6939   IdxConstraint *pRange;          /* List of < constraints */
6940   IdxScan *pNextScan;             /* Next IdxScan object for same analysis */
6941 };
6942
6943 /*
6944 ** Information regarding a single database table. Extracted from 
6945 ** "PRAGMA table_info" by function idxGetTableInfo().
6946 */
6947 struct IdxColumn {
6948   char *zName;
6949   char *zColl;
6950   int iPk;
6951 };
6952 struct IdxTable {
6953   int nCol;
6954   char *zName;                    /* Table name */
6955   IdxColumn *aCol;
6956   IdxTable *pNext;                /* Next table in linked list of all tables */
6957 };
6958
6959 /*
6960 ** An object of the following type is created for each unique table/write-op
6961 ** seen. The objects are stored in a singly-linked list beginning at
6962 ** sqlite3expert.pWrite.
6963 */
6964 struct IdxWrite {
6965   IdxTable *pTab;
6966   int eOp;                        /* SQLITE_UPDATE, DELETE or INSERT */
6967   IdxWrite *pNext;
6968 };
6969
6970 /*
6971 ** Each statement being analyzed is represented by an instance of this
6972 ** structure.
6973 */
6974 struct IdxStatement {
6975   int iId;                        /* Statement number */
6976   char *zSql;                     /* SQL statement */
6977   char *zIdx;                     /* Indexes */
6978   char *zEQP;                     /* Plan */
6979   IdxStatement *pNext;
6980 };
6981
6982
6983 /*
6984 ** A hash table for storing strings. With space for a payload string
6985 ** with each entry. Methods are:
6986 **
6987 **   idxHashInit()
6988 **   idxHashClear()
6989 **   idxHashAdd()
6990 **   idxHashSearch()
6991 */
6992 #define IDX_HASH_SIZE 1023
6993 typedef struct IdxHashEntry IdxHashEntry;
6994 typedef struct IdxHash IdxHash;
6995 struct IdxHashEntry {
6996   char *zKey;                     /* nul-terminated key */
6997   char *zVal;                     /* nul-terminated value string */
6998   char *zVal2;                    /* nul-terminated value string 2 */
6999   IdxHashEntry *pHashNext;        /* Next entry in same hash bucket */
7000   IdxHashEntry *pNext;            /* Next entry in hash */
7001 };
7002 struct IdxHash {
7003   IdxHashEntry *pFirst;
7004   IdxHashEntry *aHash[IDX_HASH_SIZE];
7005 };
7006
7007 /*
7008 ** sqlite3expert object.
7009 */
7010 struct sqlite3expert {
7011   int iSample;                    /* Percentage of tables to sample for stat1 */
7012   sqlite3 *db;                    /* User database */
7013   sqlite3 *dbm;                   /* In-memory db for this analysis */
7014   sqlite3 *dbv;                   /* Vtab schema for this analysis */
7015   IdxTable *pTable;               /* List of all IdxTable objects */
7016   IdxScan *pScan;                 /* List of scan objects */
7017   IdxWrite *pWrite;               /* List of write objects */
7018   IdxStatement *pStatement;       /* List of IdxStatement objects */
7019   int bRun;                       /* True once analysis has run */
7020   char **pzErrmsg;
7021   int rc;                         /* Error code from whereinfo hook */
7022   IdxHash hIdx;                   /* Hash containing all candidate indexes */
7023   char *zCandidates;              /* For EXPERT_REPORT_CANDIDATES */
7024 };
7025
7026
7027 /*
7028 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc(). 
7029 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
7030 */
7031 static void *idxMalloc(int *pRc, int nByte){
7032   void *pRet;
7033   assert( *pRc==SQLITE_OK );
7034   assert( nByte>0 );
7035   pRet = sqlite3_malloc(nByte);
7036   if( pRet ){
7037     memset(pRet, 0, nByte);
7038   }else{
7039     *pRc = SQLITE_NOMEM;
7040   }
7041   return pRet;
7042 }
7043
7044 /*
7045 ** Initialize an IdxHash hash table.
7046 */
7047 static void idxHashInit(IdxHash *pHash){
7048   memset(pHash, 0, sizeof(IdxHash));
7049 }
7050
7051 /*
7052 ** Reset an IdxHash hash table.
7053 */
7054 static void idxHashClear(IdxHash *pHash){
7055   int i;
7056   for(i=0; i<IDX_HASH_SIZE; i++){
7057     IdxHashEntry *pEntry;
7058     IdxHashEntry *pNext;
7059     for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
7060       pNext = pEntry->pHashNext;
7061       sqlite3_free(pEntry->zVal2);
7062       sqlite3_free(pEntry);
7063     }
7064   }
7065   memset(pHash, 0, sizeof(IdxHash));
7066 }
7067
7068 /*
7069 ** Return the index of the hash bucket that the string specified by the
7070 ** arguments to this function belongs.
7071 */
7072 static int idxHashString(const char *z, int n){
7073   unsigned int ret = 0;
7074   int i;
7075   for(i=0; i<n; i++){
7076     ret += (ret<<3) + (unsigned char)(z[i]);
7077   }
7078   return (int)(ret % IDX_HASH_SIZE);
7079 }
7080
7081 /*
7082 ** If zKey is already present in the hash table, return non-zero and do
7083 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
7084 ** the hash table passed as the second argument. 
7085 */
7086 static int idxHashAdd(
7087   int *pRc, 
7088   IdxHash *pHash, 
7089   const char *zKey,
7090   const char *zVal
7091 ){
7092   int nKey = STRLEN(zKey);
7093   int iHash = idxHashString(zKey, nKey);
7094   int nVal = (zVal ? STRLEN(zVal) : 0);
7095   IdxHashEntry *pEntry;
7096   assert( iHash>=0 );
7097   for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
7098     if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
7099       return 1;
7100     }
7101   }
7102   pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
7103   if( pEntry ){
7104     pEntry->zKey = (char*)&pEntry[1];
7105     memcpy(pEntry->zKey, zKey, nKey);
7106     if( zVal ){
7107       pEntry->zVal = &pEntry->zKey[nKey+1];
7108       memcpy(pEntry->zVal, zVal, nVal);
7109     }
7110     pEntry->pHashNext = pHash->aHash[iHash];
7111     pHash->aHash[iHash] = pEntry;
7112
7113     pEntry->pNext = pHash->pFirst;
7114     pHash->pFirst = pEntry;
7115   }
7116   return 0;
7117 }
7118
7119 /*
7120 ** If zKey/nKey is present in the hash table, return a pointer to the 
7121 ** hash-entry object.
7122 */
7123 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
7124   int iHash;
7125   IdxHashEntry *pEntry;
7126   if( nKey<0 ) nKey = STRLEN(zKey);
7127   iHash = idxHashString(zKey, nKey);
7128   assert( iHash>=0 );
7129   for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
7130     if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
7131       return pEntry;
7132     }
7133   }
7134   return 0;
7135 }
7136
7137 /*
7138 ** If the hash table contains an entry with a key equal to the string
7139 ** passed as the final two arguments to this function, return a pointer
7140 ** to the payload string. Otherwise, if zKey/nKey is not present in the
7141 ** hash table, return NULL.
7142 */
7143 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
7144   IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
7145   if( pEntry ) return pEntry->zVal;
7146   return 0;
7147 }
7148
7149 /*
7150 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
7151 ** variable to point to a copy of nul-terminated string zColl.
7152 */
7153 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
7154   IdxConstraint *pNew;
7155   int nColl = STRLEN(zColl);
7156
7157   assert( *pRc==SQLITE_OK );
7158   pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
7159   if( pNew ){
7160     pNew->zColl = (char*)&pNew[1];
7161     memcpy(pNew->zColl, zColl, nColl+1);
7162   }
7163   return pNew;
7164 }
7165
7166 /*
7167 ** An error associated with database handle db has just occurred. Pass
7168 ** the error message to callback function xOut.
7169 */
7170 static void idxDatabaseError(
7171   sqlite3 *db,                    /* Database handle */
7172   char **pzErrmsg                 /* Write error here */
7173 ){
7174   *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
7175 }
7176
7177 /*
7178 ** Prepare an SQL statement.
7179 */
7180 static int idxPrepareStmt(
7181   sqlite3 *db,                    /* Database handle to compile against */
7182   sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
7183   char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
7184   const char *zSql                /* SQL statement to compile */
7185 ){
7186   int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
7187   if( rc!=SQLITE_OK ){
7188     *ppStmt = 0;
7189     idxDatabaseError(db, pzErrmsg);
7190   }
7191   return rc;
7192 }
7193
7194 /*
7195 ** Prepare an SQL statement using the results of a printf() formatting.
7196 */
7197 static int idxPrintfPrepareStmt(
7198   sqlite3 *db,                    /* Database handle to compile against */
7199   sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
7200   char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
7201   const char *zFmt,               /* printf() format of SQL statement */
7202   ...                             /* Trailing printf() arguments */
7203 ){
7204   va_list ap;
7205   int rc;
7206   char *zSql;
7207   va_start(ap, zFmt);
7208   zSql = sqlite3_vmprintf(zFmt, ap);
7209   if( zSql==0 ){
7210     rc = SQLITE_NOMEM;
7211   }else{
7212     rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
7213     sqlite3_free(zSql);
7214   }
7215   va_end(ap);
7216   return rc;
7217 }
7218
7219
7220 /*************************************************************************
7221 ** Beginning of virtual table implementation.
7222 */
7223 typedef struct ExpertVtab ExpertVtab;
7224 struct ExpertVtab {
7225   sqlite3_vtab base;
7226   IdxTable *pTab;
7227   sqlite3expert *pExpert;
7228 };
7229
7230 typedef struct ExpertCsr ExpertCsr;
7231 struct ExpertCsr {
7232   sqlite3_vtab_cursor base;
7233   sqlite3_stmt *pData;
7234 };
7235
7236 static char *expertDequote(const char *zIn){
7237   int n = STRLEN(zIn);
7238   char *zRet = sqlite3_malloc(n);
7239
7240   assert( zIn[0]=='\'' );
7241   assert( zIn[n-1]=='\'' );
7242
7243   if( zRet ){
7244     int iOut = 0;
7245     int iIn = 0;
7246     for(iIn=1; iIn<(n-1); iIn++){
7247       if( zIn[iIn]=='\'' ){
7248         assert( zIn[iIn+1]=='\'' );
7249         iIn++;
7250       }
7251       zRet[iOut++] = zIn[iIn];
7252     }
7253     zRet[iOut] = '\0';
7254   }
7255
7256   return zRet;
7257 }
7258
7259 /* 
7260 ** This function is the implementation of both the xConnect and xCreate
7261 ** methods of the r-tree virtual table.
7262 **
7263 **   argv[0]   -> module name
7264 **   argv[1]   -> database name
7265 **   argv[2]   -> table name
7266 **   argv[...] -> column names...
7267 */
7268 static int expertConnect(
7269   sqlite3 *db,
7270   void *pAux,
7271   int argc, const char *const*argv,
7272   sqlite3_vtab **ppVtab,
7273   char **pzErr
7274 ){
7275   sqlite3expert *pExpert = (sqlite3expert*)pAux;
7276   ExpertVtab *p = 0;
7277   int rc;
7278
7279   if( argc!=4 ){
7280     *pzErr = sqlite3_mprintf("internal error!");
7281     rc = SQLITE_ERROR;
7282   }else{
7283     char *zCreateTable = expertDequote(argv[3]);
7284     if( zCreateTable ){
7285       rc = sqlite3_declare_vtab(db, zCreateTable);
7286       if( rc==SQLITE_OK ){
7287         p = idxMalloc(&rc, sizeof(ExpertVtab));
7288       }
7289       if( rc==SQLITE_OK ){
7290         p->pExpert = pExpert;
7291         p->pTab = pExpert->pTable;
7292         assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
7293       }
7294       sqlite3_free(zCreateTable);
7295     }else{
7296       rc = SQLITE_NOMEM;
7297     }
7298   }
7299
7300   *ppVtab = (sqlite3_vtab*)p;
7301   return rc;
7302 }
7303
7304 static int expertDisconnect(sqlite3_vtab *pVtab){
7305   ExpertVtab *p = (ExpertVtab*)pVtab;
7306   sqlite3_free(p);
7307   return SQLITE_OK;
7308 }
7309
7310 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
7311   ExpertVtab *p = (ExpertVtab*)pVtab;
7312   int rc = SQLITE_OK;
7313   int n = 0;
7314   IdxScan *pScan;
7315   const int opmask = 
7316     SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
7317     SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
7318     SQLITE_INDEX_CONSTRAINT_LE;
7319
7320   pScan = idxMalloc(&rc, sizeof(IdxScan));
7321   if( pScan ){
7322     int i;
7323
7324     /* Link the new scan object into the list */
7325     pScan->pTab = p->pTab;
7326     pScan->pNextScan = p->pExpert->pScan;
7327     p->pExpert->pScan = pScan;
7328
7329     /* Add the constraints to the IdxScan object */
7330     for(i=0; i<pIdxInfo->nConstraint; i++){
7331       struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
7332       if( pCons->usable 
7333        && pCons->iColumn>=0 
7334        && p->pTab->aCol[pCons->iColumn].iPk==0
7335        && (pCons->op & opmask) 
7336       ){
7337         IdxConstraint *pNew;
7338         const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
7339         pNew = idxNewConstraint(&rc, zColl);
7340         if( pNew ){
7341           pNew->iCol = pCons->iColumn;
7342           if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
7343             pNew->pNext = pScan->pEq;
7344             pScan->pEq = pNew;
7345           }else{
7346             pNew->bRange = 1;
7347             pNew->pNext = pScan->pRange;
7348             pScan->pRange = pNew;
7349           }
7350         }
7351         n++;
7352         pIdxInfo->aConstraintUsage[i].argvIndex = n;
7353       }
7354     }
7355
7356     /* Add the ORDER BY to the IdxScan object */
7357     for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
7358       int iCol = pIdxInfo->aOrderBy[i].iColumn;
7359       if( iCol>=0 ){
7360         IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
7361         if( pNew ){
7362           pNew->iCol = iCol;
7363           pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
7364           pNew->pNext = pScan->pOrder;
7365           pNew->pLink = pScan->pOrder;
7366           pScan->pOrder = pNew;
7367           n++;
7368         }
7369       }
7370     }
7371   }
7372
7373   pIdxInfo->estimatedCost = 1000000.0 / (n+1);
7374   return rc;
7375 }
7376
7377 static int expertUpdate(
7378   sqlite3_vtab *pVtab, 
7379   int nData, 
7380   sqlite3_value **azData, 
7381   sqlite_int64 *pRowid
7382 ){
7383   (void)pVtab;
7384   (void)nData;
7385   (void)azData;
7386   (void)pRowid;
7387   return SQLITE_OK;
7388 }
7389
7390 /* 
7391 ** Virtual table module xOpen method.
7392 */
7393 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
7394   int rc = SQLITE_OK;
7395   ExpertCsr *pCsr;
7396   (void)pVTab;
7397   pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
7398   *ppCursor = (sqlite3_vtab_cursor*)pCsr;
7399   return rc;
7400 }
7401
7402 /* 
7403 ** Virtual table module xClose method.
7404 */
7405 static int expertClose(sqlite3_vtab_cursor *cur){
7406   ExpertCsr *pCsr = (ExpertCsr*)cur;
7407   sqlite3_finalize(pCsr->pData);
7408   sqlite3_free(pCsr);
7409   return SQLITE_OK;
7410 }
7411
7412 /*
7413 ** Virtual table module xEof method.
7414 **
7415 ** Return non-zero if the cursor does not currently point to a valid 
7416 ** record (i.e if the scan has finished), or zero otherwise.
7417 */
7418 static int expertEof(sqlite3_vtab_cursor *cur){
7419   ExpertCsr *pCsr = (ExpertCsr*)cur;
7420   return pCsr->pData==0;
7421 }
7422
7423 /* 
7424 ** Virtual table module xNext method.
7425 */
7426 static int expertNext(sqlite3_vtab_cursor *cur){
7427   ExpertCsr *pCsr = (ExpertCsr*)cur;
7428   int rc = SQLITE_OK;
7429
7430   assert( pCsr->pData );
7431   rc = sqlite3_step(pCsr->pData);
7432   if( rc!=SQLITE_ROW ){
7433     rc = sqlite3_finalize(pCsr->pData);
7434     pCsr->pData = 0;
7435   }else{
7436     rc = SQLITE_OK;
7437   }
7438
7439   return rc;
7440 }
7441
7442 /* 
7443 ** Virtual table module xRowid method.
7444 */
7445 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
7446   (void)cur;
7447   *pRowid = 0;
7448   return SQLITE_OK;
7449 }
7450
7451 /* 
7452 ** Virtual table module xColumn method.
7453 */
7454 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
7455   ExpertCsr *pCsr = (ExpertCsr*)cur;
7456   sqlite3_value *pVal;
7457   pVal = sqlite3_column_value(pCsr->pData, i);
7458   if( pVal ){
7459     sqlite3_result_value(ctx, pVal);
7460   }
7461   return SQLITE_OK;
7462 }
7463
7464 /* 
7465 ** Virtual table module xFilter method.
7466 */
7467 static int expertFilter(
7468   sqlite3_vtab_cursor *cur, 
7469   int idxNum, const char *idxStr,
7470   int argc, sqlite3_value **argv
7471 ){
7472   ExpertCsr *pCsr = (ExpertCsr*)cur;
7473   ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
7474   sqlite3expert *pExpert = pVtab->pExpert;
7475   int rc;
7476
7477   (void)idxNum;
7478   (void)idxStr;
7479   (void)argc;
7480   (void)argv;
7481   rc = sqlite3_finalize(pCsr->pData);
7482   pCsr->pData = 0;
7483   if( rc==SQLITE_OK ){
7484     rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
7485         "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
7486     );
7487   }
7488
7489   if( rc==SQLITE_OK ){
7490     rc = expertNext(cur);
7491   }
7492   return rc;
7493 }
7494
7495 static int idxRegisterVtab(sqlite3expert *p){
7496   static sqlite3_module expertModule = {
7497     2,                            /* iVersion */
7498     expertConnect,                /* xCreate - create a table */
7499     expertConnect,                /* xConnect - connect to an existing table */
7500     expertBestIndex,              /* xBestIndex - Determine search strategy */
7501     expertDisconnect,             /* xDisconnect - Disconnect from a table */
7502     expertDisconnect,             /* xDestroy - Drop a table */
7503     expertOpen,                   /* xOpen - open a cursor */
7504     expertClose,                  /* xClose - close a cursor */
7505     expertFilter,                 /* xFilter - configure scan constraints */
7506     expertNext,                   /* xNext - advance a cursor */
7507     expertEof,                    /* xEof */
7508     expertColumn,                 /* xColumn - read data */
7509     expertRowid,                  /* xRowid - read data */
7510     expertUpdate,                 /* xUpdate - write data */
7511     0,                            /* xBegin - begin transaction */
7512     0,                            /* xSync - sync transaction */
7513     0,                            /* xCommit - commit transaction */
7514     0,                            /* xRollback - rollback transaction */
7515     0,                            /* xFindFunction - function overloading */
7516     0,                            /* xRename - rename the table */
7517     0,                            /* xSavepoint */
7518     0,                            /* xRelease */
7519     0,                            /* xRollbackTo */
7520     0,                            /* xShadowName */
7521   };
7522
7523   return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
7524 }
7525 /*
7526 ** End of virtual table implementation.
7527 *************************************************************************/
7528 /*
7529 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
7530 ** is called, set it to the return value of sqlite3_finalize() before
7531 ** returning. Otherwise, discard the sqlite3_finalize() return value.
7532 */
7533 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
7534   int rc = sqlite3_finalize(pStmt);
7535   if( *pRc==SQLITE_OK ) *pRc = rc;
7536 }
7537
7538 /*
7539 ** Attempt to allocate an IdxTable structure corresponding to table zTab
7540 ** in the main database of connection db. If successful, set (*ppOut) to
7541 ** point to the new object and return SQLITE_OK. Otherwise, return an
7542 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
7543 ** set to point to an error string.
7544 **
7545 ** It is the responsibility of the caller to eventually free either the
7546 ** IdxTable object or error message using sqlite3_free().
7547 */
7548 static int idxGetTableInfo(
7549   sqlite3 *db,                    /* Database connection to read details from */
7550   const char *zTab,               /* Table name */
7551   IdxTable **ppOut,               /* OUT: New object (if successful) */
7552   char **pzErrmsg                 /* OUT: Error message (if not) */
7553 ){
7554   sqlite3_stmt *p1 = 0;
7555   int nCol = 0;
7556   int nTab = STRLEN(zTab);
7557   int nByte = sizeof(IdxTable) + nTab + 1;
7558   IdxTable *pNew = 0;
7559   int rc, rc2;
7560   char *pCsr = 0;
7561
7562   rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_info=%Q", zTab);
7563   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
7564     const char *zCol = (const char*)sqlite3_column_text(p1, 1);
7565     nByte += 1 + STRLEN(zCol);
7566     rc = sqlite3_table_column_metadata(
7567         db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
7568     );
7569     nByte += 1 + STRLEN(zCol);
7570     nCol++;
7571   }
7572   rc2 = sqlite3_reset(p1);
7573   if( rc==SQLITE_OK ) rc = rc2;
7574
7575   nByte += sizeof(IdxColumn) * nCol;
7576   if( rc==SQLITE_OK ){
7577     pNew = idxMalloc(&rc, nByte);
7578   }
7579   if( rc==SQLITE_OK ){
7580     pNew->aCol = (IdxColumn*)&pNew[1];
7581     pNew->nCol = nCol;
7582     pCsr = (char*)&pNew->aCol[nCol];
7583   }
7584
7585   nCol = 0;
7586   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
7587     const char *zCol = (const char*)sqlite3_column_text(p1, 1);
7588     int nCopy = STRLEN(zCol) + 1;
7589     pNew->aCol[nCol].zName = pCsr;
7590     pNew->aCol[nCol].iPk = sqlite3_column_int(p1, 5);
7591     memcpy(pCsr, zCol, nCopy);
7592     pCsr += nCopy;
7593
7594     rc = sqlite3_table_column_metadata(
7595         db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
7596     );
7597     if( rc==SQLITE_OK ){
7598       nCopy = STRLEN(zCol) + 1;
7599       pNew->aCol[nCol].zColl = pCsr;
7600       memcpy(pCsr, zCol, nCopy);
7601       pCsr += nCopy;
7602     }
7603
7604     nCol++;
7605   }
7606   idxFinalize(&rc, p1);
7607
7608   if( rc!=SQLITE_OK ){
7609     sqlite3_free(pNew);
7610     pNew = 0;
7611   }else{
7612     pNew->zName = pCsr;
7613     memcpy(pNew->zName, zTab, nTab+1);
7614   }
7615
7616   *ppOut = pNew;
7617   return rc;
7618 }
7619
7620 /*
7621 ** This function is a no-op if *pRc is set to anything other than 
7622 ** SQLITE_OK when it is called.
7623 **
7624 ** If *pRc is initially set to SQLITE_OK, then the text specified by
7625 ** the printf() style arguments is appended to zIn and the result returned
7626 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
7627 ** zIn before returning.
7628 */
7629 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
7630   va_list ap;
7631   char *zAppend = 0;
7632   char *zRet = 0;
7633   int nIn = zIn ? STRLEN(zIn) : 0;
7634   int nAppend = 0;
7635   va_start(ap, zFmt);
7636   if( *pRc==SQLITE_OK ){
7637     zAppend = sqlite3_vmprintf(zFmt, ap);
7638     if( zAppend ){
7639       nAppend = STRLEN(zAppend);
7640       zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
7641     }
7642     if( zAppend && zRet ){
7643       if( nIn ) memcpy(zRet, zIn, nIn);
7644       memcpy(&zRet[nIn], zAppend, nAppend+1);
7645     }else{
7646       sqlite3_free(zRet);
7647       zRet = 0;
7648       *pRc = SQLITE_NOMEM;
7649     }
7650     sqlite3_free(zAppend);
7651     sqlite3_free(zIn);
7652   }
7653   va_end(ap);
7654   return zRet;
7655 }
7656
7657 /*
7658 ** Return true if zId must be quoted in order to use it as an SQL
7659 ** identifier, or false otherwise.
7660 */
7661 static int idxIdentifierRequiresQuotes(const char *zId){
7662   int i;
7663   for(i=0; zId[i]; i++){
7664     if( !(zId[i]=='_')
7665      && !(zId[i]>='0' && zId[i]<='9')
7666      && !(zId[i]>='a' && zId[i]<='z')
7667      && !(zId[i]>='A' && zId[i]<='Z')
7668     ){
7669       return 1;
7670     }
7671   }
7672   return 0;
7673 }
7674
7675 /*
7676 ** This function appends an index column definition suitable for constraint
7677 ** pCons to the string passed as zIn and returns the result.
7678 */
7679 static char *idxAppendColDefn(
7680   int *pRc,                       /* IN/OUT: Error code */
7681   char *zIn,                      /* Column defn accumulated so far */
7682   IdxTable *pTab,                 /* Table index will be created on */
7683   IdxConstraint *pCons
7684 ){
7685   char *zRet = zIn;
7686   IdxColumn *p = &pTab->aCol[pCons->iCol];
7687   if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
7688
7689   if( idxIdentifierRequiresQuotes(p->zName) ){
7690     zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
7691   }else{
7692     zRet = idxAppendText(pRc, zRet, "%s", p->zName);
7693   }
7694
7695   if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
7696     if( idxIdentifierRequiresQuotes(pCons->zColl) ){
7697       zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
7698     }else{
7699       zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
7700     }
7701   }
7702
7703   if( pCons->bDesc ){
7704     zRet = idxAppendText(pRc, zRet, " DESC");
7705   }
7706   return zRet;
7707 }
7708
7709 /*
7710 ** Search database dbm for an index compatible with the one idxCreateFromCons()
7711 ** would create from arguments pScan, pEq and pTail. If no error occurs and 
7712 ** such an index is found, return non-zero. Or, if no such index is found,
7713 ** return zero.
7714 **
7715 ** If an error occurs, set *pRc to an SQLite error code and return zero.
7716 */
7717 static int idxFindCompatible(
7718   int *pRc,                       /* OUT: Error code */
7719   sqlite3* dbm,                   /* Database to search */
7720   IdxScan *pScan,                 /* Scan for table to search for index on */
7721   IdxConstraint *pEq,             /* List of == constraints */
7722   IdxConstraint *pTail            /* List of range constraints */
7723 ){
7724   const char *zTbl = pScan->pTab->zName;
7725   sqlite3_stmt *pIdxList = 0;
7726   IdxConstraint *pIter;
7727   int nEq = 0;                    /* Number of elements in pEq */
7728   int rc;
7729
7730   /* Count the elements in list pEq */
7731   for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
7732
7733   rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
7734   while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
7735     int bMatch = 1;
7736     IdxConstraint *pT = pTail;
7737     sqlite3_stmt *pInfo = 0;
7738     const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
7739
7740     /* Zero the IdxConstraint.bFlag values in the pEq list */
7741     for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
7742
7743     rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
7744     while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
7745       int iIdx = sqlite3_column_int(pInfo, 0);
7746       int iCol = sqlite3_column_int(pInfo, 1);
7747       const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
7748
7749       if( iIdx<nEq ){
7750         for(pIter=pEq; pIter; pIter=pIter->pLink){
7751           if( pIter->bFlag ) continue;
7752           if( pIter->iCol!=iCol ) continue;
7753           if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
7754           pIter->bFlag = 1;
7755           break;
7756         }
7757         if( pIter==0 ){
7758           bMatch = 0;
7759           break;
7760         }
7761       }else{
7762         if( pT ){
7763           if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
7764             bMatch = 0;
7765             break;
7766           }
7767           pT = pT->pLink;
7768         }
7769       }
7770     }
7771     idxFinalize(&rc, pInfo);
7772
7773     if( rc==SQLITE_OK && bMatch ){
7774       sqlite3_finalize(pIdxList);
7775       return 1;
7776     }
7777   }
7778   idxFinalize(&rc, pIdxList);
7779
7780   *pRc = rc;
7781   return 0;
7782 }
7783
7784 static int idxCreateFromCons(
7785   sqlite3expert *p,
7786   IdxScan *pScan,
7787   IdxConstraint *pEq, 
7788   IdxConstraint *pTail
7789 ){
7790   sqlite3 *dbm = p->dbm;
7791   int rc = SQLITE_OK;
7792   if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
7793     IdxTable *pTab = pScan->pTab;
7794     char *zCols = 0;
7795     char *zIdx = 0;
7796     IdxConstraint *pCons;
7797     unsigned int h = 0;
7798     const char *zFmt;
7799
7800     for(pCons=pEq; pCons; pCons=pCons->pLink){
7801       zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
7802     }
7803     for(pCons=pTail; pCons; pCons=pCons->pLink){
7804       zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
7805     }
7806
7807     if( rc==SQLITE_OK ){
7808       /* Hash the list of columns to come up with a name for the index */
7809       const char *zTable = pScan->pTab->zName;
7810       char *zName;                /* Index name */
7811       int i;
7812       for(i=0; zCols[i]; i++){
7813         h += ((h<<3) + zCols[i]);
7814       }
7815       zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
7816       if( zName==0 ){ 
7817         rc = SQLITE_NOMEM;
7818       }else{
7819         if( idxIdentifierRequiresQuotes(zTable) ){
7820           zFmt = "CREATE INDEX '%q' ON %Q(%s)";
7821         }else{
7822           zFmt = "CREATE INDEX %s ON %s(%s)";
7823         }
7824         zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
7825         if( !zIdx ){
7826           rc = SQLITE_NOMEM;
7827         }else{
7828           rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
7829           idxHashAdd(&rc, &p->hIdx, zName, zIdx);
7830         }
7831         sqlite3_free(zName);
7832         sqlite3_free(zIdx);
7833       }
7834     }
7835
7836     sqlite3_free(zCols);
7837   }
7838   return rc;
7839 }
7840
7841 /*
7842 ** Return true if list pList (linked by IdxConstraint.pLink) contains
7843 ** a constraint compatible with *p. Otherwise return false.
7844 */
7845 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
7846   IdxConstraint *pCmp;
7847   for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
7848     if( p->iCol==pCmp->iCol ) return 1;
7849   }
7850   return 0;
7851 }
7852
7853 static int idxCreateFromWhere(
7854   sqlite3expert *p, 
7855   IdxScan *pScan,                 /* Create indexes for this scan */
7856   IdxConstraint *pTail            /* range/ORDER BY constraints for inclusion */
7857 ){
7858   IdxConstraint *p1 = 0;
7859   IdxConstraint *pCon;
7860   int rc;
7861
7862   /* Gather up all the == constraints. */
7863   for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
7864     if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
7865       pCon->pLink = p1;
7866       p1 = pCon;
7867     }
7868   }
7869
7870   /* Create an index using the == constraints collected above. And the
7871   ** range constraint/ORDER BY terms passed in by the caller, if any. */
7872   rc = idxCreateFromCons(p, pScan, p1, pTail);
7873
7874   /* If no range/ORDER BY passed by the caller, create a version of the
7875   ** index for each range constraint.  */
7876   if( pTail==0 ){
7877     for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
7878       assert( pCon->pLink==0 );
7879       if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
7880         rc = idxCreateFromCons(p, pScan, p1, pCon);
7881       }
7882     }
7883   }
7884
7885   return rc;
7886 }
7887
7888 /*
7889 ** Create candidate indexes in database [dbm] based on the data in 
7890 ** linked-list pScan.
7891 */
7892 static int idxCreateCandidates(sqlite3expert *p){
7893   int rc = SQLITE_OK;
7894   IdxScan *pIter;
7895
7896   for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
7897     rc = idxCreateFromWhere(p, pIter, 0);
7898     if( rc==SQLITE_OK && pIter->pOrder ){
7899       rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
7900     }
7901   }
7902
7903   return rc;
7904 }
7905
7906 /*
7907 ** Free all elements of the linked list starting at pConstraint.
7908 */
7909 static void idxConstraintFree(IdxConstraint *pConstraint){
7910   IdxConstraint *pNext;
7911   IdxConstraint *p;
7912
7913   for(p=pConstraint; p; p=pNext){
7914     pNext = p->pNext;
7915     sqlite3_free(p);
7916   }
7917 }
7918
7919 /*
7920 ** Free all elements of the linked list starting from pScan up until pLast
7921 ** (pLast is not freed).
7922 */
7923 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
7924   IdxScan *p;
7925   IdxScan *pNext;
7926   for(p=pScan; p!=pLast; p=pNext){
7927     pNext = p->pNextScan;
7928     idxConstraintFree(p->pOrder);
7929     idxConstraintFree(p->pEq);
7930     idxConstraintFree(p->pRange);
7931     sqlite3_free(p);
7932   }
7933 }
7934
7935 /*
7936 ** Free all elements of the linked list starting from pStatement up 
7937 ** until pLast (pLast is not freed).
7938 */
7939 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
7940   IdxStatement *p;
7941   IdxStatement *pNext;
7942   for(p=pStatement; p!=pLast; p=pNext){
7943     pNext = p->pNext;
7944     sqlite3_free(p->zEQP);
7945     sqlite3_free(p->zIdx);
7946     sqlite3_free(p);
7947   }
7948 }
7949
7950 /*
7951 ** Free the linked list of IdxTable objects starting at pTab.
7952 */
7953 static void idxTableFree(IdxTable *pTab){
7954   IdxTable *pIter;
7955   IdxTable *pNext;
7956   for(pIter=pTab; pIter; pIter=pNext){
7957     pNext = pIter->pNext;
7958     sqlite3_free(pIter);
7959   }
7960 }
7961
7962 /*
7963 ** Free the linked list of IdxWrite objects starting at pTab.
7964 */
7965 static void idxWriteFree(IdxWrite *pTab){
7966   IdxWrite *pIter;
7967   IdxWrite *pNext;
7968   for(pIter=pTab; pIter; pIter=pNext){
7969     pNext = pIter->pNext;
7970     sqlite3_free(pIter);
7971   }
7972 }
7973
7974
7975
7976 /*
7977 ** This function is called after candidate indexes have been created. It
7978 ** runs all the queries to see which indexes they prefer, and populates
7979 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
7980 */
7981 int idxFindIndexes(
7982   sqlite3expert *p,
7983   char **pzErr                         /* OUT: Error message (sqlite3_malloc) */
7984 ){
7985   IdxStatement *pStmt;
7986   sqlite3 *dbm = p->dbm;
7987   int rc = SQLITE_OK;
7988
7989   IdxHash hIdx;
7990   idxHashInit(&hIdx);
7991
7992   for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
7993     IdxHashEntry *pEntry;
7994     sqlite3_stmt *pExplain = 0;
7995     idxHashClear(&hIdx);
7996     rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
7997         "EXPLAIN QUERY PLAN %s", pStmt->zSql
7998     );
7999     while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
8000       /* int iId = sqlite3_column_int(pExplain, 0); */
8001       /* int iParent = sqlite3_column_int(pExplain, 1); */
8002       /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
8003       const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
8004       int nDetail;
8005       int i;
8006
8007       if( !zDetail ) continue;
8008       nDetail = STRLEN(zDetail);
8009
8010       for(i=0; i<nDetail; i++){
8011         const char *zIdx = 0;
8012         if( i+13<nDetail && memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
8013           zIdx = &zDetail[i+13];
8014         }else if( i+22<nDetail 
8015             && memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0 
8016         ){
8017           zIdx = &zDetail[i+22];
8018         }
8019         if( zIdx ){
8020           const char *zSql;
8021           int nIdx = 0;
8022           while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
8023             nIdx++;
8024           }
8025           zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
8026           if( zSql ){
8027             idxHashAdd(&rc, &hIdx, zSql, 0);
8028             if( rc ) goto find_indexes_out;
8029           }
8030           break;
8031         }
8032       }
8033
8034       if( zDetail[0]!='-' ){
8035         pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
8036       }
8037     }
8038
8039     for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
8040       pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
8041     }
8042
8043     idxFinalize(&rc, pExplain);
8044   }
8045
8046  find_indexes_out:
8047   idxHashClear(&hIdx);
8048   return rc;
8049 }
8050
8051 static int idxAuthCallback(
8052   void *pCtx,
8053   int eOp,
8054   const char *z3,
8055   const char *z4,
8056   const char *zDb,
8057   const char *zTrigger
8058 ){
8059   int rc = SQLITE_OK;
8060   (void)z4;
8061   (void)zTrigger;
8062   if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
8063     if( sqlite3_stricmp(zDb, "main")==0 ){
8064       sqlite3expert *p = (sqlite3expert*)pCtx;
8065       IdxTable *pTab;
8066       for(pTab=p->pTable; pTab; pTab=pTab->pNext){
8067         if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
8068       }
8069       if( pTab ){
8070         IdxWrite *pWrite;
8071         for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
8072           if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
8073         }
8074         if( pWrite==0 ){
8075           pWrite = idxMalloc(&rc, sizeof(IdxWrite));
8076           if( rc==SQLITE_OK ){
8077             pWrite->pTab = pTab;
8078             pWrite->eOp = eOp;
8079             pWrite->pNext = p->pWrite;
8080             p->pWrite = pWrite;
8081           }
8082         }
8083       }
8084     }
8085   }
8086   return rc;
8087 }
8088
8089 static int idxProcessOneTrigger(
8090   sqlite3expert *p, 
8091   IdxWrite *pWrite, 
8092   char **pzErr
8093 ){
8094   static const char *zInt = UNIQUE_TABLE_NAME;
8095   static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
8096   IdxTable *pTab = pWrite->pTab;
8097   const char *zTab = pTab->zName;
8098   const char *zSql = 
8099     "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_master "
8100     "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
8101     "ORDER BY type;";
8102   sqlite3_stmt *pSelect = 0;
8103   int rc = SQLITE_OK;
8104   char *zWrite = 0;
8105
8106   /* Create the table and its triggers in the temp schema */
8107   rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
8108   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
8109     const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
8110     rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
8111   }
8112   idxFinalize(&rc, pSelect);
8113
8114   /* Rename the table in the temp schema to zInt */
8115   if( rc==SQLITE_OK ){
8116     char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
8117     if( z==0 ){
8118       rc = SQLITE_NOMEM;
8119     }else{
8120       rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
8121       sqlite3_free(z);
8122     }
8123   }
8124
8125   switch( pWrite->eOp ){
8126     case SQLITE_INSERT: {
8127       int i;
8128       zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
8129       for(i=0; i<pTab->nCol; i++){
8130         zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
8131       }
8132       zWrite = idxAppendText(&rc, zWrite, ")");
8133       break;
8134     }
8135     case SQLITE_UPDATE: {
8136       int i;
8137       zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
8138       for(i=0; i<pTab->nCol; i++){
8139         zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ", 
8140             pTab->aCol[i].zName
8141         );
8142       }
8143       break;
8144     }
8145     default: {
8146       assert( pWrite->eOp==SQLITE_DELETE );
8147       if( rc==SQLITE_OK ){
8148         zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
8149         if( zWrite==0 ) rc = SQLITE_NOMEM;
8150       }
8151     }
8152   }
8153
8154   if( rc==SQLITE_OK ){
8155     sqlite3_stmt *pX = 0;
8156     rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
8157     idxFinalize(&rc, pX);
8158     if( rc!=SQLITE_OK ){
8159       idxDatabaseError(p->dbv, pzErr);
8160     }
8161   }
8162   sqlite3_free(zWrite);
8163
8164   if( rc==SQLITE_OK ){
8165     rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
8166   }
8167
8168   return rc;
8169 }
8170
8171 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
8172   int rc = SQLITE_OK;
8173   IdxWrite *pEnd = 0;
8174   IdxWrite *pFirst = p->pWrite;
8175
8176   while( rc==SQLITE_OK && pFirst!=pEnd ){
8177     IdxWrite *pIter;
8178     for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
8179       rc = idxProcessOneTrigger(p, pIter, pzErr);
8180     }
8181     pEnd = pFirst;
8182     pFirst = p->pWrite;
8183   }
8184
8185   return rc;
8186 }
8187
8188
8189 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
8190   int rc = idxRegisterVtab(p);
8191   sqlite3_stmt *pSchema = 0;
8192
8193   /* For each table in the main db schema:
8194   **
8195   **   1) Add an entry to the p->pTable list, and
8196   **   2) Create the equivalent virtual table in dbv.
8197   */
8198   rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
8199       "SELECT type, name, sql, 1 FROM sqlite_master "
8200       "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
8201       " UNION ALL "
8202       "SELECT type, name, sql, 2 FROM sqlite_master "
8203       "WHERE type = 'trigger'"
8204       "  AND tbl_name IN(SELECT name FROM sqlite_master WHERE type = 'view') "
8205       "ORDER BY 4, 1"
8206   );
8207   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
8208     const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
8209     const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
8210     const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
8211
8212     if( zType[0]=='v' || zType[1]=='r' ){
8213       rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
8214     }else{
8215       IdxTable *pTab;
8216       rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
8217       if( rc==SQLITE_OK ){
8218         int i;
8219         char *zInner = 0;
8220         char *zOuter = 0;
8221         pTab->pNext = p->pTable;
8222         p->pTable = pTab;
8223
8224         /* The statement the vtab will pass to sqlite3_declare_vtab() */
8225         zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
8226         for(i=0; i<pTab->nCol; i++){
8227           zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s", 
8228               (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
8229           );
8230         }
8231         zInner = idxAppendText(&rc, zInner, ")");
8232
8233         /* The CVT statement to create the vtab */
8234         zOuter = idxAppendText(&rc, 0, 
8235             "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
8236         );
8237         if( rc==SQLITE_OK ){
8238           rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
8239         }
8240         sqlite3_free(zInner);
8241         sqlite3_free(zOuter);
8242       }
8243     }
8244   }
8245   idxFinalize(&rc, pSchema);
8246   return rc;
8247 }
8248
8249 struct IdxSampleCtx {
8250   int iTarget;
8251   double target;                  /* Target nRet/nRow value */
8252   double nRow;                    /* Number of rows seen */
8253   double nRet;                    /* Number of rows returned */
8254 };
8255
8256 static void idxSampleFunc(
8257   sqlite3_context *pCtx,
8258   int argc,
8259   sqlite3_value **argv
8260 ){
8261   struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
8262   int bRet;
8263
8264   (void)argv;
8265   assert( argc==0 );
8266   if( p->nRow==0.0 ){
8267     bRet = 1;
8268   }else{
8269     bRet = (p->nRet / p->nRow) <= p->target;
8270     if( bRet==0 ){
8271       unsigned short rnd;
8272       sqlite3_randomness(2, (void*)&rnd);
8273       bRet = ((int)rnd % 100) <= p->iTarget;
8274     }
8275   }
8276
8277   sqlite3_result_int(pCtx, bRet);
8278   p->nRow += 1.0;
8279   p->nRet += (double)bRet;
8280 }
8281
8282 struct IdxRemCtx {
8283   int nSlot;
8284   struct IdxRemSlot {
8285     int eType;                    /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
8286     i64 iVal;                     /* SQLITE_INTEGER value */
8287     double rVal;                  /* SQLITE_FLOAT value */
8288     int nByte;                    /* Bytes of space allocated at z */
8289     int n;                        /* Size of buffer z */
8290     char *z;                      /* SQLITE_TEXT/BLOB value */
8291   } aSlot[1];
8292 };
8293
8294 /*
8295 ** Implementation of scalar function rem().
8296 */
8297 static void idxRemFunc(
8298   sqlite3_context *pCtx,
8299   int argc,
8300   sqlite3_value **argv
8301 ){
8302   struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
8303   struct IdxRemSlot *pSlot;
8304   int iSlot;
8305   assert( argc==2 );
8306
8307   iSlot = sqlite3_value_int(argv[0]);
8308   assert( iSlot<=p->nSlot );
8309   pSlot = &p->aSlot[iSlot];
8310
8311   switch( pSlot->eType ){
8312     case SQLITE_NULL:
8313       /* no-op */
8314       break;
8315
8316     case SQLITE_INTEGER:
8317       sqlite3_result_int64(pCtx, pSlot->iVal);
8318       break;
8319
8320     case SQLITE_FLOAT:
8321       sqlite3_result_double(pCtx, pSlot->rVal);
8322       break;
8323
8324     case SQLITE_BLOB:
8325       sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
8326       break;
8327
8328     case SQLITE_TEXT:
8329       sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
8330       break;
8331   }
8332
8333   pSlot->eType = sqlite3_value_type(argv[1]);
8334   switch( pSlot->eType ){
8335     case SQLITE_NULL:
8336       /* no-op */
8337       break;
8338
8339     case SQLITE_INTEGER:
8340       pSlot->iVal = sqlite3_value_int64(argv[1]);
8341       break;
8342
8343     case SQLITE_FLOAT:
8344       pSlot->rVal = sqlite3_value_double(argv[1]);
8345       break;
8346
8347     case SQLITE_BLOB:
8348     case SQLITE_TEXT: {
8349       int nByte = sqlite3_value_bytes(argv[1]);
8350       if( nByte>pSlot->nByte ){
8351         char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
8352         if( zNew==0 ){
8353           sqlite3_result_error_nomem(pCtx);
8354           return;
8355         }
8356         pSlot->nByte = nByte*2;
8357         pSlot->z = zNew;
8358       }
8359       pSlot->n = nByte;
8360       if( pSlot->eType==SQLITE_BLOB ){
8361         memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
8362       }else{
8363         memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
8364       }
8365       break;
8366     }
8367   }
8368 }
8369
8370 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
8371   int rc = SQLITE_OK;
8372   const char *zMax = 
8373     "SELECT max(i.seqno) FROM "
8374     "  sqlite_master AS s, "
8375     "  pragma_index_list(s.name) AS l, "
8376     "  pragma_index_info(l.name) AS i "
8377     "WHERE s.type = 'table'";
8378   sqlite3_stmt *pMax = 0;
8379
8380   *pnMax = 0;
8381   rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
8382   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
8383     *pnMax = sqlite3_column_int(pMax, 0) + 1;
8384   }
8385   idxFinalize(&rc, pMax);
8386
8387   return rc;
8388 }
8389
8390 static int idxPopulateOneStat1(
8391   sqlite3expert *p,
8392   sqlite3_stmt *pIndexXInfo,
8393   sqlite3_stmt *pWriteStat,
8394   const char *zTab,
8395   const char *zIdx,
8396   char **pzErr
8397 ){
8398   char *zCols = 0;
8399   char *zOrder = 0;
8400   char *zQuery = 0;
8401   int nCol = 0;
8402   int i;
8403   sqlite3_stmt *pQuery = 0;
8404   int *aStat = 0;
8405   int rc = SQLITE_OK;
8406
8407   assert( p->iSample>0 );
8408
8409   /* Formulate the query text */
8410   sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
8411   while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
8412     const char *zComma = zCols==0 ? "" : ", ";
8413     const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
8414     const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
8415     zCols = idxAppendText(&rc, zCols, 
8416         "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
8417     );
8418     zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
8419   }
8420   sqlite3_reset(pIndexXInfo);
8421   if( rc==SQLITE_OK ){
8422     if( p->iSample==100 ){
8423       zQuery = sqlite3_mprintf(
8424           "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
8425       );
8426     }else{
8427       zQuery = sqlite3_mprintf(
8428           "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
8429       );
8430     }
8431   }
8432   sqlite3_free(zCols);
8433   sqlite3_free(zOrder);
8434
8435   /* Formulate the query text */
8436   if( rc==SQLITE_OK ){
8437     sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
8438     rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
8439   }
8440   sqlite3_free(zQuery);
8441
8442   if( rc==SQLITE_OK ){
8443     aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
8444   }
8445   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
8446     IdxHashEntry *pEntry;
8447     char *zStat = 0;
8448     for(i=0; i<=nCol; i++) aStat[i] = 1;
8449     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
8450       aStat[0]++;
8451       for(i=0; i<nCol; i++){
8452         if( sqlite3_column_int(pQuery, i)==0 ) break;
8453       }
8454       for(/*no-op*/; i<nCol; i++){
8455         aStat[i+1]++;
8456       }
8457     }
8458
8459     if( rc==SQLITE_OK ){
8460       int s0 = aStat[0];
8461       zStat = sqlite3_mprintf("%d", s0);
8462       if( zStat==0 ) rc = SQLITE_NOMEM;
8463       for(i=1; rc==SQLITE_OK && i<=nCol; i++){
8464         zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
8465       }
8466     }
8467
8468     if( rc==SQLITE_OK ){
8469       sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
8470       sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
8471       sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
8472       sqlite3_step(pWriteStat);
8473       rc = sqlite3_reset(pWriteStat);
8474     }
8475
8476     pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
8477     if( pEntry ){
8478       assert( pEntry->zVal2==0 );
8479       pEntry->zVal2 = zStat;
8480     }else{
8481       sqlite3_free(zStat);
8482     }
8483   }
8484   sqlite3_free(aStat);
8485   idxFinalize(&rc, pQuery);
8486
8487   return rc;
8488 }
8489
8490 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
8491   int rc;
8492   char *zSql;
8493
8494   rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
8495   if( rc!=SQLITE_OK ) return rc;
8496
8497   zSql = sqlite3_mprintf(
8498       "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
8499   );
8500   if( zSql==0 ) return SQLITE_NOMEM;
8501   rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
8502   sqlite3_free(zSql);
8503
8504   return rc;
8505 }
8506
8507 /*
8508 ** This function is called as part of sqlite3_expert_analyze(). Candidate
8509 ** indexes have already been created in database sqlite3expert.dbm, this
8510 ** function populates sqlite_stat1 table in the same database.
8511 **
8512 ** The stat1 data is generated by querying the 
8513 */
8514 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
8515   int rc = SQLITE_OK;
8516   int nMax =0;
8517   struct IdxRemCtx *pCtx = 0;
8518   struct IdxSampleCtx samplectx; 
8519   int i;
8520   i64 iPrev = -100000;
8521   sqlite3_stmt *pAllIndex = 0;
8522   sqlite3_stmt *pIndexXInfo = 0;
8523   sqlite3_stmt *pWrite = 0;
8524
8525   const char *zAllIndex =
8526     "SELECT s.rowid, s.name, l.name FROM "
8527     "  sqlite_master AS s, "
8528     "  pragma_index_list(s.name) AS l "
8529     "WHERE s.type = 'table'";
8530   const char *zIndexXInfo = 
8531     "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
8532   const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
8533
8534   /* If iSample==0, no sqlite_stat1 data is required. */
8535   if( p->iSample==0 ) return SQLITE_OK;
8536
8537   rc = idxLargestIndex(p->dbm, &nMax, pzErr);
8538   if( nMax<=0 || rc!=SQLITE_OK ) return rc;
8539
8540   rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
8541
8542   if( rc==SQLITE_OK ){
8543     int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
8544     pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
8545   }
8546
8547   if( rc==SQLITE_OK ){
8548     sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
8549     rc = sqlite3_create_function(
8550         dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
8551     );
8552   }
8553   if( rc==SQLITE_OK ){
8554     rc = sqlite3_create_function(
8555         p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
8556     );
8557   }
8558
8559   if( rc==SQLITE_OK ){
8560     pCtx->nSlot = nMax+1;
8561     rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
8562   }
8563   if( rc==SQLITE_OK ){
8564     rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
8565   }
8566   if( rc==SQLITE_OK ){
8567     rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
8568   }
8569
8570   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
8571     i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
8572     const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
8573     const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
8574     if( p->iSample<100 && iPrev!=iRowid ){
8575       samplectx.target = (double)p->iSample / 100.0;
8576       samplectx.iTarget = p->iSample;
8577       samplectx.nRow = 0.0;
8578       samplectx.nRet = 0.0;
8579       rc = idxBuildSampleTable(p, zTab);
8580       if( rc!=SQLITE_OK ) break;
8581     }
8582     rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
8583     iPrev = iRowid;
8584   }
8585   if( rc==SQLITE_OK && p->iSample<100 ){
8586     rc = sqlite3_exec(p->dbv, 
8587         "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
8588     );
8589   }
8590
8591   idxFinalize(&rc, pAllIndex);
8592   idxFinalize(&rc, pIndexXInfo);
8593   idxFinalize(&rc, pWrite);
8594
8595   for(i=0; i<pCtx->nSlot; i++){
8596     sqlite3_free(pCtx->aSlot[i].z);
8597   }
8598   sqlite3_free(pCtx);
8599
8600   if( rc==SQLITE_OK ){
8601     rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_master", 0, 0, 0);
8602   }
8603
8604   sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
8605   return rc;
8606 }
8607
8608 /*
8609 ** Allocate a new sqlite3expert object.
8610 */
8611 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
8612   int rc = SQLITE_OK;
8613   sqlite3expert *pNew;
8614
8615   pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
8616
8617   /* Open two in-memory databases to work with. The "vtab database" (dbv)
8618   ** will contain a virtual table corresponding to each real table in
8619   ** the user database schema, and a copy of each view. It is used to
8620   ** collect information regarding the WHERE, ORDER BY and other clauses
8621   ** of the user's query.
8622   */
8623   if( rc==SQLITE_OK ){
8624     pNew->db = db;
8625     pNew->iSample = 100;
8626     rc = sqlite3_open(":memory:", &pNew->dbv);
8627   }
8628   if( rc==SQLITE_OK ){
8629     rc = sqlite3_open(":memory:", &pNew->dbm);
8630     if( rc==SQLITE_OK ){
8631       sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
8632     }
8633   }
8634   
8635
8636   /* Copy the entire schema of database [db] into [dbm]. */
8637   if( rc==SQLITE_OK ){
8638     sqlite3_stmt *pSql;
8639     rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg, 
8640         "SELECT sql FROM sqlite_master WHERE name NOT LIKE 'sqlite_%%'"
8641         " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
8642     );
8643     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
8644       const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
8645       rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
8646     }
8647     idxFinalize(&rc, pSql);
8648   }
8649
8650   /* Create the vtab schema */
8651   if( rc==SQLITE_OK ){
8652     rc = idxCreateVtabSchema(pNew, pzErrmsg);
8653   }
8654
8655   /* Register the auth callback with dbv */
8656   if( rc==SQLITE_OK ){
8657     sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
8658   }
8659
8660   /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
8661   ** return the new sqlite3expert handle.  */
8662   if( rc!=SQLITE_OK ){
8663     sqlite3_expert_destroy(pNew);
8664     pNew = 0;
8665   }
8666   return pNew;
8667 }
8668
8669 /*
8670 ** Configure an sqlite3expert object.
8671 */
8672 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
8673   int rc = SQLITE_OK;
8674   va_list ap;
8675   va_start(ap, op);
8676   switch( op ){
8677     case EXPERT_CONFIG_SAMPLE: {
8678       int iVal = va_arg(ap, int);
8679       if( iVal<0 ) iVal = 0;
8680       if( iVal>100 ) iVal = 100;
8681       p->iSample = iVal;
8682       break;
8683     }
8684     default:
8685       rc = SQLITE_NOTFOUND;
8686       break;
8687   }
8688
8689   va_end(ap);
8690   return rc;
8691 }
8692
8693 /*
8694 ** Add an SQL statement to the analysis.
8695 */
8696 int sqlite3_expert_sql(
8697   sqlite3expert *p,               /* From sqlite3_expert_new() */
8698   const char *zSql,               /* SQL statement to add */
8699   char **pzErr                    /* OUT: Error message (if any) */
8700 ){
8701   IdxScan *pScanOrig = p->pScan;
8702   IdxStatement *pStmtOrig = p->pStatement;
8703   int rc = SQLITE_OK;
8704   const char *zStmt = zSql;
8705
8706   if( p->bRun ) return SQLITE_MISUSE;
8707
8708   while( rc==SQLITE_OK && zStmt && zStmt[0] ){
8709     sqlite3_stmt *pStmt = 0;
8710     rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
8711     if( rc==SQLITE_OK ){
8712       if( pStmt ){
8713         IdxStatement *pNew;
8714         const char *z = sqlite3_sql(pStmt);
8715         int n = STRLEN(z);
8716         pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
8717         if( rc==SQLITE_OK ){
8718           pNew->zSql = (char*)&pNew[1];
8719           memcpy(pNew->zSql, z, n+1);
8720           pNew->pNext = p->pStatement;
8721           if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
8722           p->pStatement = pNew;
8723         }
8724         sqlite3_finalize(pStmt);
8725       }
8726     }else{
8727       idxDatabaseError(p->dbv, pzErr);
8728     }
8729   }
8730
8731   if( rc!=SQLITE_OK ){
8732     idxScanFree(p->pScan, pScanOrig);
8733     idxStatementFree(p->pStatement, pStmtOrig);
8734     p->pScan = pScanOrig;
8735     p->pStatement = pStmtOrig;
8736   }
8737
8738   return rc;
8739 }
8740
8741 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
8742   int rc;
8743   IdxHashEntry *pEntry;
8744
8745   /* Do trigger processing to collect any extra IdxScan structures */
8746   rc = idxProcessTriggers(p, pzErr);
8747
8748   /* Create candidate indexes within the in-memory database file */
8749   if( rc==SQLITE_OK ){
8750     rc = idxCreateCandidates(p);
8751   }
8752
8753   /* Generate the stat1 data */
8754   if( rc==SQLITE_OK ){
8755     rc = idxPopulateStat1(p, pzErr);
8756   }
8757
8758   /* Formulate the EXPERT_REPORT_CANDIDATES text */
8759   for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
8760     p->zCandidates = idxAppendText(&rc, p->zCandidates, 
8761         "%s;%s%s\n", pEntry->zVal, 
8762         pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
8763     );
8764   }
8765
8766   /* Figure out which of the candidate indexes are preferred by the query
8767   ** planner and report the results to the user.  */
8768   if( rc==SQLITE_OK ){
8769     rc = idxFindIndexes(p, pzErr);
8770   }
8771
8772   if( rc==SQLITE_OK ){
8773     p->bRun = 1;
8774   }
8775   return rc;
8776 }
8777
8778 /*
8779 ** Return the total number of statements that have been added to this
8780 ** sqlite3expert using sqlite3_expert_sql().
8781 */
8782 int sqlite3_expert_count(sqlite3expert *p){
8783   int nRet = 0;
8784   if( p->pStatement ) nRet = p->pStatement->iId+1;
8785   return nRet;
8786 }
8787
8788 /*
8789 ** Return a component of the report.
8790 */
8791 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
8792   const char *zRet = 0;
8793   IdxStatement *pStmt;
8794
8795   if( p->bRun==0 ) return 0;
8796   for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
8797   switch( eReport ){
8798     case EXPERT_REPORT_SQL:
8799       if( pStmt ) zRet = pStmt->zSql;
8800       break;
8801     case EXPERT_REPORT_INDEXES:
8802       if( pStmt ) zRet = pStmt->zIdx;
8803       break;
8804     case EXPERT_REPORT_PLAN:
8805       if( pStmt ) zRet = pStmt->zEQP;
8806       break;
8807     case EXPERT_REPORT_CANDIDATES:
8808       zRet = p->zCandidates;
8809       break;
8810   }
8811   return zRet;
8812 }
8813
8814 /*
8815 ** Free an sqlite3expert object.
8816 */
8817 void sqlite3_expert_destroy(sqlite3expert *p){
8818   if( p ){
8819     sqlite3_close(p->dbm);
8820     sqlite3_close(p->dbv);
8821     idxScanFree(p->pScan, 0);
8822     idxStatementFree(p->pStatement, 0);
8823     idxTableFree(p->pTable);
8824     idxWriteFree(p->pWrite);
8825     idxHashClear(&p->hIdx);
8826     sqlite3_free(p->zCandidates);
8827     sqlite3_free(p);
8828   }
8829 }
8830
8831 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
8832
8833 /************************* End ../ext/expert/sqlite3expert.c ********************/
8834
8835 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
8836 /************************* Begin ../ext/misc/dbdata.c ******************/
8837 /*
8838 ** 2019-04-17
8839 **
8840 ** The author disclaims copyright to this source code.  In place of
8841 ** a legal notice, here is a blessing:
8842 **
8843 **    May you do good and not evil.
8844 **    May you find forgiveness for yourself and forgive others.
8845 **    May you share freely, never taking more than you give.
8846 **
8847 ******************************************************************************
8848 **
8849 ** This file contains an implementation of two eponymous virtual tables,
8850 ** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
8851 ** "sqlite_dbpage" eponymous virtual table be available.
8852 **
8853 ** SQLITE_DBDATA:
8854 **   sqlite_dbdata is used to extract data directly from a database b-tree
8855 **   page and its associated overflow pages, bypassing the b-tree layer.
8856 **   The table schema is equivalent to:
8857 **
8858 **     CREATE TABLE sqlite_dbdata(
8859 **       pgno INTEGER,
8860 **       cell INTEGER,
8861 **       field INTEGER,
8862 **       value ANY,
8863 **       schema TEXT HIDDEN
8864 **     );
8865 **
8866 **   IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
8867 **   FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
8868 **   "schema".
8869 **
8870 **   Each page of the database is inspected. If it cannot be interpreted as
8871 **   a b-tree page, or if it is a b-tree page containing 0 entries, the
8872 **   sqlite_dbdata table contains no rows for that page.  Otherwise, the
8873 **   table contains one row for each field in the record associated with
8874 **   each cell on the page. For intkey b-trees, the key value is stored in
8875 **   field -1.
8876 **
8877 **   For example, for the database:
8878 **
8879 **     CREATE TABLE t1(a, b);     -- root page is page 2
8880 **     INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
8881 **     INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
8882 **
8883 **   the sqlite_dbdata table contains, as well as from entries related to 
8884 **   page 1, content equivalent to:
8885 **
8886 **     INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
8887 **         (2, 0, -1, 5     ),
8888 **         (2, 0,  0, 'v'   ),
8889 **         (2, 0,  1, 'five'),
8890 **         (2, 1, -1, 10    ),
8891 **         (2, 1,  0, 'x'   ),
8892 **         (2, 1,  1, 'ten' );
8893 **
8894 **   If database corruption is encountered, this module does not report an
8895 **   error. Instead, it attempts to extract as much data as possible and
8896 **   ignores the corruption.
8897 **
8898 ** SQLITE_DBPTR:
8899 **   The sqlite_dbptr table has the following schema:
8900 **
8901 **     CREATE TABLE sqlite_dbptr(
8902 **       pgno INTEGER,
8903 **       child INTEGER,
8904 **       schema TEXT HIDDEN
8905 **     );
8906 **
8907 **   It contains one entry for each b-tree pointer between a parent and
8908 **   child page in the database.
8909 */
8910 #if !defined(SQLITEINT_H) 
8911 /* #include "sqlite3ext.h" */
8912
8913 /* typedef unsigned char u8; */
8914
8915 #endif
8916 SQLITE_EXTENSION_INIT1
8917 #include <string.h>
8918 #include <assert.h>
8919
8920 #define DBDATA_PADDING_BYTES 100 
8921
8922 typedef struct DbdataTable DbdataTable;
8923 typedef struct DbdataCursor DbdataCursor;
8924
8925 /* Cursor object */
8926 struct DbdataCursor {
8927   sqlite3_vtab_cursor base;       /* Base class.  Must be first */
8928   sqlite3_stmt *pStmt;            /* For fetching database pages */
8929
8930   int iPgno;                      /* Current page number */
8931   u8 *aPage;                      /* Buffer containing page */
8932   int nPage;                      /* Size of aPage[] in bytes */
8933   int nCell;                      /* Number of cells on aPage[] */
8934   int iCell;                      /* Current cell number */
8935   int bOnePage;                   /* True to stop after one page */
8936   int szDb;
8937   sqlite3_int64 iRowid;
8938
8939   /* Only for the sqlite_dbdata table */
8940   u8 *pRec;                       /* Buffer containing current record */
8941   int nRec;                       /* Size of pRec[] in bytes */
8942   int nHdr;                       /* Size of header in bytes */
8943   int iField;                     /* Current field number */
8944   u8 *pHdrPtr;
8945   u8 *pPtr;
8946   
8947   sqlite3_int64 iIntkey;          /* Integer key value */
8948 };
8949
8950 /* Table object */
8951 struct DbdataTable {
8952   sqlite3_vtab base;              /* Base class.  Must be first */
8953   sqlite3 *db;                    /* The database connection */
8954   sqlite3_stmt *pStmt;            /* For fetching database pages */
8955   int bPtr;                       /* True for sqlite3_dbptr table */
8956 };
8957
8958 /* Column and schema definitions for sqlite_dbdata */
8959 #define DBDATA_COLUMN_PGNO        0
8960 #define DBDATA_COLUMN_CELL        1
8961 #define DBDATA_COLUMN_FIELD       2
8962 #define DBDATA_COLUMN_VALUE       3
8963 #define DBDATA_COLUMN_SCHEMA      4
8964 #define DBDATA_SCHEMA             \
8965       "CREATE TABLE x("           \
8966       "  pgno INTEGER,"           \
8967       "  cell INTEGER,"           \
8968       "  field INTEGER,"          \
8969       "  value ANY,"              \
8970       "  schema TEXT HIDDEN"      \
8971       ")"
8972
8973 /* Column and schema definitions for sqlite_dbptr */
8974 #define DBPTR_COLUMN_PGNO         0
8975 #define DBPTR_COLUMN_CHILD        1
8976 #define DBPTR_COLUMN_SCHEMA       2
8977 #define DBPTR_SCHEMA              \
8978       "CREATE TABLE x("           \
8979       "  pgno INTEGER,"           \
8980       "  child INTEGER,"          \
8981       "  schema TEXT HIDDEN"      \
8982       ")"
8983
8984 /*
8985 ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual 
8986 ** table.
8987 */
8988 static int dbdataConnect(
8989   sqlite3 *db,
8990   void *pAux,
8991   int argc, const char *const*argv,
8992   sqlite3_vtab **ppVtab,
8993   char **pzErr
8994 ){
8995   DbdataTable *pTab = 0;
8996   int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
8997
8998   if( rc==SQLITE_OK ){
8999     pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
9000     if( pTab==0 ){
9001       rc = SQLITE_NOMEM;
9002     }else{
9003       memset(pTab, 0, sizeof(DbdataTable));
9004       pTab->db = db;
9005       pTab->bPtr = (pAux!=0);
9006     }
9007   }
9008
9009   *ppVtab = (sqlite3_vtab*)pTab;
9010   return rc;
9011 }
9012
9013 /*
9014 ** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
9015 */
9016 static int dbdataDisconnect(sqlite3_vtab *pVtab){
9017   DbdataTable *pTab = (DbdataTable*)pVtab;
9018   if( pTab ){
9019     sqlite3_finalize(pTab->pStmt);
9020     sqlite3_free(pVtab);
9021   }
9022   return SQLITE_OK;
9023 }
9024
9025 /*
9026 ** This function interprets two types of constraints:
9027 **
9028 **       schema=?
9029 **       pgno=?
9030 **
9031 ** If neither are present, idxNum is set to 0. If schema=? is present,
9032 ** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
9033 ** in idxNum is set.
9034 **
9035 ** If both parameters are present, schema is in position 0 and pgno in
9036 ** position 1.
9037 */
9038 static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
9039   DbdataTable *pTab = (DbdataTable*)tab;
9040   int i;
9041   int iSchema = -1;
9042   int iPgno = -1;
9043   int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
9044
9045   for(i=0; i<pIdx->nConstraint; i++){
9046     struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
9047     if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
9048       if( p->iColumn==colSchema ){
9049         if( p->usable==0 ) return SQLITE_CONSTRAINT;
9050         iSchema = i;
9051       }
9052       if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
9053         iPgno = i;
9054       }
9055     }
9056   }
9057
9058   if( iSchema>=0 ){
9059     pIdx->aConstraintUsage[iSchema].argvIndex = 1;
9060     pIdx->aConstraintUsage[iSchema].omit = 1;
9061   }
9062   if( iPgno>=0 ){
9063     pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
9064     pIdx->aConstraintUsage[iPgno].omit = 1;
9065     pIdx->estimatedCost = 100;
9066     pIdx->estimatedRows =  50;
9067
9068     if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
9069       int iCol = pIdx->aOrderBy[0].iColumn;
9070       if( pIdx->nOrderBy==1 ){
9071         pIdx->orderByConsumed = (iCol==0 || iCol==1);
9072       }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
9073         pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
9074       }
9075     }
9076
9077   }else{
9078     pIdx->estimatedCost = 100000000;
9079     pIdx->estimatedRows = 1000000000;
9080   }
9081   pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
9082   return SQLITE_OK;
9083 }
9084
9085 /*
9086 ** Open a new sqlite_dbdata or sqlite_dbptr cursor.
9087 */
9088 static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
9089   DbdataCursor *pCsr;
9090
9091   pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
9092   if( pCsr==0 ){
9093     return SQLITE_NOMEM;
9094   }else{
9095     memset(pCsr, 0, sizeof(DbdataCursor));
9096     pCsr->base.pVtab = pVTab;
9097   }
9098
9099   *ppCursor = (sqlite3_vtab_cursor *)pCsr;
9100   return SQLITE_OK;
9101 }
9102
9103 /*
9104 ** Restore a cursor object to the state it was in when first allocated 
9105 ** by dbdataOpen().
9106 */
9107 static void dbdataResetCursor(DbdataCursor *pCsr){
9108   DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
9109   if( pTab->pStmt==0 ){
9110     pTab->pStmt = pCsr->pStmt;
9111   }else{
9112     sqlite3_finalize(pCsr->pStmt);
9113   }
9114   pCsr->pStmt = 0;
9115   pCsr->iPgno = 1;
9116   pCsr->iCell = 0;
9117   pCsr->iField = 0;
9118   pCsr->bOnePage = 0;
9119   sqlite3_free(pCsr->aPage);
9120   sqlite3_free(pCsr->pRec);
9121   pCsr->pRec = 0;
9122   pCsr->aPage = 0;
9123 }
9124
9125 /*
9126 ** Close an sqlite_dbdata or sqlite_dbptr cursor.
9127 */
9128 static int dbdataClose(sqlite3_vtab_cursor *pCursor){
9129   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9130   dbdataResetCursor(pCsr);
9131   sqlite3_free(pCsr);
9132   return SQLITE_OK;
9133 }
9134
9135 /* 
9136 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers. 
9137 */
9138 static unsigned int get_uint16(unsigned char *a){
9139   return (a[0]<<8)|a[1];
9140 }
9141 static unsigned int get_uint32(unsigned char *a){
9142   return ((unsigned int)a[0]<<24)
9143        | ((unsigned int)a[1]<<16)
9144        | ((unsigned int)a[2]<<8)
9145        | ((unsigned int)a[3]);
9146 }
9147
9148 /*
9149 ** Load page pgno from the database via the sqlite_dbpage virtual table.
9150 ** If successful, set (*ppPage) to point to a buffer containing the page
9151 ** data, (*pnPage) to the size of that buffer in bytes and return
9152 ** SQLITE_OK. In this case it is the responsibility of the caller to
9153 ** eventually free the buffer using sqlite3_free().
9154 **
9155 ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
9156 ** return an SQLite error code.
9157 */
9158 static int dbdataLoadPage(
9159   DbdataCursor *pCsr,             /* Cursor object */
9160   unsigned int pgno,              /* Page number of page to load */
9161   u8 **ppPage,                    /* OUT: pointer to page buffer */
9162   int *pnPage                     /* OUT: Size of (*ppPage) in bytes */
9163 ){
9164   int rc2;
9165   int rc = SQLITE_OK;
9166   sqlite3_stmt *pStmt = pCsr->pStmt;
9167
9168   *ppPage = 0;
9169   *pnPage = 0;
9170   sqlite3_bind_int64(pStmt, 2, pgno);
9171   if( SQLITE_ROW==sqlite3_step(pStmt) ){
9172     int nCopy = sqlite3_column_bytes(pStmt, 0);
9173     if( nCopy>0 ){
9174       u8 *pPage;
9175       pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
9176       if( pPage==0 ){
9177         rc = SQLITE_NOMEM;
9178       }else{
9179         const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
9180         memcpy(pPage, pCopy, nCopy);
9181         memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
9182       }
9183       *ppPage = pPage;
9184       *pnPage = nCopy;
9185     }
9186   }
9187   rc2 = sqlite3_reset(pStmt);
9188   if( rc==SQLITE_OK ) rc = rc2;
9189
9190   return rc;
9191 }
9192
9193 /*
9194 ** Read a varint.  Put the value in *pVal and return the number of bytes.
9195 */
9196 static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
9197   sqlite3_int64 v = 0;
9198   int i;
9199   for(i=0; i<8; i++){
9200     v = (v<<7) + (z[i]&0x7f);
9201     if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
9202   }
9203   v = (v<<8) + (z[i]&0xff);
9204   *pVal = v;
9205   return 9;
9206 }
9207
9208 /*
9209 ** Return the number of bytes of space used by an SQLite value of type
9210 ** eType.
9211 */
9212 static int dbdataValueBytes(int eType){
9213   switch( eType ){
9214     case 0: case 8: case 9:
9215     case 10: case 11:
9216       return 0;
9217     case 1:
9218       return 1;
9219     case 2:
9220       return 2;
9221     case 3:
9222       return 3;
9223     case 4:
9224       return 4;
9225     case 5:
9226       return 6;
9227     case 6:
9228     case 7:
9229       return 8;
9230     default:
9231       if( eType>0 ){
9232         return ((eType-12) / 2);
9233       }
9234       return 0;
9235   }
9236 }
9237
9238 /*
9239 ** Load a value of type eType from buffer pData and use it to set the
9240 ** result of context object pCtx.
9241 */
9242 static void dbdataValue(
9243   sqlite3_context *pCtx, 
9244   int eType, 
9245   u8 *pData,
9246   int nData
9247 ){
9248   if( eType>=0 && dbdataValueBytes(eType)<=nData ){
9249     switch( eType ){
9250       case 0: 
9251       case 10: 
9252       case 11: 
9253         sqlite3_result_null(pCtx);
9254         break;
9255       
9256       case 8: 
9257         sqlite3_result_int(pCtx, 0);
9258         break;
9259       case 9:
9260         sqlite3_result_int(pCtx, 1);
9261         break;
9262   
9263       case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
9264         sqlite3_uint64 v = (signed char)pData[0];
9265         pData++;
9266         switch( eType ){
9267           case 7:
9268           case 6:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
9269           case 5:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
9270           case 4:  v = (v<<8) + pData[0];  pData++;
9271           case 3:  v = (v<<8) + pData[0];  pData++;
9272           case 2:  v = (v<<8) + pData[0];  pData++;
9273         }
9274   
9275         if( eType==7 ){
9276           double r;
9277           memcpy(&r, &v, sizeof(r));
9278           sqlite3_result_double(pCtx, r);
9279         }else{
9280           sqlite3_result_int64(pCtx, (sqlite3_int64)v);
9281         }
9282         break;
9283       }
9284   
9285       default: {
9286         int n = ((eType-12) / 2);
9287         if( eType % 2 ){
9288           sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);
9289         }else{
9290           sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
9291         }
9292       }
9293     }
9294   }
9295 }
9296
9297 /*
9298 ** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
9299 */
9300 static int dbdataNext(sqlite3_vtab_cursor *pCursor){
9301   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9302   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
9303
9304   pCsr->iRowid++;
9305   while( 1 ){
9306     int rc;
9307     int iOff = (pCsr->iPgno==1 ? 100 : 0);
9308     int bNextPage = 0;
9309
9310     if( pCsr->aPage==0 ){
9311       while( 1 ){
9312         if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
9313         rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
9314         if( rc!=SQLITE_OK ) return rc;
9315         if( pCsr->aPage ) break;
9316         pCsr->iPgno++;
9317       }
9318       pCsr->iCell = pTab->bPtr ? -2 : 0;
9319       pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
9320     }
9321
9322     if( pTab->bPtr ){
9323       if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
9324         pCsr->iCell = pCsr->nCell;
9325       }
9326       pCsr->iCell++;
9327       if( pCsr->iCell>=pCsr->nCell ){
9328         sqlite3_free(pCsr->aPage);
9329         pCsr->aPage = 0;
9330         if( pCsr->bOnePage ) return SQLITE_OK;
9331         pCsr->iPgno++;
9332       }else{
9333         return SQLITE_OK;
9334       }
9335     }else{
9336       /* If there is no record loaded, load it now. */
9337       if( pCsr->pRec==0 ){
9338         int bHasRowid = 0;
9339         int nPointer = 0;
9340         sqlite3_int64 nPayload = 0;
9341         sqlite3_int64 nHdr = 0;
9342         int iHdr;
9343         int U, X;
9344         int nLocal;
9345   
9346         switch( pCsr->aPage[iOff] ){
9347           case 0x02:
9348             nPointer = 4;
9349             break;
9350           case 0x0a:
9351             break;
9352           case 0x0d:
9353             bHasRowid = 1;
9354             break;
9355           default:
9356             /* This is not a b-tree page with records on it. Continue. */
9357             pCsr->iCell = pCsr->nCell;
9358             break;
9359         }
9360
9361         if( pCsr->iCell>=pCsr->nCell ){
9362           bNextPage = 1;
9363         }else{
9364   
9365           iOff += 8 + nPointer + pCsr->iCell*2;
9366           if( iOff>pCsr->nPage ){
9367             bNextPage = 1;
9368           }else{
9369             iOff = get_uint16(&pCsr->aPage[iOff]);
9370           }
9371     
9372           /* For an interior node cell, skip past the child-page number */
9373           iOff += nPointer;
9374     
9375           /* Load the "byte of payload including overflow" field */
9376           if( bNextPage || iOff>pCsr->nPage ){
9377             bNextPage = 1;
9378           }else{
9379             iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);
9380           }
9381     
9382           /* If this is a leaf intkey cell, load the rowid */
9383           if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
9384             iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
9385           }
9386     
9387           /* Figure out how much data to read from the local page */
9388           U = pCsr->nPage;
9389           if( bHasRowid ){
9390             X = U-35;
9391           }else{
9392             X = ((U-12)*64/255)-23;
9393           }
9394           if( nPayload<=X ){
9395             nLocal = nPayload;
9396           }else{
9397             int M, K;
9398             M = ((U-12)*32/255)-23;
9399             K = M+((nPayload-M)%(U-4));
9400             if( K<=X ){
9401               nLocal = K;
9402             }else{
9403               nLocal = M;
9404             }
9405           }
9406
9407           if( bNextPage || nLocal+iOff>pCsr->nPage ){
9408             bNextPage = 1;
9409           }else{
9410
9411             /* Allocate space for payload. And a bit more to catch small buffer
9412             ** overruns caused by attempting to read a varint or similar from 
9413             ** near the end of a corrupt record.  */
9414             pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
9415             if( pCsr->pRec==0 ) return SQLITE_NOMEM;
9416             memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
9417             pCsr->nRec = nPayload;
9418
9419             /* Load the nLocal bytes of payload */
9420             memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
9421             iOff += nLocal;
9422
9423             /* Load content from overflow pages */
9424             if( nPayload>nLocal ){
9425               sqlite3_int64 nRem = nPayload - nLocal;
9426               unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
9427               while( nRem>0 ){
9428                 u8 *aOvfl = 0;
9429                 int nOvfl = 0;
9430                 int nCopy;
9431                 rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
9432                 assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
9433                 if( rc!=SQLITE_OK ) return rc;
9434                 if( aOvfl==0 ) break;
9435
9436                 nCopy = U-4;
9437                 if( nCopy>nRem ) nCopy = nRem;
9438                 memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
9439                 nRem -= nCopy;
9440
9441                 pgnoOvfl = get_uint32(aOvfl);
9442                 sqlite3_free(aOvfl);
9443               }
9444             }
9445     
9446             iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
9447             pCsr->nHdr = nHdr;
9448             pCsr->pHdrPtr = &pCsr->pRec[iHdr];
9449             pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
9450             pCsr->iField = (bHasRowid ? -1 : 0);
9451           }
9452         }
9453       }else{
9454         pCsr->iField++;
9455         if( pCsr->iField>0 ){
9456           sqlite3_int64 iType;
9457           if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
9458             bNextPage = 1;
9459           }else{
9460             pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType);
9461             pCsr->pPtr += dbdataValueBytes(iType);
9462           }
9463         }
9464       }
9465
9466       if( bNextPage ){
9467         sqlite3_free(pCsr->aPage);
9468         sqlite3_free(pCsr->pRec);
9469         pCsr->aPage = 0;
9470         pCsr->pRec = 0;
9471         if( pCsr->bOnePage ) return SQLITE_OK;
9472         pCsr->iPgno++;
9473       }else{
9474         if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
9475           return SQLITE_OK;
9476         }
9477
9478         /* Advance to the next cell. The next iteration of the loop will load
9479         ** the record and so on. */
9480         sqlite3_free(pCsr->pRec);
9481         pCsr->pRec = 0;
9482         pCsr->iCell++;
9483       }
9484     }
9485   }
9486
9487   assert( !"can't get here" );
9488   return SQLITE_OK;
9489 }
9490
9491 /* 
9492 ** Return true if the cursor is at EOF.
9493 */
9494 static int dbdataEof(sqlite3_vtab_cursor *pCursor){
9495   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9496   return pCsr->aPage==0;
9497 }
9498
9499 /* 
9500 ** Determine the size in pages of database zSchema (where zSchema is
9501 ** "main", "temp" or the name of an attached database) and set 
9502 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
9503 ** an SQLite error code.
9504 */
9505 static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
9506   DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
9507   char *zSql = 0;
9508   int rc, rc2;
9509   sqlite3_stmt *pStmt = 0;
9510
9511   zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
9512   if( zSql==0 ) return SQLITE_NOMEM;
9513   rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
9514   sqlite3_free(zSql);
9515   if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9516     pCsr->szDb = sqlite3_column_int(pStmt, 0);
9517   }
9518   rc2 = sqlite3_finalize(pStmt);
9519   if( rc==SQLITE_OK ) rc = rc2;
9520   return rc;
9521 }
9522
9523 /* 
9524 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
9525 */
9526 static int dbdataFilter(
9527   sqlite3_vtab_cursor *pCursor, 
9528   int idxNum, const char *idxStr,
9529   int argc, sqlite3_value **argv
9530 ){
9531   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9532   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
9533   int rc = SQLITE_OK;
9534   const char *zSchema = "main";
9535
9536   dbdataResetCursor(pCsr);
9537   assert( pCsr->iPgno==1 );
9538   if( idxNum & 0x01 ){
9539     zSchema = (const char*)sqlite3_value_text(argv[0]);
9540   }
9541   if( idxNum & 0x02 ){
9542     pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
9543     pCsr->bOnePage = 1;
9544   }else{
9545     pCsr->nPage = dbdataDbsize(pCsr, zSchema);
9546     rc = dbdataDbsize(pCsr, zSchema);
9547   }
9548
9549   if( rc==SQLITE_OK ){
9550     if( pTab->pStmt ){
9551       pCsr->pStmt = pTab->pStmt;
9552       pTab->pStmt = 0;
9553     }else{
9554       rc = sqlite3_prepare_v2(pTab->db, 
9555           "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
9556           &pCsr->pStmt, 0
9557       );
9558     }
9559   }
9560   if( rc==SQLITE_OK ){
9561     rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
9562   }else{
9563     pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
9564   }
9565   if( rc==SQLITE_OK ){
9566     rc = dbdataNext(pCursor);
9567   }
9568   return rc;
9569 }
9570
9571 /* 
9572 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
9573 */
9574 static int dbdataColumn(
9575   sqlite3_vtab_cursor *pCursor, 
9576   sqlite3_context *ctx, 
9577   int i
9578 ){
9579   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9580   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
9581   if( pTab->bPtr ){
9582     switch( i ){
9583       case DBPTR_COLUMN_PGNO:
9584         sqlite3_result_int64(ctx, pCsr->iPgno);
9585         break;
9586       case DBPTR_COLUMN_CHILD: {
9587         int iOff = pCsr->iPgno==1 ? 100 : 0;
9588         if( pCsr->iCell<0 ){
9589           iOff += 8;
9590         }else{
9591           iOff += 12 + pCsr->iCell*2;
9592           if( iOff>pCsr->nPage ) return SQLITE_OK;
9593           iOff = get_uint16(&pCsr->aPage[iOff]);
9594         }
9595         if( iOff<=pCsr->nPage ){
9596           sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
9597         }
9598         break;
9599       }
9600     }
9601   }else{
9602     switch( i ){
9603       case DBDATA_COLUMN_PGNO:
9604         sqlite3_result_int64(ctx, pCsr->iPgno);
9605         break;
9606       case DBDATA_COLUMN_CELL:
9607         sqlite3_result_int(ctx, pCsr->iCell);
9608         break;
9609       case DBDATA_COLUMN_FIELD:
9610         sqlite3_result_int(ctx, pCsr->iField);
9611         break;
9612       case DBDATA_COLUMN_VALUE: {
9613         if( pCsr->iField<0 ){
9614           sqlite3_result_int64(ctx, pCsr->iIntkey);
9615         }else{
9616           sqlite3_int64 iType;
9617           dbdataGetVarint(pCsr->pHdrPtr, &iType);
9618           dbdataValue(
9619               ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
9620           );
9621         }
9622         break;
9623       }
9624     }
9625   }
9626   return SQLITE_OK;
9627 }
9628
9629 /* 
9630 ** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
9631 */
9632 static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
9633   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9634   *pRowid = pCsr->iRowid;
9635   return SQLITE_OK;
9636 }
9637
9638
9639 /*
9640 ** Invoke this routine to register the "sqlite_dbdata" virtual table module
9641 */
9642 static int sqlite3DbdataRegister(sqlite3 *db){
9643   static sqlite3_module dbdata_module = {
9644     0,                            /* iVersion */
9645     0,                            /* xCreate */
9646     dbdataConnect,                /* xConnect */
9647     dbdataBestIndex,              /* xBestIndex */
9648     dbdataDisconnect,             /* xDisconnect */
9649     0,                            /* xDestroy */
9650     dbdataOpen,                   /* xOpen - open a cursor */
9651     dbdataClose,                  /* xClose - close a cursor */
9652     dbdataFilter,                 /* xFilter - configure scan constraints */
9653     dbdataNext,                   /* xNext - advance a cursor */
9654     dbdataEof,                    /* xEof - check for end of scan */
9655     dbdataColumn,                 /* xColumn - read data */
9656     dbdataRowid,                  /* xRowid - read data */
9657     0,                            /* xUpdate */
9658     0,                            /* xBegin */
9659     0,                            /* xSync */
9660     0,                            /* xCommit */
9661     0,                            /* xRollback */
9662     0,                            /* xFindMethod */
9663     0,                            /* xRename */
9664     0,                            /* xSavepoint */
9665     0,                            /* xRelease */
9666     0,                            /* xRollbackTo */
9667     0                             /* xShadowName */
9668   };
9669
9670   int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
9671   if( rc==SQLITE_OK ){
9672     rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
9673   }
9674   return rc;
9675 }
9676
9677 #ifdef _WIN32
9678
9679 #endif
9680 int sqlite3_dbdata_init(
9681   sqlite3 *db, 
9682   char **pzErrMsg, 
9683   const sqlite3_api_routines *pApi
9684 ){
9685   SQLITE_EXTENSION_INIT2(pApi);
9686   return sqlite3DbdataRegister(db);
9687 }
9688
9689 /************************* End ../ext/misc/dbdata.c ********************/
9690 #endif
9691
9692 #if defined(SQLITE_ENABLE_SESSION)
9693 /*
9694 ** State information for a single open session
9695 */
9696 typedef struct OpenSession OpenSession;
9697 struct OpenSession {
9698   char *zName;             /* Symbolic name for this session */
9699   int nFilter;             /* Number of xFilter rejection GLOB patterns */
9700   char **azFilter;         /* Array of xFilter rejection GLOB patterns */
9701   sqlite3_session *p;      /* The open session */
9702 };
9703 #endif
9704
9705 /*
9706 ** Shell output mode information from before ".explain on",
9707 ** saved so that it can be restored by ".explain off"
9708 */
9709 typedef struct SavedModeInfo SavedModeInfo;
9710 struct SavedModeInfo {
9711   int valid;          /* Is there legit data in here? */
9712   int mode;           /* Mode prior to ".explain on" */
9713   int showHeader;     /* The ".header" setting prior to ".explain on" */
9714   int colWidth[100];  /* Column widths prior to ".explain on" */
9715 };
9716
9717 typedef struct ExpertInfo ExpertInfo;
9718 struct ExpertInfo {
9719   sqlite3expert *pExpert;
9720   int bVerbose;
9721 };
9722
9723 /* A single line in the EQP output */
9724 typedef struct EQPGraphRow EQPGraphRow;
9725 struct EQPGraphRow {
9726   int iEqpId;           /* ID for this row */
9727   int iParentId;        /* ID of the parent row */
9728   EQPGraphRow *pNext;   /* Next row in sequence */
9729   char zText[1];        /* Text to display for this row */
9730 };
9731
9732 /* All EQP output is collected into an instance of the following */
9733 typedef struct EQPGraph EQPGraph;
9734 struct EQPGraph {
9735   EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
9736   EQPGraphRow *pLast;   /* Last element of the pRow list */
9737   char zPrefix[100];    /* Graph prefix */
9738 };
9739
9740 /*
9741 ** State information about the database connection is contained in an
9742 ** instance of the following structure.
9743 */
9744 typedef struct ShellState ShellState;
9745 struct ShellState {
9746   sqlite3 *db;           /* The database */
9747   u8 autoExplain;        /* Automatically turn on .explain mode */
9748   u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
9749   u8 autoEQPtest;        /* autoEQP is in test mode */
9750   u8 autoEQPtrace;       /* autoEQP is in trace mode */
9751   u8 statsOn;            /* True to display memory stats before each finalize */
9752   u8 scanstatsOn;        /* True to display scan stats before each finalize */
9753   u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
9754   u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
9755   u8 nEqpLevel;          /* Depth of the EQP output graph */
9756   u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
9757   unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
9758   int outCount;          /* Revert to stdout when reaching zero */
9759   int cnt;               /* Number of records displayed so far */
9760   int lineno;            /* Line number of last line read from in */
9761   int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
9762   FILE *in;              /* Read commands from this stream */
9763   FILE *out;             /* Write results here */
9764   FILE *traceOut;        /* Output for sqlite3_trace() */
9765   int nErr;              /* Number of errors seen */
9766   int mode;              /* An output mode setting */
9767   int modePrior;         /* Saved mode */
9768   int cMode;             /* temporary output mode for the current query */
9769   int normalMode;        /* Output mode before ".explain on" */
9770   int writableSchema;    /* True if PRAGMA writable_schema=ON */
9771   int showHeader;        /* True to show column names in List or Column mode */
9772   int nCheck;            /* Number of ".check" commands run */
9773   unsigned nProgress;    /* Number of progress callbacks encountered */
9774   unsigned mxProgress;   /* Maximum progress callbacks before failing */
9775   unsigned flgProgress;  /* Flags for the progress callback */
9776   unsigned shellFlgs;    /* Various flags */
9777   unsigned priorShFlgs;  /* Saved copy of flags */
9778   sqlite3_int64 szMax;   /* --maxsize argument to .open */
9779   char *zDestTable;      /* Name of destination table when MODE_Insert */
9780   char *zTempFile;       /* Temporary file that might need deleting */
9781   char zTestcase[30];    /* Name of current test case */
9782   char colSeparator[20]; /* Column separator character for several modes */
9783   char rowSeparator[20]; /* Row separator character for MODE_Ascii */
9784   char colSepPrior[20];  /* Saved column separator */
9785   char rowSepPrior[20];  /* Saved row separator */
9786   int colWidth[100];     /* Requested width of each column when in column mode*/
9787   int actualWidth[100];  /* Actual width of each column */
9788   char nullValue[20];    /* The text to print when a NULL comes back from
9789                          ** the database */
9790   char outfile[FILENAME_MAX]; /* Filename for *out */
9791   const char *zDbFilename;    /* name of the database file */
9792   char *zFreeOnClose;         /* Filename to free when closing */
9793   const char *zVfs;           /* Name of VFS to use */
9794   sqlite3_stmt *pStmt;   /* Current statement if any. */
9795   FILE *pLog;            /* Write log output here */
9796   int *aiIndent;         /* Array of indents used in MODE_Explain */
9797   int nIndent;           /* Size of array aiIndent[] */
9798   int iIndent;           /* Index of current op in aiIndent[] */
9799   EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
9800 #if defined(SQLITE_ENABLE_SESSION)
9801   int nSession;             /* Number of active sessions */
9802   OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
9803 #endif
9804   ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
9805 };
9806
9807
9808 /* Allowed values for ShellState.autoEQP
9809 */
9810 #define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
9811 #define AUTOEQP_on       1           /* Automatic EQP is on */
9812 #define AUTOEQP_trigger  2           /* On and also show plans for triggers */
9813 #define AUTOEQP_full     3           /* Show full EXPLAIN */
9814
9815 /* Allowed values for ShellState.openMode
9816 */
9817 #define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
9818 #define SHELL_OPEN_NORMAL      1      /* Normal database file */
9819 #define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
9820 #define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
9821 #define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
9822 #define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
9823 #define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
9824
9825 /* Allowed values for ShellState.eTraceType
9826 */
9827 #define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
9828 #define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
9829 #define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
9830
9831 /* Bits in the ShellState.flgProgress variable */
9832 #define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
9833 #define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
9834                                    ** callback limit is reached, and for each
9835                                    ** top-level SQL statement */
9836 #define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
9837
9838 /*
9839 ** These are the allowed shellFlgs values
9840 */
9841 #define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
9842 #define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
9843 #define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
9844 #define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
9845 #define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
9846 #define SHFLG_CountChanges   0x00000020 /* .changes setting */
9847 #define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
9848
9849 /*
9850 ** Macros for testing and setting shellFlgs
9851 */
9852 #define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
9853 #define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
9854 #define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
9855
9856 /*
9857 ** These are the allowed modes.
9858 */
9859 #define MODE_Line     0  /* One column per line.  Blank line between records */
9860 #define MODE_Column   1  /* One record per line in neat columns */
9861 #define MODE_List     2  /* One record per line with a separator */
9862 #define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
9863 #define MODE_Html     4  /* Generate an XHTML table */
9864 #define MODE_Insert   5  /* Generate SQL "insert" statements */
9865 #define MODE_Quote    6  /* Quote values as for SQL */
9866 #define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
9867 #define MODE_Csv      8  /* Quote strings, numbers are plain */
9868 #define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
9869 #define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
9870 #define MODE_Pretty  11  /* Pretty-print schemas */
9871 #define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
9872
9873 static const char *modeDescr[] = {
9874   "line",
9875   "column",
9876   "list",
9877   "semi",
9878   "html",
9879   "insert",
9880   "quote",
9881   "tcl",
9882   "csv",
9883   "explain",
9884   "ascii",
9885   "prettyprint",
9886   "eqp"
9887 };
9888
9889 /*
9890 ** These are the column/row/line separators used by the various
9891 ** import/export modes.
9892 */
9893 #define SEP_Column    "|"
9894 #define SEP_Row       "\n"
9895 #define SEP_Tab       "\t"
9896 #define SEP_Space     " "
9897 #define SEP_Comma     ","
9898 #define SEP_CrLf      "\r\n"
9899 #define SEP_Unit      "\x1F"
9900 #define SEP_Record    "\x1E"
9901
9902 /*
9903 ** A callback for the sqlite3_log() interface.
9904 */
9905 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
9906   ShellState *p = (ShellState*)pArg;
9907   if( p->pLog==0 ) return;
9908   utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
9909   fflush(p->pLog);
9910 }
9911
9912 /*
9913 ** SQL function:  shell_putsnl(X)
9914 **
9915 ** Write the text X to the screen (or whatever output is being directed)
9916 ** adding a newline at the end, and then return X.
9917 */
9918 static void shellPutsFunc(
9919   sqlite3_context *pCtx,
9920   int nVal,
9921   sqlite3_value **apVal
9922 ){
9923   ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
9924   (void)nVal;
9925   utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
9926   sqlite3_result_value(pCtx, apVal[0]);
9927 }
9928
9929 /*
9930 ** SQL function:   edit(VALUE)
9931 **                 edit(VALUE,EDITOR)
9932 **
9933 ** These steps:
9934 **
9935 **     (1) Write VALUE into a temporary file.
9936 **     (2) Run program EDITOR on that temporary file.
9937 **     (3) Read the temporary file back and return its content as the result.
9938 **     (4) Delete the temporary file
9939 **
9940 ** If the EDITOR argument is omitted, use the value in the VISUAL
9941 ** environment variable.  If still there is no EDITOR, through an error.
9942 **
9943 ** Also throw an error if the EDITOR program returns a non-zero exit code.
9944 */
9945 #ifndef SQLITE_NOHAVE_SYSTEM
9946 static void editFunc(
9947   sqlite3_context *context,
9948   int argc,
9949   sqlite3_value **argv
9950 ){
9951   const char *zEditor;
9952   char *zTempFile = 0;
9953   sqlite3 *db;
9954   char *zCmd = 0;
9955   int bBin;
9956   int rc;
9957   int hasCRNL = 0;
9958   FILE *f = 0;
9959   sqlite3_int64 sz;
9960   sqlite3_int64 x;
9961   unsigned char *p = 0;
9962
9963   if( argc==2 ){
9964     zEditor = (const char*)sqlite3_value_text(argv[1]);
9965   }else{
9966     zEditor = getenv("VISUAL");
9967   }
9968   if( zEditor==0 ){
9969     sqlite3_result_error(context, "no editor for edit()", -1);
9970     return;
9971   }
9972   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
9973     sqlite3_result_error(context, "NULL input to edit()", -1);
9974     return;
9975   }
9976   db = sqlite3_context_db_handle(context);
9977   zTempFile = 0;
9978   sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
9979   if( zTempFile==0 ){
9980     sqlite3_uint64 r = 0;
9981     sqlite3_randomness(sizeof(r), &r);
9982     zTempFile = sqlite3_mprintf("temp%llx", r);
9983     if( zTempFile==0 ){
9984       sqlite3_result_error_nomem(context);
9985       return;
9986     }
9987   }
9988   bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
9989   /* When writing the file to be edited, do \n to \r\n conversions on systems
9990   ** that want \r\n line endings */
9991   f = fopen(zTempFile, bBin ? "wb" : "w");
9992   if( f==0 ){
9993     sqlite3_result_error(context, "edit() cannot open temp file", -1);
9994     goto edit_func_end;
9995   }
9996   sz = sqlite3_value_bytes(argv[0]);
9997   if( bBin ){
9998     x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
9999   }else{
10000     const char *z = (const char*)sqlite3_value_text(argv[0]);
10001     /* Remember whether or not the value originally contained \r\n */
10002     if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
10003     x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
10004   }
10005   fclose(f);
10006   f = 0;
10007   if( x!=sz ){
10008     sqlite3_result_error(context, "edit() could not write the whole file", -1);
10009     goto edit_func_end;
10010   }
10011   zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
10012   if( zCmd==0 ){
10013     sqlite3_result_error_nomem(context);
10014     goto edit_func_end;
10015   }
10016   rc = system(zCmd);
10017   sqlite3_free(zCmd);
10018   if( rc ){
10019     sqlite3_result_error(context, "EDITOR returned non-zero", -1);
10020     goto edit_func_end;
10021   }
10022   f = fopen(zTempFile, "rb");
10023   if( f==0 ){
10024     sqlite3_result_error(context,
10025       "edit() cannot reopen temp file after edit", -1);
10026     goto edit_func_end;
10027   }
10028   fseek(f, 0, SEEK_END);
10029   sz = ftell(f);
10030   rewind(f);
10031   p = sqlite3_malloc64( sz+1 );
10032   if( p==0 ){
10033     sqlite3_result_error_nomem(context);
10034     goto edit_func_end;
10035   }
10036   x = fread(p, 1, (size_t)sz, f);
10037   fclose(f);
10038   f = 0;
10039   if( x!=sz ){
10040     sqlite3_result_error(context, "could not read back the whole file", -1);
10041     goto edit_func_end;
10042   }
10043   if( bBin ){
10044     sqlite3_result_blob64(context, p, sz, sqlite3_free);
10045   }else{
10046     sqlite3_int64 i, j;
10047     if( hasCRNL ){
10048       /* If the original contains \r\n then do no conversions back to \n */
10049       j = sz;
10050     }else{
10051       /* If the file did not originally contain \r\n then convert any new
10052       ** \r\n back into \n */
10053       for(i=j=0; i<sz; i++){
10054         if( p[i]=='\r' && p[i+1]=='\n' ) i++;
10055         p[j++] = p[i];
10056       }
10057       sz = j;
10058       p[sz] = 0;
10059     } 
10060     sqlite3_result_text64(context, (const char*)p, sz,
10061                           sqlite3_free, SQLITE_UTF8);
10062   }
10063   p = 0;
10064
10065 edit_func_end:
10066   if( f ) fclose(f);
10067   unlink(zTempFile);
10068   sqlite3_free(zTempFile);
10069   sqlite3_free(p);
10070 }
10071 #endif /* SQLITE_NOHAVE_SYSTEM */
10072
10073 /*
10074 ** Save or restore the current output mode
10075 */
10076 static void outputModePush(ShellState *p){
10077   p->modePrior = p->mode;
10078   p->priorShFlgs = p->shellFlgs;
10079   memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
10080   memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
10081 }
10082 static void outputModePop(ShellState *p){
10083   p->mode = p->modePrior;
10084   p->shellFlgs = p->priorShFlgs;
10085   memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
10086   memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
10087 }
10088
10089 /*
10090 ** Output the given string as a hex-encoded blob (eg. X'1234' )
10091 */
10092 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
10093   int i;
10094   char *zBlob = (char *)pBlob;
10095   raw_printf(out,"X'");
10096   for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
10097   raw_printf(out,"'");
10098 }
10099
10100 /*
10101 ** Find a string that is not found anywhere in z[].  Return a pointer
10102 ** to that string.
10103 **
10104 ** Try to use zA and zB first.  If both of those are already found in z[]
10105 ** then make up some string and store it in the buffer zBuf.
10106 */
10107 static const char *unused_string(
10108   const char *z,                    /* Result must not appear anywhere in z */
10109   const char *zA, const char *zB,   /* Try these first */
10110   char *zBuf                        /* Space to store a generated string */
10111 ){
10112   unsigned i = 0;
10113   if( strstr(z, zA)==0 ) return zA;
10114   if( strstr(z, zB)==0 ) return zB;
10115   do{
10116     sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
10117   }while( strstr(z,zBuf)!=0 );
10118   return zBuf;
10119 }
10120
10121 /*
10122 ** Output the given string as a quoted string using SQL quoting conventions.
10123 **
10124 ** See also: output_quoted_escaped_string()
10125 */
10126 static void output_quoted_string(FILE *out, const char *z){
10127   int i;
10128   char c;
10129   setBinaryMode(out, 1);
10130   for(i=0; (c = z[i])!=0 && c!='\''; i++){}
10131   if( c==0 ){
10132     utf8_printf(out,"'%s'",z);
10133   }else{
10134     raw_printf(out, "'");
10135     while( *z ){
10136       for(i=0; (c = z[i])!=0 && c!='\''; i++){}
10137       if( c=='\'' ) i++;
10138       if( i ){
10139         utf8_printf(out, "%.*s", i, z);
10140         z += i;
10141       }
10142       if( c=='\'' ){
10143         raw_printf(out, "'");
10144         continue;
10145       }
10146       if( c==0 ){
10147         break;
10148       }
10149       z++;
10150     }
10151     raw_printf(out, "'");
10152   }
10153   setTextMode(out, 1);
10154 }
10155
10156 /*
10157 ** Output the given string as a quoted string using SQL quoting conventions.
10158 ** Additionallly , escape the "\n" and "\r" characters so that they do not
10159 ** get corrupted by end-of-line translation facilities in some operating
10160 ** systems.
10161 **
10162 ** This is like output_quoted_string() but with the addition of the \r\n
10163 ** escape mechanism.
10164 */
10165 static void output_quoted_escaped_string(FILE *out, const char *z){
10166   int i;
10167   char c;
10168   setBinaryMode(out, 1);
10169   for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
10170   if( c==0 ){
10171     utf8_printf(out,"'%s'",z);
10172   }else{
10173     const char *zNL = 0;
10174     const char *zCR = 0;
10175     int nNL = 0;
10176     int nCR = 0;
10177     char zBuf1[20], zBuf2[20];
10178     for(i=0; z[i]; i++){
10179       if( z[i]=='\n' ) nNL++;
10180       if( z[i]=='\r' ) nCR++;
10181     }
10182     if( nNL ){
10183       raw_printf(out, "replace(");
10184       zNL = unused_string(z, "\\n", "\\012", zBuf1);
10185     }
10186     if( nCR ){
10187       raw_printf(out, "replace(");
10188       zCR = unused_string(z, "\\r", "\\015", zBuf2);
10189     }
10190     raw_printf(out, "'");
10191     while( *z ){
10192       for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
10193       if( c=='\'' ) i++;
10194       if( i ){
10195         utf8_printf(out, "%.*s", i, z);
10196         z += i;
10197       }
10198       if( c=='\'' ){
10199         raw_printf(out, "'");
10200         continue;
10201       }
10202       if( c==0 ){
10203         break;
10204       }
10205       z++;
10206       if( c=='\n' ){
10207         raw_printf(out, "%s", zNL);
10208         continue;
10209       }
10210       raw_printf(out, "%s", zCR);
10211     }
10212     raw_printf(out, "'");
10213     if( nCR ){
10214       raw_printf(out, ",'%s',char(13))", zCR);
10215     }
10216     if( nNL ){
10217       raw_printf(out, ",'%s',char(10))", zNL);
10218     }
10219   }
10220   setTextMode(out, 1);
10221 }
10222
10223 /*
10224 ** Output the given string as a quoted according to C or TCL quoting rules.
10225 */
10226 static void output_c_string(FILE *out, const char *z){
10227   unsigned int c;
10228   fputc('"', out);
10229   while( (c = *(z++))!=0 ){
10230     if( c=='\\' ){
10231       fputc(c, out);
10232       fputc(c, out);
10233     }else if( c=='"' ){
10234       fputc('\\', out);
10235       fputc('"', out);
10236     }else if( c=='\t' ){
10237       fputc('\\', out);
10238       fputc('t', out);
10239     }else if( c=='\n' ){
10240       fputc('\\', out);
10241       fputc('n', out);
10242     }else if( c=='\r' ){
10243       fputc('\\', out);
10244       fputc('r', out);
10245     }else if( !isprint(c&0xff) ){
10246       raw_printf(out, "\\%03o", c&0xff);
10247     }else{
10248       fputc(c, out);
10249     }
10250   }
10251   fputc('"', out);
10252 }
10253
10254 /*
10255 ** Output the given string with characters that are special to
10256 ** HTML escaped.
10257 */
10258 static void output_html_string(FILE *out, const char *z){
10259   int i;
10260   if( z==0 ) z = "";
10261   while( *z ){
10262     for(i=0;   z[i]
10263             && z[i]!='<'
10264             && z[i]!='&'
10265             && z[i]!='>'
10266             && z[i]!='\"'
10267             && z[i]!='\'';
10268         i++){}
10269     if( i>0 ){
10270       utf8_printf(out,"%.*s",i,z);
10271     }
10272     if( z[i]=='<' ){
10273       raw_printf(out,"&lt;");
10274     }else if( z[i]=='&' ){
10275       raw_printf(out,"&amp;");
10276     }else if( z[i]=='>' ){
10277       raw_printf(out,"&gt;");
10278     }else if( z[i]=='\"' ){
10279       raw_printf(out,"&quot;");
10280     }else if( z[i]=='\'' ){
10281       raw_printf(out,"&#39;");
10282     }else{
10283       break;
10284     }
10285     z += i + 1;
10286   }
10287 }
10288
10289 /*
10290 ** If a field contains any character identified by a 1 in the following
10291 ** array, then the string must be quoted for CSV.
10292 */
10293 static const char needCsvQuote[] = {
10294   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10295   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10296   1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
10297   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
10298   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
10299   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
10300   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
10301   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
10302   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10303   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10304   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10305   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10306   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10307   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10308   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10309   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10310 };
10311
10312 /*
10313 ** Output a single term of CSV.  Actually, p->colSeparator is used for
10314 ** the separator, which may or may not be a comma.  p->nullValue is
10315 ** the null value.  Strings are quoted if necessary.  The separator
10316 ** is only issued if bSep is true.
10317 */
10318 static void output_csv(ShellState *p, const char *z, int bSep){
10319   FILE *out = p->out;
10320   if( z==0 ){
10321     utf8_printf(out,"%s",p->nullValue);
10322   }else{
10323     int i;
10324     int nSep = strlen30(p->colSeparator);
10325     for(i=0; z[i]; i++){
10326       if( needCsvQuote[((unsigned char*)z)[i]]
10327          || (z[i]==p->colSeparator[0] &&
10328              (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
10329         i = 0;
10330         break;
10331       }
10332     }
10333     if( i==0 ){
10334       char *zQuoted = sqlite3_mprintf("\"%w\"", z);
10335       utf8_printf(out, "%s", zQuoted);
10336       sqlite3_free(zQuoted);
10337     }else{
10338       utf8_printf(out, "%s", z);
10339     }
10340   }
10341   if( bSep ){
10342     utf8_printf(p->out, "%s", p->colSeparator);
10343   }
10344 }
10345
10346 /*
10347 ** This routine runs when the user presses Ctrl-C
10348 */
10349 static void interrupt_handler(int NotUsed){
10350   UNUSED_PARAMETER(NotUsed);
10351   seenInterrupt++;
10352   if( seenInterrupt>2 ) exit(1);
10353   if( globalDb ) sqlite3_interrupt(globalDb);
10354 }
10355
10356 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
10357 /*
10358 ** This routine runs for console events (e.g. Ctrl-C) on Win32
10359 */
10360 static BOOL WINAPI ConsoleCtrlHandler(
10361   DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
10362 ){
10363   if( dwCtrlType==CTRL_C_EVENT ){
10364     interrupt_handler(0);
10365     return TRUE;
10366   }
10367   return FALSE;
10368 }
10369 #endif
10370
10371 #ifndef SQLITE_OMIT_AUTHORIZATION
10372 /*
10373 ** When the ".auth ON" is set, the following authorizer callback is
10374 ** invoked.  It always returns SQLITE_OK.
10375 */
10376 static int shellAuth(
10377   void *pClientData,
10378   int op,
10379   const char *zA1,
10380   const char *zA2,
10381   const char *zA3,
10382   const char *zA4
10383 ){
10384   ShellState *p = (ShellState*)pClientData;
10385   static const char *azAction[] = { 0,
10386      "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
10387      "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
10388      "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
10389      "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
10390      "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
10391      "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
10392      "PRAGMA",               "READ",                 "SELECT",
10393      "TRANSACTION",          "UPDATE",               "ATTACH",
10394      "DETACH",               "ALTER_TABLE",          "REINDEX",
10395      "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
10396      "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
10397   };
10398   int i;
10399   const char *az[4];
10400   az[0] = zA1;
10401   az[1] = zA2;
10402   az[2] = zA3;
10403   az[3] = zA4;
10404   utf8_printf(p->out, "authorizer: %s", azAction[op]);
10405   for(i=0; i<4; i++){
10406     raw_printf(p->out, " ");
10407     if( az[i] ){
10408       output_c_string(p->out, az[i]);
10409     }else{
10410       raw_printf(p->out, "NULL");
10411     }
10412   }
10413   raw_printf(p->out, "\n");
10414   return SQLITE_OK;
10415 }
10416 #endif
10417
10418 /*
10419 ** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
10420 **
10421 ** This routine converts some CREATE TABLE statements for shadow tables
10422 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
10423 */
10424 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
10425   if( z==0 ) return;
10426   if( zTail==0 ) return;
10427   if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
10428     utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
10429   }else{
10430     utf8_printf(out, "%s%s", z, zTail);
10431   }
10432 }
10433 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
10434   char c = z[n];
10435   z[n] = 0;
10436   printSchemaLine(out, z, zTail);
10437   z[n] = c;
10438 }
10439
10440 /*
10441 ** Return true if string z[] has nothing but whitespace and comments to the
10442 ** end of the first line.
10443 */
10444 static int wsToEol(const char *z){
10445   int i;
10446   for(i=0; z[i]; i++){
10447     if( z[i]=='\n' ) return 1;
10448     if( IsSpace(z[i]) ) continue;
10449     if( z[i]=='-' && z[i+1]=='-' ) return 1;
10450     return 0;
10451   }
10452   return 1;
10453 }
10454
10455 /*
10456 ** Add a new entry to the EXPLAIN QUERY PLAN data
10457 */
10458 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
10459   EQPGraphRow *pNew;
10460   int nText = strlen30(zText);
10461   if( p->autoEQPtest ){
10462     utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
10463   }
10464   pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
10465   if( pNew==0 ) shell_out_of_memory();
10466   pNew->iEqpId = iEqpId;
10467   pNew->iParentId = p2;
10468   memcpy(pNew->zText, zText, nText+1);
10469   pNew->pNext = 0;
10470   if( p->sGraph.pLast ){
10471     p->sGraph.pLast->pNext = pNew;
10472   }else{
10473     p->sGraph.pRow = pNew;
10474   }
10475   p->sGraph.pLast = pNew;
10476 }
10477
10478 /*
10479 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
10480 ** in p->sGraph.
10481 */
10482 static void eqp_reset(ShellState *p){
10483   EQPGraphRow *pRow, *pNext;
10484   for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
10485     pNext = pRow->pNext;
10486     sqlite3_free(pRow);
10487   }
10488   memset(&p->sGraph, 0, sizeof(p->sGraph));
10489 }
10490
10491 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
10492 ** pOld, or return the first such line if pOld is NULL
10493 */
10494 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
10495   EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
10496   while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
10497   return pRow;
10498 }
10499
10500 /* Render a single level of the graph that has iEqpId as its parent.  Called
10501 ** recursively to render sublevels.
10502 */
10503 static void eqp_render_level(ShellState *p, int iEqpId){
10504   EQPGraphRow *pRow, *pNext;
10505   int n = strlen30(p->sGraph.zPrefix);
10506   char *z;
10507   for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
10508     pNext = eqp_next_row(p, iEqpId, pRow);
10509     z = pRow->zText;
10510     utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
10511                 pNext ? "|--" : "`--", z);
10512     if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
10513       memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
10514       eqp_render_level(p, pRow->iEqpId);
10515       p->sGraph.zPrefix[n] = 0;
10516     }
10517   }
10518 }
10519
10520 /*
10521 ** Display and reset the EXPLAIN QUERY PLAN data
10522 */
10523 static void eqp_render(ShellState *p){
10524   EQPGraphRow *pRow = p->sGraph.pRow;
10525   if( pRow ){
10526     if( pRow->zText[0]=='-' ){
10527       if( pRow->pNext==0 ){
10528         eqp_reset(p);
10529         return;
10530       }
10531       utf8_printf(p->out, "%s\n", pRow->zText+3);
10532       p->sGraph.pRow = pRow->pNext;
10533       sqlite3_free(pRow);
10534     }else{
10535       utf8_printf(p->out, "QUERY PLAN\n");
10536     }
10537     p->sGraph.zPrefix[0] = 0;
10538     eqp_render_level(p, 0);
10539     eqp_reset(p);
10540   }
10541 }
10542
10543 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
10544 /*
10545 ** Progress handler callback.
10546 */
10547 static int progress_handler(void *pClientData) {
10548   ShellState *p = (ShellState*)pClientData;
10549   p->nProgress++;
10550   if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
10551     raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
10552     if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
10553     if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
10554     return 1;
10555   }
10556   if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
10557     raw_printf(p->out, "Progress %u\n", p->nProgress);
10558   }
10559   return 0;
10560 }
10561 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
10562
10563 /*
10564 ** This is the callback routine that the shell
10565 ** invokes for each row of a query result.
10566 */
10567 static int shell_callback(
10568   void *pArg,
10569   int nArg,        /* Number of result columns */
10570   char **azArg,    /* Text of each result column */
10571   char **azCol,    /* Column names */
10572   int *aiType      /* Column types */
10573 ){
10574   int i;
10575   ShellState *p = (ShellState*)pArg;
10576
10577   if( azArg==0 ) return 0;
10578   switch( p->cMode ){
10579     case MODE_Line: {
10580       int w = 5;
10581       if( azArg==0 ) break;
10582       for(i=0; i<nArg; i++){
10583         int len = strlen30(azCol[i] ? azCol[i] : "");
10584         if( len>w ) w = len;
10585       }
10586       if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
10587       for(i=0; i<nArg; i++){
10588         utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
10589                 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
10590       }
10591       break;
10592     }
10593     case MODE_Explain:
10594     case MODE_Column: {
10595       static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
10596       const int *colWidth;
10597       int showHdr;
10598       char *rowSep;
10599       int nWidth;
10600       if( p->cMode==MODE_Column ){
10601         colWidth = p->colWidth;
10602         nWidth = ArraySize(p->colWidth);
10603         showHdr = p->showHeader;
10604         rowSep = p->rowSeparator;
10605       }else{
10606         colWidth = aExplainWidths;
10607         nWidth = ArraySize(aExplainWidths);
10608         showHdr = 1;
10609         rowSep = SEP_Row;
10610       }
10611       if( p->cnt++==0 ){
10612         for(i=0; i<nArg; i++){
10613           int w, n;
10614           if( i<nWidth ){
10615             w = colWidth[i];
10616           }else{
10617             w = 0;
10618           }
10619           if( w==0 ){
10620             w = strlenChar(azCol[i] ? azCol[i] : "");
10621             if( w<10 ) w = 10;
10622             n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
10623             if( w<n ) w = n;
10624           }
10625           if( i<ArraySize(p->actualWidth) ){
10626             p->actualWidth[i] = w;
10627           }
10628           if( showHdr ){
10629             utf8_width_print(p->out, w, azCol[i]);
10630             utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
10631           }
10632         }
10633         if( showHdr ){
10634           for(i=0; i<nArg; i++){
10635             int w;
10636             if( i<ArraySize(p->actualWidth) ){
10637                w = p->actualWidth[i];
10638                if( w<0 ) w = -w;
10639             }else{
10640                w = 10;
10641             }
10642             utf8_printf(p->out,"%-*.*s%s",w,w,
10643                    "----------------------------------------------------------"
10644                    "----------------------------------------------------------",
10645                     i==nArg-1 ? rowSep : "  ");
10646           }
10647         }
10648       }
10649       if( azArg==0 ) break;
10650       for(i=0; i<nArg; i++){
10651         int w;
10652         if( i<ArraySize(p->actualWidth) ){
10653            w = p->actualWidth[i];
10654         }else{
10655            w = 10;
10656         }
10657         if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
10658           w = strlenChar(azArg[i]);
10659         }
10660         if( i==1 && p->aiIndent && p->pStmt ){
10661           if( p->iIndent<p->nIndent ){
10662             utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
10663           }
10664           p->iIndent++;
10665         }
10666         utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
10667         utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
10668       }
10669       break;
10670     }
10671     case MODE_Semi: {   /* .schema and .fullschema output */
10672       printSchemaLine(p->out, azArg[0], ";\n");
10673       break;
10674     }
10675     case MODE_Pretty: {  /* .schema and .fullschema with --indent */
10676       char *z;
10677       int j;
10678       int nParen = 0;
10679       char cEnd = 0;
10680       char c;
10681       int nLine = 0;
10682       assert( nArg==1 );
10683       if( azArg[0]==0 ) break;
10684       if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
10685        || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
10686       ){
10687         utf8_printf(p->out, "%s;\n", azArg[0]);
10688         break;
10689       }
10690       z = sqlite3_mprintf("%s", azArg[0]);
10691       j = 0;
10692       for(i=0; IsSpace(z[i]); i++){}
10693       for(; (c = z[i])!=0; i++){
10694         if( IsSpace(c) ){
10695           if( z[j-1]=='\r' ) z[j-1] = '\n';
10696           if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
10697         }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
10698           j--;
10699         }
10700         z[j++] = c;
10701       }
10702       while( j>0 && IsSpace(z[j-1]) ){ j--; }
10703       z[j] = 0;
10704       if( strlen30(z)>=79 ){
10705         for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
10706           if( c==cEnd ){
10707             cEnd = 0;
10708           }else if( c=='"' || c=='\'' || c=='`' ){
10709             cEnd = c;
10710           }else if( c=='[' ){
10711             cEnd = ']';
10712           }else if( c=='-' && z[i+1]=='-' ){
10713             cEnd = '\n';
10714           }else if( c=='(' ){
10715             nParen++;
10716           }else if( c==')' ){
10717             nParen--;
10718             if( nLine>0 && nParen==0 && j>0 ){
10719               printSchemaLineN(p->out, z, j, "\n");
10720               j = 0;
10721             }
10722           }
10723           z[j++] = c;
10724           if( nParen==1 && cEnd==0
10725            && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
10726           ){
10727             if( c=='\n' ) j--;
10728             printSchemaLineN(p->out, z, j, "\n  ");
10729             j = 0;
10730             nLine++;
10731             while( IsSpace(z[i+1]) ){ i++; }
10732           }
10733         }
10734         z[j] = 0;
10735       }
10736       printSchemaLine(p->out, z, ";\n");
10737       sqlite3_free(z);
10738       break;
10739     }
10740     case MODE_List: {
10741       if( p->cnt++==0 && p->showHeader ){
10742         for(i=0; i<nArg; i++){
10743           utf8_printf(p->out,"%s%s",azCol[i],
10744                   i==nArg-1 ? p->rowSeparator : p->colSeparator);
10745         }
10746       }
10747       if( azArg==0 ) break;
10748       for(i=0; i<nArg; i++){
10749         char *z = azArg[i];
10750         if( z==0 ) z = p->nullValue;
10751         utf8_printf(p->out, "%s", z);
10752         if( i<nArg-1 ){
10753           utf8_printf(p->out, "%s", p->colSeparator);
10754         }else{
10755           utf8_printf(p->out, "%s", p->rowSeparator);
10756         }
10757       }
10758       break;
10759     }
10760     case MODE_Html: {
10761       if( p->cnt++==0 && p->showHeader ){
10762         raw_printf(p->out,"<TR>");
10763         for(i=0; i<nArg; i++){
10764           raw_printf(p->out,"<TH>");
10765           output_html_string(p->out, azCol[i]);
10766           raw_printf(p->out,"</TH>\n");
10767         }
10768         raw_printf(p->out,"</TR>\n");
10769       }
10770       if( azArg==0 ) break;
10771       raw_printf(p->out,"<TR>");
10772       for(i=0; i<nArg; i++){
10773         raw_printf(p->out,"<TD>");
10774         output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
10775         raw_printf(p->out,"</TD>\n");
10776       }
10777       raw_printf(p->out,"</TR>\n");
10778       break;
10779     }
10780     case MODE_Tcl: {
10781       if( p->cnt++==0 && p->showHeader ){
10782         for(i=0; i<nArg; i++){
10783           output_c_string(p->out,azCol[i] ? azCol[i] : "");
10784           if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
10785         }
10786         utf8_printf(p->out, "%s", p->rowSeparator);
10787       }
10788       if( azArg==0 ) break;
10789       for(i=0; i<nArg; i++){
10790         output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
10791         if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
10792       }
10793       utf8_printf(p->out, "%s", p->rowSeparator);
10794       break;
10795     }
10796     case MODE_Csv: {
10797       setBinaryMode(p->out, 1);
10798       if( p->cnt++==0 && p->showHeader ){
10799         for(i=0; i<nArg; i++){
10800           output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
10801         }
10802         utf8_printf(p->out, "%s", p->rowSeparator);
10803       }
10804       if( nArg>0 ){
10805         for(i=0; i<nArg; i++){
10806           output_csv(p, azArg[i], i<nArg-1);
10807         }
10808         utf8_printf(p->out, "%s", p->rowSeparator);
10809       }
10810       setTextMode(p->out, 1);
10811       break;
10812     }
10813     case MODE_Insert: {
10814       if( azArg==0 ) break;
10815       utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
10816       if( p->showHeader ){
10817         raw_printf(p->out,"(");
10818         for(i=0; i<nArg; i++){
10819           if( i>0 ) raw_printf(p->out, ",");
10820           if( quoteChar(azCol[i]) ){
10821             char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
10822             utf8_printf(p->out, "%s", z);
10823             sqlite3_free(z);
10824           }else{
10825             raw_printf(p->out, "%s", azCol[i]);
10826           }
10827         }
10828         raw_printf(p->out,")");
10829       }
10830       p->cnt++;
10831       for(i=0; i<nArg; i++){
10832         raw_printf(p->out, i>0 ? "," : " VALUES(");
10833         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
10834           utf8_printf(p->out,"NULL");
10835         }else if( aiType && aiType[i]==SQLITE_TEXT ){
10836           if( ShellHasFlag(p, SHFLG_Newlines) ){
10837             output_quoted_string(p->out, azArg[i]);
10838           }else{
10839             output_quoted_escaped_string(p->out, azArg[i]);
10840           }
10841         }else if( aiType && aiType[i]==SQLITE_INTEGER ){
10842           utf8_printf(p->out,"%s", azArg[i]);
10843         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
10844           char z[50];
10845           double r = sqlite3_column_double(p->pStmt, i);
10846           sqlite3_uint64 ur;
10847           memcpy(&ur,&r,sizeof(r));
10848           if( ur==0x7ff0000000000000LL ){
10849             raw_printf(p->out, "1e999");
10850           }else if( ur==0xfff0000000000000LL ){
10851             raw_printf(p->out, "-1e999");
10852           }else{
10853             sqlite3_snprintf(50,z,"%!.20g", r);
10854             raw_printf(p->out, "%s", z);
10855           }
10856         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
10857           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
10858           int nBlob = sqlite3_column_bytes(p->pStmt, i);
10859           output_hex_blob(p->out, pBlob, nBlob);
10860         }else if( isNumber(azArg[i], 0) ){
10861           utf8_printf(p->out,"%s", azArg[i]);
10862         }else if( ShellHasFlag(p, SHFLG_Newlines) ){
10863           output_quoted_string(p->out, azArg[i]);
10864         }else{
10865           output_quoted_escaped_string(p->out, azArg[i]);
10866         }
10867       }
10868       raw_printf(p->out,");\n");
10869       break;
10870     }
10871     case MODE_Quote: {
10872       if( azArg==0 ) break;
10873       if( p->cnt==0 && p->showHeader ){
10874         for(i=0; i<nArg; i++){
10875           if( i>0 ) raw_printf(p->out, ",");
10876           output_quoted_string(p->out, azCol[i]);
10877         }
10878         raw_printf(p->out,"\n");
10879       }
10880       p->cnt++;
10881       for(i=0; i<nArg; i++){
10882         if( i>0 ) raw_printf(p->out, ",");
10883         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
10884           utf8_printf(p->out,"NULL");
10885         }else if( aiType && aiType[i]==SQLITE_TEXT ){
10886           output_quoted_string(p->out, azArg[i]);
10887         }else if( aiType && aiType[i]==SQLITE_INTEGER ){
10888           utf8_printf(p->out,"%s", azArg[i]);
10889         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
10890           char z[50];
10891           double r = sqlite3_column_double(p->pStmt, i);
10892           sqlite3_snprintf(50,z,"%!.20g", r);
10893           raw_printf(p->out, "%s", z);
10894         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
10895           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
10896           int nBlob = sqlite3_column_bytes(p->pStmt, i);
10897           output_hex_blob(p->out, pBlob, nBlob);
10898         }else if( isNumber(azArg[i], 0) ){
10899           utf8_printf(p->out,"%s", azArg[i]);
10900         }else{
10901           output_quoted_string(p->out, azArg[i]);
10902         }
10903       }
10904       raw_printf(p->out,"\n");
10905       break;
10906     }
10907     case MODE_Ascii: {
10908       if( p->cnt++==0 && p->showHeader ){
10909         for(i=0; i<nArg; i++){
10910           if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
10911           utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
10912         }
10913         utf8_printf(p->out, "%s", p->rowSeparator);
10914       }
10915       if( azArg==0 ) break;
10916       for(i=0; i<nArg; i++){
10917         if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
10918         utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
10919       }
10920       utf8_printf(p->out, "%s", p->rowSeparator);
10921       break;
10922     }
10923     case MODE_EQP: {
10924       eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
10925       break;
10926     }
10927   }
10928   return 0;
10929 }
10930
10931 /*
10932 ** This is the callback routine that the SQLite library
10933 ** invokes for each row of a query result.
10934 */
10935 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
10936   /* since we don't have type info, call the shell_callback with a NULL value */
10937   return shell_callback(pArg, nArg, azArg, azCol, NULL);
10938 }
10939
10940 /*
10941 ** This is the callback routine from sqlite3_exec() that appends all
10942 ** output onto the end of a ShellText object.
10943 */
10944 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
10945   ShellText *p = (ShellText*)pArg;
10946   int i;
10947   UNUSED_PARAMETER(az);
10948   if( azArg==0 ) return 0;
10949   if( p->n ) appendText(p, "|", 0);
10950   for(i=0; i<nArg; i++){
10951     if( i ) appendText(p, ",", 0);
10952     if( azArg[i] ) appendText(p, azArg[i], 0);
10953   }
10954   return 0;
10955 }
10956
10957 /*
10958 ** Generate an appropriate SELFTEST table in the main database.
10959 */
10960 static void createSelftestTable(ShellState *p){
10961   char *zErrMsg = 0;
10962   sqlite3_exec(p->db,
10963     "SAVEPOINT selftest_init;\n"
10964     "CREATE TABLE IF NOT EXISTS selftest(\n"
10965     "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
10966     "  op TEXT,\n"                   /* Operator:  memo run */
10967     "  cmd TEXT,\n"                  /* Command text */
10968     "  ans TEXT\n"                   /* Desired answer */
10969     ");"
10970     "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
10971     "INSERT INTO [_shell$self](rowid,op,cmd)\n"
10972     "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
10973     "         'memo','Tests generated by --init');\n"
10974     "INSERT INTO [_shell$self]\n"
10975     "  SELECT 'run',\n"
10976     "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
10977                                  "FROM sqlite_master ORDER BY 2'',224))',\n"
10978     "    hex(sha3_query('SELECT type,name,tbl_name,sql "
10979                           "FROM sqlite_master ORDER BY 2',224));\n"
10980     "INSERT INTO [_shell$self]\n"
10981     "  SELECT 'run',"
10982     "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
10983     "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
10984     "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
10985     "  FROM (\n"
10986     "    SELECT name FROM sqlite_master\n"
10987     "     WHERE type='table'\n"
10988     "       AND name<>'selftest'\n"
10989     "       AND coalesce(rootpage,0)>0\n"
10990     "  )\n"
10991     " ORDER BY name;\n"
10992     "INSERT INTO [_shell$self]\n"
10993     "  VALUES('run','PRAGMA integrity_check','ok');\n"
10994     "INSERT INTO selftest(tno,op,cmd,ans)"
10995     "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
10996     "DROP TABLE [_shell$self];"
10997     ,0,0,&zErrMsg);
10998   if( zErrMsg ){
10999     utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
11000     sqlite3_free(zErrMsg);
11001   }
11002   sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
11003 }
11004
11005
11006 /*
11007 ** Set the destination table field of the ShellState structure to
11008 ** the name of the table given.  Escape any quote characters in the
11009 ** table name.
11010 */
11011 static void set_table_name(ShellState *p, const char *zName){
11012   int i, n;
11013   char cQuote;
11014   char *z;
11015
11016   if( p->zDestTable ){
11017     free(p->zDestTable);
11018     p->zDestTable = 0;
11019   }
11020   if( zName==0 ) return;
11021   cQuote = quoteChar(zName);
11022   n = strlen30(zName);
11023   if( cQuote ) n += n+2;
11024   z = p->zDestTable = malloc( n+1 );
11025   if( z==0 ) shell_out_of_memory();
11026   n = 0;
11027   if( cQuote ) z[n++] = cQuote;
11028   for(i=0; zName[i]; i++){
11029     z[n++] = zName[i];
11030     if( zName[i]==cQuote ) z[n++] = cQuote;
11031   }
11032   if( cQuote ) z[n++] = cQuote;
11033   z[n] = 0;
11034 }
11035
11036
11037 /*
11038 ** Execute a query statement that will generate SQL output.  Print
11039 ** the result columns, comma-separated, on a line and then add a
11040 ** semicolon terminator to the end of that line.
11041 **
11042 ** If the number of columns is 1 and that column contains text "--"
11043 ** then write the semicolon on a separate line.  That way, if a
11044 ** "--" comment occurs at the end of the statement, the comment
11045 ** won't consume the semicolon terminator.
11046 */
11047 static int run_table_dump_query(
11048   ShellState *p,           /* Query context */
11049   const char *zSelect      /* SELECT statement to extract content */
11050 ){
11051   sqlite3_stmt *pSelect;
11052   int rc;
11053   int nResult;
11054   int i;
11055   const char *z;
11056   rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
11057   if( rc!=SQLITE_OK || !pSelect ){
11058     utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
11059                 sqlite3_errmsg(p->db));
11060     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
11061     return rc;
11062   }
11063   rc = sqlite3_step(pSelect);
11064   nResult = sqlite3_column_count(pSelect);
11065   while( rc==SQLITE_ROW ){
11066     z = (const char*)sqlite3_column_text(pSelect, 0);
11067     utf8_printf(p->out, "%s", z);
11068     for(i=1; i<nResult; i++){
11069       utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
11070     }
11071     if( z==0 ) z = "";
11072     while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
11073     if( z[0] ){
11074       raw_printf(p->out, "\n;\n");
11075     }else{
11076       raw_printf(p->out, ";\n");
11077     }
11078     rc = sqlite3_step(pSelect);
11079   }
11080   rc = sqlite3_finalize(pSelect);
11081   if( rc!=SQLITE_OK ){
11082     utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
11083                 sqlite3_errmsg(p->db));
11084     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
11085   }
11086   return rc;
11087 }
11088
11089 /*
11090 ** Allocate space and save off current error string.
11091 */
11092 static char *save_err_msg(
11093   sqlite3 *db            /* Database to query */
11094 ){
11095   int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
11096   char *zErrMsg = sqlite3_malloc64(nErrMsg);
11097   if( zErrMsg ){
11098     memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
11099   }
11100   return zErrMsg;
11101 }
11102
11103 #ifdef __linux__
11104 /*
11105 ** Attempt to display I/O stats on Linux using /proc/PID/io
11106 */
11107 static void displayLinuxIoStats(FILE *out){
11108   FILE *in;
11109   char z[200];
11110   sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
11111   in = fopen(z, "rb");
11112   if( in==0 ) return;
11113   while( fgets(z, sizeof(z), in)!=0 ){
11114     static const struct {
11115       const char *zPattern;
11116       const char *zDesc;
11117     } aTrans[] = {
11118       { "rchar: ",                  "Bytes received by read():" },
11119       { "wchar: ",                  "Bytes sent to write():"    },
11120       { "syscr: ",                  "Read() system calls:"      },
11121       { "syscw: ",                  "Write() system calls:"     },
11122       { "read_bytes: ",             "Bytes read from storage:"  },
11123       { "write_bytes: ",            "Bytes written to storage:" },
11124       { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
11125     };
11126     int i;
11127     for(i=0; i<ArraySize(aTrans); i++){
11128       int n = strlen30(aTrans[i].zPattern);
11129       if( strncmp(aTrans[i].zPattern, z, n)==0 ){
11130         utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
11131         break;
11132       }
11133     }
11134   }
11135   fclose(in);
11136 }
11137 #endif
11138
11139 /*
11140 ** Display a single line of status using 64-bit values.
11141 */
11142 static void displayStatLine(
11143   ShellState *p,            /* The shell context */
11144   char *zLabel,             /* Label for this one line */
11145   char *zFormat,            /* Format for the result */
11146   int iStatusCtrl,          /* Which status to display */
11147   int bReset                /* True to reset the stats */
11148 ){
11149   sqlite3_int64 iCur = -1;
11150   sqlite3_int64 iHiwtr = -1;
11151   int i, nPercent;
11152   char zLine[200];
11153   sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
11154   for(i=0, nPercent=0; zFormat[i]; i++){
11155     if( zFormat[i]=='%' ) nPercent++;
11156   }
11157   if( nPercent>1 ){
11158     sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
11159   }else{
11160     sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
11161   }
11162   raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
11163 }
11164
11165 /*
11166 ** Display memory stats.
11167 */
11168 static int display_stats(
11169   sqlite3 *db,                /* Database to query */
11170   ShellState *pArg,           /* Pointer to ShellState */
11171   int bReset                  /* True to reset the stats */
11172 ){
11173   int iCur;
11174   int iHiwtr;
11175   FILE *out;
11176   if( pArg==0 || pArg->out==0 ) return 0;
11177   out = pArg->out;
11178
11179   if( pArg->pStmt && (pArg->statsOn & 2) ){
11180     int nCol, i, x;
11181     sqlite3_stmt *pStmt = pArg->pStmt;
11182     char z[100];
11183     nCol = sqlite3_column_count(pStmt);
11184     raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
11185     for(i=0; i<nCol; i++){
11186       sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
11187       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
11188 #ifndef SQLITE_OMIT_DECLTYPE
11189       sqlite3_snprintf(30, z+x, "declared type:");
11190       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
11191 #endif
11192 #ifdef SQLITE_ENABLE_COLUMN_METADATA
11193       sqlite3_snprintf(30, z+x, "database name:");
11194       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
11195       sqlite3_snprintf(30, z+x, "table name:");
11196       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
11197       sqlite3_snprintf(30, z+x, "origin name:");
11198       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
11199 #endif
11200     }
11201   }
11202
11203   displayStatLine(pArg, "Memory Used:",
11204      "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
11205   displayStatLine(pArg, "Number of Outstanding Allocations:",
11206      "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
11207   if( pArg->shellFlgs & SHFLG_Pagecache ){
11208     displayStatLine(pArg, "Number of Pcache Pages Used:",
11209        "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
11210   }
11211   displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
11212      "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
11213   displayStatLine(pArg, "Largest Allocation:",
11214      "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
11215   displayStatLine(pArg, "Largest Pcache Allocation:",
11216      "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
11217 #ifdef YYTRACKMAXSTACKDEPTH
11218   displayStatLine(pArg, "Deepest Parser Stack:",
11219      "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
11220 #endif
11221
11222   if( db ){
11223     if( pArg->shellFlgs & SHFLG_Lookaside ){
11224       iHiwtr = iCur = -1;
11225       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
11226                         &iCur, &iHiwtr, bReset);
11227       raw_printf(pArg->out,
11228               "Lookaside Slots Used:                %d (max %d)\n",
11229               iCur, iHiwtr);
11230       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
11231                         &iCur, &iHiwtr, bReset);
11232       raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
11233               iHiwtr);
11234       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
11235                         &iCur, &iHiwtr, bReset);
11236       raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
11237               iHiwtr);
11238       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
11239                         &iCur, &iHiwtr, bReset);
11240       raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
11241               iHiwtr);
11242     }
11243     iHiwtr = iCur = -1;
11244     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
11245     raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
11246             iCur);
11247     iHiwtr = iCur = -1;
11248     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
11249     raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
11250     iHiwtr = iCur = -1;
11251     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
11252     raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
11253     iHiwtr = iCur = -1;
11254     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
11255     raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
11256     iHiwtr = iCur = -1;
11257     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
11258     raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
11259     iHiwtr = iCur = -1;
11260     sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
11261     raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
11262             iCur);
11263     iHiwtr = iCur = -1;
11264     sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
11265     raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
11266             iCur);
11267   }
11268
11269   if( pArg->pStmt ){
11270     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
11271                                bReset);
11272     raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
11273     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
11274     raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
11275     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
11276     raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
11277     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
11278     raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
11279     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
11280     raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
11281     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
11282     raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
11283     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
11284     raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
11285   }
11286
11287 #ifdef __linux__
11288   displayLinuxIoStats(pArg->out);
11289 #endif
11290
11291   /* Do not remove this machine readable comment: extra-stats-output-here */
11292
11293   return 0;
11294 }
11295
11296 /*
11297 ** Display scan stats.
11298 */
11299 static void display_scanstats(
11300   sqlite3 *db,                    /* Database to query */
11301   ShellState *pArg                /* Pointer to ShellState */
11302 ){
11303 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
11304   UNUSED_PARAMETER(db);
11305   UNUSED_PARAMETER(pArg);
11306 #else
11307   int i, k, n, mx;
11308   raw_printf(pArg->out, "-------- scanstats --------\n");
11309   mx = 0;
11310   for(k=0; k<=mx; k++){
11311     double rEstLoop = 1.0;
11312     for(i=n=0; 1; i++){
11313       sqlite3_stmt *p = pArg->pStmt;
11314       sqlite3_int64 nLoop, nVisit;
11315       double rEst;
11316       int iSid;
11317       const char *zExplain;
11318       if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
11319         break;
11320       }
11321       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
11322       if( iSid>mx ) mx = iSid;
11323       if( iSid!=k ) continue;
11324       if( n==0 ){
11325         rEstLoop = (double)nLoop;
11326         if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
11327       }
11328       n++;
11329       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
11330       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
11331       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
11332       utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
11333       rEstLoop *= rEst;
11334       raw_printf(pArg->out,
11335           "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
11336           nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
11337       );
11338     }
11339   }
11340   raw_printf(pArg->out, "---------------------------\n");
11341 #endif
11342 }
11343
11344 /*
11345 ** Parameter azArray points to a zero-terminated array of strings. zStr
11346 ** points to a single nul-terminated string. Return non-zero if zStr
11347 ** is equal, according to strcmp(), to any of the strings in the array.
11348 ** Otherwise, return zero.
11349 */
11350 static int str_in_array(const char *zStr, const char **azArray){
11351   int i;
11352   for(i=0; azArray[i]; i++){
11353     if( 0==strcmp(zStr, azArray[i]) ) return 1;
11354   }
11355   return 0;
11356 }
11357
11358 /*
11359 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
11360 ** and populate the ShellState.aiIndent[] array with the number of
11361 ** spaces each opcode should be indented before it is output.
11362 **
11363 ** The indenting rules are:
11364 **
11365 **     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
11366 **       all opcodes that occur between the p2 jump destination and the opcode
11367 **       itself by 2 spaces.
11368 **
11369 **     * For each "Goto", if the jump destination is earlier in the program
11370 **       and ends on one of:
11371 **          Yield  SeekGt  SeekLt  RowSetRead  Rewind
11372 **       or if the P1 parameter is one instead of zero,
11373 **       then indent all opcodes between the earlier instruction
11374 **       and "Goto" by 2 spaces.
11375 */
11376 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
11377   const char *zSql;               /* The text of the SQL statement */
11378   const char *z;                  /* Used to check if this is an EXPLAIN */
11379   int *abYield = 0;               /* True if op is an OP_Yield */
11380   int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
11381   int iOp;                        /* Index of operation in p->aiIndent[] */
11382
11383   const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
11384   const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
11385                             "Rewind", 0 };
11386   const char *azGoto[] = { "Goto", 0 };
11387
11388   /* Try to figure out if this is really an EXPLAIN statement. If this
11389   ** cannot be verified, return early.  */
11390   if( sqlite3_column_count(pSql)!=8 ){
11391     p->cMode = p->mode;
11392     return;
11393   }
11394   zSql = sqlite3_sql(pSql);
11395   if( zSql==0 ) return;
11396   for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
11397   if( sqlite3_strnicmp(z, "explain", 7) ){
11398     p->cMode = p->mode;
11399     return;
11400   }
11401
11402   for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
11403     int i;
11404     int iAddr = sqlite3_column_int(pSql, 0);
11405     const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
11406
11407     /* Set p2 to the P2 field of the current opcode. Then, assuming that
11408     ** p2 is an instruction address, set variable p2op to the index of that
11409     ** instruction in the aiIndent[] array. p2 and p2op may be different if
11410     ** the current instruction is part of a sub-program generated by an
11411     ** SQL trigger or foreign key.  */
11412     int p2 = sqlite3_column_int(pSql, 3);
11413     int p2op = (p2 + (iOp-iAddr));
11414
11415     /* Grow the p->aiIndent array as required */
11416     if( iOp>=nAlloc ){
11417       if( iOp==0 ){
11418         /* Do further verfication that this is explain output.  Abort if
11419         ** it is not */
11420         static const char *explainCols[] = {
11421            "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
11422         int jj;
11423         for(jj=0; jj<ArraySize(explainCols); jj++){
11424           if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
11425             p->cMode = p->mode;
11426             sqlite3_reset(pSql);
11427             return;
11428           }
11429         }
11430       }
11431       nAlloc += 100;
11432       p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
11433       if( p->aiIndent==0 ) shell_out_of_memory();
11434       abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
11435       if( abYield==0 ) shell_out_of_memory();
11436     }
11437     abYield[iOp] = str_in_array(zOp, azYield);
11438     p->aiIndent[iOp] = 0;
11439     p->nIndent = iOp+1;
11440
11441     if( str_in_array(zOp, azNext) ){
11442       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
11443     }
11444     if( str_in_array(zOp, azGoto) && p2op<p->nIndent
11445      && (abYield[p2op] || sqlite3_column_int(pSql, 2))
11446     ){
11447       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
11448     }
11449   }
11450
11451   p->iIndent = 0;
11452   sqlite3_free(abYield);
11453   sqlite3_reset(pSql);
11454 }
11455
11456 /*
11457 ** Free the array allocated by explain_data_prepare().
11458 */
11459 static void explain_data_delete(ShellState *p){
11460   sqlite3_free(p->aiIndent);
11461   p->aiIndent = 0;
11462   p->nIndent = 0;
11463   p->iIndent = 0;
11464 }
11465
11466 /*
11467 ** Disable and restore .wheretrace and .selecttrace settings.
11468 */
11469 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
11470 extern int sqlite3SelectTrace;
11471 static int savedSelectTrace;
11472 #endif
11473 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
11474 extern int sqlite3WhereTrace;
11475 static int savedWhereTrace;
11476 #endif
11477 static void disable_debug_trace_modes(void){
11478 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
11479   savedSelectTrace = sqlite3SelectTrace;
11480   sqlite3SelectTrace = 0;
11481 #endif
11482 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
11483   savedWhereTrace = sqlite3WhereTrace;
11484   sqlite3WhereTrace = 0;
11485 #endif
11486 }
11487 static void restore_debug_trace_modes(void){
11488 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
11489   sqlite3SelectTrace = savedSelectTrace;
11490 #endif
11491 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
11492   sqlite3WhereTrace = savedWhereTrace;
11493 #endif
11494 }
11495
11496 /* Create the TEMP table used to store parameter bindings */
11497 static void bind_table_init(ShellState *p){
11498   int wrSchema = 0;
11499   int defensiveMode = 0;
11500   sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
11501   sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
11502   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
11503   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
11504   sqlite3_exec(p->db,
11505     "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
11506     "  key TEXT PRIMARY KEY,\n"
11507     "  value ANY\n"
11508     ") WITHOUT ROWID;",
11509     0, 0, 0);
11510   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
11511   sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
11512 }
11513
11514 /*
11515 ** Bind parameters on a prepared statement.
11516 **
11517 ** Parameter bindings are taken from a TEMP table of the form:
11518 **
11519 **    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
11520 **    WITHOUT ROWID;
11521 **
11522 ** No bindings occur if this table does not exist.  The name of the table
11523 ** begins with "sqlite_" so that it will not collide with ordinary application
11524 ** tables.  The table must be in the TEMP schema.
11525 */
11526 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
11527   int nVar;
11528   int i;
11529   int rc;
11530   sqlite3_stmt *pQ = 0;
11531
11532   nVar = sqlite3_bind_parameter_count(pStmt);
11533   if( nVar==0 ) return;  /* Nothing to do */
11534   if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
11535                                     "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
11536     return; /* Parameter table does not exist */
11537   }
11538   rc = sqlite3_prepare_v2(pArg->db,
11539           "SELECT value FROM temp.sqlite_parameters"
11540           " WHERE key=?1", -1, &pQ, 0);
11541   if( rc || pQ==0 ) return;
11542   for(i=1; i<=nVar; i++){
11543     char zNum[30];
11544     const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
11545     if( zVar==0 ){
11546       sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
11547       zVar = zNum;
11548     }
11549     sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
11550     if( sqlite3_step(pQ)==SQLITE_ROW ){
11551       sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
11552     }else{
11553       sqlite3_bind_null(pStmt, i);
11554     }
11555     sqlite3_reset(pQ);
11556   }
11557   sqlite3_finalize(pQ);
11558 }
11559
11560 /*
11561 ** Run a prepared statement
11562 */
11563 static void exec_prepared_stmt(
11564   ShellState *pArg,                                /* Pointer to ShellState */
11565   sqlite3_stmt *pStmt                              /* Statment to run */
11566 ){
11567   int rc;
11568
11569   /* perform the first step.  this will tell us if we
11570   ** have a result set or not and how wide it is.
11571   */
11572   rc = sqlite3_step(pStmt);
11573   /* if we have a result set... */
11574   if( SQLITE_ROW == rc ){
11575     /* allocate space for col name ptr, value ptr, and type */
11576     int nCol = sqlite3_column_count(pStmt);
11577     void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
11578     if( !pData ){
11579       rc = SQLITE_NOMEM;
11580     }else{
11581       char **azCols = (char **)pData;      /* Names of result columns */
11582       char **azVals = &azCols[nCol];       /* Results */
11583       int *aiTypes = (int *)&azVals[nCol]; /* Result types */
11584       int i, x;
11585       assert(sizeof(int) <= sizeof(char *));
11586       /* save off ptrs to column names */
11587       for(i=0; i<nCol; i++){
11588         azCols[i] = (char *)sqlite3_column_name(pStmt, i);
11589       }
11590       do{
11591         /* extract the data and data types */
11592         for(i=0; i<nCol; i++){
11593           aiTypes[i] = x = sqlite3_column_type(pStmt, i);
11594           if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
11595             azVals[i] = "";
11596           }else{
11597             azVals[i] = (char*)sqlite3_column_text(pStmt, i);
11598           }
11599           if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
11600             rc = SQLITE_NOMEM;
11601             break; /* from for */
11602           }
11603         } /* end for */
11604
11605         /* if data and types extracted successfully... */
11606         if( SQLITE_ROW == rc ){
11607           /* call the supplied callback with the result row data */
11608           if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
11609             rc = SQLITE_ABORT;
11610           }else{
11611             rc = sqlite3_step(pStmt);
11612           }
11613         }
11614       } while( SQLITE_ROW == rc );
11615       sqlite3_free(pData);
11616     }
11617   }
11618 }
11619
11620 #ifndef SQLITE_OMIT_VIRTUALTABLE
11621 /*
11622 ** This function is called to process SQL if the previous shell command
11623 ** was ".expert". It passes the SQL in the second argument directly to
11624 ** the sqlite3expert object.
11625 **
11626 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
11627 ** code. In this case, (*pzErr) may be set to point to a buffer containing
11628 ** an English language error message. It is the responsibility of the
11629 ** caller to eventually free this buffer using sqlite3_free().
11630 */
11631 static int expertHandleSQL(
11632   ShellState *pState, 
11633   const char *zSql, 
11634   char **pzErr
11635 ){
11636   assert( pState->expert.pExpert );
11637   assert( pzErr==0 || *pzErr==0 );
11638   return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
11639 }
11640
11641 /*
11642 ** This function is called either to silently clean up the object
11643 ** created by the ".expert" command (if bCancel==1), or to generate a 
11644 ** report from it and then clean it up (if bCancel==0).
11645 **
11646 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
11647 ** code. In this case, (*pzErr) may be set to point to a buffer containing
11648 ** an English language error message. It is the responsibility of the
11649 ** caller to eventually free this buffer using sqlite3_free().
11650 */
11651 static int expertFinish(
11652   ShellState *pState,
11653   int bCancel,
11654   char **pzErr
11655 ){
11656   int rc = SQLITE_OK;
11657   sqlite3expert *p = pState->expert.pExpert;
11658   assert( p );
11659   assert( bCancel || pzErr==0 || *pzErr==0 );
11660   if( bCancel==0 ){
11661     FILE *out = pState->out;
11662     int bVerbose = pState->expert.bVerbose;
11663
11664     rc = sqlite3_expert_analyze(p, pzErr);
11665     if( rc==SQLITE_OK ){
11666       int nQuery = sqlite3_expert_count(p);
11667       int i;
11668
11669       if( bVerbose ){
11670         const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
11671         raw_printf(out, "-- Candidates -----------------------------\n");
11672         raw_printf(out, "%s\n", zCand);
11673       }
11674       for(i=0; i<nQuery; i++){
11675         const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
11676         const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
11677         const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
11678         if( zIdx==0 ) zIdx = "(no new indexes)\n";
11679         if( bVerbose ){
11680           raw_printf(out, "-- Query %d --------------------------------\n",i+1);
11681           raw_printf(out, "%s\n\n", zSql);
11682         }
11683         raw_printf(out, "%s\n", zIdx);
11684         raw_printf(out, "%s\n", zEQP);
11685       }
11686     }
11687   }
11688   sqlite3_expert_destroy(p);
11689   pState->expert.pExpert = 0;
11690   return rc;
11691 }
11692
11693 /*
11694 ** Implementation of ".expert" dot command.
11695 */
11696 static int expertDotCommand(
11697   ShellState *pState,             /* Current shell tool state */
11698   char **azArg,                   /* Array of arguments passed to dot command */
11699   int nArg                        /* Number of entries in azArg[] */
11700 ){
11701   int rc = SQLITE_OK;
11702   char *zErr = 0;
11703   int i;
11704   int iSample = 0;
11705
11706   assert( pState->expert.pExpert==0 );
11707   memset(&pState->expert, 0, sizeof(ExpertInfo));
11708
11709   for(i=1; rc==SQLITE_OK && i<nArg; i++){
11710     char *z = azArg[i];
11711     int n;
11712     if( z[0]=='-' && z[1]=='-' ) z++;
11713     n = strlen30(z);
11714     if( n>=2 && 0==strncmp(z, "-verbose", n) ){
11715       pState->expert.bVerbose = 1;
11716     }
11717     else if( n>=2 && 0==strncmp(z, "-sample", n) ){
11718       if( i==(nArg-1) ){
11719         raw_printf(stderr, "option requires an argument: %s\n", z);
11720         rc = SQLITE_ERROR;
11721       }else{
11722         iSample = (int)integerValue(azArg[++i]);
11723         if( iSample<0 || iSample>100 ){
11724           raw_printf(stderr, "value out of range: %s\n", azArg[i]);
11725           rc = SQLITE_ERROR;
11726         }
11727       }
11728     }
11729     else{
11730       raw_printf(stderr, "unknown option: %s\n", z);
11731       rc = SQLITE_ERROR;
11732     }
11733   }
11734
11735   if( rc==SQLITE_OK ){
11736     pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
11737     if( pState->expert.pExpert==0 ){
11738       raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
11739       rc = SQLITE_ERROR;
11740     }else{
11741       sqlite3_expert_config(
11742           pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
11743       );
11744     }
11745   }
11746
11747   return rc;
11748 }
11749 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
11750
11751 /*
11752 ** Execute a statement or set of statements.  Print
11753 ** any result rows/columns depending on the current mode
11754 ** set via the supplied callback.
11755 **
11756 ** This is very similar to SQLite's built-in sqlite3_exec()
11757 ** function except it takes a slightly different callback
11758 ** and callback data argument.
11759 */
11760 static int shell_exec(
11761   ShellState *pArg,                         /* Pointer to ShellState */
11762   const char *zSql,                         /* SQL to be evaluated */
11763   char **pzErrMsg                           /* Error msg written here */
11764 ){
11765   sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
11766   int rc = SQLITE_OK;             /* Return Code */
11767   int rc2;
11768   const char *zLeftover;          /* Tail of unprocessed SQL */
11769   sqlite3 *db = pArg->db;
11770
11771   if( pzErrMsg ){
11772     *pzErrMsg = NULL;
11773   }
11774
11775 #ifndef SQLITE_OMIT_VIRTUALTABLE
11776   if( pArg->expert.pExpert ){
11777     rc = expertHandleSQL(pArg, zSql, pzErrMsg);
11778     return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
11779   }
11780 #endif
11781
11782   while( zSql[0] && (SQLITE_OK == rc) ){
11783     static const char *zStmtSql;
11784     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
11785     if( SQLITE_OK != rc ){
11786       if( pzErrMsg ){
11787         *pzErrMsg = save_err_msg(db);
11788       }
11789     }else{
11790       if( !pStmt ){
11791         /* this happens for a comment or white-space */
11792         zSql = zLeftover;
11793         while( IsSpace(zSql[0]) ) zSql++;
11794         continue;
11795       }
11796       zStmtSql = sqlite3_sql(pStmt);
11797       if( zStmtSql==0 ) zStmtSql = "";
11798       while( IsSpace(zStmtSql[0]) ) zStmtSql++;
11799
11800       /* save off the prepared statment handle and reset row count */
11801       if( pArg ){
11802         pArg->pStmt = pStmt;
11803         pArg->cnt = 0;
11804       }
11805
11806       /* echo the sql statement if echo on */
11807       if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
11808         utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
11809       }
11810
11811       /* Show the EXPLAIN QUERY PLAN if .eqp is on */
11812       if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
11813         sqlite3_stmt *pExplain;
11814         char *zEQP;
11815         int triggerEQP = 0;
11816         disable_debug_trace_modes();
11817         sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
11818         if( pArg->autoEQP>=AUTOEQP_trigger ){
11819           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
11820         }
11821         zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
11822         rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
11823         if( rc==SQLITE_OK ){
11824           while( sqlite3_step(pExplain)==SQLITE_ROW ){
11825             const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
11826             int iEqpId = sqlite3_column_int(pExplain, 0);
11827             int iParentId = sqlite3_column_int(pExplain, 1);
11828             if( zEQPLine==0 ) zEQPLine = "";
11829             if( zEQPLine[0]=='-' ) eqp_render(pArg);
11830             eqp_append(pArg, iEqpId, iParentId, zEQPLine);
11831           }
11832           eqp_render(pArg);
11833         }
11834         sqlite3_finalize(pExplain);
11835         sqlite3_free(zEQP);
11836         if( pArg->autoEQP>=AUTOEQP_full ){
11837           /* Also do an EXPLAIN for ".eqp full" mode */
11838           zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
11839           rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
11840           if( rc==SQLITE_OK ){
11841             pArg->cMode = MODE_Explain;
11842             explain_data_prepare(pArg, pExplain);
11843             exec_prepared_stmt(pArg, pExplain);
11844             explain_data_delete(pArg);
11845           }
11846           sqlite3_finalize(pExplain);
11847           sqlite3_free(zEQP);
11848         }
11849         if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
11850           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
11851           /* Reprepare pStmt before reactiving trace modes */
11852           sqlite3_finalize(pStmt);
11853           sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
11854           if( pArg ) pArg->pStmt = pStmt;
11855         }
11856         restore_debug_trace_modes();
11857       }
11858
11859       if( pArg ){
11860         pArg->cMode = pArg->mode;
11861         if( pArg->autoExplain ){
11862           if( sqlite3_stmt_isexplain(pStmt)==1 ){
11863             pArg->cMode = MODE_Explain;
11864           }
11865           if( sqlite3_stmt_isexplain(pStmt)==2 ){
11866             pArg->cMode = MODE_EQP;
11867           }
11868         }
11869
11870         /* If the shell is currently in ".explain" mode, gather the extra
11871         ** data required to add indents to the output.*/
11872         if( pArg->cMode==MODE_Explain ){
11873           explain_data_prepare(pArg, pStmt);
11874         }
11875       }
11876
11877       bind_prepared_stmt(pArg, pStmt);
11878       exec_prepared_stmt(pArg, pStmt);
11879       explain_data_delete(pArg);
11880       eqp_render(pArg);
11881
11882       /* print usage stats if stats on */
11883       if( pArg && pArg->statsOn ){
11884         display_stats(db, pArg, 0);
11885       }
11886
11887       /* print loop-counters if required */
11888       if( pArg && pArg->scanstatsOn ){
11889         display_scanstats(db, pArg);
11890       }
11891
11892       /* Finalize the statement just executed. If this fails, save a
11893       ** copy of the error message. Otherwise, set zSql to point to the
11894       ** next statement to execute. */
11895       rc2 = sqlite3_finalize(pStmt);
11896       if( rc!=SQLITE_NOMEM ) rc = rc2;
11897       if( rc==SQLITE_OK ){
11898         zSql = zLeftover;
11899         while( IsSpace(zSql[0]) ) zSql++;
11900       }else if( pzErrMsg ){
11901         *pzErrMsg = save_err_msg(db);
11902       }
11903
11904       /* clear saved stmt handle */
11905       if( pArg ){
11906         pArg->pStmt = NULL;
11907       }
11908     }
11909   } /* end while */
11910
11911   return rc;
11912 }
11913
11914 /*
11915 ** Release memory previously allocated by tableColumnList().
11916 */
11917 static void freeColumnList(char **azCol){
11918   int i;
11919   for(i=1; azCol[i]; i++){
11920     sqlite3_free(azCol[i]);
11921   }
11922   /* azCol[0] is a static string */
11923   sqlite3_free(azCol);
11924 }
11925
11926 /*
11927 ** Return a list of pointers to strings which are the names of all
11928 ** columns in table zTab.   The memory to hold the names is dynamically
11929 ** allocated and must be released by the caller using a subsequent call
11930 ** to freeColumnList().
11931 **
11932 ** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
11933 ** value that needs to be preserved, then azCol[0] is filled in with the
11934 ** name of the rowid column.
11935 **
11936 ** The first regular column in the table is azCol[1].  The list is terminated
11937 ** by an entry with azCol[i]==0.
11938 */
11939 static char **tableColumnList(ShellState *p, const char *zTab){
11940   char **azCol = 0;
11941   sqlite3_stmt *pStmt;
11942   char *zSql;
11943   int nCol = 0;
11944   int nAlloc = 0;
11945   int nPK = 0;       /* Number of PRIMARY KEY columns seen */
11946   int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
11947   int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
11948   int rc;
11949
11950   zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
11951   rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
11952   sqlite3_free(zSql);
11953   if( rc ) return 0;
11954   while( sqlite3_step(pStmt)==SQLITE_ROW ){
11955     if( nCol>=nAlloc-2 ){
11956       nAlloc = nAlloc*2 + nCol + 10;
11957       azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
11958       if( azCol==0 ) shell_out_of_memory();
11959     }
11960     azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
11961     if( sqlite3_column_int(pStmt, 5) ){
11962       nPK++;
11963       if( nPK==1
11964        && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
11965                           "INTEGER")==0
11966       ){
11967         isIPK = 1;
11968       }else{
11969         isIPK = 0;
11970       }
11971     }
11972   }
11973   sqlite3_finalize(pStmt);
11974   if( azCol==0 ) return 0;
11975   azCol[0] = 0;
11976   azCol[nCol+1] = 0;
11977
11978   /* The decision of whether or not a rowid really needs to be preserved
11979   ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
11980   ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
11981   ** rowids on tables where the rowid is inaccessible because there are other
11982   ** columns in the table named "rowid", "_rowid_", and "oid".
11983   */
11984   if( preserveRowid && isIPK ){
11985     /* If a single PRIMARY KEY column with type INTEGER was seen, then it
11986     ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
11987     ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
11988     ** ROWID aliases.  To distinguish these cases, check to see if
11989     ** there is a "pk" entry in "PRAGMA index_list".  There will be
11990     ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
11991     */
11992     zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
11993                            " WHERE origin='pk'", zTab);
11994     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
11995     sqlite3_free(zSql);
11996     if( rc ){
11997       freeColumnList(azCol);
11998       return 0;
11999     }
12000     rc = sqlite3_step(pStmt);
12001     sqlite3_finalize(pStmt);
12002     preserveRowid = rc==SQLITE_ROW;
12003   }
12004   if( preserveRowid ){
12005     /* Only preserve the rowid if we can find a name to use for the
12006     ** rowid */
12007     static char *azRowid[] = { "rowid", "_rowid_", "oid" };
12008     int i, j;
12009     for(j=0; j<3; j++){
12010       for(i=1; i<=nCol; i++){
12011         if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
12012       }
12013       if( i>nCol ){
12014         /* At this point, we know that azRowid[j] is not the name of any
12015         ** ordinary column in the table.  Verify that azRowid[j] is a valid
12016         ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
12017         ** tables will fail this last check */
12018         rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
12019         if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
12020         break;
12021       }
12022     }
12023   }
12024   return azCol;
12025 }
12026
12027 /*
12028 ** Toggle the reverse_unordered_selects setting.
12029 */
12030 static void toggleSelectOrder(sqlite3 *db){
12031   sqlite3_stmt *pStmt = 0;
12032   int iSetting = 0;
12033   char zStmt[100];
12034   sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
12035   if( sqlite3_step(pStmt)==SQLITE_ROW ){
12036     iSetting = sqlite3_column_int(pStmt, 0);
12037   }
12038   sqlite3_finalize(pStmt);
12039   sqlite3_snprintf(sizeof(zStmt), zStmt,
12040        "PRAGMA reverse_unordered_selects(%d)", !iSetting);
12041   sqlite3_exec(db, zStmt, 0, 0, 0);
12042 }
12043
12044 /*
12045 ** This is a different callback routine used for dumping the database.
12046 ** Each row received by this callback consists of a table name,
12047 ** the table type ("index" or "table") and SQL to create the table.
12048 ** This routine should print text sufficient to recreate the table.
12049 */
12050 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
12051   int rc;
12052   const char *zTable;
12053   const char *zType;
12054   const char *zSql;
12055   ShellState *p = (ShellState *)pArg;
12056
12057   UNUSED_PARAMETER(azNotUsed);
12058   if( nArg!=3 || azArg==0 ) return 0;
12059   zTable = azArg[0];
12060   zType = azArg[1];
12061   zSql = azArg[2];
12062
12063   if( strcmp(zTable, "sqlite_sequence")==0 ){
12064     raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
12065   }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
12066     raw_printf(p->out, "ANALYZE sqlite_master;\n");
12067   }else if( strncmp(zTable, "sqlite_", 7)==0 ){
12068     return 0;
12069   }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
12070     char *zIns;
12071     if( !p->writableSchema ){
12072       raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
12073       p->writableSchema = 1;
12074     }
12075     zIns = sqlite3_mprintf(
12076        "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
12077        "VALUES('table','%q','%q',0,'%q');",
12078        zTable, zTable, zSql);
12079     utf8_printf(p->out, "%s\n", zIns);
12080     sqlite3_free(zIns);
12081     return 0;
12082   }else{
12083     printSchemaLine(p->out, zSql, ";\n");
12084   }
12085
12086   if( strcmp(zType, "table")==0 ){
12087     ShellText sSelect;
12088     ShellText sTable;
12089     char **azCol;
12090     int i;
12091     char *savedDestTable;
12092     int savedMode;
12093
12094     azCol = tableColumnList(p, zTable);
12095     if( azCol==0 ){
12096       p->nErr++;
12097       return 0;
12098     }
12099
12100     /* Always quote the table name, even if it appears to be pure ascii,
12101     ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
12102     initText(&sTable);
12103     appendText(&sTable, zTable, quoteChar(zTable));
12104     /* If preserving the rowid, add a column list after the table name.
12105     ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
12106     ** instead of the usual "INSERT INTO tab VALUES(...)".
12107     */
12108     if( azCol[0] ){
12109       appendText(&sTable, "(", 0);
12110       appendText(&sTable, azCol[0], 0);
12111       for(i=1; azCol[i]; i++){
12112         appendText(&sTable, ",", 0);
12113         appendText(&sTable, azCol[i], quoteChar(azCol[i]));
12114       }
12115       appendText(&sTable, ")", 0);
12116     }
12117
12118     /* Build an appropriate SELECT statement */
12119     initText(&sSelect);
12120     appendText(&sSelect, "SELECT ", 0);
12121     if( azCol[0] ){
12122       appendText(&sSelect, azCol[0], 0);
12123       appendText(&sSelect, ",", 0);
12124     }
12125     for(i=1; azCol[i]; i++){
12126       appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
12127       if( azCol[i+1] ){
12128         appendText(&sSelect, ",", 0);
12129       }
12130     }
12131     freeColumnList(azCol);
12132     appendText(&sSelect, " FROM ", 0);
12133     appendText(&sSelect, zTable, quoteChar(zTable));
12134
12135     savedDestTable = p->zDestTable;
12136     savedMode = p->mode;
12137     p->zDestTable = sTable.z;
12138     p->mode = p->cMode = MODE_Insert;
12139     rc = shell_exec(p, sSelect.z, 0);
12140     if( (rc&0xff)==SQLITE_CORRUPT ){
12141       raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
12142       toggleSelectOrder(p->db);
12143       shell_exec(p, sSelect.z, 0);
12144       toggleSelectOrder(p->db);
12145     }
12146     p->zDestTable = savedDestTable;
12147     p->mode = savedMode;
12148     freeText(&sTable);
12149     freeText(&sSelect);
12150     if( rc ) p->nErr++;
12151   }
12152   return 0;
12153 }
12154
12155 /*
12156 ** Run zQuery.  Use dump_callback() as the callback routine so that
12157 ** the contents of the query are output as SQL statements.
12158 **
12159 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
12160 ** "ORDER BY rowid DESC" to the end.
12161 */
12162 static int run_schema_dump_query(
12163   ShellState *p,
12164   const char *zQuery
12165 ){
12166   int rc;
12167   char *zErr = 0;
12168   rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
12169   if( rc==SQLITE_CORRUPT ){
12170     char *zQ2;
12171     int len = strlen30(zQuery);
12172     raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
12173     if( zErr ){
12174       utf8_printf(p->out, "/****** %s ******/\n", zErr);
12175       sqlite3_free(zErr);
12176       zErr = 0;
12177     }
12178     zQ2 = malloc( len+100 );
12179     if( zQ2==0 ) return rc;
12180     sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
12181     rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
12182     if( rc ){
12183       utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
12184     }else{
12185       rc = SQLITE_CORRUPT;
12186     }
12187     sqlite3_free(zErr);
12188     free(zQ2);
12189   }
12190   return rc;
12191 }
12192
12193 /*
12194 ** Text of help messages.
12195 **
12196 ** The help text for each individual command begins with a line that starts
12197 ** with ".".  Subsequent lines are supplimental information.
12198 **
12199 ** There must be two or more spaces between the end of the command and the
12200 ** start of the description of what that command does.
12201 */
12202 static const char *(azHelp[]) = {
12203 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
12204   ".archive ...             Manage SQL archives",
12205   "   Each command must have exactly one of the following options:",
12206   "     -c, --create               Create a new archive",
12207   "     -u, --update               Add or update files with changed mtime",
12208   "     -i, --insert               Like -u but always add even if unchanged",
12209   "     -t, --list                 List contents of archive",
12210   "     -x, --extract              Extract files from archive",
12211   "   Optional arguments:",
12212   "     -v, --verbose              Print each filename as it is processed",
12213   "     -f FILE, --file FILE       Use archive FILE (default is current db)",
12214   "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
12215   "     -C DIR, --directory DIR    Read/extract files from directory DIR",
12216   "     -n, --dryrun               Show the SQL that would have occurred",
12217   "   Examples:",
12218   "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
12219   "     .ar -tf ARCHIVE          # List members of ARCHIVE",
12220   "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
12221   "   See also:",
12222   "      http://sqlite.org/cli.html#sqlar_archive_support",
12223 #endif
12224 #ifndef SQLITE_OMIT_AUTHORIZATION
12225   ".auth ON|OFF             Show authorizer callbacks",
12226 #endif
12227   ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
12228   "       --append            Use the appendvfs",
12229   "       --async             Write to FILE without journal and fsync()",
12230   ".bail on|off             Stop after hitting an error.  Default OFF",
12231   ".binary on|off           Turn binary output on or off.  Default OFF",
12232   ".cd DIRECTORY            Change the working directory to DIRECTORY",
12233   ".changes on|off          Show number of rows changed by SQL",
12234   ".check GLOB              Fail if output since .testcase does not match",
12235   ".clone NEWDB             Clone data into NEWDB from the existing database",
12236   ".databases               List names and files of attached databases",
12237   ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
12238   ".dbinfo ?DB?             Show status information about the database",
12239   ".dump ?TABLE?            Render database content as SQL",
12240   "   Options:",
12241   "     --preserve-rowids      Include ROWID values in the output",
12242   "     --newlines             Allow unescaped newline characters in output",
12243   "   TABLE is a LIKE pattern for the tables to dump",
12244   "   Additional LIKE patterns can be given in subsequent arguments",
12245   ".echo on|off             Turn command echo on or off",
12246   ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
12247   "   Other Modes:",
12248 #ifdef SQLITE_DEBUG
12249   "      test                  Show raw EXPLAIN QUERY PLAN output",
12250   "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
12251 #endif
12252   "      trigger               Like \"full\" but also show trigger bytecode",
12253   ".excel                   Display the output of next command in spreadsheet",
12254   "   --bom                   Put a UTF8 byte-order mark on intermediate file",
12255   ".exit ?CODE?             Exit this program with return-code CODE",
12256   ".expert                  EXPERIMENTAL. Suggest indexes for queries",
12257   ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
12258   ".filectrl CMD ...        Run various sqlite3_file_control() operations",
12259   "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
12260   "   --help                  Show CMD details",
12261   ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
12262   ".headers on|off          Turn display of headers on or off",
12263   ".help ?-all? ?PATTERN?   Show help text for PATTERN",
12264   ".import FILE TABLE       Import data from FILE into TABLE",
12265   "   Options:",
12266   "     --ascii               Use \\037 and \\036 as column and row separators",
12267   "     --csv                 Use , and \\n as column and row separators",
12268   "     --skip N              Skip the first N rows of input",
12269   "     -v                    \"Verbose\" - increase auxiliary output",
12270   "   Notes:",
12271   "     *  If TABLE does not exist, it is created.  The first row of input",
12272   "        determines the column names.",
12273   "     *  If neither --csv or --ascii are used, the input mode is derived",
12274   "        from the \".mode\" output mode",
12275   "     *  If FILE begins with \"|\" then it is a command that generates the",
12276   "        input text.",
12277 #ifndef SQLITE_OMIT_TEST_CONTROL
12278   ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
12279 #endif
12280   ".indexes ?TABLE?         Show names of indexes",
12281   "                           If TABLE is specified, only show indexes for",
12282   "                           tables matching TABLE using the LIKE operator.",
12283 #ifdef SQLITE_ENABLE_IOTRACE
12284   ".iotrace FILE            Enable I/O diagnostic logging to FILE",
12285 #endif
12286   ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
12287   ".lint OPTIONS            Report potential schema issues.",
12288   "     Options:",
12289   "        fkey-indexes     Find missing foreign key indexes",
12290 #ifndef SQLITE_OMIT_LOAD_EXTENSION
12291   ".load FILE ?ENTRY?       Load an extension library",
12292 #endif
12293   ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
12294   ".mode MODE ?TABLE?       Set output mode",
12295   "   MODE is one of:",
12296   "     ascii    Columns/rows delimited by 0x1F and 0x1E",
12297   "     csv      Comma-separated values",
12298   "     column   Left-aligned columns.  (See .width)",
12299   "     html     HTML <table> code",
12300   "     insert   SQL insert statements for TABLE",
12301   "     line     One value per line",
12302   "     list     Values delimited by \"|\"",
12303   "     quote    Escape answers as for SQL",
12304   "     tabs     Tab-separated values",
12305   "     tcl      TCL list elements",
12306   ".nullvalue STRING        Use STRING in place of NULL values",
12307   ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
12308   "     If FILE begins with '|' then open as a pipe",
12309   "       --bom  Put a UTF8 byte-order mark at the beginning",
12310   "       -e     Send output to the system text editor",
12311   "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
12312 #ifdef SQLITE_DEBUG
12313   ".oom [--repeat M] [N]    Simulate an OOM error on the N-th allocation",
12314 #endif 
12315   ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
12316   "     Options:",
12317   "        --append        Use appendvfs to append database to the end of FILE",
12318 #ifdef SQLITE_ENABLE_DESERIALIZE
12319   "        --deserialize   Load into memory useing sqlite3_deserialize()",
12320   "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
12321   "        --maxsize N     Maximum size for --hexdb or --deserialized database",
12322 #endif
12323   "        --new           Initialize FILE to an empty database",
12324   "        --nofollow      Do not follow symbolic links",
12325   "        --readonly      Open FILE readonly",
12326   "        --zip           FILE is a ZIP archive",
12327   ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
12328   "   If FILE begins with '|' then open it as a pipe.",
12329   "   Options:",
12330   "     --bom                 Prefix output with a UTF8 byte-order mark",
12331   "     -e                    Send output to the system text editor",
12332   "     -x                    Send output as CSV to a spreadsheet",
12333   ".parameter CMD ...       Manage SQL parameter bindings",
12334   "   clear                   Erase all bindings",
12335   "   init                    Initialize the TEMP table that holds bindings",
12336   "   list                    List the current parameter bindings",
12337   "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
12338   "                           PARAMETER should start with one of: $ : @ ?",
12339   "   unset PARAMETER         Remove PARAMETER from the binding table",
12340   ".print STRING...         Print literal STRING",
12341 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
12342   ".progress N              Invoke progress handler after every N opcodes",
12343   "   --limit N                 Interrupt after N progress callbacks",
12344   "   --once                    Do no more than one progress interrupt",
12345   "   --quiet|-q                No output except at interrupts",
12346   "   --reset                   Reset the count for each input and interrupt",
12347 #endif
12348   ".prompt MAIN CONTINUE    Replace the standard prompts",
12349   ".quit                    Exit this program",
12350   ".read FILE               Read input from FILE",
12351 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
12352   ".recover                 Recover as much data as possible from corrupt db.",
12353   "   --freelist-corrupt       Assume the freelist is corrupt",
12354   "   --recovery-db NAME       Store recovery metadata in database file NAME",
12355   "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
12356   "   --no-rowids              Do not attempt to recover rowid values",
12357   "                            that are not also INTEGER PRIMARY KEYs",
12358 #endif
12359   ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
12360   ".save FILE               Write in-memory database into FILE",
12361   ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
12362   ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
12363   "     Options:",
12364   "         --indent            Try to pretty-print the schema",
12365   ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
12366   "    Options:",
12367   "       --init               Create a new SELFTEST table",
12368   "       -v                   Verbose output",
12369   ".separator COL ?ROW?     Change the column and row separators",
12370 #if defined(SQLITE_ENABLE_SESSION)
12371   ".session ?NAME? CMD ...  Create or control sessions",
12372   "   Subcommands:",
12373   "     attach TABLE             Attach TABLE",
12374   "     changeset FILE           Write a changeset into FILE",
12375   "     close                    Close one session",
12376   "     enable ?BOOLEAN?         Set or query the enable bit",
12377   "     filter GLOB...           Reject tables matching GLOBs",
12378   "     indirect ?BOOLEAN?       Mark or query the indirect status",
12379   "     isempty                  Query whether the session is empty",
12380   "     list                     List currently open session names",
12381   "     open DB NAME             Open a new session on DB",
12382   "     patchset FILE            Write a patchset into FILE",
12383   "   If ?NAME? is omitted, the first defined session is used.",
12384 #endif
12385   ".sha3sum ...             Compute a SHA3 hash of database content",
12386   "    Options:",
12387   "      --schema              Also hash the sqlite_master table",
12388   "      --sha3-224            Use the sha3-224 algorithm",
12389   "      --sha3-256            Use the sha3-256 algorithm (default)",
12390   "      --sha3-384            Use the sha3-384 algorithm",
12391   "      --sha3-512            Use the sha3-512 algorithm",
12392   "    Any other argument is a LIKE pattern for tables to hash",
12393 #ifndef SQLITE_NOHAVE_SYSTEM
12394   ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
12395 #endif
12396   ".show                    Show the current values for various settings",
12397   ".stats ?on|off?          Show stats or turn stats on or off",
12398 #ifndef SQLITE_NOHAVE_SYSTEM
12399   ".system CMD ARGS...      Run CMD ARGS... in a system shell",
12400 #endif
12401   ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
12402   ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
12403   ".testctrl CMD ...        Run various sqlite3_test_control() operations",
12404   "                           Run \".testctrl\" with no arguments for details",
12405   ".timeout MS              Try opening locked tables for MS milliseconds",
12406   ".timer on|off            Turn SQL timer on or off",
12407 #ifndef SQLITE_OMIT_TRACE
12408   ".trace ?OPTIONS?         Output each SQL statement as it is run",
12409   "    FILE                    Send output to FILE",
12410   "    stdout                  Send output to stdout",
12411   "    stderr                  Send output to stderr",
12412   "    off                     Disable tracing",
12413   "    --expanded              Expand query parameters",
12414 #ifdef SQLITE_ENABLE_NORMALIZE
12415   "    --normalized            Normal the SQL statements",
12416 #endif
12417   "    --plain                 Show SQL as it is input",
12418   "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
12419   "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
12420   "    --row                   Trace each row (SQLITE_TRACE_ROW)",
12421   "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
12422 #endif /* SQLITE_OMIT_TRACE */
12423 #ifdef SQLITE_DEBUG
12424   ".unmodule NAME ...       Unregister virtual table modules",
12425   "    --allexcept             Unregister everything except those named",
12426 #endif
12427   ".vfsinfo ?AUX?           Information about the top-level VFS",
12428   ".vfslist                 List all available VFSes",
12429   ".vfsname ?AUX?           Print the name of the VFS stack",
12430   ".width NUM1 NUM2 ...     Set column widths for \"column\" mode",
12431   "     Negative values right-justify",
12432 };
12433
12434 /*
12435 ** Output help text.
12436 **
12437 ** zPattern describes the set of commands for which help text is provided.
12438 ** If zPattern is NULL, then show all commands, but only give a one-line
12439 ** description of each.
12440 **
12441 ** Return the number of matches.
12442 */
12443 static int showHelp(FILE *out, const char *zPattern){
12444   int i = 0;
12445   int j = 0;
12446   int n = 0;
12447   char *zPat;
12448   if( zPattern==0
12449    || zPattern[0]=='0'
12450    || strcmp(zPattern,"-a")==0
12451    || strcmp(zPattern,"-all")==0
12452    || strcmp(zPattern,"--all")==0
12453   ){
12454     /* Show all commands, but only one line per command */
12455     if( zPattern==0 ) zPattern = "";
12456     for(i=0; i<ArraySize(azHelp); i++){
12457       if( azHelp[i][0]=='.' || zPattern[0] ){
12458         utf8_printf(out, "%s\n", azHelp[i]);
12459         n++;
12460       }
12461     }
12462   }else{
12463     /* Look for commands that for which zPattern is an exact prefix */
12464     zPat = sqlite3_mprintf(".%s*", zPattern);
12465     for(i=0; i<ArraySize(azHelp); i++){
12466       if( sqlite3_strglob(zPat, azHelp[i])==0 ){
12467         utf8_printf(out, "%s\n", azHelp[i]);
12468         j = i+1;
12469         n++;
12470       }
12471     }
12472     sqlite3_free(zPat);
12473     if( n ){
12474       if( n==1 ){
12475         /* when zPattern is a prefix of exactly one command, then include the
12476         ** details of that command, which should begin at offset j */
12477         while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
12478           utf8_printf(out, "%s\n", azHelp[j]);
12479           j++;
12480         }
12481       }
12482       return n;
12483     }
12484     /* Look for commands that contain zPattern anywhere.  Show the complete
12485     ** text of all commands that match. */
12486     zPat = sqlite3_mprintf("%%%s%%", zPattern);
12487     for(i=0; i<ArraySize(azHelp); i++){
12488       if( azHelp[i][0]=='.' ) j = i;
12489       if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
12490         utf8_printf(out, "%s\n", azHelp[j]);
12491         while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
12492           j++;
12493           utf8_printf(out, "%s\n", azHelp[j]);
12494         }
12495         i = j;
12496         n++;
12497       }
12498     }
12499     sqlite3_free(zPat);
12500   }
12501   return n;
12502 }
12503
12504 /* Forward reference */
12505 static int process_input(ShellState *p);
12506
12507 /*
12508 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
12509 ** and return a pointer to the buffer. The caller is responsible for freeing
12510 ** the memory.
12511 **
12512 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
12513 ** read.
12514 **
12515 ** For convenience, a nul-terminator byte is always appended to the data read
12516 ** from the file before the buffer is returned. This byte is not included in
12517 ** the final value of (*pnByte), if applicable.
12518 **
12519 ** NULL is returned if any error is encountered. The final value of *pnByte
12520 ** is undefined in this case.
12521 */
12522 static char *readFile(const char *zName, int *pnByte){
12523   FILE *in = fopen(zName, "rb");
12524   long nIn;
12525   size_t nRead;
12526   char *pBuf;
12527   if( in==0 ) return 0;
12528   fseek(in, 0, SEEK_END);
12529   nIn = ftell(in);
12530   rewind(in);
12531   pBuf = sqlite3_malloc64( nIn+1 );
12532   if( pBuf==0 ){ fclose(in); return 0; }
12533   nRead = fread(pBuf, nIn, 1, in);
12534   fclose(in);
12535   if( nRead!=1 ){
12536     sqlite3_free(pBuf);
12537     return 0;
12538   }
12539   pBuf[nIn] = 0;
12540   if( pnByte ) *pnByte = nIn;
12541   return pBuf;
12542 }
12543
12544 #if defined(SQLITE_ENABLE_SESSION)
12545 /*
12546 ** Close a single OpenSession object and release all of its associated
12547 ** resources.
12548 */
12549 static void session_close(OpenSession *pSession){
12550   int i;
12551   sqlite3session_delete(pSession->p);
12552   sqlite3_free(pSession->zName);
12553   for(i=0; i<pSession->nFilter; i++){
12554     sqlite3_free(pSession->azFilter[i]);
12555   }
12556   sqlite3_free(pSession->azFilter);
12557   memset(pSession, 0, sizeof(OpenSession));
12558 }
12559 #endif
12560
12561 /*
12562 ** Close all OpenSession objects and release all associated resources.
12563 */
12564 #if defined(SQLITE_ENABLE_SESSION)
12565 static void session_close_all(ShellState *p){
12566   int i;
12567   for(i=0; i<p->nSession; i++){
12568     session_close(&p->aSession[i]);
12569   }
12570   p->nSession = 0;
12571 }
12572 #else
12573 # define session_close_all(X)
12574 #endif
12575
12576 /*
12577 ** Implementation of the xFilter function for an open session.  Omit
12578 ** any tables named by ".session filter" but let all other table through.
12579 */
12580 #if defined(SQLITE_ENABLE_SESSION)
12581 static int session_filter(void *pCtx, const char *zTab){
12582   OpenSession *pSession = (OpenSession*)pCtx;
12583   int i;
12584   for(i=0; i<pSession->nFilter; i++){
12585     if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
12586   }
12587   return 1;
12588 }
12589 #endif
12590
12591 /*
12592 ** Try to deduce the type of file for zName based on its content.  Return
12593 ** one of the SHELL_OPEN_* constants.
12594 **
12595 ** If the file does not exist or is empty but its name looks like a ZIP
12596 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
12597 ** Otherwise, assume an ordinary database regardless of the filename if
12598 ** the type cannot be determined from content.
12599 */
12600 int deduceDatabaseType(const char *zName, int dfltZip){
12601   FILE *f = fopen(zName, "rb");
12602   size_t n;
12603   int rc = SHELL_OPEN_UNSPEC;
12604   char zBuf[100];
12605   if( f==0 ){
12606     if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
12607        return SHELL_OPEN_ZIPFILE;
12608     }else{
12609        return SHELL_OPEN_NORMAL;
12610     }
12611   }
12612   n = fread(zBuf, 16, 1, f);
12613   if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
12614     fclose(f);
12615     return SHELL_OPEN_NORMAL;
12616   }
12617   fseek(f, -25, SEEK_END);
12618   n = fread(zBuf, 25, 1, f);
12619   if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
12620     rc = SHELL_OPEN_APPENDVFS;
12621   }else{
12622     fseek(f, -22, SEEK_END);
12623     n = fread(zBuf, 22, 1, f);
12624     if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
12625        && zBuf[3]==0x06 ){
12626       rc = SHELL_OPEN_ZIPFILE;
12627     }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
12628       rc = SHELL_OPEN_ZIPFILE;
12629     }
12630   }
12631   fclose(f);
12632   return rc;  
12633 }
12634
12635 #ifdef SQLITE_ENABLE_DESERIALIZE
12636 /*
12637 ** Reconstruct an in-memory database using the output from the "dbtotxt"
12638 ** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
12639 ** is 0, then read from standard input.
12640 */
12641 static unsigned char *readHexDb(ShellState *p, int *pnData){
12642   unsigned char *a = 0;
12643   int nLine;
12644   int n = 0;
12645   int pgsz = 0;
12646   int iOffset = 0;
12647   int j, k;
12648   int rc;
12649   FILE *in;
12650   unsigned int x[16];
12651   char zLine[1000];
12652   if( p->zDbFilename ){
12653     in = fopen(p->zDbFilename, "r");
12654     if( in==0 ){
12655       utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
12656       return 0;
12657     }
12658     nLine = 0;
12659   }else{
12660     in = p->in;
12661     nLine = p->lineno;
12662     if( in==0 ) in = stdin;
12663   }
12664   *pnData = 0;
12665   nLine++;
12666   if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
12667   rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
12668   if( rc!=2 ) goto readHexDb_error;
12669   if( n<0 ) goto readHexDb_error;
12670   if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
12671   n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
12672   a = sqlite3_malloc( n ? n : 1 );
12673   if( a==0 ){
12674     utf8_printf(stderr, "Out of memory!\n");
12675     goto readHexDb_error;
12676   }
12677   memset(a, 0, n);
12678   if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
12679     utf8_printf(stderr, "invalid pagesize\n");
12680     goto readHexDb_error;
12681   }
12682   for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
12683     rc = sscanf(zLine, "| page %d offset %d", &j, &k);
12684     if( rc==2 ){
12685       iOffset = k;
12686       continue;
12687     }
12688     if( strncmp(zLine, "| end ", 6)==0 ){
12689       break;
12690     }
12691     rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
12692                 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
12693                 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
12694     if( rc==17 ){
12695       k = iOffset+j;
12696       if( k+16<=n ){
12697         int ii;
12698         for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
12699       }
12700     }
12701   }
12702   *pnData = n;
12703   if( in!=p->in ){
12704     fclose(in);
12705   }else{
12706     p->lineno = nLine;
12707   }
12708   return a;
12709
12710 readHexDb_error:
12711   if( in!=p->in ){
12712     fclose(in);
12713   }else{
12714     while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
12715       nLine++;
12716       if(strncmp(zLine, "| end ", 6)==0 ) break;
12717     }
12718     p->lineno = nLine;
12719   }
12720   sqlite3_free(a);
12721   utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
12722   return 0;
12723 }
12724 #endif /* SQLITE_ENABLE_DESERIALIZE */
12725
12726 /*
12727 ** Scalar function "shell_int32". The first argument to this function
12728 ** must be a blob. The second a non-negative integer. This function
12729 ** reads and returns a 32-bit big-endian integer from byte
12730 ** offset (4*<arg2>) of the blob.
12731 */
12732 static void shellInt32(
12733   sqlite3_context *context, 
12734   int argc, 
12735   sqlite3_value **argv
12736 ){
12737   const unsigned char *pBlob;
12738   int nBlob;
12739   int iInt;
12740
12741   UNUSED_PARAMETER(argc);
12742   nBlob = sqlite3_value_bytes(argv[0]);
12743   pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
12744   iInt = sqlite3_value_int(argv[1]);
12745
12746   if( iInt>=0 && (iInt+1)*4<=nBlob ){
12747     const unsigned char *a = &pBlob[iInt*4];
12748     sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
12749                        + ((sqlite3_int64)a[1]<<16)
12750                        + ((sqlite3_int64)a[2]<< 8)
12751                        + ((sqlite3_int64)a[3]<< 0);
12752     sqlite3_result_int64(context, iVal);
12753   }
12754 }
12755
12756 /*
12757 ** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
12758 ** using "..." with internal double-quote characters doubled.
12759 */
12760 static void shellIdQuote(
12761   sqlite3_context *context, 
12762   int argc, 
12763   sqlite3_value **argv
12764 ){
12765   const char *zName = (const char*)sqlite3_value_text(argv[0]);
12766   UNUSED_PARAMETER(argc);
12767   if( zName ){
12768     char *z = sqlite3_mprintf("\"%w\"", zName);
12769     sqlite3_result_text(context, z, -1, sqlite3_free);
12770   }
12771 }
12772
12773 /*
12774 ** Scalar function "shell_escape_crnl" used by the .recover command.
12775 ** The argument passed to this function is the output of built-in
12776 ** function quote(). If the first character of the input is "'", 
12777 ** indicating that the value passed to quote() was a text value,
12778 ** then this function searches the input for "\n" and "\r" characters
12779 ** and adds a wrapper similar to the following:
12780 **
12781 **   replace(replace(<input>, '\n', char(10), '\r', char(13));
12782 **
12783 ** Or, if the first character of the input is not "'", then a copy
12784 ** of the input is returned.
12785 */
12786 static void shellEscapeCrnl(
12787   sqlite3_context *context, 
12788   int argc, 
12789   sqlite3_value **argv
12790 ){
12791   const char *zText = (const char*)sqlite3_value_text(argv[0]);
12792   UNUSED_PARAMETER(argc);
12793   if( zText[0]=='\'' ){
12794     int nText = sqlite3_value_bytes(argv[0]);
12795     int i;
12796     char zBuf1[20];
12797     char zBuf2[20];
12798     const char *zNL = 0;
12799     const char *zCR = 0;
12800     int nCR = 0;
12801     int nNL = 0;
12802
12803     for(i=0; zText[i]; i++){
12804       if( zNL==0 && zText[i]=='\n' ){
12805         zNL = unused_string(zText, "\\n", "\\012", zBuf1);
12806         nNL = (int)strlen(zNL);
12807       }
12808       if( zCR==0 && zText[i]=='\r' ){
12809         zCR = unused_string(zText, "\\r", "\\015", zBuf2);
12810         nCR = (int)strlen(zCR);
12811       }
12812     }
12813
12814     if( zNL || zCR ){
12815       int iOut = 0;
12816       i64 nMax = (nNL > nCR) ? nNL : nCR;
12817       i64 nAlloc = nMax * nText + (nMax+64)*2;
12818       char *zOut = (char*)sqlite3_malloc64(nAlloc);
12819       if( zOut==0 ){
12820         sqlite3_result_error_nomem(context);
12821         return;
12822       }
12823
12824       if( zNL && zCR ){
12825         memcpy(&zOut[iOut], "replace(replace(", 16);
12826         iOut += 16;
12827       }else{
12828         memcpy(&zOut[iOut], "replace(", 8);
12829         iOut += 8;
12830       }
12831       for(i=0; zText[i]; i++){
12832         if( zText[i]=='\n' ){
12833           memcpy(&zOut[iOut], zNL, nNL);
12834           iOut += nNL;
12835         }else if( zText[i]=='\r' ){
12836           memcpy(&zOut[iOut], zCR, nCR);
12837           iOut += nCR;
12838         }else{
12839           zOut[iOut] = zText[i];
12840           iOut++;
12841         }
12842       }
12843
12844       if( zNL ){
12845         memcpy(&zOut[iOut], ",'", 2); iOut += 2;
12846         memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
12847         memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
12848       }
12849       if( zCR ){
12850         memcpy(&zOut[iOut], ",'", 2); iOut += 2;
12851         memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
12852         memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
12853       }
12854
12855       sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
12856       sqlite3_free(zOut);
12857       return;
12858     }
12859   }
12860
12861   sqlite3_result_value(context, argv[0]);
12862 }
12863
12864 /* Flags for open_db().
12865 **
12866 ** The default behavior of open_db() is to exit(1) if the database fails to
12867 ** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
12868 ** but still returns without calling exit.
12869 **
12870 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
12871 ** ZIP archive if the file does not exist or is empty and its name matches
12872 ** the *.zip pattern.
12873 */
12874 #define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
12875 #define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
12876
12877 /*
12878 ** Make sure the database is open.  If it is not, then open it.  If
12879 ** the database fails to open, print an error message and exit.
12880 */
12881 static void open_db(ShellState *p, int openFlags){
12882   if( p->db==0 ){
12883     if( p->openMode==SHELL_OPEN_UNSPEC ){
12884       if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
12885         p->openMode = SHELL_OPEN_NORMAL;
12886       }else{
12887         p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 
12888                              (openFlags & OPEN_DB_ZIPFILE)!=0);
12889       }
12890     }
12891     switch( p->openMode ){
12892       case SHELL_OPEN_APPENDVFS: {
12893         sqlite3_open_v2(p->zDbFilename, &p->db, 
12894            SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
12895         break;
12896       }
12897       case SHELL_OPEN_HEXDB:
12898       case SHELL_OPEN_DESERIALIZE: {
12899         sqlite3_open(0, &p->db);
12900         break;
12901       }
12902       case SHELL_OPEN_ZIPFILE: {
12903         sqlite3_open(":memory:", &p->db);
12904         break;
12905       }
12906       case SHELL_OPEN_READONLY: {
12907         sqlite3_open_v2(p->zDbFilename, &p->db,
12908             SQLITE_OPEN_READONLY|p->openFlags, 0);
12909         break;
12910       }
12911       case SHELL_OPEN_UNSPEC:
12912       case SHELL_OPEN_NORMAL: {
12913         sqlite3_open_v2(p->zDbFilename, &p->db,
12914            SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
12915         break;
12916       }
12917     }
12918     globalDb = p->db;
12919     if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
12920       utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
12921           p->zDbFilename, sqlite3_errmsg(p->db));
12922       if( openFlags & OPEN_DB_KEEPALIVE ){
12923         sqlite3_open(":memory:", &p->db);
12924         return;
12925       }
12926       exit(1);
12927     }
12928 #ifndef SQLITE_OMIT_LOAD_EXTENSION
12929     sqlite3_enable_load_extension(p->db, 1);
12930 #endif
12931     sqlite3_fileio_init(p->db, 0, 0);
12932     sqlite3_shathree_init(p->db, 0, 0);
12933     sqlite3_completion_init(p->db, 0, 0);
12934     sqlite3_uint_init(p->db, 0, 0);
12935 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
12936     sqlite3_dbdata_init(p->db, 0, 0);
12937 #endif
12938 #ifdef SQLITE_HAVE_ZLIB
12939     sqlite3_zipfile_init(p->db, 0, 0);
12940     sqlite3_sqlar_init(p->db, 0, 0);
12941 #endif
12942     sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
12943                             shellAddSchemaName, 0, 0);
12944     sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
12945                             shellModuleSchema, 0, 0);
12946     sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
12947                             shellPutsFunc, 0, 0);
12948     sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
12949                             shellEscapeCrnl, 0, 0);
12950     sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
12951                             shellInt32, 0, 0);
12952     sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
12953                             shellIdQuote, 0, 0);
12954 #ifndef SQLITE_NOHAVE_SYSTEM
12955     sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
12956                             editFunc, 0, 0);
12957     sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
12958                             editFunc, 0, 0);
12959 #endif
12960     if( p->openMode==SHELL_OPEN_ZIPFILE ){
12961       char *zSql = sqlite3_mprintf(
12962          "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
12963       sqlite3_exec(p->db, zSql, 0, 0, 0);
12964       sqlite3_free(zSql);
12965     }
12966 #ifdef SQLITE_ENABLE_DESERIALIZE
12967     else
12968     if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
12969       int rc;
12970       int nData = 0;
12971       unsigned char *aData;
12972       if( p->openMode==SHELL_OPEN_DESERIALIZE ){
12973         aData = (unsigned char*)readFile(p->zDbFilename, &nData);
12974       }else{
12975         aData = readHexDb(p, &nData);
12976         if( aData==0 ){
12977           return;
12978         }
12979       }
12980       rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
12981                    SQLITE_DESERIALIZE_RESIZEABLE |
12982                    SQLITE_DESERIALIZE_FREEONCLOSE);
12983       if( rc ){
12984         utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
12985       }
12986       if( p->szMax>0 ){
12987         sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
12988       }
12989     }
12990 #endif
12991   }
12992 }
12993
12994 /*
12995 ** Attempt to close the databaes connection.  Report errors.
12996 */
12997 void close_db(sqlite3 *db){
12998   int rc = sqlite3_close(db);
12999   if( rc ){
13000     utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
13001         rc, sqlite3_errmsg(db));
13002   } 
13003 }
13004
13005 #if HAVE_READLINE || HAVE_EDITLINE
13006 /*
13007 ** Readline completion callbacks
13008 */
13009 static char *readline_completion_generator(const char *text, int state){
13010   static sqlite3_stmt *pStmt = 0;
13011   char *zRet;
13012   if( state==0 ){
13013     char *zSql;
13014     sqlite3_finalize(pStmt);
13015     zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
13016                            "  FROM completion(%Q) ORDER BY 1", text);
13017     sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
13018     sqlite3_free(zSql);
13019   }
13020   if( sqlite3_step(pStmt)==SQLITE_ROW ){
13021     zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
13022   }else{
13023     sqlite3_finalize(pStmt);
13024     pStmt = 0;
13025     zRet = 0;
13026   }
13027   return zRet;
13028 }
13029 static char **readline_completion(const char *zText, int iStart, int iEnd){
13030   rl_attempted_completion_over = 1;
13031   return rl_completion_matches(zText, readline_completion_generator);
13032 }
13033
13034 #elif HAVE_LINENOISE
13035 /*
13036 ** Linenoise completion callback
13037 */
13038 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
13039   int nLine = strlen30(zLine);
13040   int i, iStart;
13041   sqlite3_stmt *pStmt = 0;
13042   char *zSql;
13043   char zBuf[1000];
13044
13045   if( nLine>sizeof(zBuf)-30 ) return;
13046   if( zLine[0]=='.' || zLine[0]=='#') return;
13047   for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
13048   if( i==nLine-1 ) return;
13049   iStart = i+1;
13050   memcpy(zBuf, zLine, iStart);
13051   zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
13052                          "  FROM completion(%Q,%Q) ORDER BY 1",
13053                          &zLine[iStart], zLine);
13054   sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
13055   sqlite3_free(zSql);
13056   sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
13057   while( sqlite3_step(pStmt)==SQLITE_ROW ){
13058     const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
13059     int nCompletion = sqlite3_column_bytes(pStmt, 0);
13060     if( iStart+nCompletion < sizeof(zBuf)-1 ){
13061       memcpy(zBuf+iStart, zCompletion, nCompletion+1);
13062       linenoiseAddCompletion(lc, zBuf);
13063     }
13064   }
13065   sqlite3_finalize(pStmt);
13066 }
13067 #endif
13068
13069 /*
13070 ** Do C-language style dequoting.
13071 **
13072 **    \a    -> alarm
13073 **    \b    -> backspace
13074 **    \t    -> tab
13075 **    \n    -> newline
13076 **    \v    -> vertical tab
13077 **    \f    -> form feed
13078 **    \r    -> carriage return
13079 **    \s    -> space
13080 **    \"    -> "
13081 **    \'    -> '
13082 **    \\    -> backslash
13083 **    \NNN  -> ascii character NNN in octal
13084 */
13085 static void resolve_backslashes(char *z){
13086   int i, j;
13087   char c;
13088   while( *z && *z!='\\' ) z++;
13089   for(i=j=0; (c = z[i])!=0; i++, j++){
13090     if( c=='\\' && z[i+1]!=0 ){
13091       c = z[++i];
13092       if( c=='a' ){
13093         c = '\a';
13094       }else if( c=='b' ){
13095         c = '\b';
13096       }else if( c=='t' ){
13097         c = '\t';
13098       }else if( c=='n' ){
13099         c = '\n';
13100       }else if( c=='v' ){
13101         c = '\v';
13102       }else if( c=='f' ){
13103         c = '\f';
13104       }else if( c=='r' ){
13105         c = '\r';
13106       }else if( c=='"' ){
13107         c = '"';
13108       }else if( c=='\'' ){
13109         c = '\'';
13110       }else if( c=='\\' ){
13111         c = '\\';
13112       }else if( c>='0' && c<='7' ){
13113         c -= '0';
13114         if( z[i+1]>='0' && z[i+1]<='7' ){
13115           i++;
13116           c = (c<<3) + z[i] - '0';
13117           if( z[i+1]>='0' && z[i+1]<='7' ){
13118             i++;
13119             c = (c<<3) + z[i] - '0';
13120           }
13121         }
13122       }
13123     }
13124     z[j] = c;
13125   }
13126   if( j<i ) z[j] = 0;
13127 }
13128
13129 /*
13130 ** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
13131 ** for TRUE and FALSE.  Return the integer value if appropriate.
13132 */
13133 static int booleanValue(const char *zArg){
13134   int i;
13135   if( zArg[0]=='0' && zArg[1]=='x' ){
13136     for(i=2; hexDigitValue(zArg[i])>=0; i++){}
13137   }else{
13138     for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
13139   }
13140   if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
13141   if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
13142     return 1;
13143   }
13144   if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
13145     return 0;
13146   }
13147   utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
13148           zArg);
13149   return 0;
13150 }
13151
13152 /*
13153 ** Set or clear a shell flag according to a boolean value.
13154 */
13155 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
13156   if( booleanValue(zArg) ){
13157     ShellSetFlag(p, mFlag);
13158   }else{
13159     ShellClearFlag(p, mFlag);
13160   }
13161 }
13162
13163 /*
13164 ** Close an output file, assuming it is not stderr or stdout
13165 */
13166 static void output_file_close(FILE *f){
13167   if( f && f!=stdout && f!=stderr ) fclose(f);
13168 }
13169
13170 /*
13171 ** Try to open an output file.   The names "stdout" and "stderr" are
13172 ** recognized and do the right thing.  NULL is returned if the output
13173 ** filename is "off".
13174 */
13175 static FILE *output_file_open(const char *zFile, int bTextMode){
13176   FILE *f;
13177   if( strcmp(zFile,"stdout")==0 ){
13178     f = stdout;
13179   }else if( strcmp(zFile, "stderr")==0 ){
13180     f = stderr;
13181   }else if( strcmp(zFile, "off")==0 ){
13182     f = 0;
13183   }else{
13184     f = fopen(zFile, bTextMode ? "w" : "wb");
13185     if( f==0 ){
13186       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
13187     }
13188   }
13189   return f;
13190 }
13191
13192 #ifndef SQLITE_OMIT_TRACE
13193 /*
13194 ** A routine for handling output from sqlite3_trace().
13195 */
13196 static int sql_trace_callback(
13197   unsigned mType,         /* The trace type */
13198   void *pArg,             /* The ShellState pointer */
13199   void *pP,               /* Usually a pointer to sqlite_stmt */
13200   void *pX                /* Auxiliary output */
13201 ){
13202   ShellState *p = (ShellState*)pArg;
13203   sqlite3_stmt *pStmt;
13204   const char *zSql;
13205   int nSql;
13206   if( p->traceOut==0 ) return 0;
13207   if( mType==SQLITE_TRACE_CLOSE ){
13208     utf8_printf(p->traceOut, "-- closing database connection\n");
13209     return 0;
13210   }
13211   if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
13212     zSql = (const char*)pX;
13213   }else{
13214     pStmt = (sqlite3_stmt*)pP;
13215     switch( p->eTraceType ){
13216       case SHELL_TRACE_EXPANDED: {
13217         zSql = sqlite3_expanded_sql(pStmt);
13218         break;
13219       }
13220 #ifdef SQLITE_ENABLE_NORMALIZE
13221       case SHELL_TRACE_NORMALIZED: {
13222         zSql = sqlite3_normalized_sql(pStmt);
13223         break;
13224       }
13225 #endif
13226       default: {
13227         zSql = sqlite3_sql(pStmt);
13228         break;
13229       }
13230     }
13231   }
13232   if( zSql==0 ) return 0;
13233   nSql = strlen30(zSql);
13234   while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
13235   switch( mType ){
13236     case SQLITE_TRACE_ROW:
13237     case SQLITE_TRACE_STMT: {
13238       utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
13239       break;
13240     }
13241     case SQLITE_TRACE_PROFILE: {
13242       sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
13243       utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
13244       break;
13245     }
13246   }
13247   return 0;
13248 }
13249 #endif
13250
13251 /*
13252 ** A no-op routine that runs with the ".breakpoint" doc-command.  This is
13253 ** a useful spot to set a debugger breakpoint.
13254 */
13255 static void test_breakpoint(void){
13256   static int nCall = 0;
13257   nCall++;
13258 }
13259
13260 /*
13261 ** An object used to read a CSV and other files for import.
13262 */
13263 typedef struct ImportCtx ImportCtx;
13264 struct ImportCtx {
13265   const char *zFile;  /* Name of the input file */
13266   FILE *in;           /* Read the CSV text from this input stream */
13267   char *z;            /* Accumulated text for a field */
13268   int n;              /* Number of bytes in z */
13269   int nAlloc;         /* Space allocated for z[] */
13270   int nLine;          /* Current line number */
13271   int nRow;           /* Number of rows imported */
13272   int nErr;           /* Number of errors encountered */
13273   int bNotFirst;      /* True if one or more bytes already read */
13274   int cTerm;          /* Character that terminated the most recent field */
13275   int cColSep;        /* The column separator character.  (Usually ",") */
13276   int cRowSep;        /* The row separator character.  (Usually "\n") */
13277 };
13278
13279 /* Append a single byte to z[] */
13280 static void import_append_char(ImportCtx *p, int c){
13281   if( p->n+1>=p->nAlloc ){
13282     p->nAlloc += p->nAlloc + 100;
13283     p->z = sqlite3_realloc64(p->z, p->nAlloc);
13284     if( p->z==0 ) shell_out_of_memory();
13285   }
13286   p->z[p->n++] = (char)c;
13287 }
13288
13289 /* Read a single field of CSV text.  Compatible with rfc4180 and extended
13290 ** with the option of having a separator other than ",".
13291 **
13292 **   +  Input comes from p->in.
13293 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
13294 **      from sqlite3_malloc64().
13295 **   +  Use p->cSep as the column separator.  The default is ",".
13296 **   +  Use p->rSep as the row separator.  The default is "\n".
13297 **   +  Keep track of the line number in p->nLine.
13298 **   +  Store the character that terminates the field in p->cTerm.  Store
13299 **      EOF on end-of-file.
13300 **   +  Report syntax errors on stderr
13301 */
13302 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
13303   int c;
13304   int cSep = p->cColSep;
13305   int rSep = p->cRowSep;
13306   p->n = 0;
13307   c = fgetc(p->in);
13308   if( c==EOF || seenInterrupt ){
13309     p->cTerm = EOF;
13310     return 0;
13311   }
13312   if( c=='"' ){
13313     int pc, ppc;
13314     int startLine = p->nLine;
13315     int cQuote = c;
13316     pc = ppc = 0;
13317     while( 1 ){
13318       c = fgetc(p->in);
13319       if( c==rSep ) p->nLine++;
13320       if( c==cQuote ){
13321         if( pc==cQuote ){
13322           pc = 0;
13323           continue;
13324         }
13325       }
13326       if( (c==cSep && pc==cQuote)
13327        || (c==rSep && pc==cQuote)
13328        || (c==rSep && pc=='\r' && ppc==cQuote)
13329        || (c==EOF && pc==cQuote)
13330       ){
13331         do{ p->n--; }while( p->z[p->n]!=cQuote );
13332         p->cTerm = c;
13333         break;
13334       }
13335       if( pc==cQuote && c!='\r' ){
13336         utf8_printf(stderr, "%s:%d: unescaped %c character\n",
13337                 p->zFile, p->nLine, cQuote);
13338       }
13339       if( c==EOF ){
13340         utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
13341                 p->zFile, startLine, cQuote);
13342         p->cTerm = c;
13343         break;
13344       }
13345       import_append_char(p, c);
13346       ppc = pc;
13347       pc = c;
13348     }
13349   }else{
13350     /* If this is the first field being parsed and it begins with the
13351     ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
13352     if( (c&0xff)==0xef && p->bNotFirst==0 ){
13353       import_append_char(p, c);
13354       c = fgetc(p->in);
13355       if( (c&0xff)==0xbb ){
13356         import_append_char(p, c);
13357         c = fgetc(p->in);
13358         if( (c&0xff)==0xbf ){
13359           p->bNotFirst = 1;
13360           p->n = 0;
13361           return csv_read_one_field(p);
13362         }
13363       }
13364     }
13365     while( c!=EOF && c!=cSep && c!=rSep ){
13366       import_append_char(p, c);
13367       c = fgetc(p->in);
13368     }
13369     if( c==rSep ){
13370       p->nLine++;
13371       if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
13372     }
13373     p->cTerm = c;
13374   }
13375   if( p->z ) p->z[p->n] = 0;
13376   p->bNotFirst = 1;
13377   return p->z;
13378 }
13379
13380 /* Read a single field of ASCII delimited text.
13381 **
13382 **   +  Input comes from p->in.
13383 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
13384 **      from sqlite3_malloc64().
13385 **   +  Use p->cSep as the column separator.  The default is "\x1F".
13386 **   +  Use p->rSep as the row separator.  The default is "\x1E".
13387 **   +  Keep track of the row number in p->nLine.
13388 **   +  Store the character that terminates the field in p->cTerm.  Store
13389 **      EOF on end-of-file.
13390 **   +  Report syntax errors on stderr
13391 */
13392 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
13393   int c;
13394   int cSep = p->cColSep;
13395   int rSep = p->cRowSep;
13396   p->n = 0;
13397   c = fgetc(p->in);
13398   if( c==EOF || seenInterrupt ){
13399     p->cTerm = EOF;
13400     return 0;
13401   }
13402   while( c!=EOF && c!=cSep && c!=rSep ){
13403     import_append_char(p, c);
13404     c = fgetc(p->in);
13405   }
13406   if( c==rSep ){
13407     p->nLine++;
13408   }
13409   p->cTerm = c;
13410   if( p->z ) p->z[p->n] = 0;
13411   return p->z;
13412 }
13413
13414 /*
13415 ** Try to transfer data for table zTable.  If an error is seen while
13416 ** moving forward, try to go backwards.  The backwards movement won't
13417 ** work for WITHOUT ROWID tables.
13418 */
13419 static void tryToCloneData(
13420   ShellState *p,
13421   sqlite3 *newDb,
13422   const char *zTable
13423 ){
13424   sqlite3_stmt *pQuery = 0;
13425   sqlite3_stmt *pInsert = 0;
13426   char *zQuery = 0;
13427   char *zInsert = 0;
13428   int rc;
13429   int i, j, n;
13430   int nTable = strlen30(zTable);
13431   int k = 0;
13432   int cnt = 0;
13433   const int spinRate = 10000;
13434
13435   zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
13436   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
13437   if( rc ){
13438     utf8_printf(stderr, "Error %d: %s on [%s]\n",
13439             sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
13440             zQuery);
13441     goto end_data_xfer;
13442   }
13443   n = sqlite3_column_count(pQuery);
13444   zInsert = sqlite3_malloc64(200 + nTable + n*3);
13445   if( zInsert==0 ) shell_out_of_memory();
13446   sqlite3_snprintf(200+nTable,zInsert,
13447                    "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
13448   i = strlen30(zInsert);
13449   for(j=1; j<n; j++){
13450     memcpy(zInsert+i, ",?", 2);
13451     i += 2;
13452   }
13453   memcpy(zInsert+i, ");", 3);
13454   rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
13455   if( rc ){
13456     utf8_printf(stderr, "Error %d: %s on [%s]\n",
13457             sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
13458             zQuery);
13459     goto end_data_xfer;
13460   }
13461   for(k=0; k<2; k++){
13462     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
13463       for(i=0; i<n; i++){
13464         switch( sqlite3_column_type(pQuery, i) ){
13465           case SQLITE_NULL: {
13466             sqlite3_bind_null(pInsert, i+1);
13467             break;
13468           }
13469           case SQLITE_INTEGER: {
13470             sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
13471             break;
13472           }
13473           case SQLITE_FLOAT: {
13474             sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
13475             break;
13476           }
13477           case SQLITE_TEXT: {
13478             sqlite3_bind_text(pInsert, i+1,
13479                              (const char*)sqlite3_column_text(pQuery,i),
13480                              -1, SQLITE_STATIC);
13481             break;
13482           }
13483           case SQLITE_BLOB: {
13484             sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
13485                                             sqlite3_column_bytes(pQuery,i),
13486                                             SQLITE_STATIC);
13487             break;
13488           }
13489         }
13490       } /* End for */
13491       rc = sqlite3_step(pInsert);
13492       if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
13493         utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
13494                         sqlite3_errmsg(newDb));
13495       }
13496       sqlite3_reset(pInsert);
13497       cnt++;
13498       if( (cnt%spinRate)==0 ){
13499         printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
13500         fflush(stdout);
13501       }
13502     } /* End while */
13503     if( rc==SQLITE_DONE ) break;
13504     sqlite3_finalize(pQuery);
13505     sqlite3_free(zQuery);
13506     zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
13507                              zTable);
13508     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
13509     if( rc ){
13510       utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
13511       break;
13512     }
13513   } /* End for(k=0...) */
13514
13515 end_data_xfer:
13516   sqlite3_finalize(pQuery);
13517   sqlite3_finalize(pInsert);
13518   sqlite3_free(zQuery);
13519   sqlite3_free(zInsert);
13520 }
13521
13522
13523 /*
13524 ** Try to transfer all rows of the schema that match zWhere.  For
13525 ** each row, invoke xForEach() on the object defined by that row.
13526 ** If an error is encountered while moving forward through the
13527 ** sqlite_master table, try again moving backwards.
13528 */
13529 static void tryToCloneSchema(
13530   ShellState *p,
13531   sqlite3 *newDb,
13532   const char *zWhere,
13533   void (*xForEach)(ShellState*,sqlite3*,const char*)
13534 ){
13535   sqlite3_stmt *pQuery = 0;
13536   char *zQuery = 0;
13537   int rc;
13538   const unsigned char *zName;
13539   const unsigned char *zSql;
13540   char *zErrMsg = 0;
13541
13542   zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
13543                            " WHERE %s", zWhere);
13544   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
13545   if( rc ){
13546     utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
13547                     sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
13548                     zQuery);
13549     goto end_schema_xfer;
13550   }
13551   while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
13552     zName = sqlite3_column_text(pQuery, 0);
13553     zSql = sqlite3_column_text(pQuery, 1);
13554     printf("%s... ", zName); fflush(stdout);
13555     sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
13556     if( zErrMsg ){
13557       utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
13558       sqlite3_free(zErrMsg);
13559       zErrMsg = 0;
13560     }
13561     if( xForEach ){
13562       xForEach(p, newDb, (const char*)zName);
13563     }
13564     printf("done\n");
13565   }
13566   if( rc!=SQLITE_DONE ){
13567     sqlite3_finalize(pQuery);
13568     sqlite3_free(zQuery);
13569     zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
13570                              " WHERE %s ORDER BY rowid DESC", zWhere);
13571     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
13572     if( rc ){
13573       utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
13574                       sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
13575                       zQuery);
13576       goto end_schema_xfer;
13577     }
13578     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
13579       zName = sqlite3_column_text(pQuery, 0);
13580       zSql = sqlite3_column_text(pQuery, 1);
13581       printf("%s... ", zName); fflush(stdout);
13582       sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
13583       if( zErrMsg ){
13584         utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
13585         sqlite3_free(zErrMsg);
13586         zErrMsg = 0;
13587       }
13588       if( xForEach ){
13589         xForEach(p, newDb, (const char*)zName);
13590       }
13591       printf("done\n");
13592     }
13593   }
13594 end_schema_xfer:
13595   sqlite3_finalize(pQuery);
13596   sqlite3_free(zQuery);
13597 }
13598
13599 /*
13600 ** Open a new database file named "zNewDb".  Try to recover as much information
13601 ** as possible out of the main database (which might be corrupt) and write it
13602 ** into zNewDb.
13603 */
13604 static void tryToClone(ShellState *p, const char *zNewDb){
13605   int rc;
13606   sqlite3 *newDb = 0;
13607   if( access(zNewDb,0)==0 ){
13608     utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
13609     return;
13610   }
13611   rc = sqlite3_open(zNewDb, &newDb);
13612   if( rc ){
13613     utf8_printf(stderr, "Cannot create output database: %s\n",
13614             sqlite3_errmsg(newDb));
13615   }else{
13616     sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
13617     sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
13618     tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
13619     tryToCloneSchema(p, newDb, "type!='table'", 0);
13620     sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
13621     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
13622   }
13623   close_db(newDb);
13624 }
13625
13626 /*
13627 ** Change the output file back to stdout.
13628 **
13629 ** If the p->doXdgOpen flag is set, that means the output was being
13630 ** redirected to a temporary file named by p->zTempFile.  In that case,
13631 ** launch start/open/xdg-open on that temporary file.
13632 */
13633 static void output_reset(ShellState *p){
13634   if( p->outfile[0]=='|' ){
13635 #ifndef SQLITE_OMIT_POPEN
13636     pclose(p->out);
13637 #endif
13638   }else{
13639     output_file_close(p->out);
13640 #ifndef SQLITE_NOHAVE_SYSTEM
13641     if( p->doXdgOpen ){
13642       const char *zXdgOpenCmd =
13643 #if defined(_WIN32)
13644       "start";
13645 #elif defined(__APPLE__)
13646       "open";
13647 #else
13648       "xdg-open";
13649 #endif
13650       char *zCmd;
13651       zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
13652       if( system(zCmd) ){
13653         utf8_printf(stderr, "Failed: [%s]\n", zCmd);
13654       }else{
13655         /* Give the start/open/xdg-open command some time to get
13656         ** going before we continue, and potential delete the
13657         ** p->zTempFile data file out from under it */
13658         sqlite3_sleep(2000);
13659       }
13660       sqlite3_free(zCmd);
13661       outputModePop(p);
13662       p->doXdgOpen = 0;
13663     }
13664 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
13665   }
13666   p->outfile[0] = 0;
13667   p->out = stdout;
13668 }
13669
13670 /*
13671 ** Run an SQL command and return the single integer result.
13672 */
13673 static int db_int(ShellState *p, const char *zSql){
13674   sqlite3_stmt *pStmt;
13675   int res = 0;
13676   sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13677   if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
13678     res = sqlite3_column_int(pStmt,0);
13679   }
13680   sqlite3_finalize(pStmt);
13681   return res;
13682 }
13683
13684 /*
13685 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
13686 */
13687 static unsigned int get2byteInt(unsigned char *a){
13688   return (a[0]<<8) + a[1];
13689 }
13690 static unsigned int get4byteInt(unsigned char *a){
13691   return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
13692 }
13693
13694 /*
13695 ** Implementation of the ".dbinfo" command.
13696 **
13697 ** Return 1 on error, 2 to exit, and 0 otherwise.
13698 */
13699 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
13700   static const struct { const char *zName; int ofst; } aField[] = {
13701      { "file change counter:",  24  },
13702      { "database page count:",  28  },
13703      { "freelist page count:",  36  },
13704      { "schema cookie:",        40  },
13705      { "schema format:",        44  },
13706      { "default cache size:",   48  },
13707      { "autovacuum top root:",  52  },
13708      { "incremental vacuum:",   64  },
13709      { "text encoding:",        56  },
13710      { "user version:",         60  },
13711      { "application id:",       68  },
13712      { "software version:",     96  },
13713   };
13714   static const struct { const char *zName; const char *zSql; } aQuery[] = {
13715      { "number of tables:",
13716        "SELECT count(*) FROM %s WHERE type='table'" },
13717      { "number of indexes:",
13718        "SELECT count(*) FROM %s WHERE type='index'" },
13719      { "number of triggers:",
13720        "SELECT count(*) FROM %s WHERE type='trigger'" },
13721      { "number of views:",
13722        "SELECT count(*) FROM %s WHERE type='view'" },
13723      { "schema size:",
13724        "SELECT total(length(sql)) FROM %s" },
13725   };
13726   int i, rc;
13727   unsigned iDataVersion;
13728   char *zSchemaTab;
13729   char *zDb = nArg>=2 ? azArg[1] : "main";
13730   sqlite3_stmt *pStmt = 0;
13731   unsigned char aHdr[100];
13732   open_db(p, 0);
13733   if( p->db==0 ) return 1;
13734   rc = sqlite3_prepare_v2(p->db,
13735              "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
13736              -1, &pStmt, 0);
13737   if( rc ){
13738     utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
13739     sqlite3_finalize(pStmt);
13740     return 1;
13741   }
13742   sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
13743   if( sqlite3_step(pStmt)==SQLITE_ROW
13744    && sqlite3_column_bytes(pStmt,0)>100
13745   ){
13746     memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
13747     sqlite3_finalize(pStmt);
13748   }else{
13749     raw_printf(stderr, "unable to read database header\n");
13750     sqlite3_finalize(pStmt);
13751     return 1;
13752   }
13753   i = get2byteInt(aHdr+16);
13754   if( i==1 ) i = 65536;
13755   utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
13756   utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
13757   utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
13758   utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
13759   for(i=0; i<ArraySize(aField); i++){
13760     int ofst = aField[i].ofst;
13761     unsigned int val = get4byteInt(aHdr + ofst);
13762     utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
13763     switch( ofst ){
13764       case 56: {
13765         if( val==1 ) raw_printf(p->out, " (utf8)");
13766         if( val==2 ) raw_printf(p->out, " (utf16le)");
13767         if( val==3 ) raw_printf(p->out, " (utf16be)");
13768       }
13769     }
13770     raw_printf(p->out, "\n");
13771   }
13772   if( zDb==0 ){
13773     zSchemaTab = sqlite3_mprintf("main.sqlite_master");
13774   }else if( strcmp(zDb,"temp")==0 ){
13775     zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
13776   }else{
13777     zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
13778   }
13779   for(i=0; i<ArraySize(aQuery); i++){
13780     char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
13781     int val = db_int(p, zSql);
13782     sqlite3_free(zSql);
13783     utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
13784   }
13785   sqlite3_free(zSchemaTab);
13786   sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
13787   utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
13788   return 0;
13789 }
13790
13791 /*
13792 ** Print the current sqlite3_errmsg() value to stderr and return 1.
13793 */
13794 static int shellDatabaseError(sqlite3 *db){
13795   const char *zErr = sqlite3_errmsg(db);
13796   utf8_printf(stderr, "Error: %s\n", zErr);
13797   return 1;
13798 }
13799
13800 /*
13801 ** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
13802 ** if they match and FALSE (0) if they do not match.
13803 **
13804 ** Globbing rules:
13805 **
13806 **      '*'       Matches any sequence of zero or more characters.
13807 **
13808 **      '?'       Matches exactly one character.
13809 **
13810 **     [...]      Matches one character from the enclosed list of
13811 **                characters.
13812 **
13813 **     [^...]     Matches one character not in the enclosed list.
13814 **
13815 **      '#'       Matches any sequence of one or more digits with an
13816 **                optional + or - sign in front
13817 **
13818 **      ' '       Any span of whitespace matches any other span of
13819 **                whitespace.
13820 **
13821 ** Extra whitespace at the end of z[] is ignored.
13822 */
13823 static int testcase_glob(const char *zGlob, const char *z){
13824   int c, c2;
13825   int invert;
13826   int seen;
13827
13828   while( (c = (*(zGlob++)))!=0 ){
13829     if( IsSpace(c) ){
13830       if( !IsSpace(*z) ) return 0;
13831       while( IsSpace(*zGlob) ) zGlob++;
13832       while( IsSpace(*z) ) z++;
13833     }else if( c=='*' ){
13834       while( (c=(*(zGlob++))) == '*' || c=='?' ){
13835         if( c=='?' && (*(z++))==0 ) return 0;
13836       }
13837       if( c==0 ){
13838         return 1;
13839       }else if( c=='[' ){
13840         while( *z && testcase_glob(zGlob-1,z)==0 ){
13841           z++;
13842         }
13843         return (*z)!=0;
13844       }
13845       while( (c2 = (*(z++)))!=0 ){
13846         while( c2!=c ){
13847           c2 = *(z++);
13848           if( c2==0 ) return 0;
13849         }
13850         if( testcase_glob(zGlob,z) ) return 1;
13851       }
13852       return 0;
13853     }else if( c=='?' ){
13854       if( (*(z++))==0 ) return 0;
13855     }else if( c=='[' ){
13856       int prior_c = 0;
13857       seen = 0;
13858       invert = 0;
13859       c = *(z++);
13860       if( c==0 ) return 0;
13861       c2 = *(zGlob++);
13862       if( c2=='^' ){
13863         invert = 1;
13864         c2 = *(zGlob++);
13865       }
13866       if( c2==']' ){
13867         if( c==']' ) seen = 1;
13868         c2 = *(zGlob++);
13869       }
13870       while( c2 && c2!=']' ){
13871         if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
13872           c2 = *(zGlob++);
13873           if( c>=prior_c && c<=c2 ) seen = 1;
13874           prior_c = 0;
13875         }else{
13876           if( c==c2 ){
13877             seen = 1;
13878           }
13879           prior_c = c2;
13880         }
13881         c2 = *(zGlob++);
13882       }
13883       if( c2==0 || (seen ^ invert)==0 ) return 0;
13884     }else if( c=='#' ){
13885       if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
13886       if( !IsDigit(z[0]) ) return 0;
13887       z++;
13888       while( IsDigit(z[0]) ){ z++; }
13889     }else{
13890       if( c!=(*(z++)) ) return 0;
13891     }
13892   }
13893   while( IsSpace(*z) ){ z++; }
13894   return *z==0;
13895 }
13896
13897
13898 /*
13899 ** Compare the string as a command-line option with either one or two
13900 ** initial "-" characters.
13901 */
13902 static int optionMatch(const char *zStr, const char *zOpt){
13903   if( zStr[0]!='-' ) return 0;
13904   zStr++;
13905   if( zStr[0]=='-' ) zStr++;
13906   return strcmp(zStr, zOpt)==0;
13907 }
13908
13909 /*
13910 ** Delete a file.
13911 */
13912 int shellDeleteFile(const char *zFilename){
13913   int rc;
13914 #ifdef _WIN32
13915   wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
13916   rc = _wunlink(z);
13917   sqlite3_free(z);
13918 #else
13919   rc = unlink(zFilename);
13920 #endif
13921   return rc;
13922 }
13923
13924 /*
13925 ** Try to delete the temporary file (if there is one) and free the
13926 ** memory used to hold the name of the temp file.
13927 */
13928 static void clearTempFile(ShellState *p){
13929   if( p->zTempFile==0 ) return;
13930   if( p->doXdgOpen ) return;
13931   if( shellDeleteFile(p->zTempFile) ) return;
13932   sqlite3_free(p->zTempFile);
13933   p->zTempFile = 0;
13934 }
13935
13936 /*
13937 ** Create a new temp file name with the given suffix.
13938 */
13939 static void newTempFile(ShellState *p, const char *zSuffix){
13940   clearTempFile(p);
13941   sqlite3_free(p->zTempFile);
13942   p->zTempFile = 0;
13943   if( p->db ){
13944     sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
13945   }
13946   if( p->zTempFile==0 ){
13947     /* If p->db is an in-memory database then the TEMPFILENAME file-control
13948     ** will not work and we will need to fallback to guessing */
13949     char *zTemp;
13950     sqlite3_uint64 r;
13951     sqlite3_randomness(sizeof(r), &r);
13952     zTemp = getenv("TEMP");
13953     if( zTemp==0 ) zTemp = getenv("TMP");
13954     if( zTemp==0 ){
13955 #ifdef _WIN32
13956       zTemp = "\\tmp";
13957 #else
13958       zTemp = "/tmp";
13959 #endif
13960     }
13961     p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
13962   }else{
13963     p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
13964   }
13965   if( p->zTempFile==0 ){
13966     raw_printf(stderr, "out of memory\n");
13967     exit(1);
13968   }
13969 }
13970
13971
13972 /*
13973 ** The implementation of SQL scalar function fkey_collate_clause(), used
13974 ** by the ".lint fkey-indexes" command. This scalar function is always
13975 ** called with four arguments - the parent table name, the parent column name,
13976 ** the child table name and the child column name.
13977 **
13978 **   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
13979 **
13980 ** If either of the named tables or columns do not exist, this function
13981 ** returns an empty string. An empty string is also returned if both tables
13982 ** and columns exist but have the same default collation sequence. Or,
13983 ** if both exist but the default collation sequences are different, this
13984 ** function returns the string " COLLATE <parent-collation>", where
13985 ** <parent-collation> is the default collation sequence of the parent column.
13986 */
13987 static void shellFkeyCollateClause(
13988   sqlite3_context *pCtx,
13989   int nVal,
13990   sqlite3_value **apVal
13991 ){
13992   sqlite3 *db = sqlite3_context_db_handle(pCtx);
13993   const char *zParent;
13994   const char *zParentCol;
13995   const char *zParentSeq;
13996   const char *zChild;
13997   const char *zChildCol;
13998   const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
13999   int rc;
14000
14001   assert( nVal==4 );
14002   zParent = (const char*)sqlite3_value_text(apVal[0]);
14003   zParentCol = (const char*)sqlite3_value_text(apVal[1]);
14004   zChild = (const char*)sqlite3_value_text(apVal[2]);
14005   zChildCol = (const char*)sqlite3_value_text(apVal[3]);
14006
14007   sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
14008   rc = sqlite3_table_column_metadata(
14009       db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
14010   );
14011   if( rc==SQLITE_OK ){
14012     rc = sqlite3_table_column_metadata(
14013         db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
14014     );
14015   }
14016
14017   if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
14018     char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
14019     sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
14020     sqlite3_free(z);
14021   }
14022 }
14023
14024
14025 /*
14026 ** The implementation of dot-command ".lint fkey-indexes".
14027 */
14028 static int lintFkeyIndexes(
14029   ShellState *pState,             /* Current shell tool state */
14030   char **azArg,                   /* Array of arguments passed to dot command */
14031   int nArg                        /* Number of entries in azArg[] */
14032 ){
14033   sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
14034   FILE *out = pState->out;        /* Stream to write non-error output to */
14035   int bVerbose = 0;               /* If -verbose is present */
14036   int bGroupByParent = 0;         /* If -groupbyparent is present */
14037   int i;                          /* To iterate through azArg[] */
14038   const char *zIndent = "";       /* How much to indent CREATE INDEX by */
14039   int rc;                         /* Return code */
14040   sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
14041
14042   /*
14043   ** This SELECT statement returns one row for each foreign key constraint
14044   ** in the schema of the main database. The column values are:
14045   **
14046   ** 0. The text of an SQL statement similar to:
14047   **
14048   **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
14049   **
14050   **    This SELECT is similar to the one that the foreign keys implementation
14051   **    needs to run internally on child tables. If there is an index that can
14052   **    be used to optimize this query, then it can also be used by the FK
14053   **    implementation to optimize DELETE or UPDATE statements on the parent
14054   **    table.
14055   **
14056   ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
14057   **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
14058   **    contains an index that can be used to optimize the query.
14059   **
14060   ** 2. Human readable text that describes the child table and columns. e.g.
14061   **
14062   **       "child_table(child_key1, child_key2)"
14063   **
14064   ** 3. Human readable text that describes the parent table and columns. e.g.
14065   **
14066   **       "parent_table(parent_key1, parent_key2)"
14067   **
14068   ** 4. A full CREATE INDEX statement for an index that could be used to
14069   **    optimize DELETE or UPDATE statements on the parent table. e.g.
14070   **
14071   **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
14072   **
14073   ** 5. The name of the parent table.
14074   **
14075   ** These six values are used by the C logic below to generate the report.
14076   */
14077   const char *zSql =
14078   "SELECT "
14079     "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
14080     "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
14081     "  || fkey_collate_clause("
14082     "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
14083     ", "
14084     "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
14085     "  || group_concat('*=?', ' AND ') || ')'"
14086     ", "
14087     "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
14088     ", "
14089     "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
14090     ", "
14091     "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
14092     "  || ' ON ' || quote(s.name) || '('"
14093     "  || group_concat(quote(f.[from]) ||"
14094     "        fkey_collate_clause("
14095     "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
14096     "  || ');'"
14097     ", "
14098     "     f.[table] "
14099     "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
14100     "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
14101     "GROUP BY s.name, f.id "
14102     "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
14103   ;
14104   const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
14105
14106   for(i=2; i<nArg; i++){
14107     int n = strlen30(azArg[i]);
14108     if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
14109       bVerbose = 1;
14110     }
14111     else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
14112       bGroupByParent = 1;
14113       zIndent = "    ";
14114     }
14115     else{
14116       raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
14117           azArg[0], azArg[1]
14118       );
14119       return SQLITE_ERROR;
14120     }
14121   }
14122
14123   /* Register the fkey_collate_clause() SQL function */
14124   rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
14125       0, shellFkeyCollateClause, 0, 0
14126   );
14127
14128
14129   if( rc==SQLITE_OK ){
14130     rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
14131   }
14132   if( rc==SQLITE_OK ){
14133     sqlite3_bind_int(pSql, 1, bGroupByParent);
14134   }
14135
14136   if( rc==SQLITE_OK ){
14137     int rc2;
14138     char *zPrev = 0;
14139     while( SQLITE_ROW==sqlite3_step(pSql) ){
14140       int res = -1;
14141       sqlite3_stmt *pExplain = 0;
14142       const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
14143       const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
14144       const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
14145       const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
14146       const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
14147       const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
14148
14149       rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
14150       if( rc!=SQLITE_OK ) break;
14151       if( SQLITE_ROW==sqlite3_step(pExplain) ){
14152         const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
14153         res = (
14154               0==sqlite3_strglob(zGlob, zPlan)
14155            || 0==sqlite3_strglob(zGlobIPK, zPlan)
14156         );
14157       }
14158       rc = sqlite3_finalize(pExplain);
14159       if( rc!=SQLITE_OK ) break;
14160
14161       if( res<0 ){
14162         raw_printf(stderr, "Error: internal error");
14163         break;
14164       }else{
14165         if( bGroupByParent
14166         && (bVerbose || res==0)
14167         && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
14168         ){
14169           raw_printf(out, "-- Parent table %s\n", zParent);
14170           sqlite3_free(zPrev);
14171           zPrev = sqlite3_mprintf("%s", zParent);
14172         }
14173
14174         if( res==0 ){
14175           raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
14176         }else if( bVerbose ){
14177           raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
14178               zIndent, zFrom, zTarget
14179           );
14180         }
14181       }
14182     }
14183     sqlite3_free(zPrev);
14184
14185     if( rc!=SQLITE_OK ){
14186       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
14187     }
14188
14189     rc2 = sqlite3_finalize(pSql);
14190     if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
14191       rc = rc2;
14192       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
14193     }
14194   }else{
14195     raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
14196   }
14197
14198   return rc;
14199 }
14200
14201 /*
14202 ** Implementation of ".lint" dot command.
14203 */
14204 static int lintDotCommand(
14205   ShellState *pState,             /* Current shell tool state */
14206   char **azArg,                   /* Array of arguments passed to dot command */
14207   int nArg                        /* Number of entries in azArg[] */
14208 ){
14209   int n;
14210   n = (nArg>=2 ? strlen30(azArg[1]) : 0);
14211   if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
14212   return lintFkeyIndexes(pState, azArg, nArg);
14213
14214  usage:
14215   raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
14216   raw_printf(stderr, "Where sub-commands are:\n");
14217   raw_printf(stderr, "    fkey-indexes\n");
14218   return SQLITE_ERROR;
14219 }
14220
14221 #if !defined SQLITE_OMIT_VIRTUALTABLE
14222 static void shellPrepare(
14223   sqlite3 *db, 
14224   int *pRc, 
14225   const char *zSql, 
14226   sqlite3_stmt **ppStmt
14227 ){
14228   *ppStmt = 0;
14229   if( *pRc==SQLITE_OK ){
14230     int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
14231     if( rc!=SQLITE_OK ){
14232       raw_printf(stderr, "sql error: %s (%d)\n", 
14233           sqlite3_errmsg(db), sqlite3_errcode(db)
14234       );
14235       *pRc = rc;
14236     }
14237   }
14238 }
14239
14240 /*
14241 ** Create a prepared statement using printf-style arguments for the SQL.
14242 **
14243 ** This routine is could be marked "static".  But it is not always used,
14244 ** depending on compile-time options.  By omitting the "static", we avoid
14245 ** nuisance compiler warnings about "defined but not used".
14246 */
14247 void shellPreparePrintf(
14248   sqlite3 *db, 
14249   int *pRc, 
14250   sqlite3_stmt **ppStmt,
14251   const char *zFmt, 
14252   ...
14253 ){
14254   *ppStmt = 0;
14255   if( *pRc==SQLITE_OK ){
14256     va_list ap;
14257     char *z;
14258     va_start(ap, zFmt);
14259     z = sqlite3_vmprintf(zFmt, ap);
14260     va_end(ap);
14261     if( z==0 ){
14262       *pRc = SQLITE_NOMEM;
14263     }else{
14264       shellPrepare(db, pRc, z, ppStmt);
14265       sqlite3_free(z);
14266     }
14267   }
14268 }
14269
14270 /* Finalize the prepared statement created using shellPreparePrintf().
14271 **
14272 ** This routine is could be marked "static".  But it is not always used,
14273 ** depending on compile-time options.  By omitting the "static", we avoid
14274 ** nuisance compiler warnings about "defined but not used".
14275 */
14276 void shellFinalize(
14277   int *pRc, 
14278   sqlite3_stmt *pStmt
14279 ){
14280   if( pStmt ){
14281     sqlite3 *db = sqlite3_db_handle(pStmt);
14282     int rc = sqlite3_finalize(pStmt);
14283     if( *pRc==SQLITE_OK ){
14284       if( rc!=SQLITE_OK ){
14285         raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
14286       }
14287       *pRc = rc;
14288     }
14289   }
14290 }
14291
14292 /* Reset the prepared statement created using shellPreparePrintf().
14293 **
14294 ** This routine is could be marked "static".  But it is not always used,
14295 ** depending on compile-time options.  By omitting the "static", we avoid
14296 ** nuisance compiler warnings about "defined but not used".
14297 */
14298 void shellReset(
14299   int *pRc, 
14300   sqlite3_stmt *pStmt
14301 ){
14302   int rc = sqlite3_reset(pStmt);
14303   if( *pRc==SQLITE_OK ){
14304     if( rc!=SQLITE_OK ){
14305       sqlite3 *db = sqlite3_db_handle(pStmt);
14306       raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
14307     }
14308     *pRc = rc;
14309   }
14310 }
14311 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
14312
14313 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
14314 /******************************************************************************
14315 ** The ".archive" or ".ar" command.
14316 */
14317 /*
14318 ** Structure representing a single ".ar" command.
14319 */
14320 typedef struct ArCommand ArCommand;
14321 struct ArCommand {
14322   u8 eCmd;                        /* An AR_CMD_* value */
14323   u8 bVerbose;                    /* True if --verbose */
14324   u8 bZip;                        /* True if the archive is a ZIP */
14325   u8 bDryRun;                     /* True if --dry-run */
14326   u8 bAppend;                     /* True if --append */
14327   u8 fromCmdLine;                 /* Run from -A instead of .archive */
14328   int nArg;                       /* Number of command arguments */
14329   char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
14330   const char *zFile;              /* --file argument, or NULL */
14331   const char *zDir;               /* --directory argument, or NULL */
14332   char **azArg;                   /* Array of command arguments */
14333   ShellState *p;                  /* Shell state */
14334   sqlite3 *db;                    /* Database containing the archive */
14335 };
14336
14337 /*
14338 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
14339 */
14340 static int arUsage(FILE *f){
14341   showHelp(f,"archive");
14342   return SQLITE_ERROR;
14343 }
14344
14345 /*
14346 ** Print an error message for the .ar command to stderr and return 
14347 ** SQLITE_ERROR.
14348 */
14349 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
14350   va_list ap;
14351   char *z;
14352   va_start(ap, zFmt);
14353   z = sqlite3_vmprintf(zFmt, ap);
14354   va_end(ap);
14355   utf8_printf(stderr, "Error: %s\n", z);
14356   if( pAr->fromCmdLine ){
14357     utf8_printf(stderr, "Use \"-A\" for more help\n");
14358   }else{
14359     utf8_printf(stderr, "Use \".archive --help\" for more help\n");
14360   }
14361   sqlite3_free(z);
14362   return SQLITE_ERROR;
14363 }
14364
14365 /*
14366 ** Values for ArCommand.eCmd.
14367 */
14368 #define AR_CMD_CREATE       1
14369 #define AR_CMD_UPDATE       2
14370 #define AR_CMD_INSERT       3
14371 #define AR_CMD_EXTRACT      4
14372 #define AR_CMD_LIST         5
14373 #define AR_CMD_HELP         6
14374
14375 /*
14376 ** Other (non-command) switches.
14377 */
14378 #define AR_SWITCH_VERBOSE     7
14379 #define AR_SWITCH_FILE        8
14380 #define AR_SWITCH_DIRECTORY   9
14381 #define AR_SWITCH_APPEND     10
14382 #define AR_SWITCH_DRYRUN     11
14383
14384 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
14385   switch( eSwitch ){
14386     case AR_CMD_CREATE:
14387     case AR_CMD_EXTRACT:
14388     case AR_CMD_LIST:
14389     case AR_CMD_UPDATE:
14390     case AR_CMD_INSERT:
14391     case AR_CMD_HELP:
14392       if( pAr->eCmd ){
14393         return arErrorMsg(pAr, "multiple command options");
14394       }
14395       pAr->eCmd = eSwitch;
14396       break;
14397
14398     case AR_SWITCH_DRYRUN:
14399       pAr->bDryRun = 1;
14400       break;
14401     case AR_SWITCH_VERBOSE:
14402       pAr->bVerbose = 1;
14403       break;
14404     case AR_SWITCH_APPEND:
14405       pAr->bAppend = 1;
14406       /* Fall thru into --file */
14407     case AR_SWITCH_FILE:
14408       pAr->zFile = zArg;
14409       break;
14410     case AR_SWITCH_DIRECTORY:
14411       pAr->zDir = zArg;
14412       break;
14413   }
14414
14415   return SQLITE_OK;
14416 }
14417
14418 /*
14419 ** Parse the command line for an ".ar" command. The results are written into
14420 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
14421 ** successfully, otherwise an error message is written to stderr and 
14422 ** SQLITE_ERROR returned.
14423 */
14424 static int arParseCommand(
14425   char **azArg,                   /* Array of arguments passed to dot command */
14426   int nArg,                       /* Number of entries in azArg[] */
14427   ArCommand *pAr                  /* Populate this object */
14428 ){
14429   struct ArSwitch {
14430     const char *zLong;
14431     char cShort;
14432     u8 eSwitch;
14433     u8 bArg;
14434   } aSwitch[] = {
14435     { "create",    'c', AR_CMD_CREATE,       0 },
14436     { "extract",   'x', AR_CMD_EXTRACT,      0 },
14437     { "insert",    'i', AR_CMD_INSERT,       0 },
14438     { "list",      't', AR_CMD_LIST,         0 },
14439     { "update",    'u', AR_CMD_UPDATE,       0 },
14440     { "help",      'h', AR_CMD_HELP,         0 },
14441     { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
14442     { "file",      'f', AR_SWITCH_FILE,      1 },
14443     { "append",    'a', AR_SWITCH_APPEND,    1 },
14444     { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
14445     { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
14446   };
14447   int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
14448   struct ArSwitch *pEnd = &aSwitch[nSwitch];
14449
14450   if( nArg<=1 ){
14451     utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
14452     return arUsage(stderr);
14453   }else{
14454     char *z = azArg[1];
14455     if( z[0]!='-' ){
14456       /* Traditional style [tar] invocation */
14457       int i;
14458       int iArg = 2;
14459       for(i=0; z[i]; i++){
14460         const char *zArg = 0;
14461         struct ArSwitch *pOpt;
14462         for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
14463           if( z[i]==pOpt->cShort ) break;
14464         }
14465         if( pOpt==pEnd ){
14466           return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
14467         }
14468         if( pOpt->bArg ){
14469           if( iArg>=nArg ){
14470             return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
14471           }
14472           zArg = azArg[iArg++];
14473         }
14474         if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
14475       }
14476       pAr->nArg = nArg-iArg;
14477       if( pAr->nArg>0 ){
14478         pAr->azArg = &azArg[iArg];
14479       }
14480     }else{
14481       /* Non-traditional invocation */
14482       int iArg;
14483       for(iArg=1; iArg<nArg; iArg++){
14484         int n;
14485         z = azArg[iArg];
14486         if( z[0]!='-' ){
14487           /* All remaining command line words are command arguments. */
14488           pAr->azArg = &azArg[iArg];
14489           pAr->nArg = nArg-iArg;
14490           break;
14491         }
14492         n = strlen30(z);
14493
14494         if( z[1]!='-' ){
14495           int i;
14496           /* One or more short options */
14497           for(i=1; i<n; i++){
14498             const char *zArg = 0;
14499             struct ArSwitch *pOpt;
14500             for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
14501               if( z[i]==pOpt->cShort ) break;
14502             }
14503             if( pOpt==pEnd ){
14504               return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
14505             }
14506             if( pOpt->bArg ){
14507               if( i<(n-1) ){
14508                 zArg = &z[i+1];
14509                 i = n;
14510               }else{
14511                 if( iArg>=(nArg-1) ){
14512                   return arErrorMsg(pAr, "option requires an argument: %c",
14513                                     z[i]);
14514                 }
14515                 zArg = azArg[++iArg];
14516               }
14517             }
14518             if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
14519           }
14520         }else if( z[2]=='\0' ){
14521           /* A -- option, indicating that all remaining command line words
14522           ** are command arguments.  */
14523           pAr->azArg = &azArg[iArg+1];
14524           pAr->nArg = nArg-iArg-1;
14525           break;
14526         }else{
14527           /* A long option */
14528           const char *zArg = 0;             /* Argument for option, if any */
14529           struct ArSwitch *pMatch = 0;      /* Matching option */
14530           struct ArSwitch *pOpt;            /* Iterator */
14531           for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
14532             const char *zLong = pOpt->zLong;
14533             if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
14534               if( pMatch ){
14535                 return arErrorMsg(pAr, "ambiguous option: %s",z);
14536               }else{
14537                 pMatch = pOpt;
14538               }
14539             }
14540           }
14541
14542           if( pMatch==0 ){
14543             return arErrorMsg(pAr, "unrecognized option: %s", z);
14544           }
14545           if( pMatch->bArg ){
14546             if( iArg>=(nArg-1) ){
14547               return arErrorMsg(pAr, "option requires an argument: %s", z);
14548             }
14549             zArg = azArg[++iArg];
14550           }
14551           if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
14552         }
14553       }
14554     }
14555   }
14556
14557   return SQLITE_OK;
14558 }
14559
14560 /*
14561 ** This function assumes that all arguments within the ArCommand.azArg[]
14562 ** array refer to archive members, as for the --extract or --list commands. 
14563 ** It checks that each of them are present. If any specified file is not
14564 ** present in the archive, an error is printed to stderr and an error
14565 ** code returned. Otherwise, if all specified arguments are present in
14566 ** the archive, SQLITE_OK is returned.
14567 **
14568 ** This function strips any trailing '/' characters from each argument.
14569 ** This is consistent with the way the [tar] command seems to work on
14570 ** Linux.
14571 */
14572 static int arCheckEntries(ArCommand *pAr){
14573   int rc = SQLITE_OK;
14574   if( pAr->nArg ){
14575     int i, j;
14576     sqlite3_stmt *pTest = 0;
14577
14578     shellPreparePrintf(pAr->db, &rc, &pTest,
14579         "SELECT name FROM %s WHERE name=$name", 
14580         pAr->zSrcTable
14581     );
14582     j = sqlite3_bind_parameter_index(pTest, "$name");
14583     for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
14584       char *z = pAr->azArg[i];
14585       int n = strlen30(z);
14586       int bOk = 0;
14587       while( n>0 && z[n-1]=='/' ) n--;
14588       z[n] = '\0';
14589       sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
14590       if( SQLITE_ROW==sqlite3_step(pTest) ){
14591         bOk = 1;
14592       }
14593       shellReset(&rc, pTest);
14594       if( rc==SQLITE_OK && bOk==0 ){
14595         utf8_printf(stderr, "not found in archive: %s\n", z);
14596         rc = SQLITE_ERROR;
14597       }
14598     }
14599     shellFinalize(&rc, pTest);
14600   }
14601   return rc;
14602 }
14603
14604 /*
14605 ** Format a WHERE clause that can be used against the "sqlar" table to
14606 ** identify all archive members that match the command arguments held
14607 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
14608 ** The caller is responsible for eventually calling sqlite3_free() on
14609 ** any non-NULL (*pzWhere) value.
14610 */
14611 static void arWhereClause(
14612   int *pRc, 
14613   ArCommand *pAr, 
14614   char **pzWhere                  /* OUT: New WHERE clause */
14615 ){
14616   char *zWhere = 0;
14617   if( *pRc==SQLITE_OK ){
14618     if( pAr->nArg==0 ){
14619       zWhere = sqlite3_mprintf("1");
14620     }else{
14621       int i;
14622       const char *zSep = "";
14623       for(i=0; i<pAr->nArg; i++){
14624         const char *z = pAr->azArg[i];
14625         zWhere = sqlite3_mprintf(
14626           "%z%s name = '%q' OR substr(name,1,%d) = '%q/'", 
14627           zWhere, zSep, z, strlen30(z)+1, z
14628         );
14629         if( zWhere==0 ){
14630           *pRc = SQLITE_NOMEM;
14631           break;
14632         }
14633         zSep = " OR ";
14634       }
14635     }
14636   }
14637   *pzWhere = zWhere;
14638 }
14639
14640 /*
14641 ** Implementation of .ar "lisT" command. 
14642 */
14643 static int arListCommand(ArCommand *pAr){
14644   const char *zSql = "SELECT %s FROM %s WHERE %s"; 
14645   const char *azCols[] = {
14646     "name",
14647     "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
14648   };
14649
14650   char *zWhere = 0;
14651   sqlite3_stmt *pSql = 0;
14652   int rc;
14653
14654   rc = arCheckEntries(pAr);
14655   arWhereClause(&rc, pAr, &zWhere);
14656
14657   shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
14658                      pAr->zSrcTable, zWhere);
14659   if( pAr->bDryRun ){
14660     utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
14661   }else{
14662     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
14663       if( pAr->bVerbose ){
14664         utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
14665             sqlite3_column_text(pSql, 0),
14666             sqlite3_column_int(pSql, 1), 
14667             sqlite3_column_text(pSql, 2),
14668             sqlite3_column_text(pSql, 3)
14669         );
14670       }else{
14671         utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
14672       }
14673     }
14674   }
14675   shellFinalize(&rc, pSql);
14676   sqlite3_free(zWhere);
14677   return rc;
14678 }
14679
14680
14681 /*
14682 ** Implementation of .ar "eXtract" command. 
14683 */
14684 static int arExtractCommand(ArCommand *pAr){
14685   const char *zSql1 = 
14686     "SELECT "
14687     " ($dir || name),"
14688     " writefile(($dir || name), %s, mode, mtime) "
14689     "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
14690     " AND name NOT GLOB '*..[/\\]*'";
14691
14692   const char *azExtraArg[] = { 
14693     "sqlar_uncompress(data, sz)",
14694     "data"
14695   };
14696
14697   sqlite3_stmt *pSql = 0;
14698   int rc = SQLITE_OK;
14699   char *zDir = 0;
14700   char *zWhere = 0;
14701   int i, j;
14702
14703   /* If arguments are specified, check that they actually exist within
14704   ** the archive before proceeding. And formulate a WHERE clause to
14705   ** match them.  */
14706   rc = arCheckEntries(pAr);
14707   arWhereClause(&rc, pAr, &zWhere);
14708
14709   if( rc==SQLITE_OK ){
14710     if( pAr->zDir ){
14711       zDir = sqlite3_mprintf("%s/", pAr->zDir);
14712     }else{
14713       zDir = sqlite3_mprintf("");
14714     }
14715     if( zDir==0 ) rc = SQLITE_NOMEM;
14716   }
14717
14718   shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, 
14719       azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
14720   );
14721
14722   if( rc==SQLITE_OK ){
14723     j = sqlite3_bind_parameter_index(pSql, "$dir");
14724     sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
14725
14726     /* Run the SELECT statement twice. The first time, writefile() is called
14727     ** for all archive members that should be extracted. The second time,
14728     ** only for the directories. This is because the timestamps for
14729     ** extracted directories must be reset after they are populated (as
14730     ** populating them changes the timestamp).  */
14731     for(i=0; i<2; i++){
14732       j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
14733       sqlite3_bind_int(pSql, j, i);
14734       if( pAr->bDryRun ){
14735         utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
14736       }else{
14737         while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
14738           if( i==0 && pAr->bVerbose ){
14739             utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
14740           }
14741         }
14742       }
14743       shellReset(&rc, pSql);
14744     }
14745     shellFinalize(&rc, pSql);
14746   }
14747
14748   sqlite3_free(zDir);
14749   sqlite3_free(zWhere);
14750   return rc;
14751 }
14752
14753 /*
14754 ** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
14755 */
14756 static int arExecSql(ArCommand *pAr, const char *zSql){
14757   int rc;
14758   if( pAr->bDryRun ){
14759     utf8_printf(pAr->p->out, "%s\n", zSql);
14760     rc = SQLITE_OK;
14761   }else{
14762     char *zErr = 0;
14763     rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
14764     if( zErr ){
14765       utf8_printf(stdout, "ERROR: %s\n", zErr);
14766       sqlite3_free(zErr);
14767     }
14768   }
14769   return rc;
14770 }
14771
14772
14773 /*
14774 ** Implementation of .ar "create", "insert", and "update" commands.
14775 **
14776 **     create    ->     Create a new SQL archive
14777 **     insert    ->     Insert or reinsert all files listed
14778 **     update    ->     Insert files that have changed or that were not
14779 **                      previously in the archive
14780 **
14781 ** Create the "sqlar" table in the database if it does not already exist.
14782 ** Then add each file in the azFile[] array to the archive. Directories
14783 ** are added recursively. If argument bVerbose is non-zero, a message is
14784 ** printed on stdout for each file archived.
14785 **
14786 ** The create command is the same as update, except that it drops
14787 ** any existing "sqlar" table before beginning.  The "insert" command
14788 ** always overwrites every file named on the command-line, where as
14789 ** "update" only overwrites if the size or mtime or mode has changed.
14790 */
14791 static int arCreateOrUpdateCommand(
14792   ArCommand *pAr,                 /* Command arguments and options */
14793   int bUpdate,                    /* true for a --create. */
14794   int bOnlyIfChanged              /* Only update if file has changed */
14795 ){
14796   const char *zCreate = 
14797       "CREATE TABLE IF NOT EXISTS sqlar(\n"
14798       "  name TEXT PRIMARY KEY,  -- name of the file\n"
14799       "  mode INT,               -- access permissions\n"
14800       "  mtime INT,              -- last modification time\n"
14801       "  sz INT,                 -- original file size\n"
14802       "  data BLOB               -- compressed content\n"
14803       ")";
14804   const char *zDrop = "DROP TABLE IF EXISTS sqlar";
14805   const char *zInsertFmt[2] = {
14806      "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
14807      "  SELECT\n"
14808      "    %s,\n"
14809      "    mode,\n"
14810      "    mtime,\n"
14811      "    CASE substr(lsmode(mode),1,1)\n"
14812      "      WHEN '-' THEN length(data)\n"
14813      "      WHEN 'd' THEN 0\n"
14814      "      ELSE -1 END,\n"
14815      "    sqlar_compress(data)\n"
14816      "  FROM fsdir(%Q,%Q) AS disk\n"
14817      "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
14818      ,
14819      "REPLACE INTO %s(name,mode,mtime,data)\n"
14820      "  SELECT\n"
14821      "    %s,\n"
14822      "    mode,\n"
14823      "    mtime,\n"
14824      "    data\n"
14825      "  FROM fsdir(%Q,%Q) AS disk\n"
14826      "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
14827   };
14828   int i;                          /* For iterating through azFile[] */
14829   int rc;                         /* Return code */
14830   const char *zTab = 0;           /* SQL table into which to insert */
14831   char *zSql;
14832   char zTemp[50];
14833   char *zExists = 0;
14834
14835   arExecSql(pAr, "PRAGMA page_size=512");
14836   rc = arExecSql(pAr, "SAVEPOINT ar;");
14837   if( rc!=SQLITE_OK ) return rc;
14838   zTemp[0] = 0; 
14839   if( pAr->bZip ){
14840     /* Initialize the zipfile virtual table, if necessary */
14841     if( pAr->zFile ){
14842       sqlite3_uint64 r;
14843       sqlite3_randomness(sizeof(r),&r);
14844       sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
14845       zTab = zTemp;
14846       zSql = sqlite3_mprintf(
14847          "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
14848          zTab, pAr->zFile
14849       );
14850       rc = arExecSql(pAr, zSql);
14851       sqlite3_free(zSql);
14852     }else{
14853       zTab = "zip";
14854     }
14855   }else{
14856     /* Initialize the table for an SQLAR */
14857     zTab = "sqlar";
14858     if( bUpdate==0 ){
14859       rc = arExecSql(pAr, zDrop);
14860       if( rc!=SQLITE_OK ) goto end_ar_transaction;
14861     }
14862     rc = arExecSql(pAr, zCreate);
14863   }
14864   if( bOnlyIfChanged ){
14865     zExists = sqlite3_mprintf(
14866       " AND NOT EXISTS("
14867           "SELECT 1 FROM %s AS mem"
14868           " WHERE mem.name=disk.name"
14869           " AND mem.mtime=disk.mtime"
14870           " AND mem.mode=disk.mode)", zTab);
14871   }else{
14872     zExists = sqlite3_mprintf("");
14873   }
14874   if( zExists==0 ) rc = SQLITE_NOMEM;
14875   for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
14876     char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
14877         pAr->bVerbose ? "shell_putsnl(name)" : "name",
14878         pAr->azArg[i], pAr->zDir, zExists);
14879     rc = arExecSql(pAr, zSql2);
14880     sqlite3_free(zSql2);
14881   }
14882 end_ar_transaction:
14883   if( rc!=SQLITE_OK ){
14884     sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
14885   }else{
14886     rc = arExecSql(pAr, "RELEASE ar;");
14887     if( pAr->bZip && pAr->zFile ){
14888       zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
14889       arExecSql(pAr, zSql);
14890       sqlite3_free(zSql);
14891     }
14892   }
14893   sqlite3_free(zExists);
14894   return rc;
14895 }
14896
14897 /*
14898 ** Implementation of ".ar" dot command.
14899 */
14900 static int arDotCommand(
14901   ShellState *pState,          /* Current shell tool state */
14902   int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
14903   char **azArg,                /* Array of arguments passed to dot command */
14904   int nArg                     /* Number of entries in azArg[] */
14905 ){
14906   ArCommand cmd;
14907   int rc;
14908   memset(&cmd, 0, sizeof(cmd));
14909   cmd.fromCmdLine = fromCmdLine;
14910   rc = arParseCommand(azArg, nArg, &cmd);
14911   if( rc==SQLITE_OK ){
14912     int eDbType = SHELL_OPEN_UNSPEC;
14913     cmd.p = pState;
14914     cmd.db = pState->db;
14915     if( cmd.zFile ){
14916       eDbType = deduceDatabaseType(cmd.zFile, 1);
14917     }else{
14918       eDbType = pState->openMode;
14919     }
14920     if( eDbType==SHELL_OPEN_ZIPFILE ){
14921       if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
14922         if( cmd.zFile==0 ){
14923           cmd.zSrcTable = sqlite3_mprintf("zip");
14924         }else{
14925           cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
14926         }
14927       }
14928       cmd.bZip = 1;
14929     }else if( cmd.zFile ){
14930       int flags;
14931       if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
14932       if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT 
14933            || cmd.eCmd==AR_CMD_UPDATE ){
14934         flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
14935       }else{
14936         flags = SQLITE_OPEN_READONLY;
14937       }
14938       cmd.db = 0;
14939       if( cmd.bDryRun ){
14940         utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
14941              eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
14942       }
14943       rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags, 
14944              eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
14945       if( rc!=SQLITE_OK ){
14946         utf8_printf(stderr, "cannot open file: %s (%s)\n", 
14947             cmd.zFile, sqlite3_errmsg(cmd.db)
14948         );
14949         goto end_ar_command;
14950       }
14951       sqlite3_fileio_init(cmd.db, 0, 0);
14952       sqlite3_sqlar_init(cmd.db, 0, 0);
14953       sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
14954                               shellPutsFunc, 0, 0);
14955
14956     }
14957     if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
14958       if( cmd.eCmd!=AR_CMD_CREATE
14959        && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
14960       ){
14961         utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
14962         rc = SQLITE_ERROR;
14963         goto end_ar_command;
14964       }
14965       cmd.zSrcTable = sqlite3_mprintf("sqlar");
14966     }
14967
14968     switch( cmd.eCmd ){
14969       case AR_CMD_CREATE:
14970         rc = arCreateOrUpdateCommand(&cmd, 0, 0);
14971         break;
14972
14973       case AR_CMD_EXTRACT:
14974         rc = arExtractCommand(&cmd);
14975         break;
14976
14977       case AR_CMD_LIST:
14978         rc = arListCommand(&cmd);
14979         break;
14980
14981       case AR_CMD_HELP:
14982         arUsage(pState->out);
14983         break;
14984
14985       case AR_CMD_INSERT:
14986         rc = arCreateOrUpdateCommand(&cmd, 1, 0);
14987         break;
14988
14989       default:
14990         assert( cmd.eCmd==AR_CMD_UPDATE );
14991         rc = arCreateOrUpdateCommand(&cmd, 1, 1);
14992         break;
14993     }
14994   }
14995 end_ar_command:
14996   if( cmd.db!=pState->db ){
14997     close_db(cmd.db);
14998   }
14999   sqlite3_free(cmd.zSrcTable);
15000
15001   return rc;
15002 }
15003 /* End of the ".archive" or ".ar" command logic
15004 *******************************************************************************/
15005 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
15006
15007 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15008 /*
15009 ** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
15010 ** Otherwise, the SQL statement or statements in zSql are executed using
15011 ** database connection db and the error code written to *pRc before
15012 ** this function returns.
15013 */
15014 static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
15015   int rc = *pRc;
15016   if( rc==SQLITE_OK ){
15017     char *zErr = 0;
15018     rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
15019     if( rc!=SQLITE_OK ){
15020       raw_printf(stderr, "SQL error: %s\n", zErr);
15021     }
15022     *pRc = rc;
15023   }
15024 }
15025
15026 /*
15027 ** Like shellExec(), except that zFmt is a printf() style format string.
15028 */
15029 static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
15030   char *z = 0;
15031   if( *pRc==SQLITE_OK ){
15032     va_list ap;
15033     va_start(ap, zFmt);
15034     z = sqlite3_vmprintf(zFmt, ap);
15035     va_end(ap);
15036     if( z==0 ){
15037       *pRc = SQLITE_NOMEM;
15038     }else{
15039       shellExec(db, pRc, z);
15040     }
15041     sqlite3_free(z);
15042   }
15043 }
15044
15045 /*
15046 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
15047 ** Otherwise, an attempt is made to allocate, zero and return a pointer
15048 ** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
15049 ** to SQLITE_NOMEM and NULL returned.
15050 */
15051 static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
15052   void *pRet = 0;
15053   if( *pRc==SQLITE_OK ){
15054     pRet = sqlite3_malloc64(nByte);
15055     if( pRet==0 ){
15056       *pRc = SQLITE_NOMEM;
15057     }else{
15058       memset(pRet, 0, nByte);
15059     }
15060   }
15061   return pRet;
15062 }
15063
15064 /*
15065 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
15066 ** Otherwise, zFmt is treated as a printf() style string. The result of
15067 ** formatting it along with any trailing arguments is written into a 
15068 ** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
15069 ** It is the responsibility of the caller to eventually free this buffer
15070 ** using a call to sqlite3_free().
15071 ** 
15072 ** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL 
15073 ** pointer returned.
15074 */
15075 static char *shellMPrintf(int *pRc, const char *zFmt, ...){
15076   char *z = 0;
15077   if( *pRc==SQLITE_OK ){
15078     va_list ap;
15079     va_start(ap, zFmt);
15080     z = sqlite3_vmprintf(zFmt, ap);
15081     va_end(ap);
15082     if( z==0 ){
15083       *pRc = SQLITE_NOMEM;
15084     }
15085   }
15086   return z;
15087 }
15088
15089 /*
15090 ** When running the ".recover" command, each output table, and the special
15091 ** orphaned row table if it is required, is represented by an instance
15092 ** of the following struct.
15093 */
15094 typedef struct RecoverTable RecoverTable;
15095 struct RecoverTable {
15096   char *zQuoted;                  /* Quoted version of table name */
15097   int nCol;                       /* Number of columns in table */
15098   char **azlCol;                  /* Array of column lists */
15099   int iPk;                        /* Index of IPK column */
15100 };
15101
15102 /*
15103 ** Free a RecoverTable object allocated by recoverFindTable() or
15104 ** recoverOrphanTable().
15105 */
15106 static void recoverFreeTable(RecoverTable *pTab){
15107   if( pTab ){
15108     sqlite3_free(pTab->zQuoted);
15109     if( pTab->azlCol ){
15110       int i;
15111       for(i=0; i<=pTab->nCol; i++){
15112         sqlite3_free(pTab->azlCol[i]);
15113       }
15114       sqlite3_free(pTab->azlCol);
15115     }
15116     sqlite3_free(pTab);
15117   }
15118 }
15119
15120 /*
15121 ** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
15122 ** Otherwise, it allocates and returns a RecoverTable object based on the
15123 ** final four arguments passed to this function. It is the responsibility
15124 ** of the caller to eventually free the returned object using
15125 ** recoverFreeTable().
15126 */
15127 static RecoverTable *recoverNewTable(
15128   int *pRc,                       /* IN/OUT: Error code */
15129   const char *zName,              /* Name of table */
15130   const char *zSql,               /* CREATE TABLE statement */
15131   int bIntkey, 
15132   int nCol
15133 ){
15134   sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
15135   int rc = *pRc;
15136   RecoverTable *pTab = 0;
15137
15138   pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
15139   if( rc==SQLITE_OK ){
15140     int nSqlCol = 0;
15141     int bSqlIntkey = 0;
15142     sqlite3_stmt *pStmt = 0;
15143     
15144     rc = sqlite3_open("", &dbtmp);
15145     if( rc==SQLITE_OK ){
15146       sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
15147                               shellIdQuote, 0, 0);
15148     }
15149     if( rc==SQLITE_OK ){
15150       rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
15151     }
15152     if( rc==SQLITE_OK ){
15153       rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
15154       if( rc==SQLITE_ERROR ){
15155         rc = SQLITE_OK;
15156         goto finished;
15157       }
15158     }
15159     shellPreparePrintf(dbtmp, &rc, &pStmt, 
15160         "SELECT count(*) FROM pragma_table_info(%Q)", zName
15161     );
15162     if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15163       nSqlCol = sqlite3_column_int(pStmt, 0);
15164     }
15165     shellFinalize(&rc, pStmt);
15166
15167     if( rc!=SQLITE_OK || nSqlCol<nCol ){
15168       goto finished;
15169     }
15170
15171     shellPreparePrintf(dbtmp, &rc, &pStmt, 
15172       "SELECT ("
15173       "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
15174       ") FROM sqlite_master WHERE name = %Q", zName
15175     );
15176     if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15177       bSqlIntkey = sqlite3_column_int(pStmt, 0);
15178     }
15179     shellFinalize(&rc, pStmt);
15180
15181     if( bIntkey==bSqlIntkey ){
15182       int i;
15183       const char *zPk = "_rowid_";
15184       sqlite3_stmt *pPkFinder = 0;
15185
15186       /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
15187       ** set zPk to the name of the PK column, and pTab->iPk to the index
15188       ** of the column, where columns are 0-numbered from left to right.
15189       ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
15190       ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
15191       pTab->iPk = -2;
15192       if( bIntkey ){
15193         shellPreparePrintf(dbtmp, &rc, &pPkFinder, 
15194           "SELECT cid, name FROM pragma_table_info(%Q) "
15195           "  WHERE pk=1 AND type='integer' COLLATE nocase"
15196           "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
15197           , zName, zName
15198         );
15199         if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
15200           pTab->iPk = sqlite3_column_int(pPkFinder, 0);
15201           zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
15202         }
15203       }
15204
15205       pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
15206       pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
15207       pTab->nCol = nSqlCol;
15208
15209       if( bIntkey ){
15210         pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
15211       }else{
15212         pTab->azlCol[0] = shellMPrintf(&rc, "");
15213       }
15214       i = 1;
15215       shellPreparePrintf(dbtmp, &rc, &pStmt, 
15216           "SELECT %Q || group_concat(shell_idquote(name), ', ') "
15217           "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
15218           "FROM pragma_table_info(%Q)", 
15219           bIntkey ? ", " : "", pTab->iPk, 
15220           bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
15221           zName
15222       );
15223       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15224         const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
15225         pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
15226         i++;
15227       }
15228       shellFinalize(&rc, pStmt);
15229
15230       shellFinalize(&rc, pPkFinder);
15231     }
15232   }
15233
15234  finished:
15235   sqlite3_close(dbtmp);
15236   *pRc = rc;
15237   if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
15238     recoverFreeTable(pTab);
15239     pTab = 0;
15240   }
15241   return pTab;
15242 }
15243
15244 /*
15245 ** This function is called to search the schema recovered from the
15246 ** sqlite_master table of the (possibly) corrupt database as part
15247 ** of a ".recover" command. Specifically, for a table with root page
15248 ** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
15249 ** table must be a WITHOUT ROWID table, or if non-zero, not one of
15250 ** those.
15251 **
15252 ** If a table is found, a (RecoverTable*) object is returned. Or, if
15253 ** no such table is found, but bIntkey is false and iRoot is the 
15254 ** root page of an index in the recovered schema, then (*pbNoop) is
15255 ** set to true and NULL returned. Or, if there is no such table or
15256 ** index, NULL is returned and (*pbNoop) set to 0, indicating that
15257 ** the caller should write data to the orphans table.
15258 */
15259 static RecoverTable *recoverFindTable(
15260   ShellState *pState,             /* Shell state object */
15261   int *pRc,                       /* IN/OUT: Error code */
15262   int iRoot,                      /* Root page of table */
15263   int bIntkey,                    /* True for an intkey table */
15264   int nCol,                       /* Number of columns in table */
15265   int *pbNoop                     /* OUT: True if iRoot is root of index */
15266 ){
15267   sqlite3_stmt *pStmt = 0;
15268   RecoverTable *pRet = 0;
15269   int bNoop = 0;
15270   const char *zSql = 0;
15271   const char *zName = 0;
15272
15273   /* Search the recovered schema for an object with root page iRoot. */
15274   shellPreparePrintf(pState->db, pRc, &pStmt,
15275       "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
15276   );
15277   while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15278     const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
15279     if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
15280       bNoop = 1;
15281       break;
15282     }
15283     if( sqlite3_stricmp(zType, "table")==0 ){
15284       zName = (const char*)sqlite3_column_text(pStmt, 1);
15285       zSql = (const char*)sqlite3_column_text(pStmt, 2);
15286       pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
15287       break;
15288     }
15289   }
15290
15291   shellFinalize(pRc, pStmt);
15292   *pbNoop = bNoop;
15293   return pRet;
15294 }
15295
15296 /*
15297 ** Return a RecoverTable object representing the orphans table.
15298 */
15299 static RecoverTable *recoverOrphanTable(
15300   ShellState *pState,             /* Shell state object */
15301   int *pRc,                       /* IN/OUT: Error code */
15302   const char *zLostAndFound,      /* Base name for orphans table */
15303   int nCol                        /* Number of user data columns */
15304 ){
15305   RecoverTable *pTab = 0;
15306   if( nCol>=0 && *pRc==SQLITE_OK ){
15307     int i;
15308
15309     /* This block determines the name of the orphan table. The prefered
15310     ** name is zLostAndFound. But if that clashes with another name
15311     ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
15312     ** and so on until a non-clashing name is found.  */
15313     int iTab = 0;
15314     char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
15315     sqlite3_stmt *pTest = 0;
15316     shellPrepare(pState->db, pRc,
15317         "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
15318     );
15319     if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
15320     while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
15321       shellReset(pRc, pTest);
15322       sqlite3_free(zTab);
15323       zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
15324       sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
15325     }
15326     shellFinalize(pRc, pTest);
15327
15328     pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
15329     if( pTab ){
15330       pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
15331       pTab->nCol = nCol;
15332       pTab->iPk = -2;
15333       if( nCol>0 ){
15334         pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
15335         if( pTab->azlCol ){
15336           pTab->azlCol[nCol] = shellMPrintf(pRc, "");
15337           for(i=nCol-1; i>=0; i--){
15338             pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
15339           }
15340         }
15341       }
15342
15343       if( *pRc!=SQLITE_OK ){
15344         recoverFreeTable(pTab);
15345         pTab = 0;
15346       }else{
15347         raw_printf(pState->out, 
15348             "CREATE TABLE %s(rootpgno INTEGER, "
15349             "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
15350         );
15351         for(i=0; i<nCol; i++){
15352           raw_printf(pState->out, ", c%d", i);
15353         }
15354         raw_printf(pState->out, ");\n");
15355       }
15356     }
15357     sqlite3_free(zTab);
15358   }
15359   return pTab;
15360 }
15361
15362 /*
15363 ** This function is called to recover data from the database. A script
15364 ** to construct a new database containing all recovered data is output
15365 ** on stream pState->out.
15366 */
15367 static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
15368   int rc = SQLITE_OK;
15369   sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
15370   sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
15371   sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
15372   const char *zRecoveryDb = "";   /* Name of "recovery" database */
15373   const char *zLostAndFound = "lost_and_found";
15374   int i;
15375   int nOrphan = -1;
15376   RecoverTable *pOrphan = 0;
15377
15378   int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
15379   int bRowids = 1;                /* 0 if --no-rowids */
15380   for(i=1; i<nArg; i++){
15381     char *z = azArg[i];
15382     int n;
15383     if( z[0]=='-' && z[1]=='-' ) z++;
15384     n = strlen30(z);
15385     if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
15386       bFreelist = 0;
15387     }else
15388     if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
15389       i++;
15390       zRecoveryDb = azArg[i];
15391     }else
15392     if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
15393       i++;
15394       zLostAndFound = azArg[i];
15395     }else
15396     if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
15397       bRowids = 0;
15398     }
15399     else{
15400       utf8_printf(stderr, "unexpected option: %s\n", azArg[i]); 
15401       showHelp(pState->out, azArg[0]);
15402       return 1;
15403     }
15404   }
15405
15406   shellExecPrintf(pState->db, &rc,
15407     /* Attach an in-memory database named 'recovery'. Create an indexed 
15408     ** cache of the sqlite_dbptr virtual table. */
15409     "PRAGMA writable_schema = on;"
15410     "ATTACH %Q AS recovery;"
15411     "DROP TABLE IF EXISTS recovery.dbptr;"
15412     "DROP TABLE IF EXISTS recovery.freelist;"
15413     "DROP TABLE IF EXISTS recovery.map;"
15414     "DROP TABLE IF EXISTS recovery.schema;"
15415     "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
15416   );
15417
15418   if( bFreelist ){
15419     shellExec(pState->db, &rc,
15420       "WITH trunk(pgno) AS ("
15421       "  SELECT shell_int32("
15422       "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
15423       "      WHERE x>0"
15424       "    UNION"
15425       "  SELECT shell_int32("
15426       "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
15427       "      FROM trunk WHERE x>0"
15428       "),"
15429       "freelist(data, n, freepgno) AS ("
15430       "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
15431       "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
15432       "    UNION ALL"
15433       "  SELECT data, n-1, shell_int32(data, 2+n) "
15434       "      FROM freelist WHERE n>=0"
15435       ")"
15436       "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
15437     );
15438   }
15439
15440   /* If this is an auto-vacuum database, add all pointer-map pages to
15441   ** the freelist table. Do this regardless of whether or not 
15442   ** --freelist-corrupt was specified.  */
15443   shellExec(pState->db, &rc, 
15444     "WITH ptrmap(pgno) AS ("
15445     "  SELECT 2 WHERE shell_int32("
15446     "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
15447     "  )"
15448     "    UNION ALL "
15449     "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
15450     "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
15451     ")"
15452     "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
15453   );
15454
15455   shellExec(pState->db, &rc, 
15456     "CREATE TABLE recovery.dbptr("
15457     "      pgno, child, PRIMARY KEY(child, pgno)"
15458     ") WITHOUT ROWID;"
15459     "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
15460     "    SELECT * FROM sqlite_dbptr"
15461     "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
15462
15463     /* Delete any pointer to page 1. This ensures that page 1 is considered
15464     ** a root page, regardless of how corrupt the db is. */
15465     "DELETE FROM recovery.dbptr WHERE child = 1;"
15466
15467     /* Delete all pointers to any pages that have more than one pointer
15468     ** to them. Such pages will be treated as root pages when recovering
15469     ** data.  */
15470     "DELETE FROM recovery.dbptr WHERE child IN ("
15471     "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
15472     ");"
15473
15474     /* Create the "map" table that will (eventually) contain instructions
15475     ** for dealing with each page in the db that contains one or more 
15476     ** records. */
15477     "CREATE TABLE recovery.map("
15478       "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
15479     ");"
15480
15481     /* Populate table [map]. If there are circular loops of pages in the
15482     ** database, the following adds all pages in such a loop to the map
15483     ** as individual root pages. This could be handled better.  */
15484     "WITH pages(i, maxlen) AS ("
15485     "  SELECT page_count, ("
15486     "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
15487     "  ) FROM pragma_page_count WHERE page_count>0"
15488     "    UNION ALL"
15489     "  SELECT i-1, ("
15490     "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
15491     "  ) FROM pages WHERE i>=2"
15492     ")"
15493     "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
15494     "  SELECT i, maxlen, NULL, ("
15495     "    WITH p(orig, pgno, parent) AS ("
15496     "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
15497     "        UNION "
15498     "      SELECT i, p.parent, "
15499     "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
15500     "    )"
15501     "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
15502     ") "
15503     "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
15504     "UPDATE recovery.map AS o SET intkey = ("
15505     "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
15506     ");"
15507
15508     /* Extract data from page 1 and any linked pages into table
15509     ** recovery.schema. With the same schema as an sqlite_master table.  */
15510     "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
15511     "INSERT INTO recovery.schema SELECT "
15512     "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
15513     "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
15514     "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
15515     "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
15516     "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
15517     "FROM sqlite_dbdata WHERE pgno IN ("
15518     "  SELECT pgno FROM recovery.map WHERE root=1"
15519     ")"
15520     "GROUP BY pgno, cell;"
15521     "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
15522   );
15523
15524   /* Open a transaction, then print out all non-virtual, non-"sqlite_%" 
15525   ** CREATE TABLE statements that extracted from the existing schema.  */
15526   if( rc==SQLITE_OK ){
15527     sqlite3_stmt *pStmt = 0;
15528     /* ".recover" might output content in an order which causes immediate
15529     ** foreign key constraints to be violated. So disable foreign-key
15530     ** constraint enforcement to prevent problems when running the output
15531     ** script. */
15532     raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
15533     raw_printf(pState->out, "BEGIN;\n");
15534     raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
15535     shellPrepare(pState->db, &rc,
15536         "SELECT sql FROM recovery.schema "
15537         "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
15538     );
15539     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15540       const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
15541       raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n", 
15542           &zCreateTable[12]
15543       );
15544     }
15545     shellFinalize(&rc, pStmt);
15546   }
15547
15548   /* Figure out if an orphan table will be required. And if so, how many
15549   ** user columns it should contain */
15550   shellPrepare(pState->db, &rc, 
15551       "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
15552       , &pLoop
15553   );
15554   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
15555     nOrphan = sqlite3_column_int(pLoop, 0);
15556   }
15557   shellFinalize(&rc, pLoop);
15558   pLoop = 0;
15559
15560   shellPrepare(pState->db, &rc,
15561       "SELECT pgno FROM recovery.map WHERE root=?", &pPages
15562   );
15563
15564   shellPrepare(pState->db, &rc,
15565       "SELECT max(field), group_concat(shell_escape_crnl(quote"
15566       "(case when (? AND field<0) then NULL else value end)"
15567       "), ', ')"
15568       ", min(field) "
15569       "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
15570       "GROUP BY cell", &pCells
15571   );
15572
15573   /* Loop through each root page. */
15574   shellPrepare(pState->db, &rc, 
15575       "SELECT root, intkey, max(maxlen) FROM recovery.map" 
15576       " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
15577       "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
15578       ")", &pLoop
15579   );
15580   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
15581     int iRoot = sqlite3_column_int(pLoop, 0);
15582     int bIntkey = sqlite3_column_int(pLoop, 1);
15583     int nCol = sqlite3_column_int(pLoop, 2);
15584     int bNoop = 0;
15585     RecoverTable *pTab;
15586
15587     assert( bIntkey==0 || bIntkey==1 );
15588     pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
15589     if( bNoop || rc ) continue;
15590     if( pTab==0 ){
15591       if( pOrphan==0 ){
15592         pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
15593       }
15594       pTab = pOrphan;
15595       if( pTab==0 ) break;
15596     }
15597
15598     if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
15599       raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
15600     }
15601     sqlite3_bind_int(pPages, 1, iRoot);
15602     if( bRowids==0 && pTab->iPk<0 ){
15603       sqlite3_bind_int(pCells, 1, 1);
15604     }else{
15605       sqlite3_bind_int(pCells, 1, 0);
15606     }
15607     sqlite3_bind_int(pCells, 3, pTab->iPk);
15608
15609     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
15610       int iPgno = sqlite3_column_int(pPages, 0);
15611       sqlite3_bind_int(pCells, 2, iPgno);
15612       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
15613         int nField = sqlite3_column_int(pCells, 0);
15614         int iMin = sqlite3_column_int(pCells, 2);
15615         const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
15616
15617         RecoverTable *pTab2 = pTab;
15618         if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
15619           if( pOrphan==0 ){
15620             pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
15621           }
15622           pTab2 = pOrphan;
15623           if( pTab2==0 ) break;
15624         }
15625
15626         nField = nField+1;
15627         if( pTab2==pOrphan ){
15628           raw_printf(pState->out, 
15629               "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
15630               pTab2->zQuoted, iRoot, iPgno, nField,
15631               iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
15632           );
15633         }else{
15634           raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n", 
15635               pTab2->zQuoted, pTab2->azlCol[nField], zVal
15636           );
15637         }
15638       }
15639       shellReset(&rc, pCells);
15640     }
15641     shellReset(&rc, pPages);
15642     if( pTab!=pOrphan ) recoverFreeTable(pTab);
15643   }
15644   shellFinalize(&rc, pLoop);
15645   shellFinalize(&rc, pPages);
15646   shellFinalize(&rc, pCells);
15647   recoverFreeTable(pOrphan);
15648
15649   /* The rest of the schema */
15650   if( rc==SQLITE_OK ){
15651     sqlite3_stmt *pStmt = 0;
15652     shellPrepare(pState->db, &rc, 
15653         "SELECT sql, name FROM recovery.schema "
15654         "WHERE sql NOT LIKE 'create table%'", &pStmt
15655     );
15656     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15657       const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
15658       if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
15659         const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
15660         char *zPrint = shellMPrintf(&rc, 
15661           "INSERT INTO sqlite_master VALUES('table', %Q, %Q, 0, %Q)",
15662           zName, zName, zSql
15663         );
15664         raw_printf(pState->out, "%s;\n", zPrint);
15665         sqlite3_free(zPrint);
15666       }else{
15667         raw_printf(pState->out, "%s;\n", zSql);
15668       }
15669     }
15670     shellFinalize(&rc, pStmt);
15671   }
15672
15673   if( rc==SQLITE_OK ){
15674     raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
15675     raw_printf(pState->out, "COMMIT;\n");
15676   }
15677   sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
15678   return rc;
15679 }
15680 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
15681
15682
15683 /*
15684 ** If an input line begins with "." then invoke this routine to
15685 ** process that line.
15686 **
15687 ** Return 1 on error, 2 to exit, and 0 otherwise.
15688 */
15689 static int do_meta_command(char *zLine, ShellState *p){
15690   int h = 1;
15691   int nArg = 0;
15692   int n, c;
15693   int rc = 0;
15694   char *azArg[52];
15695
15696 #ifndef SQLITE_OMIT_VIRTUALTABLE
15697   if( p->expert.pExpert ){
15698     expertFinish(p, 1, 0);
15699   }
15700 #endif
15701
15702   /* Parse the input line into tokens.
15703   */
15704   while( zLine[h] && nArg<ArraySize(azArg)-1 ){
15705     while( IsSpace(zLine[h]) ){ h++; }
15706     if( zLine[h]==0 ) break;
15707     if( zLine[h]=='\'' || zLine[h]=='"' ){
15708       int delim = zLine[h++];
15709       azArg[nArg++] = &zLine[h];
15710       while( zLine[h] && zLine[h]!=delim ){
15711         if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
15712         h++;
15713       }
15714       if( zLine[h]==delim ){
15715         zLine[h++] = 0;
15716       }
15717       if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
15718     }else{
15719       azArg[nArg++] = &zLine[h];
15720       while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
15721       if( zLine[h] ) zLine[h++] = 0;
15722       resolve_backslashes(azArg[nArg-1]);
15723     }
15724   }
15725   azArg[nArg] = 0;
15726
15727   /* Process the input line.
15728   */
15729   if( nArg==0 ) return 0; /* no tokens, no error */
15730   n = strlen30(azArg[0]);
15731   c = azArg[0][0];
15732   clearTempFile(p);
15733
15734 #ifndef SQLITE_OMIT_AUTHORIZATION
15735   if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
15736     if( nArg!=2 ){
15737       raw_printf(stderr, "Usage: .auth ON|OFF\n");
15738       rc = 1;
15739       goto meta_command_exit;
15740     }
15741     open_db(p, 0);
15742     if( booleanValue(azArg[1]) ){
15743       sqlite3_set_authorizer(p->db, shellAuth, p);
15744     }else{
15745       sqlite3_set_authorizer(p->db, 0, 0);
15746     }
15747   }else
15748 #endif
15749
15750 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
15751   if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
15752     open_db(p, 0);
15753     rc = arDotCommand(p, 0, azArg, nArg);
15754   }else
15755 #endif
15756
15757   if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
15758    || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
15759   ){
15760     const char *zDestFile = 0;
15761     const char *zDb = 0;
15762     sqlite3 *pDest;
15763     sqlite3_backup *pBackup;
15764     int j;
15765     int bAsync = 0;
15766     const char *zVfs = 0;
15767     for(j=1; j<nArg; j++){
15768       const char *z = azArg[j];
15769       if( z[0]=='-' ){
15770         if( z[1]=='-' ) z++;
15771         if( strcmp(z, "-append")==0 ){
15772           zVfs = "apndvfs";
15773         }else
15774         if( strcmp(z, "-async")==0 ){
15775           bAsync = 1;
15776         }else
15777         {
15778           utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
15779           return 1;
15780         }
15781       }else if( zDestFile==0 ){
15782         zDestFile = azArg[j];
15783       }else if( zDb==0 ){
15784         zDb = zDestFile;
15785         zDestFile = azArg[j];
15786       }else{
15787         raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
15788         return 1;
15789       }
15790     }
15791     if( zDestFile==0 ){
15792       raw_printf(stderr, "missing FILENAME argument on .backup\n");
15793       return 1;
15794     }
15795     if( zDb==0 ) zDb = "main";
15796     rc = sqlite3_open_v2(zDestFile, &pDest, 
15797                   SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
15798     if( rc!=SQLITE_OK ){
15799       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
15800       close_db(pDest);
15801       return 1;
15802     }
15803     if( bAsync ){
15804       sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
15805                    0, 0, 0);
15806     }
15807     open_db(p, 0);
15808     pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
15809     if( pBackup==0 ){
15810       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
15811       close_db(pDest);
15812       return 1;
15813     }
15814     while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
15815     sqlite3_backup_finish(pBackup);
15816     if( rc==SQLITE_DONE ){
15817       rc = 0;
15818     }else{
15819       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
15820       rc = 1;
15821     }
15822     close_db(pDest);
15823   }else
15824
15825   if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
15826     if( nArg==2 ){
15827       bail_on_error = booleanValue(azArg[1]);
15828     }else{
15829       raw_printf(stderr, "Usage: .bail on|off\n");
15830       rc = 1;
15831     }
15832   }else
15833
15834   if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
15835     if( nArg==2 ){
15836       if( booleanValue(azArg[1]) ){
15837         setBinaryMode(p->out, 1);
15838       }else{
15839         setTextMode(p->out, 1);
15840       }
15841     }else{
15842       raw_printf(stderr, "Usage: .binary on|off\n");
15843       rc = 1;
15844     }
15845   }else
15846
15847   if( c=='c' && strcmp(azArg[0],"cd")==0 ){
15848     if( nArg==2 ){
15849 #if defined(_WIN32) || defined(WIN32)
15850       wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
15851       rc = !SetCurrentDirectoryW(z);
15852       sqlite3_free(z);
15853 #else
15854       rc = chdir(azArg[1]);
15855 #endif
15856       if( rc ){
15857         utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
15858         rc = 1;
15859       }
15860     }else{
15861       raw_printf(stderr, "Usage: .cd DIRECTORY\n");
15862       rc = 1;
15863     }
15864   }else
15865
15866   /* The undocumented ".breakpoint" command causes a call to the no-op
15867   ** routine named test_breakpoint().
15868   */
15869   if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
15870     test_breakpoint();
15871   }else
15872
15873   if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
15874     if( nArg==2 ){
15875       setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
15876     }else{
15877       raw_printf(stderr, "Usage: .changes on|off\n");
15878       rc = 1;
15879     }
15880   }else
15881
15882   /* Cancel output redirection, if it is currently set (by .testcase)
15883   ** Then read the content of the testcase-out.txt file and compare against
15884   ** azArg[1].  If there are differences, report an error and exit.
15885   */
15886   if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
15887     char *zRes = 0;
15888     output_reset(p);
15889     if( nArg!=2 ){
15890       raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
15891       rc = 2;
15892     }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
15893       raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
15894       rc = 2;
15895     }else if( testcase_glob(azArg[1],zRes)==0 ){
15896       utf8_printf(stderr,
15897                  "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
15898                  p->zTestcase, azArg[1], zRes);
15899       rc = 1;
15900     }else{
15901       utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
15902       p->nCheck++;
15903     }
15904     sqlite3_free(zRes);
15905   }else
15906
15907   if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
15908     if( nArg==2 ){
15909       tryToClone(p, azArg[1]);
15910     }else{
15911       raw_printf(stderr, "Usage: .clone FILENAME\n");
15912       rc = 1;
15913     }
15914   }else
15915
15916   if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
15917     ShellState data;
15918     char *zErrMsg = 0;
15919     open_db(p, 0);
15920     memcpy(&data, p, sizeof(data));
15921     data.showHeader = 0;
15922     data.cMode = data.mode = MODE_List;
15923     sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
15924     data.cnt = 0;
15925     sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
15926                  callback, &data, &zErrMsg);
15927     if( zErrMsg ){
15928       utf8_printf(stderr,"Error: %s\n", zErrMsg);
15929       sqlite3_free(zErrMsg);
15930       rc = 1;
15931     }
15932   }else
15933
15934   if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
15935     static const struct DbConfigChoices {
15936       const char *zName;
15937       int op;
15938     } aDbConfig[] = {
15939         { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
15940         { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
15941         { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
15942         { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
15943         { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
15944         { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
15945         { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
15946         { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
15947         { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
15948         { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
15949         { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
15950         { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
15951         { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
15952         { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
15953         { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
15954         { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
15955     };
15956     int ii, v;
15957     open_db(p, 0);
15958     for(ii=0; ii<ArraySize(aDbConfig); ii++){
15959       if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
15960       if( nArg>=3 ){
15961         sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
15962       }
15963       sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
15964       utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
15965       if( nArg>1 ) break;
15966     }
15967     if( nArg>1 && ii==ArraySize(aDbConfig) ){
15968       utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
15969       utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
15970     }   
15971   }else
15972
15973   if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
15974     rc = shell_dbinfo_command(p, nArg, azArg);
15975   }else
15976
15977 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15978   if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
15979     open_db(p, 0);
15980     rc = recoverDatabaseCmd(p, nArg, azArg);
15981   }else
15982 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
15983
15984   if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
15985     char *zLike = 0;
15986     char *zSql;
15987     int i;
15988     int savedShowHeader = p->showHeader;
15989     int savedShellFlags = p->shellFlgs;
15990     ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo);
15991     for(i=1; i<nArg; i++){
15992       if( azArg[i][0]=='-' ){
15993         const char *z = azArg[i]+1;
15994         if( z[0]=='-' ) z++;
15995         if( strcmp(z,"preserve-rowids")==0 ){
15996 #ifdef SQLITE_OMIT_VIRTUALTABLE
15997           raw_printf(stderr, "The --preserve-rowids option is not compatible"
15998                              " with SQLITE_OMIT_VIRTUALTABLE\n");
15999           rc = 1;
16000           goto meta_command_exit;
16001 #else
16002           ShellSetFlag(p, SHFLG_PreserveRowid);
16003 #endif
16004         }else
16005         if( strcmp(z,"newlines")==0 ){
16006           ShellSetFlag(p, SHFLG_Newlines);
16007         }else
16008         {
16009           raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
16010           rc = 1;
16011           goto meta_command_exit;
16012         }
16013       }else if( zLike ){
16014         zLike = sqlite3_mprintf("%z OR name LIKE %Q ESCAPE '\\'",
16015                 zLike, azArg[i]);
16016       }else{
16017         zLike = sqlite3_mprintf("name LIKE %Q ESCAPE '\\'", azArg[i]);
16018       }
16019     }
16020
16021     open_db(p, 0);
16022
16023     /* When playing back a "dump", the content might appear in an order
16024     ** which causes immediate foreign key constraints to be violated.
16025     ** So disable foreign-key constraint enforcement to prevent problems. */
16026     raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
16027     raw_printf(p->out, "BEGIN TRANSACTION;\n");
16028     p->writableSchema = 0;
16029     p->showHeader = 0;
16030     /* Set writable_schema=ON since doing so forces SQLite to initialize
16031     ** as much of the schema as it can even if the sqlite_master table is
16032     ** corrupt. */
16033     sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
16034     p->nErr = 0;
16035     if( zLike==0 ) zLike = sqlite3_mprintf("true");
16036     zSql = sqlite3_mprintf(
16037       "SELECT name, type, sql FROM sqlite_master "
16038       "WHERE (%s) AND type=='table'"
16039       "  AND sql NOT NULL"
16040       " ORDER BY tbl_name='sqlite_sequence', rowid",
16041       zLike
16042     );
16043     run_schema_dump_query(p,zSql);
16044     sqlite3_free(zSql);
16045     zSql = sqlite3_mprintf(
16046       "SELECT sql FROM sqlite_master "
16047       "WHERE (%s) AND sql NOT NULL"
16048       "  AND type IN ('index','trigger','view')",
16049       zLike
16050     );
16051     run_table_dump_query(p, zSql);
16052     sqlite3_free(zSql);
16053     sqlite3_free(zLike);
16054     if( p->writableSchema ){
16055       raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
16056       p->writableSchema = 0;
16057     }
16058     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
16059     sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
16060     raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
16061     p->showHeader = savedShowHeader;
16062     p->shellFlgs = savedShellFlags;
16063   }else
16064
16065   if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
16066     if( nArg==2 ){
16067       setOrClearFlag(p, SHFLG_Echo, azArg[1]);
16068     }else{
16069       raw_printf(stderr, "Usage: .echo on|off\n");
16070       rc = 1;
16071     }
16072   }else
16073
16074   if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
16075     if( nArg==2 ){
16076       p->autoEQPtest = 0;
16077       if( p->autoEQPtrace ){
16078         if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
16079         p->autoEQPtrace = 0;
16080       }
16081       if( strcmp(azArg[1],"full")==0 ){
16082         p->autoEQP = AUTOEQP_full;
16083       }else if( strcmp(azArg[1],"trigger")==0 ){
16084         p->autoEQP = AUTOEQP_trigger;
16085 #ifdef SQLITE_DEBUG
16086       }else if( strcmp(azArg[1],"test")==0 ){
16087         p->autoEQP = AUTOEQP_on;
16088         p->autoEQPtest = 1;
16089       }else if( strcmp(azArg[1],"trace")==0 ){
16090         p->autoEQP = AUTOEQP_full;
16091         p->autoEQPtrace = 1;
16092         open_db(p, 0);
16093         sqlite3_exec(p->db, "SELECT name FROM sqlite_master LIMIT 1", 0, 0, 0);
16094         sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
16095 #endif
16096       }else{
16097         p->autoEQP = (u8)booleanValue(azArg[1]);
16098       }
16099     }else{
16100       raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
16101       rc = 1;
16102     }
16103   }else
16104
16105   if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
16106     if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
16107     rc = 2;
16108   }else
16109
16110   /* The ".explain" command is automatic now.  It is largely pointless.  It
16111   ** retained purely for backwards compatibility */
16112   if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
16113     int val = 1;
16114     if( nArg>=2 ){
16115       if( strcmp(azArg[1],"auto")==0 ){
16116         val = 99;
16117       }else{
16118         val =  booleanValue(azArg[1]);
16119       }
16120     }
16121     if( val==1 && p->mode!=MODE_Explain ){
16122       p->normalMode = p->mode;
16123       p->mode = MODE_Explain;
16124       p->autoExplain = 0;
16125     }else if( val==0 ){
16126       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
16127       p->autoExplain = 0;
16128     }else if( val==99 ){
16129       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
16130       p->autoExplain = 1;
16131     }
16132   }else
16133
16134 #ifndef SQLITE_OMIT_VIRTUALTABLE
16135   if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
16136     open_db(p, 0);
16137     expertDotCommand(p, azArg, nArg);
16138   }else
16139 #endif
16140
16141   if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
16142     static const struct {
16143        const char *zCtrlName;   /* Name of a test-control option */
16144        int ctrlCode;            /* Integer code for that option */
16145        const char *zUsage;      /* Usage notes */
16146     } aCtrl[] = {
16147       { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
16148       { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
16149    /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
16150       { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
16151       { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
16152    /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
16153       { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
16154       { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },  
16155       { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
16156       { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
16157     };
16158     int filectrl = -1;
16159     int iCtrl = -1;
16160     sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
16161     int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
16162     int n2, i;
16163     const char *zCmd = 0;
16164     const char *zSchema = 0;
16165
16166     open_db(p, 0);
16167     zCmd = nArg>=2 ? azArg[1] : "help";
16168
16169     if( zCmd[0]=='-' 
16170      && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
16171      && nArg>=4
16172     ){
16173       zSchema = azArg[2];
16174       for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
16175       nArg -= 2;
16176       zCmd = azArg[1];
16177     }
16178
16179     /* The argument can optionally begin with "-" or "--" */
16180     if( zCmd[0]=='-' && zCmd[1] ){
16181       zCmd++;
16182       if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
16183     }
16184
16185     /* --help lists all file-controls */
16186     if( strcmp(zCmd,"help")==0 ){
16187       utf8_printf(p->out, "Available file-controls:\n");
16188       for(i=0; i<ArraySize(aCtrl); i++){
16189         utf8_printf(p->out, "  .filectrl %s %s\n",
16190                     aCtrl[i].zCtrlName, aCtrl[i].zUsage);
16191       }
16192       rc = 1;
16193       goto meta_command_exit;
16194     }
16195
16196     /* convert filectrl text option to value. allow any unique prefix
16197     ** of the option name, or a numerical value. */
16198     n2 = strlen30(zCmd);
16199     for(i=0; i<ArraySize(aCtrl); i++){
16200       if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
16201         if( filectrl<0 ){
16202           filectrl = aCtrl[i].ctrlCode;
16203           iCtrl = i;
16204         }else{
16205           utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
16206                               "Use \".filectrl --help\" for help\n", zCmd);
16207           rc = 1;
16208           goto meta_command_exit;
16209         }
16210       }
16211     }
16212     if( filectrl<0 ){
16213       utf8_printf(stderr,"Error: unknown file-control: %s\n"
16214                          "Use \".filectrl --help\" for help\n", zCmd);
16215     }else{
16216       switch(filectrl){
16217         case SQLITE_FCNTL_SIZE_LIMIT: {
16218           if( nArg!=2 && nArg!=3 ) break;
16219           iRes = nArg==3 ? integerValue(azArg[2]) : -1;
16220           sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
16221           isOk = 1;
16222           break;
16223         }
16224         case SQLITE_FCNTL_LOCK_TIMEOUT:
16225         case SQLITE_FCNTL_CHUNK_SIZE: {
16226           int x;
16227           if( nArg!=3 ) break;
16228           x = (int)integerValue(azArg[2]);
16229           sqlite3_file_control(p->db, zSchema, filectrl, &x);
16230           isOk = 2;
16231           break;
16232         }
16233         case SQLITE_FCNTL_PERSIST_WAL:
16234         case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
16235           int x;
16236           if( nArg!=2 && nArg!=3 ) break;
16237           x = nArg==3 ? booleanValue(azArg[2]) : -1;
16238           sqlite3_file_control(p->db, zSchema, filectrl, &x);
16239           iRes = x;
16240           isOk = 1;
16241           break;
16242         }
16243         case SQLITE_FCNTL_HAS_MOVED: {
16244           int x;
16245           if( nArg!=2 ) break;
16246           sqlite3_file_control(p->db, zSchema, filectrl, &x);
16247           iRes = x;
16248           isOk = 1;
16249           break;
16250         }
16251         case SQLITE_FCNTL_TEMPFILENAME: {
16252           char *z = 0;
16253           if( nArg!=2 ) break;
16254           sqlite3_file_control(p->db, zSchema, filectrl, &z);
16255           if( z ){
16256             utf8_printf(p->out, "%s\n", z);
16257             sqlite3_free(z);
16258           }
16259           isOk = 2;
16260           break;
16261         }
16262         case SQLITE_FCNTL_RESERVE_BYTES: {
16263           int x;
16264           if( nArg>=3 ){
16265             x = atoi(azArg[2]);
16266             sqlite3_file_control(p->db, zSchema, filectrl, &x);
16267           }
16268           x = -1;
16269           sqlite3_file_control(p->db, zSchema, filectrl, &x);
16270           utf8_printf(p->out,"%d\n", x);
16271           isOk = 2;
16272           break;
16273         }
16274       }
16275     }
16276     if( isOk==0 && iCtrl>=0 ){
16277       utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
16278       rc = 1;
16279     }else if( isOk==1 ){
16280       char zBuf[100];
16281       sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
16282       raw_printf(p->out, "%s\n", zBuf);
16283     }
16284   }else
16285
16286   if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
16287     ShellState data;
16288     char *zErrMsg = 0;
16289     int doStats = 0;
16290     memcpy(&data, p, sizeof(data));
16291     data.showHeader = 0;
16292     data.cMode = data.mode = MODE_Semi;
16293     if( nArg==2 && optionMatch(azArg[1], "indent") ){
16294       data.cMode = data.mode = MODE_Pretty;
16295       nArg = 1;
16296     }
16297     if( nArg!=1 ){
16298       raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
16299       rc = 1;
16300       goto meta_command_exit;
16301     }
16302     open_db(p, 0);
16303     rc = sqlite3_exec(p->db,
16304        "SELECT sql FROM"
16305        "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
16306        "     FROM sqlite_master UNION ALL"
16307        "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
16308        "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
16309        "ORDER BY rowid",
16310        callback, &data, &zErrMsg
16311     );
16312     if( rc==SQLITE_OK ){
16313       sqlite3_stmt *pStmt;
16314       rc = sqlite3_prepare_v2(p->db,
16315                "SELECT rowid FROM sqlite_master"
16316                " WHERE name GLOB 'sqlite_stat[134]'",
16317                -1, &pStmt, 0);
16318       doStats = sqlite3_step(pStmt)==SQLITE_ROW;
16319       sqlite3_finalize(pStmt);
16320     }
16321     if( doStats==0 ){
16322       raw_printf(p->out, "/* No STAT tables available */\n");
16323     }else{
16324       raw_printf(p->out, "ANALYZE sqlite_master;\n");
16325       sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
16326                    callback, &data, &zErrMsg);
16327       data.cMode = data.mode = MODE_Insert;
16328       data.zDestTable = "sqlite_stat1";
16329       shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
16330       data.zDestTable = "sqlite_stat4";
16331       shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
16332       raw_printf(p->out, "ANALYZE sqlite_master;\n");
16333     }
16334   }else
16335
16336   if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
16337     if( nArg==2 ){
16338       p->showHeader = booleanValue(azArg[1]);
16339     }else{
16340       raw_printf(stderr, "Usage: .headers on|off\n");
16341       rc = 1;
16342     }
16343   }else
16344
16345   if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
16346     if( nArg>=2 ){
16347       n = showHelp(p->out, azArg[1]);
16348       if( n==0 ){
16349         utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
16350       }
16351     }else{
16352       showHelp(p->out, 0);
16353     }
16354   }else
16355
16356   if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
16357     char *zTable = 0;           /* Insert data into this table */
16358     char *zFile = 0;            /* Name of file to extra content from */
16359     sqlite3_stmt *pStmt = NULL; /* A statement */
16360     int nCol;                   /* Number of columns in the table */
16361     int nByte;                  /* Number of bytes in an SQL string */
16362     int i, j;                   /* Loop counters */
16363     int needCommit;             /* True to COMMIT or ROLLBACK at end */
16364     int nSep;                   /* Number of bytes in p->colSeparator[] */
16365     char *zSql;                 /* An SQL statement */
16366     ImportCtx sCtx;             /* Reader context */
16367     char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
16368     int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close file */
16369     int eVerbose = 0;           /* Larger for more console output */
16370     int nSkip = 0;              /* Initial lines to skip */
16371     int useOutputMode = 1;      /* Use output mode to determine separators */
16372
16373     memset(&sCtx, 0, sizeof(sCtx));
16374     if( p->mode==MODE_Ascii ){
16375       xRead = ascii_read_one_field;
16376     }else{
16377       xRead = csv_read_one_field;
16378     }
16379     for(i=1; i<nArg; i++){
16380       char *z = azArg[i];
16381       if( z[0]=='-' && z[1]=='-' ) z++;
16382       if( z[0]!='-' ){
16383         if( zFile==0 ){
16384           zFile = z;
16385         }else if( zTable==0 ){
16386           zTable = z;
16387         }else{
16388           utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
16389           showHelp(p->out, "import");
16390           rc = 1;
16391           goto meta_command_exit;
16392         }
16393       }else if( strcmp(z,"-v")==0 ){
16394         eVerbose++;
16395       }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
16396         nSkip = integerValue(azArg[++i]);
16397       }else if( strcmp(z,"-ascii")==0 ){
16398         sCtx.cColSep = SEP_Unit[0];
16399         sCtx.cRowSep = SEP_Record[0];
16400         xRead = ascii_read_one_field;
16401         useOutputMode = 0;
16402       }else if( strcmp(z,"-csv")==0 ){
16403         sCtx.cColSep = ',';
16404         sCtx.cRowSep = '\n';
16405         xRead = csv_read_one_field;
16406         useOutputMode = 0;
16407       }else{
16408         utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
16409         showHelp(p->out, "import");
16410         rc = 1;
16411         goto meta_command_exit;
16412       }
16413     }
16414     if( zTable==0 ){
16415       utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
16416                   zFile==0 ? "FILE" : "TABLE");
16417       showHelp(p->out, "import");
16418       rc = 1;
16419       goto meta_command_exit;
16420     }
16421     seenInterrupt = 0;
16422     open_db(p, 0);
16423     if( useOutputMode ){
16424       /* If neither the --csv or --ascii options are specified, then set
16425       ** the column and row separator characters from the output mode. */
16426       nSep = strlen30(p->colSeparator);
16427       if( nSep==0 ){
16428         raw_printf(stderr,
16429                    "Error: non-null column separator required for import\n");
16430         rc = 1;
16431         goto meta_command_exit;
16432       }
16433       if( nSep>1 ){
16434         raw_printf(stderr, 
16435               "Error: multi-character column separators not allowed"
16436               " for import\n");
16437         rc = 1;
16438         goto meta_command_exit;
16439       }
16440       nSep = strlen30(p->rowSeparator);
16441       if( nSep==0 ){
16442         raw_printf(stderr,
16443             "Error: non-null row separator required for import\n");
16444         rc = 1;
16445         goto meta_command_exit;
16446       }
16447       if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
16448         /* When importing CSV (only), if the row separator is set to the
16449         ** default output row separator, change it to the default input
16450         ** row separator.  This avoids having to maintain different input
16451         ** and output row separators. */
16452         sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16453         nSep = strlen30(p->rowSeparator);
16454       }
16455       if( nSep>1 ){
16456         raw_printf(stderr, "Error: multi-character row separators not allowed"
16457                            " for import\n");
16458         rc = 1;
16459         goto meta_command_exit;
16460       }
16461       sCtx.cColSep = p->colSeparator[0];
16462       sCtx.cRowSep = p->rowSeparator[0];
16463     }
16464     sCtx.zFile = zFile;
16465     sCtx.nLine = 1;
16466     if( sCtx.zFile[0]=='|' ){
16467 #ifdef SQLITE_OMIT_POPEN
16468       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
16469       rc = 1;
16470       goto meta_command_exit;
16471 #else
16472       sCtx.in = popen(sCtx.zFile+1, "r");
16473       sCtx.zFile = "<pipe>";
16474       xCloser = pclose;
16475 #endif
16476     }else{
16477       sCtx.in = fopen(sCtx.zFile, "rb");
16478       xCloser = fclose;
16479     }
16480     if( sCtx.in==0 ){
16481       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
16482       rc = 1;
16483       goto meta_command_exit;
16484     }
16485     if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
16486       char zSep[2];
16487       zSep[1] = 0;
16488       zSep[0] = sCtx.cColSep;
16489       utf8_printf(p->out, "Column separator ");
16490       output_c_string(p->out, zSep);
16491       utf8_printf(p->out, ", row separator ");
16492       zSep[0] = sCtx.cRowSep;
16493       output_c_string(p->out, zSep);
16494       utf8_printf(p->out, "\n");
16495     }
16496     while( (nSkip--)>0 ){
16497       while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
16498       sCtx.nLine++;
16499     }
16500     zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
16501     if( zSql==0 ){
16502       xCloser(sCtx.in);
16503       shell_out_of_memory();
16504     }
16505     nByte = strlen30(zSql);
16506     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16507     import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
16508     if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
16509       char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
16510       char cSep = '(';
16511       while( xRead(&sCtx) ){
16512         zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
16513         cSep = ',';
16514         if( sCtx.cTerm!=sCtx.cColSep ) break;
16515       }
16516       if( cSep=='(' ){
16517         sqlite3_free(zCreate);
16518         sqlite3_free(sCtx.z);
16519         xCloser(sCtx.in);
16520         utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
16521         rc = 1;
16522         goto meta_command_exit;
16523       }
16524       zCreate = sqlite3_mprintf("%z\n)", zCreate);
16525       if( eVerbose>=1 ){
16526         utf8_printf(p->out, "%s\n", zCreate);
16527       }
16528       rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
16529       sqlite3_free(zCreate);
16530       if( rc ){
16531         utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
16532                 sqlite3_errmsg(p->db));
16533         sqlite3_free(sCtx.z);
16534         xCloser(sCtx.in);
16535         rc = 1;
16536         goto meta_command_exit;
16537       }
16538       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16539     }
16540     sqlite3_free(zSql);
16541     if( rc ){
16542       if (pStmt) sqlite3_finalize(pStmt);
16543       utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
16544       xCloser(sCtx.in);
16545       rc = 1;
16546       goto meta_command_exit;
16547     }
16548     nCol = sqlite3_column_count(pStmt);
16549     sqlite3_finalize(pStmt);
16550     pStmt = 0;
16551     if( nCol==0 ) return 0; /* no columns, no error */
16552     zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
16553     if( zSql==0 ){
16554       xCloser(sCtx.in);
16555       shell_out_of_memory();
16556     }
16557     sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
16558     j = strlen30(zSql);
16559     for(i=1; i<nCol; i++){
16560       zSql[j++] = ',';
16561       zSql[j++] = '?';
16562     }
16563     zSql[j++] = ')';
16564     zSql[j] = 0;
16565     if( eVerbose>=2 ){
16566       utf8_printf(p->out, "Insert using: %s\n", zSql);
16567     }
16568     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16569     sqlite3_free(zSql);
16570     if( rc ){
16571       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
16572       if (pStmt) sqlite3_finalize(pStmt);
16573       xCloser(sCtx.in);
16574       rc = 1;
16575       goto meta_command_exit;
16576     }
16577     needCommit = sqlite3_get_autocommit(p->db);
16578     if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
16579     do{
16580       int startLine = sCtx.nLine;
16581       for(i=0; i<nCol; i++){
16582         char *z = xRead(&sCtx);
16583         /*
16584         ** Did we reach end-of-file before finding any columns?
16585         ** If so, stop instead of NULL filling the remaining columns.
16586         */
16587         if( z==0 && i==0 ) break;
16588         /*
16589         ** Did we reach end-of-file OR end-of-line before finding any
16590         ** columns in ASCII mode?  If so, stop instead of NULL filling
16591         ** the remaining columns.
16592         */
16593         if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
16594         sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
16595         if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
16596           utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
16597                           "filling the rest with NULL\n",
16598                           sCtx.zFile, startLine, nCol, i+1);
16599           i += 2;
16600           while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
16601         }
16602       }
16603       if( sCtx.cTerm==sCtx.cColSep ){
16604         do{
16605           xRead(&sCtx);
16606           i++;
16607         }while( sCtx.cTerm==sCtx.cColSep );
16608         utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
16609                         "extras ignored\n",
16610                         sCtx.zFile, startLine, nCol, i);
16611       }
16612       if( i>=nCol ){
16613         sqlite3_step(pStmt);
16614         rc = sqlite3_reset(pStmt);
16615         if( rc!=SQLITE_OK ){
16616           utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
16617                       startLine, sqlite3_errmsg(p->db));
16618           sCtx.nErr++;
16619         }else{
16620           sCtx.nRow++;
16621         }
16622       }
16623     }while( sCtx.cTerm!=EOF );
16624
16625     xCloser(sCtx.in);
16626     sqlite3_free(sCtx.z);
16627     sqlite3_finalize(pStmt);
16628     if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
16629     if( eVerbose>0 ){
16630       utf8_printf(p->out,
16631           "Added %d rows with %d errors using %d lines of input\n",
16632           sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
16633     }
16634   }else
16635
16636 #ifndef SQLITE_UNTESTABLE
16637   if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
16638     char *zSql;
16639     char *zCollist = 0;
16640     sqlite3_stmt *pStmt;
16641     int tnum = 0;
16642     int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
16643     int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
16644     int i;
16645     if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
16646       utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
16647                           "       .imposter off\n");
16648       /* Also allowed, but not documented:
16649       **
16650       **    .imposter TABLE IMPOSTER
16651       **
16652       ** where TABLE is a WITHOUT ROWID table.  In that case, the
16653       ** imposter is another WITHOUT ROWID table with the columns in
16654       ** storage order. */
16655       rc = 1;
16656       goto meta_command_exit;
16657     }
16658     open_db(p, 0);
16659     if( nArg==2 ){
16660       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
16661       goto meta_command_exit;
16662     }
16663     zSql = sqlite3_mprintf(
16664       "SELECT rootpage, 0 FROM sqlite_master"
16665       " WHERE name='%q' AND type='index'"
16666       "UNION ALL "
16667       "SELECT rootpage, 1 FROM sqlite_master"
16668       " WHERE name='%q' AND type='table'"
16669       "   AND sql LIKE '%%without%%rowid%%'",
16670       azArg[1], azArg[1]
16671     );
16672     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16673     sqlite3_free(zSql);
16674     if( sqlite3_step(pStmt)==SQLITE_ROW ){
16675       tnum = sqlite3_column_int(pStmt, 0);
16676       isWO = sqlite3_column_int(pStmt, 1);
16677     }
16678     sqlite3_finalize(pStmt);
16679     zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
16680     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16681     sqlite3_free(zSql);
16682     i = 0;
16683     while( sqlite3_step(pStmt)==SQLITE_ROW ){
16684       char zLabel[20];
16685       const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
16686       i++;
16687       if( zCol==0 ){
16688         if( sqlite3_column_int(pStmt,1)==-1 ){
16689           zCol = "_ROWID_";
16690         }else{
16691           sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
16692           zCol = zLabel;
16693         }
16694       }
16695       if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
16696         lenPK = (int)strlen(zCollist);
16697       }
16698       if( zCollist==0 ){
16699         zCollist = sqlite3_mprintf("\"%w\"", zCol);
16700       }else{
16701         zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
16702       }
16703     }
16704     sqlite3_finalize(pStmt);
16705     if( i==0 || tnum==0 ){
16706       utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
16707       rc = 1;
16708       sqlite3_free(zCollist);
16709       goto meta_command_exit;
16710     }
16711     if( lenPK==0 ) lenPK = 100000;
16712     zSql = sqlite3_mprintf(
16713           "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
16714           azArg[2], zCollist, lenPK, zCollist);
16715     sqlite3_free(zCollist);
16716     rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
16717     if( rc==SQLITE_OK ){
16718       rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
16719       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
16720       if( rc ){
16721         utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
16722       }else{
16723         utf8_printf(stdout, "%s;\n", zSql);
16724         raw_printf(stdout,
16725           "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
16726           azArg[1], isWO ? "table" : "index"
16727         );
16728       }
16729     }else{
16730       raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
16731       rc = 1;
16732     }
16733     sqlite3_free(zSql);
16734   }else
16735 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
16736
16737 #ifdef SQLITE_ENABLE_IOTRACE
16738   if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
16739     SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
16740     if( iotrace && iotrace!=stdout ) fclose(iotrace);
16741     iotrace = 0;
16742     if( nArg<2 ){
16743       sqlite3IoTrace = 0;
16744     }else if( strcmp(azArg[1], "-")==0 ){
16745       sqlite3IoTrace = iotracePrintf;
16746       iotrace = stdout;
16747     }else{
16748       iotrace = fopen(azArg[1], "w");
16749       if( iotrace==0 ){
16750         utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
16751         sqlite3IoTrace = 0;
16752         rc = 1;
16753       }else{
16754         sqlite3IoTrace = iotracePrintf;
16755       }
16756     }
16757   }else
16758 #endif
16759
16760   if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
16761     static const struct {
16762        const char *zLimitName;   /* Name of a limit */
16763        int limitCode;            /* Integer code for that limit */
16764     } aLimit[] = {
16765       { "length",                SQLITE_LIMIT_LENGTH                    },
16766       { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
16767       { "column",                SQLITE_LIMIT_COLUMN                    },
16768       { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
16769       { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
16770       { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
16771       { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
16772       { "attached",              SQLITE_LIMIT_ATTACHED                  },
16773       { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
16774       { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
16775       { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
16776       { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
16777     };
16778     int i, n2;
16779     open_db(p, 0);
16780     if( nArg==1 ){
16781       for(i=0; i<ArraySize(aLimit); i++){
16782         printf("%20s %d\n", aLimit[i].zLimitName,
16783                sqlite3_limit(p->db, aLimit[i].limitCode, -1));
16784       }
16785     }else if( nArg>3 ){
16786       raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
16787       rc = 1;
16788       goto meta_command_exit;
16789     }else{
16790       int iLimit = -1;
16791       n2 = strlen30(azArg[1]);
16792       for(i=0; i<ArraySize(aLimit); i++){
16793         if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
16794           if( iLimit<0 ){
16795             iLimit = i;
16796           }else{
16797             utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
16798             rc = 1;
16799             goto meta_command_exit;
16800           }
16801         }
16802       }
16803       if( iLimit<0 ){
16804         utf8_printf(stderr, "unknown limit: \"%s\"\n"
16805                         "enter \".limits\" with no arguments for a list.\n",
16806                          azArg[1]);
16807         rc = 1;
16808         goto meta_command_exit;
16809       }
16810       if( nArg==3 ){
16811         sqlite3_limit(p->db, aLimit[iLimit].limitCode,
16812                       (int)integerValue(azArg[2]));
16813       }
16814       printf("%20s %d\n", aLimit[iLimit].zLimitName,
16815              sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
16816     }
16817   }else
16818
16819   if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
16820     open_db(p, 0);
16821     lintDotCommand(p, azArg, nArg);
16822   }else
16823
16824 #ifndef SQLITE_OMIT_LOAD_EXTENSION
16825   if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
16826     const char *zFile, *zProc;
16827     char *zErrMsg = 0;
16828     if( nArg<2 ){
16829       raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
16830       rc = 1;
16831       goto meta_command_exit;
16832     }
16833     zFile = azArg[1];
16834     zProc = nArg>=3 ? azArg[2] : 0;
16835     open_db(p, 0);
16836     rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
16837     if( rc!=SQLITE_OK ){
16838       utf8_printf(stderr, "Error: %s\n", zErrMsg);
16839       sqlite3_free(zErrMsg);
16840       rc = 1;
16841     }
16842   }else
16843 #endif
16844
16845   if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
16846     if( nArg!=2 ){
16847       raw_printf(stderr, "Usage: .log FILENAME\n");
16848       rc = 1;
16849     }else{
16850       const char *zFile = azArg[1];
16851       output_file_close(p->pLog);
16852       p->pLog = output_file_open(zFile, 0);
16853     }
16854   }else
16855
16856   if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
16857     const char *zMode = nArg>=2 ? azArg[1] : "";
16858     int n2 = strlen30(zMode);
16859     int c2 = zMode[0];
16860     if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
16861       p->mode = MODE_Line;
16862       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16863     }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
16864       p->mode = MODE_Column;
16865       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16866     }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
16867       p->mode = MODE_List;
16868       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
16869       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16870     }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
16871       p->mode = MODE_Html;
16872     }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
16873       p->mode = MODE_Tcl;
16874       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
16875       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16876     }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
16877       p->mode = MODE_Csv;
16878       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
16879       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
16880     }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
16881       p->mode = MODE_List;
16882       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
16883     }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
16884       p->mode = MODE_Insert;
16885       set_table_name(p, nArg>=3 ? azArg[2] : "table");
16886     }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
16887       p->mode = MODE_Quote;
16888     }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
16889       p->mode = MODE_Ascii;
16890       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
16891       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
16892     }else if( nArg==1 ){
16893       raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
16894     }else{
16895       raw_printf(stderr, "Error: mode should be one of: "
16896          "ascii column csv html insert line list quote tabs tcl\n");
16897       rc = 1;
16898     }
16899     p->cMode = p->mode;
16900   }else
16901
16902   if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
16903     if( nArg==2 ){
16904       sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
16905                        "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
16906     }else{
16907       raw_printf(stderr, "Usage: .nullvalue STRING\n");
16908       rc = 1;
16909     }
16910   }else
16911
16912 #ifdef SQLITE_DEBUG
16913   if( c=='o' && strcmp(azArg[0],"oom")==0 ){
16914     int i;
16915     for(i=1; i<nArg; i++){
16916       const char *z = azArg[i];
16917       if( z[0]=='-' && z[1]=='-' ) z++;
16918       if( strcmp(z,"-repeat")==0 ){
16919         if( i==nArg-1 ){
16920           raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
16921           rc = 1;
16922         }else{
16923           oomRepeat = (int)integerValue(azArg[++i]);
16924         }
16925       }else if( IsDigit(z[0]) ){
16926         oomCounter = (int)integerValue(azArg[i]);
16927       }else{
16928         raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
16929         raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
16930         rc = 1;
16931       }
16932     }
16933     if( rc==0 ){
16934       raw_printf(p->out, "oomCounter = %d\n", oomCounter);
16935       raw_printf(p->out, "oomRepeat  = %d\n", oomRepeat);
16936     }
16937   }else
16938 #endif /* SQLITE_DEBUG */
16939
16940   if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
16941     char *zNewFilename;  /* Name of the database file to open */
16942     int iName = 1;       /* Index in azArg[] of the filename */
16943     int newFlag = 0;     /* True to delete file before opening */
16944     /* Close the existing database */
16945     session_close_all(p);
16946     close_db(p->db);
16947     p->db = 0;
16948     p->zDbFilename = 0;
16949     sqlite3_free(p->zFreeOnClose);
16950     p->zFreeOnClose = 0;
16951     p->openMode = SHELL_OPEN_UNSPEC;
16952     p->openFlags = 0;
16953     p->szMax = 0;
16954     /* Check for command-line arguments */
16955     for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
16956       const char *z = azArg[iName];
16957       if( optionMatch(z,"new") ){
16958         newFlag = 1;
16959 #ifdef SQLITE_HAVE_ZLIB
16960       }else if( optionMatch(z, "zip") ){
16961         p->openMode = SHELL_OPEN_ZIPFILE;
16962 #endif
16963       }else if( optionMatch(z, "append") ){
16964         p->openMode = SHELL_OPEN_APPENDVFS;
16965       }else if( optionMatch(z, "readonly") ){
16966         p->openMode = SHELL_OPEN_READONLY;
16967       }else if( optionMatch(z, "nofollow") ){
16968         p->openFlags |= SQLITE_OPEN_NOFOLLOW;
16969 #ifdef SQLITE_ENABLE_DESERIALIZE
16970       }else if( optionMatch(z, "deserialize") ){
16971         p->openMode = SHELL_OPEN_DESERIALIZE;
16972       }else if( optionMatch(z, "hexdb") ){
16973         p->openMode = SHELL_OPEN_HEXDB;
16974       }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
16975         p->szMax = integerValue(azArg[++iName]);
16976 #endif /* SQLITE_ENABLE_DESERIALIZE */
16977       }else if( z[0]=='-' ){
16978         utf8_printf(stderr, "unknown option: %s\n", z);
16979         rc = 1;
16980         goto meta_command_exit;
16981       }
16982     }
16983     /* If a filename is specified, try to open it first */
16984     zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
16985     if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
16986       if( newFlag ) shellDeleteFile(zNewFilename);
16987       p->zDbFilename = zNewFilename;
16988       open_db(p, OPEN_DB_KEEPALIVE);
16989       if( p->db==0 ){
16990         utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
16991         sqlite3_free(zNewFilename);
16992       }else{
16993         p->zFreeOnClose = zNewFilename;
16994       }
16995     }
16996     if( p->db==0 ){
16997       /* As a fall-back open a TEMP database */
16998       p->zDbFilename = 0;
16999       open_db(p, 0);
17000     }
17001   }else
17002
17003   if( (c=='o'
17004         && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
17005    || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
17006   ){
17007     const char *zFile = 0;
17008     int bTxtMode = 0;
17009     int i;
17010     int eMode = 0;
17011     int bBOM = 0;
17012     int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
17013
17014     if( c=='e' ){
17015       eMode = 'x';
17016       bOnce = 2;
17017     }else if( strncmp(azArg[0],"once",n)==0 ){
17018       bOnce = 1;
17019     }
17020     for(i=1; i<nArg; i++){
17021       char *z = azArg[i];
17022       if( z[0]=='-' ){
17023         if( z[1]=='-' ) z++;
17024         if( strcmp(z,"-bom")==0 ){
17025           bBOM = 1;
17026         }else if( c!='e' && strcmp(z,"-x")==0 ){
17027           eMode = 'x';  /* spreadsheet */
17028         }else if( c!='e' && strcmp(z,"-e")==0 ){
17029           eMode = 'e';  /* text editor */
17030         }else{
17031           utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
17032                       azArg[i]);
17033           showHelp(p->out, azArg[0]);
17034           rc = 1;
17035           goto meta_command_exit;
17036         }
17037       }else if( zFile==0 ){
17038         zFile = z;
17039       }else{
17040         utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
17041                     azArg[i]);
17042         showHelp(p->out, azArg[0]);
17043         rc = 1;
17044         goto meta_command_exit;
17045       }
17046     }
17047     if( zFile==0 ) zFile = "stdout";
17048     if( bOnce ){
17049       p->outCount = 2;
17050     }else{
17051       p->outCount = 0;
17052     }
17053     output_reset(p);
17054 #ifndef SQLITE_NOHAVE_SYSTEM
17055     if( eMode=='e' || eMode=='x' ){
17056       p->doXdgOpen = 1;
17057       outputModePush(p);
17058       if( eMode=='x' ){
17059         /* spreadsheet mode.  Output as CSV. */
17060         newTempFile(p, "csv");
17061         ShellClearFlag(p, SHFLG_Echo);
17062         p->mode = MODE_Csv;
17063         sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
17064         sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
17065       }else{
17066         /* text editor mode */
17067         newTempFile(p, "txt");
17068         bTxtMode = 1;
17069       }
17070       zFile = p->zTempFile;
17071     }
17072 #endif /* SQLITE_NOHAVE_SYSTEM */
17073     if( zFile[0]=='|' ){
17074 #ifdef SQLITE_OMIT_POPEN
17075       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
17076       rc = 1;
17077       p->out = stdout;
17078 #else
17079       p->out = popen(zFile + 1, "w");
17080       if( p->out==0 ){
17081         utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
17082         p->out = stdout;
17083         rc = 1;
17084       }else{
17085         if( bBOM ) fprintf(p->out,"\357\273\277");
17086         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
17087       }
17088 #endif
17089     }else{
17090       p->out = output_file_open(zFile, bTxtMode);
17091       if( p->out==0 ){
17092         if( strcmp(zFile,"off")!=0 ){
17093           utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
17094         }
17095         p->out = stdout;
17096         rc = 1;
17097       } else {
17098         if( bBOM ) fprintf(p->out,"\357\273\277");
17099         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
17100       }
17101     }
17102   }else
17103
17104   if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
17105     open_db(p,0);
17106     if( nArg<=1 ) goto parameter_syntax_error;
17107
17108     /* .parameter clear
17109     ** Clear all bind parameters by dropping the TEMP table that holds them.
17110     */
17111     if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
17112       sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
17113                    0, 0, 0);
17114     }else
17115
17116     /* .parameter list
17117     ** List all bind parameters.
17118     */
17119     if( nArg==2 && strcmp(azArg[1],"list")==0 ){
17120       sqlite3_stmt *pStmt = 0;
17121       int rx;
17122       int len = 0;
17123       rx = sqlite3_prepare_v2(p->db,
17124              "SELECT max(length(key)) "
17125              "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
17126       if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
17127         len = sqlite3_column_int(pStmt, 0);
17128         if( len>40 ) len = 40;
17129       }
17130       sqlite3_finalize(pStmt);
17131       pStmt = 0;
17132       if( len ){
17133         rx = sqlite3_prepare_v2(p->db,
17134              "SELECT key, quote(value) "
17135              "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
17136         while( sqlite3_step(pStmt)==SQLITE_ROW ){
17137           utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
17138                       sqlite3_column_text(pStmt,1));
17139         }
17140         sqlite3_finalize(pStmt);
17141       }
17142     }else
17143
17144     /* .parameter init
17145     ** Make sure the TEMP table used to hold bind parameters exists.
17146     ** Create it if necessary.
17147     */
17148     if( nArg==2 && strcmp(azArg[1],"init")==0 ){
17149       bind_table_init(p);
17150     }else
17151
17152     /* .parameter set NAME VALUE
17153     ** Set or reset a bind parameter.  NAME should be the full parameter
17154     ** name exactly as it appears in the query.  (ex: $abc, @def).  The
17155     ** VALUE can be in either SQL literal notation, or if not it will be
17156     ** understood to be a text string.
17157     */
17158     if( nArg==4 && strcmp(azArg[1],"set")==0 ){
17159       int rx;
17160       char *zSql;
17161       sqlite3_stmt *pStmt;
17162       const char *zKey = azArg[2];
17163       const char *zValue = azArg[3];
17164       bind_table_init(p);
17165       zSql = sqlite3_mprintf(
17166                   "REPLACE INTO temp.sqlite_parameters(key,value)"
17167                   "VALUES(%Q,%s);", zKey, zValue);
17168       if( zSql==0 ) shell_out_of_memory();
17169       pStmt = 0;
17170       rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
17171       sqlite3_free(zSql);
17172       if( rx!=SQLITE_OK ){
17173         sqlite3_finalize(pStmt);
17174         pStmt = 0;
17175         zSql = sqlite3_mprintf(
17176                    "REPLACE INTO temp.sqlite_parameters(key,value)"
17177                    "VALUES(%Q,%Q);", zKey, zValue);
17178         if( zSql==0 ) shell_out_of_memory();
17179         rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
17180         sqlite3_free(zSql);
17181         if( rx!=SQLITE_OK ){
17182           utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
17183           sqlite3_finalize(pStmt);
17184           pStmt = 0;
17185           rc = 1;
17186         }
17187       }
17188       sqlite3_step(pStmt);
17189       sqlite3_finalize(pStmt);
17190     }else
17191
17192     /* .parameter unset NAME
17193     ** Remove the NAME binding from the parameter binding table, if it
17194     ** exists.
17195     */
17196     if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
17197       char *zSql = sqlite3_mprintf(
17198           "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
17199       if( zSql==0 ) shell_out_of_memory();
17200       sqlite3_exec(p->db, zSql, 0, 0, 0);
17201       sqlite3_free(zSql);
17202     }else
17203     /* If no command name matches, show a syntax error */
17204     parameter_syntax_error:
17205     showHelp(p->out, "parameter");
17206   }else
17207
17208   if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
17209     int i;
17210     for(i=1; i<nArg; i++){
17211       if( i>1 ) raw_printf(p->out, " ");
17212       utf8_printf(p->out, "%s", azArg[i]);
17213     }
17214     raw_printf(p->out, "\n");
17215   }else
17216
17217 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
17218   if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
17219     int i;
17220     int nn = 0;
17221     p->flgProgress = 0;
17222     p->mxProgress = 0;
17223     p->nProgress = 0;
17224     for(i=1; i<nArg; i++){
17225       const char *z = azArg[i];
17226       if( z[0]=='-' ){
17227         z++;
17228         if( z[0]=='-' ) z++;
17229         if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
17230           p->flgProgress |= SHELL_PROGRESS_QUIET;
17231           continue;
17232         }
17233         if( strcmp(z,"reset")==0 ){
17234           p->flgProgress |= SHELL_PROGRESS_RESET;
17235           continue;
17236         }
17237         if( strcmp(z,"once")==0 ){
17238           p->flgProgress |= SHELL_PROGRESS_ONCE;
17239           continue;
17240         }
17241         if( strcmp(z,"limit")==0 ){
17242           if( i+1>=nArg ){
17243             utf8_printf(stderr, "Error: missing argument on --limit\n");
17244             rc = 1;
17245             goto meta_command_exit;
17246           }else{
17247             p->mxProgress = (int)integerValue(azArg[++i]);
17248           }
17249           continue;
17250         }
17251         utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
17252         rc = 1;
17253         goto meta_command_exit;
17254       }else{
17255         nn = (int)integerValue(z);
17256       }
17257     }
17258     open_db(p, 0);
17259     sqlite3_progress_handler(p->db, nn, progress_handler, p);
17260   }else
17261 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
17262
17263   if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
17264     if( nArg >= 2) {
17265       strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
17266     }
17267     if( nArg >= 3) {
17268       strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
17269     }
17270   }else
17271
17272   if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
17273     rc = 2;
17274   }else
17275
17276   if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
17277     FILE *inSaved = p->in;
17278     int savedLineno = p->lineno;
17279     if( nArg!=2 ){
17280       raw_printf(stderr, "Usage: .read FILE\n");
17281       rc = 1;
17282       goto meta_command_exit;
17283     }
17284     p->in = fopen(azArg[1], "rb");
17285     if( p->in==0 ){
17286       utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
17287       rc = 1;
17288     }else{
17289       rc = process_input(p);
17290       fclose(p->in);
17291     }
17292     p->in = inSaved;
17293     p->lineno = savedLineno;
17294   }else
17295
17296   if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
17297     const char *zSrcFile;
17298     const char *zDb;
17299     sqlite3 *pSrc;
17300     sqlite3_backup *pBackup;
17301     int nTimeout = 0;
17302
17303     if( nArg==2 ){
17304       zSrcFile = azArg[1];
17305       zDb = "main";
17306     }else if( nArg==3 ){
17307       zSrcFile = azArg[2];
17308       zDb = azArg[1];
17309     }else{
17310       raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
17311       rc = 1;
17312       goto meta_command_exit;
17313     }
17314     rc = sqlite3_open(zSrcFile, &pSrc);
17315     if( rc!=SQLITE_OK ){
17316       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
17317       close_db(pSrc);
17318       return 1;
17319     }
17320     open_db(p, 0);
17321     pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
17322     if( pBackup==0 ){
17323       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
17324       close_db(pSrc);
17325       return 1;
17326     }
17327     while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
17328           || rc==SQLITE_BUSY  ){
17329       if( rc==SQLITE_BUSY ){
17330         if( nTimeout++ >= 3 ) break;
17331         sqlite3_sleep(100);
17332       }
17333     }
17334     sqlite3_backup_finish(pBackup);
17335     if( rc==SQLITE_DONE ){
17336       rc = 0;
17337     }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
17338       raw_printf(stderr, "Error: source database is busy\n");
17339       rc = 1;
17340     }else{
17341       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
17342       rc = 1;
17343     }
17344     close_db(pSrc);
17345   }else
17346
17347   if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
17348     if( nArg==2 ){
17349       p->scanstatsOn = (u8)booleanValue(azArg[1]);
17350 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
17351       raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
17352 #endif
17353     }else{
17354       raw_printf(stderr, "Usage: .scanstats on|off\n");
17355       rc = 1;
17356     }
17357   }else
17358
17359   if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
17360     ShellText sSelect;
17361     ShellState data;
17362     char *zErrMsg = 0;
17363     const char *zDiv = "(";
17364     const char *zName = 0;
17365     int iSchema = 0;
17366     int bDebug = 0;
17367     int ii;
17368
17369     open_db(p, 0);
17370     memcpy(&data, p, sizeof(data));
17371     data.showHeader = 0;
17372     data.cMode = data.mode = MODE_Semi;
17373     initText(&sSelect);
17374     for(ii=1; ii<nArg; ii++){
17375       if( optionMatch(azArg[ii],"indent") ){
17376         data.cMode = data.mode = MODE_Pretty;
17377       }else if( optionMatch(azArg[ii],"debug") ){
17378         bDebug = 1;
17379       }else if( zName==0 ){
17380         zName = azArg[ii];
17381       }else{
17382         raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
17383         rc = 1;
17384         goto meta_command_exit;
17385       }
17386     }
17387     if( zName!=0 ){
17388       int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
17389       if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
17390         char *new_argv[2], *new_colv[2];
17391         new_argv[0] = sqlite3_mprintf(
17392                       "CREATE TABLE %s (\n"
17393                       "  type text,\n"
17394                       "  name text,\n"
17395                       "  tbl_name text,\n"
17396                       "  rootpage integer,\n"
17397                       "  sql text\n"
17398                       ")", isMaster ? "sqlite_master" : "sqlite_temp_master");
17399         new_argv[1] = 0;
17400         new_colv[0] = "sql";
17401         new_colv[1] = 0;
17402         callback(&data, 1, new_argv, new_colv);
17403         sqlite3_free(new_argv[0]);
17404       }
17405     }
17406     if( zDiv ){
17407       sqlite3_stmt *pStmt = 0;
17408       rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
17409                               -1, &pStmt, 0);
17410       if( rc ){
17411         utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
17412         sqlite3_finalize(pStmt);
17413         rc = 1;
17414         goto meta_command_exit;
17415       }
17416       appendText(&sSelect, "SELECT sql FROM", 0);
17417       iSchema = 0;
17418       while( sqlite3_step(pStmt)==SQLITE_ROW ){
17419         const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
17420         char zScNum[30];
17421         sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
17422         appendText(&sSelect, zDiv, 0);
17423         zDiv = " UNION ALL ";
17424         appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
17425         if( sqlite3_stricmp(zDb, "main")!=0 ){
17426           appendText(&sSelect, zDb, '\'');
17427         }else{
17428           appendText(&sSelect, "NULL", 0);
17429         }
17430         appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
17431         appendText(&sSelect, zScNum, 0);
17432         appendText(&sSelect, " AS snum, ", 0);
17433         appendText(&sSelect, zDb, '\'');
17434         appendText(&sSelect, " AS sname FROM ", 0);
17435         appendText(&sSelect, zDb, quoteChar(zDb));
17436         appendText(&sSelect, ".sqlite_master", 0);
17437       }
17438       sqlite3_finalize(pStmt);
17439 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
17440       if( zName ){
17441         appendText(&sSelect,
17442            " UNION ALL SELECT shell_module_schema(name),"
17443            " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
17444         0);
17445       }
17446 #endif
17447       appendText(&sSelect, ") WHERE ", 0);
17448       if( zName ){
17449         char *zQarg = sqlite3_mprintf("%Q", zName);
17450         int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
17451                     strchr(zName, '[') != 0;
17452         if( strchr(zName, '.') ){
17453           appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
17454         }else{
17455           appendText(&sSelect, "lower(tbl_name)", 0);
17456         }
17457         appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
17458         appendText(&sSelect, zQarg, 0);
17459         if( !bGlob ){
17460           appendText(&sSelect, " ESCAPE '\\' ", 0);
17461         }
17462         appendText(&sSelect, " AND ", 0);
17463         sqlite3_free(zQarg);
17464       }
17465       appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
17466                            " ORDER BY snum, rowid", 0);
17467       if( bDebug ){
17468         utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
17469       }else{
17470         rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
17471       }
17472       freeText(&sSelect);
17473     }
17474     if( zErrMsg ){
17475       utf8_printf(stderr,"Error: %s\n", zErrMsg);
17476       sqlite3_free(zErrMsg);
17477       rc = 1;
17478     }else if( rc != SQLITE_OK ){
17479       raw_printf(stderr,"Error: querying schema information\n");
17480       rc = 1;
17481     }else{
17482       rc = 0;
17483     }
17484   }else
17485
17486 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
17487   if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
17488     sqlite3SelectTrace = (int)integerValue(azArg[1]);
17489   }else
17490 #endif
17491
17492 #if defined(SQLITE_ENABLE_SESSION)
17493   if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
17494     OpenSession *pSession = &p->aSession[0];
17495     char **azCmd = &azArg[1];
17496     int iSes = 0;
17497     int nCmd = nArg - 1;
17498     int i;
17499     if( nArg<=1 ) goto session_syntax_error;
17500     open_db(p, 0);
17501     if( nArg>=3 ){
17502       for(iSes=0; iSes<p->nSession; iSes++){
17503         if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
17504       }
17505       if( iSes<p->nSession ){
17506         pSession = &p->aSession[iSes];
17507         azCmd++;
17508         nCmd--;
17509       }else{
17510         pSession = &p->aSession[0];
17511         iSes = 0;
17512       }
17513     }
17514
17515     /* .session attach TABLE
17516     ** Invoke the sqlite3session_attach() interface to attach a particular
17517     ** table so that it is never filtered.
17518     */
17519     if( strcmp(azCmd[0],"attach")==0 ){
17520       if( nCmd!=2 ) goto session_syntax_error;
17521       if( pSession->p==0 ){
17522         session_not_open:
17523         raw_printf(stderr, "ERROR: No sessions are open\n");
17524       }else{
17525         rc = sqlite3session_attach(pSession->p, azCmd[1]);
17526         if( rc ){
17527           raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
17528           rc = 0;
17529         }
17530       }
17531     }else
17532
17533     /* .session changeset FILE
17534     ** .session patchset FILE
17535     ** Write a changeset or patchset into a file.  The file is overwritten.
17536     */
17537     if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
17538       FILE *out = 0;
17539       if( nCmd!=2 ) goto session_syntax_error;
17540       if( pSession->p==0 ) goto session_not_open;
17541       out = fopen(azCmd[1], "wb");
17542       if( out==0 ){
17543         utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
17544                     azCmd[1]);
17545       }else{
17546         int szChng;
17547         void *pChng;
17548         if( azCmd[0][0]=='c' ){
17549           rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
17550         }else{
17551           rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
17552         }
17553         if( rc ){
17554           printf("Error: error code %d\n", rc);
17555           rc = 0;
17556         }
17557         if( pChng
17558           && fwrite(pChng, szChng, 1, out)!=1 ){
17559           raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
17560                   szChng);
17561         }
17562         sqlite3_free(pChng);
17563         fclose(out);
17564       }
17565     }else
17566
17567     /* .session close
17568     ** Close the identified session
17569     */
17570     if( strcmp(azCmd[0], "close")==0 ){
17571       if( nCmd!=1 ) goto session_syntax_error;
17572       if( p->nSession ){
17573         session_close(pSession);
17574         p->aSession[iSes] = p->aSession[--p->nSession];
17575       }
17576     }else
17577
17578     /* .session enable ?BOOLEAN?
17579     ** Query or set the enable flag
17580     */
17581     if( strcmp(azCmd[0], "enable")==0 ){
17582       int ii;
17583       if( nCmd>2 ) goto session_syntax_error;
17584       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
17585       if( p->nSession ){
17586         ii = sqlite3session_enable(pSession->p, ii);
17587         utf8_printf(p->out, "session %s enable flag = %d\n",
17588                     pSession->zName, ii);
17589       }
17590     }else
17591
17592     /* .session filter GLOB ....
17593     ** Set a list of GLOB patterns of table names to be excluded.
17594     */
17595     if( strcmp(azCmd[0], "filter")==0 ){
17596       int ii, nByte;
17597       if( nCmd<2 ) goto session_syntax_error;
17598       if( p->nSession ){
17599         for(ii=0; ii<pSession->nFilter; ii++){
17600           sqlite3_free(pSession->azFilter[ii]);
17601         }
17602         sqlite3_free(pSession->azFilter);
17603         nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
17604         pSession->azFilter = sqlite3_malloc( nByte );
17605         if( pSession->azFilter==0 ){
17606           raw_printf(stderr, "Error: out or memory\n");
17607           exit(1);
17608         }
17609         for(ii=1; ii<nCmd; ii++){
17610           pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
17611         }
17612         pSession->nFilter = ii-1;
17613       }
17614     }else
17615
17616     /* .session indirect ?BOOLEAN?
17617     ** Query or set the indirect flag
17618     */
17619     if( strcmp(azCmd[0], "indirect")==0 ){
17620       int ii;
17621       if( nCmd>2 ) goto session_syntax_error;
17622       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
17623       if( p->nSession ){
17624         ii = sqlite3session_indirect(pSession->p, ii);
17625         utf8_printf(p->out, "session %s indirect flag = %d\n",
17626                     pSession->zName, ii);
17627       }
17628     }else
17629
17630     /* .session isempty
17631     ** Determine if the session is empty
17632     */
17633     if( strcmp(azCmd[0], "isempty")==0 ){
17634       int ii;
17635       if( nCmd!=1 ) goto session_syntax_error;
17636       if( p->nSession ){
17637         ii = sqlite3session_isempty(pSession->p);
17638         utf8_printf(p->out, "session %s isempty flag = %d\n",
17639                     pSession->zName, ii);
17640       }
17641     }else
17642
17643     /* .session list
17644     ** List all currently open sessions
17645     */
17646     if( strcmp(azCmd[0],"list")==0 ){
17647       for(i=0; i<p->nSession; i++){
17648         utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
17649       }
17650     }else
17651
17652     /* .session open DB NAME
17653     ** Open a new session called NAME on the attached database DB.
17654     ** DB is normally "main".
17655     */
17656     if( strcmp(azCmd[0],"open")==0 ){
17657       char *zName;
17658       if( nCmd!=3 ) goto session_syntax_error;
17659       zName = azCmd[2];
17660       if( zName[0]==0 ) goto session_syntax_error;
17661       for(i=0; i<p->nSession; i++){
17662         if( strcmp(p->aSession[i].zName,zName)==0 ){
17663           utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
17664           goto meta_command_exit;
17665         }
17666       }
17667       if( p->nSession>=ArraySize(p->aSession) ){
17668         raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
17669         goto meta_command_exit;
17670       }
17671       pSession = &p->aSession[p->nSession];
17672       rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
17673       if( rc ){
17674         raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
17675         rc = 0;
17676         goto meta_command_exit;
17677       }
17678       pSession->nFilter = 0;
17679       sqlite3session_table_filter(pSession->p, session_filter, pSession);
17680       p->nSession++;
17681       pSession->zName = sqlite3_mprintf("%s", zName);
17682     }else
17683     /* If no command name matches, show a syntax error */
17684     session_syntax_error:
17685     showHelp(p->out, "session");
17686   }else
17687 #endif
17688
17689 #ifdef SQLITE_DEBUG
17690   /* Undocumented commands for internal testing.  Subject to change
17691   ** without notice. */
17692   if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
17693     if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
17694       int i, v;
17695       for(i=1; i<nArg; i++){
17696         v = booleanValue(azArg[i]);
17697         utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
17698       }
17699     }
17700     if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
17701       int i; sqlite3_int64 v;
17702       for(i=1; i<nArg; i++){
17703         char zBuf[200];
17704         v = integerValue(azArg[i]);
17705         sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
17706         utf8_printf(p->out, "%s", zBuf);
17707       }
17708     }
17709   }else
17710 #endif
17711
17712   if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
17713     int bIsInit = 0;         /* True to initialize the SELFTEST table */
17714     int bVerbose = 0;        /* Verbose output */
17715     int bSelftestExists;     /* True if SELFTEST already exists */
17716     int i, k;                /* Loop counters */
17717     int nTest = 0;           /* Number of tests runs */
17718     int nErr = 0;            /* Number of errors seen */
17719     ShellText str;           /* Answer for a query */
17720     sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
17721
17722     open_db(p,0);
17723     for(i=1; i<nArg; i++){
17724       const char *z = azArg[i];
17725       if( z[0]=='-' && z[1]=='-' ) z++;
17726       if( strcmp(z,"-init")==0 ){
17727         bIsInit = 1;
17728       }else
17729       if( strcmp(z,"-v")==0 ){
17730         bVerbose++;
17731       }else
17732       {
17733         utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
17734                     azArg[i], azArg[0]);
17735         raw_printf(stderr, "Should be one of: --init -v\n");
17736         rc = 1;
17737         goto meta_command_exit;
17738       }
17739     }
17740     if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
17741            != SQLITE_OK ){
17742       bSelftestExists = 0;
17743     }else{
17744       bSelftestExists = 1;
17745     }
17746     if( bIsInit ){
17747       createSelftestTable(p);
17748       bSelftestExists = 1;
17749     }
17750     initText(&str);
17751     appendText(&str, "x", 0);
17752     for(k=bSelftestExists; k>=0; k--){
17753       if( k==1 ){
17754         rc = sqlite3_prepare_v2(p->db,
17755             "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
17756             -1, &pStmt, 0);
17757       }else{
17758         rc = sqlite3_prepare_v2(p->db,
17759           "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
17760           "      (1,'run','PRAGMA integrity_check','ok')",
17761           -1, &pStmt, 0);
17762       }
17763       if( rc ){
17764         raw_printf(stderr, "Error querying the selftest table\n");
17765         rc = 1;
17766         sqlite3_finalize(pStmt);
17767         goto meta_command_exit;
17768       }
17769       for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
17770         int tno = sqlite3_column_int(pStmt, 0);
17771         const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
17772         const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
17773         const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
17774
17775         k = 0;
17776         if( bVerbose>0 ){
17777           char *zQuote = sqlite3_mprintf("%q", zSql);
17778           printf("%d: %s %s\n", tno, zOp, zSql);
17779           sqlite3_free(zQuote);
17780         }
17781         if( strcmp(zOp,"memo")==0 ){
17782           utf8_printf(p->out, "%s\n", zSql);
17783         }else
17784         if( strcmp(zOp,"run")==0 ){
17785           char *zErrMsg = 0;
17786           str.n = 0;
17787           str.z[0] = 0;
17788           rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
17789           nTest++;
17790           if( bVerbose ){
17791             utf8_printf(p->out, "Result: %s\n", str.z);
17792           }
17793           if( rc || zErrMsg ){
17794             nErr++;
17795             rc = 1;
17796             utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
17797             sqlite3_free(zErrMsg);
17798           }else if( strcmp(zAns,str.z)!=0 ){
17799             nErr++;
17800             rc = 1;
17801             utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
17802             utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
17803           }
17804         }else
17805         {
17806           utf8_printf(stderr,
17807             "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
17808           rc = 1;
17809           break;
17810         }
17811       } /* End loop over rows of content from SELFTEST */
17812       sqlite3_finalize(pStmt);
17813     } /* End loop over k */
17814     freeText(&str);
17815     utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
17816   }else
17817
17818   if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
17819     if( nArg<2 || nArg>3 ){
17820       raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
17821       rc = 1;
17822     }
17823     if( nArg>=2 ){
17824       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
17825                        "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
17826     }
17827     if( nArg>=3 ){
17828       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
17829                        "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
17830     }
17831   }else
17832
17833   if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
17834     const char *zLike = 0;   /* Which table to checksum. 0 means everything */
17835     int i;                   /* Loop counter */
17836     int bSchema = 0;         /* Also hash the schema */
17837     int bSeparate = 0;       /* Hash each table separately */
17838     int iSize = 224;         /* Hash algorithm to use */
17839     int bDebug = 0;          /* Only show the query that would have run */
17840     sqlite3_stmt *pStmt;     /* For querying tables names */
17841     char *zSql;              /* SQL to be run */
17842     char *zSep;              /* Separator */
17843     ShellText sSql;          /* Complete SQL for the query to run the hash */
17844     ShellText sQuery;        /* Set of queries used to read all content */
17845     open_db(p, 0);
17846     for(i=1; i<nArg; i++){
17847       const char *z = azArg[i];
17848       if( z[0]=='-' ){
17849         z++;
17850         if( z[0]=='-' ) z++;
17851         if( strcmp(z,"schema")==0 ){
17852           bSchema = 1;
17853         }else
17854         if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
17855          || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
17856         ){
17857           iSize = atoi(&z[5]);
17858         }else
17859         if( strcmp(z,"debug")==0 ){
17860           bDebug = 1;
17861         }else
17862         {
17863           utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
17864                       azArg[i], azArg[0]);
17865           showHelp(p->out, azArg[0]);
17866           rc = 1;
17867           goto meta_command_exit;
17868         }
17869       }else if( zLike ){
17870         raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
17871         rc = 1;
17872         goto meta_command_exit;
17873       }else{
17874         zLike = z;
17875         bSeparate = 1;
17876         if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
17877       }
17878     }
17879     if( bSchema ){
17880       zSql = "SELECT lower(name) FROM sqlite_master"
17881              " WHERE type='table' AND coalesce(rootpage,0)>1"
17882              " UNION ALL SELECT 'sqlite_master'"
17883              " ORDER BY 1 collate nocase";
17884     }else{
17885       zSql = "SELECT lower(name) FROM sqlite_master"
17886              " WHERE type='table' AND coalesce(rootpage,0)>1"
17887              " AND name NOT LIKE 'sqlite_%'"
17888              " ORDER BY 1 collate nocase";
17889     }
17890     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
17891     initText(&sQuery);
17892     initText(&sSql);
17893     appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
17894     zSep = "VALUES(";
17895     while( SQLITE_ROW==sqlite3_step(pStmt) ){
17896       const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
17897       if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
17898       if( strncmp(zTab, "sqlite_",7)!=0 ){
17899         appendText(&sQuery,"SELECT * FROM ", 0);
17900         appendText(&sQuery,zTab,'"');
17901         appendText(&sQuery," NOT INDEXED;", 0);
17902       }else if( strcmp(zTab, "sqlite_master")==0 ){
17903         appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
17904                            " ORDER BY name;", 0);
17905       }else if( strcmp(zTab, "sqlite_sequence")==0 ){
17906         appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
17907                            " ORDER BY name;", 0);
17908       }else if( strcmp(zTab, "sqlite_stat1")==0 ){
17909         appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
17910                            " ORDER BY tbl,idx;", 0);
17911       }else if( strcmp(zTab, "sqlite_stat4")==0 ){
17912         appendText(&sQuery, "SELECT * FROM ", 0);
17913         appendText(&sQuery, zTab, 0);
17914         appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
17915       }
17916       appendText(&sSql, zSep, 0);
17917       appendText(&sSql, sQuery.z, '\'');
17918       sQuery.n = 0;
17919       appendText(&sSql, ",", 0);
17920       appendText(&sSql, zTab, '\'');
17921       zSep = "),(";
17922     }
17923     sqlite3_finalize(pStmt);
17924     if( bSeparate ){
17925       zSql = sqlite3_mprintf(
17926           "%s))"
17927           " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
17928           "   FROM [sha3sum$query]",
17929           sSql.z, iSize);
17930     }else{
17931       zSql = sqlite3_mprintf(
17932           "%s))"
17933           " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
17934           "   FROM [sha3sum$query]",
17935           sSql.z, iSize);
17936     }
17937     freeText(&sQuery);
17938     freeText(&sSql);
17939     if( bDebug ){
17940       utf8_printf(p->out, "%s\n", zSql);
17941     }else{
17942       shell_exec(p, zSql, 0);
17943     }
17944     sqlite3_free(zSql);
17945   }else
17946
17947 #ifndef SQLITE_NOHAVE_SYSTEM
17948   if( c=='s'
17949    && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
17950   ){
17951     char *zCmd;
17952     int i, x;
17953     if( nArg<2 ){
17954       raw_printf(stderr, "Usage: .system COMMAND\n");
17955       rc = 1;
17956       goto meta_command_exit;
17957     }
17958     zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
17959     for(i=2; i<nArg; i++){
17960       zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
17961                              zCmd, azArg[i]);
17962     }
17963     x = system(zCmd);
17964     sqlite3_free(zCmd);
17965     if( x ) raw_printf(stderr, "System command returns %d\n", x);
17966   }else
17967 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
17968
17969   if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
17970     static const char *azBool[] = { "off", "on", "trigger", "full"};
17971     int i;
17972     if( nArg!=1 ){
17973       raw_printf(stderr, "Usage: .show\n");
17974       rc = 1;
17975       goto meta_command_exit;
17976     }
17977     utf8_printf(p->out, "%12.12s: %s\n","echo",
17978                                   azBool[ShellHasFlag(p, SHFLG_Echo)]);
17979     utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
17980     utf8_printf(p->out, "%12.12s: %s\n","explain",
17981          p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
17982     utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
17983     utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
17984     utf8_printf(p->out, "%12.12s: ", "nullvalue");
17985       output_c_string(p->out, p->nullValue);
17986       raw_printf(p->out, "\n");
17987     utf8_printf(p->out,"%12.12s: %s\n","output",
17988             strlen30(p->outfile) ? p->outfile : "stdout");
17989     utf8_printf(p->out,"%12.12s: ", "colseparator");
17990       output_c_string(p->out, p->colSeparator);
17991       raw_printf(p->out, "\n");
17992     utf8_printf(p->out,"%12.12s: ", "rowseparator");
17993       output_c_string(p->out, p->rowSeparator);
17994       raw_printf(p->out, "\n");
17995     utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
17996     utf8_printf(p->out, "%12.12s: ", "width");
17997     for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
17998       raw_printf(p->out, "%d ", p->colWidth[i]);
17999     }
18000     raw_printf(p->out, "\n");
18001     utf8_printf(p->out, "%12.12s: %s\n", "filename",
18002                 p->zDbFilename ? p->zDbFilename : "");
18003   }else
18004
18005   if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
18006     if( nArg==2 ){
18007       p->statsOn = (u8)booleanValue(azArg[1]);
18008     }else if( nArg==1 ){
18009       display_stats(p->db, p, 0);
18010     }else{
18011       raw_printf(stderr, "Usage: .stats ?on|off?\n");
18012       rc = 1;
18013     }
18014   }else
18015
18016   if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
18017    || (c=='i' && (strncmp(azArg[0], "indices", n)==0
18018                  || strncmp(azArg[0], "indexes", n)==0) )
18019   ){
18020     sqlite3_stmt *pStmt;
18021     char **azResult;
18022     int nRow, nAlloc;
18023     int ii;
18024     ShellText s;
18025     initText(&s);
18026     open_db(p, 0);
18027     rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
18028     if( rc ){
18029       sqlite3_finalize(pStmt);
18030       return shellDatabaseError(p->db);
18031     }
18032
18033     if( nArg>2 && c=='i' ){
18034       /* It is an historical accident that the .indexes command shows an error
18035       ** when called with the wrong number of arguments whereas the .tables
18036       ** command does not. */
18037       raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
18038       rc = 1;
18039       sqlite3_finalize(pStmt);
18040       goto meta_command_exit;
18041     }
18042     for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
18043       const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
18044       if( zDbName==0 ) continue;
18045       if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
18046       if( sqlite3_stricmp(zDbName, "main")==0 ){
18047         appendText(&s, "SELECT name FROM ", 0);
18048       }else{
18049         appendText(&s, "SELECT ", 0);
18050         appendText(&s, zDbName, '\'');
18051         appendText(&s, "||'.'||name FROM ", 0);
18052       }
18053       appendText(&s, zDbName, '"');
18054       appendText(&s, ".sqlite_master ", 0);
18055       if( c=='t' ){
18056         appendText(&s," WHERE type IN ('table','view')"
18057                       "   AND name NOT LIKE 'sqlite_%'"
18058                       "   AND name LIKE ?1", 0);
18059       }else{
18060         appendText(&s," WHERE type='index'"
18061                       "   AND tbl_name LIKE ?1", 0);
18062       }
18063     }
18064     rc = sqlite3_finalize(pStmt);
18065     appendText(&s, " ORDER BY 1", 0);
18066     rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
18067     freeText(&s);
18068     if( rc ) return shellDatabaseError(p->db);
18069
18070     /* Run the SQL statement prepared by the above block. Store the results
18071     ** as an array of nul-terminated strings in azResult[].  */
18072     nRow = nAlloc = 0;
18073     azResult = 0;
18074     if( nArg>1 ){
18075       sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
18076     }else{
18077       sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
18078     }
18079     while( sqlite3_step(pStmt)==SQLITE_ROW ){
18080       if( nRow>=nAlloc ){
18081         char **azNew;
18082         int n2 = nAlloc*2 + 10;
18083         azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
18084         if( azNew==0 ) shell_out_of_memory();
18085         nAlloc = n2;
18086         azResult = azNew;
18087       }
18088       azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
18089       if( 0==azResult[nRow] ) shell_out_of_memory();
18090       nRow++;
18091     }
18092     if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
18093       rc = shellDatabaseError(p->db);
18094     }
18095
18096     /* Pretty-print the contents of array azResult[] to the output */
18097     if( rc==0 && nRow>0 ){
18098       int len, maxlen = 0;
18099       int i, j;
18100       int nPrintCol, nPrintRow;
18101       for(i=0; i<nRow; i++){
18102         len = strlen30(azResult[i]);
18103         if( len>maxlen ) maxlen = len;
18104       }
18105       nPrintCol = 80/(maxlen+2);
18106       if( nPrintCol<1 ) nPrintCol = 1;
18107       nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
18108       for(i=0; i<nPrintRow; i++){
18109         for(j=i; j<nRow; j+=nPrintRow){
18110           char *zSp = j<nPrintRow ? "" : "  ";
18111           utf8_printf(p->out, "%s%-*s", zSp, maxlen,
18112                       azResult[j] ? azResult[j]:"");
18113         }
18114         raw_printf(p->out, "\n");
18115       }
18116     }
18117
18118     for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
18119     sqlite3_free(azResult);
18120   }else
18121
18122   /* Begin redirecting output to the file "testcase-out.txt" */
18123   if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
18124     output_reset(p);
18125     p->out = output_file_open("testcase-out.txt", 0);
18126     if( p->out==0 ){
18127       raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
18128     }
18129     if( nArg>=2 ){
18130       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
18131     }else{
18132       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
18133     }
18134   }else
18135
18136 #ifndef SQLITE_UNTESTABLE
18137   if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
18138     static const struct {
18139        const char *zCtrlName;   /* Name of a test-control option */
18140        int ctrlCode;            /* Integer code for that option */
18141        const char *zUsage;      /* Usage notes */
18142     } aCtrl[] = {
18143       { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
18144       { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
18145     /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
18146     /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
18147       { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
18148       { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
18149     /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
18150       { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
18151       { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
18152       { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
18153       { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
18154       { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
18155 #ifdef YYCOVERAGE
18156       { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
18157 #endif
18158       { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
18159       { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
18160       { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
18161       { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
18162     };
18163     int testctrl = -1;
18164     int iCtrl = -1;
18165     int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
18166     int isOk = 0;
18167     int i, n2;
18168     const char *zCmd = 0;
18169
18170     open_db(p, 0);
18171     zCmd = nArg>=2 ? azArg[1] : "help";
18172
18173     /* The argument can optionally begin with "-" or "--" */
18174     if( zCmd[0]=='-' && zCmd[1] ){
18175       zCmd++;
18176       if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
18177     }
18178
18179     /* --help lists all test-controls */
18180     if( strcmp(zCmd,"help")==0 ){
18181       utf8_printf(p->out, "Available test-controls:\n");
18182       for(i=0; i<ArraySize(aCtrl); i++){
18183         utf8_printf(p->out, "  .testctrl %s %s\n",
18184                     aCtrl[i].zCtrlName, aCtrl[i].zUsage);
18185       }
18186       rc = 1;
18187       goto meta_command_exit;
18188     }
18189
18190     /* convert testctrl text option to value. allow any unique prefix
18191     ** of the option name, or a numerical value. */
18192     n2 = strlen30(zCmd);
18193     for(i=0; i<ArraySize(aCtrl); i++){
18194       if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
18195         if( testctrl<0 ){
18196           testctrl = aCtrl[i].ctrlCode;
18197           iCtrl = i;
18198         }else{
18199           utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
18200                               "Use \".testctrl --help\" for help\n", zCmd);
18201           rc = 1;
18202           goto meta_command_exit;
18203         }
18204       }
18205     }
18206     if( testctrl<0 ){
18207       utf8_printf(stderr,"Error: unknown test-control: %s\n"
18208                          "Use \".testctrl --help\" for help\n", zCmd);
18209     }else{
18210       switch(testctrl){
18211
18212         /* sqlite3_test_control(int, db, int) */
18213         case SQLITE_TESTCTRL_OPTIMIZATIONS:
18214           if( nArg==3 ){
18215             int opt = (int)strtol(azArg[2], 0, 0);
18216             rc2 = sqlite3_test_control(testctrl, p->db, opt);
18217             isOk = 3;
18218           }
18219           break;
18220
18221         /* sqlite3_test_control(int) */
18222         case SQLITE_TESTCTRL_PRNG_SAVE:
18223         case SQLITE_TESTCTRL_PRNG_RESTORE:
18224         case SQLITE_TESTCTRL_PRNG_RESET:
18225         case SQLITE_TESTCTRL_BYTEORDER:
18226           if( nArg==2 ){
18227             rc2 = sqlite3_test_control(testctrl);
18228             isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
18229           }
18230           break;
18231
18232         /* sqlite3_test_control(int, uint) */
18233         case SQLITE_TESTCTRL_PENDING_BYTE:
18234           if( nArg==3 ){
18235             unsigned int opt = (unsigned int)integerValue(azArg[2]);
18236             rc2 = sqlite3_test_control(testctrl, opt);
18237             isOk = 3;
18238           }
18239           break;
18240
18241         /* sqlite3_test_control(int, int, sqlite3*) */
18242         case SQLITE_TESTCTRL_PRNG_SEED:
18243           if( nArg==3 || nArg==4 ){
18244             int ii = (int)integerValue(azArg[2]);
18245             sqlite3 *db;
18246             if( ii==0 && strcmp(azArg[2],"random")==0 ){
18247               sqlite3_randomness(sizeof(ii),&ii);
18248               printf("-- random seed: %d\n", ii);
18249             }
18250             if( nArg==3 ){
18251               db = 0;
18252             }else{
18253               db = p->db;
18254               /* Make sure the schema has been loaded */
18255               sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
18256             }
18257             rc2 = sqlite3_test_control(testctrl, ii, db);
18258             isOk = 3;
18259           }
18260           break;
18261
18262         /* sqlite3_test_control(int, int) */
18263         case SQLITE_TESTCTRL_ASSERT:
18264         case SQLITE_TESTCTRL_ALWAYS:
18265           if( nArg==3 ){
18266             int opt = booleanValue(azArg[2]);
18267             rc2 = sqlite3_test_control(testctrl, opt);
18268             isOk = 1;
18269           }
18270           break;
18271
18272         /* sqlite3_test_control(int, int) */
18273         case SQLITE_TESTCTRL_LOCALTIME_FAULT:
18274         case SQLITE_TESTCTRL_NEVER_CORRUPT:
18275           if( nArg==3 ){
18276             int opt = booleanValue(azArg[2]);
18277             rc2 = sqlite3_test_control(testctrl, opt);
18278             isOk = 3;
18279           }
18280           break;
18281
18282         /* sqlite3_test_control(sqlite3*) */
18283         case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
18284           rc2 = sqlite3_test_control(testctrl, p->db);
18285           isOk = 3;
18286           break;
18287
18288         case SQLITE_TESTCTRL_IMPOSTER:
18289           if( nArg==5 ){
18290             rc2 = sqlite3_test_control(testctrl, p->db,
18291                           azArg[2],
18292                           integerValue(azArg[3]),
18293                           integerValue(azArg[4]));
18294             isOk = 3;
18295           }
18296           break;
18297
18298 #ifdef YYCOVERAGE
18299         case SQLITE_TESTCTRL_PARSER_COVERAGE:
18300           if( nArg==2 ){
18301             sqlite3_test_control(testctrl, p->out);
18302             isOk = 3;
18303           }
18304 #endif
18305       }
18306     }
18307     if( isOk==0 && iCtrl>=0 ){
18308       utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
18309       rc = 1;
18310     }else if( isOk==1 ){
18311       raw_printf(p->out, "%d\n", rc2);
18312     }else if( isOk==2 ){
18313       raw_printf(p->out, "0x%08x\n", rc2);
18314     }
18315   }else
18316 #endif /* !defined(SQLITE_UNTESTABLE) */
18317
18318   if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
18319     open_db(p, 0);
18320     sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
18321   }else
18322
18323   if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
18324     if( nArg==2 ){
18325       enableTimer = booleanValue(azArg[1]);
18326       if( enableTimer && !HAS_TIMER ){
18327         raw_printf(stderr, "Error: timer not available on this system.\n");
18328         enableTimer = 0;
18329       }
18330     }else{
18331       raw_printf(stderr, "Usage: .timer on|off\n");
18332       rc = 1;
18333     }
18334   }else
18335
18336 #ifndef SQLITE_OMIT_TRACE
18337   if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
18338     int mType = 0;
18339     int jj;
18340     open_db(p, 0);
18341     for(jj=1; jj<nArg; jj++){
18342       const char *z = azArg[jj];
18343       if( z[0]=='-' ){
18344         if( optionMatch(z, "expanded") ){
18345           p->eTraceType = SHELL_TRACE_EXPANDED;
18346         }
18347 #ifdef SQLITE_ENABLE_NORMALIZE
18348         else if( optionMatch(z, "normalized") ){
18349           p->eTraceType = SHELL_TRACE_NORMALIZED;
18350         }
18351 #endif
18352         else if( optionMatch(z, "plain") ){
18353           p->eTraceType = SHELL_TRACE_PLAIN;
18354         }
18355         else if( optionMatch(z, "profile") ){
18356           mType |= SQLITE_TRACE_PROFILE;
18357         }
18358         else if( optionMatch(z, "row") ){
18359           mType |= SQLITE_TRACE_ROW;
18360         }
18361         else if( optionMatch(z, "stmt") ){
18362           mType |= SQLITE_TRACE_STMT;
18363         }
18364         else if( optionMatch(z, "close") ){
18365           mType |= SQLITE_TRACE_CLOSE;
18366         }
18367         else {
18368           raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
18369           rc = 1;
18370           goto meta_command_exit;
18371         }
18372       }else{
18373         output_file_close(p->traceOut);
18374         p->traceOut = output_file_open(azArg[1], 0);
18375       }
18376     }
18377     if( p->traceOut==0 ){
18378       sqlite3_trace_v2(p->db, 0, 0, 0);
18379     }else{
18380       if( mType==0 ) mType = SQLITE_TRACE_STMT;
18381       sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
18382     }
18383   }else
18384 #endif /* !defined(SQLITE_OMIT_TRACE) */
18385
18386 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
18387   if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
18388     int ii;
18389     int lenOpt;
18390     char *zOpt;
18391     if( nArg<2 ){
18392       raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
18393       rc = 1;
18394       goto meta_command_exit;
18395     }
18396     open_db(p, 0);
18397     zOpt = azArg[1];
18398     if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
18399     lenOpt = (int)strlen(zOpt);
18400     if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
18401       assert( azArg[nArg]==0 );
18402       sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
18403     }else{
18404       for(ii=1; ii<nArg; ii++){
18405         sqlite3_create_module(p->db, azArg[ii], 0, 0);
18406       }
18407     }
18408   }else
18409 #endif
18410
18411 #if SQLITE_USER_AUTHENTICATION
18412   if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
18413     if( nArg<2 ){
18414       raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
18415       rc = 1;
18416       goto meta_command_exit;
18417     }
18418     open_db(p, 0);
18419     if( strcmp(azArg[1],"login")==0 ){
18420       if( nArg!=4 ){
18421         raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
18422         rc = 1;
18423         goto meta_command_exit;
18424       }
18425       rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
18426                                      strlen30(azArg[3]));
18427       if( rc ){
18428         utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
18429         rc = 1;
18430       }
18431     }else if( strcmp(azArg[1],"add")==0 ){
18432       if( nArg!=5 ){
18433         raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
18434         rc = 1;
18435         goto meta_command_exit;
18436       }
18437       rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
18438                             booleanValue(azArg[4]));
18439       if( rc ){
18440         raw_printf(stderr, "User-Add failed: %d\n", rc);
18441         rc = 1;
18442       }
18443     }else if( strcmp(azArg[1],"edit")==0 ){
18444       if( nArg!=5 ){
18445         raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
18446         rc = 1;
18447         goto meta_command_exit;
18448       }
18449       rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
18450                               booleanValue(azArg[4]));
18451       if( rc ){
18452         raw_printf(stderr, "User-Edit failed: %d\n", rc);
18453         rc = 1;
18454       }
18455     }else if( strcmp(azArg[1],"delete")==0 ){
18456       if( nArg!=3 ){
18457         raw_printf(stderr, "Usage: .user delete USER\n");
18458         rc = 1;
18459         goto meta_command_exit;
18460       }
18461       rc = sqlite3_user_delete(p->db, azArg[2]);
18462       if( rc ){
18463         raw_printf(stderr, "User-Delete failed: %d\n", rc);
18464         rc = 1;
18465       }
18466     }else{
18467       raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
18468       rc = 1;
18469       goto meta_command_exit;
18470     }
18471   }else
18472 #endif /* SQLITE_USER_AUTHENTICATION */
18473
18474   if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
18475     utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
18476         sqlite3_libversion(), sqlite3_sourceid());
18477 #if SQLITE_HAVE_ZLIB
18478     utf8_printf(p->out, "zlib version %s\n", zlibVersion());
18479 #endif
18480 #define CTIMEOPT_VAL_(opt) #opt
18481 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
18482 #if defined(__clang__) && defined(__clang_major__)
18483     utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
18484                     CTIMEOPT_VAL(__clang_minor__) "."
18485                     CTIMEOPT_VAL(__clang_patchlevel__) "\n");
18486 #elif defined(_MSC_VER)
18487     utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
18488 #elif defined(__GNUC__) && defined(__VERSION__)
18489     utf8_printf(p->out, "gcc-" __VERSION__ "\n");
18490 #endif
18491   }else
18492
18493   if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
18494     const char *zDbName = nArg==2 ? azArg[1] : "main";
18495     sqlite3_vfs *pVfs = 0;
18496     if( p->db ){
18497       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
18498       if( pVfs ){
18499         utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
18500         raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
18501         raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
18502         raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
18503       }
18504     }
18505   }else
18506
18507   if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
18508     sqlite3_vfs *pVfs;
18509     sqlite3_vfs *pCurrent = 0;
18510     if( p->db ){
18511       sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
18512     }
18513     for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
18514       utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
18515            pVfs==pCurrent ? "  <--- CURRENT" : "");
18516       raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
18517       raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
18518       raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
18519       if( pVfs->pNext ){
18520         raw_printf(p->out, "-----------------------------------\n");
18521       }
18522     }
18523   }else
18524
18525   if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
18526     const char *zDbName = nArg==2 ? azArg[1] : "main";
18527     char *zVfsName = 0;
18528     if( p->db ){
18529       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
18530       if( zVfsName ){
18531         utf8_printf(p->out, "%s\n", zVfsName);
18532         sqlite3_free(zVfsName);
18533       }
18534     }
18535   }else
18536
18537 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
18538   if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
18539     sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
18540   }else
18541 #endif
18542
18543   if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
18544     int j;
18545     assert( nArg<=ArraySize(azArg) );
18546     for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
18547       p->colWidth[j-1] = (int)integerValue(azArg[j]);
18548     }
18549   }else
18550
18551   {
18552     utf8_printf(stderr, "Error: unknown command or invalid arguments: "
18553       " \"%s\". Enter \".help\" for help\n", azArg[0]);
18554     rc = 1;
18555   }
18556
18557 meta_command_exit:
18558   if( p->outCount ){
18559     p->outCount--;
18560     if( p->outCount==0 ) output_reset(p);
18561   }
18562   return rc;
18563 }
18564
18565 /*
18566 ** Return TRUE if a semicolon occurs anywhere in the first N characters
18567 ** of string z[].
18568 */
18569 static int line_contains_semicolon(const char *z, int N){
18570   int i;
18571   for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
18572   return 0;
18573 }
18574
18575 /*
18576 ** Test to see if a line consists entirely of whitespace.
18577 */
18578 static int _all_whitespace(const char *z){
18579   for(; *z; z++){
18580     if( IsSpace(z[0]) ) continue;
18581     if( *z=='/' && z[1]=='*' ){
18582       z += 2;
18583       while( *z && (*z!='*' || z[1]!='/') ){ z++; }
18584       if( *z==0 ) return 0;
18585       z++;
18586       continue;
18587     }
18588     if( *z=='-' && z[1]=='-' ){
18589       z += 2;
18590       while( *z && *z!='\n' ){ z++; }
18591       if( *z==0 ) return 1;
18592       continue;
18593     }
18594     return 0;
18595   }
18596   return 1;
18597 }
18598
18599 /*
18600 ** Return TRUE if the line typed in is an SQL command terminator other
18601 ** than a semi-colon.  The SQL Server style "go" command is understood
18602 ** as is the Oracle "/".
18603 */
18604 static int line_is_command_terminator(const char *zLine){
18605   while( IsSpace(zLine[0]) ){ zLine++; };
18606   if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
18607     return 1;  /* Oracle */
18608   }
18609   if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
18610          && _all_whitespace(&zLine[2]) ){
18611     return 1;  /* SQL Server */
18612   }
18613   return 0;
18614 }
18615
18616 /*
18617 ** We need a default sqlite3_complete() implementation to use in case
18618 ** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
18619 ** any arbitrary text is a complete SQL statement.  This is not very
18620 ** user-friendly, but it does seem to work.
18621 */
18622 #ifdef SQLITE_OMIT_COMPLETE
18623 #define sqlite3_complete(x) 1
18624 #endif
18625
18626 /*
18627 ** Return true if zSql is a complete SQL statement.  Return false if it
18628 ** ends in the middle of a string literal or C-style comment.
18629 */
18630 static int line_is_complete(char *zSql, int nSql){
18631   int rc;
18632   if( zSql==0 ) return 1;
18633   zSql[nSql] = ';';
18634   zSql[nSql+1] = 0;
18635   rc = sqlite3_complete(zSql);
18636   zSql[nSql] = 0;
18637   return rc;
18638 }
18639
18640 /*
18641 ** Run a single line of SQL.  Return the number of errors.
18642 */
18643 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
18644   int rc;
18645   char *zErrMsg = 0;
18646
18647   open_db(p, 0);
18648   if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
18649   if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
18650   BEGIN_TIMER;
18651   rc = shell_exec(p, zSql, &zErrMsg);
18652   END_TIMER;
18653   if( rc || zErrMsg ){
18654     char zPrefix[100];
18655     if( in!=0 || !stdin_is_interactive ){
18656       sqlite3_snprintf(sizeof(zPrefix), zPrefix,
18657                        "Error: near line %d:", startline);
18658     }else{
18659       sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
18660     }
18661     if( zErrMsg!=0 ){
18662       utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
18663       sqlite3_free(zErrMsg);
18664       zErrMsg = 0;
18665     }else{
18666       utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
18667     }
18668     return 1;
18669   }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
18670     raw_printf(p->out, "changes: %3d   total_changes: %d\n",
18671             sqlite3_changes(p->db), sqlite3_total_changes(p->db));
18672   }
18673   return 0;
18674 }
18675
18676
18677 /*
18678 ** Read input from *in and process it.  If *in==0 then input
18679 ** is interactive - the user is typing it it.  Otherwise, input
18680 ** is coming from a file or device.  A prompt is issued and history
18681 ** is saved only if input is interactive.  An interrupt signal will
18682 ** cause this routine to exit immediately, unless input is interactive.
18683 **
18684 ** Return the number of errors.
18685 */
18686 static int process_input(ShellState *p){
18687   char *zLine = 0;          /* A single input line */
18688   char *zSql = 0;           /* Accumulated SQL text */
18689   int nLine;                /* Length of current line */
18690   int nSql = 0;             /* Bytes of zSql[] used */
18691   int nAlloc = 0;           /* Allocated zSql[] space */
18692   int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
18693   int rc;                   /* Error code */
18694   int errCnt = 0;           /* Number of errors seen */
18695   int startline = 0;        /* Line number for start of current input */
18696
18697   p->lineno = 0;
18698   while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
18699     fflush(p->out);
18700     zLine = one_input_line(p->in, zLine, nSql>0);
18701     if( zLine==0 ){
18702       /* End of input */
18703       if( p->in==0 && stdin_is_interactive ) printf("\n");
18704       break;
18705     }
18706     if( seenInterrupt ){
18707       if( p->in!=0 ) break;
18708       seenInterrupt = 0;
18709     }
18710     p->lineno++;
18711     if( nSql==0 && _all_whitespace(zLine) ){
18712       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
18713       continue;
18714     }
18715     if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
18716       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
18717       if( zLine[0]=='.' ){
18718         rc = do_meta_command(zLine, p);
18719         if( rc==2 ){ /* exit requested */
18720           break;
18721         }else if( rc ){
18722           errCnt++;
18723         }
18724       }
18725       continue;
18726     }
18727     if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
18728       memcpy(zLine,";",2);
18729     }
18730     nLine = strlen30(zLine);
18731     if( nSql+nLine+2>=nAlloc ){
18732       nAlloc = nSql+nLine+100;
18733       zSql = realloc(zSql, nAlloc);
18734       if( zSql==0 ) shell_out_of_memory();
18735     }
18736     nSqlPrior = nSql;
18737     if( nSql==0 ){
18738       int i;
18739       for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
18740       assert( nAlloc>0 && zSql!=0 );
18741       memcpy(zSql, zLine+i, nLine+1-i);
18742       startline = p->lineno;
18743       nSql = nLine-i;
18744     }else{
18745       zSql[nSql++] = '\n';
18746       memcpy(zSql+nSql, zLine, nLine+1);
18747       nSql += nLine;
18748     }
18749     if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
18750                 && sqlite3_complete(zSql) ){
18751       errCnt += runOneSqlLine(p, zSql, p->in, startline);
18752       nSql = 0;
18753       if( p->outCount ){
18754         output_reset(p);
18755         p->outCount = 0;
18756       }else{
18757         clearTempFile(p);
18758       }
18759     }else if( nSql && _all_whitespace(zSql) ){
18760       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
18761       nSql = 0;
18762     }
18763   }
18764   if( nSql && !_all_whitespace(zSql) ){
18765     errCnt += runOneSqlLine(p, zSql, p->in, startline);
18766   }
18767   free(zSql);
18768   free(zLine);
18769   return errCnt>0;
18770 }
18771
18772 /*
18773 ** Return a pathname which is the user's home directory.  A
18774 ** 0 return indicates an error of some kind.
18775 */
18776 static char *find_home_dir(int clearFlag){
18777   static char *home_dir = NULL;
18778   if( clearFlag ){
18779     free(home_dir);
18780     home_dir = 0;
18781     return 0;
18782   }
18783   if( home_dir ) return home_dir;
18784
18785 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
18786      && !defined(__RTP__) && !defined(_WRS_KERNEL)
18787   {
18788     struct passwd *pwent;
18789     uid_t uid = getuid();
18790     if( (pwent=getpwuid(uid)) != NULL) {
18791       home_dir = pwent->pw_dir;
18792     }
18793   }
18794 #endif
18795
18796 #if defined(_WIN32_WCE)
18797   /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
18798    */
18799   home_dir = "/";
18800 #else
18801
18802 #if defined(_WIN32) || defined(WIN32)
18803   if (!home_dir) {
18804     home_dir = getenv("USERPROFILE");
18805   }
18806 #endif
18807
18808   if (!home_dir) {
18809     home_dir = getenv("HOME");
18810   }
18811
18812 #if defined(_WIN32) || defined(WIN32)
18813   if (!home_dir) {
18814     char *zDrive, *zPath;
18815     int n;
18816     zDrive = getenv("HOMEDRIVE");
18817     zPath = getenv("HOMEPATH");
18818     if( zDrive && zPath ){
18819       n = strlen30(zDrive) + strlen30(zPath) + 1;
18820       home_dir = malloc( n );
18821       if( home_dir==0 ) return 0;
18822       sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
18823       return home_dir;
18824     }
18825     home_dir = "c:\\";
18826   }
18827 #endif
18828
18829 #endif /* !_WIN32_WCE */
18830
18831   if( home_dir ){
18832     int n = strlen30(home_dir) + 1;
18833     char *z = malloc( n );
18834     if( z ) memcpy(z, home_dir, n);
18835     home_dir = z;
18836   }
18837
18838   return home_dir;
18839 }
18840
18841 /*
18842 ** Read input from the file given by sqliterc_override.  Or if that
18843 ** parameter is NULL, take input from ~/.sqliterc
18844 **
18845 ** Returns the number of errors.
18846 */
18847 static void process_sqliterc(
18848   ShellState *p,                  /* Configuration data */
18849   const char *sqliterc_override   /* Name of config file. NULL to use default */
18850 ){
18851   char *home_dir = NULL;
18852   const char *sqliterc = sqliterc_override;
18853   char *zBuf = 0;
18854   FILE *inSaved = p->in;
18855   int savedLineno = p->lineno;
18856
18857   if (sqliterc == NULL) {
18858     home_dir = find_home_dir(0);
18859     if( home_dir==0 ){
18860       raw_printf(stderr, "-- warning: cannot find home directory;"
18861                       " cannot read ~/.sqliterc\n");
18862       return;
18863     }
18864     zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
18865     sqliterc = zBuf;
18866   }
18867   p->in = fopen(sqliterc,"rb");
18868   if( p->in ){
18869     if( stdin_is_interactive ){
18870       utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
18871     }
18872     process_input(p);
18873     fclose(p->in);
18874   }
18875   p->in = inSaved;
18876   p->lineno = savedLineno;
18877   sqlite3_free(zBuf);
18878 }
18879
18880 /*
18881 ** Show available command line options
18882 */
18883 static const char zOptions[] =
18884 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
18885   "   -A ARGS...           run \".archive ARGS\" and exit\n"
18886 #endif
18887   "   -append              append the database to the end of the file\n"
18888   "   -ascii               set output mode to 'ascii'\n"
18889   "   -bail                stop after hitting an error\n"
18890   "   -batch               force batch I/O\n"
18891   "   -column              set output mode to 'column'\n"
18892   "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
18893   "   -csv                 set output mode to 'csv'\n"
18894 #if defined(SQLITE_ENABLE_DESERIALIZE)
18895   "   -deserialize         open the database using sqlite3_deserialize()\n"
18896 #endif
18897   "   -echo                print commands before execution\n"
18898   "   -init FILENAME       read/process named file\n"
18899   "   -[no]header          turn headers on or off\n"
18900 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
18901   "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
18902 #endif
18903   "   -help                show this message\n"
18904   "   -html                set output mode to HTML\n"
18905   "   -interactive         force interactive I/O\n"
18906   "   -line                set output mode to 'line'\n"
18907   "   -list                set output mode to 'list'\n"
18908   "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
18909 #if defined(SQLITE_ENABLE_DESERIALIZE)
18910   "   -maxsize N           maximum size for a --deserialize database\n"
18911 #endif
18912   "   -memtrace            trace all memory allocations and deallocations\n"
18913   "   -mmap N              default mmap size set to N\n"
18914 #ifdef SQLITE_ENABLE_MULTIPLEX
18915   "   -multiplex           enable the multiplexor VFS\n"
18916 #endif
18917   "   -newline SEP         set output row separator. Default: '\\n'\n"
18918   "   -nofollow            refuse to open symbolic links to database files\n"
18919   "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
18920   "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
18921   "   -quote               set output mode to 'quote'\n"
18922   "   -readonly            open the database read-only\n"
18923   "   -separator SEP       set output column separator. Default: '|'\n"
18924 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
18925   "   -sorterref SIZE      sorter references threshold size\n"
18926 #endif
18927   "   -stats               print memory stats before each finalize\n"
18928   "   -version             show SQLite version\n"
18929   "   -vfs NAME            use NAME as the default VFS\n"
18930 #ifdef SQLITE_ENABLE_VFSTRACE
18931   "   -vfstrace            enable tracing of all VFS calls\n"
18932 #endif
18933 #ifdef SQLITE_HAVE_ZLIB
18934   "   -zip                 open the file as a ZIP Archive\n"
18935 #endif
18936 ;
18937 static void usage(int showDetail){
18938   utf8_printf(stderr,
18939       "Usage: %s [OPTIONS] FILENAME [SQL]\n"
18940       "FILENAME is the name of an SQLite database. A new database is created\n"
18941       "if the file does not previously exist.\n", Argv0);
18942   if( showDetail ){
18943     utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
18944   }else{
18945     raw_printf(stderr, "Use the -help option for additional information\n");
18946   }
18947   exit(1);
18948 }
18949
18950 /*
18951 ** Internal check:  Verify that the SQLite is uninitialized.  Print a
18952 ** error message if it is initialized.
18953 */
18954 static void verify_uninitialized(void){
18955   if( sqlite3_config(-1)==SQLITE_MISUSE ){
18956     utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
18957                         " initialization.\n");
18958   }
18959 }
18960
18961 /*
18962 ** Initialize the state information in data
18963 */
18964 static void main_init(ShellState *data) {
18965   memset(data, 0, sizeof(*data));
18966   data->normalMode = data->cMode = data->mode = MODE_List;
18967   data->autoExplain = 1;
18968   memcpy(data->colSeparator,SEP_Column, 2);
18969   memcpy(data->rowSeparator,SEP_Row, 2);
18970   data->showHeader = 0;
18971   data->shellFlgs = SHFLG_Lookaside;
18972   verify_uninitialized();
18973   sqlite3_config(SQLITE_CONFIG_URI, 1);
18974   sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
18975   sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
18976   sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
18977   sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
18978 }
18979
18980 /*
18981 ** Output text to the console in a font that attracts extra attention.
18982 */
18983 #ifdef _WIN32
18984 static void printBold(const char *zText){
18985 #if !SQLITE_OS_WINRT
18986   HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
18987   CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
18988   GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
18989   SetConsoleTextAttribute(out,
18990          FOREGROUND_RED|FOREGROUND_INTENSITY
18991   );
18992 #endif
18993   printf("%s", zText);
18994 #if !SQLITE_OS_WINRT
18995   SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
18996 #endif
18997 }
18998 #else
18999 static void printBold(const char *zText){
19000   printf("\033[1m%s\033[0m", zText);
19001 }
19002 #endif
19003
19004 /*
19005 ** Get the argument to an --option.  Throw an error and die if no argument
19006 ** is available.
19007 */
19008 static char *cmdline_option_value(int argc, char **argv, int i){
19009   if( i==argc ){
19010     utf8_printf(stderr, "%s: Error: missing argument to %s\n",
19011             argv[0], argv[argc-1]);
19012     exit(1);
19013   }
19014   return argv[i];
19015 }
19016
19017 #ifndef SQLITE_SHELL_IS_UTF8
19018 #  if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
19019 #    define SQLITE_SHELL_IS_UTF8          (0)
19020 #  else
19021 #    define SQLITE_SHELL_IS_UTF8          (1)
19022 #  endif
19023 #endif
19024
19025 #if SQLITE_SHELL_IS_UTF8
19026 int SQLITE_CDECL main(int argc, char **argv){
19027 #else
19028 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
19029   char **argv;
19030 #endif
19031   char *zErrMsg = 0;
19032   ShellState data;
19033   const char *zInitFile = 0;
19034   int i;
19035   int rc = 0;
19036   int warnInmemoryDb = 0;
19037   int readStdin = 1;
19038   int nCmd = 0;
19039   char **azCmd = 0;
19040   const char *zVfs = 0;           /* Value of -vfs command-line option */
19041 #if !SQLITE_SHELL_IS_UTF8
19042   char **argvToFree = 0;
19043   int argcToFree = 0;
19044 #endif
19045
19046   setBinaryMode(stdin, 0);
19047   setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
19048   stdin_is_interactive = isatty(0);
19049   stdout_is_console = isatty(1);
19050
19051 #ifdef SQLITE_DEBUG
19052   registerOomSimulator();
19053 #endif
19054
19055 #if !defined(_WIN32_WCE)
19056   if( getenv("SQLITE_DEBUG_BREAK") ){
19057     if( isatty(0) && isatty(2) ){
19058       fprintf(stderr,
19059           "attach debugger to process %d and press any key to continue.\n",
19060           GETPID());
19061       fgetc(stdin);
19062     }else{
19063 #if defined(_WIN32) || defined(WIN32)
19064 #if SQLITE_OS_WINRT
19065       __debugbreak();
19066 #else
19067       DebugBreak();
19068 #endif
19069 #elif defined(SIGTRAP)
19070       raise(SIGTRAP);
19071 #endif
19072     }
19073   }
19074 #endif
19075
19076 #if USE_SYSTEM_SQLITE+0!=1
19077   if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
19078     utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
19079             sqlite3_sourceid(), SQLITE_SOURCE_ID);
19080     exit(1);
19081   }
19082 #endif
19083   main_init(&data);
19084
19085   /* On Windows, we must translate command-line arguments into UTF-8.
19086   ** The SQLite memory allocator subsystem has to be enabled in order to
19087   ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
19088   ** subsequent sqlite3_config() calls will work.  So copy all results into
19089   ** memory that does not come from the SQLite memory allocator.
19090   */
19091 #if !SQLITE_SHELL_IS_UTF8
19092   sqlite3_initialize();
19093   argvToFree = malloc(sizeof(argv[0])*argc*2);
19094   argcToFree = argc;
19095   argv = argvToFree + argc;
19096   if( argv==0 ) shell_out_of_memory();
19097   for(i=0; i<argc; i++){
19098     char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
19099     int n;
19100     if( z==0 ) shell_out_of_memory();
19101     n = (int)strlen(z);
19102     argv[i] = malloc( n+1 );
19103     if( argv[i]==0 ) shell_out_of_memory();
19104     memcpy(argv[i], z, n+1);
19105     argvToFree[i] = argv[i];
19106     sqlite3_free(z);
19107   }
19108   sqlite3_shutdown();
19109 #endif
19110
19111   assert( argc>=1 && argv && argv[0] );
19112   Argv0 = argv[0];
19113
19114   /* Make sure we have a valid signal handler early, before anything
19115   ** else is done.
19116   */
19117 #ifdef SIGINT
19118   signal(SIGINT, interrupt_handler);
19119 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
19120   SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
19121 #endif
19122
19123 #ifdef SQLITE_SHELL_DBNAME_PROC
19124   {
19125     /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
19126     ** of a C-function that will provide the name of the database file.  Use
19127     ** this compile-time option to embed this shell program in larger
19128     ** applications. */
19129     extern void SQLITE_SHELL_DBNAME_PROC(const char**);
19130     SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
19131     warnInmemoryDb = 0;
19132   }
19133 #endif
19134
19135   /* Do an initial pass through the command-line argument to locate
19136   ** the name of the database file, the name of the initialization file,
19137   ** the size of the alternative malloc heap,
19138   ** and the first command to execute.
19139   */
19140   verify_uninitialized();
19141   for(i=1; i<argc; i++){
19142     char *z;
19143     z = argv[i];
19144     if( z[0]!='-' ){
19145       if( data.zDbFilename==0 ){
19146         data.zDbFilename = z;
19147       }else{
19148         /* Excesss arguments are interpreted as SQL (or dot-commands) and
19149         ** mean that nothing is read from stdin */
19150         readStdin = 0;
19151         nCmd++;
19152         azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
19153         if( azCmd==0 ) shell_out_of_memory();
19154         azCmd[nCmd-1] = z;
19155       }
19156     }
19157     if( z[1]=='-' ) z++;
19158     if( strcmp(z,"-separator")==0
19159      || strcmp(z,"-nullvalue")==0
19160      || strcmp(z,"-newline")==0
19161      || strcmp(z,"-cmd")==0
19162     ){
19163       (void)cmdline_option_value(argc, argv, ++i);
19164     }else if( strcmp(z,"-init")==0 ){
19165       zInitFile = cmdline_option_value(argc, argv, ++i);
19166     }else if( strcmp(z,"-batch")==0 ){
19167       /* Need to check for batch mode here to so we can avoid printing
19168       ** informational messages (like from process_sqliterc) before
19169       ** we do the actual processing of arguments later in a second pass.
19170       */
19171       stdin_is_interactive = 0;
19172     }else if( strcmp(z,"-heap")==0 ){
19173 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
19174       const char *zSize;
19175       sqlite3_int64 szHeap;
19176
19177       zSize = cmdline_option_value(argc, argv, ++i);
19178       szHeap = integerValue(zSize);
19179       if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
19180       sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
19181 #else
19182       (void)cmdline_option_value(argc, argv, ++i);
19183 #endif
19184     }else if( strcmp(z,"-pagecache")==0 ){
19185       int n, sz;
19186       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
19187       if( sz>70000 ) sz = 70000;
19188       if( sz<0 ) sz = 0;
19189       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
19190       sqlite3_config(SQLITE_CONFIG_PAGECACHE,
19191                     (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
19192       data.shellFlgs |= SHFLG_Pagecache;
19193     }else if( strcmp(z,"-lookaside")==0 ){
19194       int n, sz;
19195       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
19196       if( sz<0 ) sz = 0;
19197       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
19198       if( n<0 ) n = 0;
19199       sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
19200       if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
19201 #ifdef SQLITE_ENABLE_VFSTRACE
19202     }else if( strcmp(z,"-vfstrace")==0 ){
19203       extern int vfstrace_register(
19204          const char *zTraceName,
19205          const char *zOldVfsName,
19206          int (*xOut)(const char*,void*),
19207          void *pOutArg,
19208          int makeDefault
19209       );
19210       vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
19211 #endif
19212 #ifdef SQLITE_ENABLE_MULTIPLEX
19213     }else if( strcmp(z,"-multiplex")==0 ){
19214       extern int sqlite3_multiple_initialize(const char*,int);
19215       sqlite3_multiplex_initialize(0, 1);
19216 #endif
19217     }else if( strcmp(z,"-mmap")==0 ){
19218       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
19219       sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
19220 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
19221     }else if( strcmp(z,"-sorterref")==0 ){
19222       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
19223       sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
19224 #endif
19225     }else if( strcmp(z,"-vfs")==0 ){
19226       zVfs = cmdline_option_value(argc, argv, ++i);
19227 #ifdef SQLITE_HAVE_ZLIB
19228     }else if( strcmp(z,"-zip")==0 ){
19229       data.openMode = SHELL_OPEN_ZIPFILE;
19230 #endif
19231     }else if( strcmp(z,"-append")==0 ){
19232       data.openMode = SHELL_OPEN_APPENDVFS;
19233 #ifdef SQLITE_ENABLE_DESERIALIZE
19234     }else if( strcmp(z,"-deserialize")==0 ){
19235       data.openMode = SHELL_OPEN_DESERIALIZE;
19236     }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
19237       data.szMax = integerValue(argv[++i]);
19238 #endif
19239     }else if( strcmp(z,"-readonly")==0 ){
19240       data.openMode = SHELL_OPEN_READONLY;
19241     }else if( strcmp(z,"-nofollow")==0 ){
19242       data.openFlags = SQLITE_OPEN_NOFOLLOW;
19243 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
19244     }else if( strncmp(z, "-A",2)==0 ){
19245       /* All remaining command-line arguments are passed to the ".archive"
19246       ** command, so ignore them */
19247       break;
19248 #endif
19249     }else if( strcmp(z, "-memtrace")==0 ){
19250       sqlite3MemTraceActivate(stderr);
19251     }
19252   }
19253   verify_uninitialized();
19254
19255
19256 #ifdef SQLITE_SHELL_INIT_PROC
19257   {
19258     /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
19259     ** of a C-function that will perform initialization actions on SQLite that
19260     ** occur just before or after sqlite3_initialize(). Use this compile-time
19261     ** option to embed this shell program in larger applications. */
19262     extern void SQLITE_SHELL_INIT_PROC(void);
19263     SQLITE_SHELL_INIT_PROC();
19264   }
19265 #else
19266   /* All the sqlite3_config() calls have now been made. So it is safe
19267   ** to call sqlite3_initialize() and process any command line -vfs option. */
19268   sqlite3_initialize();
19269 #endif
19270
19271   if( zVfs ){
19272     sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
19273     if( pVfs ){
19274       sqlite3_vfs_register(pVfs, 1);
19275     }else{
19276       utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
19277       exit(1);
19278     }
19279   }
19280
19281   if( data.zDbFilename==0 ){
19282 #ifndef SQLITE_OMIT_MEMORYDB
19283     data.zDbFilename = ":memory:";
19284     warnInmemoryDb = argc==1;
19285 #else
19286     utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
19287     return 1;
19288 #endif
19289   }
19290   data.out = stdout;
19291   sqlite3_appendvfs_init(0,0,0);
19292
19293   /* Go ahead and open the database file if it already exists.  If the
19294   ** file does not exist, delay opening it.  This prevents empty database
19295   ** files from being created if a user mistypes the database name argument
19296   ** to the sqlite command-line tool.
19297   */
19298   if( access(data.zDbFilename, 0)==0 ){
19299     open_db(&data, 0);
19300   }
19301
19302   /* Process the initialization file if there is one.  If no -init option
19303   ** is given on the command line, look for a file named ~/.sqliterc and
19304   ** try to process it.
19305   */
19306   process_sqliterc(&data,zInitFile);
19307
19308   /* Make a second pass through the command-line argument and set
19309   ** options.  This second pass is delayed until after the initialization
19310   ** file is processed so that the command-line arguments will override
19311   ** settings in the initialization file.
19312   */
19313   for(i=1; i<argc; i++){
19314     char *z = argv[i];
19315     if( z[0]!='-' ) continue;
19316     if( z[1]=='-' ){ z++; }
19317     if( strcmp(z,"-init")==0 ){
19318       i++;
19319     }else if( strcmp(z,"-html")==0 ){
19320       data.mode = MODE_Html;
19321     }else if( strcmp(z,"-list")==0 ){
19322       data.mode = MODE_List;
19323     }else if( strcmp(z,"-quote")==0 ){
19324       data.mode = MODE_Quote;
19325     }else if( strcmp(z,"-line")==0 ){
19326       data.mode = MODE_Line;
19327     }else if( strcmp(z,"-column")==0 ){
19328       data.mode = MODE_Column;
19329     }else if( strcmp(z,"-csv")==0 ){
19330       data.mode = MODE_Csv;
19331       memcpy(data.colSeparator,",",2);
19332 #ifdef SQLITE_HAVE_ZLIB
19333     }else if( strcmp(z,"-zip")==0 ){
19334       data.openMode = SHELL_OPEN_ZIPFILE;
19335 #endif
19336     }else if( strcmp(z,"-append")==0 ){
19337       data.openMode = SHELL_OPEN_APPENDVFS;
19338 #ifdef SQLITE_ENABLE_DESERIALIZE
19339     }else if( strcmp(z,"-deserialize")==0 ){
19340       data.openMode = SHELL_OPEN_DESERIALIZE;
19341     }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
19342       data.szMax = integerValue(argv[++i]);
19343 #endif
19344     }else if( strcmp(z,"-readonly")==0 ){
19345       data.openMode = SHELL_OPEN_READONLY;
19346     }else if( strcmp(z,"-nofollow")==0 ){
19347       data.openFlags |= SQLITE_OPEN_NOFOLLOW;
19348     }else if( strcmp(z,"-ascii")==0 ){
19349       data.mode = MODE_Ascii;
19350       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
19351                        SEP_Unit);
19352       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
19353                        SEP_Record);
19354     }else if( strcmp(z,"-separator")==0 ){
19355       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
19356                        "%s",cmdline_option_value(argc,argv,++i));
19357     }else if( strcmp(z,"-newline")==0 ){
19358       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
19359                        "%s",cmdline_option_value(argc,argv,++i));
19360     }else if( strcmp(z,"-nullvalue")==0 ){
19361       sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
19362                        "%s",cmdline_option_value(argc,argv,++i));
19363     }else if( strcmp(z,"-header")==0 ){
19364       data.showHeader = 1;
19365     }else if( strcmp(z,"-noheader")==0 ){
19366       data.showHeader = 0;
19367     }else if( strcmp(z,"-echo")==0 ){
19368       ShellSetFlag(&data, SHFLG_Echo);
19369     }else if( strcmp(z,"-eqp")==0 ){
19370       data.autoEQP = AUTOEQP_on;
19371     }else if( strcmp(z,"-eqpfull")==0 ){
19372       data.autoEQP = AUTOEQP_full;
19373     }else if( strcmp(z,"-stats")==0 ){
19374       data.statsOn = 1;
19375     }else if( strcmp(z,"-scanstats")==0 ){
19376       data.scanstatsOn = 1;
19377     }else if( strcmp(z,"-backslash")==0 ){
19378       /* Undocumented command-line option: -backslash
19379       ** Causes C-style backslash escapes to be evaluated in SQL statements
19380       ** prior to sending the SQL into SQLite.  Useful for injecting
19381       ** crazy bytes in the middle of SQL statements for testing and debugging.
19382       */
19383       ShellSetFlag(&data, SHFLG_Backslash);
19384     }else if( strcmp(z,"-bail")==0 ){
19385       bail_on_error = 1;
19386     }else if( strcmp(z,"-version")==0 ){
19387       printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
19388       return 0;
19389     }else if( strcmp(z,"-interactive")==0 ){
19390       stdin_is_interactive = 1;
19391     }else if( strcmp(z,"-batch")==0 ){
19392       stdin_is_interactive = 0;
19393     }else if( strcmp(z,"-heap")==0 ){
19394       i++;
19395     }else if( strcmp(z,"-pagecache")==0 ){
19396       i+=2;
19397     }else if( strcmp(z,"-lookaside")==0 ){
19398       i+=2;
19399     }else if( strcmp(z,"-mmap")==0 ){
19400       i++;
19401     }else if( strcmp(z,"-memtrace")==0 ){
19402       i++;
19403 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
19404     }else if( strcmp(z,"-sorterref")==0 ){
19405       i++;
19406 #endif
19407     }else if( strcmp(z,"-vfs")==0 ){
19408       i++;
19409 #ifdef SQLITE_ENABLE_VFSTRACE
19410     }else if( strcmp(z,"-vfstrace")==0 ){
19411       i++;
19412 #endif
19413 #ifdef SQLITE_ENABLE_MULTIPLEX
19414     }else if( strcmp(z,"-multiplex")==0 ){
19415       i++;
19416 #endif
19417     }else if( strcmp(z,"-help")==0 ){
19418       usage(1);
19419     }else if( strcmp(z,"-cmd")==0 ){
19420       /* Run commands that follow -cmd first and separately from commands
19421       ** that simply appear on the command-line.  This seems goofy.  It would
19422       ** be better if all commands ran in the order that they appear.  But
19423       ** we retain the goofy behavior for historical compatibility. */
19424       if( i==argc-1 ) break;
19425       z = cmdline_option_value(argc,argv,++i);
19426       if( z[0]=='.' ){
19427         rc = do_meta_command(z, &data);
19428         if( rc && bail_on_error ) return rc==2 ? 0 : rc;
19429       }else{
19430         open_db(&data, 0);
19431         rc = shell_exec(&data, z, &zErrMsg);
19432         if( zErrMsg!=0 ){
19433           utf8_printf(stderr,"Error: %s\n", zErrMsg);
19434           if( bail_on_error ) return rc!=0 ? rc : 1;
19435         }else if( rc!=0 ){
19436           utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
19437           if( bail_on_error ) return rc;
19438         }
19439       }
19440 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
19441     }else if( strncmp(z, "-A", 2)==0 ){
19442       if( nCmd>0 ){
19443         utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
19444                             " with \"%s\"\n", z);
19445         return 1;
19446       }
19447       open_db(&data, OPEN_DB_ZIPFILE);
19448       if( z[2] ){
19449         argv[i] = &z[2];
19450         arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
19451       }else{
19452         arDotCommand(&data, 1, argv+i, argc-i);
19453       }
19454       readStdin = 0;
19455       break;
19456 #endif
19457     }else{
19458       utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
19459       raw_printf(stderr,"Use -help for a list of options.\n");
19460       return 1;
19461     }
19462     data.cMode = data.mode;
19463   }
19464
19465   if( !readStdin ){
19466     /* Run all arguments that do not begin with '-' as if they were separate
19467     ** command-line inputs, except for the argToSkip argument which contains
19468     ** the database filename.
19469     */
19470     for(i=0; i<nCmd; i++){
19471       if( azCmd[i][0]=='.' ){
19472         rc = do_meta_command(azCmd[i], &data);
19473         if( rc ) return rc==2 ? 0 : rc;
19474       }else{
19475         open_db(&data, 0);
19476         rc = shell_exec(&data, azCmd[i], &zErrMsg);
19477         if( zErrMsg!=0 ){
19478           utf8_printf(stderr,"Error: %s\n", zErrMsg);
19479           return rc!=0 ? rc : 1;
19480         }else if( rc!=0 ){
19481           utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
19482           return rc;
19483         }
19484       }
19485     }
19486     free(azCmd);
19487   }else{
19488     /* Run commands received from standard input
19489     */
19490     if( stdin_is_interactive ){
19491       char *zHome;
19492       char *zHistory;
19493       int nHistory;
19494       printf(
19495         "SQLite version %s %.19s\n" /*extra-version-info*/
19496         "Enter \".help\" for usage hints.\n",
19497         sqlite3_libversion(), sqlite3_sourceid()
19498       );
19499       if( warnInmemoryDb ){
19500         printf("Connected to a ");
19501         printBold("transient in-memory database");
19502         printf(".\nUse \".open FILENAME\" to reopen on a "
19503                "persistent database.\n");
19504       }
19505       zHistory = getenv("SQLITE_HISTORY");
19506       if( zHistory ){
19507         zHistory = strdup(zHistory);
19508       }else if( (zHome = find_home_dir(0))!=0 ){
19509         nHistory = strlen30(zHome) + 20;
19510         if( (zHistory = malloc(nHistory))!=0 ){
19511           sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
19512         }
19513       }
19514       if( zHistory ){ shell_read_history(zHistory); }
19515 #if HAVE_READLINE || HAVE_EDITLINE
19516       rl_attempted_completion_function = readline_completion;
19517 #elif HAVE_LINENOISE
19518       linenoiseSetCompletionCallback(linenoise_completion);
19519 #endif
19520       data.in = 0;
19521       rc = process_input(&data);
19522       if( zHistory ){
19523         shell_stifle_history(2000);
19524         shell_write_history(zHistory);
19525         free(zHistory);
19526       }
19527     }else{
19528       data.in = stdin;
19529       rc = process_input(&data);
19530     }
19531   }
19532   set_table_name(&data, 0);
19533   if( data.db ){
19534     session_close_all(&data);
19535     close_db(data.db);
19536   }
19537   sqlite3_free(data.zFreeOnClose);
19538   find_home_dir(1);
19539   output_reset(&data);
19540   data.doXdgOpen = 0;
19541   clearTempFile(&data);
19542 #if !SQLITE_SHELL_IS_UTF8
19543   for(i=0; i<argcToFree; i++) free(argvToFree[i]);
19544   free(argvToFree);
19545 #endif
19546   /* Clear the global data structure so that valgrind will detect memory
19547   ** leaks */
19548   memset(&data, 0, sizeof(data));
19549   return rc;
19550 }