blob: 3f7094e09637a2881705b53ee8959aec400a9528 [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**
drha9b17162003-04-29 18:01:28 +000015** $Id: shell.c,v 1.74 2003/04/29 18:01:28 drh Exp $
drh75897232000-05-29 14:26:00 +000016*/
17#include <stdlib.h>
18#include <string.h>
19#include <stdio.h>
20#include "sqlite.h"
drh75897232000-05-29 14:26:00 +000021#include <ctype.h>
persicom7e2dfdd2002-04-18 02:46:52 +000022
drh820f3812003-01-08 13:02:52 +000023#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
drh4c504392000-10-16 22:06:40 +000024# include <signal.h>
drhdd45df82002-04-18 12:39:03 +000025# include <pwd.h>
26# include <unistd.h>
27# include <sys/types.h>
drh4c504392000-10-16 22:06:40 +000028#endif
drh75897232000-05-29 14:26:00 +000029
drh820f3812003-01-08 13:02:52 +000030#ifdef __MACOS__
31# include <console.h>
32# include <signal.h>
33# include <unistd.h>
34# include <extras.h>
35# include <Files.h>
36# include <Folders.h>
37#endif
38
drh16e59552000-07-31 11:57:37 +000039#if defined(HAVE_READLINE) && HAVE_READLINE==1
drh8e7e7a22000-05-30 18:45:23 +000040# include <readline/readline.h>
41# include <readline/history.h>
42#else
drh5e00f6c2001-09-13 13:46:56 +000043# define readline(p) getline(p,stdin)
persicom1d0b8722002-04-18 02:53:04 +000044# define add_history(X)
drh67505e72002-04-19 12:34:06 +000045# define read_history(X)
46# define write_history(X)
47# define stifle_history(X)
drh75897232000-05-29 14:26:00 +000048#endif
49
drh4328c8b2003-04-26 02:50:11 +000050/* Make sure isatty() has a prototype.
51*/
52extern int isatty();
53
drh75897232000-05-29 14:26:00 +000054/*
drh4c504392000-10-16 22:06:40 +000055** The following is the open SQLite database. We make a pointer
56** to this database a static variable so that it can be accessed
57** by the SIGINT handler to interrupt database processing.
58*/
59static sqlite *db = 0;
60
61/*
drh67505e72002-04-19 12:34:06 +000062** True if an interrupt (Control-C) has been received.
63*/
64static int seenInterrupt = 0;
65
66/*
persicom7e2dfdd2002-04-18 02:46:52 +000067** This is the name of our program. It is set in main(), used
68** in a number of other places, mostly for error messages.
69*/
70static char *Argv0;
71
72/*
73** Prompt strings. Initialized in main. Settable with
74** .prompt main continue
75*/
76static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
77static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
78
79/*
drh83965662003-04-17 02:54:13 +000080** Determines if a string is a number of not.
81*/
82extern int sqliteIsNumber(const char*);
83
84/*
drh8e7e7a22000-05-30 18:45:23 +000085** This routine reads a line of text from standard input, stores
86** the text in memory obtained from malloc() and returns a pointer
87** to the text. NULL is returned at end of file, or if malloc()
88** fails.
89**
90** The interface is like "readline" but no command-line editing
91** is done.
92*/
drhdaffd0e2001-04-11 14:28:42 +000093static char *getline(char *zPrompt, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +000094 char *zLine;
95 int nLine;
drh8e7e7a22000-05-30 18:45:23 +000096 int n;
97 int eol;
98
99 if( zPrompt && *zPrompt ){
100 printf("%s",zPrompt);
101 fflush(stdout);
102 }
103 nLine = 100;
104 zLine = malloc( nLine );
105 if( zLine==0 ) return 0;
106 n = 0;
107 eol = 0;
108 while( !eol ){
109 if( n+100>nLine ){
110 nLine = nLine*2 + 100;
111 zLine = realloc(zLine, nLine);
112 if( zLine==0 ) return 0;
113 }
drhdaffd0e2001-04-11 14:28:42 +0000114 if( fgets(&zLine[n], nLine - n, in)==0 ){
drh8e7e7a22000-05-30 18:45:23 +0000115 if( n==0 ){
116 free(zLine);
117 return 0;
118 }
119 zLine[n] = 0;
120 eol = 1;
121 break;
122 }
123 while( zLine[n] ){ n++; }
124 if( n>0 && zLine[n-1]=='\n' ){
125 n--;
126 zLine[n] = 0;
127 eol = 1;
128 }
129 }
130 zLine = realloc( zLine, n+1 );
131 return zLine;
132}
133
134/*
135** Retrieve a single line of input text. "isatty" is true if text
136** is coming from a terminal. In that case, we issue a prompt and
137** attempt to use "readline" for command-line editing. If "isatty"
drhaacc5432002-01-06 17:07:40 +0000138** is false, use "getline" instead of "readline" and issue no prompt.
drh8e7e7a22000-05-30 18:45:23 +0000139**
140** zPrior is a string of prior text retrieved. If not the empty
141** string, then issue a continuation prompt.
142*/
drhdaffd0e2001-04-11 14:28:42 +0000143static char *one_input_line(const char *zPrior, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +0000144 char *zPrompt;
145 char *zResult;
drhdaffd0e2001-04-11 14:28:42 +0000146 if( in!=0 ){
147 return getline(0, in);
drh8e7e7a22000-05-30 18:45:23 +0000148 }
149 if( zPrior && zPrior[0] ){
persicom7e2dfdd2002-04-18 02:46:52 +0000150 zPrompt = continuePrompt;
drh8e7e7a22000-05-30 18:45:23 +0000151 }else{
persicom7e2dfdd2002-04-18 02:46:52 +0000152 zPrompt = mainPrompt;
drh8e7e7a22000-05-30 18:45:23 +0000153 }
154 zResult = readline(zPrompt);
drh2dfbbca2000-07-28 14:32:48 +0000155 if( zResult ) add_history(zResult);
drh8e7e7a22000-05-30 18:45:23 +0000156 return zResult;
157}
158
persicom7e2dfdd2002-04-18 02:46:52 +0000159struct previous_mode_data {
160 int valid; /* Is there legit data in here? */
161 int mode;
162 int showHeader;
163 int colWidth[100];
164};
drh8e7e7a22000-05-30 18:45:23 +0000165/*
drh75897232000-05-29 14:26:00 +0000166** An pointer to an instance of this structure is passed from
167** the main program to the callback. This is used to communicate
168** state and mode information.
169*/
170struct callback_data {
drh28bd4bc2000-06-15 15:57:22 +0000171 sqlite *db; /* The database */
drhdaffd0e2001-04-11 14:28:42 +0000172 int echoOn; /* True to echo input commands */
drh28bd4bc2000-06-15 15:57:22 +0000173 int cnt; /* Number of records displayed so far */
174 FILE *out; /* Write results here */
175 int mode; /* An output mode setting */
176 int showHeader; /* True to show column names in List or Column mode */
drh33048c02001-10-01 14:29:22 +0000177 char *zDestTable; /* Name of destination table when MODE_Insert */
drh28bd4bc2000-06-15 15:57:22 +0000178 char separator[20]; /* Separator character for MODE_List */
drha0c66f52000-07-29 13:20:21 +0000179 int colWidth[100]; /* Requested width of each column when in column mode*/
180 int actualWidth[100]; /* Actual width of each column */
drh83965662003-04-17 02:54:13 +0000181 char nullvalue[20]; /* The text to print when a NULL comes back from
182 ** the database */
persicom7e2dfdd2002-04-18 02:46:52 +0000183 struct previous_mode_data explainPrev;
drh83965662003-04-17 02:54:13 +0000184 /* Holds the mode information just before
185 ** .explain ON */
persicom7e2dfdd2002-04-18 02:46:52 +0000186 char outfile[FILENAME_MAX];
187 /* Filename for *out */
drh75897232000-05-29 14:26:00 +0000188};
189
190/*
191** These are the allowed modes.
192*/
drh967e8b72000-06-21 13:59:10 +0000193#define MODE_Line 0 /* One column per line. Blank line between records */
drh75897232000-05-29 14:26:00 +0000194#define MODE_Column 1 /* One record per line in neat columns */
195#define MODE_List 2 /* One record per line with a separator */
drhe3710332000-09-29 13:30:53 +0000196#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
197#define MODE_Html 4 /* Generate an XHTML table */
198#define MODE_Insert 5 /* Generate SQL "insert" statements */
persicom7e2dfdd2002-04-18 02:46:52 +0000199#define MODE_NUM_OF 6
200
201char *modeDescr[MODE_NUM_OF] = {
202 "line",
203 "column",
204 "list",
205 "semi",
206 "html",
207 "insert"
208};
drh75897232000-05-29 14:26:00 +0000209
210/*
211** Number of elements in an array
212*/
213#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
214
215/*
drh28bd4bc2000-06-15 15:57:22 +0000216** Output the given string as a quoted string using SQL quoting conventions.
217*/
218static void output_quoted_string(FILE *out, const char *z){
219 int i;
220 int nSingle = 0;
drh28bd4bc2000-06-15 15:57:22 +0000221 for(i=0; z[i]; i++){
222 if( z[i]=='\'' ) nSingle++;
drh28bd4bc2000-06-15 15:57:22 +0000223 }
224 if( nSingle==0 ){
225 fprintf(out,"'%s'",z);
drh28bd4bc2000-06-15 15:57:22 +0000226 }else{
227 fprintf(out,"'");
228 while( *z ){
229 for(i=0; z[i] && z[i]!='\''; i++){}
230 if( i==0 ){
231 fprintf(out,"''");
232 z++;
233 }else if( z[i]=='\'' ){
234 fprintf(out,"%.*s''",i,z);
235 z += i+1;
236 }else{
drhcd7d2732002-02-26 23:24:26 +0000237 fprintf(out,"%s",z);
drh28bd4bc2000-06-15 15:57:22 +0000238 break;
239 }
240 }
drhcd7d2732002-02-26 23:24:26 +0000241 fprintf(out,"'");
drh28bd4bc2000-06-15 15:57:22 +0000242 }
243}
244
245/*
drhc08a4f12000-06-15 16:49:48 +0000246** Output the given string with characters that are special to
247** HTML escaped.
248*/
249static void output_html_string(FILE *out, const char *z){
250 int i;
251 while( *z ){
252 for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){}
253 if( i>0 ){
254 fprintf(out,"%.*s",i,z);
255 }
256 if( z[i]=='<' ){
257 fprintf(out,"&lt;");
258 }else if( z[i]=='&' ){
259 fprintf(out,"&amp;");
260 }else{
261 break;
262 }
263 z += i + 1;
264 }
265}
266
267/*
drh4c504392000-10-16 22:06:40 +0000268** This routine runs when the user presses Ctrl-C
269*/
270static void interrupt_handler(int NotUsed){
drh67505e72002-04-19 12:34:06 +0000271 seenInterrupt = 1;
drh4c504392000-10-16 22:06:40 +0000272 if( db ) sqlite_interrupt(db);
273}
274
275/*
drh75897232000-05-29 14:26:00 +0000276** This is the callback routine that the SQLite library
277** invokes for each row of a query result.
278*/
279static int callback(void *pArg, int nArg, char **azArg, char **azCol){
280 int i;
281 struct callback_data *p = (struct callback_data*)pArg;
282 switch( p->mode ){
283 case MODE_Line: {
drhe3710332000-09-29 13:30:53 +0000284 int w = 5;
drh6a535342001-10-19 16:44:56 +0000285 if( azArg==0 ) break;
drhe3710332000-09-29 13:30:53 +0000286 for(i=0; i<nArg; i++){
287 int len = strlen(azCol[i]);
288 if( len>w ) w = len;
289 }
drh75897232000-05-29 14:26:00 +0000290 if( p->cnt++>0 ) fprintf(p->out,"\n");
291 for(i=0; i<nArg; i++){
drha69d9162003-04-17 22:57:53 +0000292 fprintf(p->out,"%*s = %s\n", w, azCol[i],
293 azArg[i] ? azArg[i] : p->nullvalue);
drh75897232000-05-29 14:26:00 +0000294 }
295 break;
296 }
297 case MODE_Column: {
drha0c66f52000-07-29 13:20:21 +0000298 if( p->cnt++==0 ){
drh75897232000-05-29 14:26:00 +0000299 for(i=0; i<nArg; i++){
drha0c66f52000-07-29 13:20:21 +0000300 int w, n;
301 if( i<ArraySize(p->colWidth) ){
drh75897232000-05-29 14:26:00 +0000302 w = p->colWidth[i];
303 }else{
drha0c66f52000-07-29 13:20:21 +0000304 w = 0;
drh75897232000-05-29 14:26:00 +0000305 }
drha0c66f52000-07-29 13:20:21 +0000306 if( w<=0 ){
drhff6e9112000-08-28 16:21:58 +0000307 w = strlen(azCol[i] ? azCol[i] : "");
drha0c66f52000-07-29 13:20:21 +0000308 if( w<10 ) w = 10;
persicom7e2dfdd2002-04-18 02:46:52 +0000309 n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue);
drha0c66f52000-07-29 13:20:21 +0000310 if( w<n ) w = n;
311 }
312 if( i<ArraySize(p->actualWidth) ){
persicom1d0b8722002-04-18 02:53:04 +0000313 p->actualWidth[i] = w;
drha0c66f52000-07-29 13:20:21 +0000314 }
315 if( p->showHeader ){
316 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
317 }
318 }
319 if( p->showHeader ){
320 for(i=0; i<nArg; i++){
321 int w;
322 if( i<ArraySize(p->actualWidth) ){
323 w = p->actualWidth[i];
324 }else{
325 w = 10;
326 }
327 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
328 "----------------------------------------------------------",
329 i==nArg-1 ? "\n": " ");
330 }
drh75897232000-05-29 14:26:00 +0000331 }
332 }
drh6a535342001-10-19 16:44:56 +0000333 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000334 for(i=0; i<nArg; i++){
335 int w;
drha0c66f52000-07-29 13:20:21 +0000336 if( i<ArraySize(p->actualWidth) ){
337 w = p->actualWidth[i];
drh75897232000-05-29 14:26:00 +0000338 }else{
339 w = 10;
340 }
drhc61053b2000-06-04 12:58:36 +0000341 fprintf(p->out,"%-*.*s%s",w,w,
persicom7e2dfdd2002-04-18 02:46:52 +0000342 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
drh75897232000-05-29 14:26:00 +0000343 }
344 break;
345 }
drhe3710332000-09-29 13:30:53 +0000346 case MODE_Semi:
drh75897232000-05-29 14:26:00 +0000347 case MODE_List: {
348 if( p->cnt++==0 && p->showHeader ){
349 for(i=0; i<nArg; i++){
350 fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
351 }
352 }
drh6a535342001-10-19 16:44:56 +0000353 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000354 for(i=0; i<nArg; i++){
drh4c653a02000-06-07 01:27:47 +0000355 char *z = azArg[i];
persicom7e2dfdd2002-04-18 02:46:52 +0000356 if( z==0 ) z = p->nullvalue;
drh71172c52002-01-24 00:00:21 +0000357 fprintf(p->out, "%s", z);
drhe3710332000-09-29 13:30:53 +0000358 if( i<nArg-1 ){
359 fprintf(p->out, "%s", p->separator);
360 }else if( p->mode==MODE_Semi ){
361 fprintf(p->out, ";\n");
362 }else{
363 fprintf(p->out, "\n");
364 }
drh75897232000-05-29 14:26:00 +0000365 }
366 break;
367 }
drh1e5d0e92000-05-31 23:33:17 +0000368 case MODE_Html: {
369 if( p->cnt++==0 && p->showHeader ){
370 fprintf(p->out,"<TR>");
371 for(i=0; i<nArg; i++){
372 fprintf(p->out,"<TH>%s</TH>",azCol[i]);
373 }
374 fprintf(p->out,"</TR>\n");
375 }
drh6a535342001-10-19 16:44:56 +0000376 if( azArg==0 ) break;
drh28bd4bc2000-06-15 15:57:22 +0000377 fprintf(p->out,"<TR>");
drh1e5d0e92000-05-31 23:33:17 +0000378 for(i=0; i<nArg; i++){
drhc08a4f12000-06-15 16:49:48 +0000379 fprintf(p->out,"<TD>");
persicom7e2dfdd2002-04-18 02:46:52 +0000380 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
drhc08a4f12000-06-15 16:49:48 +0000381 fprintf(p->out,"</TD>\n");
drh1e5d0e92000-05-31 23:33:17 +0000382 }
drh7d686b22002-11-11 13:56:47 +0000383 fprintf(p->out,"</TR>\n");
drh1e5d0e92000-05-31 23:33:17 +0000384 break;
385 }
drh28bd4bc2000-06-15 15:57:22 +0000386 case MODE_Insert: {
drh6a535342001-10-19 16:44:56 +0000387 if( azArg==0 ) break;
drh33048c02001-10-01 14:29:22 +0000388 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
drh28bd4bc2000-06-15 15:57:22 +0000389 for(i=0; i<nArg; i++){
390 char *zSep = i>0 ? ",": "";
391 if( azArg[i]==0 ){
392 fprintf(p->out,"%sNULL",zSep);
drh83965662003-04-17 02:54:13 +0000393 }else if( sqliteIsNumber(azArg[i]) ){
drh28bd4bc2000-06-15 15:57:22 +0000394 fprintf(p->out,"%s%s",zSep, azArg[i]);
395 }else{
396 if( zSep[0] ) fprintf(p->out,"%s",zSep);
397 output_quoted_string(p->out, azArg[i]);
398 }
399 }
400 fprintf(p->out,");\n");
drh6a535342001-10-19 16:44:56 +0000401 break;
drh28bd4bc2000-06-15 15:57:22 +0000402 }
persicom1d0b8722002-04-18 02:53:04 +0000403 }
drh75897232000-05-29 14:26:00 +0000404 return 0;
405}
406
407/*
drh33048c02001-10-01 14:29:22 +0000408** Set the destination table field of the callback_data structure to
409** the name of the table given. Escape any quote characters in the
410** table name.
411*/
412static void set_table_name(struct callback_data *p, const char *zName){
413 int i, n;
414 int needQuote;
415 char *z;
416
417 if( p->zDestTable ){
418 free(p->zDestTable);
419 p->zDestTable = 0;
420 }
421 if( zName==0 ) return;
422 needQuote = !isalpha(*zName) && *zName!='_';
423 for(i=n=0; zName[i]; i++, n++){
424 if( !isalnum(zName[i]) && zName[i]!='_' ){
425 needQuote = 1;
426 if( zName[i]=='\'' ) n++;
427 }
428 }
429 if( needQuote ) n += 2;
430 z = p->zDestTable = malloc( n+1 );
431 if( z==0 ){
432 fprintf(stderr,"Out of memory!\n");
433 exit(1);
434 }
435 n = 0;
436 if( needQuote ) z[n++] = '\'';
437 for(i=0; zName[i]; i++){
438 z[n++] = zName[i];
439 if( zName[i]=='\'' ) z[n++] = '\'';
440 }
441 if( needQuote ) z[n++] = '\'';
442 z[n] = 0;
443}
444
445/*
drh4c653a02000-06-07 01:27:47 +0000446** This is a different callback routine used for dumping the database.
447** Each row received by this callback consists of a table name,
448** the table type ("index" or "table") and SQL to create the table.
449** This routine should print text sufficient to recreate the table.
450*/
451static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
drhdaffd0e2001-04-11 14:28:42 +0000452 struct callback_data *p = (struct callback_data *)pArg;
drh4c653a02000-06-07 01:27:47 +0000453 if( nArg!=3 ) return 1;
drhdaffd0e2001-04-11 14:28:42 +0000454 fprintf(p->out, "%s;\n", azArg[2]);
drh4c653a02000-06-07 01:27:47 +0000455 if( strcmp(azArg[1],"table")==0 ){
456 struct callback_data d2;
drhdaffd0e2001-04-11 14:28:42 +0000457 d2 = *p;
drh33048c02001-10-01 14:29:22 +0000458 d2.mode = MODE_Insert;
459 d2.zDestTable = 0;
460 set_table_name(&d2, azArg[0]);
persicom1d0b8722002-04-18 02:53:04 +0000461 sqlite_exec_printf(p->db,
drha18c5682000-10-08 22:20:57 +0000462 "SELECT * FROM '%q'",
463 callback, &d2, 0, azArg[0]
464 );
drh33048c02001-10-01 14:29:22 +0000465 set_table_name(&d2, 0);
drh4c653a02000-06-07 01:27:47 +0000466 }
drh4c653a02000-06-07 01:27:47 +0000467 return 0;
468}
469
470/*
drh75897232000-05-29 14:26:00 +0000471** Text of a help message
472*/
persicom1d0b8722002-04-18 02:53:04 +0000473static char zHelp[] =
drh4c653a02000-06-07 01:27:47 +0000474 ".dump ?TABLE? ... Dump the database in an text format\n"
drhdaffd0e2001-04-11 14:28:42 +0000475 ".echo ON|OFF Turn command echo on or off\n"
drh75897232000-05-29 14:26:00 +0000476 ".exit Exit this program\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000477 ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000478 ".header(s) ON|OFF Turn display of headers on or off\n"
drh75897232000-05-29 14:26:00 +0000479 ".help Show this message\n"
480 ".indices TABLE Show names of all indices on TABLE\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000481 ".mode MODE Set mode to one of \"line(s)\", \"column(s)\", \n"
drhe3710332000-09-29 13:30:53 +0000482 " \"insert\", \"list\", or \"html\"\n"
drhc08a4f12000-06-15 16:49:48 +0000483 ".mode insert TABLE Generate SQL insert statements for TABLE\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000484 ".nullvalue STRING Print STRING instead of nothing for NULL data\n"
drh75897232000-05-29 14:26:00 +0000485 ".output FILENAME Send output to FILENAME\n"
486 ".output stdout Send output to the screen\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000487 ".prompt MAIN CONTINUE Replace the standard prompts\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000488 ".quit Exit this program\n"
drhdaffd0e2001-04-11 14:28:42 +0000489 ".read FILENAME Execute SQL in FILENAME\n"
drh75897232000-05-29 14:26:00 +0000490 ".schema ?TABLE? Show the CREATE statements\n"
491 ".separator STRING Change separator string for \"list\" mode\n"
drhdd45df82002-04-18 12:39:03 +0000492 ".show Show the current values for various settings\n"
drha50da102000-08-08 20:19:09 +0000493 ".tables ?PATTERN? List names of tables matching a pattern\n"
drh2dfbbca2000-07-28 14:32:48 +0000494 ".timeout MS Try opening locked tables for MS milliseconds\n"
drh75897232000-05-29 14:26:00 +0000495 ".width NUM NUM ... Set column widths for \"column\" mode\n"
496;
497
drhdaffd0e2001-04-11 14:28:42 +0000498/* Forward reference */
499static void process_input(struct callback_data *p, FILE *in);
500
drh75897232000-05-29 14:26:00 +0000501/*
502** If an input line begins with "." then invoke this routine to
503** process that line.
drh67505e72002-04-19 12:34:06 +0000504**
505** Return 1 to exit and 0 to continue.
drh75897232000-05-29 14:26:00 +0000506*/
drh67505e72002-04-19 12:34:06 +0000507static int do_meta_command(char *zLine, sqlite *db, struct callback_data *p){
drh75897232000-05-29 14:26:00 +0000508 int i = 1;
509 int nArg = 0;
510 int n, c;
drh67505e72002-04-19 12:34:06 +0000511 int rc = 0;
drh75897232000-05-29 14:26:00 +0000512 char *azArg[50];
513
514 /* Parse the input line into tokens.
515 */
516 while( zLine[i] && nArg<ArraySize(azArg) ){
517 while( isspace(zLine[i]) ){ i++; }
518 if( zLine[i]=='\'' || zLine[i]=='"' ){
519 int delim = zLine[i++];
520 azArg[nArg++] = &zLine[i];
521 while( zLine[i] && zLine[i]!=delim ){ i++; }
522 if( zLine[i]==delim ){
523 zLine[i++] = 0;
524 }
525 }else{
526 azArg[nArg++] = &zLine[i];
527 while( zLine[i] && !isspace(zLine[i]) ){ i++; }
528 if( zLine[i] ) zLine[i++] = 0;
529 }
530 }
531
532 /* Process the input line.
533 */
drh67505e72002-04-19 12:34:06 +0000534 if( nArg==0 ) return rc;
drh75897232000-05-29 14:26:00 +0000535 n = strlen(azArg[0]);
536 c = azArg[0][0];
drh4c653a02000-06-07 01:27:47 +0000537 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
538 char *zErrMsg = 0;
drh33048c02001-10-01 14:29:22 +0000539 fprintf(p->out, "BEGIN TRANSACTION;\n");
drh4c653a02000-06-07 01:27:47 +0000540 if( nArg==1 ){
drha18c5682000-10-08 22:20:57 +0000541 sqlite_exec(db,
542 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000543 "WHERE type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000544 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000545 dump_callback, p, &zErrMsg
546 );
drh4c653a02000-06-07 01:27:47 +0000547 }else{
548 int i;
549 for(i=1; i<nArg && zErrMsg==0; i++){
persicom1d0b8722002-04-18 02:53:04 +0000550 sqlite_exec_printf(db,
drha18c5682000-10-08 22:20:57 +0000551 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000552 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000553 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000554 dump_callback, p, &zErrMsg, azArg[i]
555 );
drh4c653a02000-06-07 01:27:47 +0000556 }
557 }
558 if( zErrMsg ){
559 fprintf(stderr,"Error: %s\n", zErrMsg);
560 free(zErrMsg);
drh33048c02001-10-01 14:29:22 +0000561 }else{
562 fprintf(p->out, "COMMIT;\n");
drh4c653a02000-06-07 01:27:47 +0000563 }
564 }else
drh75897232000-05-29 14:26:00 +0000565
drhdaffd0e2001-04-11 14:28:42 +0000566 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
567 int j;
568 char *z = azArg[1];
569 int val = atoi(azArg[1]);
570 for(j=0; z[j]; j++){
571 if( isupper(z[j]) ) z[j] = tolower(z[j]);
572 }
573 if( strcmp(z,"on")==0 ){
574 val = 1;
575 }else if( strcmp(z,"yes")==0 ){
576 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000577 }
drhdaffd0e2001-04-11 14:28:42 +0000578 p->echoOn = val;
579 }else
580
drh75897232000-05-29 14:26:00 +0000581 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
drh67505e72002-04-19 12:34:06 +0000582 rc = 1;
drh75897232000-05-29 14:26:00 +0000583 }else
584
drhdd45df82002-04-18 12:39:03 +0000585 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +0000586 int j;
drhdd45df82002-04-18 12:39:03 +0000587 char *z = nArg>=2 ? azArg[1] : "1";
588 int val = atoi(z);
persicom7e2dfdd2002-04-18 02:46:52 +0000589 for(j=0; z[j]; j++){
590 if( isupper(z[j]) ) z[j] = tolower(z[j]);
591 }
592 if( strcmp(z,"on")==0 ){
593 val = 1;
594 }else if( strcmp(z,"yes")==0 ){
595 val = 1;
596 }
597 if(val == 1) {
598 if(!p->explainPrev.valid) {
599 p->explainPrev.valid = 1;
600 p->explainPrev.mode = p->mode;
601 p->explainPrev.showHeader = p->showHeader;
602 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
603 }
604 /* We could put this code under the !p->explainValid
605 ** condition so that it does not execute if we are already in
606 ** explain mode. However, always executing it allows us an easy
607 ** was to reset to explain mode in case the user previously
608 ** did an .explain followed by a .width, .mode or .header
609 ** command.
610 */
611 p->mode = MODE_Column;
612 p->showHeader = 1;
613 memset(p->colWidth,0,ArraySize(p->colWidth));
614 p->colWidth[0] = 4;
615 p->colWidth[1] = 12;
616 p->colWidth[2] = 10;
617 p->colWidth[3] = 10;
618 p->colWidth[4] = 35;
619 }else if (p->explainPrev.valid) {
620 p->explainPrev.valid = 0;
621 p->mode = p->explainPrev.mode;
622 p->showHeader = p->explainPrev.showHeader;
623 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
624 }
drh75897232000-05-29 14:26:00 +0000625 }else
626
persicom7e2dfdd2002-04-18 02:46:52 +0000627 if( c=='h' && (strncmp(azArg[0], "header", n)==0
628 ||
629 strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){
drh75897232000-05-29 14:26:00 +0000630 int j;
631 char *z = azArg[1];
632 int val = atoi(azArg[1]);
633 for(j=0; z[j]; j++){
634 if( isupper(z[j]) ) z[j] = tolower(z[j]);
635 }
636 if( strcmp(z,"on")==0 ){
637 val = 1;
638 }else if( strcmp(z,"yes")==0 ){
639 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000640 }
drh75897232000-05-29 14:26:00 +0000641 p->showHeader = val;
642 }else
643
644 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
645 fprintf(stderr,zHelp);
646 }else
647
648 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
649 struct callback_data data;
650 char *zErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000651 memcpy(&data, p, sizeof(data));
652 data.showHeader = 0;
653 data.mode = MODE_List;
persicom1d0b8722002-04-18 02:53:04 +0000654 sqlite_exec_printf(db,
drha18c5682000-10-08 22:20:57 +0000655 "SELECT name FROM sqlite_master "
656 "WHERE type='index' AND tbl_name LIKE '%q' "
drhe0bc4042002-06-25 01:09:11 +0000657 "UNION ALL "
658 "SELECT name FROM sqlite_temp_master "
659 "WHERE type='index' AND tbl_name LIKE '%q' "
660 "ORDER BY 1",
661 callback, &data, &zErrMsg, azArg[1], azArg[1]
drha18c5682000-10-08 22:20:57 +0000662 );
drh75897232000-05-29 14:26:00 +0000663 if( zErrMsg ){
664 fprintf(stderr,"Error: %s\n", zErrMsg);
665 free(zErrMsg);
666 }
667 }else
668
drh28bd4bc2000-06-15 15:57:22 +0000669 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
drh75897232000-05-29 14:26:00 +0000670 int n2 = strlen(azArg[1]);
persicom7e2dfdd2002-04-18 02:46:52 +0000671 if( strncmp(azArg[1],"line",n2)==0
672 ||
673 strncmp(azArg[1],"lines",n2)==0 ){
drh75897232000-05-29 14:26:00 +0000674 p->mode = MODE_Line;
persicom7e2dfdd2002-04-18 02:46:52 +0000675 }else if( strncmp(azArg[1],"column",n2)==0
676 ||
677 strncmp(azArg[1],"columns",n2)==0 ){
drh75897232000-05-29 14:26:00 +0000678 p->mode = MODE_Column;
679 }else if( strncmp(azArg[1],"list",n2)==0 ){
680 p->mode = MODE_List;
drh1e5d0e92000-05-31 23:33:17 +0000681 }else if( strncmp(azArg[1],"html",n2)==0 ){
682 p->mode = MODE_Html;
drh28bd4bc2000-06-15 15:57:22 +0000683 }else if( strncmp(azArg[1],"insert",n2)==0 ){
684 p->mode = MODE_Insert;
685 if( nArg>=3 ){
drh33048c02001-10-01 14:29:22 +0000686 set_table_name(p, azArg[2]);
drh28bd4bc2000-06-15 15:57:22 +0000687 }else{
drh33048c02001-10-01 14:29:22 +0000688 set_table_name(p, "table");
drh28bd4bc2000-06-15 15:57:22 +0000689 }
drhdaffd0e2001-04-11 14:28:42 +0000690 }else {
691 fprintf(stderr,"mode should be on of: column html insert line list\n");
drh75897232000-05-29 14:26:00 +0000692 }
693 }else
694
persicom7e2dfdd2002-04-18 02:46:52 +0000695 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
696 sprintf(p->nullvalue, "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
697 }else
698
drh75897232000-05-29 14:26:00 +0000699 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
700 if( p->out!=stdout ){
701 fclose(p->out);
702 }
703 if( strcmp(azArg[1],"stdout")==0 ){
704 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +0000705 strcpy(p->outfile,"stdout");
drh75897232000-05-29 14:26:00 +0000706 }else{
707 p->out = fopen(azArg[1], "w");
708 if( p->out==0 ){
709 fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
710 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +0000711 } else {
712 strcpy(p->outfile,azArg[1]);
drh75897232000-05-29 14:26:00 +0000713 }
714 }
715 }else
716
drhdd45df82002-04-18 12:39:03 +0000717 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
persicom7e2dfdd2002-04-18 02:46:52 +0000718 if( nArg >= 2) {
719 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
720 }
721 if( nArg >= 3) {
722 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
723 }
724 }else
725
726 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
727 sqlite_close(db);
728 exit(0);
729 }else
730
drhdaffd0e2001-04-11 14:28:42 +0000731 if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
732 FILE *alt = fopen(azArg[1], "r");
733 if( alt==0 ){
734 fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
735 }else{
736 process_input(p, alt);
737 fclose(alt);
738 }
739 }else
740
drh75897232000-05-29 14:26:00 +0000741 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
742 struct callback_data data;
743 char *zErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000744 memcpy(&data, p, sizeof(data));
745 data.showHeader = 0;
drhe3710332000-09-29 13:30:53 +0000746 data.mode = MODE_Semi;
drh75897232000-05-29 14:26:00 +0000747 if( nArg>1 ){
drhff9821a2001-04-04 21:22:14 +0000748 extern int sqliteStrICmp(const char*,const char*);
drha18c5682000-10-08 22:20:57 +0000749 if( sqliteStrICmp(azArg[1],"sqlite_master")==0 ){
750 char *new_argv[2], *new_colv[2];
751 new_argv[0] = "CREATE TABLE sqlite_master (\n"
752 " type text,\n"
753 " name text,\n"
754 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000755 " rootpage integer,\n"
drha18c5682000-10-08 22:20:57 +0000756 " sql text\n"
757 ")";
758 new_argv[1] = 0;
759 new_colv[0] = "sql";
760 new_colv[1] = 0;
761 callback(&data, 1, new_argv, new_colv);
drhe0bc4042002-06-25 01:09:11 +0000762 }else if( sqliteStrICmp(azArg[1],"sqlite_temp_master")==0 ){
763 char *new_argv[2], *new_colv[2];
764 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
765 " type text,\n"
766 " name text,\n"
767 " tbl_name text,\n"
768 " rootpage integer,\n"
769 " sql text\n"
770 ")";
771 new_argv[1] = 0;
772 new_colv[0] = "sql";
773 new_colv[1] = 0;
774 callback(&data, 1, new_argv, new_colv);
drha18c5682000-10-08 22:20:57 +0000775 }else{
776 sqlite_exec_printf(db,
drhe0bc4042002-06-25 01:09:11 +0000777 "SELECT sql FROM "
778 " (SELECT * FROM sqlite_master UNION ALL"
779 " SELECT * FROM sqlite_temp_master) "
drh33048c02001-10-01 14:29:22 +0000780 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +0000781 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000782 callback, &data, &zErrMsg, azArg[1]);
783 }
drh75897232000-05-29 14:26:00 +0000784 }else{
drha18c5682000-10-08 22:20:57 +0000785 sqlite_exec(db,
drhe0bc4042002-06-25 01:09:11 +0000786 "SELECT sql FROM "
787 " (SELECT * FROM sqlite_master UNION ALL"
788 " SELECT * FROM sqlite_temp_master) "
drh33048c02001-10-01 14:29:22 +0000789 "WHERE type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +0000790 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000791 callback, &data, &zErrMsg
792 );
drh75897232000-05-29 14:26:00 +0000793 }
drh75897232000-05-29 14:26:00 +0000794 if( zErrMsg ){
795 fprintf(stderr,"Error: %s\n", zErrMsg);
796 free(zErrMsg);
797 }
798 }else
799
800 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
801 sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
802 }else
803
persicom7e2dfdd2002-04-18 02:46:52 +0000804 if( c=='s' && strncmp(azArg[0], "show", n)==0){
805 int i;
806 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
drh67505e72002-04-19 12:34:06 +0000807 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
drhdd45df82002-04-18 12:39:03 +0000808 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
persicom7e2dfdd2002-04-18 02:46:52 +0000809 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
810 fprintf(p->out,"%9.9s: %s\n","nullvalue", p->nullvalue);
drh67505e72002-04-19 12:34:06 +0000811 fprintf(p->out,"%9.9s: %s\n","output",
812 strlen(p->outfile) ? p->outfile : "stdout");
persicom7e2dfdd2002-04-18 02:46:52 +0000813 fprintf(p->out,"%9.9s: %s\n","separator", p->separator);
814 fprintf(p->out,"%9.9s: ","width");
815 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
816 fprintf(p->out,"%d ",p->colWidth[i]);
817 }
818 fprintf(p->out,"\n\n");
819 }else
820
drh2dfbbca2000-07-28 14:32:48 +0000821 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
drhe3710332000-09-29 13:30:53 +0000822 char **azResult;
823 int nRow, rc;
824 char *zErrMsg;
drha50da102000-08-08 20:19:09 +0000825 if( nArg==1 ){
drha18c5682000-10-08 22:20:57 +0000826 rc = sqlite_get_table(db,
drha50da102000-08-08 20:19:09 +0000827 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +0000828 "WHERE type IN ('table','view') "
drhe0bc4042002-06-25 01:09:11 +0000829 "UNION ALL "
830 "SELECT name FROM sqlite_temp_master "
831 "WHERE type IN ('table','view') "
832 "ORDER BY 1",
drha18c5682000-10-08 22:20:57 +0000833 &azResult, &nRow, 0, &zErrMsg
834 );
drha50da102000-08-08 20:19:09 +0000835 }else{
drha18c5682000-10-08 22:20:57 +0000836 rc = sqlite_get_table_printf(db,
drha50da102000-08-08 20:19:09 +0000837 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +0000838 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
drhe0bc4042002-06-25 01:09:11 +0000839 "UNION ALL "
840 "SELECT name FROM sqlite_temp_master "
841 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
842 "ORDER BY 1",
843 &azResult, &nRow, 0, &zErrMsg, azArg[1], azArg[1]
drha18c5682000-10-08 22:20:57 +0000844 );
drha50da102000-08-08 20:19:09 +0000845 }
drh75897232000-05-29 14:26:00 +0000846 if( zErrMsg ){
847 fprintf(stderr,"Error: %s\n", zErrMsg);
848 free(zErrMsg);
849 }
drhe3710332000-09-29 13:30:53 +0000850 if( rc==SQLITE_OK ){
851 int len, maxlen = 0;
852 int i, j;
853 int nPrintCol, nPrintRow;
854 for(i=1; i<=nRow; i++){
855 if( azResult[i]==0 ) continue;
856 len = strlen(azResult[i]);
857 if( len>maxlen ) maxlen = len;
858 }
859 nPrintCol = 80/(maxlen+2);
860 if( nPrintCol<1 ) nPrintCol = 1;
861 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
862 for(i=0; i<nPrintRow; i++){
863 for(j=i+1; j<=nRow; j+=nPrintRow){
864 char *zSp = j<=nPrintRow ? "" : " ";
865 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
866 }
867 printf("\n");
868 }
869 }
870 sqlite_free_table(azResult);
drh75897232000-05-29 14:26:00 +0000871 }else
872
drh2dfbbca2000-07-28 14:32:48 +0000873 if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
874 sqlite_busy_timeout(db, atoi(azArg[1]));
875 }else
876
drh75897232000-05-29 14:26:00 +0000877 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
878 int j;
879 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
880 p->colWidth[j-1] = atoi(azArg[j]);
881 }
882 }else
883
884 {
drh67505e72002-04-19 12:34:06 +0000885 fprintf(stderr, "unknown command or invalid arguments: "
886 " \"%s\". Enter \".help\" for help\n", azArg[0]);
drh75897232000-05-29 14:26:00 +0000887 }
drh67505e72002-04-19 12:34:06 +0000888
889 return rc;
drh75897232000-05-29 14:26:00 +0000890}
891
drh67505e72002-04-19 12:34:06 +0000892/*
drh324ccef2003-02-05 14:06:20 +0000893** Return TRUE if the last non-whitespace character in z[] is a semicolon.
894** z[] is N characters long.
895*/
896static int _ends_with_semicolon(const char *z, int N){
897 while( N>0 && isspace(z[N-1]) ){ N--; }
898 return N>0 && z[N-1]==';';
899}
900
901/*
drh70c7a4b2003-04-26 03:03:06 +0000902** Test to see if a line consists entirely of whitespace.
903*/
904static int _all_whitespace(const char *z){
905 for(; *z; z++){
906 if( isspace(*z) ) continue;
907 if( *z=='/' && z[1]=='*' ){
908 z += 2;
909 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
910 if( *z==0 ) return 0;
911 z++;
912 continue;
913 }
914 if( *z=='-' && z[1]=='-' ){
915 z += 2;
916 while( *z && *z!='\n' ){ z++; }
917 if( *z==0 ) return 1;
918 continue;
919 }
920 return 0;
921 }
922 return 1;
923}
924
925/*
drha9b17162003-04-29 18:01:28 +0000926** Return TRUE if the line typed in is an SQL command terminator other
927** than a semi-colon. The SQL Server style "go" command is understood
928** as is the Oracle "/".
929*/
930static int _is_command_terminator(const char *zLine){
931 extern int sqliteStrNICmp(const char*,const char*,int);
932 while( isspace(*zLine) ){ zLine++; };
933 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ) return 1; /* Oracle */
934 if( sqliteStrNICmp(zLine,"go",2)==0 && _all_whitespace(&zLine[2]) ){
935 return 1; /* SQL Server */
936 }
937 return 0;
938}
939
940/*
drh67505e72002-04-19 12:34:06 +0000941** Read input from *in and process it. If *in==0 then input
942** is interactive - the user is typing it it. Otherwise, input
943** is coming from a file or device. A prompt is issued and history
944** is saved only if input is interactive. An interrupt signal will
945** cause this routine to exit immediately, unless input is interactive.
946*/
drhdaffd0e2001-04-11 14:28:42 +0000947static void process_input(struct callback_data *p, FILE *in){
948 char *zLine;
949 char *zSql = 0;
950 int nSql = 0;
951 char *zErrMsg;
drh7f953e22002-07-13 17:33:45 +0000952 int rc;
drh41e941d2002-04-04 15:10:12 +0000953 while( fflush(p->out), (zLine = one_input_line(zSql, in))!=0 ){
drh67505e72002-04-19 12:34:06 +0000954 if( seenInterrupt ){
955 if( in!=0 ) break;
956 seenInterrupt = 0;
957 }
drhdaffd0e2001-04-11 14:28:42 +0000958 if( p->echoOn ) printf("%s\n", zLine);
drh70c7a4b2003-04-26 03:03:06 +0000959 if( _all_whitespace(zLine) ) continue;
drh2af0b2d2002-02-21 02:25:02 +0000960 if( zLine && zLine[0]=='.' && nSql==0 ){
drh67505e72002-04-19 12:34:06 +0000961 int rc = do_meta_command(zLine, db, p);
drhdaffd0e2001-04-11 14:28:42 +0000962 free(zLine);
drh67505e72002-04-19 12:34:06 +0000963 if( rc ) break;
drhdaffd0e2001-04-11 14:28:42 +0000964 continue;
965 }
drha9b17162003-04-29 18:01:28 +0000966 if( _is_command_terminator(zLine) ){
967 strcpy(zLine,";");
968 }
drhdaffd0e2001-04-11 14:28:42 +0000969 if( zSql==0 ){
970 int i;
971 for(i=0; zLine[i] && isspace(zLine[i]); i++){}
972 if( zLine[i]!=0 ){
973 nSql = strlen(zLine);
974 zSql = malloc( nSql+1 );
975 strcpy(zSql, zLine);
976 }
977 }else{
978 int len = strlen(zLine);
979 zSql = realloc( zSql, nSql + len + 2 );
980 if( zSql==0 ){
981 fprintf(stderr,"%s: out of memory!\n", Argv0);
982 exit(1);
983 }
984 strcpy(&zSql[nSql++], "\n");
985 strcpy(&zSql[nSql], zLine);
986 nSql += len;
987 }
988 free(zLine);
drh324ccef2003-02-05 14:06:20 +0000989 if( zSql && _ends_with_semicolon(zSql, nSql) && sqlite_complete(zSql) ){
drhdaffd0e2001-04-11 14:28:42 +0000990 p->cnt = 0;
drh7f953e22002-07-13 17:33:45 +0000991 rc = sqlite_exec(db, zSql, callback, p, &zErrMsg);
992 if( rc || zErrMsg ){
drhdaffd0e2001-04-11 14:28:42 +0000993 if( in!=0 && !p->echoOn ) printf("%s\n",zSql);
drh7f953e22002-07-13 17:33:45 +0000994 if( zErrMsg!=0 ){
995 printf("SQL error: %s\n", zErrMsg);
996 free(zErrMsg);
997 zErrMsg = 0;
998 }else{
999 printf("SQL error: %s\n", sqlite_error_string(rc));
1000 }
drhdaffd0e2001-04-11 14:28:42 +00001001 }
1002 free(zSql);
1003 zSql = 0;
1004 nSql = 0;
1005 }
1006 }
1007 if( zSql ){
drh70c7a4b2003-04-26 03:03:06 +00001008 if( !_all_whitespace(zSql) ) printf("Incomplete SQL: %s\n", zSql);
drhdaffd0e2001-04-11 14:28:42 +00001009 free(zSql);
1010 }
1011}
1012
drh67505e72002-04-19 12:34:06 +00001013/*
1014** Return a pathname which is the user's home directory. A
1015** 0 return indicates an error of some kind. Space to hold the
1016** resulting string is obtained from malloc(). The calling
1017** function should free the result.
1018*/
1019static char *find_home_dir(void){
1020 char *home_dir = NULL;
persicom7e2dfdd2002-04-18 02:46:52 +00001021
drh820f3812003-01-08 13:02:52 +00001022#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
drh67505e72002-04-19 12:34:06 +00001023 struct passwd *pwent;
1024 uid_t uid = getuid();
drhbd842ba2002-08-21 11:26:41 +00001025 if( (pwent=getpwuid(uid)) != NULL) {
1026 home_dir = pwent->pw_dir;
drh67505e72002-04-19 12:34:06 +00001027 }
1028#endif
1029
drh820f3812003-01-08 13:02:52 +00001030#ifdef __MACOS__
1031 char home_path[_MAX_PATH+1];
1032 home_dir = getcwd(home_path, _MAX_PATH);
1033#endif
1034
drh67505e72002-04-19 12:34:06 +00001035 if (!home_dir) {
1036 home_dir = getenv("HOME");
1037 if (!home_dir) {
1038 home_dir = getenv("HOMEPATH"); /* Windows? */
1039 }
1040 }
1041
drhe98d4fa2002-04-21 19:06:22 +00001042#if defined(_WIN32) || defined(WIN32)
1043 if (!home_dir) {
1044 home_dir = "c:";
1045 }
1046#endif
1047
drh67505e72002-04-19 12:34:06 +00001048 if( home_dir ){
1049 char *z = malloc( strlen(home_dir)+1 );
1050 if( z ) strcpy(z, home_dir);
1051 home_dir = z;
1052 }
drhe98d4fa2002-04-21 19:06:22 +00001053
drh67505e72002-04-19 12:34:06 +00001054 return home_dir;
1055}
1056
1057/*
1058** Read input from the file given by sqliterc_override. Or if that
1059** parameter is NULL, take input from ~/.sqliterc
1060*/
1061static void process_sqliterc(struct callback_data *p, char *sqliterc_override){
persicom7e2dfdd2002-04-18 02:46:52 +00001062 char *home_dir = NULL;
1063 char *sqliterc = sqliterc_override;
persicom7e2dfdd2002-04-18 02:46:52 +00001064 FILE *in = NULL;
1065
1066 if (sqliterc == NULL) {
drh67505e72002-04-19 12:34:06 +00001067 home_dir = find_home_dir();
drhe98d4fa2002-04-21 19:06:22 +00001068 if( home_dir==0 ){
1069 fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0);
1070 return;
1071 }
1072 sqliterc = malloc(strlen(home_dir) + 15);
persicom7e2dfdd2002-04-18 02:46:52 +00001073 if( sqliterc==0 ){
1074 fprintf(stderr,"%s: out of memory!\n", Argv0);
1075 exit(1);
1076 }
1077 sprintf(sqliterc,"%s/.sqliterc",home_dir);
drh67505e72002-04-19 12:34:06 +00001078 free(home_dir);
persicom7e2dfdd2002-04-18 02:46:52 +00001079 }
1080 in = fopen(sqliterc,"r");
drh4328c8b2003-04-26 02:50:11 +00001081 if(in && isatty(fileno(stdout))) {
persicom7e2dfdd2002-04-18 02:46:52 +00001082 printf("Loading resources from %s\n",sqliterc);
1083 process_input(p,in);
drhdd45df82002-04-18 12:39:03 +00001084 fclose(in);
persicom7e2dfdd2002-04-18 02:46:52 +00001085 }
1086 return;
1087}
1088
drh67505e72002-04-19 12:34:06 +00001089/*
1090** Initialize the state information in data
1091*/
persicom7e2dfdd2002-04-18 02:46:52 +00001092void main_init(struct callback_data *data) {
1093 memset(data, 0, sizeof(*data));
1094 data->mode = MODE_List;
1095 strcpy(data->separator,"|");
1096 data->showHeader = 0;
1097 strcpy(mainPrompt,"sqlite> ");
1098 strcpy(continuePrompt," ...> ");
1099}
1100
drh75897232000-05-29 14:26:00 +00001101int main(int argc, char **argv){
drh75897232000-05-29 14:26:00 +00001102 char *zErrMsg = 0;
1103 struct callback_data data;
persicom7e2dfdd2002-04-18 02:46:52 +00001104 int origArgc = argc;
1105 char **origArgv = argv;
drh75897232000-05-29 14:26:00 +00001106
drh820f3812003-01-08 13:02:52 +00001107#ifdef __MACOS__
1108 argc = ccommand(&argv);
1109 origArgc = argc;
1110 origArgv = argv;
1111#endif
1112
drhdaffd0e2001-04-11 14:28:42 +00001113 Argv0 = argv[0];
persicom7e2dfdd2002-04-18 02:46:52 +00001114 main_init(&data);
1115 process_sqliterc(&data,NULL);
1116
drh4c504392000-10-16 22:06:40 +00001117#ifdef SIGINT
1118 signal(SIGINT, interrupt_handler);
1119#endif
drh1e5d0e92000-05-31 23:33:17 +00001120 while( argc>=2 && argv[1][0]=='-' ){
persicom7e2dfdd2002-04-18 02:46:52 +00001121 if( argc>=3 && strcmp(argv[1],"-init")==0 ){
1122 /* If we get a -init to do, we have to pretend that
1123 ** it replaced the .sqliterc file. Soooo, in order to
1124 ** do that we need to start from scratch...*/
1125 main_init(&data);
1126
1127 /* treat this file as the sqliterc... */
1128 process_sqliterc(&data,argv[2]);
1129
1130 /* fix up the command line so we do not re-read
1131 ** the option next time around... */
1132 {
1133 int i = 1;
1134 for(i=1;i<=argc-2;i++) {
1135 argv[i] = argv[i+2];
1136 }
1137 }
1138 origArgc-=2;
1139
1140 /* and reset the command line options to be re-read.*/
1141 argv = origArgv;
1142 argc = origArgc;
1143
1144 }else if( strcmp(argv[1],"-html")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001145 data.mode = MODE_Html;
1146 argc--;
1147 argv++;
1148 }else if( strcmp(argv[1],"-list")==0 ){
1149 data.mode = MODE_List;
1150 argc--;
1151 argv++;
1152 }else if( strcmp(argv[1],"-line")==0 ){
1153 data.mode = MODE_Line;
1154 argc--;
1155 argv++;
drh8b32e172002-04-08 02:42:57 +00001156 }else if( strcmp(argv[1],"-column")==0 ){
1157 data.mode = MODE_Column;
1158 argc--;
1159 argv++;
drh7613bfa2002-01-22 12:39:24 +00001160 }else if( argc>=3 && strcmp(argv[1],"-separator")==0 ){
drhbed86902000-06-02 13:27:59 +00001161 sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[2]);
drh1e5d0e92000-05-31 23:33:17 +00001162 argc -= 2;
1163 argv += 2;
persicom7e2dfdd2002-04-18 02:46:52 +00001164 }else if( argc>=3 && strcmp(argv[1],"-nullvalue")==0 ){
1165 sprintf(data.nullvalue,"%.*s",(int)sizeof(data.nullvalue)-1,argv[2]);
1166 argc -= 2;
1167 argv += 2;
drh1e5d0e92000-05-31 23:33:17 +00001168 }else if( strcmp(argv[1],"-header")==0 ){
1169 data.showHeader = 1;
1170 argc--;
1171 argv++;
1172 }else if( strcmp(argv[1],"-noheader")==0 ){
1173 data.showHeader = 0;
1174 argc--;
1175 argv++;
drh660f68d2001-01-04 14:27:07 +00001176 }else if( strcmp(argv[1],"-echo")==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001177 data.echoOn = 1;
drh660f68d2001-01-04 14:27:07 +00001178 argc--;
1179 argv++;
drh1e5d0e92000-05-31 23:33:17 +00001180 }else{
drhdaffd0e2001-04-11 14:28:42 +00001181 fprintf(stderr,"%s: unknown option: %s\n", Argv0, argv[1]);
drh1e5d0e92000-05-31 23:33:17 +00001182 return 1;
1183 }
1184 }
drh75897232000-05-29 14:26:00 +00001185 if( argc!=2 && argc!=3 ){
drhdaffd0e2001-04-11 14:28:42 +00001186 fprintf(stderr,"Usage: %s ?OPTIONS? FILENAME ?SQL?\n", Argv0);
drh75897232000-05-29 14:26:00 +00001187 exit(1);
1188 }
drh4c653a02000-06-07 01:27:47 +00001189 data.db = db = sqlite_open(argv[1], 0666, &zErrMsg);
drh75897232000-05-29 14:26:00 +00001190 if( db==0 ){
drh167a4b12000-08-17 09:49:59 +00001191 data.db = db = sqlite_open(argv[1], 0444, &zErrMsg);
1192 if( db==0 ){
1193 if( zErrMsg ){
1194 fprintf(stderr,"Unable to open database \"%s\": %s\n", argv[1],zErrMsg);
1195 }else{
1196 fprintf(stderr,"Unable to open database %s\n", argv[1]);
1197 }
1198 exit(1);
drhd1dedb82000-06-05 02:07:04 +00001199 }else{
drh80afdca2000-08-22 13:27:22 +00001200 fprintf(stderr,"Database \"%s\" opened READ ONLY!\n", argv[1]);
drhd1dedb82000-06-05 02:07:04 +00001201 }
drh75897232000-05-29 14:26:00 +00001202 }
drh75897232000-05-29 14:26:00 +00001203 data.out = stdout;
1204 if( argc==3 ){
drh6ff13852001-11-25 13:18:23 +00001205 if( argv[2][0]=='.' ){
1206 do_meta_command(argv[2], db, &data);
1207 exit(0);
1208 }else{
1209 int rc;
1210 rc = sqlite_exec(db, argv[2], callback, &data, &zErrMsg);
1211 if( rc!=0 && zErrMsg!=0 ){
1212 fprintf(stderr,"SQL error: %s\n", zErrMsg);
1213 exit(1);
1214 }
drh75897232000-05-29 14:26:00 +00001215 }
1216 }else{
drh5cf590c2003-04-24 01:45:04 +00001217 if( isatty(fileno(stdout)) && isatty(fileno(stdin)) ){
drh67505e72002-04-19 12:34:06 +00001218 char *zHome;
1219 char *zHistory = 0;
drh75897232000-05-29 14:26:00 +00001220 printf(
drhb217a572000-08-22 13:40:18 +00001221 "SQLite version %s\n"
1222 "Enter \".help\" for instructions\n",
1223 sqlite_version
drh75897232000-05-29 14:26:00 +00001224 );
drh67505e72002-04-19 12:34:06 +00001225 zHome = find_home_dir();
1226 if( zHome && (zHistory = malloc(strlen(zHome)+20))!=0 ){
1227 sprintf(zHistory,"%s/.sqlite_history", zHome);
1228 }
1229 if( zHistory ) read_history(zHistory);
drhdaffd0e2001-04-11 14:28:42 +00001230 process_input(&data, 0);
drh67505e72002-04-19 12:34:06 +00001231 if( zHistory ){
1232 stifle_history(100);
1233 write_history(zHistory);
1234 }
drhdaffd0e2001-04-11 14:28:42 +00001235 }else{
1236 process_input(&data, stdin);
drh75897232000-05-29 14:26:00 +00001237 }
1238 }
drh33048c02001-10-01 14:29:22 +00001239 set_table_name(&data, 0);
drh75897232000-05-29 14:26:00 +00001240 sqlite_close(db);
1241 return 0;
1242}