blob: 03a8081a7986db7b381749757803170229fc60a9 [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**
drhfeac5f82004-08-01 00:10:45 +000015** $Id: shell.c,v 1.108 2004/08/01 00:10:45 drh Exp $
drh75897232000-05-29 14:26:00 +000016*/
17#include <stdlib.h>
18#include <string.h>
19#include <stdio.h>
danielk19772a02e332004-06-05 08:04:36 +000020#include <assert.h>
drh1d482dd2004-05-31 18:23:07 +000021#include "sqlite3.h"
drh75897232000-05-29 14:26:00 +000022#include <ctype.h>
persicom7e2dfdd2002-04-18 02:46:52 +000023
drh820f3812003-01-08 13:02:52 +000024#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
drh4c504392000-10-16 22:06:40 +000025# include <signal.h>
drhdd45df82002-04-18 12:39:03 +000026# include <pwd.h>
27# include <unistd.h>
28# include <sys/types.h>
drh4c504392000-10-16 22:06:40 +000029#endif
drh75897232000-05-29 14:26:00 +000030
drh820f3812003-01-08 13:02:52 +000031#ifdef __MACOS__
32# include <console.h>
33# include <signal.h>
34# include <unistd.h>
35# include <extras.h>
36# include <Files.h>
37# include <Folders.h>
38#endif
39
drh16e59552000-07-31 11:57:37 +000040#if defined(HAVE_READLINE) && HAVE_READLINE==1
drh8e7e7a22000-05-30 18:45:23 +000041# include <readline/readline.h>
42# include <readline/history.h>
43#else
drh9347b202003-07-18 01:30:59 +000044# define readline(p) local_getline(p,stdin)
persicom1d0b8722002-04-18 02:53:04 +000045# define add_history(X)
drh67505e72002-04-19 12:34:06 +000046# define read_history(X)
47# define write_history(X)
48# define stifle_history(X)
drh75897232000-05-29 14:26:00 +000049#endif
50
drh4328c8b2003-04-26 02:50:11 +000051/* Make sure isatty() has a prototype.
52*/
53extern int isatty();
54
drh75897232000-05-29 14:26:00 +000055/*
drh4c504392000-10-16 22:06:40 +000056** The following is the open SQLite database. We make a pointer
57** to this database a static variable so that it can be accessed
58** by the SIGINT handler to interrupt database processing.
59*/
danielk197792f9a1b2004-06-19 09:08:16 +000060static sqlite3 *db = 0;
drh4c504392000-10-16 22:06:40 +000061
62/*
drh67505e72002-04-19 12:34:06 +000063** True if an interrupt (Control-C) has been received.
64*/
65static int seenInterrupt = 0;
66
67/*
persicom7e2dfdd2002-04-18 02:46:52 +000068** This is the name of our program. It is set in main(), used
69** in a number of other places, mostly for error messages.
70*/
71static char *Argv0;
72
73/*
74** Prompt strings. Initialized in main. Settable with
75** .prompt main continue
76*/
77static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
78static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
79
drh44c2eb12003-04-30 11:38:26 +000080
persicom7e2dfdd2002-04-18 02:46:52 +000081/*
drh83965662003-04-17 02:54:13 +000082** Determines if a string is a number of not.
83*/
danielk19778a6b5412004-05-24 07:04:25 +000084extern int sqlite3IsNumber(const char*, int*, unsigned char);
drh83965662003-04-17 02:54:13 +000085
86/*
danielk1977bc6ada42004-06-30 08:20:16 +000087** A global char* and an SQL function to access its current value
88** from within an SQL statement. This program used to use the
89** sqlite_exec_printf() API to substitue a string into an SQL statement.
90** The correct way to do this with sqlite3 is to use the bind API, but
91** since the shell is built around the callback paradigm it would be a lot
92** of work. Instead just use this hack, which is quite harmless.
93*/
94static const char *zShellStatic = 0;
95static void shellstaticFunc(
96 sqlite3_context *context,
97 int argc,
98 sqlite3_value **argv
99){
100 assert( 0==argc );
101 assert( zShellStatic );
102 sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC);
103}
104
105
106/*
drhfeac5f82004-08-01 00:10:45 +0000107** This routine reads a line of text from FILE in, stores
drh8e7e7a22000-05-30 18:45:23 +0000108** the text in memory obtained from malloc() and returns a pointer
109** to the text. NULL is returned at end of file, or if malloc()
110** fails.
111**
112** The interface is like "readline" but no command-line editing
113** is done.
114*/
drh9347b202003-07-18 01:30:59 +0000115static char *local_getline(char *zPrompt, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +0000116 char *zLine;
117 int nLine;
drh8e7e7a22000-05-30 18:45:23 +0000118 int n;
119 int eol;
120
121 if( zPrompt && *zPrompt ){
122 printf("%s",zPrompt);
123 fflush(stdout);
124 }
125 nLine = 100;
126 zLine = malloc( nLine );
127 if( zLine==0 ) return 0;
128 n = 0;
129 eol = 0;
130 while( !eol ){
131 if( n+100>nLine ){
132 nLine = nLine*2 + 100;
133 zLine = realloc(zLine, nLine);
134 if( zLine==0 ) return 0;
135 }
drhdaffd0e2001-04-11 14:28:42 +0000136 if( fgets(&zLine[n], nLine - n, in)==0 ){
drh8e7e7a22000-05-30 18:45:23 +0000137 if( n==0 ){
138 free(zLine);
139 return 0;
140 }
141 zLine[n] = 0;
142 eol = 1;
143 break;
144 }
145 while( zLine[n] ){ n++; }
146 if( n>0 && zLine[n-1]=='\n' ){
147 n--;
148 zLine[n] = 0;
149 eol = 1;
150 }
151 }
152 zLine = realloc( zLine, n+1 );
153 return zLine;
154}
155
156/*
157** Retrieve a single line of input text. "isatty" is true if text
158** is coming from a terminal. In that case, we issue a prompt and
159** attempt to use "readline" for command-line editing. If "isatty"
drh9347b202003-07-18 01:30:59 +0000160** is false, use "local_getline" instead of "readline" and issue no prompt.
drh8e7e7a22000-05-30 18:45:23 +0000161**
162** zPrior is a string of prior text retrieved. If not the empty
163** string, then issue a continuation prompt.
164*/
drhdaffd0e2001-04-11 14:28:42 +0000165static char *one_input_line(const char *zPrior, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +0000166 char *zPrompt;
167 char *zResult;
drhdaffd0e2001-04-11 14:28:42 +0000168 if( in!=0 ){
drh9347b202003-07-18 01:30:59 +0000169 return local_getline(0, in);
drh8e7e7a22000-05-30 18:45:23 +0000170 }
171 if( zPrior && zPrior[0] ){
persicom7e2dfdd2002-04-18 02:46:52 +0000172 zPrompt = continuePrompt;
drh8e7e7a22000-05-30 18:45:23 +0000173 }else{
persicom7e2dfdd2002-04-18 02:46:52 +0000174 zPrompt = mainPrompt;
drh8e7e7a22000-05-30 18:45:23 +0000175 }
176 zResult = readline(zPrompt);
drh2dfbbca2000-07-28 14:32:48 +0000177 if( zResult ) add_history(zResult);
drh8e7e7a22000-05-30 18:45:23 +0000178 return zResult;
179}
180
persicom7e2dfdd2002-04-18 02:46:52 +0000181struct previous_mode_data {
182 int valid; /* Is there legit data in here? */
183 int mode;
184 int showHeader;
185 int colWidth[100];
186};
drh8e7e7a22000-05-30 18:45:23 +0000187/*
drh75897232000-05-29 14:26:00 +0000188** An pointer to an instance of this structure is passed from
189** the main program to the callback. This is used to communicate
190** state and mode information.
191*/
192struct callback_data {
danielk197792f9a1b2004-06-19 09:08:16 +0000193 sqlite3 *db; /* The database */
drhdaffd0e2001-04-11 14:28:42 +0000194 int echoOn; /* True to echo input commands */
drh28bd4bc2000-06-15 15:57:22 +0000195 int cnt; /* Number of records displayed so far */
196 FILE *out; /* Write results here */
197 int mode; /* An output mode setting */
198 int showHeader; /* True to show column names in List or Column mode */
drh33048c02001-10-01 14:29:22 +0000199 char *zDestTable; /* Name of destination table when MODE_Insert */
drh28bd4bc2000-06-15 15:57:22 +0000200 char separator[20]; /* Separator character for MODE_List */
drha0c66f52000-07-29 13:20:21 +0000201 int colWidth[100]; /* Requested width of each column when in column mode*/
202 int actualWidth[100]; /* Actual width of each column */
drh83965662003-04-17 02:54:13 +0000203 char nullvalue[20]; /* The text to print when a NULL comes back from
204 ** the database */
persicom7e2dfdd2002-04-18 02:46:52 +0000205 struct previous_mode_data explainPrev;
drh83965662003-04-17 02:54:13 +0000206 /* Holds the mode information just before
207 ** .explain ON */
drh44c2eb12003-04-30 11:38:26 +0000208 char outfile[FILENAME_MAX]; /* Filename for *out */
209 const char *zDbFilename; /* name of the database file */
drh22fbcb82004-02-01 01:22:50 +0000210 char *zKey; /* Encryption key */
drh75897232000-05-29 14:26:00 +0000211};
212
213/*
214** These are the allowed modes.
215*/
drh967e8b72000-06-21 13:59:10 +0000216#define MODE_Line 0 /* One column per line. Blank line between records */
drh75897232000-05-29 14:26:00 +0000217#define MODE_Column 1 /* One record per line in neat columns */
218#define MODE_List 2 /* One record per line with a separator */
drhe3710332000-09-29 13:30:53 +0000219#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
220#define MODE_Html 4 /* Generate an XHTML table */
221#define MODE_Insert 5 /* Generate SQL "insert" statements */
drhfeac5f82004-08-01 00:10:45 +0000222#define MODE_Tcl 6 /* Generate ANSI-C or TCL quoted elements */
223#define MODE_NUM_OF 7 /* The number of modes (not a mode itself) */
persicom7e2dfdd2002-04-18 02:46:52 +0000224
225char *modeDescr[MODE_NUM_OF] = {
226 "line",
227 "column",
228 "list",
229 "semi",
230 "html",
drhfeac5f82004-08-01 00:10:45 +0000231 "insert",
232 "tcl",
persicom7e2dfdd2002-04-18 02:46:52 +0000233};
drh75897232000-05-29 14:26:00 +0000234
235/*
236** Number of elements in an array
237*/
238#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
239
240/*
drh28bd4bc2000-06-15 15:57:22 +0000241** Output the given string as a quoted string using SQL quoting conventions.
242*/
243static void output_quoted_string(FILE *out, const char *z){
244 int i;
245 int nSingle = 0;
drh28bd4bc2000-06-15 15:57:22 +0000246 for(i=0; z[i]; i++){
247 if( z[i]=='\'' ) nSingle++;
drh28bd4bc2000-06-15 15:57:22 +0000248 }
249 if( nSingle==0 ){
250 fprintf(out,"'%s'",z);
drh28bd4bc2000-06-15 15:57:22 +0000251 }else{
252 fprintf(out,"'");
253 while( *z ){
254 for(i=0; z[i] && z[i]!='\''; i++){}
255 if( i==0 ){
256 fprintf(out,"''");
257 z++;
258 }else if( z[i]=='\'' ){
259 fprintf(out,"%.*s''",i,z);
260 z += i+1;
261 }else{
drhcd7d2732002-02-26 23:24:26 +0000262 fprintf(out,"%s",z);
drh28bd4bc2000-06-15 15:57:22 +0000263 break;
264 }
265 }
drhcd7d2732002-02-26 23:24:26 +0000266 fprintf(out,"'");
drh28bd4bc2000-06-15 15:57:22 +0000267 }
268}
269
270/*
drhfeac5f82004-08-01 00:10:45 +0000271** Output the given string as a quoted according to C or TCL quoting rules.
272*/
273static void output_c_string(FILE *out, const char *z){
274 unsigned int c;
275 fputc('"', out);
276 while( (c = *(z++))!=0 ){
277 if( c=='\\' ){
278 fputc(c, out);
279 fputc(c, out);
280 }else if( c=='\t' ){
281 fputc('\\', out);
282 fputc('t', out);
283 }else if( c=='\n' ){
284 fputc('\\', out);
285 fputc('n', out);
286 }else if( c=='\r' ){
287 fputc('\\', out);
288 fputc('r', out);
289 }else if( !isprint(c) ){
290 fprintf(out, "\\%03o", c);
291 }else{
292 fputc(c, out);
293 }
294 }
295 fputc('"', out);
296}
297
298/*
drhc08a4f12000-06-15 16:49:48 +0000299** Output the given string with characters that are special to
300** HTML escaped.
301*/
302static void output_html_string(FILE *out, const char *z){
303 int i;
304 while( *z ){
305 for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){}
306 if( i>0 ){
307 fprintf(out,"%.*s",i,z);
308 }
309 if( z[i]=='<' ){
310 fprintf(out,"&lt;");
311 }else if( z[i]=='&' ){
312 fprintf(out,"&amp;");
313 }else{
314 break;
315 }
316 z += i + 1;
317 }
318}
319
320/*
drh4c504392000-10-16 22:06:40 +0000321** This routine runs when the user presses Ctrl-C
322*/
323static void interrupt_handler(int NotUsed){
drh67505e72002-04-19 12:34:06 +0000324 seenInterrupt = 1;
danielk19776f8a5032004-05-10 10:34:51 +0000325 if( db ) sqlite3_interrupt(db);
drh4c504392000-10-16 22:06:40 +0000326}
327
328/*
drh75897232000-05-29 14:26:00 +0000329** This is the callback routine that the SQLite library
330** invokes for each row of a query result.
331*/
332static int callback(void *pArg, int nArg, char **azArg, char **azCol){
333 int i;
334 struct callback_data *p = (struct callback_data*)pArg;
335 switch( p->mode ){
336 case MODE_Line: {
drhe3710332000-09-29 13:30:53 +0000337 int w = 5;
drh6a535342001-10-19 16:44:56 +0000338 if( azArg==0 ) break;
drhe3710332000-09-29 13:30:53 +0000339 for(i=0; i<nArg; i++){
340 int len = strlen(azCol[i]);
341 if( len>w ) w = len;
342 }
drh75897232000-05-29 14:26:00 +0000343 if( p->cnt++>0 ) fprintf(p->out,"\n");
344 for(i=0; i<nArg; i++){
drha69d9162003-04-17 22:57:53 +0000345 fprintf(p->out,"%*s = %s\n", w, azCol[i],
346 azArg[i] ? azArg[i] : p->nullvalue);
drh75897232000-05-29 14:26:00 +0000347 }
348 break;
349 }
350 case MODE_Column: {
drha0c66f52000-07-29 13:20:21 +0000351 if( p->cnt++==0 ){
drh75897232000-05-29 14:26:00 +0000352 for(i=0; i<nArg; i++){
drha0c66f52000-07-29 13:20:21 +0000353 int w, n;
354 if( i<ArraySize(p->colWidth) ){
drh75897232000-05-29 14:26:00 +0000355 w = p->colWidth[i];
356 }else{
drha0c66f52000-07-29 13:20:21 +0000357 w = 0;
drh75897232000-05-29 14:26:00 +0000358 }
drha0c66f52000-07-29 13:20:21 +0000359 if( w<=0 ){
drhff6e9112000-08-28 16:21:58 +0000360 w = strlen(azCol[i] ? azCol[i] : "");
drha0c66f52000-07-29 13:20:21 +0000361 if( w<10 ) w = 10;
persicom7e2dfdd2002-04-18 02:46:52 +0000362 n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue);
drha0c66f52000-07-29 13:20:21 +0000363 if( w<n ) w = n;
364 }
365 if( i<ArraySize(p->actualWidth) ){
persicom1d0b8722002-04-18 02:53:04 +0000366 p->actualWidth[i] = w;
drha0c66f52000-07-29 13:20:21 +0000367 }
368 if( p->showHeader ){
369 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
370 }
371 }
372 if( p->showHeader ){
373 for(i=0; i<nArg; i++){
374 int w;
375 if( i<ArraySize(p->actualWidth) ){
376 w = p->actualWidth[i];
377 }else{
378 w = 10;
379 }
380 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
381 "----------------------------------------------------------",
382 i==nArg-1 ? "\n": " ");
383 }
drh75897232000-05-29 14:26:00 +0000384 }
385 }
drh6a535342001-10-19 16:44:56 +0000386 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000387 for(i=0; i<nArg; i++){
388 int w;
drha0c66f52000-07-29 13:20:21 +0000389 if( i<ArraySize(p->actualWidth) ){
390 w = p->actualWidth[i];
drh75897232000-05-29 14:26:00 +0000391 }else{
392 w = 10;
393 }
drhc61053b2000-06-04 12:58:36 +0000394 fprintf(p->out,"%-*.*s%s",w,w,
persicom7e2dfdd2002-04-18 02:46:52 +0000395 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
drh75897232000-05-29 14:26:00 +0000396 }
397 break;
398 }
drhe3710332000-09-29 13:30:53 +0000399 case MODE_Semi:
drh75897232000-05-29 14:26:00 +0000400 case MODE_List: {
401 if( p->cnt++==0 && p->showHeader ){
402 for(i=0; i<nArg; i++){
403 fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
404 }
405 }
drh6a535342001-10-19 16:44:56 +0000406 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000407 for(i=0; i<nArg; i++){
drh4c653a02000-06-07 01:27:47 +0000408 char *z = azArg[i];
persicom7e2dfdd2002-04-18 02:46:52 +0000409 if( z==0 ) z = p->nullvalue;
drh71172c52002-01-24 00:00:21 +0000410 fprintf(p->out, "%s", z);
drhe3710332000-09-29 13:30:53 +0000411 if( i<nArg-1 ){
412 fprintf(p->out, "%s", p->separator);
413 }else if( p->mode==MODE_Semi ){
414 fprintf(p->out, ";\n");
415 }else{
416 fprintf(p->out, "\n");
417 }
drh75897232000-05-29 14:26:00 +0000418 }
419 break;
420 }
drh1e5d0e92000-05-31 23:33:17 +0000421 case MODE_Html: {
422 if( p->cnt++==0 && p->showHeader ){
423 fprintf(p->out,"<TR>");
424 for(i=0; i<nArg; i++){
425 fprintf(p->out,"<TH>%s</TH>",azCol[i]);
426 }
427 fprintf(p->out,"</TR>\n");
428 }
drh6a535342001-10-19 16:44:56 +0000429 if( azArg==0 ) break;
drh28bd4bc2000-06-15 15:57:22 +0000430 fprintf(p->out,"<TR>");
drh1e5d0e92000-05-31 23:33:17 +0000431 for(i=0; i<nArg; i++){
drhc08a4f12000-06-15 16:49:48 +0000432 fprintf(p->out,"<TD>");
persicom7e2dfdd2002-04-18 02:46:52 +0000433 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
drhc08a4f12000-06-15 16:49:48 +0000434 fprintf(p->out,"</TD>\n");
drh1e5d0e92000-05-31 23:33:17 +0000435 }
drh7d686b22002-11-11 13:56:47 +0000436 fprintf(p->out,"</TR>\n");
drh1e5d0e92000-05-31 23:33:17 +0000437 break;
438 }
drhfeac5f82004-08-01 00:10:45 +0000439 case MODE_Tcl: {
440 if( p->cnt++==0 && p->showHeader ){
441 for(i=0; i<nArg; i++){
442 output_c_string(p->out,azCol[i]);
443 fprintf(p->out, "%s", p->separator);
444 }
445 fprintf(p->out,"\n");
446 }
447 if( azArg==0 ) break;
448 for(i=0; i<nArg; i++){
449 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
450 fprintf(p->out, "%s", p->separator);
451 }
452 fprintf(p->out,"\n");
453 break;
454 }
drh28bd4bc2000-06-15 15:57:22 +0000455 case MODE_Insert: {
drh6a535342001-10-19 16:44:56 +0000456 if( azArg==0 ) break;
drh33048c02001-10-01 14:29:22 +0000457 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
drh28bd4bc2000-06-15 15:57:22 +0000458 for(i=0; i<nArg; i++){
459 char *zSep = i>0 ? ",": "";
460 if( azArg[i]==0 ){
461 fprintf(p->out,"%sNULL",zSep);
danielk19778a6b5412004-05-24 07:04:25 +0000462 }else if( sqlite3IsNumber(azArg[i], 0, 1) ){
drh28bd4bc2000-06-15 15:57:22 +0000463 fprintf(p->out,"%s%s",zSep, azArg[i]);
464 }else{
465 if( zSep[0] ) fprintf(p->out,"%s",zSep);
466 output_quoted_string(p->out, azArg[i]);
467 }
468 }
469 fprintf(p->out,");\n");
drh6a535342001-10-19 16:44:56 +0000470 break;
drh28bd4bc2000-06-15 15:57:22 +0000471 }
persicom1d0b8722002-04-18 02:53:04 +0000472 }
drh75897232000-05-29 14:26:00 +0000473 return 0;
474}
475
476/*
drh33048c02001-10-01 14:29:22 +0000477** Set the destination table field of the callback_data structure to
478** the name of the table given. Escape any quote characters in the
479** table name.
480*/
481static void set_table_name(struct callback_data *p, const char *zName){
482 int i, n;
483 int needQuote;
484 char *z;
485
486 if( p->zDestTable ){
487 free(p->zDestTable);
488 p->zDestTable = 0;
489 }
490 if( zName==0 ) return;
491 needQuote = !isalpha(*zName) && *zName!='_';
492 for(i=n=0; zName[i]; i++, n++){
493 if( !isalnum(zName[i]) && zName[i]!='_' ){
494 needQuote = 1;
495 if( zName[i]=='\'' ) n++;
496 }
497 }
498 if( needQuote ) n += 2;
499 z = p->zDestTable = malloc( n+1 );
500 if( z==0 ){
501 fprintf(stderr,"Out of memory!\n");
502 exit(1);
503 }
504 n = 0;
505 if( needQuote ) z[n++] = '\'';
506 for(i=0; zName[i]; i++){
507 z[n++] = zName[i];
508 if( zName[i]=='\'' ) z[n++] = '\'';
509 }
510 if( needQuote ) z[n++] = '\'';
511 z[n] = 0;
512}
513
danielk19772a02e332004-06-05 08:04:36 +0000514/* zIn is either a pointer to a NULL-terminated string in memory obtained
515** from malloc(), or a NULL pointer. The string pointed to by zAppend is
516** added to zIn, and the result returned in memory obtained from malloc().
517** zIn, if it was not NULL, is freed.
518**
519** If the third argument, quote, is not '\0', then it is used as a
520** quote character for zAppend.
521*/
522static char * appendText(char *zIn, char const *zAppend, char quote){
523 int len;
524 int i;
525 int nAppend = strlen(zAppend);
526 int nIn = (zIn?strlen(zIn):0);
527
528 len = nAppend+nIn+1;
529 if( quote ){
530 len += 2;
531 for(i=0; i<nAppend; i++){
532 if( zAppend[i]==quote ) len++;
533 }
534 }
535
536 zIn = (char *)realloc(zIn, len);
537 if( !zIn ){
538 return 0;
539 }
540
541 if( quote ){
542 char *zCsr = &zIn[nIn];
543 *zCsr++ = quote;
544 for(i=0; i<nAppend; i++){
545 *zCsr++ = zAppend[i];
546 if( zAppend[i]==quote ) *zCsr++ = quote;
547 }
548 *zCsr++ = quote;
549 *zCsr++ = '\0';
550 assert( (zCsr-zIn)==len );
551 }else{
552 memcpy(&zIn[nIn], zAppend, nAppend);
553 zIn[len-1] = '\0';
554 }
555
556 return zIn;
557}
558
drh33048c02001-10-01 14:29:22 +0000559/*
drh4c653a02000-06-07 01:27:47 +0000560** This is a different callback routine used for dumping the database.
561** Each row received by this callback consists of a table name,
562** the table type ("index" or "table") and SQL to create the table.
563** This routine should print text sufficient to recreate the table.
564*/
565static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
danielk19772a02e332004-06-05 08:04:36 +0000566 int rc;
567 const char *zTable;
568 const char *zType;
569 const char *zSql;
drhdaffd0e2001-04-11 14:28:42 +0000570 struct callback_data *p = (struct callback_data *)pArg;
danielk19772a02e332004-06-05 08:04:36 +0000571
drh4c653a02000-06-07 01:27:47 +0000572 if( nArg!=3 ) return 1;
danielk19772a02e332004-06-05 08:04:36 +0000573 zTable = azArg[0];
574 zType = azArg[1];
575 zSql = azArg[2];
576
577 fprintf(p->out, "%s;\n", zSql);
578
579 if( strcmp(zType, "table")==0 ){
580 sqlite3_stmt *pTableInfo = 0;
581 sqlite3_stmt *pSelect = 0;
582 char *zSelect = 0;
583 char *zTableInfo = 0;
584 char *zTmp = 0;
585
586 zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
587 zTableInfo = appendText(zTableInfo, zTable, '"');
588 zTableInfo = appendText(zTableInfo, ");", 0);
589
590 rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0);
591 if( zTableInfo ) free(zTableInfo);
592 if( rc!=SQLITE_OK || !pTableInfo ){
593 return 1;
594 }
595
596 zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
597 zTmp = appendText(zTmp, zTable, '"');
598 if( zTmp ){
599 zSelect = appendText(zSelect, zTmp, '\'');
600 }
601 zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
602 rc = sqlite3_step(pTableInfo);
603 while( rc==SQLITE_ROW ){
danielk19773f41e972004-06-08 00:39:01 +0000604 zSelect = appendText(zSelect, "quote(", 0);
danielk19772a02e332004-06-05 08:04:36 +0000605 zSelect = appendText(zSelect, sqlite3_column_text(pTableInfo, 1), '"');
606 rc = sqlite3_step(pTableInfo);
607 if( rc==SQLITE_ROW ){
608 zSelect = appendText(zSelect, ") || ', ' || ", 0);
609 }else{
610 zSelect = appendText(zSelect, ") ", 0);
611 }
612 }
613 rc = sqlite3_finalize(pTableInfo);
614 if( rc!=SQLITE_OK ){
615 if( zSelect ) free(zSelect);
616 return 1;
617 }
618 zSelect = appendText(zSelect, "|| ')' FROM ", 0);
619 zSelect = appendText(zSelect, zTable, '"');
620
621 rc = sqlite3_prepare(p->db, zSelect, -1, &pSelect, 0);
622 if( zSelect ) free(zSelect);
623 if( rc!=SQLITE_OK || !pSelect ){
624 return 1;
625 }
626
627 rc = sqlite3_step(pSelect);
628 while( rc==SQLITE_ROW ){
629 fprintf(p->out, "%s;\n", sqlite3_column_text(pSelect, 0));
630 rc = sqlite3_step(pSelect);
631 }
632 rc = sqlite3_finalize(pSelect);
633 if( rc!=SQLITE_OK ){
634 return 1;
635 }
drh4c653a02000-06-07 01:27:47 +0000636 }
danielk19772a02e332004-06-05 08:04:36 +0000637
drh4c653a02000-06-07 01:27:47 +0000638 return 0;
639}
640
641/*
drh75897232000-05-29 14:26:00 +0000642** Text of a help message
643*/
persicom1d0b8722002-04-18 02:53:04 +0000644static char zHelp[] =
jplyon6a65bb32003-05-04 07:25:57 +0000645 ".databases List names and files of attached databases\n"
646 ".dump ?TABLE? ... Dump the database in a text format\n"
drhdaffd0e2001-04-11 14:28:42 +0000647 ".echo ON|OFF Turn command echo on or off\n"
drh75897232000-05-29 14:26:00 +0000648 ".exit Exit this program\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000649 ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000650 ".header(s) ON|OFF Turn display of headers on or off\n"
drh75897232000-05-29 14:26:00 +0000651 ".help Show this message\n"
drhfeac5f82004-08-01 00:10:45 +0000652 ".import FILE TABLE Import data from FILE\n"
drh75897232000-05-29 14:26:00 +0000653 ".indices TABLE Show names of all indices on TABLE\n"
drhfeac5f82004-08-01 00:10:45 +0000654 ".mode MODE Set mode to one of: cvs column html insert line\n"
655 " list tabs tcl\n"
drhc08a4f12000-06-15 16:49:48 +0000656 ".mode insert TABLE Generate SQL insert statements for TABLE\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000657 ".nullvalue STRING Print STRING instead of nothing for NULL data\n"
drh75897232000-05-29 14:26:00 +0000658 ".output FILENAME Send output to FILENAME\n"
659 ".output stdout Send output to the screen\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000660 ".prompt MAIN CONTINUE Replace the standard prompts\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000661 ".quit Exit this program\n"
drhdaffd0e2001-04-11 14:28:42 +0000662 ".read FILENAME Execute SQL in FILENAME\n"
drh9eb9e262004-02-11 02:18:05 +0000663#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000664 ".rekey OLD NEW NEW Change the encryption key\n"
665#endif
drh75897232000-05-29 14:26:00 +0000666 ".schema ?TABLE? Show the CREATE statements\n"
drhfeac5f82004-08-01 00:10:45 +0000667 ".separator STRING Change separator string\n"
drhdd45df82002-04-18 12:39:03 +0000668 ".show Show the current values for various settings\n"
drhfeac5f82004-08-01 00:10:45 +0000669 ".tables ?PATTERN? List names of tables matching a LIKE pattern\n"
drh2dfbbca2000-07-28 14:32:48 +0000670 ".timeout MS Try opening locked tables for MS milliseconds\n"
drh75897232000-05-29 14:26:00 +0000671 ".width NUM NUM ... Set column widths for \"column\" mode\n"
672;
673
drhdaffd0e2001-04-11 14:28:42 +0000674/* Forward reference */
675static void process_input(struct callback_data *p, FILE *in);
676
drh75897232000-05-29 14:26:00 +0000677/*
drh44c2eb12003-04-30 11:38:26 +0000678** Make sure the database is open. If it is not, then open it. If
679** the database fails to open, print an error message and exit.
680*/
681static void open_db(struct callback_data *p){
682 if( p->db==0 ){
danielk19774f057f92004-06-08 00:02:33 +0000683 sqlite3_open(p->zDbFilename, &p->db);
danielk197780290862004-05-22 09:21:21 +0000684 db = p->db;
drh2011d5f2004-07-22 02:40:37 +0000685#ifdef SQLITE_HAS_CODEC
686 sqlite3_key(p->db, p->zKey, p->zKey ? strlen(p->zKey) : 0);
drheb8ed702004-02-11 10:37:23 +0000687#endif
danielk1977bc6ada42004-06-30 08:20:16 +0000688 sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0,
689 shellstaticFunc, 0, 0);
danielk197780290862004-05-22 09:21:21 +0000690 if( SQLITE_OK!=sqlite3_errcode(db) ){
691 fprintf(stderr,"Unable to open database \"%s\": %s\n",
692 p->zDbFilename, sqlite3_errmsg(db));
drh22fbcb82004-02-01 01:22:50 +0000693 exit(1);
drh44c2eb12003-04-30 11:38:26 +0000694 }
695 }
696}
697
698/*
drhfeac5f82004-08-01 00:10:45 +0000699** Do C-language style dequoting.
700**
701** \t -> tab
702** \n -> newline
703** \r -> carriage return
704** \NNN -> ascii character NNN in octal
705** \\ -> backslash
706*/
707static void resolve_backslashes(char *z){
708 int i, j, c;
709 for(i=j=0; (c = z[i])!=0; i++, j++){
710 if( c=='\\' ){
711 c = z[++i];
712 if( c=='n' ){
713 c = '\n';
714 }else if( c=='t' ){
715 c = '\t';
716 }else if( c=='r' ){
717 c = '\r';
718 }else if( c>='0' && c<='7' ){
719 c =- '0';
720 if( z[i+1]>='0' && z[i+1]<='7' ){
721 i++;
722 c = (c<<3) + z[i] - '0';
723 if( z[i+1]>='0' && z[i+1]<='7' ){
724 i++;
725 c = (c<<3) + z[i] - '0';
726 }
727 }
728 }
729 }
730 z[j] = c;
731 }
732 z[j] = 0;
733}
734
735/*
drh75897232000-05-29 14:26:00 +0000736** If an input line begins with "." then invoke this routine to
737** process that line.
drh67505e72002-04-19 12:34:06 +0000738**
739** Return 1 to exit and 0 to continue.
drh75897232000-05-29 14:26:00 +0000740*/
drh44c2eb12003-04-30 11:38:26 +0000741static int do_meta_command(char *zLine, struct callback_data *p){
drh75897232000-05-29 14:26:00 +0000742 int i = 1;
743 int nArg = 0;
744 int n, c;
drh67505e72002-04-19 12:34:06 +0000745 int rc = 0;
drh75897232000-05-29 14:26:00 +0000746 char *azArg[50];
747
748 /* Parse the input line into tokens.
749 */
750 while( zLine[i] && nArg<ArraySize(azArg) ){
751 while( isspace(zLine[i]) ){ i++; }
drh06333682004-03-09 13:37:45 +0000752 if( zLine[i]==0 ) break;
drh75897232000-05-29 14:26:00 +0000753 if( zLine[i]=='\'' || zLine[i]=='"' ){
754 int delim = zLine[i++];
755 azArg[nArg++] = &zLine[i];
756 while( zLine[i] && zLine[i]!=delim ){ i++; }
757 if( zLine[i]==delim ){
758 zLine[i++] = 0;
759 }
drhfeac5f82004-08-01 00:10:45 +0000760 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
drh75897232000-05-29 14:26:00 +0000761 }else{
762 azArg[nArg++] = &zLine[i];
763 while( zLine[i] && !isspace(zLine[i]) ){ i++; }
764 if( zLine[i] ) zLine[i++] = 0;
drhfeac5f82004-08-01 00:10:45 +0000765 resolve_backslashes(azArg[nArg-1]);
drh75897232000-05-29 14:26:00 +0000766 }
767 }
768
769 /* Process the input line.
770 */
drh67505e72002-04-19 12:34:06 +0000771 if( nArg==0 ) return rc;
drh75897232000-05-29 14:26:00 +0000772 n = strlen(azArg[0]);
773 c = azArg[0][0];
jplyon6a65bb32003-05-04 07:25:57 +0000774 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
jplyon672a1ed2003-05-11 20:07:05 +0000775 struct callback_data data;
776 char *zErrMsg = 0;
jplyon6a65bb32003-05-04 07:25:57 +0000777 open_db(p);
jplyon672a1ed2003-05-11 20:07:05 +0000778 memcpy(&data, p, sizeof(data));
drhd8885442004-03-17 23:42:12 +0000779 data.showHeader = 1;
jplyon672a1ed2003-05-11 20:07:05 +0000780 data.mode = MODE_Column;
drhd8885442004-03-17 23:42:12 +0000781 data.colWidth[0] = 3;
782 data.colWidth[1] = 15;
783 data.colWidth[2] = 58;
danielk19776f8a5032004-05-10 10:34:51 +0000784 sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
jplyon672a1ed2003-05-11 20:07:05 +0000785 if( zErrMsg ){
786 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000787 sqlite3_free(zErrMsg);
jplyon6a65bb32003-05-04 07:25:57 +0000788 }
789 }else
790
drh4c653a02000-06-07 01:27:47 +0000791 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
792 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000793 open_db(p);
drh33048c02001-10-01 14:29:22 +0000794 fprintf(p->out, "BEGIN TRANSACTION;\n");
drh4c653a02000-06-07 01:27:47 +0000795 if( nArg==1 ){
danielk19776f8a5032004-05-10 10:34:51 +0000796 sqlite3_exec(p->db,
drha18c5682000-10-08 22:20:57 +0000797 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000798 "WHERE type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000799 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000800 dump_callback, p, &zErrMsg
801 );
drh4c653a02000-06-07 01:27:47 +0000802 }else{
803 int i;
804 for(i=1; i<nArg && zErrMsg==0; i++){
danielk1977bc6ada42004-06-30 08:20:16 +0000805 zShellStatic = azArg[i];
806 sqlite3_exec(p->db,
drha18c5682000-10-08 22:20:57 +0000807 "SELECT name, type, sql FROM sqlite_master "
danielk1977bc6ada42004-06-30 08:20:16 +0000808 "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000809 "ORDER BY substr(type,2,1), name",
danielk1977bc6ada42004-06-30 08:20:16 +0000810 dump_callback, p, &zErrMsg
drha18c5682000-10-08 22:20:57 +0000811 );
danielk1977bc6ada42004-06-30 08:20:16 +0000812 zShellStatic = 0;
drh4c653a02000-06-07 01:27:47 +0000813 }
814 }
815 if( zErrMsg ){
816 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000817 sqlite3_free(zErrMsg);
drh33048c02001-10-01 14:29:22 +0000818 }else{
819 fprintf(p->out, "COMMIT;\n");
drh4c653a02000-06-07 01:27:47 +0000820 }
821 }else
drh75897232000-05-29 14:26:00 +0000822
drhdaffd0e2001-04-11 14:28:42 +0000823 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
824 int j;
825 char *z = azArg[1];
826 int val = atoi(azArg[1]);
827 for(j=0; z[j]; j++){
828 if( isupper(z[j]) ) z[j] = tolower(z[j]);
829 }
830 if( strcmp(z,"on")==0 ){
831 val = 1;
832 }else if( strcmp(z,"yes")==0 ){
833 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000834 }
drhdaffd0e2001-04-11 14:28:42 +0000835 p->echoOn = val;
836 }else
837
drh75897232000-05-29 14:26:00 +0000838 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
drh67505e72002-04-19 12:34:06 +0000839 rc = 1;
drh75897232000-05-29 14:26:00 +0000840 }else
841
drhdd45df82002-04-18 12:39:03 +0000842 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +0000843 int j;
drhdd45df82002-04-18 12:39:03 +0000844 char *z = nArg>=2 ? azArg[1] : "1";
845 int val = atoi(z);
persicom7e2dfdd2002-04-18 02:46:52 +0000846 for(j=0; z[j]; j++){
847 if( isupper(z[j]) ) z[j] = tolower(z[j]);
848 }
849 if( strcmp(z,"on")==0 ){
850 val = 1;
851 }else if( strcmp(z,"yes")==0 ){
852 val = 1;
853 }
854 if(val == 1) {
855 if(!p->explainPrev.valid) {
856 p->explainPrev.valid = 1;
857 p->explainPrev.mode = p->mode;
858 p->explainPrev.showHeader = p->showHeader;
859 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
860 }
861 /* We could put this code under the !p->explainValid
862 ** condition so that it does not execute if we are already in
863 ** explain mode. However, always executing it allows us an easy
864 ** was to reset to explain mode in case the user previously
865 ** did an .explain followed by a .width, .mode or .header
866 ** command.
867 */
868 p->mode = MODE_Column;
869 p->showHeader = 1;
870 memset(p->colWidth,0,ArraySize(p->colWidth));
871 p->colWidth[0] = 4;
872 p->colWidth[1] = 12;
873 p->colWidth[2] = 10;
874 p->colWidth[3] = 10;
875 p->colWidth[4] = 35;
876 }else if (p->explainPrev.valid) {
877 p->explainPrev.valid = 0;
878 p->mode = p->explainPrev.mode;
879 p->showHeader = p->explainPrev.showHeader;
880 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
881 }
drh75897232000-05-29 14:26:00 +0000882 }else
883
persicom7e2dfdd2002-04-18 02:46:52 +0000884 if( c=='h' && (strncmp(azArg[0], "header", n)==0
885 ||
886 strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){
drh75897232000-05-29 14:26:00 +0000887 int j;
888 char *z = azArg[1];
889 int val = atoi(azArg[1]);
890 for(j=0; z[j]; j++){
891 if( isupper(z[j]) ) z[j] = tolower(z[j]);
892 }
893 if( strcmp(z,"on")==0 ){
894 val = 1;
895 }else if( strcmp(z,"yes")==0 ){
896 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000897 }
drh75897232000-05-29 14:26:00 +0000898 p->showHeader = val;
899 }else
900
901 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
902 fprintf(stderr,zHelp);
903 }else
904
drhfeac5f82004-08-01 00:10:45 +0000905 if( c=='i' && strncmp(azArg[0], "import", n)==0 && nArg>=3 ){
906 char *zTable = azArg[2]; /* Insert data into this table */
907 char *zFile = azArg[1]; /* The file from which to extract data */
908 sqlite3_stmt *pStmt; /* A statement */
909 int rc; /* Result code */
910 int nCol; /* Number of columns in the table */
911 int nByte; /* Number of bytes in an SQL string */
912 int i, j; /* Loop counters */
913 int nSep; /* Number of bytes in p->separator[] */
914 char *zSql; /* An SQL statement */
915 char *zLine; /* A single line of input from the file */
916 char **azCol; /* zLine[] broken up into columns */
917 char *zCommit; /* How to commit changes */
918 FILE *in; /* The input file */
919
920 nSep = strlen(p->separator);
921 if( nSep==0 ){
922 fprintf(stderr, "non-null separator required for import\n");
923 return 0;
924 }
925 zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
926 if( zSql==0 ) return 0;
927 nByte = strlen(zSql);
928 rc = sqlite3_prepare(p->db, zSql, 0, &pStmt, 0);
929 sqlite3_free(zSql);
930 if( rc ){
931 fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
932 nCol = 0;
933 }else{
934 nCol = sqlite3_column_count(pStmt);
935 }
936 sqlite3_finalize(pStmt);
937 if( nCol==0 ) return 0;
938 zSql = malloc( nByte + 20 + nCol*2 );
939 if( zSql==0 ) return 0;
940 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO '%q' VALUES(?", zTable);
941 j = strlen(zSql);
942 for(i=1; i<nCol; i++){
943 zSql[j++] = ',';
944 zSql[j++] = '?';
945 }
946 zSql[j++] = ')';
947 zSql[j] = 0;
948 rc = sqlite3_prepare(p->db, zSql, 0, &pStmt, 0);
949 free(zSql);
950 if( rc ){
951 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
952 sqlite3_finalize(pStmt);
953 return 0;
954 }
955 in = fopen(zFile, "rb");
956 if( in==0 ){
957 fprintf(stderr, "cannot open file: %s\n", zFile);
958 sqlite3_finalize(pStmt);
959 return 0;
960 }
961 azCol = malloc( sizeof(azCol[0])*(nCol+1) );
962 if( azCol==0 ) return 0;
963 sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
964 zCommit = "COMMIT";
965 while( (zLine = local_getline(0, in))!=0 ){
966 char *z;
967 i = 0;
968 azCol[0] = zLine;
969 for(i=0, z=zLine; *z; z++){
970 if( *z==p->separator[0] && strncmp(z, p->separator, nSep)==0 ){
971 *z = 0;
972 i++;
973 if( i>=nCol ) break;
974 azCol[i] = &z[nSep];
975 z += nSep-1;
976 }
977 }
978 while( i<nCol ) azCol[i++] = 0;
979 for(i=0; i<nCol; i++){
980 sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
981 }
982 sqlite3_step(pStmt);
983 rc = sqlite3_reset(pStmt);
984 free(zLine);
985 if( rc!=SQLITE_OK ){
986 fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
987 zCommit = "ROLLBACK";
988 break;
989 }
990 }
991 free(azCol);
992 fclose(in);
993 sqlite3_finalize(pStmt);
994 sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
995 }else
996
drh75897232000-05-29 14:26:00 +0000997 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
998 struct callback_data data;
999 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +00001000 open_db(p);
drh75897232000-05-29 14:26:00 +00001001 memcpy(&data, p, sizeof(data));
1002 data.showHeader = 0;
1003 data.mode = MODE_List;
danielk1977bc6ada42004-06-30 08:20:16 +00001004 zShellStatic = azArg[1];
1005 sqlite3_exec(p->db,
drha18c5682000-10-08 22:20:57 +00001006 "SELECT name FROM sqlite_master "
danielk1977bc6ada42004-06-30 08:20:16 +00001007 "WHERE type='index' AND tbl_name LIKE shellstatic() "
drhe0bc4042002-06-25 01:09:11 +00001008 "UNION ALL "
1009 "SELECT name FROM sqlite_temp_master "
danielk1977bc6ada42004-06-30 08:20:16 +00001010 "WHERE type='index' AND tbl_name LIKE shellstatic() "
drhe0bc4042002-06-25 01:09:11 +00001011 "ORDER BY 1",
danielk1977bc6ada42004-06-30 08:20:16 +00001012 callback, &data, &zErrMsg
drha18c5682000-10-08 22:20:57 +00001013 );
danielk1977bc6ada42004-06-30 08:20:16 +00001014 zShellStatic = 0;
drh75897232000-05-29 14:26:00 +00001015 if( zErrMsg ){
1016 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001017 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00001018 }
1019 }else
1020
drh28bd4bc2000-06-15 15:57:22 +00001021 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
drh75897232000-05-29 14:26:00 +00001022 int n2 = strlen(azArg[1]);
persicom7e2dfdd2002-04-18 02:46:52 +00001023 if( strncmp(azArg[1],"line",n2)==0
1024 ||
1025 strncmp(azArg[1],"lines",n2)==0 ){
drh75897232000-05-29 14:26:00 +00001026 p->mode = MODE_Line;
persicom7e2dfdd2002-04-18 02:46:52 +00001027 }else if( strncmp(azArg[1],"column",n2)==0
1028 ||
1029 strncmp(azArg[1],"columns",n2)==0 ){
drh75897232000-05-29 14:26:00 +00001030 p->mode = MODE_Column;
1031 }else if( strncmp(azArg[1],"list",n2)==0 ){
1032 p->mode = MODE_List;
drh1e5d0e92000-05-31 23:33:17 +00001033 }else if( strncmp(azArg[1],"html",n2)==0 ){
1034 p->mode = MODE_Html;
drhfeac5f82004-08-01 00:10:45 +00001035 }else if( strncmp(azArg[1],"tcl",n2)==0 ){
1036 p->mode = MODE_Tcl;
1037 }else if( strncmp(azArg[1],"csv",n2)==0 ){
1038 p->mode = MODE_List;
1039 strcpy(p->separator, ",");
1040 }else if( strncmp(azArg[1],"tabs",n2)==0 ){
1041 p->mode = MODE_List;
1042 strcpy(p->separator, "\t");
drh28bd4bc2000-06-15 15:57:22 +00001043 }else if( strncmp(azArg[1],"insert",n2)==0 ){
1044 p->mode = MODE_Insert;
1045 if( nArg>=3 ){
drh33048c02001-10-01 14:29:22 +00001046 set_table_name(p, azArg[2]);
drh28bd4bc2000-06-15 15:57:22 +00001047 }else{
drh33048c02001-10-01 14:29:22 +00001048 set_table_name(p, "table");
drh28bd4bc2000-06-15 15:57:22 +00001049 }
drhdaffd0e2001-04-11 14:28:42 +00001050 }else {
drhfeac5f82004-08-01 00:10:45 +00001051 fprintf(stderr,"mode should be on of: "
1052 "column csv html insert line list tabs tcl\n");
drh75897232000-05-29 14:26:00 +00001053 }
1054 }else
1055
persicom7e2dfdd2002-04-18 02:46:52 +00001056 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
1057 sprintf(p->nullvalue, "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
1058 }else
1059
drh75897232000-05-29 14:26:00 +00001060 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
1061 if( p->out!=stdout ){
1062 fclose(p->out);
1063 }
1064 if( strcmp(azArg[1],"stdout")==0 ){
1065 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +00001066 strcpy(p->outfile,"stdout");
drh75897232000-05-29 14:26:00 +00001067 }else{
drha1f9b5e2004-02-14 16:31:02 +00001068 p->out = fopen(azArg[1], "wb");
drh75897232000-05-29 14:26:00 +00001069 if( p->out==0 ){
1070 fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
1071 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +00001072 } else {
1073 strcpy(p->outfile,azArg[1]);
drh75897232000-05-29 14:26:00 +00001074 }
1075 }
1076 }else
1077
drhdd45df82002-04-18 12:39:03 +00001078 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
persicom7e2dfdd2002-04-18 02:46:52 +00001079 if( nArg >= 2) {
1080 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
1081 }
1082 if( nArg >= 3) {
1083 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
1084 }
1085 }else
1086
1087 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
drhf73287c2004-02-25 02:25:37 +00001088 rc = 1;
persicom7e2dfdd2002-04-18 02:46:52 +00001089 }else
1090
drhdaffd0e2001-04-11 14:28:42 +00001091 if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
drha1f9b5e2004-02-14 16:31:02 +00001092 FILE *alt = fopen(azArg[1], "rb");
drhdaffd0e2001-04-11 14:28:42 +00001093 if( alt==0 ){
1094 fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
1095 }else{
1096 process_input(p, alt);
1097 fclose(alt);
1098 }
1099 }else
1100
drh9eb9e262004-02-11 02:18:05 +00001101#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00001102 if( c=='r' && strncmp(azArg[0],"rekey", n)==0 && nArg==4 ){
1103 char *zOld = p->zKey;
1104 if( zOld==0 ) zOld = "";
1105 if( strcmp(azArg[1],zOld) ){
1106 fprintf(stderr,"old key is incorrect\n");
1107 }else if( strcmp(azArg[2], azArg[3]) ){
1108 fprintf(stderr,"2nd copy of new key does not match the 1st\n");
1109 }else{
drh3f4fedb2004-05-31 19:34:33 +00001110 sqlite3_free(p->zKey);
danielk19776f8a5032004-05-10 10:34:51 +00001111 p->zKey = sqlite3_mprintf("%s", azArg[2]);
drh2011d5f2004-07-22 02:40:37 +00001112 sqlite3_rekey(p->db, p->zKey, strlen(p->zKey));
drh22fbcb82004-02-01 01:22:50 +00001113 }
1114 }else
1115#endif
1116
drh75897232000-05-29 14:26:00 +00001117 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
1118 struct callback_data data;
1119 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +00001120 open_db(p);
drh75897232000-05-29 14:26:00 +00001121 memcpy(&data, p, sizeof(data));
1122 data.showHeader = 0;
drhe3710332000-09-29 13:30:53 +00001123 data.mode = MODE_Semi;
drh75897232000-05-29 14:26:00 +00001124 if( nArg>1 ){
danielk19774adee202004-05-08 08:23:19 +00001125 extern int sqlite3StrICmp(const char*,const char*);
1126 if( sqlite3StrICmp(azArg[1],"sqlite_master")==0 ){
drha18c5682000-10-08 22:20:57 +00001127 char *new_argv[2], *new_colv[2];
1128 new_argv[0] = "CREATE TABLE sqlite_master (\n"
1129 " type text,\n"
1130 " name text,\n"
1131 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +00001132 " rootpage integer,\n"
drha18c5682000-10-08 22:20:57 +00001133 " sql text\n"
1134 ")";
1135 new_argv[1] = 0;
1136 new_colv[0] = "sql";
1137 new_colv[1] = 0;
1138 callback(&data, 1, new_argv, new_colv);
danielk19774adee202004-05-08 08:23:19 +00001139 }else if( sqlite3StrICmp(azArg[1],"sqlite_temp_master")==0 ){
drhe0bc4042002-06-25 01:09:11 +00001140 char *new_argv[2], *new_colv[2];
1141 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
1142 " type text,\n"
1143 " name text,\n"
1144 " tbl_name text,\n"
1145 " rootpage integer,\n"
1146 " sql text\n"
1147 ")";
1148 new_argv[1] = 0;
1149 new_colv[0] = "sql";
1150 new_colv[1] = 0;
1151 callback(&data, 1, new_argv, new_colv);
drha18c5682000-10-08 22:20:57 +00001152 }else{
danielk1977bc6ada42004-06-30 08:20:16 +00001153 zShellStatic = azArg[1];
1154 sqlite3_exec(p->db,
drhe0bc4042002-06-25 01:09:11 +00001155 "SELECT sql FROM "
1156 " (SELECT * FROM sqlite_master UNION ALL"
1157 " SELECT * FROM sqlite_temp_master) "
danielk1977bc6ada42004-06-30 08:20:16 +00001158 "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +00001159 "ORDER BY substr(type,2,1), name",
danielk1977bc6ada42004-06-30 08:20:16 +00001160 callback, &data, &zErrMsg);
1161 zShellStatic = 0;
drha18c5682000-10-08 22:20:57 +00001162 }
drh75897232000-05-29 14:26:00 +00001163 }else{
danielk19776f8a5032004-05-10 10:34:51 +00001164 sqlite3_exec(p->db,
drhe0bc4042002-06-25 01:09:11 +00001165 "SELECT sql FROM "
1166 " (SELECT * FROM sqlite_master UNION ALL"
1167 " SELECT * FROM sqlite_temp_master) "
drh33048c02001-10-01 14:29:22 +00001168 "WHERE type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +00001169 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +00001170 callback, &data, &zErrMsg
1171 );
drh75897232000-05-29 14:26:00 +00001172 }
drh75897232000-05-29 14:26:00 +00001173 if( zErrMsg ){
1174 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001175 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00001176 }
1177 }else
1178
1179 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
1180 sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
1181 }else
1182
persicom7e2dfdd2002-04-18 02:46:52 +00001183 if( c=='s' && strncmp(azArg[0], "show", n)==0){
1184 int i;
1185 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
drh67505e72002-04-19 12:34:06 +00001186 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
drhdd45df82002-04-18 12:39:03 +00001187 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
persicom7e2dfdd2002-04-18 02:46:52 +00001188 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
drhfeac5f82004-08-01 00:10:45 +00001189 fprintf(p->out,"%9.9s: ", "nullvalue");
1190 output_c_string(p->out, p->nullvalue);
1191 fprintf(p->out, "\n");
drh67505e72002-04-19 12:34:06 +00001192 fprintf(p->out,"%9.9s: %s\n","output",
1193 strlen(p->outfile) ? p->outfile : "stdout");
drhfeac5f82004-08-01 00:10:45 +00001194 fprintf(p->out,"%9.9s: ", "separator");
1195 output_c_string(p->out, p->separator);
1196 fprintf(p->out, "\n");
persicom7e2dfdd2002-04-18 02:46:52 +00001197 fprintf(p->out,"%9.9s: ","width");
1198 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
drhfeac5f82004-08-01 00:10:45 +00001199 fprintf(p->out,"%d ",p->colWidth[i]);
persicom7e2dfdd2002-04-18 02:46:52 +00001200 }
drhfeac5f82004-08-01 00:10:45 +00001201 fprintf(p->out,"\n");
persicom7e2dfdd2002-04-18 02:46:52 +00001202 }else
1203
drh2dfbbca2000-07-28 14:32:48 +00001204 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
drhe3710332000-09-29 13:30:53 +00001205 char **azResult;
1206 int nRow, rc;
1207 char *zErrMsg;
drh44c2eb12003-04-30 11:38:26 +00001208 open_db(p);
drha50da102000-08-08 20:19:09 +00001209 if( nArg==1 ){
danielk19776f8a5032004-05-10 10:34:51 +00001210 rc = sqlite3_get_table(p->db,
drha50da102000-08-08 20:19:09 +00001211 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +00001212 "WHERE type IN ('table','view') "
drhe0bc4042002-06-25 01:09:11 +00001213 "UNION ALL "
1214 "SELECT name FROM sqlite_temp_master "
1215 "WHERE type IN ('table','view') "
1216 "ORDER BY 1",
drha18c5682000-10-08 22:20:57 +00001217 &azResult, &nRow, 0, &zErrMsg
1218 );
drha50da102000-08-08 20:19:09 +00001219 }else{
danielk1977bc6ada42004-06-30 08:20:16 +00001220 zShellStatic = azArg[1];
1221 rc = sqlite3_get_table(p->db,
drha50da102000-08-08 20:19:09 +00001222 "SELECT name FROM sqlite_master "
danielk1977bc6ada42004-06-30 08:20:16 +00001223 "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' "
drhe0bc4042002-06-25 01:09:11 +00001224 "UNION ALL "
1225 "SELECT name FROM sqlite_temp_master "
danielk1977bc6ada42004-06-30 08:20:16 +00001226 "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' "
drhe0bc4042002-06-25 01:09:11 +00001227 "ORDER BY 1",
danielk1977bc6ada42004-06-30 08:20:16 +00001228 &azResult, &nRow, 0, &zErrMsg
drha18c5682000-10-08 22:20:57 +00001229 );
danielk1977bc6ada42004-06-30 08:20:16 +00001230 zShellStatic = 0;
drha50da102000-08-08 20:19:09 +00001231 }
drh75897232000-05-29 14:26:00 +00001232 if( zErrMsg ){
1233 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001234 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00001235 }
drhe3710332000-09-29 13:30:53 +00001236 if( rc==SQLITE_OK ){
1237 int len, maxlen = 0;
1238 int i, j;
1239 int nPrintCol, nPrintRow;
1240 for(i=1; i<=nRow; i++){
1241 if( azResult[i]==0 ) continue;
1242 len = strlen(azResult[i]);
1243 if( len>maxlen ) maxlen = len;
1244 }
1245 nPrintCol = 80/(maxlen+2);
1246 if( nPrintCol<1 ) nPrintCol = 1;
1247 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
1248 for(i=0; i<nPrintRow; i++){
1249 for(j=i+1; j<=nRow; j+=nPrintRow){
1250 char *zSp = j<=nPrintRow ? "" : " ";
1251 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
1252 }
1253 printf("\n");
1254 }
1255 }
danielk19776f8a5032004-05-10 10:34:51 +00001256 sqlite3_free_table(azResult);
drh75897232000-05-29 14:26:00 +00001257 }else
1258
drh2dfbbca2000-07-28 14:32:48 +00001259 if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
drh44c2eb12003-04-30 11:38:26 +00001260 open_db(p);
danielk19776f8a5032004-05-10 10:34:51 +00001261 sqlite3_busy_timeout(p->db, atoi(azArg[1]));
drh2dfbbca2000-07-28 14:32:48 +00001262 }else
1263
drh75897232000-05-29 14:26:00 +00001264 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
1265 int j;
1266 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
1267 p->colWidth[j-1] = atoi(azArg[j]);
1268 }
1269 }else
1270
1271 {
drh67505e72002-04-19 12:34:06 +00001272 fprintf(stderr, "unknown command or invalid arguments: "
1273 " \"%s\". Enter \".help\" for help\n", azArg[0]);
drh75897232000-05-29 14:26:00 +00001274 }
drh67505e72002-04-19 12:34:06 +00001275
1276 return rc;
drh75897232000-05-29 14:26:00 +00001277}
1278
drh67505e72002-04-19 12:34:06 +00001279/*
drh324ccef2003-02-05 14:06:20 +00001280** Return TRUE if the last non-whitespace character in z[] is a semicolon.
1281** z[] is N characters long.
1282*/
1283static int _ends_with_semicolon(const char *z, int N){
1284 while( N>0 && isspace(z[N-1]) ){ N--; }
1285 return N>0 && z[N-1]==';';
1286}
1287
1288/*
drh70c7a4b2003-04-26 03:03:06 +00001289** Test to see if a line consists entirely of whitespace.
1290*/
1291static int _all_whitespace(const char *z){
1292 for(; *z; z++){
1293 if( isspace(*z) ) continue;
1294 if( *z=='/' && z[1]=='*' ){
1295 z += 2;
1296 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
1297 if( *z==0 ) return 0;
1298 z++;
1299 continue;
1300 }
1301 if( *z=='-' && z[1]=='-' ){
1302 z += 2;
1303 while( *z && *z!='\n' ){ z++; }
1304 if( *z==0 ) return 1;
1305 continue;
1306 }
1307 return 0;
1308 }
1309 return 1;
1310}
1311
1312/*
drha9b17162003-04-29 18:01:28 +00001313** Return TRUE if the line typed in is an SQL command terminator other
1314** than a semi-colon. The SQL Server style "go" command is understood
1315** as is the Oracle "/".
1316*/
1317static int _is_command_terminator(const char *zLine){
danielk19774adee202004-05-08 08:23:19 +00001318 extern int sqlite3StrNICmp(const char*,const char*,int);
drha9b17162003-04-29 18:01:28 +00001319 while( isspace(*zLine) ){ zLine++; };
1320 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ) return 1; /* Oracle */
danielk19774adee202004-05-08 08:23:19 +00001321 if( sqlite3StrNICmp(zLine,"go",2)==0 && _all_whitespace(&zLine[2]) ){
drha9b17162003-04-29 18:01:28 +00001322 return 1; /* SQL Server */
1323 }
1324 return 0;
1325}
1326
1327/*
drh67505e72002-04-19 12:34:06 +00001328** Read input from *in and process it. If *in==0 then input
1329** is interactive - the user is typing it it. Otherwise, input
1330** is coming from a file or device. A prompt is issued and history
1331** is saved only if input is interactive. An interrupt signal will
1332** cause this routine to exit immediately, unless input is interactive.
1333*/
drhdaffd0e2001-04-11 14:28:42 +00001334static void process_input(struct callback_data *p, FILE *in){
1335 char *zLine;
1336 char *zSql = 0;
1337 int nSql = 0;
1338 char *zErrMsg;
drh7f953e22002-07-13 17:33:45 +00001339 int rc;
drh41e941d2002-04-04 15:10:12 +00001340 while( fflush(p->out), (zLine = one_input_line(zSql, in))!=0 ){
drh67505e72002-04-19 12:34:06 +00001341 if( seenInterrupt ){
1342 if( in!=0 ) break;
1343 seenInterrupt = 0;
1344 }
drhdaffd0e2001-04-11 14:28:42 +00001345 if( p->echoOn ) printf("%s\n", zLine);
drhf817b6b2003-06-16 00:16:41 +00001346 if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
drh2af0b2d2002-02-21 02:25:02 +00001347 if( zLine && zLine[0]=='.' && nSql==0 ){
drh44c2eb12003-04-30 11:38:26 +00001348 int rc = do_meta_command(zLine, p);
drhdaffd0e2001-04-11 14:28:42 +00001349 free(zLine);
drh67505e72002-04-19 12:34:06 +00001350 if( rc ) break;
drhdaffd0e2001-04-11 14:28:42 +00001351 continue;
1352 }
drha9b17162003-04-29 18:01:28 +00001353 if( _is_command_terminator(zLine) ){
1354 strcpy(zLine,";");
1355 }
drhdaffd0e2001-04-11 14:28:42 +00001356 if( zSql==0 ){
1357 int i;
1358 for(i=0; zLine[i] && isspace(zLine[i]); i++){}
1359 if( zLine[i]!=0 ){
1360 nSql = strlen(zLine);
1361 zSql = malloc( nSql+1 );
1362 strcpy(zSql, zLine);
1363 }
1364 }else{
1365 int len = strlen(zLine);
1366 zSql = realloc( zSql, nSql + len + 2 );
1367 if( zSql==0 ){
1368 fprintf(stderr,"%s: out of memory!\n", Argv0);
1369 exit(1);
1370 }
1371 strcpy(&zSql[nSql++], "\n");
1372 strcpy(&zSql[nSql], zLine);
1373 nSql += len;
1374 }
1375 free(zLine);
danielk19776f8a5032004-05-10 10:34:51 +00001376 if( zSql && _ends_with_semicolon(zSql, nSql) && sqlite3_complete(zSql) ){
drhdaffd0e2001-04-11 14:28:42 +00001377 p->cnt = 0;
drh44c2eb12003-04-30 11:38:26 +00001378 open_db(p);
danielk19776f8a5032004-05-10 10:34:51 +00001379 rc = sqlite3_exec(p->db, zSql, callback, p, &zErrMsg);
drh7f953e22002-07-13 17:33:45 +00001380 if( rc || zErrMsg ){
drhdaffd0e2001-04-11 14:28:42 +00001381 if( in!=0 && !p->echoOn ) printf("%s\n",zSql);
drh7f953e22002-07-13 17:33:45 +00001382 if( zErrMsg!=0 ){
1383 printf("SQL error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001384 sqlite3_free(zErrMsg);
drh7f953e22002-07-13 17:33:45 +00001385 zErrMsg = 0;
1386 }else{
danielk19772a02e332004-06-05 08:04:36 +00001387 printf("SQL error: %s\n", sqlite3_errmsg(p->db));
drh7f953e22002-07-13 17:33:45 +00001388 }
drhdaffd0e2001-04-11 14:28:42 +00001389 }
1390 free(zSql);
1391 zSql = 0;
1392 nSql = 0;
1393 }
1394 }
1395 if( zSql ){
drh70c7a4b2003-04-26 03:03:06 +00001396 if( !_all_whitespace(zSql) ) printf("Incomplete SQL: %s\n", zSql);
drhdaffd0e2001-04-11 14:28:42 +00001397 free(zSql);
1398 }
1399}
1400
drh67505e72002-04-19 12:34:06 +00001401/*
1402** Return a pathname which is the user's home directory. A
1403** 0 return indicates an error of some kind. Space to hold the
1404** resulting string is obtained from malloc(). The calling
1405** function should free the result.
1406*/
1407static char *find_home_dir(void){
1408 char *home_dir = NULL;
persicom7e2dfdd2002-04-18 02:46:52 +00001409
drh820f3812003-01-08 13:02:52 +00001410#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
drh67505e72002-04-19 12:34:06 +00001411 struct passwd *pwent;
1412 uid_t uid = getuid();
drhbd842ba2002-08-21 11:26:41 +00001413 if( (pwent=getpwuid(uid)) != NULL) {
1414 home_dir = pwent->pw_dir;
drh67505e72002-04-19 12:34:06 +00001415 }
1416#endif
1417
drh820f3812003-01-08 13:02:52 +00001418#ifdef __MACOS__
1419 char home_path[_MAX_PATH+1];
1420 home_dir = getcwd(home_path, _MAX_PATH);
1421#endif
1422
drh67505e72002-04-19 12:34:06 +00001423 if (!home_dir) {
1424 home_dir = getenv("HOME");
1425 if (!home_dir) {
1426 home_dir = getenv("HOMEPATH"); /* Windows? */
1427 }
1428 }
1429
drhe98d4fa2002-04-21 19:06:22 +00001430#if defined(_WIN32) || defined(WIN32)
1431 if (!home_dir) {
1432 home_dir = "c:";
1433 }
1434#endif
1435
drh67505e72002-04-19 12:34:06 +00001436 if( home_dir ){
1437 char *z = malloc( strlen(home_dir)+1 );
1438 if( z ) strcpy(z, home_dir);
1439 home_dir = z;
1440 }
drhe98d4fa2002-04-21 19:06:22 +00001441
drh67505e72002-04-19 12:34:06 +00001442 return home_dir;
1443}
1444
1445/*
1446** Read input from the file given by sqliterc_override. Or if that
1447** parameter is NULL, take input from ~/.sqliterc
1448*/
drh22fbcb82004-02-01 01:22:50 +00001449static void process_sqliterc(
1450 struct callback_data *p, /* Configuration data */
1451 const char *sqliterc_override /* Name of config file. NULL to use default */
1452){
persicom7e2dfdd2002-04-18 02:46:52 +00001453 char *home_dir = NULL;
drh22fbcb82004-02-01 01:22:50 +00001454 const char *sqliterc = sqliterc_override;
1455 char *zBuf;
persicom7e2dfdd2002-04-18 02:46:52 +00001456 FILE *in = NULL;
1457
1458 if (sqliterc == NULL) {
drh67505e72002-04-19 12:34:06 +00001459 home_dir = find_home_dir();
drhe98d4fa2002-04-21 19:06:22 +00001460 if( home_dir==0 ){
1461 fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0);
1462 return;
1463 }
drh22fbcb82004-02-01 01:22:50 +00001464 zBuf = malloc(strlen(home_dir) + 15);
1465 if( zBuf==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +00001466 fprintf(stderr,"%s: out of memory!\n", Argv0);
1467 exit(1);
1468 }
drh22fbcb82004-02-01 01:22:50 +00001469 sprintf(zBuf,"%s/.sqliterc",home_dir);
drh67505e72002-04-19 12:34:06 +00001470 free(home_dir);
drh22fbcb82004-02-01 01:22:50 +00001471 sqliterc = (const char*)zBuf;
persicom7e2dfdd2002-04-18 02:46:52 +00001472 }
drha1f9b5e2004-02-14 16:31:02 +00001473 in = fopen(sqliterc,"rb");
drh22fbcb82004-02-01 01:22:50 +00001474 if( in ){
1475 if( isatty(fileno(stdout)) ){
1476 printf("Loading resources from %s\n",sqliterc);
1477 }
persicom7e2dfdd2002-04-18 02:46:52 +00001478 process_input(p,in);
drhdd45df82002-04-18 12:39:03 +00001479 fclose(in);
persicom7e2dfdd2002-04-18 02:46:52 +00001480 }
1481 return;
1482}
1483
drh67505e72002-04-19 12:34:06 +00001484/*
drhe1e38c42003-05-04 18:30:59 +00001485** Show available command line options
1486*/
1487static const char zOptions[] =
1488 " -init filename read/process named file\n"
1489 " -echo print commands before execution\n"
1490 " -[no]header turn headers on or off\n"
1491 " -column set output mode to 'column'\n"
1492 " -html set output mode to HTML\n"
drh9eb9e262004-02-11 02:18:05 +00001493#ifdef SQLITE_HAS_CODEC
drh57ced912004-02-10 02:57:59 +00001494 " -key KEY encryption key\n"
1495#endif
drhe1e38c42003-05-04 18:30:59 +00001496 " -line set output mode to 'line'\n"
1497 " -list set output mode to 'list'\n"
1498 " -separator 'x' set output field separator (|)\n"
1499 " -nullvalue 'text' set text string for NULL values\n"
1500 " -version show SQLite version\n"
1501 " -help show this text, also show dot-commands\n"
1502;
1503static void usage(int showDetail){
1504 fprintf(stderr, "Usage: %s [OPTIONS] FILENAME [SQL]\n", Argv0);
1505 if( showDetail ){
1506 fprintf(stderr, "Options are:\n%s", zOptions);
1507 }else{
1508 fprintf(stderr, "Use the -help option for additional information\n");
1509 }
1510 exit(1);
1511}
1512
1513/*
drh67505e72002-04-19 12:34:06 +00001514** Initialize the state information in data
1515*/
persicom7e2dfdd2002-04-18 02:46:52 +00001516void main_init(struct callback_data *data) {
1517 memset(data, 0, sizeof(*data));
1518 data->mode = MODE_List;
1519 strcpy(data->separator,"|");
1520 data->showHeader = 0;
1521 strcpy(mainPrompt,"sqlite> ");
1522 strcpy(continuePrompt," ...> ");
1523}
1524
drh75897232000-05-29 14:26:00 +00001525int main(int argc, char **argv){
drh75897232000-05-29 14:26:00 +00001526 char *zErrMsg = 0;
1527 struct callback_data data;
drh22fbcb82004-02-01 01:22:50 +00001528 const char *zInitFile = 0;
1529 char *zFirstCmd = 0;
drh44c2eb12003-04-30 11:38:26 +00001530 int i;
danielk19774adee202004-05-08 08:23:19 +00001531 extern int sqlite3OsFileExists(const char*);
drh75897232000-05-29 14:26:00 +00001532
drh820f3812003-01-08 13:02:52 +00001533#ifdef __MACOS__
1534 argc = ccommand(&argv);
drh820f3812003-01-08 13:02:52 +00001535#endif
1536
drhdaffd0e2001-04-11 14:28:42 +00001537 Argv0 = argv[0];
persicom7e2dfdd2002-04-18 02:46:52 +00001538 main_init(&data);
persicom7e2dfdd2002-04-18 02:46:52 +00001539
drh44c2eb12003-04-30 11:38:26 +00001540 /* Make sure we have a valid signal handler early, before anything
1541 ** else is done.
1542 */
drh4c504392000-10-16 22:06:40 +00001543#ifdef SIGINT
1544 signal(SIGINT, interrupt_handler);
1545#endif
drh44c2eb12003-04-30 11:38:26 +00001546
drh22fbcb82004-02-01 01:22:50 +00001547 /* Do an initial pass through the command-line argument to locate
1548 ** the name of the database file, the name of the initialization file,
1549 ** and the first command to execute.
drh44c2eb12003-04-30 11:38:26 +00001550 */
drh22fbcb82004-02-01 01:22:50 +00001551 for(i=1; i<argc-1; i++){
drh44c2eb12003-04-30 11:38:26 +00001552 if( argv[i][0]!='-' ) break;
1553 if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){
1554 i++;
drh22fbcb82004-02-01 01:22:50 +00001555 }else if( strcmp(argv[i],"-init")==0 ){
1556 i++;
1557 zInitFile = argv[i];
1558 }else if( strcmp(argv[i],"-key")==0 ){
1559 i++;
danielk19776f8a5032004-05-10 10:34:51 +00001560 data.zKey = sqlite3_mprintf("%s",argv[i]);
drh44c2eb12003-04-30 11:38:26 +00001561 }
1562 }
drh22fbcb82004-02-01 01:22:50 +00001563 if( i<argc ){
1564 data.zDbFilename = argv[i++];
1565 }else{
1566 data.zDbFilename = ":memory:";
1567 }
1568 if( i<argc ){
1569 zFirstCmd = argv[i++];
1570 }
drh44c2eb12003-04-30 11:38:26 +00001571 data.out = stdout;
1572
1573 /* Go ahead and open the database file if it already exists. If the
1574 ** file does not exist, delay opening it. This prevents empty database
1575 ** files from being created if a user mistypes the database name argument
1576 ** to the sqlite command-line tool.
1577 */
danielk19774adee202004-05-08 08:23:19 +00001578 if( sqlite3OsFileExists(data.zDbFilename) ){
drh44c2eb12003-04-30 11:38:26 +00001579 open_db(&data);
1580 }
1581
drh22fbcb82004-02-01 01:22:50 +00001582 /* Process the initialization file if there is one. If no -init option
1583 ** is given on the command line, look for a file named ~/.sqliterc and
1584 ** try to process it.
drh44c2eb12003-04-30 11:38:26 +00001585 */
drh22fbcb82004-02-01 01:22:50 +00001586 process_sqliterc(&data,zInitFile);
drh44c2eb12003-04-30 11:38:26 +00001587
drh22fbcb82004-02-01 01:22:50 +00001588 /* Make a second pass through the command-line argument and set
1589 ** options. This second pass is delayed until after the initialization
1590 ** file is processed so that the command-line arguments will override
1591 ** settings in the initialization file.
drh44c2eb12003-04-30 11:38:26 +00001592 */
drh22fbcb82004-02-01 01:22:50 +00001593 for(i=1; i<argc && argv[i][0]=='-'; i++){
1594 char *z = argv[i];
1595 if( strcmp(z,"-init")==0 || strcmp(z,"-key")==0 ){
1596 i++;
1597 }else if( strcmp(z,"-html")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001598 data.mode = MODE_Html;
drh22fbcb82004-02-01 01:22:50 +00001599 }else if( strcmp(z,"-list")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001600 data.mode = MODE_List;
drh22fbcb82004-02-01 01:22:50 +00001601 }else if( strcmp(z,"-line")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001602 data.mode = MODE_Line;
drh22fbcb82004-02-01 01:22:50 +00001603 }else if( strcmp(z,"-column")==0 ){
drh8b32e172002-04-08 02:42:57 +00001604 data.mode = MODE_Column;
drh22fbcb82004-02-01 01:22:50 +00001605 }else if( strcmp(z,"-separator")==0 ){
1606 i++;
1607 sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[i]);
1608 }else if( strcmp(z,"-nullvalue")==0 ){
1609 i++;
1610 sprintf(data.nullvalue,"%.*s",(int)sizeof(data.nullvalue)-1,argv[i]);
1611 }else if( strcmp(z,"-header")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001612 data.showHeader = 1;
drh22fbcb82004-02-01 01:22:50 +00001613 }else if( strcmp(z,"-noheader")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001614 data.showHeader = 0;
drh22fbcb82004-02-01 01:22:50 +00001615 }else if( strcmp(z,"-echo")==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001616 data.echoOn = 1;
drh22fbcb82004-02-01 01:22:50 +00001617 }else if( strcmp(z,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001618 printf("%s\n", sqlite3_version);
drhe1e38c42003-05-04 18:30:59 +00001619 return 1;
drh22fbcb82004-02-01 01:22:50 +00001620 }else if( strcmp(z,"-help")==0 ){
drhe1e38c42003-05-04 18:30:59 +00001621 usage(1);
drh1e5d0e92000-05-31 23:33:17 +00001622 }else{
drh22fbcb82004-02-01 01:22:50 +00001623 fprintf(stderr,"%s: unknown option: %s\n", Argv0, z);
drhe1e38c42003-05-04 18:30:59 +00001624 fprintf(stderr,"Use -help for a list of options.\n");
drh1e5d0e92000-05-31 23:33:17 +00001625 return 1;
1626 }
1627 }
drh44c2eb12003-04-30 11:38:26 +00001628
drh22fbcb82004-02-01 01:22:50 +00001629 if( zFirstCmd ){
drh44c2eb12003-04-30 11:38:26 +00001630 /* Run just the command that follows the database name
1631 */
drh22fbcb82004-02-01 01:22:50 +00001632 if( zFirstCmd[0]=='.' ){
1633 do_meta_command(zFirstCmd, &data);
drh6ff13852001-11-25 13:18:23 +00001634 exit(0);
1635 }else{
1636 int rc;
drh44c2eb12003-04-30 11:38:26 +00001637 open_db(&data);
danielk19776f8a5032004-05-10 10:34:51 +00001638 rc = sqlite3_exec(data.db, zFirstCmd, callback, &data, &zErrMsg);
drh6ff13852001-11-25 13:18:23 +00001639 if( rc!=0 && zErrMsg!=0 ){
1640 fprintf(stderr,"SQL error: %s\n", zErrMsg);
1641 exit(1);
1642 }
drh75897232000-05-29 14:26:00 +00001643 }
1644 }else{
drh44c2eb12003-04-30 11:38:26 +00001645 /* Run commands received from standard input
1646 */
drh5cf590c2003-04-24 01:45:04 +00001647 if( isatty(fileno(stdout)) && isatty(fileno(stdin)) ){
drh67505e72002-04-19 12:34:06 +00001648 char *zHome;
1649 char *zHistory = 0;
drh75897232000-05-29 14:26:00 +00001650 printf(
drhb217a572000-08-22 13:40:18 +00001651 "SQLite version %s\n"
1652 "Enter \".help\" for instructions\n",
danielk19776f8a5032004-05-10 10:34:51 +00001653 sqlite3_version
drh75897232000-05-29 14:26:00 +00001654 );
drh67505e72002-04-19 12:34:06 +00001655 zHome = find_home_dir();
1656 if( zHome && (zHistory = malloc(strlen(zHome)+20))!=0 ){
1657 sprintf(zHistory,"%s/.sqlite_history", zHome);
1658 }
1659 if( zHistory ) read_history(zHistory);
drhdaffd0e2001-04-11 14:28:42 +00001660 process_input(&data, 0);
drh67505e72002-04-19 12:34:06 +00001661 if( zHistory ){
1662 stifle_history(100);
1663 write_history(zHistory);
1664 }
drhdaffd0e2001-04-11 14:28:42 +00001665 }else{
1666 process_input(&data, stdin);
drh75897232000-05-29 14:26:00 +00001667 }
1668 }
drh33048c02001-10-01 14:29:22 +00001669 set_table_name(&data, 0);
danielk19776f8a5032004-05-10 10:34:51 +00001670 if( db ) sqlite3_close(db);
drh75897232000-05-29 14:26:00 +00001671 return 0;
1672}