blob: 444be5d5bd252531757b0674e84fdd91b3370b03 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** This file contains code to implement the "sqlite" command line
13** utility for accessing SQLite databases.
14**
danielk19773f41e972004-06-08 00:39:01 +000015** $Id: shell.c,v 1.104 2004/06/08 00:39:01 danielk1977 Exp $
drh75897232000-05-29 14:26:00 +000016*/
17#include <stdlib.h>
18#include <string.h>
19#include <stdio.h>
danielk19772a02e332004-06-05 08:04:36 +000020#include <assert.h>
drh1d482dd2004-05-31 18:23:07 +000021#include "sqlite3.h"
drh75897232000-05-29 14:26:00 +000022#include <ctype.h>
persicom7e2dfdd2002-04-18 02:46:52 +000023
drh820f3812003-01-08 13:02:52 +000024#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
drh4c504392000-10-16 22:06:40 +000025# include <signal.h>
drhdd45df82002-04-18 12:39:03 +000026# include <pwd.h>
27# include <unistd.h>
28# include <sys/types.h>
drh4c504392000-10-16 22:06:40 +000029#endif
drh75897232000-05-29 14:26:00 +000030
drh820f3812003-01-08 13:02:52 +000031#ifdef __MACOS__
32# include <console.h>
33# include <signal.h>
34# include <unistd.h>
35# include <extras.h>
36# include <Files.h>
37# include <Folders.h>
38#endif
39
drh16e59552000-07-31 11:57:37 +000040#if defined(HAVE_READLINE) && HAVE_READLINE==1
drh8e7e7a22000-05-30 18:45:23 +000041# include <readline/readline.h>
42# include <readline/history.h>
43#else
drh9347b202003-07-18 01:30:59 +000044# define readline(p) local_getline(p,stdin)
persicom1d0b8722002-04-18 02:53:04 +000045# define add_history(X)
drh67505e72002-04-19 12:34:06 +000046# define read_history(X)
47# define write_history(X)
48# define stifle_history(X)
drh75897232000-05-29 14:26:00 +000049#endif
50
drh4328c8b2003-04-26 02:50:11 +000051/* Make sure isatty() has a prototype.
52*/
53extern int isatty();
54
drh75897232000-05-29 14:26:00 +000055/*
drh4c504392000-10-16 22:06:40 +000056** The following is the open SQLite database. We make a pointer
57** to this database a static variable so that it can be accessed
58** by the SIGINT handler to interrupt database processing.
59*/
60static sqlite *db = 0;
61
62/*
drh67505e72002-04-19 12:34:06 +000063** True if an interrupt (Control-C) has been received.
64*/
65static int seenInterrupt = 0;
66
67/*
persicom7e2dfdd2002-04-18 02:46:52 +000068** This is the name of our program. It is set in main(), used
69** in a number of other places, mostly for error messages.
70*/
71static char *Argv0;
72
73/*
74** Prompt strings. Initialized in main. Settable with
75** .prompt main continue
76*/
77static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
78static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
79
drh44c2eb12003-04-30 11:38:26 +000080
persicom7e2dfdd2002-04-18 02:46:52 +000081/*
drh83965662003-04-17 02:54:13 +000082** Determines if a string is a number of not.
83*/
danielk19778a6b5412004-05-24 07:04:25 +000084extern int sqlite3IsNumber(const char*, int*, unsigned char);
drh83965662003-04-17 02:54:13 +000085
86/*
drh8e7e7a22000-05-30 18:45:23 +000087** This routine reads a line of text from standard input, stores
88** the text in memory obtained from malloc() and returns a pointer
89** to the text. NULL is returned at end of file, or if malloc()
90** fails.
91**
92** The interface is like "readline" but no command-line editing
93** is done.
94*/
drh9347b202003-07-18 01:30:59 +000095static char *local_getline(char *zPrompt, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +000096 char *zLine;
97 int nLine;
drh8e7e7a22000-05-30 18:45:23 +000098 int n;
99 int eol;
100
101 if( zPrompt && *zPrompt ){
102 printf("%s",zPrompt);
103 fflush(stdout);
104 }
105 nLine = 100;
106 zLine = malloc( nLine );
107 if( zLine==0 ) return 0;
108 n = 0;
109 eol = 0;
110 while( !eol ){
111 if( n+100>nLine ){
112 nLine = nLine*2 + 100;
113 zLine = realloc(zLine, nLine);
114 if( zLine==0 ) return 0;
115 }
drhdaffd0e2001-04-11 14:28:42 +0000116 if( fgets(&zLine[n], nLine - n, in)==0 ){
drh8e7e7a22000-05-30 18:45:23 +0000117 if( n==0 ){
118 free(zLine);
119 return 0;
120 }
121 zLine[n] = 0;
122 eol = 1;
123 break;
124 }
125 while( zLine[n] ){ n++; }
126 if( n>0 && zLine[n-1]=='\n' ){
127 n--;
128 zLine[n] = 0;
129 eol = 1;
130 }
131 }
132 zLine = realloc( zLine, n+1 );
133 return zLine;
134}
135
136/*
137** Retrieve a single line of input text. "isatty" is true if text
138** is coming from a terminal. In that case, we issue a prompt and
139** attempt to use "readline" for command-line editing. If "isatty"
drh9347b202003-07-18 01:30:59 +0000140** is false, use "local_getline" instead of "readline" and issue no prompt.
drh8e7e7a22000-05-30 18:45:23 +0000141**
142** zPrior is a string of prior text retrieved. If not the empty
143** string, then issue a continuation prompt.
144*/
drhdaffd0e2001-04-11 14:28:42 +0000145static char *one_input_line(const char *zPrior, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +0000146 char *zPrompt;
147 char *zResult;
drhdaffd0e2001-04-11 14:28:42 +0000148 if( in!=0 ){
drh9347b202003-07-18 01:30:59 +0000149 return local_getline(0, in);
drh8e7e7a22000-05-30 18:45:23 +0000150 }
151 if( zPrior && zPrior[0] ){
persicom7e2dfdd2002-04-18 02:46:52 +0000152 zPrompt = continuePrompt;
drh8e7e7a22000-05-30 18:45:23 +0000153 }else{
persicom7e2dfdd2002-04-18 02:46:52 +0000154 zPrompt = mainPrompt;
drh8e7e7a22000-05-30 18:45:23 +0000155 }
156 zResult = readline(zPrompt);
drh2dfbbca2000-07-28 14:32:48 +0000157 if( zResult ) add_history(zResult);
drh8e7e7a22000-05-30 18:45:23 +0000158 return zResult;
159}
160
persicom7e2dfdd2002-04-18 02:46:52 +0000161struct previous_mode_data {
162 int valid; /* Is there legit data in here? */
163 int mode;
164 int showHeader;
165 int colWidth[100];
166};
drh8e7e7a22000-05-30 18:45:23 +0000167/*
drh75897232000-05-29 14:26:00 +0000168** An pointer to an instance of this structure is passed from
169** the main program to the callback. This is used to communicate
170** state and mode information.
171*/
172struct callback_data {
drh28bd4bc2000-06-15 15:57:22 +0000173 sqlite *db; /* The database */
drhdaffd0e2001-04-11 14:28:42 +0000174 int echoOn; /* True to echo input commands */
drh28bd4bc2000-06-15 15:57:22 +0000175 int cnt; /* Number of records displayed so far */
176 FILE *out; /* Write results here */
177 int mode; /* An output mode setting */
178 int showHeader; /* True to show column names in List or Column mode */
drh33048c02001-10-01 14:29:22 +0000179 char *zDestTable; /* Name of destination table when MODE_Insert */
drh28bd4bc2000-06-15 15:57:22 +0000180 char separator[20]; /* Separator character for MODE_List */
drha0c66f52000-07-29 13:20:21 +0000181 int colWidth[100]; /* Requested width of each column when in column mode*/
182 int actualWidth[100]; /* Actual width of each column */
drh83965662003-04-17 02:54:13 +0000183 char nullvalue[20]; /* The text to print when a NULL comes back from
184 ** the database */
persicom7e2dfdd2002-04-18 02:46:52 +0000185 struct previous_mode_data explainPrev;
drh83965662003-04-17 02:54:13 +0000186 /* Holds the mode information just before
187 ** .explain ON */
drh44c2eb12003-04-30 11:38:26 +0000188 char outfile[FILENAME_MAX]; /* Filename for *out */
189 const char *zDbFilename; /* name of the database file */
drh22fbcb82004-02-01 01:22:50 +0000190 char *zKey; /* Encryption key */
drh75897232000-05-29 14:26:00 +0000191};
192
193/*
194** These are the allowed modes.
195*/
drh967e8b72000-06-21 13:59:10 +0000196#define MODE_Line 0 /* One column per line. Blank line between records */
drh75897232000-05-29 14:26:00 +0000197#define MODE_Column 1 /* One record per line in neat columns */
198#define MODE_List 2 /* One record per line with a separator */
drhe3710332000-09-29 13:30:53 +0000199#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
200#define MODE_Html 4 /* Generate an XHTML table */
201#define MODE_Insert 5 /* Generate SQL "insert" statements */
jplyon672a1ed2003-05-11 20:07:05 +0000202#define MODE_NUM_OF 6 /* The number of modes (not a mode itself) */
persicom7e2dfdd2002-04-18 02:46:52 +0000203
204char *modeDescr[MODE_NUM_OF] = {
205 "line",
206 "column",
207 "list",
208 "semi",
209 "html",
210 "insert"
211};
drh75897232000-05-29 14:26:00 +0000212
213/*
214** Number of elements in an array
215*/
216#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
217
218/*
drh28bd4bc2000-06-15 15:57:22 +0000219** Output the given string as a quoted string using SQL quoting conventions.
220*/
221static void output_quoted_string(FILE *out, const char *z){
222 int i;
223 int nSingle = 0;
drh28bd4bc2000-06-15 15:57:22 +0000224 for(i=0; z[i]; i++){
225 if( z[i]=='\'' ) nSingle++;
drh28bd4bc2000-06-15 15:57:22 +0000226 }
227 if( nSingle==0 ){
228 fprintf(out,"'%s'",z);
drh28bd4bc2000-06-15 15:57:22 +0000229 }else{
230 fprintf(out,"'");
231 while( *z ){
232 for(i=0; z[i] && z[i]!='\''; i++){}
233 if( i==0 ){
234 fprintf(out,"''");
235 z++;
236 }else if( z[i]=='\'' ){
237 fprintf(out,"%.*s''",i,z);
238 z += i+1;
239 }else{
drhcd7d2732002-02-26 23:24:26 +0000240 fprintf(out,"%s",z);
drh28bd4bc2000-06-15 15:57:22 +0000241 break;
242 }
243 }
drhcd7d2732002-02-26 23:24:26 +0000244 fprintf(out,"'");
drh28bd4bc2000-06-15 15:57:22 +0000245 }
246}
247
248/*
drhc08a4f12000-06-15 16:49:48 +0000249** Output the given string with characters that are special to
250** HTML escaped.
251*/
252static void output_html_string(FILE *out, const char *z){
253 int i;
254 while( *z ){
255 for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){}
256 if( i>0 ){
257 fprintf(out,"%.*s",i,z);
258 }
259 if( z[i]=='<' ){
260 fprintf(out,"&lt;");
261 }else if( z[i]=='&' ){
262 fprintf(out,"&amp;");
263 }else{
264 break;
265 }
266 z += i + 1;
267 }
268}
269
270/*
drh4c504392000-10-16 22:06:40 +0000271** This routine runs when the user presses Ctrl-C
272*/
273static void interrupt_handler(int NotUsed){
drh67505e72002-04-19 12:34:06 +0000274 seenInterrupt = 1;
danielk19776f8a5032004-05-10 10:34:51 +0000275 if( db ) sqlite3_interrupt(db);
drh4c504392000-10-16 22:06:40 +0000276}
277
278/*
drh75897232000-05-29 14:26:00 +0000279** This is the callback routine that the SQLite library
280** invokes for each row of a query result.
281*/
282static int callback(void *pArg, int nArg, char **azArg, char **azCol){
283 int i;
284 struct callback_data *p = (struct callback_data*)pArg;
285 switch( p->mode ){
286 case MODE_Line: {
drhe3710332000-09-29 13:30:53 +0000287 int w = 5;
drh6a535342001-10-19 16:44:56 +0000288 if( azArg==0 ) break;
drhe3710332000-09-29 13:30:53 +0000289 for(i=0; i<nArg; i++){
290 int len = strlen(azCol[i]);
291 if( len>w ) w = len;
292 }
drh75897232000-05-29 14:26:00 +0000293 if( p->cnt++>0 ) fprintf(p->out,"\n");
294 for(i=0; i<nArg; i++){
drha69d9162003-04-17 22:57:53 +0000295 fprintf(p->out,"%*s = %s\n", w, azCol[i],
296 azArg[i] ? azArg[i] : p->nullvalue);
drh75897232000-05-29 14:26:00 +0000297 }
298 break;
299 }
300 case MODE_Column: {
drha0c66f52000-07-29 13:20:21 +0000301 if( p->cnt++==0 ){
drh75897232000-05-29 14:26:00 +0000302 for(i=0; i<nArg; i++){
drha0c66f52000-07-29 13:20:21 +0000303 int w, n;
304 if( i<ArraySize(p->colWidth) ){
drh75897232000-05-29 14:26:00 +0000305 w = p->colWidth[i];
306 }else{
drha0c66f52000-07-29 13:20:21 +0000307 w = 0;
drh75897232000-05-29 14:26:00 +0000308 }
drha0c66f52000-07-29 13:20:21 +0000309 if( w<=0 ){
drhff6e9112000-08-28 16:21:58 +0000310 w = strlen(azCol[i] ? azCol[i] : "");
drha0c66f52000-07-29 13:20:21 +0000311 if( w<10 ) w = 10;
persicom7e2dfdd2002-04-18 02:46:52 +0000312 n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue);
drha0c66f52000-07-29 13:20:21 +0000313 if( w<n ) w = n;
314 }
315 if( i<ArraySize(p->actualWidth) ){
persicom1d0b8722002-04-18 02:53:04 +0000316 p->actualWidth[i] = w;
drha0c66f52000-07-29 13:20:21 +0000317 }
318 if( p->showHeader ){
319 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
320 }
321 }
322 if( p->showHeader ){
323 for(i=0; i<nArg; i++){
324 int w;
325 if( i<ArraySize(p->actualWidth) ){
326 w = p->actualWidth[i];
327 }else{
328 w = 10;
329 }
330 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
331 "----------------------------------------------------------",
332 i==nArg-1 ? "\n": " ");
333 }
drh75897232000-05-29 14:26:00 +0000334 }
335 }
drh6a535342001-10-19 16:44:56 +0000336 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000337 for(i=0; i<nArg; i++){
338 int w;
drha0c66f52000-07-29 13:20:21 +0000339 if( i<ArraySize(p->actualWidth) ){
340 w = p->actualWidth[i];
drh75897232000-05-29 14:26:00 +0000341 }else{
342 w = 10;
343 }
drhc61053b2000-06-04 12:58:36 +0000344 fprintf(p->out,"%-*.*s%s",w,w,
persicom7e2dfdd2002-04-18 02:46:52 +0000345 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
drh75897232000-05-29 14:26:00 +0000346 }
347 break;
348 }
drhe3710332000-09-29 13:30:53 +0000349 case MODE_Semi:
drh75897232000-05-29 14:26:00 +0000350 case MODE_List: {
351 if( p->cnt++==0 && p->showHeader ){
352 for(i=0; i<nArg; i++){
353 fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
354 }
355 }
drh6a535342001-10-19 16:44:56 +0000356 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000357 for(i=0; i<nArg; i++){
drh4c653a02000-06-07 01:27:47 +0000358 char *z = azArg[i];
persicom7e2dfdd2002-04-18 02:46:52 +0000359 if( z==0 ) z = p->nullvalue;
drh71172c52002-01-24 00:00:21 +0000360 fprintf(p->out, "%s", z);
drhe3710332000-09-29 13:30:53 +0000361 if( i<nArg-1 ){
362 fprintf(p->out, "%s", p->separator);
363 }else if( p->mode==MODE_Semi ){
364 fprintf(p->out, ";\n");
365 }else{
366 fprintf(p->out, "\n");
367 }
drh75897232000-05-29 14:26:00 +0000368 }
369 break;
370 }
drh1e5d0e92000-05-31 23:33:17 +0000371 case MODE_Html: {
372 if( p->cnt++==0 && p->showHeader ){
373 fprintf(p->out,"<TR>");
374 for(i=0; i<nArg; i++){
375 fprintf(p->out,"<TH>%s</TH>",azCol[i]);
376 }
377 fprintf(p->out,"</TR>\n");
378 }
drh6a535342001-10-19 16:44:56 +0000379 if( azArg==0 ) break;
drh28bd4bc2000-06-15 15:57:22 +0000380 fprintf(p->out,"<TR>");
drh1e5d0e92000-05-31 23:33:17 +0000381 for(i=0; i<nArg; i++){
drhc08a4f12000-06-15 16:49:48 +0000382 fprintf(p->out,"<TD>");
persicom7e2dfdd2002-04-18 02:46:52 +0000383 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
drhc08a4f12000-06-15 16:49:48 +0000384 fprintf(p->out,"</TD>\n");
drh1e5d0e92000-05-31 23:33:17 +0000385 }
drh7d686b22002-11-11 13:56:47 +0000386 fprintf(p->out,"</TR>\n");
drh1e5d0e92000-05-31 23:33:17 +0000387 break;
388 }
drh28bd4bc2000-06-15 15:57:22 +0000389 case MODE_Insert: {
drh6a535342001-10-19 16:44:56 +0000390 if( azArg==0 ) break;
drh33048c02001-10-01 14:29:22 +0000391 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
drh28bd4bc2000-06-15 15:57:22 +0000392 for(i=0; i<nArg; i++){
393 char *zSep = i>0 ? ",": "";
394 if( azArg[i]==0 ){
395 fprintf(p->out,"%sNULL",zSep);
danielk19778a6b5412004-05-24 07:04:25 +0000396 }else if( sqlite3IsNumber(azArg[i], 0, 1) ){
drh28bd4bc2000-06-15 15:57:22 +0000397 fprintf(p->out,"%s%s",zSep, azArg[i]);
398 }else{
399 if( zSep[0] ) fprintf(p->out,"%s",zSep);
400 output_quoted_string(p->out, azArg[i]);
401 }
402 }
403 fprintf(p->out,");\n");
drh6a535342001-10-19 16:44:56 +0000404 break;
drh28bd4bc2000-06-15 15:57:22 +0000405 }
persicom1d0b8722002-04-18 02:53:04 +0000406 }
drh75897232000-05-29 14:26:00 +0000407 return 0;
408}
409
410/*
drh33048c02001-10-01 14:29:22 +0000411** Set the destination table field of the callback_data structure to
412** the name of the table given. Escape any quote characters in the
413** table name.
414*/
415static void set_table_name(struct callback_data *p, const char *zName){
416 int i, n;
417 int needQuote;
418 char *z;
419
420 if( p->zDestTable ){
421 free(p->zDestTable);
422 p->zDestTable = 0;
423 }
424 if( zName==0 ) return;
425 needQuote = !isalpha(*zName) && *zName!='_';
426 for(i=n=0; zName[i]; i++, n++){
427 if( !isalnum(zName[i]) && zName[i]!='_' ){
428 needQuote = 1;
429 if( zName[i]=='\'' ) n++;
430 }
431 }
432 if( needQuote ) n += 2;
433 z = p->zDestTable = malloc( n+1 );
434 if( z==0 ){
435 fprintf(stderr,"Out of memory!\n");
436 exit(1);
437 }
438 n = 0;
439 if( needQuote ) z[n++] = '\'';
440 for(i=0; zName[i]; i++){
441 z[n++] = zName[i];
442 if( zName[i]=='\'' ) z[n++] = '\'';
443 }
444 if( needQuote ) z[n++] = '\'';
445 z[n] = 0;
446}
447
danielk19772a02e332004-06-05 08:04:36 +0000448/* zIn is either a pointer to a NULL-terminated string in memory obtained
449** from malloc(), or a NULL pointer. The string pointed to by zAppend is
450** added to zIn, and the result returned in memory obtained from malloc().
451** zIn, if it was not NULL, is freed.
452**
453** If the third argument, quote, is not '\0', then it is used as a
454** quote character for zAppend.
455*/
456static char * appendText(char *zIn, char const *zAppend, char quote){
457 int len;
458 int i;
459 int nAppend = strlen(zAppend);
460 int nIn = (zIn?strlen(zIn):0);
461
462 len = nAppend+nIn+1;
463 if( quote ){
464 len += 2;
465 for(i=0; i<nAppend; i++){
466 if( zAppend[i]==quote ) len++;
467 }
468 }
469
470 zIn = (char *)realloc(zIn, len);
471 if( !zIn ){
472 return 0;
473 }
474
475 if( quote ){
476 char *zCsr = &zIn[nIn];
477 *zCsr++ = quote;
478 for(i=0; i<nAppend; i++){
479 *zCsr++ = zAppend[i];
480 if( zAppend[i]==quote ) *zCsr++ = quote;
481 }
482 *zCsr++ = quote;
483 *zCsr++ = '\0';
484 assert( (zCsr-zIn)==len );
485 }else{
486 memcpy(&zIn[nIn], zAppend, nAppend);
487 zIn[len-1] = '\0';
488 }
489
490 return zIn;
491}
492
drh33048c02001-10-01 14:29:22 +0000493/*
drh4c653a02000-06-07 01:27:47 +0000494** This is a different callback routine used for dumping the database.
495** Each row received by this callback consists of a table name,
496** the table type ("index" or "table") and SQL to create the table.
497** This routine should print text sufficient to recreate the table.
498*/
499static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
danielk19772a02e332004-06-05 08:04:36 +0000500 int rc;
501 const char *zTable;
502 const char *zType;
503 const char *zSql;
drhdaffd0e2001-04-11 14:28:42 +0000504 struct callback_data *p = (struct callback_data *)pArg;
danielk19772a02e332004-06-05 08:04:36 +0000505
drh4c653a02000-06-07 01:27:47 +0000506 if( nArg!=3 ) return 1;
danielk19772a02e332004-06-05 08:04:36 +0000507 zTable = azArg[0];
508 zType = azArg[1];
509 zSql = azArg[2];
510
511 fprintf(p->out, "%s;\n", zSql);
512
513 if( strcmp(zType, "table")==0 ){
514 sqlite3_stmt *pTableInfo = 0;
515 sqlite3_stmt *pSelect = 0;
516 char *zSelect = 0;
517 char *zTableInfo = 0;
518 char *zTmp = 0;
519
520 zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
521 zTableInfo = appendText(zTableInfo, zTable, '"');
522 zTableInfo = appendText(zTableInfo, ");", 0);
523
524 rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0);
525 if( zTableInfo ) free(zTableInfo);
526 if( rc!=SQLITE_OK || !pTableInfo ){
527 return 1;
528 }
529
530 zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
531 zTmp = appendText(zTmp, zTable, '"');
532 if( zTmp ){
533 zSelect = appendText(zSelect, zTmp, '\'');
534 }
535 zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
536 rc = sqlite3_step(pTableInfo);
537 while( rc==SQLITE_ROW ){
danielk19773f41e972004-06-08 00:39:01 +0000538 zSelect = appendText(zSelect, "quote(", 0);
danielk19772a02e332004-06-05 08:04:36 +0000539 zSelect = appendText(zSelect, sqlite3_column_text(pTableInfo, 1), '"');
540 rc = sqlite3_step(pTableInfo);
541 if( rc==SQLITE_ROW ){
542 zSelect = appendText(zSelect, ") || ', ' || ", 0);
543 }else{
544 zSelect = appendText(zSelect, ") ", 0);
545 }
546 }
547 rc = sqlite3_finalize(pTableInfo);
548 if( rc!=SQLITE_OK ){
549 if( zSelect ) free(zSelect);
550 return 1;
551 }
552 zSelect = appendText(zSelect, "|| ')' FROM ", 0);
553 zSelect = appendText(zSelect, zTable, '"');
554
555 rc = sqlite3_prepare(p->db, zSelect, -1, &pSelect, 0);
556 if( zSelect ) free(zSelect);
557 if( rc!=SQLITE_OK || !pSelect ){
558 return 1;
559 }
560
561 rc = sqlite3_step(pSelect);
562 while( rc==SQLITE_ROW ){
563 fprintf(p->out, "%s;\n", sqlite3_column_text(pSelect, 0));
564 rc = sqlite3_step(pSelect);
565 }
566 rc = sqlite3_finalize(pSelect);
567 if( rc!=SQLITE_OK ){
568 return 1;
569 }
drh4c653a02000-06-07 01:27:47 +0000570 }
danielk19772a02e332004-06-05 08:04:36 +0000571
drh4c653a02000-06-07 01:27:47 +0000572 return 0;
573}
574
575/*
drh75897232000-05-29 14:26:00 +0000576** Text of a help message
577*/
persicom1d0b8722002-04-18 02:53:04 +0000578static char zHelp[] =
jplyon6a65bb32003-05-04 07:25:57 +0000579 ".databases List names and files of attached databases\n"
580 ".dump ?TABLE? ... Dump the database in a text format\n"
drhdaffd0e2001-04-11 14:28:42 +0000581 ".echo ON|OFF Turn command echo on or off\n"
drh75897232000-05-29 14:26:00 +0000582 ".exit Exit this program\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000583 ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000584 ".header(s) ON|OFF Turn display of headers on or off\n"
drh75897232000-05-29 14:26:00 +0000585 ".help Show this message\n"
586 ".indices TABLE Show names of all indices on TABLE\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000587 ".mode MODE Set mode to one of \"line(s)\", \"column(s)\", \n"
drhe3710332000-09-29 13:30:53 +0000588 " \"insert\", \"list\", or \"html\"\n"
drhc08a4f12000-06-15 16:49:48 +0000589 ".mode insert TABLE Generate SQL insert statements for TABLE\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000590 ".nullvalue STRING Print STRING instead of nothing for NULL data\n"
drh75897232000-05-29 14:26:00 +0000591 ".output FILENAME Send output to FILENAME\n"
592 ".output stdout Send output to the screen\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000593 ".prompt MAIN CONTINUE Replace the standard prompts\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000594 ".quit Exit this program\n"
drhdaffd0e2001-04-11 14:28:42 +0000595 ".read FILENAME Execute SQL in FILENAME\n"
drh9eb9e262004-02-11 02:18:05 +0000596#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000597 ".rekey OLD NEW NEW Change the encryption key\n"
598#endif
drh75897232000-05-29 14:26:00 +0000599 ".schema ?TABLE? Show the CREATE statements\n"
600 ".separator STRING Change separator string for \"list\" mode\n"
drhdd45df82002-04-18 12:39:03 +0000601 ".show Show the current values for various settings\n"
drha50da102000-08-08 20:19:09 +0000602 ".tables ?PATTERN? List names of tables matching a pattern\n"
drh2dfbbca2000-07-28 14:32:48 +0000603 ".timeout MS Try opening locked tables for MS milliseconds\n"
drh75897232000-05-29 14:26:00 +0000604 ".width NUM NUM ... Set column widths for \"column\" mode\n"
605;
606
drhdaffd0e2001-04-11 14:28:42 +0000607/* Forward reference */
608static void process_input(struct callback_data *p, FILE *in);
609
drh75897232000-05-29 14:26:00 +0000610/*
drh44c2eb12003-04-30 11:38:26 +0000611** Make sure the database is open. If it is not, then open it. If
612** the database fails to open, print an error message and exit.
613*/
614static void open_db(struct callback_data *p){
615 if( p->db==0 ){
drh9eb9e262004-02-11 02:18:05 +0000616#ifdef SQLITE_HAS_CODEC
drheb8ed702004-02-11 10:37:23 +0000617 int n = p->zKey ? strlen(p->zKey) : 0;
danielk19776f8a5032004-05-10 10:34:51 +0000618 db = p->db = sqlite3_open_encrypted(p->zDbFilename, p->zKey, n, 0, &zErrMsg);
danielk197780290862004-05-22 09:21:21 +0000619 assert(0); /* Encrypted databases are broken in SQLite 3 */
drheb8ed702004-02-11 10:37:23 +0000620#else
danielk19774f057f92004-06-08 00:02:33 +0000621 sqlite3_open(p->zDbFilename, &p->db);
danielk197780290862004-05-22 09:21:21 +0000622 db = p->db;
drheb8ed702004-02-11 10:37:23 +0000623#endif
danielk197780290862004-05-22 09:21:21 +0000624 if( SQLITE_OK!=sqlite3_errcode(db) ){
625 fprintf(stderr,"Unable to open database \"%s\": %s\n",
626 p->zDbFilename, sqlite3_errmsg(db));
drh22fbcb82004-02-01 01:22:50 +0000627 exit(1);
drh44c2eb12003-04-30 11:38:26 +0000628 }
629 }
630}
631
632/*
drh75897232000-05-29 14:26:00 +0000633** If an input line begins with "." then invoke this routine to
634** process that line.
drh67505e72002-04-19 12:34:06 +0000635**
636** Return 1 to exit and 0 to continue.
drh75897232000-05-29 14:26:00 +0000637*/
drh44c2eb12003-04-30 11:38:26 +0000638static int do_meta_command(char *zLine, struct callback_data *p){
drh75897232000-05-29 14:26:00 +0000639 int i = 1;
640 int nArg = 0;
641 int n, c;
drh67505e72002-04-19 12:34:06 +0000642 int rc = 0;
drh75897232000-05-29 14:26:00 +0000643 char *azArg[50];
644
645 /* Parse the input line into tokens.
646 */
647 while( zLine[i] && nArg<ArraySize(azArg) ){
648 while( isspace(zLine[i]) ){ i++; }
drh06333682004-03-09 13:37:45 +0000649 if( zLine[i]==0 ) break;
drh75897232000-05-29 14:26:00 +0000650 if( zLine[i]=='\'' || zLine[i]=='"' ){
651 int delim = zLine[i++];
652 azArg[nArg++] = &zLine[i];
653 while( zLine[i] && zLine[i]!=delim ){ i++; }
654 if( zLine[i]==delim ){
655 zLine[i++] = 0;
656 }
657 }else{
658 azArg[nArg++] = &zLine[i];
659 while( zLine[i] && !isspace(zLine[i]) ){ i++; }
660 if( zLine[i] ) zLine[i++] = 0;
661 }
662 }
663
664 /* Process the input line.
665 */
drh67505e72002-04-19 12:34:06 +0000666 if( nArg==0 ) return rc;
drh75897232000-05-29 14:26:00 +0000667 n = strlen(azArg[0]);
668 c = azArg[0][0];
jplyon6a65bb32003-05-04 07:25:57 +0000669 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
jplyon672a1ed2003-05-11 20:07:05 +0000670 struct callback_data data;
671 char *zErrMsg = 0;
jplyon6a65bb32003-05-04 07:25:57 +0000672 open_db(p);
jplyon672a1ed2003-05-11 20:07:05 +0000673 memcpy(&data, p, sizeof(data));
drhd8885442004-03-17 23:42:12 +0000674 data.showHeader = 1;
jplyon672a1ed2003-05-11 20:07:05 +0000675 data.mode = MODE_Column;
drhd8885442004-03-17 23:42:12 +0000676 data.colWidth[0] = 3;
677 data.colWidth[1] = 15;
678 data.colWidth[2] = 58;
danielk19776f8a5032004-05-10 10:34:51 +0000679 sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
jplyon672a1ed2003-05-11 20:07:05 +0000680 if( zErrMsg ){
681 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000682 sqlite3_free(zErrMsg);
jplyon6a65bb32003-05-04 07:25:57 +0000683 }
684 }else
685
drh4c653a02000-06-07 01:27:47 +0000686 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
687 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000688 open_db(p);
drh33048c02001-10-01 14:29:22 +0000689 fprintf(p->out, "BEGIN TRANSACTION;\n");
drh4c653a02000-06-07 01:27:47 +0000690 if( nArg==1 ){
danielk19776f8a5032004-05-10 10:34:51 +0000691 sqlite3_exec(p->db,
drha18c5682000-10-08 22:20:57 +0000692 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000693 "WHERE type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000694 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000695 dump_callback, p, &zErrMsg
696 );
drh4c653a02000-06-07 01:27:47 +0000697 }else{
698 int i;
699 for(i=1; i<nArg && zErrMsg==0; i++){
danielk19776f8a5032004-05-10 10:34:51 +0000700 sqlite3_exec_printf(p->db,
drha18c5682000-10-08 22:20:57 +0000701 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000702 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000703 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000704 dump_callback, p, &zErrMsg, azArg[i]
705 );
drh4c653a02000-06-07 01:27:47 +0000706 }
707 }
708 if( zErrMsg ){
709 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000710 sqlite3_free(zErrMsg);
drh33048c02001-10-01 14:29:22 +0000711 }else{
712 fprintf(p->out, "COMMIT;\n");
drh4c653a02000-06-07 01:27:47 +0000713 }
714 }else
drh75897232000-05-29 14:26:00 +0000715
drhdaffd0e2001-04-11 14:28:42 +0000716 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
717 int j;
718 char *z = azArg[1];
719 int val = atoi(azArg[1]);
720 for(j=0; z[j]; j++){
721 if( isupper(z[j]) ) z[j] = tolower(z[j]);
722 }
723 if( strcmp(z,"on")==0 ){
724 val = 1;
725 }else if( strcmp(z,"yes")==0 ){
726 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000727 }
drhdaffd0e2001-04-11 14:28:42 +0000728 p->echoOn = val;
729 }else
730
drh75897232000-05-29 14:26:00 +0000731 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
drh67505e72002-04-19 12:34:06 +0000732 rc = 1;
drh75897232000-05-29 14:26:00 +0000733 }else
734
drhdd45df82002-04-18 12:39:03 +0000735 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +0000736 int j;
drhdd45df82002-04-18 12:39:03 +0000737 char *z = nArg>=2 ? azArg[1] : "1";
738 int val = atoi(z);
persicom7e2dfdd2002-04-18 02:46:52 +0000739 for(j=0; z[j]; j++){
740 if( isupper(z[j]) ) z[j] = tolower(z[j]);
741 }
742 if( strcmp(z,"on")==0 ){
743 val = 1;
744 }else if( strcmp(z,"yes")==0 ){
745 val = 1;
746 }
747 if(val == 1) {
748 if(!p->explainPrev.valid) {
749 p->explainPrev.valid = 1;
750 p->explainPrev.mode = p->mode;
751 p->explainPrev.showHeader = p->showHeader;
752 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
753 }
754 /* We could put this code under the !p->explainValid
755 ** condition so that it does not execute if we are already in
756 ** explain mode. However, always executing it allows us an easy
757 ** was to reset to explain mode in case the user previously
758 ** did an .explain followed by a .width, .mode or .header
759 ** command.
760 */
761 p->mode = MODE_Column;
762 p->showHeader = 1;
763 memset(p->colWidth,0,ArraySize(p->colWidth));
764 p->colWidth[0] = 4;
765 p->colWidth[1] = 12;
766 p->colWidth[2] = 10;
767 p->colWidth[3] = 10;
768 p->colWidth[4] = 35;
769 }else if (p->explainPrev.valid) {
770 p->explainPrev.valid = 0;
771 p->mode = p->explainPrev.mode;
772 p->showHeader = p->explainPrev.showHeader;
773 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
774 }
drh75897232000-05-29 14:26:00 +0000775 }else
776
persicom7e2dfdd2002-04-18 02:46:52 +0000777 if( c=='h' && (strncmp(azArg[0], "header", n)==0
778 ||
779 strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){
drh75897232000-05-29 14:26:00 +0000780 int j;
781 char *z = azArg[1];
782 int val = atoi(azArg[1]);
783 for(j=0; z[j]; j++){
784 if( isupper(z[j]) ) z[j] = tolower(z[j]);
785 }
786 if( strcmp(z,"on")==0 ){
787 val = 1;
788 }else if( strcmp(z,"yes")==0 ){
789 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000790 }
drh75897232000-05-29 14:26:00 +0000791 p->showHeader = val;
792 }else
793
794 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
795 fprintf(stderr,zHelp);
796 }else
797
798 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
799 struct callback_data data;
800 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000801 open_db(p);
drh75897232000-05-29 14:26:00 +0000802 memcpy(&data, p, sizeof(data));
803 data.showHeader = 0;
804 data.mode = MODE_List;
danielk19776f8a5032004-05-10 10:34:51 +0000805 sqlite3_exec_printf(p->db,
drha18c5682000-10-08 22:20:57 +0000806 "SELECT name FROM sqlite_master "
807 "WHERE type='index' AND tbl_name LIKE '%q' "
drhe0bc4042002-06-25 01:09:11 +0000808 "UNION ALL "
809 "SELECT name FROM sqlite_temp_master "
810 "WHERE type='index' AND tbl_name LIKE '%q' "
811 "ORDER BY 1",
812 callback, &data, &zErrMsg, azArg[1], azArg[1]
drha18c5682000-10-08 22:20:57 +0000813 );
drh75897232000-05-29 14:26:00 +0000814 if( zErrMsg ){
815 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000816 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +0000817 }
818 }else
819
drh28bd4bc2000-06-15 15:57:22 +0000820 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
drh75897232000-05-29 14:26:00 +0000821 int n2 = strlen(azArg[1]);
persicom7e2dfdd2002-04-18 02:46:52 +0000822 if( strncmp(azArg[1],"line",n2)==0
823 ||
824 strncmp(azArg[1],"lines",n2)==0 ){
drh75897232000-05-29 14:26:00 +0000825 p->mode = MODE_Line;
persicom7e2dfdd2002-04-18 02:46:52 +0000826 }else if( strncmp(azArg[1],"column",n2)==0
827 ||
828 strncmp(azArg[1],"columns",n2)==0 ){
drh75897232000-05-29 14:26:00 +0000829 p->mode = MODE_Column;
830 }else if( strncmp(azArg[1],"list",n2)==0 ){
831 p->mode = MODE_List;
drh1e5d0e92000-05-31 23:33:17 +0000832 }else if( strncmp(azArg[1],"html",n2)==0 ){
833 p->mode = MODE_Html;
drh28bd4bc2000-06-15 15:57:22 +0000834 }else if( strncmp(azArg[1],"insert",n2)==0 ){
835 p->mode = MODE_Insert;
836 if( nArg>=3 ){
drh33048c02001-10-01 14:29:22 +0000837 set_table_name(p, azArg[2]);
drh28bd4bc2000-06-15 15:57:22 +0000838 }else{
drh33048c02001-10-01 14:29:22 +0000839 set_table_name(p, "table");
drh28bd4bc2000-06-15 15:57:22 +0000840 }
drhdaffd0e2001-04-11 14:28:42 +0000841 }else {
842 fprintf(stderr,"mode should be on of: column html insert line list\n");
drh75897232000-05-29 14:26:00 +0000843 }
844 }else
845
persicom7e2dfdd2002-04-18 02:46:52 +0000846 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
847 sprintf(p->nullvalue, "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
848 }else
849
drh75897232000-05-29 14:26:00 +0000850 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
851 if( p->out!=stdout ){
852 fclose(p->out);
853 }
854 if( strcmp(azArg[1],"stdout")==0 ){
855 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +0000856 strcpy(p->outfile,"stdout");
drh75897232000-05-29 14:26:00 +0000857 }else{
drha1f9b5e2004-02-14 16:31:02 +0000858 p->out = fopen(azArg[1], "wb");
drh75897232000-05-29 14:26:00 +0000859 if( p->out==0 ){
860 fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
861 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +0000862 } else {
863 strcpy(p->outfile,azArg[1]);
drh75897232000-05-29 14:26:00 +0000864 }
865 }
866 }else
867
drhdd45df82002-04-18 12:39:03 +0000868 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
persicom7e2dfdd2002-04-18 02:46:52 +0000869 if( nArg >= 2) {
870 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
871 }
872 if( nArg >= 3) {
873 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
874 }
875 }else
876
877 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
drhf73287c2004-02-25 02:25:37 +0000878 rc = 1;
persicom7e2dfdd2002-04-18 02:46:52 +0000879 }else
880
drhdaffd0e2001-04-11 14:28:42 +0000881 if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
drha1f9b5e2004-02-14 16:31:02 +0000882 FILE *alt = fopen(azArg[1], "rb");
drhdaffd0e2001-04-11 14:28:42 +0000883 if( alt==0 ){
884 fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
885 }else{
886 process_input(p, alt);
887 fclose(alt);
888 }
889 }else
890
drh9eb9e262004-02-11 02:18:05 +0000891#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000892 if( c=='r' && strncmp(azArg[0],"rekey", n)==0 && nArg==4 ){
893 char *zOld = p->zKey;
894 if( zOld==0 ) zOld = "";
895 if( strcmp(azArg[1],zOld) ){
896 fprintf(stderr,"old key is incorrect\n");
897 }else if( strcmp(azArg[2], azArg[3]) ){
898 fprintf(stderr,"2nd copy of new key does not match the 1st\n");
899 }else{
drh3f4fedb2004-05-31 19:34:33 +0000900 sqlite3_free(p->zKey);
danielk19776f8a5032004-05-10 10:34:51 +0000901 p->zKey = sqlite3_mprintf("%s", azArg[2]);
drh22fbcb82004-02-01 01:22:50 +0000902 sqlite_rekey(p->db, p->zKey, strlen(p->zKey));
903 }
904 }else
905#endif
906
drh75897232000-05-29 14:26:00 +0000907 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
908 struct callback_data data;
909 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000910 open_db(p);
drh75897232000-05-29 14:26:00 +0000911 memcpy(&data, p, sizeof(data));
912 data.showHeader = 0;
drhe3710332000-09-29 13:30:53 +0000913 data.mode = MODE_Semi;
drh75897232000-05-29 14:26:00 +0000914 if( nArg>1 ){
danielk19774adee202004-05-08 08:23:19 +0000915 extern int sqlite3StrICmp(const char*,const char*);
916 if( sqlite3StrICmp(azArg[1],"sqlite_master")==0 ){
drha18c5682000-10-08 22:20:57 +0000917 char *new_argv[2], *new_colv[2];
918 new_argv[0] = "CREATE TABLE sqlite_master (\n"
919 " type text,\n"
920 " name text,\n"
921 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000922 " rootpage integer,\n"
drha18c5682000-10-08 22:20:57 +0000923 " sql text\n"
924 ")";
925 new_argv[1] = 0;
926 new_colv[0] = "sql";
927 new_colv[1] = 0;
928 callback(&data, 1, new_argv, new_colv);
danielk19774adee202004-05-08 08:23:19 +0000929 }else if( sqlite3StrICmp(azArg[1],"sqlite_temp_master")==0 ){
drhe0bc4042002-06-25 01:09:11 +0000930 char *new_argv[2], *new_colv[2];
931 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
932 " type text,\n"
933 " name text,\n"
934 " tbl_name text,\n"
935 " rootpage integer,\n"
936 " sql text\n"
937 ")";
938 new_argv[1] = 0;
939 new_colv[0] = "sql";
940 new_colv[1] = 0;
941 callback(&data, 1, new_argv, new_colv);
drha18c5682000-10-08 22:20:57 +0000942 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000943 sqlite3_exec_printf(p->db,
drhe0bc4042002-06-25 01:09:11 +0000944 "SELECT sql FROM "
945 " (SELECT * FROM sqlite_master UNION ALL"
946 " SELECT * FROM sqlite_temp_master) "
drh33048c02001-10-01 14:29:22 +0000947 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +0000948 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000949 callback, &data, &zErrMsg, azArg[1]);
950 }
drh75897232000-05-29 14:26:00 +0000951 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000952 sqlite3_exec(p->db,
drhe0bc4042002-06-25 01:09:11 +0000953 "SELECT sql FROM "
954 " (SELECT * FROM sqlite_master UNION ALL"
955 " SELECT * FROM sqlite_temp_master) "
drh33048c02001-10-01 14:29:22 +0000956 "WHERE type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +0000957 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000958 callback, &data, &zErrMsg
959 );
drh75897232000-05-29 14:26:00 +0000960 }
drh75897232000-05-29 14:26:00 +0000961 if( zErrMsg ){
962 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000963 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +0000964 }
965 }else
966
967 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
968 sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
969 }else
970
persicom7e2dfdd2002-04-18 02:46:52 +0000971 if( c=='s' && strncmp(azArg[0], "show", n)==0){
972 int i;
973 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
drh67505e72002-04-19 12:34:06 +0000974 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
drhdd45df82002-04-18 12:39:03 +0000975 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
persicom7e2dfdd2002-04-18 02:46:52 +0000976 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
977 fprintf(p->out,"%9.9s: %s\n","nullvalue", p->nullvalue);
drh67505e72002-04-19 12:34:06 +0000978 fprintf(p->out,"%9.9s: %s\n","output",
979 strlen(p->outfile) ? p->outfile : "stdout");
persicom7e2dfdd2002-04-18 02:46:52 +0000980 fprintf(p->out,"%9.9s: %s\n","separator", p->separator);
981 fprintf(p->out,"%9.9s: ","width");
982 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
983 fprintf(p->out,"%d ",p->colWidth[i]);
984 }
985 fprintf(p->out,"\n\n");
986 }else
987
drh2dfbbca2000-07-28 14:32:48 +0000988 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
drhe3710332000-09-29 13:30:53 +0000989 char **azResult;
990 int nRow, rc;
991 char *zErrMsg;
drh44c2eb12003-04-30 11:38:26 +0000992 open_db(p);
drha50da102000-08-08 20:19:09 +0000993 if( nArg==1 ){
danielk19776f8a5032004-05-10 10:34:51 +0000994 rc = sqlite3_get_table(p->db,
drha50da102000-08-08 20:19:09 +0000995 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +0000996 "WHERE type IN ('table','view') "
drhe0bc4042002-06-25 01:09:11 +0000997 "UNION ALL "
998 "SELECT name FROM sqlite_temp_master "
999 "WHERE type IN ('table','view') "
1000 "ORDER BY 1",
drha18c5682000-10-08 22:20:57 +00001001 &azResult, &nRow, 0, &zErrMsg
1002 );
drha50da102000-08-08 20:19:09 +00001003 }else{
danielk19776f8a5032004-05-10 10:34:51 +00001004 rc = sqlite3_get_table_printf(p->db,
drha50da102000-08-08 20:19:09 +00001005 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +00001006 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
drhe0bc4042002-06-25 01:09:11 +00001007 "UNION ALL "
1008 "SELECT name FROM sqlite_temp_master "
1009 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
1010 "ORDER BY 1",
1011 &azResult, &nRow, 0, &zErrMsg, azArg[1], azArg[1]
drha18c5682000-10-08 22:20:57 +00001012 );
drha50da102000-08-08 20:19:09 +00001013 }
drh75897232000-05-29 14:26:00 +00001014 if( zErrMsg ){
1015 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001016 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00001017 }
drhe3710332000-09-29 13:30:53 +00001018 if( rc==SQLITE_OK ){
1019 int len, maxlen = 0;
1020 int i, j;
1021 int nPrintCol, nPrintRow;
1022 for(i=1; i<=nRow; i++){
1023 if( azResult[i]==0 ) continue;
1024 len = strlen(azResult[i]);
1025 if( len>maxlen ) maxlen = len;
1026 }
1027 nPrintCol = 80/(maxlen+2);
1028 if( nPrintCol<1 ) nPrintCol = 1;
1029 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
1030 for(i=0; i<nPrintRow; i++){
1031 for(j=i+1; j<=nRow; j+=nPrintRow){
1032 char *zSp = j<=nPrintRow ? "" : " ";
1033 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
1034 }
1035 printf("\n");
1036 }
1037 }
danielk19776f8a5032004-05-10 10:34:51 +00001038 sqlite3_free_table(azResult);
drh75897232000-05-29 14:26:00 +00001039 }else
1040
drh2dfbbca2000-07-28 14:32:48 +00001041 if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
drh44c2eb12003-04-30 11:38:26 +00001042 open_db(p);
danielk19776f8a5032004-05-10 10:34:51 +00001043 sqlite3_busy_timeout(p->db, atoi(azArg[1]));
drh2dfbbca2000-07-28 14:32:48 +00001044 }else
1045
drh75897232000-05-29 14:26:00 +00001046 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
1047 int j;
1048 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
1049 p->colWidth[j-1] = atoi(azArg[j]);
1050 }
1051 }else
1052
1053 {
drh67505e72002-04-19 12:34:06 +00001054 fprintf(stderr, "unknown command or invalid arguments: "
1055 " \"%s\". Enter \".help\" for help\n", azArg[0]);
drh75897232000-05-29 14:26:00 +00001056 }
drh67505e72002-04-19 12:34:06 +00001057
1058 return rc;
drh75897232000-05-29 14:26:00 +00001059}
1060
drh67505e72002-04-19 12:34:06 +00001061/*
drh324ccef2003-02-05 14:06:20 +00001062** Return TRUE if the last non-whitespace character in z[] is a semicolon.
1063** z[] is N characters long.
1064*/
1065static int _ends_with_semicolon(const char *z, int N){
1066 while( N>0 && isspace(z[N-1]) ){ N--; }
1067 return N>0 && z[N-1]==';';
1068}
1069
1070/*
drh70c7a4b2003-04-26 03:03:06 +00001071** Test to see if a line consists entirely of whitespace.
1072*/
1073static int _all_whitespace(const char *z){
1074 for(; *z; z++){
1075 if( isspace(*z) ) continue;
1076 if( *z=='/' && z[1]=='*' ){
1077 z += 2;
1078 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
1079 if( *z==0 ) return 0;
1080 z++;
1081 continue;
1082 }
1083 if( *z=='-' && z[1]=='-' ){
1084 z += 2;
1085 while( *z && *z!='\n' ){ z++; }
1086 if( *z==0 ) return 1;
1087 continue;
1088 }
1089 return 0;
1090 }
1091 return 1;
1092}
1093
1094/*
drha9b17162003-04-29 18:01:28 +00001095** Return TRUE if the line typed in is an SQL command terminator other
1096** than a semi-colon. The SQL Server style "go" command is understood
1097** as is the Oracle "/".
1098*/
1099static int _is_command_terminator(const char *zLine){
danielk19774adee202004-05-08 08:23:19 +00001100 extern int sqlite3StrNICmp(const char*,const char*,int);
drha9b17162003-04-29 18:01:28 +00001101 while( isspace(*zLine) ){ zLine++; };
1102 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ) return 1; /* Oracle */
danielk19774adee202004-05-08 08:23:19 +00001103 if( sqlite3StrNICmp(zLine,"go",2)==0 && _all_whitespace(&zLine[2]) ){
drha9b17162003-04-29 18:01:28 +00001104 return 1; /* SQL Server */
1105 }
1106 return 0;
1107}
1108
1109/*
drh67505e72002-04-19 12:34:06 +00001110** Read input from *in and process it. If *in==0 then input
1111** is interactive - the user is typing it it. Otherwise, input
1112** is coming from a file or device. A prompt is issued and history
1113** is saved only if input is interactive. An interrupt signal will
1114** cause this routine to exit immediately, unless input is interactive.
1115*/
drhdaffd0e2001-04-11 14:28:42 +00001116static void process_input(struct callback_data *p, FILE *in){
1117 char *zLine;
1118 char *zSql = 0;
1119 int nSql = 0;
1120 char *zErrMsg;
drh7f953e22002-07-13 17:33:45 +00001121 int rc;
drh41e941d2002-04-04 15:10:12 +00001122 while( fflush(p->out), (zLine = one_input_line(zSql, in))!=0 ){
drh67505e72002-04-19 12:34:06 +00001123 if( seenInterrupt ){
1124 if( in!=0 ) break;
1125 seenInterrupt = 0;
1126 }
drhdaffd0e2001-04-11 14:28:42 +00001127 if( p->echoOn ) printf("%s\n", zLine);
drhf817b6b2003-06-16 00:16:41 +00001128 if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
drh2af0b2d2002-02-21 02:25:02 +00001129 if( zLine && zLine[0]=='.' && nSql==0 ){
drh44c2eb12003-04-30 11:38:26 +00001130 int rc = do_meta_command(zLine, p);
drhdaffd0e2001-04-11 14:28:42 +00001131 free(zLine);
drh67505e72002-04-19 12:34:06 +00001132 if( rc ) break;
drhdaffd0e2001-04-11 14:28:42 +00001133 continue;
1134 }
drha9b17162003-04-29 18:01:28 +00001135 if( _is_command_terminator(zLine) ){
1136 strcpy(zLine,";");
1137 }
drhdaffd0e2001-04-11 14:28:42 +00001138 if( zSql==0 ){
1139 int i;
1140 for(i=0; zLine[i] && isspace(zLine[i]); i++){}
1141 if( zLine[i]!=0 ){
1142 nSql = strlen(zLine);
1143 zSql = malloc( nSql+1 );
1144 strcpy(zSql, zLine);
1145 }
1146 }else{
1147 int len = strlen(zLine);
1148 zSql = realloc( zSql, nSql + len + 2 );
1149 if( zSql==0 ){
1150 fprintf(stderr,"%s: out of memory!\n", Argv0);
1151 exit(1);
1152 }
1153 strcpy(&zSql[nSql++], "\n");
1154 strcpy(&zSql[nSql], zLine);
1155 nSql += len;
1156 }
1157 free(zLine);
danielk19776f8a5032004-05-10 10:34:51 +00001158 if( zSql && _ends_with_semicolon(zSql, nSql) && sqlite3_complete(zSql) ){
drhdaffd0e2001-04-11 14:28:42 +00001159 p->cnt = 0;
drh44c2eb12003-04-30 11:38:26 +00001160 open_db(p);
danielk19776f8a5032004-05-10 10:34:51 +00001161 rc = sqlite3_exec(p->db, zSql, callback, p, &zErrMsg);
drh7f953e22002-07-13 17:33:45 +00001162 if( rc || zErrMsg ){
drhdaffd0e2001-04-11 14:28:42 +00001163 if( in!=0 && !p->echoOn ) printf("%s\n",zSql);
drh7f953e22002-07-13 17:33:45 +00001164 if( zErrMsg!=0 ){
1165 printf("SQL error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001166 sqlite3_free(zErrMsg);
drh7f953e22002-07-13 17:33:45 +00001167 zErrMsg = 0;
1168 }else{
danielk19772a02e332004-06-05 08:04:36 +00001169 printf("SQL error: %s\n", sqlite3_errmsg(p->db));
drh7f953e22002-07-13 17:33:45 +00001170 }
drhdaffd0e2001-04-11 14:28:42 +00001171 }
1172 free(zSql);
1173 zSql = 0;
1174 nSql = 0;
1175 }
1176 }
1177 if( zSql ){
drh70c7a4b2003-04-26 03:03:06 +00001178 if( !_all_whitespace(zSql) ) printf("Incomplete SQL: %s\n", zSql);
drhdaffd0e2001-04-11 14:28:42 +00001179 free(zSql);
1180 }
1181}
1182
drh67505e72002-04-19 12:34:06 +00001183/*
1184** Return a pathname which is the user's home directory. A
1185** 0 return indicates an error of some kind. Space to hold the
1186** resulting string is obtained from malloc(). The calling
1187** function should free the result.
1188*/
1189static char *find_home_dir(void){
1190 char *home_dir = NULL;
persicom7e2dfdd2002-04-18 02:46:52 +00001191
drh820f3812003-01-08 13:02:52 +00001192#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
drh67505e72002-04-19 12:34:06 +00001193 struct passwd *pwent;
1194 uid_t uid = getuid();
drhbd842ba2002-08-21 11:26:41 +00001195 if( (pwent=getpwuid(uid)) != NULL) {
1196 home_dir = pwent->pw_dir;
drh67505e72002-04-19 12:34:06 +00001197 }
1198#endif
1199
drh820f3812003-01-08 13:02:52 +00001200#ifdef __MACOS__
1201 char home_path[_MAX_PATH+1];
1202 home_dir = getcwd(home_path, _MAX_PATH);
1203#endif
1204
drh67505e72002-04-19 12:34:06 +00001205 if (!home_dir) {
1206 home_dir = getenv("HOME");
1207 if (!home_dir) {
1208 home_dir = getenv("HOMEPATH"); /* Windows? */
1209 }
1210 }
1211
drhe98d4fa2002-04-21 19:06:22 +00001212#if defined(_WIN32) || defined(WIN32)
1213 if (!home_dir) {
1214 home_dir = "c:";
1215 }
1216#endif
1217
drh67505e72002-04-19 12:34:06 +00001218 if( home_dir ){
1219 char *z = malloc( strlen(home_dir)+1 );
1220 if( z ) strcpy(z, home_dir);
1221 home_dir = z;
1222 }
drhe98d4fa2002-04-21 19:06:22 +00001223
drh67505e72002-04-19 12:34:06 +00001224 return home_dir;
1225}
1226
1227/*
1228** Read input from the file given by sqliterc_override. Or if that
1229** parameter is NULL, take input from ~/.sqliterc
1230*/
drh22fbcb82004-02-01 01:22:50 +00001231static void process_sqliterc(
1232 struct callback_data *p, /* Configuration data */
1233 const char *sqliterc_override /* Name of config file. NULL to use default */
1234){
persicom7e2dfdd2002-04-18 02:46:52 +00001235 char *home_dir = NULL;
drh22fbcb82004-02-01 01:22:50 +00001236 const char *sqliterc = sqliterc_override;
1237 char *zBuf;
persicom7e2dfdd2002-04-18 02:46:52 +00001238 FILE *in = NULL;
1239
1240 if (sqliterc == NULL) {
drh67505e72002-04-19 12:34:06 +00001241 home_dir = find_home_dir();
drhe98d4fa2002-04-21 19:06:22 +00001242 if( home_dir==0 ){
1243 fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0);
1244 return;
1245 }
drh22fbcb82004-02-01 01:22:50 +00001246 zBuf = malloc(strlen(home_dir) + 15);
1247 if( zBuf==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +00001248 fprintf(stderr,"%s: out of memory!\n", Argv0);
1249 exit(1);
1250 }
drh22fbcb82004-02-01 01:22:50 +00001251 sprintf(zBuf,"%s/.sqliterc",home_dir);
drh67505e72002-04-19 12:34:06 +00001252 free(home_dir);
drh22fbcb82004-02-01 01:22:50 +00001253 sqliterc = (const char*)zBuf;
persicom7e2dfdd2002-04-18 02:46:52 +00001254 }
drha1f9b5e2004-02-14 16:31:02 +00001255 in = fopen(sqliterc,"rb");
drh22fbcb82004-02-01 01:22:50 +00001256 if( in ){
1257 if( isatty(fileno(stdout)) ){
1258 printf("Loading resources from %s\n",sqliterc);
1259 }
persicom7e2dfdd2002-04-18 02:46:52 +00001260 process_input(p,in);
drhdd45df82002-04-18 12:39:03 +00001261 fclose(in);
persicom7e2dfdd2002-04-18 02:46:52 +00001262 }
1263 return;
1264}
1265
drh67505e72002-04-19 12:34:06 +00001266/*
drhe1e38c42003-05-04 18:30:59 +00001267** Show available command line options
1268*/
1269static const char zOptions[] =
1270 " -init filename read/process named file\n"
1271 " -echo print commands before execution\n"
1272 " -[no]header turn headers on or off\n"
1273 " -column set output mode to 'column'\n"
1274 " -html set output mode to HTML\n"
drh9eb9e262004-02-11 02:18:05 +00001275#ifdef SQLITE_HAS_CODEC
drh57ced912004-02-10 02:57:59 +00001276 " -key KEY encryption key\n"
1277#endif
drhe1e38c42003-05-04 18:30:59 +00001278 " -line set output mode to 'line'\n"
1279 " -list set output mode to 'list'\n"
1280 " -separator 'x' set output field separator (|)\n"
1281 " -nullvalue 'text' set text string for NULL values\n"
1282 " -version show SQLite version\n"
1283 " -help show this text, also show dot-commands\n"
1284;
1285static void usage(int showDetail){
1286 fprintf(stderr, "Usage: %s [OPTIONS] FILENAME [SQL]\n", Argv0);
1287 if( showDetail ){
1288 fprintf(stderr, "Options are:\n%s", zOptions);
1289 }else{
1290 fprintf(stderr, "Use the -help option for additional information\n");
1291 }
1292 exit(1);
1293}
1294
1295/*
drh67505e72002-04-19 12:34:06 +00001296** Initialize the state information in data
1297*/
persicom7e2dfdd2002-04-18 02:46:52 +00001298void main_init(struct callback_data *data) {
1299 memset(data, 0, sizeof(*data));
1300 data->mode = MODE_List;
1301 strcpy(data->separator,"|");
1302 data->showHeader = 0;
1303 strcpy(mainPrompt,"sqlite> ");
1304 strcpy(continuePrompt," ...> ");
1305}
1306
drh75897232000-05-29 14:26:00 +00001307int main(int argc, char **argv){
drh75897232000-05-29 14:26:00 +00001308 char *zErrMsg = 0;
1309 struct callback_data data;
drh22fbcb82004-02-01 01:22:50 +00001310 const char *zInitFile = 0;
1311 char *zFirstCmd = 0;
drh44c2eb12003-04-30 11:38:26 +00001312 int i;
danielk19774adee202004-05-08 08:23:19 +00001313 extern int sqlite3OsFileExists(const char*);
drh75897232000-05-29 14:26:00 +00001314
drh820f3812003-01-08 13:02:52 +00001315#ifdef __MACOS__
1316 argc = ccommand(&argv);
drh820f3812003-01-08 13:02:52 +00001317#endif
1318
drhdaffd0e2001-04-11 14:28:42 +00001319 Argv0 = argv[0];
persicom7e2dfdd2002-04-18 02:46:52 +00001320 main_init(&data);
persicom7e2dfdd2002-04-18 02:46:52 +00001321
drh44c2eb12003-04-30 11:38:26 +00001322 /* Make sure we have a valid signal handler early, before anything
1323 ** else is done.
1324 */
drh4c504392000-10-16 22:06:40 +00001325#ifdef SIGINT
1326 signal(SIGINT, interrupt_handler);
1327#endif
drh44c2eb12003-04-30 11:38:26 +00001328
drh22fbcb82004-02-01 01:22:50 +00001329 /* Do an initial pass through the command-line argument to locate
1330 ** the name of the database file, the name of the initialization file,
1331 ** and the first command to execute.
drh44c2eb12003-04-30 11:38:26 +00001332 */
drh22fbcb82004-02-01 01:22:50 +00001333 for(i=1; i<argc-1; i++){
drh44c2eb12003-04-30 11:38:26 +00001334 if( argv[i][0]!='-' ) break;
1335 if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){
1336 i++;
drh22fbcb82004-02-01 01:22:50 +00001337 }else if( strcmp(argv[i],"-init")==0 ){
1338 i++;
1339 zInitFile = argv[i];
1340 }else if( strcmp(argv[i],"-key")==0 ){
1341 i++;
danielk19776f8a5032004-05-10 10:34:51 +00001342 data.zKey = sqlite3_mprintf("%s",argv[i]);
drh44c2eb12003-04-30 11:38:26 +00001343 }
1344 }
drh22fbcb82004-02-01 01:22:50 +00001345 if( i<argc ){
1346 data.zDbFilename = argv[i++];
1347 }else{
1348 data.zDbFilename = ":memory:";
1349 }
1350 if( i<argc ){
1351 zFirstCmd = argv[i++];
1352 }
drh44c2eb12003-04-30 11:38:26 +00001353 data.out = stdout;
1354
1355 /* Go ahead and open the database file if it already exists. If the
1356 ** file does not exist, delay opening it. This prevents empty database
1357 ** files from being created if a user mistypes the database name argument
1358 ** to the sqlite command-line tool.
1359 */
danielk19774adee202004-05-08 08:23:19 +00001360 if( sqlite3OsFileExists(data.zDbFilename) ){
drh44c2eb12003-04-30 11:38:26 +00001361 open_db(&data);
1362 }
1363
drh22fbcb82004-02-01 01:22:50 +00001364 /* Process the initialization file if there is one. If no -init option
1365 ** is given on the command line, look for a file named ~/.sqliterc and
1366 ** try to process it.
drh44c2eb12003-04-30 11:38:26 +00001367 */
drh22fbcb82004-02-01 01:22:50 +00001368 process_sqliterc(&data,zInitFile);
drh44c2eb12003-04-30 11:38:26 +00001369
drh22fbcb82004-02-01 01:22:50 +00001370 /* Make a second pass through the command-line argument and set
1371 ** options. This second pass is delayed until after the initialization
1372 ** file is processed so that the command-line arguments will override
1373 ** settings in the initialization file.
drh44c2eb12003-04-30 11:38:26 +00001374 */
drh22fbcb82004-02-01 01:22:50 +00001375 for(i=1; i<argc && argv[i][0]=='-'; i++){
1376 char *z = argv[i];
1377 if( strcmp(z,"-init")==0 || strcmp(z,"-key")==0 ){
1378 i++;
1379 }else if( strcmp(z,"-html")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001380 data.mode = MODE_Html;
drh22fbcb82004-02-01 01:22:50 +00001381 }else if( strcmp(z,"-list")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001382 data.mode = MODE_List;
drh22fbcb82004-02-01 01:22:50 +00001383 }else if( strcmp(z,"-line")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001384 data.mode = MODE_Line;
drh22fbcb82004-02-01 01:22:50 +00001385 }else if( strcmp(z,"-column")==0 ){
drh8b32e172002-04-08 02:42:57 +00001386 data.mode = MODE_Column;
drh22fbcb82004-02-01 01:22:50 +00001387 }else if( strcmp(z,"-separator")==0 ){
1388 i++;
1389 sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[i]);
1390 }else if( strcmp(z,"-nullvalue")==0 ){
1391 i++;
1392 sprintf(data.nullvalue,"%.*s",(int)sizeof(data.nullvalue)-1,argv[i]);
1393 }else if( strcmp(z,"-header")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001394 data.showHeader = 1;
drh22fbcb82004-02-01 01:22:50 +00001395 }else if( strcmp(z,"-noheader")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001396 data.showHeader = 0;
drh22fbcb82004-02-01 01:22:50 +00001397 }else if( strcmp(z,"-echo")==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001398 data.echoOn = 1;
drh22fbcb82004-02-01 01:22:50 +00001399 }else if( strcmp(z,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001400 printf("%s\n", sqlite3_version);
drhe1e38c42003-05-04 18:30:59 +00001401 return 1;
drh22fbcb82004-02-01 01:22:50 +00001402 }else if( strcmp(z,"-help")==0 ){
drhe1e38c42003-05-04 18:30:59 +00001403 usage(1);
drh1e5d0e92000-05-31 23:33:17 +00001404 }else{
drh22fbcb82004-02-01 01:22:50 +00001405 fprintf(stderr,"%s: unknown option: %s\n", Argv0, z);
drhe1e38c42003-05-04 18:30:59 +00001406 fprintf(stderr,"Use -help for a list of options.\n");
drh1e5d0e92000-05-31 23:33:17 +00001407 return 1;
1408 }
1409 }
drh44c2eb12003-04-30 11:38:26 +00001410
drh22fbcb82004-02-01 01:22:50 +00001411 if( zFirstCmd ){
drh44c2eb12003-04-30 11:38:26 +00001412 /* Run just the command that follows the database name
1413 */
drh22fbcb82004-02-01 01:22:50 +00001414 if( zFirstCmd[0]=='.' ){
1415 do_meta_command(zFirstCmd, &data);
drh6ff13852001-11-25 13:18:23 +00001416 exit(0);
1417 }else{
1418 int rc;
drh44c2eb12003-04-30 11:38:26 +00001419 open_db(&data);
danielk19776f8a5032004-05-10 10:34:51 +00001420 rc = sqlite3_exec(data.db, zFirstCmd, callback, &data, &zErrMsg);
drh6ff13852001-11-25 13:18:23 +00001421 if( rc!=0 && zErrMsg!=0 ){
1422 fprintf(stderr,"SQL error: %s\n", zErrMsg);
1423 exit(1);
1424 }
drh75897232000-05-29 14:26:00 +00001425 }
1426 }else{
drh44c2eb12003-04-30 11:38:26 +00001427 /* Run commands received from standard input
1428 */
drh5cf590c2003-04-24 01:45:04 +00001429 if( isatty(fileno(stdout)) && isatty(fileno(stdin)) ){
drh67505e72002-04-19 12:34:06 +00001430 char *zHome;
1431 char *zHistory = 0;
drh75897232000-05-29 14:26:00 +00001432 printf(
drhb217a572000-08-22 13:40:18 +00001433 "SQLite version %s\n"
1434 "Enter \".help\" for instructions\n",
danielk19776f8a5032004-05-10 10:34:51 +00001435 sqlite3_version
drh75897232000-05-29 14:26:00 +00001436 );
drh67505e72002-04-19 12:34:06 +00001437 zHome = find_home_dir();
1438 if( zHome && (zHistory = malloc(strlen(zHome)+20))!=0 ){
1439 sprintf(zHistory,"%s/.sqlite_history", zHome);
1440 }
1441 if( zHistory ) read_history(zHistory);
drhdaffd0e2001-04-11 14:28:42 +00001442 process_input(&data, 0);
drh67505e72002-04-19 12:34:06 +00001443 if( zHistory ){
1444 stifle_history(100);
1445 write_history(zHistory);
1446 }
drhdaffd0e2001-04-11 14:28:42 +00001447 }else{
1448 process_input(&data, stdin);
drh75897232000-05-29 14:26:00 +00001449 }
1450 }
drh33048c02001-10-01 14:29:22 +00001451 set_table_name(&data, 0);
danielk19776f8a5032004-05-10 10:34:51 +00001452 if( db ) sqlite3_close(db);
drh75897232000-05-29 14:26:00 +00001453 return 0;
1454}