blob: 43ea5066376d8d5ca6942e5dc592acf3384440b0 [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**
drh2e584cd2006-09-25 13:09:22 +000015** $Id: shell.c,v 1.150 2006/09/25 13:09:23 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
drhcdb36b72006-06-12 12:57:45 +000024#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__) && !defined(__OS2__)
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
drhcdb36b72006-06-12 12:57:45 +000040#ifdef __OS2__
41# include <unistd.h>
42#endif
43
drh16e59552000-07-31 11:57:37 +000044#if defined(HAVE_READLINE) && HAVE_READLINE==1
drh8e7e7a22000-05-30 18:45:23 +000045# include <readline/readline.h>
46# include <readline/history.h>
47#else
drh9347b202003-07-18 01:30:59 +000048# define readline(p) local_getline(p,stdin)
persicom1d0b8722002-04-18 02:53:04 +000049# define add_history(X)
drh67505e72002-04-19 12:34:06 +000050# define read_history(X)
51# define write_history(X)
52# define stifle_history(X)
drh75897232000-05-29 14:26:00 +000053#endif
54
adamd2e8464a2006-09-06 21:39:40 +000055#if defined(_WIN32) || defined(WIN32)
56# include <io.h>
57#else
drh4328c8b2003-04-26 02:50:11 +000058/* Make sure isatty() has a prototype.
59*/
60extern int isatty();
adamd2e8464a2006-09-06 21:39:40 +000061#endif
drh4328c8b2003-04-26 02:50:11 +000062
drh75897232000-05-29 14:26:00 +000063/*
drh4c504392000-10-16 22:06:40 +000064** The following is the open SQLite database. We make a pointer
65** to this database a static variable so that it can be accessed
66** by the SIGINT handler to interrupt database processing.
67*/
danielk197792f9a1b2004-06-19 09:08:16 +000068static sqlite3 *db = 0;
drh4c504392000-10-16 22:06:40 +000069
70/*
drh67505e72002-04-19 12:34:06 +000071** True if an interrupt (Control-C) has been received.
72*/
drh43617e92006-03-06 20:55:46 +000073static volatile int seenInterrupt = 0;
drh67505e72002-04-19 12:34:06 +000074
75/*
persicom7e2dfdd2002-04-18 02:46:52 +000076** This is the name of our program. It is set in main(), used
77** in a number of other places, mostly for error messages.
78*/
79static char *Argv0;
80
81/*
82** Prompt strings. Initialized in main. Settable with
83** .prompt main continue
84*/
85static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
86static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
87
drh44c2eb12003-04-30 11:38:26 +000088
persicom7e2dfdd2002-04-18 02:46:52 +000089/*
drh83965662003-04-17 02:54:13 +000090** Determines if a string is a number of not.
91*/
danielk19772e588c72005-12-09 14:25:08 +000092static int isNumber(const char *z, int *realnum){
drhc8d74412004-08-31 23:41:26 +000093 if( *z=='-' || *z=='+' ) z++;
94 if( !isdigit(*z) ){
95 return 0;
96 }
97 z++;
98 if( realnum ) *realnum = 0;
99 while( isdigit(*z) ){ z++; }
100 if( *z=='.' ){
101 z++;
102 if( !isdigit(*z) ) return 0;
103 while( isdigit(*z) ){ z++; }
104 if( realnum ) *realnum = 1;
105 }
106 if( *z=='e' || *z=='E' ){
107 z++;
108 if( *z=='+' || *z=='-' ) z++;
109 if( !isdigit(*z) ) return 0;
110 while( isdigit(*z) ){ z++; }
111 if( realnum ) *realnum = 1;
112 }
113 return *z==0;
114}
drh83965662003-04-17 02:54:13 +0000115
116/*
danielk1977bc6ada42004-06-30 08:20:16 +0000117** A global char* and an SQL function to access its current value
118** from within an SQL statement. This program used to use the
119** sqlite_exec_printf() API to substitue a string into an SQL statement.
120** The correct way to do this with sqlite3 is to use the bind API, but
121** since the shell is built around the callback paradigm it would be a lot
122** of work. Instead just use this hack, which is quite harmless.
123*/
124static const char *zShellStatic = 0;
125static void shellstaticFunc(
126 sqlite3_context *context,
127 int argc,
128 sqlite3_value **argv
129){
130 assert( 0==argc );
131 assert( zShellStatic );
132 sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC);
133}
134
135
136/*
drhfeac5f82004-08-01 00:10:45 +0000137** This routine reads a line of text from FILE in, stores
drh8e7e7a22000-05-30 18:45:23 +0000138** the text in memory obtained from malloc() and returns a pointer
139** to the text. NULL is returned at end of file, or if malloc()
140** fails.
141**
142** The interface is like "readline" but no command-line editing
143** is done.
144*/
drh9347b202003-07-18 01:30:59 +0000145static char *local_getline(char *zPrompt, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +0000146 char *zLine;
147 int nLine;
drh8e7e7a22000-05-30 18:45:23 +0000148 int n;
149 int eol;
150
151 if( zPrompt && *zPrompt ){
152 printf("%s",zPrompt);
153 fflush(stdout);
154 }
155 nLine = 100;
156 zLine = malloc( nLine );
157 if( zLine==0 ) return 0;
158 n = 0;
159 eol = 0;
160 while( !eol ){
161 if( n+100>nLine ){
162 nLine = nLine*2 + 100;
163 zLine = realloc(zLine, nLine);
164 if( zLine==0 ) return 0;
165 }
drhdaffd0e2001-04-11 14:28:42 +0000166 if( fgets(&zLine[n], nLine - n, in)==0 ){
drh8e7e7a22000-05-30 18:45:23 +0000167 if( n==0 ){
168 free(zLine);
169 return 0;
170 }
171 zLine[n] = 0;
172 eol = 1;
173 break;
174 }
175 while( zLine[n] ){ n++; }
176 if( n>0 && zLine[n-1]=='\n' ){
177 n--;
178 zLine[n] = 0;
179 eol = 1;
180 }
181 }
182 zLine = realloc( zLine, n+1 );
183 return zLine;
184}
185
186/*
187** Retrieve a single line of input text. "isatty" is true if text
188** is coming from a terminal. In that case, we issue a prompt and
189** attempt to use "readline" for command-line editing. If "isatty"
drh9347b202003-07-18 01:30:59 +0000190** is false, use "local_getline" instead of "readline" and issue no prompt.
drh8e7e7a22000-05-30 18:45:23 +0000191**
192** zPrior is a string of prior text retrieved. If not the empty
193** string, then issue a continuation prompt.
194*/
drhdaffd0e2001-04-11 14:28:42 +0000195static char *one_input_line(const char *zPrior, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +0000196 char *zPrompt;
197 char *zResult;
drhdaffd0e2001-04-11 14:28:42 +0000198 if( in!=0 ){
drh9347b202003-07-18 01:30:59 +0000199 return local_getline(0, in);
drh8e7e7a22000-05-30 18:45:23 +0000200 }
201 if( zPrior && zPrior[0] ){
persicom7e2dfdd2002-04-18 02:46:52 +0000202 zPrompt = continuePrompt;
drh8e7e7a22000-05-30 18:45:23 +0000203 }else{
persicom7e2dfdd2002-04-18 02:46:52 +0000204 zPrompt = mainPrompt;
drh8e7e7a22000-05-30 18:45:23 +0000205 }
206 zResult = readline(zPrompt);
danielk19774af00c62005-01-23 23:43:21 +0000207#if defined(HAVE_READLINE) && HAVE_READLINE==1
drheb741d52006-06-03 17:37:25 +0000208 if( zResult && *zResult ) add_history(zResult);
danielk19774af00c62005-01-23 23:43:21 +0000209#endif
drh8e7e7a22000-05-30 18:45:23 +0000210 return zResult;
211}
212
persicom7e2dfdd2002-04-18 02:46:52 +0000213struct previous_mode_data {
214 int valid; /* Is there legit data in here? */
215 int mode;
216 int showHeader;
217 int colWidth[100];
218};
drh8e7e7a22000-05-30 18:45:23 +0000219/*
drh75897232000-05-29 14:26:00 +0000220** An pointer to an instance of this structure is passed from
221** the main program to the callback. This is used to communicate
222** state and mode information.
223*/
224struct callback_data {
danielk197792f9a1b2004-06-19 09:08:16 +0000225 sqlite3 *db; /* The database */
drhdaffd0e2001-04-11 14:28:42 +0000226 int echoOn; /* True to echo input commands */
drh28bd4bc2000-06-15 15:57:22 +0000227 int cnt; /* Number of records displayed so far */
228 FILE *out; /* Write results here */
229 int mode; /* An output mode setting */
230 int showHeader; /* True to show column names in List or Column mode */
drh33048c02001-10-01 14:29:22 +0000231 char *zDestTable; /* Name of destination table when MODE_Insert */
drh28bd4bc2000-06-15 15:57:22 +0000232 char separator[20]; /* Separator character for MODE_List */
drha0c66f52000-07-29 13:20:21 +0000233 int colWidth[100]; /* Requested width of each column when in column mode*/
234 int actualWidth[100]; /* Actual width of each column */
drh83965662003-04-17 02:54:13 +0000235 char nullvalue[20]; /* The text to print when a NULL comes back from
236 ** the database */
persicom7e2dfdd2002-04-18 02:46:52 +0000237 struct previous_mode_data explainPrev;
drh83965662003-04-17 02:54:13 +0000238 /* Holds the mode information just before
239 ** .explain ON */
drh44c2eb12003-04-30 11:38:26 +0000240 char outfile[FILENAME_MAX]; /* Filename for *out */
241 const char *zDbFilename; /* name of the database file */
drh75897232000-05-29 14:26:00 +0000242};
243
244/*
245** These are the allowed modes.
246*/
drh967e8b72000-06-21 13:59:10 +0000247#define MODE_Line 0 /* One column per line. Blank line between records */
drh75897232000-05-29 14:26:00 +0000248#define MODE_Column 1 /* One record per line in neat columns */
249#define MODE_List 2 /* One record per line with a separator */
drhe3710332000-09-29 13:30:53 +0000250#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
251#define MODE_Html 4 /* Generate an XHTML table */
252#define MODE_Insert 5 /* Generate SQL "insert" statements */
drhfeac5f82004-08-01 00:10:45 +0000253#define MODE_Tcl 6 /* Generate ANSI-C or TCL quoted elements */
drh8e64d1c2004-10-07 00:32:39 +0000254#define MODE_Csv 7 /* Quote strings, numbers are plain */
255#define MODE_NUM_OF 8 /* The number of modes (not a mode itself) */
persicom7e2dfdd2002-04-18 02:46:52 +0000256
drh0850b532006-01-31 19:31:43 +0000257static const char *modeDescr[MODE_NUM_OF] = {
persicom7e2dfdd2002-04-18 02:46:52 +0000258 "line",
259 "column",
260 "list",
261 "semi",
262 "html",
drhfeac5f82004-08-01 00:10:45 +0000263 "insert",
264 "tcl",
drh8e64d1c2004-10-07 00:32:39 +0000265 "csv",
persicom7e2dfdd2002-04-18 02:46:52 +0000266};
drh75897232000-05-29 14:26:00 +0000267
268/*
269** Number of elements in an array
270*/
271#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
272
273/*
drh28bd4bc2000-06-15 15:57:22 +0000274** Output the given string as a quoted string using SQL quoting conventions.
275*/
276static void output_quoted_string(FILE *out, const char *z){
277 int i;
278 int nSingle = 0;
drh28bd4bc2000-06-15 15:57:22 +0000279 for(i=0; z[i]; i++){
280 if( z[i]=='\'' ) nSingle++;
drh28bd4bc2000-06-15 15:57:22 +0000281 }
282 if( nSingle==0 ){
283 fprintf(out,"'%s'",z);
drh28bd4bc2000-06-15 15:57:22 +0000284 }else{
285 fprintf(out,"'");
286 while( *z ){
287 for(i=0; z[i] && z[i]!='\''; i++){}
288 if( i==0 ){
289 fprintf(out,"''");
290 z++;
291 }else if( z[i]=='\'' ){
292 fprintf(out,"%.*s''",i,z);
293 z += i+1;
294 }else{
drhcd7d2732002-02-26 23:24:26 +0000295 fprintf(out,"%s",z);
drh28bd4bc2000-06-15 15:57:22 +0000296 break;
297 }
298 }
drhcd7d2732002-02-26 23:24:26 +0000299 fprintf(out,"'");
drh28bd4bc2000-06-15 15:57:22 +0000300 }
301}
302
303/*
drhfeac5f82004-08-01 00:10:45 +0000304** Output the given string as a quoted according to C or TCL quoting rules.
305*/
306static void output_c_string(FILE *out, const char *z){
307 unsigned int c;
308 fputc('"', out);
309 while( (c = *(z++))!=0 ){
310 if( c=='\\' ){
311 fputc(c, out);
312 fputc(c, out);
313 }else if( c=='\t' ){
314 fputc('\\', out);
315 fputc('t', out);
316 }else if( c=='\n' ){
317 fputc('\\', out);
318 fputc('n', out);
319 }else if( c=='\r' ){
320 fputc('\\', out);
321 fputc('r', out);
322 }else if( !isprint(c) ){
drh0a8640d2005-08-30 20:12:02 +0000323 fprintf(out, "\\%03o", c&0xff);
drhfeac5f82004-08-01 00:10:45 +0000324 }else{
325 fputc(c, out);
326 }
327 }
328 fputc('"', out);
329}
330
331/*
drhc08a4f12000-06-15 16:49:48 +0000332** Output the given string with characters that are special to
333** HTML escaped.
334*/
335static void output_html_string(FILE *out, const char *z){
336 int i;
337 while( *z ){
338 for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){}
339 if( i>0 ){
340 fprintf(out,"%.*s",i,z);
341 }
342 if( z[i]=='<' ){
343 fprintf(out,"&lt;");
344 }else if( z[i]=='&' ){
345 fprintf(out,"&amp;");
346 }else{
347 break;
348 }
349 z += i + 1;
350 }
351}
352
353/*
drh8e64d1c2004-10-07 00:32:39 +0000354** Output a single term of CSV. Actually, p->separator is used for
355** the separator, which may or may not be a comma. p->nullvalue is
356** the null value. Strings are quoted using ANSI-C rules. Numbers
357** appear outside of quotes.
358*/
359static void output_csv(struct callback_data *p, const char *z, int bSep){
360 if( z==0 ){
361 fprintf(p->out,"%s",p->nullvalue);
362 }else if( isNumber(z, 0) ){
363 fprintf(p->out,"%s",z);
364 }else{
365 output_c_string(p->out, z);
366 }
367 if( bSep ){
368 fprintf(p->out, p->separator);
369 }
370}
371
danielk19774af00c62005-01-23 23:43:21 +0000372#ifdef SIGINT
drh8e64d1c2004-10-07 00:32:39 +0000373/*
drh4c504392000-10-16 22:06:40 +0000374** This routine runs when the user presses Ctrl-C
375*/
376static void interrupt_handler(int NotUsed){
drh67505e72002-04-19 12:34:06 +0000377 seenInterrupt = 1;
danielk19776f8a5032004-05-10 10:34:51 +0000378 if( db ) sqlite3_interrupt(db);
drh4c504392000-10-16 22:06:40 +0000379}
danielk19774af00c62005-01-23 23:43:21 +0000380#endif
drh4c504392000-10-16 22:06:40 +0000381
382/*
drh75897232000-05-29 14:26:00 +0000383** This is the callback routine that the SQLite library
384** invokes for each row of a query result.
385*/
386static int callback(void *pArg, int nArg, char **azArg, char **azCol){
387 int i;
388 struct callback_data *p = (struct callback_data*)pArg;
389 switch( p->mode ){
390 case MODE_Line: {
drhe3710332000-09-29 13:30:53 +0000391 int w = 5;
drh6a535342001-10-19 16:44:56 +0000392 if( azArg==0 ) break;
drhe3710332000-09-29 13:30:53 +0000393 for(i=0; i<nArg; i++){
drh2cc55692006-06-27 20:39:04 +0000394 int len = strlen(azCol[i] ? azCol[i] : "");
drhe3710332000-09-29 13:30:53 +0000395 if( len>w ) w = len;
396 }
drh75897232000-05-29 14:26:00 +0000397 if( p->cnt++>0 ) fprintf(p->out,"\n");
398 for(i=0; i<nArg; i++){
drh2cc55692006-06-27 20:39:04 +0000399 fprintf(p->out,"%*s = %s\n", w, azCol[i],
drha69d9162003-04-17 22:57:53 +0000400 azArg[i] ? azArg[i] : p->nullvalue);
drh75897232000-05-29 14:26:00 +0000401 }
402 break;
403 }
404 case MODE_Column: {
drha0c66f52000-07-29 13:20:21 +0000405 if( p->cnt++==0 ){
drh75897232000-05-29 14:26:00 +0000406 for(i=0; i<nArg; i++){
drha0c66f52000-07-29 13:20:21 +0000407 int w, n;
408 if( i<ArraySize(p->colWidth) ){
drh75897232000-05-29 14:26:00 +0000409 w = p->colWidth[i];
410 }else{
drha0c66f52000-07-29 13:20:21 +0000411 w = 0;
drh75897232000-05-29 14:26:00 +0000412 }
drha0c66f52000-07-29 13:20:21 +0000413 if( w<=0 ){
drhff6e9112000-08-28 16:21:58 +0000414 w = strlen(azCol[i] ? azCol[i] : "");
drha0c66f52000-07-29 13:20:21 +0000415 if( w<10 ) w = 10;
persicom7e2dfdd2002-04-18 02:46:52 +0000416 n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue);
drha0c66f52000-07-29 13:20:21 +0000417 if( w<n ) w = n;
418 }
419 if( i<ArraySize(p->actualWidth) ){
persicom1d0b8722002-04-18 02:53:04 +0000420 p->actualWidth[i] = w;
drha0c66f52000-07-29 13:20:21 +0000421 }
422 if( p->showHeader ){
423 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
424 }
425 }
426 if( p->showHeader ){
427 for(i=0; i<nArg; i++){
428 int w;
429 if( i<ArraySize(p->actualWidth) ){
430 w = p->actualWidth[i];
431 }else{
432 w = 10;
433 }
434 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
435 "----------------------------------------------------------",
436 i==nArg-1 ? "\n": " ");
437 }
drh75897232000-05-29 14:26:00 +0000438 }
439 }
drh6a535342001-10-19 16:44:56 +0000440 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000441 for(i=0; i<nArg; i++){
442 int w;
drha0c66f52000-07-29 13:20:21 +0000443 if( i<ArraySize(p->actualWidth) ){
444 w = p->actualWidth[i];
drh75897232000-05-29 14:26:00 +0000445 }else{
446 w = 10;
447 }
drhc61053b2000-06-04 12:58:36 +0000448 fprintf(p->out,"%-*.*s%s",w,w,
persicom7e2dfdd2002-04-18 02:46:52 +0000449 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
drh75897232000-05-29 14:26:00 +0000450 }
451 break;
452 }
drhe3710332000-09-29 13:30:53 +0000453 case MODE_Semi:
drh75897232000-05-29 14:26:00 +0000454 case MODE_List: {
455 if( p->cnt++==0 && p->showHeader ){
456 for(i=0; i<nArg; i++){
457 fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
458 }
459 }
drh6a535342001-10-19 16:44:56 +0000460 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000461 for(i=0; i<nArg; i++){
drh4c653a02000-06-07 01:27:47 +0000462 char *z = azArg[i];
persicom7e2dfdd2002-04-18 02:46:52 +0000463 if( z==0 ) z = p->nullvalue;
drh71172c52002-01-24 00:00:21 +0000464 fprintf(p->out, "%s", z);
drhe3710332000-09-29 13:30:53 +0000465 if( i<nArg-1 ){
466 fprintf(p->out, "%s", p->separator);
467 }else if( p->mode==MODE_Semi ){
468 fprintf(p->out, ";\n");
469 }else{
470 fprintf(p->out, "\n");
471 }
drh75897232000-05-29 14:26:00 +0000472 }
473 break;
474 }
drh1e5d0e92000-05-31 23:33:17 +0000475 case MODE_Html: {
476 if( p->cnt++==0 && p->showHeader ){
477 fprintf(p->out,"<TR>");
478 for(i=0; i<nArg; i++){
479 fprintf(p->out,"<TH>%s</TH>",azCol[i]);
480 }
481 fprintf(p->out,"</TR>\n");
482 }
drh6a535342001-10-19 16:44:56 +0000483 if( azArg==0 ) break;
drh28bd4bc2000-06-15 15:57:22 +0000484 fprintf(p->out,"<TR>");
drh1e5d0e92000-05-31 23:33:17 +0000485 for(i=0; i<nArg; i++){
drhc08a4f12000-06-15 16:49:48 +0000486 fprintf(p->out,"<TD>");
persicom7e2dfdd2002-04-18 02:46:52 +0000487 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
drhc08a4f12000-06-15 16:49:48 +0000488 fprintf(p->out,"</TD>\n");
drh1e5d0e92000-05-31 23:33:17 +0000489 }
drh7d686b22002-11-11 13:56:47 +0000490 fprintf(p->out,"</TR>\n");
drh1e5d0e92000-05-31 23:33:17 +0000491 break;
492 }
drhfeac5f82004-08-01 00:10:45 +0000493 case MODE_Tcl: {
494 if( p->cnt++==0 && p->showHeader ){
495 for(i=0; i<nArg; i++){
drh2cc55692006-06-27 20:39:04 +0000496 output_c_string(p->out,azCol[i] ? azCol[i] : "");
drhfeac5f82004-08-01 00:10:45 +0000497 fprintf(p->out, "%s", p->separator);
498 }
499 fprintf(p->out,"\n");
500 }
501 if( azArg==0 ) break;
502 for(i=0; i<nArg; i++){
503 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
504 fprintf(p->out, "%s", p->separator);
505 }
506 fprintf(p->out,"\n");
507 break;
508 }
drh8e64d1c2004-10-07 00:32:39 +0000509 case MODE_Csv: {
510 if( p->cnt++==0 && p->showHeader ){
511 for(i=0; i<nArg; i++){
drh2cc55692006-06-27 20:39:04 +0000512 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
drh8e64d1c2004-10-07 00:32:39 +0000513 }
514 fprintf(p->out,"\n");
515 }
516 if( azArg==0 ) break;
517 for(i=0; i<nArg; i++){
518 output_csv(p, azArg[i], i<nArg-1);
519 }
520 fprintf(p->out,"\n");
521 break;
522 }
drh28bd4bc2000-06-15 15:57:22 +0000523 case MODE_Insert: {
drh6a535342001-10-19 16:44:56 +0000524 if( azArg==0 ) break;
drh33048c02001-10-01 14:29:22 +0000525 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
drh28bd4bc2000-06-15 15:57:22 +0000526 for(i=0; i<nArg; i++){
527 char *zSep = i>0 ? ",": "";
528 if( azArg[i]==0 ){
529 fprintf(p->out,"%sNULL",zSep);
drhc8d74412004-08-31 23:41:26 +0000530 }else if( isNumber(azArg[i], 0) ){
drh28bd4bc2000-06-15 15:57:22 +0000531 fprintf(p->out,"%s%s",zSep, azArg[i]);
532 }else{
533 if( zSep[0] ) fprintf(p->out,"%s",zSep);
534 output_quoted_string(p->out, azArg[i]);
535 }
536 }
537 fprintf(p->out,");\n");
drh6a535342001-10-19 16:44:56 +0000538 break;
drh28bd4bc2000-06-15 15:57:22 +0000539 }
persicom1d0b8722002-04-18 02:53:04 +0000540 }
drh75897232000-05-29 14:26:00 +0000541 return 0;
542}
543
544/*
drh33048c02001-10-01 14:29:22 +0000545** Set the destination table field of the callback_data structure to
546** the name of the table given. Escape any quote characters in the
547** table name.
548*/
549static void set_table_name(struct callback_data *p, const char *zName){
550 int i, n;
551 int needQuote;
552 char *z;
553
554 if( p->zDestTable ){
555 free(p->zDestTable);
556 p->zDestTable = 0;
557 }
558 if( zName==0 ) return;
drh4c755c02004-08-08 20:22:17 +0000559 needQuote = !isalpha((unsigned char)*zName) && *zName!='_';
drh33048c02001-10-01 14:29:22 +0000560 for(i=n=0; zName[i]; i++, n++){
drh4c755c02004-08-08 20:22:17 +0000561 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){
drh33048c02001-10-01 14:29:22 +0000562 needQuote = 1;
563 if( zName[i]=='\'' ) n++;
564 }
565 }
566 if( needQuote ) n += 2;
567 z = p->zDestTable = malloc( n+1 );
568 if( z==0 ){
569 fprintf(stderr,"Out of memory!\n");
570 exit(1);
571 }
572 n = 0;
573 if( needQuote ) z[n++] = '\'';
574 for(i=0; zName[i]; i++){
575 z[n++] = zName[i];
576 if( zName[i]=='\'' ) z[n++] = '\'';
577 }
578 if( needQuote ) z[n++] = '\'';
579 z[n] = 0;
580}
581
danielk19772a02e332004-06-05 08:04:36 +0000582/* zIn is either a pointer to a NULL-terminated string in memory obtained
583** from malloc(), or a NULL pointer. The string pointed to by zAppend is
584** added to zIn, and the result returned in memory obtained from malloc().
585** zIn, if it was not NULL, is freed.
586**
587** If the third argument, quote, is not '\0', then it is used as a
588** quote character for zAppend.
589*/
590static char * appendText(char *zIn, char const *zAppend, char quote){
591 int len;
592 int i;
593 int nAppend = strlen(zAppend);
594 int nIn = (zIn?strlen(zIn):0);
595
596 len = nAppend+nIn+1;
597 if( quote ){
598 len += 2;
599 for(i=0; i<nAppend; i++){
600 if( zAppend[i]==quote ) len++;
601 }
602 }
603
604 zIn = (char *)realloc(zIn, len);
605 if( !zIn ){
606 return 0;
607 }
608
609 if( quote ){
610 char *zCsr = &zIn[nIn];
611 *zCsr++ = quote;
612 for(i=0; i<nAppend; i++){
613 *zCsr++ = zAppend[i];
614 if( zAppend[i]==quote ) *zCsr++ = quote;
615 }
616 *zCsr++ = quote;
617 *zCsr++ = '\0';
618 assert( (zCsr-zIn)==len );
619 }else{
620 memcpy(&zIn[nIn], zAppend, nAppend);
621 zIn[len-1] = '\0';
622 }
623
624 return zIn;
625}
626
drhdd3d4592004-08-30 01:54:05 +0000627
628/*
629** Execute a query statement that has a single result column. Print
630** that result column on a line by itself with a semicolon terminator.
631*/
632static int run_table_dump_query(FILE *out, sqlite3 *db, const char *zSelect){
633 sqlite3_stmt *pSelect;
634 int rc;
635 rc = sqlite3_prepare(db, zSelect, -1, &pSelect, 0);
636 if( rc!=SQLITE_OK || !pSelect ){
637 return rc;
638 }
639 rc = sqlite3_step(pSelect);
640 while( rc==SQLITE_ROW ){
641 fprintf(out, "%s;\n", sqlite3_column_text(pSelect, 0));
642 rc = sqlite3_step(pSelect);
643 }
644 return sqlite3_finalize(pSelect);
645}
646
647
drh33048c02001-10-01 14:29:22 +0000648/*
drh4c653a02000-06-07 01:27:47 +0000649** This is a different callback routine used for dumping the database.
650** Each row received by this callback consists of a table name,
651** the table type ("index" or "table") and SQL to create the table.
652** This routine should print text sufficient to recreate the table.
653*/
654static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
danielk19772a02e332004-06-05 08:04:36 +0000655 int rc;
656 const char *zTable;
657 const char *zType;
658 const char *zSql;
drhdaffd0e2001-04-11 14:28:42 +0000659 struct callback_data *p = (struct callback_data *)pArg;
danielk19772a02e332004-06-05 08:04:36 +0000660
drh4c653a02000-06-07 01:27:47 +0000661 if( nArg!=3 ) return 1;
danielk19772a02e332004-06-05 08:04:36 +0000662 zTable = azArg[0];
663 zType = azArg[1];
664 zSql = azArg[2];
665
drh00b950d2005-09-11 02:03:03 +0000666 if( strcmp(zTable, "sqlite_sequence")==0 ){
drhf8eb96a2005-02-03 00:42:34 +0000667 fprintf(p->out, "DELETE FROM sqlite_sequence;\n");
drh00b950d2005-09-11 02:03:03 +0000668 }else if( strcmp(zTable, "sqlite_stat1")==0 ){
669 fprintf(p->out, "ANALYZE sqlite_master;\n");
670 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
671 return 0;
672 }else{
673 fprintf(p->out, "%s;\n", zSql);
drhf8eb96a2005-02-03 00:42:34 +0000674 }
danielk19772a02e332004-06-05 08:04:36 +0000675
676 if( strcmp(zType, "table")==0 ){
677 sqlite3_stmt *pTableInfo = 0;
danielk19772a02e332004-06-05 08:04:36 +0000678 char *zSelect = 0;
679 char *zTableInfo = 0;
680 char *zTmp = 0;
681
682 zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
683 zTableInfo = appendText(zTableInfo, zTable, '"');
684 zTableInfo = appendText(zTableInfo, ");", 0);
685
686 rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0);
687 if( zTableInfo ) free(zTableInfo);
688 if( rc!=SQLITE_OK || !pTableInfo ){
689 return 1;
690 }
691
692 zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
693 zTmp = appendText(zTmp, zTable, '"');
694 if( zTmp ){
695 zSelect = appendText(zSelect, zTmp, '\'');
696 }
697 zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
698 rc = sqlite3_step(pTableInfo);
699 while( rc==SQLITE_ROW ){
danielk19772e588c72005-12-09 14:25:08 +0000700 const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1);
danielk19773f41e972004-06-08 00:39:01 +0000701 zSelect = appendText(zSelect, "quote(", 0);
danielk19772e588c72005-12-09 14:25:08 +0000702 zSelect = appendText(zSelect, zText, '"');
danielk19772a02e332004-06-05 08:04:36 +0000703 rc = sqlite3_step(pTableInfo);
704 if( rc==SQLITE_ROW ){
705 zSelect = appendText(zSelect, ") || ', ' || ", 0);
706 }else{
707 zSelect = appendText(zSelect, ") ", 0);
708 }
709 }
710 rc = sqlite3_finalize(pTableInfo);
711 if( rc!=SQLITE_OK ){
712 if( zSelect ) free(zSelect);
713 return 1;
714 }
715 zSelect = appendText(zSelect, "|| ')' FROM ", 0);
716 zSelect = appendText(zSelect, zTable, '"');
717
drhdd3d4592004-08-30 01:54:05 +0000718 rc = run_table_dump_query(p->out, p->db, zSelect);
719 if( rc==SQLITE_CORRUPT ){
720 zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
721 rc = run_table_dump_query(p->out, p->db, zSelect);
722 }
danielk19772a02e332004-06-05 08:04:36 +0000723 if( zSelect ) free(zSelect);
danielk19772a02e332004-06-05 08:04:36 +0000724 if( rc!=SQLITE_OK ){
725 return 1;
726 }
drh4c653a02000-06-07 01:27:47 +0000727 }
drh4c653a02000-06-07 01:27:47 +0000728 return 0;
729}
730
731/*
drhdd3d4592004-08-30 01:54:05 +0000732** Run zQuery. Update dump_callback() as the callback routine.
733** If we get a SQLITE_CORRUPT error, rerun the query after appending
734** "ORDER BY rowid DESC" to the end.
735*/
736static int run_schema_dump_query(
737 struct callback_data *p,
738 const char *zQuery,
739 char **pzErrMsg
740){
741 int rc;
742 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, pzErrMsg);
743 if( rc==SQLITE_CORRUPT ){
744 char *zQ2;
745 int len = strlen(zQuery);
746 if( pzErrMsg ) sqlite3_free(*pzErrMsg);
747 zQ2 = malloc( len+100 );
748 if( zQ2==0 ) return rc;
749 sprintf(zQ2, "%s ORDER BY rowid DESC", zQuery);
750 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, pzErrMsg);
751 free(zQ2);
752 }
753 return rc;
754}
755
756/*
drh75897232000-05-29 14:26:00 +0000757** Text of a help message
758*/
persicom1d0b8722002-04-18 02:53:04 +0000759static char zHelp[] =
jplyon6a65bb32003-05-04 07:25:57 +0000760 ".databases List names and files of attached databases\n"
drhb860bc92004-08-04 15:16:55 +0000761 ".dump ?TABLE? ... Dump the database in an SQL text format\n"
drhdaffd0e2001-04-11 14:28:42 +0000762 ".echo ON|OFF Turn command echo on or off\n"
drh75897232000-05-29 14:26:00 +0000763 ".exit Exit this program\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000764 ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000765 ".header(s) ON|OFF Turn display of headers on or off\n"
drh75897232000-05-29 14:26:00 +0000766 ".help Show this message\n"
drhb860bc92004-08-04 15:16:55 +0000767 ".import FILE TABLE Import data from FILE into TABLE\n"
drh75897232000-05-29 14:26:00 +0000768 ".indices TABLE Show names of all indices on TABLE\n"
drh70df4fe2006-06-13 15:12:21 +0000769#ifndef SQLITE_OMIT_LOAD_EXTENSION
drh1e397f82006-06-08 15:28:43 +0000770 ".load FILE ?ENTRY? Load an extension library\n"
drh70df4fe2006-06-13 15:12:21 +0000771#endif
danielk19776b77a362005-01-13 11:10:25 +0000772 ".mode MODE ?TABLE? Set output mode where MODE is one of:\n"
drh3b584fa2004-09-24 12:50:03 +0000773 " csv Comma-separated values\n"
drhb860bc92004-08-04 15:16:55 +0000774 " column Left-aligned columns. (See .width)\n"
775 " html HTML <table> code\n"
776 " insert SQL insert statements for TABLE\n"
777 " line One value per line\n"
778 " list Values delimited by .separator string\n"
779 " tabs Tab-separated values\n"
780 " tcl TCL list elements\n"
781 ".nullvalue STRING Print STRING in place of NULL values\n"
drh75897232000-05-29 14:26:00 +0000782 ".output FILENAME Send output to FILENAME\n"
783 ".output stdout Send output to the screen\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000784 ".prompt MAIN CONTINUE Replace the standard prompts\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000785 ".quit Exit this program\n"
drhdaffd0e2001-04-11 14:28:42 +0000786 ".read FILENAME Execute SQL in FILENAME\n"
drh75897232000-05-29 14:26:00 +0000787 ".schema ?TABLE? Show the CREATE statements\n"
drhb860bc92004-08-04 15:16:55 +0000788 ".separator STRING Change separator used by output mode and .import\n"
drhdd45df82002-04-18 12:39:03 +0000789 ".show Show the current values for various settings\n"
drhfeac5f82004-08-01 00:10:45 +0000790 ".tables ?PATTERN? List names of tables matching a LIKE pattern\n"
drh2dfbbca2000-07-28 14:32:48 +0000791 ".timeout MS Try opening locked tables for MS milliseconds\n"
drh75897232000-05-29 14:26:00 +0000792 ".width NUM NUM ... Set column widths for \"column\" mode\n"
793;
794
drhdaffd0e2001-04-11 14:28:42 +0000795/* Forward reference */
796static void process_input(struct callback_data *p, FILE *in);
797
drh75897232000-05-29 14:26:00 +0000798/*
drh44c2eb12003-04-30 11:38:26 +0000799** Make sure the database is open. If it is not, then open it. If
800** the database fails to open, print an error message and exit.
801*/
802static void open_db(struct callback_data *p){
803 if( p->db==0 ){
danielk19774f057f92004-06-08 00:02:33 +0000804 sqlite3_open(p->zDbFilename, &p->db);
danielk197780290862004-05-22 09:21:21 +0000805 db = p->db;
danielk1977bc6ada42004-06-30 08:20:16 +0000806 sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0,
807 shellstaticFunc, 0, 0);
danielk197780290862004-05-22 09:21:21 +0000808 if( SQLITE_OK!=sqlite3_errcode(db) ){
809 fprintf(stderr,"Unable to open database \"%s\": %s\n",
810 p->zDbFilename, sqlite3_errmsg(db));
drh22fbcb82004-02-01 01:22:50 +0000811 exit(1);
drh44c2eb12003-04-30 11:38:26 +0000812 }
drhc2e87a32006-06-27 15:16:14 +0000813#ifndef SQLITE_OMIT_LOAD_EXTENSION
814 sqlite3_enable_load_extension(p->db, 1);
815#endif
drh44c2eb12003-04-30 11:38:26 +0000816 }
817}
818
819/*
drhfeac5f82004-08-01 00:10:45 +0000820** Do C-language style dequoting.
821**
822** \t -> tab
823** \n -> newline
824** \r -> carriage return
825** \NNN -> ascii character NNN in octal
826** \\ -> backslash
827*/
828static void resolve_backslashes(char *z){
829 int i, j, c;
830 for(i=j=0; (c = z[i])!=0; i++, j++){
831 if( c=='\\' ){
832 c = z[++i];
833 if( c=='n' ){
834 c = '\n';
835 }else if( c=='t' ){
836 c = '\t';
837 }else if( c=='r' ){
838 c = '\r';
839 }else if( c>='0' && c<='7' ){
drhaa816082005-12-29 12:53:09 +0000840 c -= '0';
drhfeac5f82004-08-01 00:10:45 +0000841 if( z[i+1]>='0' && z[i+1]<='7' ){
842 i++;
843 c = (c<<3) + z[i] - '0';
844 if( z[i+1]>='0' && z[i+1]<='7' ){
845 i++;
846 c = (c<<3) + z[i] - '0';
847 }
848 }
849 }
850 }
851 z[j] = c;
852 }
853 z[j] = 0;
854}
855
856/*
drh75897232000-05-29 14:26:00 +0000857** If an input line begins with "." then invoke this routine to
858** process that line.
drh67505e72002-04-19 12:34:06 +0000859**
860** Return 1 to exit and 0 to continue.
drh75897232000-05-29 14:26:00 +0000861*/
drh44c2eb12003-04-30 11:38:26 +0000862static int do_meta_command(char *zLine, struct callback_data *p){
drh75897232000-05-29 14:26:00 +0000863 int i = 1;
864 int nArg = 0;
865 int n, c;
drh67505e72002-04-19 12:34:06 +0000866 int rc = 0;
drh75897232000-05-29 14:26:00 +0000867 char *azArg[50];
868
869 /* Parse the input line into tokens.
870 */
871 while( zLine[i] && nArg<ArraySize(azArg) ){
drh4c755c02004-08-08 20:22:17 +0000872 while( isspace((unsigned char)zLine[i]) ){ i++; }
drh06333682004-03-09 13:37:45 +0000873 if( zLine[i]==0 ) break;
drh75897232000-05-29 14:26:00 +0000874 if( zLine[i]=='\'' || zLine[i]=='"' ){
875 int delim = zLine[i++];
876 azArg[nArg++] = &zLine[i];
877 while( zLine[i] && zLine[i]!=delim ){ i++; }
878 if( zLine[i]==delim ){
879 zLine[i++] = 0;
880 }
drhfeac5f82004-08-01 00:10:45 +0000881 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
drh75897232000-05-29 14:26:00 +0000882 }else{
883 azArg[nArg++] = &zLine[i];
drh4c755c02004-08-08 20:22:17 +0000884 while( zLine[i] && !isspace((unsigned char)zLine[i]) ){ i++; }
drh75897232000-05-29 14:26:00 +0000885 if( zLine[i] ) zLine[i++] = 0;
drhfeac5f82004-08-01 00:10:45 +0000886 resolve_backslashes(azArg[nArg-1]);
drh75897232000-05-29 14:26:00 +0000887 }
888 }
889
890 /* Process the input line.
891 */
drh67505e72002-04-19 12:34:06 +0000892 if( nArg==0 ) return rc;
drh75897232000-05-29 14:26:00 +0000893 n = strlen(azArg[0]);
894 c = azArg[0][0];
jplyon6a65bb32003-05-04 07:25:57 +0000895 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
jplyon672a1ed2003-05-11 20:07:05 +0000896 struct callback_data data;
897 char *zErrMsg = 0;
jplyon6a65bb32003-05-04 07:25:57 +0000898 open_db(p);
jplyon672a1ed2003-05-11 20:07:05 +0000899 memcpy(&data, p, sizeof(data));
drhd8885442004-03-17 23:42:12 +0000900 data.showHeader = 1;
jplyon672a1ed2003-05-11 20:07:05 +0000901 data.mode = MODE_Column;
drhd8885442004-03-17 23:42:12 +0000902 data.colWidth[0] = 3;
903 data.colWidth[1] = 15;
904 data.colWidth[2] = 58;
drh0b2110c2004-10-26 00:08:10 +0000905 data.cnt = 0;
danielk19776f8a5032004-05-10 10:34:51 +0000906 sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
jplyon672a1ed2003-05-11 20:07:05 +0000907 if( zErrMsg ){
908 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000909 sqlite3_free(zErrMsg);
jplyon6a65bb32003-05-04 07:25:57 +0000910 }
911 }else
912
drh4c653a02000-06-07 01:27:47 +0000913 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
914 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000915 open_db(p);
drh33048c02001-10-01 14:29:22 +0000916 fprintf(p->out, "BEGIN TRANSACTION;\n");
drh4c653a02000-06-07 01:27:47 +0000917 if( nArg==1 ){
drhdd3d4592004-08-30 01:54:05 +0000918 run_schema_dump_query(p,
drha18c5682000-10-08 22:20:57 +0000919 "SELECT name, type, sql FROM sqlite_master "
drh0b9a5942006-09-13 20:22:02 +0000920 "WHERE sql NOT NULL AND type=='table' AND rootpage!=0", 0
drhdd3d4592004-08-30 01:54:05 +0000921 );
922 run_schema_dump_query(p,
923 "SELECT name, type, sql FROM sqlite_master "
drh0b9a5942006-09-13 20:22:02 +0000924 "WHERE sql NOT NULL AND "
925 " AND type!='table' AND type!='meta'", 0
926 );
927 run_table_dump_query(p->out, p->db,
928 "SELECT sql FROM sqlite_master "
929 "WHERE sql NOT NULL AND rootpage==0 AND type='table'"
drha18c5682000-10-08 22:20:57 +0000930 );
drh4c653a02000-06-07 01:27:47 +0000931 }else{
932 int i;
drhdd3d4592004-08-30 01:54:05 +0000933 for(i=1; i<nArg; i++){
danielk1977bc6ada42004-06-30 08:20:16 +0000934 zShellStatic = azArg[i];
drhdd3d4592004-08-30 01:54:05 +0000935 run_schema_dump_query(p,
drha18c5682000-10-08 22:20:57 +0000936 "SELECT name, type, sql FROM sqlite_master "
drhdd3d4592004-08-30 01:54:05 +0000937 "WHERE tbl_name LIKE shellstatic() AND type=='table'"
drh0b9a5942006-09-13 20:22:02 +0000938 " AND rootpage!=0 AND sql NOT NULL", 0);
drhdd3d4592004-08-30 01:54:05 +0000939 run_schema_dump_query(p,
940 "SELECT name, type, sql FROM sqlite_master "
941 "WHERE tbl_name LIKE shellstatic() AND type!='table'"
942 " AND type!='meta' AND sql NOT NULL", 0);
drh0b9a5942006-09-13 20:22:02 +0000943 run_table_dump_query(p->out, p->db,
944 "SELECT sql FROM sqlite_master "
945 "WHERE sql NOT NULL AND rootpage==0 AND type='table'"
946 " AND tbl_name LIKE shellstatic()"
947 );
danielk1977bc6ada42004-06-30 08:20:16 +0000948 zShellStatic = 0;
drh4c653a02000-06-07 01:27:47 +0000949 }
950 }
951 if( zErrMsg ){
952 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000953 sqlite3_free(zErrMsg);
drh33048c02001-10-01 14:29:22 +0000954 }else{
955 fprintf(p->out, "COMMIT;\n");
drh4c653a02000-06-07 01:27:47 +0000956 }
957 }else
drh75897232000-05-29 14:26:00 +0000958
drhdaffd0e2001-04-11 14:28:42 +0000959 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
960 int j;
961 char *z = azArg[1];
962 int val = atoi(azArg[1]);
963 for(j=0; z[j]; j++){
drh4c755c02004-08-08 20:22:17 +0000964 z[j] = tolower((unsigned char)z[j]);
drhdaffd0e2001-04-11 14:28:42 +0000965 }
966 if( strcmp(z,"on")==0 ){
967 val = 1;
968 }else if( strcmp(z,"yes")==0 ){
969 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000970 }
drhdaffd0e2001-04-11 14:28:42 +0000971 p->echoOn = val;
972 }else
973
drh75897232000-05-29 14:26:00 +0000974 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
drh67505e72002-04-19 12:34:06 +0000975 rc = 1;
drh75897232000-05-29 14:26:00 +0000976 }else
977
drhdd45df82002-04-18 12:39:03 +0000978 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +0000979 int j;
drh472cbf62004-08-14 18:18:44 +0000980 static char zOne[] = "1";
981 char *z = nArg>=2 ? azArg[1] : zOne;
drhdd45df82002-04-18 12:39:03 +0000982 int val = atoi(z);
persicom7e2dfdd2002-04-18 02:46:52 +0000983 for(j=0; z[j]; j++){
drh4c755c02004-08-08 20:22:17 +0000984 z[j] = tolower((unsigned char)z[j]);
persicom7e2dfdd2002-04-18 02:46:52 +0000985 }
986 if( strcmp(z,"on")==0 ){
987 val = 1;
988 }else if( strcmp(z,"yes")==0 ){
989 val = 1;
990 }
991 if(val == 1) {
992 if(!p->explainPrev.valid) {
993 p->explainPrev.valid = 1;
994 p->explainPrev.mode = p->mode;
995 p->explainPrev.showHeader = p->showHeader;
996 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
997 }
998 /* We could put this code under the !p->explainValid
999 ** condition so that it does not execute if we are already in
1000 ** explain mode. However, always executing it allows us an easy
1001 ** was to reset to explain mode in case the user previously
1002 ** did an .explain followed by a .width, .mode or .header
1003 ** command.
1004 */
1005 p->mode = MODE_Column;
1006 p->showHeader = 1;
1007 memset(p->colWidth,0,ArraySize(p->colWidth));
1008 p->colWidth[0] = 4;
drhe69cc5b2005-08-27 01:50:53 +00001009 p->colWidth[1] = 14;
persicom7e2dfdd2002-04-18 02:46:52 +00001010 p->colWidth[2] = 10;
1011 p->colWidth[3] = 10;
drhe69cc5b2005-08-27 01:50:53 +00001012 p->colWidth[4] = 33;
persicom7e2dfdd2002-04-18 02:46:52 +00001013 }else if (p->explainPrev.valid) {
1014 p->explainPrev.valid = 0;
1015 p->mode = p->explainPrev.mode;
1016 p->showHeader = p->explainPrev.showHeader;
1017 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
1018 }
drh75897232000-05-29 14:26:00 +00001019 }else
1020
persicom7e2dfdd2002-04-18 02:46:52 +00001021 if( c=='h' && (strncmp(azArg[0], "header", n)==0
1022 ||
1023 strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){
drh75897232000-05-29 14:26:00 +00001024 int j;
1025 char *z = azArg[1];
1026 int val = atoi(azArg[1]);
1027 for(j=0; z[j]; j++){
drh4c755c02004-08-08 20:22:17 +00001028 z[j] = tolower((unsigned char)z[j]);
drh75897232000-05-29 14:26:00 +00001029 }
1030 if( strcmp(z,"on")==0 ){
1031 val = 1;
1032 }else if( strcmp(z,"yes")==0 ){
1033 val = 1;
persicom1d0b8722002-04-18 02:53:04 +00001034 }
drh75897232000-05-29 14:26:00 +00001035 p->showHeader = val;
1036 }else
1037
1038 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
1039 fprintf(stderr,zHelp);
1040 }else
1041
drhfeac5f82004-08-01 00:10:45 +00001042 if( c=='i' && strncmp(azArg[0], "import", n)==0 && nArg>=3 ){
1043 char *zTable = azArg[2]; /* Insert data into this table */
1044 char *zFile = azArg[1]; /* The file from which to extract data */
1045 sqlite3_stmt *pStmt; /* A statement */
1046 int rc; /* Result code */
1047 int nCol; /* Number of columns in the table */
1048 int nByte; /* Number of bytes in an SQL string */
1049 int i, j; /* Loop counters */
1050 int nSep; /* Number of bytes in p->separator[] */
1051 char *zSql; /* An SQL statement */
1052 char *zLine; /* A single line of input from the file */
1053 char **azCol; /* zLine[] broken up into columns */
1054 char *zCommit; /* How to commit changes */
drhb860bc92004-08-04 15:16:55 +00001055 FILE *in; /* The input file */
1056 int lineno = 0; /* Line number of input file */
drhfeac5f82004-08-01 00:10:45 +00001057
drha543c822006-06-08 16:10:14 +00001058 open_db(p);
drhfeac5f82004-08-01 00:10:45 +00001059 nSep = strlen(p->separator);
1060 if( nSep==0 ){
1061 fprintf(stderr, "non-null separator required for import\n");
1062 return 0;
1063 }
1064 zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
1065 if( zSql==0 ) return 0;
1066 nByte = strlen(zSql);
drh5e6078b2006-01-31 19:07:22 +00001067 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
drhfeac5f82004-08-01 00:10:45 +00001068 sqlite3_free(zSql);
1069 if( rc ){
1070 fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
1071 nCol = 0;
1072 }else{
1073 nCol = sqlite3_column_count(pStmt);
1074 }
1075 sqlite3_finalize(pStmt);
1076 if( nCol==0 ) return 0;
1077 zSql = malloc( nByte + 20 + nCol*2 );
1078 if( zSql==0 ) return 0;
1079 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO '%q' VALUES(?", zTable);
1080 j = strlen(zSql);
1081 for(i=1; i<nCol; i++){
1082 zSql[j++] = ',';
1083 zSql[j++] = '?';
1084 }
1085 zSql[j++] = ')';
1086 zSql[j] = 0;
drh5e6078b2006-01-31 19:07:22 +00001087 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
drhfeac5f82004-08-01 00:10:45 +00001088 free(zSql);
1089 if( rc ){
1090 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
1091 sqlite3_finalize(pStmt);
1092 return 0;
1093 }
1094 in = fopen(zFile, "rb");
1095 if( in==0 ){
1096 fprintf(stderr, "cannot open file: %s\n", zFile);
1097 sqlite3_finalize(pStmt);
1098 return 0;
1099 }
1100 azCol = malloc( sizeof(azCol[0])*(nCol+1) );
drh43617e92006-03-06 20:55:46 +00001101 if( azCol==0 ){
1102 fclose(in);
1103 return 0;
1104 }
drhfeac5f82004-08-01 00:10:45 +00001105 sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
1106 zCommit = "COMMIT";
1107 while( (zLine = local_getline(0, in))!=0 ){
1108 char *z;
1109 i = 0;
drhb860bc92004-08-04 15:16:55 +00001110 lineno++;
drhfeac5f82004-08-01 00:10:45 +00001111 azCol[0] = zLine;
drh36d4e972004-10-06 14:39:06 +00001112 for(i=0, z=zLine; *z && *z!='\n' && *z!='\r'; z++){
drhfeac5f82004-08-01 00:10:45 +00001113 if( *z==p->separator[0] && strncmp(z, p->separator, nSep)==0 ){
1114 *z = 0;
1115 i++;
drhb860bc92004-08-04 15:16:55 +00001116 if( i<nCol ){
1117 azCol[i] = &z[nSep];
1118 z += nSep-1;
1119 }
drhfeac5f82004-08-01 00:10:45 +00001120 }
1121 }
drh1cd7f832005-08-05 18:50:51 +00001122 *z = 0;
drhb860bc92004-08-04 15:16:55 +00001123 if( i+1!=nCol ){
1124 fprintf(stderr,"%s line %d: expected %d columns of data but found %d\n",
1125 zFile, lineno, nCol, i+1);
1126 zCommit = "ROLLBACK";
1127 break;
1128 }
drhfeac5f82004-08-01 00:10:45 +00001129 for(i=0; i<nCol; i++){
1130 sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
1131 }
1132 sqlite3_step(pStmt);
1133 rc = sqlite3_reset(pStmt);
1134 free(zLine);
1135 if( rc!=SQLITE_OK ){
1136 fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
1137 zCommit = "ROLLBACK";
1138 break;
1139 }
1140 }
1141 free(azCol);
1142 fclose(in);
1143 sqlite3_finalize(pStmt);
drhb860bc92004-08-04 15:16:55 +00001144 sqlite3_exec(p->db, zCommit, 0, 0, 0);
drhfeac5f82004-08-01 00:10:45 +00001145 }else
1146
drh75897232000-05-29 14:26:00 +00001147 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
1148 struct callback_data data;
1149 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +00001150 open_db(p);
drh75897232000-05-29 14:26:00 +00001151 memcpy(&data, p, sizeof(data));
1152 data.showHeader = 0;
1153 data.mode = MODE_List;
danielk1977bc6ada42004-06-30 08:20:16 +00001154 zShellStatic = azArg[1];
1155 sqlite3_exec(p->db,
drha18c5682000-10-08 22:20:57 +00001156 "SELECT name FROM sqlite_master "
danielk1977bc6ada42004-06-30 08:20:16 +00001157 "WHERE type='index' AND tbl_name LIKE shellstatic() "
drhe0bc4042002-06-25 01:09:11 +00001158 "UNION ALL "
1159 "SELECT name FROM sqlite_temp_master "
danielk1977bc6ada42004-06-30 08:20:16 +00001160 "WHERE type='index' AND tbl_name LIKE shellstatic() "
drhe0bc4042002-06-25 01:09:11 +00001161 "ORDER BY 1",
danielk1977bc6ada42004-06-30 08:20:16 +00001162 callback, &data, &zErrMsg
drha18c5682000-10-08 22:20:57 +00001163 );
danielk1977bc6ada42004-06-30 08:20:16 +00001164 zShellStatic = 0;
drh75897232000-05-29 14:26:00 +00001165 if( zErrMsg ){
1166 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001167 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00001168 }
1169 }else
1170
drh70df4fe2006-06-13 15:12:21 +00001171#ifndef SQLITE_OMIT_LOAD_EXTENSION
drh1e397f82006-06-08 15:28:43 +00001172 if( c=='l' && strncmp(azArg[0], "load", n)==0 && nArg>=2 ){
1173 const char *zFile, *zProc;
1174 char *zErrMsg = 0;
1175 int rc;
1176 zFile = azArg[1];
1177 zProc = nArg>=3 ? azArg[2] : 0;
1178 open_db(p);
1179 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
1180 if( rc!=SQLITE_OK ){
1181 fprintf(stderr, "%s\n", zErrMsg);
1182 sqlite3_free(zErrMsg);
1183 }
1184 }else
drh70df4fe2006-06-13 15:12:21 +00001185#endif
drh1e397f82006-06-08 15:28:43 +00001186
drh28bd4bc2000-06-15 15:57:22 +00001187 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
drh75897232000-05-29 14:26:00 +00001188 int n2 = strlen(azArg[1]);
persicom7e2dfdd2002-04-18 02:46:52 +00001189 if( strncmp(azArg[1],"line",n2)==0
1190 ||
1191 strncmp(azArg[1],"lines",n2)==0 ){
drh75897232000-05-29 14:26:00 +00001192 p->mode = MODE_Line;
persicom7e2dfdd2002-04-18 02:46:52 +00001193 }else if( strncmp(azArg[1],"column",n2)==0
1194 ||
1195 strncmp(azArg[1],"columns",n2)==0 ){
drh75897232000-05-29 14:26:00 +00001196 p->mode = MODE_Column;
1197 }else if( strncmp(azArg[1],"list",n2)==0 ){
1198 p->mode = MODE_List;
drh1e5d0e92000-05-31 23:33:17 +00001199 }else if( strncmp(azArg[1],"html",n2)==0 ){
1200 p->mode = MODE_Html;
drhfeac5f82004-08-01 00:10:45 +00001201 }else if( strncmp(azArg[1],"tcl",n2)==0 ){
1202 p->mode = MODE_Tcl;
1203 }else if( strncmp(azArg[1],"csv",n2)==0 ){
drh8e64d1c2004-10-07 00:32:39 +00001204 p->mode = MODE_Csv;
drhfeac5f82004-08-01 00:10:45 +00001205 strcpy(p->separator, ",");
1206 }else if( strncmp(azArg[1],"tabs",n2)==0 ){
1207 p->mode = MODE_List;
1208 strcpy(p->separator, "\t");
drh28bd4bc2000-06-15 15:57:22 +00001209 }else if( strncmp(azArg[1],"insert",n2)==0 ){
1210 p->mode = MODE_Insert;
1211 if( nArg>=3 ){
drh33048c02001-10-01 14:29:22 +00001212 set_table_name(p, azArg[2]);
drh28bd4bc2000-06-15 15:57:22 +00001213 }else{
drh33048c02001-10-01 14:29:22 +00001214 set_table_name(p, "table");
drh28bd4bc2000-06-15 15:57:22 +00001215 }
drhdaffd0e2001-04-11 14:28:42 +00001216 }else {
drhfeac5f82004-08-01 00:10:45 +00001217 fprintf(stderr,"mode should be on of: "
1218 "column csv html insert line list tabs tcl\n");
drh75897232000-05-29 14:26:00 +00001219 }
1220 }else
1221
persicom7e2dfdd2002-04-18 02:46:52 +00001222 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
1223 sprintf(p->nullvalue, "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
1224 }else
1225
drh75897232000-05-29 14:26:00 +00001226 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
1227 if( p->out!=stdout ){
1228 fclose(p->out);
1229 }
1230 if( strcmp(azArg[1],"stdout")==0 ){
1231 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +00001232 strcpy(p->outfile,"stdout");
drh75897232000-05-29 14:26:00 +00001233 }else{
drha1f9b5e2004-02-14 16:31:02 +00001234 p->out = fopen(azArg[1], "wb");
drh75897232000-05-29 14:26:00 +00001235 if( p->out==0 ){
1236 fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
1237 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +00001238 } else {
1239 strcpy(p->outfile,azArg[1]);
drh75897232000-05-29 14:26:00 +00001240 }
1241 }
1242 }else
1243
drhdd45df82002-04-18 12:39:03 +00001244 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
persicom7e2dfdd2002-04-18 02:46:52 +00001245 if( nArg >= 2) {
1246 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
1247 }
1248 if( nArg >= 3) {
1249 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
1250 }
1251 }else
1252
1253 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
drhf73287c2004-02-25 02:25:37 +00001254 rc = 1;
persicom7e2dfdd2002-04-18 02:46:52 +00001255 }else
1256
drhdaffd0e2001-04-11 14:28:42 +00001257 if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
drha1f9b5e2004-02-14 16:31:02 +00001258 FILE *alt = fopen(azArg[1], "rb");
drhdaffd0e2001-04-11 14:28:42 +00001259 if( alt==0 ){
1260 fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
1261 }else{
1262 process_input(p, alt);
1263 fclose(alt);
1264 }
1265 }else
1266
drh75897232000-05-29 14:26:00 +00001267 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
1268 struct callback_data data;
1269 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +00001270 open_db(p);
drh75897232000-05-29 14:26:00 +00001271 memcpy(&data, p, sizeof(data));
1272 data.showHeader = 0;
drhe3710332000-09-29 13:30:53 +00001273 data.mode = MODE_Semi;
drh75897232000-05-29 14:26:00 +00001274 if( nArg>1 ){
drhc8d74412004-08-31 23:41:26 +00001275 int i;
1276 for(i=0; azArg[1][i]; i++) azArg[1][i] = tolower(azArg[1][i]);
1277 if( strcmp(azArg[1],"sqlite_master")==0 ){
drha18c5682000-10-08 22:20:57 +00001278 char *new_argv[2], *new_colv[2];
1279 new_argv[0] = "CREATE TABLE sqlite_master (\n"
1280 " type text,\n"
1281 " name text,\n"
1282 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +00001283 " rootpage integer,\n"
drha18c5682000-10-08 22:20:57 +00001284 " sql text\n"
1285 ")";
1286 new_argv[1] = 0;
1287 new_colv[0] = "sql";
1288 new_colv[1] = 0;
1289 callback(&data, 1, new_argv, new_colv);
drhc8d74412004-08-31 23:41:26 +00001290 }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){
drhe0bc4042002-06-25 01:09:11 +00001291 char *new_argv[2], *new_colv[2];
1292 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
1293 " type text,\n"
1294 " name text,\n"
1295 " tbl_name text,\n"
1296 " rootpage integer,\n"
1297 " sql text\n"
1298 ")";
1299 new_argv[1] = 0;
1300 new_colv[0] = "sql";
1301 new_colv[1] = 0;
1302 callback(&data, 1, new_argv, new_colv);
drha18c5682000-10-08 22:20:57 +00001303 }else{
danielk1977bc6ada42004-06-30 08:20:16 +00001304 zShellStatic = azArg[1];
1305 sqlite3_exec(p->db,
drhe0bc4042002-06-25 01:09:11 +00001306 "SELECT sql FROM "
1307 " (SELECT * FROM sqlite_master UNION ALL"
1308 " SELECT * FROM sqlite_temp_master) "
danielk1977bc6ada42004-06-30 08:20:16 +00001309 "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +00001310 "ORDER BY substr(type,2,1), name",
danielk1977bc6ada42004-06-30 08:20:16 +00001311 callback, &data, &zErrMsg);
1312 zShellStatic = 0;
drha18c5682000-10-08 22:20:57 +00001313 }
drh75897232000-05-29 14:26:00 +00001314 }else{
danielk19776f8a5032004-05-10 10:34:51 +00001315 sqlite3_exec(p->db,
drhe0bc4042002-06-25 01:09:11 +00001316 "SELECT sql FROM "
1317 " (SELECT * FROM sqlite_master UNION ALL"
1318 " SELECT * FROM sqlite_temp_master) "
drh0c356672005-09-10 22:40:53 +00001319 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
drhe0bc4042002-06-25 01:09:11 +00001320 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +00001321 callback, &data, &zErrMsg
1322 );
drh75897232000-05-29 14:26:00 +00001323 }
drh75897232000-05-29 14:26:00 +00001324 if( zErrMsg ){
1325 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001326 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00001327 }
1328 }else
1329
1330 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
1331 sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
1332 }else
1333
persicom7e2dfdd2002-04-18 02:46:52 +00001334 if( c=='s' && strncmp(azArg[0], "show", n)==0){
1335 int i;
1336 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
drh67505e72002-04-19 12:34:06 +00001337 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
drhdd45df82002-04-18 12:39:03 +00001338 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
persicom7e2dfdd2002-04-18 02:46:52 +00001339 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
drhfeac5f82004-08-01 00:10:45 +00001340 fprintf(p->out,"%9.9s: ", "nullvalue");
1341 output_c_string(p->out, p->nullvalue);
1342 fprintf(p->out, "\n");
drh67505e72002-04-19 12:34:06 +00001343 fprintf(p->out,"%9.9s: %s\n","output",
1344 strlen(p->outfile) ? p->outfile : "stdout");
drhfeac5f82004-08-01 00:10:45 +00001345 fprintf(p->out,"%9.9s: ", "separator");
1346 output_c_string(p->out, p->separator);
1347 fprintf(p->out, "\n");
persicom7e2dfdd2002-04-18 02:46:52 +00001348 fprintf(p->out,"%9.9s: ","width");
1349 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
drhfeac5f82004-08-01 00:10:45 +00001350 fprintf(p->out,"%d ",p->colWidth[i]);
persicom7e2dfdd2002-04-18 02:46:52 +00001351 }
drhfeac5f82004-08-01 00:10:45 +00001352 fprintf(p->out,"\n");
persicom7e2dfdd2002-04-18 02:46:52 +00001353 }else
1354
drh2dfbbca2000-07-28 14:32:48 +00001355 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
drhe3710332000-09-29 13:30:53 +00001356 char **azResult;
1357 int nRow, rc;
1358 char *zErrMsg;
drh44c2eb12003-04-30 11:38:26 +00001359 open_db(p);
drha50da102000-08-08 20:19:09 +00001360 if( nArg==1 ){
danielk19776f8a5032004-05-10 10:34:51 +00001361 rc = sqlite3_get_table(p->db,
drha50da102000-08-08 20:19:09 +00001362 "SELECT name FROM sqlite_master "
drh0c356672005-09-10 22:40:53 +00001363 "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'"
drhe0bc4042002-06-25 01:09:11 +00001364 "UNION ALL "
1365 "SELECT name FROM sqlite_temp_master "
1366 "WHERE type IN ('table','view') "
1367 "ORDER BY 1",
drha18c5682000-10-08 22:20:57 +00001368 &azResult, &nRow, 0, &zErrMsg
1369 );
drha50da102000-08-08 20:19:09 +00001370 }else{
danielk1977bc6ada42004-06-30 08:20:16 +00001371 zShellStatic = azArg[1];
1372 rc = sqlite3_get_table(p->db,
drha50da102000-08-08 20:19:09 +00001373 "SELECT name FROM sqlite_master "
danielk1977bc6ada42004-06-30 08:20:16 +00001374 "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' "
drhe0bc4042002-06-25 01:09:11 +00001375 "UNION ALL "
1376 "SELECT name FROM sqlite_temp_master "
danielk1977bc6ada42004-06-30 08:20:16 +00001377 "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' "
drhe0bc4042002-06-25 01:09:11 +00001378 "ORDER BY 1",
danielk1977bc6ada42004-06-30 08:20:16 +00001379 &azResult, &nRow, 0, &zErrMsg
drha18c5682000-10-08 22:20:57 +00001380 );
danielk1977bc6ada42004-06-30 08:20:16 +00001381 zShellStatic = 0;
drha50da102000-08-08 20:19:09 +00001382 }
drh75897232000-05-29 14:26:00 +00001383 if( zErrMsg ){
1384 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001385 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00001386 }
drhe3710332000-09-29 13:30:53 +00001387 if( rc==SQLITE_OK ){
1388 int len, maxlen = 0;
1389 int i, j;
1390 int nPrintCol, nPrintRow;
1391 for(i=1; i<=nRow; i++){
1392 if( azResult[i]==0 ) continue;
1393 len = strlen(azResult[i]);
1394 if( len>maxlen ) maxlen = len;
1395 }
1396 nPrintCol = 80/(maxlen+2);
1397 if( nPrintCol<1 ) nPrintCol = 1;
1398 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
1399 for(i=0; i<nPrintRow; i++){
1400 for(j=i+1; j<=nRow; j+=nPrintRow){
1401 char *zSp = j<=nPrintRow ? "" : " ";
1402 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
1403 }
1404 printf("\n");
1405 }
1406 }
danielk19776f8a5032004-05-10 10:34:51 +00001407 sqlite3_free_table(azResult);
drh75897232000-05-29 14:26:00 +00001408 }else
1409
drh2dfbbca2000-07-28 14:32:48 +00001410 if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
drh44c2eb12003-04-30 11:38:26 +00001411 open_db(p);
danielk19776f8a5032004-05-10 10:34:51 +00001412 sqlite3_busy_timeout(p->db, atoi(azArg[1]));
drh2dfbbca2000-07-28 14:32:48 +00001413 }else
1414
drh75897232000-05-29 14:26:00 +00001415 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
1416 int j;
drh43617e92006-03-06 20:55:46 +00001417 assert( nArg<=ArraySize(azArg) );
drh75897232000-05-29 14:26:00 +00001418 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
1419 p->colWidth[j-1] = atoi(azArg[j]);
1420 }
1421 }else
1422
1423 {
drh67505e72002-04-19 12:34:06 +00001424 fprintf(stderr, "unknown command or invalid arguments: "
1425 " \"%s\". Enter \".help\" for help\n", azArg[0]);
drh75897232000-05-29 14:26:00 +00001426 }
drh67505e72002-04-19 12:34:06 +00001427
1428 return rc;
drh75897232000-05-29 14:26:00 +00001429}
1430
drh67505e72002-04-19 12:34:06 +00001431/*
drh324ccef2003-02-05 14:06:20 +00001432** Return TRUE if the last non-whitespace character in z[] is a semicolon.
1433** z[] is N characters long.
1434*/
1435static int _ends_with_semicolon(const char *z, int N){
drh4c755c02004-08-08 20:22:17 +00001436 while( N>0 && isspace((unsigned char)z[N-1]) ){ N--; }
drh324ccef2003-02-05 14:06:20 +00001437 return N>0 && z[N-1]==';';
1438}
1439
1440/*
drh70c7a4b2003-04-26 03:03:06 +00001441** Test to see if a line consists entirely of whitespace.
1442*/
1443static int _all_whitespace(const char *z){
1444 for(; *z; z++){
drh4c755c02004-08-08 20:22:17 +00001445 if( isspace(*(unsigned char*)z) ) continue;
drh70c7a4b2003-04-26 03:03:06 +00001446 if( *z=='/' && z[1]=='*' ){
1447 z += 2;
1448 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
1449 if( *z==0 ) return 0;
1450 z++;
1451 continue;
1452 }
1453 if( *z=='-' && z[1]=='-' ){
1454 z += 2;
1455 while( *z && *z!='\n' ){ z++; }
1456 if( *z==0 ) return 1;
1457 continue;
1458 }
1459 return 0;
1460 }
1461 return 1;
1462}
1463
1464/*
drha9b17162003-04-29 18:01:28 +00001465** Return TRUE if the line typed in is an SQL command terminator other
1466** than a semi-colon. The SQL Server style "go" command is understood
1467** as is the Oracle "/".
1468*/
1469static int _is_command_terminator(const char *zLine){
drh4c755c02004-08-08 20:22:17 +00001470 while( isspace(*(unsigned char*)zLine) ){ zLine++; };
drha9b17162003-04-29 18:01:28 +00001471 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ) return 1; /* Oracle */
drhc8d74412004-08-31 23:41:26 +00001472 if( tolower(zLine[0])=='g' && tolower(zLine[1])=='o'
1473 && _all_whitespace(&zLine[2]) ){
drha9b17162003-04-29 18:01:28 +00001474 return 1; /* SQL Server */
1475 }
1476 return 0;
1477}
1478
1479/*
drh67505e72002-04-19 12:34:06 +00001480** Read input from *in and process it. If *in==0 then input
1481** is interactive - the user is typing it it. Otherwise, input
1482** is coming from a file or device. A prompt is issued and history
1483** is saved only if input is interactive. An interrupt signal will
1484** cause this routine to exit immediately, unless input is interactive.
1485*/
drhdaffd0e2001-04-11 14:28:42 +00001486static void process_input(struct callback_data *p, FILE *in){
1487 char *zLine;
1488 char *zSql = 0;
1489 int nSql = 0;
1490 char *zErrMsg;
drh7f953e22002-07-13 17:33:45 +00001491 int rc;
drh41e941d2002-04-04 15:10:12 +00001492 while( fflush(p->out), (zLine = one_input_line(zSql, in))!=0 ){
drh67505e72002-04-19 12:34:06 +00001493 if( seenInterrupt ){
1494 if( in!=0 ) break;
1495 seenInterrupt = 0;
1496 }
drhdaffd0e2001-04-11 14:28:42 +00001497 if( p->echoOn ) printf("%s\n", zLine);
drhf817b6b2003-06-16 00:16:41 +00001498 if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
drh2af0b2d2002-02-21 02:25:02 +00001499 if( zLine && zLine[0]=='.' && nSql==0 ){
drh44c2eb12003-04-30 11:38:26 +00001500 int rc = do_meta_command(zLine, p);
drhdaffd0e2001-04-11 14:28:42 +00001501 free(zLine);
drh67505e72002-04-19 12:34:06 +00001502 if( rc ) break;
drhdaffd0e2001-04-11 14:28:42 +00001503 continue;
1504 }
drha9b17162003-04-29 18:01:28 +00001505 if( _is_command_terminator(zLine) ){
1506 strcpy(zLine,";");
1507 }
drhdaffd0e2001-04-11 14:28:42 +00001508 if( zSql==0 ){
1509 int i;
drh4c755c02004-08-08 20:22:17 +00001510 for(i=0; zLine[i] && isspace((unsigned char)zLine[i]); i++){}
drhdaffd0e2001-04-11 14:28:42 +00001511 if( zLine[i]!=0 ){
1512 nSql = strlen(zLine);
1513 zSql = malloc( nSql+1 );
drhc1f44942006-05-10 14:39:13 +00001514 if( zSql==0 ){
1515 fprintf(stderr, "out of memory\n");
1516 exit(1);
1517 }
drhdaffd0e2001-04-11 14:28:42 +00001518 strcpy(zSql, zLine);
1519 }
1520 }else{
1521 int len = strlen(zLine);
1522 zSql = realloc( zSql, nSql + len + 2 );
1523 if( zSql==0 ){
1524 fprintf(stderr,"%s: out of memory!\n", Argv0);
1525 exit(1);
1526 }
1527 strcpy(&zSql[nSql++], "\n");
1528 strcpy(&zSql[nSql], zLine);
1529 nSql += len;
1530 }
1531 free(zLine);
danielk19776f8a5032004-05-10 10:34:51 +00001532 if( zSql && _ends_with_semicolon(zSql, nSql) && sqlite3_complete(zSql) ){
drhdaffd0e2001-04-11 14:28:42 +00001533 p->cnt = 0;
drh44c2eb12003-04-30 11:38:26 +00001534 open_db(p);
danielk19776f8a5032004-05-10 10:34:51 +00001535 rc = sqlite3_exec(p->db, zSql, callback, p, &zErrMsg);
drh7f953e22002-07-13 17:33:45 +00001536 if( rc || zErrMsg ){
drh05a82982006-03-19 13:00:25 +00001537 /* if( in!=0 && !p->echoOn ) printf("%s\n",zSql); */
drh7f953e22002-07-13 17:33:45 +00001538 if( zErrMsg!=0 ){
1539 printf("SQL error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001540 sqlite3_free(zErrMsg);
drh7f953e22002-07-13 17:33:45 +00001541 zErrMsg = 0;
1542 }else{
danielk19772a02e332004-06-05 08:04:36 +00001543 printf("SQL error: %s\n", sqlite3_errmsg(p->db));
drh7f953e22002-07-13 17:33:45 +00001544 }
drhdaffd0e2001-04-11 14:28:42 +00001545 }
1546 free(zSql);
1547 zSql = 0;
1548 nSql = 0;
1549 }
1550 }
1551 if( zSql ){
drh70c7a4b2003-04-26 03:03:06 +00001552 if( !_all_whitespace(zSql) ) printf("Incomplete SQL: %s\n", zSql);
drhdaffd0e2001-04-11 14:28:42 +00001553 free(zSql);
1554 }
1555}
1556
drh67505e72002-04-19 12:34:06 +00001557/*
1558** Return a pathname which is the user's home directory. A
1559** 0 return indicates an error of some kind. Space to hold the
1560** resulting string is obtained from malloc(). The calling
1561** function should free the result.
1562*/
1563static char *find_home_dir(void){
1564 char *home_dir = NULL;
persicom7e2dfdd2002-04-18 02:46:52 +00001565
drhcdb36b72006-06-12 12:57:45 +00001566#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__) && !defined(__OS2__)
drh67505e72002-04-19 12:34:06 +00001567 struct passwd *pwent;
1568 uid_t uid = getuid();
drhbd842ba2002-08-21 11:26:41 +00001569 if( (pwent=getpwuid(uid)) != NULL) {
1570 home_dir = pwent->pw_dir;
drh67505e72002-04-19 12:34:06 +00001571 }
1572#endif
1573
drh820f3812003-01-08 13:02:52 +00001574#ifdef __MACOS__
1575 char home_path[_MAX_PATH+1];
1576 home_dir = getcwd(home_path, _MAX_PATH);
1577#endif
1578
drh164a1b62006-08-19 11:15:20 +00001579#if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
1580 if (!home_dir) {
1581 home_dir = getenv("USERPROFILE");
1582 }
1583#endif
1584
drh67505e72002-04-19 12:34:06 +00001585 if (!home_dir) {
1586 home_dir = getenv("HOME");
drh67505e72002-04-19 12:34:06 +00001587 }
1588
drhcdb36b72006-06-12 12:57:45 +00001589#if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
drhe98d4fa2002-04-21 19:06:22 +00001590 if (!home_dir) {
drh164a1b62006-08-19 11:15:20 +00001591 char *zDrive, *zPath;
1592 int n;
1593 zDrive = getenv("HOMEDRIVE");
1594 zPath = getenv("HOMEPATH");
1595 if( zDrive && zPath ){
1596 n = strlen(zDrive) + strlen(zPath) + 1;
1597 home_dir = malloc( n );
1598 if( home_dir==0 ) return 0;
1599 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
1600 return home_dir;
1601 }
1602 home_dir = "c:\\";
drhe98d4fa2002-04-21 19:06:22 +00001603 }
1604#endif
1605
drh67505e72002-04-19 12:34:06 +00001606 if( home_dir ){
1607 char *z = malloc( strlen(home_dir)+1 );
1608 if( z ) strcpy(z, home_dir);
1609 home_dir = z;
1610 }
drhe98d4fa2002-04-21 19:06:22 +00001611
drh67505e72002-04-19 12:34:06 +00001612 return home_dir;
1613}
1614
1615/*
1616** Read input from the file given by sqliterc_override. Or if that
1617** parameter is NULL, take input from ~/.sqliterc
1618*/
drh22fbcb82004-02-01 01:22:50 +00001619static void process_sqliterc(
1620 struct callback_data *p, /* Configuration data */
1621 const char *sqliterc_override /* Name of config file. NULL to use default */
1622){
persicom7e2dfdd2002-04-18 02:46:52 +00001623 char *home_dir = NULL;
drh22fbcb82004-02-01 01:22:50 +00001624 const char *sqliterc = sqliterc_override;
drh43617e92006-03-06 20:55:46 +00001625 char *zBuf = 0;
persicom7e2dfdd2002-04-18 02:46:52 +00001626 FILE *in = NULL;
1627
1628 if (sqliterc == NULL) {
drh67505e72002-04-19 12:34:06 +00001629 home_dir = find_home_dir();
drhe98d4fa2002-04-21 19:06:22 +00001630 if( home_dir==0 ){
1631 fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0);
1632 return;
1633 }
drh22fbcb82004-02-01 01:22:50 +00001634 zBuf = malloc(strlen(home_dir) + 15);
1635 if( zBuf==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +00001636 fprintf(stderr,"%s: out of memory!\n", Argv0);
1637 exit(1);
1638 }
drh22fbcb82004-02-01 01:22:50 +00001639 sprintf(zBuf,"%s/.sqliterc",home_dir);
drh67505e72002-04-19 12:34:06 +00001640 free(home_dir);
drh22fbcb82004-02-01 01:22:50 +00001641 sqliterc = (const char*)zBuf;
persicom7e2dfdd2002-04-18 02:46:52 +00001642 }
drha1f9b5e2004-02-14 16:31:02 +00001643 in = fopen(sqliterc,"rb");
drh22fbcb82004-02-01 01:22:50 +00001644 if( in ){
1645 if( isatty(fileno(stdout)) ){
1646 printf("Loading resources from %s\n",sqliterc);
1647 }
persicom7e2dfdd2002-04-18 02:46:52 +00001648 process_input(p,in);
drhdd45df82002-04-18 12:39:03 +00001649 fclose(in);
persicom7e2dfdd2002-04-18 02:46:52 +00001650 }
drh43617e92006-03-06 20:55:46 +00001651 free(zBuf);
persicom7e2dfdd2002-04-18 02:46:52 +00001652 return;
1653}
1654
drh67505e72002-04-19 12:34:06 +00001655/*
drhe1e38c42003-05-04 18:30:59 +00001656** Show available command line options
1657*/
1658static const char zOptions[] =
1659 " -init filename read/process named file\n"
1660 " -echo print commands before execution\n"
1661 " -[no]header turn headers on or off\n"
1662 " -column set output mode to 'column'\n"
1663 " -html set output mode to HTML\n"
1664 " -line set output mode to 'line'\n"
1665 " -list set output mode to 'list'\n"
1666 " -separator 'x' set output field separator (|)\n"
1667 " -nullvalue 'text' set text string for NULL values\n"
1668 " -version show SQLite version\n"
drhe1e38c42003-05-04 18:30:59 +00001669;
1670static void usage(int showDetail){
drh80e8be92006-08-29 12:04:19 +00001671 fprintf(stderr,
1672 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
1673 "FILENAME is the name of an SQLite database. A new database is created\n"
1674 "if the file does not previously exist.\n", Argv0);
drhe1e38c42003-05-04 18:30:59 +00001675 if( showDetail ){
drh80e8be92006-08-29 12:04:19 +00001676 fprintf(stderr, "OPTIONS include:\n%s", zOptions);
drhe1e38c42003-05-04 18:30:59 +00001677 }else{
1678 fprintf(stderr, "Use the -help option for additional information\n");
1679 }
1680 exit(1);
1681}
1682
1683/*
drh67505e72002-04-19 12:34:06 +00001684** Initialize the state information in data
1685*/
drh0850b532006-01-31 19:31:43 +00001686static void main_init(struct callback_data *data) {
persicom7e2dfdd2002-04-18 02:46:52 +00001687 memset(data, 0, sizeof(*data));
1688 data->mode = MODE_List;
1689 strcpy(data->separator,"|");
1690 data->showHeader = 0;
1691 strcpy(mainPrompt,"sqlite> ");
1692 strcpy(continuePrompt," ...> ");
1693}
1694
drh75897232000-05-29 14:26:00 +00001695int main(int argc, char **argv){
drh75897232000-05-29 14:26:00 +00001696 char *zErrMsg = 0;
1697 struct callback_data data;
drh22fbcb82004-02-01 01:22:50 +00001698 const char *zInitFile = 0;
1699 char *zFirstCmd = 0;
drh44c2eb12003-04-30 11:38:26 +00001700 int i;
drh75897232000-05-29 14:26:00 +00001701
drh820f3812003-01-08 13:02:52 +00001702#ifdef __MACOS__
1703 argc = ccommand(&argv);
drh820f3812003-01-08 13:02:52 +00001704#endif
1705
drhdaffd0e2001-04-11 14:28:42 +00001706 Argv0 = argv[0];
persicom7e2dfdd2002-04-18 02:46:52 +00001707 main_init(&data);
persicom7e2dfdd2002-04-18 02:46:52 +00001708
drh44c2eb12003-04-30 11:38:26 +00001709 /* Make sure we have a valid signal handler early, before anything
1710 ** else is done.
1711 */
drh4c504392000-10-16 22:06:40 +00001712#ifdef SIGINT
1713 signal(SIGINT, interrupt_handler);
1714#endif
drh44c2eb12003-04-30 11:38:26 +00001715
drh22fbcb82004-02-01 01:22:50 +00001716 /* Do an initial pass through the command-line argument to locate
1717 ** the name of the database file, the name of the initialization file,
1718 ** and the first command to execute.
drh44c2eb12003-04-30 11:38:26 +00001719 */
drh22fbcb82004-02-01 01:22:50 +00001720 for(i=1; i<argc-1; i++){
drh44c2eb12003-04-30 11:38:26 +00001721 if( argv[i][0]!='-' ) break;
1722 if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){
1723 i++;
drh22fbcb82004-02-01 01:22:50 +00001724 }else if( strcmp(argv[i],"-init")==0 ){
1725 i++;
1726 zInitFile = argv[i];
drh44c2eb12003-04-30 11:38:26 +00001727 }
1728 }
drh22fbcb82004-02-01 01:22:50 +00001729 if( i<argc ){
1730 data.zDbFilename = argv[i++];
1731 }else{
danielk197703aded42004-11-22 05:26:27 +00001732#ifndef SQLITE_OMIT_MEMORYDB
drh22fbcb82004-02-01 01:22:50 +00001733 data.zDbFilename = ":memory:";
danielk197703aded42004-11-22 05:26:27 +00001734#else
1735 data.zDbFilename = 0;
1736#endif
drh22fbcb82004-02-01 01:22:50 +00001737 }
1738 if( i<argc ){
1739 zFirstCmd = argv[i++];
1740 }
drh44c2eb12003-04-30 11:38:26 +00001741 data.out = stdout;
1742
drh01b41712005-08-29 23:06:23 +00001743#ifdef SQLITE_OMIT_MEMORYDB
1744 if( data.zDbFilename==0 ){
1745 fprintf(stderr,"%s: no database filename specified\n", argv[0]);
1746 exit(1);
1747 }
1748#endif
1749
drh44c2eb12003-04-30 11:38:26 +00001750 /* Go ahead and open the database file if it already exists. If the
1751 ** file does not exist, delay opening it. This prevents empty database
1752 ** files from being created if a user mistypes the database name argument
1753 ** to the sqlite command-line tool.
1754 */
drhc8d74412004-08-31 23:41:26 +00001755 if( access(data.zDbFilename, 0)==0 ){
drh44c2eb12003-04-30 11:38:26 +00001756 open_db(&data);
1757 }
1758
drh22fbcb82004-02-01 01:22:50 +00001759 /* Process the initialization file if there is one. If no -init option
1760 ** is given on the command line, look for a file named ~/.sqliterc and
1761 ** try to process it.
drh44c2eb12003-04-30 11:38:26 +00001762 */
drh22fbcb82004-02-01 01:22:50 +00001763 process_sqliterc(&data,zInitFile);
drh44c2eb12003-04-30 11:38:26 +00001764
drh22fbcb82004-02-01 01:22:50 +00001765 /* Make a second pass through the command-line argument and set
1766 ** options. This second pass is delayed until after the initialization
1767 ** file is processed so that the command-line arguments will override
1768 ** settings in the initialization file.
drh44c2eb12003-04-30 11:38:26 +00001769 */
drh22fbcb82004-02-01 01:22:50 +00001770 for(i=1; i<argc && argv[i][0]=='-'; i++){
1771 char *z = argv[i];
drh2e584cd2006-09-25 13:09:22 +00001772 if( strcmp(z,"-init")==0 ){
drh22fbcb82004-02-01 01:22:50 +00001773 i++;
1774 }else if( strcmp(z,"-html")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001775 data.mode = MODE_Html;
drh22fbcb82004-02-01 01:22:50 +00001776 }else if( strcmp(z,"-list")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001777 data.mode = MODE_List;
drh22fbcb82004-02-01 01:22:50 +00001778 }else if( strcmp(z,"-line")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001779 data.mode = MODE_Line;
drh22fbcb82004-02-01 01:22:50 +00001780 }else if( strcmp(z,"-column")==0 ){
drh8b32e172002-04-08 02:42:57 +00001781 data.mode = MODE_Column;
drh22fbcb82004-02-01 01:22:50 +00001782 }else if( strcmp(z,"-separator")==0 ){
1783 i++;
1784 sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[i]);
1785 }else if( strcmp(z,"-nullvalue")==0 ){
1786 i++;
1787 sprintf(data.nullvalue,"%.*s",(int)sizeof(data.nullvalue)-1,argv[i]);
1788 }else if( strcmp(z,"-header")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001789 data.showHeader = 1;
drh22fbcb82004-02-01 01:22:50 +00001790 }else if( strcmp(z,"-noheader")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001791 data.showHeader = 0;
drh22fbcb82004-02-01 01:22:50 +00001792 }else if( strcmp(z,"-echo")==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001793 data.echoOn = 1;
drh22fbcb82004-02-01 01:22:50 +00001794 }else if( strcmp(z,"-version")==0 ){
drhc8d74412004-08-31 23:41:26 +00001795 printf("%s\n", sqlite3_libversion());
drh151e3e12006-06-06 12:32:21 +00001796 return 0;
drh80e8be92006-08-29 12:04:19 +00001797 }else if( strcmp(z,"-help")==0 || strcmp(z, "--help")==0 ){
drhe1e38c42003-05-04 18:30:59 +00001798 usage(1);
drh1e5d0e92000-05-31 23:33:17 +00001799 }else{
drh22fbcb82004-02-01 01:22:50 +00001800 fprintf(stderr,"%s: unknown option: %s\n", Argv0, z);
drhe1e38c42003-05-04 18:30:59 +00001801 fprintf(stderr,"Use -help for a list of options.\n");
drh1e5d0e92000-05-31 23:33:17 +00001802 return 1;
1803 }
1804 }
drh44c2eb12003-04-30 11:38:26 +00001805
drh22fbcb82004-02-01 01:22:50 +00001806 if( zFirstCmd ){
drh44c2eb12003-04-30 11:38:26 +00001807 /* Run just the command that follows the database name
1808 */
drh22fbcb82004-02-01 01:22:50 +00001809 if( zFirstCmd[0]=='.' ){
1810 do_meta_command(zFirstCmd, &data);
drh6ff13852001-11-25 13:18:23 +00001811 exit(0);
1812 }else{
1813 int rc;
drh44c2eb12003-04-30 11:38:26 +00001814 open_db(&data);
danielk19776f8a5032004-05-10 10:34:51 +00001815 rc = sqlite3_exec(data.db, zFirstCmd, callback, &data, &zErrMsg);
drh6ff13852001-11-25 13:18:23 +00001816 if( rc!=0 && zErrMsg!=0 ){
1817 fprintf(stderr,"SQL error: %s\n", zErrMsg);
1818 exit(1);
1819 }
drh75897232000-05-29 14:26:00 +00001820 }
1821 }else{
drh44c2eb12003-04-30 11:38:26 +00001822 /* Run commands received from standard input
1823 */
drh5cf590c2003-04-24 01:45:04 +00001824 if( isatty(fileno(stdout)) && isatty(fileno(stdin)) ){
drh67505e72002-04-19 12:34:06 +00001825 char *zHome;
1826 char *zHistory = 0;
drh75897232000-05-29 14:26:00 +00001827 printf(
drhb217a572000-08-22 13:40:18 +00001828 "SQLite version %s\n"
1829 "Enter \".help\" for instructions\n",
drhc8d74412004-08-31 23:41:26 +00001830 sqlite3_libversion()
drh75897232000-05-29 14:26:00 +00001831 );
drh67505e72002-04-19 12:34:06 +00001832 zHome = find_home_dir();
1833 if( zHome && (zHistory = malloc(strlen(zHome)+20))!=0 ){
1834 sprintf(zHistory,"%s/.sqlite_history", zHome);
1835 }
danielk19774af00c62005-01-23 23:43:21 +00001836#if defined(HAVE_READLINE) && HAVE_READLINE==1
drh67505e72002-04-19 12:34:06 +00001837 if( zHistory ) read_history(zHistory);
danielk19774af00c62005-01-23 23:43:21 +00001838#endif
drhdaffd0e2001-04-11 14:28:42 +00001839 process_input(&data, 0);
drh67505e72002-04-19 12:34:06 +00001840 if( zHistory ){
1841 stifle_history(100);
1842 write_history(zHistory);
adamd0a3daa32006-07-28 20:16:14 +00001843 free(zHistory);
drh67505e72002-04-19 12:34:06 +00001844 }
adamd0a3daa32006-07-28 20:16:14 +00001845 free(zHome);
drhdaffd0e2001-04-11 14:28:42 +00001846 }else{
1847 process_input(&data, stdin);
drh75897232000-05-29 14:26:00 +00001848 }
1849 }
drh33048c02001-10-01 14:29:22 +00001850 set_table_name(&data, 0);
adamd0a3daa32006-07-28 20:16:14 +00001851 if( db ){
1852 if( sqlite3_close(db)!=SQLITE_OK ){
1853 fprintf(stderr,"error closing database: %s\n", sqlite3_errmsg(db));
1854 }
1855 }
drh75897232000-05-29 14:26:00 +00001856 return 0;
1857}