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