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