/ Check-in [9b8458bb]
Login

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

Overview
Comment::-) (CVS 30)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:9b8458bbcef19882f14f0e96575fc3f0a86df97a
User & Date: drh 2000-05-31 23:33:17
Context
2000-06-01
00:03
:-) (CVS 31) check-in: f035dec9 user: drh tags: trunk
2000-05-31
23:33
:-) (CVS 30) check-in: 9b8458bb user: drh tags: trunk
22:58
:-) (CVS 29) check-in: 09054df3 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/shell.c.

    20     20   **   drh@hwaci.com
    21     21   **   http://www.hwaci.com/drh/
    22     22   **
    23     23   *************************************************************************
    24     24   ** This file contains code to implement the "sqlite" command line
    25     25   ** utility for accessing SQLite databases.
    26     26   **
    27         -** $Id: shell.c,v 1.4 2000/05/31 02:27:49 drh Exp $
           27  +** $Id: shell.c,v 1.5 2000/05/31 23:33:17 drh Exp $
    28     28   */
    29     29   #include <stdlib.h>
    30     30   #include <string.h>
    31     31   #include <stdio.h>
    32     32   #include "sqlite.h"
    33     33   #include <unistd.h>
    34     34   #include <ctype.h>
................................................................................
   133    133   
   134    134   /*
   135    135   ** These are the allowed modes.
   136    136   */
   137    137   #define MODE_Line     0  /* One field per line.  Blank line between records */
   138    138   #define MODE_Column   1  /* One record per line in neat columns */
   139    139   #define MODE_List     2  /* One record per line with a separator */
          140  +#define MODE_Html     3  /* Generate an XHTML table */
   140    141   
   141    142   /*
   142    143   ** Number of elements in an array
   143    144   */
   144    145   #define ArraySize(X)  (sizeof(X)/sizeof(X[0]))
   145    146   
   146    147   /*
................................................................................
   172    173           for(i=0; i<nArg; i++){
   173    174             int w;
   174    175             if( i<ArraySize(p->colWidth) && p->colWidth[i]>0 ){
   175    176                w = p->colWidth[i];
   176    177             }else{
   177    178                w = 10;
   178    179             }
   179         -          fprintf(p->out,"%-*.*s%s",w,w,"-------------------------------------",
          180  +          fprintf(p->out,"%-*.*s%s",w,w,"-------------------------------------"
          181  +                 "------------------------------------------------------------",
   180    182                     i==nArg-1 ? "\n": "  ");
   181    183           }
   182    184         }
   183    185         for(i=0; i<nArg; i++){
   184    186           int w;
   185    187           if( i<ArraySize(p->colWidth) && p->colWidth[i]>0 ){
   186    188              w = p->colWidth[i];
................................................................................
   198    200           }
   199    201         }
   200    202         for(i=0; i<nArg; i++){
   201    203           fprintf(p->out,"%s%s",azArg[i], i==nArg-1 ? "\n" : p->separator);
   202    204         }
   203    205         break;
   204    206       }
          207  +    case MODE_Html: {
          208  +      if( p->cnt++==0 && p->showHeader ){
          209  +        fprintf(p->out,"<TR>");
          210  +        for(i=0; i<nArg; i++){
          211  +          fprintf(p->out,"<TH>%s</TH>",azCol[i]);
          212  +        }
          213  +        fprintf(p->out,"</TR>\n");
          214  +      }
          215  +      for(i=0; i<nArg; i++){
          216  +        fprintf(p->out,"<TR>");
          217  +        for(i=0; i<nArg; i++){
          218  +          fprintf(p->out,"<TD>%s</TD>",azArg[i]);
          219  +        }
          220  +        fprintf(p->out,"</TD>\n");
          221  +      }
          222  +      break;
          223  +    }
   205    224     }      
   206    225     return 0;
   207    226   }
   208    227   
   209    228   /*
   210    229   ** Text of a help message
   211    230   */
   212    231   static char zHelp[] = 
   213    232     ".exit                  Exit this program\n"
   214    233     ".explain               Set output mode suitable for EXPLAIN\n"
   215    234     ".header ON|OFF         Turn display of headers on or off\n"
   216    235     ".help                  Show this message\n"
   217    236     ".indices TABLE         Show names of all indices on TABLE\n"
   218         -  ".mode MODE             Set mode to one of \"line\", \"column\", or"
   219         -                                      " \"list\"\n"
          237  +  ".mode MODE             Set mode to one of \"line\", \"column\", "
          238  +                                      "\"list\", or \"html\"\n"
   220    239     ".output FILENAME       Send output to FILENAME\n"
   221    240     ".output stdout         Send output to the screen\n"
   222    241     ".schema ?TABLE?        Show the CREATE statements\n"
   223    242     ".separator STRING      Change separator string for \"list\" mode\n"
   224    243     ".tables                List names all tables in the database\n"
   225    244     ".width NUM NUM ...     Set column widths for \"column\" mode\n"
   226    245   ;
................................................................................
   313    332       int n2 = strlen(azArg[1]);
   314    333       if( strncmp(azArg[1],"line",n2)==0 ){
   315    334         p->mode = MODE_Line;
   316    335       }else if( strncmp(azArg[1],"column",n2)==0 ){
   317    336         p->mode = MODE_Column;
   318    337       }else if( strncmp(azArg[1],"list",n2)==0 ){
   319    338         p->mode = MODE_List;
          339  +    }else if( strncmp(azArg[1],"html",n2)==0 ){
          340  +      p->mode = MODE_Html;
   320    341       }
   321    342     }else
   322    343   
   323    344     if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
   324    345       if( p->out!=stdout ){
   325    346         fclose(p->out);
   326    347       }
................................................................................
   387    408         azArg[0]);
   388    409     }
   389    410   }
   390    411   
   391    412   int main(int argc, char **argv){
   392    413     sqlite *db;
   393    414     char *zErrMsg = 0;
          415  +  char *argv0 = argv[0];
   394    416     struct callback_data data;
   395    417   
          418  +  memset(&data, 0, sizeof(data));
          419  +  data.mode = MODE_List;
          420  +  strcpy(data.separator,"|");
          421  +  data.showHeader = 0;
          422  +  while( argc>=2 && argv[1][0]=='-' ){
          423  +    if( strcmp(argv[1],"-html")==0 ){
          424  +      data.mode = MODE_Html;
          425  +      argc--;
          426  +      argv++;
          427  +    }else if( strcmp(argv[1],"-list")==0 ){
          428  +      data.mode = MODE_List;
          429  +      argc--;
          430  +      argv++;
          431  +    }else if( strcmp(argv[1],"-line")==0 ){
          432  +      data.mode = MODE_Line;
          433  +      argc--;
          434  +      argv++;
          435  +    }else if( argc>=3 && strcmp(argv[0],"-separator")==0 ){
          436  +      sprintf(data.separator,"%.*s",sizeof(data.separator)-1,argv[2]);
          437  +      argc -= 2;
          438  +      argv += 2;
          439  +    }else if( strcmp(argv[1],"-header")==0 ){
          440  +      data.showHeader = 1;
          441  +      argc--;
          442  +      argv++;
          443  +    }else if( strcmp(argv[1],"-noheader")==0 ){
          444  +      data.showHeader = 0;
          445  +      argc--;
          446  +      argv++;
          447  +    }else{
          448  +      fprintf(stderr,"%s: unknown option: %s\n", argv0, argv[1]);
          449  +      return 1;
          450  +    }
          451  +  }
   396    452     if( argc!=2 && argc!=3 ){
   397         -    fprintf(stderr,"Usage: %s FILENAME ?SQL?\n", *argv);
          453  +    fprintf(stderr,"Usage: %s ?OPTIONS? FILENAME ?SQL?\n", argv0);
   398    454       exit(1);
   399    455     }
   400    456     db = sqlite_open(argv[1], 0666, &zErrMsg);
   401    457     if( db==0 ){
   402    458       fprintf(stderr,"Unable to open database \"%s\": %s\n", argv[1], zErrMsg);
   403    459       exit(1);
   404    460     }
   405         -  memset(&data, 0, sizeof(data));
   406    461     data.out = stdout;
   407    462     if( argc==3 ){
   408         -    data.mode = MODE_List;
   409         -    strcpy(data.separator,"|");
   410    463       if( sqlite_exec(db, argv[2], callback, &data, &zErrMsg)!=0 && zErrMsg!=0 ){
   411    464         fprintf(stderr,"SQL error: %s\n", zErrMsg);
   412    465         exit(1);
   413    466       }
   414    467     }else{
   415    468       char *zLine;
   416    469       char *zSql = 0;
   417    470       int nSql = 0;
   418    471       int istty = isatty(0);
   419         -    data.mode = MODE_Line;
   420         -    strcpy(data.separator,"|");
   421         -    data.showHeader = 0;
   422    472       if( istty ){
   423    473         printf(
   424    474           "Enter \".help\" for instructions\n"
   425    475         );
   426    476       }
   427    477       while( (zLine = one_input_line(zSql, istty))!=0 ){
   428    478         if( zLine && zLine[0]=='.' ){
................................................................................
   434    484           nSql = strlen(zLine);
   435    485           zSql = malloc( nSql+1 );
   436    486           strcpy(zSql, zLine);
   437    487         }else{
   438    488           int len = strlen(zLine);
   439    489           zSql = realloc( zSql, nSql + len + 2 );
   440    490           if( zSql==0 ){
   441         -          fprintf(stderr,"%s: out of memory!\n", *argv);
          491  +          fprintf(stderr,"%s: out of memory!\n", argv0);
   442    492             exit(1);
   443    493           }
   444    494           strcpy(&zSql[nSql++], "\n");
   445    495           strcpy(&zSql[nSql], zLine);
   446    496           nSql += len;
   447    497         }
   448    498         free(zLine);