blob: 953a994ba1a6be793de5a027528778bee294e93f [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**
danielk1977bc6ada42004-06-30 08:20:16 +000015** $Id: shell.c,v 1.106 2004/06/30 08:20:16 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*/
danielk197792f9a1b2004-06-19 09:08:16 +000060static sqlite3 *db = 0;
drh4c504392000-10-16 22:06:40 +000061
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/*
danielk1977bc6ada42004-06-30 08:20:16 +000087** A global char* and an SQL function to access its current value
88** from within an SQL statement. This program used to use the
89** sqlite_exec_printf() API to substitue a string into an SQL statement.
90** The correct way to do this with sqlite3 is to use the bind API, but
91** since the shell is built around the callback paradigm it would be a lot
92** of work. Instead just use this hack, which is quite harmless.
93*/
94static const char *zShellStatic = 0;
95static void shellstaticFunc(
96 sqlite3_context *context,
97 int argc,
98 sqlite3_value **argv
99){
100 assert( 0==argc );
101 assert( zShellStatic );
102 sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC);
103}
104
105
106/*
drh8e7e7a22000-05-30 18:45:23 +0000107** This routine reads a line of text from standard input, stores
108** the text in memory obtained from malloc() and returns a pointer
109** to the text. NULL is returned at end of file, or if malloc()
110** fails.
111**
112** The interface is like "readline" but no command-line editing
113** is done.
114*/
drh9347b202003-07-18 01:30:59 +0000115static char *local_getline(char *zPrompt, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +0000116 char *zLine;
117 int nLine;
drh8e7e7a22000-05-30 18:45:23 +0000118 int n;
119 int eol;
120
121 if( zPrompt && *zPrompt ){
122 printf("%s",zPrompt);
123 fflush(stdout);
124 }
125 nLine = 100;
126 zLine = malloc( nLine );
127 if( zLine==0 ) return 0;
128 n = 0;
129 eol = 0;
130 while( !eol ){
131 if( n+100>nLine ){
132 nLine = nLine*2 + 100;
133 zLine = realloc(zLine, nLine);
134 if( zLine==0 ) return 0;
135 }
drhdaffd0e2001-04-11 14:28:42 +0000136 if( fgets(&zLine[n], nLine - n, in)==0 ){
drh8e7e7a22000-05-30 18:45:23 +0000137 if( n==0 ){
138 free(zLine);
139 return 0;
140 }
141 zLine[n] = 0;
142 eol = 1;
143 break;
144 }
145 while( zLine[n] ){ n++; }
146 if( n>0 && zLine[n-1]=='\n' ){
147 n--;
148 zLine[n] = 0;
149 eol = 1;
150 }
151 }
152 zLine = realloc( zLine, n+1 );
153 return zLine;
154}
155
156/*
157** Retrieve a single line of input text. "isatty" is true if text
158** is coming from a terminal. In that case, we issue a prompt and
159** attempt to use "readline" for command-line editing. If "isatty"
drh9347b202003-07-18 01:30:59 +0000160** is false, use "local_getline" instead of "readline" and issue no prompt.
drh8e7e7a22000-05-30 18:45:23 +0000161**
162** zPrior is a string of prior text retrieved. If not the empty
163** string, then issue a continuation prompt.
164*/
drhdaffd0e2001-04-11 14:28:42 +0000165static char *one_input_line(const char *zPrior, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +0000166 char *zPrompt;
167 char *zResult;
drhdaffd0e2001-04-11 14:28:42 +0000168 if( in!=0 ){
drh9347b202003-07-18 01:30:59 +0000169 return local_getline(0, in);
drh8e7e7a22000-05-30 18:45:23 +0000170 }
171 if( zPrior && zPrior[0] ){
persicom7e2dfdd2002-04-18 02:46:52 +0000172 zPrompt = continuePrompt;
drh8e7e7a22000-05-30 18:45:23 +0000173 }else{
persicom7e2dfdd2002-04-18 02:46:52 +0000174 zPrompt = mainPrompt;
drh8e7e7a22000-05-30 18:45:23 +0000175 }
176 zResult = readline(zPrompt);
drh2dfbbca2000-07-28 14:32:48 +0000177 if( zResult ) add_history(zResult);
drh8e7e7a22000-05-30 18:45:23 +0000178 return zResult;
179}
180
persicom7e2dfdd2002-04-18 02:46:52 +0000181struct previous_mode_data {
182 int valid; /* Is there legit data in here? */
183 int mode;
184 int showHeader;
185 int colWidth[100];
186};
drh8e7e7a22000-05-30 18:45:23 +0000187/*
drh75897232000-05-29 14:26:00 +0000188** An pointer to an instance of this structure is passed from
189** the main program to the callback. This is used to communicate
190** state and mode information.
191*/
192struct callback_data {
danielk197792f9a1b2004-06-19 09:08:16 +0000193 sqlite3 *db; /* The database */
drhdaffd0e2001-04-11 14:28:42 +0000194 int echoOn; /* True to echo input commands */
drh28bd4bc2000-06-15 15:57:22 +0000195 int cnt; /* Number of records displayed so far */
196 FILE *out; /* Write results here */
197 int mode; /* An output mode setting */
198 int showHeader; /* True to show column names in List or Column mode */
drh33048c02001-10-01 14:29:22 +0000199 char *zDestTable; /* Name of destination table when MODE_Insert */
drh28bd4bc2000-06-15 15:57:22 +0000200 char separator[20]; /* Separator character for MODE_List */
drha0c66f52000-07-29 13:20:21 +0000201 int colWidth[100]; /* Requested width of each column when in column mode*/
202 int actualWidth[100]; /* Actual width of each column */
drh83965662003-04-17 02:54:13 +0000203 char nullvalue[20]; /* The text to print when a NULL comes back from
204 ** the database */
persicom7e2dfdd2002-04-18 02:46:52 +0000205 struct previous_mode_data explainPrev;
drh83965662003-04-17 02:54:13 +0000206 /* Holds the mode information just before
207 ** .explain ON */
drh44c2eb12003-04-30 11:38:26 +0000208 char outfile[FILENAME_MAX]; /* Filename for *out */
209 const char *zDbFilename; /* name of the database file */
drh22fbcb82004-02-01 01:22:50 +0000210 char *zKey; /* Encryption key */
drh75897232000-05-29 14:26:00 +0000211};
212
213/*
214** These are the allowed modes.
215*/
drh967e8b72000-06-21 13:59:10 +0000216#define MODE_Line 0 /* One column per line. Blank line between records */
drh75897232000-05-29 14:26:00 +0000217#define MODE_Column 1 /* One record per line in neat columns */
218#define MODE_List 2 /* One record per line with a separator */
drhe3710332000-09-29 13:30:53 +0000219#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
220#define MODE_Html 4 /* Generate an XHTML table */
221#define MODE_Insert 5 /* Generate SQL "insert" statements */
jplyon672a1ed2003-05-11 20:07:05 +0000222#define MODE_NUM_OF 6 /* The number of modes (not a mode itself) */
persicom7e2dfdd2002-04-18 02:46:52 +0000223
224char *modeDescr[MODE_NUM_OF] = {
225 "line",
226 "column",
227 "list",
228 "semi",
229 "html",
230 "insert"
231};
drh75897232000-05-29 14:26:00 +0000232
233/*
234** Number of elements in an array
235*/
236#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
237
238/*
drh28bd4bc2000-06-15 15:57:22 +0000239** Output the given string as a quoted string using SQL quoting conventions.
240*/
241static void output_quoted_string(FILE *out, const char *z){
242 int i;
243 int nSingle = 0;
drh28bd4bc2000-06-15 15:57:22 +0000244 for(i=0; z[i]; i++){
245 if( z[i]=='\'' ) nSingle++;
drh28bd4bc2000-06-15 15:57:22 +0000246 }
247 if( nSingle==0 ){
248 fprintf(out,"'%s'",z);
drh28bd4bc2000-06-15 15:57:22 +0000249 }else{
250 fprintf(out,"'");
251 while( *z ){
252 for(i=0; z[i] && z[i]!='\''; i++){}
253 if( i==0 ){
254 fprintf(out,"''");
255 z++;
256 }else if( z[i]=='\'' ){
257 fprintf(out,"%.*s''",i,z);
258 z += i+1;
259 }else{
drhcd7d2732002-02-26 23:24:26 +0000260 fprintf(out,"%s",z);
drh28bd4bc2000-06-15 15:57:22 +0000261 break;
262 }
263 }
drhcd7d2732002-02-26 23:24:26 +0000264 fprintf(out,"'");
drh28bd4bc2000-06-15 15:57:22 +0000265 }
266}
267
268/*
drhc08a4f12000-06-15 16:49:48 +0000269** Output the given string with characters that are special to
270** HTML escaped.
271*/
272static void output_html_string(FILE *out, const char *z){
273 int i;
274 while( *z ){
275 for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){}
276 if( i>0 ){
277 fprintf(out,"%.*s",i,z);
278 }
279 if( z[i]=='<' ){
280 fprintf(out,"&lt;");
281 }else if( z[i]=='&' ){
282 fprintf(out,"&amp;");
283 }else{
284 break;
285 }
286 z += i + 1;
287 }
288}
289
290/*
drh4c504392000-10-16 22:06:40 +0000291** This routine runs when the user presses Ctrl-C
292*/
293static void interrupt_handler(int NotUsed){
drh67505e72002-04-19 12:34:06 +0000294 seenInterrupt = 1;
danielk19776f8a5032004-05-10 10:34:51 +0000295 if( db ) sqlite3_interrupt(db);
drh4c504392000-10-16 22:06:40 +0000296}
297
298/*
drh75897232000-05-29 14:26:00 +0000299** This is the callback routine that the SQLite library
300** invokes for each row of a query result.
301*/
302static int callback(void *pArg, int nArg, char **azArg, char **azCol){
303 int i;
304 struct callback_data *p = (struct callback_data*)pArg;
305 switch( p->mode ){
306 case MODE_Line: {
drhe3710332000-09-29 13:30:53 +0000307 int w = 5;
drh6a535342001-10-19 16:44:56 +0000308 if( azArg==0 ) break;
drhe3710332000-09-29 13:30:53 +0000309 for(i=0; i<nArg; i++){
310 int len = strlen(azCol[i]);
311 if( len>w ) w = len;
312 }
drh75897232000-05-29 14:26:00 +0000313 if( p->cnt++>0 ) fprintf(p->out,"\n");
314 for(i=0; i<nArg; i++){
drha69d9162003-04-17 22:57:53 +0000315 fprintf(p->out,"%*s = %s\n", w, azCol[i],
316 azArg[i] ? azArg[i] : p->nullvalue);
drh75897232000-05-29 14:26:00 +0000317 }
318 break;
319 }
320 case MODE_Column: {
drha0c66f52000-07-29 13:20:21 +0000321 if( p->cnt++==0 ){
drh75897232000-05-29 14:26:00 +0000322 for(i=0; i<nArg; i++){
drha0c66f52000-07-29 13:20:21 +0000323 int w, n;
324 if( i<ArraySize(p->colWidth) ){
drh75897232000-05-29 14:26:00 +0000325 w = p->colWidth[i];
326 }else{
drha0c66f52000-07-29 13:20:21 +0000327 w = 0;
drh75897232000-05-29 14:26:00 +0000328 }
drha0c66f52000-07-29 13:20:21 +0000329 if( w<=0 ){
drhff6e9112000-08-28 16:21:58 +0000330 w = strlen(azCol[i] ? azCol[i] : "");
drha0c66f52000-07-29 13:20:21 +0000331 if( w<10 ) w = 10;
persicom7e2dfdd2002-04-18 02:46:52 +0000332 n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue);
drha0c66f52000-07-29 13:20:21 +0000333 if( w<n ) w = n;
334 }
335 if( i<ArraySize(p->actualWidth) ){
persicom1d0b8722002-04-18 02:53:04 +0000336 p->actualWidth[i] = w;
drha0c66f52000-07-29 13:20:21 +0000337 }
338 if( p->showHeader ){
339 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
340 }
341 }
342 if( p->showHeader ){
343 for(i=0; i<nArg; i++){
344 int w;
345 if( i<ArraySize(p->actualWidth) ){
346 w = p->actualWidth[i];
347 }else{
348 w = 10;
349 }
350 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
351 "----------------------------------------------------------",
352 i==nArg-1 ? "\n": " ");
353 }
drh75897232000-05-29 14:26:00 +0000354 }
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++){
358 int w;
drha0c66f52000-07-29 13:20:21 +0000359 if( i<ArraySize(p->actualWidth) ){
360 w = p->actualWidth[i];
drh75897232000-05-29 14:26:00 +0000361 }else{
362 w = 10;
363 }
drhc61053b2000-06-04 12:58:36 +0000364 fprintf(p->out,"%-*.*s%s",w,w,
persicom7e2dfdd2002-04-18 02:46:52 +0000365 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
drh75897232000-05-29 14:26:00 +0000366 }
367 break;
368 }
drhe3710332000-09-29 13:30:53 +0000369 case MODE_Semi:
drh75897232000-05-29 14:26:00 +0000370 case MODE_List: {
371 if( p->cnt++==0 && p->showHeader ){
372 for(i=0; i<nArg; i++){
373 fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
374 }
375 }
drh6a535342001-10-19 16:44:56 +0000376 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000377 for(i=0; i<nArg; i++){
drh4c653a02000-06-07 01:27:47 +0000378 char *z = azArg[i];
persicom7e2dfdd2002-04-18 02:46:52 +0000379 if( z==0 ) z = p->nullvalue;
drh71172c52002-01-24 00:00:21 +0000380 fprintf(p->out, "%s", z);
drhe3710332000-09-29 13:30:53 +0000381 if( i<nArg-1 ){
382 fprintf(p->out, "%s", p->separator);
383 }else if( p->mode==MODE_Semi ){
384 fprintf(p->out, ";\n");
385 }else{
386 fprintf(p->out, "\n");
387 }
drh75897232000-05-29 14:26:00 +0000388 }
389 break;
390 }
drh1e5d0e92000-05-31 23:33:17 +0000391 case MODE_Html: {
392 if( p->cnt++==0 && p->showHeader ){
393 fprintf(p->out,"<TR>");
394 for(i=0; i<nArg; i++){
395 fprintf(p->out,"<TH>%s</TH>",azCol[i]);
396 }
397 fprintf(p->out,"</TR>\n");
398 }
drh6a535342001-10-19 16:44:56 +0000399 if( azArg==0 ) break;
drh28bd4bc2000-06-15 15:57:22 +0000400 fprintf(p->out,"<TR>");
drh1e5d0e92000-05-31 23:33:17 +0000401 for(i=0; i<nArg; i++){
drhc08a4f12000-06-15 16:49:48 +0000402 fprintf(p->out,"<TD>");
persicom7e2dfdd2002-04-18 02:46:52 +0000403 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
drhc08a4f12000-06-15 16:49:48 +0000404 fprintf(p->out,"</TD>\n");
drh1e5d0e92000-05-31 23:33:17 +0000405 }
drh7d686b22002-11-11 13:56:47 +0000406 fprintf(p->out,"</TR>\n");
drh1e5d0e92000-05-31 23:33:17 +0000407 break;
408 }
drh28bd4bc2000-06-15 15:57:22 +0000409 case MODE_Insert: {
drh6a535342001-10-19 16:44:56 +0000410 if( azArg==0 ) break;
drh33048c02001-10-01 14:29:22 +0000411 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
drh28bd4bc2000-06-15 15:57:22 +0000412 for(i=0; i<nArg; i++){
413 char *zSep = i>0 ? ",": "";
414 if( azArg[i]==0 ){
415 fprintf(p->out,"%sNULL",zSep);
danielk19778a6b5412004-05-24 07:04:25 +0000416 }else if( sqlite3IsNumber(azArg[i], 0, 1) ){
drh28bd4bc2000-06-15 15:57:22 +0000417 fprintf(p->out,"%s%s",zSep, azArg[i]);
418 }else{
419 if( zSep[0] ) fprintf(p->out,"%s",zSep);
420 output_quoted_string(p->out, azArg[i]);
421 }
422 }
423 fprintf(p->out,");\n");
drh6a535342001-10-19 16:44:56 +0000424 break;
drh28bd4bc2000-06-15 15:57:22 +0000425 }
persicom1d0b8722002-04-18 02:53:04 +0000426 }
drh75897232000-05-29 14:26:00 +0000427 return 0;
428}
429
430/*
drh33048c02001-10-01 14:29:22 +0000431** Set the destination table field of the callback_data structure to
432** the name of the table given. Escape any quote characters in the
433** table name.
434*/
435static void set_table_name(struct callback_data *p, const char *zName){
436 int i, n;
437 int needQuote;
438 char *z;
439
440 if( p->zDestTable ){
441 free(p->zDestTable);
442 p->zDestTable = 0;
443 }
444 if( zName==0 ) return;
445 needQuote = !isalpha(*zName) && *zName!='_';
446 for(i=n=0; zName[i]; i++, n++){
447 if( !isalnum(zName[i]) && zName[i]!='_' ){
448 needQuote = 1;
449 if( zName[i]=='\'' ) n++;
450 }
451 }
452 if( needQuote ) n += 2;
453 z = p->zDestTable = malloc( n+1 );
454 if( z==0 ){
455 fprintf(stderr,"Out of memory!\n");
456 exit(1);
457 }
458 n = 0;
459 if( needQuote ) z[n++] = '\'';
460 for(i=0; zName[i]; i++){
461 z[n++] = zName[i];
462 if( zName[i]=='\'' ) z[n++] = '\'';
463 }
464 if( needQuote ) z[n++] = '\'';
465 z[n] = 0;
466}
467
danielk19772a02e332004-06-05 08:04:36 +0000468/* zIn is either a pointer to a NULL-terminated string in memory obtained
469** from malloc(), or a NULL pointer. The string pointed to by zAppend is
470** added to zIn, and the result returned in memory obtained from malloc().
471** zIn, if it was not NULL, is freed.
472**
473** If the third argument, quote, is not '\0', then it is used as a
474** quote character for zAppend.
475*/
476static char * appendText(char *zIn, char const *zAppend, char quote){
477 int len;
478 int i;
479 int nAppend = strlen(zAppend);
480 int nIn = (zIn?strlen(zIn):0);
481
482 len = nAppend+nIn+1;
483 if( quote ){
484 len += 2;
485 for(i=0; i<nAppend; i++){
486 if( zAppend[i]==quote ) len++;
487 }
488 }
489
490 zIn = (char *)realloc(zIn, len);
491 if( !zIn ){
492 return 0;
493 }
494
495 if( quote ){
496 char *zCsr = &zIn[nIn];
497 *zCsr++ = quote;
498 for(i=0; i<nAppend; i++){
499 *zCsr++ = zAppend[i];
500 if( zAppend[i]==quote ) *zCsr++ = quote;
501 }
502 *zCsr++ = quote;
503 *zCsr++ = '\0';
504 assert( (zCsr-zIn)==len );
505 }else{
506 memcpy(&zIn[nIn], zAppend, nAppend);
507 zIn[len-1] = '\0';
508 }
509
510 return zIn;
511}
512
drh33048c02001-10-01 14:29:22 +0000513/*
drh4c653a02000-06-07 01:27:47 +0000514** This is a different callback routine used for dumping the database.
515** Each row received by this callback consists of a table name,
516** the table type ("index" or "table") and SQL to create the table.
517** This routine should print text sufficient to recreate the table.
518*/
519static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
danielk19772a02e332004-06-05 08:04:36 +0000520 int rc;
521 const char *zTable;
522 const char *zType;
523 const char *zSql;
drhdaffd0e2001-04-11 14:28:42 +0000524 struct callback_data *p = (struct callback_data *)pArg;
danielk19772a02e332004-06-05 08:04:36 +0000525
drh4c653a02000-06-07 01:27:47 +0000526 if( nArg!=3 ) return 1;
danielk19772a02e332004-06-05 08:04:36 +0000527 zTable = azArg[0];
528 zType = azArg[1];
529 zSql = azArg[2];
530
531 fprintf(p->out, "%s;\n", zSql);
532
533 if( strcmp(zType, "table")==0 ){
534 sqlite3_stmt *pTableInfo = 0;
535 sqlite3_stmt *pSelect = 0;
536 char *zSelect = 0;
537 char *zTableInfo = 0;
538 char *zTmp = 0;
539
540 zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
541 zTableInfo = appendText(zTableInfo, zTable, '"');
542 zTableInfo = appendText(zTableInfo, ");", 0);
543
544 rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0);
545 if( zTableInfo ) free(zTableInfo);
546 if( rc!=SQLITE_OK || !pTableInfo ){
547 return 1;
548 }
549
550 zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
551 zTmp = appendText(zTmp, zTable, '"');
552 if( zTmp ){
553 zSelect = appendText(zSelect, zTmp, '\'');
554 }
555 zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
556 rc = sqlite3_step(pTableInfo);
557 while( rc==SQLITE_ROW ){
danielk19773f41e972004-06-08 00:39:01 +0000558 zSelect = appendText(zSelect, "quote(", 0);
danielk19772a02e332004-06-05 08:04:36 +0000559 zSelect = appendText(zSelect, sqlite3_column_text(pTableInfo, 1), '"');
560 rc = sqlite3_step(pTableInfo);
561 if( rc==SQLITE_ROW ){
562 zSelect = appendText(zSelect, ") || ', ' || ", 0);
563 }else{
564 zSelect = appendText(zSelect, ") ", 0);
565 }
566 }
567 rc = sqlite3_finalize(pTableInfo);
568 if( rc!=SQLITE_OK ){
569 if( zSelect ) free(zSelect);
570 return 1;
571 }
572 zSelect = appendText(zSelect, "|| ')' FROM ", 0);
573 zSelect = appendText(zSelect, zTable, '"');
574
575 rc = sqlite3_prepare(p->db, zSelect, -1, &pSelect, 0);
576 if( zSelect ) free(zSelect);
577 if( rc!=SQLITE_OK || !pSelect ){
578 return 1;
579 }
580
581 rc = sqlite3_step(pSelect);
582 while( rc==SQLITE_ROW ){
583 fprintf(p->out, "%s;\n", sqlite3_column_text(pSelect, 0));
584 rc = sqlite3_step(pSelect);
585 }
586 rc = sqlite3_finalize(pSelect);
587 if( rc!=SQLITE_OK ){
588 return 1;
589 }
drh4c653a02000-06-07 01:27:47 +0000590 }
danielk19772a02e332004-06-05 08:04:36 +0000591
drh4c653a02000-06-07 01:27:47 +0000592 return 0;
593}
594
595/*
drh75897232000-05-29 14:26:00 +0000596** Text of a help message
597*/
persicom1d0b8722002-04-18 02:53:04 +0000598static char zHelp[] =
jplyon6a65bb32003-05-04 07:25:57 +0000599 ".databases List names and files of attached databases\n"
600 ".dump ?TABLE? ... Dump the database in a text format\n"
drhdaffd0e2001-04-11 14:28:42 +0000601 ".echo ON|OFF Turn command echo on or off\n"
drh75897232000-05-29 14:26:00 +0000602 ".exit Exit this program\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000603 ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000604 ".header(s) ON|OFF Turn display of headers on or off\n"
drh75897232000-05-29 14:26:00 +0000605 ".help Show this message\n"
606 ".indices TABLE Show names of all indices on TABLE\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000607 ".mode MODE Set mode to one of \"line(s)\", \"column(s)\", \n"
drhe3710332000-09-29 13:30:53 +0000608 " \"insert\", \"list\", or \"html\"\n"
drhc08a4f12000-06-15 16:49:48 +0000609 ".mode insert TABLE Generate SQL insert statements for TABLE\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000610 ".nullvalue STRING Print STRING instead of nothing for NULL data\n"
drh75897232000-05-29 14:26:00 +0000611 ".output FILENAME Send output to FILENAME\n"
612 ".output stdout Send output to the screen\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000613 ".prompt MAIN CONTINUE Replace the standard prompts\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000614 ".quit Exit this program\n"
drhdaffd0e2001-04-11 14:28:42 +0000615 ".read FILENAME Execute SQL in FILENAME\n"
drh9eb9e262004-02-11 02:18:05 +0000616#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000617 ".rekey OLD NEW NEW Change the encryption key\n"
618#endif
drh75897232000-05-29 14:26:00 +0000619 ".schema ?TABLE? Show the CREATE statements\n"
620 ".separator STRING Change separator string for \"list\" mode\n"
drhdd45df82002-04-18 12:39:03 +0000621 ".show Show the current values for various settings\n"
drha50da102000-08-08 20:19:09 +0000622 ".tables ?PATTERN? List names of tables matching a pattern\n"
drh2dfbbca2000-07-28 14:32:48 +0000623 ".timeout MS Try opening locked tables for MS milliseconds\n"
drh75897232000-05-29 14:26:00 +0000624 ".width NUM NUM ... Set column widths for \"column\" mode\n"
625;
626
drhdaffd0e2001-04-11 14:28:42 +0000627/* Forward reference */
628static void process_input(struct callback_data *p, FILE *in);
629
drh75897232000-05-29 14:26:00 +0000630/*
drh44c2eb12003-04-30 11:38:26 +0000631** Make sure the database is open. If it is not, then open it. If
632** the database fails to open, print an error message and exit.
633*/
634static void open_db(struct callback_data *p){
635 if( p->db==0 ){
drh9eb9e262004-02-11 02:18:05 +0000636#ifdef SQLITE_HAS_CODEC
drheb8ed702004-02-11 10:37:23 +0000637 int n = p->zKey ? strlen(p->zKey) : 0;
danielk19776f8a5032004-05-10 10:34:51 +0000638 db = p->db = sqlite3_open_encrypted(p->zDbFilename, p->zKey, n, 0, &zErrMsg);
danielk197780290862004-05-22 09:21:21 +0000639 assert(0); /* Encrypted databases are broken in SQLite 3 */
drheb8ed702004-02-11 10:37:23 +0000640#else
danielk19774f057f92004-06-08 00:02:33 +0000641 sqlite3_open(p->zDbFilename, &p->db);
danielk197780290862004-05-22 09:21:21 +0000642 db = p->db;
drheb8ed702004-02-11 10:37:23 +0000643#endif
danielk1977bc6ada42004-06-30 08:20:16 +0000644 sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0,
645 shellstaticFunc, 0, 0);
danielk197780290862004-05-22 09:21:21 +0000646 if( SQLITE_OK!=sqlite3_errcode(db) ){
647 fprintf(stderr,"Unable to open database \"%s\": %s\n",
648 p->zDbFilename, sqlite3_errmsg(db));
drh22fbcb82004-02-01 01:22:50 +0000649 exit(1);
drh44c2eb12003-04-30 11:38:26 +0000650 }
651 }
652}
653
654/*
drh75897232000-05-29 14:26:00 +0000655** If an input line begins with "." then invoke this routine to
656** process that line.
drh67505e72002-04-19 12:34:06 +0000657**
658** Return 1 to exit and 0 to continue.
drh75897232000-05-29 14:26:00 +0000659*/
drh44c2eb12003-04-30 11:38:26 +0000660static int do_meta_command(char *zLine, struct callback_data *p){
drh75897232000-05-29 14:26:00 +0000661 int i = 1;
662 int nArg = 0;
663 int n, c;
drh67505e72002-04-19 12:34:06 +0000664 int rc = 0;
drh75897232000-05-29 14:26:00 +0000665 char *azArg[50];
666
667 /* Parse the input line into tokens.
668 */
669 while( zLine[i] && nArg<ArraySize(azArg) ){
670 while( isspace(zLine[i]) ){ i++; }
drh06333682004-03-09 13:37:45 +0000671 if( zLine[i]==0 ) break;
drh75897232000-05-29 14:26:00 +0000672 if( zLine[i]=='\'' || zLine[i]=='"' ){
673 int delim = zLine[i++];
674 azArg[nArg++] = &zLine[i];
675 while( zLine[i] && zLine[i]!=delim ){ i++; }
676 if( zLine[i]==delim ){
677 zLine[i++] = 0;
678 }
679 }else{
680 azArg[nArg++] = &zLine[i];
681 while( zLine[i] && !isspace(zLine[i]) ){ i++; }
682 if( zLine[i] ) zLine[i++] = 0;
683 }
684 }
685
686 /* Process the input line.
687 */
drh67505e72002-04-19 12:34:06 +0000688 if( nArg==0 ) return rc;
drh75897232000-05-29 14:26:00 +0000689 n = strlen(azArg[0]);
690 c = azArg[0][0];
jplyon6a65bb32003-05-04 07:25:57 +0000691 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
jplyon672a1ed2003-05-11 20:07:05 +0000692 struct callback_data data;
693 char *zErrMsg = 0;
jplyon6a65bb32003-05-04 07:25:57 +0000694 open_db(p);
jplyon672a1ed2003-05-11 20:07:05 +0000695 memcpy(&data, p, sizeof(data));
drhd8885442004-03-17 23:42:12 +0000696 data.showHeader = 1;
jplyon672a1ed2003-05-11 20:07:05 +0000697 data.mode = MODE_Column;
drhd8885442004-03-17 23:42:12 +0000698 data.colWidth[0] = 3;
699 data.colWidth[1] = 15;
700 data.colWidth[2] = 58;
danielk19776f8a5032004-05-10 10:34:51 +0000701 sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
jplyon672a1ed2003-05-11 20:07:05 +0000702 if( zErrMsg ){
703 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000704 sqlite3_free(zErrMsg);
jplyon6a65bb32003-05-04 07:25:57 +0000705 }
706 }else
707
drh4c653a02000-06-07 01:27:47 +0000708 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
709 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000710 open_db(p);
drh33048c02001-10-01 14:29:22 +0000711 fprintf(p->out, "BEGIN TRANSACTION;\n");
drh4c653a02000-06-07 01:27:47 +0000712 if( nArg==1 ){
danielk19776f8a5032004-05-10 10:34:51 +0000713 sqlite3_exec(p->db,
drha18c5682000-10-08 22:20:57 +0000714 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000715 "WHERE type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000716 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000717 dump_callback, p, &zErrMsg
718 );
drh4c653a02000-06-07 01:27:47 +0000719 }else{
720 int i;
721 for(i=1; i<nArg && zErrMsg==0; i++){
danielk1977bc6ada42004-06-30 08:20:16 +0000722 zShellStatic = azArg[i];
723 sqlite3_exec(p->db,
drha18c5682000-10-08 22:20:57 +0000724 "SELECT name, type, sql FROM sqlite_master "
danielk1977bc6ada42004-06-30 08:20:16 +0000725 "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000726 "ORDER BY substr(type,2,1), name",
danielk1977bc6ada42004-06-30 08:20:16 +0000727 dump_callback, p, &zErrMsg
drha18c5682000-10-08 22:20:57 +0000728 );
danielk1977bc6ada42004-06-30 08:20:16 +0000729 zShellStatic = 0;
drh4c653a02000-06-07 01:27:47 +0000730 }
731 }
732 if( zErrMsg ){
733 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000734 sqlite3_free(zErrMsg);
drh33048c02001-10-01 14:29:22 +0000735 }else{
736 fprintf(p->out, "COMMIT;\n");
drh4c653a02000-06-07 01:27:47 +0000737 }
738 }else
drh75897232000-05-29 14:26:00 +0000739
drhdaffd0e2001-04-11 14:28:42 +0000740 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
741 int j;
742 char *z = azArg[1];
743 int val = atoi(azArg[1]);
744 for(j=0; z[j]; j++){
745 if( isupper(z[j]) ) z[j] = tolower(z[j]);
746 }
747 if( strcmp(z,"on")==0 ){
748 val = 1;
749 }else if( strcmp(z,"yes")==0 ){
750 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000751 }
drhdaffd0e2001-04-11 14:28:42 +0000752 p->echoOn = val;
753 }else
754
drh75897232000-05-29 14:26:00 +0000755 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
drh67505e72002-04-19 12:34:06 +0000756 rc = 1;
drh75897232000-05-29 14:26:00 +0000757 }else
758
drhdd45df82002-04-18 12:39:03 +0000759 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +0000760 int j;
drhdd45df82002-04-18 12:39:03 +0000761 char *z = nArg>=2 ? azArg[1] : "1";
762 int val = atoi(z);
persicom7e2dfdd2002-04-18 02:46:52 +0000763 for(j=0; z[j]; j++){
764 if( isupper(z[j]) ) z[j] = tolower(z[j]);
765 }
766 if( strcmp(z,"on")==0 ){
767 val = 1;
768 }else if( strcmp(z,"yes")==0 ){
769 val = 1;
770 }
771 if(val == 1) {
772 if(!p->explainPrev.valid) {
773 p->explainPrev.valid = 1;
774 p->explainPrev.mode = p->mode;
775 p->explainPrev.showHeader = p->showHeader;
776 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
777 }
778 /* We could put this code under the !p->explainValid
779 ** condition so that it does not execute if we are already in
780 ** explain mode. However, always executing it allows us an easy
781 ** was to reset to explain mode in case the user previously
782 ** did an .explain followed by a .width, .mode or .header
783 ** command.
784 */
785 p->mode = MODE_Column;
786 p->showHeader = 1;
787 memset(p->colWidth,0,ArraySize(p->colWidth));
788 p->colWidth[0] = 4;
789 p->colWidth[1] = 12;
790 p->colWidth[2] = 10;
791 p->colWidth[3] = 10;
792 p->colWidth[4] = 35;
793 }else if (p->explainPrev.valid) {
794 p->explainPrev.valid = 0;
795 p->mode = p->explainPrev.mode;
796 p->showHeader = p->explainPrev.showHeader;
797 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
798 }
drh75897232000-05-29 14:26:00 +0000799 }else
800
persicom7e2dfdd2002-04-18 02:46:52 +0000801 if( c=='h' && (strncmp(azArg[0], "header", n)==0
802 ||
803 strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){
drh75897232000-05-29 14:26:00 +0000804 int j;
805 char *z = azArg[1];
806 int val = atoi(azArg[1]);
807 for(j=0; z[j]; j++){
808 if( isupper(z[j]) ) z[j] = tolower(z[j]);
809 }
810 if( strcmp(z,"on")==0 ){
811 val = 1;
812 }else if( strcmp(z,"yes")==0 ){
813 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000814 }
drh75897232000-05-29 14:26:00 +0000815 p->showHeader = val;
816 }else
817
818 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
819 fprintf(stderr,zHelp);
820 }else
821
822 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
823 struct callback_data data;
824 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000825 open_db(p);
drh75897232000-05-29 14:26:00 +0000826 memcpy(&data, p, sizeof(data));
827 data.showHeader = 0;
828 data.mode = MODE_List;
danielk1977bc6ada42004-06-30 08:20:16 +0000829 zShellStatic = azArg[1];
830 sqlite3_exec(p->db,
drha18c5682000-10-08 22:20:57 +0000831 "SELECT name FROM sqlite_master "
danielk1977bc6ada42004-06-30 08:20:16 +0000832 "WHERE type='index' AND tbl_name LIKE shellstatic() "
drhe0bc4042002-06-25 01:09:11 +0000833 "UNION ALL "
834 "SELECT name FROM sqlite_temp_master "
danielk1977bc6ada42004-06-30 08:20:16 +0000835 "WHERE type='index' AND tbl_name LIKE shellstatic() "
drhe0bc4042002-06-25 01:09:11 +0000836 "ORDER BY 1",
danielk1977bc6ada42004-06-30 08:20:16 +0000837 callback, &data, &zErrMsg
drha18c5682000-10-08 22:20:57 +0000838 );
danielk1977bc6ada42004-06-30 08:20:16 +0000839 zShellStatic = 0;
drh75897232000-05-29 14:26:00 +0000840 if( zErrMsg ){
841 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000842 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +0000843 }
844 }else
845
drh28bd4bc2000-06-15 15:57:22 +0000846 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
drh75897232000-05-29 14:26:00 +0000847 int n2 = strlen(azArg[1]);
persicom7e2dfdd2002-04-18 02:46:52 +0000848 if( strncmp(azArg[1],"line",n2)==0
849 ||
850 strncmp(azArg[1],"lines",n2)==0 ){
drh75897232000-05-29 14:26:00 +0000851 p->mode = MODE_Line;
persicom7e2dfdd2002-04-18 02:46:52 +0000852 }else if( strncmp(azArg[1],"column",n2)==0
853 ||
854 strncmp(azArg[1],"columns",n2)==0 ){
drh75897232000-05-29 14:26:00 +0000855 p->mode = MODE_Column;
856 }else if( strncmp(azArg[1],"list",n2)==0 ){
857 p->mode = MODE_List;
drh1e5d0e92000-05-31 23:33:17 +0000858 }else if( strncmp(azArg[1],"html",n2)==0 ){
859 p->mode = MODE_Html;
drh28bd4bc2000-06-15 15:57:22 +0000860 }else if( strncmp(azArg[1],"insert",n2)==0 ){
861 p->mode = MODE_Insert;
862 if( nArg>=3 ){
drh33048c02001-10-01 14:29:22 +0000863 set_table_name(p, azArg[2]);
drh28bd4bc2000-06-15 15:57:22 +0000864 }else{
drh33048c02001-10-01 14:29:22 +0000865 set_table_name(p, "table");
drh28bd4bc2000-06-15 15:57:22 +0000866 }
drhdaffd0e2001-04-11 14:28:42 +0000867 }else {
868 fprintf(stderr,"mode should be on of: column html insert line list\n");
drh75897232000-05-29 14:26:00 +0000869 }
870 }else
871
persicom7e2dfdd2002-04-18 02:46:52 +0000872 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
873 sprintf(p->nullvalue, "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
874 }else
875
drh75897232000-05-29 14:26:00 +0000876 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
877 if( p->out!=stdout ){
878 fclose(p->out);
879 }
880 if( strcmp(azArg[1],"stdout")==0 ){
881 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +0000882 strcpy(p->outfile,"stdout");
drh75897232000-05-29 14:26:00 +0000883 }else{
drha1f9b5e2004-02-14 16:31:02 +0000884 p->out = fopen(azArg[1], "wb");
drh75897232000-05-29 14:26:00 +0000885 if( p->out==0 ){
886 fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
887 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +0000888 } else {
889 strcpy(p->outfile,azArg[1]);
drh75897232000-05-29 14:26:00 +0000890 }
891 }
892 }else
893
drhdd45df82002-04-18 12:39:03 +0000894 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
persicom7e2dfdd2002-04-18 02:46:52 +0000895 if( nArg >= 2) {
896 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
897 }
898 if( nArg >= 3) {
899 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
900 }
901 }else
902
903 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
drhf73287c2004-02-25 02:25:37 +0000904 rc = 1;
persicom7e2dfdd2002-04-18 02:46:52 +0000905 }else
906
drhdaffd0e2001-04-11 14:28:42 +0000907 if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
drha1f9b5e2004-02-14 16:31:02 +0000908 FILE *alt = fopen(azArg[1], "rb");
drhdaffd0e2001-04-11 14:28:42 +0000909 if( alt==0 ){
910 fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
911 }else{
912 process_input(p, alt);
913 fclose(alt);
914 }
915 }else
916
drh9eb9e262004-02-11 02:18:05 +0000917#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000918 if( c=='r' && strncmp(azArg[0],"rekey", n)==0 && nArg==4 ){
919 char *zOld = p->zKey;
920 if( zOld==0 ) zOld = "";
921 if( strcmp(azArg[1],zOld) ){
922 fprintf(stderr,"old key is incorrect\n");
923 }else if( strcmp(azArg[2], azArg[3]) ){
924 fprintf(stderr,"2nd copy of new key does not match the 1st\n");
925 }else{
drh3f4fedb2004-05-31 19:34:33 +0000926 sqlite3_free(p->zKey);
danielk19776f8a5032004-05-10 10:34:51 +0000927 p->zKey = sqlite3_mprintf("%s", azArg[2]);
drh22fbcb82004-02-01 01:22:50 +0000928 sqlite_rekey(p->db, p->zKey, strlen(p->zKey));
929 }
930 }else
931#endif
932
drh75897232000-05-29 14:26:00 +0000933 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
934 struct callback_data data;
935 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000936 open_db(p);
drh75897232000-05-29 14:26:00 +0000937 memcpy(&data, p, sizeof(data));
938 data.showHeader = 0;
drhe3710332000-09-29 13:30:53 +0000939 data.mode = MODE_Semi;
drh75897232000-05-29 14:26:00 +0000940 if( nArg>1 ){
danielk19774adee202004-05-08 08:23:19 +0000941 extern int sqlite3StrICmp(const char*,const char*);
942 if( sqlite3StrICmp(azArg[1],"sqlite_master")==0 ){
drha18c5682000-10-08 22:20:57 +0000943 char *new_argv[2], *new_colv[2];
944 new_argv[0] = "CREATE TABLE sqlite_master (\n"
945 " type text,\n"
946 " name text,\n"
947 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000948 " rootpage integer,\n"
drha18c5682000-10-08 22:20:57 +0000949 " sql text\n"
950 ")";
951 new_argv[1] = 0;
952 new_colv[0] = "sql";
953 new_colv[1] = 0;
954 callback(&data, 1, new_argv, new_colv);
danielk19774adee202004-05-08 08:23:19 +0000955 }else if( sqlite3StrICmp(azArg[1],"sqlite_temp_master")==0 ){
drhe0bc4042002-06-25 01:09:11 +0000956 char *new_argv[2], *new_colv[2];
957 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
958 " type text,\n"
959 " name text,\n"
960 " tbl_name text,\n"
961 " rootpage integer,\n"
962 " sql text\n"
963 ")";
964 new_argv[1] = 0;
965 new_colv[0] = "sql";
966 new_colv[1] = 0;
967 callback(&data, 1, new_argv, new_colv);
drha18c5682000-10-08 22:20:57 +0000968 }else{
danielk1977bc6ada42004-06-30 08:20:16 +0000969 zShellStatic = azArg[1];
970 sqlite3_exec(p->db,
drhe0bc4042002-06-25 01:09:11 +0000971 "SELECT sql FROM "
972 " (SELECT * FROM sqlite_master UNION ALL"
973 " SELECT * FROM sqlite_temp_master) "
danielk1977bc6ada42004-06-30 08:20:16 +0000974 "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +0000975 "ORDER BY substr(type,2,1), name",
danielk1977bc6ada42004-06-30 08:20:16 +0000976 callback, &data, &zErrMsg);
977 zShellStatic = 0;
drha18c5682000-10-08 22:20:57 +0000978 }
drh75897232000-05-29 14:26:00 +0000979 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000980 sqlite3_exec(p->db,
drhe0bc4042002-06-25 01:09:11 +0000981 "SELECT sql FROM "
982 " (SELECT * FROM sqlite_master UNION ALL"
983 " SELECT * FROM sqlite_temp_master) "
drh33048c02001-10-01 14:29:22 +0000984 "WHERE type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +0000985 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000986 callback, &data, &zErrMsg
987 );
drh75897232000-05-29 14:26:00 +0000988 }
drh75897232000-05-29 14:26:00 +0000989 if( zErrMsg ){
990 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000991 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +0000992 }
993 }else
994
995 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
996 sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
997 }else
998
persicom7e2dfdd2002-04-18 02:46:52 +0000999 if( c=='s' && strncmp(azArg[0], "show", n)==0){
1000 int i;
1001 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
drh67505e72002-04-19 12:34:06 +00001002 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
drhdd45df82002-04-18 12:39:03 +00001003 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
persicom7e2dfdd2002-04-18 02:46:52 +00001004 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
1005 fprintf(p->out,"%9.9s: %s\n","nullvalue", p->nullvalue);
drh67505e72002-04-19 12:34:06 +00001006 fprintf(p->out,"%9.9s: %s\n","output",
1007 strlen(p->outfile) ? p->outfile : "stdout");
persicom7e2dfdd2002-04-18 02:46:52 +00001008 fprintf(p->out,"%9.9s: %s\n","separator", p->separator);
1009 fprintf(p->out,"%9.9s: ","width");
1010 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
1011 fprintf(p->out,"%d ",p->colWidth[i]);
1012 }
1013 fprintf(p->out,"\n\n");
1014 }else
1015
drh2dfbbca2000-07-28 14:32:48 +00001016 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
drhe3710332000-09-29 13:30:53 +00001017 char **azResult;
1018 int nRow, rc;
1019 char *zErrMsg;
drh44c2eb12003-04-30 11:38:26 +00001020 open_db(p);
drha50da102000-08-08 20:19:09 +00001021 if( nArg==1 ){
danielk19776f8a5032004-05-10 10:34:51 +00001022 rc = sqlite3_get_table(p->db,
drha50da102000-08-08 20:19:09 +00001023 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +00001024 "WHERE type IN ('table','view') "
drhe0bc4042002-06-25 01:09:11 +00001025 "UNION ALL "
1026 "SELECT name FROM sqlite_temp_master "
1027 "WHERE type IN ('table','view') "
1028 "ORDER BY 1",
drha18c5682000-10-08 22:20:57 +00001029 &azResult, &nRow, 0, &zErrMsg
1030 );
drha50da102000-08-08 20:19:09 +00001031 }else{
danielk1977bc6ada42004-06-30 08:20:16 +00001032 zShellStatic = azArg[1];
1033 rc = sqlite3_get_table(p->db,
drha50da102000-08-08 20:19:09 +00001034 "SELECT name FROM sqlite_master "
danielk1977bc6ada42004-06-30 08:20:16 +00001035 "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' "
drhe0bc4042002-06-25 01:09:11 +00001036 "UNION ALL "
1037 "SELECT name FROM sqlite_temp_master "
danielk1977bc6ada42004-06-30 08:20:16 +00001038 "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' "
drhe0bc4042002-06-25 01:09:11 +00001039 "ORDER BY 1",
danielk1977bc6ada42004-06-30 08:20:16 +00001040 &azResult, &nRow, 0, &zErrMsg
drha18c5682000-10-08 22:20:57 +00001041 );
danielk1977bc6ada42004-06-30 08:20:16 +00001042 zShellStatic = 0;
drha50da102000-08-08 20:19:09 +00001043 }
drh75897232000-05-29 14:26:00 +00001044 if( zErrMsg ){
1045 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001046 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00001047 }
drhe3710332000-09-29 13:30:53 +00001048 if( rc==SQLITE_OK ){
1049 int len, maxlen = 0;
1050 int i, j;
1051 int nPrintCol, nPrintRow;
1052 for(i=1; i<=nRow; i++){
1053 if( azResult[i]==0 ) continue;
1054 len = strlen(azResult[i]);
1055 if( len>maxlen ) maxlen = len;
1056 }
1057 nPrintCol = 80/(maxlen+2);
1058 if( nPrintCol<1 ) nPrintCol = 1;
1059 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
1060 for(i=0; i<nPrintRow; i++){
1061 for(j=i+1; j<=nRow; j+=nPrintRow){
1062 char *zSp = j<=nPrintRow ? "" : " ";
1063 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
1064 }
1065 printf("\n");
1066 }
1067 }
danielk19776f8a5032004-05-10 10:34:51 +00001068 sqlite3_free_table(azResult);
drh75897232000-05-29 14:26:00 +00001069 }else
1070
drh2dfbbca2000-07-28 14:32:48 +00001071 if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
drh44c2eb12003-04-30 11:38:26 +00001072 open_db(p);
danielk19776f8a5032004-05-10 10:34:51 +00001073 sqlite3_busy_timeout(p->db, atoi(azArg[1]));
drh2dfbbca2000-07-28 14:32:48 +00001074 }else
1075
drh75897232000-05-29 14:26:00 +00001076 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
1077 int j;
1078 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
1079 p->colWidth[j-1] = atoi(azArg[j]);
1080 }
1081 }else
1082
1083 {
drh67505e72002-04-19 12:34:06 +00001084 fprintf(stderr, "unknown command or invalid arguments: "
1085 " \"%s\". Enter \".help\" for help\n", azArg[0]);
drh75897232000-05-29 14:26:00 +00001086 }
drh67505e72002-04-19 12:34:06 +00001087
1088 return rc;
drh75897232000-05-29 14:26:00 +00001089}
1090
drh67505e72002-04-19 12:34:06 +00001091/*
drh324ccef2003-02-05 14:06:20 +00001092** Return TRUE if the last non-whitespace character in z[] is a semicolon.
1093** z[] is N characters long.
1094*/
1095static int _ends_with_semicolon(const char *z, int N){
1096 while( N>0 && isspace(z[N-1]) ){ N--; }
1097 return N>0 && z[N-1]==';';
1098}
1099
1100/*
drh70c7a4b2003-04-26 03:03:06 +00001101** Test to see if a line consists entirely of whitespace.
1102*/
1103static int _all_whitespace(const char *z){
1104 for(; *z; z++){
1105 if( isspace(*z) ) continue;
1106 if( *z=='/' && z[1]=='*' ){
1107 z += 2;
1108 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
1109 if( *z==0 ) return 0;
1110 z++;
1111 continue;
1112 }
1113 if( *z=='-' && z[1]=='-' ){
1114 z += 2;
1115 while( *z && *z!='\n' ){ z++; }
1116 if( *z==0 ) return 1;
1117 continue;
1118 }
1119 return 0;
1120 }
1121 return 1;
1122}
1123
1124/*
drha9b17162003-04-29 18:01:28 +00001125** Return TRUE if the line typed in is an SQL command terminator other
1126** than a semi-colon. The SQL Server style "go" command is understood
1127** as is the Oracle "/".
1128*/
1129static int _is_command_terminator(const char *zLine){
danielk19774adee202004-05-08 08:23:19 +00001130 extern int sqlite3StrNICmp(const char*,const char*,int);
drha9b17162003-04-29 18:01:28 +00001131 while( isspace(*zLine) ){ zLine++; };
1132 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ) return 1; /* Oracle */
danielk19774adee202004-05-08 08:23:19 +00001133 if( sqlite3StrNICmp(zLine,"go",2)==0 && _all_whitespace(&zLine[2]) ){
drha9b17162003-04-29 18:01:28 +00001134 return 1; /* SQL Server */
1135 }
1136 return 0;
1137}
1138
1139/*
drh67505e72002-04-19 12:34:06 +00001140** Read input from *in and process it. If *in==0 then input
1141** is interactive - the user is typing it it. Otherwise, input
1142** is coming from a file or device. A prompt is issued and history
1143** is saved only if input is interactive. An interrupt signal will
1144** cause this routine to exit immediately, unless input is interactive.
1145*/
drhdaffd0e2001-04-11 14:28:42 +00001146static void process_input(struct callback_data *p, FILE *in){
1147 char *zLine;
1148 char *zSql = 0;
1149 int nSql = 0;
1150 char *zErrMsg;
drh7f953e22002-07-13 17:33:45 +00001151 int rc;
drh41e941d2002-04-04 15:10:12 +00001152 while( fflush(p->out), (zLine = one_input_line(zSql, in))!=0 ){
drh67505e72002-04-19 12:34:06 +00001153 if( seenInterrupt ){
1154 if( in!=0 ) break;
1155 seenInterrupt = 0;
1156 }
drhdaffd0e2001-04-11 14:28:42 +00001157 if( p->echoOn ) printf("%s\n", zLine);
drhf817b6b2003-06-16 00:16:41 +00001158 if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
drh2af0b2d2002-02-21 02:25:02 +00001159 if( zLine && zLine[0]=='.' && nSql==0 ){
drh44c2eb12003-04-30 11:38:26 +00001160 int rc = do_meta_command(zLine, p);
drhdaffd0e2001-04-11 14:28:42 +00001161 free(zLine);
drh67505e72002-04-19 12:34:06 +00001162 if( rc ) break;
drhdaffd0e2001-04-11 14:28:42 +00001163 continue;
1164 }
drha9b17162003-04-29 18:01:28 +00001165 if( _is_command_terminator(zLine) ){
1166 strcpy(zLine,";");
1167 }
drhdaffd0e2001-04-11 14:28:42 +00001168 if( zSql==0 ){
1169 int i;
1170 for(i=0; zLine[i] && isspace(zLine[i]); i++){}
1171 if( zLine[i]!=0 ){
1172 nSql = strlen(zLine);
1173 zSql = malloc( nSql+1 );
1174 strcpy(zSql, zLine);
1175 }
1176 }else{
1177 int len = strlen(zLine);
1178 zSql = realloc( zSql, nSql + len + 2 );
1179 if( zSql==0 ){
1180 fprintf(stderr,"%s: out of memory!\n", Argv0);
1181 exit(1);
1182 }
1183 strcpy(&zSql[nSql++], "\n");
1184 strcpy(&zSql[nSql], zLine);
1185 nSql += len;
1186 }
1187 free(zLine);
danielk19776f8a5032004-05-10 10:34:51 +00001188 if( zSql && _ends_with_semicolon(zSql, nSql) && sqlite3_complete(zSql) ){
drhdaffd0e2001-04-11 14:28:42 +00001189 p->cnt = 0;
drh44c2eb12003-04-30 11:38:26 +00001190 open_db(p);
danielk19776f8a5032004-05-10 10:34:51 +00001191 rc = sqlite3_exec(p->db, zSql, callback, p, &zErrMsg);
drh7f953e22002-07-13 17:33:45 +00001192 if( rc || zErrMsg ){
drhdaffd0e2001-04-11 14:28:42 +00001193 if( in!=0 && !p->echoOn ) printf("%s\n",zSql);
drh7f953e22002-07-13 17:33:45 +00001194 if( zErrMsg!=0 ){
1195 printf("SQL error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001196 sqlite3_free(zErrMsg);
drh7f953e22002-07-13 17:33:45 +00001197 zErrMsg = 0;
1198 }else{
danielk19772a02e332004-06-05 08:04:36 +00001199 printf("SQL error: %s\n", sqlite3_errmsg(p->db));
drh7f953e22002-07-13 17:33:45 +00001200 }
drhdaffd0e2001-04-11 14:28:42 +00001201 }
1202 free(zSql);
1203 zSql = 0;
1204 nSql = 0;
1205 }
1206 }
1207 if( zSql ){
drh70c7a4b2003-04-26 03:03:06 +00001208 if( !_all_whitespace(zSql) ) printf("Incomplete SQL: %s\n", zSql);
drhdaffd0e2001-04-11 14:28:42 +00001209 free(zSql);
1210 }
1211}
1212
drh67505e72002-04-19 12:34:06 +00001213/*
1214** Return a pathname which is the user's home directory. A
1215** 0 return indicates an error of some kind. Space to hold the
1216** resulting string is obtained from malloc(). The calling
1217** function should free the result.
1218*/
1219static char *find_home_dir(void){
1220 char *home_dir = NULL;
persicom7e2dfdd2002-04-18 02:46:52 +00001221
drh820f3812003-01-08 13:02:52 +00001222#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
drh67505e72002-04-19 12:34:06 +00001223 struct passwd *pwent;
1224 uid_t uid = getuid();
drhbd842ba2002-08-21 11:26:41 +00001225 if( (pwent=getpwuid(uid)) != NULL) {
1226 home_dir = pwent->pw_dir;
drh67505e72002-04-19 12:34:06 +00001227 }
1228#endif
1229
drh820f3812003-01-08 13:02:52 +00001230#ifdef __MACOS__
1231 char home_path[_MAX_PATH+1];
1232 home_dir = getcwd(home_path, _MAX_PATH);
1233#endif
1234
drh67505e72002-04-19 12:34:06 +00001235 if (!home_dir) {
1236 home_dir = getenv("HOME");
1237 if (!home_dir) {
1238 home_dir = getenv("HOMEPATH"); /* Windows? */
1239 }
1240 }
1241
drhe98d4fa2002-04-21 19:06:22 +00001242#if defined(_WIN32) || defined(WIN32)
1243 if (!home_dir) {
1244 home_dir = "c:";
1245 }
1246#endif
1247
drh67505e72002-04-19 12:34:06 +00001248 if( home_dir ){
1249 char *z = malloc( strlen(home_dir)+1 );
1250 if( z ) strcpy(z, home_dir);
1251 home_dir = z;
1252 }
drhe98d4fa2002-04-21 19:06:22 +00001253
drh67505e72002-04-19 12:34:06 +00001254 return home_dir;
1255}
1256
1257/*
1258** Read input from the file given by sqliterc_override. Or if that
1259** parameter is NULL, take input from ~/.sqliterc
1260*/
drh22fbcb82004-02-01 01:22:50 +00001261static void process_sqliterc(
1262 struct callback_data *p, /* Configuration data */
1263 const char *sqliterc_override /* Name of config file. NULL to use default */
1264){
persicom7e2dfdd2002-04-18 02:46:52 +00001265 char *home_dir = NULL;
drh22fbcb82004-02-01 01:22:50 +00001266 const char *sqliterc = sqliterc_override;
1267 char *zBuf;
persicom7e2dfdd2002-04-18 02:46:52 +00001268 FILE *in = NULL;
1269
1270 if (sqliterc == NULL) {
drh67505e72002-04-19 12:34:06 +00001271 home_dir = find_home_dir();
drhe98d4fa2002-04-21 19:06:22 +00001272 if( home_dir==0 ){
1273 fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0);
1274 return;
1275 }
drh22fbcb82004-02-01 01:22:50 +00001276 zBuf = malloc(strlen(home_dir) + 15);
1277 if( zBuf==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +00001278 fprintf(stderr,"%s: out of memory!\n", Argv0);
1279 exit(1);
1280 }
drh22fbcb82004-02-01 01:22:50 +00001281 sprintf(zBuf,"%s/.sqliterc",home_dir);
drh67505e72002-04-19 12:34:06 +00001282 free(home_dir);
drh22fbcb82004-02-01 01:22:50 +00001283 sqliterc = (const char*)zBuf;
persicom7e2dfdd2002-04-18 02:46:52 +00001284 }
drha1f9b5e2004-02-14 16:31:02 +00001285 in = fopen(sqliterc,"rb");
drh22fbcb82004-02-01 01:22:50 +00001286 if( in ){
1287 if( isatty(fileno(stdout)) ){
1288 printf("Loading resources from %s\n",sqliterc);
1289 }
persicom7e2dfdd2002-04-18 02:46:52 +00001290 process_input(p,in);
drhdd45df82002-04-18 12:39:03 +00001291 fclose(in);
persicom7e2dfdd2002-04-18 02:46:52 +00001292 }
1293 return;
1294}
1295
drh67505e72002-04-19 12:34:06 +00001296/*
drhe1e38c42003-05-04 18:30:59 +00001297** Show available command line options
1298*/
1299static const char zOptions[] =
1300 " -init filename read/process named file\n"
1301 " -echo print commands before execution\n"
1302 " -[no]header turn headers on or off\n"
1303 " -column set output mode to 'column'\n"
1304 " -html set output mode to HTML\n"
drh9eb9e262004-02-11 02:18:05 +00001305#ifdef SQLITE_HAS_CODEC
drh57ced912004-02-10 02:57:59 +00001306 " -key KEY encryption key\n"
1307#endif
drhe1e38c42003-05-04 18:30:59 +00001308 " -line set output mode to 'line'\n"
1309 " -list set output mode to 'list'\n"
1310 " -separator 'x' set output field separator (|)\n"
1311 " -nullvalue 'text' set text string for NULL values\n"
1312 " -version show SQLite version\n"
1313 " -help show this text, also show dot-commands\n"
1314;
1315static void usage(int showDetail){
1316 fprintf(stderr, "Usage: %s [OPTIONS] FILENAME [SQL]\n", Argv0);
1317 if( showDetail ){
1318 fprintf(stderr, "Options are:\n%s", zOptions);
1319 }else{
1320 fprintf(stderr, "Use the -help option for additional information\n");
1321 }
1322 exit(1);
1323}
1324
1325/*
drh67505e72002-04-19 12:34:06 +00001326** Initialize the state information in data
1327*/
persicom7e2dfdd2002-04-18 02:46:52 +00001328void main_init(struct callback_data *data) {
1329 memset(data, 0, sizeof(*data));
1330 data->mode = MODE_List;
1331 strcpy(data->separator,"|");
1332 data->showHeader = 0;
1333 strcpy(mainPrompt,"sqlite> ");
1334 strcpy(continuePrompt," ...> ");
1335}
1336
drh75897232000-05-29 14:26:00 +00001337int main(int argc, char **argv){
drh75897232000-05-29 14:26:00 +00001338 char *zErrMsg = 0;
1339 struct callback_data data;
drh22fbcb82004-02-01 01:22:50 +00001340 const char *zInitFile = 0;
1341 char *zFirstCmd = 0;
drh44c2eb12003-04-30 11:38:26 +00001342 int i;
danielk19774adee202004-05-08 08:23:19 +00001343 extern int sqlite3OsFileExists(const char*);
drh75897232000-05-29 14:26:00 +00001344
drh820f3812003-01-08 13:02:52 +00001345#ifdef __MACOS__
1346 argc = ccommand(&argv);
drh820f3812003-01-08 13:02:52 +00001347#endif
1348
drhdaffd0e2001-04-11 14:28:42 +00001349 Argv0 = argv[0];
persicom7e2dfdd2002-04-18 02:46:52 +00001350 main_init(&data);
persicom7e2dfdd2002-04-18 02:46:52 +00001351
drh44c2eb12003-04-30 11:38:26 +00001352 /* Make sure we have a valid signal handler early, before anything
1353 ** else is done.
1354 */
drh4c504392000-10-16 22:06:40 +00001355#ifdef SIGINT
1356 signal(SIGINT, interrupt_handler);
1357#endif
drh44c2eb12003-04-30 11:38:26 +00001358
drh22fbcb82004-02-01 01:22:50 +00001359 /* Do an initial pass through the command-line argument to locate
1360 ** the name of the database file, the name of the initialization file,
1361 ** and the first command to execute.
drh44c2eb12003-04-30 11:38:26 +00001362 */
drh22fbcb82004-02-01 01:22:50 +00001363 for(i=1; i<argc-1; i++){
drh44c2eb12003-04-30 11:38:26 +00001364 if( argv[i][0]!='-' ) break;
1365 if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){
1366 i++;
drh22fbcb82004-02-01 01:22:50 +00001367 }else if( strcmp(argv[i],"-init")==0 ){
1368 i++;
1369 zInitFile = argv[i];
1370 }else if( strcmp(argv[i],"-key")==0 ){
1371 i++;
danielk19776f8a5032004-05-10 10:34:51 +00001372 data.zKey = sqlite3_mprintf("%s",argv[i]);
drh44c2eb12003-04-30 11:38:26 +00001373 }
1374 }
drh22fbcb82004-02-01 01:22:50 +00001375 if( i<argc ){
1376 data.zDbFilename = argv[i++];
1377 }else{
1378 data.zDbFilename = ":memory:";
1379 }
1380 if( i<argc ){
1381 zFirstCmd = argv[i++];
1382 }
drh44c2eb12003-04-30 11:38:26 +00001383 data.out = stdout;
1384
1385 /* Go ahead and open the database file if it already exists. If the
1386 ** file does not exist, delay opening it. This prevents empty database
1387 ** files from being created if a user mistypes the database name argument
1388 ** to the sqlite command-line tool.
1389 */
danielk19774adee202004-05-08 08:23:19 +00001390 if( sqlite3OsFileExists(data.zDbFilename) ){
drh44c2eb12003-04-30 11:38:26 +00001391 open_db(&data);
1392 }
1393
drh22fbcb82004-02-01 01:22:50 +00001394 /* Process the initialization file if there is one. If no -init option
1395 ** is given on the command line, look for a file named ~/.sqliterc and
1396 ** try to process it.
drh44c2eb12003-04-30 11:38:26 +00001397 */
drh22fbcb82004-02-01 01:22:50 +00001398 process_sqliterc(&data,zInitFile);
drh44c2eb12003-04-30 11:38:26 +00001399
drh22fbcb82004-02-01 01:22:50 +00001400 /* Make a second pass through the command-line argument and set
1401 ** options. This second pass is delayed until after the initialization
1402 ** file is processed so that the command-line arguments will override
1403 ** settings in the initialization file.
drh44c2eb12003-04-30 11:38:26 +00001404 */
drh22fbcb82004-02-01 01:22:50 +00001405 for(i=1; i<argc && argv[i][0]=='-'; i++){
1406 char *z = argv[i];
1407 if( strcmp(z,"-init")==0 || strcmp(z,"-key")==0 ){
1408 i++;
1409 }else if( strcmp(z,"-html")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001410 data.mode = MODE_Html;
drh22fbcb82004-02-01 01:22:50 +00001411 }else if( strcmp(z,"-list")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001412 data.mode = MODE_List;
drh22fbcb82004-02-01 01:22:50 +00001413 }else if( strcmp(z,"-line")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001414 data.mode = MODE_Line;
drh22fbcb82004-02-01 01:22:50 +00001415 }else if( strcmp(z,"-column")==0 ){
drh8b32e172002-04-08 02:42:57 +00001416 data.mode = MODE_Column;
drh22fbcb82004-02-01 01:22:50 +00001417 }else if( strcmp(z,"-separator")==0 ){
1418 i++;
1419 sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[i]);
1420 }else if( strcmp(z,"-nullvalue")==0 ){
1421 i++;
1422 sprintf(data.nullvalue,"%.*s",(int)sizeof(data.nullvalue)-1,argv[i]);
1423 }else if( strcmp(z,"-header")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001424 data.showHeader = 1;
drh22fbcb82004-02-01 01:22:50 +00001425 }else if( strcmp(z,"-noheader")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001426 data.showHeader = 0;
drh22fbcb82004-02-01 01:22:50 +00001427 }else if( strcmp(z,"-echo")==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001428 data.echoOn = 1;
drh22fbcb82004-02-01 01:22:50 +00001429 }else if( strcmp(z,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001430 printf("%s\n", sqlite3_version);
drhe1e38c42003-05-04 18:30:59 +00001431 return 1;
drh22fbcb82004-02-01 01:22:50 +00001432 }else if( strcmp(z,"-help")==0 ){
drhe1e38c42003-05-04 18:30:59 +00001433 usage(1);
drh1e5d0e92000-05-31 23:33:17 +00001434 }else{
drh22fbcb82004-02-01 01:22:50 +00001435 fprintf(stderr,"%s: unknown option: %s\n", Argv0, z);
drhe1e38c42003-05-04 18:30:59 +00001436 fprintf(stderr,"Use -help for a list of options.\n");
drh1e5d0e92000-05-31 23:33:17 +00001437 return 1;
1438 }
1439 }
drh44c2eb12003-04-30 11:38:26 +00001440
drh22fbcb82004-02-01 01:22:50 +00001441 if( zFirstCmd ){
drh44c2eb12003-04-30 11:38:26 +00001442 /* Run just the command that follows the database name
1443 */
drh22fbcb82004-02-01 01:22:50 +00001444 if( zFirstCmd[0]=='.' ){
1445 do_meta_command(zFirstCmd, &data);
drh6ff13852001-11-25 13:18:23 +00001446 exit(0);
1447 }else{
1448 int rc;
drh44c2eb12003-04-30 11:38:26 +00001449 open_db(&data);
danielk19776f8a5032004-05-10 10:34:51 +00001450 rc = sqlite3_exec(data.db, zFirstCmd, callback, &data, &zErrMsg);
drh6ff13852001-11-25 13:18:23 +00001451 if( rc!=0 && zErrMsg!=0 ){
1452 fprintf(stderr,"SQL error: %s\n", zErrMsg);
1453 exit(1);
1454 }
drh75897232000-05-29 14:26:00 +00001455 }
1456 }else{
drh44c2eb12003-04-30 11:38:26 +00001457 /* Run commands received from standard input
1458 */
drh5cf590c2003-04-24 01:45:04 +00001459 if( isatty(fileno(stdout)) && isatty(fileno(stdin)) ){
drh67505e72002-04-19 12:34:06 +00001460 char *zHome;
1461 char *zHistory = 0;
drh75897232000-05-29 14:26:00 +00001462 printf(
drhb217a572000-08-22 13:40:18 +00001463 "SQLite version %s\n"
1464 "Enter \".help\" for instructions\n",
danielk19776f8a5032004-05-10 10:34:51 +00001465 sqlite3_version
drh75897232000-05-29 14:26:00 +00001466 );
drh67505e72002-04-19 12:34:06 +00001467 zHome = find_home_dir();
1468 if( zHome && (zHistory = malloc(strlen(zHome)+20))!=0 ){
1469 sprintf(zHistory,"%s/.sqlite_history", zHome);
1470 }
1471 if( zHistory ) read_history(zHistory);
drhdaffd0e2001-04-11 14:28:42 +00001472 process_input(&data, 0);
drh67505e72002-04-19 12:34:06 +00001473 if( zHistory ){
1474 stifle_history(100);
1475 write_history(zHistory);
1476 }
drhdaffd0e2001-04-11 14:28:42 +00001477 }else{
1478 process_input(&data, stdin);
drh75897232000-05-29 14:26:00 +00001479 }
1480 }
drh33048c02001-10-01 14:29:22 +00001481 set_table_name(&data, 0);
danielk19776f8a5032004-05-10 10:34:51 +00001482 if( db ) sqlite3_close(db);
drh75897232000-05-29 14:26:00 +00001483 return 0;
1484}