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