Add the ".once -e" and ".once -x" commands to the CLI, and the ".excel" command
that is an alias for ".once -x". FossilOrigin-Name: a6ed5ab9bc9741267fd1207e63b4c49d074291085c7e3f25e9f62a82f916a656
This commit is contained in:
commit
df53b05e3c
15
manifest
15
manifest
@ -1,5 +1,5 @@
|
||||
C Fix\sa\sharmless\scompiler\swarning\sin\szipfile.c
|
||||
D 2018-01-10T19:50:40.811
|
||||
C Add\sthe\s".once\s-e"\sand\s".once\s-x"\scommands\sto\sthe\sCLI,\sand\sthe\s".excel"\scommand\nthat\sis\san\salias\sfor\s".once\s-x".
|
||||
D 2018-01-10T23:29:42.447
|
||||
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
|
||||
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
|
||||
F Makefile.in 38f84f301cbef443b2d269f67a74b8cc536469831f70df7c3e912acc04932cc2
|
||||
@ -484,7 +484,7 @@ F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384
|
||||
F src/resolve.c bbee7e31d369a18a2f4836644769882e9c5d40ef4a3af911db06410b65cb3730
|
||||
F src/rowset.c 7b7e7e479212e65b723bf40128c7b36dc5afdfac
|
||||
F src/select.c 8b22abe193e4d8243befa2038e4ae2405802fed1c446e5e502d11f652e09ba74
|
||||
F src/shell.c.in 2e72b9dc9dabde7e1d26142bf1080dfbc182ac1bf9431f85307b5931062a041b
|
||||
F src/shell.c.in 0baa3d017e3e46ed935413f7e8d09b8c77a8f870c84cf8b2d8b81528517bf485
|
||||
F src/sqlite.h.in 1f1a2da222ec57465794e8984d77f32d0bd0da80cdc136beadda461a0be9d80c
|
||||
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
|
||||
F src/sqlite3ext.h c02d628cca67f3889c689d82d25c3eb45e2c155db08e4c6089b5840d64687d34
|
||||
@ -1218,7 +1218,7 @@ F test/sharedA.test 0cdf1a76dfa00e6beee66af5b534b1e8df2720f5
|
||||
F test/sharedB.test 16cc7178e20965d75278f410943109b77b2e645e
|
||||
F test/shared_err.test 32634e404a3317eeb94abc7a099c556a346fdb8fb3858dbe222a4cbb8926a939
|
||||
F test/sharedlock.test 5ede3c37439067c43b0198f580fd374ebf15d304
|
||||
F test/shell1.test 6d69e08039aea13f2c42749f162fe05eab7b5c93729f31d49d7d27cf36226e5a
|
||||
F test/shell1.test 9f8b8da05a79b134e252a5e1d8d411245ad83ac7126c262900b9f42b43108ffd
|
||||
F test/shell2.test e242a9912f44f4c23c3d1d802a83e934e84c853b
|
||||
F test/shell3.test ac8c2b744014c3e9a0e26bfd829ab65f00923dc1a91ffd044863e9423cc91494
|
||||
F test/shell4.test 89ad573879a745974ff2df20ff97c5d6ffffbd5d
|
||||
@ -1697,7 +1697,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
|
||||
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
|
||||
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
|
||||
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
|
||||
P 6a6a3d495633b906ca31f513c30d31e6daf0f0f105be9ba0a0dc07d201d5b630
|
||||
R 47ef8e39d746f78565d92ddee819e4f1
|
||||
P 60c694c1ab26a7a096f17ccea5a93ecda0f9f2113ab5fdc8b17dbffc787724fc fbf5e43c07e7c012cb39b33a74b3fab9e46ba946c48497fbd990110692125f57
|
||||
R cbc8182064251451cd032585a1a8098f
|
||||
T +closed fbf5e43c07e7c012cb39b33a74b3fab9e46ba946c48497fbd990110692125f57
|
||||
U drh
|
||||
Z 8e9572c27b3beeb784a18b126ea860dc
|
||||
Z c9d5887fceb0e0e24e4f6ea11d33b5eb
|
||||
|
@ -1 +1 @@
|
||||
60c694c1ab26a7a096f17ccea5a93ecda0f9f2113ab5fdc8b17dbffc787724fc
|
||||
a6ed5ab9bc9741267fd1207e63b4c49d074291085c7e3f25e9f62a82f916a656
|
126
src/shell.c.in
126
src/shell.c.in
@ -1012,12 +1012,14 @@ struct ShellState {
|
||||
u8 statsOn; /* True to display memory stats before each finalize */
|
||||
u8 scanstatsOn; /* True to display scan stats before each finalize */
|
||||
u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
|
||||
u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
|
||||
int outCount; /* Revert to stdout when reaching zero */
|
||||
int cnt; /* Number of records displayed so far */
|
||||
FILE *out; /* Write results here */
|
||||
FILE *traceOut; /* Output for sqlite3_trace() */
|
||||
int nErr; /* Number of errors seen */
|
||||
int mode; /* An output mode setting */
|
||||
int modePrior; /* Saved mode */
|
||||
int cMode; /* temporary output mode for the current query */
|
||||
int normalMode; /* Output mode before ".explain on" */
|
||||
int writableSchema; /* True if PRAGMA writable_schema=ON */
|
||||
@ -1025,9 +1027,12 @@ struct ShellState {
|
||||
int nCheck; /* Number of ".check" commands run */
|
||||
unsigned shellFlgs; /* Various flags */
|
||||
char *zDestTable; /* Name of destination table when MODE_Insert */
|
||||
char *zTempFile; /* Temporary file that might need deleting */
|
||||
char zTestcase[30]; /* Name of current test case */
|
||||
char colSeparator[20]; /* Column separator character for several modes */
|
||||
char rowSeparator[20]; /* Row separator character for MODE_Ascii */
|
||||
char colSepPrior[20]; /* Saved column separator */
|
||||
char rowSepPrior[20]; /* Saved row separator */
|
||||
int colWidth[100]; /* Requested width of each column when in column mode*/
|
||||
int actualWidth[100]; /* Actual width of each column */
|
||||
char nullValue[20]; /* The text to print when a NULL comes back from
|
||||
@ -1151,6 +1156,20 @@ static void shellPutsFunc(
|
||||
sqlite3_result_value(pCtx, apVal[0]);
|
||||
}
|
||||
|
||||
/*
|
||||
** Save or restore the current output mode
|
||||
*/
|
||||
static void outputModePush(ShellState *p){
|
||||
p->modePrior = p->mode;
|
||||
memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
|
||||
memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
|
||||
}
|
||||
static void outputModePop(ShellState *p){
|
||||
p->mode = p->modePrior;
|
||||
memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
|
||||
memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
|
||||
}
|
||||
|
||||
/*
|
||||
** Output the given string as a hex-encoded blob (eg. X'1234' )
|
||||
*/
|
||||
@ -3064,6 +3083,7 @@ static char zHelp[] =
|
||||
" LIKE pattern TABLE.\n"
|
||||
".echo on|off Turn command echo on or off\n"
|
||||
".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n"
|
||||
".excel Display the output of next command in a spreadsheet\n"
|
||||
".exit Exit this program\n"
|
||||
".expert EXPERIMENTAL. Suggest indexes for specified queries\n"
|
||||
/* Because explain mode comes on automatically now, the ".explain" mode
|
||||
@ -3504,7 +3524,7 @@ static void output_file_close(FILE *f){
|
||||
** recognized and do the right thing. NULL is returned if the output
|
||||
** filename is "off".
|
||||
*/
|
||||
static FILE *output_file_open(const char *zFile){
|
||||
static FILE *output_file_open(const char *zFile, int bTextMode){
|
||||
FILE *f;
|
||||
if( strcmp(zFile,"stdout")==0 ){
|
||||
f = stdout;
|
||||
@ -3513,7 +3533,7 @@ static FILE *output_file_open(const char *zFile){
|
||||
}else if( strcmp(zFile, "off")==0 ){
|
||||
f = 0;
|
||||
}else{
|
||||
f = fopen(zFile, "wb");
|
||||
f = fopen(zFile, bTextMode ? "w" : "wb");
|
||||
if( f==0 ){
|
||||
utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
|
||||
}
|
||||
@ -3926,7 +3946,11 @@ static void tryToClone(ShellState *p, const char *zNewDb){
|
||||
}
|
||||
|
||||
/*
|
||||
** Change the output file back to stdout
|
||||
** Change the output file back to stdout.
|
||||
**
|
||||
** If the p->doXdgOpen flag is set, that means the output was being
|
||||
** redirected to a temporary file named by p->zTempFile. In that case,
|
||||
** launch start/open/xdg-open on that temporary file.
|
||||
*/
|
||||
static void output_reset(ShellState *p){
|
||||
if( p->outfile[0]=='|' ){
|
||||
@ -3935,6 +3959,24 @@ static void output_reset(ShellState *p){
|
||||
#endif
|
||||
}else{
|
||||
output_file_close(p->out);
|
||||
if( p->doXdgOpen ){
|
||||
const char *zXdgOpenCmd =
|
||||
#if defined(_WIN32)
|
||||
"start";
|
||||
#elif defined(__APPLE__)
|
||||
"open";
|
||||
#else
|
||||
"xdg-open";
|
||||
#endif
|
||||
char *zCmd;
|
||||
zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
|
||||
if( system(zCmd) ){
|
||||
utf8_printf(stderr, "Failed: [%s]\n", zCmd);
|
||||
}
|
||||
sqlite3_free(zCmd);
|
||||
outputModePop(p);
|
||||
p->doXdgOpen = 0;
|
||||
}
|
||||
}
|
||||
p->outfile[0] = 0;
|
||||
p->out = stdout;
|
||||
@ -4193,6 +4235,40 @@ int shellDeleteFile(const char *zFilename){
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
** Try to delete the temporary file (if there is one) and free the
|
||||
** memory used to hold the name of the temp file.
|
||||
*/
|
||||
static void clearTempFile(ShellState *p){
|
||||
if( p->zTempFile==0 ) return;
|
||||
if( shellDeleteFile(p->zTempFile) ) return;
|
||||
sqlite3_free(p->zTempFile);
|
||||
p->zTempFile = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Create a new temp file name with the given suffix.
|
||||
*/
|
||||
static void newTempFile(ShellState *p, const char *zSuffix){
|
||||
clearTempFile(p);
|
||||
sqlite3_free(p->zTempFile);
|
||||
p->zTempFile = 0;
|
||||
if( p->db ){
|
||||
sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
|
||||
}
|
||||
if( p->zTempFile==0 ){
|
||||
sqlite3_uint64 r;
|
||||
sqlite3_randomness(sizeof(r), &r);
|
||||
p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix);
|
||||
}else{
|
||||
p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
|
||||
}
|
||||
if( p->zTempFile==0 ){
|
||||
raw_printf(stderr, "out of memory\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** The implementation of SQL scalar function fkey_collate_clause(), used
|
||||
@ -5205,6 +5281,7 @@ static int do_meta_command(char *zLine, ShellState *p){
|
||||
if( nArg==0 ) return 0; /* no tokens, no error */
|
||||
n = strlen30(azArg[0]);
|
||||
c = azArg[0][0];
|
||||
clearTempFile(p);
|
||||
|
||||
#ifndef SQLITE_OMIT_AUTHORIZATION
|
||||
if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
|
||||
@ -6000,7 +6077,7 @@ static int do_meta_command(char *zLine, ShellState *p){
|
||||
}else{
|
||||
const char *zFile = azArg[1];
|
||||
output_file_close(p->pLog);
|
||||
p->pLog = output_file_open(zFile);
|
||||
p->pLog = output_file_open(zFile, 0);
|
||||
}
|
||||
}else
|
||||
|
||||
@ -6109,18 +6186,27 @@ static int do_meta_command(char *zLine, ShellState *p){
|
||||
}
|
||||
}else
|
||||
|
||||
if( c=='o'
|
||||
&& (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0)
|
||||
if( (c=='o'
|
||||
&& (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
|
||||
|| (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
|
||||
){
|
||||
const char *zFile = nArg>=2 ? azArg[1] : "stdout";
|
||||
int bTxtMode = 0;
|
||||
if( azArg[0][0]=='e' ){
|
||||
/* Transform the ".excel" command into ".once -x" */
|
||||
nArg = 2;
|
||||
azArg[0] = "once";
|
||||
zFile = azArg[1] = "-x";
|
||||
n = 4;
|
||||
}
|
||||
if( nArg>2 ){
|
||||
utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]);
|
||||
utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]);
|
||||
rc = 1;
|
||||
goto meta_command_exit;
|
||||
}
|
||||
if( n>1 && strncmp(azArg[0], "once", n)==0 ){
|
||||
if( nArg<2 ){
|
||||
raw_printf(stderr, "Usage: .once FILE\n");
|
||||
raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n");
|
||||
rc = 1;
|
||||
goto meta_command_exit;
|
||||
}
|
||||
@ -6129,6 +6215,21 @@ static int do_meta_command(char *zLine, ShellState *p){
|
||||
p->outCount = 0;
|
||||
}
|
||||
output_reset(p);
|
||||
if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
|
||||
if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
|
||||
p->doXdgOpen = 1;
|
||||
outputModePush(p);
|
||||
if( zFile[1]=='x' ){
|
||||
newTempFile(p, "csv");
|
||||
p->mode = MODE_Csv;
|
||||
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
|
||||
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
|
||||
}else{
|
||||
newTempFile(p, "txt");
|
||||
bTxtMode = 1;
|
||||
}
|
||||
zFile = p->zTempFile;
|
||||
}
|
||||
if( zFile[0]=='|' ){
|
||||
#ifdef SQLITE_OMIT_POPEN
|
||||
raw_printf(stderr, "Error: pipes are not supported in this OS\n");
|
||||
@ -6145,7 +6246,7 @@ static int do_meta_command(char *zLine, ShellState *p){
|
||||
}
|
||||
#endif
|
||||
}else{
|
||||
p->out = output_file_open(zFile);
|
||||
p->out = output_file_open(zFile, bTxtMode);
|
||||
if( p->out==0 ){
|
||||
if( strcmp(zFile,"off")!=0 ){
|
||||
utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
|
||||
@ -7022,7 +7123,7 @@ static int do_meta_command(char *zLine, ShellState *p){
|
||||
/* Begin redirecting output to the file "testcase-out.txt" */
|
||||
if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
|
||||
output_reset(p);
|
||||
p->out = output_file_open("testcase-out.txt");
|
||||
p->out = output_file_open("testcase-out.txt", 0);
|
||||
if( p->out==0 ){
|
||||
raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
|
||||
}
|
||||
@ -7228,7 +7329,7 @@ static int do_meta_command(char *zLine, ShellState *p){
|
||||
goto meta_command_exit;
|
||||
}
|
||||
output_file_close(p->traceOut);
|
||||
p->traceOut = output_file_open(azArg[1]);
|
||||
p->traceOut = output_file_open(azArg[1], 0);
|
||||
#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
|
||||
if( p->traceOut==0 ){
|
||||
sqlite3_trace_v2(p->db, 0, 0, 0);
|
||||
@ -7558,6 +7659,8 @@ static int process_input(ShellState *p, FILE *in){
|
||||
if( p->outCount ){
|
||||
output_reset(p);
|
||||
p->outCount = 0;
|
||||
}else{
|
||||
clearTempFile(p);
|
||||
}
|
||||
}else if( nSql && _all_whitespace(zSql) ){
|
||||
if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
|
||||
@ -8180,6 +8283,7 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
|
||||
}
|
||||
sqlite3_free(data.zFreeOnClose);
|
||||
find_home_dir(1);
|
||||
clearTempFile(&data);
|
||||
#if !SQLITE_SHELL_IS_UTF8
|
||||
for(i=0; i<argc; i++) sqlite3_free(argv[i]);
|
||||
sqlite3_free(argv);
|
||||
|
@ -495,7 +495,7 @@ do_test shell1-3.15.2 {
|
||||
do_test shell1-3.15.3 {
|
||||
# too many arguments
|
||||
catchcmd "test.db" ".output FOO BAD"
|
||||
} {1 {Usage: .output FILE}}
|
||||
} {1 {Usage: .output [-e|-x|FILE]}}
|
||||
|
||||
# .output stdout Send output to the screen
|
||||
do_test shell1-3.16.1 {
|
||||
@ -504,7 +504,7 @@ do_test shell1-3.16.1 {
|
||||
do_test shell1-3.16.2 {
|
||||
# too many arguments
|
||||
catchcmd "test.db" ".output stdout BAD"
|
||||
} {1 {Usage: .output FILE}}
|
||||
} {1 {Usage: .output [-e|-x|FILE]}}
|
||||
|
||||
# .prompt MAIN CONTINUE Replace the standard prompts
|
||||
do_test shell1-3.17.1 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user