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