1*4520Snw141292 /*
2*4520Snw141292 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
3*4520Snw141292 * Use is subject to license terms.
4*4520Snw141292 */
5*4520Snw141292
6*4520Snw141292 #pragma ident "%Z%%M% %I% %E% SMI"
7*4520Snw141292
8*4520Snw141292 /*
9*4520Snw141292 ** 2001 September 15
10*4520Snw141292 **
11*4520Snw141292 ** The author disclaims copyright to this source code. In place of
12*4520Snw141292 ** a legal notice, here is a blessing:
13*4520Snw141292 **
14*4520Snw141292 ** May you do good and not evil.
15*4520Snw141292 ** May you find forgiveness for yourself and forgive others.
16*4520Snw141292 ** May you share freely, never taking more than you give.
17*4520Snw141292 **
18*4520Snw141292 *************************************************************************
19*4520Snw141292 ** This file contains code to implement the "sqlite" command line
20*4520Snw141292 ** utility for accessing SQLite databases.
21*4520Snw141292 **
22*4520Snw141292 ** $Id: shell.c,v 1.93 2004/03/17 23:42:13 drh Exp $
23*4520Snw141292 */
24*4520Snw141292 #include <stdlib.h>
25*4520Snw141292 #include <string.h>
26*4520Snw141292 #include <stdio.h>
27*4520Snw141292 #include "sqlite.h"
28*4520Snw141292 #include "sqlite-misc.h" /* SUNW addition */
29*4520Snw141292 #include <ctype.h>
30*4520Snw141292
31*4520Snw141292 #if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
32*4520Snw141292 # include <signal.h>
33*4520Snw141292 # include <pwd.h>
34*4520Snw141292 # include <unistd.h>
35*4520Snw141292 # include <sys/types.h>
36*4520Snw141292 #endif
37*4520Snw141292
38*4520Snw141292 #ifdef __MACOS__
39*4520Snw141292 # include <console.h>
40*4520Snw141292 # include <signal.h>
41*4520Snw141292 # include <unistd.h>
42*4520Snw141292 # include <extras.h>
43*4520Snw141292 # include <Files.h>
44*4520Snw141292 # include <Folders.h>
45*4520Snw141292 #endif
46*4520Snw141292
47*4520Snw141292 #if defined(HAVE_READLINE) && HAVE_READLINE==1
48*4520Snw141292 # include <readline/readline.h>
49*4520Snw141292 # include <readline/history.h>
50*4520Snw141292 #else
51*4520Snw141292 # define readline(p) local_getline(p,stdin)
52*4520Snw141292 # define add_history(X)
53*4520Snw141292 # define read_history(X)
54*4520Snw141292 # define write_history(X)
55*4520Snw141292 # define stifle_history(X)
56*4520Snw141292 #endif
57*4520Snw141292
58*4520Snw141292 /* Make sure isatty() has a prototype.
59*4520Snw141292 */
60*4520Snw141292 extern int isatty();
61*4520Snw141292
62*4520Snw141292 /*
63*4520Snw141292 ** The following is the open SQLite database. We make a pointer
64*4520Snw141292 ** to this database a static variable so that it can be accessed
65*4520Snw141292 ** by the SIGINT handler to interrupt database processing.
66*4520Snw141292 */
67*4520Snw141292 static sqlite *db = 0;
68*4520Snw141292
69*4520Snw141292 /*
70*4520Snw141292 ** True if an interrupt (Control-C) has been received.
71*4520Snw141292 */
72*4520Snw141292 static int seenInterrupt = 0;
73*4520Snw141292
74*4520Snw141292 /*
75*4520Snw141292 ** This is the name of our program. It is set in main(), used
76*4520Snw141292 ** in a number of other places, mostly for error messages.
77*4520Snw141292 */
78*4520Snw141292 static char *Argv0;
79*4520Snw141292
80*4520Snw141292 /*
81*4520Snw141292 ** Prompt strings. Initialized in main. Settable with
82*4520Snw141292 ** .prompt main continue
83*4520Snw141292 */
84*4520Snw141292 static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
85*4520Snw141292 static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
86*4520Snw141292
87*4520Snw141292
88*4520Snw141292 /*
89*4520Snw141292 ** Determines if a string is a number of not.
90*4520Snw141292 */
91*4520Snw141292 extern int sqliteIsNumber(const char*);
92*4520Snw141292
93*4520Snw141292 /*
94*4520Snw141292 ** This routine reads a line of text from standard input, stores
95*4520Snw141292 ** the text in memory obtained from malloc() and returns a pointer
96*4520Snw141292 ** to the text. NULL is returned at end of file, or if malloc()
97*4520Snw141292 ** fails.
98*4520Snw141292 **
99*4520Snw141292 ** The interface is like "readline" but no command-line editing
100*4520Snw141292 ** is done.
101*4520Snw141292 */
local_getline(char * zPrompt,FILE * in)102*4520Snw141292 static char *local_getline(char *zPrompt, FILE *in){
103*4520Snw141292 char *zLine;
104*4520Snw141292 int nLine;
105*4520Snw141292 int n;
106*4520Snw141292 int eol;
107*4520Snw141292
108*4520Snw141292 if( zPrompt && *zPrompt ){
109*4520Snw141292 printf("%s",zPrompt);
110*4520Snw141292 fflush(stdout);
111*4520Snw141292 }
112*4520Snw141292 nLine = 100;
113*4520Snw141292 zLine = malloc( nLine );
114*4520Snw141292 if( zLine==0 ) return 0;
115*4520Snw141292 n = 0;
116*4520Snw141292 eol = 0;
117*4520Snw141292 while( !eol ){
118*4520Snw141292 if( n+100>nLine ){
119*4520Snw141292 nLine = nLine*2 + 100;
120*4520Snw141292 zLine = realloc(zLine, nLine);
121*4520Snw141292 if( zLine==0 ) return 0;
122*4520Snw141292 }
123*4520Snw141292 if( fgets(&zLine[n], nLine - n, in)==0 ){
124*4520Snw141292 if( n==0 ){
125*4520Snw141292 free(zLine);
126*4520Snw141292 return 0;
127*4520Snw141292 }
128*4520Snw141292 zLine[n] = 0;
129*4520Snw141292 eol = 1;
130*4520Snw141292 break;
131*4520Snw141292 }
132*4520Snw141292 while( zLine[n] ){ n++; }
133*4520Snw141292 if( n>0 && zLine[n-1]=='\n' ){
134*4520Snw141292 n--;
135*4520Snw141292 zLine[n] = 0;
136*4520Snw141292 eol = 1;
137*4520Snw141292 }
138*4520Snw141292 }
139*4520Snw141292 zLine = realloc( zLine, n+1 );
140*4520Snw141292 return zLine;
141*4520Snw141292 }
142*4520Snw141292
143*4520Snw141292 /*
144*4520Snw141292 ** Retrieve a single line of input text. "isatty" is true if text
145*4520Snw141292 ** is coming from a terminal. In that case, we issue a prompt and
146*4520Snw141292 ** attempt to use "readline" for command-line editing. If "isatty"
147*4520Snw141292 ** is false, use "local_getline" instead of "readline" and issue no prompt.
148*4520Snw141292 **
149*4520Snw141292 ** zPrior is a string of prior text retrieved. If not the empty
150*4520Snw141292 ** string, then issue a continuation prompt.
151*4520Snw141292 */
one_input_line(const char * zPrior,FILE * in)152*4520Snw141292 static char *one_input_line(const char *zPrior, FILE *in){
153*4520Snw141292 char *zPrompt;
154*4520Snw141292 char *zResult;
155*4520Snw141292 if( in!=0 ){
156*4520Snw141292 return local_getline(0, in);
157*4520Snw141292 }
158*4520Snw141292 if( zPrior && zPrior[0] ){
159*4520Snw141292 zPrompt = continuePrompt;
160*4520Snw141292 }else{
161*4520Snw141292 zPrompt = mainPrompt;
162*4520Snw141292 }
163*4520Snw141292 zResult = readline(zPrompt);
164*4520Snw141292 if( zResult ) add_history(zResult);
165*4520Snw141292 return zResult;
166*4520Snw141292 }
167*4520Snw141292
168*4520Snw141292 struct previous_mode_data {
169*4520Snw141292 int valid; /* Is there legit data in here? */
170*4520Snw141292 int mode;
171*4520Snw141292 int showHeader;
172*4520Snw141292 int colWidth[100];
173*4520Snw141292 };
174*4520Snw141292 /*
175*4520Snw141292 ** An pointer to an instance of this structure is passed from
176*4520Snw141292 ** the main program to the callback. This is used to communicate
177*4520Snw141292 ** state and mode information.
178*4520Snw141292 */
179*4520Snw141292 struct callback_data {
180*4520Snw141292 sqlite *db; /* The database */
181*4520Snw141292 int echoOn; /* True to echo input commands */
182*4520Snw141292 int cnt; /* Number of records displayed so far */
183*4520Snw141292 FILE *out; /* Write results here */
184*4520Snw141292 int mode; /* An output mode setting */
185*4520Snw141292 int showHeader; /* True to show column names in List or Column mode */
186*4520Snw141292 char *zDestTable; /* Name of destination table when MODE_Insert */
187*4520Snw141292 char separator[20]; /* Separator character for MODE_List */
188*4520Snw141292 int colWidth[100]; /* Requested width of each column when in column mode*/
189*4520Snw141292 int actualWidth[100]; /* Actual width of each column */
190*4520Snw141292 char nullvalue[20]; /* The text to print when a NULL comes back from
191*4520Snw141292 ** the database */
192*4520Snw141292 struct previous_mode_data explainPrev;
193*4520Snw141292 /* Holds the mode information just before
194*4520Snw141292 ** .explain ON */
195*4520Snw141292 char outfile[FILENAME_MAX]; /* Filename for *out */
196*4520Snw141292 const char *zDbFilename; /* name of the database file */
197*4520Snw141292 char *zKey; /* Encryption key */
198*4520Snw141292 };
199*4520Snw141292
200*4520Snw141292 /*
201*4520Snw141292 ** These are the allowed modes.
202*4520Snw141292 */
203*4520Snw141292 #define MODE_Line 0 /* One column per line. Blank line between records */
204*4520Snw141292 #define MODE_Column 1 /* One record per line in neat columns */
205*4520Snw141292 #define MODE_List 2 /* One record per line with a separator */
206*4520Snw141292 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
207*4520Snw141292 #define MODE_Html 4 /* Generate an XHTML table */
208*4520Snw141292 #define MODE_Insert 5 /* Generate SQL "insert" statements */
209*4520Snw141292 #define MODE_NUM_OF 6 /* The number of modes (not a mode itself) */
210*4520Snw141292
211*4520Snw141292 char *modeDescr[MODE_NUM_OF] = {
212*4520Snw141292 "line",
213*4520Snw141292 "column",
214*4520Snw141292 "list",
215*4520Snw141292 "semi",
216*4520Snw141292 "html",
217*4520Snw141292 "insert"
218*4520Snw141292 };
219*4520Snw141292
220*4520Snw141292 /*
221*4520Snw141292 ** Number of elements in an array
222*4520Snw141292 */
223*4520Snw141292 #define ArraySize(X) (sizeof(X)/sizeof(X[0]))
224*4520Snw141292
225*4520Snw141292 /*
226*4520Snw141292 ** Output the given string as a quoted string using SQL quoting conventions.
227*4520Snw141292 */
output_quoted_string(FILE * out,const char * z)228*4520Snw141292 static void output_quoted_string(FILE *out, const char *z){
229*4520Snw141292 int i;
230*4520Snw141292 int nSingle = 0;
231*4520Snw141292 for(i=0; z[i]; i++){
232*4520Snw141292 if( z[i]=='\'' ) nSingle++;
233*4520Snw141292 }
234*4520Snw141292 if( nSingle==0 ){
235*4520Snw141292 fprintf(out,"'%s'",z);
236*4520Snw141292 }else{
237*4520Snw141292 fprintf(out,"'");
238*4520Snw141292 while( *z ){
239*4520Snw141292 for(i=0; z[i] && z[i]!='\''; i++){}
240*4520Snw141292 if( i==0 ){
241*4520Snw141292 fprintf(out,"''");
242*4520Snw141292 z++;
243*4520Snw141292 }else if( z[i]=='\'' ){
244*4520Snw141292 fprintf(out,"%.*s''",i,z);
245*4520Snw141292 z += i+1;
246*4520Snw141292 }else{
247*4520Snw141292 fprintf(out,"%s",z);
248*4520Snw141292 break;
249*4520Snw141292 }
250*4520Snw141292 }
251*4520Snw141292 fprintf(out,"'");
252*4520Snw141292 }
253*4520Snw141292 }
254*4520Snw141292
255*4520Snw141292 /*
256*4520Snw141292 ** Output the given string with characters that are special to
257*4520Snw141292 ** HTML escaped.
258*4520Snw141292 */
output_html_string(FILE * out,const char * z)259*4520Snw141292 static void output_html_string(FILE *out, const char *z){
260*4520Snw141292 int i;
261*4520Snw141292 while( *z ){
262*4520Snw141292 for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){}
263*4520Snw141292 if( i>0 ){
264*4520Snw141292 fprintf(out,"%.*s",i,z);
265*4520Snw141292 }
266*4520Snw141292 if( z[i]=='<' ){
267*4520Snw141292 fprintf(out,"<");
268*4520Snw141292 }else if( z[i]=='&' ){
269*4520Snw141292 fprintf(out,"&");
270*4520Snw141292 }else{
271*4520Snw141292 break;
272*4520Snw141292 }
273*4520Snw141292 z += i + 1;
274*4520Snw141292 }
275*4520Snw141292 }
276*4520Snw141292
277*4520Snw141292 /*
278*4520Snw141292 ** This routine runs when the user presses Ctrl-C
279*4520Snw141292 */
interrupt_handler(int NotUsed)280*4520Snw141292 static void interrupt_handler(int NotUsed){
281*4520Snw141292 seenInterrupt = 1;
282*4520Snw141292 if( db ) sqlite_interrupt(db);
283*4520Snw141292 }
284*4520Snw141292
285*4520Snw141292 /*
286*4520Snw141292 ** This is the callback routine that the SQLite library
287*4520Snw141292 ** invokes for each row of a query result.
288*4520Snw141292 */
callback(void * pArg,int nArg,char ** azArg,char ** azCol)289*4520Snw141292 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
290*4520Snw141292 int i;
291*4520Snw141292 struct callback_data *p = (struct callback_data*)pArg;
292*4520Snw141292 switch( p->mode ){
293*4520Snw141292 case MODE_Line: {
294*4520Snw141292 int w = 5;
295*4520Snw141292 if( azArg==0 ) break;
296*4520Snw141292 for(i=0; i<nArg; i++){
297*4520Snw141292 int len = strlen(azCol[i]);
298*4520Snw141292 if( len>w ) w = len;
299*4520Snw141292 }
300*4520Snw141292 if( p->cnt++>0 ) fprintf(p->out,"\n");
301*4520Snw141292 for(i=0; i<nArg; i++){
302*4520Snw141292 fprintf(p->out,"%*s = %s\n", w, azCol[i],
303*4520Snw141292 azArg[i] ? azArg[i] : p->nullvalue);
304*4520Snw141292 }
305*4520Snw141292 break;
306*4520Snw141292 }
307*4520Snw141292 case MODE_Column: {
308*4520Snw141292 if( p->cnt++==0 ){
309*4520Snw141292 for(i=0; i<nArg; i++){
310*4520Snw141292 int w, n;
311*4520Snw141292 if( i<ArraySize(p->colWidth) ){
312*4520Snw141292 w = p->colWidth[i];
313*4520Snw141292 }else{
314*4520Snw141292 w = 0;
315*4520Snw141292 }
316*4520Snw141292 if( w<=0 ){
317*4520Snw141292 w = strlen(azCol[i] ? azCol[i] : "");
318*4520Snw141292 if( w<10 ) w = 10;
319*4520Snw141292 n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue);
320*4520Snw141292 if( w<n ) w = n;
321*4520Snw141292 }
322*4520Snw141292 if( i<ArraySize(p->actualWidth) ){
323*4520Snw141292 p->actualWidth[i] = w;
324*4520Snw141292 }
325*4520Snw141292 if( p->showHeader ){
326*4520Snw141292 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
327*4520Snw141292 }
328*4520Snw141292 }
329*4520Snw141292 if( p->showHeader ){
330*4520Snw141292 for(i=0; i<nArg; i++){
331*4520Snw141292 int w;
332*4520Snw141292 if( i<ArraySize(p->actualWidth) ){
333*4520Snw141292 w = p->actualWidth[i];
334*4520Snw141292 }else{
335*4520Snw141292 w = 10;
336*4520Snw141292 }
337*4520Snw141292 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
338*4520Snw141292 "----------------------------------------------------------",
339*4520Snw141292 i==nArg-1 ? "\n": " ");
340*4520Snw141292 }
341*4520Snw141292 }
342*4520Snw141292 }
343*4520Snw141292 if( azArg==0 ) break;
344*4520Snw141292 for(i=0; i<nArg; i++){
345*4520Snw141292 int w;
346*4520Snw141292 if( i<ArraySize(p->actualWidth) ){
347*4520Snw141292 w = p->actualWidth[i];
348*4520Snw141292 }else{
349*4520Snw141292 w = 10;
350*4520Snw141292 }
351*4520Snw141292 fprintf(p->out,"%-*.*s%s",w,w,
352*4520Snw141292 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
353*4520Snw141292 }
354*4520Snw141292 break;
355*4520Snw141292 }
356*4520Snw141292 case MODE_Semi:
357*4520Snw141292 case MODE_List: {
358*4520Snw141292 if( p->cnt++==0 && p->showHeader ){
359*4520Snw141292 for(i=0; i<nArg; i++){
360*4520Snw141292 fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
361*4520Snw141292 }
362*4520Snw141292 }
363*4520Snw141292 if( azArg==0 ) break;
364*4520Snw141292 for(i=0; i<nArg; i++){
365*4520Snw141292 char *z = azArg[i];
366*4520Snw141292 if( z==0 ) z = p->nullvalue;
367*4520Snw141292 fprintf(p->out, "%s", z);
368*4520Snw141292 if( i<nArg-1 ){
369*4520Snw141292 fprintf(p->out, "%s", p->separator);
370*4520Snw141292 }else if( p->mode==MODE_Semi ){
371*4520Snw141292 fprintf(p->out, ";\n");
372*4520Snw141292 }else{
373*4520Snw141292 fprintf(p->out, "\n");
374*4520Snw141292 }
375*4520Snw141292 }
376*4520Snw141292 break;
377*4520Snw141292 }
378*4520Snw141292 case MODE_Html: {
379*4520Snw141292 if( p->cnt++==0 && p->showHeader ){
380*4520Snw141292 fprintf(p->out,"<TR>");
381*4520Snw141292 for(i=0; i<nArg; i++){
382*4520Snw141292 fprintf(p->out,"<TH>%s</TH>",azCol[i]);
383*4520Snw141292 }
384*4520Snw141292 fprintf(p->out,"</TR>\n");
385*4520Snw141292 }
386*4520Snw141292 if( azArg==0 ) break;
387*4520Snw141292 fprintf(p->out,"<TR>");
388*4520Snw141292 for(i=0; i<nArg; i++){
389*4520Snw141292 fprintf(p->out,"<TD>");
390*4520Snw141292 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
391*4520Snw141292 fprintf(p->out,"</TD>\n");
392*4520Snw141292 }
393*4520Snw141292 fprintf(p->out,"</TR>\n");
394*4520Snw141292 break;
395*4520Snw141292 }
396*4520Snw141292 case MODE_Insert: {
397*4520Snw141292 if( azArg==0 ) break;
398*4520Snw141292 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
399*4520Snw141292 for(i=0; i<nArg; i++){
400*4520Snw141292 char *zSep = i>0 ? ",": "";
401*4520Snw141292 if( azArg[i]==0 ){
402*4520Snw141292 fprintf(p->out,"%sNULL",zSep);
403*4520Snw141292 }else if( sqliteIsNumber(azArg[i]) ){
404*4520Snw141292 fprintf(p->out,"%s%s",zSep, azArg[i]);
405*4520Snw141292 }else{
406*4520Snw141292 if( zSep[0] ) fprintf(p->out,"%s",zSep);
407*4520Snw141292 output_quoted_string(p->out, azArg[i]);
408*4520Snw141292 }
409*4520Snw141292 }
410*4520Snw141292 fprintf(p->out,");\n");
411*4520Snw141292 break;
412*4520Snw141292 }
413*4520Snw141292 }
414*4520Snw141292 return 0;
415*4520Snw141292 }
416*4520Snw141292
417*4520Snw141292 /*
418*4520Snw141292 ** Set the destination table field of the callback_data structure to
419*4520Snw141292 ** the name of the table given. Escape any quote characters in the
420*4520Snw141292 ** table name.
421*4520Snw141292 */
set_table_name(struct callback_data * p,const char * zName)422*4520Snw141292 static void set_table_name(struct callback_data *p, const char *zName){
423*4520Snw141292 int i, n;
424*4520Snw141292 int needQuote;
425*4520Snw141292 char *z;
426*4520Snw141292
427*4520Snw141292 if( p->zDestTable ){
428*4520Snw141292 free(p->zDestTable);
429*4520Snw141292 p->zDestTable = 0;
430*4520Snw141292 }
431*4520Snw141292 if( zName==0 ) return;
432*4520Snw141292 needQuote = !isalpha(*zName) && *zName!='_';
433*4520Snw141292 for(i=n=0; zName[i]; i++, n++){
434*4520Snw141292 if( !isalnum(zName[i]) && zName[i]!='_' ){
435*4520Snw141292 needQuote = 1;
436*4520Snw141292 if( zName[i]=='\'' ) n++;
437*4520Snw141292 }
438*4520Snw141292 }
439*4520Snw141292 if( needQuote ) n += 2;
440*4520Snw141292 z = p->zDestTable = malloc( n+1 );
441*4520Snw141292 if( z==0 ){
442*4520Snw141292 fprintf(stderr,"Out of memory!\n");
443*4520Snw141292 exit(1);
444*4520Snw141292 }
445*4520Snw141292 n = 0;
446*4520Snw141292 if( needQuote ) z[n++] = '\'';
447*4520Snw141292 for(i=0; zName[i]; i++){
448*4520Snw141292 z[n++] = zName[i];
449*4520Snw141292 if( zName[i]=='\'' ) z[n++] = '\'';
450*4520Snw141292 }
451*4520Snw141292 if( needQuote ) z[n++] = '\'';
452*4520Snw141292 z[n] = 0;
453*4520Snw141292 }
454*4520Snw141292
455*4520Snw141292 /*
456*4520Snw141292 ** This is a different callback routine used for dumping the database.
457*4520Snw141292 ** Each row received by this callback consists of a table name,
458*4520Snw141292 ** the table type ("index" or "table") and SQL to create the table.
459*4520Snw141292 ** This routine should print text sufficient to recreate the table.
460*4520Snw141292 */
dump_callback(void * pArg,int nArg,char ** azArg,char ** azCol)461*4520Snw141292 static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
462*4520Snw141292 struct callback_data *p = (struct callback_data *)pArg;
463*4520Snw141292 if( nArg!=3 ) return 1;
464*4520Snw141292 fprintf(p->out, "%s;\n", azArg[2]);
465*4520Snw141292 if( strcmp(azArg[1],"table")==0 ){
466*4520Snw141292 struct callback_data d2;
467*4520Snw141292 d2 = *p;
468*4520Snw141292 d2.mode = MODE_Insert;
469*4520Snw141292 d2.zDestTable = 0;
470*4520Snw141292 set_table_name(&d2, azArg[0]);
471*4520Snw141292 sqlite_exec_printf(p->db,
472*4520Snw141292 "SELECT * FROM '%q'",
473*4520Snw141292 callback, &d2, 0, azArg[0]
474*4520Snw141292 );
475*4520Snw141292 set_table_name(&d2, 0);
476*4520Snw141292 }
477*4520Snw141292 return 0;
478*4520Snw141292 }
479*4520Snw141292
480*4520Snw141292 /*
481*4520Snw141292 ** Text of a help message
482*4520Snw141292 */
483*4520Snw141292 static char zHelp[] =
484*4520Snw141292 ".databases List names and files of attached databases\n"
485*4520Snw141292 ".dump ?TABLE? ... Dump the database in a text format\n"
486*4520Snw141292 ".echo ON|OFF Turn command echo on or off\n"
487*4520Snw141292 ".exit Exit this program\n"
488*4520Snw141292 ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n"
489*4520Snw141292 ".header(s) ON|OFF Turn display of headers on or off\n"
490*4520Snw141292 ".help Show this message\n"
491*4520Snw141292 ".indices TABLE Show names of all indices on TABLE\n"
492*4520Snw141292 ".mode MODE Set mode to one of \"line(s)\", \"column(s)\", \n"
493*4520Snw141292 " \"insert\", \"list\", or \"html\"\n"
494*4520Snw141292 ".mode insert TABLE Generate SQL insert statements for TABLE\n"
495*4520Snw141292 ".nullvalue STRING Print STRING instead of nothing for NULL data\n"
496*4520Snw141292 ".output FILENAME Send output to FILENAME\n"
497*4520Snw141292 ".output stdout Send output to the screen\n"
498*4520Snw141292 ".prompt MAIN CONTINUE Replace the standard prompts\n"
499*4520Snw141292 ".quit Exit this program\n"
500*4520Snw141292 ".read FILENAME Execute SQL in FILENAME\n"
501*4520Snw141292 #ifdef SQLITE_HAS_CODEC
502*4520Snw141292 ".rekey OLD NEW NEW Change the encryption key\n"
503*4520Snw141292 #endif
504*4520Snw141292 ".schema ?TABLE? Show the CREATE statements\n"
505*4520Snw141292 ".separator STRING Change separator string for \"list\" mode\n"
506*4520Snw141292 ".show Show the current values for various settings\n"
507*4520Snw141292 ".tables ?PATTERN? List names of tables matching a pattern\n"
508*4520Snw141292 ".timeout MS Try opening locked tables for MS milliseconds\n"
509*4520Snw141292 ".width NUM NUM ... Set column widths for \"column\" mode\n"
510*4520Snw141292 ;
511*4520Snw141292
512*4520Snw141292 /* Forward reference */
513*4520Snw141292 static void process_input(struct callback_data *p, FILE *in);
514*4520Snw141292
515*4520Snw141292 /*
516*4520Snw141292 ** Make sure the database is open. If it is not, then open it. If
517*4520Snw141292 ** the database fails to open, print an error message and exit.
518*4520Snw141292 */
open_db(struct callback_data * p)519*4520Snw141292 static void open_db(struct callback_data *p){
520*4520Snw141292 if( p->db==0 ){
521*4520Snw141292 char *zErrMsg = 0;
522*4520Snw141292 #ifdef SQLITE_HAS_CODEC
523*4520Snw141292 int n = p->zKey ? strlen(p->zKey) : 0;
524*4520Snw141292 db = p->db = sqlite_open_encrypted(p->zDbFilename, p->zKey, n, 0, &zErrMsg);
525*4520Snw141292 #else
526*4520Snw141292 db = p->db = sqlite_open(p->zDbFilename, 0, &zErrMsg);
527*4520Snw141292 #endif
528*4520Snw141292 if( p->db==0 ){
529*4520Snw141292 if( zErrMsg ){
530*4520Snw141292 fprintf(stderr,"Unable to open database \"%s\": %s\n",
531*4520Snw141292 p->zDbFilename, zErrMsg);
532*4520Snw141292 }else{
533*4520Snw141292 fprintf(stderr,"Unable to open database %s\n", p->zDbFilename);
534*4520Snw141292 }
535*4520Snw141292 exit(1);
536*4520Snw141292 }
537*4520Snw141292 }
538*4520Snw141292 }
539*4520Snw141292
540*4520Snw141292 /*
541*4520Snw141292 ** If an input line begins with "." then invoke this routine to
542*4520Snw141292 ** process that line.
543*4520Snw141292 **
544*4520Snw141292 ** Return 1 to exit and 0 to continue.
545*4520Snw141292 */
do_meta_command(char * zLine,struct callback_data * p)546*4520Snw141292 static int do_meta_command(char *zLine, struct callback_data *p){
547*4520Snw141292 int i = 1;
548*4520Snw141292 int nArg = 0;
549*4520Snw141292 int n, c;
550*4520Snw141292 int rc = 0;
551*4520Snw141292 char *azArg[50];
552*4520Snw141292
553*4520Snw141292 /* Parse the input line into tokens.
554*4520Snw141292 */
555*4520Snw141292 while( zLine[i] && nArg<ArraySize(azArg) ){
556*4520Snw141292 while( isspace(zLine[i]) ){ i++; }
557*4520Snw141292 if( zLine[i]==0 ) break;
558*4520Snw141292 if( zLine[i]=='\'' || zLine[i]=='"' ){
559*4520Snw141292 int delim = zLine[i++];
560*4520Snw141292 azArg[nArg++] = &zLine[i];
561*4520Snw141292 while( zLine[i] && zLine[i]!=delim ){ i++; }
562*4520Snw141292 if( zLine[i]==delim ){
563*4520Snw141292 zLine[i++] = 0;
564*4520Snw141292 }
565*4520Snw141292 }else{
566*4520Snw141292 azArg[nArg++] = &zLine[i];
567*4520Snw141292 while( zLine[i] && !isspace(zLine[i]) ){ i++; }
568*4520Snw141292 if( zLine[i] ) zLine[i++] = 0;
569*4520Snw141292 }
570*4520Snw141292 }
571*4520Snw141292
572*4520Snw141292 /* Process the input line.
573*4520Snw141292 */
574*4520Snw141292 if( nArg==0 ) return rc;
575*4520Snw141292 n = strlen(azArg[0]);
576*4520Snw141292 c = azArg[0][0];
577*4520Snw141292 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
578*4520Snw141292 struct callback_data data;
579*4520Snw141292 char *zErrMsg = 0;
580*4520Snw141292 open_db(p);
581*4520Snw141292 memcpy(&data, p, sizeof(data));
582*4520Snw141292 data.showHeader = 1;
583*4520Snw141292 data.mode = MODE_Column;
584*4520Snw141292 data.colWidth[0] = 3;
585*4520Snw141292 data.colWidth[1] = 15;
586*4520Snw141292 data.colWidth[2] = 58;
587*4520Snw141292 sqlite_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
588*4520Snw141292 if( zErrMsg ){
589*4520Snw141292 fprintf(stderr,"Error: %s\n", zErrMsg);
590*4520Snw141292 sqlite_freemem(zErrMsg);
591*4520Snw141292 }
592*4520Snw141292 }else
593*4520Snw141292
594*4520Snw141292 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
595*4520Snw141292 char *zErrMsg = 0;
596*4520Snw141292 open_db(p);
597*4520Snw141292 fprintf(p->out, "BEGIN TRANSACTION;\n");
598*4520Snw141292 if( nArg==1 ){
599*4520Snw141292 sqlite_exec(p->db,
600*4520Snw141292 "SELECT name, type, sql FROM sqlite_master "
601*4520Snw141292 "WHERE type!='meta' AND sql NOT NULL "
602*4520Snw141292 "ORDER BY substr(type,2,1), name",
603*4520Snw141292 dump_callback, p, &zErrMsg
604*4520Snw141292 );
605*4520Snw141292 }else{
606*4520Snw141292 int i;
607*4520Snw141292 for(i=1; i<nArg && zErrMsg==0; i++){
608*4520Snw141292 sqlite_exec_printf(p->db,
609*4520Snw141292 "SELECT name, type, sql FROM sqlite_master "
610*4520Snw141292 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOT NULL "
611*4520Snw141292 "ORDER BY substr(type,2,1), name",
612*4520Snw141292 dump_callback, p, &zErrMsg, azArg[i]
613*4520Snw141292 );
614*4520Snw141292 }
615*4520Snw141292 }
616*4520Snw141292 if( zErrMsg ){
617*4520Snw141292 fprintf(stderr,"Error: %s\n", zErrMsg);
618*4520Snw141292 sqlite_freemem(zErrMsg);
619*4520Snw141292 }else{
620*4520Snw141292 fprintf(p->out, "COMMIT;\n");
621*4520Snw141292 }
622*4520Snw141292 }else
623*4520Snw141292
624*4520Snw141292 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
625*4520Snw141292 int j;
626*4520Snw141292 char *z = azArg[1];
627*4520Snw141292 int val = atoi(azArg[1]);
628*4520Snw141292 for(j=0; z[j]; j++){
629*4520Snw141292 if( isupper(z[j]) ) z[j] = tolower(z[j]);
630*4520Snw141292 }
631*4520Snw141292 if( strcmp(z,"on")==0 ){
632*4520Snw141292 val = 1;
633*4520Snw141292 }else if( strcmp(z,"yes")==0 ){
634*4520Snw141292 val = 1;
635*4520Snw141292 }
636*4520Snw141292 p->echoOn = val;
637*4520Snw141292 }else
638*4520Snw141292
639*4520Snw141292 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
640*4520Snw141292 rc = 1;
641*4520Snw141292 }else
642*4520Snw141292
643*4520Snw141292 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
644*4520Snw141292 int j;
645*4520Snw141292 char *z = nArg>=2 ? azArg[1] : "1";
646*4520Snw141292 int val = atoi(z);
647*4520Snw141292 for(j=0; z[j]; j++){
648*4520Snw141292 if( isupper(z[j]) ) z[j] = tolower(z[j]);
649*4520Snw141292 }
650*4520Snw141292 if( strcmp(z,"on")==0 ){
651*4520Snw141292 val = 1;
652*4520Snw141292 }else if( strcmp(z,"yes")==0 ){
653*4520Snw141292 val = 1;
654*4520Snw141292 }
655*4520Snw141292 if(val == 1) {
656*4520Snw141292 if(!p->explainPrev.valid) {
657*4520Snw141292 p->explainPrev.valid = 1;
658*4520Snw141292 p->explainPrev.mode = p->mode;
659*4520Snw141292 p->explainPrev.showHeader = p->showHeader;
660*4520Snw141292 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
661*4520Snw141292 }
662*4520Snw141292 /* We could put this code under the !p->explainValid
663*4520Snw141292 ** condition so that it does not execute if we are already in
664*4520Snw141292 ** explain mode. However, always executing it allows us an easy
665*4520Snw141292 ** was to reset to explain mode in case the user previously
666*4520Snw141292 ** did an .explain followed by a .width, .mode or .header
667*4520Snw141292 ** command.
668*4520Snw141292 */
669*4520Snw141292 p->mode = MODE_Column;
670*4520Snw141292 p->showHeader = 1;
671*4520Snw141292 memset(p->colWidth,0,ArraySize(p->colWidth));
672*4520Snw141292 p->colWidth[0] = 4;
673*4520Snw141292 p->colWidth[1] = 12;
674*4520Snw141292 p->colWidth[2] = 10;
675*4520Snw141292 p->colWidth[3] = 10;
676*4520Snw141292 p->colWidth[4] = 35;
677*4520Snw141292 }else if (p->explainPrev.valid) {
678*4520Snw141292 p->explainPrev.valid = 0;
679*4520Snw141292 p->mode = p->explainPrev.mode;
680*4520Snw141292 p->showHeader = p->explainPrev.showHeader;
681*4520Snw141292 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
682*4520Snw141292 }
683*4520Snw141292 }else
684*4520Snw141292
685*4520Snw141292 if( c=='h' && (strncmp(azArg[0], "header", n)==0
686*4520Snw141292 ||
687*4520Snw141292 strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){
688*4520Snw141292 int j;
689*4520Snw141292 char *z = azArg[1];
690*4520Snw141292 int val = atoi(azArg[1]);
691*4520Snw141292 for(j=0; z[j]; j++){
692*4520Snw141292 if( isupper(z[j]) ) z[j] = tolower(z[j]);
693*4520Snw141292 }
694*4520Snw141292 if( strcmp(z,"on")==0 ){
695*4520Snw141292 val = 1;
696*4520Snw141292 }else if( strcmp(z,"yes")==0 ){
697*4520Snw141292 val = 1;
698*4520Snw141292 }
699*4520Snw141292 p->showHeader = val;
700*4520Snw141292 }else
701*4520Snw141292
702*4520Snw141292 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
703*4520Snw141292 fprintf(stderr,zHelp);
704*4520Snw141292 }else
705*4520Snw141292
706*4520Snw141292 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
707*4520Snw141292 struct callback_data data;
708*4520Snw141292 char *zErrMsg = 0;
709*4520Snw141292 open_db(p);
710*4520Snw141292 memcpy(&data, p, sizeof(data));
711*4520Snw141292 data.showHeader = 0;
712*4520Snw141292 data.mode = MODE_List;
713*4520Snw141292 sqlite_exec_printf(p->db,
714*4520Snw141292 "SELECT name FROM sqlite_master "
715*4520Snw141292 "WHERE type='index' AND tbl_name LIKE '%q' "
716*4520Snw141292 "UNION ALL "
717*4520Snw141292 "SELECT name FROM sqlite_temp_master "
718*4520Snw141292 "WHERE type='index' AND tbl_name LIKE '%q' "
719*4520Snw141292 "ORDER BY 1",
720*4520Snw141292 callback, &data, &zErrMsg, azArg[1], azArg[1]
721*4520Snw141292 );
722*4520Snw141292 if( zErrMsg ){
723*4520Snw141292 fprintf(stderr,"Error: %s\n", zErrMsg);
724*4520Snw141292 sqlite_freemem(zErrMsg);
725*4520Snw141292 }
726*4520Snw141292 }else
727*4520Snw141292
728*4520Snw141292 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
729*4520Snw141292 int n2 = strlen(azArg[1]);
730*4520Snw141292 if( strncmp(azArg[1],"line",n2)==0
731*4520Snw141292 ||
732*4520Snw141292 strncmp(azArg[1],"lines",n2)==0 ){
733*4520Snw141292 p->mode = MODE_Line;
734*4520Snw141292 }else if( strncmp(azArg[1],"column",n2)==0
735*4520Snw141292 ||
736*4520Snw141292 strncmp(azArg[1],"columns",n2)==0 ){
737*4520Snw141292 p->mode = MODE_Column;
738*4520Snw141292 }else if( strncmp(azArg[1],"list",n2)==0 ){
739*4520Snw141292 p->mode = MODE_List;
740*4520Snw141292 }else if( strncmp(azArg[1],"html",n2)==0 ){
741*4520Snw141292 p->mode = MODE_Html;
742*4520Snw141292 }else if( strncmp(azArg[1],"insert",n2)==0 ){
743*4520Snw141292 p->mode = MODE_Insert;
744*4520Snw141292 if( nArg>=3 ){
745*4520Snw141292 set_table_name(p, azArg[2]);
746*4520Snw141292 }else{
747*4520Snw141292 set_table_name(p, "table");
748*4520Snw141292 }
749*4520Snw141292 }else {
750*4520Snw141292 fprintf(stderr,"mode should be on of: column html insert line list\n");
751*4520Snw141292 }
752*4520Snw141292 }else
753*4520Snw141292
754*4520Snw141292 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
755*4520Snw141292 sprintf(p->nullvalue, "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
756*4520Snw141292 }else
757*4520Snw141292
758*4520Snw141292 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
759*4520Snw141292 if( p->out!=stdout ){
760*4520Snw141292 fclose(p->out);
761*4520Snw141292 }
762*4520Snw141292 if( strcmp(azArg[1],"stdout")==0 ){
763*4520Snw141292 p->out = stdout;
764*4520Snw141292 strcpy(p->outfile,"stdout");
765*4520Snw141292 }else{
766*4520Snw141292 p->out = fopen(azArg[1], "wb");
767*4520Snw141292 if( p->out==0 ){
768*4520Snw141292 fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
769*4520Snw141292 p->out = stdout;
770*4520Snw141292 } else {
771*4520Snw141292 strcpy(p->outfile,azArg[1]);
772*4520Snw141292 }
773*4520Snw141292 }
774*4520Snw141292 }else
775*4520Snw141292
776*4520Snw141292 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
777*4520Snw141292 if( nArg >= 2) {
778*4520Snw141292 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
779*4520Snw141292 }
780*4520Snw141292 if( nArg >= 3) {
781*4520Snw141292 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
782*4520Snw141292 }
783*4520Snw141292 }else
784*4520Snw141292
785*4520Snw141292 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
786*4520Snw141292 rc = 1;
787*4520Snw141292 }else
788*4520Snw141292
789*4520Snw141292 if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
790*4520Snw141292 FILE *alt = fopen(azArg[1], "rb");
791*4520Snw141292 if( alt==0 ){
792*4520Snw141292 fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
793*4520Snw141292 }else{
794*4520Snw141292 process_input(p, alt);
795*4520Snw141292 fclose(alt);
796*4520Snw141292 }
797*4520Snw141292 }else
798*4520Snw141292
799*4520Snw141292 #ifdef SQLITE_HAS_CODEC
800*4520Snw141292 if( c=='r' && strncmp(azArg[0],"rekey", n)==0 && nArg==4 ){
801*4520Snw141292 char *zOld = p->zKey;
802*4520Snw141292 if( zOld==0 ) zOld = "";
803*4520Snw141292 if( strcmp(azArg[1],zOld) ){
804*4520Snw141292 fprintf(stderr,"old key is incorrect\n");
805*4520Snw141292 }else if( strcmp(azArg[2], azArg[3]) ){
806*4520Snw141292 fprintf(stderr,"2nd copy of new key does not match the 1st\n");
807*4520Snw141292 }else{
808*4520Snw141292 sqlite_freemem(p->zKey);
809*4520Snw141292 p->zKey = sqlite_mprintf("%s", azArg[2]);
810*4520Snw141292 sqlite_rekey(p->db, p->zKey, strlen(p->zKey));
811*4520Snw141292 }
812*4520Snw141292 }else
813*4520Snw141292 #endif
814*4520Snw141292
815*4520Snw141292 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
816*4520Snw141292 struct callback_data data;
817*4520Snw141292 char *zErrMsg = 0;
818*4520Snw141292 open_db(p);
819*4520Snw141292 memcpy(&data, p, sizeof(data));
820*4520Snw141292 data.showHeader = 0;
821*4520Snw141292 data.mode = MODE_Semi;
822*4520Snw141292 if( nArg>1 ){
823*4520Snw141292 extern int sqliteStrICmp(const char*,const char*);
824*4520Snw141292 if( sqliteStrICmp(azArg[1],"sqlite_master")==0 ){
825*4520Snw141292 char *new_argv[2], *new_colv[2];
826*4520Snw141292 new_argv[0] = "CREATE TABLE sqlite_master (\n"
827*4520Snw141292 " type text,\n"
828*4520Snw141292 " name text,\n"
829*4520Snw141292 " tbl_name text,\n"
830*4520Snw141292 " rootpage integer,\n"
831*4520Snw141292 " sql text\n"
832*4520Snw141292 ")";
833*4520Snw141292 new_argv[1] = 0;
834*4520Snw141292 new_colv[0] = "sql";
835*4520Snw141292 new_colv[1] = 0;
836*4520Snw141292 callback(&data, 1, new_argv, new_colv);
837*4520Snw141292 }else if( sqliteStrICmp(azArg[1],"sqlite_temp_master")==0 ){
838*4520Snw141292 char *new_argv[2], *new_colv[2];
839*4520Snw141292 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
840*4520Snw141292 " type text,\n"
841*4520Snw141292 " name text,\n"
842*4520Snw141292 " tbl_name text,\n"
843*4520Snw141292 " rootpage integer,\n"
844*4520Snw141292 " sql text\n"
845*4520Snw141292 ")";
846*4520Snw141292 new_argv[1] = 0;
847*4520Snw141292 new_colv[0] = "sql";
848*4520Snw141292 new_colv[1] = 0;
849*4520Snw141292 callback(&data, 1, new_argv, new_colv);
850*4520Snw141292 }else{
851*4520Snw141292 sqlite_exec_printf(p->db,
852*4520Snw141292 "SELECT sql FROM "
853*4520Snw141292 " (SELECT * FROM sqlite_master UNION ALL"
854*4520Snw141292 " SELECT * FROM sqlite_temp_master) "
855*4520Snw141292 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOTNULL "
856*4520Snw141292 "ORDER BY substr(type,2,1), name",
857*4520Snw141292 callback, &data, &zErrMsg, azArg[1]);
858*4520Snw141292 }
859*4520Snw141292 }else{
860*4520Snw141292 sqlite_exec(p->db,
861*4520Snw141292 "SELECT sql FROM "
862*4520Snw141292 " (SELECT * FROM sqlite_master UNION ALL"
863*4520Snw141292 " SELECT * FROM sqlite_temp_master) "
864*4520Snw141292 "WHERE type!='meta' AND sql NOTNULL "
865*4520Snw141292 "ORDER BY substr(type,2,1), name",
866*4520Snw141292 callback, &data, &zErrMsg
867*4520Snw141292 );
868*4520Snw141292 }
869*4520Snw141292 if( zErrMsg ){
870*4520Snw141292 fprintf(stderr,"Error: %s\n", zErrMsg);
871*4520Snw141292 sqlite_freemem(zErrMsg);
872*4520Snw141292 }
873*4520Snw141292 }else
874*4520Snw141292
875*4520Snw141292 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
876*4520Snw141292 sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
877*4520Snw141292 }else
878*4520Snw141292
879*4520Snw141292 if( c=='s' && strncmp(azArg[0], "show", n)==0){
880*4520Snw141292 int i;
881*4520Snw141292 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
882*4520Snw141292 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
883*4520Snw141292 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
884*4520Snw141292 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
885*4520Snw141292 fprintf(p->out,"%9.9s: %s\n","nullvalue", p->nullvalue);
886*4520Snw141292 fprintf(p->out,"%9.9s: %s\n","output",
887*4520Snw141292 strlen(p->outfile) ? p->outfile : "stdout");
888*4520Snw141292 fprintf(p->out,"%9.9s: %s\n","separator", p->separator);
889*4520Snw141292 fprintf(p->out,"%9.9s: ","width");
890*4520Snw141292 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
891*4520Snw141292 fprintf(p->out,"%d ",p->colWidth[i]);
892*4520Snw141292 }
893*4520Snw141292 fprintf(p->out,"\n\n");
894*4520Snw141292 }else
895*4520Snw141292
896*4520Snw141292 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
897*4520Snw141292 char **azResult;
898*4520Snw141292 int nRow, rc;
899*4520Snw141292 char *zErrMsg;
900*4520Snw141292 open_db(p);
901*4520Snw141292 if( nArg==1 ){
902*4520Snw141292 rc = sqlite_get_table(p->db,
903*4520Snw141292 "SELECT name FROM sqlite_master "
904*4520Snw141292 "WHERE type IN ('table','view') "
905*4520Snw141292 "UNION ALL "
906*4520Snw141292 "SELECT name FROM sqlite_temp_master "
907*4520Snw141292 "WHERE type IN ('table','view') "
908*4520Snw141292 "ORDER BY 1",
909*4520Snw141292 &azResult, &nRow, 0, &zErrMsg
910*4520Snw141292 );
911*4520Snw141292 }else{
912*4520Snw141292 rc = sqlite_get_table_printf(p->db,
913*4520Snw141292 "SELECT name FROM sqlite_master "
914*4520Snw141292 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
915*4520Snw141292 "UNION ALL "
916*4520Snw141292 "SELECT name FROM sqlite_temp_master "
917*4520Snw141292 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
918*4520Snw141292 "ORDER BY 1",
919*4520Snw141292 &azResult, &nRow, 0, &zErrMsg, azArg[1], azArg[1]
920*4520Snw141292 );
921*4520Snw141292 }
922*4520Snw141292 if( zErrMsg ){
923*4520Snw141292 fprintf(stderr,"Error: %s\n", zErrMsg);
924*4520Snw141292 sqlite_freemem(zErrMsg);
925*4520Snw141292 }
926*4520Snw141292 if( rc==SQLITE_OK ){
927*4520Snw141292 int len, maxlen = 0;
928*4520Snw141292 int i, j;
929*4520Snw141292 int nPrintCol, nPrintRow;
930*4520Snw141292 for(i=1; i<=nRow; i++){
931*4520Snw141292 if( azResult[i]==0 ) continue;
932*4520Snw141292 len = strlen(azResult[i]);
933*4520Snw141292 if( len>maxlen ) maxlen = len;
934*4520Snw141292 }
935*4520Snw141292 nPrintCol = 80/(maxlen+2);
936*4520Snw141292 if( nPrintCol<1 ) nPrintCol = 1;
937*4520Snw141292 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
938*4520Snw141292 for(i=0; i<nPrintRow; i++){
939*4520Snw141292 for(j=i+1; j<=nRow; j+=nPrintRow){
940*4520Snw141292 char *zSp = j<=nPrintRow ? "" : " ";
941*4520Snw141292 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
942*4520Snw141292 }
943*4520Snw141292 printf("\n");
944*4520Snw141292 }
945*4520Snw141292 }
946*4520Snw141292 sqlite_free_table(azResult);
947*4520Snw141292 }else
948*4520Snw141292
949*4520Snw141292 if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
950*4520Snw141292 open_db(p);
951*4520Snw141292 sqlite_busy_timeout(p->db, atoi(azArg[1]));
952*4520Snw141292 }else
953*4520Snw141292
954*4520Snw141292 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
955*4520Snw141292 int j;
956*4520Snw141292 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
957*4520Snw141292 p->colWidth[j-1] = atoi(azArg[j]);
958*4520Snw141292 }
959*4520Snw141292 }else
960*4520Snw141292
961*4520Snw141292 {
962*4520Snw141292 fprintf(stderr, "unknown command or invalid arguments: "
963*4520Snw141292 " \"%s\". Enter \".help\" for help\n", azArg[0]);
964*4520Snw141292 }
965*4520Snw141292
966*4520Snw141292 return rc;
967*4520Snw141292 }
968*4520Snw141292
969*4520Snw141292 /*
970*4520Snw141292 ** Return TRUE if the last non-whitespace character in z[] is a semicolon.
971*4520Snw141292 ** z[] is N characters long.
972*4520Snw141292 */
_ends_with_semicolon(const char * z,int N)973*4520Snw141292 static int _ends_with_semicolon(const char *z, int N){
974*4520Snw141292 while( N>0 && isspace(z[N-1]) ){ N--; }
975*4520Snw141292 return N>0 && z[N-1]==';';
976*4520Snw141292 }
977*4520Snw141292
978*4520Snw141292 /*
979*4520Snw141292 ** Test to see if a line consists entirely of whitespace.
980*4520Snw141292 */
_all_whitespace(const char * z)981*4520Snw141292 static int _all_whitespace(const char *z){
982*4520Snw141292 for(; *z; z++){
983*4520Snw141292 if( isspace(*z) ) continue;
984*4520Snw141292 if( *z=='/' && z[1]=='*' ){
985*4520Snw141292 z += 2;
986*4520Snw141292 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
987*4520Snw141292 if( *z==0 ) return 0;
988*4520Snw141292 z++;
989*4520Snw141292 continue;
990*4520Snw141292 }
991*4520Snw141292 if( *z=='-' && z[1]=='-' ){
992*4520Snw141292 z += 2;
993*4520Snw141292 while( *z && *z!='\n' ){ z++; }
994*4520Snw141292 if( *z==0 ) return 1;
995*4520Snw141292 continue;
996*4520Snw141292 }
997*4520Snw141292 return 0;
998*4520Snw141292 }
999*4520Snw141292 return 1;
1000*4520Snw141292 }
1001*4520Snw141292
1002*4520Snw141292 /*
1003*4520Snw141292 ** Return TRUE if the line typed in is an SQL command terminator other
1004*4520Snw141292 ** than a semi-colon. The SQL Server style "go" command is understood
1005*4520Snw141292 ** as is the Oracle "/".
1006*4520Snw141292 */
_is_command_terminator(const char * zLine)1007*4520Snw141292 static int _is_command_terminator(const char *zLine){
1008*4520Snw141292 extern int sqliteStrNICmp(const char*,const char*,int);
1009*4520Snw141292 while( isspace(*zLine) ){ zLine++; };
1010*4520Snw141292 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ) return 1; /* Oracle */
1011*4520Snw141292 if( sqliteStrNICmp(zLine,"go",2)==0 && _all_whitespace(&zLine[2]) ){
1012*4520Snw141292 return 1; /* SQL Server */
1013*4520Snw141292 }
1014*4520Snw141292 return 0;
1015*4520Snw141292 }
1016*4520Snw141292
1017*4520Snw141292 /*
1018*4520Snw141292 ** Read input from *in and process it. If *in==0 then input
1019*4520Snw141292 ** is interactive - the user is typing it it. Otherwise, input
1020*4520Snw141292 ** is coming from a file or device. A prompt is issued and history
1021*4520Snw141292 ** is saved only if input is interactive. An interrupt signal will
1022*4520Snw141292 ** cause this routine to exit immediately, unless input is interactive.
1023*4520Snw141292 */
process_input(struct callback_data * p,FILE * in)1024*4520Snw141292 static void process_input(struct callback_data *p, FILE *in){
1025*4520Snw141292 char *zLine;
1026*4520Snw141292 char *zSql = 0;
1027*4520Snw141292 int nSql = 0;
1028*4520Snw141292 char *zErrMsg;
1029*4520Snw141292 int rc;
1030*4520Snw141292 while( fflush(p->out), (zLine = one_input_line(zSql, in))!=0 ){
1031*4520Snw141292 if( seenInterrupt ){
1032*4520Snw141292 if( in!=0 ) break;
1033*4520Snw141292 seenInterrupt = 0;
1034*4520Snw141292 }
1035*4520Snw141292 if( p->echoOn ) printf("%s\n", zLine);
1036*4520Snw141292 if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
1037*4520Snw141292 if( zLine && zLine[0]=='.' && nSql==0 ){
1038*4520Snw141292 int rc = do_meta_command(zLine, p);
1039*4520Snw141292 free(zLine);
1040*4520Snw141292 if( rc ) break;
1041*4520Snw141292 continue;
1042*4520Snw141292 }
1043*4520Snw141292 if( _is_command_terminator(zLine) ){
1044*4520Snw141292 strcpy(zLine,";");
1045*4520Snw141292 }
1046*4520Snw141292 if( zSql==0 ){
1047*4520Snw141292 int i;
1048*4520Snw141292 for(i=0; zLine[i] && isspace(zLine[i]); i++){}
1049*4520Snw141292 if( zLine[i]!=0 ){
1050*4520Snw141292 nSql = strlen(zLine);
1051*4520Snw141292 zSql = malloc( nSql+1 );
1052*4520Snw141292 strcpy(zSql, zLine);
1053*4520Snw141292 }
1054*4520Snw141292 }else{
1055*4520Snw141292 int len = strlen(zLine);
1056*4520Snw141292 zSql = realloc( zSql, nSql + len + 2 );
1057*4520Snw141292 if( zSql==0 ){
1058*4520Snw141292 fprintf(stderr,"%s: out of memory!\n", Argv0);
1059*4520Snw141292 exit(1);
1060*4520Snw141292 }
1061*4520Snw141292 strcpy(&zSql[nSql++], "\n");
1062*4520Snw141292 strcpy(&zSql[nSql], zLine);
1063*4520Snw141292 nSql += len;
1064*4520Snw141292 }
1065*4520Snw141292 free(zLine);
1066*4520Snw141292 if( zSql && _ends_with_semicolon(zSql, nSql) && sqlite_complete(zSql) ){
1067*4520Snw141292 p->cnt = 0;
1068*4520Snw141292 open_db(p);
1069*4520Snw141292 rc = sqlite_exec(p->db, zSql, callback, p, &zErrMsg);
1070*4520Snw141292 if( rc || zErrMsg ){
1071*4520Snw141292 if( in!=0 && !p->echoOn ) printf("%s\n",zSql);
1072*4520Snw141292 if( zErrMsg!=0 ){
1073*4520Snw141292 printf("SQL error: %s\n", zErrMsg);
1074*4520Snw141292 sqlite_freemem(zErrMsg);
1075*4520Snw141292 zErrMsg = 0;
1076*4520Snw141292 }else{
1077*4520Snw141292 printf("SQL error: %s\n", sqlite_error_string(rc));
1078*4520Snw141292 }
1079*4520Snw141292 }
1080*4520Snw141292 free(zSql);
1081*4520Snw141292 zSql = 0;
1082*4520Snw141292 nSql = 0;
1083*4520Snw141292 }
1084*4520Snw141292 }
1085*4520Snw141292 if( zSql ){
1086*4520Snw141292 if( !_all_whitespace(zSql) ) printf("Incomplete SQL: %s\n", zSql);
1087*4520Snw141292 free(zSql);
1088*4520Snw141292 }
1089*4520Snw141292 }
1090*4520Snw141292
1091*4520Snw141292 /*
1092*4520Snw141292 ** Return a pathname which is the user's home directory. A
1093*4520Snw141292 ** 0 return indicates an error of some kind. Space to hold the
1094*4520Snw141292 ** resulting string is obtained from malloc(). The calling
1095*4520Snw141292 ** function should free the result.
1096*4520Snw141292 */
find_home_dir(void)1097*4520Snw141292 static char *find_home_dir(void){
1098*4520Snw141292 char *home_dir = NULL;
1099*4520Snw141292
1100*4520Snw141292 #if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
1101*4520Snw141292 struct passwd *pwent;
1102*4520Snw141292 uid_t uid = getuid();
1103*4520Snw141292 if( (pwent=getpwuid(uid)) != NULL) {
1104*4520Snw141292 home_dir = pwent->pw_dir;
1105*4520Snw141292 }
1106*4520Snw141292 #endif
1107*4520Snw141292
1108*4520Snw141292 #ifdef __MACOS__
1109*4520Snw141292 char home_path[_MAX_PATH+1];
1110*4520Snw141292 home_dir = getcwd(home_path, _MAX_PATH);
1111*4520Snw141292 #endif
1112*4520Snw141292
1113*4520Snw141292 if (!home_dir) {
1114*4520Snw141292 home_dir = getenv("HOME");
1115*4520Snw141292 if (!home_dir) {
1116*4520Snw141292 home_dir = getenv("HOMEPATH"); /* Windows? */
1117*4520Snw141292 }
1118*4520Snw141292 }
1119*4520Snw141292
1120*4520Snw141292 #if defined(_WIN32) || defined(WIN32)
1121*4520Snw141292 if (!home_dir) {
1122*4520Snw141292 home_dir = "c:";
1123*4520Snw141292 }
1124*4520Snw141292 #endif
1125*4520Snw141292
1126*4520Snw141292 if( home_dir ){
1127*4520Snw141292 char *z = malloc( strlen(home_dir)+1 );
1128*4520Snw141292 if( z ) strcpy(z, home_dir);
1129*4520Snw141292 home_dir = z;
1130*4520Snw141292 }
1131*4520Snw141292
1132*4520Snw141292 return home_dir;
1133*4520Snw141292 }
1134*4520Snw141292
1135*4520Snw141292 /*
1136*4520Snw141292 ** Read input from the file given by sqliterc_override. Or if that
1137*4520Snw141292 ** parameter is NULL, take input from ~/.sqliterc
1138*4520Snw141292 */
process_sqliterc(struct callback_data * p,const char * sqliterc_override)1139*4520Snw141292 static void process_sqliterc(
1140*4520Snw141292 struct callback_data *p, /* Configuration data */
1141*4520Snw141292 const char *sqliterc_override /* Name of config file. NULL to use default */
1142*4520Snw141292 ){
1143*4520Snw141292 char *home_dir = NULL;
1144*4520Snw141292 const char *sqliterc = sqliterc_override;
1145*4520Snw141292 char *zBuf;
1146*4520Snw141292 FILE *in = NULL;
1147*4520Snw141292
1148*4520Snw141292 if (sqliterc == NULL) {
1149*4520Snw141292 home_dir = find_home_dir();
1150*4520Snw141292 if( home_dir==0 ){
1151*4520Snw141292 fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0);
1152*4520Snw141292 return;
1153*4520Snw141292 }
1154*4520Snw141292 zBuf = malloc(strlen(home_dir) + 15);
1155*4520Snw141292 if( zBuf==0 ){
1156*4520Snw141292 fprintf(stderr,"%s: out of memory!\n", Argv0);
1157*4520Snw141292 exit(1);
1158*4520Snw141292 }
1159*4520Snw141292 sprintf(zBuf,"%s/.sqliterc",home_dir);
1160*4520Snw141292 free(home_dir);
1161*4520Snw141292 sqliterc = (const char*)zBuf;
1162*4520Snw141292 }
1163*4520Snw141292 in = fopen(sqliterc,"rb");
1164*4520Snw141292 if( in ){
1165*4520Snw141292 if( isatty(fileno(stdout)) ){
1166*4520Snw141292 printf("Loading resources from %s\n",sqliterc);
1167*4520Snw141292 }
1168*4520Snw141292 process_input(p,in);
1169*4520Snw141292 fclose(in);
1170*4520Snw141292 }
1171*4520Snw141292 return;
1172*4520Snw141292 }
1173*4520Snw141292
1174*4520Snw141292 /*
1175*4520Snw141292 ** Show available command line options
1176*4520Snw141292 */
1177*4520Snw141292 static const char zOptions[] =
1178*4520Snw141292 " -init filename read/process named file\n"
1179*4520Snw141292 " -echo print commands before execution\n"
1180*4520Snw141292 " -[no]header turn headers on or off\n"
1181*4520Snw141292 " -column set output mode to 'column'\n"
1182*4520Snw141292 " -html set output mode to HTML\n"
1183*4520Snw141292 #ifdef SQLITE_HAS_CODEC
1184*4520Snw141292 " -key KEY encryption key\n"
1185*4520Snw141292 #endif
1186*4520Snw141292 " -line set output mode to 'line'\n"
1187*4520Snw141292 " -list set output mode to 'list'\n"
1188*4520Snw141292 " -separator 'x' set output field separator (|)\n"
1189*4520Snw141292 " -nullvalue 'text' set text string for NULL values\n"
1190*4520Snw141292 " -version show SQLite version\n"
1191*4520Snw141292 " -help show this text, also show dot-commands\n"
1192*4520Snw141292 ;
usage(int showDetail)1193*4520Snw141292 static void usage(int showDetail){
1194*4520Snw141292 fprintf(stderr, "Usage: %s [OPTIONS] FILENAME [SQL]\n", Argv0);
1195*4520Snw141292 if( showDetail ){
1196*4520Snw141292 fprintf(stderr, "Options are:\n%s", zOptions);
1197*4520Snw141292 }else{
1198*4520Snw141292 fprintf(stderr, "Use the -help option for additional information\n");
1199*4520Snw141292 }
1200*4520Snw141292 exit(1);
1201*4520Snw141292 }
1202*4520Snw141292
1203*4520Snw141292 /*
1204*4520Snw141292 ** Initialize the state information in data
1205*4520Snw141292 */
main_init(struct callback_data * data)1206*4520Snw141292 void main_init(struct callback_data *data) {
1207*4520Snw141292 memset(data, 0, sizeof(*data));
1208*4520Snw141292 data->mode = MODE_List;
1209*4520Snw141292 strcpy(data->separator,"|");
1210*4520Snw141292 data->showHeader = 0;
1211*4520Snw141292 strcpy(mainPrompt,"sqlite> ");
1212*4520Snw141292 strcpy(continuePrompt," ...> ");
1213*4520Snw141292 }
1214*4520Snw141292
main(int argc,char ** argv)1215*4520Snw141292 int main(int argc, char **argv){
1216*4520Snw141292 char *zErrMsg = 0;
1217*4520Snw141292 struct callback_data data;
1218*4520Snw141292 const char *zInitFile = 0;
1219*4520Snw141292 char *zFirstCmd = 0;
1220*4520Snw141292 int i;
1221*4520Snw141292 extern int sqliteOsFileExists(const char*);
1222*4520Snw141292
1223*4520Snw141292 sqlite_temp_directory = "/etc/svc/volatile"; /* SUNW addition */
1224*4520Snw141292
1225*4520Snw141292 #ifdef __MACOS__
1226*4520Snw141292 argc = ccommand(&argv);
1227*4520Snw141292 #endif
1228*4520Snw141292
1229*4520Snw141292 Argv0 = argv[0];
1230*4520Snw141292 main_init(&data);
1231*4520Snw141292
1232*4520Snw141292 /* Make sure we have a valid signal handler early, before anything
1233*4520Snw141292 ** else is done.
1234*4520Snw141292 */
1235*4520Snw141292 #ifdef SIGINT
1236*4520Snw141292 signal(SIGINT, interrupt_handler);
1237*4520Snw141292 #endif
1238*4520Snw141292
1239*4520Snw141292 /* Do an initial pass through the command-line argument to locate
1240*4520Snw141292 ** the name of the database file, the name of the initialization file,
1241*4520Snw141292 ** and the first command to execute.
1242*4520Snw141292 */
1243*4520Snw141292 for(i=1; i<argc-1; i++){
1244*4520Snw141292 if( argv[i][0]!='-' ) break;
1245*4520Snw141292 if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){
1246*4520Snw141292 i++;
1247*4520Snw141292 }else if( strcmp(argv[i],"-init")==0 ){
1248*4520Snw141292 i++;
1249*4520Snw141292 zInitFile = argv[i];
1250*4520Snw141292 }else if( strcmp(argv[i],"-key")==0 ){
1251*4520Snw141292 i++;
1252*4520Snw141292 data.zKey = sqlite_mprintf("%s",argv[i]);
1253*4520Snw141292 }
1254*4520Snw141292 }
1255*4520Snw141292 if( i<argc ){
1256*4520Snw141292 data.zDbFilename = argv[i++];
1257*4520Snw141292 }else{
1258*4520Snw141292 data.zDbFilename = ":memory:";
1259*4520Snw141292 }
1260*4520Snw141292 if( i<argc ){
1261*4520Snw141292 zFirstCmd = argv[i++];
1262*4520Snw141292 }
1263*4520Snw141292 data.out = stdout;
1264*4520Snw141292
1265*4520Snw141292 /* Go ahead and open the database file if it already exists. If the
1266*4520Snw141292 ** file does not exist, delay opening it. This prevents empty database
1267*4520Snw141292 ** files from being created if a user mistypes the database name argument
1268*4520Snw141292 ** to the sqlite command-line tool.
1269*4520Snw141292 */
1270*4520Snw141292 if( sqliteOsFileExists(data.zDbFilename) ){
1271*4520Snw141292 open_db(&data);
1272*4520Snw141292 }
1273*4520Snw141292
1274*4520Snw141292 /* Process the initialization file if there is one. If no -init option
1275*4520Snw141292 ** is given on the command line, look for a file named ~/.sqliterc and
1276*4520Snw141292 ** try to process it.
1277*4520Snw141292 */
1278*4520Snw141292 process_sqliterc(&data,zInitFile);
1279*4520Snw141292
1280*4520Snw141292 /* Make a second pass through the command-line argument and set
1281*4520Snw141292 ** options. This second pass is delayed until after the initialization
1282*4520Snw141292 ** file is processed so that the command-line arguments will override
1283*4520Snw141292 ** settings in the initialization file.
1284*4520Snw141292 */
1285*4520Snw141292 for(i=1; i<argc && argv[i][0]=='-'; i++){
1286*4520Snw141292 char *z = argv[i];
1287*4520Snw141292 if( strcmp(z,"-init")==0 || strcmp(z,"-key")==0 ){
1288*4520Snw141292 i++;
1289*4520Snw141292 }else if( strcmp(z,"-html")==0 ){
1290*4520Snw141292 data.mode = MODE_Html;
1291*4520Snw141292 }else if( strcmp(z,"-list")==0 ){
1292*4520Snw141292 data.mode = MODE_List;
1293*4520Snw141292 }else if( strcmp(z,"-line")==0 ){
1294*4520Snw141292 data.mode = MODE_Line;
1295*4520Snw141292 }else if( strcmp(z,"-column")==0 ){
1296*4520Snw141292 data.mode = MODE_Column;
1297*4520Snw141292 }else if( strcmp(z,"-separator")==0 ){
1298*4520Snw141292 i++;
1299*4520Snw141292 sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[i]);
1300*4520Snw141292 }else if( strcmp(z,"-nullvalue")==0 ){
1301*4520Snw141292 i++;
1302*4520Snw141292 sprintf(data.nullvalue,"%.*s",(int)sizeof(data.nullvalue)-1,argv[i]);
1303*4520Snw141292 }else if( strcmp(z,"-header")==0 ){
1304*4520Snw141292 data.showHeader = 1;
1305*4520Snw141292 }else if( strcmp(z,"-noheader")==0 ){
1306*4520Snw141292 data.showHeader = 0;
1307*4520Snw141292 }else if( strcmp(z,"-echo")==0 ){
1308*4520Snw141292 data.echoOn = 1;
1309*4520Snw141292 }else if( strcmp(z,"-version")==0 ){
1310*4520Snw141292 printf("%s\n", sqlite_version);
1311*4520Snw141292 return 1;
1312*4520Snw141292 }else if( strcmp(z,"-help")==0 ){
1313*4520Snw141292 usage(1);
1314*4520Snw141292 }else{
1315*4520Snw141292 fprintf(stderr,"%s: unknown option: %s\n", Argv0, z);
1316*4520Snw141292 fprintf(stderr,"Use -help for a list of options.\n");
1317*4520Snw141292 return 1;
1318*4520Snw141292 }
1319*4520Snw141292 }
1320*4520Snw141292
1321*4520Snw141292 if( zFirstCmd ){
1322*4520Snw141292 /* Run just the command that follows the database name
1323*4520Snw141292 */
1324*4520Snw141292 if( zFirstCmd[0]=='.' ){
1325*4520Snw141292 do_meta_command(zFirstCmd, &data);
1326*4520Snw141292 exit(0);
1327*4520Snw141292 }else{
1328*4520Snw141292 int rc;
1329*4520Snw141292 open_db(&data);
1330*4520Snw141292 rc = sqlite_exec(data.db, zFirstCmd, callback, &data, &zErrMsg);
1331*4520Snw141292 if( rc!=0 && zErrMsg!=0 ){
1332*4520Snw141292 fprintf(stderr,"SQL error: %s\n", zErrMsg);
1333*4520Snw141292 exit(1);
1334*4520Snw141292 }
1335*4520Snw141292 }
1336*4520Snw141292 }else{
1337*4520Snw141292 /* Run commands received from standard input
1338*4520Snw141292 */
1339*4520Snw141292 if( isatty(fileno(stdout)) && isatty(fileno(stdin)) ){
1340*4520Snw141292 char *zHome;
1341*4520Snw141292 char *zHistory = 0;
1342*4520Snw141292 printf(
1343*4520Snw141292 "SQLite version %s\n"
1344*4520Snw141292 "Enter \".help\" for instructions\n",
1345*4520Snw141292 sqlite_version
1346*4520Snw141292 );
1347*4520Snw141292 zHome = find_home_dir();
1348*4520Snw141292 if( zHome && (zHistory = malloc(strlen(zHome)+20))!=0 ){
1349*4520Snw141292 sprintf(zHistory,"%s/.sqlite_history", zHome);
1350*4520Snw141292 }
1351*4520Snw141292 if( zHistory ) read_history(zHistory);
1352*4520Snw141292 process_input(&data, 0);
1353*4520Snw141292 if( zHistory ){
1354*4520Snw141292 stifle_history(100);
1355*4520Snw141292 write_history(zHistory);
1356*4520Snw141292 }
1357*4520Snw141292 }else{
1358*4520Snw141292 process_input(&data, stdin);
1359*4520Snw141292 }
1360*4520Snw141292 }
1361*4520Snw141292 set_table_name(&data, 0);
1362*4520Snw141292 if( db ) sqlite_close(db);
1363*4520Snw141292 return 0;
1364*4520Snw141292 }
1365