blob: 7616f6482acebae66f429a74ea84053a423a8b02 [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**
drh7f953e22002-07-13 17:33:45 +000015** $Id: shell.c,v 1.61 2002/07/13 17:33:45 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
drha297b5c2002-01-15 18:39:43 +000023#if !defined(_WIN32) && !defined(WIN32)
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
drh16e59552000-07-31 11:57:37 +000030#if defined(HAVE_READLINE) && HAVE_READLINE==1
drh8e7e7a22000-05-30 18:45:23 +000031# include <readline/readline.h>
32# include <readline/history.h>
33#else
drh5e00f6c2001-09-13 13:46:56 +000034# define readline(p) getline(p,stdin)
persicom1d0b8722002-04-18 02:53:04 +000035# define add_history(X)
drh67505e72002-04-19 12:34:06 +000036# define read_history(X)
37# define write_history(X)
38# define stifle_history(X)
drh75897232000-05-29 14:26:00 +000039#endif
40
41/*
drh4c504392000-10-16 22:06:40 +000042** The following is the open SQLite database. We make a pointer
43** to this database a static variable so that it can be accessed
44** by the SIGINT handler to interrupt database processing.
45*/
46static sqlite *db = 0;
47
48/*
drh67505e72002-04-19 12:34:06 +000049** True if an interrupt (Control-C) has been received.
50*/
51static int seenInterrupt = 0;
52
53/*
persicom7e2dfdd2002-04-18 02:46:52 +000054** This is the name of our program. It is set in main(), used
55** in a number of other places, mostly for error messages.
56*/
57static char *Argv0;
58
59/*
60** Prompt strings. Initialized in main. Settable with
61** .prompt main continue
62*/
63static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
64static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
65
66/*
drh8e7e7a22000-05-30 18:45:23 +000067** This routine reads a line of text from standard input, stores
68** the text in memory obtained from malloc() and returns a pointer
69** to the text. NULL is returned at end of file, or if malloc()
70** fails.
71**
72** The interface is like "readline" but no command-line editing
73** is done.
74*/
drhdaffd0e2001-04-11 14:28:42 +000075static char *getline(char *zPrompt, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +000076 char *zLine;
77 int nLine;
drh8e7e7a22000-05-30 18:45:23 +000078 int n;
79 int eol;
80
81 if( zPrompt && *zPrompt ){
82 printf("%s",zPrompt);
83 fflush(stdout);
84 }
85 nLine = 100;
86 zLine = malloc( nLine );
87 if( zLine==0 ) return 0;
88 n = 0;
89 eol = 0;
90 while( !eol ){
91 if( n+100>nLine ){
92 nLine = nLine*2 + 100;
93 zLine = realloc(zLine, nLine);
94 if( zLine==0 ) return 0;
95 }
drhdaffd0e2001-04-11 14:28:42 +000096 if( fgets(&zLine[n], nLine - n, in)==0 ){
drh8e7e7a22000-05-30 18:45:23 +000097 if( n==0 ){
98 free(zLine);
99 return 0;
100 }
101 zLine[n] = 0;
102 eol = 1;
103 break;
104 }
105 while( zLine[n] ){ n++; }
106 if( n>0 && zLine[n-1]=='\n' ){
107 n--;
108 zLine[n] = 0;
109 eol = 1;
110 }
111 }
112 zLine = realloc( zLine, n+1 );
113 return zLine;
114}
115
116/*
117** Retrieve a single line of input text. "isatty" is true if text
118** is coming from a terminal. In that case, we issue a prompt and
119** attempt to use "readline" for command-line editing. If "isatty"
drhaacc5432002-01-06 17:07:40 +0000120** is false, use "getline" instead of "readline" and issue no prompt.
drh8e7e7a22000-05-30 18:45:23 +0000121**
122** zPrior is a string of prior text retrieved. If not the empty
123** string, then issue a continuation prompt.
124*/
drhdaffd0e2001-04-11 14:28:42 +0000125static char *one_input_line(const char *zPrior, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +0000126 char *zPrompt;
127 char *zResult;
drhdaffd0e2001-04-11 14:28:42 +0000128 if( in!=0 ){
129 return getline(0, in);
drh8e7e7a22000-05-30 18:45:23 +0000130 }
131 if( zPrior && zPrior[0] ){
persicom7e2dfdd2002-04-18 02:46:52 +0000132 zPrompt = continuePrompt;
drh8e7e7a22000-05-30 18:45:23 +0000133 }else{
persicom7e2dfdd2002-04-18 02:46:52 +0000134 zPrompt = mainPrompt;
drh8e7e7a22000-05-30 18:45:23 +0000135 }
136 zResult = readline(zPrompt);
drh2dfbbca2000-07-28 14:32:48 +0000137 if( zResult ) add_history(zResult);
drh8e7e7a22000-05-30 18:45:23 +0000138 return zResult;
139}
140
persicom7e2dfdd2002-04-18 02:46:52 +0000141struct previous_mode_data {
142 int valid; /* Is there legit data in here? */
143 int mode;
144 int showHeader;
145 int colWidth[100];
146};
drh8e7e7a22000-05-30 18:45:23 +0000147/*
drh75897232000-05-29 14:26:00 +0000148** An pointer to an instance of this structure is passed from
149** the main program to the callback. This is used to communicate
150** state and mode information.
151*/
152struct callback_data {
drh28bd4bc2000-06-15 15:57:22 +0000153 sqlite *db; /* The database */
drhdaffd0e2001-04-11 14:28:42 +0000154 int echoOn; /* True to echo input commands */
drh28bd4bc2000-06-15 15:57:22 +0000155 int cnt; /* Number of records displayed so far */
156 FILE *out; /* Write results here */
157 int mode; /* An output mode setting */
158 int showHeader; /* True to show column names in List or Column mode */
drh33048c02001-10-01 14:29:22 +0000159 char *zDestTable; /* Name of destination table when MODE_Insert */
drh28bd4bc2000-06-15 15:57:22 +0000160 char separator[20]; /* Separator character for MODE_List */
drha0c66f52000-07-29 13:20:21 +0000161 int colWidth[100]; /* Requested width of each column when in column mode*/
162 int actualWidth[100]; /* Actual width of each column */
persicom7e2dfdd2002-04-18 02:46:52 +0000163 char nullvalue[20]; /* The text to print when a NULL comes back from the database */
164 struct previous_mode_data explainPrev;
165 /* Holds the mode information just before .explain ON */
166 char outfile[FILENAME_MAX];
167 /* Filename for *out */
drh75897232000-05-29 14:26:00 +0000168};
169
170/*
171** These are the allowed modes.
172*/
drh967e8b72000-06-21 13:59:10 +0000173#define MODE_Line 0 /* One column per line. Blank line between records */
drh75897232000-05-29 14:26:00 +0000174#define MODE_Column 1 /* One record per line in neat columns */
175#define MODE_List 2 /* One record per line with a separator */
drhe3710332000-09-29 13:30:53 +0000176#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
177#define MODE_Html 4 /* Generate an XHTML table */
178#define MODE_Insert 5 /* Generate SQL "insert" statements */
persicom7e2dfdd2002-04-18 02:46:52 +0000179#define MODE_NUM_OF 6
180
181char *modeDescr[MODE_NUM_OF] = {
182 "line",
183 "column",
184 "list",
185 "semi",
186 "html",
187 "insert"
188};
drh75897232000-05-29 14:26:00 +0000189
190/*
191** Number of elements in an array
192*/
193#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
194
195/*
drh28bd4bc2000-06-15 15:57:22 +0000196** Return TRUE if the string supplied is a number of some kinds.
197*/
198static int is_numeric(const char *z){
199 int seen_digit = 0;
200 if( *z=='-' || *z=='+' ){
201 z++;
202 }
persicom1d0b8722002-04-18 02:53:04 +0000203 while( isdigit(*z) ){
drh28bd4bc2000-06-15 15:57:22 +0000204 seen_digit = 1;
205 z++;
206 }
207 if( seen_digit && *z=='.' ){
208 z++;
209 while( isdigit(*z) ){ z++; }
210 }
211 if( seen_digit && (*z=='e' || *z=='E')
212 && (isdigit(z[1]) || ((z[1]=='-' || z[1]=='+') && isdigit(z[2])))
213 ){
214 z+=2;
215 while( isdigit(*z) ){ z++; }
216 }
217 return seen_digit && *z==0;
218}
219
220/*
221** Output the given string as a quoted string using SQL quoting conventions.
222*/
223static void output_quoted_string(FILE *out, const char *z){
224 int i;
225 int nSingle = 0;
drh28bd4bc2000-06-15 15:57:22 +0000226 for(i=0; z[i]; i++){
227 if( z[i]=='\'' ) nSingle++;
drh28bd4bc2000-06-15 15:57:22 +0000228 }
229 if( nSingle==0 ){
230 fprintf(out,"'%s'",z);
drh28bd4bc2000-06-15 15:57:22 +0000231 }else{
232 fprintf(out,"'");
233 while( *z ){
234 for(i=0; z[i] && z[i]!='\''; i++){}
235 if( i==0 ){
236 fprintf(out,"''");
237 z++;
238 }else if( z[i]=='\'' ){
239 fprintf(out,"%.*s''",i,z);
240 z += i+1;
241 }else{
drhcd7d2732002-02-26 23:24:26 +0000242 fprintf(out,"%s",z);
drh28bd4bc2000-06-15 15:57:22 +0000243 break;
244 }
245 }
drhcd7d2732002-02-26 23:24:26 +0000246 fprintf(out,"'");
drh28bd4bc2000-06-15 15:57:22 +0000247 }
248}
249
250/*
drhc08a4f12000-06-15 16:49:48 +0000251** Output the given string with characters that are special to
252** HTML escaped.
253*/
254static void output_html_string(FILE *out, const char *z){
255 int i;
256 while( *z ){
257 for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){}
258 if( i>0 ){
259 fprintf(out,"%.*s",i,z);
260 }
261 if( z[i]=='<' ){
262 fprintf(out,"&lt;");
263 }else if( z[i]=='&' ){
264 fprintf(out,"&amp;");
265 }else{
266 break;
267 }
268 z += i + 1;
269 }
270}
271
272/*
drh4c504392000-10-16 22:06:40 +0000273** This routine runs when the user presses Ctrl-C
274*/
275static void interrupt_handler(int NotUsed){
drh67505e72002-04-19 12:34:06 +0000276 seenInterrupt = 1;
drh4c504392000-10-16 22:06:40 +0000277 if( db ) sqlite_interrupt(db);
278}
279
280/*
drh75897232000-05-29 14:26:00 +0000281** This is the callback routine that the SQLite library
282** invokes for each row of a query result.
283*/
284static int callback(void *pArg, int nArg, char **azArg, char **azCol){
285 int i;
286 struct callback_data *p = (struct callback_data*)pArg;
287 switch( p->mode ){
288 case MODE_Line: {
drhe3710332000-09-29 13:30:53 +0000289 int w = 5;
drh6a535342001-10-19 16:44:56 +0000290 if( azArg==0 ) break;
drhe3710332000-09-29 13:30:53 +0000291 for(i=0; i<nArg; i++){
292 int len = strlen(azCol[i]);
293 if( len>w ) w = len;
294 }
drh75897232000-05-29 14:26:00 +0000295 if( p->cnt++>0 ) fprintf(p->out,"\n");
296 for(i=0; i<nArg; i++){
persicom7e2dfdd2002-04-18 02:46:52 +0000297 fprintf(p->out,"%*s = %s\n", w, azCol[i], azArg[i] ? azArg[i] : p->nullvalue);
drh75897232000-05-29 14:26:00 +0000298 }
299 break;
300 }
301 case MODE_Column: {
drha0c66f52000-07-29 13:20:21 +0000302 if( p->cnt++==0 ){
drh75897232000-05-29 14:26:00 +0000303 for(i=0; i<nArg; i++){
drha0c66f52000-07-29 13:20:21 +0000304 int w, n;
305 if( i<ArraySize(p->colWidth) ){
drh75897232000-05-29 14:26:00 +0000306 w = p->colWidth[i];
307 }else{
drha0c66f52000-07-29 13:20:21 +0000308 w = 0;
drh75897232000-05-29 14:26:00 +0000309 }
drha0c66f52000-07-29 13:20:21 +0000310 if( w<=0 ){
drhff6e9112000-08-28 16:21:58 +0000311 w = strlen(azCol[i] ? azCol[i] : "");
drha0c66f52000-07-29 13:20:21 +0000312 if( w<10 ) w = 10;
persicom7e2dfdd2002-04-18 02:46:52 +0000313 n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue);
drha0c66f52000-07-29 13:20:21 +0000314 if( w<n ) w = n;
315 }
316 if( i<ArraySize(p->actualWidth) ){
persicom1d0b8722002-04-18 02:53:04 +0000317 p->actualWidth[i] = w;
drha0c66f52000-07-29 13:20:21 +0000318 }
319 if( p->showHeader ){
320 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
321 }
322 }
323 if( p->showHeader ){
324 for(i=0; i<nArg; i++){
325 int w;
326 if( i<ArraySize(p->actualWidth) ){
327 w = p->actualWidth[i];
328 }else{
329 w = 10;
330 }
331 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
332 "----------------------------------------------------------",
333 i==nArg-1 ? "\n": " ");
334 }
drh75897232000-05-29 14:26:00 +0000335 }
336 }
drh6a535342001-10-19 16:44:56 +0000337 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000338 for(i=0; i<nArg; i++){
339 int w;
drha0c66f52000-07-29 13:20:21 +0000340 if( i<ArraySize(p->actualWidth) ){
341 w = p->actualWidth[i];
drh75897232000-05-29 14:26:00 +0000342 }else{
343 w = 10;
344 }
drhc61053b2000-06-04 12:58:36 +0000345 fprintf(p->out,"%-*.*s%s",w,w,
persicom7e2dfdd2002-04-18 02:46:52 +0000346 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
drh75897232000-05-29 14:26:00 +0000347 }
348 break;
349 }
drhe3710332000-09-29 13:30:53 +0000350 case MODE_Semi:
drh75897232000-05-29 14:26:00 +0000351 case MODE_List: {
352 if( p->cnt++==0 && p->showHeader ){
353 for(i=0; i<nArg; i++){
354 fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
355 }
356 }
drh6a535342001-10-19 16:44:56 +0000357 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000358 for(i=0; i<nArg; i++){
drh4c653a02000-06-07 01:27:47 +0000359 char *z = azArg[i];
persicom7e2dfdd2002-04-18 02:46:52 +0000360 if( z==0 ) z = p->nullvalue;
drh71172c52002-01-24 00:00:21 +0000361 fprintf(p->out, "%s", z);
drhe3710332000-09-29 13:30:53 +0000362 if( i<nArg-1 ){
363 fprintf(p->out, "%s", p->separator);
364 }else if( p->mode==MODE_Semi ){
365 fprintf(p->out, ";\n");
366 }else{
367 fprintf(p->out, "\n");
368 }
drh75897232000-05-29 14:26:00 +0000369 }
370 break;
371 }
drh1e5d0e92000-05-31 23:33:17 +0000372 case MODE_Html: {
373 if( p->cnt++==0 && p->showHeader ){
374 fprintf(p->out,"<TR>");
375 for(i=0; i<nArg; i++){
376 fprintf(p->out,"<TH>%s</TH>",azCol[i]);
377 }
378 fprintf(p->out,"</TR>\n");
379 }
drh6a535342001-10-19 16:44:56 +0000380 if( azArg==0 ) break;
drh28bd4bc2000-06-15 15:57:22 +0000381 fprintf(p->out,"<TR>");
drh1e5d0e92000-05-31 23:33:17 +0000382 for(i=0; i<nArg; i++){
drhc08a4f12000-06-15 16:49:48 +0000383 fprintf(p->out,"<TD>");
persicom7e2dfdd2002-04-18 02:46:52 +0000384 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
drhc08a4f12000-06-15 16:49:48 +0000385 fprintf(p->out,"</TD>\n");
drh1e5d0e92000-05-31 23:33:17 +0000386 }
drh28bd4bc2000-06-15 15:57:22 +0000387 fprintf(p->out,"</TD></TR>\n");
drh1e5d0e92000-05-31 23:33:17 +0000388 break;
389 }
drh28bd4bc2000-06-15 15:57:22 +0000390 case MODE_Insert: {
drh6a535342001-10-19 16:44:56 +0000391 if( azArg==0 ) break;
drh33048c02001-10-01 14:29:22 +0000392 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
drh28bd4bc2000-06-15 15:57:22 +0000393 for(i=0; i<nArg; i++){
394 char *zSep = i>0 ? ",": "";
395 if( azArg[i]==0 ){
396 fprintf(p->out,"%sNULL",zSep);
397 }else if( is_numeric(azArg[i]) ){
398 fprintf(p->out,"%s%s",zSep, azArg[i]);
399 }else{
400 if( zSep[0] ) fprintf(p->out,"%s",zSep);
401 output_quoted_string(p->out, azArg[i]);
402 }
403 }
404 fprintf(p->out,");\n");
drh6a535342001-10-19 16:44:56 +0000405 break;
drh28bd4bc2000-06-15 15:57:22 +0000406 }
persicom1d0b8722002-04-18 02:53:04 +0000407 }
drh75897232000-05-29 14:26:00 +0000408 return 0;
409}
410
411/*
drh33048c02001-10-01 14:29:22 +0000412** Set the destination table field of the callback_data structure to
413** the name of the table given. Escape any quote characters in the
414** table name.
415*/
416static void set_table_name(struct callback_data *p, const char *zName){
417 int i, n;
418 int needQuote;
419 char *z;
420
421 if( p->zDestTable ){
422 free(p->zDestTable);
423 p->zDestTable = 0;
424 }
425 if( zName==0 ) return;
426 needQuote = !isalpha(*zName) && *zName!='_';
427 for(i=n=0; zName[i]; i++, n++){
428 if( !isalnum(zName[i]) && zName[i]!='_' ){
429 needQuote = 1;
430 if( zName[i]=='\'' ) n++;
431 }
432 }
433 if( needQuote ) n += 2;
434 z = p->zDestTable = malloc( n+1 );
435 if( z==0 ){
436 fprintf(stderr,"Out of memory!\n");
437 exit(1);
438 }
439 n = 0;
440 if( needQuote ) z[n++] = '\'';
441 for(i=0; zName[i]; i++){
442 z[n++] = zName[i];
443 if( zName[i]=='\'' ) z[n++] = '\'';
444 }
445 if( needQuote ) z[n++] = '\'';
446 z[n] = 0;
447}
448
449/*
drh4c653a02000-06-07 01:27:47 +0000450** This is a different callback routine used for dumping the database.
451** Each row received by this callback consists of a table name,
452** the table type ("index" or "table") and SQL to create the table.
453** This routine should print text sufficient to recreate the table.
454*/
455static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
drhdaffd0e2001-04-11 14:28:42 +0000456 struct callback_data *p = (struct callback_data *)pArg;
drh4c653a02000-06-07 01:27:47 +0000457 if( nArg!=3 ) return 1;
drhdaffd0e2001-04-11 14:28:42 +0000458 fprintf(p->out, "%s;\n", azArg[2]);
drh4c653a02000-06-07 01:27:47 +0000459 if( strcmp(azArg[1],"table")==0 ){
460 struct callback_data d2;
drhdaffd0e2001-04-11 14:28:42 +0000461 d2 = *p;
drh33048c02001-10-01 14:29:22 +0000462 d2.mode = MODE_Insert;
463 d2.zDestTable = 0;
464 set_table_name(&d2, azArg[0]);
persicom1d0b8722002-04-18 02:53:04 +0000465 sqlite_exec_printf(p->db,
drha18c5682000-10-08 22:20:57 +0000466 "SELECT * FROM '%q'",
467 callback, &d2, 0, azArg[0]
468 );
drh33048c02001-10-01 14:29:22 +0000469 set_table_name(&d2, 0);
drh4c653a02000-06-07 01:27:47 +0000470 }
drh4c653a02000-06-07 01:27:47 +0000471 return 0;
472}
473
474/*
drh75897232000-05-29 14:26:00 +0000475** Text of a help message
476*/
persicom1d0b8722002-04-18 02:53:04 +0000477static char zHelp[] =
drh4c653a02000-06-07 01:27:47 +0000478 ".dump ?TABLE? ... Dump the database in an text format\n"
drhdaffd0e2001-04-11 14:28:42 +0000479 ".echo ON|OFF Turn command echo on or off\n"
drh75897232000-05-29 14:26:00 +0000480 ".exit Exit this program\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000481 ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000482 ".header(s) ON|OFF Turn display of headers on or off\n"
drh75897232000-05-29 14:26:00 +0000483 ".help Show this message\n"
484 ".indices TABLE Show names of all indices on TABLE\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000485 ".mode MODE Set mode to one of \"line(s)\", \"column(s)\", \n"
drhe3710332000-09-29 13:30:53 +0000486 " \"insert\", \"list\", or \"html\"\n"
drhc08a4f12000-06-15 16:49:48 +0000487 ".mode insert TABLE Generate SQL insert statements for TABLE\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000488 ".nullvalue STRING Print STRING instead of nothing for NULL data\n"
drh411995d2002-06-25 19:31:18 +0000489 ".openaux FILENAME Use FILENAME to hold TEMP tables\n"
drh75897232000-05-29 14:26:00 +0000490 ".output FILENAME Send output to FILENAME\n"
491 ".output stdout Send output to the screen\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000492 ".prompt MAIN CONTINUE Replace the standard prompts\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000493 ".quit Exit this program\n"
drhdaffd0e2001-04-11 14:28:42 +0000494 ".read FILENAME Execute SQL in FILENAME\n"
drh75897232000-05-29 14:26:00 +0000495 ".schema ?TABLE? Show the CREATE statements\n"
496 ".separator STRING Change separator string for \"list\" mode\n"
drhdd45df82002-04-18 12:39:03 +0000497 ".show Show the current values for various settings\n"
drha50da102000-08-08 20:19:09 +0000498 ".tables ?PATTERN? List names of tables matching a pattern\n"
drh2dfbbca2000-07-28 14:32:48 +0000499 ".timeout MS Try opening locked tables for MS milliseconds\n"
drh75897232000-05-29 14:26:00 +0000500 ".width NUM NUM ... Set column widths for \"column\" mode\n"
501;
502
drhdaffd0e2001-04-11 14:28:42 +0000503/* Forward reference */
504static void process_input(struct callback_data *p, FILE *in);
505
drh75897232000-05-29 14:26:00 +0000506/*
507** If an input line begins with "." then invoke this routine to
508** process that line.
drh67505e72002-04-19 12:34:06 +0000509**
510** Return 1 to exit and 0 to continue.
drh75897232000-05-29 14:26:00 +0000511*/
drh67505e72002-04-19 12:34:06 +0000512static int do_meta_command(char *zLine, sqlite *db, struct callback_data *p){
drh75897232000-05-29 14:26:00 +0000513 int i = 1;
514 int nArg = 0;
515 int n, c;
drh67505e72002-04-19 12:34:06 +0000516 int rc = 0;
drh75897232000-05-29 14:26:00 +0000517 char *azArg[50];
518
519 /* Parse the input line into tokens.
520 */
521 while( zLine[i] && nArg<ArraySize(azArg) ){
522 while( isspace(zLine[i]) ){ i++; }
523 if( zLine[i]=='\'' || zLine[i]=='"' ){
524 int delim = zLine[i++];
525 azArg[nArg++] = &zLine[i];
526 while( zLine[i] && zLine[i]!=delim ){ i++; }
527 if( zLine[i]==delim ){
528 zLine[i++] = 0;
529 }
530 }else{
531 azArg[nArg++] = &zLine[i];
532 while( zLine[i] && !isspace(zLine[i]) ){ i++; }
533 if( zLine[i] ) zLine[i++] = 0;
534 }
535 }
536
537 /* Process the input line.
538 */
drh67505e72002-04-19 12:34:06 +0000539 if( nArg==0 ) return rc;
drh75897232000-05-29 14:26:00 +0000540 n = strlen(azArg[0]);
541 c = azArg[0][0];
drh4c653a02000-06-07 01:27:47 +0000542 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
543 char *zErrMsg = 0;
drh33048c02001-10-01 14:29:22 +0000544 fprintf(p->out, "BEGIN TRANSACTION;\n");
drh4c653a02000-06-07 01:27:47 +0000545 if( nArg==1 ){
drha18c5682000-10-08 22:20:57 +0000546 sqlite_exec(db,
547 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000548 "WHERE type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000549 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000550 dump_callback, p, &zErrMsg
551 );
drh4c653a02000-06-07 01:27:47 +0000552 }else{
553 int i;
554 for(i=1; i<nArg && zErrMsg==0; i++){
persicom1d0b8722002-04-18 02:53:04 +0000555 sqlite_exec_printf(db,
drha18c5682000-10-08 22:20:57 +0000556 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000557 "WHERE tbl_name LIKE '%q' AND 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, azArg[i]
560 );
drh4c653a02000-06-07 01:27:47 +0000561 }
562 }
563 if( zErrMsg ){
564 fprintf(stderr,"Error: %s\n", zErrMsg);
565 free(zErrMsg);
drh33048c02001-10-01 14:29:22 +0000566 }else{
567 fprintf(p->out, "COMMIT;\n");
drh4c653a02000-06-07 01:27:47 +0000568 }
569 }else
drh75897232000-05-29 14:26:00 +0000570
drhdaffd0e2001-04-11 14:28:42 +0000571 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
572 int j;
573 char *z = azArg[1];
574 int val = atoi(azArg[1]);
575 for(j=0; z[j]; j++){
576 if( isupper(z[j]) ) z[j] = tolower(z[j]);
577 }
578 if( strcmp(z,"on")==0 ){
579 val = 1;
580 }else if( strcmp(z,"yes")==0 ){
581 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000582 }
drhdaffd0e2001-04-11 14:28:42 +0000583 p->echoOn = val;
584 }else
585
drh75897232000-05-29 14:26:00 +0000586 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
drh67505e72002-04-19 12:34:06 +0000587 rc = 1;
drh75897232000-05-29 14:26:00 +0000588 }else
589
drhdd45df82002-04-18 12:39:03 +0000590 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +0000591 int j;
drhdd45df82002-04-18 12:39:03 +0000592 char *z = nArg>=2 ? azArg[1] : "1";
593 int val = atoi(z);
persicom7e2dfdd2002-04-18 02:46:52 +0000594 for(j=0; z[j]; j++){
595 if( isupper(z[j]) ) z[j] = tolower(z[j]);
596 }
597 if( strcmp(z,"on")==0 ){
598 val = 1;
599 }else if( strcmp(z,"yes")==0 ){
600 val = 1;
601 }
602 if(val == 1) {
603 if(!p->explainPrev.valid) {
604 p->explainPrev.valid = 1;
605 p->explainPrev.mode = p->mode;
606 p->explainPrev.showHeader = p->showHeader;
607 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
608 }
609 /* We could put this code under the !p->explainValid
610 ** condition so that it does not execute if we are already in
611 ** explain mode. However, always executing it allows us an easy
612 ** was to reset to explain mode in case the user previously
613 ** did an .explain followed by a .width, .mode or .header
614 ** command.
615 */
616 p->mode = MODE_Column;
617 p->showHeader = 1;
618 memset(p->colWidth,0,ArraySize(p->colWidth));
619 p->colWidth[0] = 4;
620 p->colWidth[1] = 12;
621 p->colWidth[2] = 10;
622 p->colWidth[3] = 10;
623 p->colWidth[4] = 35;
624 }else if (p->explainPrev.valid) {
625 p->explainPrev.valid = 0;
626 p->mode = p->explainPrev.mode;
627 p->showHeader = p->explainPrev.showHeader;
628 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
629 }
drh75897232000-05-29 14:26:00 +0000630 }else
631
persicom7e2dfdd2002-04-18 02:46:52 +0000632 if( c=='h' && (strncmp(azArg[0], "header", n)==0
633 ||
634 strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){
drh75897232000-05-29 14:26:00 +0000635 int j;
636 char *z = azArg[1];
637 int val = atoi(azArg[1]);
638 for(j=0; z[j]; j++){
639 if( isupper(z[j]) ) z[j] = tolower(z[j]);
640 }
641 if( strcmp(z,"on")==0 ){
642 val = 1;
643 }else if( strcmp(z,"yes")==0 ){
644 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000645 }
drh75897232000-05-29 14:26:00 +0000646 p->showHeader = val;
647 }else
648
649 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
650 fprintf(stderr,zHelp);
651 }else
652
653 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
654 struct callback_data data;
655 char *zErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000656 memcpy(&data, p, sizeof(data));
657 data.showHeader = 0;
658 data.mode = MODE_List;
persicom1d0b8722002-04-18 02:53:04 +0000659 sqlite_exec_printf(db,
drha18c5682000-10-08 22:20:57 +0000660 "SELECT name FROM sqlite_master "
661 "WHERE type='index' AND tbl_name LIKE '%q' "
drhe0bc4042002-06-25 01:09:11 +0000662 "UNION ALL "
663 "SELECT name FROM sqlite_temp_master "
664 "WHERE type='index' AND tbl_name LIKE '%q' "
665 "ORDER BY 1",
666 callback, &data, &zErrMsg, azArg[1], azArg[1]
drha18c5682000-10-08 22:20:57 +0000667 );
drh75897232000-05-29 14:26:00 +0000668 if( zErrMsg ){
669 fprintf(stderr,"Error: %s\n", zErrMsg);
670 free(zErrMsg);
671 }
672 }else
673
drh28bd4bc2000-06-15 15:57:22 +0000674 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
drh75897232000-05-29 14:26:00 +0000675 int n2 = strlen(azArg[1]);
persicom7e2dfdd2002-04-18 02:46:52 +0000676 if( strncmp(azArg[1],"line",n2)==0
677 ||
678 strncmp(azArg[1],"lines",n2)==0 ){
drh75897232000-05-29 14:26:00 +0000679 p->mode = MODE_Line;
persicom7e2dfdd2002-04-18 02:46:52 +0000680 }else if( strncmp(azArg[1],"column",n2)==0
681 ||
682 strncmp(azArg[1],"columns",n2)==0 ){
drh75897232000-05-29 14:26:00 +0000683 p->mode = MODE_Column;
684 }else if( strncmp(azArg[1],"list",n2)==0 ){
685 p->mode = MODE_List;
drh1e5d0e92000-05-31 23:33:17 +0000686 }else if( strncmp(azArg[1],"html",n2)==0 ){
687 p->mode = MODE_Html;
drh28bd4bc2000-06-15 15:57:22 +0000688 }else if( strncmp(azArg[1],"insert",n2)==0 ){
689 p->mode = MODE_Insert;
690 if( nArg>=3 ){
drh33048c02001-10-01 14:29:22 +0000691 set_table_name(p, azArg[2]);
drh28bd4bc2000-06-15 15:57:22 +0000692 }else{
drh33048c02001-10-01 14:29:22 +0000693 set_table_name(p, "table");
drh28bd4bc2000-06-15 15:57:22 +0000694 }
drhdaffd0e2001-04-11 14:28:42 +0000695 }else {
696 fprintf(stderr,"mode should be on of: column html insert line list\n");
drh75897232000-05-29 14:26:00 +0000697 }
698 }else
699
persicom7e2dfdd2002-04-18 02:46:52 +0000700 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
701 sprintf(p->nullvalue, "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
702 }else
703
drh411995d2002-06-25 19:31:18 +0000704 if( c=='o' && strncmp(azArg[0], "openaux", n)==0 ){
705 char *zErrMsg = 0;
706 sqlite_open_aux_file(db, nArg>=2 ? azArg[1] : 0, &zErrMsg);
707 if( zErrMsg ){
708 fprintf(stderr,"Error: %s\n", zErrMsg);
709 free(zErrMsg);
710 }
711 }else
712
drh75897232000-05-29 14:26:00 +0000713 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
714 if( p->out!=stdout ){
715 fclose(p->out);
716 }
717 if( strcmp(azArg[1],"stdout")==0 ){
718 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +0000719 strcpy(p->outfile,"stdout");
drh75897232000-05-29 14:26:00 +0000720 }else{
721 p->out = fopen(azArg[1], "w");
722 if( p->out==0 ){
723 fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
724 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +0000725 } else {
726 strcpy(p->outfile,azArg[1]);
drh75897232000-05-29 14:26:00 +0000727 }
728 }
729 }else
730
drhdd45df82002-04-18 12:39:03 +0000731 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
persicom7e2dfdd2002-04-18 02:46:52 +0000732 if( nArg >= 2) {
733 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
734 }
735 if( nArg >= 3) {
736 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
737 }
738 }else
739
740 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
741 sqlite_close(db);
742 exit(0);
743 }else
744
drhdaffd0e2001-04-11 14:28:42 +0000745 if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
746 FILE *alt = fopen(azArg[1], "r");
747 if( alt==0 ){
748 fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
749 }else{
750 process_input(p, alt);
751 fclose(alt);
752 }
753 }else
754
drh75897232000-05-29 14:26:00 +0000755 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
756 struct callback_data data;
757 char *zErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000758 memcpy(&data, p, sizeof(data));
759 data.showHeader = 0;
drhe3710332000-09-29 13:30:53 +0000760 data.mode = MODE_Semi;
drh75897232000-05-29 14:26:00 +0000761 if( nArg>1 ){
drhff9821a2001-04-04 21:22:14 +0000762 extern int sqliteStrICmp(const char*,const char*);
drha18c5682000-10-08 22:20:57 +0000763 if( sqliteStrICmp(azArg[1],"sqlite_master")==0 ){
764 char *new_argv[2], *new_colv[2];
765 new_argv[0] = "CREATE TABLE sqlite_master (\n"
766 " type text,\n"
767 " name text,\n"
768 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000769 " rootpage integer,\n"
drha18c5682000-10-08 22:20:57 +0000770 " sql text\n"
771 ")";
772 new_argv[1] = 0;
773 new_colv[0] = "sql";
774 new_colv[1] = 0;
775 callback(&data, 1, new_argv, new_colv);
drhe0bc4042002-06-25 01:09:11 +0000776 }else if( sqliteStrICmp(azArg[1],"sqlite_temp_master")==0 ){
777 char *new_argv[2], *new_colv[2];
778 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
779 " type text,\n"
780 " name text,\n"
781 " tbl_name text,\n"
782 " rootpage integer,\n"
783 " sql text\n"
784 ")";
785 new_argv[1] = 0;
786 new_colv[0] = "sql";
787 new_colv[1] = 0;
788 callback(&data, 1, new_argv, new_colv);
drha18c5682000-10-08 22:20:57 +0000789 }else{
790 sqlite_exec_printf(db,
drhe0bc4042002-06-25 01:09:11 +0000791 "SELECT sql FROM "
792 " (SELECT * FROM sqlite_master UNION ALL"
793 " SELECT * FROM sqlite_temp_master) "
drh33048c02001-10-01 14:29:22 +0000794 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +0000795 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000796 callback, &data, &zErrMsg, azArg[1]);
797 }
drh75897232000-05-29 14:26:00 +0000798 }else{
drha18c5682000-10-08 22:20:57 +0000799 sqlite_exec(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 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
806 );
drh75897232000-05-29 14:26:00 +0000807 }
drh75897232000-05-29 14:26:00 +0000808 if( zErrMsg ){
809 fprintf(stderr,"Error: %s\n", zErrMsg);
810 free(zErrMsg);
811 }
812 }else
813
814 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
815 sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
816 }else
817
persicom7e2dfdd2002-04-18 02:46:52 +0000818 if( c=='s' && strncmp(azArg[0], "show", n)==0){
819 int i;
820 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
drh67505e72002-04-19 12:34:06 +0000821 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
drhdd45df82002-04-18 12:39:03 +0000822 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
persicom7e2dfdd2002-04-18 02:46:52 +0000823 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
824 fprintf(p->out,"%9.9s: %s\n","nullvalue", p->nullvalue);
drh67505e72002-04-19 12:34:06 +0000825 fprintf(p->out,"%9.9s: %s\n","output",
826 strlen(p->outfile) ? p->outfile : "stdout");
persicom7e2dfdd2002-04-18 02:46:52 +0000827 fprintf(p->out,"%9.9s: %s\n","separator", p->separator);
828 fprintf(p->out,"%9.9s: ","width");
829 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
830 fprintf(p->out,"%d ",p->colWidth[i]);
831 }
832 fprintf(p->out,"\n\n");
833 }else
834
drh2dfbbca2000-07-28 14:32:48 +0000835 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
drhe3710332000-09-29 13:30:53 +0000836 char **azResult;
837 int nRow, rc;
838 char *zErrMsg;
drha50da102000-08-08 20:19:09 +0000839 if( nArg==1 ){
drha18c5682000-10-08 22:20:57 +0000840 rc = sqlite_get_table(db,
drha50da102000-08-08 20:19:09 +0000841 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +0000842 "WHERE type IN ('table','view') "
drhe0bc4042002-06-25 01:09:11 +0000843 "UNION ALL "
844 "SELECT name FROM sqlite_temp_master "
845 "WHERE type IN ('table','view') "
846 "ORDER BY 1",
drha18c5682000-10-08 22:20:57 +0000847 &azResult, &nRow, 0, &zErrMsg
848 );
drha50da102000-08-08 20:19:09 +0000849 }else{
drha18c5682000-10-08 22:20:57 +0000850 rc = sqlite_get_table_printf(db,
drha50da102000-08-08 20:19:09 +0000851 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +0000852 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
drhe0bc4042002-06-25 01:09:11 +0000853 "UNION ALL "
854 "SELECT name FROM sqlite_temp_master "
855 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
856 "ORDER BY 1",
857 &azResult, &nRow, 0, &zErrMsg, azArg[1], azArg[1]
drha18c5682000-10-08 22:20:57 +0000858 );
drha50da102000-08-08 20:19:09 +0000859 }
drh75897232000-05-29 14:26:00 +0000860 if( zErrMsg ){
861 fprintf(stderr,"Error: %s\n", zErrMsg);
862 free(zErrMsg);
863 }
drhe3710332000-09-29 13:30:53 +0000864 if( rc==SQLITE_OK ){
865 int len, maxlen = 0;
866 int i, j;
867 int nPrintCol, nPrintRow;
868 for(i=1; i<=nRow; i++){
869 if( azResult[i]==0 ) continue;
870 len = strlen(azResult[i]);
871 if( len>maxlen ) maxlen = len;
872 }
873 nPrintCol = 80/(maxlen+2);
874 if( nPrintCol<1 ) nPrintCol = 1;
875 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
876 for(i=0; i<nPrintRow; i++){
877 for(j=i+1; j<=nRow; j+=nPrintRow){
878 char *zSp = j<=nPrintRow ? "" : " ";
879 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
880 }
881 printf("\n");
882 }
883 }
884 sqlite_free_table(azResult);
drh75897232000-05-29 14:26:00 +0000885 }else
886
drh2dfbbca2000-07-28 14:32:48 +0000887 if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
888 sqlite_busy_timeout(db, atoi(azArg[1]));
889 }else
890
drh75897232000-05-29 14:26:00 +0000891 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
892 int j;
893 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
894 p->colWidth[j-1] = atoi(azArg[j]);
895 }
896 }else
897
898 {
drh67505e72002-04-19 12:34:06 +0000899 fprintf(stderr, "unknown command or invalid arguments: "
900 " \"%s\". Enter \".help\" for help\n", azArg[0]);
drh75897232000-05-29 14:26:00 +0000901 }
drh67505e72002-04-19 12:34:06 +0000902
903 return rc;
drh75897232000-05-29 14:26:00 +0000904}
905
drh67505e72002-04-19 12:34:06 +0000906/*
907** Read input from *in and process it. If *in==0 then input
908** is interactive - the user is typing it it. Otherwise, input
909** is coming from a file or device. A prompt is issued and history
910** is saved only if input is interactive. An interrupt signal will
911** cause this routine to exit immediately, unless input is interactive.
912*/
drhdaffd0e2001-04-11 14:28:42 +0000913static void process_input(struct callback_data *p, FILE *in){
914 char *zLine;
915 char *zSql = 0;
916 int nSql = 0;
917 char *zErrMsg;
drh7f953e22002-07-13 17:33:45 +0000918 int rc;
drh41e941d2002-04-04 15:10:12 +0000919 while( fflush(p->out), (zLine = one_input_line(zSql, in))!=0 ){
drh67505e72002-04-19 12:34:06 +0000920 if( seenInterrupt ){
921 if( in!=0 ) break;
922 seenInterrupt = 0;
923 }
drhdaffd0e2001-04-11 14:28:42 +0000924 if( p->echoOn ) printf("%s\n", zLine);
drh2af0b2d2002-02-21 02:25:02 +0000925 if( zLine && zLine[0]=='.' && nSql==0 ){
drh67505e72002-04-19 12:34:06 +0000926 int rc = do_meta_command(zLine, db, p);
drhdaffd0e2001-04-11 14:28:42 +0000927 free(zLine);
drh67505e72002-04-19 12:34:06 +0000928 if( rc ) break;
drhdaffd0e2001-04-11 14:28:42 +0000929 continue;
930 }
931 if( zSql==0 ){
932 int i;
933 for(i=0; zLine[i] && isspace(zLine[i]); i++){}
934 if( zLine[i]!=0 ){
935 nSql = strlen(zLine);
936 zSql = malloc( nSql+1 );
937 strcpy(zSql, zLine);
938 }
939 }else{
940 int len = strlen(zLine);
941 zSql = realloc( zSql, nSql + len + 2 );
942 if( zSql==0 ){
943 fprintf(stderr,"%s: out of memory!\n", Argv0);
944 exit(1);
945 }
946 strcpy(&zSql[nSql++], "\n");
947 strcpy(&zSql[nSql], zLine);
948 nSql += len;
949 }
950 free(zLine);
951 if( zSql && sqlite_complete(zSql) ){
952 p->cnt = 0;
drh7f953e22002-07-13 17:33:45 +0000953 rc = sqlite_exec(db, zSql, callback, p, &zErrMsg);
954 if( rc || zErrMsg ){
drhdaffd0e2001-04-11 14:28:42 +0000955 if( in!=0 && !p->echoOn ) printf("%s\n",zSql);
drh7f953e22002-07-13 17:33:45 +0000956 if( zErrMsg!=0 ){
957 printf("SQL error: %s\n", zErrMsg);
958 free(zErrMsg);
959 zErrMsg = 0;
960 }else{
961 printf("SQL error: %s\n", sqlite_error_string(rc));
962 }
drhdaffd0e2001-04-11 14:28:42 +0000963 }
964 free(zSql);
965 zSql = 0;
966 nSql = 0;
967 }
968 }
969 if( zSql ){
970 printf("Incomplete SQL: %s\n", zSql);
971 free(zSql);
972 }
973}
974
drh67505e72002-04-19 12:34:06 +0000975/*
976** Return a pathname which is the user's home directory. A
977** 0 return indicates an error of some kind. Space to hold the
978** resulting string is obtained from malloc(). The calling
979** function should free the result.
980*/
981static char *find_home_dir(void){
982 char *home_dir = NULL;
persicom7e2dfdd2002-04-18 02:46:52 +0000983
drh67505e72002-04-19 12:34:06 +0000984#if !defined(_WIN32) && !defined(WIN32)
985 struct passwd *pwent;
986 uid_t uid = getuid();
987 while( (pwent=getpwent()) != NULL) {
988 if(pwent->pw_uid == uid) {
989 home_dir = pwent->pw_dir;
990 break;
991 }
992 }
993#endif
994
995 if (!home_dir) {
996 home_dir = getenv("HOME");
997 if (!home_dir) {
998 home_dir = getenv("HOMEPATH"); /* Windows? */
999 }
1000 }
1001
drhe98d4fa2002-04-21 19:06:22 +00001002#if defined(_WIN32) || defined(WIN32)
1003 if (!home_dir) {
1004 home_dir = "c:";
1005 }
1006#endif
1007
drh67505e72002-04-19 12:34:06 +00001008 if( home_dir ){
1009 char *z = malloc( strlen(home_dir)+1 );
1010 if( z ) strcpy(z, home_dir);
1011 home_dir = z;
1012 }
drhe98d4fa2002-04-21 19:06:22 +00001013
drh67505e72002-04-19 12:34:06 +00001014 return home_dir;
1015}
1016
1017/*
1018** Read input from the file given by sqliterc_override. Or if that
1019** parameter is NULL, take input from ~/.sqliterc
1020*/
1021static void process_sqliterc(struct callback_data *p, char *sqliterc_override){
persicom7e2dfdd2002-04-18 02:46:52 +00001022 char *home_dir = NULL;
1023 char *sqliterc = sqliterc_override;
persicom7e2dfdd2002-04-18 02:46:52 +00001024 FILE *in = NULL;
1025
1026 if (sqliterc == NULL) {
drh67505e72002-04-19 12:34:06 +00001027 home_dir = find_home_dir();
drhe98d4fa2002-04-21 19:06:22 +00001028 if( home_dir==0 ){
1029 fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0);
1030 return;
1031 }
1032 sqliterc = malloc(strlen(home_dir) + 15);
persicom7e2dfdd2002-04-18 02:46:52 +00001033 if( sqliterc==0 ){
1034 fprintf(stderr,"%s: out of memory!\n", Argv0);
1035 exit(1);
1036 }
1037 sprintf(sqliterc,"%s/.sqliterc",home_dir);
drh67505e72002-04-19 12:34:06 +00001038 free(home_dir);
persicom7e2dfdd2002-04-18 02:46:52 +00001039 }
1040 in = fopen(sqliterc,"r");
drh67505e72002-04-19 12:34:06 +00001041 if(in) {
persicom7e2dfdd2002-04-18 02:46:52 +00001042 printf("Loading resources from %s\n",sqliterc);
1043 process_input(p,in);
drhdd45df82002-04-18 12:39:03 +00001044 fclose(in);
persicom7e2dfdd2002-04-18 02:46:52 +00001045 }
1046 return;
1047}
1048
drh67505e72002-04-19 12:34:06 +00001049/*
1050** Initialize the state information in data
1051*/
persicom7e2dfdd2002-04-18 02:46:52 +00001052void main_init(struct callback_data *data) {
1053 memset(data, 0, sizeof(*data));
1054 data->mode = MODE_List;
1055 strcpy(data->separator,"|");
1056 data->showHeader = 0;
1057 strcpy(mainPrompt,"sqlite> ");
1058 strcpy(continuePrompt," ...> ");
1059}
1060
drh75897232000-05-29 14:26:00 +00001061int main(int argc, char **argv){
drh75897232000-05-29 14:26:00 +00001062 char *zErrMsg = 0;
1063 struct callback_data data;
persicom7e2dfdd2002-04-18 02:46:52 +00001064 int origArgc = argc;
1065 char **origArgv = argv;
drh75897232000-05-29 14:26:00 +00001066
drhdaffd0e2001-04-11 14:28:42 +00001067 Argv0 = argv[0];
persicom7e2dfdd2002-04-18 02:46:52 +00001068 main_init(&data);
1069 process_sqliterc(&data,NULL);
1070
drh4c504392000-10-16 22:06:40 +00001071#ifdef SIGINT
1072 signal(SIGINT, interrupt_handler);
1073#endif
drh1e5d0e92000-05-31 23:33:17 +00001074 while( argc>=2 && argv[1][0]=='-' ){
persicom7e2dfdd2002-04-18 02:46:52 +00001075 if( argc>=3 && strcmp(argv[1],"-init")==0 ){
1076 /* If we get a -init to do, we have to pretend that
1077 ** it replaced the .sqliterc file. Soooo, in order to
1078 ** do that we need to start from scratch...*/
1079 main_init(&data);
1080
1081 /* treat this file as the sqliterc... */
1082 process_sqliterc(&data,argv[2]);
1083
1084 /* fix up the command line so we do not re-read
1085 ** the option next time around... */
1086 {
1087 int i = 1;
1088 for(i=1;i<=argc-2;i++) {
1089 argv[i] = argv[i+2];
1090 }
1091 }
1092 origArgc-=2;
1093
1094 /* and reset the command line options to be re-read.*/
1095 argv = origArgv;
1096 argc = origArgc;
1097
1098 }else if( strcmp(argv[1],"-html")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001099 data.mode = MODE_Html;
1100 argc--;
1101 argv++;
1102 }else if( strcmp(argv[1],"-list")==0 ){
1103 data.mode = MODE_List;
1104 argc--;
1105 argv++;
1106 }else if( strcmp(argv[1],"-line")==0 ){
1107 data.mode = MODE_Line;
1108 argc--;
1109 argv++;
drh8b32e172002-04-08 02:42:57 +00001110 }else if( strcmp(argv[1],"-column")==0 ){
1111 data.mode = MODE_Column;
1112 argc--;
1113 argv++;
drh7613bfa2002-01-22 12:39:24 +00001114 }else if( argc>=3 && strcmp(argv[1],"-separator")==0 ){
drhbed86902000-06-02 13:27:59 +00001115 sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[2]);
drh1e5d0e92000-05-31 23:33:17 +00001116 argc -= 2;
1117 argv += 2;
persicom7e2dfdd2002-04-18 02:46:52 +00001118 }else if( argc>=3 && strcmp(argv[1],"-nullvalue")==0 ){
1119 sprintf(data.nullvalue,"%.*s",(int)sizeof(data.nullvalue)-1,argv[2]);
1120 argc -= 2;
1121 argv += 2;
drh1e5d0e92000-05-31 23:33:17 +00001122 }else if( strcmp(argv[1],"-header")==0 ){
1123 data.showHeader = 1;
1124 argc--;
1125 argv++;
1126 }else if( strcmp(argv[1],"-noheader")==0 ){
1127 data.showHeader = 0;
1128 argc--;
1129 argv++;
drh660f68d2001-01-04 14:27:07 +00001130 }else if( strcmp(argv[1],"-echo")==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001131 data.echoOn = 1;
drh660f68d2001-01-04 14:27:07 +00001132 argc--;
1133 argv++;
drh1e5d0e92000-05-31 23:33:17 +00001134 }else{
drhdaffd0e2001-04-11 14:28:42 +00001135 fprintf(stderr,"%s: unknown option: %s\n", Argv0, argv[1]);
drh1e5d0e92000-05-31 23:33:17 +00001136 return 1;
1137 }
1138 }
drh75897232000-05-29 14:26:00 +00001139 if( argc!=2 && argc!=3 ){
drhdaffd0e2001-04-11 14:28:42 +00001140 fprintf(stderr,"Usage: %s ?OPTIONS? FILENAME ?SQL?\n", Argv0);
drh75897232000-05-29 14:26:00 +00001141 exit(1);
1142 }
drh4c653a02000-06-07 01:27:47 +00001143 data.db = db = sqlite_open(argv[1], 0666, &zErrMsg);
drh75897232000-05-29 14:26:00 +00001144 if( db==0 ){
drh167a4b12000-08-17 09:49:59 +00001145 data.db = db = sqlite_open(argv[1], 0444, &zErrMsg);
1146 if( db==0 ){
1147 if( zErrMsg ){
1148 fprintf(stderr,"Unable to open database \"%s\": %s\n", argv[1],zErrMsg);
1149 }else{
1150 fprintf(stderr,"Unable to open database %s\n", argv[1]);
1151 }
1152 exit(1);
drhd1dedb82000-06-05 02:07:04 +00001153 }else{
drh80afdca2000-08-22 13:27:22 +00001154 fprintf(stderr,"Database \"%s\" opened READ ONLY!\n", argv[1]);
drhd1dedb82000-06-05 02:07:04 +00001155 }
drh75897232000-05-29 14:26:00 +00001156 }
drh75897232000-05-29 14:26:00 +00001157 data.out = stdout;
1158 if( argc==3 ){
drh6ff13852001-11-25 13:18:23 +00001159 if( argv[2][0]=='.' ){
1160 do_meta_command(argv[2], db, &data);
1161 exit(0);
1162 }else{
1163 int rc;
1164 rc = sqlite_exec(db, argv[2], callback, &data, &zErrMsg);
1165 if( rc!=0 && zErrMsg!=0 ){
1166 fprintf(stderr,"SQL error: %s\n", zErrMsg);
1167 exit(1);
1168 }
drh75897232000-05-29 14:26:00 +00001169 }
1170 }else{
drh382c0242001-10-06 16:33:02 +00001171 extern int isatty();
drhdaffd0e2001-04-11 14:28:42 +00001172 if( isatty(0) ){
drh67505e72002-04-19 12:34:06 +00001173 char *zHome;
1174 char *zHistory = 0;
drh75897232000-05-29 14:26:00 +00001175 printf(
drhb217a572000-08-22 13:40:18 +00001176 "SQLite version %s\n"
1177 "Enter \".help\" for instructions\n",
1178 sqlite_version
drh75897232000-05-29 14:26:00 +00001179 );
drh67505e72002-04-19 12:34:06 +00001180 zHome = find_home_dir();
1181 if( zHome && (zHistory = malloc(strlen(zHome)+20))!=0 ){
1182 sprintf(zHistory,"%s/.sqlite_history", zHome);
1183 }
1184 if( zHistory ) read_history(zHistory);
drhdaffd0e2001-04-11 14:28:42 +00001185 process_input(&data, 0);
drh67505e72002-04-19 12:34:06 +00001186 if( zHistory ){
1187 stifle_history(100);
1188 write_history(zHistory);
1189 }
drhdaffd0e2001-04-11 14:28:42 +00001190 }else{
1191 process_input(&data, stdin);
drh75897232000-05-29 14:26:00 +00001192 }
1193 }
drh33048c02001-10-01 14:29:22 +00001194 set_table_name(&data, 0);
drh75897232000-05-29 14:26:00 +00001195 sqlite_close(db);
1196 return 0;
1197}