blob: 84efe0518a48bfdbcfe1f8679cfa9f095c7013f1 [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**
danielk19772a02e332004-06-05 08:04:36 +000015** $Id: shell.c,v 1.102 2004/06/05 08:04:44 danielk1977 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*/
60static sqlite *db = 0;
61
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/*
drh8e7e7a22000-05-30 18:45:23 +000087** This routine reads a line of text from standard input, stores
88** the text in memory obtained from malloc() and returns a pointer
89** to the text. NULL is returned at end of file, or if malloc()
90** fails.
91**
92** The interface is like "readline" but no command-line editing
93** is done.
94*/
drh9347b202003-07-18 01:30:59 +000095static char *local_getline(char *zPrompt, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +000096 char *zLine;
97 int nLine;
drh8e7e7a22000-05-30 18:45:23 +000098 int n;
99 int eol;
100
101 if( zPrompt && *zPrompt ){
102 printf("%s",zPrompt);
103 fflush(stdout);
104 }
105 nLine = 100;
106 zLine = malloc( nLine );
107 if( zLine==0 ) return 0;
108 n = 0;
109 eol = 0;
110 while( !eol ){
111 if( n+100>nLine ){
112 nLine = nLine*2 + 100;
113 zLine = realloc(zLine, nLine);
114 if( zLine==0 ) return 0;
115 }
drhdaffd0e2001-04-11 14:28:42 +0000116 if( fgets(&zLine[n], nLine - n, in)==0 ){
drh8e7e7a22000-05-30 18:45:23 +0000117 if( n==0 ){
118 free(zLine);
119 return 0;
120 }
121 zLine[n] = 0;
122 eol = 1;
123 break;
124 }
125 while( zLine[n] ){ n++; }
126 if( n>0 && zLine[n-1]=='\n' ){
127 n--;
128 zLine[n] = 0;
129 eol = 1;
130 }
131 }
132 zLine = realloc( zLine, n+1 );
133 return zLine;
134}
135
136/*
137** Retrieve a single line of input text. "isatty" is true if text
138** is coming from a terminal. In that case, we issue a prompt and
139** attempt to use "readline" for command-line editing. If "isatty"
drh9347b202003-07-18 01:30:59 +0000140** is false, use "local_getline" instead of "readline" and issue no prompt.
drh8e7e7a22000-05-30 18:45:23 +0000141**
142** zPrior is a string of prior text retrieved. If not the empty
143** string, then issue a continuation prompt.
144*/
drhdaffd0e2001-04-11 14:28:42 +0000145static char *one_input_line(const char *zPrior, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +0000146 char *zPrompt;
147 char *zResult;
drhdaffd0e2001-04-11 14:28:42 +0000148 if( in!=0 ){
drh9347b202003-07-18 01:30:59 +0000149 return local_getline(0, in);
drh8e7e7a22000-05-30 18:45:23 +0000150 }
151 if( zPrior && zPrior[0] ){
persicom7e2dfdd2002-04-18 02:46:52 +0000152 zPrompt = continuePrompt;
drh8e7e7a22000-05-30 18:45:23 +0000153 }else{
persicom7e2dfdd2002-04-18 02:46:52 +0000154 zPrompt = mainPrompt;
drh8e7e7a22000-05-30 18:45:23 +0000155 }
156 zResult = readline(zPrompt);
drh2dfbbca2000-07-28 14:32:48 +0000157 if( zResult ) add_history(zResult);
drh8e7e7a22000-05-30 18:45:23 +0000158 return zResult;
159}
160
persicom7e2dfdd2002-04-18 02:46:52 +0000161struct previous_mode_data {
162 int valid; /* Is there legit data in here? */
163 int mode;
164 int showHeader;
165 int colWidth[100];
166};
drh8e7e7a22000-05-30 18:45:23 +0000167/*
drh75897232000-05-29 14:26:00 +0000168** An pointer to an instance of this structure is passed from
169** the main program to the callback. This is used to communicate
170** state and mode information.
171*/
172struct callback_data {
drh28bd4bc2000-06-15 15:57:22 +0000173 sqlite *db; /* The database */
drhdaffd0e2001-04-11 14:28:42 +0000174 int echoOn; /* True to echo input commands */
drh28bd4bc2000-06-15 15:57:22 +0000175 int cnt; /* Number of records displayed so far */
176 FILE *out; /* Write results here */
177 int mode; /* An output mode setting */
178 int showHeader; /* True to show column names in List or Column mode */
drh33048c02001-10-01 14:29:22 +0000179 char *zDestTable; /* Name of destination table when MODE_Insert */
drh28bd4bc2000-06-15 15:57:22 +0000180 char separator[20]; /* Separator character for MODE_List */
drha0c66f52000-07-29 13:20:21 +0000181 int colWidth[100]; /* Requested width of each column when in column mode*/
182 int actualWidth[100]; /* Actual width of each column */
drh83965662003-04-17 02:54:13 +0000183 char nullvalue[20]; /* The text to print when a NULL comes back from
184 ** the database */
persicom7e2dfdd2002-04-18 02:46:52 +0000185 struct previous_mode_data explainPrev;
drh83965662003-04-17 02:54:13 +0000186 /* Holds the mode information just before
187 ** .explain ON */
drh44c2eb12003-04-30 11:38:26 +0000188 char outfile[FILENAME_MAX]; /* Filename for *out */
189 const char *zDbFilename; /* name of the database file */
drh22fbcb82004-02-01 01:22:50 +0000190 char *zKey; /* Encryption key */
drh75897232000-05-29 14:26:00 +0000191};
192
193/*
194** These are the allowed modes.
195*/
drh967e8b72000-06-21 13:59:10 +0000196#define MODE_Line 0 /* One column per line. Blank line between records */
drh75897232000-05-29 14:26:00 +0000197#define MODE_Column 1 /* One record per line in neat columns */
198#define MODE_List 2 /* One record per line with a separator */
drhe3710332000-09-29 13:30:53 +0000199#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
200#define MODE_Html 4 /* Generate an XHTML table */
201#define MODE_Insert 5 /* Generate SQL "insert" statements */
jplyon672a1ed2003-05-11 20:07:05 +0000202#define MODE_NUM_OF 6 /* The number of modes (not a mode itself) */
persicom7e2dfdd2002-04-18 02:46:52 +0000203
204char *modeDescr[MODE_NUM_OF] = {
205 "line",
206 "column",
207 "list",
208 "semi",
209 "html",
210 "insert"
211};
drh75897232000-05-29 14:26:00 +0000212
213/*
214** Number of elements in an array
215*/
216#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
217
218/*
drh28bd4bc2000-06-15 15:57:22 +0000219** Output the given string as a quoted string using SQL quoting conventions.
220*/
221static void output_quoted_string(FILE *out, const char *z){
222 int i;
223 int nSingle = 0;
drh28bd4bc2000-06-15 15:57:22 +0000224 for(i=0; z[i]; i++){
225 if( z[i]=='\'' ) nSingle++;
drh28bd4bc2000-06-15 15:57:22 +0000226 }
227 if( nSingle==0 ){
228 fprintf(out,"'%s'",z);
drh28bd4bc2000-06-15 15:57:22 +0000229 }else{
230 fprintf(out,"'");
231 while( *z ){
232 for(i=0; z[i] && z[i]!='\''; i++){}
233 if( i==0 ){
234 fprintf(out,"''");
235 z++;
236 }else if( z[i]=='\'' ){
237 fprintf(out,"%.*s''",i,z);
238 z += i+1;
239 }else{
drhcd7d2732002-02-26 23:24:26 +0000240 fprintf(out,"%s",z);
drh28bd4bc2000-06-15 15:57:22 +0000241 break;
242 }
243 }
drhcd7d2732002-02-26 23:24:26 +0000244 fprintf(out,"'");
drh28bd4bc2000-06-15 15:57:22 +0000245 }
246}
247
248/*
drhc08a4f12000-06-15 16:49:48 +0000249** Output the given string with characters that are special to
250** HTML escaped.
251*/
252static void output_html_string(FILE *out, const char *z){
253 int i;
254 while( *z ){
255 for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){}
256 if( i>0 ){
257 fprintf(out,"%.*s",i,z);
258 }
259 if( z[i]=='<' ){
260 fprintf(out,"&lt;");
261 }else if( z[i]=='&' ){
262 fprintf(out,"&amp;");
263 }else{
264 break;
265 }
266 z += i + 1;
267 }
268}
269
270/*
drh4c504392000-10-16 22:06:40 +0000271** This routine runs when the user presses Ctrl-C
272*/
273static void interrupt_handler(int NotUsed){
drh67505e72002-04-19 12:34:06 +0000274 seenInterrupt = 1;
danielk19776f8a5032004-05-10 10:34:51 +0000275 if( db ) sqlite3_interrupt(db);
drh4c504392000-10-16 22:06:40 +0000276}
277
278/*
drh75897232000-05-29 14:26:00 +0000279** This is the callback routine that the SQLite library
280** invokes for each row of a query result.
281*/
282static int callback(void *pArg, int nArg, char **azArg, char **azCol){
283 int i;
284 struct callback_data *p = (struct callback_data*)pArg;
285 switch( p->mode ){
286 case MODE_Line: {
drhe3710332000-09-29 13:30:53 +0000287 int w = 5;
drh6a535342001-10-19 16:44:56 +0000288 if( azArg==0 ) break;
drhe3710332000-09-29 13:30:53 +0000289 for(i=0; i<nArg; i++){
290 int len = strlen(azCol[i]);
291 if( len>w ) w = len;
292 }
drh75897232000-05-29 14:26:00 +0000293 if( p->cnt++>0 ) fprintf(p->out,"\n");
294 for(i=0; i<nArg; i++){
drha69d9162003-04-17 22:57:53 +0000295 fprintf(p->out,"%*s = %s\n", w, azCol[i],
296 azArg[i] ? azArg[i] : p->nullvalue);
drh75897232000-05-29 14:26:00 +0000297 }
298 break;
299 }
300 case MODE_Column: {
drha0c66f52000-07-29 13:20:21 +0000301 if( p->cnt++==0 ){
drh75897232000-05-29 14:26:00 +0000302 for(i=0; i<nArg; i++){
drha0c66f52000-07-29 13:20:21 +0000303 int w, n;
304 if( i<ArraySize(p->colWidth) ){
drh75897232000-05-29 14:26:00 +0000305 w = p->colWidth[i];
306 }else{
drha0c66f52000-07-29 13:20:21 +0000307 w = 0;
drh75897232000-05-29 14:26:00 +0000308 }
drha0c66f52000-07-29 13:20:21 +0000309 if( w<=0 ){
drhff6e9112000-08-28 16:21:58 +0000310 w = strlen(azCol[i] ? azCol[i] : "");
drha0c66f52000-07-29 13:20:21 +0000311 if( w<10 ) w = 10;
persicom7e2dfdd2002-04-18 02:46:52 +0000312 n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue);
drha0c66f52000-07-29 13:20:21 +0000313 if( w<n ) w = n;
314 }
315 if( i<ArraySize(p->actualWidth) ){
persicom1d0b8722002-04-18 02:53:04 +0000316 p->actualWidth[i] = w;
drha0c66f52000-07-29 13:20:21 +0000317 }
318 if( p->showHeader ){
319 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
320 }
321 }
322 if( p->showHeader ){
323 for(i=0; i<nArg; i++){
324 int w;
325 if( i<ArraySize(p->actualWidth) ){
326 w = p->actualWidth[i];
327 }else{
328 w = 10;
329 }
330 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
331 "----------------------------------------------------------",
332 i==nArg-1 ? "\n": " ");
333 }
drh75897232000-05-29 14:26:00 +0000334 }
335 }
drh6a535342001-10-19 16:44:56 +0000336 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000337 for(i=0; i<nArg; i++){
338 int w;
drha0c66f52000-07-29 13:20:21 +0000339 if( i<ArraySize(p->actualWidth) ){
340 w = p->actualWidth[i];
drh75897232000-05-29 14:26:00 +0000341 }else{
342 w = 10;
343 }
drhc61053b2000-06-04 12:58:36 +0000344 fprintf(p->out,"%-*.*s%s",w,w,
persicom7e2dfdd2002-04-18 02:46:52 +0000345 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
drh75897232000-05-29 14:26:00 +0000346 }
347 break;
348 }
drhe3710332000-09-29 13:30:53 +0000349 case MODE_Semi:
drh75897232000-05-29 14:26:00 +0000350 case MODE_List: {
351 if( p->cnt++==0 && p->showHeader ){
352 for(i=0; i<nArg; i++){
353 fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
354 }
355 }
drh6a535342001-10-19 16:44:56 +0000356 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000357 for(i=0; i<nArg; i++){
drh4c653a02000-06-07 01:27:47 +0000358 char *z = azArg[i];
persicom7e2dfdd2002-04-18 02:46:52 +0000359 if( z==0 ) z = p->nullvalue;
drh71172c52002-01-24 00:00:21 +0000360 fprintf(p->out, "%s", z);
drhe3710332000-09-29 13:30:53 +0000361 if( i<nArg-1 ){
362 fprintf(p->out, "%s", p->separator);
363 }else if( p->mode==MODE_Semi ){
364 fprintf(p->out, ";\n");
365 }else{
366 fprintf(p->out, "\n");
367 }
drh75897232000-05-29 14:26:00 +0000368 }
369 break;
370 }
drh1e5d0e92000-05-31 23:33:17 +0000371 case MODE_Html: {
372 if( p->cnt++==0 && p->showHeader ){
373 fprintf(p->out,"<TR>");
374 for(i=0; i<nArg; i++){
375 fprintf(p->out,"<TH>%s</TH>",azCol[i]);
376 }
377 fprintf(p->out,"</TR>\n");
378 }
drh6a535342001-10-19 16:44:56 +0000379 if( azArg==0 ) break;
drh28bd4bc2000-06-15 15:57:22 +0000380 fprintf(p->out,"<TR>");
drh1e5d0e92000-05-31 23:33:17 +0000381 for(i=0; i<nArg; i++){
drhc08a4f12000-06-15 16:49:48 +0000382 fprintf(p->out,"<TD>");
persicom7e2dfdd2002-04-18 02:46:52 +0000383 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
drhc08a4f12000-06-15 16:49:48 +0000384 fprintf(p->out,"</TD>\n");
drh1e5d0e92000-05-31 23:33:17 +0000385 }
drh7d686b22002-11-11 13:56:47 +0000386 fprintf(p->out,"</TR>\n");
drh1e5d0e92000-05-31 23:33:17 +0000387 break;
388 }
drh28bd4bc2000-06-15 15:57:22 +0000389 case MODE_Insert: {
drh6a535342001-10-19 16:44:56 +0000390 if( azArg==0 ) break;
drh33048c02001-10-01 14:29:22 +0000391 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
drh28bd4bc2000-06-15 15:57:22 +0000392 for(i=0; i<nArg; i++){
393 char *zSep = i>0 ? ",": "";
394 if( azArg[i]==0 ){
395 fprintf(p->out,"%sNULL",zSep);
danielk19778a6b5412004-05-24 07:04:25 +0000396 }else if( sqlite3IsNumber(azArg[i], 0, 1) ){
drh28bd4bc2000-06-15 15:57:22 +0000397 fprintf(p->out,"%s%s",zSep, azArg[i]);
398 }else{
399 if( zSep[0] ) fprintf(p->out,"%s",zSep);
400 output_quoted_string(p->out, azArg[i]);
401 }
402 }
403 fprintf(p->out,");\n");
drh6a535342001-10-19 16:44:56 +0000404 break;
drh28bd4bc2000-06-15 15:57:22 +0000405 }
persicom1d0b8722002-04-18 02:53:04 +0000406 }
drh75897232000-05-29 14:26:00 +0000407 return 0;
408}
409
410/*
drh33048c02001-10-01 14:29:22 +0000411** Set the destination table field of the callback_data structure to
412** the name of the table given. Escape any quote characters in the
413** table name.
414*/
415static void set_table_name(struct callback_data *p, const char *zName){
416 int i, n;
417 int needQuote;
418 char *z;
419
420 if( p->zDestTable ){
421 free(p->zDestTable);
422 p->zDestTable = 0;
423 }
424 if( zName==0 ) return;
425 needQuote = !isalpha(*zName) && *zName!='_';
426 for(i=n=0; zName[i]; i++, n++){
427 if( !isalnum(zName[i]) && zName[i]!='_' ){
428 needQuote = 1;
429 if( zName[i]=='\'' ) n++;
430 }
431 }
432 if( needQuote ) n += 2;
433 z = p->zDestTable = malloc( n+1 );
434 if( z==0 ){
435 fprintf(stderr,"Out of memory!\n");
436 exit(1);
437 }
438 n = 0;
439 if( needQuote ) z[n++] = '\'';
440 for(i=0; zName[i]; i++){
441 z[n++] = zName[i];
442 if( zName[i]=='\'' ) z[n++] = '\'';
443 }
444 if( needQuote ) z[n++] = '\'';
445 z[n] = 0;
446}
447
danielk19772a02e332004-06-05 08:04:36 +0000448/* zIn is either a pointer to a NULL-terminated string in memory obtained
449** from malloc(), or a NULL pointer. The string pointed to by zAppend is
450** added to zIn, and the result returned in memory obtained from malloc().
451** zIn, if it was not NULL, is freed.
452**
453** If the third argument, quote, is not '\0', then it is used as a
454** quote character for zAppend.
455*/
456static char * appendText(char *zIn, char const *zAppend, char quote){
457 int len;
458 int i;
459 int nAppend = strlen(zAppend);
460 int nIn = (zIn?strlen(zIn):0);
461
462 len = nAppend+nIn+1;
463 if( quote ){
464 len += 2;
465 for(i=0; i<nAppend; i++){
466 if( zAppend[i]==quote ) len++;
467 }
468 }
469
470 zIn = (char *)realloc(zIn, len);
471 if( !zIn ){
472 return 0;
473 }
474
475 if( quote ){
476 char *zCsr = &zIn[nIn];
477 *zCsr++ = quote;
478 for(i=0; i<nAppend; i++){
479 *zCsr++ = zAppend[i];
480 if( zAppend[i]==quote ) *zCsr++ = quote;
481 }
482 *zCsr++ = quote;
483 *zCsr++ = '\0';
484 assert( (zCsr-zIn)==len );
485 }else{
486 memcpy(&zIn[nIn], zAppend, nAppend);
487 zIn[len-1] = '\0';
488 }
489
490 return zIn;
491}
492
493/* This function implements the SQL scalar function dump_literal()used by
494 * the '.dump' built-in. It takes one argument and returns the fully quoted
495** literal version of the argument depending on the type.
496**
497** Type Example
498** -----------------------
499** NULL "NULL"
500** INTEGER "0"
501** REAL "0.0"
502** TEXT "'abc'"
503** BLOB "X'89AB'"
504**
505*/
506static void dump_literalFunc(
507 sqlite3_context *context,
508 int argc,
509 sqlite3_value **argv
510){
511 static const char hexdigits[] = {
512 '0', '1', '2', '3', '4', '5', '6', '7',
513 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
514 };
515 assert( argc==1 );
516
517 switch( sqlite3_value_type(argv[0]) ){
518 case SQLITE_NULL:
519 sqlite3_result_text(context, "NULL", -1, 0);
520 break;
521 case SQLITE_INTEGER:
522 sqlite3_result_text(context, sqlite3_value_text(argv[0]), -1, 1);
523 break;
524 case SQLITE_FLOAT: {
525 char zBuf[40];
526 sprintf(zBuf, "%.15g", sqlite3_value_double(argv[0]));
527 sqlite3_result_text(context, zBuf, -1, 1);
528 break;
529 }
530 case SQLITE_TEXT: {
531 char *zText;
532 zText = appendText(0, sqlite3_value_text(argv[0]), '\'');
533 if( !zText ){
534 sqlite3_result_error(context, "out of memory", -1);
535 }else{
536 sqlite3_result_text(context, zText, -1, 1);
537 free(zText);
538 }
539 break;
540 }
541 case SQLITE_BLOB: {
542 char *zText = 0;
543 int nBlob = sqlite3_value_bytes(argv[0]);
544 char const *zBlob = sqlite3_value_blob(argv[0]);
545
546 zText = (char *)malloc((2*nBlob)+4);
547 if( !zText ){
548 sqlite3_result_error(context, "out of memory", -1);
549 }else{
550 int i;
551 for(i=0; i<nBlob; i++){
552 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
553 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
554 }
555 zText[(nBlob*2)+2] = '\'';
556 zText[(nBlob*2)+3] = '\0';
557 zText[0] = 'X';
558 zText[1] = '\'';
559 sqlite3_result_text(context, zText, -1, 1);
560 free(zText);
561 }
562 break;
563 }
564 }
565}
566
drh33048c02001-10-01 14:29:22 +0000567/*
drh4c653a02000-06-07 01:27:47 +0000568** This is a different callback routine used for dumping the database.
569** Each row received by this callback consists of a table name,
570** the table type ("index" or "table") and SQL to create the table.
571** This routine should print text sufficient to recreate the table.
572*/
573static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
danielk19772a02e332004-06-05 08:04:36 +0000574 int rc;
575 const char *zTable;
576 const char *zType;
577 const char *zSql;
drhdaffd0e2001-04-11 14:28:42 +0000578 struct callback_data *p = (struct callback_data *)pArg;
danielk19772a02e332004-06-05 08:04:36 +0000579
drh4c653a02000-06-07 01:27:47 +0000580 if( nArg!=3 ) return 1;
danielk19772a02e332004-06-05 08:04:36 +0000581 zTable = azArg[0];
582 zType = azArg[1];
583 zSql = azArg[2];
584
585 fprintf(p->out, "%s;\n", zSql);
586
587 if( strcmp(zType, "table")==0 ){
588 sqlite3_stmt *pTableInfo = 0;
589 sqlite3_stmt *pSelect = 0;
590 char *zSelect = 0;
591 char *zTableInfo = 0;
592 char *zTmp = 0;
593
594 zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
595 zTableInfo = appendText(zTableInfo, zTable, '"');
596 zTableInfo = appendText(zTableInfo, ");", 0);
597
598 rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0);
599 if( zTableInfo ) free(zTableInfo);
600 if( rc!=SQLITE_OK || !pTableInfo ){
601 return 1;
602 }
603
604 zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
605 zTmp = appendText(zTmp, zTable, '"');
606 if( zTmp ){
607 zSelect = appendText(zSelect, zTmp, '\'');
608 }
609 zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
610 rc = sqlite3_step(pTableInfo);
611 while( rc==SQLITE_ROW ){
612 zSelect = appendText(zSelect, "dump_literal(", 0);
613 zSelect = appendText(zSelect, sqlite3_column_text(pTableInfo, 1), '"');
614 rc = sqlite3_step(pTableInfo);
615 if( rc==SQLITE_ROW ){
616 zSelect = appendText(zSelect, ") || ', ' || ", 0);
617 }else{
618 zSelect = appendText(zSelect, ") ", 0);
619 }
620 }
621 rc = sqlite3_finalize(pTableInfo);
622 if( rc!=SQLITE_OK ){
623 if( zSelect ) free(zSelect);
624 return 1;
625 }
626 zSelect = appendText(zSelect, "|| ')' FROM ", 0);
627 zSelect = appendText(zSelect, zTable, '"');
628
629 rc = sqlite3_prepare(p->db, zSelect, -1, &pSelect, 0);
630 if( zSelect ) free(zSelect);
631 if( rc!=SQLITE_OK || !pSelect ){
632 return 1;
633 }
634
635 rc = sqlite3_step(pSelect);
636 while( rc==SQLITE_ROW ){
637 fprintf(p->out, "%s;\n", sqlite3_column_text(pSelect, 0));
638 rc = sqlite3_step(pSelect);
639 }
640 rc = sqlite3_finalize(pSelect);
641 if( rc!=SQLITE_OK ){
642 return 1;
643 }
drh4c653a02000-06-07 01:27:47 +0000644 }
danielk19772a02e332004-06-05 08:04:36 +0000645
drh4c653a02000-06-07 01:27:47 +0000646 return 0;
647}
648
649/*
drh75897232000-05-29 14:26:00 +0000650** Text of a help message
651*/
persicom1d0b8722002-04-18 02:53:04 +0000652static char zHelp[] =
jplyon6a65bb32003-05-04 07:25:57 +0000653 ".databases List names and files of attached databases\n"
654 ".dump ?TABLE? ... Dump the database in a text format\n"
drhdaffd0e2001-04-11 14:28:42 +0000655 ".echo ON|OFF Turn command echo on or off\n"
drh75897232000-05-29 14:26:00 +0000656 ".exit Exit this program\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000657 ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000658 ".header(s) ON|OFF Turn display of headers on or off\n"
drh75897232000-05-29 14:26:00 +0000659 ".help Show this message\n"
660 ".indices TABLE Show names of all indices on TABLE\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000661 ".mode MODE Set mode to one of \"line(s)\", \"column(s)\", \n"
drhe3710332000-09-29 13:30:53 +0000662 " \"insert\", \"list\", or \"html\"\n"
drhc08a4f12000-06-15 16:49:48 +0000663 ".mode insert TABLE Generate SQL insert statements for TABLE\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000664 ".nullvalue STRING Print STRING instead of nothing for NULL data\n"
drh75897232000-05-29 14:26:00 +0000665 ".output FILENAME Send output to FILENAME\n"
666 ".output stdout Send output to the screen\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000667 ".prompt MAIN CONTINUE Replace the standard prompts\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000668 ".quit Exit this program\n"
drhdaffd0e2001-04-11 14:28:42 +0000669 ".read FILENAME Execute SQL in FILENAME\n"
drh9eb9e262004-02-11 02:18:05 +0000670#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000671 ".rekey OLD NEW NEW Change the encryption key\n"
672#endif
drh75897232000-05-29 14:26:00 +0000673 ".schema ?TABLE? Show the CREATE statements\n"
674 ".separator STRING Change separator string for \"list\" mode\n"
drhdd45df82002-04-18 12:39:03 +0000675 ".show Show the current values for various settings\n"
drha50da102000-08-08 20:19:09 +0000676 ".tables ?PATTERN? List names of tables matching a pattern\n"
drh2dfbbca2000-07-28 14:32:48 +0000677 ".timeout MS Try opening locked tables for MS milliseconds\n"
drh75897232000-05-29 14:26:00 +0000678 ".width NUM NUM ... Set column widths for \"column\" mode\n"
679;
680
drhdaffd0e2001-04-11 14:28:42 +0000681/* Forward reference */
682static void process_input(struct callback_data *p, FILE *in);
683
drh75897232000-05-29 14:26:00 +0000684/*
drh44c2eb12003-04-30 11:38:26 +0000685** Make sure the database is open. If it is not, then open it. If
686** the database fails to open, print an error message and exit.
687*/
688static void open_db(struct callback_data *p){
689 if( p->db==0 ){
drh9eb9e262004-02-11 02:18:05 +0000690#ifdef SQLITE_HAS_CODEC
drheb8ed702004-02-11 10:37:23 +0000691 int n = p->zKey ? strlen(p->zKey) : 0;
danielk19776f8a5032004-05-10 10:34:51 +0000692 db = p->db = sqlite3_open_encrypted(p->zDbFilename, p->zKey, n, 0, &zErrMsg);
danielk197780290862004-05-22 09:21:21 +0000693 assert(0); /* Encrypted databases are broken in SQLite 3 */
drheb8ed702004-02-11 10:37:23 +0000694#else
danielk197780290862004-05-22 09:21:21 +0000695 sqlite3_open(p->zDbFilename, &p->db, 0);
696 db = p->db;
drheb8ed702004-02-11 10:37:23 +0000697#endif
danielk197780290862004-05-22 09:21:21 +0000698 if( SQLITE_OK!=sqlite3_errcode(db) ){
699 fprintf(stderr,"Unable to open database \"%s\": %s\n",
700 p->zDbFilename, sqlite3_errmsg(db));
drh22fbcb82004-02-01 01:22:50 +0000701 exit(1);
drh44c2eb12003-04-30 11:38:26 +0000702 }
danielk19772a02e332004-06-05 08:04:36 +0000703
704 /* Add the 'dump_literal' SQL function used by .dump */
705 sqlite3_create_function(db,"dump_literal",1,0,0,0,dump_literalFunc,0,0);
drh44c2eb12003-04-30 11:38:26 +0000706 }
707}
708
709/*
drh75897232000-05-29 14:26:00 +0000710** If an input line begins with "." then invoke this routine to
711** process that line.
drh67505e72002-04-19 12:34:06 +0000712**
713** Return 1 to exit and 0 to continue.
drh75897232000-05-29 14:26:00 +0000714*/
drh44c2eb12003-04-30 11:38:26 +0000715static int do_meta_command(char *zLine, struct callback_data *p){
drh75897232000-05-29 14:26:00 +0000716 int i = 1;
717 int nArg = 0;
718 int n, c;
drh67505e72002-04-19 12:34:06 +0000719 int rc = 0;
drh75897232000-05-29 14:26:00 +0000720 char *azArg[50];
721
722 /* Parse the input line into tokens.
723 */
724 while( zLine[i] && nArg<ArraySize(azArg) ){
725 while( isspace(zLine[i]) ){ i++; }
drh06333682004-03-09 13:37:45 +0000726 if( zLine[i]==0 ) break;
drh75897232000-05-29 14:26:00 +0000727 if( zLine[i]=='\'' || zLine[i]=='"' ){
728 int delim = zLine[i++];
729 azArg[nArg++] = &zLine[i];
730 while( zLine[i] && zLine[i]!=delim ){ i++; }
731 if( zLine[i]==delim ){
732 zLine[i++] = 0;
733 }
734 }else{
735 azArg[nArg++] = &zLine[i];
736 while( zLine[i] && !isspace(zLine[i]) ){ i++; }
737 if( zLine[i] ) zLine[i++] = 0;
738 }
739 }
740
741 /* Process the input line.
742 */
drh67505e72002-04-19 12:34:06 +0000743 if( nArg==0 ) return rc;
drh75897232000-05-29 14:26:00 +0000744 n = strlen(azArg[0]);
745 c = azArg[0][0];
jplyon6a65bb32003-05-04 07:25:57 +0000746 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
jplyon672a1ed2003-05-11 20:07:05 +0000747 struct callback_data data;
748 char *zErrMsg = 0;
jplyon6a65bb32003-05-04 07:25:57 +0000749 open_db(p);
jplyon672a1ed2003-05-11 20:07:05 +0000750 memcpy(&data, p, sizeof(data));
drhd8885442004-03-17 23:42:12 +0000751 data.showHeader = 1;
jplyon672a1ed2003-05-11 20:07:05 +0000752 data.mode = MODE_Column;
drhd8885442004-03-17 23:42:12 +0000753 data.colWidth[0] = 3;
754 data.colWidth[1] = 15;
755 data.colWidth[2] = 58;
danielk19776f8a5032004-05-10 10:34:51 +0000756 sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
jplyon672a1ed2003-05-11 20:07:05 +0000757 if( zErrMsg ){
758 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000759 sqlite3_free(zErrMsg);
jplyon6a65bb32003-05-04 07:25:57 +0000760 }
761 }else
762
drh4c653a02000-06-07 01:27:47 +0000763 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
764 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000765 open_db(p);
drh33048c02001-10-01 14:29:22 +0000766 fprintf(p->out, "BEGIN TRANSACTION;\n");
drh4c653a02000-06-07 01:27:47 +0000767 if( nArg==1 ){
danielk19776f8a5032004-05-10 10:34:51 +0000768 sqlite3_exec(p->db,
drha18c5682000-10-08 22:20:57 +0000769 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000770 "WHERE type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000771 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000772 dump_callback, p, &zErrMsg
773 );
drh4c653a02000-06-07 01:27:47 +0000774 }else{
775 int i;
776 for(i=1; i<nArg && zErrMsg==0; i++){
danielk19776f8a5032004-05-10 10:34:51 +0000777 sqlite3_exec_printf(p->db,
drha18c5682000-10-08 22:20:57 +0000778 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000779 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOT NULL "
drhfc6cdfe2002-04-13 23:42:24 +0000780 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +0000781 dump_callback, p, &zErrMsg, azArg[i]
782 );
drh4c653a02000-06-07 01:27:47 +0000783 }
784 }
785 if( zErrMsg ){
786 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000787 sqlite3_free(zErrMsg);
drh33048c02001-10-01 14:29:22 +0000788 }else{
789 fprintf(p->out, "COMMIT;\n");
drh4c653a02000-06-07 01:27:47 +0000790 }
791 }else
drh75897232000-05-29 14:26:00 +0000792
drhdaffd0e2001-04-11 14:28:42 +0000793 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
794 int j;
795 char *z = azArg[1];
796 int val = atoi(azArg[1]);
797 for(j=0; z[j]; j++){
798 if( isupper(z[j]) ) z[j] = tolower(z[j]);
799 }
800 if( strcmp(z,"on")==0 ){
801 val = 1;
802 }else if( strcmp(z,"yes")==0 ){
803 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000804 }
drhdaffd0e2001-04-11 14:28:42 +0000805 p->echoOn = val;
806 }else
807
drh75897232000-05-29 14:26:00 +0000808 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
drh67505e72002-04-19 12:34:06 +0000809 rc = 1;
drh75897232000-05-29 14:26:00 +0000810 }else
811
drhdd45df82002-04-18 12:39:03 +0000812 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +0000813 int j;
drhdd45df82002-04-18 12:39:03 +0000814 char *z = nArg>=2 ? azArg[1] : "1";
815 int val = atoi(z);
persicom7e2dfdd2002-04-18 02:46:52 +0000816 for(j=0; z[j]; j++){
817 if( isupper(z[j]) ) z[j] = tolower(z[j]);
818 }
819 if( strcmp(z,"on")==0 ){
820 val = 1;
821 }else if( strcmp(z,"yes")==0 ){
822 val = 1;
823 }
824 if(val == 1) {
825 if(!p->explainPrev.valid) {
826 p->explainPrev.valid = 1;
827 p->explainPrev.mode = p->mode;
828 p->explainPrev.showHeader = p->showHeader;
829 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
830 }
831 /* We could put this code under the !p->explainValid
832 ** condition so that it does not execute if we are already in
833 ** explain mode. However, always executing it allows us an easy
834 ** was to reset to explain mode in case the user previously
835 ** did an .explain followed by a .width, .mode or .header
836 ** command.
837 */
838 p->mode = MODE_Column;
839 p->showHeader = 1;
840 memset(p->colWidth,0,ArraySize(p->colWidth));
841 p->colWidth[0] = 4;
842 p->colWidth[1] = 12;
843 p->colWidth[2] = 10;
844 p->colWidth[3] = 10;
845 p->colWidth[4] = 35;
846 }else if (p->explainPrev.valid) {
847 p->explainPrev.valid = 0;
848 p->mode = p->explainPrev.mode;
849 p->showHeader = p->explainPrev.showHeader;
850 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
851 }
drh75897232000-05-29 14:26:00 +0000852 }else
853
persicom7e2dfdd2002-04-18 02:46:52 +0000854 if( c=='h' && (strncmp(azArg[0], "header", n)==0
855 ||
856 strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){
drh75897232000-05-29 14:26:00 +0000857 int j;
858 char *z = azArg[1];
859 int val = atoi(azArg[1]);
860 for(j=0; z[j]; j++){
861 if( isupper(z[j]) ) z[j] = tolower(z[j]);
862 }
863 if( strcmp(z,"on")==0 ){
864 val = 1;
865 }else if( strcmp(z,"yes")==0 ){
866 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000867 }
drh75897232000-05-29 14:26:00 +0000868 p->showHeader = val;
869 }else
870
871 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
872 fprintf(stderr,zHelp);
873 }else
874
875 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
876 struct callback_data data;
877 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000878 open_db(p);
drh75897232000-05-29 14:26:00 +0000879 memcpy(&data, p, sizeof(data));
880 data.showHeader = 0;
881 data.mode = MODE_List;
danielk19776f8a5032004-05-10 10:34:51 +0000882 sqlite3_exec_printf(p->db,
drha18c5682000-10-08 22:20:57 +0000883 "SELECT name FROM sqlite_master "
884 "WHERE type='index' AND tbl_name LIKE '%q' "
drhe0bc4042002-06-25 01:09:11 +0000885 "UNION ALL "
886 "SELECT name FROM sqlite_temp_master "
887 "WHERE type='index' AND tbl_name LIKE '%q' "
888 "ORDER BY 1",
889 callback, &data, &zErrMsg, azArg[1], azArg[1]
drha18c5682000-10-08 22:20:57 +0000890 );
drh75897232000-05-29 14:26:00 +0000891 if( zErrMsg ){
892 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000893 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +0000894 }
895 }else
896
drh28bd4bc2000-06-15 15:57:22 +0000897 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
drh75897232000-05-29 14:26:00 +0000898 int n2 = strlen(azArg[1]);
persicom7e2dfdd2002-04-18 02:46:52 +0000899 if( strncmp(azArg[1],"line",n2)==0
900 ||
901 strncmp(azArg[1],"lines",n2)==0 ){
drh75897232000-05-29 14:26:00 +0000902 p->mode = MODE_Line;
persicom7e2dfdd2002-04-18 02:46:52 +0000903 }else if( strncmp(azArg[1],"column",n2)==0
904 ||
905 strncmp(azArg[1],"columns",n2)==0 ){
drh75897232000-05-29 14:26:00 +0000906 p->mode = MODE_Column;
907 }else if( strncmp(azArg[1],"list",n2)==0 ){
908 p->mode = MODE_List;
drh1e5d0e92000-05-31 23:33:17 +0000909 }else if( strncmp(azArg[1],"html",n2)==0 ){
910 p->mode = MODE_Html;
drh28bd4bc2000-06-15 15:57:22 +0000911 }else if( strncmp(azArg[1],"insert",n2)==0 ){
912 p->mode = MODE_Insert;
913 if( nArg>=3 ){
drh33048c02001-10-01 14:29:22 +0000914 set_table_name(p, azArg[2]);
drh28bd4bc2000-06-15 15:57:22 +0000915 }else{
drh33048c02001-10-01 14:29:22 +0000916 set_table_name(p, "table");
drh28bd4bc2000-06-15 15:57:22 +0000917 }
drhdaffd0e2001-04-11 14:28:42 +0000918 }else {
919 fprintf(stderr,"mode should be on of: column html insert line list\n");
drh75897232000-05-29 14:26:00 +0000920 }
921 }else
922
persicom7e2dfdd2002-04-18 02:46:52 +0000923 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
924 sprintf(p->nullvalue, "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
925 }else
926
drh75897232000-05-29 14:26:00 +0000927 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
928 if( p->out!=stdout ){
929 fclose(p->out);
930 }
931 if( strcmp(azArg[1],"stdout")==0 ){
932 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +0000933 strcpy(p->outfile,"stdout");
drh75897232000-05-29 14:26:00 +0000934 }else{
drha1f9b5e2004-02-14 16:31:02 +0000935 p->out = fopen(azArg[1], "wb");
drh75897232000-05-29 14:26:00 +0000936 if( p->out==0 ){
937 fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
938 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +0000939 } else {
940 strcpy(p->outfile,azArg[1]);
drh75897232000-05-29 14:26:00 +0000941 }
942 }
943 }else
944
drhdd45df82002-04-18 12:39:03 +0000945 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
persicom7e2dfdd2002-04-18 02:46:52 +0000946 if( nArg >= 2) {
947 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
948 }
949 if( nArg >= 3) {
950 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
951 }
952 }else
953
954 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
drhf73287c2004-02-25 02:25:37 +0000955 rc = 1;
persicom7e2dfdd2002-04-18 02:46:52 +0000956 }else
957
drhdaffd0e2001-04-11 14:28:42 +0000958 if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
drha1f9b5e2004-02-14 16:31:02 +0000959 FILE *alt = fopen(azArg[1], "rb");
drhdaffd0e2001-04-11 14:28:42 +0000960 if( alt==0 ){
961 fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
962 }else{
963 process_input(p, alt);
964 fclose(alt);
965 }
966 }else
967
drh9eb9e262004-02-11 02:18:05 +0000968#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000969 if( c=='r' && strncmp(azArg[0],"rekey", n)==0 && nArg==4 ){
970 char *zOld = p->zKey;
971 if( zOld==0 ) zOld = "";
972 if( strcmp(azArg[1],zOld) ){
973 fprintf(stderr,"old key is incorrect\n");
974 }else if( strcmp(azArg[2], azArg[3]) ){
975 fprintf(stderr,"2nd copy of new key does not match the 1st\n");
976 }else{
drh3f4fedb2004-05-31 19:34:33 +0000977 sqlite3_free(p->zKey);
danielk19776f8a5032004-05-10 10:34:51 +0000978 p->zKey = sqlite3_mprintf("%s", azArg[2]);
drh22fbcb82004-02-01 01:22:50 +0000979 sqlite_rekey(p->db, p->zKey, strlen(p->zKey));
980 }
981 }else
982#endif
983
drh75897232000-05-29 14:26:00 +0000984 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
985 struct callback_data data;
986 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000987 open_db(p);
drh75897232000-05-29 14:26:00 +0000988 memcpy(&data, p, sizeof(data));
989 data.showHeader = 0;
drhe3710332000-09-29 13:30:53 +0000990 data.mode = MODE_Semi;
drh75897232000-05-29 14:26:00 +0000991 if( nArg>1 ){
danielk19774adee202004-05-08 08:23:19 +0000992 extern int sqlite3StrICmp(const char*,const char*);
993 if( sqlite3StrICmp(azArg[1],"sqlite_master")==0 ){
drha18c5682000-10-08 22:20:57 +0000994 char *new_argv[2], *new_colv[2];
995 new_argv[0] = "CREATE TABLE sqlite_master (\n"
996 " type text,\n"
997 " name text,\n"
998 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000999 " rootpage integer,\n"
drha18c5682000-10-08 22:20:57 +00001000 " sql text\n"
1001 ")";
1002 new_argv[1] = 0;
1003 new_colv[0] = "sql";
1004 new_colv[1] = 0;
1005 callback(&data, 1, new_argv, new_colv);
danielk19774adee202004-05-08 08:23:19 +00001006 }else if( sqlite3StrICmp(azArg[1],"sqlite_temp_master")==0 ){
drhe0bc4042002-06-25 01:09:11 +00001007 char *new_argv[2], *new_colv[2];
1008 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
1009 " type text,\n"
1010 " name text,\n"
1011 " tbl_name text,\n"
1012 " rootpage integer,\n"
1013 " sql text\n"
1014 ")";
1015 new_argv[1] = 0;
1016 new_colv[0] = "sql";
1017 new_colv[1] = 0;
1018 callback(&data, 1, new_argv, new_colv);
drha18c5682000-10-08 22:20:57 +00001019 }else{
danielk19776f8a5032004-05-10 10:34:51 +00001020 sqlite3_exec_printf(p->db,
drhe0bc4042002-06-25 01:09:11 +00001021 "SELECT sql FROM "
1022 " (SELECT * FROM sqlite_master UNION ALL"
1023 " SELECT * FROM sqlite_temp_master) "
drh33048c02001-10-01 14:29:22 +00001024 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +00001025 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +00001026 callback, &data, &zErrMsg, azArg[1]);
1027 }
drh75897232000-05-29 14:26:00 +00001028 }else{
danielk19776f8a5032004-05-10 10:34:51 +00001029 sqlite3_exec(p->db,
drhe0bc4042002-06-25 01:09:11 +00001030 "SELECT sql FROM "
1031 " (SELECT * FROM sqlite_master UNION ALL"
1032 " SELECT * FROM sqlite_temp_master) "
drh33048c02001-10-01 14:29:22 +00001033 "WHERE type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +00001034 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +00001035 callback, &data, &zErrMsg
1036 );
drh75897232000-05-29 14:26:00 +00001037 }
drh75897232000-05-29 14:26:00 +00001038 if( zErrMsg ){
1039 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001040 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00001041 }
1042 }else
1043
1044 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
1045 sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
1046 }else
1047
persicom7e2dfdd2002-04-18 02:46:52 +00001048 if( c=='s' && strncmp(azArg[0], "show", n)==0){
1049 int i;
1050 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
drh67505e72002-04-19 12:34:06 +00001051 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
drhdd45df82002-04-18 12:39:03 +00001052 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
persicom7e2dfdd2002-04-18 02:46:52 +00001053 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
1054 fprintf(p->out,"%9.9s: %s\n","nullvalue", p->nullvalue);
drh67505e72002-04-19 12:34:06 +00001055 fprintf(p->out,"%9.9s: %s\n","output",
1056 strlen(p->outfile) ? p->outfile : "stdout");
persicom7e2dfdd2002-04-18 02:46:52 +00001057 fprintf(p->out,"%9.9s: %s\n","separator", p->separator);
1058 fprintf(p->out,"%9.9s: ","width");
1059 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
1060 fprintf(p->out,"%d ",p->colWidth[i]);
1061 }
1062 fprintf(p->out,"\n\n");
1063 }else
1064
drh2dfbbca2000-07-28 14:32:48 +00001065 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
drhe3710332000-09-29 13:30:53 +00001066 char **azResult;
1067 int nRow, rc;
1068 char *zErrMsg;
drh44c2eb12003-04-30 11:38:26 +00001069 open_db(p);
drha50da102000-08-08 20:19:09 +00001070 if( nArg==1 ){
danielk19776f8a5032004-05-10 10:34:51 +00001071 rc = sqlite3_get_table(p->db,
drha50da102000-08-08 20:19:09 +00001072 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +00001073 "WHERE type IN ('table','view') "
drhe0bc4042002-06-25 01:09:11 +00001074 "UNION ALL "
1075 "SELECT name FROM sqlite_temp_master "
1076 "WHERE type IN ('table','view') "
1077 "ORDER BY 1",
drha18c5682000-10-08 22:20:57 +00001078 &azResult, &nRow, 0, &zErrMsg
1079 );
drha50da102000-08-08 20:19:09 +00001080 }else{
danielk19776f8a5032004-05-10 10:34:51 +00001081 rc = sqlite3_get_table_printf(p->db,
drha50da102000-08-08 20:19:09 +00001082 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +00001083 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
drhe0bc4042002-06-25 01:09:11 +00001084 "UNION ALL "
1085 "SELECT name FROM sqlite_temp_master "
1086 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
1087 "ORDER BY 1",
1088 &azResult, &nRow, 0, &zErrMsg, azArg[1], azArg[1]
drha18c5682000-10-08 22:20:57 +00001089 );
drha50da102000-08-08 20:19:09 +00001090 }
drh75897232000-05-29 14:26:00 +00001091 if( zErrMsg ){
1092 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001093 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00001094 }
drhe3710332000-09-29 13:30:53 +00001095 if( rc==SQLITE_OK ){
1096 int len, maxlen = 0;
1097 int i, j;
1098 int nPrintCol, nPrintRow;
1099 for(i=1; i<=nRow; i++){
1100 if( azResult[i]==0 ) continue;
1101 len = strlen(azResult[i]);
1102 if( len>maxlen ) maxlen = len;
1103 }
1104 nPrintCol = 80/(maxlen+2);
1105 if( nPrintCol<1 ) nPrintCol = 1;
1106 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
1107 for(i=0; i<nPrintRow; i++){
1108 for(j=i+1; j<=nRow; j+=nPrintRow){
1109 char *zSp = j<=nPrintRow ? "" : " ";
1110 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
1111 }
1112 printf("\n");
1113 }
1114 }
danielk19776f8a5032004-05-10 10:34:51 +00001115 sqlite3_free_table(azResult);
drh75897232000-05-29 14:26:00 +00001116 }else
1117
drh2dfbbca2000-07-28 14:32:48 +00001118 if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
drh44c2eb12003-04-30 11:38:26 +00001119 open_db(p);
danielk19776f8a5032004-05-10 10:34:51 +00001120 sqlite3_busy_timeout(p->db, atoi(azArg[1]));
drh2dfbbca2000-07-28 14:32:48 +00001121 }else
1122
drh75897232000-05-29 14:26:00 +00001123 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
1124 int j;
1125 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
1126 p->colWidth[j-1] = atoi(azArg[j]);
1127 }
1128 }else
1129
1130 {
drh67505e72002-04-19 12:34:06 +00001131 fprintf(stderr, "unknown command or invalid arguments: "
1132 " \"%s\". Enter \".help\" for help\n", azArg[0]);
drh75897232000-05-29 14:26:00 +00001133 }
drh67505e72002-04-19 12:34:06 +00001134
1135 return rc;
drh75897232000-05-29 14:26:00 +00001136}
1137
drh67505e72002-04-19 12:34:06 +00001138/*
drh324ccef2003-02-05 14:06:20 +00001139** Return TRUE if the last non-whitespace character in z[] is a semicolon.
1140** z[] is N characters long.
1141*/
1142static int _ends_with_semicolon(const char *z, int N){
1143 while( N>0 && isspace(z[N-1]) ){ N--; }
1144 return N>0 && z[N-1]==';';
1145}
1146
1147/*
drh70c7a4b2003-04-26 03:03:06 +00001148** Test to see if a line consists entirely of whitespace.
1149*/
1150static int _all_whitespace(const char *z){
1151 for(; *z; z++){
1152 if( isspace(*z) ) continue;
1153 if( *z=='/' && z[1]=='*' ){
1154 z += 2;
1155 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
1156 if( *z==0 ) return 0;
1157 z++;
1158 continue;
1159 }
1160 if( *z=='-' && z[1]=='-' ){
1161 z += 2;
1162 while( *z && *z!='\n' ){ z++; }
1163 if( *z==0 ) return 1;
1164 continue;
1165 }
1166 return 0;
1167 }
1168 return 1;
1169}
1170
1171/*
drha9b17162003-04-29 18:01:28 +00001172** Return TRUE if the line typed in is an SQL command terminator other
1173** than a semi-colon. The SQL Server style "go" command is understood
1174** as is the Oracle "/".
1175*/
1176static int _is_command_terminator(const char *zLine){
danielk19774adee202004-05-08 08:23:19 +00001177 extern int sqlite3StrNICmp(const char*,const char*,int);
drha9b17162003-04-29 18:01:28 +00001178 while( isspace(*zLine) ){ zLine++; };
1179 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ) return 1; /* Oracle */
danielk19774adee202004-05-08 08:23:19 +00001180 if( sqlite3StrNICmp(zLine,"go",2)==0 && _all_whitespace(&zLine[2]) ){
drha9b17162003-04-29 18:01:28 +00001181 return 1; /* SQL Server */
1182 }
1183 return 0;
1184}
1185
1186/*
drh67505e72002-04-19 12:34:06 +00001187** Read input from *in and process it. If *in==0 then input
1188** is interactive - the user is typing it it. Otherwise, input
1189** is coming from a file or device. A prompt is issued and history
1190** is saved only if input is interactive. An interrupt signal will
1191** cause this routine to exit immediately, unless input is interactive.
1192*/
drhdaffd0e2001-04-11 14:28:42 +00001193static void process_input(struct callback_data *p, FILE *in){
1194 char *zLine;
1195 char *zSql = 0;
1196 int nSql = 0;
1197 char *zErrMsg;
drh7f953e22002-07-13 17:33:45 +00001198 int rc;
drh41e941d2002-04-04 15:10:12 +00001199 while( fflush(p->out), (zLine = one_input_line(zSql, in))!=0 ){
drh67505e72002-04-19 12:34:06 +00001200 if( seenInterrupt ){
1201 if( in!=0 ) break;
1202 seenInterrupt = 0;
1203 }
drhdaffd0e2001-04-11 14:28:42 +00001204 if( p->echoOn ) printf("%s\n", zLine);
drhf817b6b2003-06-16 00:16:41 +00001205 if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
drh2af0b2d2002-02-21 02:25:02 +00001206 if( zLine && zLine[0]=='.' && nSql==0 ){
drh44c2eb12003-04-30 11:38:26 +00001207 int rc = do_meta_command(zLine, p);
drhdaffd0e2001-04-11 14:28:42 +00001208 free(zLine);
drh67505e72002-04-19 12:34:06 +00001209 if( rc ) break;
drhdaffd0e2001-04-11 14:28:42 +00001210 continue;
1211 }
drha9b17162003-04-29 18:01:28 +00001212 if( _is_command_terminator(zLine) ){
1213 strcpy(zLine,";");
1214 }
drhdaffd0e2001-04-11 14:28:42 +00001215 if( zSql==0 ){
1216 int i;
1217 for(i=0; zLine[i] && isspace(zLine[i]); i++){}
1218 if( zLine[i]!=0 ){
1219 nSql = strlen(zLine);
1220 zSql = malloc( nSql+1 );
1221 strcpy(zSql, zLine);
1222 }
1223 }else{
1224 int len = strlen(zLine);
1225 zSql = realloc( zSql, nSql + len + 2 );
1226 if( zSql==0 ){
1227 fprintf(stderr,"%s: out of memory!\n", Argv0);
1228 exit(1);
1229 }
1230 strcpy(&zSql[nSql++], "\n");
1231 strcpy(&zSql[nSql], zLine);
1232 nSql += len;
1233 }
1234 free(zLine);
danielk19776f8a5032004-05-10 10:34:51 +00001235 if( zSql && _ends_with_semicolon(zSql, nSql) && sqlite3_complete(zSql) ){
drhdaffd0e2001-04-11 14:28:42 +00001236 p->cnt = 0;
drh44c2eb12003-04-30 11:38:26 +00001237 open_db(p);
danielk19776f8a5032004-05-10 10:34:51 +00001238 rc = sqlite3_exec(p->db, zSql, callback, p, &zErrMsg);
drh7f953e22002-07-13 17:33:45 +00001239 if( rc || zErrMsg ){
drhdaffd0e2001-04-11 14:28:42 +00001240 if( in!=0 && !p->echoOn ) printf("%s\n",zSql);
drh7f953e22002-07-13 17:33:45 +00001241 if( zErrMsg!=0 ){
1242 printf("SQL error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001243 sqlite3_free(zErrMsg);
drh7f953e22002-07-13 17:33:45 +00001244 zErrMsg = 0;
1245 }else{
danielk19772a02e332004-06-05 08:04:36 +00001246 printf("SQL error: %s\n", sqlite3_errmsg(p->db));
drh7f953e22002-07-13 17:33:45 +00001247 }
drhdaffd0e2001-04-11 14:28:42 +00001248 }
1249 free(zSql);
1250 zSql = 0;
1251 nSql = 0;
1252 }
1253 }
1254 if( zSql ){
drh70c7a4b2003-04-26 03:03:06 +00001255 if( !_all_whitespace(zSql) ) printf("Incomplete SQL: %s\n", zSql);
drhdaffd0e2001-04-11 14:28:42 +00001256 free(zSql);
1257 }
1258}
1259
drh67505e72002-04-19 12:34:06 +00001260/*
1261** Return a pathname which is the user's home directory. A
1262** 0 return indicates an error of some kind. Space to hold the
1263** resulting string is obtained from malloc(). The calling
1264** function should free the result.
1265*/
1266static char *find_home_dir(void){
1267 char *home_dir = NULL;
persicom7e2dfdd2002-04-18 02:46:52 +00001268
drh820f3812003-01-08 13:02:52 +00001269#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
drh67505e72002-04-19 12:34:06 +00001270 struct passwd *pwent;
1271 uid_t uid = getuid();
drhbd842ba2002-08-21 11:26:41 +00001272 if( (pwent=getpwuid(uid)) != NULL) {
1273 home_dir = pwent->pw_dir;
drh67505e72002-04-19 12:34:06 +00001274 }
1275#endif
1276
drh820f3812003-01-08 13:02:52 +00001277#ifdef __MACOS__
1278 char home_path[_MAX_PATH+1];
1279 home_dir = getcwd(home_path, _MAX_PATH);
1280#endif
1281
drh67505e72002-04-19 12:34:06 +00001282 if (!home_dir) {
1283 home_dir = getenv("HOME");
1284 if (!home_dir) {
1285 home_dir = getenv("HOMEPATH"); /* Windows? */
1286 }
1287 }
1288
drhe98d4fa2002-04-21 19:06:22 +00001289#if defined(_WIN32) || defined(WIN32)
1290 if (!home_dir) {
1291 home_dir = "c:";
1292 }
1293#endif
1294
drh67505e72002-04-19 12:34:06 +00001295 if( home_dir ){
1296 char *z = malloc( strlen(home_dir)+1 );
1297 if( z ) strcpy(z, home_dir);
1298 home_dir = z;
1299 }
drhe98d4fa2002-04-21 19:06:22 +00001300
drh67505e72002-04-19 12:34:06 +00001301 return home_dir;
1302}
1303
1304/*
1305** Read input from the file given by sqliterc_override. Or if that
1306** parameter is NULL, take input from ~/.sqliterc
1307*/
drh22fbcb82004-02-01 01:22:50 +00001308static void process_sqliterc(
1309 struct callback_data *p, /* Configuration data */
1310 const char *sqliterc_override /* Name of config file. NULL to use default */
1311){
persicom7e2dfdd2002-04-18 02:46:52 +00001312 char *home_dir = NULL;
drh22fbcb82004-02-01 01:22:50 +00001313 const char *sqliterc = sqliterc_override;
1314 char *zBuf;
persicom7e2dfdd2002-04-18 02:46:52 +00001315 FILE *in = NULL;
1316
1317 if (sqliterc == NULL) {
drh67505e72002-04-19 12:34:06 +00001318 home_dir = find_home_dir();
drhe98d4fa2002-04-21 19:06:22 +00001319 if( home_dir==0 ){
1320 fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0);
1321 return;
1322 }
drh22fbcb82004-02-01 01:22:50 +00001323 zBuf = malloc(strlen(home_dir) + 15);
1324 if( zBuf==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +00001325 fprintf(stderr,"%s: out of memory!\n", Argv0);
1326 exit(1);
1327 }
drh22fbcb82004-02-01 01:22:50 +00001328 sprintf(zBuf,"%s/.sqliterc",home_dir);
drh67505e72002-04-19 12:34:06 +00001329 free(home_dir);
drh22fbcb82004-02-01 01:22:50 +00001330 sqliterc = (const char*)zBuf;
persicom7e2dfdd2002-04-18 02:46:52 +00001331 }
drha1f9b5e2004-02-14 16:31:02 +00001332 in = fopen(sqliterc,"rb");
drh22fbcb82004-02-01 01:22:50 +00001333 if( in ){
1334 if( isatty(fileno(stdout)) ){
1335 printf("Loading resources from %s\n",sqliterc);
1336 }
persicom7e2dfdd2002-04-18 02:46:52 +00001337 process_input(p,in);
drhdd45df82002-04-18 12:39:03 +00001338 fclose(in);
persicom7e2dfdd2002-04-18 02:46:52 +00001339 }
1340 return;
1341}
1342
drh67505e72002-04-19 12:34:06 +00001343/*
drhe1e38c42003-05-04 18:30:59 +00001344** Show available command line options
1345*/
1346static const char zOptions[] =
1347 " -init filename read/process named file\n"
1348 " -echo print commands before execution\n"
1349 " -[no]header turn headers on or off\n"
1350 " -column set output mode to 'column'\n"
1351 " -html set output mode to HTML\n"
drh9eb9e262004-02-11 02:18:05 +00001352#ifdef SQLITE_HAS_CODEC
drh57ced912004-02-10 02:57:59 +00001353 " -key KEY encryption key\n"
1354#endif
drhe1e38c42003-05-04 18:30:59 +00001355 " -line set output mode to 'line'\n"
1356 " -list set output mode to 'list'\n"
1357 " -separator 'x' set output field separator (|)\n"
1358 " -nullvalue 'text' set text string for NULL values\n"
1359 " -version show SQLite version\n"
1360 " -help show this text, also show dot-commands\n"
1361;
1362static void usage(int showDetail){
1363 fprintf(stderr, "Usage: %s [OPTIONS] FILENAME [SQL]\n", Argv0);
1364 if( showDetail ){
1365 fprintf(stderr, "Options are:\n%s", zOptions);
1366 }else{
1367 fprintf(stderr, "Use the -help option for additional information\n");
1368 }
1369 exit(1);
1370}
1371
1372/*
drh67505e72002-04-19 12:34:06 +00001373** Initialize the state information in data
1374*/
persicom7e2dfdd2002-04-18 02:46:52 +00001375void main_init(struct callback_data *data) {
1376 memset(data, 0, sizeof(*data));
1377 data->mode = MODE_List;
1378 strcpy(data->separator,"|");
1379 data->showHeader = 0;
1380 strcpy(mainPrompt,"sqlite> ");
1381 strcpy(continuePrompt," ...> ");
1382}
1383
drh75897232000-05-29 14:26:00 +00001384int main(int argc, char **argv){
drh75897232000-05-29 14:26:00 +00001385 char *zErrMsg = 0;
1386 struct callback_data data;
drh22fbcb82004-02-01 01:22:50 +00001387 const char *zInitFile = 0;
1388 char *zFirstCmd = 0;
drh44c2eb12003-04-30 11:38:26 +00001389 int i;
danielk19774adee202004-05-08 08:23:19 +00001390 extern int sqlite3OsFileExists(const char*);
drh75897232000-05-29 14:26:00 +00001391
drh820f3812003-01-08 13:02:52 +00001392#ifdef __MACOS__
1393 argc = ccommand(&argv);
drh820f3812003-01-08 13:02:52 +00001394#endif
1395
drhdaffd0e2001-04-11 14:28:42 +00001396 Argv0 = argv[0];
persicom7e2dfdd2002-04-18 02:46:52 +00001397 main_init(&data);
persicom7e2dfdd2002-04-18 02:46:52 +00001398
drh44c2eb12003-04-30 11:38:26 +00001399 /* Make sure we have a valid signal handler early, before anything
1400 ** else is done.
1401 */
drh4c504392000-10-16 22:06:40 +00001402#ifdef SIGINT
1403 signal(SIGINT, interrupt_handler);
1404#endif
drh44c2eb12003-04-30 11:38:26 +00001405
drh22fbcb82004-02-01 01:22:50 +00001406 /* Do an initial pass through the command-line argument to locate
1407 ** the name of the database file, the name of the initialization file,
1408 ** and the first command to execute.
drh44c2eb12003-04-30 11:38:26 +00001409 */
drh22fbcb82004-02-01 01:22:50 +00001410 for(i=1; i<argc-1; i++){
drh44c2eb12003-04-30 11:38:26 +00001411 if( argv[i][0]!='-' ) break;
1412 if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){
1413 i++;
drh22fbcb82004-02-01 01:22:50 +00001414 }else if( strcmp(argv[i],"-init")==0 ){
1415 i++;
1416 zInitFile = argv[i];
1417 }else if( strcmp(argv[i],"-key")==0 ){
1418 i++;
danielk19776f8a5032004-05-10 10:34:51 +00001419 data.zKey = sqlite3_mprintf("%s",argv[i]);
drh44c2eb12003-04-30 11:38:26 +00001420 }
1421 }
drh22fbcb82004-02-01 01:22:50 +00001422 if( i<argc ){
1423 data.zDbFilename = argv[i++];
1424 }else{
1425 data.zDbFilename = ":memory:";
1426 }
1427 if( i<argc ){
1428 zFirstCmd = argv[i++];
1429 }
drh44c2eb12003-04-30 11:38:26 +00001430 data.out = stdout;
1431
1432 /* Go ahead and open the database file if it already exists. If the
1433 ** file does not exist, delay opening it. This prevents empty database
1434 ** files from being created if a user mistypes the database name argument
1435 ** to the sqlite command-line tool.
1436 */
danielk19774adee202004-05-08 08:23:19 +00001437 if( sqlite3OsFileExists(data.zDbFilename) ){
drh44c2eb12003-04-30 11:38:26 +00001438 open_db(&data);
1439 }
1440
drh22fbcb82004-02-01 01:22:50 +00001441 /* Process the initialization file if there is one. If no -init option
1442 ** is given on the command line, look for a file named ~/.sqliterc and
1443 ** try to process it.
drh44c2eb12003-04-30 11:38:26 +00001444 */
drh22fbcb82004-02-01 01:22:50 +00001445 process_sqliterc(&data,zInitFile);
drh44c2eb12003-04-30 11:38:26 +00001446
drh22fbcb82004-02-01 01:22:50 +00001447 /* Make a second pass through the command-line argument and set
1448 ** options. This second pass is delayed until after the initialization
1449 ** file is processed so that the command-line arguments will override
1450 ** settings in the initialization file.
drh44c2eb12003-04-30 11:38:26 +00001451 */
drh22fbcb82004-02-01 01:22:50 +00001452 for(i=1; i<argc && argv[i][0]=='-'; i++){
1453 char *z = argv[i];
1454 if( strcmp(z,"-init")==0 || strcmp(z,"-key")==0 ){
1455 i++;
1456 }else if( strcmp(z,"-html")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001457 data.mode = MODE_Html;
drh22fbcb82004-02-01 01:22:50 +00001458 }else if( strcmp(z,"-list")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001459 data.mode = MODE_List;
drh22fbcb82004-02-01 01:22:50 +00001460 }else if( strcmp(z,"-line")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001461 data.mode = MODE_Line;
drh22fbcb82004-02-01 01:22:50 +00001462 }else if( strcmp(z,"-column")==0 ){
drh8b32e172002-04-08 02:42:57 +00001463 data.mode = MODE_Column;
drh22fbcb82004-02-01 01:22:50 +00001464 }else if( strcmp(z,"-separator")==0 ){
1465 i++;
1466 sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[i]);
1467 }else if( strcmp(z,"-nullvalue")==0 ){
1468 i++;
1469 sprintf(data.nullvalue,"%.*s",(int)sizeof(data.nullvalue)-1,argv[i]);
1470 }else if( strcmp(z,"-header")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001471 data.showHeader = 1;
drh22fbcb82004-02-01 01:22:50 +00001472 }else if( strcmp(z,"-noheader")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001473 data.showHeader = 0;
drh22fbcb82004-02-01 01:22:50 +00001474 }else if( strcmp(z,"-echo")==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001475 data.echoOn = 1;
drh22fbcb82004-02-01 01:22:50 +00001476 }else if( strcmp(z,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001477 printf("%s\n", sqlite3_version);
drhe1e38c42003-05-04 18:30:59 +00001478 return 1;
drh22fbcb82004-02-01 01:22:50 +00001479 }else if( strcmp(z,"-help")==0 ){
drhe1e38c42003-05-04 18:30:59 +00001480 usage(1);
drh1e5d0e92000-05-31 23:33:17 +00001481 }else{
drh22fbcb82004-02-01 01:22:50 +00001482 fprintf(stderr,"%s: unknown option: %s\n", Argv0, z);
drhe1e38c42003-05-04 18:30:59 +00001483 fprintf(stderr,"Use -help for a list of options.\n");
drh1e5d0e92000-05-31 23:33:17 +00001484 return 1;
1485 }
1486 }
drh44c2eb12003-04-30 11:38:26 +00001487
drh22fbcb82004-02-01 01:22:50 +00001488 if( zFirstCmd ){
drh44c2eb12003-04-30 11:38:26 +00001489 /* Run just the command that follows the database name
1490 */
drh22fbcb82004-02-01 01:22:50 +00001491 if( zFirstCmd[0]=='.' ){
1492 do_meta_command(zFirstCmd, &data);
drh6ff13852001-11-25 13:18:23 +00001493 exit(0);
1494 }else{
1495 int rc;
drh44c2eb12003-04-30 11:38:26 +00001496 open_db(&data);
danielk19776f8a5032004-05-10 10:34:51 +00001497 rc = sqlite3_exec(data.db, zFirstCmd, callback, &data, &zErrMsg);
drh6ff13852001-11-25 13:18:23 +00001498 if( rc!=0 && zErrMsg!=0 ){
1499 fprintf(stderr,"SQL error: %s\n", zErrMsg);
1500 exit(1);
1501 }
drh75897232000-05-29 14:26:00 +00001502 }
1503 }else{
drh44c2eb12003-04-30 11:38:26 +00001504 /* Run commands received from standard input
1505 */
drh5cf590c2003-04-24 01:45:04 +00001506 if( isatty(fileno(stdout)) && isatty(fileno(stdin)) ){
drh67505e72002-04-19 12:34:06 +00001507 char *zHome;
1508 char *zHistory = 0;
drh75897232000-05-29 14:26:00 +00001509 printf(
drhb217a572000-08-22 13:40:18 +00001510 "SQLite version %s\n"
1511 "Enter \".help\" for instructions\n",
danielk19776f8a5032004-05-10 10:34:51 +00001512 sqlite3_version
drh75897232000-05-29 14:26:00 +00001513 );
drh67505e72002-04-19 12:34:06 +00001514 zHome = find_home_dir();
1515 if( zHome && (zHistory = malloc(strlen(zHome)+20))!=0 ){
1516 sprintf(zHistory,"%s/.sqlite_history", zHome);
1517 }
1518 if( zHistory ) read_history(zHistory);
drhdaffd0e2001-04-11 14:28:42 +00001519 process_input(&data, 0);
drh67505e72002-04-19 12:34:06 +00001520 if( zHistory ){
1521 stifle_history(100);
1522 write_history(zHistory);
1523 }
drhdaffd0e2001-04-11 14:28:42 +00001524 }else{
1525 process_input(&data, stdin);
drh75897232000-05-29 14:26:00 +00001526 }
1527 }
drh33048c02001-10-01 14:29:22 +00001528 set_table_name(&data, 0);
danielk19776f8a5032004-05-10 10:34:51 +00001529 if( db ) sqlite3_close(db);
drh75897232000-05-29 14:26:00 +00001530 return 0;
1531}