I have found a minor bug in the sqlite3 shell program. If you use .read to run 
a 
file with exactly two errors in it, it will exit from sqlite3 to the operating 
system.
To demonstrate, I first create a file with two lines each of which causes an 
(called e2.sql)
 
select nonexistentfunction();
select nonexistentfunction();
 
Then I use .read on this file:
 
sqlite3 -header -column label.esrigdb
SQLite version 3.7.14.1 2012-10-04 19:37:12 Enter ".help" for instructions 
Enter 
SQL statements terminated with a ";"
sqlite> .q
/rococo3/peter/sqliteexe101 > sqlite3 x.db SQLite version 3.7.14.1 2012-10-04 
19:37:12 Enter ".help" for instructions Enter SQL statements terminated with a 
";"
sqlite> .read e2.sql
Error: near line 1: no such function: nonexistentfunction
Error: near line 2: no such function: nonexistentfunction
/rococo3/peter/sqliteexe101 > 
 
And I'm back at the command prompt! This works this way on both Solaris and 
Windows. 

A quick examination of shell.c shows me that in do_meta_command(), quit sets rc 
to 2, and that read sets rc to the output from process_input (which is errCnt) :
.
.
.
  if( c=='q' && strncmp(azArg[0], "quit", n)==0 && nArg==1 ){
    rc = 2;
  }else
.
.
.
  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
    FILE *alt = fopen(azArg[1], "rb");
    if( alt==0 ){
      fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
      rc = 1;
    }else{
      rc = process_input(p, alt);
      fclose(alt);
    }
  }else
.
.
.
  return rc;
}
 
And this in process_input() (note the return of errCnt):
 
.
.
.
  while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){ .
.
.
.
      if( zLine && zLine[0]=='.' && nSql==0 ){
        if( p->echoOn ) printf("%s\n", zLine);
        rc = do_meta_command(zLine, p);
        if( rc==2 ){ /* exit requested */
          break;
      }else if( rc ){
        errCnt++;
      }
      continue;
    }
.
.
.
  }
  free(zLine);
  return errCnt;
}
 
And when process_input() returns after being called from main(),sqlite3 exits.
 
Adding the following line (the one with the comment) to do_meta_command() seems 
to fix the problem:
 
.
.
.
    if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
      FILE *alt = fopen(azArg[1], "rb");
      if( alt==0 ){
        fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
        rc = 1;
    }else{
      rc = process_input(p, alt);
      fclose(alt);
      if (rc>1) rc = 1; /* rc == errCnt, but when rc == 2, shell exits. */
    }
  }else
.
.
.
 
Peter
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to