From 13c209330d03e162aa8cabc196fb1f1f24c24b7c Mon Sep 17 00:00:00 2001
From: drh <drh@noemail.net>
Date: Wed, 10 Jan 2018 21:41:55 +0000
Subject: [PATCH 1/5] Add support for the ".excel" command (and ".once -e" and
 ".once -x") in the CLI.

FossilOrigin-Name: 23fa7c57c2b204d1ddcc2a939b5271628cf26689ad4ede6976038113095a9801
---
 manifest       | 15 +++++----
 manifest.uuid  |  2 +-
 src/shell.c.in | 90 +++++++++++++++++++++++++++++++++++++++++++++++---
 3 files changed, 95 insertions(+), 12 deletions(-)

diff --git a/manifest b/manifest
index 63c68f6f7e..751835d013 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\sa\sharmless\scompiler\swarning\sin\szipfile.c
-D 2018-01-10T19:50:40.811
+C Add\ssupport\sfor\sthe\s".excel"\scommand\s(and\s".once\s-e"\sand\s".once\s-x")\sin\nthe\sCLI.
+D 2018-01-10T21:41:55.211
 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 d1dbc1514d74b425db41be006f2a4f91d26c5e1c2db4ab198b45c12920171e11
 F src/sqlite.h.in 1f1a2da222ec57465794e8984d77f32d0bd0da80cdc136beadda461a0be9d80c
 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
 F src/sqlite3ext.h c02d628cca67f3889c689d82d25c3eb45e2c155db08e4c6089b5840d64687d34
@@ -1697,7 +1697,10 @@ 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
+R bd40eb18946fee4f15d02cd4e13ad69c
+T *branch * excel-shell-cmd
+T *sym-excel-shell-cmd *
+T -sym-trunk *
 U drh
-Z 8e9572c27b3beeb784a18b126ea860dc
+Z f708fb338ffb9679393be576ce4d2140
diff --git a/manifest.uuid b/manifest.uuid
index 12625a7794..5bb31c1d62 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-60c694c1ab26a7a096f17ccea5a93ecda0f9f2113ab5fdc8b17dbffc787724fc
\ No newline at end of file
+23fa7c57c2b204d1ddcc2a939b5271628cf26689ad4ede6976038113095a9801
\ No newline at end of file
diff --git a/src/shell.c.in b/src/shell.c.in
index e1fca6a072..81677bf5e8 100644
--- a/src/shell.c.in
+++ b/src/shell.c.in
@@ -1012,6 +1012,7 @@ 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 */
@@ -1025,6 +1026,7 @@ 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 */
@@ -3064,6 +3066,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
@@ -3926,7 +3929,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 +3942,22 @@ 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);
+      system(zCmd);
+      sqlite3_free(zCmd);
+      p->mode = p->doXdgOpen - 1;
+      p->doXdgOpen = 0;
+    }
   }
   p->outfile[0] = 0;
   p->out = stdout;
@@ -4193,6 +4216,38 @@ 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;
+  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 +5260,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 ){
@@ -6109,18 +6165,26 @@ 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";
+    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 +6193,19 @@ 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 = p->mode + 1;
+      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");
+      }
+      zFile = p->zTempFile;
+    }
     if( zFile[0]=='|' ){
 #ifdef SQLITE_OMIT_POPEN
       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
@@ -7558,6 +7635,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 +8259,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);

From 7f3bf8a9fb9144422cb3d3c1936087f13ab46771 Mon Sep 17 00:00:00 2001
From: drh <drh@noemail.net>
Date: Wed, 10 Jan 2018 21:50:08 +0000
Subject: [PATCH 2/5] Fix a potential SQLITE_MISUSE in the .excel command when
 no database is open.

FossilOrigin-Name: 9b95ff1abfb8d49bbe5a727f5c917a455e4289b4d69196377dc9294409341d70
---
 manifest       | 15 ++++++---------
 manifest.uuid  |  2 +-
 src/shell.c.in |  4 +++-
 3 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/manifest b/manifest
index 751835d013..6b08444735 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\ssupport\sfor\sthe\s".excel"\scommand\s(and\s".once\s-e"\sand\s".once\s-x")\sin\nthe\sCLI.
-D 2018-01-10T21:41:55.211
+C Fix\sa\spotential\sSQLITE_MISUSE\sin\sthe\s.excel\scommand\swhen\sno\sdatabase\sis\sopen.
+D 2018-01-10T21:50:08.964
 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 d1dbc1514d74b425db41be006f2a4f91d26c5e1c2db4ab198b45c12920171e11
+F src/shell.c.in 362e3af76b80c3bd688f1f6fc8df407e651b71a65f0d919287eb6db5c357260c
 F src/sqlite.h.in 1f1a2da222ec57465794e8984d77f32d0bd0da80cdc136beadda461a0be9d80c
 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
 F src/sqlite3ext.h c02d628cca67f3889c689d82d25c3eb45e2c155db08e4c6089b5840d64687d34
@@ -1697,10 +1697,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 60c694c1ab26a7a096f17ccea5a93ecda0f9f2113ab5fdc8b17dbffc787724fc
-R bd40eb18946fee4f15d02cd4e13ad69c
-T *branch * excel-shell-cmd
-T *sym-excel-shell-cmd *
-T -sym-trunk *
+P 23fa7c57c2b204d1ddcc2a939b5271628cf26689ad4ede6976038113095a9801
+R ba511ddf16c10fcf8fdff3518b957cd4
 U drh
-Z f708fb338ffb9679393be576ce4d2140
+Z 0681d6d553d7112baca91b9b1918eb9d
diff --git a/manifest.uuid b/manifest.uuid
index 5bb31c1d62..4442a7e3cf 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-23fa7c57c2b204d1ddcc2a939b5271628cf26689ad4ede6976038113095a9801
\ No newline at end of file
+9b95ff1abfb8d49bbe5a727f5c917a455e4289b4d69196377dc9294409341d70
\ No newline at end of file
diff --git a/src/shell.c.in b/src/shell.c.in
index 81677bf5e8..c76b305450 100644
--- a/src/shell.c.in
+++ b/src/shell.c.in
@@ -4234,7 +4234,9 @@ static void newTempFile(ShellState *p, const char *zSuffix){
   clearTempFile(p);
   sqlite3_free(p->zTempFile);
   p->zTempFile = 0;
-  sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
+  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);

From a92a01a77eadf7a582f2973efe2fd371c38fe92b Mon Sep 17 00:00:00 2001
From: drh <drh@noemail.net>
Date: Wed, 10 Jan 2018 22:15:37 +0000
Subject: [PATCH 3/5] An attempt to get ".once -e" working reliably on Windows.

FossilOrigin-Name: 9b97f9d2c876162139dbd9485fcf68412d1572d9ddc179b08938b8a602e895d6
---
 manifest       | 12 ++++++------
 manifest.uuid  |  2 +-
 src/shell.c.in | 18 +++++++++++-------
 3 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/manifest b/manifest
index 6b08444735..acd4d81085 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\sa\spotential\sSQLITE_MISUSE\sin\sthe\s.excel\scommand\swhen\sno\sdatabase\sis\sopen.
-D 2018-01-10T21:50:08.964
+C An\sattempt\sto\sget\s".once\s-e"\sworking\sreliably\son\sWindows.
+D 2018-01-10T22:15:37.810
 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 362e3af76b80c3bd688f1f6fc8df407e651b71a65f0d919287eb6db5c357260c
+F src/shell.c.in 8ac56c0e57d87f04c7c021d17d541063fd4cd9e5cd558caf40a63bf42875b8a9
 F src/sqlite.h.in 1f1a2da222ec57465794e8984d77f32d0bd0da80cdc136beadda461a0be9d80c
 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
 F src/sqlite3ext.h c02d628cca67f3889c689d82d25c3eb45e2c155db08e4c6089b5840d64687d34
@@ -1697,7 +1697,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 23fa7c57c2b204d1ddcc2a939b5271628cf26689ad4ede6976038113095a9801
-R ba511ddf16c10fcf8fdff3518b957cd4
+P 9b95ff1abfb8d49bbe5a727f5c917a455e4289b4d69196377dc9294409341d70
+R 0c911dcb6179b38e957861df043bdcc3
 U drh
-Z 0681d6d553d7112baca91b9b1918eb9d
+Z 8c9fcb0a09d556e95bbd5b8371ae82b1
diff --git a/manifest.uuid b/manifest.uuid
index 4442a7e3cf..e5bf4629ab 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-9b95ff1abfb8d49bbe5a727f5c917a455e4289b4d69196377dc9294409341d70
\ No newline at end of file
+9b97f9d2c876162139dbd9485fcf68412d1572d9ddc179b08938b8a602e895d6
\ No newline at end of file
diff --git a/src/shell.c.in b/src/shell.c.in
index c76b305450..fae70a1ece 100644
--- a/src/shell.c.in
+++ b/src/shell.c.in
@@ -3507,7 +3507,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;
@@ -3516,7 +3516,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);
     }
@@ -3953,7 +3953,9 @@ static void output_reset(ShellState *p){
 #endif
       char *zCmd;
       zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
-      system(zCmd);
+      if( system(zCmd) ){
+        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
+      }
       sqlite3_free(zCmd);
       p->mode = p->doXdgOpen - 1;
       p->doXdgOpen = 0;
@@ -6058,7 +6060,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
 
@@ -6172,6 +6174,7 @@ static int do_meta_command(char *zLine, ShellState *p){
    || (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;
@@ -6205,6 +6208,7 @@ static int do_meta_command(char *zLine, ShellState *p){
         sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
       }else{
         newTempFile(p, "txt");
+        bTxtMode = 1;
       }
       zFile = p->zTempFile;
     }
@@ -6224,7 +6228,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);
@@ -7101,7 +7105,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");
     }
@@ -7307,7 +7311,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);

From 3c484e8c1f87eddca84af1b63f3751c6265614e8 Mon Sep 17 00:00:00 2001
From: drh <drh@noemail.net>
Date: Wed, 10 Jan 2018 22:27:21 +0000
Subject: [PATCH 4/5] Save and restore the output mode when doing ".once -x" or
 ".excel".

FossilOrigin-Name: f697c164518d36f2a63c87d9f2708d0f9481fad3ded2de61f3f48c393cf7a500
---
 manifest       | 12 ++++++------
 manifest.uuid  |  2 +-
 src/shell.c.in | 22 ++++++++++++++++++++--
 3 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/manifest b/manifest
index acd4d81085..34e956876e 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C An\sattempt\sto\sget\s".once\s-e"\sworking\sreliably\son\sWindows.
-D 2018-01-10T22:15:37.810
+C Save\sand\srestore\sthe\soutput\smode\swhen\sdoing\s".once\s-x"\sor\s".excel".
+D 2018-01-10T22:27:21.115
 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 8ac56c0e57d87f04c7c021d17d541063fd4cd9e5cd558caf40a63bf42875b8a9
+F src/shell.c.in 0baa3d017e3e46ed935413f7e8d09b8c77a8f870c84cf8b2d8b81528517bf485
 F src/sqlite.h.in 1f1a2da222ec57465794e8984d77f32d0bd0da80cdc136beadda461a0be9d80c
 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
 F src/sqlite3ext.h c02d628cca67f3889c689d82d25c3eb45e2c155db08e4c6089b5840d64687d34
@@ -1697,7 +1697,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 9b95ff1abfb8d49bbe5a727f5c917a455e4289b4d69196377dc9294409341d70
-R 0c911dcb6179b38e957861df043bdcc3
+P 9b97f9d2c876162139dbd9485fcf68412d1572d9ddc179b08938b8a602e895d6
+R d864377b5e3ba9e6ed72c01bab712abd
 U drh
-Z 8c9fcb0a09d556e95bbd5b8371ae82b1
+Z 372298188cf6cc90b3d7d11dd46f07c5
diff --git a/manifest.uuid b/manifest.uuid
index e5bf4629ab..3358c70b40 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-9b97f9d2c876162139dbd9485fcf68412d1572d9ddc179b08938b8a602e895d6
\ No newline at end of file
+f697c164518d36f2a63c87d9f2708d0f9481fad3ded2de61f3f48c393cf7a500
\ No newline at end of file
diff --git a/src/shell.c.in b/src/shell.c.in
index fae70a1ece..e9c268e42a 100644
--- a/src/shell.c.in
+++ b/src/shell.c.in
@@ -1019,6 +1019,7 @@ struct ShellState {
   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 */
@@ -1030,6 +1031,8 @@ struct ShellState {
   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
@@ -1153,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' )
 */
@@ -3957,7 +3974,7 @@ static void output_reset(ShellState *p){
         utf8_printf(stderr, "Failed: [%s]\n", zCmd);
       }
       sqlite3_free(zCmd);
-      p->mode = p->doXdgOpen - 1;
+      outputModePop(p);
       p->doXdgOpen = 0;
     }
   }
@@ -6200,7 +6217,8 @@ static int do_meta_command(char *zLine, ShellState *p){
     output_reset(p);
     if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
     if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
-      p->doXdgOpen = p->mode + 1;
+      p->doXdgOpen = 1;
+      outputModePush(p);
       if( zFile[1]=='x' ){
         newTempFile(p, "csv");
         p->mode = MODE_Csv;

From 6c9dcc427d38a3eccf4ed8ae5b4218c981a737d0 Mon Sep 17 00:00:00 2001
From: drh <drh@noemail.net>
Date: Wed, 10 Jan 2018 23:27:30 +0000
Subject: [PATCH 5/5] Update test cases for the new "usage" for .output.

FossilOrigin-Name: fbf5e43c07e7c012cb39b33a74b3fab9e46ba946c48497fbd990110692125f57
---
 manifest         | 12 ++++++------
 manifest.uuid    |  2 +-
 test/shell1.test |  4 ++--
 3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/manifest b/manifest
index 34e956876e..4766b6496a 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Save\sand\srestore\sthe\soutput\smode\swhen\sdoing\s".once\s-x"\sor\s".excel".
-D 2018-01-10T22:27:21.115
+C Update\stest\scases\sfor\sthe\snew\s"usage"\sfor\s.output.
+D 2018-01-10T23:27:30.001
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F Makefile.in 38f84f301cbef443b2d269f67a74b8cc536469831f70df7c3e912acc04932cc2
@@ -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,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 9b97f9d2c876162139dbd9485fcf68412d1572d9ddc179b08938b8a602e895d6
-R d864377b5e3ba9e6ed72c01bab712abd
+P f697c164518d36f2a63c87d9f2708d0f9481fad3ded2de61f3f48c393cf7a500
+R cbc8182064251451cd032585a1a8098f
 U drh
-Z 372298188cf6cc90b3d7d11dd46f07c5
+Z 0fca8af732176ea4c3e2417e5169029d
diff --git a/manifest.uuid b/manifest.uuid
index 3358c70b40..6ef1621db8 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-f697c164518d36f2a63c87d9f2708d0f9481fad3ded2de61f3f48c393cf7a500
\ No newline at end of file
+fbf5e43c07e7c012cb39b33a74b3fab9e46ba946c48497fbd990110692125f57
\ No newline at end of file
diff --git a/test/shell1.test b/test/shell1.test
index 0d03c64f78..81883a8d18 100644
--- a/test/shell1.test
+++ b/test/shell1.test
@@ -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 {