2 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 #pragma ident "%Z%%M% %I% %E% SMI"
11 ** The author disclaims copyright to this source code. In place of
12 ** a legal notice, here is a blessing:
14 ** May you do good and not evil.
15 ** May you find forgiveness for yourself and forgive others.
16 ** May you share freely, never taking more than you give.
18 *************************************************************************
19 ** This file contains code to implement the "sqlite" command line
20 ** utility for accessing SQLite databases.
22 ** $Id: shell.c,v 1.93 2004/03/17 23:42:13 drh Exp $
28 #include "sqlite-misc.h" /* SUNW addition */
31 #if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
35 # include <sys/types.h>
47 #if defined(HAVE_READLINE) && HAVE_READLINE==1
48 # include <readline/readline.h>
49 # include <readline/history.h>
51 # define readline(p) local_getline(p,stdin)
52 # define add_history(X)
53 # define read_history(X)
54 # define write_history(X)
55 # define stifle_history(X)
58 /* Make sure isatty() has a prototype.
63 ** The following is the open SQLite database. We make a pointer
64 ** to this database a static variable so that it can be accessed
65 ** by the SIGINT handler to interrupt database processing.
67 static sqlite
*db
= 0;
70 ** True if an interrupt (Control-C) has been received.
72 static int seenInterrupt
= 0;
75 ** This is the name of our program. It is set in main(), used
76 ** in a number of other places, mostly for error messages.
81 ** Prompt strings. Initialized in main. Settable with
82 ** .prompt main continue
84 static char mainPrompt
[20]; /* First line prompt. default: "sqlite> "*/
85 static char continuePrompt
[20]; /* Continuation prompt. default: " ...> " */
89 ** Determines if a string is a number of not.
91 extern int sqliteIsNumber(const char*);
94 ** This routine reads a line of text from standard input, stores
95 ** the text in memory obtained from malloc() and returns a pointer
96 ** to the text. NULL is returned at end of file, or if malloc()
99 ** The interface is like "readline" but no command-line editing
102 static char *local_getline(char *zPrompt
, FILE *in
){
108 if( zPrompt
&& *zPrompt
){
109 printf("%s",zPrompt
);
113 zLine
= malloc( nLine
);
114 if( zLine
==0 ) return 0;
119 nLine
= nLine
*2 + 100;
120 zLine
= realloc(zLine
, nLine
);
121 if( zLine
==0 ) return 0;
123 if( fgets(&zLine
[n
], nLine
- n
, in
)==0 ){
132 while( zLine
[n
] ){ n
++; }
133 if( n
>0 && zLine
[n
-1]=='\n' ){
139 zLine
= realloc( zLine
, n
+1 );
144 ** Retrieve a single line of input text. "isatty" is true if text
145 ** is coming from a terminal. In that case, we issue a prompt and
146 ** attempt to use "readline" for command-line editing. If "isatty"
147 ** is false, use "local_getline" instead of "readline" and issue no prompt.
149 ** zPrior is a string of prior text retrieved. If not the empty
150 ** string, then issue a continuation prompt.
152 static char *one_input_line(const char *zPrior
, FILE *in
){
156 return local_getline(0, in
);
158 if( zPrior
&& zPrior
[0] ){
159 zPrompt
= continuePrompt
;
161 zPrompt
= mainPrompt
;
163 zResult
= readline(zPrompt
);
164 if( zResult
) add_history(zResult
);
168 struct previous_mode_data
{
169 int valid
; /* Is there legit data in here? */
175 ** An pointer to an instance of this structure is passed from
176 ** the main program to the callback. This is used to communicate
177 ** state and mode information.
179 struct callback_data
{
180 sqlite
*db
; /* The database */
181 int echoOn
; /* True to echo input commands */
182 int cnt
; /* Number of records displayed so far */
183 FILE *out
; /* Write results here */
184 int mode
; /* An output mode setting */
185 int showHeader
; /* True to show column names in List or Column mode */
186 char *zDestTable
; /* Name of destination table when MODE_Insert */
187 char separator
[20]; /* Separator character for MODE_List */
188 int colWidth
[100]; /* Requested width of each column when in column mode*/
189 int actualWidth
[100]; /* Actual width of each column */
190 char nullvalue
[20]; /* The text to print when a NULL comes back from
192 struct previous_mode_data explainPrev
;
193 /* Holds the mode information just before
195 char outfile
[FILENAME_MAX
]; /* Filename for *out */
196 const char *zDbFilename
; /* name of the database file */
197 char *zKey
; /* Encryption key */
201 ** These are the allowed modes.
203 #define MODE_Line 0 /* One column per line. Blank line between records */
204 #define MODE_Column 1 /* One record per line in neat columns */
205 #define MODE_List 2 /* One record per line with a separator */
206 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
207 #define MODE_Html 4 /* Generate an XHTML table */
208 #define MODE_Insert 5 /* Generate SQL "insert" statements */
209 #define MODE_NUM_OF 6 /* The number of modes (not a mode itself) */
211 char *modeDescr
[MODE_NUM_OF
] = {
221 ** Number of elements in an array
223 #define ArraySize(X) (sizeof(X)/sizeof(X[0]))
226 ** Output the given string as a quoted string using SQL quoting conventions.
228 static void output_quoted_string(FILE *out
, const char *z
){
232 if( z
[i
]=='\'' ) nSingle
++;
235 fprintf(out
,"'%s'",z
);
239 for(i
=0; z
[i
] && z
[i
]!='\''; i
++){}
243 }else if( z
[i
]=='\'' ){
244 fprintf(out
,"%.*s''",i
,z
);
256 ** Output the given string with characters that are special to
259 static void output_html_string(FILE *out
, const char *z
){
262 for(i
=0; z
[i
] && z
[i
]!='<' && z
[i
]!='&'; i
++){}
264 fprintf(out
,"%.*s",i
,z
);
268 }else if( z
[i
]=='&' ){
269 fprintf(out
,"&");
278 ** This routine runs when the user presses Ctrl-C
280 static void interrupt_handler(int NotUsed
){
282 if( db
) sqlite_interrupt(db
);
286 ** This is the callback routine that the SQLite library
287 ** invokes for each row of a query result.
289 static int callback(void *pArg
, int nArg
, char **azArg
, char **azCol
){
291 struct callback_data
*p
= (struct callback_data
*)pArg
;
295 if( azArg
==0 ) break;
296 for(i
=0; i
<nArg
; i
++){
297 int len
= strlen(azCol
[i
]);
300 if( p
->cnt
++>0 ) fprintf(p
->out
,"\n");
301 for(i
=0; i
<nArg
; i
++){
302 fprintf(p
->out
,"%*s = %s\n", w
, azCol
[i
],
303 azArg
[i
] ? azArg
[i
] : p
->nullvalue
);
309 for(i
=0; i
<nArg
; i
++){
311 if( i
<ArraySize(p
->colWidth
) ){
317 w
= strlen(azCol
[i
] ? azCol
[i
] : "");
319 n
= strlen(azArg
&& azArg
[i
] ? azArg
[i
] : p
->nullvalue
);
322 if( i
<ArraySize(p
->actualWidth
) ){
323 p
->actualWidth
[i
] = w
;
326 fprintf(p
->out
,"%-*.*s%s",w
,w
,azCol
[i
], i
==nArg
-1 ? "\n": " ");
330 for(i
=0; i
<nArg
; i
++){
332 if( i
<ArraySize(p
->actualWidth
) ){
333 w
= p
->actualWidth
[i
];
337 fprintf(p
->out
,"%-*.*s%s",w
,w
,"-----------------------------------"
338 "----------------------------------------------------------",
339 i
==nArg
-1 ? "\n": " ");
343 if( azArg
==0 ) break;
344 for(i
=0; i
<nArg
; i
++){
346 if( i
<ArraySize(p
->actualWidth
) ){
347 w
= p
->actualWidth
[i
];
351 fprintf(p
->out
,"%-*.*s%s",w
,w
,
352 azArg
[i
] ? azArg
[i
] : p
->nullvalue
, i
==nArg
-1 ? "\n": " ");
358 if( p
->cnt
++==0 && p
->showHeader
){
359 for(i
=0; i
<nArg
; i
++){
360 fprintf(p
->out
,"%s%s",azCol
[i
], i
==nArg
-1 ? "\n" : p
->separator
);
363 if( azArg
==0 ) break;
364 for(i
=0; i
<nArg
; i
++){
366 if( z
==0 ) z
= p
->nullvalue
;
367 fprintf(p
->out
, "%s", z
);
369 fprintf(p
->out
, "%s", p
->separator
);
370 }else if( p
->mode
==MODE_Semi
){
371 fprintf(p
->out
, ";\n");
373 fprintf(p
->out
, "\n");
379 if( p
->cnt
++==0 && p
->showHeader
){
380 fprintf(p
->out
,"<TR>");
381 for(i
=0; i
<nArg
; i
++){
382 fprintf(p
->out
,"<TH>%s</TH>",azCol
[i
]);
384 fprintf(p
->out
,"</TR>\n");
386 if( azArg
==0 ) break;
387 fprintf(p
->out
,"<TR>");
388 for(i
=0; i
<nArg
; i
++){
389 fprintf(p
->out
,"<TD>");
390 output_html_string(p
->out
, azArg
[i
] ? azArg
[i
] : p
->nullvalue
);
391 fprintf(p
->out
,"</TD>\n");
393 fprintf(p
->out
,"</TR>\n");
397 if( azArg
==0 ) break;
398 fprintf(p
->out
,"INSERT INTO %s VALUES(",p
->zDestTable
);
399 for(i
=0; i
<nArg
; i
++){
400 char *zSep
= i
>0 ? ",": "";
402 fprintf(p
->out
,"%sNULL",zSep
);
403 }else if( sqliteIsNumber(azArg
[i
]) ){
404 fprintf(p
->out
,"%s%s",zSep
, azArg
[i
]);
406 if( zSep
[0] ) fprintf(p
->out
,"%s",zSep
);
407 output_quoted_string(p
->out
, azArg
[i
]);
410 fprintf(p
->out
,");\n");
418 ** Set the destination table field of the callback_data structure to
419 ** the name of the table given. Escape any quote characters in the
422 static void set_table_name(struct callback_data
*p
, const char *zName
){
431 if( zName
==0 ) return;
432 needQuote
= !isalpha(*zName
) && *zName
!='_';
433 for(i
=n
=0; zName
[i
]; i
++, n
++){
434 if( !isalnum(zName
[i
]) && zName
[i
]!='_' ){
436 if( zName
[i
]=='\'' ) n
++;
439 if( needQuote
) n
+= 2;
440 z
= p
->zDestTable
= malloc( n
+1 );
442 fprintf(stderr
,"Out of memory!\n");
446 if( needQuote
) z
[n
++] = '\'';
447 for(i
=0; zName
[i
]; i
++){
449 if( zName
[i
]=='\'' ) z
[n
++] = '\'';
451 if( needQuote
) z
[n
++] = '\'';
456 ** This is a different callback routine used for dumping the database.
457 ** Each row received by this callback consists of a table name,
458 ** the table type ("index" or "table") and SQL to create the table.
459 ** This routine should print text sufficient to recreate the table.
461 static int dump_callback(void *pArg
, int nArg
, char **azArg
, char **azCol
){
462 struct callback_data
*p
= (struct callback_data
*)pArg
;
463 if( nArg
!=3 ) return 1;
464 fprintf(p
->out
, "%s;\n", azArg
[2]);
465 if( strcmp(azArg
[1],"table")==0 ){
466 struct callback_data d2
;
468 d2
.mode
= MODE_Insert
;
470 set_table_name(&d2
, azArg
[0]);
471 sqlite_exec_printf(p
->db
,
472 "SELECT * FROM '%q'",
473 callback
, &d2
, 0, azArg
[0]
475 set_table_name(&d2
, 0);
481 ** Text of a help message
483 static char zHelp
[] =
484 ".databases List names and files of attached databases\n"
485 ".dump ?TABLE? ... Dump the database in a text format\n"
486 ".echo ON|OFF Turn command echo on or off\n"
487 ".exit Exit this program\n"
488 ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n"
489 ".header(s) ON|OFF Turn display of headers on or off\n"
490 ".help Show this message\n"
491 ".indices TABLE Show names of all indices on TABLE\n"
492 ".mode MODE Set mode to one of \"line(s)\", \"column(s)\", \n"
493 " \"insert\", \"list\", or \"html\"\n"
494 ".mode insert TABLE Generate SQL insert statements for TABLE\n"
495 ".nullvalue STRING Print STRING instead of nothing for NULL data\n"
496 ".output FILENAME Send output to FILENAME\n"
497 ".output stdout Send output to the screen\n"
498 ".prompt MAIN CONTINUE Replace the standard prompts\n"
499 ".quit Exit this program\n"
500 ".read FILENAME Execute SQL in FILENAME\n"
501 #ifdef SQLITE_HAS_CODEC
502 ".rekey OLD NEW NEW Change the encryption key\n"
504 ".schema ?TABLE? Show the CREATE statements\n"
505 ".separator STRING Change separator string for \"list\" mode\n"
506 ".show Show the current values for various settings\n"
507 ".tables ?PATTERN? List names of tables matching a pattern\n"
508 ".timeout MS Try opening locked tables for MS milliseconds\n"
509 ".width NUM NUM ... Set column widths for \"column\" mode\n"
512 /* Forward reference */
513 static void process_input(struct callback_data
*p
, FILE *in
);
516 ** Make sure the database is open. If it is not, then open it. If
517 ** the database fails to open, print an error message and exit.
519 static void open_db(struct callback_data
*p
){
522 #ifdef SQLITE_HAS_CODEC
523 int n
= p
->zKey
? strlen(p
->zKey
) : 0;
524 db
= p
->db
= sqlite_open_encrypted(p
->zDbFilename
, p
->zKey
, n
, 0, &zErrMsg
);
526 db
= p
->db
= sqlite_open(p
->zDbFilename
, 0, &zErrMsg
);
530 fprintf(stderr
,"Unable to open database \"%s\": %s\n",
531 p
->zDbFilename
, zErrMsg
);
533 fprintf(stderr
,"Unable to open database %s\n", p
->zDbFilename
);
541 ** If an input line begins with "." then invoke this routine to
542 ** process that line.
544 ** Return 1 to exit and 0 to continue.
546 static int do_meta_command(char *zLine
, struct callback_data
*p
){
553 /* Parse the input line into tokens.
555 while( zLine
[i
] && nArg
<ArraySize(azArg
) ){
556 while( isspace(zLine
[i
]) ){ i
++; }
557 if( zLine
[i
]==0 ) break;
558 if( zLine
[i
]=='\'' || zLine
[i
]=='"' ){
559 int delim
= zLine
[i
++];
560 azArg
[nArg
++] = &zLine
[i
];
561 while( zLine
[i
] && zLine
[i
]!=delim
){ i
++; }
562 if( zLine
[i
]==delim
){
566 azArg
[nArg
++] = &zLine
[i
];
567 while( zLine
[i
] && !isspace(zLine
[i
]) ){ i
++; }
568 if( zLine
[i
] ) zLine
[i
++] = 0;
572 /* Process the input line.
574 if( nArg
==0 ) return rc
;
575 n
= strlen(azArg
[0]);
577 if( c
=='d' && n
>1 && strncmp(azArg
[0], "databases", n
)==0 ){
578 struct callback_data data
;
581 memcpy(&data
, p
, sizeof(data
));
583 data
.mode
= MODE_Column
;
584 data
.colWidth
[0] = 3;
585 data
.colWidth
[1] = 15;
586 data
.colWidth
[2] = 58;
587 sqlite_exec(p
->db
, "PRAGMA database_list; ", callback
, &data
, &zErrMsg
);
589 fprintf(stderr
,"Error: %s\n", zErrMsg
);
590 sqlite_freemem(zErrMsg
);
594 if( c
=='d' && strncmp(azArg
[0], "dump", n
)==0 ){
597 fprintf(p
->out
, "BEGIN TRANSACTION;\n");
600 "SELECT name, type, sql FROM sqlite_master "
601 "WHERE type!='meta' AND sql NOT NULL "
602 "ORDER BY substr(type,2,1), name",
603 dump_callback
, p
, &zErrMsg
607 for(i
=1; i
<nArg
&& zErrMsg
==0; i
++){
608 sqlite_exec_printf(p
->db
,
609 "SELECT name, type, sql FROM sqlite_master "
610 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOT NULL "
611 "ORDER BY substr(type,2,1), name",
612 dump_callback
, p
, &zErrMsg
, azArg
[i
]
617 fprintf(stderr
,"Error: %s\n", zErrMsg
);
618 sqlite_freemem(zErrMsg
);
620 fprintf(p
->out
, "COMMIT;\n");
624 if( c
=='e' && strncmp(azArg
[0], "echo", n
)==0 && nArg
>1 ){
627 int val
= atoi(azArg
[1]);
629 if( isupper(z
[j
]) ) z
[j
] = tolower(z
[j
]);
631 if( strcmp(z
,"on")==0 ){
633 }else if( strcmp(z
,"yes")==0 ){
639 if( c
=='e' && strncmp(azArg
[0], "exit", n
)==0 ){
643 if( c
=='e' && strncmp(azArg
[0], "explain", n
)==0 ){
645 char *z
= nArg
>=2 ? azArg
[1] : "1";
648 if( isupper(z
[j
]) ) z
[j
] = tolower(z
[j
]);
650 if( strcmp(z
,"on")==0 ){
652 }else if( strcmp(z
,"yes")==0 ){
656 if(!p
->explainPrev
.valid
) {
657 p
->explainPrev
.valid
= 1;
658 p
->explainPrev
.mode
= p
->mode
;
659 p
->explainPrev
.showHeader
= p
->showHeader
;
660 memcpy(p
->explainPrev
.colWidth
,p
->colWidth
,sizeof(p
->colWidth
));
662 /* We could put this code under the !p->explainValid
663 ** condition so that it does not execute if we are already in
664 ** explain mode. However, always executing it allows us an easy
665 ** was to reset to explain mode in case the user previously
666 ** did an .explain followed by a .width, .mode or .header
669 p
->mode
= MODE_Column
;
671 memset(p
->colWidth
,0,ArraySize(p
->colWidth
));
677 }else if (p
->explainPrev
.valid
) {
678 p
->explainPrev
.valid
= 0;
679 p
->mode
= p
->explainPrev
.mode
;
680 p
->showHeader
= p
->explainPrev
.showHeader
;
681 memcpy(p
->colWidth
,p
->explainPrev
.colWidth
,sizeof(p
->colWidth
));
685 if( c
=='h' && (strncmp(azArg
[0], "header", n
)==0
687 strncmp(azArg
[0], "headers", n
)==0 )&& nArg
>1 ){
690 int val
= atoi(azArg
[1]);
692 if( isupper(z
[j
]) ) z
[j
] = tolower(z
[j
]);
694 if( strcmp(z
,"on")==0 ){
696 }else if( strcmp(z
,"yes")==0 ){
702 if( c
=='h' && strncmp(azArg
[0], "help", n
)==0 ){
703 fprintf(stderr
,zHelp
);
706 if( c
=='i' && strncmp(azArg
[0], "indices", n
)==0 && nArg
>1 ){
707 struct callback_data data
;
710 memcpy(&data
, p
, sizeof(data
));
712 data
.mode
= MODE_List
;
713 sqlite_exec_printf(p
->db
,
714 "SELECT name FROM sqlite_master "
715 "WHERE type='index' AND tbl_name LIKE '%q' "
717 "SELECT name FROM sqlite_temp_master "
718 "WHERE type='index' AND tbl_name LIKE '%q' "
720 callback
, &data
, &zErrMsg
, azArg
[1], azArg
[1]
723 fprintf(stderr
,"Error: %s\n", zErrMsg
);
724 sqlite_freemem(zErrMsg
);
728 if( c
=='m' && strncmp(azArg
[0], "mode", n
)==0 && nArg
>=2 ){
729 int n2
= strlen(azArg
[1]);
730 if( strncmp(azArg
[1],"line",n2
)==0
732 strncmp(azArg
[1],"lines",n2
)==0 ){
734 }else if( strncmp(azArg
[1],"column",n2
)==0
736 strncmp(azArg
[1],"columns",n2
)==0 ){
737 p
->mode
= MODE_Column
;
738 }else if( strncmp(azArg
[1],"list",n2
)==0 ){
740 }else if( strncmp(azArg
[1],"html",n2
)==0 ){
742 }else if( strncmp(azArg
[1],"insert",n2
)==0 ){
743 p
->mode
= MODE_Insert
;
745 set_table_name(p
, azArg
[2]);
747 set_table_name(p
, "table");
750 fprintf(stderr
,"mode should be on of: column html insert line list\n");
754 if( c
=='n' && strncmp(azArg
[0], "nullvalue", n
)==0 && nArg
==2 ) {
755 sprintf(p
->nullvalue
, "%.*s", (int)ArraySize(p
->nullvalue
)-1, azArg
[1]);
758 if( c
=='o' && strncmp(azArg
[0], "output", n
)==0 && nArg
==2 ){
759 if( p
->out
!=stdout
){
762 if( strcmp(azArg
[1],"stdout")==0 ){
764 strcpy(p
->outfile
,"stdout");
766 p
->out
= fopen(azArg
[1], "wb");
768 fprintf(stderr
,"can't write to \"%s\"\n", azArg
[1]);
771 strcpy(p
->outfile
,azArg
[1]);
776 if( c
=='p' && strncmp(azArg
[0], "prompt", n
)==0 && (nArg
==2 || nArg
==3)){
778 strncpy(mainPrompt
,azArg
[1],(int)ArraySize(mainPrompt
)-1);
781 strncpy(continuePrompt
,azArg
[2],(int)ArraySize(continuePrompt
)-1);
785 if( c
=='q' && strncmp(azArg
[0], "quit", n
)==0 ){
789 if( c
=='r' && strncmp(azArg
[0], "read", n
)==0 && nArg
==2 ){
790 FILE *alt
= fopen(azArg
[1], "rb");
792 fprintf(stderr
,"can't open \"%s\"\n", azArg
[1]);
794 process_input(p
, alt
);
799 #ifdef SQLITE_HAS_CODEC
800 if( c
=='r' && strncmp(azArg
[0],"rekey", n
)==0 && nArg
==4 ){
801 char *zOld
= p
->zKey
;
802 if( zOld
==0 ) zOld
= "";
803 if( strcmp(azArg
[1],zOld
) ){
804 fprintf(stderr
,"old key is incorrect\n");
805 }else if( strcmp(azArg
[2], azArg
[3]) ){
806 fprintf(stderr
,"2nd copy of new key does not match the 1st\n");
808 sqlite_freemem(p
->zKey
);
809 p
->zKey
= sqlite_mprintf("%s", azArg
[2]);
810 sqlite_rekey(p
->db
, p
->zKey
, strlen(p
->zKey
));
815 if( c
=='s' && strncmp(azArg
[0], "schema", n
)==0 ){
816 struct callback_data data
;
819 memcpy(&data
, p
, sizeof(data
));
821 data
.mode
= MODE_Semi
;
823 extern int sqliteStrICmp(const char*,const char*);
824 if( sqliteStrICmp(azArg
[1],"sqlite_master")==0 ){
825 char *new_argv
[2], *new_colv
[2];
826 new_argv
[0] = "CREATE TABLE sqlite_master (\n"
830 " rootpage integer,\n"
836 callback(&data
, 1, new_argv
, new_colv
);
837 }else if( sqliteStrICmp(azArg
[1],"sqlite_temp_master")==0 ){
838 char *new_argv
[2], *new_colv
[2];
839 new_argv
[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
843 " rootpage integer,\n"
849 callback(&data
, 1, new_argv
, new_colv
);
851 sqlite_exec_printf(p
->db
,
853 " (SELECT * FROM sqlite_master UNION ALL"
854 " SELECT * FROM sqlite_temp_master) "
855 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOTNULL "
856 "ORDER BY substr(type,2,1), name",
857 callback
, &data
, &zErrMsg
, azArg
[1]);
862 " (SELECT * FROM sqlite_master UNION ALL"
863 " SELECT * FROM sqlite_temp_master) "
864 "WHERE type!='meta' AND sql NOTNULL "
865 "ORDER BY substr(type,2,1), name",
866 callback
, &data
, &zErrMsg
870 fprintf(stderr
,"Error: %s\n", zErrMsg
);
871 sqlite_freemem(zErrMsg
);
875 if( c
=='s' && strncmp(azArg
[0], "separator", n
)==0 && nArg
==2 ){
876 sprintf(p
->separator
, "%.*s", (int)ArraySize(p
->separator
)-1, azArg
[1]);
879 if( c
=='s' && strncmp(azArg
[0], "show", n
)==0){
881 fprintf(p
->out
,"%9.9s: %s\n","echo", p
->echoOn
? "on" : "off");
882 fprintf(p
->out
,"%9.9s: %s\n","explain", p
->explainPrev
.valid
? "on" :"off");
883 fprintf(p
->out
,"%9.9s: %s\n","headers", p
->showHeader
? "on" : "off");
884 fprintf(p
->out
,"%9.9s: %s\n","mode", modeDescr
[p
->mode
]);
885 fprintf(p
->out
,"%9.9s: %s\n","nullvalue", p
->nullvalue
);
886 fprintf(p
->out
,"%9.9s: %s\n","output",
887 strlen(p
->outfile
) ? p
->outfile
: "stdout");
888 fprintf(p
->out
,"%9.9s: %s\n","separator", p
->separator
);
889 fprintf(p
->out
,"%9.9s: ","width");
890 for (i
=0;i
<(int)ArraySize(p
->colWidth
) && p
->colWidth
[i
] != 0;i
++) {
891 fprintf(p
->out
,"%d ",p
->colWidth
[i
]);
893 fprintf(p
->out
,"\n\n");
896 if( c
=='t' && n
>1 && strncmp(azArg
[0], "tables", n
)==0 ){
902 rc
= sqlite_get_table(p
->db
,
903 "SELECT name FROM sqlite_master "
904 "WHERE type IN ('table','view') "
906 "SELECT name FROM sqlite_temp_master "
907 "WHERE type IN ('table','view') "
909 &azResult
, &nRow
, 0, &zErrMsg
912 rc
= sqlite_get_table_printf(p
->db
,
913 "SELECT name FROM sqlite_master "
914 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
916 "SELECT name FROM sqlite_temp_master "
917 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
919 &azResult
, &nRow
, 0, &zErrMsg
, azArg
[1], azArg
[1]
923 fprintf(stderr
,"Error: %s\n", zErrMsg
);
924 sqlite_freemem(zErrMsg
);
929 int nPrintCol
, nPrintRow
;
930 for(i
=1; i
<=nRow
; i
++){
931 if( azResult
[i
]==0 ) continue;
932 len
= strlen(azResult
[i
]);
933 if( len
>maxlen
) maxlen
= len
;
935 nPrintCol
= 80/(maxlen
+2);
936 if( nPrintCol
<1 ) nPrintCol
= 1;
937 nPrintRow
= (nRow
+ nPrintCol
- 1)/nPrintCol
;
938 for(i
=0; i
<nPrintRow
; i
++){
939 for(j
=i
+1; j
<=nRow
; j
+=nPrintRow
){
940 char *zSp
= j
<=nPrintRow
? "" : " ";
941 printf("%s%-*s", zSp
, maxlen
, azResult
[j
] ? azResult
[j
] : "");
946 sqlite_free_table(azResult
);
949 if( c
=='t' && n
>1 && strncmp(azArg
[0], "timeout", n
)==0 && nArg
>=2 ){
951 sqlite_busy_timeout(p
->db
, atoi(azArg
[1]));
954 if( c
=='w' && strncmp(azArg
[0], "width", n
)==0 ){
956 for(j
=1; j
<nArg
&& j
<ArraySize(p
->colWidth
); j
++){
957 p
->colWidth
[j
-1] = atoi(azArg
[j
]);
962 fprintf(stderr
, "unknown command or invalid arguments: "
963 " \"%s\". Enter \".help\" for help\n", azArg
[0]);
970 ** Return TRUE if the last non-whitespace character in z[] is a semicolon.
971 ** z[] is N characters long.
973 static int _ends_with_semicolon(const char *z
, int N
){
974 while( N
>0 && isspace(z
[N
-1]) ){ N
--; }
975 return N
>0 && z
[N
-1]==';';
979 ** Test to see if a line consists entirely of whitespace.
981 static int _all_whitespace(const char *z
){
983 if( isspace(*z
) ) continue;
984 if( *z
=='/' && z
[1]=='*' ){
986 while( *z
&& (*z
!='*' || z
[1]!='/') ){ z
++; }
987 if( *z
==0 ) return 0;
991 if( *z
=='-' && z
[1]=='-' ){
993 while( *z
&& *z
!='\n' ){ z
++; }
994 if( *z
==0 ) return 1;
1003 ** Return TRUE if the line typed in is an SQL command terminator other
1004 ** than a semi-colon. The SQL Server style "go" command is understood
1005 ** as is the Oracle "/".
1007 static int _is_command_terminator(const char *zLine
){
1008 extern int sqliteStrNICmp(const char*,const char*,int);
1009 while( isspace(*zLine
) ){ zLine
++; };
1010 if( zLine
[0]=='/' && _all_whitespace(&zLine
[1]) ) return 1; /* Oracle */
1011 if( sqliteStrNICmp(zLine
,"go",2)==0 && _all_whitespace(&zLine
[2]) ){
1012 return 1; /* SQL Server */
1018 ** Read input from *in and process it. If *in==0 then input
1019 ** is interactive - the user is typing it it. Otherwise, input
1020 ** is coming from a file or device. A prompt is issued and history
1021 ** is saved only if input is interactive. An interrupt signal will
1022 ** cause this routine to exit immediately, unless input is interactive.
1024 static void process_input(struct callback_data
*p
, FILE *in
){
1030 while( fflush(p
->out
), (zLine
= one_input_line(zSql
, in
))!=0 ){
1031 if( seenInterrupt
){
1035 if( p
->echoOn
) printf("%s\n", zLine
);
1036 if( (zSql
==0 || zSql
[0]==0) && _all_whitespace(zLine
) ) continue;
1037 if( zLine
&& zLine
[0]=='.' && nSql
==0 ){
1038 int rc
= do_meta_command(zLine
, p
);
1043 if( _is_command_terminator(zLine
) ){
1048 for(i
=0; zLine
[i
] && isspace(zLine
[i
]); i
++){}
1050 nSql
= strlen(zLine
);
1051 zSql
= malloc( nSql
+1 );
1052 strcpy(zSql
, zLine
);
1055 int len
= strlen(zLine
);
1056 zSql
= realloc( zSql
, nSql
+ len
+ 2 );
1058 fprintf(stderr
,"%s: out of memory!\n", Argv0
);
1061 strcpy(&zSql
[nSql
++], "\n");
1062 strcpy(&zSql
[nSql
], zLine
);
1066 if( zSql
&& _ends_with_semicolon(zSql
, nSql
) && sqlite_complete(zSql
) ){
1069 rc
= sqlite_exec(p
->db
, zSql
, callback
, p
, &zErrMsg
);
1070 if( rc
|| zErrMsg
){
1071 if( in
!=0 && !p
->echoOn
) printf("%s\n",zSql
);
1073 printf("SQL error: %s\n", zErrMsg
);
1074 sqlite_freemem(zErrMsg
);
1077 printf("SQL error: %s\n", sqlite_error_string(rc
));
1086 if( !_all_whitespace(zSql
) ) printf("Incomplete SQL: %s\n", zSql
);
1092 ** Return a pathname which is the user's home directory. A
1093 ** 0 return indicates an error of some kind. Space to hold the
1094 ** resulting string is obtained from malloc(). The calling
1095 ** function should free the result.
1097 static char *find_home_dir(void){
1098 char *home_dir
= NULL
;
1100 #if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
1101 struct passwd
*pwent
;
1102 uid_t uid
= getuid();
1103 if( (pwent
=getpwuid(uid
)) != NULL
) {
1104 home_dir
= pwent
->pw_dir
;
1109 char home_path
[_MAX_PATH
+1];
1110 home_dir
= getcwd(home_path
, _MAX_PATH
);
1114 home_dir
= getenv("HOME");
1116 home_dir
= getenv("HOMEPATH"); /* Windows? */
1120 #if defined(_WIN32) || defined(WIN32)
1127 char *z
= malloc( strlen(home_dir
)+1 );
1128 if( z
) strcpy(z
, home_dir
);
1136 ** Read input from the file given by sqliterc_override. Or if that
1137 ** parameter is NULL, take input from ~/.sqliterc
1139 static void process_sqliterc(
1140 struct callback_data
*p
, /* Configuration data */
1141 const char *sqliterc_override
/* Name of config file. NULL to use default */
1143 char *home_dir
= NULL
;
1144 const char *sqliterc
= sqliterc_override
;
1148 if (sqliterc
== NULL
) {
1149 home_dir
= find_home_dir();
1151 fprintf(stderr
,"%s: cannot locate your home directory!\n", Argv0
);
1154 zBuf
= malloc(strlen(home_dir
) + 15);
1156 fprintf(stderr
,"%s: out of memory!\n", Argv0
);
1159 sprintf(zBuf
,"%s/.sqliterc",home_dir
);
1161 sqliterc
= (const char*)zBuf
;
1163 in
= fopen(sqliterc
,"rb");
1165 if( isatty(fileno(stdout
)) ){
1166 printf("Loading resources from %s\n",sqliterc
);
1168 process_input(p
,in
);
1175 ** Show available command line options
1177 static const char zOptions
[] =
1178 " -init filename read/process named file\n"
1179 " -echo print commands before execution\n"
1180 " -[no]header turn headers on or off\n"
1181 " -column set output mode to 'column'\n"
1182 " -html set output mode to HTML\n"
1183 #ifdef SQLITE_HAS_CODEC
1184 " -key KEY encryption key\n"
1186 " -line set output mode to 'line'\n"
1187 " -list set output mode to 'list'\n"
1188 " -separator 'x' set output field separator (|)\n"
1189 " -nullvalue 'text' set text string for NULL values\n"
1190 " -version show SQLite version\n"
1191 " -help show this text, also show dot-commands\n"
1193 static void usage(int showDetail
){
1194 fprintf(stderr
, "Usage: %s [OPTIONS] FILENAME [SQL]\n", Argv0
);
1196 fprintf(stderr
, "Options are:\n%s", zOptions
);
1198 fprintf(stderr
, "Use the -help option for additional information\n");
1204 ** Initialize the state information in data
1206 void main_init(struct callback_data
*data
) {
1207 memset(data
, 0, sizeof(*data
));
1208 data
->mode
= MODE_List
;
1209 strcpy(data
->separator
,"|");
1210 data
->showHeader
= 0;
1211 strcpy(mainPrompt
,"sqlite> ");
1212 strcpy(continuePrompt
," ...> ");
1215 int main(int argc
, char **argv
){
1217 struct callback_data data
;
1218 const char *zInitFile
= 0;
1219 char *zFirstCmd
= 0;
1221 extern int sqliteOsFileExists(const char*);
1223 sqlite_temp_directory
= "/etc/svc/volatile"; /* SUNW addition */
1226 argc
= ccommand(&argv
);
1232 /* Make sure we have a valid signal handler early, before anything
1236 signal(SIGINT
, interrupt_handler
);
1239 /* Do an initial pass through the command-line argument to locate
1240 ** the name of the database file, the name of the initialization file,
1241 ** and the first command to execute.
1243 for(i
=1; i
<argc
-1; i
++){
1244 if( argv
[i
][0]!='-' ) break;
1245 if( strcmp(argv
[i
],"-separator")==0 || strcmp(argv
[i
],"-nullvalue")==0 ){
1247 }else if( strcmp(argv
[i
],"-init")==0 ){
1249 zInitFile
= argv
[i
];
1250 }else if( strcmp(argv
[i
],"-key")==0 ){
1252 data
.zKey
= sqlite_mprintf("%s",argv
[i
]);
1256 data
.zDbFilename
= argv
[i
++];
1258 data
.zDbFilename
= ":memory:";
1261 zFirstCmd
= argv
[i
++];
1265 /* Go ahead and open the database file if it already exists. If the
1266 ** file does not exist, delay opening it. This prevents empty database
1267 ** files from being created if a user mistypes the database name argument
1268 ** to the sqlite command-line tool.
1270 if( sqliteOsFileExists(data
.zDbFilename
) ){
1274 /* Process the initialization file if there is one. If no -init option
1275 ** is given on the command line, look for a file named ~/.sqliterc and
1276 ** try to process it.
1278 process_sqliterc(&data
,zInitFile
);
1280 /* Make a second pass through the command-line argument and set
1281 ** options. This second pass is delayed until after the initialization
1282 ** file is processed so that the command-line arguments will override
1283 ** settings in the initialization file.
1285 for(i
=1; i
<argc
&& argv
[i
][0]=='-'; i
++){
1287 if( strcmp(z
,"-init")==0 || strcmp(z
,"-key")==0 ){
1289 }else if( strcmp(z
,"-html")==0 ){
1290 data
.mode
= MODE_Html
;
1291 }else if( strcmp(z
,"-list")==0 ){
1292 data
.mode
= MODE_List
;
1293 }else if( strcmp(z
,"-line")==0 ){
1294 data
.mode
= MODE_Line
;
1295 }else if( strcmp(z
,"-column")==0 ){
1296 data
.mode
= MODE_Column
;
1297 }else if( strcmp(z
,"-separator")==0 ){
1299 sprintf(data
.separator
,"%.*s",(int)sizeof(data
.separator
)-1,argv
[i
]);
1300 }else if( strcmp(z
,"-nullvalue")==0 ){
1302 sprintf(data
.nullvalue
,"%.*s",(int)sizeof(data
.nullvalue
)-1,argv
[i
]);
1303 }else if( strcmp(z
,"-header")==0 ){
1304 data
.showHeader
= 1;
1305 }else if( strcmp(z
,"-noheader")==0 ){
1306 data
.showHeader
= 0;
1307 }else if( strcmp(z
,"-echo")==0 ){
1309 }else if( strcmp(z
,"-version")==0 ){
1310 printf("%s\n", sqlite_version
);
1312 }else if( strcmp(z
,"-help")==0 ){
1315 fprintf(stderr
,"%s: unknown option: %s\n", Argv0
, z
);
1316 fprintf(stderr
,"Use -help for a list of options.\n");
1322 /* Run just the command that follows the database name
1324 if( zFirstCmd
[0]=='.' ){
1325 do_meta_command(zFirstCmd
, &data
);
1330 rc
= sqlite_exec(data
.db
, zFirstCmd
, callback
, &data
, &zErrMsg
);
1331 if( rc
!=0 && zErrMsg
!=0 ){
1332 fprintf(stderr
,"SQL error: %s\n", zErrMsg
);
1337 /* Run commands received from standard input
1339 if( isatty(fileno(stdout
)) && isatty(fileno(stdin
)) ){
1343 "SQLite version %s\n"
1344 "Enter \".help\" for instructions\n",
1347 zHome
= find_home_dir();
1348 if( zHome
&& (zHistory
= malloc(strlen(zHome
)+20))!=0 ){
1349 sprintf(zHistory
,"%s/.sqlite_history", zHome
);
1351 if( zHistory
) read_history(zHistory
);
1352 process_input(&data
, 0);
1354 stifle_history(100);
1355 write_history(zHistory
);
1358 process_input(&data
, stdin
);
1361 set_table_name(&data
, 0);
1362 if( db
) sqlite_close(db
);