blob: 803c320cd6d3bf9f226862f5f12a15c5aa04dcec [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**
persicom7e2dfdd2002-04-18 02:46:52 +000015** $Id: shell.c,v 1.51 2002/04/18 02:46:52 persicom 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#include <pwd.h>
23#include <sys/types.h>
24
drha297b5c2002-01-15 18:39:43 +000025#if !defined(_WIN32) && !defined(WIN32)
drh4c504392000-10-16 22:06:40 +000026# include <signal.h>
27#endif
drh75897232000-05-29 14:26:00 +000028
drh16e59552000-07-31 11:57:37 +000029#if defined(HAVE_READLINE) && HAVE_READLINE==1
drh8e7e7a22000-05-30 18:45:23 +000030# include <readline/readline.h>
31# include <readline/history.h>
32#else
drh5e00f6c2001-09-13 13:46:56 +000033# define readline(p) getline(p,stdin)
drh8e7e7a22000-05-30 18:45:23 +000034# define add_history(X)
drh75897232000-05-29 14:26:00 +000035#endif
36
37/*
drh4c504392000-10-16 22:06:40 +000038** The following is the open SQLite database. We make a pointer
39** to this database a static variable so that it can be accessed
40** by the SIGINT handler to interrupt database processing.
41*/
42static sqlite *db = 0;
43
44/*
persicom7e2dfdd2002-04-18 02:46:52 +000045** This is the name of our program. It is set in main(), used
46** in a number of other places, mostly for error messages.
47*/
48static char *Argv0;
49
50/*
51** Prompt strings. Initialized in main. Settable with
52** .prompt main continue
53*/
54static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
55static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
56
57/*
drh8e7e7a22000-05-30 18:45:23 +000058** This routine reads a line of text from standard input, stores
59** the text in memory obtained from malloc() and returns a pointer
60** to the text. NULL is returned at end of file, or if malloc()
61** fails.
62**
63** The interface is like "readline" but no command-line editing
64** is done.
65*/
drhdaffd0e2001-04-11 14:28:42 +000066static char *getline(char *zPrompt, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +000067 char *zLine;
68 int nLine;
drh8e7e7a22000-05-30 18:45:23 +000069 int n;
70 int eol;
71
72 if( zPrompt && *zPrompt ){
73 printf("%s",zPrompt);
74 fflush(stdout);
75 }
76 nLine = 100;
77 zLine = malloc( nLine );
78 if( zLine==0 ) return 0;
79 n = 0;
80 eol = 0;
81 while( !eol ){
82 if( n+100>nLine ){
83 nLine = nLine*2 + 100;
84 zLine = realloc(zLine, nLine);
85 if( zLine==0 ) return 0;
86 }
drhdaffd0e2001-04-11 14:28:42 +000087 if( fgets(&zLine[n], nLine - n, in)==0 ){
drh8e7e7a22000-05-30 18:45:23 +000088 if( n==0 ){
89 free(zLine);
90 return 0;
91 }
92 zLine[n] = 0;
93 eol = 1;
94 break;
95 }
96 while( zLine[n] ){ n++; }
97 if( n>0 && zLine[n-1]=='\n' ){
98 n--;
99 zLine[n] = 0;
100 eol = 1;
101 }
102 }
103 zLine = realloc( zLine, n+1 );
104 return zLine;
105}
106
107/*
108** Retrieve a single line of input text. "isatty" is true if text
109** is coming from a terminal. In that case, we issue a prompt and
110** attempt to use "readline" for command-line editing. If "isatty"
drhaacc5432002-01-06 17:07:40 +0000111** is false, use "getline" instead of "readline" and issue no prompt.
drh8e7e7a22000-05-30 18:45:23 +0000112**
113** zPrior is a string of prior text retrieved. If not the empty
114** string, then issue a continuation prompt.
115*/
drhdaffd0e2001-04-11 14:28:42 +0000116static char *one_input_line(const char *zPrior, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +0000117 char *zPrompt;
118 char *zResult;
drhdaffd0e2001-04-11 14:28:42 +0000119 if( in!=0 ){
120 return getline(0, in);
drh8e7e7a22000-05-30 18:45:23 +0000121 }
122 if( zPrior && zPrior[0] ){
persicom7e2dfdd2002-04-18 02:46:52 +0000123 zPrompt = continuePrompt;
drh8e7e7a22000-05-30 18:45:23 +0000124 }else{
persicom7e2dfdd2002-04-18 02:46:52 +0000125 zPrompt = mainPrompt;
drh8e7e7a22000-05-30 18:45:23 +0000126 }
127 zResult = readline(zPrompt);
drh2dfbbca2000-07-28 14:32:48 +0000128 if( zResult ) add_history(zResult);
drh8e7e7a22000-05-30 18:45:23 +0000129 return zResult;
130}
131
persicom7e2dfdd2002-04-18 02:46:52 +0000132struct previous_mode_data {
133 int valid; /* Is there legit data in here? */
134 int mode;
135 int showHeader;
136 int colWidth[100];
137};
drh8e7e7a22000-05-30 18:45:23 +0000138/*
drh75897232000-05-29 14:26:00 +0000139** An pointer to an instance of this structure is passed from
140** the main program to the callback. This is used to communicate
141** state and mode information.
142*/
143struct callback_data {
drh28bd4bc2000-06-15 15:57:22 +0000144 sqlite *db; /* The database */
drhdaffd0e2001-04-11 14:28:42 +0000145 int echoOn; /* True to echo input commands */
drh28bd4bc2000-06-15 15:57:22 +0000146 int cnt; /* Number of records displayed so far */
147 FILE *out; /* Write results here */
148 int mode; /* An output mode setting */
149 int showHeader; /* True to show column names in List or Column mode */
drh33048c02001-10-01 14:29:22 +0000150 char *zDestTable; /* Name of destination table when MODE_Insert */
drh28bd4bc2000-06-15 15:57:22 +0000151 char separator[20]; /* Separator character for MODE_List */
drha0c66f52000-07-29 13:20:21 +0000152 int colWidth[100]; /* Requested width of each column when in column mode*/
153 int actualWidth[100]; /* Actual width of each column */
persicom7e2dfdd2002-04-18 02:46:52 +0000154 char nullvalue[20]; /* The text to print when a NULL comes back from the database */
155 struct previous_mode_data explainPrev;
156 /* Holds the mode information just before .explain ON */
157 char outfile[FILENAME_MAX];
158 /* Filename for *out */
drh75897232000-05-29 14:26:00 +0000159};
160
161/*
162** These are the allowed modes.
163*/
drh967e8b72000-06-21 13:59:10 +0000164#define MODE_Line 0 /* One column per line. Blank line between records */
drh75897232000-05-29 14:26:00 +0000165#define MODE_Column 1 /* One record per line in neat columns */
166#define MODE_List 2 /* One record per line with a separator */
drhe3710332000-09-29 13:30:53 +0000167#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
168#define MODE_Html 4 /* Generate an XHTML table */
169#define MODE_Insert 5 /* Generate SQL "insert" statements */
persicom7e2dfdd2002-04-18 02:46:52 +0000170#define MODE_NUM_OF 6
171
172char *modeDescr[MODE_NUM_OF] = {
173 "line",
174 "column",
175 "list",
176 "semi",
177 "html",
178 "insert"
179};
drh75897232000-05-29 14:26:00 +0000180
181/*
182** Number of elements in an array
183*/
184#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
185
186/*
drh28bd4bc2000-06-15 15:57:22 +0000187** Return TRUE if the string supplied is a number of some kinds.
188*/
189static int is_numeric(const char *z){
190 int seen_digit = 0;
191 if( *z=='-' || *z=='+' ){
192 z++;
193 }
194 while( isdigit(*z) ){
195 seen_digit = 1;
196 z++;
197 }
198 if( seen_digit && *z=='.' ){
199 z++;
200 while( isdigit(*z) ){ z++; }
201 }
202 if( seen_digit && (*z=='e' || *z=='E')
203 && (isdigit(z[1]) || ((z[1]=='-' || z[1]=='+') && isdigit(z[2])))
204 ){
205 z+=2;
206 while( isdigit(*z) ){ z++; }
207 }
208 return seen_digit && *z==0;
209}
210
211/*
212** Output the given string as a quoted string using SQL quoting conventions.
213*/
214static void output_quoted_string(FILE *out, const char *z){
215 int i;
216 int nSingle = 0;
217 int nDouble = 0;
218 for(i=0; z[i]; i++){
219 if( z[i]=='\'' ) nSingle++;
220 else if( z[i]=='"' ) nDouble++;
221 }
222 if( nSingle==0 ){
223 fprintf(out,"'%s'",z);
224 }else if( nDouble==0 ){
225 fprintf(out,"\"%s\"",z);
226 }else{
227 fprintf(out,"'");
228 while( *z ){
229 for(i=0; z[i] && z[i]!='\''; i++){}
230 if( i==0 ){
231 fprintf(out,"''");
232 z++;
233 }else if( z[i]=='\'' ){
234 fprintf(out,"%.*s''",i,z);
235 z += i+1;
236 }else{
drhcd7d2732002-02-26 23:24:26 +0000237 fprintf(out,"%s",z);
drh28bd4bc2000-06-15 15:57:22 +0000238 break;
239 }
240 }
drhcd7d2732002-02-26 23:24:26 +0000241 fprintf(out,"'");
drh28bd4bc2000-06-15 15:57:22 +0000242 }
243}
244
245/*
drhc08a4f12000-06-15 16:49:48 +0000246** Output the given string with characters that are special to
247** HTML escaped.
248*/
249static void output_html_string(FILE *out, const char *z){
250 int i;
251 while( *z ){
252 for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){}
253 if( i>0 ){
254 fprintf(out,"%.*s",i,z);
255 }
256 if( z[i]=='<' ){
257 fprintf(out,"&lt;");
258 }else if( z[i]=='&' ){
259 fprintf(out,"&amp;");
260 }else{
261 break;
262 }
263 z += i + 1;
264 }
265}
266
267/*
drh4c504392000-10-16 22:06:40 +0000268** This routine runs when the user presses Ctrl-C
269*/
270static void interrupt_handler(int NotUsed){
271 if( db ) sqlite_interrupt(db);
272}
273
274/*
drh75897232000-05-29 14:26:00 +0000275** This is the callback routine that the SQLite library
276** invokes for each row of a query result.
277*/
278static int callback(void *pArg, int nArg, char **azArg, char **azCol){
279 int i;
280 struct callback_data *p = (struct callback_data*)pArg;
281 switch( p->mode ){
282 case MODE_Line: {
drhe3710332000-09-29 13:30:53 +0000283 int w = 5;
drh6a535342001-10-19 16:44:56 +0000284 if( azArg==0 ) break;
drhe3710332000-09-29 13:30:53 +0000285 for(i=0; i<nArg; i++){
286 int len = strlen(azCol[i]);
287 if( len>w ) w = len;
288 }
drh75897232000-05-29 14:26:00 +0000289 if( p->cnt++>0 ) fprintf(p->out,"\n");
290 for(i=0; i<nArg; i++){
persicom7e2dfdd2002-04-18 02:46:52 +0000291 fprintf(p->out,"%*s = %s\n", w, azCol[i], azArg[i] ? azArg[i] : p->nullvalue);
drh75897232000-05-29 14:26:00 +0000292 }
293 break;
294 }
295 case MODE_Column: {
drha0c66f52000-07-29 13:20:21 +0000296 if( p->cnt++==0 ){
drh75897232000-05-29 14:26:00 +0000297 for(i=0; i<nArg; i++){
drha0c66f52000-07-29 13:20:21 +0000298 int w, n;
299 if( i<ArraySize(p->colWidth) ){
drh75897232000-05-29 14:26:00 +0000300 w = p->colWidth[i];
301 }else{
drha0c66f52000-07-29 13:20:21 +0000302 w = 0;
drh75897232000-05-29 14:26:00 +0000303 }
drha0c66f52000-07-29 13:20:21 +0000304 if( w<=0 ){
drhff6e9112000-08-28 16:21:58 +0000305 w = strlen(azCol[i] ? azCol[i] : "");
drha0c66f52000-07-29 13:20:21 +0000306 if( w<10 ) w = 10;
persicom7e2dfdd2002-04-18 02:46:52 +0000307 n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue);
drha0c66f52000-07-29 13:20:21 +0000308 if( w<n ) w = n;
309 }
310 if( i<ArraySize(p->actualWidth) ){
311 p->actualWidth[i] = w;
312 }
313 if( p->showHeader ){
314 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
315 }
316 }
317 if( p->showHeader ){
318 for(i=0; i<nArg; i++){
319 int w;
320 if( i<ArraySize(p->actualWidth) ){
321 w = p->actualWidth[i];
322 }else{
323 w = 10;
324 }
325 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
326 "----------------------------------------------------------",
327 i==nArg-1 ? "\n": " ");
328 }
drh75897232000-05-29 14:26:00 +0000329 }
330 }
drh6a535342001-10-19 16:44:56 +0000331 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000332 for(i=0; i<nArg; i++){
333 int w;
drha0c66f52000-07-29 13:20:21 +0000334 if( i<ArraySize(p->actualWidth) ){
335 w = p->actualWidth[i];
drh75897232000-05-29 14:26:00 +0000336 }else{
337 w = 10;
338 }
drhc61053b2000-06-04 12:58:36 +0000339 fprintf(p->out,"%-*.*s%s",w,w,
persicom7e2dfdd2002-04-18 02:46:52 +0000340 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
drh75897232000-05-29 14:26:00 +0000341 }
342 break;
343 }
drhe3710332000-09-29 13:30:53 +0000344 case MODE_Semi:
drh75897232000-05-29 14:26:00 +0000345 case MODE_List: {
346 if( p->cnt++==0 && p->showHeader ){
347 for(i=0; i<nArg; i++){
348 fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
349 }
350 }
drh6a535342001-10-19 16:44:56 +0000351 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000352 for(i=0; i<nArg; i++){
drh4c653a02000-06-07 01:27:47 +0000353 char *z = azArg[i];
persicom7e2dfdd2002-04-18 02:46:52 +0000354 if( z==0 ) z = p->nullvalue;
drh71172c52002-01-24 00:00:21 +0000355 fprintf(p->out, "%s", z);
drhe3710332000-09-29 13:30:53 +0000356 if( i<nArg-1 ){
357 fprintf(p->out, "%s", p->separator);
358 }else if( p->mode==MODE_Semi ){
359 fprintf(p->out, ";\n");
360 }else{
361 fprintf(p->out, "\n");
362 }
drh75897232000-05-29 14:26:00 +0000363 }
364 break;
365 }
drh1e5d0e92000-05-31 23:33:17 +0000366 case MODE_Html: {
367 if( p->cnt++==0 && p->showHeader ){
368 fprintf(p->out,"<TR>");
369 for(i=0; i<nArg; i++){
370 fprintf(p->out,"<TH>%s</TH>",azCol[i]);
371 }
372 fprintf(p->out,"</TR>\n");
373 }
drh6a535342001-10-19 16:44:56 +0000374 if( azArg==0 ) break;
drh28bd4bc2000-06-15 15:57:22 +0000375 fprintf(p->out,"<TR>");
drh1e5d0e92000-05-31 23:33:17 +0000376 for(i=0; i<nArg; i++){
drhc08a4f12000-06-15 16:49:48 +0000377 fprintf(p->out,"<TD>");
persicom7e2dfdd2002-04-18 02:46:52 +0000378 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
drhc08a4f12000-06-15 16:49:48 +0000379 fprintf(p->out,"</TD>\n");
drh1e5d0e92000-05-31 23:33:17 +0000380 }
drh28bd4bc2000-06-15 15:57:22 +0000381 fprintf(p->out,"</TD></TR>\n");
drh1e5d0e92000-05-31 23:33:17 +0000382 break;
383 }
drh28bd4bc2000-06-15 15:57:22 +0000384 case MODE_Insert: {
drh6a535342001-10-19 16:44:56 +0000385 if( azArg==0 ) break;
drh33048c02001-10-01 14:29:22 +0000386 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
drh28bd4bc2000-06-15 15:57:22 +0000387 for(i=0; i<nArg; i++){
388 char *zSep = i>0 ? ",": "";
389 if( azArg[i]==0 ){
390 fprintf(p->out,"%sNULL",zSep);
391 }else if( is_numeric(azArg[i]) ){
392 fprintf(p->out,"%s%s",zSep, azArg[i]);
393 }else{
394 if( zSep[0] ) fprintf(p->out,"%s",zSep);
395 output_quoted_string(p->out, azArg[i]);
396 }
397 }
398 fprintf(p->out,");\n");
drh6a535342001-10-19 16:44:56 +0000399 break;
drh28bd4bc2000-06-15 15:57:22 +0000400 }
drh75897232000-05-29 14:26:00 +0000401 }
402 return 0;
403}
404
405/*
drh33048c02001-10-01 14:29:22 +0000406** Set the destination table field of the callback_data structure to
407** the name of the table given. Escape any quote characters in the
408** table name.
409*/
410static void set_table_name(struct callback_data *p, const char *zName){
411 int i, n;
412 int needQuote;
413 char *z;
414
415 if( p->zDestTable ){
416 free(p->zDestTable);
417 p->zDestTable = 0;
418 }
419 if( zName==0 ) return;
420 needQuote = !isalpha(*zName) && *zName!='_';
421 for(i=n=0; zName[i]; i++, n++){
422 if( !isalnum(zName[i]) && zName[i]!='_' ){
423 needQuote = 1;
424 if( zName[i]=='\'' ) n++;
425 }
426 }
427 if( needQuote ) n += 2;
428 z = p->zDestTable = malloc( n+1 );
429 if( z==0 ){
430 fprintf(stderr,"Out of memory!\n");
431 exit(1);
432 }
433 n = 0;
434 if( needQuote ) z[n++] = '\'';
435 for(i=0; zName[i]; i++){
436 z[n++] = zName[i];
437 if( zName[i]=='\'' ) z[n++] = '\'';
438 }
439 if( needQuote ) z[n++] = '\'';
440 z[n] = 0;
441}
442
443/*
drh4c653a02000-06-07 01:27:47 +0000444** This is a different callback routine used for dumping the database.
445** Each row received by this callback consists of a table name,
446** the table type ("index" or "table") and SQL to create the table.
447** This routine should print text sufficient to recreate the table.
448*/
449static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
drhdaffd0e2001-04-11 14:28:42 +0000450 struct callback_data *p = (struct callback_data *)pArg;
drh4c653a02000-06-07 01:27:47 +0000451 if( nArg!=3 ) return 1;
drhdaffd0e2001-04-11 14:28:42 +0000452 fprintf(p->out, "%s;\n", azArg[2]);
drh4c653a02000-06-07 01:27:47 +0000453 if( strcmp(azArg[1],"table")==0 ){
454 struct callback_data d2;
drhdaffd0e2001-04-11 14:28:42 +0000455 d2 = *p;
drh33048c02001-10-01 14:29:22 +0000456 d2.mode = MODE_Insert;
457 d2.zDestTable = 0;
458 set_table_name(&d2, azArg[0]);
drhdaffd0e2001-04-11 14:28:42 +0000459 sqlite_exec_printf(p->db,
drha18c5682000-10-08 22:20:57 +0000460 "SELECT * FROM '%q'",
461 callback, &d2, 0, azArg[0]
462 );
drh33048c02001-10-01 14:29:22 +0000463 set_table_name(&d2, 0);
drh4c653a02000-06-07 01:27:47 +0000464 }
drh4c653a02000-06-07 01:27:47 +0000465 return 0;
466}
467
468/*
drh75897232000-05-29 14:26:00 +0000469** Text of a help message
470*/
471static char zHelp[] =
drh4c653a02000-06-07 01:27:47 +0000472 ".dump ?TABLE? ... Dump the database in an text format\n"
drhdaffd0e2001-04-11 14:28:42 +0000473 ".echo ON|OFF Turn command echo on or off\n"
drh75897232000-05-29 14:26:00 +0000474 ".exit Exit this program\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000475 ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n"
476 " \"off\" will revert to the output mode that was\n"
477 " previously in effect\n"
478 ".header(s) ON|OFF Turn display of headers on or off\n"
drh75897232000-05-29 14:26:00 +0000479 ".help Show this message\n"
480 ".indices TABLE Show names of all indices on TABLE\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000481 ".mode MODE Set mode to one of \"line(s)\", \"column(s)\", \n"
drhe3710332000-09-29 13:30:53 +0000482 " \"insert\", \"list\", or \"html\"\n"
drhc08a4f12000-06-15 16:49:48 +0000483 ".mode insert TABLE Generate SQL insert statements for TABLE\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000484 ".nullvalue STRING Print STRING instead of nothing for NULL data\n"
drh75897232000-05-29 14:26:00 +0000485 ".output FILENAME Send output to FILENAME\n"
486 ".output stdout Send output to the screen\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000487 ".prompt MAIN CONTINUE Replace the standard prompts\n"
488 " \"sqlite > \" and \" ...> \"\n"
489 " with the strings MAIN and CONTINUE\n"
490 " CONTINUE is optional.\n"
491 " Special characters are:\n"
492 ".quit Exit this program\n"
drhdaffd0e2001-04-11 14:28:42 +0000493 ".read FILENAME Execute SQL in FILENAME\n"
494 ".reindex ?TABLE? Rebuild indices\n"
495/* ".rename OLD NEW Change the name of a table or index\n" */
drh75897232000-05-29 14:26:00 +0000496 ".schema ?TABLE? Show the CREATE statements\n"
497 ".separator STRING Change separator string for \"list\" mode\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000498 ".show Show the current values for the following:\n"
499 " .echo\n"
500 " .explain\n"
501 " .mode\n"
502 " .nullvalue\n"
503 " .output\n"
504 " .separator\n"
505 " .width\n"
drha50da102000-08-08 20:19:09 +0000506 ".tables ?PATTERN? List names of tables matching a pattern\n"
drh2dfbbca2000-07-28 14:32:48 +0000507 ".timeout MS Try opening locked tables for MS milliseconds\n"
drh75897232000-05-29 14:26:00 +0000508 ".width NUM NUM ... Set column widths for \"column\" mode\n"
509;
510
drhdaffd0e2001-04-11 14:28:42 +0000511/* Forward reference */
512static void process_input(struct callback_data *p, FILE *in);
513
drh75897232000-05-29 14:26:00 +0000514/*
515** If an input line begins with "." then invoke this routine to
516** process that line.
517*/
518static void do_meta_command(char *zLine, sqlite *db, struct callback_data *p){
519 int i = 1;
520 int nArg = 0;
521 int n, c;
522 char *azArg[50];
523
524 /* Parse the input line into tokens.
525 */
526 while( zLine[i] && nArg<ArraySize(azArg) ){
527 while( isspace(zLine[i]) ){ i++; }
528 if( zLine[i]=='\'' || zLine[i]=='"' ){
529 int delim = zLine[i++];
530 azArg[nArg++] = &zLine[i];
531 while( zLine[i] && zLine[i]!=delim ){ i++; }
532 if( zLine[i]==delim ){
533 zLine[i++] = 0;
534 }
535 }else{
536 azArg[nArg++] = &zLine[i];
537 while( zLine[i] && !isspace(zLine[i]) ){ i++; }
538 if( zLine[i] ) zLine[i++] = 0;
539 }
540 }
541
542 /* Process the input line.
543 */
544 if( nArg==0 ) return;
545 n = strlen(azArg[0]);
546 c = azArg[0][0];
drh4c653a02000-06-07 01:27:47 +0000547 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
548 char *zErrMsg = 0;
drh33048c02001-10-01 14:29:22 +0000549 fprintf(p->out, "BEGIN TRANSACTION;\n");
drh4c653a02000-06-07 01:27:47 +0000550 if( nArg==1 ){
drha18c5682000-10-08 22:20:57 +0000551 sqlite_exec(db,
552 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000553 "WHERE type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000554 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000555 dump_callback, p, &zErrMsg
556 );
drh4c653a02000-06-07 01:27:47 +0000557 }else{
558 int i;
559 for(i=1; i<nArg && zErrMsg==0; i++){
drha18c5682000-10-08 22:20:57 +0000560 sqlite_exec_printf(db,
561 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000562 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000563 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000564 dump_callback, p, &zErrMsg, azArg[i]
565 );
drh4c653a02000-06-07 01:27:47 +0000566 }
567 }
568 if( zErrMsg ){
569 fprintf(stderr,"Error: %s\n", zErrMsg);
570 free(zErrMsg);
drh33048c02001-10-01 14:29:22 +0000571 }else{
572 fprintf(p->out, "COMMIT;\n");
drh4c653a02000-06-07 01:27:47 +0000573 }
574 }else
drh75897232000-05-29 14:26:00 +0000575
drhdaffd0e2001-04-11 14:28:42 +0000576 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
577 int j;
578 char *z = azArg[1];
579 int val = atoi(azArg[1]);
580 for(j=0; z[j]; j++){
581 if( isupper(z[j]) ) z[j] = tolower(z[j]);
582 }
583 if( strcmp(z,"on")==0 ){
584 val = 1;
585 }else if( strcmp(z,"yes")==0 ){
586 val = 1;
587 }
588 p->echoOn = val;
589 }else
590
drh75897232000-05-29 14:26:00 +0000591 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
drh04096482001-11-09 22:41:44 +0000592 sqlite_close(db);
drh75897232000-05-29 14:26:00 +0000593 exit(0);
594 }else
595
persicom7e2dfdd2002-04-18 02:46:52 +0000596 if( c=='e' && strncmp(azArg[0], "explain", n)==0 && nArg>1){
597 int j;
598 char *z = azArg[1];
599 int val = atoi(azArg[1]);
600 for(j=0; z[j]; j++){
601 if( isupper(z[j]) ) z[j] = tolower(z[j]);
602 }
603 if( strcmp(z,"on")==0 ){
604 val = 1;
605 }else if( strcmp(z,"yes")==0 ){
606 val = 1;
607 }
608 if(val == 1) {
609 if(!p->explainPrev.valid) {
610 p->explainPrev.valid = 1;
611 p->explainPrev.mode = p->mode;
612 p->explainPrev.showHeader = p->showHeader;
613 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
614 }
615 /* We could put this code under the !p->explainValid
616 ** condition so that it does not execute if we are already in
617 ** explain mode. However, always executing it allows us an easy
618 ** was to reset to explain mode in case the user previously
619 ** did an .explain followed by a .width, .mode or .header
620 ** command.
621 */
622 p->mode = MODE_Column;
623 p->showHeader = 1;
624 memset(p->colWidth,0,ArraySize(p->colWidth));
625 p->colWidth[0] = 4;
626 p->colWidth[1] = 12;
627 p->colWidth[2] = 10;
628 p->colWidth[3] = 10;
629 p->colWidth[4] = 35;
630 }else if (p->explainPrev.valid) {
631 p->explainPrev.valid = 0;
632 p->mode = p->explainPrev.mode;
633 p->showHeader = p->explainPrev.showHeader;
634 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
635 }
drh75897232000-05-29 14:26:00 +0000636 }else
637
persicom7e2dfdd2002-04-18 02:46:52 +0000638 if( c=='h' && (strncmp(azArg[0], "header", n)==0
639 ||
640 strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){
drh75897232000-05-29 14:26:00 +0000641 int j;
642 char *z = azArg[1];
643 int val = atoi(azArg[1]);
644 for(j=0; z[j]; j++){
645 if( isupper(z[j]) ) z[j] = tolower(z[j]);
646 }
647 if( strcmp(z,"on")==0 ){
648 val = 1;
649 }else if( strcmp(z,"yes")==0 ){
650 val = 1;
651 }
652 p->showHeader = val;
653 }else
654
655 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
656 fprintf(stderr,zHelp);
657 }else
658
659 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
660 struct callback_data data;
661 char *zErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000662 memcpy(&data, p, sizeof(data));
663 data.showHeader = 0;
664 data.mode = MODE_List;
drha18c5682000-10-08 22:20:57 +0000665 sqlite_exec_printf(db,
666 "SELECT name FROM sqlite_master "
667 "WHERE type='index' AND tbl_name LIKE '%q' "
668 "ORDER BY name",
669 callback, &data, &zErrMsg, azArg[1]
670 );
drh75897232000-05-29 14:26:00 +0000671 if( zErrMsg ){
672 fprintf(stderr,"Error: %s\n", zErrMsg);
673 free(zErrMsg);
674 }
675 }else
676
drh28bd4bc2000-06-15 15:57:22 +0000677 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
drh75897232000-05-29 14:26:00 +0000678 int n2 = strlen(azArg[1]);
persicom7e2dfdd2002-04-18 02:46:52 +0000679 if( strncmp(azArg[1],"line",n2)==0
680 ||
681 strncmp(azArg[1],"lines",n2)==0 ){
drh75897232000-05-29 14:26:00 +0000682 p->mode = MODE_Line;
persicom7e2dfdd2002-04-18 02:46:52 +0000683 }else if( strncmp(azArg[1],"column",n2)==0
684 ||
685 strncmp(azArg[1],"columns",n2)==0 ){
drh75897232000-05-29 14:26:00 +0000686 p->mode = MODE_Column;
687 }else if( strncmp(azArg[1],"list",n2)==0 ){
688 p->mode = MODE_List;
drh1e5d0e92000-05-31 23:33:17 +0000689 }else if( strncmp(azArg[1],"html",n2)==0 ){
690 p->mode = MODE_Html;
drh28bd4bc2000-06-15 15:57:22 +0000691 }else if( strncmp(azArg[1],"insert",n2)==0 ){
692 p->mode = MODE_Insert;
693 if( nArg>=3 ){
drh33048c02001-10-01 14:29:22 +0000694 set_table_name(p, azArg[2]);
drh28bd4bc2000-06-15 15:57:22 +0000695 }else{
drh33048c02001-10-01 14:29:22 +0000696 set_table_name(p, "table");
drh28bd4bc2000-06-15 15:57:22 +0000697 }
drhdaffd0e2001-04-11 14:28:42 +0000698 }else {
699 fprintf(stderr,"mode should be on of: column html insert line list\n");
drh75897232000-05-29 14:26:00 +0000700 }
701 }else
702
persicom7e2dfdd2002-04-18 02:46:52 +0000703 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
704 sprintf(p->nullvalue, "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
705 }else
706
drh75897232000-05-29 14:26:00 +0000707 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
708 if( p->out!=stdout ){
709 fclose(p->out);
710 }
711 if( strcmp(azArg[1],"stdout")==0 ){
712 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +0000713 strcpy(p->outfile,"stdout");
drh75897232000-05-29 14:26:00 +0000714 }else{
715 p->out = fopen(azArg[1], "w");
716 if( p->out==0 ){
717 fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
718 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +0000719 } else {
720 strcpy(p->outfile,azArg[1]);
drh75897232000-05-29 14:26:00 +0000721 }
722 }
723 }else
724
persicom7e2dfdd2002-04-18 02:46:52 +0000725 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && nArg==2 || nArg==3){
726 if( nArg >= 2) {
727 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
728 }
729 if( nArg >= 3) {
730 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
731 }
732 }else
733
734 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
735 sqlite_close(db);
736 exit(0);
737 }else
738
drhdaffd0e2001-04-11 14:28:42 +0000739 if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
740 FILE *alt = fopen(azArg[1], "r");
741 if( alt==0 ){
742 fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
743 }else{
744 process_input(p, alt);
745 fclose(alt);
746 }
747 }else
748
749 if( c=='r' && strncmp(azArg[0], "reindex", n)==0 ){
750 char **azResult;
751 int nRow, rc;
752 char *zErrMsg;
753 int i;
754 char *zSql;
755 if( nArg==1 ){
756 rc = sqlite_get_table(db,
757 "SELECT name, sql FROM sqlite_master "
758 "WHERE type='index'",
759 &azResult, &nRow, 0, &zErrMsg
760 );
761 }else{
762 rc = sqlite_get_table_printf(db,
763 "SELECT name, sql FROM sqlite_master "
764 "WHERE type='index' AND tbl_name LIKE '%q'",
765 &azResult, &nRow, 0, &zErrMsg, azArg[1]
766 );
767 }
768 for(i=1; rc==SQLITE_OK && i<=nRow; i++){
769 extern char *sqlite_mprintf(const char *, ...);
770 zSql = sqlite_mprintf(
771 "DROP INDEX '%q';\n%s;\nVACUUM '%q';",
772 azResult[i*2], azResult[i*2+1], azResult[i*2]);
773 if( p->echoOn ) printf("%s\n", zSql);
774 rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg);
775 }
776 sqlite_free_table(azResult);
777 if( zErrMsg ){
778 fprintf(stderr,"Error: %s\n", zErrMsg);
779 free(zErrMsg);
780 }
781 }else
782
drh75897232000-05-29 14:26:00 +0000783 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
784 struct callback_data data;
785 char *zErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000786 memcpy(&data, p, sizeof(data));
787 data.showHeader = 0;
drhe3710332000-09-29 13:30:53 +0000788 data.mode = MODE_Semi;
drh75897232000-05-29 14:26:00 +0000789 if( nArg>1 ){
drhff9821a2001-04-04 21:22:14 +0000790 extern int sqliteStrICmp(const char*,const char*);
drha18c5682000-10-08 22:20:57 +0000791 if( sqliteStrICmp(azArg[1],"sqlite_master")==0 ){
792 char *new_argv[2], *new_colv[2];
793 new_argv[0] = "CREATE TABLE sqlite_master (\n"
794 " type text,\n"
795 " name text,\n"
796 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000797 " rootpage integer,\n"
drha18c5682000-10-08 22:20:57 +0000798 " sql text\n"
799 ")";
800 new_argv[1] = 0;
801 new_colv[0] = "sql";
802 new_colv[1] = 0;
803 callback(&data, 1, new_argv, new_colv);
804 }else{
805 sqlite_exec_printf(db,
806 "SELECT sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000807 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOTNULL "
drha18c5682000-10-08 22:20:57 +0000808 "ORDER BY type DESC, name",
809 callback, &data, &zErrMsg, azArg[1]);
810 }
drh75897232000-05-29 14:26:00 +0000811 }else{
drha18c5682000-10-08 22:20:57 +0000812 sqlite_exec(db,
813 "SELECT sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000814 "WHERE type!='meta' AND sql NOTNULL "
drha18c5682000-10-08 22:20:57 +0000815 "ORDER BY tbl_name, type DESC, name",
816 callback, &data, &zErrMsg
817 );
drh75897232000-05-29 14:26:00 +0000818 }
drh75897232000-05-29 14:26:00 +0000819 if( zErrMsg ){
820 fprintf(stderr,"Error: %s\n", zErrMsg);
821 free(zErrMsg);
822 }
823 }else
824
825 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
826 sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
827 }else
828
persicom7e2dfdd2002-04-18 02:46:52 +0000829 if( c=='s' && strncmp(azArg[0], "show", n)==0){
830 int i;
831 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
832 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" : "off");
833 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
834 fprintf(p->out,"%9.9s: %s\n","nullvalue", p->nullvalue);
835 fprintf(p->out,"%9.9s: %s\n","output", strlen(p->outfile) ? p->outfile : "stdout");
836 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') "
drha18c5682000-10-08 22:20:57 +0000852 "ORDER BY name",
853 &azResult, &nRow, 0, &zErrMsg
854 );
drha50da102000-08-08 20:19:09 +0000855 }else{
drha18c5682000-10-08 22:20:57 +0000856 rc = sqlite_get_table_printf(db,
drha50da102000-08-08 20:19:09 +0000857 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +0000858 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
drha18c5682000-10-08 22:20:57 +0000859 "ORDER BY name",
860 &azResult, &nRow, 0, &zErrMsg, azArg[1]
861 );
drha50da102000-08-08 20:19:09 +0000862 }
drh75897232000-05-29 14:26:00 +0000863 if( zErrMsg ){
864 fprintf(stderr,"Error: %s\n", zErrMsg);
865 free(zErrMsg);
866 }
drhe3710332000-09-29 13:30:53 +0000867 if( rc==SQLITE_OK ){
868 int len, maxlen = 0;
869 int i, j;
870 int nPrintCol, nPrintRow;
871 for(i=1; i<=nRow; i++){
872 if( azResult[i]==0 ) continue;
873 len = strlen(azResult[i]);
874 if( len>maxlen ) maxlen = len;
875 }
876 nPrintCol = 80/(maxlen+2);
877 if( nPrintCol<1 ) nPrintCol = 1;
878 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
879 for(i=0; i<nPrintRow; i++){
880 for(j=i+1; j<=nRow; j+=nPrintRow){
881 char *zSp = j<=nPrintRow ? "" : " ";
882 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
883 }
884 printf("\n");
885 }
886 }
887 sqlite_free_table(azResult);
drh75897232000-05-29 14:26:00 +0000888 }else
889
drh2dfbbca2000-07-28 14:32:48 +0000890 if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
891 sqlite_busy_timeout(db, atoi(azArg[1]));
892 }else
893
drh75897232000-05-29 14:26:00 +0000894 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
895 int j;
896 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
897 p->colWidth[j-1] = atoi(azArg[j]);
898 }
899 }else
900
901 {
persicom7e2dfdd2002-04-18 02:46:52 +0000902 fprintf(stderr, "unknown command or invalid arguments: \"%s\". Enter \".help\" for help\n",
drh75897232000-05-29 14:26:00 +0000903 azArg[0]);
904 }
905}
906
drhdaffd0e2001-04-11 14:28:42 +0000907static void process_input(struct callback_data *p, FILE *in){
908 char *zLine;
909 char *zSql = 0;
910 int nSql = 0;
911 char *zErrMsg;
drh41e941d2002-04-04 15:10:12 +0000912 while( fflush(p->out), (zLine = one_input_line(zSql, in))!=0 ){
drhdaffd0e2001-04-11 14:28:42 +0000913 if( p->echoOn ) printf("%s\n", zLine);
drh2af0b2d2002-02-21 02:25:02 +0000914 if( zLine && zLine[0]=='.' && nSql==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000915 do_meta_command(zLine, db, p);
916 free(zLine);
917 continue;
918 }
919 if( zSql==0 ){
920 int i;
921 for(i=0; zLine[i] && isspace(zLine[i]); i++){}
922 if( zLine[i]!=0 ){
923 nSql = strlen(zLine);
924 zSql = malloc( nSql+1 );
925 strcpy(zSql, zLine);
926 }
927 }else{
928 int len = strlen(zLine);
929 zSql = realloc( zSql, nSql + len + 2 );
930 if( zSql==0 ){
931 fprintf(stderr,"%s: out of memory!\n", Argv0);
932 exit(1);
933 }
934 strcpy(&zSql[nSql++], "\n");
935 strcpy(&zSql[nSql], zLine);
936 nSql += len;
937 }
938 free(zLine);
939 if( zSql && sqlite_complete(zSql) ){
940 p->cnt = 0;
941 if( sqlite_exec(db, zSql, callback, p, &zErrMsg)!=0
942 && zErrMsg!=0 ){
943 if( in!=0 && !p->echoOn ) printf("%s\n",zSql);
944 printf("SQL error: %s\n", zErrMsg);
945 free(zErrMsg);
946 zErrMsg = 0;
947 }
948 free(zSql);
949 zSql = 0;
950 nSql = 0;
951 }
952 }
953 if( zSql ){
954 printf("Incomplete SQL: %s\n", zSql);
955 free(zSql);
956 }
957}
958
persicom7e2dfdd2002-04-18 02:46:52 +0000959static void process_sqliterc(struct callback_data *p,
960 char *sqliterc_override){
961
962 char *home_dir = NULL;
963 char *sqliterc = sqliterc_override;
964 struct passwd *pwent;
965 uid_t uid = getuid();
966 FILE *in = NULL;
967
968 if (sqliterc == NULL) {
969 /* Figure out the user's home directory */
970 while( (pwent=getpwent()) != NULL) {
971 if(pwent->pw_uid == uid) {
972 home_dir = pwent->pw_dir;
973 break;
974 }
975 }
976
977 if (!home_dir) {
978 home_dir = getenv("HOME");
979 if (!home_dir) {
980 home_dir = getenv("HOMEPATH"); /* Windows? */
981 }
982 if (!home_dir) {
983 printf("Cannot find home directory from which to load .sqliterc\n");
984 return;
985 }
986 }
987
988 /* With the home directory, open the init file and process */
989 sqliterc = (char *)calloc(strlen(home_dir) +
990 strlen("/.sqliterc") +
991 1,
992 sizeof(char));
993 if( sqliterc==0 ){
994 fprintf(stderr,"%s: out of memory!\n", Argv0);
995 exit(1);
996 }
997 sprintf(sqliterc,"%s/.sqliterc",home_dir);
998 }
999 in = fopen(sqliterc,"r");
1000 if(!in) {
1001 /* File either had an error or was not found. Since I cannot
1002 * tell, I cannot bark. */
1003 return;
1004 } else {
1005 printf("Loading resources from %s\n",sqliterc);
1006 process_input(p,in);
1007 close (in);
1008 }
1009 return;
1010}
1011
1012void main_init(struct callback_data *data) {
1013 memset(data, 0, sizeof(*data));
1014 data->mode = MODE_List;
1015 strcpy(data->separator,"|");
1016 data->showHeader = 0;
1017 strcpy(mainPrompt,"sqlite> ");
1018 strcpy(continuePrompt," ...> ");
1019}
1020
drh75897232000-05-29 14:26:00 +00001021int main(int argc, char **argv){
drh75897232000-05-29 14:26:00 +00001022 char *zErrMsg = 0;
1023 struct callback_data data;
persicom7e2dfdd2002-04-18 02:46:52 +00001024 int origArgc = argc;
1025 char **origArgv = argv;
drh75897232000-05-29 14:26:00 +00001026
drhdaffd0e2001-04-11 14:28:42 +00001027 Argv0 = argv[0];
persicom7e2dfdd2002-04-18 02:46:52 +00001028 main_init(&data);
1029 process_sqliterc(&data,NULL);
1030
drh4c504392000-10-16 22:06:40 +00001031#ifdef SIGINT
1032 signal(SIGINT, interrupt_handler);
1033#endif
drh1e5d0e92000-05-31 23:33:17 +00001034 while( argc>=2 && argv[1][0]=='-' ){
persicom7e2dfdd2002-04-18 02:46:52 +00001035 if( argc>=3 && strcmp(argv[1],"-init")==0 ){
1036 /* If we get a -init to do, we have to pretend that
1037 ** it replaced the .sqliterc file. Soooo, in order to
1038 ** do that we need to start from scratch...*/
1039 main_init(&data);
1040
1041 /* treat this file as the sqliterc... */
1042 process_sqliterc(&data,argv[2]);
1043
1044 /* fix up the command line so we do not re-read
1045 ** the option next time around... */
1046 {
1047 int i = 1;
1048 for(i=1;i<=argc-2;i++) {
1049 argv[i] = argv[i+2];
1050 }
1051 }
1052 origArgc-=2;
1053
1054 /* and reset the command line options to be re-read.*/
1055 argv = origArgv;
1056 argc = origArgc;
1057
1058 }else if( strcmp(argv[1],"-html")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001059 data.mode = MODE_Html;
1060 argc--;
1061 argv++;
1062 }else if( strcmp(argv[1],"-list")==0 ){
1063 data.mode = MODE_List;
1064 argc--;
1065 argv++;
1066 }else if( strcmp(argv[1],"-line")==0 ){
1067 data.mode = MODE_Line;
1068 argc--;
1069 argv++;
drh8b32e172002-04-08 02:42:57 +00001070 }else if( strcmp(argv[1],"-column")==0 ){
1071 data.mode = MODE_Column;
1072 argc--;
1073 argv++;
drh7613bfa2002-01-22 12:39:24 +00001074 }else if( argc>=3 && strcmp(argv[1],"-separator")==0 ){
drhbed86902000-06-02 13:27:59 +00001075 sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[2]);
drh1e5d0e92000-05-31 23:33:17 +00001076 argc -= 2;
1077 argv += 2;
persicom7e2dfdd2002-04-18 02:46:52 +00001078 }else if( argc>=3 && strcmp(argv[1],"-nullvalue")==0 ){
1079 sprintf(data.nullvalue,"%.*s",(int)sizeof(data.nullvalue)-1,argv[2]);
1080 argc -= 2;
1081 argv += 2;
drh1e5d0e92000-05-31 23:33:17 +00001082 }else if( strcmp(argv[1],"-header")==0 ){
1083 data.showHeader = 1;
1084 argc--;
1085 argv++;
1086 }else if( strcmp(argv[1],"-noheader")==0 ){
1087 data.showHeader = 0;
1088 argc--;
1089 argv++;
drh660f68d2001-01-04 14:27:07 +00001090 }else if( strcmp(argv[1],"-echo")==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001091 data.echoOn = 1;
drh660f68d2001-01-04 14:27:07 +00001092 argc--;
1093 argv++;
drh1e5d0e92000-05-31 23:33:17 +00001094 }else{
drhdaffd0e2001-04-11 14:28:42 +00001095 fprintf(stderr,"%s: unknown option: %s\n", Argv0, argv[1]);
drh1e5d0e92000-05-31 23:33:17 +00001096 return 1;
1097 }
1098 }
drh75897232000-05-29 14:26:00 +00001099 if( argc!=2 && argc!=3 ){
drhdaffd0e2001-04-11 14:28:42 +00001100 fprintf(stderr,"Usage: %s ?OPTIONS? FILENAME ?SQL?\n", Argv0);
drh75897232000-05-29 14:26:00 +00001101 exit(1);
1102 }
drh4c653a02000-06-07 01:27:47 +00001103 data.db = db = sqlite_open(argv[1], 0666, &zErrMsg);
drh75897232000-05-29 14:26:00 +00001104 if( db==0 ){
drh167a4b12000-08-17 09:49:59 +00001105 data.db = db = sqlite_open(argv[1], 0444, &zErrMsg);
1106 if( db==0 ){
1107 if( zErrMsg ){
1108 fprintf(stderr,"Unable to open database \"%s\": %s\n", argv[1],zErrMsg);
1109 }else{
1110 fprintf(stderr,"Unable to open database %s\n", argv[1]);
1111 }
1112 exit(1);
drhd1dedb82000-06-05 02:07:04 +00001113 }else{
drh80afdca2000-08-22 13:27:22 +00001114 fprintf(stderr,"Database \"%s\" opened READ ONLY!\n", argv[1]);
drhd1dedb82000-06-05 02:07:04 +00001115 }
drh75897232000-05-29 14:26:00 +00001116 }
drh75897232000-05-29 14:26:00 +00001117 data.out = stdout;
1118 if( argc==3 ){
drh6ff13852001-11-25 13:18:23 +00001119 if( argv[2][0]=='.' ){
1120 do_meta_command(argv[2], db, &data);
1121 exit(0);
1122 }else{
1123 int rc;
1124 rc = sqlite_exec(db, argv[2], callback, &data, &zErrMsg);
1125 if( rc!=0 && zErrMsg!=0 ){
1126 fprintf(stderr,"SQL error: %s\n", zErrMsg);
1127 exit(1);
1128 }
drh75897232000-05-29 14:26:00 +00001129 }
1130 }else{
drh382c0242001-10-06 16:33:02 +00001131 extern int isatty();
drhdaffd0e2001-04-11 14:28:42 +00001132 if( isatty(0) ){
drh75897232000-05-29 14:26:00 +00001133 printf(
drhb217a572000-08-22 13:40:18 +00001134 "SQLite version %s\n"
1135 "Enter \".help\" for instructions\n",
1136 sqlite_version
drh75897232000-05-29 14:26:00 +00001137 );
drhdaffd0e2001-04-11 14:28:42 +00001138 process_input(&data, 0);
1139 }else{
1140 process_input(&data, stdin);
drh75897232000-05-29 14:26:00 +00001141 }
1142 }
drh33048c02001-10-01 14:29:22 +00001143 set_table_name(&data, 0);
drh75897232000-05-29 14:26:00 +00001144 sqlite_close(db);
1145 return 0;
1146}