blob: 0c6c9afba5f6ff9e844010860b5b3bf4ee747a0c [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**
drh1d482dd2004-05-31 18:23:07 +000015** $Id: shell.c,v 1.99 2004/05/31 18:23:08 drh Exp $
drh75897232000-05-29 14:26:00 +000016*/
17#include <stdlib.h>
18#include <string.h>
19#include <stdio.h>
drh1d482dd2004-05-31 18:23:07 +000020#include "sqlite3.h"
drh75897232000-05-29 14:26:00 +000021#include <ctype.h>
persicom7e2dfdd2002-04-18 02:46:52 +000022
drh820f3812003-01-08 13:02:52 +000023#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
drh4c504392000-10-16 22:06:40 +000024# include <signal.h>
drhdd45df82002-04-18 12:39:03 +000025# include <pwd.h>
26# include <unistd.h>
27# include <sys/types.h>
drh4c504392000-10-16 22:06:40 +000028#endif
drh75897232000-05-29 14:26:00 +000029
drh820f3812003-01-08 13:02:52 +000030#ifdef __MACOS__
31# include <console.h>
32# include <signal.h>
33# include <unistd.h>
34# include <extras.h>
35# include <Files.h>
36# include <Folders.h>
37#endif
38
drh16e59552000-07-31 11:57:37 +000039#if defined(HAVE_READLINE) && HAVE_READLINE==1
drh8e7e7a22000-05-30 18:45:23 +000040# include <readline/readline.h>
41# include <readline/history.h>
42#else
drh9347b202003-07-18 01:30:59 +000043# define readline(p) local_getline(p,stdin)
persicom1d0b8722002-04-18 02:53:04 +000044# define add_history(X)
drh67505e72002-04-19 12:34:06 +000045# define read_history(X)
46# define write_history(X)
47# define stifle_history(X)
drh75897232000-05-29 14:26:00 +000048#endif
49
drh4328c8b2003-04-26 02:50:11 +000050/* Make sure isatty() has a prototype.
51*/
52extern int isatty();
53
drh75897232000-05-29 14:26:00 +000054/*
drh4c504392000-10-16 22:06:40 +000055** The following is the open SQLite database. We make a pointer
56** to this database a static variable so that it can be accessed
57** by the SIGINT handler to interrupt database processing.
58*/
59static sqlite *db = 0;
60
61/*
drh67505e72002-04-19 12:34:06 +000062** True if an interrupt (Control-C) has been received.
63*/
64static int seenInterrupt = 0;
65
66/*
persicom7e2dfdd2002-04-18 02:46:52 +000067** This is the name of our program. It is set in main(), used
68** in a number of other places, mostly for error messages.
69*/
70static char *Argv0;
71
72/*
73** Prompt strings. Initialized in main. Settable with
74** .prompt main continue
75*/
76static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
77static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
78
drh44c2eb12003-04-30 11:38:26 +000079
persicom7e2dfdd2002-04-18 02:46:52 +000080/*
drh83965662003-04-17 02:54:13 +000081** Determines if a string is a number of not.
82*/
danielk19778a6b5412004-05-24 07:04:25 +000083extern int sqlite3IsNumber(const char*, int*, unsigned char);
drh83965662003-04-17 02:54:13 +000084
85/*
drh8e7e7a22000-05-30 18:45:23 +000086** This routine reads a line of text from standard input, stores
87** the text in memory obtained from malloc() and returns a pointer
88** to the text. NULL is returned at end of file, or if malloc()
89** fails.
90**
91** The interface is like "readline" but no command-line editing
92** is done.
93*/
drh9347b202003-07-18 01:30:59 +000094static char *local_getline(char *zPrompt, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +000095 char *zLine;
96 int nLine;
drh8e7e7a22000-05-30 18:45:23 +000097 int n;
98 int eol;
99
100 if( zPrompt && *zPrompt ){
101 printf("%s",zPrompt);
102 fflush(stdout);
103 }
104 nLine = 100;
105 zLine = malloc( nLine );
106 if( zLine==0 ) return 0;
107 n = 0;
108 eol = 0;
109 while( !eol ){
110 if( n+100>nLine ){
111 nLine = nLine*2 + 100;
112 zLine = realloc(zLine, nLine);
113 if( zLine==0 ) return 0;
114 }
drhdaffd0e2001-04-11 14:28:42 +0000115 if( fgets(&zLine[n], nLine - n, in)==0 ){
drh8e7e7a22000-05-30 18:45:23 +0000116 if( n==0 ){
117 free(zLine);
118 return 0;
119 }
120 zLine[n] = 0;
121 eol = 1;
122 break;
123 }
124 while( zLine[n] ){ n++; }
125 if( n>0 && zLine[n-1]=='\n' ){
126 n--;
127 zLine[n] = 0;
128 eol = 1;
129 }
130 }
131 zLine = realloc( zLine, n+1 );
132 return zLine;
133}
134
135/*
136** Retrieve a single line of input text. "isatty" is true if text
137** is coming from a terminal. In that case, we issue a prompt and
138** attempt to use "readline" for command-line editing. If "isatty"
drh9347b202003-07-18 01:30:59 +0000139** is false, use "local_getline" instead of "readline" and issue no prompt.
drh8e7e7a22000-05-30 18:45:23 +0000140**
141** zPrior is a string of prior text retrieved. If not the empty
142** string, then issue a continuation prompt.
143*/
drhdaffd0e2001-04-11 14:28:42 +0000144static char *one_input_line(const char *zPrior, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +0000145 char *zPrompt;
146 char *zResult;
drhdaffd0e2001-04-11 14:28:42 +0000147 if( in!=0 ){
drh9347b202003-07-18 01:30:59 +0000148 return local_getline(0, in);
drh8e7e7a22000-05-30 18:45:23 +0000149 }
150 if( zPrior && zPrior[0] ){
persicom7e2dfdd2002-04-18 02:46:52 +0000151 zPrompt = continuePrompt;
drh8e7e7a22000-05-30 18:45:23 +0000152 }else{
persicom7e2dfdd2002-04-18 02:46:52 +0000153 zPrompt = mainPrompt;
drh8e7e7a22000-05-30 18:45:23 +0000154 }
155 zResult = readline(zPrompt);
drh2dfbbca2000-07-28 14:32:48 +0000156 if( zResult ) add_history(zResult);
drh8e7e7a22000-05-30 18:45:23 +0000157 return zResult;
158}
159
persicom7e2dfdd2002-04-18 02:46:52 +0000160struct previous_mode_data {
161 int valid; /* Is there legit data in here? */
162 int mode;
163 int showHeader;
164 int colWidth[100];
165};
drh8e7e7a22000-05-30 18:45:23 +0000166/*
drh75897232000-05-29 14:26:00 +0000167** An pointer to an instance of this structure is passed from
168** the main program to the callback. This is used to communicate
169** state and mode information.
170*/
171struct callback_data {
drh28bd4bc2000-06-15 15:57:22 +0000172 sqlite *db; /* The database */
drhdaffd0e2001-04-11 14:28:42 +0000173 int echoOn; /* True to echo input commands */
drh28bd4bc2000-06-15 15:57:22 +0000174 int cnt; /* Number of records displayed so far */
175 FILE *out; /* Write results here */
176 int mode; /* An output mode setting */
177 int showHeader; /* True to show column names in List or Column mode */
drh33048c02001-10-01 14:29:22 +0000178 char *zDestTable; /* Name of destination table when MODE_Insert */
drh28bd4bc2000-06-15 15:57:22 +0000179 char separator[20]; /* Separator character for MODE_List */
drha0c66f52000-07-29 13:20:21 +0000180 int colWidth[100]; /* Requested width of each column when in column mode*/
181 int actualWidth[100]; /* Actual width of each column */
drh83965662003-04-17 02:54:13 +0000182 char nullvalue[20]; /* The text to print when a NULL comes back from
183 ** the database */
persicom7e2dfdd2002-04-18 02:46:52 +0000184 struct previous_mode_data explainPrev;
drh83965662003-04-17 02:54:13 +0000185 /* Holds the mode information just before
186 ** .explain ON */
drh44c2eb12003-04-30 11:38:26 +0000187 char outfile[FILENAME_MAX]; /* Filename for *out */
188 const char *zDbFilename; /* name of the database file */
drh22fbcb82004-02-01 01:22:50 +0000189 char *zKey; /* Encryption key */
drh75897232000-05-29 14:26:00 +0000190};
191
192/*
193** These are the allowed modes.
194*/
drh967e8b72000-06-21 13:59:10 +0000195#define MODE_Line 0 /* One column per line. Blank line between records */
drh75897232000-05-29 14:26:00 +0000196#define MODE_Column 1 /* One record per line in neat columns */
197#define MODE_List 2 /* One record per line with a separator */
drhe3710332000-09-29 13:30:53 +0000198#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
199#define MODE_Html 4 /* Generate an XHTML table */
200#define MODE_Insert 5 /* Generate SQL "insert" statements */
jplyon672a1ed2003-05-11 20:07:05 +0000201#define MODE_NUM_OF 6 /* The number of modes (not a mode itself) */
persicom7e2dfdd2002-04-18 02:46:52 +0000202
203char *modeDescr[MODE_NUM_OF] = {
204 "line",
205 "column",
206 "list",
207 "semi",
208 "html",
209 "insert"
210};
drh75897232000-05-29 14:26:00 +0000211
212/*
213** Number of elements in an array
214*/
215#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
216
217/*
drh28bd4bc2000-06-15 15:57:22 +0000218** Output the given string as a quoted string using SQL quoting conventions.
219*/
220static void output_quoted_string(FILE *out, const char *z){
221 int i;
222 int nSingle = 0;
drh28bd4bc2000-06-15 15:57:22 +0000223 for(i=0; z[i]; i++){
224 if( z[i]=='\'' ) nSingle++;
drh28bd4bc2000-06-15 15:57:22 +0000225 }
226 if( nSingle==0 ){
227 fprintf(out,"'%s'",z);
drh28bd4bc2000-06-15 15:57:22 +0000228 }else{
229 fprintf(out,"'");
230 while( *z ){
231 for(i=0; z[i] && z[i]!='\''; i++){}
232 if( i==0 ){
233 fprintf(out,"''");
234 z++;
235 }else if( z[i]=='\'' ){
236 fprintf(out,"%.*s''",i,z);
237 z += i+1;
238 }else{
drhcd7d2732002-02-26 23:24:26 +0000239 fprintf(out,"%s",z);
drh28bd4bc2000-06-15 15:57:22 +0000240 break;
241 }
242 }
drhcd7d2732002-02-26 23:24:26 +0000243 fprintf(out,"'");
drh28bd4bc2000-06-15 15:57:22 +0000244 }
245}
246
247/*
drhc08a4f12000-06-15 16:49:48 +0000248** Output the given string with characters that are special to
249** HTML escaped.
250*/
251static void output_html_string(FILE *out, const char *z){
252 int i;
253 while( *z ){
254 for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){}
255 if( i>0 ){
256 fprintf(out,"%.*s",i,z);
257 }
258 if( z[i]=='<' ){
259 fprintf(out,"&lt;");
260 }else if( z[i]=='&' ){
261 fprintf(out,"&amp;");
262 }else{
263 break;
264 }
265 z += i + 1;
266 }
267}
268
269/*
drh4c504392000-10-16 22:06:40 +0000270** This routine runs when the user presses Ctrl-C
271*/
272static void interrupt_handler(int NotUsed){
drh67505e72002-04-19 12:34:06 +0000273 seenInterrupt = 1;
danielk19776f8a5032004-05-10 10:34:51 +0000274 if( db ) sqlite3_interrupt(db);
drh4c504392000-10-16 22:06:40 +0000275}
276
277/*
drh75897232000-05-29 14:26:00 +0000278** This is the callback routine that the SQLite library
279** invokes for each row of a query result.
280*/
281static int callback(void *pArg, int nArg, char **azArg, char **azCol){
282 int i;
283 struct callback_data *p = (struct callback_data*)pArg;
284 switch( p->mode ){
285 case MODE_Line: {
drhe3710332000-09-29 13:30:53 +0000286 int w = 5;
drh6a535342001-10-19 16:44:56 +0000287 if( azArg==0 ) break;
drhe3710332000-09-29 13:30:53 +0000288 for(i=0; i<nArg; i++){
289 int len = strlen(azCol[i]);
290 if( len>w ) w = len;
291 }
drh75897232000-05-29 14:26:00 +0000292 if( p->cnt++>0 ) fprintf(p->out,"\n");
293 for(i=0; i<nArg; i++){
drha69d9162003-04-17 22:57:53 +0000294 fprintf(p->out,"%*s = %s\n", w, azCol[i],
295 azArg[i] ? azArg[i] : p->nullvalue);
drh75897232000-05-29 14:26:00 +0000296 }
297 break;
298 }
299 case MODE_Column: {
drha0c66f52000-07-29 13:20:21 +0000300 if( p->cnt++==0 ){
drh75897232000-05-29 14:26:00 +0000301 for(i=0; i<nArg; i++){
drha0c66f52000-07-29 13:20:21 +0000302 int w, n;
303 if( i<ArraySize(p->colWidth) ){
drh75897232000-05-29 14:26:00 +0000304 w = p->colWidth[i];
305 }else{
drha0c66f52000-07-29 13:20:21 +0000306 w = 0;
drh75897232000-05-29 14:26:00 +0000307 }
drha0c66f52000-07-29 13:20:21 +0000308 if( w<=0 ){
drhff6e9112000-08-28 16:21:58 +0000309 w = strlen(azCol[i] ? azCol[i] : "");
drha0c66f52000-07-29 13:20:21 +0000310 if( w<10 ) w = 10;
persicom7e2dfdd2002-04-18 02:46:52 +0000311 n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue);
drha0c66f52000-07-29 13:20:21 +0000312 if( w<n ) w = n;
313 }
314 if( i<ArraySize(p->actualWidth) ){
persicom1d0b8722002-04-18 02:53:04 +0000315 p->actualWidth[i] = w;
drha0c66f52000-07-29 13:20:21 +0000316 }
317 if( p->showHeader ){
318 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
319 }
320 }
321 if( p->showHeader ){
322 for(i=0; i<nArg; i++){
323 int w;
324 if( i<ArraySize(p->actualWidth) ){
325 w = p->actualWidth[i];
326 }else{
327 w = 10;
328 }
329 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
330 "----------------------------------------------------------",
331 i==nArg-1 ? "\n": " ");
332 }
drh75897232000-05-29 14:26:00 +0000333 }
334 }
drh6a535342001-10-19 16:44:56 +0000335 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000336 for(i=0; i<nArg; i++){
337 int w;
drha0c66f52000-07-29 13:20:21 +0000338 if( i<ArraySize(p->actualWidth) ){
339 w = p->actualWidth[i];
drh75897232000-05-29 14:26:00 +0000340 }else{
341 w = 10;
342 }
drhc61053b2000-06-04 12:58:36 +0000343 fprintf(p->out,"%-*.*s%s",w,w,
persicom7e2dfdd2002-04-18 02:46:52 +0000344 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
drh75897232000-05-29 14:26:00 +0000345 }
346 break;
347 }
drhe3710332000-09-29 13:30:53 +0000348 case MODE_Semi:
drh75897232000-05-29 14:26:00 +0000349 case MODE_List: {
350 if( p->cnt++==0 && p->showHeader ){
351 for(i=0; i<nArg; i++){
352 fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
353 }
354 }
drh6a535342001-10-19 16:44:56 +0000355 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000356 for(i=0; i<nArg; i++){
drh4c653a02000-06-07 01:27:47 +0000357 char *z = azArg[i];
persicom7e2dfdd2002-04-18 02:46:52 +0000358 if( z==0 ) z = p->nullvalue;
drh71172c52002-01-24 00:00:21 +0000359 fprintf(p->out, "%s", z);
drhe3710332000-09-29 13:30:53 +0000360 if( i<nArg-1 ){
361 fprintf(p->out, "%s", p->separator);
362 }else if( p->mode==MODE_Semi ){
363 fprintf(p->out, ";\n");
364 }else{
365 fprintf(p->out, "\n");
366 }
drh75897232000-05-29 14:26:00 +0000367 }
368 break;
369 }
drh1e5d0e92000-05-31 23:33:17 +0000370 case MODE_Html: {
371 if( p->cnt++==0 && p->showHeader ){
372 fprintf(p->out,"<TR>");
373 for(i=0; i<nArg; i++){
374 fprintf(p->out,"<TH>%s</TH>",azCol[i]);
375 }
376 fprintf(p->out,"</TR>\n");
377 }
drh6a535342001-10-19 16:44:56 +0000378 if( azArg==0 ) break;
drh28bd4bc2000-06-15 15:57:22 +0000379 fprintf(p->out,"<TR>");
drh1e5d0e92000-05-31 23:33:17 +0000380 for(i=0; i<nArg; i++){
drhc08a4f12000-06-15 16:49:48 +0000381 fprintf(p->out,"<TD>");
persicom7e2dfdd2002-04-18 02:46:52 +0000382 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
drhc08a4f12000-06-15 16:49:48 +0000383 fprintf(p->out,"</TD>\n");
drh1e5d0e92000-05-31 23:33:17 +0000384 }
drh7d686b22002-11-11 13:56:47 +0000385 fprintf(p->out,"</TR>\n");
drh1e5d0e92000-05-31 23:33:17 +0000386 break;
387 }
drh28bd4bc2000-06-15 15:57:22 +0000388 case MODE_Insert: {
drh6a535342001-10-19 16:44:56 +0000389 if( azArg==0 ) break;
drh33048c02001-10-01 14:29:22 +0000390 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
drh28bd4bc2000-06-15 15:57:22 +0000391 for(i=0; i<nArg; i++){
392 char *zSep = i>0 ? ",": "";
393 if( azArg[i]==0 ){
394 fprintf(p->out,"%sNULL",zSep);
danielk19778a6b5412004-05-24 07:04:25 +0000395 }else if( sqlite3IsNumber(azArg[i], 0, 1) ){
drh28bd4bc2000-06-15 15:57:22 +0000396 fprintf(p->out,"%s%s",zSep, azArg[i]);
397 }else{
398 if( zSep[0] ) fprintf(p->out,"%s",zSep);
399 output_quoted_string(p->out, azArg[i]);
400 }
401 }
402 fprintf(p->out,");\n");
drh6a535342001-10-19 16:44:56 +0000403 break;
drh28bd4bc2000-06-15 15:57:22 +0000404 }
persicom1d0b8722002-04-18 02:53:04 +0000405 }
drh75897232000-05-29 14:26:00 +0000406 return 0;
407}
408
409/*
drh33048c02001-10-01 14:29:22 +0000410** Set the destination table field of the callback_data structure to
411** the name of the table given. Escape any quote characters in the
412** table name.
413*/
414static void set_table_name(struct callback_data *p, const char *zName){
415 int i, n;
416 int needQuote;
417 char *z;
418
419 if( p->zDestTable ){
420 free(p->zDestTable);
421 p->zDestTable = 0;
422 }
423 if( zName==0 ) return;
424 needQuote = !isalpha(*zName) && *zName!='_';
425 for(i=n=0; zName[i]; i++, n++){
426 if( !isalnum(zName[i]) && zName[i]!='_' ){
427 needQuote = 1;
428 if( zName[i]=='\'' ) n++;
429 }
430 }
431 if( needQuote ) n += 2;
432 z = p->zDestTable = malloc( n+1 );
433 if( z==0 ){
434 fprintf(stderr,"Out of memory!\n");
435 exit(1);
436 }
437 n = 0;
438 if( needQuote ) z[n++] = '\'';
439 for(i=0; zName[i]; i++){
440 z[n++] = zName[i];
441 if( zName[i]=='\'' ) z[n++] = '\'';
442 }
443 if( needQuote ) z[n++] = '\'';
444 z[n] = 0;
445}
446
447/*
drh4c653a02000-06-07 01:27:47 +0000448** This is a different callback routine used for dumping the database.
449** Each row received by this callback consists of a table name,
450** the table type ("index" or "table") and SQL to create the table.
451** This routine should print text sufficient to recreate the table.
452*/
453static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
drhdaffd0e2001-04-11 14:28:42 +0000454 struct callback_data *p = (struct callback_data *)pArg;
drh4c653a02000-06-07 01:27:47 +0000455 if( nArg!=3 ) return 1;
drhdaffd0e2001-04-11 14:28:42 +0000456 fprintf(p->out, "%s;\n", azArg[2]);
drh4c653a02000-06-07 01:27:47 +0000457 if( strcmp(azArg[1],"table")==0 ){
458 struct callback_data d2;
drhdaffd0e2001-04-11 14:28:42 +0000459 d2 = *p;
drh33048c02001-10-01 14:29:22 +0000460 d2.mode = MODE_Insert;
461 d2.zDestTable = 0;
462 set_table_name(&d2, azArg[0]);
danielk19776f8a5032004-05-10 10:34:51 +0000463 sqlite3_exec_printf(p->db,
drha18c5682000-10-08 22:20:57 +0000464 "SELECT * FROM '%q'",
465 callback, &d2, 0, azArg[0]
466 );
drh33048c02001-10-01 14:29:22 +0000467 set_table_name(&d2, 0);
drh4c653a02000-06-07 01:27:47 +0000468 }
drh4c653a02000-06-07 01:27:47 +0000469 return 0;
470}
471
472/*
drh75897232000-05-29 14:26:00 +0000473** Text of a help message
474*/
persicom1d0b8722002-04-18 02:53:04 +0000475static char zHelp[] =
jplyon6a65bb32003-05-04 07:25:57 +0000476 ".databases List names and files of attached databases\n"
477 ".dump ?TABLE? ... Dump the database in a text format\n"
drhdaffd0e2001-04-11 14:28:42 +0000478 ".echo ON|OFF Turn command echo on or off\n"
drh75897232000-05-29 14:26:00 +0000479 ".exit Exit this program\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000480 ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000481 ".header(s) ON|OFF Turn display of headers on or off\n"
drh75897232000-05-29 14:26:00 +0000482 ".help Show this message\n"
483 ".indices TABLE Show names of all indices on TABLE\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000484 ".mode MODE Set mode to one of \"line(s)\", \"column(s)\", \n"
drhe3710332000-09-29 13:30:53 +0000485 " \"insert\", \"list\", or \"html\"\n"
drhc08a4f12000-06-15 16:49:48 +0000486 ".mode insert TABLE Generate SQL insert statements for TABLE\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000487 ".nullvalue STRING Print STRING instead of nothing for NULL data\n"
drh75897232000-05-29 14:26:00 +0000488 ".output FILENAME Send output to FILENAME\n"
489 ".output stdout Send output to the screen\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000490 ".prompt MAIN CONTINUE Replace the standard prompts\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000491 ".quit Exit this program\n"
drhdaffd0e2001-04-11 14:28:42 +0000492 ".read FILENAME Execute SQL in FILENAME\n"
drh9eb9e262004-02-11 02:18:05 +0000493#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000494 ".rekey OLD NEW NEW Change the encryption key\n"
495#endif
drh75897232000-05-29 14:26:00 +0000496 ".schema ?TABLE? Show the CREATE statements\n"
497 ".separator STRING Change separator string for \"list\" mode\n"
drhdd45df82002-04-18 12:39:03 +0000498 ".show Show the current values for various settings\n"
drha50da102000-08-08 20:19:09 +0000499 ".tables ?PATTERN? List names of tables matching a pattern\n"
drh2dfbbca2000-07-28 14:32:48 +0000500 ".timeout MS Try opening locked tables for MS milliseconds\n"
drh75897232000-05-29 14:26:00 +0000501 ".width NUM NUM ... Set column widths for \"column\" mode\n"
502;
503
drhdaffd0e2001-04-11 14:28:42 +0000504/* Forward reference */
505static void process_input(struct callback_data *p, FILE *in);
506
drh75897232000-05-29 14:26:00 +0000507/*
drh44c2eb12003-04-30 11:38:26 +0000508** Make sure the database is open. If it is not, then open it. If
509** the database fails to open, print an error message and exit.
510*/
511static void open_db(struct callback_data *p){
512 if( p->db==0 ){
drh9eb9e262004-02-11 02:18:05 +0000513#ifdef SQLITE_HAS_CODEC
drheb8ed702004-02-11 10:37:23 +0000514 int n = p->zKey ? strlen(p->zKey) : 0;
danielk19776f8a5032004-05-10 10:34:51 +0000515 db = p->db = sqlite3_open_encrypted(p->zDbFilename, p->zKey, n, 0, &zErrMsg);
danielk197780290862004-05-22 09:21:21 +0000516 assert(0); /* Encrypted databases are broken in SQLite 3 */
drheb8ed702004-02-11 10:37:23 +0000517#else
danielk197780290862004-05-22 09:21:21 +0000518 sqlite3_open(p->zDbFilename, &p->db, 0);
519 db = p->db;
drheb8ed702004-02-11 10:37:23 +0000520#endif
danielk197780290862004-05-22 09:21:21 +0000521 if( SQLITE_OK!=sqlite3_errcode(db) ){
522 fprintf(stderr,"Unable to open database \"%s\": %s\n",
523 p->zDbFilename, sqlite3_errmsg(db));
drh22fbcb82004-02-01 01:22:50 +0000524 exit(1);
drh44c2eb12003-04-30 11:38:26 +0000525 }
526 }
527}
528
529/*
drh75897232000-05-29 14:26:00 +0000530** If an input line begins with "." then invoke this routine to
531** process that line.
drh67505e72002-04-19 12:34:06 +0000532**
533** Return 1 to exit and 0 to continue.
drh75897232000-05-29 14:26:00 +0000534*/
drh44c2eb12003-04-30 11:38:26 +0000535static int do_meta_command(char *zLine, struct callback_data *p){
drh75897232000-05-29 14:26:00 +0000536 int i = 1;
537 int nArg = 0;
538 int n, c;
drh67505e72002-04-19 12:34:06 +0000539 int rc = 0;
drh75897232000-05-29 14:26:00 +0000540 char *azArg[50];
541
542 /* Parse the input line into tokens.
543 */
544 while( zLine[i] && nArg<ArraySize(azArg) ){
545 while( isspace(zLine[i]) ){ i++; }
drh06333682004-03-09 13:37:45 +0000546 if( zLine[i]==0 ) break;
drh75897232000-05-29 14:26:00 +0000547 if( zLine[i]=='\'' || zLine[i]=='"' ){
548 int delim = zLine[i++];
549 azArg[nArg++] = &zLine[i];
550 while( zLine[i] && zLine[i]!=delim ){ i++; }
551 if( zLine[i]==delim ){
552 zLine[i++] = 0;
553 }
554 }else{
555 azArg[nArg++] = &zLine[i];
556 while( zLine[i] && !isspace(zLine[i]) ){ i++; }
557 if( zLine[i] ) zLine[i++] = 0;
558 }
559 }
560
561 /* Process the input line.
562 */
drh67505e72002-04-19 12:34:06 +0000563 if( nArg==0 ) return rc;
drh75897232000-05-29 14:26:00 +0000564 n = strlen(azArg[0]);
565 c = azArg[0][0];
jplyon6a65bb32003-05-04 07:25:57 +0000566 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
jplyon672a1ed2003-05-11 20:07:05 +0000567 struct callback_data data;
568 char *zErrMsg = 0;
jplyon6a65bb32003-05-04 07:25:57 +0000569 open_db(p);
jplyon672a1ed2003-05-11 20:07:05 +0000570 memcpy(&data, p, sizeof(data));
drhd8885442004-03-17 23:42:12 +0000571 data.showHeader = 1;
jplyon672a1ed2003-05-11 20:07:05 +0000572 data.mode = MODE_Column;
drhd8885442004-03-17 23:42:12 +0000573 data.colWidth[0] = 3;
574 data.colWidth[1] = 15;
575 data.colWidth[2] = 58;
danielk19776f8a5032004-05-10 10:34:51 +0000576 sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
jplyon672a1ed2003-05-11 20:07:05 +0000577 if( zErrMsg ){
578 fprintf(stderr,"Error: %s\n", zErrMsg);
danielk19776f8a5032004-05-10 10:34:51 +0000579 sqlite3_freemem(zErrMsg);
jplyon6a65bb32003-05-04 07:25:57 +0000580 }
581 }else
582
drh4c653a02000-06-07 01:27:47 +0000583 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
584 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000585 open_db(p);
drh33048c02001-10-01 14:29:22 +0000586 fprintf(p->out, "BEGIN TRANSACTION;\n");
drh4c653a02000-06-07 01:27:47 +0000587 if( nArg==1 ){
danielk19776f8a5032004-05-10 10:34:51 +0000588 sqlite3_exec(p->db,
drha18c5682000-10-08 22:20:57 +0000589 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000590 "WHERE type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000591 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000592 dump_callback, p, &zErrMsg
593 );
drh4c653a02000-06-07 01:27:47 +0000594 }else{
595 int i;
596 for(i=1; i<nArg && zErrMsg==0; i++){
danielk19776f8a5032004-05-10 10:34:51 +0000597 sqlite3_exec_printf(p->db,
drha18c5682000-10-08 22:20:57 +0000598 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000599 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000600 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000601 dump_callback, p, &zErrMsg, azArg[i]
602 );
drh4c653a02000-06-07 01:27:47 +0000603 }
604 }
605 if( zErrMsg ){
606 fprintf(stderr,"Error: %s\n", zErrMsg);
danielk19776f8a5032004-05-10 10:34:51 +0000607 sqlite3_freemem(zErrMsg);
drh33048c02001-10-01 14:29:22 +0000608 }else{
609 fprintf(p->out, "COMMIT;\n");
drh4c653a02000-06-07 01:27:47 +0000610 }
611 }else
drh75897232000-05-29 14:26:00 +0000612
drhdaffd0e2001-04-11 14:28:42 +0000613 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
614 int j;
615 char *z = azArg[1];
616 int val = atoi(azArg[1]);
617 for(j=0; z[j]; j++){
618 if( isupper(z[j]) ) z[j] = tolower(z[j]);
619 }
620 if( strcmp(z,"on")==0 ){
621 val = 1;
622 }else if( strcmp(z,"yes")==0 ){
623 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000624 }
drhdaffd0e2001-04-11 14:28:42 +0000625 p->echoOn = val;
626 }else
627
drh75897232000-05-29 14:26:00 +0000628 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
drh67505e72002-04-19 12:34:06 +0000629 rc = 1;
drh75897232000-05-29 14:26:00 +0000630 }else
631
drhdd45df82002-04-18 12:39:03 +0000632 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +0000633 int j;
drhdd45df82002-04-18 12:39:03 +0000634 char *z = nArg>=2 ? azArg[1] : "1";
635 int val = atoi(z);
persicom7e2dfdd2002-04-18 02:46:52 +0000636 for(j=0; z[j]; j++){
637 if( isupper(z[j]) ) z[j] = tolower(z[j]);
638 }
639 if( strcmp(z,"on")==0 ){
640 val = 1;
641 }else if( strcmp(z,"yes")==0 ){
642 val = 1;
643 }
644 if(val == 1) {
645 if(!p->explainPrev.valid) {
646 p->explainPrev.valid = 1;
647 p->explainPrev.mode = p->mode;
648 p->explainPrev.showHeader = p->showHeader;
649 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
650 }
651 /* We could put this code under the !p->explainValid
652 ** condition so that it does not execute if we are already in
653 ** explain mode. However, always executing it allows us an easy
654 ** was to reset to explain mode in case the user previously
655 ** did an .explain followed by a .width, .mode or .header
656 ** command.
657 */
658 p->mode = MODE_Column;
659 p->showHeader = 1;
660 memset(p->colWidth,0,ArraySize(p->colWidth));
661 p->colWidth[0] = 4;
662 p->colWidth[1] = 12;
663 p->colWidth[2] = 10;
664 p->colWidth[3] = 10;
665 p->colWidth[4] = 35;
666 }else if (p->explainPrev.valid) {
667 p->explainPrev.valid = 0;
668 p->mode = p->explainPrev.mode;
669 p->showHeader = p->explainPrev.showHeader;
670 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
671 }
drh75897232000-05-29 14:26:00 +0000672 }else
673
persicom7e2dfdd2002-04-18 02:46:52 +0000674 if( c=='h' && (strncmp(azArg[0], "header", n)==0
675 ||
676 strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){
drh75897232000-05-29 14:26:00 +0000677 int j;
678 char *z = azArg[1];
679 int val = atoi(azArg[1]);
680 for(j=0; z[j]; j++){
681 if( isupper(z[j]) ) z[j] = tolower(z[j]);
682 }
683 if( strcmp(z,"on")==0 ){
684 val = 1;
685 }else if( strcmp(z,"yes")==0 ){
686 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000687 }
drh75897232000-05-29 14:26:00 +0000688 p->showHeader = val;
689 }else
690
691 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
692 fprintf(stderr,zHelp);
693 }else
694
695 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
696 struct callback_data data;
697 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000698 open_db(p);
drh75897232000-05-29 14:26:00 +0000699 memcpy(&data, p, sizeof(data));
700 data.showHeader = 0;
701 data.mode = MODE_List;
danielk19776f8a5032004-05-10 10:34:51 +0000702 sqlite3_exec_printf(p->db,
drha18c5682000-10-08 22:20:57 +0000703 "SELECT name FROM sqlite_master "
704 "WHERE type='index' AND tbl_name LIKE '%q' "
drhe0bc4042002-06-25 01:09:11 +0000705 "UNION ALL "
706 "SELECT name FROM sqlite_temp_master "
707 "WHERE type='index' AND tbl_name LIKE '%q' "
708 "ORDER BY 1",
709 callback, &data, &zErrMsg, azArg[1], azArg[1]
drha18c5682000-10-08 22:20:57 +0000710 );
drh75897232000-05-29 14:26:00 +0000711 if( zErrMsg ){
712 fprintf(stderr,"Error: %s\n", zErrMsg);
danielk19776f8a5032004-05-10 10:34:51 +0000713 sqlite3_freemem(zErrMsg);
drh75897232000-05-29 14:26:00 +0000714 }
715 }else
716
drh28bd4bc2000-06-15 15:57:22 +0000717 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
drh75897232000-05-29 14:26:00 +0000718 int n2 = strlen(azArg[1]);
persicom7e2dfdd2002-04-18 02:46:52 +0000719 if( strncmp(azArg[1],"line",n2)==0
720 ||
721 strncmp(azArg[1],"lines",n2)==0 ){
drh75897232000-05-29 14:26:00 +0000722 p->mode = MODE_Line;
persicom7e2dfdd2002-04-18 02:46:52 +0000723 }else if( strncmp(azArg[1],"column",n2)==0
724 ||
725 strncmp(azArg[1],"columns",n2)==0 ){
drh75897232000-05-29 14:26:00 +0000726 p->mode = MODE_Column;
727 }else if( strncmp(azArg[1],"list",n2)==0 ){
728 p->mode = MODE_List;
drh1e5d0e92000-05-31 23:33:17 +0000729 }else if( strncmp(azArg[1],"html",n2)==0 ){
730 p->mode = MODE_Html;
drh28bd4bc2000-06-15 15:57:22 +0000731 }else if( strncmp(azArg[1],"insert",n2)==0 ){
732 p->mode = MODE_Insert;
733 if( nArg>=3 ){
drh33048c02001-10-01 14:29:22 +0000734 set_table_name(p, azArg[2]);
drh28bd4bc2000-06-15 15:57:22 +0000735 }else{
drh33048c02001-10-01 14:29:22 +0000736 set_table_name(p, "table");
drh28bd4bc2000-06-15 15:57:22 +0000737 }
drhdaffd0e2001-04-11 14:28:42 +0000738 }else {
739 fprintf(stderr,"mode should be on of: column html insert line list\n");
drh75897232000-05-29 14:26:00 +0000740 }
741 }else
742
persicom7e2dfdd2002-04-18 02:46:52 +0000743 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
744 sprintf(p->nullvalue, "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
745 }else
746
drh75897232000-05-29 14:26:00 +0000747 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
748 if( p->out!=stdout ){
749 fclose(p->out);
750 }
751 if( strcmp(azArg[1],"stdout")==0 ){
752 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +0000753 strcpy(p->outfile,"stdout");
drh75897232000-05-29 14:26:00 +0000754 }else{
drha1f9b5e2004-02-14 16:31:02 +0000755 p->out = fopen(azArg[1], "wb");
drh75897232000-05-29 14:26:00 +0000756 if( p->out==0 ){
757 fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
758 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +0000759 } else {
760 strcpy(p->outfile,azArg[1]);
drh75897232000-05-29 14:26:00 +0000761 }
762 }
763 }else
764
drhdd45df82002-04-18 12:39:03 +0000765 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
persicom7e2dfdd2002-04-18 02:46:52 +0000766 if( nArg >= 2) {
767 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
768 }
769 if( nArg >= 3) {
770 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
771 }
772 }else
773
774 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
drhf73287c2004-02-25 02:25:37 +0000775 rc = 1;
persicom7e2dfdd2002-04-18 02:46:52 +0000776 }else
777
drhdaffd0e2001-04-11 14:28:42 +0000778 if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
drha1f9b5e2004-02-14 16:31:02 +0000779 FILE *alt = fopen(azArg[1], "rb");
drhdaffd0e2001-04-11 14:28:42 +0000780 if( alt==0 ){
781 fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
782 }else{
783 process_input(p, alt);
784 fclose(alt);
785 }
786 }else
787
drh9eb9e262004-02-11 02:18:05 +0000788#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000789 if( c=='r' && strncmp(azArg[0],"rekey", n)==0 && nArg==4 ){
790 char *zOld = p->zKey;
791 if( zOld==0 ) zOld = "";
792 if( strcmp(azArg[1],zOld) ){
793 fprintf(stderr,"old key is incorrect\n");
794 }else if( strcmp(azArg[2], azArg[3]) ){
795 fprintf(stderr,"2nd copy of new key does not match the 1st\n");
796 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000797 sqlite3_freemem(p->zKey);
798 p->zKey = sqlite3_mprintf("%s", azArg[2]);
drh22fbcb82004-02-01 01:22:50 +0000799 sqlite_rekey(p->db, p->zKey, strlen(p->zKey));
800 }
801 }else
802#endif
803
drh75897232000-05-29 14:26:00 +0000804 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
805 struct callback_data data;
806 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000807 open_db(p);
drh75897232000-05-29 14:26:00 +0000808 memcpy(&data, p, sizeof(data));
809 data.showHeader = 0;
drhe3710332000-09-29 13:30:53 +0000810 data.mode = MODE_Semi;
drh75897232000-05-29 14:26:00 +0000811 if( nArg>1 ){
danielk19774adee202004-05-08 08:23:19 +0000812 extern int sqlite3StrICmp(const char*,const char*);
813 if( sqlite3StrICmp(azArg[1],"sqlite_master")==0 ){
drha18c5682000-10-08 22:20:57 +0000814 char *new_argv[2], *new_colv[2];
815 new_argv[0] = "CREATE TABLE sqlite_master (\n"
816 " type text,\n"
817 " name text,\n"
818 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000819 " rootpage integer,\n"
drha18c5682000-10-08 22:20:57 +0000820 " sql text\n"
821 ")";
822 new_argv[1] = 0;
823 new_colv[0] = "sql";
824 new_colv[1] = 0;
825 callback(&data, 1, new_argv, new_colv);
danielk19774adee202004-05-08 08:23:19 +0000826 }else if( sqlite3StrICmp(azArg[1],"sqlite_temp_master")==0 ){
drhe0bc4042002-06-25 01:09:11 +0000827 char *new_argv[2], *new_colv[2];
828 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
829 " type text,\n"
830 " name text,\n"
831 " tbl_name text,\n"
832 " rootpage integer,\n"
833 " sql text\n"
834 ")";
835 new_argv[1] = 0;
836 new_colv[0] = "sql";
837 new_colv[1] = 0;
838 callback(&data, 1, new_argv, new_colv);
drha18c5682000-10-08 22:20:57 +0000839 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000840 sqlite3_exec_printf(p->db,
drhe0bc4042002-06-25 01:09:11 +0000841 "SELECT sql FROM "
842 " (SELECT * FROM sqlite_master UNION ALL"
843 " SELECT * FROM sqlite_temp_master) "
drh33048c02001-10-01 14:29:22 +0000844 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +0000845 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000846 callback, &data, &zErrMsg, azArg[1]);
847 }
drh75897232000-05-29 14:26:00 +0000848 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000849 sqlite3_exec(p->db,
drhe0bc4042002-06-25 01:09:11 +0000850 "SELECT sql FROM "
851 " (SELECT * FROM sqlite_master UNION ALL"
852 " SELECT * FROM sqlite_temp_master) "
drh33048c02001-10-01 14:29:22 +0000853 "WHERE type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +0000854 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000855 callback, &data, &zErrMsg
856 );
drh75897232000-05-29 14:26:00 +0000857 }
drh75897232000-05-29 14:26:00 +0000858 if( zErrMsg ){
859 fprintf(stderr,"Error: %s\n", zErrMsg);
danielk19776f8a5032004-05-10 10:34:51 +0000860 sqlite3_freemem(zErrMsg);
drh75897232000-05-29 14:26:00 +0000861 }
862 }else
863
864 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
865 sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
866 }else
867
persicom7e2dfdd2002-04-18 02:46:52 +0000868 if( c=='s' && strncmp(azArg[0], "show", n)==0){
869 int i;
870 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
drh67505e72002-04-19 12:34:06 +0000871 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
drhdd45df82002-04-18 12:39:03 +0000872 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
persicom7e2dfdd2002-04-18 02:46:52 +0000873 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
874 fprintf(p->out,"%9.9s: %s\n","nullvalue", p->nullvalue);
drh67505e72002-04-19 12:34:06 +0000875 fprintf(p->out,"%9.9s: %s\n","output",
876 strlen(p->outfile) ? p->outfile : "stdout");
persicom7e2dfdd2002-04-18 02:46:52 +0000877 fprintf(p->out,"%9.9s: %s\n","separator", p->separator);
878 fprintf(p->out,"%9.9s: ","width");
879 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
880 fprintf(p->out,"%d ",p->colWidth[i]);
881 }
882 fprintf(p->out,"\n\n");
883 }else
884
drh2dfbbca2000-07-28 14:32:48 +0000885 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
drhe3710332000-09-29 13:30:53 +0000886 char **azResult;
887 int nRow, rc;
888 char *zErrMsg;
drh44c2eb12003-04-30 11:38:26 +0000889 open_db(p);
drha50da102000-08-08 20:19:09 +0000890 if( nArg==1 ){
danielk19776f8a5032004-05-10 10:34:51 +0000891 rc = sqlite3_get_table(p->db,
drha50da102000-08-08 20:19:09 +0000892 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +0000893 "WHERE type IN ('table','view') "
drhe0bc4042002-06-25 01:09:11 +0000894 "UNION ALL "
895 "SELECT name FROM sqlite_temp_master "
896 "WHERE type IN ('table','view') "
897 "ORDER BY 1",
drha18c5682000-10-08 22:20:57 +0000898 &azResult, &nRow, 0, &zErrMsg
899 );
drha50da102000-08-08 20:19:09 +0000900 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000901 rc = sqlite3_get_table_printf(p->db,
drha50da102000-08-08 20:19:09 +0000902 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +0000903 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
drhe0bc4042002-06-25 01:09:11 +0000904 "UNION ALL "
905 "SELECT name FROM sqlite_temp_master "
906 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
907 "ORDER BY 1",
908 &azResult, &nRow, 0, &zErrMsg, azArg[1], azArg[1]
drha18c5682000-10-08 22:20:57 +0000909 );
drha50da102000-08-08 20:19:09 +0000910 }
drh75897232000-05-29 14:26:00 +0000911 if( zErrMsg ){
912 fprintf(stderr,"Error: %s\n", zErrMsg);
danielk19776f8a5032004-05-10 10:34:51 +0000913 sqlite3_freemem(zErrMsg);
drh75897232000-05-29 14:26:00 +0000914 }
drhe3710332000-09-29 13:30:53 +0000915 if( rc==SQLITE_OK ){
916 int len, maxlen = 0;
917 int i, j;
918 int nPrintCol, nPrintRow;
919 for(i=1; i<=nRow; i++){
920 if( azResult[i]==0 ) continue;
921 len = strlen(azResult[i]);
922 if( len>maxlen ) maxlen = len;
923 }
924 nPrintCol = 80/(maxlen+2);
925 if( nPrintCol<1 ) nPrintCol = 1;
926 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
927 for(i=0; i<nPrintRow; i++){
928 for(j=i+1; j<=nRow; j+=nPrintRow){
929 char *zSp = j<=nPrintRow ? "" : " ";
930 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
931 }
932 printf("\n");
933 }
934 }
danielk19776f8a5032004-05-10 10:34:51 +0000935 sqlite3_free_table(azResult);
drh75897232000-05-29 14:26:00 +0000936 }else
937
drh2dfbbca2000-07-28 14:32:48 +0000938 if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
drh44c2eb12003-04-30 11:38:26 +0000939 open_db(p);
danielk19776f8a5032004-05-10 10:34:51 +0000940 sqlite3_busy_timeout(p->db, atoi(azArg[1]));
drh2dfbbca2000-07-28 14:32:48 +0000941 }else
942
drh75897232000-05-29 14:26:00 +0000943 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
944 int j;
945 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
946 p->colWidth[j-1] = atoi(azArg[j]);
947 }
948 }else
949
950 {
drh67505e72002-04-19 12:34:06 +0000951 fprintf(stderr, "unknown command or invalid arguments: "
952 " \"%s\". Enter \".help\" for help\n", azArg[0]);
drh75897232000-05-29 14:26:00 +0000953 }
drh67505e72002-04-19 12:34:06 +0000954
955 return rc;
drh75897232000-05-29 14:26:00 +0000956}
957
drh67505e72002-04-19 12:34:06 +0000958/*
drh324ccef2003-02-05 14:06:20 +0000959** Return TRUE if the last non-whitespace character in z[] is a semicolon.
960** z[] is N characters long.
961*/
962static int _ends_with_semicolon(const char *z, int N){
963 while( N>0 && isspace(z[N-1]) ){ N--; }
964 return N>0 && z[N-1]==';';
965}
966
967/*
drh70c7a4b2003-04-26 03:03:06 +0000968** Test to see if a line consists entirely of whitespace.
969*/
970static int _all_whitespace(const char *z){
971 for(; *z; z++){
972 if( isspace(*z) ) continue;
973 if( *z=='/' && z[1]=='*' ){
974 z += 2;
975 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
976 if( *z==0 ) return 0;
977 z++;
978 continue;
979 }
980 if( *z=='-' && z[1]=='-' ){
981 z += 2;
982 while( *z && *z!='\n' ){ z++; }
983 if( *z==0 ) return 1;
984 continue;
985 }
986 return 0;
987 }
988 return 1;
989}
990
991/*
drha9b17162003-04-29 18:01:28 +0000992** Return TRUE if the line typed in is an SQL command terminator other
993** than a semi-colon. The SQL Server style "go" command is understood
994** as is the Oracle "/".
995*/
996static int _is_command_terminator(const char *zLine){
danielk19774adee202004-05-08 08:23:19 +0000997 extern int sqlite3StrNICmp(const char*,const char*,int);
drha9b17162003-04-29 18:01:28 +0000998 while( isspace(*zLine) ){ zLine++; };
999 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ) return 1; /* Oracle */
danielk19774adee202004-05-08 08:23:19 +00001000 if( sqlite3StrNICmp(zLine,"go",2)==0 && _all_whitespace(&zLine[2]) ){
drha9b17162003-04-29 18:01:28 +00001001 return 1; /* SQL Server */
1002 }
1003 return 0;
1004}
1005
1006/*
drh67505e72002-04-19 12:34:06 +00001007** Read input from *in and process it. If *in==0 then input
1008** is interactive - the user is typing it it. Otherwise, input
1009** is coming from a file or device. A prompt is issued and history
1010** is saved only if input is interactive. An interrupt signal will
1011** cause this routine to exit immediately, unless input is interactive.
1012*/
drhdaffd0e2001-04-11 14:28:42 +00001013static void process_input(struct callback_data *p, FILE *in){
1014 char *zLine;
1015 char *zSql = 0;
1016 int nSql = 0;
1017 char *zErrMsg;
drh7f953e22002-07-13 17:33:45 +00001018 int rc;
drh41e941d2002-04-04 15:10:12 +00001019 while( fflush(p->out), (zLine = one_input_line(zSql, in))!=0 ){
drh67505e72002-04-19 12:34:06 +00001020 if( seenInterrupt ){
1021 if( in!=0 ) break;
1022 seenInterrupt = 0;
1023 }
drhdaffd0e2001-04-11 14:28:42 +00001024 if( p->echoOn ) printf("%s\n", zLine);
drhf817b6b2003-06-16 00:16:41 +00001025 if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
drh2af0b2d2002-02-21 02:25:02 +00001026 if( zLine && zLine[0]=='.' && nSql==0 ){
drh44c2eb12003-04-30 11:38:26 +00001027 int rc = do_meta_command(zLine, p);
drhdaffd0e2001-04-11 14:28:42 +00001028 free(zLine);
drh67505e72002-04-19 12:34:06 +00001029 if( rc ) break;
drhdaffd0e2001-04-11 14:28:42 +00001030 continue;
1031 }
drha9b17162003-04-29 18:01:28 +00001032 if( _is_command_terminator(zLine) ){
1033 strcpy(zLine,";");
1034 }
drhdaffd0e2001-04-11 14:28:42 +00001035 if( zSql==0 ){
1036 int i;
1037 for(i=0; zLine[i] && isspace(zLine[i]); i++){}
1038 if( zLine[i]!=0 ){
1039 nSql = strlen(zLine);
1040 zSql = malloc( nSql+1 );
1041 strcpy(zSql, zLine);
1042 }
1043 }else{
1044 int len = strlen(zLine);
1045 zSql = realloc( zSql, nSql + len + 2 );
1046 if( zSql==0 ){
1047 fprintf(stderr,"%s: out of memory!\n", Argv0);
1048 exit(1);
1049 }
1050 strcpy(&zSql[nSql++], "\n");
1051 strcpy(&zSql[nSql], zLine);
1052 nSql += len;
1053 }
1054 free(zLine);
danielk19776f8a5032004-05-10 10:34:51 +00001055 if( zSql && _ends_with_semicolon(zSql, nSql) && sqlite3_complete(zSql) ){
drhdaffd0e2001-04-11 14:28:42 +00001056 p->cnt = 0;
drh44c2eb12003-04-30 11:38:26 +00001057 open_db(p);
danielk19776f8a5032004-05-10 10:34:51 +00001058 rc = sqlite3_exec(p->db, zSql, callback, p, &zErrMsg);
drh7f953e22002-07-13 17:33:45 +00001059 if( rc || zErrMsg ){
drhdaffd0e2001-04-11 14:28:42 +00001060 if( in!=0 && !p->echoOn ) printf("%s\n",zSql);
drh7f953e22002-07-13 17:33:45 +00001061 if( zErrMsg!=0 ){
1062 printf("SQL error: %s\n", zErrMsg);
danielk19776f8a5032004-05-10 10:34:51 +00001063 sqlite3_freemem(zErrMsg);
drh7f953e22002-07-13 17:33:45 +00001064 zErrMsg = 0;
1065 }else{
danielk19776f8a5032004-05-10 10:34:51 +00001066 printf("SQL error: %s\n", sqlite3_error_string(rc));
drh7f953e22002-07-13 17:33:45 +00001067 }
drhdaffd0e2001-04-11 14:28:42 +00001068 }
1069 free(zSql);
1070 zSql = 0;
1071 nSql = 0;
1072 }
1073 }
1074 if( zSql ){
drh70c7a4b2003-04-26 03:03:06 +00001075 if( !_all_whitespace(zSql) ) printf("Incomplete SQL: %s\n", zSql);
drhdaffd0e2001-04-11 14:28:42 +00001076 free(zSql);
1077 }
1078}
1079
drh67505e72002-04-19 12:34:06 +00001080/*
1081** Return a pathname which is the user's home directory. A
1082** 0 return indicates an error of some kind. Space to hold the
1083** resulting string is obtained from malloc(). The calling
1084** function should free the result.
1085*/
1086static char *find_home_dir(void){
1087 char *home_dir = NULL;
persicom7e2dfdd2002-04-18 02:46:52 +00001088
drh820f3812003-01-08 13:02:52 +00001089#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
drh67505e72002-04-19 12:34:06 +00001090 struct passwd *pwent;
1091 uid_t uid = getuid();
drhbd842ba2002-08-21 11:26:41 +00001092 if( (pwent=getpwuid(uid)) != NULL) {
1093 home_dir = pwent->pw_dir;
drh67505e72002-04-19 12:34:06 +00001094 }
1095#endif
1096
drh820f3812003-01-08 13:02:52 +00001097#ifdef __MACOS__
1098 char home_path[_MAX_PATH+1];
1099 home_dir = getcwd(home_path, _MAX_PATH);
1100#endif
1101
drh67505e72002-04-19 12:34:06 +00001102 if (!home_dir) {
1103 home_dir = getenv("HOME");
1104 if (!home_dir) {
1105 home_dir = getenv("HOMEPATH"); /* Windows? */
1106 }
1107 }
1108
drhe98d4fa2002-04-21 19:06:22 +00001109#if defined(_WIN32) || defined(WIN32)
1110 if (!home_dir) {
1111 home_dir = "c:";
1112 }
1113#endif
1114
drh67505e72002-04-19 12:34:06 +00001115 if( home_dir ){
1116 char *z = malloc( strlen(home_dir)+1 );
1117 if( z ) strcpy(z, home_dir);
1118 home_dir = z;
1119 }
drhe98d4fa2002-04-21 19:06:22 +00001120
drh67505e72002-04-19 12:34:06 +00001121 return home_dir;
1122}
1123
1124/*
1125** Read input from the file given by sqliterc_override. Or if that
1126** parameter is NULL, take input from ~/.sqliterc
1127*/
drh22fbcb82004-02-01 01:22:50 +00001128static void process_sqliterc(
1129 struct callback_data *p, /* Configuration data */
1130 const char *sqliterc_override /* Name of config file. NULL to use default */
1131){
persicom7e2dfdd2002-04-18 02:46:52 +00001132 char *home_dir = NULL;
drh22fbcb82004-02-01 01:22:50 +00001133 const char *sqliterc = sqliterc_override;
1134 char *zBuf;
persicom7e2dfdd2002-04-18 02:46:52 +00001135 FILE *in = NULL;
1136
1137 if (sqliterc == NULL) {
drh67505e72002-04-19 12:34:06 +00001138 home_dir = find_home_dir();
drhe98d4fa2002-04-21 19:06:22 +00001139 if( home_dir==0 ){
1140 fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0);
1141 return;
1142 }
drh22fbcb82004-02-01 01:22:50 +00001143 zBuf = malloc(strlen(home_dir) + 15);
1144 if( zBuf==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +00001145 fprintf(stderr,"%s: out of memory!\n", Argv0);
1146 exit(1);
1147 }
drh22fbcb82004-02-01 01:22:50 +00001148 sprintf(zBuf,"%s/.sqliterc",home_dir);
drh67505e72002-04-19 12:34:06 +00001149 free(home_dir);
drh22fbcb82004-02-01 01:22:50 +00001150 sqliterc = (const char*)zBuf;
persicom7e2dfdd2002-04-18 02:46:52 +00001151 }
drha1f9b5e2004-02-14 16:31:02 +00001152 in = fopen(sqliterc,"rb");
drh22fbcb82004-02-01 01:22:50 +00001153 if( in ){
1154 if( isatty(fileno(stdout)) ){
1155 printf("Loading resources from %s\n",sqliterc);
1156 }
persicom7e2dfdd2002-04-18 02:46:52 +00001157 process_input(p,in);
drhdd45df82002-04-18 12:39:03 +00001158 fclose(in);
persicom7e2dfdd2002-04-18 02:46:52 +00001159 }
1160 return;
1161}
1162
drh67505e72002-04-19 12:34:06 +00001163/*
drhe1e38c42003-05-04 18:30:59 +00001164** Show available command line options
1165*/
1166static const char zOptions[] =
1167 " -init filename read/process named file\n"
1168 " -echo print commands before execution\n"
1169 " -[no]header turn headers on or off\n"
1170 " -column set output mode to 'column'\n"
1171 " -html set output mode to HTML\n"
drh9eb9e262004-02-11 02:18:05 +00001172#ifdef SQLITE_HAS_CODEC
drh57ced912004-02-10 02:57:59 +00001173 " -key KEY encryption key\n"
1174#endif
drhe1e38c42003-05-04 18:30:59 +00001175 " -line set output mode to 'line'\n"
1176 " -list set output mode to 'list'\n"
1177 " -separator 'x' set output field separator (|)\n"
1178 " -nullvalue 'text' set text string for NULL values\n"
1179 " -version show SQLite version\n"
1180 " -help show this text, also show dot-commands\n"
1181;
1182static void usage(int showDetail){
1183 fprintf(stderr, "Usage: %s [OPTIONS] FILENAME [SQL]\n", Argv0);
1184 if( showDetail ){
1185 fprintf(stderr, "Options are:\n%s", zOptions);
1186 }else{
1187 fprintf(stderr, "Use the -help option for additional information\n");
1188 }
1189 exit(1);
1190}
1191
1192/*
drh67505e72002-04-19 12:34:06 +00001193** Initialize the state information in data
1194*/
persicom7e2dfdd2002-04-18 02:46:52 +00001195void main_init(struct callback_data *data) {
1196 memset(data, 0, sizeof(*data));
1197 data->mode = MODE_List;
1198 strcpy(data->separator,"|");
1199 data->showHeader = 0;
1200 strcpy(mainPrompt,"sqlite> ");
1201 strcpy(continuePrompt," ...> ");
1202}
1203
drh75897232000-05-29 14:26:00 +00001204int main(int argc, char **argv){
drh75897232000-05-29 14:26:00 +00001205 char *zErrMsg = 0;
1206 struct callback_data data;
drh22fbcb82004-02-01 01:22:50 +00001207 const char *zInitFile = 0;
1208 char *zFirstCmd = 0;
drh44c2eb12003-04-30 11:38:26 +00001209 int i;
danielk19774adee202004-05-08 08:23:19 +00001210 extern int sqlite3OsFileExists(const char*);
drh75897232000-05-29 14:26:00 +00001211
drh820f3812003-01-08 13:02:52 +00001212#ifdef __MACOS__
1213 argc = ccommand(&argv);
drh820f3812003-01-08 13:02:52 +00001214#endif
1215
drhdaffd0e2001-04-11 14:28:42 +00001216 Argv0 = argv[0];
persicom7e2dfdd2002-04-18 02:46:52 +00001217 main_init(&data);
persicom7e2dfdd2002-04-18 02:46:52 +00001218
drh44c2eb12003-04-30 11:38:26 +00001219 /* Make sure we have a valid signal handler early, before anything
1220 ** else is done.
1221 */
drh4c504392000-10-16 22:06:40 +00001222#ifdef SIGINT
1223 signal(SIGINT, interrupt_handler);
1224#endif
drh44c2eb12003-04-30 11:38:26 +00001225
drh22fbcb82004-02-01 01:22:50 +00001226 /* Do an initial pass through the command-line argument to locate
1227 ** the name of the database file, the name of the initialization file,
1228 ** and the first command to execute.
drh44c2eb12003-04-30 11:38:26 +00001229 */
drh22fbcb82004-02-01 01:22:50 +00001230 for(i=1; i<argc-1; i++){
drh44c2eb12003-04-30 11:38:26 +00001231 if( argv[i][0]!='-' ) break;
1232 if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){
1233 i++;
drh22fbcb82004-02-01 01:22:50 +00001234 }else if( strcmp(argv[i],"-init")==0 ){
1235 i++;
1236 zInitFile = argv[i];
1237 }else if( strcmp(argv[i],"-key")==0 ){
1238 i++;
danielk19776f8a5032004-05-10 10:34:51 +00001239 data.zKey = sqlite3_mprintf("%s",argv[i]);
drh44c2eb12003-04-30 11:38:26 +00001240 }
1241 }
drh22fbcb82004-02-01 01:22:50 +00001242 if( i<argc ){
1243 data.zDbFilename = argv[i++];
1244 }else{
1245 data.zDbFilename = ":memory:";
1246 }
1247 if( i<argc ){
1248 zFirstCmd = argv[i++];
1249 }
drh44c2eb12003-04-30 11:38:26 +00001250 data.out = stdout;
1251
1252 /* Go ahead and open the database file if it already exists. If the
1253 ** file does not exist, delay opening it. This prevents empty database
1254 ** files from being created if a user mistypes the database name argument
1255 ** to the sqlite command-line tool.
1256 */
danielk19774adee202004-05-08 08:23:19 +00001257 if( sqlite3OsFileExists(data.zDbFilename) ){
drh44c2eb12003-04-30 11:38:26 +00001258 open_db(&data);
1259 }
1260
drh22fbcb82004-02-01 01:22:50 +00001261 /* Process the initialization file if there is one. If no -init option
1262 ** is given on the command line, look for a file named ~/.sqliterc and
1263 ** try to process it.
drh44c2eb12003-04-30 11:38:26 +00001264 */
drh22fbcb82004-02-01 01:22:50 +00001265 process_sqliterc(&data,zInitFile);
drh44c2eb12003-04-30 11:38:26 +00001266
drh22fbcb82004-02-01 01:22:50 +00001267 /* Make a second pass through the command-line argument and set
1268 ** options. This second pass is delayed until after the initialization
1269 ** file is processed so that the command-line arguments will override
1270 ** settings in the initialization file.
drh44c2eb12003-04-30 11:38:26 +00001271 */
drh22fbcb82004-02-01 01:22:50 +00001272 for(i=1; i<argc && argv[i][0]=='-'; i++){
1273 char *z = argv[i];
1274 if( strcmp(z,"-init")==0 || strcmp(z,"-key")==0 ){
1275 i++;
1276 }else if( strcmp(z,"-html")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001277 data.mode = MODE_Html;
drh22fbcb82004-02-01 01:22:50 +00001278 }else if( strcmp(z,"-list")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001279 data.mode = MODE_List;
drh22fbcb82004-02-01 01:22:50 +00001280 }else if( strcmp(z,"-line")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001281 data.mode = MODE_Line;
drh22fbcb82004-02-01 01:22:50 +00001282 }else if( strcmp(z,"-column")==0 ){
drh8b32e172002-04-08 02:42:57 +00001283 data.mode = MODE_Column;
drh22fbcb82004-02-01 01:22:50 +00001284 }else if( strcmp(z,"-separator")==0 ){
1285 i++;
1286 sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[i]);
1287 }else if( strcmp(z,"-nullvalue")==0 ){
1288 i++;
1289 sprintf(data.nullvalue,"%.*s",(int)sizeof(data.nullvalue)-1,argv[i]);
1290 }else if( strcmp(z,"-header")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001291 data.showHeader = 1;
drh22fbcb82004-02-01 01:22:50 +00001292 }else if( strcmp(z,"-noheader")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001293 data.showHeader = 0;
drh22fbcb82004-02-01 01:22:50 +00001294 }else if( strcmp(z,"-echo")==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001295 data.echoOn = 1;
drh22fbcb82004-02-01 01:22:50 +00001296 }else if( strcmp(z,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001297 printf("%s\n", sqlite3_version);
drhe1e38c42003-05-04 18:30:59 +00001298 return 1;
drh22fbcb82004-02-01 01:22:50 +00001299 }else if( strcmp(z,"-help")==0 ){
drhe1e38c42003-05-04 18:30:59 +00001300 usage(1);
drh1e5d0e92000-05-31 23:33:17 +00001301 }else{
drh22fbcb82004-02-01 01:22:50 +00001302 fprintf(stderr,"%s: unknown option: %s\n", Argv0, z);
drhe1e38c42003-05-04 18:30:59 +00001303 fprintf(stderr,"Use -help for a list of options.\n");
drh1e5d0e92000-05-31 23:33:17 +00001304 return 1;
1305 }
1306 }
drh44c2eb12003-04-30 11:38:26 +00001307
drh22fbcb82004-02-01 01:22:50 +00001308 if( zFirstCmd ){
drh44c2eb12003-04-30 11:38:26 +00001309 /* Run just the command that follows the database name
1310 */
drh22fbcb82004-02-01 01:22:50 +00001311 if( zFirstCmd[0]=='.' ){
1312 do_meta_command(zFirstCmd, &data);
drh6ff13852001-11-25 13:18:23 +00001313 exit(0);
1314 }else{
1315 int rc;
drh44c2eb12003-04-30 11:38:26 +00001316 open_db(&data);
danielk19776f8a5032004-05-10 10:34:51 +00001317 rc = sqlite3_exec(data.db, zFirstCmd, callback, &data, &zErrMsg);
drh6ff13852001-11-25 13:18:23 +00001318 if( rc!=0 && zErrMsg!=0 ){
1319 fprintf(stderr,"SQL error: %s\n", zErrMsg);
1320 exit(1);
1321 }
drh75897232000-05-29 14:26:00 +00001322 }
1323 }else{
drh44c2eb12003-04-30 11:38:26 +00001324 /* Run commands received from standard input
1325 */
drh5cf590c2003-04-24 01:45:04 +00001326 if( isatty(fileno(stdout)) && isatty(fileno(stdin)) ){
drh67505e72002-04-19 12:34:06 +00001327 char *zHome;
1328 char *zHistory = 0;
drh75897232000-05-29 14:26:00 +00001329 printf(
drhb217a572000-08-22 13:40:18 +00001330 "SQLite version %s\n"
1331 "Enter \".help\" for instructions\n",
danielk19776f8a5032004-05-10 10:34:51 +00001332 sqlite3_version
drh75897232000-05-29 14:26:00 +00001333 );
drh67505e72002-04-19 12:34:06 +00001334 zHome = find_home_dir();
1335 if( zHome && (zHistory = malloc(strlen(zHome)+20))!=0 ){
1336 sprintf(zHistory,"%s/.sqlite_history", zHome);
1337 }
1338 if( zHistory ) read_history(zHistory);
drhdaffd0e2001-04-11 14:28:42 +00001339 process_input(&data, 0);
drh67505e72002-04-19 12:34:06 +00001340 if( zHistory ){
1341 stifle_history(100);
1342 write_history(zHistory);
1343 }
drhdaffd0e2001-04-11 14:28:42 +00001344 }else{
1345 process_input(&data, stdin);
drh75897232000-05-29 14:26:00 +00001346 }
1347 }
drh33048c02001-10-01 14:29:22 +00001348 set_table_name(&data, 0);
danielk19776f8a5032004-05-10 10:34:51 +00001349 if( db ) sqlite3_close(db);
drh75897232000-05-29 14:26:00 +00001350 return 0;
1351}