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 | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 15 | ** $Id: shell.c,v 1.108 2004/08/01 00:10:45 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> |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 20 | #include <assert.h> |
drh | 1d482dd | 2004-05-31 18:23:07 +0000 | [diff] [blame] | 21 | #include "sqlite3.h" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 22 | #include <ctype.h> |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 23 | |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 24 | #if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__) |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 25 | # include <signal.h> |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 26 | # include <pwd.h> |
| 27 | # include <unistd.h> |
| 28 | # include <sys/types.h> |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 29 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 30 | |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 31 | #ifdef __MACOS__ |
| 32 | # include <console.h> |
| 33 | # include <signal.h> |
| 34 | # include <unistd.h> |
| 35 | # include <extras.h> |
| 36 | # include <Files.h> |
| 37 | # include <Folders.h> |
| 38 | #endif |
| 39 | |
drh | 16e5955 | 2000-07-31 11:57:37 +0000 | [diff] [blame] | 40 | #if defined(HAVE_READLINE) && HAVE_READLINE==1 |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 41 | # include <readline/readline.h> |
| 42 | # include <readline/history.h> |
| 43 | #else |
drh | 9347b20 | 2003-07-18 01:30:59 +0000 | [diff] [blame] | 44 | # define readline(p) local_getline(p,stdin) |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 45 | # define add_history(X) |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 46 | # define read_history(X) |
| 47 | # define write_history(X) |
| 48 | # define stifle_history(X) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 49 | #endif |
| 50 | |
drh | 4328c8b | 2003-04-26 02:50:11 +0000 | [diff] [blame] | 51 | /* Make sure isatty() has a prototype. |
| 52 | */ |
| 53 | extern int isatty(); |
| 54 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 55 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 56 | ** The following is the open SQLite database. We make a pointer |
| 57 | ** to this database a static variable so that it can be accessed |
| 58 | ** by the SIGINT handler to interrupt database processing. |
| 59 | */ |
danielk1977 | 92f9a1b | 2004-06-19 09:08:16 +0000 | [diff] [blame] | 60 | static sqlite3 *db = 0; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 61 | |
| 62 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 63 | ** True if an interrupt (Control-C) has been received. |
| 64 | */ |
| 65 | static int seenInterrupt = 0; |
| 66 | |
| 67 | /* |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 68 | ** This is the name of our program. It is set in main(), used |
| 69 | ** in a number of other places, mostly for error messages. |
| 70 | */ |
| 71 | static char *Argv0; |
| 72 | |
| 73 | /* |
| 74 | ** Prompt strings. Initialized in main. Settable with |
| 75 | ** .prompt main continue |
| 76 | */ |
| 77 | static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/ |
| 78 | static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ |
| 79 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 80 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 81 | /* |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 82 | ** Determines if a string is a number of not. |
| 83 | */ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 84 | extern int sqlite3IsNumber(const char*, int*, unsigned char); |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 85 | |
| 86 | /* |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 87 | ** A global char* and an SQL function to access its current value |
| 88 | ** from within an SQL statement. This program used to use the |
| 89 | ** sqlite_exec_printf() API to substitue a string into an SQL statement. |
| 90 | ** The correct way to do this with sqlite3 is to use the bind API, but |
| 91 | ** since the shell is built around the callback paradigm it would be a lot |
| 92 | ** of work. Instead just use this hack, which is quite harmless. |
| 93 | */ |
| 94 | static const char *zShellStatic = 0; |
| 95 | static void shellstaticFunc( |
| 96 | sqlite3_context *context, |
| 97 | int argc, |
| 98 | sqlite3_value **argv |
| 99 | ){ |
| 100 | assert( 0==argc ); |
| 101 | assert( zShellStatic ); |
| 102 | sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 107 | ** This routine reads a line of text from FILE in, stores |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 108 | ** the text in memory obtained from malloc() and returns a pointer |
| 109 | ** to the text. NULL is returned at end of file, or if malloc() |
| 110 | ** fails. |
| 111 | ** |
| 112 | ** The interface is like "readline" but no command-line editing |
| 113 | ** is done. |
| 114 | */ |
drh | 9347b20 | 2003-07-18 01:30:59 +0000 | [diff] [blame] | 115 | static char *local_getline(char *zPrompt, FILE *in){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 116 | char *zLine; |
| 117 | int nLine; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 118 | int n; |
| 119 | int eol; |
| 120 | |
| 121 | if( zPrompt && *zPrompt ){ |
| 122 | printf("%s",zPrompt); |
| 123 | fflush(stdout); |
| 124 | } |
| 125 | nLine = 100; |
| 126 | zLine = malloc( nLine ); |
| 127 | if( zLine==0 ) return 0; |
| 128 | n = 0; |
| 129 | eol = 0; |
| 130 | while( !eol ){ |
| 131 | if( n+100>nLine ){ |
| 132 | nLine = nLine*2 + 100; |
| 133 | zLine = realloc(zLine, nLine); |
| 134 | if( zLine==0 ) return 0; |
| 135 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 136 | if( fgets(&zLine[n], nLine - n, in)==0 ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 137 | if( n==0 ){ |
| 138 | free(zLine); |
| 139 | return 0; |
| 140 | } |
| 141 | zLine[n] = 0; |
| 142 | eol = 1; |
| 143 | break; |
| 144 | } |
| 145 | while( zLine[n] ){ n++; } |
| 146 | if( n>0 && zLine[n-1]=='\n' ){ |
| 147 | n--; |
| 148 | zLine[n] = 0; |
| 149 | eol = 1; |
| 150 | } |
| 151 | } |
| 152 | zLine = realloc( zLine, n+1 ); |
| 153 | return zLine; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | ** Retrieve a single line of input text. "isatty" is true if text |
| 158 | ** is coming from a terminal. In that case, we issue a prompt and |
| 159 | ** attempt to use "readline" for command-line editing. If "isatty" |
drh | 9347b20 | 2003-07-18 01:30:59 +0000 | [diff] [blame] | 160 | ** is false, use "local_getline" instead of "readline" and issue no prompt. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 161 | ** |
| 162 | ** zPrior is a string of prior text retrieved. If not the empty |
| 163 | ** string, then issue a continuation prompt. |
| 164 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 165 | static char *one_input_line(const char *zPrior, FILE *in){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 166 | char *zPrompt; |
| 167 | char *zResult; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 168 | if( in!=0 ){ |
drh | 9347b20 | 2003-07-18 01:30:59 +0000 | [diff] [blame] | 169 | return local_getline(0, in); |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 170 | } |
| 171 | if( zPrior && zPrior[0] ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 172 | zPrompt = continuePrompt; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 173 | }else{ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 174 | zPrompt = mainPrompt; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 175 | } |
| 176 | zResult = readline(zPrompt); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 177 | if( zResult ) add_history(zResult); |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 178 | return zResult; |
| 179 | } |
| 180 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 181 | struct previous_mode_data { |
| 182 | int valid; /* Is there legit data in here? */ |
| 183 | int mode; |
| 184 | int showHeader; |
| 185 | int colWidth[100]; |
| 186 | }; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 187 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 188 | ** An pointer to an instance of this structure is passed from |
| 189 | ** the main program to the callback. This is used to communicate |
| 190 | ** state and mode information. |
| 191 | */ |
| 192 | struct callback_data { |
danielk1977 | 92f9a1b | 2004-06-19 09:08:16 +0000 | [diff] [blame] | 193 | sqlite3 *db; /* The database */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 194 | int echoOn; /* True to echo input commands */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 195 | int cnt; /* Number of records displayed so far */ |
| 196 | FILE *out; /* Write results here */ |
| 197 | int mode; /* An output mode setting */ |
| 198 | int showHeader; /* True to show column names in List or Column mode */ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 199 | char *zDestTable; /* Name of destination table when MODE_Insert */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 200 | char separator[20]; /* Separator character for MODE_List */ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 201 | int colWidth[100]; /* Requested width of each column when in column mode*/ |
| 202 | int actualWidth[100]; /* Actual width of each column */ |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 203 | char nullvalue[20]; /* The text to print when a NULL comes back from |
| 204 | ** the database */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 205 | struct previous_mode_data explainPrev; |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 206 | /* Holds the mode information just before |
| 207 | ** .explain ON */ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 208 | char outfile[FILENAME_MAX]; /* Filename for *out */ |
| 209 | const char *zDbFilename; /* name of the database file */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 210 | char *zKey; /* Encryption key */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | /* |
| 214 | ** These are the allowed modes. |
| 215 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 216 | #define MODE_Line 0 /* One column per line. Blank line between records */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 217 | #define MODE_Column 1 /* One record per line in neat columns */ |
| 218 | #define MODE_List 2 /* One record per line with a separator */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 219 | #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
| 220 | #define MODE_Html 4 /* Generate an XHTML table */ |
| 221 | #define MODE_Insert 5 /* Generate SQL "insert" statements */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 222 | #define MODE_Tcl 6 /* Generate ANSI-C or TCL quoted elements */ |
| 223 | #define MODE_NUM_OF 7 /* The number of modes (not a mode itself) */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 224 | |
| 225 | char *modeDescr[MODE_NUM_OF] = { |
| 226 | "line", |
| 227 | "column", |
| 228 | "list", |
| 229 | "semi", |
| 230 | "html", |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 231 | "insert", |
| 232 | "tcl", |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 233 | }; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 234 | |
| 235 | /* |
| 236 | ** Number of elements in an array |
| 237 | */ |
| 238 | #define ArraySize(X) (sizeof(X)/sizeof(X[0])) |
| 239 | |
| 240 | /* |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 241 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 242 | */ |
| 243 | static void output_quoted_string(FILE *out, const char *z){ |
| 244 | int i; |
| 245 | int nSingle = 0; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 246 | for(i=0; z[i]; i++){ |
| 247 | if( z[i]=='\'' ) nSingle++; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 248 | } |
| 249 | if( nSingle==0 ){ |
| 250 | fprintf(out,"'%s'",z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 251 | }else{ |
| 252 | fprintf(out,"'"); |
| 253 | while( *z ){ |
| 254 | for(i=0; z[i] && z[i]!='\''; i++){} |
| 255 | if( i==0 ){ |
| 256 | fprintf(out,"''"); |
| 257 | z++; |
| 258 | }else if( z[i]=='\'' ){ |
| 259 | fprintf(out,"%.*s''",i,z); |
| 260 | z += i+1; |
| 261 | }else{ |
drh | cd7d273 | 2002-02-26 23:24:26 +0000 | [diff] [blame] | 262 | fprintf(out,"%s",z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 263 | break; |
| 264 | } |
| 265 | } |
drh | cd7d273 | 2002-02-26 23:24:26 +0000 | [diff] [blame] | 266 | fprintf(out,"'"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
| 270 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 271 | ** Output the given string as a quoted according to C or TCL quoting rules. |
| 272 | */ |
| 273 | static void output_c_string(FILE *out, const char *z){ |
| 274 | unsigned int c; |
| 275 | fputc('"', out); |
| 276 | while( (c = *(z++))!=0 ){ |
| 277 | if( c=='\\' ){ |
| 278 | fputc(c, out); |
| 279 | fputc(c, out); |
| 280 | }else if( c=='\t' ){ |
| 281 | fputc('\\', out); |
| 282 | fputc('t', out); |
| 283 | }else if( c=='\n' ){ |
| 284 | fputc('\\', out); |
| 285 | fputc('n', out); |
| 286 | }else if( c=='\r' ){ |
| 287 | fputc('\\', out); |
| 288 | fputc('r', out); |
| 289 | }else if( !isprint(c) ){ |
| 290 | fprintf(out, "\\%03o", c); |
| 291 | }else{ |
| 292 | fputc(c, out); |
| 293 | } |
| 294 | } |
| 295 | fputc('"', out); |
| 296 | } |
| 297 | |
| 298 | /* |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 299 | ** Output the given string with characters that are special to |
| 300 | ** HTML escaped. |
| 301 | */ |
| 302 | static void output_html_string(FILE *out, const char *z){ |
| 303 | int i; |
| 304 | while( *z ){ |
| 305 | for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){} |
| 306 | if( i>0 ){ |
| 307 | fprintf(out,"%.*s",i,z); |
| 308 | } |
| 309 | if( z[i]=='<' ){ |
| 310 | fprintf(out,"<"); |
| 311 | }else if( z[i]=='&' ){ |
| 312 | fprintf(out,"&"); |
| 313 | }else{ |
| 314 | break; |
| 315 | } |
| 316 | z += i + 1; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 321 | ** This routine runs when the user presses Ctrl-C |
| 322 | */ |
| 323 | static void interrupt_handler(int NotUsed){ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 324 | seenInterrupt = 1; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 325 | if( db ) sqlite3_interrupt(db); |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 329 | ** This is the callback routine that the SQLite library |
| 330 | ** invokes for each row of a query result. |
| 331 | */ |
| 332 | static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 333 | int i; |
| 334 | struct callback_data *p = (struct callback_data*)pArg; |
| 335 | switch( p->mode ){ |
| 336 | case MODE_Line: { |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 337 | int w = 5; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 338 | if( azArg==0 ) break; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 339 | for(i=0; i<nArg; i++){ |
| 340 | int len = strlen(azCol[i]); |
| 341 | if( len>w ) w = len; |
| 342 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 343 | if( p->cnt++>0 ) fprintf(p->out,"\n"); |
| 344 | for(i=0; i<nArg; i++){ |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 345 | fprintf(p->out,"%*s = %s\n", w, azCol[i], |
| 346 | azArg[i] ? azArg[i] : p->nullvalue); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 347 | } |
| 348 | break; |
| 349 | } |
| 350 | case MODE_Column: { |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 351 | if( p->cnt++==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 352 | for(i=0; i<nArg; i++){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 353 | int w, n; |
| 354 | if( i<ArraySize(p->colWidth) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 355 | w = p->colWidth[i]; |
| 356 | }else{ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 357 | w = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 358 | } |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 359 | if( w<=0 ){ |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 360 | w = strlen(azCol[i] ? azCol[i] : ""); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 361 | if( w<10 ) w = 10; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 362 | n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 363 | if( w<n ) w = n; |
| 364 | } |
| 365 | if( i<ArraySize(p->actualWidth) ){ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 366 | p->actualWidth[i] = w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 367 | } |
| 368 | if( p->showHeader ){ |
| 369 | fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " "); |
| 370 | } |
| 371 | } |
| 372 | if( p->showHeader ){ |
| 373 | for(i=0; i<nArg; i++){ |
| 374 | int w; |
| 375 | if( i<ArraySize(p->actualWidth) ){ |
| 376 | w = p->actualWidth[i]; |
| 377 | }else{ |
| 378 | w = 10; |
| 379 | } |
| 380 | fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------" |
| 381 | "----------------------------------------------------------", |
| 382 | i==nArg-1 ? "\n": " "); |
| 383 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 386 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 387 | for(i=0; i<nArg; i++){ |
| 388 | int w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 389 | if( i<ArraySize(p->actualWidth) ){ |
| 390 | w = p->actualWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 391 | }else{ |
| 392 | w = 10; |
| 393 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 394 | fprintf(p->out,"%-*.*s%s",w,w, |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 395 | azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " "); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 396 | } |
| 397 | break; |
| 398 | } |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 399 | case MODE_Semi: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 400 | case MODE_List: { |
| 401 | if( p->cnt++==0 && p->showHeader ){ |
| 402 | for(i=0; i<nArg; i++){ |
| 403 | fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator); |
| 404 | } |
| 405 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 406 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 407 | for(i=0; i<nArg; i++){ |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 408 | char *z = azArg[i]; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 409 | if( z==0 ) z = p->nullvalue; |
drh | 71172c5 | 2002-01-24 00:00:21 +0000 | [diff] [blame] | 410 | fprintf(p->out, "%s", z); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 411 | if( i<nArg-1 ){ |
| 412 | fprintf(p->out, "%s", p->separator); |
| 413 | }else if( p->mode==MODE_Semi ){ |
| 414 | fprintf(p->out, ";\n"); |
| 415 | }else{ |
| 416 | fprintf(p->out, "\n"); |
| 417 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 418 | } |
| 419 | break; |
| 420 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 421 | case MODE_Html: { |
| 422 | if( p->cnt++==0 && p->showHeader ){ |
| 423 | fprintf(p->out,"<TR>"); |
| 424 | for(i=0; i<nArg; i++){ |
| 425 | fprintf(p->out,"<TH>%s</TH>",azCol[i]); |
| 426 | } |
| 427 | fprintf(p->out,"</TR>\n"); |
| 428 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 429 | if( azArg==0 ) break; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 430 | fprintf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 431 | for(i=0; i<nArg; i++){ |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 432 | fprintf(p->out,"<TD>"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 433 | output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 434 | fprintf(p->out,"</TD>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 435 | } |
drh | 7d686b2 | 2002-11-11 13:56:47 +0000 | [diff] [blame] | 436 | fprintf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 437 | break; |
| 438 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 439 | case MODE_Tcl: { |
| 440 | if( p->cnt++==0 && p->showHeader ){ |
| 441 | for(i=0; i<nArg; i++){ |
| 442 | output_c_string(p->out,azCol[i]); |
| 443 | fprintf(p->out, "%s", p->separator); |
| 444 | } |
| 445 | fprintf(p->out,"\n"); |
| 446 | } |
| 447 | if( azArg==0 ) break; |
| 448 | for(i=0; i<nArg; i++){ |
| 449 | output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue); |
| 450 | fprintf(p->out, "%s", p->separator); |
| 451 | } |
| 452 | fprintf(p->out,"\n"); |
| 453 | break; |
| 454 | } |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 455 | case MODE_Insert: { |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 456 | if( azArg==0 ) break; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 457 | fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 458 | for(i=0; i<nArg; i++){ |
| 459 | char *zSep = i>0 ? ",": ""; |
| 460 | if( azArg[i]==0 ){ |
| 461 | fprintf(p->out,"%sNULL",zSep); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 462 | }else if( sqlite3IsNumber(azArg[i], 0, 1) ){ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 463 | fprintf(p->out,"%s%s",zSep, azArg[i]); |
| 464 | }else{ |
| 465 | if( zSep[0] ) fprintf(p->out,"%s",zSep); |
| 466 | output_quoted_string(p->out, azArg[i]); |
| 467 | } |
| 468 | } |
| 469 | fprintf(p->out,");\n"); |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 470 | break; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 471 | } |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 472 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | /* |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 477 | ** Set the destination table field of the callback_data structure to |
| 478 | ** the name of the table given. Escape any quote characters in the |
| 479 | ** table name. |
| 480 | */ |
| 481 | static void set_table_name(struct callback_data *p, const char *zName){ |
| 482 | int i, n; |
| 483 | int needQuote; |
| 484 | char *z; |
| 485 | |
| 486 | if( p->zDestTable ){ |
| 487 | free(p->zDestTable); |
| 488 | p->zDestTable = 0; |
| 489 | } |
| 490 | if( zName==0 ) return; |
| 491 | needQuote = !isalpha(*zName) && *zName!='_'; |
| 492 | for(i=n=0; zName[i]; i++, n++){ |
| 493 | if( !isalnum(zName[i]) && zName[i]!='_' ){ |
| 494 | needQuote = 1; |
| 495 | if( zName[i]=='\'' ) n++; |
| 496 | } |
| 497 | } |
| 498 | if( needQuote ) n += 2; |
| 499 | z = p->zDestTable = malloc( n+1 ); |
| 500 | if( z==0 ){ |
| 501 | fprintf(stderr,"Out of memory!\n"); |
| 502 | exit(1); |
| 503 | } |
| 504 | n = 0; |
| 505 | if( needQuote ) z[n++] = '\''; |
| 506 | for(i=0; zName[i]; i++){ |
| 507 | z[n++] = zName[i]; |
| 508 | if( zName[i]=='\'' ) z[n++] = '\''; |
| 509 | } |
| 510 | if( needQuote ) z[n++] = '\''; |
| 511 | z[n] = 0; |
| 512 | } |
| 513 | |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 514 | /* zIn is either a pointer to a NULL-terminated string in memory obtained |
| 515 | ** from malloc(), or a NULL pointer. The string pointed to by zAppend is |
| 516 | ** added to zIn, and the result returned in memory obtained from malloc(). |
| 517 | ** zIn, if it was not NULL, is freed. |
| 518 | ** |
| 519 | ** If the third argument, quote, is not '\0', then it is used as a |
| 520 | ** quote character for zAppend. |
| 521 | */ |
| 522 | static char * appendText(char *zIn, char const *zAppend, char quote){ |
| 523 | int len; |
| 524 | int i; |
| 525 | int nAppend = strlen(zAppend); |
| 526 | int nIn = (zIn?strlen(zIn):0); |
| 527 | |
| 528 | len = nAppend+nIn+1; |
| 529 | if( quote ){ |
| 530 | len += 2; |
| 531 | for(i=0; i<nAppend; i++){ |
| 532 | if( zAppend[i]==quote ) len++; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | zIn = (char *)realloc(zIn, len); |
| 537 | if( !zIn ){ |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | if( quote ){ |
| 542 | char *zCsr = &zIn[nIn]; |
| 543 | *zCsr++ = quote; |
| 544 | for(i=0; i<nAppend; i++){ |
| 545 | *zCsr++ = zAppend[i]; |
| 546 | if( zAppend[i]==quote ) *zCsr++ = quote; |
| 547 | } |
| 548 | *zCsr++ = quote; |
| 549 | *zCsr++ = '\0'; |
| 550 | assert( (zCsr-zIn)==len ); |
| 551 | }else{ |
| 552 | memcpy(&zIn[nIn], zAppend, nAppend); |
| 553 | zIn[len-1] = '\0'; |
| 554 | } |
| 555 | |
| 556 | return zIn; |
| 557 | } |
| 558 | |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 559 | /* |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 560 | ** This is a different callback routine used for dumping the database. |
| 561 | ** Each row received by this callback consists of a table name, |
| 562 | ** the table type ("index" or "table") and SQL to create the table. |
| 563 | ** This routine should print text sufficient to recreate the table. |
| 564 | */ |
| 565 | static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){ |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 566 | int rc; |
| 567 | const char *zTable; |
| 568 | const char *zType; |
| 569 | const char *zSql; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 570 | struct callback_data *p = (struct callback_data *)pArg; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 571 | |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 572 | if( nArg!=3 ) return 1; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 573 | zTable = azArg[0]; |
| 574 | zType = azArg[1]; |
| 575 | zSql = azArg[2]; |
| 576 | |
| 577 | fprintf(p->out, "%s;\n", zSql); |
| 578 | |
| 579 | if( strcmp(zType, "table")==0 ){ |
| 580 | sqlite3_stmt *pTableInfo = 0; |
| 581 | sqlite3_stmt *pSelect = 0; |
| 582 | char *zSelect = 0; |
| 583 | char *zTableInfo = 0; |
| 584 | char *zTmp = 0; |
| 585 | |
| 586 | zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0); |
| 587 | zTableInfo = appendText(zTableInfo, zTable, '"'); |
| 588 | zTableInfo = appendText(zTableInfo, ");", 0); |
| 589 | |
| 590 | rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0); |
| 591 | if( zTableInfo ) free(zTableInfo); |
| 592 | if( rc!=SQLITE_OK || !pTableInfo ){ |
| 593 | return 1; |
| 594 | } |
| 595 | |
| 596 | zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0); |
| 597 | zTmp = appendText(zTmp, zTable, '"'); |
| 598 | if( zTmp ){ |
| 599 | zSelect = appendText(zSelect, zTmp, '\''); |
| 600 | } |
| 601 | zSelect = appendText(zSelect, " || ' VALUES(' || ", 0); |
| 602 | rc = sqlite3_step(pTableInfo); |
| 603 | while( rc==SQLITE_ROW ){ |
danielk1977 | 3f41e97 | 2004-06-08 00:39:01 +0000 | [diff] [blame] | 604 | zSelect = appendText(zSelect, "quote(", 0); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 605 | zSelect = appendText(zSelect, sqlite3_column_text(pTableInfo, 1), '"'); |
| 606 | rc = sqlite3_step(pTableInfo); |
| 607 | if( rc==SQLITE_ROW ){ |
| 608 | zSelect = appendText(zSelect, ") || ', ' || ", 0); |
| 609 | }else{ |
| 610 | zSelect = appendText(zSelect, ") ", 0); |
| 611 | } |
| 612 | } |
| 613 | rc = sqlite3_finalize(pTableInfo); |
| 614 | if( rc!=SQLITE_OK ){ |
| 615 | if( zSelect ) free(zSelect); |
| 616 | return 1; |
| 617 | } |
| 618 | zSelect = appendText(zSelect, "|| ')' FROM ", 0); |
| 619 | zSelect = appendText(zSelect, zTable, '"'); |
| 620 | |
| 621 | rc = sqlite3_prepare(p->db, zSelect, -1, &pSelect, 0); |
| 622 | if( zSelect ) free(zSelect); |
| 623 | if( rc!=SQLITE_OK || !pSelect ){ |
| 624 | return 1; |
| 625 | } |
| 626 | |
| 627 | rc = sqlite3_step(pSelect); |
| 628 | while( rc==SQLITE_ROW ){ |
| 629 | fprintf(p->out, "%s;\n", sqlite3_column_text(pSelect, 0)); |
| 630 | rc = sqlite3_step(pSelect); |
| 631 | } |
| 632 | rc = sqlite3_finalize(pSelect); |
| 633 | if( rc!=SQLITE_OK ){ |
| 634 | return 1; |
| 635 | } |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 636 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 637 | |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 642 | ** Text of a help message |
| 643 | */ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 644 | static char zHelp[] = |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 645 | ".databases List names and files of attached databases\n" |
| 646 | ".dump ?TABLE? ... Dump the database in a text format\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 647 | ".echo ON|OFF Turn command echo on or off\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 648 | ".exit Exit this program\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 649 | ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 650 | ".header(s) ON|OFF Turn display of headers on or off\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 651 | ".help Show this message\n" |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 652 | ".import FILE TABLE Import data from FILE\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 653 | ".indices TABLE Show names of all indices on TABLE\n" |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 654 | ".mode MODE Set mode to one of: cvs column html insert line\n" |
| 655 | " list tabs tcl\n" |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 656 | ".mode insert TABLE Generate SQL insert statements for TABLE\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 657 | ".nullvalue STRING Print STRING instead of nothing for NULL data\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 658 | ".output FILENAME Send output to FILENAME\n" |
| 659 | ".output stdout Send output to the screen\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 660 | ".prompt MAIN CONTINUE Replace the standard prompts\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 661 | ".quit Exit this program\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 662 | ".read FILENAME Execute SQL in FILENAME\n" |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 663 | #ifdef SQLITE_HAS_CODEC |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 664 | ".rekey OLD NEW NEW Change the encryption key\n" |
| 665 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 666 | ".schema ?TABLE? Show the CREATE statements\n" |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 667 | ".separator STRING Change separator string\n" |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 668 | ".show Show the current values for various settings\n" |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 669 | ".tables ?PATTERN? List names of tables matching a LIKE pattern\n" |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 670 | ".timeout MS Try opening locked tables for MS milliseconds\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 671 | ".width NUM NUM ... Set column widths for \"column\" mode\n" |
| 672 | ; |
| 673 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 674 | /* Forward reference */ |
| 675 | static void process_input(struct callback_data *p, FILE *in); |
| 676 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 677 | /* |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 678 | ** Make sure the database is open. If it is not, then open it. If |
| 679 | ** the database fails to open, print an error message and exit. |
| 680 | */ |
| 681 | static void open_db(struct callback_data *p){ |
| 682 | if( p->db==0 ){ |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 683 | sqlite3_open(p->zDbFilename, &p->db); |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 684 | db = p->db; |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 685 | #ifdef SQLITE_HAS_CODEC |
| 686 | sqlite3_key(p->db, p->zKey, p->zKey ? strlen(p->zKey) : 0); |
drh | eb8ed70 | 2004-02-11 10:37:23 +0000 | [diff] [blame] | 687 | #endif |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 688 | sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0, |
| 689 | shellstaticFunc, 0, 0); |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 690 | if( SQLITE_OK!=sqlite3_errcode(db) ){ |
| 691 | fprintf(stderr,"Unable to open database \"%s\": %s\n", |
| 692 | p->zDbFilename, sqlite3_errmsg(db)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 693 | exit(1); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 699 | ** Do C-language style dequoting. |
| 700 | ** |
| 701 | ** \t -> tab |
| 702 | ** \n -> newline |
| 703 | ** \r -> carriage return |
| 704 | ** \NNN -> ascii character NNN in octal |
| 705 | ** \\ -> backslash |
| 706 | */ |
| 707 | static void resolve_backslashes(char *z){ |
| 708 | int i, j, c; |
| 709 | for(i=j=0; (c = z[i])!=0; i++, j++){ |
| 710 | if( c=='\\' ){ |
| 711 | c = z[++i]; |
| 712 | if( c=='n' ){ |
| 713 | c = '\n'; |
| 714 | }else if( c=='t' ){ |
| 715 | c = '\t'; |
| 716 | }else if( c=='r' ){ |
| 717 | c = '\r'; |
| 718 | }else if( c>='0' && c<='7' ){ |
| 719 | c =- '0'; |
| 720 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 721 | i++; |
| 722 | c = (c<<3) + z[i] - '0'; |
| 723 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 724 | i++; |
| 725 | c = (c<<3) + z[i] - '0'; |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | z[j] = c; |
| 731 | } |
| 732 | z[j] = 0; |
| 733 | } |
| 734 | |
| 735 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 736 | ** If an input line begins with "." then invoke this routine to |
| 737 | ** process that line. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 738 | ** |
| 739 | ** Return 1 to exit and 0 to continue. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 740 | */ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 741 | static int do_meta_command(char *zLine, struct callback_data *p){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 742 | int i = 1; |
| 743 | int nArg = 0; |
| 744 | int n, c; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 745 | int rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 746 | char *azArg[50]; |
| 747 | |
| 748 | /* Parse the input line into tokens. |
| 749 | */ |
| 750 | while( zLine[i] && nArg<ArraySize(azArg) ){ |
| 751 | while( isspace(zLine[i]) ){ i++; } |
drh | 0633368 | 2004-03-09 13:37:45 +0000 | [diff] [blame] | 752 | if( zLine[i]==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 753 | if( zLine[i]=='\'' || zLine[i]=='"' ){ |
| 754 | int delim = zLine[i++]; |
| 755 | azArg[nArg++] = &zLine[i]; |
| 756 | while( zLine[i] && zLine[i]!=delim ){ i++; } |
| 757 | if( zLine[i]==delim ){ |
| 758 | zLine[i++] = 0; |
| 759 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 760 | if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 761 | }else{ |
| 762 | azArg[nArg++] = &zLine[i]; |
| 763 | while( zLine[i] && !isspace(zLine[i]) ){ i++; } |
| 764 | if( zLine[i] ) zLine[i++] = 0; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 765 | resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | |
| 769 | /* Process the input line. |
| 770 | */ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 771 | if( nArg==0 ) return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 772 | n = strlen(azArg[0]); |
| 773 | c = azArg[0][0]; |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 774 | if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 775 | struct callback_data data; |
| 776 | char *zErrMsg = 0; |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 777 | open_db(p); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 778 | memcpy(&data, p, sizeof(data)); |
drh | d888544 | 2004-03-17 23:42:12 +0000 | [diff] [blame] | 779 | data.showHeader = 1; |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 780 | data.mode = MODE_Column; |
drh | d888544 | 2004-03-17 23:42:12 +0000 | [diff] [blame] | 781 | data.colWidth[0] = 3; |
| 782 | data.colWidth[1] = 15; |
| 783 | data.colWidth[2] = 58; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 784 | sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 785 | if( zErrMsg ){ |
| 786 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 787 | sqlite3_free(zErrMsg); |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 788 | } |
| 789 | }else |
| 790 | |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 791 | if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
| 792 | char *zErrMsg = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 793 | open_db(p); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 794 | fprintf(p->out, "BEGIN TRANSACTION;\n"); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 795 | if( nArg==1 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 796 | sqlite3_exec(p->db, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 797 | "SELECT name, type, sql FROM sqlite_master " |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 798 | "WHERE type!='meta' AND sql NOT NULL " |
drh | fc6cdfe | 2002-04-13 23:42:24 +0000 | [diff] [blame] | 799 | "ORDER BY substr(type,2,1), name", |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 800 | dump_callback, p, &zErrMsg |
| 801 | ); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 802 | }else{ |
| 803 | int i; |
| 804 | for(i=1; i<nArg && zErrMsg==0; i++){ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 805 | zShellStatic = azArg[i]; |
| 806 | sqlite3_exec(p->db, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 807 | "SELECT name, type, sql FROM sqlite_master " |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 808 | "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOT NULL " |
drh | fc6cdfe | 2002-04-13 23:42:24 +0000 | [diff] [blame] | 809 | "ORDER BY substr(type,2,1), name", |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 810 | dump_callback, p, &zErrMsg |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 811 | ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 812 | zShellStatic = 0; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | if( zErrMsg ){ |
| 816 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 817 | sqlite3_free(zErrMsg); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 818 | }else{ |
| 819 | fprintf(p->out, "COMMIT;\n"); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 820 | } |
| 821 | }else |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 822 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 823 | if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){ |
| 824 | int j; |
| 825 | char *z = azArg[1]; |
| 826 | int val = atoi(azArg[1]); |
| 827 | for(j=0; z[j]; j++){ |
| 828 | if( isupper(z[j]) ) z[j] = tolower(z[j]); |
| 829 | } |
| 830 | if( strcmp(z,"on")==0 ){ |
| 831 | val = 1; |
| 832 | }else if( strcmp(z,"yes")==0 ){ |
| 833 | val = 1; |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 834 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 835 | p->echoOn = val; |
| 836 | }else |
| 837 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 838 | if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 839 | rc = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 840 | }else |
| 841 | |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 842 | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 843 | int j; |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 844 | char *z = nArg>=2 ? azArg[1] : "1"; |
| 845 | int val = atoi(z); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 846 | for(j=0; z[j]; j++){ |
| 847 | if( isupper(z[j]) ) z[j] = tolower(z[j]); |
| 848 | } |
| 849 | if( strcmp(z,"on")==0 ){ |
| 850 | val = 1; |
| 851 | }else if( strcmp(z,"yes")==0 ){ |
| 852 | val = 1; |
| 853 | } |
| 854 | if(val == 1) { |
| 855 | if(!p->explainPrev.valid) { |
| 856 | p->explainPrev.valid = 1; |
| 857 | p->explainPrev.mode = p->mode; |
| 858 | p->explainPrev.showHeader = p->showHeader; |
| 859 | memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth)); |
| 860 | } |
| 861 | /* We could put this code under the !p->explainValid |
| 862 | ** condition so that it does not execute if we are already in |
| 863 | ** explain mode. However, always executing it allows us an easy |
| 864 | ** was to reset to explain mode in case the user previously |
| 865 | ** did an .explain followed by a .width, .mode or .header |
| 866 | ** command. |
| 867 | */ |
| 868 | p->mode = MODE_Column; |
| 869 | p->showHeader = 1; |
| 870 | memset(p->colWidth,0,ArraySize(p->colWidth)); |
| 871 | p->colWidth[0] = 4; |
| 872 | p->colWidth[1] = 12; |
| 873 | p->colWidth[2] = 10; |
| 874 | p->colWidth[3] = 10; |
| 875 | p->colWidth[4] = 35; |
| 876 | }else if (p->explainPrev.valid) { |
| 877 | p->explainPrev.valid = 0; |
| 878 | p->mode = p->explainPrev.mode; |
| 879 | p->showHeader = p->explainPrev.showHeader; |
| 880 | memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth)); |
| 881 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 882 | }else |
| 883 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 884 | if( c=='h' && (strncmp(azArg[0], "header", n)==0 |
| 885 | || |
| 886 | strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 887 | int j; |
| 888 | char *z = azArg[1]; |
| 889 | int val = atoi(azArg[1]); |
| 890 | for(j=0; z[j]; j++){ |
| 891 | if( isupper(z[j]) ) z[j] = tolower(z[j]); |
| 892 | } |
| 893 | if( strcmp(z,"on")==0 ){ |
| 894 | val = 1; |
| 895 | }else if( strcmp(z,"yes")==0 ){ |
| 896 | val = 1; |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 897 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 898 | p->showHeader = val; |
| 899 | }else |
| 900 | |
| 901 | if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
| 902 | fprintf(stderr,zHelp); |
| 903 | }else |
| 904 | |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 905 | if( c=='i' && strncmp(azArg[0], "import", n)==0 && nArg>=3 ){ |
| 906 | char *zTable = azArg[2]; /* Insert data into this table */ |
| 907 | char *zFile = azArg[1]; /* The file from which to extract data */ |
| 908 | sqlite3_stmt *pStmt; /* A statement */ |
| 909 | int rc; /* Result code */ |
| 910 | int nCol; /* Number of columns in the table */ |
| 911 | int nByte; /* Number of bytes in an SQL string */ |
| 912 | int i, j; /* Loop counters */ |
| 913 | int nSep; /* Number of bytes in p->separator[] */ |
| 914 | char *zSql; /* An SQL statement */ |
| 915 | char *zLine; /* A single line of input from the file */ |
| 916 | char **azCol; /* zLine[] broken up into columns */ |
| 917 | char *zCommit; /* How to commit changes */ |
| 918 | FILE *in; /* The input file */ |
| 919 | |
| 920 | nSep = strlen(p->separator); |
| 921 | if( nSep==0 ){ |
| 922 | fprintf(stderr, "non-null separator required for import\n"); |
| 923 | return 0; |
| 924 | } |
| 925 | zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable); |
| 926 | if( zSql==0 ) return 0; |
| 927 | nByte = strlen(zSql); |
| 928 | rc = sqlite3_prepare(p->db, zSql, 0, &pStmt, 0); |
| 929 | sqlite3_free(zSql); |
| 930 | if( rc ){ |
| 931 | fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db)); |
| 932 | nCol = 0; |
| 933 | }else{ |
| 934 | nCol = sqlite3_column_count(pStmt); |
| 935 | } |
| 936 | sqlite3_finalize(pStmt); |
| 937 | if( nCol==0 ) return 0; |
| 938 | zSql = malloc( nByte + 20 + nCol*2 ); |
| 939 | if( zSql==0 ) return 0; |
| 940 | sqlite3_snprintf(nByte+20, zSql, "INSERT INTO '%q' VALUES(?", zTable); |
| 941 | j = strlen(zSql); |
| 942 | for(i=1; i<nCol; i++){ |
| 943 | zSql[j++] = ','; |
| 944 | zSql[j++] = '?'; |
| 945 | } |
| 946 | zSql[j++] = ')'; |
| 947 | zSql[j] = 0; |
| 948 | rc = sqlite3_prepare(p->db, zSql, 0, &pStmt, 0); |
| 949 | free(zSql); |
| 950 | if( rc ){ |
| 951 | fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db)); |
| 952 | sqlite3_finalize(pStmt); |
| 953 | return 0; |
| 954 | } |
| 955 | in = fopen(zFile, "rb"); |
| 956 | if( in==0 ){ |
| 957 | fprintf(stderr, "cannot open file: %s\n", zFile); |
| 958 | sqlite3_finalize(pStmt); |
| 959 | return 0; |
| 960 | } |
| 961 | azCol = malloc( sizeof(azCol[0])*(nCol+1) ); |
| 962 | if( azCol==0 ) return 0; |
| 963 | sqlite3_exec(p->db, "BEGIN", 0, 0, 0); |
| 964 | zCommit = "COMMIT"; |
| 965 | while( (zLine = local_getline(0, in))!=0 ){ |
| 966 | char *z; |
| 967 | i = 0; |
| 968 | azCol[0] = zLine; |
| 969 | for(i=0, z=zLine; *z; z++){ |
| 970 | if( *z==p->separator[0] && strncmp(z, p->separator, nSep)==0 ){ |
| 971 | *z = 0; |
| 972 | i++; |
| 973 | if( i>=nCol ) break; |
| 974 | azCol[i] = &z[nSep]; |
| 975 | z += nSep-1; |
| 976 | } |
| 977 | } |
| 978 | while( i<nCol ) azCol[i++] = 0; |
| 979 | for(i=0; i<nCol; i++){ |
| 980 | sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC); |
| 981 | } |
| 982 | sqlite3_step(pStmt); |
| 983 | rc = sqlite3_reset(pStmt); |
| 984 | free(zLine); |
| 985 | if( rc!=SQLITE_OK ){ |
| 986 | fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db)); |
| 987 | zCommit = "ROLLBACK"; |
| 988 | break; |
| 989 | } |
| 990 | } |
| 991 | free(azCol); |
| 992 | fclose(in); |
| 993 | sqlite3_finalize(pStmt); |
| 994 | sqlite3_exec(p->db, "COMMIT", 0, 0, 0); |
| 995 | }else |
| 996 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 997 | if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){ |
| 998 | struct callback_data data; |
| 999 | char *zErrMsg = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1000 | open_db(p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1001 | memcpy(&data, p, sizeof(data)); |
| 1002 | data.showHeader = 0; |
| 1003 | data.mode = MODE_List; |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1004 | zShellStatic = azArg[1]; |
| 1005 | sqlite3_exec(p->db, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1006 | "SELECT name FROM sqlite_master " |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1007 | "WHERE type='index' AND tbl_name LIKE shellstatic() " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1008 | "UNION ALL " |
| 1009 | "SELECT name FROM sqlite_temp_master " |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1010 | "WHERE type='index' AND tbl_name LIKE shellstatic() " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1011 | "ORDER BY 1", |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1012 | callback, &data, &zErrMsg |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1013 | ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1014 | zShellStatic = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1015 | if( zErrMsg ){ |
| 1016 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1017 | sqlite3_free(zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1018 | } |
| 1019 | }else |
| 1020 | |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1021 | if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1022 | int n2 = strlen(azArg[1]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1023 | if( strncmp(azArg[1],"line",n2)==0 |
| 1024 | || |
| 1025 | strncmp(azArg[1],"lines",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1026 | p->mode = MODE_Line; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1027 | }else if( strncmp(azArg[1],"column",n2)==0 |
| 1028 | || |
| 1029 | strncmp(azArg[1],"columns",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1030 | p->mode = MODE_Column; |
| 1031 | }else if( strncmp(azArg[1],"list",n2)==0 ){ |
| 1032 | p->mode = MODE_List; |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1033 | }else if( strncmp(azArg[1],"html",n2)==0 ){ |
| 1034 | p->mode = MODE_Html; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 1035 | }else if( strncmp(azArg[1],"tcl",n2)==0 ){ |
| 1036 | p->mode = MODE_Tcl; |
| 1037 | }else if( strncmp(azArg[1],"csv",n2)==0 ){ |
| 1038 | p->mode = MODE_List; |
| 1039 | strcpy(p->separator, ","); |
| 1040 | }else if( strncmp(azArg[1],"tabs",n2)==0 ){ |
| 1041 | p->mode = MODE_List; |
| 1042 | strcpy(p->separator, "\t"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1043 | }else if( strncmp(azArg[1],"insert",n2)==0 ){ |
| 1044 | p->mode = MODE_Insert; |
| 1045 | if( nArg>=3 ){ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1046 | set_table_name(p, azArg[2]); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1047 | }else{ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1048 | set_table_name(p, "table"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1049 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1050 | }else { |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 1051 | fprintf(stderr,"mode should be on of: " |
| 1052 | "column csv html insert line list tabs tcl\n"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1053 | } |
| 1054 | }else |
| 1055 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1056 | if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) { |
| 1057 | sprintf(p->nullvalue, "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]); |
| 1058 | }else |
| 1059 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1060 | if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){ |
| 1061 | if( p->out!=stdout ){ |
| 1062 | fclose(p->out); |
| 1063 | } |
| 1064 | if( strcmp(azArg[1],"stdout")==0 ){ |
| 1065 | p->out = stdout; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1066 | strcpy(p->outfile,"stdout"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1067 | }else{ |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 1068 | p->out = fopen(azArg[1], "wb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1069 | if( p->out==0 ){ |
| 1070 | fprintf(stderr,"can't write to \"%s\"\n", azArg[1]); |
| 1071 | p->out = stdout; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1072 | } else { |
| 1073 | strcpy(p->outfile,azArg[1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | }else |
| 1077 | |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 1078 | if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1079 | if( nArg >= 2) { |
| 1080 | strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
| 1081 | } |
| 1082 | if( nArg >= 3) { |
| 1083 | strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); |
| 1084 | } |
| 1085 | }else |
| 1086 | |
| 1087 | if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ |
drh | f73287c | 2004-02-25 02:25:37 +0000 | [diff] [blame] | 1088 | rc = 1; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1089 | }else |
| 1090 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1091 | if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){ |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 1092 | FILE *alt = fopen(azArg[1], "rb"); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1093 | if( alt==0 ){ |
| 1094 | fprintf(stderr,"can't open \"%s\"\n", azArg[1]); |
| 1095 | }else{ |
| 1096 | process_input(p, alt); |
| 1097 | fclose(alt); |
| 1098 | } |
| 1099 | }else |
| 1100 | |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 1101 | #ifdef SQLITE_HAS_CODEC |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1102 | if( c=='r' && strncmp(azArg[0],"rekey", n)==0 && nArg==4 ){ |
| 1103 | char *zOld = p->zKey; |
| 1104 | if( zOld==0 ) zOld = ""; |
| 1105 | if( strcmp(azArg[1],zOld) ){ |
| 1106 | fprintf(stderr,"old key is incorrect\n"); |
| 1107 | }else if( strcmp(azArg[2], azArg[3]) ){ |
| 1108 | fprintf(stderr,"2nd copy of new key does not match the 1st\n"); |
| 1109 | }else{ |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1110 | sqlite3_free(p->zKey); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1111 | p->zKey = sqlite3_mprintf("%s", azArg[2]); |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1112 | sqlite3_rekey(p->db, p->zKey, strlen(p->zKey)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1113 | } |
| 1114 | }else |
| 1115 | #endif |
| 1116 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1117 | if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
| 1118 | struct callback_data data; |
| 1119 | char *zErrMsg = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1120 | open_db(p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1121 | memcpy(&data, p, sizeof(data)); |
| 1122 | data.showHeader = 0; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1123 | data.mode = MODE_Semi; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1124 | if( nArg>1 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1125 | extern int sqlite3StrICmp(const char*,const char*); |
| 1126 | if( sqlite3StrICmp(azArg[1],"sqlite_master")==0 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1127 | char *new_argv[2], *new_colv[2]; |
| 1128 | new_argv[0] = "CREATE TABLE sqlite_master (\n" |
| 1129 | " type text,\n" |
| 1130 | " name text,\n" |
| 1131 | " tbl_name text,\n" |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1132 | " rootpage integer,\n" |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1133 | " sql text\n" |
| 1134 | ")"; |
| 1135 | new_argv[1] = 0; |
| 1136 | new_colv[0] = "sql"; |
| 1137 | new_colv[1] = 0; |
| 1138 | callback(&data, 1, new_argv, new_colv); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1139 | }else if( sqlite3StrICmp(azArg[1],"sqlite_temp_master")==0 ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1140 | char *new_argv[2], *new_colv[2]; |
| 1141 | new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n" |
| 1142 | " type text,\n" |
| 1143 | " name text,\n" |
| 1144 | " tbl_name text,\n" |
| 1145 | " rootpage integer,\n" |
| 1146 | " sql text\n" |
| 1147 | ")"; |
| 1148 | new_argv[1] = 0; |
| 1149 | new_colv[0] = "sql"; |
| 1150 | new_colv[1] = 0; |
| 1151 | callback(&data, 1, new_argv, new_colv); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1152 | }else{ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1153 | zShellStatic = azArg[1]; |
| 1154 | sqlite3_exec(p->db, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1155 | "SELECT sql FROM " |
| 1156 | " (SELECT * FROM sqlite_master UNION ALL" |
| 1157 | " SELECT * FROM sqlite_temp_master) " |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1158 | "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOTNULL " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1159 | "ORDER BY substr(type,2,1), name", |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1160 | callback, &data, &zErrMsg); |
| 1161 | zShellStatic = 0; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1162 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1163 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1164 | sqlite3_exec(p->db, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1165 | "SELECT sql FROM " |
| 1166 | " (SELECT * FROM sqlite_master UNION ALL" |
| 1167 | " SELECT * FROM sqlite_temp_master) " |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1168 | "WHERE type!='meta' AND sql NOTNULL " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1169 | "ORDER BY substr(type,2,1), name", |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1170 | callback, &data, &zErrMsg |
| 1171 | ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1172 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1173 | if( zErrMsg ){ |
| 1174 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1175 | sqlite3_free(zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1176 | } |
| 1177 | }else |
| 1178 | |
| 1179 | if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){ |
| 1180 | sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]); |
| 1181 | }else |
| 1182 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1183 | if( c=='s' && strncmp(azArg[0], "show", n)==0){ |
| 1184 | int i; |
| 1185 | fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off"); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1186 | fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off"); |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 1187 | fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1188 | fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 1189 | fprintf(p->out,"%9.9s: ", "nullvalue"); |
| 1190 | output_c_string(p->out, p->nullvalue); |
| 1191 | fprintf(p->out, "\n"); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1192 | fprintf(p->out,"%9.9s: %s\n","output", |
| 1193 | strlen(p->outfile) ? p->outfile : "stdout"); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 1194 | fprintf(p->out,"%9.9s: ", "separator"); |
| 1195 | output_c_string(p->out, p->separator); |
| 1196 | fprintf(p->out, "\n"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1197 | fprintf(p->out,"%9.9s: ","width"); |
| 1198 | for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 1199 | fprintf(p->out,"%d ",p->colWidth[i]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1200 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame^] | 1201 | fprintf(p->out,"\n"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1202 | }else |
| 1203 | |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1204 | if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1205 | char **azResult; |
| 1206 | int nRow, rc; |
| 1207 | char *zErrMsg; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1208 | open_db(p); |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 1209 | if( nArg==1 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1210 | rc = sqlite3_get_table(p->db, |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 1211 | "SELECT name FROM sqlite_master " |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1212 | "WHERE type IN ('table','view') " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1213 | "UNION ALL " |
| 1214 | "SELECT name FROM sqlite_temp_master " |
| 1215 | "WHERE type IN ('table','view') " |
| 1216 | "ORDER BY 1", |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1217 | &azResult, &nRow, 0, &zErrMsg |
| 1218 | ); |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 1219 | }else{ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1220 | zShellStatic = azArg[1]; |
| 1221 | rc = sqlite3_get_table(p->db, |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 1222 | "SELECT name FROM sqlite_master " |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1223 | "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1224 | "UNION ALL " |
| 1225 | "SELECT name FROM sqlite_temp_master " |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1226 | "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1227 | "ORDER BY 1", |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1228 | &azResult, &nRow, 0, &zErrMsg |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1229 | ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1230 | zShellStatic = 0; |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 1231 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1232 | if( zErrMsg ){ |
| 1233 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1234 | sqlite3_free(zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1235 | } |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1236 | if( rc==SQLITE_OK ){ |
| 1237 | int len, maxlen = 0; |
| 1238 | int i, j; |
| 1239 | int nPrintCol, nPrintRow; |
| 1240 | for(i=1; i<=nRow; i++){ |
| 1241 | if( azResult[i]==0 ) continue; |
| 1242 | len = strlen(azResult[i]); |
| 1243 | if( len>maxlen ) maxlen = len; |
| 1244 | } |
| 1245 | nPrintCol = 80/(maxlen+2); |
| 1246 | if( nPrintCol<1 ) nPrintCol = 1; |
| 1247 | nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
| 1248 | for(i=0; i<nPrintRow; i++){ |
| 1249 | for(j=i+1; j<=nRow; j+=nPrintRow){ |
| 1250 | char *zSp = j<=nPrintRow ? "" : " "; |
| 1251 | printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : ""); |
| 1252 | } |
| 1253 | printf("\n"); |
| 1254 | } |
| 1255 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1256 | sqlite3_free_table(azResult); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1257 | }else |
| 1258 | |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1259 | if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1260 | open_db(p); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1261 | sqlite3_busy_timeout(p->db, atoi(azArg[1])); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1262 | }else |
| 1263 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1264 | if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
| 1265 | int j; |
| 1266 | for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ |
| 1267 | p->colWidth[j-1] = atoi(azArg[j]); |
| 1268 | } |
| 1269 | }else |
| 1270 | |
| 1271 | { |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1272 | fprintf(stderr, "unknown command or invalid arguments: " |
| 1273 | " \"%s\". Enter \".help\" for help\n", azArg[0]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1274 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1275 | |
| 1276 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1279 | /* |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 1280 | ** Return TRUE if the last non-whitespace character in z[] is a semicolon. |
| 1281 | ** z[] is N characters long. |
| 1282 | */ |
| 1283 | static int _ends_with_semicolon(const char *z, int N){ |
| 1284 | while( N>0 && isspace(z[N-1]) ){ N--; } |
| 1285 | return N>0 && z[N-1]==';'; |
| 1286 | } |
| 1287 | |
| 1288 | /* |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 1289 | ** Test to see if a line consists entirely of whitespace. |
| 1290 | */ |
| 1291 | static int _all_whitespace(const char *z){ |
| 1292 | for(; *z; z++){ |
| 1293 | if( isspace(*z) ) continue; |
| 1294 | if( *z=='/' && z[1]=='*' ){ |
| 1295 | z += 2; |
| 1296 | while( *z && (*z!='*' || z[1]!='/') ){ z++; } |
| 1297 | if( *z==0 ) return 0; |
| 1298 | z++; |
| 1299 | continue; |
| 1300 | } |
| 1301 | if( *z=='-' && z[1]=='-' ){ |
| 1302 | z += 2; |
| 1303 | while( *z && *z!='\n' ){ z++; } |
| 1304 | if( *z==0 ) return 1; |
| 1305 | continue; |
| 1306 | } |
| 1307 | return 0; |
| 1308 | } |
| 1309 | return 1; |
| 1310 | } |
| 1311 | |
| 1312 | /* |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 1313 | ** Return TRUE if the line typed in is an SQL command terminator other |
| 1314 | ** than a semi-colon. The SQL Server style "go" command is understood |
| 1315 | ** as is the Oracle "/". |
| 1316 | */ |
| 1317 | static int _is_command_terminator(const char *zLine){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1318 | extern int sqlite3StrNICmp(const char*,const char*,int); |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 1319 | while( isspace(*zLine) ){ zLine++; }; |
| 1320 | if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ) return 1; /* Oracle */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1321 | if( sqlite3StrNICmp(zLine,"go",2)==0 && _all_whitespace(&zLine[2]) ){ |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 1322 | return 1; /* SQL Server */ |
| 1323 | } |
| 1324 | return 0; |
| 1325 | } |
| 1326 | |
| 1327 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1328 | ** Read input from *in and process it. If *in==0 then input |
| 1329 | ** is interactive - the user is typing it it. Otherwise, input |
| 1330 | ** is coming from a file or device. A prompt is issued and history |
| 1331 | ** is saved only if input is interactive. An interrupt signal will |
| 1332 | ** cause this routine to exit immediately, unless input is interactive. |
| 1333 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1334 | static void process_input(struct callback_data *p, FILE *in){ |
| 1335 | char *zLine; |
| 1336 | char *zSql = 0; |
| 1337 | int nSql = 0; |
| 1338 | char *zErrMsg; |
drh | 7f953e2 | 2002-07-13 17:33:45 +0000 | [diff] [blame] | 1339 | int rc; |
drh | 41e941d | 2002-04-04 15:10:12 +0000 | [diff] [blame] | 1340 | while( fflush(p->out), (zLine = one_input_line(zSql, in))!=0 ){ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1341 | if( seenInterrupt ){ |
| 1342 | if( in!=0 ) break; |
| 1343 | seenInterrupt = 0; |
| 1344 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1345 | if( p->echoOn ) printf("%s\n", zLine); |
drh | f817b6b | 2003-06-16 00:16:41 +0000 | [diff] [blame] | 1346 | if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue; |
drh | 2af0b2d | 2002-02-21 02:25:02 +0000 | [diff] [blame] | 1347 | if( zLine && zLine[0]=='.' && nSql==0 ){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1348 | int rc = do_meta_command(zLine, p); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1349 | free(zLine); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1350 | if( rc ) break; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1351 | continue; |
| 1352 | } |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 1353 | if( _is_command_terminator(zLine) ){ |
| 1354 | strcpy(zLine,";"); |
| 1355 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1356 | if( zSql==0 ){ |
| 1357 | int i; |
| 1358 | for(i=0; zLine[i] && isspace(zLine[i]); i++){} |
| 1359 | if( zLine[i]!=0 ){ |
| 1360 | nSql = strlen(zLine); |
| 1361 | zSql = malloc( nSql+1 ); |
| 1362 | strcpy(zSql, zLine); |
| 1363 | } |
| 1364 | }else{ |
| 1365 | int len = strlen(zLine); |
| 1366 | zSql = realloc( zSql, nSql + len + 2 ); |
| 1367 | if( zSql==0 ){ |
| 1368 | fprintf(stderr,"%s: out of memory!\n", Argv0); |
| 1369 | exit(1); |
| 1370 | } |
| 1371 | strcpy(&zSql[nSql++], "\n"); |
| 1372 | strcpy(&zSql[nSql], zLine); |
| 1373 | nSql += len; |
| 1374 | } |
| 1375 | free(zLine); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1376 | if( zSql && _ends_with_semicolon(zSql, nSql) && sqlite3_complete(zSql) ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1377 | p->cnt = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1378 | open_db(p); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1379 | rc = sqlite3_exec(p->db, zSql, callback, p, &zErrMsg); |
drh | 7f953e2 | 2002-07-13 17:33:45 +0000 | [diff] [blame] | 1380 | if( rc || zErrMsg ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1381 | if( in!=0 && !p->echoOn ) printf("%s\n",zSql); |
drh | 7f953e2 | 2002-07-13 17:33:45 +0000 | [diff] [blame] | 1382 | if( zErrMsg!=0 ){ |
| 1383 | printf("SQL error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1384 | sqlite3_free(zErrMsg); |
drh | 7f953e2 | 2002-07-13 17:33:45 +0000 | [diff] [blame] | 1385 | zErrMsg = 0; |
| 1386 | }else{ |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1387 | printf("SQL error: %s\n", sqlite3_errmsg(p->db)); |
drh | 7f953e2 | 2002-07-13 17:33:45 +0000 | [diff] [blame] | 1388 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1389 | } |
| 1390 | free(zSql); |
| 1391 | zSql = 0; |
| 1392 | nSql = 0; |
| 1393 | } |
| 1394 | } |
| 1395 | if( zSql ){ |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 1396 | if( !_all_whitespace(zSql) ) printf("Incomplete SQL: %s\n", zSql); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1397 | free(zSql); |
| 1398 | } |
| 1399 | } |
| 1400 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1401 | /* |
| 1402 | ** Return a pathname which is the user's home directory. A |
| 1403 | ** 0 return indicates an error of some kind. Space to hold the |
| 1404 | ** resulting string is obtained from malloc(). The calling |
| 1405 | ** function should free the result. |
| 1406 | */ |
| 1407 | static char *find_home_dir(void){ |
| 1408 | char *home_dir = NULL; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1409 | |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 1410 | #if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__) |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1411 | struct passwd *pwent; |
| 1412 | uid_t uid = getuid(); |
drh | bd842ba | 2002-08-21 11:26:41 +0000 | [diff] [blame] | 1413 | if( (pwent=getpwuid(uid)) != NULL) { |
| 1414 | home_dir = pwent->pw_dir; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1415 | } |
| 1416 | #endif |
| 1417 | |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 1418 | #ifdef __MACOS__ |
| 1419 | char home_path[_MAX_PATH+1]; |
| 1420 | home_dir = getcwd(home_path, _MAX_PATH); |
| 1421 | #endif |
| 1422 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1423 | if (!home_dir) { |
| 1424 | home_dir = getenv("HOME"); |
| 1425 | if (!home_dir) { |
| 1426 | home_dir = getenv("HOMEPATH"); /* Windows? */ |
| 1427 | } |
| 1428 | } |
| 1429 | |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 1430 | #if defined(_WIN32) || defined(WIN32) |
| 1431 | if (!home_dir) { |
| 1432 | home_dir = "c:"; |
| 1433 | } |
| 1434 | #endif |
| 1435 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1436 | if( home_dir ){ |
| 1437 | char *z = malloc( strlen(home_dir)+1 ); |
| 1438 | if( z ) strcpy(z, home_dir); |
| 1439 | home_dir = z; |
| 1440 | } |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 1441 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1442 | return home_dir; |
| 1443 | } |
| 1444 | |
| 1445 | /* |
| 1446 | ** Read input from the file given by sqliterc_override. Or if that |
| 1447 | ** parameter is NULL, take input from ~/.sqliterc |
| 1448 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1449 | static void process_sqliterc( |
| 1450 | struct callback_data *p, /* Configuration data */ |
| 1451 | const char *sqliterc_override /* Name of config file. NULL to use default */ |
| 1452 | ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1453 | char *home_dir = NULL; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1454 | const char *sqliterc = sqliterc_override; |
| 1455 | char *zBuf; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1456 | FILE *in = NULL; |
| 1457 | |
| 1458 | if (sqliterc == NULL) { |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1459 | home_dir = find_home_dir(); |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 1460 | if( home_dir==0 ){ |
| 1461 | fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0); |
| 1462 | return; |
| 1463 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1464 | zBuf = malloc(strlen(home_dir) + 15); |
| 1465 | if( zBuf==0 ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1466 | fprintf(stderr,"%s: out of memory!\n", Argv0); |
| 1467 | exit(1); |
| 1468 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1469 | sprintf(zBuf,"%s/.sqliterc",home_dir); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1470 | free(home_dir); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1471 | sqliterc = (const char*)zBuf; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1472 | } |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 1473 | in = fopen(sqliterc,"rb"); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1474 | if( in ){ |
| 1475 | if( isatty(fileno(stdout)) ){ |
| 1476 | printf("Loading resources from %s\n",sqliterc); |
| 1477 | } |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1478 | process_input(p,in); |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 1479 | fclose(in); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1480 | } |
| 1481 | return; |
| 1482 | } |
| 1483 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1484 | /* |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 1485 | ** Show available command line options |
| 1486 | */ |
| 1487 | static const char zOptions[] = |
| 1488 | " -init filename read/process named file\n" |
| 1489 | " -echo print commands before execution\n" |
| 1490 | " -[no]header turn headers on or off\n" |
| 1491 | " -column set output mode to 'column'\n" |
| 1492 | " -html set output mode to HTML\n" |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 1493 | #ifdef SQLITE_HAS_CODEC |
drh | 57ced91 | 2004-02-10 02:57:59 +0000 | [diff] [blame] | 1494 | " -key KEY encryption key\n" |
| 1495 | #endif |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 1496 | " -line set output mode to 'line'\n" |
| 1497 | " -list set output mode to 'list'\n" |
| 1498 | " -separator 'x' set output field separator (|)\n" |
| 1499 | " -nullvalue 'text' set text string for NULL values\n" |
| 1500 | " -version show SQLite version\n" |
| 1501 | " -help show this text, also show dot-commands\n" |
| 1502 | ; |
| 1503 | static void usage(int showDetail){ |
| 1504 | fprintf(stderr, "Usage: %s [OPTIONS] FILENAME [SQL]\n", Argv0); |
| 1505 | if( showDetail ){ |
| 1506 | fprintf(stderr, "Options are:\n%s", zOptions); |
| 1507 | }else{ |
| 1508 | fprintf(stderr, "Use the -help option for additional information\n"); |
| 1509 | } |
| 1510 | exit(1); |
| 1511 | } |
| 1512 | |
| 1513 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1514 | ** Initialize the state information in data |
| 1515 | */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1516 | void main_init(struct callback_data *data) { |
| 1517 | memset(data, 0, sizeof(*data)); |
| 1518 | data->mode = MODE_List; |
| 1519 | strcpy(data->separator,"|"); |
| 1520 | data->showHeader = 0; |
| 1521 | strcpy(mainPrompt,"sqlite> "); |
| 1522 | strcpy(continuePrompt," ...> "); |
| 1523 | } |
| 1524 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1525 | int main(int argc, char **argv){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1526 | char *zErrMsg = 0; |
| 1527 | struct callback_data data; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1528 | const char *zInitFile = 0; |
| 1529 | char *zFirstCmd = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1530 | int i; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1531 | extern int sqlite3OsFileExists(const char*); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1532 | |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 1533 | #ifdef __MACOS__ |
| 1534 | argc = ccommand(&argv); |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 1535 | #endif |
| 1536 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1537 | Argv0 = argv[0]; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1538 | main_init(&data); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1539 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1540 | /* Make sure we have a valid signal handler early, before anything |
| 1541 | ** else is done. |
| 1542 | */ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1543 | #ifdef SIGINT |
| 1544 | signal(SIGINT, interrupt_handler); |
| 1545 | #endif |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1546 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1547 | /* Do an initial pass through the command-line argument to locate |
| 1548 | ** the name of the database file, the name of the initialization file, |
| 1549 | ** and the first command to execute. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1550 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1551 | for(i=1; i<argc-1; i++){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1552 | if( argv[i][0]!='-' ) break; |
| 1553 | if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){ |
| 1554 | i++; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1555 | }else if( strcmp(argv[i],"-init")==0 ){ |
| 1556 | i++; |
| 1557 | zInitFile = argv[i]; |
| 1558 | }else if( strcmp(argv[i],"-key")==0 ){ |
| 1559 | i++; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1560 | data.zKey = sqlite3_mprintf("%s",argv[i]); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1561 | } |
| 1562 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1563 | if( i<argc ){ |
| 1564 | data.zDbFilename = argv[i++]; |
| 1565 | }else{ |
| 1566 | data.zDbFilename = ":memory:"; |
| 1567 | } |
| 1568 | if( i<argc ){ |
| 1569 | zFirstCmd = argv[i++]; |
| 1570 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1571 | data.out = stdout; |
| 1572 | |
| 1573 | /* Go ahead and open the database file if it already exists. If the |
| 1574 | ** file does not exist, delay opening it. This prevents empty database |
| 1575 | ** files from being created if a user mistypes the database name argument |
| 1576 | ** to the sqlite command-line tool. |
| 1577 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1578 | if( sqlite3OsFileExists(data.zDbFilename) ){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1579 | open_db(&data); |
| 1580 | } |
| 1581 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1582 | /* Process the initialization file if there is one. If no -init option |
| 1583 | ** is given on the command line, look for a file named ~/.sqliterc and |
| 1584 | ** try to process it. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1585 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1586 | process_sqliterc(&data,zInitFile); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1587 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1588 | /* Make a second pass through the command-line argument and set |
| 1589 | ** options. This second pass is delayed until after the initialization |
| 1590 | ** file is processed so that the command-line arguments will override |
| 1591 | ** settings in the initialization file. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1592 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1593 | for(i=1; i<argc && argv[i][0]=='-'; i++){ |
| 1594 | char *z = argv[i]; |
| 1595 | if( strcmp(z,"-init")==0 || strcmp(z,"-key")==0 ){ |
| 1596 | i++; |
| 1597 | }else if( strcmp(z,"-html")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1598 | data.mode = MODE_Html; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1599 | }else if( strcmp(z,"-list")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1600 | data.mode = MODE_List; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1601 | }else if( strcmp(z,"-line")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1602 | data.mode = MODE_Line; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1603 | }else if( strcmp(z,"-column")==0 ){ |
drh | 8b32e17 | 2002-04-08 02:42:57 +0000 | [diff] [blame] | 1604 | data.mode = MODE_Column; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1605 | }else if( strcmp(z,"-separator")==0 ){ |
| 1606 | i++; |
| 1607 | sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[i]); |
| 1608 | }else if( strcmp(z,"-nullvalue")==0 ){ |
| 1609 | i++; |
| 1610 | sprintf(data.nullvalue,"%.*s",(int)sizeof(data.nullvalue)-1,argv[i]); |
| 1611 | }else if( strcmp(z,"-header")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1612 | data.showHeader = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1613 | }else if( strcmp(z,"-noheader")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1614 | data.showHeader = 0; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1615 | }else if( strcmp(z,"-echo")==0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1616 | data.echoOn = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1617 | }else if( strcmp(z,"-version")==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1618 | printf("%s\n", sqlite3_version); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 1619 | return 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1620 | }else if( strcmp(z,"-help")==0 ){ |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 1621 | usage(1); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1622 | }else{ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1623 | fprintf(stderr,"%s: unknown option: %s\n", Argv0, z); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 1624 | fprintf(stderr,"Use -help for a list of options.\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1625 | return 1; |
| 1626 | } |
| 1627 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1628 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1629 | if( zFirstCmd ){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1630 | /* Run just the command that follows the database name |
| 1631 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1632 | if( zFirstCmd[0]=='.' ){ |
| 1633 | do_meta_command(zFirstCmd, &data); |
drh | 6ff1385 | 2001-11-25 13:18:23 +0000 | [diff] [blame] | 1634 | exit(0); |
| 1635 | }else{ |
| 1636 | int rc; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1637 | open_db(&data); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1638 | rc = sqlite3_exec(data.db, zFirstCmd, callback, &data, &zErrMsg); |
drh | 6ff1385 | 2001-11-25 13:18:23 +0000 | [diff] [blame] | 1639 | if( rc!=0 && zErrMsg!=0 ){ |
| 1640 | fprintf(stderr,"SQL error: %s\n", zErrMsg); |
| 1641 | exit(1); |
| 1642 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1643 | } |
| 1644 | }else{ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1645 | /* Run commands received from standard input |
| 1646 | */ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 1647 | if( isatty(fileno(stdout)) && isatty(fileno(stdin)) ){ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1648 | char *zHome; |
| 1649 | char *zHistory = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1650 | printf( |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 1651 | "SQLite version %s\n" |
| 1652 | "Enter \".help\" for instructions\n", |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1653 | sqlite3_version |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1654 | ); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1655 | zHome = find_home_dir(); |
| 1656 | if( zHome && (zHistory = malloc(strlen(zHome)+20))!=0 ){ |
| 1657 | sprintf(zHistory,"%s/.sqlite_history", zHome); |
| 1658 | } |
| 1659 | if( zHistory ) read_history(zHistory); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1660 | process_input(&data, 0); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1661 | if( zHistory ){ |
| 1662 | stifle_history(100); |
| 1663 | write_history(zHistory); |
| 1664 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1665 | }else{ |
| 1666 | process_input(&data, stdin); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1667 | } |
| 1668 | } |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1669 | set_table_name(&data, 0); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1670 | if( db ) sqlite3_close(db); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1671 | return 0; |
| 1672 | } |