blob: 6d261a844c4e1a2ae128664d68432d6abe9f96cd [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**
jplyon6a65bb32003-05-04 07:25:57 +000015** $Id: shell.c,v 1.76 2003/05/04 07:25:58 jplyon Exp $
drh75897232000-05-29 14:26:00 +000016*/
17#include <stdlib.h>
18#include <string.h>
19#include <stdio.h>
20#include "sqlite.h"
jplyon6a65bb32003-05-04 07:25:57 +000021#include "sqliteInt.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
drh5e00f6c2001-09-13 13:46:56 +000044# define readline(p) getline(p,stdin)
persicom1d0b8722002-04-18 02:53:04 +000045# define add_history(X)
drh67505e72002-04-19 12:34:06 +000046# define read_history(X)
47# define write_history(X)
48# define stifle_history(X)
drh75897232000-05-29 14:26:00 +000049#endif
50
drh4328c8b2003-04-26 02:50:11 +000051/* Make sure isatty() has a prototype.
52*/
53extern int isatty();
54
drh75897232000-05-29 14:26:00 +000055/*
drh4c504392000-10-16 22:06:40 +000056** The following is the open SQLite database. We make a pointer
57** to this database a static variable so that it can be accessed
58** by the SIGINT handler to interrupt database processing.
59*/
60static sqlite *db = 0;
61
62/*
drh67505e72002-04-19 12:34:06 +000063** True if an interrupt (Control-C) has been received.
64*/
65static int seenInterrupt = 0;
66
67/*
persicom7e2dfdd2002-04-18 02:46:52 +000068** This is the name of our program. It is set in main(), used
69** in a number of other places, mostly for error messages.
70*/
71static char *Argv0;
72
73/*
74** Prompt strings. Initialized in main. Settable with
75** .prompt main continue
76*/
77static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
78static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
79
drh44c2eb12003-04-30 11:38:26 +000080
persicom7e2dfdd2002-04-18 02:46:52 +000081/*
drh83965662003-04-17 02:54:13 +000082** Determines if a string is a number of not.
83*/
84extern int sqliteIsNumber(const char*);
85
86/*
drh8e7e7a22000-05-30 18:45:23 +000087** This routine reads a line of text from standard input, stores
88** the text in memory obtained from malloc() and returns a pointer
89** to the text. NULL is returned at end of file, or if malloc()
90** fails.
91**
92** The interface is like "readline" but no command-line editing
93** is done.
94*/
drhdaffd0e2001-04-11 14:28:42 +000095static char *getline(char *zPrompt, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +000096 char *zLine;
97 int nLine;
drh8e7e7a22000-05-30 18:45:23 +000098 int n;
99 int eol;
100
101 if( zPrompt && *zPrompt ){
102 printf("%s",zPrompt);
103 fflush(stdout);
104 }
105 nLine = 100;
106 zLine = malloc( nLine );
107 if( zLine==0 ) return 0;
108 n = 0;
109 eol = 0;
110 while( !eol ){
111 if( n+100>nLine ){
112 nLine = nLine*2 + 100;
113 zLine = realloc(zLine, nLine);
114 if( zLine==0 ) return 0;
115 }
drhdaffd0e2001-04-11 14:28:42 +0000116 if( fgets(&zLine[n], nLine - n, in)==0 ){
drh8e7e7a22000-05-30 18:45:23 +0000117 if( n==0 ){
118 free(zLine);
119 return 0;
120 }
121 zLine[n] = 0;
122 eol = 1;
123 break;
124 }
125 while( zLine[n] ){ n++; }
126 if( n>0 && zLine[n-1]=='\n' ){
127 n--;
128 zLine[n] = 0;
129 eol = 1;
130 }
131 }
132 zLine = realloc( zLine, n+1 );
133 return zLine;
134}
135
136/*
137** Retrieve a single line of input text. "isatty" is true if text
138** is coming from a terminal. In that case, we issue a prompt and
139** attempt to use "readline" for command-line editing. If "isatty"
drhaacc5432002-01-06 17:07:40 +0000140** is false, use "getline" instead of "readline" and issue no prompt.
drh8e7e7a22000-05-30 18:45:23 +0000141**
142** zPrior is a string of prior text retrieved. If not the empty
143** string, then issue a continuation prompt.
144*/
drhdaffd0e2001-04-11 14:28:42 +0000145static char *one_input_line(const char *zPrior, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +0000146 char *zPrompt;
147 char *zResult;
drhdaffd0e2001-04-11 14:28:42 +0000148 if( in!=0 ){
149 return getline(0, in);
drh8e7e7a22000-05-30 18:45:23 +0000150 }
151 if( zPrior && zPrior[0] ){
persicom7e2dfdd2002-04-18 02:46:52 +0000152 zPrompt = continuePrompt;
drh8e7e7a22000-05-30 18:45:23 +0000153 }else{
persicom7e2dfdd2002-04-18 02:46:52 +0000154 zPrompt = mainPrompt;
drh8e7e7a22000-05-30 18:45:23 +0000155 }
156 zResult = readline(zPrompt);
drh2dfbbca2000-07-28 14:32:48 +0000157 if( zResult ) add_history(zResult);
drh8e7e7a22000-05-30 18:45:23 +0000158 return zResult;
159}
160
persicom7e2dfdd2002-04-18 02:46:52 +0000161struct previous_mode_data {
162 int valid; /* Is there legit data in here? */
163 int mode;
164 int showHeader;
165 int colWidth[100];
166};
drh8e7e7a22000-05-30 18:45:23 +0000167/*
drh75897232000-05-29 14:26:00 +0000168** An pointer to an instance of this structure is passed from
169** the main program to the callback. This is used to communicate
170** state and mode information.
171*/
172struct callback_data {
drh28bd4bc2000-06-15 15:57:22 +0000173 sqlite *db; /* The database */
drhdaffd0e2001-04-11 14:28:42 +0000174 int echoOn; /* True to echo input commands */
drh28bd4bc2000-06-15 15:57:22 +0000175 int cnt; /* Number of records displayed so far */
176 FILE *out; /* Write results here */
177 int mode; /* An output mode setting */
178 int showHeader; /* True to show column names in List or Column mode */
drh33048c02001-10-01 14:29:22 +0000179 char *zDestTable; /* Name of destination table when MODE_Insert */
drh28bd4bc2000-06-15 15:57:22 +0000180 char separator[20]; /* Separator character for MODE_List */
drha0c66f52000-07-29 13:20:21 +0000181 int colWidth[100]; /* Requested width of each column when in column mode*/
182 int actualWidth[100]; /* Actual width of each column */
drh83965662003-04-17 02:54:13 +0000183 char nullvalue[20]; /* The text to print when a NULL comes back from
184 ** the database */
persicom7e2dfdd2002-04-18 02:46:52 +0000185 struct previous_mode_data explainPrev;
drh83965662003-04-17 02:54:13 +0000186 /* Holds the mode information just before
187 ** .explain ON */
drh44c2eb12003-04-30 11:38:26 +0000188 char outfile[FILENAME_MAX]; /* Filename for *out */
189 const char *zDbFilename; /* name of the database file */
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 */
persicom7e2dfdd2002-04-18 02:46:52 +0000201#define MODE_NUM_OF 6
202
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;
drh4c504392000-10-16 22:06:40 +0000274 if( db ) sqlite_interrupt(db);
275}
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);
drh83965662003-04-17 02:54:13 +0000395 }else if( sqliteIsNumber(azArg[i]) ){
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]);
persicom1d0b8722002-04-18 02:53:04 +0000463 sqlite_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"
drh75897232000-05-29 14:26:00 +0000493 ".schema ?TABLE? Show the CREATE statements\n"
494 ".separator STRING Change separator string for \"list\" mode\n"
drhdd45df82002-04-18 12:39:03 +0000495 ".show Show the current values for various settings\n"
drha50da102000-08-08 20:19:09 +0000496 ".tables ?PATTERN? List names of tables matching a pattern\n"
drh2dfbbca2000-07-28 14:32:48 +0000497 ".timeout MS Try opening locked tables for MS milliseconds\n"
drh75897232000-05-29 14:26:00 +0000498 ".width NUM NUM ... Set column widths for \"column\" mode\n"
499;
500
drhdaffd0e2001-04-11 14:28:42 +0000501/* Forward reference */
502static void process_input(struct callback_data *p, FILE *in);
503
drh75897232000-05-29 14:26:00 +0000504/*
drh44c2eb12003-04-30 11:38:26 +0000505** Make sure the database is open. If it is not, then open it. If
506** the database fails to open, print an error message and exit.
507*/
508static void open_db(struct callback_data *p){
509 if( p->db==0 ){
510 char *zErrMsg = 0;
511 p->db = db = sqlite_open(p->zDbFilename, 0666, &zErrMsg);
512 if( db==0 ){
513 p->db = db = sqlite_open(p->zDbFilename, 0444, &zErrMsg);
514 if( db==0 ){
515 if( zErrMsg ){
516 fprintf(stderr,"Unable to open database \"%s\": %s\n",
517 p->zDbFilename, zErrMsg);
518 }else{
519 fprintf(stderr,"Unable to open database %s\n", p->zDbFilename);
520 }
521 exit(1);
522 }else{
523 fprintf(stderr,"Database \"%s\" opened READ ONLY!\n", p->zDbFilename);
524 }
525 }
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++; }
546 if( zLine[i]=='\'' || zLine[i]=='"' ){
547 int delim = zLine[i++];
548 azArg[nArg++] = &zLine[i];
549 while( zLine[i] && zLine[i]!=delim ){ i++; }
550 if( zLine[i]==delim ){
551 zLine[i++] = 0;
552 }
553 }else{
554 azArg[nArg++] = &zLine[i];
555 while( zLine[i] && !isspace(zLine[i]) ){ i++; }
556 if( zLine[i] ) zLine[i++] = 0;
557 }
558 }
559
560 /* Process the input line.
561 */
drh67505e72002-04-19 12:34:06 +0000562 if( nArg==0 ) return rc;
drh75897232000-05-29 14:26:00 +0000563 n = strlen(azArg[0]);
564 c = azArg[0][0];
jplyon6a65bb32003-05-04 07:25:57 +0000565 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
566 int i;
567 open_db(p);
568 fprintf(p->out, "[Name]\t[File]\n");
569 for(i=0; i<db->nDb; i++){
570 sqlite *db = p->db;
571 Db *pDb = &db->aDb[i];
572 BtOps* pOps = btOps(pDb->pBt);
573 const char *zName = pDb->zName;
574 const char *zFilename = pOps->GetFilename(pDb->pBt);
575 fprintf(p->out, "%s\t%s\n", zName, zFilename);
576 }
577 }else
578
drh4c653a02000-06-07 01:27:47 +0000579 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
580 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000581 open_db(p);
drh33048c02001-10-01 14:29:22 +0000582 fprintf(p->out, "BEGIN TRANSACTION;\n");
drh4c653a02000-06-07 01:27:47 +0000583 if( nArg==1 ){
drh44c2eb12003-04-30 11:38:26 +0000584 sqlite_exec(p->db,
drha18c5682000-10-08 22:20:57 +0000585 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000586 "WHERE type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000587 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000588 dump_callback, p, &zErrMsg
589 );
drh4c653a02000-06-07 01:27:47 +0000590 }else{
591 int i;
592 for(i=1; i<nArg && zErrMsg==0; i++){
drh44c2eb12003-04-30 11:38:26 +0000593 sqlite_exec_printf(p->db,
drha18c5682000-10-08 22:20:57 +0000594 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000595 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000596 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000597 dump_callback, p, &zErrMsg, azArg[i]
598 );
drh4c653a02000-06-07 01:27:47 +0000599 }
600 }
601 if( zErrMsg ){
602 fprintf(stderr,"Error: %s\n", zErrMsg);
603 free(zErrMsg);
drh33048c02001-10-01 14:29:22 +0000604 }else{
605 fprintf(p->out, "COMMIT;\n");
drh4c653a02000-06-07 01:27:47 +0000606 }
607 }else
drh75897232000-05-29 14:26:00 +0000608
drhdaffd0e2001-04-11 14:28:42 +0000609 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
610 int j;
611 char *z = azArg[1];
612 int val = atoi(azArg[1]);
613 for(j=0; z[j]; j++){
614 if( isupper(z[j]) ) z[j] = tolower(z[j]);
615 }
616 if( strcmp(z,"on")==0 ){
617 val = 1;
618 }else if( strcmp(z,"yes")==0 ){
619 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000620 }
drhdaffd0e2001-04-11 14:28:42 +0000621 p->echoOn = val;
622 }else
623
drh75897232000-05-29 14:26:00 +0000624 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
drh67505e72002-04-19 12:34:06 +0000625 rc = 1;
drh75897232000-05-29 14:26:00 +0000626 }else
627
drhdd45df82002-04-18 12:39:03 +0000628 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +0000629 int j;
drhdd45df82002-04-18 12:39:03 +0000630 char *z = nArg>=2 ? azArg[1] : "1";
631 int val = atoi(z);
persicom7e2dfdd2002-04-18 02:46:52 +0000632 for(j=0; z[j]; j++){
633 if( isupper(z[j]) ) z[j] = tolower(z[j]);
634 }
635 if( strcmp(z,"on")==0 ){
636 val = 1;
637 }else if( strcmp(z,"yes")==0 ){
638 val = 1;
639 }
640 if(val == 1) {
641 if(!p->explainPrev.valid) {
642 p->explainPrev.valid = 1;
643 p->explainPrev.mode = p->mode;
644 p->explainPrev.showHeader = p->showHeader;
645 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
646 }
647 /* We could put this code under the !p->explainValid
648 ** condition so that it does not execute if we are already in
649 ** explain mode. However, always executing it allows us an easy
650 ** was to reset to explain mode in case the user previously
651 ** did an .explain followed by a .width, .mode or .header
652 ** command.
653 */
654 p->mode = MODE_Column;
655 p->showHeader = 1;
656 memset(p->colWidth,0,ArraySize(p->colWidth));
657 p->colWidth[0] = 4;
658 p->colWidth[1] = 12;
659 p->colWidth[2] = 10;
660 p->colWidth[3] = 10;
661 p->colWidth[4] = 35;
662 }else if (p->explainPrev.valid) {
663 p->explainPrev.valid = 0;
664 p->mode = p->explainPrev.mode;
665 p->showHeader = p->explainPrev.showHeader;
666 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
667 }
drh75897232000-05-29 14:26:00 +0000668 }else
669
persicom7e2dfdd2002-04-18 02:46:52 +0000670 if( c=='h' && (strncmp(azArg[0], "header", n)==0
671 ||
672 strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){
drh75897232000-05-29 14:26:00 +0000673 int j;
674 char *z = azArg[1];
675 int val = atoi(azArg[1]);
676 for(j=0; z[j]; j++){
677 if( isupper(z[j]) ) z[j] = tolower(z[j]);
678 }
679 if( strcmp(z,"on")==0 ){
680 val = 1;
681 }else if( strcmp(z,"yes")==0 ){
682 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000683 }
drh75897232000-05-29 14:26:00 +0000684 p->showHeader = val;
685 }else
686
687 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
688 fprintf(stderr,zHelp);
689 }else
690
691 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
692 struct callback_data data;
693 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000694 open_db(p);
drh75897232000-05-29 14:26:00 +0000695 memcpy(&data, p, sizeof(data));
696 data.showHeader = 0;
697 data.mode = MODE_List;
drh44c2eb12003-04-30 11:38:26 +0000698 sqlite_exec_printf(p->db,
drha18c5682000-10-08 22:20:57 +0000699 "SELECT name FROM sqlite_master "
700 "WHERE type='index' AND tbl_name LIKE '%q' "
drhe0bc4042002-06-25 01:09:11 +0000701 "UNION ALL "
702 "SELECT name FROM sqlite_temp_master "
703 "WHERE type='index' AND tbl_name LIKE '%q' "
704 "ORDER BY 1",
705 callback, &data, &zErrMsg, azArg[1], azArg[1]
drha18c5682000-10-08 22:20:57 +0000706 );
drh75897232000-05-29 14:26:00 +0000707 if( zErrMsg ){
708 fprintf(stderr,"Error: %s\n", zErrMsg);
709 free(zErrMsg);
710 }
711 }else
712
drh28bd4bc2000-06-15 15:57:22 +0000713 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
drh75897232000-05-29 14:26:00 +0000714 int n2 = strlen(azArg[1]);
persicom7e2dfdd2002-04-18 02:46:52 +0000715 if( strncmp(azArg[1],"line",n2)==0
716 ||
717 strncmp(azArg[1],"lines",n2)==0 ){
drh75897232000-05-29 14:26:00 +0000718 p->mode = MODE_Line;
persicom7e2dfdd2002-04-18 02:46:52 +0000719 }else if( strncmp(azArg[1],"column",n2)==0
720 ||
721 strncmp(azArg[1],"columns",n2)==0 ){
drh75897232000-05-29 14:26:00 +0000722 p->mode = MODE_Column;
723 }else if( strncmp(azArg[1],"list",n2)==0 ){
724 p->mode = MODE_List;
drh1e5d0e92000-05-31 23:33:17 +0000725 }else if( strncmp(azArg[1],"html",n2)==0 ){
726 p->mode = MODE_Html;
drh28bd4bc2000-06-15 15:57:22 +0000727 }else if( strncmp(azArg[1],"insert",n2)==0 ){
728 p->mode = MODE_Insert;
729 if( nArg>=3 ){
drh33048c02001-10-01 14:29:22 +0000730 set_table_name(p, azArg[2]);
drh28bd4bc2000-06-15 15:57:22 +0000731 }else{
drh33048c02001-10-01 14:29:22 +0000732 set_table_name(p, "table");
drh28bd4bc2000-06-15 15:57:22 +0000733 }
drhdaffd0e2001-04-11 14:28:42 +0000734 }else {
735 fprintf(stderr,"mode should be on of: column html insert line list\n");
drh75897232000-05-29 14:26:00 +0000736 }
737 }else
738
persicom7e2dfdd2002-04-18 02:46:52 +0000739 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
740 sprintf(p->nullvalue, "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
741 }else
742
drh75897232000-05-29 14:26:00 +0000743 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
744 if( p->out!=stdout ){
745 fclose(p->out);
746 }
747 if( strcmp(azArg[1],"stdout")==0 ){
748 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +0000749 strcpy(p->outfile,"stdout");
drh75897232000-05-29 14:26:00 +0000750 }else{
751 p->out = fopen(azArg[1], "w");
752 if( p->out==0 ){
753 fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
754 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +0000755 } else {
756 strcpy(p->outfile,azArg[1]);
drh75897232000-05-29 14:26:00 +0000757 }
758 }
759 }else
760
drhdd45df82002-04-18 12:39:03 +0000761 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
persicom7e2dfdd2002-04-18 02:46:52 +0000762 if( nArg >= 2) {
763 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
764 }
765 if( nArg >= 3) {
766 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
767 }
768 }else
769
770 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
drh44c2eb12003-04-30 11:38:26 +0000771 sqlite_close(p->db);
persicom7e2dfdd2002-04-18 02:46:52 +0000772 exit(0);
773 }else
774
drhdaffd0e2001-04-11 14:28:42 +0000775 if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
776 FILE *alt = fopen(azArg[1], "r");
777 if( alt==0 ){
778 fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
779 }else{
780 process_input(p, alt);
781 fclose(alt);
782 }
783 }else
784
drh75897232000-05-29 14:26:00 +0000785 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
786 struct callback_data data;
787 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000788 open_db(p);
drh75897232000-05-29 14:26:00 +0000789 memcpy(&data, p, sizeof(data));
790 data.showHeader = 0;
drhe3710332000-09-29 13:30:53 +0000791 data.mode = MODE_Semi;
drh75897232000-05-29 14:26:00 +0000792 if( nArg>1 ){
drhff9821a2001-04-04 21:22:14 +0000793 extern int sqliteStrICmp(const char*,const char*);
drha18c5682000-10-08 22:20:57 +0000794 if( sqliteStrICmp(azArg[1],"sqlite_master")==0 ){
795 char *new_argv[2], *new_colv[2];
796 new_argv[0] = "CREATE TABLE sqlite_master (\n"
797 " type text,\n"
798 " name text,\n"
799 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000800 " rootpage integer,\n"
drha18c5682000-10-08 22:20:57 +0000801 " sql text\n"
802 ")";
803 new_argv[1] = 0;
804 new_colv[0] = "sql";
805 new_colv[1] = 0;
806 callback(&data, 1, new_argv, new_colv);
drhe0bc4042002-06-25 01:09:11 +0000807 }else if( sqliteStrICmp(azArg[1],"sqlite_temp_master")==0 ){
808 char *new_argv[2], *new_colv[2];
809 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
810 " type text,\n"
811 " name text,\n"
812 " tbl_name text,\n"
813 " rootpage integer,\n"
814 " sql text\n"
815 ")";
816 new_argv[1] = 0;
817 new_colv[0] = "sql";
818 new_colv[1] = 0;
819 callback(&data, 1, new_argv, new_colv);
drha18c5682000-10-08 22:20:57 +0000820 }else{
drh44c2eb12003-04-30 11:38:26 +0000821 sqlite_exec_printf(p->db,
drhe0bc4042002-06-25 01:09:11 +0000822 "SELECT sql FROM "
823 " (SELECT * FROM sqlite_master UNION ALL"
824 " SELECT * FROM sqlite_temp_master) "
drh33048c02001-10-01 14:29:22 +0000825 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +0000826 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000827 callback, &data, &zErrMsg, azArg[1]);
828 }
drh75897232000-05-29 14:26:00 +0000829 }else{
drh44c2eb12003-04-30 11:38:26 +0000830 sqlite_exec(p->db,
drhe0bc4042002-06-25 01:09:11 +0000831 "SELECT sql FROM "
832 " (SELECT * FROM sqlite_master UNION ALL"
833 " SELECT * FROM sqlite_temp_master) "
drh33048c02001-10-01 14:29:22 +0000834 "WHERE type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +0000835 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000836 callback, &data, &zErrMsg
837 );
drh75897232000-05-29 14:26:00 +0000838 }
drh75897232000-05-29 14:26:00 +0000839 if( zErrMsg ){
840 fprintf(stderr,"Error: %s\n", zErrMsg);
841 free(zErrMsg);
842 }
843 }else
844
845 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
846 sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
847 }else
848
persicom7e2dfdd2002-04-18 02:46:52 +0000849 if( c=='s' && strncmp(azArg[0], "show", n)==0){
850 int i;
851 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
drh67505e72002-04-19 12:34:06 +0000852 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
drhdd45df82002-04-18 12:39:03 +0000853 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
persicom7e2dfdd2002-04-18 02:46:52 +0000854 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
855 fprintf(p->out,"%9.9s: %s\n","nullvalue", p->nullvalue);
drh67505e72002-04-19 12:34:06 +0000856 fprintf(p->out,"%9.9s: %s\n","output",
857 strlen(p->outfile) ? p->outfile : "stdout");
persicom7e2dfdd2002-04-18 02:46:52 +0000858 fprintf(p->out,"%9.9s: %s\n","separator", p->separator);
859 fprintf(p->out,"%9.9s: ","width");
860 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
861 fprintf(p->out,"%d ",p->colWidth[i]);
862 }
863 fprintf(p->out,"\n\n");
864 }else
865
drh2dfbbca2000-07-28 14:32:48 +0000866 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
drhe3710332000-09-29 13:30:53 +0000867 char **azResult;
868 int nRow, rc;
869 char *zErrMsg;
drh44c2eb12003-04-30 11:38:26 +0000870 open_db(p);
drha50da102000-08-08 20:19:09 +0000871 if( nArg==1 ){
drh44c2eb12003-04-30 11:38:26 +0000872 rc = sqlite_get_table(p->db,
drha50da102000-08-08 20:19:09 +0000873 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +0000874 "WHERE type IN ('table','view') "
drhe0bc4042002-06-25 01:09:11 +0000875 "UNION ALL "
876 "SELECT name FROM sqlite_temp_master "
877 "WHERE type IN ('table','view') "
878 "ORDER BY 1",
drha18c5682000-10-08 22:20:57 +0000879 &azResult, &nRow, 0, &zErrMsg
880 );
drha50da102000-08-08 20:19:09 +0000881 }else{
drh44c2eb12003-04-30 11:38:26 +0000882 rc = sqlite_get_table_printf(p->db,
drha50da102000-08-08 20:19:09 +0000883 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +0000884 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
drhe0bc4042002-06-25 01:09:11 +0000885 "UNION ALL "
886 "SELECT name FROM sqlite_temp_master "
887 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
888 "ORDER BY 1",
889 &azResult, &nRow, 0, &zErrMsg, azArg[1], azArg[1]
drha18c5682000-10-08 22:20:57 +0000890 );
drha50da102000-08-08 20:19:09 +0000891 }
drh75897232000-05-29 14:26:00 +0000892 if( zErrMsg ){
893 fprintf(stderr,"Error: %s\n", zErrMsg);
894 free(zErrMsg);
895 }
drhe3710332000-09-29 13:30:53 +0000896 if( rc==SQLITE_OK ){
897 int len, maxlen = 0;
898 int i, j;
899 int nPrintCol, nPrintRow;
900 for(i=1; i<=nRow; i++){
901 if( azResult[i]==0 ) continue;
902 len = strlen(azResult[i]);
903 if( len>maxlen ) maxlen = len;
904 }
905 nPrintCol = 80/(maxlen+2);
906 if( nPrintCol<1 ) nPrintCol = 1;
907 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
908 for(i=0; i<nPrintRow; i++){
909 for(j=i+1; j<=nRow; j+=nPrintRow){
910 char *zSp = j<=nPrintRow ? "" : " ";
911 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
912 }
913 printf("\n");
914 }
915 }
916 sqlite_free_table(azResult);
drh75897232000-05-29 14:26:00 +0000917 }else
918
drh2dfbbca2000-07-28 14:32:48 +0000919 if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
drh44c2eb12003-04-30 11:38:26 +0000920 open_db(p);
921 sqlite_busy_timeout(p->db, atoi(azArg[1]));
drh2dfbbca2000-07-28 14:32:48 +0000922 }else
923
drh75897232000-05-29 14:26:00 +0000924 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
925 int j;
926 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
927 p->colWidth[j-1] = atoi(azArg[j]);
928 }
929 }else
930
931 {
drh67505e72002-04-19 12:34:06 +0000932 fprintf(stderr, "unknown command or invalid arguments: "
933 " \"%s\". Enter \".help\" for help\n", azArg[0]);
drh75897232000-05-29 14:26:00 +0000934 }
drh67505e72002-04-19 12:34:06 +0000935
936 return rc;
drh75897232000-05-29 14:26:00 +0000937}
938
drh67505e72002-04-19 12:34:06 +0000939/*
drh324ccef2003-02-05 14:06:20 +0000940** Return TRUE if the last non-whitespace character in z[] is a semicolon.
941** z[] is N characters long.
942*/
943static int _ends_with_semicolon(const char *z, int N){
944 while( N>0 && isspace(z[N-1]) ){ N--; }
945 return N>0 && z[N-1]==';';
946}
947
948/*
drh70c7a4b2003-04-26 03:03:06 +0000949** Test to see if a line consists entirely of whitespace.
950*/
951static int _all_whitespace(const char *z){
952 for(; *z; z++){
953 if( isspace(*z) ) continue;
954 if( *z=='/' && z[1]=='*' ){
955 z += 2;
956 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
957 if( *z==0 ) return 0;
958 z++;
959 continue;
960 }
961 if( *z=='-' && z[1]=='-' ){
962 z += 2;
963 while( *z && *z!='\n' ){ z++; }
964 if( *z==0 ) return 1;
965 continue;
966 }
967 return 0;
968 }
969 return 1;
970}
971
972/*
drha9b17162003-04-29 18:01:28 +0000973** Return TRUE if the line typed in is an SQL command terminator other
974** than a semi-colon. The SQL Server style "go" command is understood
975** as is the Oracle "/".
976*/
977static int _is_command_terminator(const char *zLine){
978 extern int sqliteStrNICmp(const char*,const char*,int);
979 while( isspace(*zLine) ){ zLine++; };
980 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ) return 1; /* Oracle */
981 if( sqliteStrNICmp(zLine,"go",2)==0 && _all_whitespace(&zLine[2]) ){
982 return 1; /* SQL Server */
983 }
984 return 0;
985}
986
987/*
drh67505e72002-04-19 12:34:06 +0000988** Read input from *in and process it. If *in==0 then input
989** is interactive - the user is typing it it. Otherwise, input
990** is coming from a file or device. A prompt is issued and history
991** is saved only if input is interactive. An interrupt signal will
992** cause this routine to exit immediately, unless input is interactive.
993*/
drhdaffd0e2001-04-11 14:28:42 +0000994static void process_input(struct callback_data *p, FILE *in){
995 char *zLine;
996 char *zSql = 0;
997 int nSql = 0;
998 char *zErrMsg;
drh7f953e22002-07-13 17:33:45 +0000999 int rc;
drh41e941d2002-04-04 15:10:12 +00001000 while( fflush(p->out), (zLine = one_input_line(zSql, in))!=0 ){
drh67505e72002-04-19 12:34:06 +00001001 if( seenInterrupt ){
1002 if( in!=0 ) break;
1003 seenInterrupt = 0;
1004 }
drhdaffd0e2001-04-11 14:28:42 +00001005 if( p->echoOn ) printf("%s\n", zLine);
drh70c7a4b2003-04-26 03:03:06 +00001006 if( _all_whitespace(zLine) ) continue;
drh2af0b2d2002-02-21 02:25:02 +00001007 if( zLine && zLine[0]=='.' && nSql==0 ){
drh44c2eb12003-04-30 11:38:26 +00001008 int rc = do_meta_command(zLine, p);
drhdaffd0e2001-04-11 14:28:42 +00001009 free(zLine);
drh67505e72002-04-19 12:34:06 +00001010 if( rc ) break;
drhdaffd0e2001-04-11 14:28:42 +00001011 continue;
1012 }
drha9b17162003-04-29 18:01:28 +00001013 if( _is_command_terminator(zLine) ){
1014 strcpy(zLine,";");
1015 }
drhdaffd0e2001-04-11 14:28:42 +00001016 if( zSql==0 ){
1017 int i;
1018 for(i=0; zLine[i] && isspace(zLine[i]); i++){}
1019 if( zLine[i]!=0 ){
1020 nSql = strlen(zLine);
1021 zSql = malloc( nSql+1 );
1022 strcpy(zSql, zLine);
1023 }
1024 }else{
1025 int len = strlen(zLine);
1026 zSql = realloc( zSql, nSql + len + 2 );
1027 if( zSql==0 ){
1028 fprintf(stderr,"%s: out of memory!\n", Argv0);
1029 exit(1);
1030 }
1031 strcpy(&zSql[nSql++], "\n");
1032 strcpy(&zSql[nSql], zLine);
1033 nSql += len;
1034 }
1035 free(zLine);
drh324ccef2003-02-05 14:06:20 +00001036 if( zSql && _ends_with_semicolon(zSql, nSql) && sqlite_complete(zSql) ){
drhdaffd0e2001-04-11 14:28:42 +00001037 p->cnt = 0;
drh44c2eb12003-04-30 11:38:26 +00001038 open_db(p);
1039 rc = sqlite_exec(p->db, zSql, callback, p, &zErrMsg);
drh7f953e22002-07-13 17:33:45 +00001040 if( rc || zErrMsg ){
drhdaffd0e2001-04-11 14:28:42 +00001041 if( in!=0 && !p->echoOn ) printf("%s\n",zSql);
drh7f953e22002-07-13 17:33:45 +00001042 if( zErrMsg!=0 ){
1043 printf("SQL error: %s\n", zErrMsg);
1044 free(zErrMsg);
1045 zErrMsg = 0;
1046 }else{
1047 printf("SQL error: %s\n", sqlite_error_string(rc));
1048 }
drhdaffd0e2001-04-11 14:28:42 +00001049 }
1050 free(zSql);
1051 zSql = 0;
1052 nSql = 0;
1053 }
1054 }
1055 if( zSql ){
drh70c7a4b2003-04-26 03:03:06 +00001056 if( !_all_whitespace(zSql) ) printf("Incomplete SQL: %s\n", zSql);
drhdaffd0e2001-04-11 14:28:42 +00001057 free(zSql);
1058 }
1059}
1060
drh67505e72002-04-19 12:34:06 +00001061/*
1062** Return a pathname which is the user's home directory. A
1063** 0 return indicates an error of some kind. Space to hold the
1064** resulting string is obtained from malloc(). The calling
1065** function should free the result.
1066*/
1067static char *find_home_dir(void){
1068 char *home_dir = NULL;
persicom7e2dfdd2002-04-18 02:46:52 +00001069
drh820f3812003-01-08 13:02:52 +00001070#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
drh67505e72002-04-19 12:34:06 +00001071 struct passwd *pwent;
1072 uid_t uid = getuid();
drhbd842ba2002-08-21 11:26:41 +00001073 if( (pwent=getpwuid(uid)) != NULL) {
1074 home_dir = pwent->pw_dir;
drh67505e72002-04-19 12:34:06 +00001075 }
1076#endif
1077
drh820f3812003-01-08 13:02:52 +00001078#ifdef __MACOS__
1079 char home_path[_MAX_PATH+1];
1080 home_dir = getcwd(home_path, _MAX_PATH);
1081#endif
1082
drh67505e72002-04-19 12:34:06 +00001083 if (!home_dir) {
1084 home_dir = getenv("HOME");
1085 if (!home_dir) {
1086 home_dir = getenv("HOMEPATH"); /* Windows? */
1087 }
1088 }
1089
drhe98d4fa2002-04-21 19:06:22 +00001090#if defined(_WIN32) || defined(WIN32)
1091 if (!home_dir) {
1092 home_dir = "c:";
1093 }
1094#endif
1095
drh67505e72002-04-19 12:34:06 +00001096 if( home_dir ){
1097 char *z = malloc( strlen(home_dir)+1 );
1098 if( z ) strcpy(z, home_dir);
1099 home_dir = z;
1100 }
drhe98d4fa2002-04-21 19:06:22 +00001101
drh67505e72002-04-19 12:34:06 +00001102 return home_dir;
1103}
1104
1105/*
1106** Read input from the file given by sqliterc_override. Or if that
1107** parameter is NULL, take input from ~/.sqliterc
1108*/
1109static void process_sqliterc(struct callback_data *p, char *sqliterc_override){
persicom7e2dfdd2002-04-18 02:46:52 +00001110 char *home_dir = NULL;
1111 char *sqliterc = sqliterc_override;
persicom7e2dfdd2002-04-18 02:46:52 +00001112 FILE *in = NULL;
1113
1114 if (sqliterc == NULL) {
drh67505e72002-04-19 12:34:06 +00001115 home_dir = find_home_dir();
drhe98d4fa2002-04-21 19:06:22 +00001116 if( home_dir==0 ){
1117 fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0);
1118 return;
1119 }
1120 sqliterc = malloc(strlen(home_dir) + 15);
persicom7e2dfdd2002-04-18 02:46:52 +00001121 if( sqliterc==0 ){
1122 fprintf(stderr,"%s: out of memory!\n", Argv0);
1123 exit(1);
1124 }
1125 sprintf(sqliterc,"%s/.sqliterc",home_dir);
drh67505e72002-04-19 12:34:06 +00001126 free(home_dir);
persicom7e2dfdd2002-04-18 02:46:52 +00001127 }
1128 in = fopen(sqliterc,"r");
drh4328c8b2003-04-26 02:50:11 +00001129 if(in && isatty(fileno(stdout))) {
persicom7e2dfdd2002-04-18 02:46:52 +00001130 printf("Loading resources from %s\n",sqliterc);
1131 process_input(p,in);
drhdd45df82002-04-18 12:39:03 +00001132 fclose(in);
persicom7e2dfdd2002-04-18 02:46:52 +00001133 }
1134 return;
1135}
1136
drh67505e72002-04-19 12:34:06 +00001137/*
1138** Initialize the state information in data
1139*/
persicom7e2dfdd2002-04-18 02:46:52 +00001140void main_init(struct callback_data *data) {
1141 memset(data, 0, sizeof(*data));
1142 data->mode = MODE_List;
1143 strcpy(data->separator,"|");
1144 data->showHeader = 0;
1145 strcpy(mainPrompt,"sqlite> ");
1146 strcpy(continuePrompt," ...> ");
1147}
1148
drh75897232000-05-29 14:26:00 +00001149int main(int argc, char **argv){
drh75897232000-05-29 14:26:00 +00001150 char *zErrMsg = 0;
1151 struct callback_data data;
persicom7e2dfdd2002-04-18 02:46:52 +00001152 int origArgc = argc;
1153 char **origArgv = argv;
drh44c2eb12003-04-30 11:38:26 +00001154 int i;
1155 extern int sqliteOsFileExists(const char*);
drh75897232000-05-29 14:26:00 +00001156
drh820f3812003-01-08 13:02:52 +00001157#ifdef __MACOS__
1158 argc = ccommand(&argv);
1159 origArgc = argc;
1160 origArgv = argv;
1161#endif
1162
drhdaffd0e2001-04-11 14:28:42 +00001163 Argv0 = argv[0];
persicom7e2dfdd2002-04-18 02:46:52 +00001164 main_init(&data);
persicom7e2dfdd2002-04-18 02:46:52 +00001165
drh44c2eb12003-04-30 11:38:26 +00001166 /* Make sure we have a valid signal handler early, before anything
1167 ** else is done.
1168 */
drh4c504392000-10-16 22:06:40 +00001169#ifdef SIGINT
1170 signal(SIGINT, interrupt_handler);
1171#endif
drh44c2eb12003-04-30 11:38:26 +00001172
1173 /* Locate the name of the database file
1174 */
1175 for(i=1; i<argc; i++){
1176 if( argv[i][0]!='-' ) break;
1177 if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){
1178 i++;
1179 }
1180 }
1181 if( i!=argc-1 && i!=argc-2 ){
1182 fprintf(stderr,"Usage: %s ?OPTIONS? FILENAME ?SQL?\n", Argv0);
1183 exit(1);
1184 }
1185 data.zDbFilename = argv[i];
1186 data.out = stdout;
1187
1188 /* Go ahead and open the database file if it already exists. If the
1189 ** file does not exist, delay opening it. This prevents empty database
1190 ** files from being created if a user mistypes the database name argument
1191 ** to the sqlite command-line tool.
1192 */
1193 if( sqliteOsFileExists(data.zDbFilename) ){
1194 open_db(&data);
1195 }
1196
1197 /* Process the ~/.sqliterc file, if there is one
1198 */
1199 process_sqliterc(&data,NULL);
1200
1201 /* Process command-line options
1202 */
drh1e5d0e92000-05-31 23:33:17 +00001203 while( argc>=2 && argv[1][0]=='-' ){
persicom7e2dfdd2002-04-18 02:46:52 +00001204 if( argc>=3 && strcmp(argv[1],"-init")==0 ){
1205 /* If we get a -init to do, we have to pretend that
1206 ** it replaced the .sqliterc file. Soooo, in order to
1207 ** do that we need to start from scratch...*/
1208 main_init(&data);
1209
1210 /* treat this file as the sqliterc... */
1211 process_sqliterc(&data,argv[2]);
1212
1213 /* fix up the command line so we do not re-read
1214 ** the option next time around... */
1215 {
1216 int i = 1;
1217 for(i=1;i<=argc-2;i++) {
1218 argv[i] = argv[i+2];
1219 }
1220 }
1221 origArgc-=2;
1222
1223 /* and reset the command line options to be re-read.*/
1224 argv = origArgv;
1225 argc = origArgc;
1226
1227 }else if( strcmp(argv[1],"-html")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001228 data.mode = MODE_Html;
1229 argc--;
1230 argv++;
1231 }else if( strcmp(argv[1],"-list")==0 ){
1232 data.mode = MODE_List;
1233 argc--;
1234 argv++;
1235 }else if( strcmp(argv[1],"-line")==0 ){
1236 data.mode = MODE_Line;
1237 argc--;
1238 argv++;
drh8b32e172002-04-08 02:42:57 +00001239 }else if( strcmp(argv[1],"-column")==0 ){
1240 data.mode = MODE_Column;
1241 argc--;
1242 argv++;
drh7613bfa2002-01-22 12:39:24 +00001243 }else if( argc>=3 && strcmp(argv[1],"-separator")==0 ){
drhbed86902000-06-02 13:27:59 +00001244 sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[2]);
drh1e5d0e92000-05-31 23:33:17 +00001245 argc -= 2;
1246 argv += 2;
persicom7e2dfdd2002-04-18 02:46:52 +00001247 }else if( argc>=3 && strcmp(argv[1],"-nullvalue")==0 ){
1248 sprintf(data.nullvalue,"%.*s",(int)sizeof(data.nullvalue)-1,argv[2]);
1249 argc -= 2;
1250 argv += 2;
drh1e5d0e92000-05-31 23:33:17 +00001251 }else if( strcmp(argv[1],"-header")==0 ){
1252 data.showHeader = 1;
1253 argc--;
1254 argv++;
1255 }else if( strcmp(argv[1],"-noheader")==0 ){
1256 data.showHeader = 0;
1257 argc--;
1258 argv++;
drh660f68d2001-01-04 14:27:07 +00001259 }else if( strcmp(argv[1],"-echo")==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001260 data.echoOn = 1;
drh660f68d2001-01-04 14:27:07 +00001261 argc--;
1262 argv++;
drh1e5d0e92000-05-31 23:33:17 +00001263 }else{
drhdaffd0e2001-04-11 14:28:42 +00001264 fprintf(stderr,"%s: unknown option: %s\n", Argv0, argv[1]);
drh1e5d0e92000-05-31 23:33:17 +00001265 return 1;
1266 }
1267 }
drh44c2eb12003-04-30 11:38:26 +00001268
drh75897232000-05-29 14:26:00 +00001269 if( argc==3 ){
drh44c2eb12003-04-30 11:38:26 +00001270 /* Run just the command that follows the database name
1271 */
drh6ff13852001-11-25 13:18:23 +00001272 if( argv[2][0]=='.' ){
drh44c2eb12003-04-30 11:38:26 +00001273 do_meta_command(argv[2], &data);
drh6ff13852001-11-25 13:18:23 +00001274 exit(0);
1275 }else{
1276 int rc;
drh44c2eb12003-04-30 11:38:26 +00001277 open_db(&data);
drh6ff13852001-11-25 13:18:23 +00001278 rc = sqlite_exec(db, argv[2], callback, &data, &zErrMsg);
1279 if( rc!=0 && zErrMsg!=0 ){
1280 fprintf(stderr,"SQL error: %s\n", zErrMsg);
1281 exit(1);
1282 }
drh75897232000-05-29 14:26:00 +00001283 }
1284 }else{
drh44c2eb12003-04-30 11:38:26 +00001285 /* Run commands received from standard input
1286 */
drh5cf590c2003-04-24 01:45:04 +00001287 if( isatty(fileno(stdout)) && isatty(fileno(stdin)) ){
drh67505e72002-04-19 12:34:06 +00001288 char *zHome;
1289 char *zHistory = 0;
drh75897232000-05-29 14:26:00 +00001290 printf(
drhb217a572000-08-22 13:40:18 +00001291 "SQLite version %s\n"
1292 "Enter \".help\" for instructions\n",
1293 sqlite_version
drh75897232000-05-29 14:26:00 +00001294 );
drh67505e72002-04-19 12:34:06 +00001295 zHome = find_home_dir();
1296 if( zHome && (zHistory = malloc(strlen(zHome)+20))!=0 ){
1297 sprintf(zHistory,"%s/.sqlite_history", zHome);
1298 }
1299 if( zHistory ) read_history(zHistory);
drhdaffd0e2001-04-11 14:28:42 +00001300 process_input(&data, 0);
drh67505e72002-04-19 12:34:06 +00001301 if( zHistory ){
1302 stifle_history(100);
1303 write_history(zHistory);
1304 }
drhdaffd0e2001-04-11 14:28:42 +00001305 }else{
1306 process_input(&data, stdin);
drh75897232000-05-29 14:26:00 +00001307 }
1308 }
drh33048c02001-10-01 14:29:22 +00001309 set_table_name(&data, 0);
drh44c2eb12003-04-30 11:38:26 +00001310 if( db ) sqlite_close(db);
drh75897232000-05-29 14:26:00 +00001311 return 0;
1312}