1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** This file contains code to implement the "sqlite" command line
13 ** utility for accessing SQLite databases.
14 */
15 #if defined(_WIN32) || defined(WIN32)
16 /* This needs to come before any includes for MSVC compiler */
17 #define _CRT_SECURE_NO_WARNINGS
18 #endif
19 
20 /*
21 ** Include the configuration header output by 'configure' if we're using the
22 ** autoconf-based build
23 */
24 #ifdef _HAVE_SQLITE_CONFIG_H
25 #include "config.h"
26 #endif
27 
28 #ifdef ANDROID
29 #ifndef NO_ANDROID_FUNCS
30 #include<sqlite3_android.h>
31 #endif
32 #endif
33 
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <assert.h>
38 #include "sqlite3.h"
39 #include <ctype.h>
40 #include <stdarg.h>
41 
42 #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__)
43 # include <signal.h>
44 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
45 #  include <pwd.h>
46 # endif
47 # include <unistd.h>
48 # include <sys/types.h>
49 #endif
50 
51 #ifdef __OS2__
52 # include <unistd.h>
53 #endif
54 
55 #if defined (HAVE_EDITLINE) && HAVE_EDITLINE==1
56 # include <editline/editline.h>
57 #elif defined(HAVE_READLINE) && HAVE_READLINE==1
58 # include <readline/readline.h>
59 # include <readline/history.h>
60 #endif
61 #if (!defined(HAVE_EDITLINE) || HAVE_EDITLINE!=1) && (!defined(HAVE_READLINE) || HAVE_READLINE!=1)
62 # define readline(p) local_getline(p,stdin)
63 # define add_history(X)
64 # define read_history(X)
65 # define write_history(X)
66 # define stifle_history(X)
67 #endif
68 
69 #if defined(_WIN32) || defined(WIN32)
70 # include <io.h>
71 #define isatty(h) _isatty(h)
72 #define access(f,m) _access((f),(m))
73 #else
74 /* Make sure isatty() has a prototype.
75 */
76 extern int isatty();
77 #endif
78 
79 #if defined(_WIN32_WCE)
80 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
81  * thus we always assume that we have a console. That can be
82  * overridden with the -batch command line option.
83  */
84 #define isatty(x) 1
85 #endif
86 
87 /* True if the timer is enabled */
88 static int enableTimer = 0;
89 
90 #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(__RTP__) && !defined(_WRS_KERNEL)
91 #include <sys/time.h>
92 #include <sys/resource.h>
93 
94 /* Saved resource information for the beginning of an operation */
95 static struct rusage sBegin;
96 
97 /*
98 ** Begin timing an operation
99 */
beginTimer(void)100 static void beginTimer(void){
101   if( enableTimer ){
102     getrusage(RUSAGE_SELF, &sBegin);
103   }
104 }
105 
106 /* Return the difference of two time_structs in seconds */
timeDiff(struct timeval * pStart,struct timeval * pEnd)107 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
108   return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
109          (double)(pEnd->tv_sec - pStart->tv_sec);
110 }
111 
112 /*
113 ** Print the timing results.
114 */
endTimer(void)115 static void endTimer(void){
116   if( enableTimer ){
117     struct rusage sEnd;
118     getrusage(RUSAGE_SELF, &sEnd);
119     printf("CPU Time: user %f sys %f\n",
120        timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
121        timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
122   }
123 }
124 
125 #define BEGIN_TIMER beginTimer()
126 #define END_TIMER endTimer()
127 #define HAS_TIMER 1
128 
129 #elif (defined(_WIN32) || defined(WIN32))
130 
131 #include <windows.h>
132 
133 /* Saved resource information for the beginning of an operation */
134 static HANDLE hProcess;
135 static FILETIME ftKernelBegin;
136 static FILETIME ftUserBegin;
137 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, LPFILETIME, LPFILETIME);
138 static GETPROCTIMES getProcessTimesAddr = NULL;
139 
140 /*
141 ** Check to see if we have timer support.  Return 1 if necessary
142 ** support found (or found previously).
143 */
hasTimer(void)144 static int hasTimer(void){
145   if( getProcessTimesAddr ){
146     return 1;
147   } else {
148     /* GetProcessTimes() isn't supported in WIN95 and some other Windows versions.
149     ** See if the version we are running on has it, and if it does, save off
150     ** a pointer to it and the current process handle.
151     */
152     hProcess = GetCurrentProcess();
153     if( hProcess ){
154       HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
155       if( NULL != hinstLib ){
156         getProcessTimesAddr = (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
157         if( NULL != getProcessTimesAddr ){
158           return 1;
159         }
160         FreeLibrary(hinstLib);
161       }
162     }
163   }
164   return 0;
165 }
166 
167 /*
168 ** Begin timing an operation
169 */
beginTimer(void)170 static void beginTimer(void){
171   if( enableTimer && getProcessTimesAddr ){
172     FILETIME ftCreation, ftExit;
173     getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelBegin, &ftUserBegin);
174   }
175 }
176 
177 /* Return the difference of two FILETIME structs in seconds */
timeDiff(FILETIME * pStart,FILETIME * pEnd)178 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
179   sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
180   sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
181   return (double) ((i64End - i64Start) / 10000000.0);
182 }
183 
184 /*
185 ** Print the timing results.
186 */
endTimer(void)187 static void endTimer(void){
188   if( enableTimer && getProcessTimesAddr){
189     FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
190     getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelEnd, &ftUserEnd);
191     printf("CPU Time: user %f sys %f\n",
192        timeDiff(&ftUserBegin, &ftUserEnd),
193        timeDiff(&ftKernelBegin, &ftKernelEnd));
194   }
195 }
196 
197 #define BEGIN_TIMER beginTimer()
198 #define END_TIMER endTimer()
199 #define HAS_TIMER hasTimer()
200 
201 #else
202 #define BEGIN_TIMER
203 #define END_TIMER
204 #define HAS_TIMER 0
205 #endif
206 
207 /*
208 ** Used to prevent warnings about unused parameters
209 */
210 #define UNUSED_PARAMETER(x) (void)(x)
211 
212 /*
213 ** If the following flag is set, then command execution stops
214 ** at an error if we are not interactive.
215 */
216 static int bail_on_error = 0;
217 
218 /*
219 ** Threat stdin as an interactive input if the following variable
220 ** is true.  Otherwise, assume stdin is connected to a file or pipe.
221 */
222 static int stdin_is_interactive = 1;
223 
224 /*
225 ** The following is the open SQLite database.  We make a pointer
226 ** to this database a static variable so that it can be accessed
227 ** by the SIGINT handler to interrupt database processing.
228 */
229 static sqlite3 *db = 0;
230 
231 /*
232 ** True if an interrupt (Control-C) has been received.
233 */
234 static volatile int seenInterrupt = 0;
235 
236 /*
237 ** This is the name of our program. It is set in main(), used
238 ** in a number of other places, mostly for error messages.
239 */
240 static char *Argv0;
241 
242 /*
243 ** Prompt strings. Initialized in main. Settable with
244 **   .prompt main continue
245 */
246 static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
247 static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
248 
249 /*
250 ** Write I/O traces to the following stream.
251 */
252 #ifdef SQLITE_ENABLE_IOTRACE
253 static FILE *iotrace = 0;
254 #endif
255 
256 /*
257 ** This routine works like printf in that its first argument is a
258 ** format string and subsequent arguments are values to be substituted
259 ** in place of % fields.  The result of formatting this string
260 ** is written to iotrace.
261 */
262 #ifdef SQLITE_ENABLE_IOTRACE
iotracePrintf(const char * zFormat,...)263 static void iotracePrintf(const char *zFormat, ...){
264   va_list ap;
265   char *z;
266   if( iotrace==0 ) return;
267   va_start(ap, zFormat);
268   z = sqlite3_vmprintf(zFormat, ap);
269   va_end(ap);
270   fprintf(iotrace, "%s", z);
271   sqlite3_free(z);
272 }
273 #endif
274 
275 
276 /*
277 ** Determines if a string is a number of not.
278 */
isNumber(const char * z,int * realnum)279 static int isNumber(const char *z, int *realnum){
280   if( *z=='-' || *z=='+' ) z++;
281   if( !isdigit(*z) ){
282     return 0;
283   }
284   z++;
285   if( realnum ) *realnum = 0;
286   while( isdigit(*z) ){ z++; }
287   if( *z=='.' ){
288     z++;
289     if( !isdigit(*z) ) return 0;
290     while( isdigit(*z) ){ z++; }
291     if( realnum ) *realnum = 1;
292   }
293   if( *z=='e' || *z=='E' ){
294     z++;
295     if( *z=='+' || *z=='-' ) z++;
296     if( !isdigit(*z) ) return 0;
297     while( isdigit(*z) ){ z++; }
298     if( realnum ) *realnum = 1;
299   }
300   return *z==0;
301 }
302 
303 /*
304 ** A global char* and an SQL function to access its current value
305 ** from within an SQL statement. This program used to use the
306 ** sqlite_exec_printf() API to substitue a string into an SQL statement.
307 ** The correct way to do this with sqlite3 is to use the bind API, but
308 ** since the shell is built around the callback paradigm it would be a lot
309 ** of work. Instead just use this hack, which is quite harmless.
310 */
311 static const char *zShellStatic = 0;
shellstaticFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)312 static void shellstaticFunc(
313   sqlite3_context *context,
314   int argc,
315   sqlite3_value **argv
316 ){
317   assert( 0==argc );
318   assert( zShellStatic );
319   UNUSED_PARAMETER(argc);
320   UNUSED_PARAMETER(argv);
321   sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC);
322 }
323 
324 
325 /*
326 ** This routine reads a line of text from FILE in, stores
327 ** the text in memory obtained from malloc() and returns a pointer
328 ** to the text.  NULL is returned at end of file, or if malloc()
329 ** fails.
330 **
331 ** The interface is like "readline" but no command-line editing
332 ** is done.
333 */
local_getline(char * zPrompt,FILE * in)334 static char *local_getline(char *zPrompt, FILE *in){
335   char *zLine;
336   int nLine;
337   int n;
338   int eol;
339 
340   if( zPrompt && *zPrompt ){
341     printf("%s",zPrompt);
342     fflush(stdout);
343   }
344   nLine = 100;
345   zLine = malloc( nLine );
346   if( zLine==0 ) return 0;
347   n = 0;
348   eol = 0;
349   while( !eol ){
350     if( n+100>nLine ){
351       nLine = nLine*2 + 100;
352       zLine = realloc(zLine, nLine);
353       if( zLine==0 ) return 0;
354     }
355     if( fgets(&zLine[n], nLine - n, in)==0 ){
356       if( n==0 ){
357         free(zLine);
358         return 0;
359       }
360       zLine[n] = 0;
361       eol = 1;
362       break;
363     }
364     while( zLine[n] ){ n++; }
365     if( n>0 && zLine[n-1]=='\n' ){
366       n--;
367       if( n>0 && zLine[n-1]=='\r' ) n--;
368       zLine[n] = 0;
369       eol = 1;
370     }
371   }
372   zLine = realloc( zLine, n+1 );
373   return zLine;
374 }
375 
376 /*
377 ** Retrieve a single line of input text.
378 **
379 ** zPrior is a string of prior text retrieved.  If not the empty
380 ** string, then issue a continuation prompt.
381 */
one_input_line(const char * zPrior,FILE * in)382 static char *one_input_line(const char *zPrior, FILE *in){
383   char *zPrompt;
384   char *zResult;
385   if( in!=0 ){
386     return local_getline(0, in);
387   }
388   if( zPrior && zPrior[0] ){
389     zPrompt = continuePrompt;
390   }else{
391     zPrompt = mainPrompt;
392   }
393   zResult = readline(zPrompt);
394 #if defined(HAVE_READLINE) && HAVE_READLINE==1
395   if( zResult && *zResult ) add_history(zResult);
396 #endif
397   return zResult;
398 }
399 
400 struct previous_mode_data {
401   int valid;        /* Is there legit data in here? */
402   int mode;
403   int showHeader;
404   int colWidth[100];
405 };
406 
407 /*
408 ** An pointer to an instance of this structure is passed from
409 ** the main program to the callback.  This is used to communicate
410 ** state and mode information.
411 */
412 struct callback_data {
413   sqlite3 *db;           /* The database */
414   int echoOn;            /* True to echo input commands */
415   int statsOn;           /* True to display memory stats before each finalize */
416   int cnt;               /* Number of records displayed so far */
417   FILE *out;             /* Write results here */
418   int mode;              /* An output mode setting */
419   int writableSchema;    /* True if PRAGMA writable_schema=ON */
420   int showHeader;        /* True to show column names in List or Column mode */
421   char *zDestTable;      /* Name of destination table when MODE_Insert */
422   char separator[20];    /* Separator character for MODE_List */
423   int colWidth[100];     /* Requested width of each column when in column mode*/
424   int actualWidth[100];  /* Actual width of each column */
425   char nullvalue[20];    /* The text to print when a NULL comes back from
426                          ** the database */
427   struct previous_mode_data explainPrev;
428                          /* Holds the mode information just before
429                          ** .explain ON */
430   char outfile[FILENAME_MAX]; /* Filename for *out */
431   const char *zDbFilename;    /* name of the database file */
432   const char *zVfs;           /* Name of VFS to use */
433   sqlite3_stmt *pStmt;   /* Current statement if any. */
434   FILE *pLog;            /* Write log output here */
435 };
436 
437 /*
438 ** These are the allowed modes.
439 */
440 #define MODE_Line     0  /* One column per line.  Blank line between records */
441 #define MODE_Column   1  /* One record per line in neat columns */
442 #define MODE_List     2  /* One record per line with a separator */
443 #define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
444 #define MODE_Html     4  /* Generate an XHTML table */
445 #define MODE_Insert   5  /* Generate SQL "insert" statements */
446 #define MODE_Tcl      6  /* Generate ANSI-C or TCL quoted elements */
447 #define MODE_Csv      7  /* Quote strings, numbers are plain */
448 #define MODE_Explain  8  /* Like MODE_Column, but do not truncate data */
449 
450 static const char *modeDescr[] = {
451   "line",
452   "column",
453   "list",
454   "semi",
455   "html",
456   "insert",
457   "tcl",
458   "csv",
459   "explain",
460 };
461 
462 /*
463 ** Number of elements in an array
464 */
465 #define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
466 
467 /*
468 ** Compute a string length that is limited to what can be stored in
469 ** lower 30 bits of a 32-bit signed integer.
470 */
strlen30(const char * z)471 static int strlen30(const char *z){
472   const char *z2 = z;
473   while( *z2 ){ z2++; }
474   return 0x3fffffff & (int)(z2 - z);
475 }
476 
477 /*
478 ** A callback for the sqlite3_log() interface.
479 */
shellLog(void * pArg,int iErrCode,const char * zMsg)480 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
481   struct callback_data *p = (struct callback_data*)pArg;
482   if( p->pLog==0 ) return;
483   fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
484   fflush(p->pLog);
485 }
486 
487 /*
488 ** Output the given string as a hex-encoded blob (eg. X'1234' )
489 */
output_hex_blob(FILE * out,const void * pBlob,int nBlob)490 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
491   int i;
492   char *zBlob = (char *)pBlob;
493   fprintf(out,"X'");
494   for(i=0; i<nBlob; i++){ fprintf(out,"%02x",zBlob[i]); }
495   fprintf(out,"'");
496 }
497 
498 /*
499 ** Output the given string as a quoted string using SQL quoting conventions.
500 */
output_quoted_string(FILE * out,const char * z)501 static void output_quoted_string(FILE *out, const char *z){
502   int i;
503   int nSingle = 0;
504   for(i=0; z[i]; i++){
505     if( z[i]=='\'' ) nSingle++;
506   }
507   if( nSingle==0 ){
508     fprintf(out,"'%s'",z);
509   }else{
510     fprintf(out,"'");
511     while( *z ){
512       for(i=0; z[i] && z[i]!='\''; i++){}
513       if( i==0 ){
514         fprintf(out,"''");
515         z++;
516       }else if( z[i]=='\'' ){
517         fprintf(out,"%.*s''",i,z);
518         z += i+1;
519       }else{
520         fprintf(out,"%s",z);
521         break;
522       }
523     }
524     fprintf(out,"'");
525   }
526 }
527 
528 /*
529 ** Output the given string as a quoted according to C or TCL quoting rules.
530 */
output_c_string(FILE * out,const char * z)531 static void output_c_string(FILE *out, const char *z){
532   unsigned int c;
533   fputc('"', out);
534   while( (c = *(z++))!=0 ){
535     if( c=='\\' ){
536       fputc(c, out);
537       fputc(c, out);
538     }else if( c=='\t' ){
539       fputc('\\', out);
540       fputc('t', out);
541     }else if( c=='\n' ){
542       fputc('\\', out);
543       fputc('n', out);
544     }else if( c=='\r' ){
545       fputc('\\', out);
546       fputc('r', out);
547     }else if( !isprint(c) ){
548       fprintf(out, "\\%03o", c&0xff);
549     }else{
550       fputc(c, out);
551     }
552   }
553   fputc('"', out);
554 }
555 
556 /*
557 ** Output the given string with characters that are special to
558 ** HTML escaped.
559 */
output_html_string(FILE * out,const char * z)560 static void output_html_string(FILE *out, const char *z){
561   int i;
562   while( *z ){
563     for(i=0;   z[i]
564             && z[i]!='<'
565             && z[i]!='&'
566             && z[i]!='>'
567             && z[i]!='\"'
568             && z[i]!='\'';
569         i++){}
570     if( i>0 ){
571       fprintf(out,"%.*s",i,z);
572     }
573     if( z[i]=='<' ){
574       fprintf(out,"&lt;");
575     }else if( z[i]=='&' ){
576       fprintf(out,"&amp;");
577     }else if( z[i]=='>' ){
578       fprintf(out,"&gt;");
579     }else if( z[i]=='\"' ){
580       fprintf(out,"&quot;");
581     }else if( z[i]=='\'' ){
582       fprintf(out,"&#39;");
583     }else{
584       break;
585     }
586     z += i + 1;
587   }
588 }
589 
590 /*
591 ** If a field contains any character identified by a 1 in the following
592 ** array, then the string must be quoted for CSV.
593 */
594 static const char needCsvQuote[] = {
595   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
596   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
597   1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
598   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
599   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
600   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
601   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
602   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
603   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
604   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
605   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
606   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
607   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
608   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
609   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
610   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
611 };
612 
613 /*
614 ** Output a single term of CSV.  Actually, p->separator is used for
615 ** the separator, which may or may not be a comma.  p->nullvalue is
616 ** the null value.  Strings are quoted using ANSI-C rules.  Numbers
617 ** appear outside of quotes.
618 */
output_csv(struct callback_data * p,const char * z,int bSep)619 static void output_csv(struct callback_data *p, const char *z, int bSep){
620   FILE *out = p->out;
621   if( z==0 ){
622     fprintf(out,"%s",p->nullvalue);
623   }else{
624     int i;
625     int nSep = strlen30(p->separator);
626     for(i=0; z[i]; i++){
627       if( needCsvQuote[((unsigned char*)z)[i]]
628          || (z[i]==p->separator[0] &&
629              (nSep==1 || memcmp(z, p->separator, nSep)==0)) ){
630         i = 0;
631         break;
632       }
633     }
634     if( i==0 ){
635       putc('"', out);
636       for(i=0; z[i]; i++){
637         if( z[i]=='"' ) putc('"', out);
638         putc(z[i], out);
639       }
640       putc('"', out);
641     }else{
642       fprintf(out, "%s", z);
643     }
644   }
645   if( bSep ){
646     fprintf(p->out, "%s", p->separator);
647   }
648 }
649 
650 #ifdef SIGINT
651 /*
652 ** This routine runs when the user presses Ctrl-C
653 */
interrupt_handler(int NotUsed)654 static void interrupt_handler(int NotUsed){
655   UNUSED_PARAMETER(NotUsed);
656   seenInterrupt = 1;
657   if( db ) sqlite3_interrupt(db);
658 }
659 #endif
660 
661 /*
662 ** This is the callback routine that the shell
663 ** invokes for each row of a query result.
664 */
shell_callback(void * pArg,int nArg,char ** azArg,char ** azCol,int * aiType)665 static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){
666   int i;
667   struct callback_data *p = (struct callback_data*)pArg;
668 
669   switch( p->mode ){
670     case MODE_Line: {
671       int w = 5;
672       if( azArg==0 ) break;
673       for(i=0; i<nArg; i++){
674         int len = strlen30(azCol[i] ? azCol[i] : "");
675         if( len>w ) w = len;
676       }
677       if( p->cnt++>0 ) fprintf(p->out,"\n");
678       for(i=0; i<nArg; i++){
679         fprintf(p->out,"%*s = %s\n", w, azCol[i],
680                 azArg[i] ? azArg[i] : p->nullvalue);
681       }
682       break;
683     }
684     case MODE_Explain:
685     case MODE_Column: {
686       if( p->cnt++==0 ){
687         for(i=0; i<nArg; i++){
688           int w, n;
689           if( i<ArraySize(p->colWidth) ){
690             w = p->colWidth[i];
691           }else{
692             w = 0;
693           }
694           if( w<=0 ){
695             w = strlen30(azCol[i] ? azCol[i] : "");
696             if( w<10 ) w = 10;
697             n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullvalue);
698             if( w<n ) w = n;
699           }
700           if( i<ArraySize(p->actualWidth) ){
701             p->actualWidth[i] = w;
702           }
703           if( p->showHeader ){
704             fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": "  ");
705           }
706         }
707         if( p->showHeader ){
708           for(i=0; i<nArg; i++){
709             int w;
710             if( i<ArraySize(p->actualWidth) ){
711                w = p->actualWidth[i];
712             }else{
713                w = 10;
714             }
715             fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
716                    "----------------------------------------------------------",
717                     i==nArg-1 ? "\n": "  ");
718           }
719         }
720       }
721       if( azArg==0 ) break;
722       for(i=0; i<nArg; i++){
723         int w;
724         if( i<ArraySize(p->actualWidth) ){
725            w = p->actualWidth[i];
726         }else{
727            w = 10;
728         }
729         if( p->mode==MODE_Explain && azArg[i] &&
730            strlen30(azArg[i])>w ){
731           w = strlen30(azArg[i]);
732         }
733         fprintf(p->out,"%-*.*s%s",w,w,
734             azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": "  ");
735       }
736       break;
737     }
738     case MODE_Semi:
739     case MODE_List: {
740       if( p->cnt++==0 && p->showHeader ){
741         for(i=0; i<nArg; i++){
742           fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
743         }
744       }
745       if( azArg==0 ) break;
746       for(i=0; i<nArg; i++){
747         char *z = azArg[i];
748         if( z==0 ) z = p->nullvalue;
749         fprintf(p->out, "%s", z);
750         if( i<nArg-1 ){
751           fprintf(p->out, "%s", p->separator);
752         }else if( p->mode==MODE_Semi ){
753           fprintf(p->out, ";\n");
754         }else{
755           fprintf(p->out, "\n");
756         }
757       }
758       break;
759     }
760     case MODE_Html: {
761       if( p->cnt++==0 && p->showHeader ){
762         fprintf(p->out,"<TR>");
763         for(i=0; i<nArg; i++){
764           fprintf(p->out,"<TH>");
765           output_html_string(p->out, azCol[i]);
766           fprintf(p->out,"</TH>\n");
767         }
768         fprintf(p->out,"</TR>\n");
769       }
770       if( azArg==0 ) break;
771       fprintf(p->out,"<TR>");
772       for(i=0; i<nArg; i++){
773         fprintf(p->out,"<TD>");
774         output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
775         fprintf(p->out,"</TD>\n");
776       }
777       fprintf(p->out,"</TR>\n");
778       break;
779     }
780     case MODE_Tcl: {
781       if( p->cnt++==0 && p->showHeader ){
782         for(i=0; i<nArg; i++){
783           output_c_string(p->out,azCol[i] ? azCol[i] : "");
784           fprintf(p->out, "%s", p->separator);
785         }
786         fprintf(p->out,"\n");
787       }
788       if( azArg==0 ) break;
789       for(i=0; i<nArg; i++){
790         output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
791         fprintf(p->out, "%s", p->separator);
792       }
793       fprintf(p->out,"\n");
794       break;
795     }
796     case MODE_Csv: {
797       if( p->cnt++==0 && p->showHeader ){
798         for(i=0; i<nArg; i++){
799           output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
800         }
801         fprintf(p->out,"\n");
802       }
803       if( azArg==0 ) break;
804       for(i=0; i<nArg; i++){
805         output_csv(p, azArg[i], i<nArg-1);
806       }
807       fprintf(p->out,"\n");
808       break;
809     }
810     case MODE_Insert: {
811       p->cnt++;
812       if( azArg==0 ) break;
813       fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
814       for(i=0; i<nArg; i++){
815         char *zSep = i>0 ? ",": "";
816         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
817           fprintf(p->out,"%sNULL",zSep);
818         }else if( aiType && aiType[i]==SQLITE_TEXT ){
819           if( zSep[0] ) fprintf(p->out,"%s",zSep);
820           output_quoted_string(p->out, azArg[i]);
821         }else if( aiType && (aiType[i]==SQLITE_INTEGER || aiType[i]==SQLITE_FLOAT) ){
822           fprintf(p->out,"%s%s",zSep, azArg[i]);
823         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
824           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
825           int nBlob = sqlite3_column_bytes(p->pStmt, i);
826           if( zSep[0] ) fprintf(p->out,"%s",zSep);
827           output_hex_blob(p->out, pBlob, nBlob);
828         }else if( isNumber(azArg[i], 0) ){
829           fprintf(p->out,"%s%s",zSep, azArg[i]);
830         }else{
831           if( zSep[0] ) fprintf(p->out,"%s",zSep);
832           output_quoted_string(p->out, azArg[i]);
833         }
834       }
835       fprintf(p->out,");\n");
836       break;
837     }
838   }
839   return 0;
840 }
841 
842 /*
843 ** This is the callback routine that the SQLite library
844 ** invokes for each row of a query result.
845 */
callback(void * pArg,int nArg,char ** azArg,char ** azCol)846 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
847   /* since we don't have type info, call the shell_callback with a NULL value */
848   return shell_callback(pArg, nArg, azArg, azCol, NULL);
849 }
850 
851 /*
852 ** Set the destination table field of the callback_data structure to
853 ** the name of the table given.  Escape any quote characters in the
854 ** table name.
855 */
set_table_name(struct callback_data * p,const char * zName)856 static void set_table_name(struct callback_data *p, const char *zName){
857   int i, n;
858   int needQuote;
859   char *z;
860 
861   if( p->zDestTable ){
862     free(p->zDestTable);
863     p->zDestTable = 0;
864   }
865   if( zName==0 ) return;
866   needQuote = !isalpha((unsigned char)*zName) && *zName!='_';
867   for(i=n=0; zName[i]; i++, n++){
868     if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){
869       needQuote = 1;
870       if( zName[i]=='\'' ) n++;
871     }
872   }
873   if( needQuote ) n += 2;
874   z = p->zDestTable = malloc( n+1 );
875   if( z==0 ){
876     fprintf(stderr,"Error: out of memory\n");
877     exit(1);
878   }
879   n = 0;
880   if( needQuote ) z[n++] = '\'';
881   for(i=0; zName[i]; i++){
882     z[n++] = zName[i];
883     if( zName[i]=='\'' ) z[n++] = '\'';
884   }
885   if( needQuote ) z[n++] = '\'';
886   z[n] = 0;
887 }
888 
889 /* zIn is either a pointer to a NULL-terminated string in memory obtained
890 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
891 ** added to zIn, and the result returned in memory obtained from malloc().
892 ** zIn, if it was not NULL, is freed.
893 **
894 ** If the third argument, quote, is not '\0', then it is used as a
895 ** quote character for zAppend.
896 */
appendText(char * zIn,char const * zAppend,char quote)897 static char *appendText(char *zIn, char const *zAppend, char quote){
898   int len;
899   int i;
900   int nAppend = strlen30(zAppend);
901   int nIn = (zIn?strlen30(zIn):0);
902 
903   len = nAppend+nIn+1;
904   if( quote ){
905     len += 2;
906     for(i=0; i<nAppend; i++){
907       if( zAppend[i]==quote ) len++;
908     }
909   }
910 
911   zIn = (char *)realloc(zIn, len);
912   if( !zIn ){
913     return 0;
914   }
915 
916   if( quote ){
917     char *zCsr = &zIn[nIn];
918     *zCsr++ = quote;
919     for(i=0; i<nAppend; i++){
920       *zCsr++ = zAppend[i];
921       if( zAppend[i]==quote ) *zCsr++ = quote;
922     }
923     *zCsr++ = quote;
924     *zCsr++ = '\0';
925     assert( (zCsr-zIn)==len );
926   }else{
927     memcpy(&zIn[nIn], zAppend, nAppend);
928     zIn[len-1] = '\0';
929   }
930 
931   return zIn;
932 }
933 
934 
935 /*
936 ** Execute a query statement that has a single result column.  Print
937 ** that result column on a line by itself with a semicolon terminator.
938 **
939 ** This is used, for example, to show the schema of the database by
940 ** querying the SQLITE_MASTER table.
941 */
run_table_dump_query(FILE * out,sqlite3 * db,const char * zSelect,const char * zFirstRow)942 static int run_table_dump_query(
943   FILE *out,              /* Send output here */
944   sqlite3 *db,            /* Database to query */
945   const char *zSelect,    /* SELECT statement to extract content */
946   const char *zFirstRow   /* Print before first row, if not NULL */
947 ){
948   sqlite3_stmt *pSelect;
949   int rc;
950   rc = sqlite3_prepare(db, zSelect, -1, &pSelect, 0);
951   if( rc!=SQLITE_OK || !pSelect ){
952     return rc;
953   }
954   rc = sqlite3_step(pSelect);
955   while( rc==SQLITE_ROW ){
956     if( zFirstRow ){
957       fprintf(out, "%s", zFirstRow);
958       zFirstRow = 0;
959     }
960     fprintf(out, "%s;\n", sqlite3_column_text(pSelect, 0));
961     rc = sqlite3_step(pSelect);
962   }
963   return sqlite3_finalize(pSelect);
964 }
965 
966 /*
967 ** Allocate space and save off current error string.
968 */
save_err_msg(sqlite3 * db)969 static char *save_err_msg(
970   sqlite3 *db            /* Database to query */
971 ){
972   int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
973   char *zErrMsg = sqlite3_malloc(nErrMsg);
974   if( zErrMsg ){
975     memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
976   }
977   return zErrMsg;
978 }
979 
980 /*
981 ** Display memory stats.
982 */
display_stats(sqlite3 * db,struct callback_data * pArg,int bReset)983 static int display_stats(
984   sqlite3 *db,                /* Database to query */
985   struct callback_data *pArg, /* Pointer to struct callback_data */
986   int bReset                  /* True to reset the stats */
987 ){
988   int iCur;
989   int iHiwtr;
990 
991   if( pArg && pArg->out ){
992 
993     iHiwtr = iCur = -1;
994     sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
995     fprintf(pArg->out, "Memory Used:                         %d (max %d) bytes\n", iCur, iHiwtr);
996     iHiwtr = iCur = -1;
997     sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
998     fprintf(pArg->out, "Number of Outstanding Allocations:   %d (max %d)\n", iCur, iHiwtr);
999 /*
1000 ** Not currently used by the CLI.
1001 **    iHiwtr = iCur = -1;
1002 **    sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
1003 **    fprintf(pArg->out, "Number of Pcache Pages Used:         %d (max %d) pages\n", iCur, iHiwtr);
1004 */
1005     iHiwtr = iCur = -1;
1006     sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
1007     fprintf(pArg->out, "Number of Pcache Overflow Bytes:     %d (max %d) bytes\n", iCur, iHiwtr);
1008 /*
1009 ** Not currently used by the CLI.
1010 **    iHiwtr = iCur = -1;
1011 **    sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
1012 **    fprintf(pArg->out, "Number of Scratch Allocations Used:  %d (max %d)\n", iCur, iHiwtr);
1013 */
1014     iHiwtr = iCur = -1;
1015     sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
1016     fprintf(pArg->out, "Number of Scratch Overflow Bytes:    %d (max %d) bytes\n", iCur, iHiwtr);
1017     iHiwtr = iCur = -1;
1018     sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
1019     fprintf(pArg->out, "Largest Allocation:                  %d bytes\n", iHiwtr);
1020     iHiwtr = iCur = -1;
1021     sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset);
1022     fprintf(pArg->out, "Largest Pcache Allocation:           %d bytes\n", iHiwtr);
1023     iHiwtr = iCur = -1;
1024     sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset);
1025     fprintf(pArg->out, "Largest Scratch Allocation:          %d bytes\n", iHiwtr);
1026 #ifdef YYTRACKMAXSTACKDEPTH
1027     iHiwtr = iCur = -1;
1028     sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset);
1029     fprintf(pArg->out, "Deepest Parser Stack:                %d (max %d)\n", iCur, iHiwtr);
1030 #endif
1031   }
1032 
1033   if( pArg && pArg->out && db ){
1034     iHiwtr = iCur = -1;
1035     sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
1036     fprintf(pArg->out, "Lookaside Slots Used:                %d (max %d)\n", iCur, iHiwtr);
1037     sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
1038     fprintf(pArg->out, "Successful lookaside attempts:       %d\n", iHiwtr);
1039     sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
1040     fprintf(pArg->out, "Lookaside failures due to size:      %d\n", iHiwtr);
1041     sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
1042     fprintf(pArg->out, "Lookaside failures due to OOM:       %d\n", iHiwtr);
1043     iHiwtr = iCur = -1;
1044     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
1045     fprintf(pArg->out, "Pager Heap Usage:                    %d bytes\n", iCur);
1046     iHiwtr = iCur = -1;
1047     sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
1048     fprintf(pArg->out, "Schema Heap Usage:                   %d bytes\n", iCur);
1049     iHiwtr = iCur = -1;
1050     sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
1051     fprintf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n", iCur);
1052   }
1053 
1054   if( pArg && pArg->out && db && pArg->pStmt ){
1055     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, bReset);
1056     fprintf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
1057     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
1058     fprintf(pArg->out, "Sort Operations:                     %d\n", iCur);
1059     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX, bReset);
1060     fprintf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
1061   }
1062 
1063   return 0;
1064 }
1065 
1066 /*
1067 ** Execute a statement or set of statements.  Print
1068 ** any result rows/columns depending on the current mode
1069 ** set via the supplied callback.
1070 **
1071 ** This is very similar to SQLite's built-in sqlite3_exec()
1072 ** function except it takes a slightly different callback
1073 ** and callback data argument.
1074 */
shell_exec(sqlite3 * db,const char * zSql,int (* xCallback)(void *,int,char **,char **,int *),struct callback_data * pArg,char ** pzErrMsg)1075 static int shell_exec(
1076   sqlite3 *db,                                /* An open database */
1077   const char *zSql,                           /* SQL to be evaluated */
1078   int (*xCallback)(void*,int,char**,char**,int*),   /* Callback function */
1079                                               /* (not the same as sqlite3_exec) */
1080   struct callback_data *pArg,                 /* Pointer to struct callback_data */
1081   char **pzErrMsg                             /* Error msg written here */
1082 ){
1083   sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
1084   int rc = SQLITE_OK;             /* Return Code */
1085   const char *zLeftover;          /* Tail of unprocessed SQL */
1086 
1087   if( pzErrMsg ){
1088     *pzErrMsg = NULL;
1089   }
1090 
1091   while( zSql[0] && (SQLITE_OK == rc) ){
1092     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
1093     if( SQLITE_OK != rc ){
1094       if( pzErrMsg ){
1095         *pzErrMsg = save_err_msg(db);
1096       }
1097     }else{
1098       if( !pStmt ){
1099         /* this happens for a comment or white-space */
1100         zSql = zLeftover;
1101         while( isspace(zSql[0]) ) zSql++;
1102         continue;
1103       }
1104 
1105       /* save off the prepared statment handle and reset row count */
1106       if( pArg ){
1107         pArg->pStmt = pStmt;
1108         pArg->cnt = 0;
1109       }
1110 
1111       /* echo the sql statement if echo on */
1112       if( pArg && pArg->echoOn ){
1113         const char *zStmtSql = sqlite3_sql(pStmt);
1114         fprintf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
1115       }
1116 
1117       /* perform the first step.  this will tell us if we
1118       ** have a result set or not and how wide it is.
1119       */
1120       rc = sqlite3_step(pStmt);
1121       /* if we have a result set... */
1122       if( SQLITE_ROW == rc ){
1123         /* if we have a callback... */
1124         if( xCallback ){
1125           /* allocate space for col name ptr, value ptr, and type */
1126           int nCol = sqlite3_column_count(pStmt);
1127           void *pData = sqlite3_malloc(3*nCol*sizeof(const char*) + 1);
1128           if( !pData ){
1129             rc = SQLITE_NOMEM;
1130           }else{
1131             char **azCols = (char **)pData;      /* Names of result columns */
1132             char **azVals = &azCols[nCol];       /* Results */
1133             int *aiTypes = (int *)&azVals[nCol]; /* Result types */
1134             int i;
1135             assert(sizeof(int) <= sizeof(char *));
1136             /* save off ptrs to column names */
1137             for(i=0; i<nCol; i++){
1138               azCols[i] = (char *)sqlite3_column_name(pStmt, i);
1139             }
1140             do{
1141               /* extract the data and data types */
1142               for(i=0; i<nCol; i++){
1143                 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
1144                 aiTypes[i] = sqlite3_column_type(pStmt, i);
1145                 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
1146                   rc = SQLITE_NOMEM;
1147                   break; /* from for */
1148                 }
1149               } /* end for */
1150 
1151               /* if data and types extracted successfully... */
1152               if( SQLITE_ROW == rc ){
1153                 /* call the supplied callback with the result row data */
1154                 if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
1155                   rc = SQLITE_ABORT;
1156                 }else{
1157                   rc = sqlite3_step(pStmt);
1158                 }
1159               }
1160             } while( SQLITE_ROW == rc );
1161             sqlite3_free(pData);
1162           }
1163         }else{
1164           do{
1165             rc = sqlite3_step(pStmt);
1166           } while( rc == SQLITE_ROW );
1167         }
1168       }
1169 
1170       /* print usage stats if stats on */
1171       if( pArg && pArg->statsOn ){
1172         display_stats(db, pArg, 0);
1173       }
1174 
1175       /* Finalize the statement just executed. If this fails, save a
1176       ** copy of the error message. Otherwise, set zSql to point to the
1177       ** next statement to execute. */
1178       rc = sqlite3_finalize(pStmt);
1179       if( rc==SQLITE_OK ){
1180         zSql = zLeftover;
1181         while( isspace(zSql[0]) ) zSql++;
1182       }else if( pzErrMsg ){
1183         *pzErrMsg = save_err_msg(db);
1184       }
1185 
1186       /* clear saved stmt handle */
1187       if( pArg ){
1188         pArg->pStmt = NULL;
1189       }
1190     }
1191   } /* end while */
1192 
1193   return rc;
1194 }
1195 
1196 
1197 /*
1198 ** This is a different callback routine used for dumping the database.
1199 ** Each row received by this callback consists of a table name,
1200 ** the table type ("index" or "table") and SQL to create the table.
1201 ** This routine should print text sufficient to recreate the table.
1202 */
dump_callback(void * pArg,int nArg,char ** azArg,char ** azCol)1203 static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
1204   int rc;
1205   const char *zTable;
1206   const char *zType;
1207   const char *zSql;
1208   const char *zPrepStmt = 0;
1209   struct callback_data *p = (struct callback_data *)pArg;
1210 
1211   UNUSED_PARAMETER(azCol);
1212   if( nArg!=3 ) return 1;
1213   zTable = azArg[0];
1214   zType = azArg[1];
1215   zSql = azArg[2];
1216 
1217   if( strcmp(zTable, "sqlite_sequence")==0 ){
1218     zPrepStmt = "DELETE FROM sqlite_sequence;\n";
1219   }else if( strcmp(zTable, "sqlite_stat1")==0 ){
1220     fprintf(p->out, "ANALYZE sqlite_master;\n");
1221   }else if( strncmp(zTable, "sqlite_", 7)==0 ){
1222     return 0;
1223   }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
1224     char *zIns;
1225     if( !p->writableSchema ){
1226       fprintf(p->out, "PRAGMA writable_schema=ON;\n");
1227       p->writableSchema = 1;
1228     }
1229     zIns = sqlite3_mprintf(
1230        "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
1231        "VALUES('table','%q','%q',0,'%q');",
1232        zTable, zTable, zSql);
1233     fprintf(p->out, "%s\n", zIns);
1234     sqlite3_free(zIns);
1235     return 0;
1236   }else{
1237     fprintf(p->out, "%s;\n", zSql);
1238   }
1239 
1240   if( strcmp(zType, "table")==0 ){
1241     sqlite3_stmt *pTableInfo = 0;
1242     char *zSelect = 0;
1243     char *zTableInfo = 0;
1244     char *zTmp = 0;
1245     int nRow = 0;
1246 
1247     zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
1248     zTableInfo = appendText(zTableInfo, zTable, '"');
1249     zTableInfo = appendText(zTableInfo, ");", 0);
1250 
1251     rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0);
1252     free(zTableInfo);
1253     if( rc!=SQLITE_OK || !pTableInfo ){
1254       return 1;
1255     }
1256 
1257     zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
1258     zTmp = appendText(zTmp, zTable, '"');
1259     if( zTmp ){
1260       zSelect = appendText(zSelect, zTmp, '\'');
1261     }
1262     zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
1263     rc = sqlite3_step(pTableInfo);
1264     while( rc==SQLITE_ROW ){
1265       const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1);
1266       zSelect = appendText(zSelect, "quote(", 0);
1267       zSelect = appendText(zSelect, zText, '"');
1268       rc = sqlite3_step(pTableInfo);
1269       if( rc==SQLITE_ROW ){
1270         zSelect = appendText(zSelect, ") || ',' || ", 0);
1271       }else{
1272         zSelect = appendText(zSelect, ") ", 0);
1273       }
1274       nRow++;
1275     }
1276     rc = sqlite3_finalize(pTableInfo);
1277     if( rc!=SQLITE_OK || nRow==0 ){
1278       free(zSelect);
1279       return 1;
1280     }
1281     zSelect = appendText(zSelect, "|| ')' FROM  ", 0);
1282     zSelect = appendText(zSelect, zTable, '"');
1283 
1284     rc = run_table_dump_query(p->out, p->db, zSelect, zPrepStmt);
1285     if( rc==SQLITE_CORRUPT ){
1286       zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
1287       rc = run_table_dump_query(p->out, p->db, zSelect, 0);
1288     }
1289     if( zSelect ) free(zSelect);
1290   }
1291   return 0;
1292 }
1293 
1294 /*
1295 ** Run zQuery.  Use dump_callback() as the callback routine so that
1296 ** the contents of the query are output as SQL statements.
1297 **
1298 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
1299 ** "ORDER BY rowid DESC" to the end.
1300 */
run_schema_dump_query(struct callback_data * p,const char * zQuery,char ** pzErrMsg)1301 static int run_schema_dump_query(
1302   struct callback_data *p,
1303   const char *zQuery,
1304   char **pzErrMsg
1305 ){
1306   int rc;
1307   rc = sqlite3_exec(p->db, zQuery, dump_callback, p, pzErrMsg);
1308   if( rc==SQLITE_CORRUPT ){
1309     char *zQ2;
1310     int len = strlen30(zQuery);
1311     if( pzErrMsg ) sqlite3_free(*pzErrMsg);
1312     zQ2 = malloc( len+100 );
1313     if( zQ2==0 ) return rc;
1314     sqlite3_snprintf(sizeof(zQ2), zQ2, "%s ORDER BY rowid DESC", zQuery);
1315     rc = sqlite3_exec(p->db, zQ2, dump_callback, p, pzErrMsg);
1316     free(zQ2);
1317   }
1318   return rc;
1319 }
1320 
1321 /*
1322 ** Text of a help message
1323 */
1324 static char zHelp[] =
1325   ".backup ?DB? FILE      Backup DB (default \"main\") to FILE\n"
1326   ".bail ON|OFF           Stop after hitting an error.  Default OFF\n"
1327   ".databases             List names and files of attached databases\n"
1328   ".dump ?TABLE? ...      Dump the database in an SQL text format\n"
1329   "                         If TABLE specified, only dump tables matching\n"
1330   "                         LIKE pattern TABLE.\n"
1331   ".echo ON|OFF           Turn command echo on or off\n"
1332   ".exit                  Exit this program\n"
1333   ".explain ?ON|OFF?      Turn output mode suitable for EXPLAIN on or off.\n"
1334   "                         With no args, it turns EXPLAIN on.\n"
1335   ".header(s) ON|OFF      Turn display of headers on or off\n"
1336   ".help                  Show this message\n"
1337   ".import FILE TABLE     Import data from FILE into TABLE\n"
1338   ".indices ?TABLE?       Show names of all indices\n"
1339   "                         If TABLE specified, only show indices for tables\n"
1340   "                         matching LIKE pattern TABLE.\n"
1341 #ifdef SQLITE_ENABLE_IOTRACE
1342   ".iotrace FILE          Enable I/O diagnostic logging to FILE\n"
1343 #endif
1344 #ifndef SQLITE_OMIT_LOAD_EXTENSION
1345   ".load FILE ?ENTRY?     Load an extension library\n"
1346 #endif
1347   ".log FILE|off          Turn logging on or off.  FILE can be stderr/stdout\n"
1348   ".mode MODE ?TABLE?     Set output mode where MODE is one of:\n"
1349   "                         csv      Comma-separated values\n"
1350   "                         column   Left-aligned columns.  (See .width)\n"
1351   "                         html     HTML <table> code\n"
1352   "                         insert   SQL insert statements for TABLE\n"
1353   "                         line     One value per line\n"
1354   "                         list     Values delimited by .separator string\n"
1355   "                         tabs     Tab-separated values\n"
1356   "                         tcl      TCL list elements\n"
1357   ".nullvalue STRING      Print STRING in place of NULL values\n"
1358   ".output FILENAME       Send output to FILENAME\n"
1359   ".output stdout         Send output to the screen\n"
1360   ".prompt MAIN CONTINUE  Replace the standard prompts\n"
1361   ".quit                  Exit this program\n"
1362   ".read FILENAME         Execute SQL in FILENAME\n"
1363   ".restore ?DB? FILE     Restore content of DB (default \"main\") from FILE\n"
1364   ".schema ?TABLE?        Show the CREATE statements\n"
1365   "                         If TABLE specified, only show tables matching\n"
1366   "                         LIKE pattern TABLE.\n"
1367   ".separator STRING      Change separator used by output mode and .import\n"
1368   ".show                  Show the current values for various settings\n"
1369   ".stat ?ITEM?           Print statistics\n"
1370   "                         If ITEM=':env:', print statistics for the\n"
1371   "                         Berkeley DB environment.\n"
1372   "                         If ITEM=':rep:', print a summary of replication\n"
1373   "                         statistics for the Berkeley DB environment.\n"
1374   "                         If ITEM is the name of a table or index, print\n"
1375   "                         statistics for the table or index.\n"
1376   "                         If ITEM is not specified, print statistics for\n"
1377   "                         the Berkeley DB environment followed by\n"
1378   "                         statistics for all tables and indexes within the\n"
1379   "                         database.\n"
1380   ".stats ON|OFF          Turn stats on or off\n"
1381   ".tables ?TABLE?        List names of tables\n"
1382   "                         If TABLE specified, only list tables matching\n"
1383   "                         LIKE pattern TABLE.\n"
1384   ".timeout MS            Try opening locked tables for MS milliseconds\n"
1385   ".width NUM1 NUM2 ...   Set column widths for \"column\" mode\n"
1386 ;
1387 
1388 static char zTimerHelp[] =
1389   ".timer ON|OFF          Turn the CPU timer measurement on or off\n"
1390 ;
1391 
1392 /* Forward reference */
1393 static int process_input(struct callback_data *p, FILE *in);
1394 
1395 /*
1396 ** Make sure the database is open.  If it is not, then open it.  If
1397 ** the database fails to open, print an error message and exit.
1398 */
open_db(struct callback_data * p)1399 static void open_db(struct callback_data *p){
1400   if( p->db==0 ){
1401     sqlite3_open(p->zDbFilename, &p->db);
1402     db = p->db;
1403     if( db && sqlite3_errcode(db)==SQLITE_OK ){
1404       sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0,
1405           shellstaticFunc, 0, 0);
1406     }
1407 #ifdef ANDROID
1408 #ifndef NO_ANDROID_FUNCS
1409     register_android_functions(db, 0);
1410     register_localized_collators(db, "", 0);
1411 #endif
1412 #endif
1413 
1414     if( db==0 || SQLITE_OK!=sqlite3_errcode(db) ){
1415       fprintf(stderr,"Error: unable to open database \"%s\": %s\n",
1416           p->zDbFilename, sqlite3_errmsg(db));
1417       exit(1);
1418     }
1419 #ifndef SQLITE_OMIT_LOAD_EXTENSION
1420     sqlite3_enable_load_extension(p->db, 1);
1421 #endif
1422   }
1423 }
1424 
1425 /*
1426 ** Do C-language style dequoting.
1427 **
1428 **    \t    -> tab
1429 **    \n    -> newline
1430 **    \r    -> carriage return
1431 **    \NNN  -> ascii character NNN in octal
1432 **    \\    -> backslash
1433 */
resolve_backslashes(char * z)1434 static void resolve_backslashes(char *z){
1435   int i, j;
1436   char c;
1437   for(i=j=0; (c = z[i])!=0; i++, j++){
1438     if( c=='\\' ){
1439       c = z[++i];
1440       if( c=='n' ){
1441         c = '\n';
1442       }else if( c=='t' ){
1443         c = '\t';
1444       }else if( c=='r' ){
1445         c = '\r';
1446       }else if( c>='0' && c<='7' ){
1447         c -= '0';
1448         if( z[i+1]>='0' && z[i+1]<='7' ){
1449           i++;
1450           c = (c<<3) + z[i] - '0';
1451           if( z[i+1]>='0' && z[i+1]<='7' ){
1452             i++;
1453             c = (c<<3) + z[i] - '0';
1454           }
1455         }
1456       }
1457     }
1458     z[j] = c;
1459   }
1460   z[j] = 0;
1461 }
1462 
1463 /*
1464 ** Interpret zArg as a boolean value.  Return either 0 or 1.
1465 */
booleanValue(char * zArg)1466 static int booleanValue(char *zArg){
1467   int val = atoi(zArg);
1468   int j;
1469   for(j=0; zArg[j]; j++){
1470     zArg[j] = (char)tolower(zArg[j]);
1471   }
1472   if( strcmp(zArg,"on")==0 ){
1473     val = 1;
1474   }else if( strcmp(zArg,"yes")==0 ){
1475     val = 1;
1476   }
1477   return val;
1478 }
1479 
1480 /*
1481 ** If an input line begins with "." then invoke this routine to
1482 ** process that line.
1483 **
1484 ** Return 1 on error, 2 to exit, and 0 otherwise.
1485 */
do_meta_command(char * zLine,struct callback_data * p)1486 static int do_meta_command(char *zLine, struct callback_data *p){
1487   int i = 1;
1488   int nArg = 0;
1489   int n, c;
1490   int rc = 0;
1491   char *azArg[50];
1492 
1493   /* Parse the input line into tokens.
1494   */
1495   while( zLine[i] && nArg<ArraySize(azArg) ){
1496     while( isspace((unsigned char)zLine[i]) ){ i++; }
1497     if( zLine[i]==0 ) break;
1498     if( zLine[i]=='\'' || zLine[i]=='"' ){
1499       int delim = zLine[i++];
1500       azArg[nArg++] = &zLine[i];
1501       while( zLine[i] && zLine[i]!=delim ){ i++; }
1502       if( zLine[i]==delim ){
1503         zLine[i++] = 0;
1504       }
1505       if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
1506     }else{
1507       azArg[nArg++] = &zLine[i];
1508       while( zLine[i] && !isspace((unsigned char)zLine[i]) ){ i++; }
1509       if( zLine[i] ) zLine[i++] = 0;
1510       resolve_backslashes(azArg[nArg-1]);
1511     }
1512   }
1513 
1514   /* Process the input line.
1515   */
1516   if( nArg==0 ) return 0; /* no tokens, no error */
1517   n = strlen30(azArg[0]);
1518   c = azArg[0][0];
1519   if( c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0 && nArg>1 && nArg<4){
1520     const char *zDestFile;
1521     const char *zDb;
1522     sqlite3 *pDest;
1523     sqlite3_backup *pBackup;
1524     if( nArg==2 ){
1525       zDestFile = azArg[1];
1526       zDb = "main";
1527     }else{
1528       zDestFile = azArg[2];
1529       zDb = azArg[1];
1530     }
1531     rc = sqlite3_open(zDestFile, &pDest);
1532     if( rc!=SQLITE_OK ){
1533       fprintf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
1534       sqlite3_close(pDest);
1535       return 1;
1536     }
1537     open_db(p);
1538     pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
1539     if( pBackup==0 ){
1540       fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
1541       sqlite3_close(pDest);
1542       return 1;
1543     }
1544     while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
1545     sqlite3_backup_finish(pBackup);
1546     if( rc==SQLITE_DONE ){
1547       rc = 0;
1548     }else{
1549       fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
1550       rc = 1;
1551     }
1552     sqlite3_close(pDest);
1553   }else
1554 
1555   if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 && nArg>1 && nArg<3 ){
1556     bail_on_error = booleanValue(azArg[1]);
1557   }else
1558 
1559   if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 && nArg==1 ){
1560     struct callback_data data;
1561     char *zErrMsg = 0;
1562     open_db(p);
1563     memcpy(&data, p, sizeof(data));
1564     data.showHeader = 1;
1565     data.mode = MODE_Column;
1566     data.colWidth[0] = 3;
1567     data.colWidth[1] = 15;
1568     data.colWidth[2] = 58;
1569     data.cnt = 0;
1570     sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
1571     if( zErrMsg ){
1572       fprintf(stderr,"Error: %s\n", zErrMsg);
1573       sqlite3_free(zErrMsg);
1574       rc = 1;
1575     }
1576   }else
1577 
1578   if( c=='d' && strncmp(azArg[0], "dump", n)==0 && nArg<3 ){
1579     char *zErrMsg = 0;
1580     open_db(p);
1581     /* When playing back a "dump", the content might appear in an order
1582     ** which causes immediate foreign key constraints to be violated.
1583     ** So disable foreign-key constraint enforcement to prevent problems. */
1584     fprintf(p->out, "PRAGMA foreign_keys=OFF;\n");
1585     fprintf(p->out, "BEGIN TRANSACTION;\n");
1586     p->writableSchema = 0;
1587     sqlite3_exec(p->db, "PRAGMA writable_schema=ON", 0, 0, 0);
1588     if( nArg==1 ){
1589       run_schema_dump_query(p,
1590         "SELECT name, type, sql FROM sqlite_master "
1591         "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'", 0
1592       );
1593       run_schema_dump_query(p,
1594         "SELECT name, type, sql FROM sqlite_master "
1595         "WHERE name=='sqlite_sequence'", 0
1596       );
1597       run_table_dump_query(p->out, p->db,
1598         "SELECT sql FROM sqlite_master "
1599         "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
1600       );
1601     }else{
1602       int i;
1603       for(i=1; i<nArg; i++){
1604         zShellStatic = azArg[i];
1605         run_schema_dump_query(p,
1606           "SELECT name, type, sql FROM sqlite_master "
1607           "WHERE tbl_name LIKE shellstatic() AND type=='table'"
1608           "  AND sql NOT NULL", 0);
1609         run_table_dump_query(p->out, p->db,
1610           "SELECT sql FROM sqlite_master "
1611           "WHERE sql NOT NULL"
1612           "  AND type IN ('index','trigger','view')"
1613           "  AND tbl_name LIKE shellstatic()", 0
1614         );
1615         zShellStatic = 0;
1616       }
1617     }
1618     if( p->writableSchema ){
1619       fprintf(p->out, "PRAGMA writable_schema=OFF;\n");
1620       p->writableSchema = 0;
1621     }
1622     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF", 0, 0, 0);
1623     if( zErrMsg ){
1624       fprintf(stderr,"Error: %s\n", zErrMsg);
1625       sqlite3_free(zErrMsg);
1626     }else{
1627       fprintf(p->out, "COMMIT;\n");
1628     }
1629   }else
1630 
1631   if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 && nArg<3 ){
1632     p->echoOn = booleanValue(azArg[1]);
1633   }else
1634 
1635   if( c=='e' && strncmp(azArg[0], "exit", n)==0  && nArg==1 ){
1636     rc = 2;
1637   }else
1638 
1639   if( c=='e' && strncmp(azArg[0], "explain", n)==0 && nArg<3 ){
1640     int val = nArg>=2 ? booleanValue(azArg[1]) : 1;
1641     if(val == 1) {
1642       if(!p->explainPrev.valid) {
1643         p->explainPrev.valid = 1;
1644         p->explainPrev.mode = p->mode;
1645         p->explainPrev.showHeader = p->showHeader;
1646         memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
1647       }
1648       /* We could put this code under the !p->explainValid
1649       ** condition so that it does not execute if we are already in
1650       ** explain mode. However, always executing it allows us an easy
1651       ** was to reset to explain mode in case the user previously
1652       ** did an .explain followed by a .width, .mode or .header
1653       ** command.
1654       */
1655       p->mode = MODE_Explain;
1656       p->showHeader = 1;
1657       memset(p->colWidth,0,ArraySize(p->colWidth));
1658       p->colWidth[0] = 4;                  /* addr */
1659       p->colWidth[1] = 13;                 /* opcode */
1660       p->colWidth[2] = 4;                  /* P1 */
1661       p->colWidth[3] = 4;                  /* P2 */
1662       p->colWidth[4] = 4;                  /* P3 */
1663       p->colWidth[5] = 13;                 /* P4 */
1664       p->colWidth[6] = 2;                  /* P5 */
1665       p->colWidth[7] = 13;                  /* Comment */
1666     }else if (p->explainPrev.valid) {
1667       p->explainPrev.valid = 0;
1668       p->mode = p->explainPrev.mode;
1669       p->showHeader = p->explainPrev.showHeader;
1670       memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
1671     }
1672   }else
1673 
1674   if( c=='h' && (strncmp(azArg[0], "header", n)==0 ||
1675                  strncmp(azArg[0], "headers", n)==0) && nArg>1 && nArg<3 ){
1676     p->showHeader = booleanValue(azArg[1]);
1677   }else
1678 
1679   if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
1680     fprintf(stderr,"%s",zHelp);
1681     if( HAS_TIMER ){
1682       fprintf(stderr,"%s",zTimerHelp);
1683     }
1684   }else
1685 
1686   if( c=='i' && strncmp(azArg[0], "import", n)==0 && nArg==3 ){
1687     char *zTable = azArg[2];    /* Insert data into this table */
1688     char *zFile = azArg[1];     /* The file from which to extract data */
1689     sqlite3_stmt *pStmt = NULL; /* A statement */
1690     int nCol;                   /* Number of columns in the table */
1691     int nByte;                  /* Number of bytes in an SQL string */
1692     int i, j;                   /* Loop counters */
1693     int nSep;                   /* Number of bytes in p->separator[] */
1694     char *zSql;                 /* An SQL statement */
1695     char *zLine;                /* A single line of input from the file */
1696     char **azCol;               /* zLine[] broken up into columns */
1697     char *zCommit;              /* How to commit changes */
1698     FILE *in;                   /* The input file */
1699     int lineno = 0;             /* Line number of input file */
1700 
1701     open_db(p);
1702     nSep = strlen30(p->separator);
1703     if( nSep==0 ){
1704       fprintf(stderr, "Error: non-null separator required for import\n");
1705       return 1;
1706     }
1707     zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
1708     if( zSql==0 ){
1709       fprintf(stderr, "Error: out of memory\n");
1710       return 1;
1711     }
1712     nByte = strlen30(zSql);
1713     rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
1714     sqlite3_free(zSql);
1715     if( rc ){
1716       if (pStmt) sqlite3_finalize(pStmt);
1717       fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
1718       return 1;
1719     }
1720     nCol = sqlite3_column_count(pStmt);
1721     sqlite3_finalize(pStmt);
1722     pStmt = 0;
1723     if( nCol==0 ) return 0; /* no columns, no error */
1724     zSql = malloc( nByte + 20 + nCol*2 );
1725     if( zSql==0 ){
1726       fprintf(stderr, "Error: out of memory\n");
1727       return 1;
1728     }
1729     sqlite3_snprintf(nByte+20, zSql, "INSERT INTO '%q' VALUES(?", zTable);
1730     j = strlen30(zSql);
1731     for(i=1; i<nCol; i++){
1732       zSql[j++] = ',';
1733       zSql[j++] = '?';
1734     }
1735     zSql[j++] = ')';
1736     zSql[j] = 0;
1737     rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
1738     free(zSql);
1739     if( rc ){
1740       fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
1741       if (pStmt) sqlite3_finalize(pStmt);
1742       return 1;
1743     }
1744     in = fopen(zFile, "rb");
1745     if( in==0 ){
1746       fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);
1747       sqlite3_finalize(pStmt);
1748       return 1;
1749     }
1750     azCol = malloc( sizeof(azCol[0])*(nCol+1) );
1751     if( azCol==0 ){
1752       fprintf(stderr, "Error: out of memory\n");
1753       fclose(in);
1754       sqlite3_finalize(pStmt);
1755       return 1;
1756     }
1757     sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
1758     zCommit = "COMMIT";
1759     while( (zLine = local_getline(0, in))!=0 ){
1760       char *z;
1761       i = 0;
1762       lineno++;
1763       azCol[0] = zLine;
1764       for(i=0, z=zLine; *z && *z!='\n' && *z!='\r'; z++){
1765         if( *z==p->separator[0] && strncmp(z, p->separator, nSep)==0 ){
1766           *z = 0;
1767           i++;
1768           if( i<nCol ){
1769             azCol[i] = &z[nSep];
1770             z += nSep-1;
1771           }
1772         }
1773       } /* end for */
1774       *z = 0;
1775       if( i+1!=nCol ){
1776         fprintf(stderr,
1777                 "Error: %s line %d: expected %d columns of data but found %d\n",
1778                 zFile, lineno, nCol, i+1);
1779         zCommit = "ROLLBACK";
1780         free(zLine);
1781         rc = 1;
1782         break; /* from while */
1783       }
1784       for(i=0; i<nCol; i++){
1785         sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
1786       }
1787       sqlite3_step(pStmt);
1788       rc = sqlite3_reset(pStmt);
1789       free(zLine);
1790       if( rc!=SQLITE_OK ){
1791         fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
1792         zCommit = "ROLLBACK";
1793         rc = 1;
1794         break; /* from while */
1795       }
1796     } /* end while */
1797     free(azCol);
1798     fclose(in);
1799     sqlite3_finalize(pStmt);
1800     sqlite3_exec(p->db, zCommit, 0, 0, 0);
1801   }else
1802 
1803   if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg<3 ){
1804     struct callback_data data;
1805     char *zErrMsg = 0;
1806     open_db(p);
1807     memcpy(&data, p, sizeof(data));
1808     data.showHeader = 0;
1809     data.mode = MODE_List;
1810     if( nArg==1 ){
1811       rc = sqlite3_exec(p->db,
1812         "SELECT name FROM sqlite_master "
1813         "WHERE type='index' AND name NOT LIKE 'sqlite_%' "
1814         "UNION ALL "
1815         "SELECT name FROM sqlite_temp_master "
1816         "WHERE type='index' "
1817         "ORDER BY 1",
1818         callback, &data, &zErrMsg
1819       );
1820     }else{
1821       zShellStatic = azArg[1];
1822       rc = sqlite3_exec(p->db,
1823         "SELECT name FROM sqlite_master "
1824         "WHERE type='index' AND tbl_name LIKE shellstatic() "
1825         "UNION ALL "
1826         "SELECT name FROM sqlite_temp_master "
1827         "WHERE type='index' AND tbl_name LIKE shellstatic() "
1828         "ORDER BY 1",
1829         callback, &data, &zErrMsg
1830       );
1831       zShellStatic = 0;
1832     }
1833     if( zErrMsg ){
1834       fprintf(stderr,"Error: %s\n", zErrMsg);
1835       sqlite3_free(zErrMsg);
1836       rc = 1;
1837     }else if( rc != SQLITE_OK ){
1838       fprintf(stderr,"Error: querying sqlite_master and sqlite_temp_master\n");
1839       rc = 1;
1840     }
1841   }else
1842 
1843 #ifdef SQLITE_ENABLE_IOTRACE
1844   if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
1845     extern void (*sqlite3IoTrace)(const char*, ...);
1846     if( iotrace && iotrace!=stdout ) fclose(iotrace);
1847     iotrace = 0;
1848     if( nArg<2 ){
1849       sqlite3IoTrace = 0;
1850     }else if( strcmp(azArg[1], "-")==0 ){
1851       sqlite3IoTrace = iotracePrintf;
1852       iotrace = stdout;
1853     }else{
1854       iotrace = fopen(azArg[1], "w");
1855       if( iotrace==0 ){
1856         fprintf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
1857         sqlite3IoTrace = 0;
1858         rc = 1;
1859       }else{
1860         sqlite3IoTrace = iotracePrintf;
1861       }
1862     }
1863   }else
1864 #endif
1865 
1866 #ifndef SQLITE_OMIT_LOAD_EXTENSION
1867   if( c=='l' && strncmp(azArg[0], "load", n)==0 && nArg>=2 ){
1868     const char *zFile, *zProc;
1869     char *zErrMsg = 0;
1870     zFile = azArg[1];
1871     zProc = nArg>=3 ? azArg[2] : 0;
1872     open_db(p);
1873     rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
1874     if( rc!=SQLITE_OK ){
1875       fprintf(stderr, "Error: %s\n", zErrMsg);
1876       sqlite3_free(zErrMsg);
1877       rc = 1;
1878     }
1879   }else
1880 #endif
1881 
1882   if( c=='l' && strncmp(azArg[0], "log", n)==0 && nArg>=2 ){
1883     const char *zFile = azArg[1];
1884     if( p->pLog && p->pLog!=stdout && p->pLog!=stderr ){
1885       fclose(p->pLog);
1886       p->pLog = 0;
1887     }
1888     if( strcmp(zFile,"stdout")==0 ){
1889       p->pLog = stdout;
1890     }else if( strcmp(zFile, "stderr")==0 ){
1891       p->pLog = stderr;
1892     }else if( strcmp(zFile, "off")==0 ){
1893       p->pLog = 0;
1894     }else{
1895       p->pLog = fopen(zFile, "w");
1896       if( p->pLog==0 ){
1897         fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);
1898       }
1899     }
1900   }else
1901 
1902   if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg==2 ){
1903     int n2 = strlen30(azArg[1]);
1904     if( (n2==4 && strncmp(azArg[1],"line",n2)==0)
1905         ||
1906         (n2==5 && strncmp(azArg[1],"lines",n2)==0) ){
1907       p->mode = MODE_Line;
1908     }else if( (n2==6 && strncmp(azArg[1],"column",n2)==0)
1909               ||
1910               (n2==7 && strncmp(azArg[1],"columns",n2)==0) ){
1911       p->mode = MODE_Column;
1912     }else if( n2==4 && strncmp(azArg[1],"list",n2)==0 ){
1913       p->mode = MODE_List;
1914     }else if( n2==4 && strncmp(azArg[1],"html",n2)==0 ){
1915       p->mode = MODE_Html;
1916     }else if( n2==3 && strncmp(azArg[1],"tcl",n2)==0 ){
1917       p->mode = MODE_Tcl;
1918     }else if( n2==3 && strncmp(azArg[1],"csv",n2)==0 ){
1919       p->mode = MODE_Csv;
1920       sqlite3_snprintf(sizeof(p->separator), p->separator, ",");
1921     }else if( n2==4 && strncmp(azArg[1],"tabs",n2)==0 ){
1922       p->mode = MODE_List;
1923       sqlite3_snprintf(sizeof(p->separator), p->separator, "\t");
1924     }else if( n2==6 && strncmp(azArg[1],"insert",n2)==0 ){
1925       p->mode = MODE_Insert;
1926       set_table_name(p, "table");
1927     }else {
1928       fprintf(stderr,"Error: mode should be one of: "
1929          "column csv html insert line list tabs tcl\n");
1930       rc = 1;
1931     }
1932   }else
1933 
1934   if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg==3 ){
1935     int n2 = strlen30(azArg[1]);
1936     if( n2==6 && strncmp(azArg[1],"insert",n2)==0 ){
1937       p->mode = MODE_Insert;
1938       set_table_name(p, azArg[2]);
1939     }else {
1940       fprintf(stderr, "Error: invalid arguments: "
1941         " \"%s\". Enter \".help\" for help\n", azArg[2]);
1942       rc = 1;
1943     }
1944   }else
1945 
1946   if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
1947     sqlite3_snprintf(sizeof(p->nullvalue), p->nullvalue,
1948                      "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
1949   }else
1950 
1951   if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
1952     if( p->out!=stdout ){
1953       fclose(p->out);
1954     }
1955     if( strcmp(azArg[1],"stdout")==0 ){
1956       p->out = stdout;
1957       sqlite3_snprintf(sizeof(p->outfile), p->outfile, "stdout");
1958     }else{
1959       p->out = fopen(azArg[1], "wb");
1960       if( p->out==0 ){
1961         fprintf(stderr,"Error: cannot write to \"%s\"\n", azArg[1]);
1962         p->out = stdout;
1963         rc = 1;
1964       } else {
1965          sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
1966       }
1967     }
1968   }else
1969 
1970   if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
1971     if( nArg >= 2) {
1972       strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
1973     }
1974     if( nArg >= 3) {
1975       strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
1976     }
1977   }else
1978 
1979   if( c=='q' && strncmp(azArg[0], "quit", n)==0 && nArg==1 ){
1980     rc = 2;
1981   }else
1982 
1983   if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
1984     FILE *alt = fopen(azArg[1], "rb");
1985     if( alt==0 ){
1986       fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
1987       rc = 1;
1988     }else{
1989       rc = process_input(p, alt);
1990       fclose(alt);
1991     }
1992   }else
1993 
1994   if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 && nArg>1 && nArg<4){
1995     const char *zSrcFile;
1996     const char *zDb;
1997     sqlite3 *pSrc;
1998     sqlite3_backup *pBackup;
1999     int nTimeout = 0;
2000 
2001     if( nArg==2 ){
2002       zSrcFile = azArg[1];
2003       zDb = "main";
2004     }else{
2005       zSrcFile = azArg[2];
2006       zDb = azArg[1];
2007     }
2008     rc = sqlite3_open(zSrcFile, &pSrc);
2009     if( rc!=SQLITE_OK ){
2010       fprintf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
2011       sqlite3_close(pSrc);
2012       return 1;
2013     }
2014     open_db(p);
2015     pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
2016     if( pBackup==0 ){
2017       fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
2018       sqlite3_close(pSrc);
2019       return 1;
2020     }
2021     while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
2022           || rc==SQLITE_BUSY  ){
2023       if( rc==SQLITE_BUSY ){
2024         if( nTimeout++ >= 3 ) break;
2025         sqlite3_sleep(100);
2026       }
2027     }
2028     sqlite3_backup_finish(pBackup);
2029     if( rc==SQLITE_DONE ){
2030       rc = 0;
2031     }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
2032       fprintf(stderr, "Error: source database is busy\n");
2033       rc = 1;
2034     }else{
2035       fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
2036       rc = 1;
2037     }
2038     sqlite3_close(pSrc);
2039   }else
2040 
2041   if( c=='s' && strncmp(azArg[0], "schema", n)==0 && nArg<3 ){
2042     struct callback_data data;
2043     char *zErrMsg = 0;
2044     open_db(p);
2045     memcpy(&data, p, sizeof(data));
2046     data.showHeader = 0;
2047     data.mode = MODE_Semi;
2048     if( nArg>1 ){
2049       int i;
2050       for(i=0; azArg[1][i]; i++) azArg[1][i] = (char)tolower(azArg[1][i]);
2051       if( strcmp(azArg[1],"sqlite_master")==0 ){
2052         char *new_argv[2], *new_colv[2];
2053         new_argv[0] = "CREATE TABLE sqlite_master (\n"
2054                       "  type text,\n"
2055                       "  name text,\n"
2056                       "  tbl_name text,\n"
2057                       "  rootpage integer,\n"
2058                       "  sql text\n"
2059                       ")";
2060         new_argv[1] = 0;
2061         new_colv[0] = "sql";
2062         new_colv[1] = 0;
2063         callback(&data, 1, new_argv, new_colv);
2064         rc = SQLITE_OK;
2065       }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){
2066         char *new_argv[2], *new_colv[2];
2067         new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
2068                       "  type text,\n"
2069                       "  name text,\n"
2070                       "  tbl_name text,\n"
2071                       "  rootpage integer,\n"
2072                       "  sql text\n"
2073                       ")";
2074         new_argv[1] = 0;
2075         new_colv[0] = "sql";
2076         new_colv[1] = 0;
2077         callback(&data, 1, new_argv, new_colv);
2078         rc = SQLITE_OK;
2079       }else{
2080         zShellStatic = azArg[1];
2081         rc = sqlite3_exec(p->db,
2082           "SELECT sql FROM "
2083           "  (SELECT sql sql, type type, tbl_name tbl_name, name name"
2084           "     FROM sqlite_master UNION ALL"
2085           "   SELECT sql, type, tbl_name, name FROM sqlite_temp_master) "
2086           "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOTNULL "
2087           "ORDER BY substr(type,2,1), name",
2088           callback, &data, &zErrMsg);
2089         zShellStatic = 0;
2090       }
2091     }else{
2092       rc = sqlite3_exec(p->db,
2093          "SELECT sql FROM "
2094          "  (SELECT sql sql, type type, tbl_name tbl_name, name name"
2095          "     FROM sqlite_master UNION ALL"
2096          "   SELECT sql, type, tbl_name, name FROM sqlite_temp_master) "
2097          "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
2098          "ORDER BY substr(type,2,1), name",
2099          callback, &data, &zErrMsg
2100       );
2101     }
2102     if( zErrMsg ){
2103       fprintf(stderr,"Error: %s\n", zErrMsg);
2104       sqlite3_free(zErrMsg);
2105       rc = 1;
2106     }else if( rc != SQLITE_OK ){
2107       fprintf(stderr,"Error: querying schema information\n");
2108       rc = 1;
2109     }else{
2110       rc = 0;
2111     }
2112   }else
2113 
2114   if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
2115     sqlite3_snprintf(sizeof(p->separator), p->separator,
2116                      "%.*s", (int)sizeof(p->separator)-1, azArg[1]);
2117   }else
2118 
2119   if( c=='s' && strncmp(azArg[0], "show", n)==0 && nArg==1 ){
2120     int i;
2121     fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
2122     fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
2123     fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
2124     fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
2125     fprintf(p->out,"%9.9s: ", "nullvalue");
2126       output_c_string(p->out, p->nullvalue);
2127       fprintf(p->out, "\n");
2128     fprintf(p->out,"%9.9s: %s\n","output",
2129             strlen30(p->outfile) ? p->outfile : "stdout");
2130     fprintf(p->out,"%9.9s: ", "separator");
2131       output_c_string(p->out, p->separator);
2132       fprintf(p->out, "\n");
2133     fprintf(p->out,"%9.9s: %s\n","stats", p->statsOn ? "on" : "off");
2134     fprintf(p->out,"%9.9s: ","width");
2135     for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
2136       fprintf(p->out,"%d ",p->colWidth[i]);
2137     }
2138     fprintf(p->out,"\n");
2139   }else
2140 
2141   if( c=='s' && strncmp(azArg[0], "stat", n)==0 ){
2142     extern int bdbSqlDbStatPrint(sqlite3 *, FILE *, char *);
2143     extern int bdbSqlEnvStatPrint(sqlite3 *db, FILE *);
2144     extern int bdbSqlRepSumStatPrint(sqlite3 *db, FILE *);
2145 
2146     open_db(p);
2147 
2148     if (nArg == 1 || nArg == 2 && strcmp(azArg[1], ":env:") == 0)
2149       rc = bdbSqlEnvStatPrint(p->db, p->out);
2150     else if (nArg == 2 && strcmp(azArg[1], ":rep:") == 0)
2151       rc = bdbSqlRepSumStatPrint(p->db, p->out);
2152     if (rc != SQLITE_OK) {
2153       fprintf(stderr, "Error: environment not created yet\n");
2154       rc = 1;
2155     }
2156     else if (nArg == 1)
2157       rc = bdbSqlDbStatPrint(p->db, p->out, NULL);
2158     else
2159       rc = bdbSqlDbStatPrint(p->db, p->out, azArg[1]);
2160   }else
2161 
2162   if( c=='s' && strncmp(azArg[0], "stats", n)==0 && nArg>1 && nArg<3 ){
2163     p->statsOn = booleanValue(azArg[1]);
2164   }else
2165 
2166   if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 && nArg<3 ){
2167     char **azResult;
2168     int nRow;
2169     char *zErrMsg;
2170     open_db(p);
2171     if( nArg==1 ){
2172       rc = sqlite3_get_table(p->db,
2173         "SELECT name FROM sqlite_master "
2174         "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' "
2175         "UNION ALL "
2176         "SELECT name FROM sqlite_temp_master "
2177         "WHERE type IN ('table','view') "
2178         "ORDER BY 1",
2179         &azResult, &nRow, 0, &zErrMsg
2180       );
2181     }else{
2182       zShellStatic = azArg[1];
2183       rc = sqlite3_get_table(p->db,
2184         "SELECT name FROM sqlite_master "
2185         "WHERE type IN ('table','view') AND name LIKE shellstatic() "
2186         "UNION ALL "
2187         "SELECT name FROM sqlite_temp_master "
2188         "WHERE type IN ('table','view') AND name LIKE shellstatic() "
2189         "ORDER BY 1",
2190         &azResult, &nRow, 0, &zErrMsg
2191       );
2192       zShellStatic = 0;
2193     }
2194     if( zErrMsg ){
2195       fprintf(stderr,"Error: %s\n", zErrMsg);
2196       sqlite3_free(zErrMsg);
2197       rc = 1;
2198     }else if( rc != SQLITE_OK ){
2199       fprintf(stderr,"Error: querying sqlite_master and sqlite_temp_master\n");
2200       rc = 1;
2201     }else{
2202       int len, maxlen = 0;
2203       int i, j;
2204       int nPrintCol, nPrintRow;
2205       for(i=1; i<=nRow; i++){
2206         if( azResult[i]==0 ) continue;
2207         len = strlen30(azResult[i]);
2208         if( len>maxlen ) maxlen = len;
2209       }
2210       nPrintCol = 80/(maxlen+2);
2211       if( nPrintCol<1 ) nPrintCol = 1;
2212       nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
2213       for(i=0; i<nPrintRow; i++){
2214         for(j=i+1; j<=nRow; j+=nPrintRow){
2215           char *zSp = j<=nPrintRow ? "" : "  ";
2216           printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
2217         }
2218         printf("\n");
2219       }
2220     }
2221     sqlite3_free_table(azResult);
2222   }else
2223 
2224   if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
2225     static const struct {
2226        const char *zCtrlName;   /* Name of a test-control option */
2227        int ctrlCode;            /* Integer code for that option */
2228     } aCtrl[] = {
2229       { "prng_save",             SQLITE_TESTCTRL_PRNG_SAVE              },
2230       { "prng_restore",          SQLITE_TESTCTRL_PRNG_RESTORE           },
2231       { "prng_reset",            SQLITE_TESTCTRL_PRNG_RESET             },
2232       { "bitvec_test",           SQLITE_TESTCTRL_BITVEC_TEST            },
2233       { "fault_install",         SQLITE_TESTCTRL_FAULT_INSTALL          },
2234       { "benign_malloc_hooks",   SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS    },
2235       { "pending_byte",          SQLITE_TESTCTRL_PENDING_BYTE           },
2236       { "assert",                SQLITE_TESTCTRL_ASSERT                 },
2237       { "always",                SQLITE_TESTCTRL_ALWAYS                 },
2238       { "reserve",               SQLITE_TESTCTRL_RESERVE                },
2239       { "optimizations",         SQLITE_TESTCTRL_OPTIMIZATIONS          },
2240       { "iskeyword",             SQLITE_TESTCTRL_ISKEYWORD              },
2241       { "pghdrsz",               SQLITE_TESTCTRL_PGHDRSZ                },
2242       { "scratchmalloc",         SQLITE_TESTCTRL_SCRATCHMALLOC          },
2243     };
2244     int testctrl = -1;
2245     int rc = 0;
2246     int i, n;
2247     open_db(p);
2248 
2249     /* convert testctrl text option to value. allow any unique prefix
2250     ** of the option name, or a numerical value. */
2251     n = strlen30(azArg[1]);
2252     for(i=0; i<(int)(sizeof(aCtrl)/sizeof(aCtrl[0])); i++){
2253       if( strncmp(azArg[1], aCtrl[i].zCtrlName, n)==0 ){
2254         if( testctrl<0 ){
2255           testctrl = aCtrl[i].ctrlCode;
2256         }else{
2257           fprintf(stderr, "ambiguous option name: \"%s\"\n", azArg[i]);
2258           testctrl = -1;
2259           break;
2260         }
2261       }
2262     }
2263     if( testctrl<0 ) testctrl = atoi(azArg[1]);
2264     if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){
2265       fprintf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]);
2266     }else{
2267       switch(testctrl){
2268 
2269         /* sqlite3_test_control(int, db, int) */
2270         case SQLITE_TESTCTRL_OPTIMIZATIONS:
2271         case SQLITE_TESTCTRL_RESERVE:
2272           if( nArg==3 ){
2273             int opt = (int)strtol(azArg[2], 0, 0);
2274             rc = sqlite3_test_control(testctrl, p->db, opt);
2275             printf("%d (0x%08x)\n", rc, rc);
2276           } else {
2277             fprintf(stderr,"Error: testctrl %s takes a single int option\n",
2278                     azArg[1]);
2279           }
2280           break;
2281 
2282         /* sqlite3_test_control(int) */
2283         case SQLITE_TESTCTRL_PRNG_SAVE:
2284         case SQLITE_TESTCTRL_PRNG_RESTORE:
2285         case SQLITE_TESTCTRL_PRNG_RESET:
2286         case SQLITE_TESTCTRL_PGHDRSZ:
2287           if( nArg==2 ){
2288             rc = sqlite3_test_control(testctrl);
2289             printf("%d (0x%08x)\n", rc, rc);
2290           } else {
2291             fprintf(stderr,"Error: testctrl %s takes no options\n", azArg[1]);
2292           }
2293           break;
2294 
2295         /* sqlite3_test_control(int, uint) */
2296         case SQLITE_TESTCTRL_PENDING_BYTE:
2297           if( nArg==3 ){
2298             unsigned int opt = (unsigned int)atoi(azArg[2]);
2299             rc = sqlite3_test_control(testctrl, opt);
2300             printf("%d (0x%08x)\n", rc, rc);
2301           } else {
2302             fprintf(stderr,"Error: testctrl %s takes a single unsigned"
2303                            " int option\n", azArg[1]);
2304           }
2305           break;
2306 
2307         /* sqlite3_test_control(int, int) */
2308         case SQLITE_TESTCTRL_ASSERT:
2309         case SQLITE_TESTCTRL_ALWAYS:
2310           if( nArg==3 ){
2311             int opt = atoi(azArg[2]);
2312             rc = sqlite3_test_control(testctrl, opt);
2313             printf("%d (0x%08x)\n", rc, rc);
2314           } else {
2315             fprintf(stderr,"Error: testctrl %s takes a single int option\n",
2316                             azArg[1]);
2317           }
2318           break;
2319 
2320         /* sqlite3_test_control(int, char *) */
2321 #ifdef SQLITE_N_KEYWORD
2322         case SQLITE_TESTCTRL_ISKEYWORD:
2323           if( nArg==3 ){
2324             const char *opt = azArg[2];
2325             rc = sqlite3_test_control(testctrl, opt);
2326             printf("%d (0x%08x)\n", rc, rc);
2327           } else {
2328             fprintf(stderr,"Error: testctrl %s takes a single char * option\n",
2329                             azArg[1]);
2330           }
2331           break;
2332 #endif
2333 
2334         case SQLITE_TESTCTRL_BITVEC_TEST:
2335         case SQLITE_TESTCTRL_FAULT_INSTALL:
2336         case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS:
2337         case SQLITE_TESTCTRL_SCRATCHMALLOC:
2338         default:
2339           fprintf(stderr,"Error: CLI support for testctrl %s not implemented\n",
2340                   azArg[1]);
2341           break;
2342       }
2343     }
2344   }else
2345 
2346   if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 && nArg==2 ){
2347     open_db(p);
2348     sqlite3_busy_timeout(p->db, atoi(azArg[1]));
2349   }else
2350 
2351   if( HAS_TIMER && c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0
2352    && nArg==2
2353   ){
2354     enableTimer = booleanValue(azArg[1]);
2355   }else
2356 
2357   if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){
2358     int j;
2359     assert( nArg<=ArraySize(azArg) );
2360     for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
2361       p->colWidth[j-1] = atoi(azArg[j]);
2362     }
2363   }else
2364 
2365   {
2366     fprintf(stderr, "Error: unknown command or invalid arguments: "
2367       " \"%s\". Enter \".help\" for help\n", azArg[0]);
2368     rc = 1;
2369   }
2370 
2371   return rc;
2372 }
2373 
2374 /*
2375 ** Return TRUE if a semicolon occurs anywhere in the first N characters
2376 ** of string z[].
2377 */
_contains_semicolon(const char * z,int N)2378 static int _contains_semicolon(const char *z, int N){
2379   int i;
2380   for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
2381   return 0;
2382 }
2383 
2384 /*
2385 ** Test to see if a line consists entirely of whitespace.
2386 */
_all_whitespace(const char * z)2387 static int _all_whitespace(const char *z){
2388   for(; *z; z++){
2389     if( isspace(*(unsigned char*)z) ) continue;
2390     if( *z=='/' && z[1]=='*' ){
2391       z += 2;
2392       while( *z && (*z!='*' || z[1]!='/') ){ z++; }
2393       if( *z==0 ) return 0;
2394       z++;
2395       continue;
2396     }
2397     if( *z=='-' && z[1]=='-' ){
2398       z += 2;
2399       while( *z && *z!='\n' ){ z++; }
2400       if( *z==0 ) return 1;
2401       continue;
2402     }
2403     return 0;
2404   }
2405   return 1;
2406 }
2407 
2408 /*
2409 ** Return TRUE if the line typed in is an SQL command terminator other
2410 ** than a semi-colon.  The SQL Server style "go" command is understood
2411 ** as is the Oracle "/".
2412 */
_is_command_terminator(const char * zLine)2413 static int _is_command_terminator(const char *zLine){
2414   while( isspace(*(unsigned char*)zLine) ){ zLine++; };
2415   if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
2416     return 1;  /* Oracle */
2417   }
2418   if( tolower(zLine[0])=='g' && tolower(zLine[1])=='o'
2419          && _all_whitespace(&zLine[2]) ){
2420     return 1;  /* SQL Server */
2421   }
2422   return 0;
2423 }
2424 
2425 /*
2426 ** Return true if zSql is a complete SQL statement.  Return false if it
2427 ** ends in the middle of a string literal or C-style comment.
2428 */
_is_complete(char * zSql,int nSql)2429 static int _is_complete(char *zSql, int nSql){
2430   int rc;
2431   if( zSql==0 ) return 1;
2432   zSql[nSql] = ';';
2433   zSql[nSql+1] = 0;
2434   rc = sqlite3_complete(zSql);
2435   zSql[nSql] = 0;
2436   return rc;
2437 }
2438 
2439 /*
2440 ** Read input from *in and process it.  If *in==0 then input
2441 ** is interactive - the user is typing it it.  Otherwise, input
2442 ** is coming from a file or device.  A prompt is issued and history
2443 ** is saved only if input is interactive.  An interrupt signal will
2444 ** cause this routine to exit immediately, unless input is interactive.
2445 **
2446 ** Return the number of errors.
2447 */
process_input(struct callback_data * p,FILE * in)2448 static int process_input(struct callback_data *p, FILE *in){
2449   char *zLine = 0;
2450   char *zSql = 0;
2451   int nSql = 0;
2452   int nSqlPrior = 0;
2453   char *zErrMsg;
2454   int rc;
2455   int errCnt = 0;
2456   int lineno = 0;
2457   int startline = 0;
2458 
2459   while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
2460     fflush(p->out);
2461     free(zLine);
2462     zLine = one_input_line(zSql, in);
2463     if( zLine==0 ){
2464       break;  /* We have reached EOF */
2465     }
2466     if( seenInterrupt ){
2467       if( in!=0 ) break;
2468       seenInterrupt = 0;
2469     }
2470     lineno++;
2471     if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
2472     if( zLine && zLine[0]=='.' && nSql==0 ){
2473       if( p->echoOn ) printf("%s\n", zLine);
2474       rc = do_meta_command(zLine, p);
2475       if( rc==2 ){ /* exit requested */
2476         break;
2477       }else if( rc ){
2478         errCnt++;
2479       }
2480       continue;
2481     }
2482     if( _is_command_terminator(zLine) && _is_complete(zSql, nSql) ){
2483       memcpy(zLine,";",2);
2484     }
2485     nSqlPrior = nSql;
2486     if( zSql==0 ){
2487       int i;
2488       for(i=0; zLine[i] && isspace((unsigned char)zLine[i]); i++){}
2489       if( zLine[i]!=0 ){
2490         nSql = strlen30(zLine);
2491         zSql = malloc( nSql+3 );
2492         if( zSql==0 ){
2493           fprintf(stderr, "Error: out of memory\n");
2494           exit(1);
2495         }
2496         memcpy(zSql, zLine, nSql+1);
2497         startline = lineno;
2498       }
2499     }else{
2500       int len = strlen30(zLine);
2501       zSql = realloc( zSql, nSql + len + 4 );
2502       if( zSql==0 ){
2503         fprintf(stderr,"Error: out of memory\n");
2504         exit(1);
2505       }
2506       zSql[nSql++] = '\n';
2507       memcpy(&zSql[nSql], zLine, len+1);
2508       nSql += len;
2509     }
2510     if( zSql && _contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
2511                 && sqlite3_complete(zSql) ){
2512       p->cnt = 0;
2513       open_db(p);
2514       BEGIN_TIMER;
2515       rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
2516       END_TIMER;
2517       if( rc || zErrMsg ){
2518         char zPrefix[100];
2519         if( in!=0 || !stdin_is_interactive ){
2520           sqlite3_snprintf(sizeof(zPrefix), zPrefix,
2521                            "Error: near line %d:", startline);
2522         }else{
2523           sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
2524         }
2525         if( zErrMsg!=0 ){
2526           fprintf(stderr, "%s %s\n", zPrefix, zErrMsg);
2527           sqlite3_free(zErrMsg);
2528           zErrMsg = 0;
2529         }else{
2530           fprintf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
2531         }
2532         errCnt++;
2533       }
2534       free(zSql);
2535       zSql = 0;
2536       nSql = 0;
2537     }
2538   }
2539   if( zSql ){
2540     if( !_all_whitespace(zSql) ){
2541       fprintf(stderr, "Error: incomplete SQL: %s\n", zSql);
2542     }
2543     free(zSql);
2544   }
2545   free(zLine);
2546   return errCnt;
2547 }
2548 
2549 /*
2550 ** Return a pathname which is the user's home directory.  A
2551 ** 0 return indicates an error of some kind.  Space to hold the
2552 ** resulting string is obtained from malloc().  The calling
2553 ** function should free the result.
2554 */
find_home_dir(void)2555 static char *find_home_dir(void){
2556   char *home_dir = NULL;
2557 
2558 #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL)
2559   struct passwd *pwent;
2560   uid_t uid = getuid();
2561   if( (pwent=getpwuid(uid)) != NULL) {
2562     home_dir = pwent->pw_dir;
2563   }
2564 #endif
2565 
2566 #if defined(_WIN32_WCE)
2567   /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
2568    */
2569   home_dir = strdup("/");
2570 #else
2571 
2572 #if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
2573   if (!home_dir) {
2574     home_dir = getenv("USERPROFILE");
2575   }
2576 #endif
2577 
2578   if (!home_dir) {
2579     home_dir = getenv("HOME");
2580   }
2581 
2582 #if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
2583   if (!home_dir) {
2584     char *zDrive, *zPath;
2585     int n;
2586     zDrive = getenv("HOMEDRIVE");
2587     zPath = getenv("HOMEPATH");
2588     if( zDrive && zPath ){
2589       n = strlen30(zDrive) + strlen30(zPath) + 1;
2590       home_dir = malloc( n );
2591       if( home_dir==0 ) return 0;
2592       sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
2593       return home_dir;
2594     }
2595     home_dir = "c:\\";
2596   }
2597 #endif
2598 
2599 #endif /* !_WIN32_WCE */
2600 
2601   if( home_dir ){
2602     int n = strlen30(home_dir) + 1;
2603     char *z = malloc( n );
2604     if( z ) memcpy(z, home_dir, n);
2605     home_dir = z;
2606   }
2607 
2608   return home_dir;
2609 }
2610 
2611 /*
2612 ** Read input from the file given by sqliterc_override.  Or if that
2613 ** parameter is NULL, take input from ~/.sqliterc
2614 **
2615 ** Returns the number of errors.
2616 */
process_sqliterc(struct callback_data * p,const char * sqliterc_override)2617 static int process_sqliterc(
2618   struct callback_data *p,        /* Configuration data */
2619   const char *sqliterc_override   /* Name of config file. NULL to use default */
2620 ){
2621   char *home_dir = NULL;
2622   const char *sqliterc = sqliterc_override;
2623   char *zBuf = 0;
2624   FILE *in = NULL;
2625   int nBuf;
2626   int rc = 0;
2627 
2628   if (sqliterc == NULL) {
2629     home_dir = find_home_dir();
2630     if( home_dir==0 ){
2631 #if !defined(__RTP__) && !defined(_WRS_KERNEL)
2632       fprintf(stderr,"%s: Error: cannot locate your home directory\n", Argv0);
2633 #endif
2634       return 1;
2635     }
2636     nBuf = strlen30(home_dir) + 16;
2637     zBuf = malloc( nBuf );
2638     if( zBuf==0 ){
2639       fprintf(stderr,"%s: Error: out of memory\n",Argv0);
2640       return 1;
2641     }
2642     sqlite3_snprintf(nBuf, zBuf,"%s/.sqliterc",home_dir);
2643     free(home_dir);
2644     sqliterc = (const char*)zBuf;
2645   }
2646   in = fopen(sqliterc,"rb");
2647   if( in ){
2648     if( stdin_is_interactive ){
2649       fprintf(stderr,"-- Loading resources from %s\n",sqliterc);
2650     }
2651     rc = process_input(p,in);
2652     fclose(in);
2653   }
2654   free(zBuf);
2655   return rc;
2656 }
2657 
2658 /*
2659 ** Show available command line options
2660 */
2661 static const char zOptions[] =
2662   "   -help                show this message\n"
2663   "   -init filename       read/process named file\n"
2664   "   -echo                print commands before execution\n"
2665   "   -[no]header          turn headers on or off\n"
2666   "   -bail                stop after hitting an error\n"
2667   "   -interactive         force interactive I/O\n"
2668   "   -batch               force batch I/O\n"
2669   "   -column              set output mode to 'column'\n"
2670   "   -csv                 set output mode to 'csv'\n"
2671   "   -html                set output mode to HTML\n"
2672   "   -line                set output mode to 'line'\n"
2673   "   -list                set output mode to 'list'\n"
2674   "   -separator 'x'       set output field separator (|)\n"
2675   "   -stats               print memory stats before each finalize\n"
2676   "   -nullvalue 'text'    set text string for NULL values\n"
2677   "   -version             show SQLite version\n"
2678   "   -vfs NAME            use NAME as the default VFS\n"
2679 #ifdef SQLITE_ENABLE_VFSTRACE
2680   "   -vfstrace            enable tracing of all VFS calls\n"
2681 #endif
2682 ;
usage(int showDetail)2683 static void usage(int showDetail){
2684   fprintf(stderr,
2685       "Usage: %s [OPTIONS] FILENAME [SQL]\n"
2686       "FILENAME is the name of an SQLite database. A new database is created\n"
2687       "if the file does not previously exist.\n", Argv0);
2688   if( showDetail ){
2689     fprintf(stderr, "OPTIONS include:\n%s", zOptions);
2690   }else{
2691     fprintf(stderr, "Use the -help option for additional information\n");
2692   }
2693   exit(1);
2694 }
2695 
2696 /*
2697 ** Initialize the state information in data
2698 */
main_init(struct callback_data * data)2699 static void main_init(struct callback_data *data) {
2700   memset(data, 0, sizeof(*data));
2701   data->mode = MODE_List;
2702   memcpy(data->separator,"|", 2);
2703   data->showHeader = 0;
2704   sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
2705   sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"dbsql> ");
2706   sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
2707   sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
2708 }
2709 
main(int argc,char ** argv)2710 int main(int argc, char **argv){
2711   char *zErrMsg = 0;
2712   struct callback_data data;
2713   const char *zInitFile = 0;
2714   char *zFirstCmd = 0;
2715   int i;
2716   int rc = 0;
2717 
2718   Argv0 = argv[0];
2719   main_init(&data);
2720   stdin_is_interactive = isatty(0);
2721 
2722   /* Make sure we have a valid signal handler early, before anything
2723   ** else is done.
2724   */
2725 #ifdef SIGINT
2726   signal(SIGINT, interrupt_handler);
2727 #endif
2728 
2729   /* Do an initial pass through the command-line argument to locate
2730   ** the name of the database file, the name of the initialization file,
2731   ** the size of the alternative malloc heap,
2732   ** and the first command to execute.
2733   */
2734   for(i=1; i<argc-1; i++){
2735     char *z;
2736     if( argv[i][0]!='-' ) break;
2737     z = argv[i];
2738     if( z[0]=='-' && z[1]=='-' ) z++;
2739     if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){
2740       i++;
2741     }else if( strcmp(argv[i],"-init")==0 ){
2742       i++;
2743       zInitFile = argv[i];
2744     /* Need to check for batch mode here to so we can avoid printing
2745     ** informational messages (like from process_sqliterc) before
2746     ** we do the actual processing of arguments later in a second pass.
2747     */
2748     }else if( strcmp(argv[i],"-batch")==0 ){
2749       stdin_is_interactive = 0;
2750     }else if( strcmp(argv[i],"-heap")==0 ){
2751       int j, c;
2752       const char *zSize;
2753       sqlite3_int64 szHeap;
2754 
2755       zSize = argv[++i];
2756       szHeap = atoi(zSize);
2757       for(j=0; (c = zSize[j])!=0; j++){
2758         if( c=='M' ){ szHeap *= 1000000; break; }
2759         if( c=='K' ){ szHeap *= 1000; break; }
2760         if( c=='G' ){ szHeap *= 1000000000; break; }
2761       }
2762       if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
2763 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
2764       sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
2765 #endif
2766 #ifdef SQLITE_ENABLE_VFSTRACE
2767     }else if( strcmp(argv[i],"-vfstrace")==0 ){
2768       extern int vfstrace_register(
2769          const char *zTraceName,
2770          const char *zOldVfsName,
2771          int (*xOut)(const char*,void*),
2772          void *pOutArg,
2773          int makeDefault
2774       );
2775       vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
2776 #endif
2777     }else if( strcmp(argv[i],"-vfs")==0 ){
2778       sqlite3_vfs *pVfs = sqlite3_vfs_find(argv[++i]);
2779       if( pVfs ){
2780         sqlite3_vfs_register(pVfs, 1);
2781       }else{
2782         fprintf(stderr, "no such VFS: \"%s\"\n", argv[i]);
2783         exit(1);
2784       }
2785     }
2786   }
2787   if( i<argc ){
2788 #if defined(SQLITE_OS_OS2) && SQLITE_OS_OS2
2789     data.zDbFilename = (const char *)convertCpPathToUtf8( argv[i++] );
2790 #else
2791     data.zDbFilename = argv[i++];
2792 #endif
2793   }else{
2794 #ifndef SQLITE_OMIT_MEMORYDB
2795     data.zDbFilename = ":memory:";
2796 #else
2797     data.zDbFilename = 0;
2798 #endif
2799   }
2800   if( i<argc ){
2801     zFirstCmd = argv[i++];
2802   }
2803   if( i<argc ){
2804     fprintf(stderr,"%s: Error: too many options: \"%s\"\n", Argv0, argv[i]);
2805     fprintf(stderr,"Use -help for a list of options.\n");
2806     return 1;
2807   }
2808   data.out = stdout;
2809 
2810 #ifdef SQLITE_OMIT_MEMORYDB
2811   if( data.zDbFilename==0 ){
2812     fprintf(stderr,"%s: Error: no database filename specified\n", Argv0);
2813     return 1;
2814   }
2815 #endif
2816 
2817   /* Go ahead and open the database file if it already exists.  If the
2818   ** file does not exist, delay opening it.  This prevents empty database
2819   ** files from being created if a user mistypes the database name argument
2820   ** to the sqlite command-line tool.
2821   */
2822   if( access(data.zDbFilename, 0)==0 ){
2823     open_db(&data);
2824   }
2825 
2826   /* Process the initialization file if there is one.  If no -init option
2827   ** is given on the command line, look for a file named ~/.sqliterc and
2828   ** try to process it.
2829   */
2830   rc = process_sqliterc(&data,zInitFile);
2831   if( rc>0 ){
2832     return rc;
2833   }
2834 
2835   /* Make a second pass through the command-line argument and set
2836   ** options.  This second pass is delayed until after the initialization
2837   ** file is processed so that the command-line arguments will override
2838   ** settings in the initialization file.
2839   */
2840   for(i=1; i<argc && argv[i][0]=='-'; i++){
2841     char *z = argv[i];
2842     if( z[1]=='-' ){ z++; }
2843     if( strcmp(z,"-init")==0 ){
2844       i++;
2845     }else if( strcmp(z,"-html")==0 ){
2846       data.mode = MODE_Html;
2847     }else if( strcmp(z,"-list")==0 ){
2848       data.mode = MODE_List;
2849     }else if( strcmp(z,"-line")==0 ){
2850       data.mode = MODE_Line;
2851     }else if( strcmp(z,"-column")==0 ){
2852       data.mode = MODE_Column;
2853     }else if( strcmp(z,"-csv")==0 ){
2854       data.mode = MODE_Csv;
2855       memcpy(data.separator,",",2);
2856     }else if( strcmp(z,"-separator")==0 ){
2857       i++;
2858       if(i>=argc){
2859         fprintf(stderr,"%s: Error: missing argument for option: %s\n", Argv0, z);
2860         fprintf(stderr,"Use -help for a list of options.\n");
2861         return 1;
2862       }
2863       sqlite3_snprintf(sizeof(data.separator), data.separator,
2864                        "%.*s",(int)sizeof(data.separator)-1,argv[i]);
2865     }else if( strcmp(z,"-nullvalue")==0 ){
2866       i++;
2867       if(i>=argc){
2868         fprintf(stderr,"%s: Error: missing argument for option: %s\n", Argv0, z);
2869         fprintf(stderr,"Use -help for a list of options.\n");
2870         return 1;
2871       }
2872       sqlite3_snprintf(sizeof(data.nullvalue), data.nullvalue,
2873                        "%.*s",(int)sizeof(data.nullvalue)-1,argv[i]);
2874     }else if( strcmp(z,"-header")==0 ){
2875       data.showHeader = 1;
2876     }else if( strcmp(z,"-noheader")==0 ){
2877       data.showHeader = 0;
2878     }else if( strcmp(z,"-echo")==0 ){
2879       data.echoOn = 1;
2880     }else if( strcmp(z,"-stats")==0 ){
2881       data.statsOn = 1;
2882     }else if( strcmp(z,"-bail")==0 ){
2883       bail_on_error = 1;
2884     }else if( strcmp(z,"-version")==0 ){
2885       printf("%s\n", sqlite3_libversion());
2886       return 0;
2887     }else if( strcmp(z,"-interactive")==0 ){
2888       stdin_is_interactive = 1;
2889     }else if( strcmp(z,"-batch")==0 ){
2890       stdin_is_interactive = 0;
2891     }else if( strcmp(z,"-heap")==0 ){
2892       i++;
2893     }else if( strcmp(z,"-vfs")==0 ){
2894       i++;
2895     }else if( strcmp(z,"-vfstrace")==0 ){
2896       i++;
2897     }else if( strcmp(z,"-help")==0 || strcmp(z, "--help")==0 ){
2898       usage(1);
2899     }else{
2900       fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
2901       fprintf(stderr,"Use -help for a list of options.\n");
2902       return 1;
2903     }
2904   }
2905 
2906   if( zFirstCmd ){
2907     /* Run just the command that follows the database name
2908     */
2909     if( zFirstCmd[0]=='.' ){
2910       rc = do_meta_command(zFirstCmd, &data);
2911     }else{
2912       open_db(&data);
2913       rc = shell_exec(data.db, zFirstCmd, shell_callback, &data, &zErrMsg);
2914       if( zErrMsg!=0 ){
2915         fprintf(stderr,"Error: %s\n", zErrMsg);
2916         return rc!=0 ? rc : 1;
2917       }else if( rc!=0 ){
2918         fprintf(stderr,"Error: unable to process SQL \"%s\"\n", zFirstCmd);
2919         return rc;
2920       }
2921     }
2922   }else{
2923     /* Run commands received from standard input
2924     */
2925     if( stdin_is_interactive ){
2926       char *zHome;
2927       char *zHistory = 0;
2928       int nHistory;
2929       extern char *db_full_version(int *, int *, int *, int *, int *);
2930       printf(
2931         "%s\n"
2932         "Enter \".help\" for instructions\n"
2933         "Enter SQL statements terminated with a \";\"\n",
2934         db_full_version(NULL, NULL, NULL, NULL, NULL)
2935       );
2936       zHome = find_home_dir();
2937       if( zHome ){
2938         nHistory = strlen30(zHome) + 20;
2939         if( (zHistory = malloc(nHistory))!=0 ){
2940           sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
2941         }
2942       }
2943 #if defined(HAVE_READLINE) && HAVE_READLINE==1
2944       if( zHistory ) read_history(zHistory);
2945 #endif
2946       rc = process_input(&data, 0);
2947       if( zHistory ){
2948         stifle_history(100);
2949         write_history(zHistory);
2950         free(zHistory);
2951       }
2952       free(zHome);
2953     }else{
2954       rc = process_input(&data, stdin);
2955     }
2956   }
2957   set_table_name(&data, 0);
2958   if( data.db ){
2959     sqlite3_close(data.db);
2960   }
2961   return rc;
2962 }
2963