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