/ Check-in [f697c164]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Save and restore the output mode when doing ".once -x" or ".excel".
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | excel-shell-cmd
Files: files | file ages | folders
SHA3-256: f697c164518d36f2a63c87d9f2708d0f9481fad3ded2de61f3f48c393cf7a500
User & Date: drh 2018-01-10 22:27:21
Context
2018-01-10
23:27
Update test cases for the new "usage" for .output. Closed-Leaf check-in: fbf5e43c user: drh tags: excel-shell-cmd
22:27
Save and restore the output mode when doing ".once -x" or ".excel". check-in: f697c164 user: drh tags: excel-shell-cmd
22:15
An attempt to get ".once -e" working reliably on Windows. check-in: 9b97f9d2 user: drh tags: excel-shell-cmd
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/shell.c.in.

  1015   1015     u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
  1016   1016     int outCount;          /* Revert to stdout when reaching zero */
  1017   1017     int cnt;               /* Number of records displayed so far */
  1018   1018     FILE *out;             /* Write results here */
  1019   1019     FILE *traceOut;        /* Output for sqlite3_trace() */
  1020   1020     int nErr;              /* Number of errors seen */
  1021   1021     int mode;              /* An output mode setting */
         1022  +  int modePrior;         /* Saved mode */
  1022   1023     int cMode;             /* temporary output mode for the current query */
  1023   1024     int normalMode;        /* Output mode before ".explain on" */
  1024   1025     int writableSchema;    /* True if PRAGMA writable_schema=ON */
  1025   1026     int showHeader;        /* True to show column names in List or Column mode */
  1026   1027     int nCheck;            /* Number of ".check" commands run */
  1027   1028     unsigned shellFlgs;    /* Various flags */
  1028   1029     char *zDestTable;      /* Name of destination table when MODE_Insert */
  1029   1030     char *zTempFile;       /* Temporary file that might need deleting */
  1030   1031     char zTestcase[30];    /* Name of current test case */
  1031   1032     char colSeparator[20]; /* Column separator character for several modes */
  1032   1033     char rowSeparator[20]; /* Row separator character for MODE_Ascii */
         1034  +  char colSepPrior[20];  /* Saved column separator */
         1035  +  char rowSepPrior[20];  /* Saved row separator */
  1033   1036     int colWidth[100];     /* Requested width of each column when in column mode*/
  1034   1037     int actualWidth[100];  /* Actual width of each column */
  1035   1038     char nullValue[20];    /* The text to print when a NULL comes back from
  1036   1039                            ** the database */
  1037   1040     char outfile[FILENAME_MAX]; /* Filename for *out */
  1038   1041     const char *zDbFilename;    /* name of the database file */
  1039   1042     char *zFreeOnClose;         /* Filename to free when closing */
................................................................................
  1148   1151     int nVal,
  1149   1152     sqlite3_value **apVal
  1150   1153   ){
  1151   1154     ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
  1152   1155     utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
  1153   1156     sqlite3_result_value(pCtx, apVal[0]);
  1154   1157   }
         1158  +
         1159  +/*
         1160  +** Save or restore the current output mode
         1161  +*/
         1162  +static void outputModePush(ShellState *p){
         1163  +  p->modePrior = p->mode;
         1164  +  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
         1165  +  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
         1166  +}
         1167  +static void outputModePop(ShellState *p){
         1168  +  p->mode = p->modePrior;
         1169  +  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
         1170  +  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
         1171  +}
  1155   1172   
  1156   1173   /*
  1157   1174   ** Output the given string as a hex-encoded blob (eg. X'1234' )
  1158   1175   */
  1159   1176   static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
  1160   1177     int i;
  1161   1178     char *zBlob = (char *)pBlob;
................................................................................
  3953   3970   #endif
  3954   3971         char *zCmd;
  3955   3972         zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
  3956   3973         if( system(zCmd) ){
  3957   3974           utf8_printf(stderr, "Failed: [%s]\n", zCmd);
  3958   3975         }
  3959   3976         sqlite3_free(zCmd);
  3960         -      p->mode = p->doXdgOpen - 1;
         3977  +      outputModePop(p);
  3961   3978         p->doXdgOpen = 0;
  3962   3979       }
  3963   3980     }
  3964   3981     p->outfile[0] = 0;
  3965   3982     p->out = stdout;
  3966   3983   }
  3967   3984   
................................................................................
  6196   6213         p->outCount = 2;
  6197   6214       }else{
  6198   6215         p->outCount = 0;
  6199   6216       }
  6200   6217       output_reset(p);
  6201   6218       if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
  6202   6219       if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
  6203         -      p->doXdgOpen = p->mode + 1;
         6220  +      p->doXdgOpen = 1;
         6221  +      outputModePush(p);
  6204   6222         if( zFile[1]=='x' ){
  6205   6223           newTempFile(p, "csv");
  6206   6224           p->mode = MODE_Csv;
  6207   6225           sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
  6208   6226           sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
  6209   6227         }else{
  6210   6228           newTempFile(p, "txt");