SQLite

Check-in [0582168b8b]
Login

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

Overview
Comment:Change shell.c so that it will compile under windows. Shorten the help command somewhat. Add the state of ".header" to the output of ".show". (CVS 534)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 0582168b8b853559b484f4a024d28c67192160c4
User & Date: drh 2002-04-18 12:39:03.000
Context
2002-04-19
01:00
Added rights release for Matthew O. Persico (CVS 535) (check-in: 6c32c07e82 user: persicom tags: trunk)
2002-04-18
12:39
Change shell.c so that it will compile under windows. Shorten the help command somewhat. Add the state of ".header" to the output of ".show". (CVS 534) (check-in: 0582168b8b user: drh tags: trunk)
02:53
Updated to match new shell.c functionality. (CVS 533) (check-in: ff67ad4010 user: persicom tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/shell.c.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26



27
28
29
30
31
32
33
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains code to implement the "sqlite" command line
** utility for accessing SQLite databases.
**
** $Id: shell.c,v 1.52 2002/04/18 02:53:05 persicom Exp $
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "sqlite.h"
#include <ctype.h>
#include <pwd.h>
#include <sys/types.h>

#if !defined(_WIN32) && !defined(WIN32)
# include <signal.h>



#endif

#if defined(HAVE_READLINE) && HAVE_READLINE==1
# include <readline/readline.h>
# include <readline/history.h>
#else
# define readline(p) getline(p,stdin)







|






<
<



>
>
>







8
9
10
11
12
13
14
15
16
17
18
19
20
21


22
23
24
25
26
27
28
29
30
31
32
33
34
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains code to implement the "sqlite" command line
** utility for accessing SQLite databases.
**
** $Id: shell.c,v 1.53 2002/04/18 12:39:03 drh Exp $
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "sqlite.h"
#include <ctype.h>



#if !defined(_WIN32) && !defined(WIN32)
# include <signal.h>
# include <pwd.h>
# include <unistd.h>
# include <sys/types.h>
#endif

#if defined(HAVE_READLINE) && HAVE_READLINE==1
# include <readline/readline.h>
# include <readline/history.h>
#else
# define readline(p) getline(p,stdin)
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
** Text of a help message
*/
static char zHelp[] =
  ".dump ?TABLE? ...      Dump the database in an text format\n"
  ".echo ON|OFF           Turn command echo on or off\n"
  ".exit                  Exit this program\n"
  ".explain ON|OFF        Turn output mode suitable for EXPLAIN on or off.\n"
  "                       \"off\" will revert to the output mode that was\n"
  "                       previously in effect\n"
  ".header(s) ON|OFF      Turn display of headers on or off\n"
  ".help                  Show this message\n"
  ".indices TABLE         Show names of all indices on TABLE\n"
  ".mode MODE             Set mode to one of \"line(s)\", \"column(s)\", \n"
  "                       \"insert\", \"list\", or \"html\"\n"
  ".mode insert TABLE     Generate SQL insert statements for TABLE\n"
  ".nullvalue STRING      Print STRING instead of nothing for NULL data\n"
  ".output FILENAME       Send output to FILENAME\n"
  ".output stdout         Send output to the screen\n"
  ".prompt MAIN CONTINUE  Replace the standard prompts\n"
  "                       \"sqlite > \" and \"   ...> \"\n"
  "                       with the strings MAIN and CONTINUE\n"
  "                       CONTINUE is optional.\n"
  ".quit                  Exit this program\n"
  ".read FILENAME         Execute SQL in FILENAME\n"
  ".reindex ?TABLE?       Rebuild indices\n"
/*  ".rename OLD NEW        Change the name of a table or index\n" */
  ".schema ?TABLE?        Show the CREATE statements\n"
  ".separator STRING      Change separator string for \"list\" mode\n"
  ".show                  Show the current values for the following:\n"
  "                       .echo\n"
  "                       .explain\n"
  "                       .mode\n"
  "                       .nullvalue\n"
  "                       .output\n"
  "                       .separator\n"
  "                       .width\n"
  ".tables ?PATTERN?      List names of tables matching a pattern\n"
  ".timeout MS            Try opening locked tables for MS milliseconds\n"
  ".width NUM NUM ...     Set column widths for \"column\" mode\n"
;

/* Forward reference */
static void process_input(struct callback_data *p, FILE *in);







<
<










<
<
<






|
<
<
<
<
<
<
<







470
471
472
473
474
475
476


477
478
479
480
481
482
483
484
485
486



487
488
489
490
491
492
493







494
495
496
497
498
499
500
** Text of a help message
*/
static char zHelp[] =
  ".dump ?TABLE? ...      Dump the database in an text format\n"
  ".echo ON|OFF           Turn command echo on or off\n"
  ".exit                  Exit this program\n"
  ".explain ON|OFF        Turn output mode suitable for EXPLAIN on or off.\n"


  ".header(s) ON|OFF      Turn display of headers on or off\n"
  ".help                  Show this message\n"
  ".indices TABLE         Show names of all indices on TABLE\n"
  ".mode MODE             Set mode to one of \"line(s)\", \"column(s)\", \n"
  "                       \"insert\", \"list\", or \"html\"\n"
  ".mode insert TABLE     Generate SQL insert statements for TABLE\n"
  ".nullvalue STRING      Print STRING instead of nothing for NULL data\n"
  ".output FILENAME       Send output to FILENAME\n"
  ".output stdout         Send output to the screen\n"
  ".prompt MAIN CONTINUE  Replace the standard prompts\n"



  ".quit                  Exit this program\n"
  ".read FILENAME         Execute SQL in FILENAME\n"
  ".reindex ?TABLE?       Rebuild indices\n"
/*  ".rename OLD NEW        Change the name of a table or index\n" */
  ".schema ?TABLE?        Show the CREATE statements\n"
  ".separator STRING      Change separator string for \"list\" mode\n"
  ".show                  Show the current values for various settings\n"







  ".tables ?PATTERN?      List names of tables matching a pattern\n"
  ".timeout MS            Try opening locked tables for MS milliseconds\n"
  ".width NUM NUM ...     Set column widths for \"column\" mode\n"
;

/* Forward reference */
static void process_input(struct callback_data *p, FILE *in);
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
  }else

  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
    sqlite_close(db);
    exit(0);
  }else

  if( c=='e' && strncmp(azArg[0], "explain", n)==0 && nArg>1){
    int j;
    char *z = azArg[1];
    int val = atoi(azArg[1]);
    for(j=0; z[j]; j++){
      if( isupper(z[j]) ) z[j] = tolower(z[j]);
    }
    if( strcmp(z,"on")==0 ){
      val = 1;
    }else if( strcmp(z,"yes")==0 ){
      val = 1;







|

|
|







577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
  }else

  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
    sqlite_close(db);
    exit(0);
  }else

  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
    int j;
    char *z = nArg>=2 ? azArg[1] : "1";
    int val = atoi(z);
    for(j=0; z[j]; j++){
      if( isupper(z[j]) ) z[j] = tolower(z[j]);
    }
    if( strcmp(z,"on")==0 ){
      val = 1;
    }else if( strcmp(z,"yes")==0 ){
      val = 1;
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
        p->out = stdout;
      } else {
         strcpy(p->outfile,azArg[1]);
      }
    }
  }else

  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && nArg==2 || nArg==3){
    if( nArg >= 2) {
      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
    }
    if( nArg >= 3) {
      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
    }
  }else







|







706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
        p->out = stdout;
      } else {
         strcpy(p->outfile,azArg[1]);
      }
    }
  }else

  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
    if( nArg >= 2) {
      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
    }
    if( nArg >= 3) {
      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
    }
  }else
825
826
827
828
829
830
831

832
833
834
835
836
837
838
    sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
  }else

  if( c=='s' && strncmp(azArg[0], "show", n)==0){
    int i;
    fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
    fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" : "off");

    fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
    fprintf(p->out,"%9.9s: %s\n","nullvalue", p->nullvalue);
    fprintf(p->out,"%9.9s: %s\n","output", strlen(p->outfile) ? p->outfile : "stdout");
    fprintf(p->out,"%9.9s: %s\n","separator", p->separator);
    fprintf(p->out,"%9.9s: ","width");
    for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
        fprintf(p->out,"%d ",p->colWidth[i]);







>







814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
    sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
  }else

  if( c=='s' && strncmp(azArg[0], "show", n)==0){
    int i;
    fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
    fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" : "off");
    fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
    fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
    fprintf(p->out,"%9.9s: %s\n","nullvalue", p->nullvalue);
    fprintf(p->out,"%9.9s: %s\n","output", strlen(p->outfile) ? p->outfile : "stdout");
    fprintf(p->out,"%9.9s: %s\n","separator", p->separator);
    fprintf(p->out,"%9.9s: ","width");
    for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
        fprintf(p->out,"%d ",p->colWidth[i]);
956
957
958
959
960
961
962
963
964
965
966
967
968



969
970
971
972
973
974

975
976
977
978
979
980
981
}

static void process_sqliterc(struct callback_data *p,
                             char *sqliterc_override){

  char *home_dir = NULL;
  char *sqliterc = sqliterc_override;
  struct passwd *pwent;
  uid_t uid = getuid();
  FILE *in = NULL;

  if (sqliterc == NULL) {
    /* Figure out the user's home directory */



    while( (pwent=getpwent()) != NULL) {
      if(pwent->pw_uid == uid) {
        home_dir = pwent->pw_dir;
        break;
      }
    }


    if (!home_dir) {
      home_dir = getenv("HOME");
      if (!home_dir) {
        home_dir = getenv("HOMEPATH"); /* Windows? */
      }
      if (!home_dir) {







<
<




>
>
>






>







946
947
948
949
950
951
952


953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
}

static void process_sqliterc(struct callback_data *p,
                             char *sqliterc_override){

  char *home_dir = NULL;
  char *sqliterc = sqliterc_override;


  FILE *in = NULL;

  if (sqliterc == NULL) {
    /* Figure out the user's home directory */
#if !defined(_WIN32) && !defined(WIN32)
    struct passwd *pwent;
    uid_t uid = getuid();
    while( (pwent=getpwent()) != NULL) {
      if(pwent->pw_uid == uid) {
        home_dir = pwent->pw_dir;
        break;
      }
    }
#endif

    if (!home_dir) {
      home_dir = getenv("HOME");
      if (!home_dir) {
        home_dir = getenv("HOMEPATH"); /* Windows? */
      }
      if (!home_dir) {
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
  if(!in) {
    /* File either had an error or was not found. Since I cannot
     * tell, I cannot bark. */
    return;
  } else {
    printf("Loading resources from %s\n",sqliterc);
    process_input(p,in);
    close (in);
  }
  return;
}

void main_init(struct callback_data *data) {
  memset(data, 0, sizeof(*data));
  data->mode = MODE_List;







|







991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
  if(!in) {
    /* File either had an error or was not found. Since I cannot
     * tell, I cannot bark. */
    return;
  } else {
    printf("Loading resources from %s\n",sqliterc);
    process_input(p,in);
    fclose(in);
  }
  return;
}

void main_init(struct callback_data *data) {
  memset(data, 0, sizeof(*data));
  data->mode = MODE_List;