blob: 7e93dca5a6723167859ed7a30bd04c83f4c25d60 [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**
drh324ccef2003-02-05 14:06:20 +000015** $Id: shell.c,v 1.66 2003/02/05 14:06:20 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
50/*
drh4c504392000-10-16 22:06:40 +000051** The following is the open SQLite database. We make a pointer
52** to this database a static variable so that it can be accessed
53** by the SIGINT handler to interrupt database processing.
54*/
55static sqlite *db = 0;
56
57/*
drh67505e72002-04-19 12:34:06 +000058** True if an interrupt (Control-C) has been received.
59*/
60static int seenInterrupt = 0;
61
62/*
persicom7e2dfdd2002-04-18 02:46:52 +000063** This is the name of our program. It is set in main(), used
64** in a number of other places, mostly for error messages.
65*/
66static char *Argv0;
67
68/*
69** Prompt strings. Initialized in main. Settable with
70** .prompt main continue
71*/
72static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
73static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
74
75/*
drh8e7e7a22000-05-30 18:45:23 +000076** This routine reads a line of text from standard input, stores
77** the text in memory obtained from malloc() and returns a pointer
78** to the text. NULL is returned at end of file, or if malloc()
79** fails.
80**
81** The interface is like "readline" but no command-line editing
82** is done.
83*/
drhdaffd0e2001-04-11 14:28:42 +000084static char *getline(char *zPrompt, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +000085 char *zLine;
86 int nLine;
drh8e7e7a22000-05-30 18:45:23 +000087 int n;
88 int eol;
89
90 if( zPrompt && *zPrompt ){
91 printf("%s",zPrompt);
92 fflush(stdout);
93 }
94 nLine = 100;
95 zLine = malloc( nLine );
96 if( zLine==0 ) return 0;
97 n = 0;
98 eol = 0;
99 while( !eol ){
100 if( n+100>nLine ){
101 nLine = nLine*2 + 100;
102 zLine = realloc(zLine, nLine);
103 if( zLine==0 ) return 0;
104 }
drhdaffd0e2001-04-11 14:28:42 +0000105 if( fgets(&zLine[n], nLine - n, in)==0 ){
drh8e7e7a22000-05-30 18:45:23 +0000106 if( n==0 ){
107 free(zLine);
108 return 0;
109 }
110 zLine[n] = 0;
111 eol = 1;
112 break;
113 }
114 while( zLine[n] ){ n++; }
115 if( n>0 && zLine[n-1]=='\n' ){
116 n--;
117 zLine[n] = 0;
118 eol = 1;
119 }
120 }
121 zLine = realloc( zLine, n+1 );
122 return zLine;
123}
124
125/*
126** Retrieve a single line of input text. "isatty" is true if text
127** is coming from a terminal. In that case, we issue a prompt and
128** attempt to use "readline" for command-line editing. If "isatty"
drhaacc5432002-01-06 17:07:40 +0000129** is false, use "getline" instead of "readline" and issue no prompt.
drh8e7e7a22000-05-30 18:45:23 +0000130**
131** zPrior is a string of prior text retrieved. If not the empty
132** string, then issue a continuation prompt.
133*/
drhdaffd0e2001-04-11 14:28:42 +0000134static char *one_input_line(const char *zPrior, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +0000135 char *zPrompt;
136 char *zResult;
drhdaffd0e2001-04-11 14:28:42 +0000137 if( in!=0 ){
138 return getline(0, in);
drh8e7e7a22000-05-30 18:45:23 +0000139 }
140 if( zPrior && zPrior[0] ){
persicom7e2dfdd2002-04-18 02:46:52 +0000141 zPrompt = continuePrompt;
drh8e7e7a22000-05-30 18:45:23 +0000142 }else{
persicom7e2dfdd2002-04-18 02:46:52 +0000143 zPrompt = mainPrompt;
drh8e7e7a22000-05-30 18:45:23 +0000144 }
145 zResult = readline(zPrompt);
drh2dfbbca2000-07-28 14:32:48 +0000146 if( zResult ) add_history(zResult);
drh8e7e7a22000-05-30 18:45:23 +0000147 return zResult;
148}
149
persicom7e2dfdd2002-04-18 02:46:52 +0000150struct previous_mode_data {
151 int valid; /* Is there legit data in here? */
152 int mode;
153 int showHeader;
154 int colWidth[100];
155};
drh8e7e7a22000-05-30 18:45:23 +0000156/*
drh75897232000-05-29 14:26:00 +0000157** An pointer to an instance of this structure is passed from
158** the main program to the callback. This is used to communicate
159** state and mode information.
160*/
161struct callback_data {
drh28bd4bc2000-06-15 15:57:22 +0000162 sqlite *db; /* The database */
drhdaffd0e2001-04-11 14:28:42 +0000163 int echoOn; /* True to echo input commands */
drh28bd4bc2000-06-15 15:57:22 +0000164 int cnt; /* Number of records displayed so far */
165 FILE *out; /* Write results here */
166 int mode; /* An output mode setting */
167 int showHeader; /* True to show column names in List or Column mode */
drh33048c02001-10-01 14:29:22 +0000168 char *zDestTable; /* Name of destination table when MODE_Insert */
drh28bd4bc2000-06-15 15:57:22 +0000169 char separator[20]; /* Separator character for MODE_List */
drha0c66f52000-07-29 13:20:21 +0000170 int colWidth[100]; /* Requested width of each column when in column mode*/
171 int actualWidth[100]; /* Actual width of each column */
persicom7e2dfdd2002-04-18 02:46:52 +0000172 char nullvalue[20]; /* The text to print when a NULL comes back from the database */
173 struct previous_mode_data explainPrev;
174 /* Holds the mode information just before .explain ON */
175 char outfile[FILENAME_MAX];
176 /* Filename for *out */
drh75897232000-05-29 14:26:00 +0000177};
178
179/*
180** These are the allowed modes.
181*/
drh967e8b72000-06-21 13:59:10 +0000182#define MODE_Line 0 /* One column per line. Blank line between records */
drh75897232000-05-29 14:26:00 +0000183#define MODE_Column 1 /* One record per line in neat columns */
184#define MODE_List 2 /* One record per line with a separator */
drhe3710332000-09-29 13:30:53 +0000185#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
186#define MODE_Html 4 /* Generate an XHTML table */
187#define MODE_Insert 5 /* Generate SQL "insert" statements */
persicom7e2dfdd2002-04-18 02:46:52 +0000188#define MODE_NUM_OF 6
189
190char *modeDescr[MODE_NUM_OF] = {
191 "line",
192 "column",
193 "list",
194 "semi",
195 "html",
196 "insert"
197};
drh75897232000-05-29 14:26:00 +0000198
199/*
200** Number of elements in an array
201*/
202#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
203
204/*
drh28bd4bc2000-06-15 15:57:22 +0000205** Return TRUE if the string supplied is a number of some kinds.
206*/
207static int is_numeric(const char *z){
208 int seen_digit = 0;
209 if( *z=='-' || *z=='+' ){
210 z++;
211 }
persicom1d0b8722002-04-18 02:53:04 +0000212 while( isdigit(*z) ){
drh28bd4bc2000-06-15 15:57:22 +0000213 seen_digit = 1;
214 z++;
215 }
216 if( seen_digit && *z=='.' ){
217 z++;
218 while( isdigit(*z) ){ z++; }
219 }
220 if( seen_digit && (*z=='e' || *z=='E')
221 && (isdigit(z[1]) || ((z[1]=='-' || z[1]=='+') && isdigit(z[2])))
222 ){
223 z+=2;
224 while( isdigit(*z) ){ z++; }
225 }
226 return seen_digit && *z==0;
227}
228
229/*
230** Output the given string as a quoted string using SQL quoting conventions.
231*/
232static void output_quoted_string(FILE *out, const char *z){
233 int i;
234 int nSingle = 0;
drh28bd4bc2000-06-15 15:57:22 +0000235 for(i=0; z[i]; i++){
236 if( z[i]=='\'' ) nSingle++;
drh28bd4bc2000-06-15 15:57:22 +0000237 }
238 if( nSingle==0 ){
239 fprintf(out,"'%s'",z);
drh28bd4bc2000-06-15 15:57:22 +0000240 }else{
241 fprintf(out,"'");
242 while( *z ){
243 for(i=0; z[i] && z[i]!='\''; i++){}
244 if( i==0 ){
245 fprintf(out,"''");
246 z++;
247 }else if( z[i]=='\'' ){
248 fprintf(out,"%.*s''",i,z);
249 z += i+1;
250 }else{
drhcd7d2732002-02-26 23:24:26 +0000251 fprintf(out,"%s",z);
drh28bd4bc2000-06-15 15:57:22 +0000252 break;
253 }
254 }
drhcd7d2732002-02-26 23:24:26 +0000255 fprintf(out,"'");
drh28bd4bc2000-06-15 15:57:22 +0000256 }
257}
258
259/*
drhc08a4f12000-06-15 16:49:48 +0000260** Output the given string with characters that are special to
261** HTML escaped.
262*/
263static void output_html_string(FILE *out, const char *z){
264 int i;
265 while( *z ){
266 for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){}
267 if( i>0 ){
268 fprintf(out,"%.*s",i,z);
269 }
270 if( z[i]=='<' ){
271 fprintf(out,"&lt;");
272 }else if( z[i]=='&' ){
273 fprintf(out,"&amp;");
274 }else{
275 break;
276 }
277 z += i + 1;
278 }
279}
280
281/*
drh4c504392000-10-16 22:06:40 +0000282** This routine runs when the user presses Ctrl-C
283*/
284static void interrupt_handler(int NotUsed){
drh67505e72002-04-19 12:34:06 +0000285 seenInterrupt = 1;
drh4c504392000-10-16 22:06:40 +0000286 if( db ) sqlite_interrupt(db);
287}
288
289/*
drh75897232000-05-29 14:26:00 +0000290** This is the callback routine that the SQLite library
291** invokes for each row of a query result.
292*/
293static int callback(void *pArg, int nArg, char **azArg, char **azCol){
294 int i;
295 struct callback_data *p = (struct callback_data*)pArg;
296 switch( p->mode ){
297 case MODE_Line: {
drhe3710332000-09-29 13:30:53 +0000298 int w = 5;
drh6a535342001-10-19 16:44:56 +0000299 if( azArg==0 ) break;
drhe3710332000-09-29 13:30:53 +0000300 for(i=0; i<nArg; i++){
301 int len = strlen(azCol[i]);
302 if( len>w ) w = len;
303 }
drh75897232000-05-29 14:26:00 +0000304 if( p->cnt++>0 ) fprintf(p->out,"\n");
305 for(i=0; i<nArg; i++){
persicom7e2dfdd2002-04-18 02:46:52 +0000306 fprintf(p->out,"%*s = %s\n", w, azCol[i], azArg[i] ? azArg[i] : p->nullvalue);
drh75897232000-05-29 14:26:00 +0000307 }
308 break;
309 }
310 case MODE_Column: {
drha0c66f52000-07-29 13:20:21 +0000311 if( p->cnt++==0 ){
drh75897232000-05-29 14:26:00 +0000312 for(i=0; i<nArg; i++){
drha0c66f52000-07-29 13:20:21 +0000313 int w, n;
314 if( i<ArraySize(p->colWidth) ){
drh75897232000-05-29 14:26:00 +0000315 w = p->colWidth[i];
316 }else{
drha0c66f52000-07-29 13:20:21 +0000317 w = 0;
drh75897232000-05-29 14:26:00 +0000318 }
drha0c66f52000-07-29 13:20:21 +0000319 if( w<=0 ){
drhff6e9112000-08-28 16:21:58 +0000320 w = strlen(azCol[i] ? azCol[i] : "");
drha0c66f52000-07-29 13:20:21 +0000321 if( w<10 ) w = 10;
persicom7e2dfdd2002-04-18 02:46:52 +0000322 n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue);
drha0c66f52000-07-29 13:20:21 +0000323 if( w<n ) w = n;
324 }
325 if( i<ArraySize(p->actualWidth) ){
persicom1d0b8722002-04-18 02:53:04 +0000326 p->actualWidth[i] = w;
drha0c66f52000-07-29 13:20:21 +0000327 }
328 if( p->showHeader ){
329 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
330 }
331 }
332 if( p->showHeader ){
333 for(i=0; i<nArg; i++){
334 int w;
335 if( i<ArraySize(p->actualWidth) ){
336 w = p->actualWidth[i];
337 }else{
338 w = 10;
339 }
340 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
341 "----------------------------------------------------------",
342 i==nArg-1 ? "\n": " ");
343 }
drh75897232000-05-29 14:26:00 +0000344 }
345 }
drh6a535342001-10-19 16:44:56 +0000346 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000347 for(i=0; i<nArg; i++){
348 int w;
drha0c66f52000-07-29 13:20:21 +0000349 if( i<ArraySize(p->actualWidth) ){
350 w = p->actualWidth[i];
drh75897232000-05-29 14:26:00 +0000351 }else{
352 w = 10;
353 }
drhc61053b2000-06-04 12:58:36 +0000354 fprintf(p->out,"%-*.*s%s",w,w,
persicom7e2dfdd2002-04-18 02:46:52 +0000355 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
drh75897232000-05-29 14:26:00 +0000356 }
357 break;
358 }
drhe3710332000-09-29 13:30:53 +0000359 case MODE_Semi:
drh75897232000-05-29 14:26:00 +0000360 case MODE_List: {
361 if( p->cnt++==0 && p->showHeader ){
362 for(i=0; i<nArg; i++){
363 fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
364 }
365 }
drh6a535342001-10-19 16:44:56 +0000366 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000367 for(i=0; i<nArg; i++){
drh4c653a02000-06-07 01:27:47 +0000368 char *z = azArg[i];
persicom7e2dfdd2002-04-18 02:46:52 +0000369 if( z==0 ) z = p->nullvalue;
drh71172c52002-01-24 00:00:21 +0000370 fprintf(p->out, "%s", z);
drhe3710332000-09-29 13:30:53 +0000371 if( i<nArg-1 ){
372 fprintf(p->out, "%s", p->separator);
373 }else if( p->mode==MODE_Semi ){
374 fprintf(p->out, ";\n");
375 }else{
376 fprintf(p->out, "\n");
377 }
drh75897232000-05-29 14:26:00 +0000378 }
379 break;
380 }
drh1e5d0e92000-05-31 23:33:17 +0000381 case MODE_Html: {
382 if( p->cnt++==0 && p->showHeader ){
383 fprintf(p->out,"<TR>");
384 for(i=0; i<nArg; i++){
385 fprintf(p->out,"<TH>%s</TH>",azCol[i]);
386 }
387 fprintf(p->out,"</TR>\n");
388 }
drh6a535342001-10-19 16:44:56 +0000389 if( azArg==0 ) break;
drh28bd4bc2000-06-15 15:57:22 +0000390 fprintf(p->out,"<TR>");
drh1e5d0e92000-05-31 23:33:17 +0000391 for(i=0; i<nArg; i++){
drhc08a4f12000-06-15 16:49:48 +0000392 fprintf(p->out,"<TD>");
persicom7e2dfdd2002-04-18 02:46:52 +0000393 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
drhc08a4f12000-06-15 16:49:48 +0000394 fprintf(p->out,"</TD>\n");
drh1e5d0e92000-05-31 23:33:17 +0000395 }
drh7d686b22002-11-11 13:56:47 +0000396 fprintf(p->out,"</TR>\n");
drh1e5d0e92000-05-31 23:33:17 +0000397 break;
398 }
drh28bd4bc2000-06-15 15:57:22 +0000399 case MODE_Insert: {
drh6a535342001-10-19 16:44:56 +0000400 if( azArg==0 ) break;
drh33048c02001-10-01 14:29:22 +0000401 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
drh28bd4bc2000-06-15 15:57:22 +0000402 for(i=0; i<nArg; i++){
403 char *zSep = i>0 ? ",": "";
404 if( azArg[i]==0 ){
405 fprintf(p->out,"%sNULL",zSep);
406 }else if( is_numeric(azArg[i]) ){
407 fprintf(p->out,"%s%s",zSep, azArg[i]);
408 }else{
409 if( zSep[0] ) fprintf(p->out,"%s",zSep);
410 output_quoted_string(p->out, azArg[i]);
411 }
412 }
413 fprintf(p->out,");\n");
drh6a535342001-10-19 16:44:56 +0000414 break;
drh28bd4bc2000-06-15 15:57:22 +0000415 }
persicom1d0b8722002-04-18 02:53:04 +0000416 }
drh75897232000-05-29 14:26:00 +0000417 return 0;
418}
419
420/*
drh33048c02001-10-01 14:29:22 +0000421** Set the destination table field of the callback_data structure to
422** the name of the table given. Escape any quote characters in the
423** table name.
424*/
425static void set_table_name(struct callback_data *p, const char *zName){
426 int i, n;
427 int needQuote;
428 char *z;
429
430 if( p->zDestTable ){
431 free(p->zDestTable);
432 p->zDestTable = 0;
433 }
434 if( zName==0 ) return;
435 needQuote = !isalpha(*zName) && *zName!='_';
436 for(i=n=0; zName[i]; i++, n++){
437 if( !isalnum(zName[i]) && zName[i]!='_' ){
438 needQuote = 1;
439 if( zName[i]=='\'' ) n++;
440 }
441 }
442 if( needQuote ) n += 2;
443 z = p->zDestTable = malloc( n+1 );
444 if( z==0 ){
445 fprintf(stderr,"Out of memory!\n");
446 exit(1);
447 }
448 n = 0;
449 if( needQuote ) z[n++] = '\'';
450 for(i=0; zName[i]; i++){
451 z[n++] = zName[i];
452 if( zName[i]=='\'' ) z[n++] = '\'';
453 }
454 if( needQuote ) z[n++] = '\'';
455 z[n] = 0;
456}
457
458/*
drh4c653a02000-06-07 01:27:47 +0000459** This is a different callback routine used for dumping the database.
460** Each row received by this callback consists of a table name,
461** the table type ("index" or "table") and SQL to create the table.
462** This routine should print text sufficient to recreate the table.
463*/
464static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
drhdaffd0e2001-04-11 14:28:42 +0000465 struct callback_data *p = (struct callback_data *)pArg;
drh4c653a02000-06-07 01:27:47 +0000466 if( nArg!=3 ) return 1;
drhdaffd0e2001-04-11 14:28:42 +0000467 fprintf(p->out, "%s;\n", azArg[2]);
drh4c653a02000-06-07 01:27:47 +0000468 if( strcmp(azArg[1],"table")==0 ){
469 struct callback_data d2;
drhdaffd0e2001-04-11 14:28:42 +0000470 d2 = *p;
drh33048c02001-10-01 14:29:22 +0000471 d2.mode = MODE_Insert;
472 d2.zDestTable = 0;
473 set_table_name(&d2, azArg[0]);
persicom1d0b8722002-04-18 02:53:04 +0000474 sqlite_exec_printf(p->db,
drha18c5682000-10-08 22:20:57 +0000475 "SELECT * FROM '%q'",
476 callback, &d2, 0, azArg[0]
477 );
drh33048c02001-10-01 14:29:22 +0000478 set_table_name(&d2, 0);
drh4c653a02000-06-07 01:27:47 +0000479 }
drh4c653a02000-06-07 01:27:47 +0000480 return 0;
481}
482
483/*
drh75897232000-05-29 14:26:00 +0000484** Text of a help message
485*/
persicom1d0b8722002-04-18 02:53:04 +0000486static char zHelp[] =
drh4c653a02000-06-07 01:27:47 +0000487 ".dump ?TABLE? ... Dump the database in an text format\n"
drhdaffd0e2001-04-11 14:28:42 +0000488 ".echo ON|OFF Turn command echo on or off\n"
drh75897232000-05-29 14:26:00 +0000489 ".exit Exit this program\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000490 ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000491 ".header(s) ON|OFF Turn display of headers on or off\n"
drh75897232000-05-29 14:26:00 +0000492 ".help Show this message\n"
493 ".indices TABLE Show names of all indices on TABLE\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000494 ".mode MODE Set mode to one of \"line(s)\", \"column(s)\", \n"
drhe3710332000-09-29 13:30:53 +0000495 " \"insert\", \"list\", or \"html\"\n"
drhc08a4f12000-06-15 16:49:48 +0000496 ".mode insert TABLE Generate SQL insert statements for TABLE\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000497 ".nullvalue STRING Print STRING instead of nothing for NULL data\n"
drh411995d2002-06-25 19:31:18 +0000498 ".openaux FILENAME Use FILENAME to hold TEMP tables\n"
drh75897232000-05-29 14:26:00 +0000499 ".output FILENAME Send output to FILENAME\n"
500 ".output stdout Send output to the screen\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000501 ".prompt MAIN CONTINUE Replace the standard prompts\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000502 ".quit Exit this program\n"
drhdaffd0e2001-04-11 14:28:42 +0000503 ".read FILENAME Execute SQL in FILENAME\n"
drh75897232000-05-29 14:26:00 +0000504 ".schema ?TABLE? Show the CREATE statements\n"
505 ".separator STRING Change separator string for \"list\" mode\n"
drhdd45df82002-04-18 12:39:03 +0000506 ".show Show the current values for various settings\n"
drha50da102000-08-08 20:19:09 +0000507 ".tables ?PATTERN? List names of tables matching a pattern\n"
drh2dfbbca2000-07-28 14:32:48 +0000508 ".timeout MS Try opening locked tables for MS milliseconds\n"
drh75897232000-05-29 14:26:00 +0000509 ".width NUM NUM ... Set column widths for \"column\" mode\n"
510;
511
drhdaffd0e2001-04-11 14:28:42 +0000512/* Forward reference */
513static void process_input(struct callback_data *p, FILE *in);
514
drh75897232000-05-29 14:26:00 +0000515/*
516** If an input line begins with "." then invoke this routine to
517** process that line.
drh67505e72002-04-19 12:34:06 +0000518**
519** Return 1 to exit and 0 to continue.
drh75897232000-05-29 14:26:00 +0000520*/
drh67505e72002-04-19 12:34:06 +0000521static int do_meta_command(char *zLine, sqlite *db, struct callback_data *p){
drh75897232000-05-29 14:26:00 +0000522 int i = 1;
523 int nArg = 0;
524 int n, c;
drh67505e72002-04-19 12:34:06 +0000525 int rc = 0;
drh75897232000-05-29 14:26:00 +0000526 char *azArg[50];
527
528 /* Parse the input line into tokens.
529 */
530 while( zLine[i] && nArg<ArraySize(azArg) ){
531 while( isspace(zLine[i]) ){ i++; }
532 if( zLine[i]=='\'' || zLine[i]=='"' ){
533 int delim = zLine[i++];
534 azArg[nArg++] = &zLine[i];
535 while( zLine[i] && zLine[i]!=delim ){ i++; }
536 if( zLine[i]==delim ){
537 zLine[i++] = 0;
538 }
539 }else{
540 azArg[nArg++] = &zLine[i];
541 while( zLine[i] && !isspace(zLine[i]) ){ i++; }
542 if( zLine[i] ) zLine[i++] = 0;
543 }
544 }
545
546 /* Process the input line.
547 */
drh67505e72002-04-19 12:34:06 +0000548 if( nArg==0 ) return rc;
drh75897232000-05-29 14:26:00 +0000549 n = strlen(azArg[0]);
550 c = azArg[0][0];
drh4c653a02000-06-07 01:27:47 +0000551 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
552 char *zErrMsg = 0;
drh33048c02001-10-01 14:29:22 +0000553 fprintf(p->out, "BEGIN TRANSACTION;\n");
drh4c653a02000-06-07 01:27:47 +0000554 if( nArg==1 ){
drha18c5682000-10-08 22:20:57 +0000555 sqlite_exec(db,
556 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000557 "WHERE type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000558 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000559 dump_callback, p, &zErrMsg
560 );
drh4c653a02000-06-07 01:27:47 +0000561 }else{
562 int i;
563 for(i=1; i<nArg && zErrMsg==0; i++){
persicom1d0b8722002-04-18 02:53:04 +0000564 sqlite_exec_printf(db,
drha18c5682000-10-08 22:20:57 +0000565 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000566 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000567 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000568 dump_callback, p, &zErrMsg, azArg[i]
569 );
drh4c653a02000-06-07 01:27:47 +0000570 }
571 }
572 if( zErrMsg ){
573 fprintf(stderr,"Error: %s\n", zErrMsg);
574 free(zErrMsg);
drh33048c02001-10-01 14:29:22 +0000575 }else{
576 fprintf(p->out, "COMMIT;\n");
drh4c653a02000-06-07 01:27:47 +0000577 }
578 }else
drh75897232000-05-29 14:26:00 +0000579
drhdaffd0e2001-04-11 14:28:42 +0000580 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
581 int j;
582 char *z = azArg[1];
583 int val = atoi(azArg[1]);
584 for(j=0; z[j]; j++){
585 if( isupper(z[j]) ) z[j] = tolower(z[j]);
586 }
587 if( strcmp(z,"on")==0 ){
588 val = 1;
589 }else if( strcmp(z,"yes")==0 ){
590 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000591 }
drhdaffd0e2001-04-11 14:28:42 +0000592 p->echoOn = val;
593 }else
594
drh75897232000-05-29 14:26:00 +0000595 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
drh67505e72002-04-19 12:34:06 +0000596 rc = 1;
drh75897232000-05-29 14:26:00 +0000597 }else
598
drhdd45df82002-04-18 12:39:03 +0000599 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +0000600 int j;
drhdd45df82002-04-18 12:39:03 +0000601 char *z = nArg>=2 ? azArg[1] : "1";
602 int val = atoi(z);
persicom7e2dfdd2002-04-18 02:46:52 +0000603 for(j=0; z[j]; j++){
604 if( isupper(z[j]) ) z[j] = tolower(z[j]);
605 }
606 if( strcmp(z,"on")==0 ){
607 val = 1;
608 }else if( strcmp(z,"yes")==0 ){
609 val = 1;
610 }
611 if(val == 1) {
612 if(!p->explainPrev.valid) {
613 p->explainPrev.valid = 1;
614 p->explainPrev.mode = p->mode;
615 p->explainPrev.showHeader = p->showHeader;
616 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
617 }
618 /* We could put this code under the !p->explainValid
619 ** condition so that it does not execute if we are already in
620 ** explain mode. However, always executing it allows us an easy
621 ** was to reset to explain mode in case the user previously
622 ** did an .explain followed by a .width, .mode or .header
623 ** command.
624 */
625 p->mode = MODE_Column;
626 p->showHeader = 1;
627 memset(p->colWidth,0,ArraySize(p->colWidth));
628 p->colWidth[0] = 4;
629 p->colWidth[1] = 12;
630 p->colWidth[2] = 10;
631 p->colWidth[3] = 10;
632 p->colWidth[4] = 35;
633 }else if (p->explainPrev.valid) {
634 p->explainPrev.valid = 0;
635 p->mode = p->explainPrev.mode;
636 p->showHeader = p->explainPrev.showHeader;
637 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
638 }
drh75897232000-05-29 14:26:00 +0000639 }else
640
persicom7e2dfdd2002-04-18 02:46:52 +0000641 if( c=='h' && (strncmp(azArg[0], "header", n)==0
642 ||
643 strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){
drh75897232000-05-29 14:26:00 +0000644 int j;
645 char *z = azArg[1];
646 int val = atoi(azArg[1]);
647 for(j=0; z[j]; j++){
648 if( isupper(z[j]) ) z[j] = tolower(z[j]);
649 }
650 if( strcmp(z,"on")==0 ){
651 val = 1;
652 }else if( strcmp(z,"yes")==0 ){
653 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000654 }
drh75897232000-05-29 14:26:00 +0000655 p->showHeader = val;
656 }else
657
658 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
659 fprintf(stderr,zHelp);
660 }else
661
662 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
663 struct callback_data data;
664 char *zErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000665 memcpy(&data, p, sizeof(data));
666 data.showHeader = 0;
667 data.mode = MODE_List;
persicom1d0b8722002-04-18 02:53:04 +0000668 sqlite_exec_printf(db,
drha18c5682000-10-08 22:20:57 +0000669 "SELECT name FROM sqlite_master "
670 "WHERE type='index' AND tbl_name LIKE '%q' "
drhe0bc4042002-06-25 01:09:11 +0000671 "UNION ALL "
672 "SELECT name FROM sqlite_temp_master "
673 "WHERE type='index' AND tbl_name LIKE '%q' "
674 "ORDER BY 1",
675 callback, &data, &zErrMsg, azArg[1], azArg[1]
drha18c5682000-10-08 22:20:57 +0000676 );
drh75897232000-05-29 14:26:00 +0000677 if( zErrMsg ){
678 fprintf(stderr,"Error: %s\n", zErrMsg);
679 free(zErrMsg);
680 }
681 }else
682
drh28bd4bc2000-06-15 15:57:22 +0000683 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
drh75897232000-05-29 14:26:00 +0000684 int n2 = strlen(azArg[1]);
persicom7e2dfdd2002-04-18 02:46:52 +0000685 if( strncmp(azArg[1],"line",n2)==0
686 ||
687 strncmp(azArg[1],"lines",n2)==0 ){
drh75897232000-05-29 14:26:00 +0000688 p->mode = MODE_Line;
persicom7e2dfdd2002-04-18 02:46:52 +0000689 }else if( strncmp(azArg[1],"column",n2)==0
690 ||
691 strncmp(azArg[1],"columns",n2)==0 ){
drh75897232000-05-29 14:26:00 +0000692 p->mode = MODE_Column;
693 }else if( strncmp(azArg[1],"list",n2)==0 ){
694 p->mode = MODE_List;
drh1e5d0e92000-05-31 23:33:17 +0000695 }else if( strncmp(azArg[1],"html",n2)==0 ){
696 p->mode = MODE_Html;
drh28bd4bc2000-06-15 15:57:22 +0000697 }else if( strncmp(azArg[1],"insert",n2)==0 ){
698 p->mode = MODE_Insert;
699 if( nArg>=3 ){
drh33048c02001-10-01 14:29:22 +0000700 set_table_name(p, azArg[2]);
drh28bd4bc2000-06-15 15:57:22 +0000701 }else{
drh33048c02001-10-01 14:29:22 +0000702 set_table_name(p, "table");
drh28bd4bc2000-06-15 15:57:22 +0000703 }
drhdaffd0e2001-04-11 14:28:42 +0000704 }else {
705 fprintf(stderr,"mode should be on of: column html insert line list\n");
drh75897232000-05-29 14:26:00 +0000706 }
707 }else
708
persicom7e2dfdd2002-04-18 02:46:52 +0000709 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
710 sprintf(p->nullvalue, "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
711 }else
712
drh411995d2002-06-25 19:31:18 +0000713 if( c=='o' && strncmp(azArg[0], "openaux", n)==0 ){
714 char *zErrMsg = 0;
715 sqlite_open_aux_file(db, nArg>=2 ? azArg[1] : 0, &zErrMsg);
716 if( zErrMsg ){
717 fprintf(stderr,"Error: %s\n", zErrMsg);
718 free(zErrMsg);
719 }
720 }else
721
drh75897232000-05-29 14:26:00 +0000722 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
723 if( p->out!=stdout ){
724 fclose(p->out);
725 }
726 if( strcmp(azArg[1],"stdout")==0 ){
727 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +0000728 strcpy(p->outfile,"stdout");
drh75897232000-05-29 14:26:00 +0000729 }else{
730 p->out = fopen(azArg[1], "w");
731 if( p->out==0 ){
732 fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
733 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +0000734 } else {
735 strcpy(p->outfile,azArg[1]);
drh75897232000-05-29 14:26:00 +0000736 }
737 }
738 }else
739
drhdd45df82002-04-18 12:39:03 +0000740 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
persicom7e2dfdd2002-04-18 02:46:52 +0000741 if( nArg >= 2) {
742 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
743 }
744 if( nArg >= 3) {
745 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
746 }
747 }else
748
749 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
750 sqlite_close(db);
751 exit(0);
752 }else
753
drhdaffd0e2001-04-11 14:28:42 +0000754 if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
755 FILE *alt = fopen(azArg[1], "r");
756 if( alt==0 ){
757 fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
758 }else{
759 process_input(p, alt);
760 fclose(alt);
761 }
762 }else
763
drh75897232000-05-29 14:26:00 +0000764 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
765 struct callback_data data;
766 char *zErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000767 memcpy(&data, p, sizeof(data));
768 data.showHeader = 0;
drhe3710332000-09-29 13:30:53 +0000769 data.mode = MODE_Semi;
drh75897232000-05-29 14:26:00 +0000770 if( nArg>1 ){
drhff9821a2001-04-04 21:22:14 +0000771 extern int sqliteStrICmp(const char*,const char*);
drha18c5682000-10-08 22:20:57 +0000772 if( sqliteStrICmp(azArg[1],"sqlite_master")==0 ){
773 char *new_argv[2], *new_colv[2];
774 new_argv[0] = "CREATE TABLE sqlite_master (\n"
775 " type text,\n"
776 " name text,\n"
777 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000778 " rootpage integer,\n"
drha18c5682000-10-08 22:20:57 +0000779 " sql text\n"
780 ")";
781 new_argv[1] = 0;
782 new_colv[0] = "sql";
783 new_colv[1] = 0;
784 callback(&data, 1, new_argv, new_colv);
drhe0bc4042002-06-25 01:09:11 +0000785 }else if( sqliteStrICmp(azArg[1],"sqlite_temp_master")==0 ){
786 char *new_argv[2], *new_colv[2];
787 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
788 " type text,\n"
789 " name text,\n"
790 " tbl_name text,\n"
791 " rootpage integer,\n"
792 " sql text\n"
793 ")";
794 new_argv[1] = 0;
795 new_colv[0] = "sql";
796 new_colv[1] = 0;
797 callback(&data, 1, new_argv, new_colv);
drha18c5682000-10-08 22:20:57 +0000798 }else{
799 sqlite_exec_printf(db,
drhe0bc4042002-06-25 01:09:11 +0000800 "SELECT sql FROM "
801 " (SELECT * FROM sqlite_master UNION ALL"
802 " SELECT * FROM sqlite_temp_master) "
drh33048c02001-10-01 14:29:22 +0000803 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +0000804 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000805 callback, &data, &zErrMsg, azArg[1]);
806 }
drh75897232000-05-29 14:26:00 +0000807 }else{
drha18c5682000-10-08 22:20:57 +0000808 sqlite_exec(db,
drhe0bc4042002-06-25 01:09:11 +0000809 "SELECT sql FROM "
810 " (SELECT * FROM sqlite_master UNION ALL"
811 " SELECT * FROM sqlite_temp_master) "
drh33048c02001-10-01 14:29:22 +0000812 "WHERE type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +0000813 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000814 callback, &data, &zErrMsg
815 );
drh75897232000-05-29 14:26:00 +0000816 }
drh75897232000-05-29 14:26:00 +0000817 if( zErrMsg ){
818 fprintf(stderr,"Error: %s\n", zErrMsg);
819 free(zErrMsg);
820 }
821 }else
822
823 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
824 sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
825 }else
826
persicom7e2dfdd2002-04-18 02:46:52 +0000827 if( c=='s' && strncmp(azArg[0], "show", n)==0){
828 int i;
829 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
drh67505e72002-04-19 12:34:06 +0000830 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
drhdd45df82002-04-18 12:39:03 +0000831 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
persicom7e2dfdd2002-04-18 02:46:52 +0000832 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
833 fprintf(p->out,"%9.9s: %s\n","nullvalue", p->nullvalue);
drh67505e72002-04-19 12:34:06 +0000834 fprintf(p->out,"%9.9s: %s\n","output",
835 strlen(p->outfile) ? p->outfile : "stdout");
persicom7e2dfdd2002-04-18 02:46:52 +0000836 fprintf(p->out,"%9.9s: %s\n","separator", p->separator);
837 fprintf(p->out,"%9.9s: ","width");
838 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
839 fprintf(p->out,"%d ",p->colWidth[i]);
840 }
841 fprintf(p->out,"\n\n");
842 }else
843
drh2dfbbca2000-07-28 14:32:48 +0000844 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
drhe3710332000-09-29 13:30:53 +0000845 char **azResult;
846 int nRow, rc;
847 char *zErrMsg;
drha50da102000-08-08 20:19:09 +0000848 if( nArg==1 ){
drha18c5682000-10-08 22:20:57 +0000849 rc = sqlite_get_table(db,
drha50da102000-08-08 20:19:09 +0000850 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +0000851 "WHERE type IN ('table','view') "
drhe0bc4042002-06-25 01:09:11 +0000852 "UNION ALL "
853 "SELECT name FROM sqlite_temp_master "
854 "WHERE type IN ('table','view') "
855 "ORDER BY 1",
drha18c5682000-10-08 22:20:57 +0000856 &azResult, &nRow, 0, &zErrMsg
857 );
drha50da102000-08-08 20:19:09 +0000858 }else{
drha18c5682000-10-08 22:20:57 +0000859 rc = sqlite_get_table_printf(db,
drha50da102000-08-08 20:19:09 +0000860 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +0000861 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
drhe0bc4042002-06-25 01:09:11 +0000862 "UNION ALL "
863 "SELECT name FROM sqlite_temp_master "
864 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
865 "ORDER BY 1",
866 &azResult, &nRow, 0, &zErrMsg, azArg[1], azArg[1]
drha18c5682000-10-08 22:20:57 +0000867 );
drha50da102000-08-08 20:19:09 +0000868 }
drh75897232000-05-29 14:26:00 +0000869 if( zErrMsg ){
870 fprintf(stderr,"Error: %s\n", zErrMsg);
871 free(zErrMsg);
872 }
drhe3710332000-09-29 13:30:53 +0000873 if( rc==SQLITE_OK ){
874 int len, maxlen = 0;
875 int i, j;
876 int nPrintCol, nPrintRow;
877 for(i=1; i<=nRow; i++){
878 if( azResult[i]==0 ) continue;
879 len = strlen(azResult[i]);
880 if( len>maxlen ) maxlen = len;
881 }
882 nPrintCol = 80/(maxlen+2);
883 if( nPrintCol<1 ) nPrintCol = 1;
884 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
885 for(i=0; i<nPrintRow; i++){
886 for(j=i+1; j<=nRow; j+=nPrintRow){
887 char *zSp = j<=nPrintRow ? "" : " ";
888 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
889 }
890 printf("\n");
891 }
892 }
893 sqlite_free_table(azResult);
drh75897232000-05-29 14:26:00 +0000894 }else
895
drh2dfbbca2000-07-28 14:32:48 +0000896 if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
897 sqlite_busy_timeout(db, atoi(azArg[1]));
898 }else
899
drh75897232000-05-29 14:26:00 +0000900 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
901 int j;
902 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
903 p->colWidth[j-1] = atoi(azArg[j]);
904 }
905 }else
906
907 {
drh67505e72002-04-19 12:34:06 +0000908 fprintf(stderr, "unknown command or invalid arguments: "
909 " \"%s\". Enter \".help\" for help\n", azArg[0]);
drh75897232000-05-29 14:26:00 +0000910 }
drh67505e72002-04-19 12:34:06 +0000911
912 return rc;
drh75897232000-05-29 14:26:00 +0000913}
914
drh67505e72002-04-19 12:34:06 +0000915/*
drhbe4f31c2003-01-18 17:05:00 +0000916** Skip over an SQL comments and whitespace. Return a pointer to the text that
917** follows the comments and whitespace.
918*/
919static char *skip_whitespace(char *z){
920 while( isspace(*z) ){ z++; }
921 while( z[0]=='-' && z[1]=='-' ){
922 z += 2;
923 while( *z && *z!='\n' ){ z++; }
924 while( isspace(*z) ){ z++; }
925 }
926 return z;
927}
928
929/*
drh324ccef2003-02-05 14:06:20 +0000930** Return TRUE if the last non-whitespace character in z[] is a semicolon.
931** z[] is N characters long.
932*/
933static int _ends_with_semicolon(const char *z, int N){
934 while( N>0 && isspace(z[N-1]) ){ N--; }
935 return N>0 && z[N-1]==';';
936}
937
938/*
drh67505e72002-04-19 12:34:06 +0000939** Read input from *in and process it. If *in==0 then input
940** is interactive - the user is typing it it. Otherwise, input
941** is coming from a file or device. A prompt is issued and history
942** is saved only if input is interactive. An interrupt signal will
943** cause this routine to exit immediately, unless input is interactive.
944*/
drhdaffd0e2001-04-11 14:28:42 +0000945static void process_input(struct callback_data *p, FILE *in){
946 char *zLine;
947 char *zSql = 0;
948 int nSql = 0;
949 char *zErrMsg;
drh7f953e22002-07-13 17:33:45 +0000950 int rc;
drh41e941d2002-04-04 15:10:12 +0000951 while( fflush(p->out), (zLine = one_input_line(zSql, in))!=0 ){
drh67505e72002-04-19 12:34:06 +0000952 if( seenInterrupt ){
953 if( in!=0 ) break;
954 seenInterrupt = 0;
955 }
drhdaffd0e2001-04-11 14:28:42 +0000956 if( p->echoOn ) printf("%s\n", zLine);
drh2af0b2d2002-02-21 02:25:02 +0000957 if( zLine && zLine[0]=='.' && nSql==0 ){
drh67505e72002-04-19 12:34:06 +0000958 int rc = do_meta_command(zLine, db, p);
drhdaffd0e2001-04-11 14:28:42 +0000959 free(zLine);
drh67505e72002-04-19 12:34:06 +0000960 if( rc ) break;
drhdaffd0e2001-04-11 14:28:42 +0000961 continue;
962 }
963 if( zSql==0 ){
964 int i;
965 for(i=0; zLine[i] && isspace(zLine[i]); i++){}
966 if( zLine[i]!=0 ){
967 nSql = strlen(zLine);
968 zSql = malloc( nSql+1 );
969 strcpy(zSql, zLine);
970 }
971 }else{
972 int len = strlen(zLine);
973 zSql = realloc( zSql, nSql + len + 2 );
974 if( zSql==0 ){
975 fprintf(stderr,"%s: out of memory!\n", Argv0);
976 exit(1);
977 }
978 strcpy(&zSql[nSql++], "\n");
979 strcpy(&zSql[nSql], zLine);
980 nSql += len;
981 }
982 free(zLine);
drh324ccef2003-02-05 14:06:20 +0000983 if( zSql && _ends_with_semicolon(zSql, nSql) && sqlite_complete(zSql) ){
drhdaffd0e2001-04-11 14:28:42 +0000984 p->cnt = 0;
drh7f953e22002-07-13 17:33:45 +0000985 rc = sqlite_exec(db, zSql, callback, p, &zErrMsg);
986 if( rc || zErrMsg ){
drhdaffd0e2001-04-11 14:28:42 +0000987 if( in!=0 && !p->echoOn ) printf("%s\n",zSql);
drh7f953e22002-07-13 17:33:45 +0000988 if( zErrMsg!=0 ){
989 printf("SQL error: %s\n", zErrMsg);
990 free(zErrMsg);
991 zErrMsg = 0;
992 }else{
993 printf("SQL error: %s\n", sqlite_error_string(rc));
994 }
drhdaffd0e2001-04-11 14:28:42 +0000995 }
996 free(zSql);
997 zSql = 0;
998 nSql = 0;
999 }
1000 }
1001 if( zSql ){
drhbe4f31c2003-01-18 17:05:00 +00001002 char *zTail = skip_whitespace(zSql);
1003 if( zTail && zTail[0] ) printf("Incomplete SQL: %s\n", zSql);
drhdaffd0e2001-04-11 14:28:42 +00001004 free(zSql);
1005 }
1006}
1007
drh67505e72002-04-19 12:34:06 +00001008/*
1009** Return a pathname which is the user's home directory. A
1010** 0 return indicates an error of some kind. Space to hold the
1011** resulting string is obtained from malloc(). The calling
1012** function should free the result.
1013*/
1014static char *find_home_dir(void){
1015 char *home_dir = NULL;
persicom7e2dfdd2002-04-18 02:46:52 +00001016
drh820f3812003-01-08 13:02:52 +00001017#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
drh67505e72002-04-19 12:34:06 +00001018 struct passwd *pwent;
1019 uid_t uid = getuid();
drhbd842ba2002-08-21 11:26:41 +00001020 if( (pwent=getpwuid(uid)) != NULL) {
1021 home_dir = pwent->pw_dir;
drh67505e72002-04-19 12:34:06 +00001022 }
1023#endif
1024
drh820f3812003-01-08 13:02:52 +00001025#ifdef __MACOS__
1026 char home_path[_MAX_PATH+1];
1027 home_dir = getcwd(home_path, _MAX_PATH);
1028#endif
1029
drh67505e72002-04-19 12:34:06 +00001030 if (!home_dir) {
1031 home_dir = getenv("HOME");
1032 if (!home_dir) {
1033 home_dir = getenv("HOMEPATH"); /* Windows? */
1034 }
1035 }
1036
drhe98d4fa2002-04-21 19:06:22 +00001037#if defined(_WIN32) || defined(WIN32)
1038 if (!home_dir) {
1039 home_dir = "c:";
1040 }
1041#endif
1042
drh67505e72002-04-19 12:34:06 +00001043 if( home_dir ){
1044 char *z = malloc( strlen(home_dir)+1 );
1045 if( z ) strcpy(z, home_dir);
1046 home_dir = z;
1047 }
drhe98d4fa2002-04-21 19:06:22 +00001048
drh67505e72002-04-19 12:34:06 +00001049 return home_dir;
1050}
1051
1052/*
1053** Read input from the file given by sqliterc_override. Or if that
1054** parameter is NULL, take input from ~/.sqliterc
1055*/
1056static void process_sqliterc(struct callback_data *p, char *sqliterc_override){
persicom7e2dfdd2002-04-18 02:46:52 +00001057 char *home_dir = NULL;
1058 char *sqliterc = sqliterc_override;
persicom7e2dfdd2002-04-18 02:46:52 +00001059 FILE *in = NULL;
1060
1061 if (sqliterc == NULL) {
drh67505e72002-04-19 12:34:06 +00001062 home_dir = find_home_dir();
drhe98d4fa2002-04-21 19:06:22 +00001063 if( home_dir==0 ){
1064 fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0);
1065 return;
1066 }
1067 sqliterc = malloc(strlen(home_dir) + 15);
persicom7e2dfdd2002-04-18 02:46:52 +00001068 if( sqliterc==0 ){
1069 fprintf(stderr,"%s: out of memory!\n", Argv0);
1070 exit(1);
1071 }
1072 sprintf(sqliterc,"%s/.sqliterc",home_dir);
drh67505e72002-04-19 12:34:06 +00001073 free(home_dir);
persicom7e2dfdd2002-04-18 02:46:52 +00001074 }
1075 in = fopen(sqliterc,"r");
drh67505e72002-04-19 12:34:06 +00001076 if(in) {
persicom7e2dfdd2002-04-18 02:46:52 +00001077 printf("Loading resources from %s\n",sqliterc);
1078 process_input(p,in);
drhdd45df82002-04-18 12:39:03 +00001079 fclose(in);
persicom7e2dfdd2002-04-18 02:46:52 +00001080 }
1081 return;
1082}
1083
drh67505e72002-04-19 12:34:06 +00001084/*
1085** Initialize the state information in data
1086*/
persicom7e2dfdd2002-04-18 02:46:52 +00001087void main_init(struct callback_data *data) {
1088 memset(data, 0, sizeof(*data));
1089 data->mode = MODE_List;
1090 strcpy(data->separator,"|");
1091 data->showHeader = 0;
1092 strcpy(mainPrompt,"sqlite> ");
1093 strcpy(continuePrompt," ...> ");
1094}
1095
drh75897232000-05-29 14:26:00 +00001096int main(int argc, char **argv){
drh75897232000-05-29 14:26:00 +00001097 char *zErrMsg = 0;
1098 struct callback_data data;
persicom7e2dfdd2002-04-18 02:46:52 +00001099 int origArgc = argc;
1100 char **origArgv = argv;
drh75897232000-05-29 14:26:00 +00001101
drh820f3812003-01-08 13:02:52 +00001102#ifdef __MACOS__
1103 argc = ccommand(&argv);
1104 origArgc = argc;
1105 origArgv = argv;
1106#endif
1107
drhdaffd0e2001-04-11 14:28:42 +00001108 Argv0 = argv[0];
persicom7e2dfdd2002-04-18 02:46:52 +00001109 main_init(&data);
1110 process_sqliterc(&data,NULL);
1111
drh4c504392000-10-16 22:06:40 +00001112#ifdef SIGINT
1113 signal(SIGINT, interrupt_handler);
1114#endif
drh1e5d0e92000-05-31 23:33:17 +00001115 while( argc>=2 && argv[1][0]=='-' ){
persicom7e2dfdd2002-04-18 02:46:52 +00001116 if( argc>=3 && strcmp(argv[1],"-init")==0 ){
1117 /* If we get a -init to do, we have to pretend that
1118 ** it replaced the .sqliterc file. Soooo, in order to
1119 ** do that we need to start from scratch...*/
1120 main_init(&data);
1121
1122 /* treat this file as the sqliterc... */
1123 process_sqliterc(&data,argv[2]);
1124
1125 /* fix up the command line so we do not re-read
1126 ** the option next time around... */
1127 {
1128 int i = 1;
1129 for(i=1;i<=argc-2;i++) {
1130 argv[i] = argv[i+2];
1131 }
1132 }
1133 origArgc-=2;
1134
1135 /* and reset the command line options to be re-read.*/
1136 argv = origArgv;
1137 argc = origArgc;
1138
1139 }else if( strcmp(argv[1],"-html")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001140 data.mode = MODE_Html;
1141 argc--;
1142 argv++;
1143 }else if( strcmp(argv[1],"-list")==0 ){
1144 data.mode = MODE_List;
1145 argc--;
1146 argv++;
1147 }else if( strcmp(argv[1],"-line")==0 ){
1148 data.mode = MODE_Line;
1149 argc--;
1150 argv++;
drh8b32e172002-04-08 02:42:57 +00001151 }else if( strcmp(argv[1],"-column")==0 ){
1152 data.mode = MODE_Column;
1153 argc--;
1154 argv++;
drh7613bfa2002-01-22 12:39:24 +00001155 }else if( argc>=3 && strcmp(argv[1],"-separator")==0 ){
drhbed86902000-06-02 13:27:59 +00001156 sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[2]);
drh1e5d0e92000-05-31 23:33:17 +00001157 argc -= 2;
1158 argv += 2;
persicom7e2dfdd2002-04-18 02:46:52 +00001159 }else if( argc>=3 && strcmp(argv[1],"-nullvalue")==0 ){
1160 sprintf(data.nullvalue,"%.*s",(int)sizeof(data.nullvalue)-1,argv[2]);
1161 argc -= 2;
1162 argv += 2;
drh1e5d0e92000-05-31 23:33:17 +00001163 }else if( strcmp(argv[1],"-header")==0 ){
1164 data.showHeader = 1;
1165 argc--;
1166 argv++;
1167 }else if( strcmp(argv[1],"-noheader")==0 ){
1168 data.showHeader = 0;
1169 argc--;
1170 argv++;
drh660f68d2001-01-04 14:27:07 +00001171 }else if( strcmp(argv[1],"-echo")==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001172 data.echoOn = 1;
drh660f68d2001-01-04 14:27:07 +00001173 argc--;
1174 argv++;
drh1e5d0e92000-05-31 23:33:17 +00001175 }else{
drhdaffd0e2001-04-11 14:28:42 +00001176 fprintf(stderr,"%s: unknown option: %s\n", Argv0, argv[1]);
drh1e5d0e92000-05-31 23:33:17 +00001177 return 1;
1178 }
1179 }
drh75897232000-05-29 14:26:00 +00001180 if( argc!=2 && argc!=3 ){
drhdaffd0e2001-04-11 14:28:42 +00001181 fprintf(stderr,"Usage: %s ?OPTIONS? FILENAME ?SQL?\n", Argv0);
drh75897232000-05-29 14:26:00 +00001182 exit(1);
1183 }
drh4c653a02000-06-07 01:27:47 +00001184 data.db = db = sqlite_open(argv[1], 0666, &zErrMsg);
drh75897232000-05-29 14:26:00 +00001185 if( db==0 ){
drh167a4b12000-08-17 09:49:59 +00001186 data.db = db = sqlite_open(argv[1], 0444, &zErrMsg);
1187 if( db==0 ){
1188 if( zErrMsg ){
1189 fprintf(stderr,"Unable to open database \"%s\": %s\n", argv[1],zErrMsg);
1190 }else{
1191 fprintf(stderr,"Unable to open database %s\n", argv[1]);
1192 }
1193 exit(1);
drhd1dedb82000-06-05 02:07:04 +00001194 }else{
drh80afdca2000-08-22 13:27:22 +00001195 fprintf(stderr,"Database \"%s\" opened READ ONLY!\n", argv[1]);
drhd1dedb82000-06-05 02:07:04 +00001196 }
drh75897232000-05-29 14:26:00 +00001197 }
drh75897232000-05-29 14:26:00 +00001198 data.out = stdout;
1199 if( argc==3 ){
drh6ff13852001-11-25 13:18:23 +00001200 if( argv[2][0]=='.' ){
1201 do_meta_command(argv[2], db, &data);
1202 exit(0);
1203 }else{
1204 int rc;
1205 rc = sqlite_exec(db, argv[2], callback, &data, &zErrMsg);
1206 if( rc!=0 && zErrMsg!=0 ){
1207 fprintf(stderr,"SQL error: %s\n", zErrMsg);
1208 exit(1);
1209 }
drh75897232000-05-29 14:26:00 +00001210 }
1211 }else{
drh382c0242001-10-06 16:33:02 +00001212 extern int isatty();
drhdaffd0e2001-04-11 14:28:42 +00001213 if( isatty(0) ){
drh67505e72002-04-19 12:34:06 +00001214 char *zHome;
1215 char *zHistory = 0;
drh75897232000-05-29 14:26:00 +00001216 printf(
drhb217a572000-08-22 13:40:18 +00001217 "SQLite version %s\n"
1218 "Enter \".help\" for instructions\n",
1219 sqlite_version
drh75897232000-05-29 14:26:00 +00001220 );
drh67505e72002-04-19 12:34:06 +00001221 zHome = find_home_dir();
1222 if( zHome && (zHistory = malloc(strlen(zHome)+20))!=0 ){
1223 sprintf(zHistory,"%s/.sqlite_history", zHome);
1224 }
1225 if( zHistory ) read_history(zHistory);
drhdaffd0e2001-04-11 14:28:42 +00001226 process_input(&data, 0);
drh67505e72002-04-19 12:34:06 +00001227 if( zHistory ){
1228 stifle_history(100);
1229 write_history(zHistory);
1230 }
drhdaffd0e2001-04-11 14:28:42 +00001231 }else{
1232 process_input(&data, stdin);
drh75897232000-05-29 14:26:00 +00001233 }
1234 }
drh33048c02001-10-01 14:29:22 +00001235 set_table_name(&data, 0);
drh75897232000-05-29 14:26:00 +00001236 sqlite_close(db);
1237 return 0;
1238}