version 1.1.1.4, 2012/11/29 05:14:10 |
version 1.1.1.5, 2013/03/18 10:49:34 |
|
|
if( c=='\\' ){ |
if( c=='\\' ){ |
fputc(c, out); |
fputc(c, out); |
fputc(c, out); |
fputc(c, out); |
|
}else if( c=='"' ){ |
|
fputc('\\', out); |
|
fputc('"', out); |
}else if( c=='\t' ){ |
}else if( c=='\t' ){ |
fputc('\\', out); |
fputc('\\', out); |
fputc('t', out); |
fputc('t', out); |
|
|
}else{ |
}else{ |
w = 0; |
w = 0; |
} |
} |
if( w<=0 ){ |
if( w==0 ){ |
w = strlen30(azCol[i] ? azCol[i] : ""); |
w = strlen30(azCol[i] ? azCol[i] : ""); |
if( w<10 ) w = 10; |
if( w<10 ) w = 10; |
n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullvalue); |
n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullvalue); |
|
|
p->actualWidth[i] = w; |
p->actualWidth[i] = w; |
} |
} |
if( p->showHeader ){ |
if( p->showHeader ){ |
fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " "); |
if( w<0 ){ |
|
fprintf(p->out,"%*.*s%s",-w,-w,azCol[i], i==nArg-1 ? "\n": " "); |
|
}else{ |
|
fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " "); |
|
} |
} |
} |
} |
} |
if( p->showHeader ){ |
if( p->showHeader ){ |
|
|
int w; |
int w; |
if( i<ArraySize(p->actualWidth) ){ |
if( i<ArraySize(p->actualWidth) ){ |
w = p->actualWidth[i]; |
w = p->actualWidth[i]; |
|
if( w<0 ) w = -w; |
}else{ |
}else{ |
w = 10; |
w = 10; |
} |
} |
|
|
strlen30(azArg[i])>w ){ |
strlen30(azArg[i])>w ){ |
w = strlen30(azArg[i]); |
w = strlen30(azArg[i]); |
} |
} |
fprintf(p->out,"%-*.*s%s",w,w, |
if( w<0 ){ |
azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " "); |
fprintf(p->out,"%*.*s%s",-w,-w, |
|
azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " "); |
|
}else{ |
|
fprintf(p->out,"%-*.*s%s",w,w, |
|
azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " "); |
|
} |
} |
} |
break; |
break; |
} |
} |
|
|
if( p->cnt++==0 && p->showHeader ){ |
if( p->cnt++==0 && p->showHeader ){ |
for(i=0; i<nArg; i++){ |
for(i=0; i<nArg; i++){ |
output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
fprintf(p->out, "%s", p->separator); |
if(i<nArg-1) fprintf(p->out, "%s", p->separator); |
} |
} |
fprintf(p->out,"\n"); |
fprintf(p->out,"\n"); |
} |
} |
if( azArg==0 ) break; |
if( azArg==0 ) break; |
for(i=0; i<nArg; i++){ |
for(i=0; i<nArg; i++){ |
output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue); |
output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue); |
fprintf(p->out, "%s", p->separator); |
if(i<nArg-1) fprintf(p->out, "%s", p->separator); |
} |
} |
fprintf(p->out,"\n"); |
fprintf(p->out,"\n"); |
break; |
break; |
|
|
" list Values delimited by .separator string\n" |
" list Values delimited by .separator string\n" |
" tabs Tab-separated values\n" |
" tabs Tab-separated values\n" |
" tcl TCL list elements\n" |
" tcl TCL list elements\n" |
".nullvalue STRING Print STRING in place of NULL values\n" |
".nullvalue STRING Use STRING in place of NULL values\n" |
".output FILENAME Send output to FILENAME\n" |
".output FILENAME Send output to FILENAME\n" |
".output stdout Send output to the screen\n" |
".output stdout Send output to the screen\n" |
|
".print STRING... Print literal STRING\n" |
".prompt MAIN CONTINUE Replace the standard prompts\n" |
".prompt MAIN CONTINUE Replace the standard prompts\n" |
".quit Exit this program\n" |
".quit Exit this program\n" |
".read FILENAME Execute SQL in FILENAME\n" |
".read FILENAME Execute SQL in FILENAME\n" |
|
|
p->mode = MODE_Html; |
p->mode = MODE_Html; |
}else if( n2==3 && strncmp(azArg[1],"tcl",n2)==0 ){ |
}else if( n2==3 && strncmp(azArg[1],"tcl",n2)==0 ){ |
p->mode = MODE_Tcl; |
p->mode = MODE_Tcl; |
|
sqlite3_snprintf(sizeof(p->separator), p->separator, " "); |
}else if( n2==3 && strncmp(azArg[1],"csv",n2)==0 ){ |
}else if( n2==3 && strncmp(azArg[1],"csv",n2)==0 ){ |
p->mode = MODE_Csv; |
p->mode = MODE_Csv; |
sqlite3_snprintf(sizeof(p->separator), p->separator, ","); |
sqlite3_snprintf(sizeof(p->separator), p->separator, ","); |
|
|
} |
} |
}else |
}else |
|
|
|
if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ |
|
int i; |
|
for(i=1; i<nArg; i++){ |
|
if( i>1 ) fprintf(p->out, " "); |
|
fprintf(p->out, "%s", azArg[i]); |
|
} |
|
fprintf(p->out, "\n"); |
|
}else |
|
|
if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){ |
if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){ |
if( nArg >= 2) { |
if( nArg >= 2) { |
strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
|
|
} |
} |
}else |
}else |
|
|
|
#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
|
if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ |
|
extern int sqlite3WhereTrace; |
|
sqlite3WhereTrace = atoi(azArg[1]); |
|
}else |
|
#endif |
|
|
if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){ |
if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){ |
int j; |
int j; |
assert( nArg<=ArraySize(azArg) ); |
assert( nArg<=ArraySize(azArg) ); |
|
|
free(zSql); |
free(zSql); |
} |
} |
free(zLine); |
free(zLine); |
return errCnt; |
return errCnt>0; |
} |
} |
|
|
/* |
/* |
|
|
" -bail stop after hitting an error\n" |
" -bail stop after hitting an error\n" |
" -batch force batch I/O\n" |
" -batch force batch I/O\n" |
" -column set output mode to 'column'\n" |
" -column set output mode to 'column'\n" |
" -cmd command run \"command\" before reading stdin\n" |
" -cmd COMMAND run \"COMMAND\" before reading stdin\n" |
" -csv set output mode to 'csv'\n" |
" -csv set output mode to 'csv'\n" |
" -echo print commands before execution\n" |
" -echo print commands before execution\n" |
" -init filename read/process named file\n" |
" -init FILENAME read/process named file\n" |
" -[no]header turn headers on or off\n" |
" -[no]header turn headers on or off\n" |
|
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
|
" -heap SIZE Size of heap for memsys3 or memsys5\n" |
|
#endif |
" -help show this message\n" |
" -help show this message\n" |
" -html set output mode to HTML\n" |
" -html set output mode to HTML\n" |
" -interactive force interactive I/O\n" |
" -interactive force interactive I/O\n" |
|
|
#ifdef SQLITE_ENABLE_MULTIPLEX |
#ifdef SQLITE_ENABLE_MULTIPLEX |
" -multiplex enable the multiplexor VFS\n" |
" -multiplex enable the multiplexor VFS\n" |
#endif |
#endif |
" -nullvalue 'text' set text string for NULL values\n" |
" -nullvalue TEXT set text string for NULL values. Default ''\n" |
" -separator 'x' set output field separator (|)\n" |
" -separator SEP set output field separator. Default: '|'\n" |
" -stats print memory stats before each finalize\n" |
" -stats print memory stats before each finalize\n" |
" -version show SQLite version\n" |
" -version show SQLite version\n" |
" -vfs NAME use NAME as the default VFS\n" |
" -vfs NAME use NAME as the default VFS\n" |
|
|
sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); |
sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); |
} |
} |
|
|
|
/* |
|
** Get the argument to an --option. Throw an error and die if no argument |
|
** is available. |
|
*/ |
|
static char *cmdline_option_value(int argc, char **argv, int i){ |
|
if( i==argc ){ |
|
fprintf(stderr, "%s: Error: missing argument to %s\n", |
|
argv[0], argv[argc-1]); |
|
exit(1); |
|
} |
|
return argv[i]; |
|
} |
|
|
int main(int argc, char **argv){ |
int main(int argc, char **argv){ |
char *zErrMsg = 0; |
char *zErrMsg = 0; |
struct callback_data data; |
struct callback_data data; |
|
|
** the size of the alternative malloc heap, |
** the size of the alternative malloc heap, |
** and the first command to execute. |
** and the first command to execute. |
*/ |
*/ |
for(i=1; i<argc-1; i++){ |
for(i=1; i<argc; i++){ |
char *z; |
char *z; |
if( argv[i][0]!='-' ) break; |
|
z = argv[i]; |
z = argv[i]; |
|
if( z[0]!='-' ){ |
|
if( data.zDbFilename==0 ){ |
|
data.zDbFilename = z; |
|
continue; |
|
} |
|
if( zFirstCmd==0 ){ |
|
zFirstCmd = z; |
|
continue; |
|
} |
|
fprintf(stderr,"%s: Error: too many options: \"%s\"\n", Argv0, argv[i]); |
|
fprintf(stderr,"Use -help for a list of options.\n"); |
|
return 1; |
|
} |
if( z[1]=='-' ) z++; |
if( z[1]=='-' ) z++; |
if( strcmp(z,"-separator")==0 |
if( strcmp(z,"-separator")==0 |
|| strcmp(z,"-nullvalue")==0 |
|| strcmp(z,"-nullvalue")==0 |
|| strcmp(z,"-cmd")==0 |
|| strcmp(z,"-cmd")==0 |
){ |
){ |
i++; |
(void)cmdline_option_value(argc, argv, ++i); |
}else if( strcmp(z,"-init")==0 ){ |
}else if( strcmp(z,"-init")==0 ){ |
i++; |
zInitFile = cmdline_option_value(argc, argv, ++i); |
zInitFile = argv[i]; |
|
/* Need to check for batch mode here to so we can avoid printing |
|
** informational messages (like from process_sqliterc) before |
|
** we do the actual processing of arguments later in a second pass. |
|
*/ |
|
}else if( strcmp(z,"-batch")==0 ){ |
}else if( strcmp(z,"-batch")==0 ){ |
|
/* Need to check for batch mode here to so we can avoid printing |
|
** informational messages (like from process_sqliterc) before |
|
** we do the actual processing of arguments later in a second pass. |
|
*/ |
stdin_is_interactive = 0; |
stdin_is_interactive = 0; |
}else if( strcmp(z,"-heap")==0 ){ |
}else if( strcmp(z,"-heap")==0 ){ |
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
|
|
const char *zSize; |
const char *zSize; |
sqlite3_int64 szHeap; |
sqlite3_int64 szHeap; |
|
|
zSize = argv[++i]; |
zSize = cmdline_option_value(argc, argv, ++i); |
szHeap = atoi(zSize); |
szHeap = atoi(zSize); |
for(j=0; (c = zSize[j])!=0; j++){ |
for(j=0; (c = zSize[j])!=0; j++){ |
if( c=='M' ){ szHeap *= 1000000; break; } |
if( c=='M' ){ szHeap *= 1000000; break; } |
|
|
sqlite3_multiplex_initialize(0, 1); |
sqlite3_multiplex_initialize(0, 1); |
#endif |
#endif |
}else if( strcmp(z,"-vfs")==0 ){ |
}else if( strcmp(z,"-vfs")==0 ){ |
sqlite3_vfs *pVfs = sqlite3_vfs_find(argv[++i]); |
sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i)); |
if( pVfs ){ |
if( pVfs ){ |
sqlite3_vfs_register(pVfs, 1); |
sqlite3_vfs_register(pVfs, 1); |
}else{ |
}else{ |
|
|
} |
} |
} |
} |
} |
} |
if( i<argc ){ |
if( data.zDbFilename==0 ){ |
data.zDbFilename = argv[i++]; |
|
}else{ |
|
#ifndef SQLITE_OMIT_MEMORYDB |
#ifndef SQLITE_OMIT_MEMORYDB |
data.zDbFilename = ":memory:"; |
data.zDbFilename = ":memory:"; |
#else |
#else |
data.zDbFilename = 0; |
fprintf(stderr,"%s: Error: no database filename specified\n", Argv0); |
|
return 1; |
#endif |
#endif |
} |
} |
if( i<argc ){ |
|
zFirstCmd = argv[i++]; |
|
} |
|
if( i<argc ){ |
|
fprintf(stderr,"%s: Error: too many options: \"%s\"\n", Argv0, argv[i]); |
|
fprintf(stderr,"Use -help for a list of options.\n"); |
|
return 1; |
|
} |
|
data.out = stdout; |
data.out = stdout; |
|
|
#ifdef SQLITE_OMIT_MEMORYDB |
|
if( data.zDbFilename==0 ){ |
|
fprintf(stderr,"%s: Error: no database filename specified\n", Argv0); |
|
return 1; |
|
} |
|
#endif |
|
|
|
/* Go ahead and open the database file if it already exists. If the |
/* Go ahead and open the database file if it already exists. If the |
** file does not exist, delay opening it. This prevents empty database |
** file does not exist, delay opening it. This prevents empty database |
** files from being created if a user mistypes the database name argument |
** files from being created if a user mistypes the database name argument |
|
|
** file is processed so that the command-line arguments will override |
** file is processed so that the command-line arguments will override |
** settings in the initialization file. |
** settings in the initialization file. |
*/ |
*/ |
for(i=1; i<argc && argv[i][0]=='-'; i++){ |
for(i=1; i<argc; i++){ |
char *z = argv[i]; |
char *z = argv[i]; |
|
if( z[0]!='-' ) continue; |
if( z[1]=='-' ){ z++; } |
if( z[1]=='-' ){ z++; } |
if( strcmp(z,"-init")==0 ){ |
if( strcmp(z,"-init")==0 ){ |
i++; |
i++; |
|
|
data.mode = MODE_Csv; |
data.mode = MODE_Csv; |
memcpy(data.separator,",",2); |
memcpy(data.separator,",",2); |
}else if( strcmp(z,"-separator")==0 ){ |
}else if( strcmp(z,"-separator")==0 ){ |
i++; |
|
if(i>=argc){ |
|
fprintf(stderr,"%s: Error: missing argument for option: %s\n", |
|
Argv0, z); |
|
fprintf(stderr,"Use -help for a list of options.\n"); |
|
return 1; |
|
} |
|
sqlite3_snprintf(sizeof(data.separator), data.separator, |
sqlite3_snprintf(sizeof(data.separator), data.separator, |
"%.*s",(int)sizeof(data.separator)-1,argv[i]); |
"%s",cmdline_option_value(argc,argv,++i)); |
}else if( strcmp(z,"-nullvalue")==0 ){ |
}else if( strcmp(z,"-nullvalue")==0 ){ |
i++; |
|
if(i>=argc){ |
|
fprintf(stderr,"%s: Error: missing argument for option: %s\n", |
|
Argv0, z); |
|
fprintf(stderr,"Use -help for a list of options.\n"); |
|
return 1; |
|
} |
|
sqlite3_snprintf(sizeof(data.nullvalue), data.nullvalue, |
sqlite3_snprintf(sizeof(data.nullvalue), data.nullvalue, |
"%.*s",(int)sizeof(data.nullvalue)-1,argv[i]); |
"%s",cmdline_option_value(argc,argv,++i)); |
}else if( strcmp(z,"-header")==0 ){ |
}else if( strcmp(z,"-header")==0 ){ |
data.showHeader = 1; |
data.showHeader = 1; |
}else if( strcmp(z,"-noheader")==0 ){ |
}else if( strcmp(z,"-noheader")==0 ){ |
|
|
usage(1); |
usage(1); |
}else if( strcmp(z,"-cmd")==0 ){ |
}else if( strcmp(z,"-cmd")==0 ){ |
if( i==argc-1 ) break; |
if( i==argc-1 ) break; |
i++; |
z = cmdline_option_value(argc,argv,++i); |
z = argv[i]; |
|
if( z[0]=='.' ){ |
if( z[0]=='.' ){ |
rc = do_meta_command(z, &data); |
rc = do_meta_command(z, &data); |
if( rc && bail_on_error ) return rc; |
if( rc && bail_on_error ) return rc; |