shell.c File Reference

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "sqlite3.h"
#include <ctype.h>
#include <stdarg.h>
#include <signal.h>
#include <pwd.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>

Go to the source code of this file.

Data Structures

struct  callback_data
struct  previous_mode_data

Defines

#define _FILE_OFFSET_BITS   64
#define _LARGE_FILE   1
#define _LARGEFILE_SOURCE   1
#define add_history(X)
#define ArraySize(X)   (int)(sizeof(X)/sizeof(X[0]))
#define BEGIN_TIMER   beginTimer()
#define END_TIMER   endTimer()
#define HAS_TIMER   1
#define IsDigit(X)   isdigit((unsigned char)X)
#define IsSpace(X)   isspace((unsigned char)X)
#define MODE_Column   1
#define MODE_Csv   7
#define MODE_Explain   8
#define MODE_Html   4
#define MODE_Insert   5
#define MODE_Line   0
#define MODE_List   2
#define MODE_Semi   3
#define MODE_Tcl   6
#define read_history(X)
#define readline(p)   local_getline(p,stdin)
#define stifle_history(X)
#define ToLower(X)   (char)tolower((unsigned char)X)
#define UNUSED_PARAMETER(x)   (void)(x)
#define write_history(X)

Functions

static int _all_whitespace (const char *z)
static int _contains_semicolon (const char *z, int N)
static int _is_command_terminator (const char *zLine)
static int _is_complete (char *zSql, int nSql)
static char * appendText (char *zIn, char const *zAppend, char quote)
static void beginTimer (void)
static int booleanValue (char *zArg)
static int callback (void *pArg, int nArg, char **azArg, char **azCol)
static int display_stats (sqlite3 *db, struct callback_data *pArg, int bReset)
static int do_meta_command (char *zLine, struct callback_data *p)
static int dump_callback (void *pArg, int nArg, char **azArg, char **azCol)
static void endTimer (void)
static char * find_home_dir (void)
int isatty (int)
static int isNumber (const char *z, int *realnum)
static char * local_getline (char *zPrompt, FILE *in)
int main (int argc, char **argv)
static void main_init (struct callback_data *data)
static char * one_input_line (const char *zPrior, FILE *in)
static void open_db (struct callback_data *p)
static void output_c_string (FILE *out, const char *z)
static void output_csv (struct callback_data *p, const char *z, int bSep)
static void output_hex_blob (FILE *out, const void *pBlob, int nBlob)
static void output_html_string (FILE *out, const char *z)
static void output_quoted_string (FILE *out, const char *z)
static int process_input (struct callback_data *p, FILE *in)
static int process_sqliterc (struct callback_data *p, const char *sqliterc_override)
static void resolve_backslashes (char *z)
static int run_schema_dump_query (struct callback_data *p, const char *zQuery)
static int run_table_dump_query (struct callback_data *p, const char *zSelect, const char *zFirstRow)
static char * save_err_msg (sqlite3 *db)
static void set_table_name (struct callback_data *p, const char *zName)
static int shell_callback (void *pArg, int nArg, char **azArg, char **azCol, int *aiType)
static int shell_exec (sqlite3 *db, const char *zSql, int(*xCallback)(void *, int, char **, char **, int *), struct callback_data *pArg, char **pzErrMsg)
static void shellLog (void *pArg, int iErrCode, const char *zMsg)
static void shellstaticFunc (sqlite3_context *context, int argc, sqlite3_value **argv)
static int strlen30 (const char *z)
static double timeDiff (struct timeval *pStart, struct timeval *pEnd)
static void usage (int showDetail)

Variables

static char * Argv0
static int bail_on_error = 0
static char continuePrompt [20]
static sqlite3db = 0
static int enableTimer = 0
static char mainPrompt [20]
static const char * modeDescr []
static const char needCsvQuote []
static struct rusage sBegin
static volatile int seenInterrupt = 0
static int stdin_is_interactive = 1
static char zHelp []
static const char zOptions []
static const char * zShellStatic = 0
static char zTimerHelp []


Define Documentation

#define _FILE_OFFSET_BITS   64

Definition at line 26 of file shell.c.

#define _LARGE_FILE   1

Definition at line 24 of file shell.c.

#define _LARGEFILE_SOURCE   1

Definition at line 28 of file shell.c.

#define add_history (  ) 

Definition at line 61 of file shell.c.

Referenced by one_input_line().

#define ArraySize (  )     (int)(sizeof(X)/sizeof(X[0]))

Definition at line 466 of file shell.c.

Referenced by balance_nonroot(), cacheEntryClear(), createMask(), createTableStmt(), do_meta_command(), flagPragma(), getSafetyLevel(), insertCell(), openStatTable(), parseModifier(), pthreadMutexAlloc(), shell_callback(), sqlite3_close(), sqlite3_compileoption_get(), sqlite3_compileoption_used(), sqlite3_db_config(), sqlite3_os_init(), sqlite3_status(), sqlite3AlterFunctions(), sqlite3DropTriggerPtr(), sqlite3FindFunction(), sqlite3FuncDefInsert(), sqlite3InitOne(), sqlite3JoinType(), sqlite3JournalModename(), sqlite3Pragma(), sqlite3RegisterDateTimeFunctions(), sqlite3RegisterGlobalFunctions(), sqlite3ReleaseTempReg(), sqlite3RunVacuum(), sqlite3StatusAdd(), sqlite3StatusSet(), sqlite3StatusValue(), sqlite3VdbeExec(), sqlite3VXPrintf(), unixNextSystemCall(), walMergesort(), and whereClauseInit().

#define BEGIN_TIMER   beginTimer()

Definition at line 128 of file shell.c.

Referenced by process_input().

#define END_TIMER   endTimer()

Definition at line 129 of file shell.c.

Referenced by process_input().

#define HAS_TIMER   1

Definition at line 130 of file shell.c.

Referenced by do_meta_command().

#define IsDigit (  )     isdigit((unsigned char)X)

Definition at line 90 of file shell.c.

Referenced by isNumber().

#define IsSpace (  )     isspace((unsigned char)X)

Definition at line 89 of file shell.c.

Referenced by _all_whitespace(), _is_command_terminator(), do_meta_command(), process_input(), and shell_exec().

#define MODE_Column   1

Definition at line 442 of file shell.c.

Referenced by do_meta_command(), main(), and shell_callback().

#define MODE_Csv   7

Definition at line 448 of file shell.c.

Referenced by do_meta_command(), main(), and shell_callback().

#define MODE_Explain   8

Definition at line 449 of file shell.c.

Referenced by do_meta_command(), and shell_callback().

#define MODE_Html   4

Definition at line 445 of file shell.c.

Referenced by do_meta_command(), main(), and shell_callback().

#define MODE_Insert   5

Definition at line 446 of file shell.c.

Referenced by do_meta_command(), and shell_callback().

#define MODE_Line   0

Definition at line 441 of file shell.c.

Referenced by do_meta_command(), main(), and shell_callback().

#define MODE_List   2

Definition at line 443 of file shell.c.

Referenced by do_meta_command(), main(), main_init(), and shell_callback().

#define MODE_Semi   3

Definition at line 444 of file shell.c.

Referenced by do_meta_command(), and shell_callback().

#define MODE_Tcl   6

Definition at line 447 of file shell.c.

Referenced by do_meta_command(), and shell_callback().

#define read_history (  ) 

Definition at line 62 of file shell.c.

Referenced by main().

#define readline ( p   )     local_getline(p,stdin)

Definition at line 60 of file shell.c.

Referenced by one_input_line().

#define stifle_history (  ) 

Definition at line 64 of file shell.c.

Referenced by main().

#define ToLower (  )     (char)tolower((unsigned char)X)

Definition at line 91 of file shell.c.

Referenced by _is_command_terminator(), booleanValue(), and do_meta_command().

#define UNUSED_PARAMETER ( x   )     (void)(x)

Definition at line 213 of file shell.c.

Referenced by absFunc(), attachFunc(), compileoptiongetFunc(), compileoptionusedFunc(), detachFunc(), dump_callback(), errlogFunc(), full_fsync(), hexFunc(), lengthFunc(), lowerFunc(), memjrnlTruncate(), memjrnlWrite(), minmaxStep(), nocaseCollatingFunc(), nolockCheckReservedLock(), noopMutexAlloc(), noopMutexEnter(), noopMutexFree(), noopMutexLeave(), noopMutexTry(), nullifFunc(), pager_incr_changecounter(), pcache1Init(), pcache1Shutdown(), quoteFunc(), randomBlob(), renameParentFunc(), renameTableFunc(), renameTriggerFunc(), replaceFunc(), resolveAsName(), selectNodeIsConstant(), shellstaticFunc(), sqlite3_release_memory(), sqlite3_wal_autocheckpoint(), sqlite3DeleteIndexSamples(), sqlite3MemInit(), sqlite3MemShutdown(), sqlite3VdbeIdxRowid(), sumStep(), typeofFunc(), unixAccess(), unixCurrentTime(), unixCurrentTimeInt64(), unixDelete(), unixDeviceCharacteristics(), unixDlClose(), unixDlError(), unixDlOpen(), unixDlSym(), unixFullPathname(), unixGetLastError(), unixGetSystemCall(), unixNextSystemCall(), unixRandomness(), unixSectorSize(), unixSetSystemCall(), unixShmBarrier(), unixSleep(), upperFunc(), vdbeCommit(), whereRangeScanEst(), yy_syntax_error(), yyStackOverflow(), and zeroblobFunc().

#define write_history (  ) 

Definition at line 63 of file shell.c.

Referenced by main().


Function Documentation

static int _all_whitespace ( const char *  z  )  [static]

Definition at line 2373 of file shell.c.

References IsSpace.

Referenced by _is_command_terminator(), and process_input().

02373                                          {
02374   for(; *z; z++){
02375     if( IsSpace(z[0]) ) continue;
02376     if( *z=='/' && z[1]=='*' ){
02377       z += 2;
02378       while( *z && (*z!='*' || z[1]!='/') ){ z++; }
02379       if( *z==0 ) return 0;
02380       z++;
02381       continue;
02382     }
02383     if( *z=='-' && z[1]=='-' ){
02384       z += 2;
02385       while( *z && *z!='\n' ){ z++; }
02386       if( *z==0 ) return 1;
02387       continue;
02388     }
02389     return 0;
02390   }
02391   return 1;
02392 }

static int _contains_semicolon ( const char *  z,
int  N 
) [static]

Definition at line 2364 of file shell.c.

Referenced by process_input().

02364                                                     {
02365   int i;
02366   for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
02367   return 0;
02368 }

static int _is_command_terminator ( const char *  zLine  )  [static]

Definition at line 2399 of file shell.c.

References _all_whitespace(), IsSpace, and ToLower.

Referenced by process_input().

02399                                                     {
02400   while( IsSpace(zLine[0]) ){ zLine++; };
02401   if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
02402     return 1;  /* Oracle */
02403   }
02404   if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
02405          && _all_whitespace(&zLine[2]) ){
02406     return 1;  /* SQL Server */
02407   }
02408   return 0;
02409 }

static int _is_complete ( char *  zSql,
int  nSql 
) [static]

Definition at line 2415 of file shell.c.

References sqlite3_complete.

Referenced by process_input().

02415                                              {
02416   int rc;
02417   if( zSql==0 ) return 1;
02418   zSql[nSql] = ';';
02419   zSql[nSql+1] = 0;
02420   rc = sqlite3_complete(zSql);
02421   zSql[nSql] = 0;
02422   return rc;
02423 }

static char* appendText ( char *  zIn,
char const *  zAppend,
char  quote 
) [static]

Definition at line 898 of file shell.c.

References memcpy(), and strlen30().

Referenced by dump_callback().

00898                                                                    {
00899   int len;
00900   int i;
00901   int nAppend = strlen30(zAppend);
00902   int nIn = (zIn?strlen30(zIn):0);
00903 
00904   len = nAppend+nIn+1;
00905   if( quote ){
00906     len += 2;
00907     for(i=0; i<nAppend; i++){
00908       if( zAppend[i]==quote ) len++;
00909     }
00910   }
00911 
00912   zIn = (char *)realloc(zIn, len);
00913   if( !zIn ){
00914     return 0;
00915   }
00916 
00917   if( quote ){
00918     char *zCsr = &zIn[nIn];
00919     *zCsr++ = quote;
00920     for(i=0; i<nAppend; i++){
00921       *zCsr++ = zAppend[i];
00922       if( zAppend[i]==quote ) *zCsr++ = quote;
00923     }
00924     *zCsr++ = quote;
00925     *zCsr++ = '\0';
00926     assert( (zCsr-zIn)==len );
00927   }else{
00928     memcpy(&zIn[nIn], zAppend, nAppend);
00929     zIn[len-1] = '\0';
00930   }
00931 
00932   return zIn;
00933 }

static void beginTimer ( void   )  [static]

Definition at line 103 of file shell.c.

References enableTimer, and sBegin.

00103                             {
00104   if( enableTimer ){
00105     getrusage(RUSAGE_SELF, &sBegin);
00106   }
00107 }

static int booleanValue ( char *  zArg  )  [static]

Definition at line 1473 of file shell.c.

References ToLower.

Referenced by do_meta_command().

01473                                    {
01474   int val = atoi(zArg);
01475   int j;
01476   for(j=0; zArg[j]; j++){
01477     zArg[j] = ToLower(zArg[j]);
01478   }
01479   if( strcmp(zArg,"on")==0 ){
01480     val = 1;
01481   }else if( strcmp(zArg,"yes")==0 ){
01482     val = 1;
01483   }
01484   return val;
01485 }

static int callback ( void *  pArg,
int  nArg,
char **  azArg,
char **  azCol 
) [static]

Definition at line 847 of file shell.c.

References NULL, and shell_callback().

Referenced by do_meta_command().

00847                                                                      {
00848   /* since we don't have type info, call the shell_callback with a NULL value */
00849   return shell_callback(pArg, nArg, azArg, azCol, NULL);
00850 }

static int display_stats ( sqlite3 db,
struct callback_data pArg,
int  bReset 
) [static]

Definition at line 990 of file shell.c.

References callback_data::out, callback_data::pStmt, sqlite3_db_status, sqlite3_status, sqlite3_stmt_status, SQLITE_DBSTATUS_CACHE_HIT, SQLITE_DBSTATUS_CACHE_MISS, SQLITE_DBSTATUS_CACHE_USED, SQLITE_DBSTATUS_LOOKASIDE_HIT, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, SQLITE_DBSTATUS_LOOKASIDE_USED, SQLITE_DBSTATUS_SCHEMA_USED, SQLITE_DBSTATUS_STMT_USED, SQLITE_STATUS_MALLOC_COUNT, SQLITE_STATUS_MALLOC_SIZE, SQLITE_STATUS_MEMORY_USED, SQLITE_STATUS_PAGECACHE_OVERFLOW, SQLITE_STATUS_PAGECACHE_SIZE, SQLITE_STATUS_PARSER_STACK, SQLITE_STATUS_SCRATCH_OVERFLOW, SQLITE_STATUS_SCRATCH_SIZE, SQLITE_STMTSTATUS_AUTOINDEX, SQLITE_STMTSTATUS_FULLSCAN_STEP, and SQLITE_STMTSTATUS_SORT.

Referenced by shell_exec().

00994  {
00995   int iCur;
00996   int iHiwtr;
00997 
00998   if( pArg && pArg->out ){
00999     
01000     iHiwtr = iCur = -1;
01001     sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
01002     fprintf(pArg->out, "Memory Used:                         %d (max %d) bytes\n", iCur, iHiwtr);
01003     iHiwtr = iCur = -1;
01004     sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
01005     fprintf(pArg->out, "Number of Outstanding Allocations:   %d (max %d)\n", iCur, iHiwtr);
01006 /*
01007 ** Not currently used by the CLI.
01008 **    iHiwtr = iCur = -1;
01009 **    sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
01010 **    fprintf(pArg->out, "Number of Pcache Pages Used:         %d (max %d) pages\n", iCur, iHiwtr);
01011 */
01012     iHiwtr = iCur = -1;
01013     sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
01014     fprintf(pArg->out, "Number of Pcache Overflow Bytes:     %d (max %d) bytes\n", iCur, iHiwtr);
01015 /*
01016 ** Not currently used by the CLI.
01017 **    iHiwtr = iCur = -1;
01018 **    sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
01019 **    fprintf(pArg->out, "Number of Scratch Allocations Used:  %d (max %d)\n", iCur, iHiwtr);
01020 */
01021     iHiwtr = iCur = -1;
01022     sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
01023     fprintf(pArg->out, "Number of Scratch Overflow Bytes:    %d (max %d) bytes\n", iCur, iHiwtr);
01024     iHiwtr = iCur = -1;
01025     sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
01026     fprintf(pArg->out, "Largest Allocation:                  %d bytes\n", iHiwtr);
01027     iHiwtr = iCur = -1;
01028     sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset);
01029     fprintf(pArg->out, "Largest Pcache Allocation:           %d bytes\n", iHiwtr);
01030     iHiwtr = iCur = -1;
01031     sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset);
01032     fprintf(pArg->out, "Largest Scratch Allocation:          %d bytes\n", iHiwtr);
01033 #ifdef YYTRACKMAXSTACKDEPTH
01034     iHiwtr = iCur = -1;
01035     sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset);
01036     fprintf(pArg->out, "Deepest Parser Stack:                %d (max %d)\n", iCur, iHiwtr);
01037 #endif
01038   }
01039 
01040   if( pArg && pArg->out && db ){
01041     iHiwtr = iCur = -1;
01042     sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
01043     fprintf(pArg->out, "Lookaside Slots Used:                %d (max %d)\n", iCur, iHiwtr);
01044     sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
01045     fprintf(pArg->out, "Successful lookaside attempts:       %d\n", iHiwtr);
01046     sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
01047     fprintf(pArg->out, "Lookaside failures due to size:      %d\n", iHiwtr);
01048     sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
01049     fprintf(pArg->out, "Lookaside failures due to OOM:       %d\n", iHiwtr);
01050     iHiwtr = iCur = -1;
01051     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
01052     fprintf(pArg->out, "Pager Heap Usage:                    %d bytes\n", iCur);    iHiwtr = iCur = -1;
01053     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
01054     fprintf(pArg->out, "Page cache hits:                     %d\n", iCur);
01055     iHiwtr = iCur = -1;
01056     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
01057     fprintf(pArg->out, "Page cache misses:                   %d\n", iCur); 
01058     iHiwtr = iCur = -1;
01059     sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
01060     fprintf(pArg->out, "Schema Heap Usage:                   %d bytes\n", iCur); 
01061     iHiwtr = iCur = -1;
01062     sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
01063     fprintf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n", iCur); 
01064   }
01065 
01066   if( pArg && pArg->out && db && pArg->pStmt ){
01067     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, bReset);
01068     fprintf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
01069     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
01070     fprintf(pArg->out, "Sort Operations:                     %d\n", iCur);
01071     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX, bReset);
01072     fprintf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
01073   }
01074 
01075   return 0;
01076 }

static int do_meta_command ( char *  zLine,
struct callback_data p 
) [static]

Definition at line 1493 of file shell.c.

References ArraySize, booleanValue(), callback(), callback_data::cnt, previous_mode_data::colWidth, callback_data::colWidth, callback_data::db, callback_data::echoOn, enableTimer, callback_data::explainPrev, HAS_TIMER, IsSpace, lineno, local_getline(), memcpy(), previous_mode_data::mode, callback_data::mode, MODE_Column, MODE_Csv, MODE_Explain, MODE_Html, MODE_Insert, MODE_Line, MODE_List, MODE_Semi, MODE_Tcl, callback_data::nErr, NULL, callback_data::nullvalue, open_db(), callback_data::out, callback_data::outfile, output_c_string(), callback_data::pLog, process_input(), callback_data::pStmt, resolve_backslashes(), run_schema_dump_query(), run_table_dump_query(), callback_data::separator, set_table_name(), previous_mode_data::showHeader, callback_data::showHeader, sqlite3_backup_finish, sqlite3_backup_init, sqlite3_backup_step, sqlite3_bind_text, sqlite3_busy_timeout, sqlite3_close, sqlite3_column_count, sqlite3_errmsg, sqlite3_exec, sqlite3_finalize, sqlite3_free, sqlite3_free_table, sqlite3_get_table, sqlite3_libversion, sqlite3_load_extension(), sqlite3_mprintf, sqlite3_open, sqlite3_prepare, sqlite3_reset, sqlite3_sleep, sqlite3_snprintf, sqlite3_sourceid, sqlite3_step, sqlite3_test_control, SQLITE_BUSY, SQLITE_DONE, SQLITE_LOCKED, SQLITE_OK, SQLITE_STATIC, SQLITE_TESTCTRL_ALWAYS, SQLITE_TESTCTRL_ASSERT, SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, SQLITE_TESTCTRL_BITVEC_TEST, SQLITE_TESTCTRL_FAULT_INSTALL, SQLITE_TESTCTRL_FIRST, SQLITE_TESTCTRL_ISKEYWORD, SQLITE_TESTCTRL_LAST, SQLITE_TESTCTRL_OPTIMIZATIONS, SQLITE_TESTCTRL_PENDING_BYTE, SQLITE_TESTCTRL_PGHDRSZ, SQLITE_TESTCTRL_PRNG_RESET, SQLITE_TESTCTRL_PRNG_RESTORE, SQLITE_TESTCTRL_PRNG_SAVE, SQLITE_TESTCTRL_RESERVE, SQLITE_TESTCTRL_SCRATCHMALLOC, callback_data::statsOn, strlen30(), ToLower, previous_mode_data::valid, and callback_data::writableSchema.

Referenced by main(), and process_input().

01493                                                                 {
01494   int i = 1;
01495   int nArg = 0;
01496   int n, c;
01497   int rc = 0;
01498   char *azArg[50];
01499 
01500   /* Parse the input line into tokens.
01501   */
01502   while( zLine[i] && nArg<ArraySize(azArg) ){
01503     while( IsSpace(zLine[i]) ){ i++; }
01504     if( zLine[i]==0 ) break;
01505     if( zLine[i]=='\'' || zLine[i]=='"' ){
01506       int delim = zLine[i++];
01507       azArg[nArg++] = &zLine[i];
01508       while( zLine[i] && zLine[i]!=delim ){ i++; }
01509       if( zLine[i]==delim ){
01510         zLine[i++] = 0;
01511       }
01512       if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
01513     }else{
01514       azArg[nArg++] = &zLine[i];
01515       while( zLine[i] && !IsSpace(zLine[i]) ){ i++; }
01516       if( zLine[i] ) zLine[i++] = 0;
01517       resolve_backslashes(azArg[nArg-1]);
01518     }
01519   }
01520 
01521   /* Process the input line.
01522   */
01523   if( nArg==0 ) return 0; /* no tokens, no error */
01524   n = strlen30(azArg[0]);
01525   c = azArg[0][0];
01526   if( c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0 && nArg>1 && nArg<4){
01527     const char *zDestFile;
01528     const char *zDb;
01529     sqlite3 *pDest;
01530     sqlite3_backup *pBackup;
01531     if( nArg==2 ){
01532       zDestFile = azArg[1];
01533       zDb = "main";
01534     }else{
01535       zDestFile = azArg[2];
01536       zDb = azArg[1];
01537     }
01538     rc = sqlite3_open(zDestFile, &pDest);
01539     if( rc!=SQLITE_OK ){
01540       fprintf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
01541       sqlite3_close(pDest);
01542       return 1;
01543     }
01544     open_db(p);
01545     pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
01546     if( pBackup==0 ){
01547       fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
01548       sqlite3_close(pDest);
01549       return 1;
01550     }
01551     while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
01552     sqlite3_backup_finish(pBackup);
01553     if( rc==SQLITE_DONE ){
01554       rc = 0;
01555     }else{
01556       fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
01557       rc = 1;
01558     }
01559     sqlite3_close(pDest);
01560   }else
01561 
01562   if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 && nArg>1 && nArg<3 ){
01563     bail_on_error = booleanValue(azArg[1]);
01564   }else
01565 
01566   if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 && nArg==1 ){
01567     struct callback_data data;
01568     char *zErrMsg = 0;
01569     open_db(p);
01570     memcpy(&data, p, sizeof(data));
01571     data.showHeader = 1;
01572     data.mode = MODE_Column;
01573     data.colWidth[0] = 3;
01574     data.colWidth[1] = 15;
01575     data.colWidth[2] = 58;
01576     data.cnt = 0;
01577     sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
01578     if( zErrMsg ){
01579       fprintf(stderr,"Error: %s\n", zErrMsg);
01580       sqlite3_free(zErrMsg);
01581       rc = 1;
01582     }
01583   }else
01584 
01585   if( c=='d' && strncmp(azArg[0], "dump", n)==0 && nArg<3 ){
01586     open_db(p);
01587     /* When playing back a "dump", the content might appear in an order
01588     ** which causes immediate foreign key constraints to be violated.
01589     ** So disable foreign-key constraint enforcement to prevent problems. */
01590     fprintf(p->out, "PRAGMA foreign_keys=OFF;\n");
01591     fprintf(p->out, "BEGIN TRANSACTION;\n");
01592     p->writableSchema = 0;
01593     sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
01594     p->nErr = 0;
01595     if( nArg==1 ){
01596       run_schema_dump_query(p, 
01597         "SELECT name, type, sql FROM sqlite_master "
01598         "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
01599       );
01600       run_schema_dump_query(p, 
01601         "SELECT name, type, sql FROM sqlite_master "
01602         "WHERE name=='sqlite_sequence'"
01603       );
01604       run_table_dump_query(p,
01605         "SELECT sql FROM sqlite_master "
01606         "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
01607       );
01608     }else{
01609       int i;
01610       for(i=1; i<nArg; i++){
01611         zShellStatic = azArg[i];
01612         run_schema_dump_query(p,
01613           "SELECT name, type, sql FROM sqlite_master "
01614           "WHERE tbl_name LIKE shellstatic() AND type=='table'"
01615           "  AND sql NOT NULL");
01616         run_table_dump_query(p,
01617           "SELECT sql FROM sqlite_master "
01618           "WHERE sql NOT NULL"
01619           "  AND type IN ('index','trigger','view')"
01620           "  AND tbl_name LIKE shellstatic()", 0
01621         );
01622         zShellStatic = 0;
01623       }
01624     }
01625     if( p->writableSchema ){
01626       fprintf(p->out, "PRAGMA writable_schema=OFF;\n");
01627       p->writableSchema = 0;
01628     }
01629     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
01630     sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
01631     fprintf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
01632   }else
01633 
01634   if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 && nArg<3 ){
01635     p->echoOn = booleanValue(azArg[1]);
01636   }else
01637 
01638   if( c=='e' && strncmp(azArg[0], "exit", n)==0  && nArg==1 ){
01639     rc = 2;
01640   }else
01641 
01642   if( c=='e' && strncmp(azArg[0], "explain", n)==0 && nArg<3 ){
01643     int val = nArg>=2 ? booleanValue(azArg[1]) : 1;
01644     if(val == 1) {
01645       if(!p->explainPrev.valid) {
01646         p->explainPrev.valid = 1;
01647         p->explainPrev.mode = p->mode;
01648         p->explainPrev.showHeader = p->showHeader;
01649         memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
01650       }
01651       /* We could put this code under the !p->explainValid
01652       ** condition so that it does not execute if we are already in
01653       ** explain mode. However, always executing it allows us an easy
01654       ** was to reset to explain mode in case the user previously
01655       ** did an .explain followed by a .width, .mode or .header
01656       ** command.
01657       */
01658       p->mode = MODE_Explain;
01659       p->showHeader = 1;
01660       memset(p->colWidth,0,ArraySize(p->colWidth));
01661       p->colWidth[0] = 4;                  /* addr */
01662       p->colWidth[1] = 13;                 /* opcode */
01663       p->colWidth[2] = 4;                  /* P1 */
01664       p->colWidth[3] = 4;                  /* P2 */
01665       p->colWidth[4] = 4;                  /* P3 */
01666       p->colWidth[5] = 13;                 /* P4 */
01667       p->colWidth[6] = 2;                  /* P5 */
01668       p->colWidth[7] = 13;                  /* Comment */
01669     }else if (p->explainPrev.valid) {
01670       p->explainPrev.valid = 0;
01671       p->mode = p->explainPrev.mode;
01672       p->showHeader = p->explainPrev.showHeader;
01673       memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
01674     }
01675   }else
01676 
01677   if( c=='h' && (strncmp(azArg[0], "header", n)==0 ||
01678                  strncmp(azArg[0], "headers", n)==0) && nArg>1 && nArg<3 ){
01679     p->showHeader = booleanValue(azArg[1]);
01680   }else
01681 
01682   if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
01683     fprintf(stderr,"%s",zHelp);
01684     if( HAS_TIMER ){
01685       fprintf(stderr,"%s",zTimerHelp);
01686     }
01687   }else
01688 
01689   if( c=='i' && strncmp(azArg[0], "import", n)==0 && nArg==3 ){
01690     char *zTable = azArg[2];    /* Insert data into this table */
01691     char *zFile = azArg[1];     /* The file from which to extract data */
01692     sqlite3_stmt *pStmt = NULL; /* A statement */
01693     int nCol;                   /* Number of columns in the table */
01694     int nByte;                  /* Number of bytes in an SQL string */
01695     int i, j;                   /* Loop counters */
01696     int nSep;                   /* Number of bytes in p->separator[] */
01697     char *zSql;                 /* An SQL statement */
01698     char *zLine;                /* A single line of input from the file */
01699     char **azCol;               /* zLine[] broken up into columns */
01700     char *zCommit;              /* How to commit changes */   
01701     FILE *in;                   /* The input file */
01702     int lineno = 0;             /* Line number of input file */
01703 
01704     open_db(p);
01705     nSep = strlen30(p->separator);
01706     if( nSep==0 ){
01707       fprintf(stderr, "Error: non-null separator required for import\n");
01708       return 1;
01709     }
01710     zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
01711     if( zSql==0 ){
01712       fprintf(stderr, "Error: out of memory\n");
01713       return 1;
01714     }
01715     nByte = strlen30(zSql);
01716     rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
01717     sqlite3_free(zSql);
01718     if( rc ){
01719       if (pStmt) sqlite3_finalize(pStmt);
01720       fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
01721       return 1;
01722     }
01723     nCol = sqlite3_column_count(pStmt);
01724     sqlite3_finalize(pStmt);
01725     pStmt = 0;
01726     if( nCol==0 ) return 0; /* no columns, no error */
01727     zSql = malloc( nByte + 20 + nCol*2 );
01728     if( zSql==0 ){
01729       fprintf(stderr, "Error: out of memory\n");
01730       return 1;
01731     }
01732     sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zTable);
01733     j = strlen30(zSql);
01734     for(i=1; i<nCol; i++){
01735       zSql[j++] = ',';
01736       zSql[j++] = '?';
01737     }
01738     zSql[j++] = ')';
01739     zSql[j] = 0;
01740     rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
01741     free(zSql);
01742     if( rc ){
01743       fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
01744       if (pStmt) sqlite3_finalize(pStmt);
01745       return 1;
01746     }
01747     in = fopen(zFile, "rb");
01748     if( in==0 ){
01749       fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);
01750       sqlite3_finalize(pStmt);
01751       return 1;
01752     }
01753     azCol = malloc( sizeof(azCol[0])*(nCol+1) );
01754     if( azCol==0 ){
01755       fprintf(stderr, "Error: out of memory\n");
01756       fclose(in);
01757       sqlite3_finalize(pStmt);
01758       return 1;
01759     }
01760     sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
01761     zCommit = "COMMIT";
01762     while( (zLine = local_getline(0, in))!=0 ){
01763       char *z;
01764       lineno++;
01765       azCol[0] = zLine;
01766       for(i=0, z=zLine; *z && *z!='\n' && *z!='\r'; z++){
01767         if( *z==p->separator[0] && strncmp(z, p->separator, nSep)==0 ){
01768           *z = 0;
01769           i++;
01770           if( i<nCol ){
01771             azCol[i] = &z[nSep];
01772             z += nSep-1;
01773           }
01774         }
01775       } /* end for */
01776       *z = 0;
01777       if( i+1!=nCol ){
01778         fprintf(stderr,
01779                 "Error: %s line %d: expected %d columns of data but found %d\n",
01780                 zFile, lineno, nCol, i+1);
01781         zCommit = "ROLLBACK";
01782         free(zLine);
01783         rc = 1;
01784         break; /* from while */
01785       }
01786       for(i=0; i<nCol; i++){
01787         sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
01788       }
01789       sqlite3_step(pStmt);
01790       rc = sqlite3_reset(pStmt);
01791       free(zLine);
01792       if( rc!=SQLITE_OK ){
01793         fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
01794         zCommit = "ROLLBACK";
01795         rc = 1;
01796         break; /* from while */
01797       }
01798     } /* end while */
01799     free(azCol);
01800     fclose(in);
01801     sqlite3_finalize(pStmt);
01802     sqlite3_exec(p->db, zCommit, 0, 0, 0);
01803   }else
01804 
01805   if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg<3 ){
01806     struct callback_data data;
01807     char *zErrMsg = 0;
01808     open_db(p);
01809     memcpy(&data, p, sizeof(data));
01810     data.showHeader = 0;
01811     data.mode = MODE_List;
01812     if( nArg==1 ){
01813       rc = sqlite3_exec(p->db,
01814         "SELECT name FROM sqlite_master "
01815         "WHERE type='index' AND name NOT LIKE 'sqlite_%' "
01816         "UNION ALL "
01817         "SELECT name FROM sqlite_temp_master "
01818         "WHERE type='index' "
01819         "ORDER BY 1",
01820         callback, &data, &zErrMsg
01821       );
01822     }else{
01823       zShellStatic = azArg[1];
01824       rc = sqlite3_exec(p->db,
01825         "SELECT name FROM sqlite_master "
01826         "WHERE type='index' AND tbl_name LIKE shellstatic() "
01827         "UNION ALL "
01828         "SELECT name FROM sqlite_temp_master "
01829         "WHERE type='index' AND tbl_name LIKE shellstatic() "
01830         "ORDER BY 1",
01831         callback, &data, &zErrMsg
01832       );
01833       zShellStatic = 0;
01834     }
01835     if( zErrMsg ){
01836       fprintf(stderr,"Error: %s\n", zErrMsg);
01837       sqlite3_free(zErrMsg);
01838       rc = 1;
01839     }else if( rc != SQLITE_OK ){
01840       fprintf(stderr,"Error: querying sqlite_master and sqlite_temp_master\n");
01841       rc = 1;
01842     }
01843   }else
01844 
01845 #ifdef SQLITE_ENABLE_IOTRACE
01846   if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
01847     extern void (*sqlite3IoTrace)(const char*, ...);
01848     if( iotrace && iotrace!=stdout ) fclose(iotrace);
01849     iotrace = 0;
01850     if( nArg<2 ){
01851       sqlite3IoTrace = 0;
01852     }else if( strcmp(azArg[1], "-")==0 ){
01853       sqlite3IoTrace = iotracePrintf;
01854       iotrace = stdout;
01855     }else{
01856       iotrace = fopen(azArg[1], "w");
01857       if( iotrace==0 ){
01858         fprintf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
01859         sqlite3IoTrace = 0;
01860         rc = 1;
01861       }else{
01862         sqlite3IoTrace = iotracePrintf;
01863       }
01864     }
01865   }else
01866 #endif
01867 
01868 #ifndef SQLITE_OMIT_LOAD_EXTENSION
01869   if( c=='l' && strncmp(azArg[0], "load", n)==0 && nArg>=2 ){
01870     const char *zFile, *zProc;
01871     char *zErrMsg = 0;
01872     zFile = azArg[1];
01873     zProc = nArg>=3 ? azArg[2] : 0;
01874     open_db(p);
01875     rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
01876     if( rc!=SQLITE_OK ){
01877       fprintf(stderr, "Error: %s\n", zErrMsg);
01878       sqlite3_free(zErrMsg);
01879       rc = 1;
01880     }
01881   }else
01882 #endif
01883 
01884   if( c=='l' && strncmp(azArg[0], "log", n)==0 && nArg>=2 ){
01885     const char *zFile = azArg[1];
01886     if( p->pLog && p->pLog!=stdout && p->pLog!=stderr ){
01887       fclose(p->pLog);
01888       p->pLog = 0;
01889     }
01890     if( strcmp(zFile,"stdout")==0 ){
01891       p->pLog = stdout;
01892     }else if( strcmp(zFile, "stderr")==0 ){
01893       p->pLog = stderr;
01894     }else if( strcmp(zFile, "off")==0 ){
01895       p->pLog = 0;
01896     }else{
01897       p->pLog = fopen(zFile, "w");
01898       if( p->pLog==0 ){
01899         fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);
01900       }
01901     }
01902   }else
01903 
01904   if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg==2 ){
01905     int n2 = strlen30(azArg[1]);
01906     if( (n2==4 && strncmp(azArg[1],"line",n2)==0)
01907         ||
01908         (n2==5 && strncmp(azArg[1],"lines",n2)==0) ){
01909       p->mode = MODE_Line;
01910     }else if( (n2==6 && strncmp(azArg[1],"column",n2)==0)
01911               ||
01912               (n2==7 && strncmp(azArg[1],"columns",n2)==0) ){
01913       p->mode = MODE_Column;
01914     }else if( n2==4 && strncmp(azArg[1],"list",n2)==0 ){
01915       p->mode = MODE_List;
01916     }else if( n2==4 && strncmp(azArg[1],"html",n2)==0 ){
01917       p->mode = MODE_Html;
01918     }else if( n2==3 && strncmp(azArg[1],"tcl",n2)==0 ){
01919       p->mode = MODE_Tcl;
01920     }else if( n2==3 && strncmp(azArg[1],"csv",n2)==0 ){
01921       p->mode = MODE_Csv;
01922       sqlite3_snprintf(sizeof(p->separator), p->separator, ",");
01923     }else if( n2==4 && strncmp(azArg[1],"tabs",n2)==0 ){
01924       p->mode = MODE_List;
01925       sqlite3_snprintf(sizeof(p->separator), p->separator, "\t");
01926     }else if( n2==6 && strncmp(azArg[1],"insert",n2)==0 ){
01927       p->mode = MODE_Insert;
01928       set_table_name(p, "table");
01929     }else {
01930       fprintf(stderr,"Error: mode should be one of: "
01931          "column csv html insert line list tabs tcl\n");
01932       rc = 1;
01933     }
01934   }else
01935 
01936   if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg==3 ){
01937     int n2 = strlen30(azArg[1]);
01938     if( n2==6 && strncmp(azArg[1],"insert",n2)==0 ){
01939       p->mode = MODE_Insert;
01940       set_table_name(p, azArg[2]);
01941     }else {
01942       fprintf(stderr, "Error: invalid arguments: "
01943         " \"%s\". Enter \".help\" for help\n", azArg[2]);
01944       rc = 1;
01945     }
01946   }else
01947 
01948   if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
01949     sqlite3_snprintf(sizeof(p->nullvalue), p->nullvalue,
01950                      "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
01951   }else
01952 
01953   if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
01954     if( p->out!=stdout ){
01955       fclose(p->out);
01956     }
01957     if( strcmp(azArg[1],"stdout")==0 ){
01958       p->out = stdout;
01959       sqlite3_snprintf(sizeof(p->outfile), p->outfile, "stdout");
01960     }else{
01961       p->out = fopen(azArg[1], "wb");
01962       if( p->out==0 ){
01963         fprintf(stderr,"Error: cannot write to \"%s\"\n", azArg[1]);
01964         p->out = stdout;
01965         rc = 1;
01966       } else {
01967          sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
01968       }
01969     }
01970   }else
01971 
01972   if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
01973     if( nArg >= 2) {
01974       strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
01975     }
01976     if( nArg >= 3) {
01977       strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
01978     }
01979   }else
01980 
01981   if( c=='q' && strncmp(azArg[0], "quit", n)==0 && nArg==1 ){
01982     rc = 2;
01983   }else
01984 
01985   if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
01986     FILE *alt = fopen(azArg[1], "rb");
01987     if( alt==0 ){
01988       fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
01989       rc = 1;
01990     }else{
01991       rc = process_input(p, alt);
01992       fclose(alt);
01993     }
01994   }else
01995 
01996   if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 && nArg>1 && nArg<4){
01997     const char *zSrcFile;
01998     const char *zDb;
01999     sqlite3 *pSrc;
02000     sqlite3_backup *pBackup;
02001     int nTimeout = 0;
02002 
02003     if( nArg==2 ){
02004       zSrcFile = azArg[1];
02005       zDb = "main";
02006     }else{
02007       zSrcFile = azArg[2];
02008       zDb = azArg[1];
02009     }
02010     rc = sqlite3_open(zSrcFile, &pSrc);
02011     if( rc!=SQLITE_OK ){
02012       fprintf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
02013       sqlite3_close(pSrc);
02014       return 1;
02015     }
02016     open_db(p);
02017     pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
02018     if( pBackup==0 ){
02019       fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
02020       sqlite3_close(pSrc);
02021       return 1;
02022     }
02023     while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
02024           || rc==SQLITE_BUSY  ){
02025       if( rc==SQLITE_BUSY ){
02026         if( nTimeout++ >= 3 ) break;
02027         sqlite3_sleep(100);
02028       }
02029     }
02030     sqlite3_backup_finish(pBackup);
02031     if( rc==SQLITE_DONE ){
02032       rc = 0;
02033     }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
02034       fprintf(stderr, "Error: source database is busy\n");
02035       rc = 1;
02036     }else{
02037       fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
02038       rc = 1;
02039     }
02040     sqlite3_close(pSrc);
02041   }else
02042 
02043   if( c=='s' && strncmp(azArg[0], "schema", n)==0 && nArg<3 ){
02044     struct callback_data data;
02045     char *zErrMsg = 0;
02046     open_db(p);
02047     memcpy(&data, p, sizeof(data));
02048     data.showHeader = 0;
02049     data.mode = MODE_Semi;
02050     if( nArg>1 ){
02051       int i;
02052       for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]);
02053       if( strcmp(azArg[1],"sqlite_master")==0 ){
02054         char *new_argv[2], *new_colv[2];
02055         new_argv[0] = "CREATE TABLE sqlite_master (\n"
02056                       "  type text,\n"
02057                       "  name text,\n"
02058                       "  tbl_name text,\n"
02059                       "  rootpage integer,\n"
02060                       "  sql text\n"
02061                       ")";
02062         new_argv[1] = 0;
02063         new_colv[0] = "sql";
02064         new_colv[1] = 0;
02065         callback(&data, 1, new_argv, new_colv);
02066         rc = SQLITE_OK;
02067       }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){
02068         char *new_argv[2], *new_colv[2];
02069         new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
02070                       "  type text,\n"
02071                       "  name text,\n"
02072                       "  tbl_name text,\n"
02073                       "  rootpage integer,\n"
02074                       "  sql text\n"
02075                       ")";
02076         new_argv[1] = 0;
02077         new_colv[0] = "sql";
02078         new_colv[1] = 0;
02079         callback(&data, 1, new_argv, new_colv);
02080         rc = SQLITE_OK;
02081       }else{
02082         zShellStatic = azArg[1];
02083         rc = sqlite3_exec(p->db,
02084           "SELECT sql FROM "
02085           "  (SELECT sql sql, type type, tbl_name tbl_name, name name"
02086           "     FROM sqlite_master UNION ALL"
02087           "   SELECT sql, type, tbl_name, name FROM sqlite_temp_master) "
02088           "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOTNULL "
02089           "ORDER BY substr(type,2,1), name",
02090           callback, &data, &zErrMsg);
02091         zShellStatic = 0;
02092       }
02093     }else{
02094       rc = sqlite3_exec(p->db,
02095          "SELECT sql FROM "
02096          "  (SELECT sql sql, type type, tbl_name tbl_name, name name"
02097          "     FROM sqlite_master UNION ALL"
02098          "   SELECT sql, type, tbl_name, name FROM sqlite_temp_master) "
02099          "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
02100          "ORDER BY substr(type,2,1), name",
02101          callback, &data, &zErrMsg
02102       );
02103     }
02104     if( zErrMsg ){
02105       fprintf(stderr,"Error: %s\n", zErrMsg);
02106       sqlite3_free(zErrMsg);
02107       rc = 1;
02108     }else if( rc != SQLITE_OK ){
02109       fprintf(stderr,"Error: querying schema information\n");
02110       rc = 1;
02111     }else{
02112       rc = 0;
02113     }
02114   }else
02115 
02116   if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
02117     sqlite3_snprintf(sizeof(p->separator), p->separator,
02118                      "%.*s", (int)sizeof(p->separator)-1, azArg[1]);
02119   }else
02120 
02121   if( c=='s' && strncmp(azArg[0], "show", n)==0 && nArg==1 ){
02122     int i;
02123     fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
02124     fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
02125     fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
02126     fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
02127     fprintf(p->out,"%9.9s: ", "nullvalue");
02128       output_c_string(p->out, p->nullvalue);
02129       fprintf(p->out, "\n");
02130     fprintf(p->out,"%9.9s: %s\n","output",
02131             strlen30(p->outfile) ? p->outfile : "stdout");
02132     fprintf(p->out,"%9.9s: ", "separator");
02133       output_c_string(p->out, p->separator);
02134       fprintf(p->out, "\n");
02135     fprintf(p->out,"%9.9s: %s\n","stats", p->statsOn ? "on" : "off");
02136     fprintf(p->out,"%9.9s: ","width");
02137     for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
02138       fprintf(p->out,"%d ",p->colWidth[i]);
02139     }
02140     fprintf(p->out,"\n");
02141   }else
02142 
02143   if( c=='s' && strncmp(azArg[0], "stats", n)==0 && nArg>1 && nArg<3 ){
02144     p->statsOn = booleanValue(azArg[1]);
02145   }else
02146 
02147   if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 && nArg<3 ){
02148     char **azResult;
02149     int nRow;
02150     char *zErrMsg;
02151     open_db(p);
02152     if( nArg==1 ){
02153       rc = sqlite3_get_table(p->db,
02154         "SELECT name FROM sqlite_master "
02155         "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' "
02156         "UNION ALL "
02157         "SELECT name FROM sqlite_temp_master "
02158         "WHERE type IN ('table','view') "
02159         "ORDER BY 1",
02160         &azResult, &nRow, 0, &zErrMsg
02161       );
02162     }else{
02163       zShellStatic = azArg[1];
02164       rc = sqlite3_get_table(p->db,
02165         "SELECT name FROM sqlite_master "
02166         "WHERE type IN ('table','view') AND name LIKE shellstatic() "
02167         "UNION ALL "
02168         "SELECT name FROM sqlite_temp_master "
02169         "WHERE type IN ('table','view') AND name LIKE shellstatic() "
02170         "ORDER BY 1",
02171         &azResult, &nRow, 0, &zErrMsg
02172       );
02173       zShellStatic = 0;
02174     }
02175     if( zErrMsg ){
02176       fprintf(stderr,"Error: %s\n", zErrMsg);
02177       sqlite3_free(zErrMsg);
02178       rc = 1;
02179     }else if( rc != SQLITE_OK ){
02180       fprintf(stderr,"Error: querying sqlite_master and sqlite_temp_master\n");
02181       rc = 1;
02182     }else{
02183       int len, maxlen = 0;
02184       int i, j;
02185       int nPrintCol, nPrintRow;
02186       for(i=1; i<=nRow; i++){
02187         if( azResult[i]==0 ) continue;
02188         len = strlen30(azResult[i]);
02189         if( len>maxlen ) maxlen = len;
02190       }
02191       nPrintCol = 80/(maxlen+2);
02192       if( nPrintCol<1 ) nPrintCol = 1;
02193       nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
02194       for(i=0; i<nPrintRow; i++){
02195         for(j=i+1; j<=nRow; j+=nPrintRow){
02196           char *zSp = j<=nPrintRow ? "" : "  ";
02197           printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
02198         }
02199         printf("\n");
02200       }
02201     }
02202     sqlite3_free_table(azResult);
02203   }else
02204 
02205   if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
02206     static const struct {
02207        const char *zCtrlName;   /* Name of a test-control option */
02208        int ctrlCode;            /* Integer code for that option */
02209     } aCtrl[] = {
02210       { "prng_save",             SQLITE_TESTCTRL_PRNG_SAVE              },
02211       { "prng_restore",          SQLITE_TESTCTRL_PRNG_RESTORE           },
02212       { "prng_reset",            SQLITE_TESTCTRL_PRNG_RESET             },
02213       { "bitvec_test",           SQLITE_TESTCTRL_BITVEC_TEST            },
02214       { "fault_install",         SQLITE_TESTCTRL_FAULT_INSTALL          },
02215       { "benign_malloc_hooks",   SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS    },
02216       { "pending_byte",          SQLITE_TESTCTRL_PENDING_BYTE           },
02217       { "assert",                SQLITE_TESTCTRL_ASSERT                 },
02218       { "always",                SQLITE_TESTCTRL_ALWAYS                 },
02219       { "reserve",               SQLITE_TESTCTRL_RESERVE                },
02220       { "optimizations",         SQLITE_TESTCTRL_OPTIMIZATIONS          },
02221       { "iskeyword",             SQLITE_TESTCTRL_ISKEYWORD              },
02222       { "pghdrsz",               SQLITE_TESTCTRL_PGHDRSZ                },
02223       { "scratchmalloc",         SQLITE_TESTCTRL_SCRATCHMALLOC          },
02224     };
02225     int testctrl = -1;
02226     int rc = 0;
02227     int i, n;
02228     open_db(p);
02229 
02230     /* convert testctrl text option to value. allow any unique prefix
02231     ** of the option name, or a numerical value. */
02232     n = strlen30(azArg[1]);
02233     for(i=0; i<(int)(sizeof(aCtrl)/sizeof(aCtrl[0])); i++){
02234       if( strncmp(azArg[1], aCtrl[i].zCtrlName, n)==0 ){
02235         if( testctrl<0 ){
02236           testctrl = aCtrl[i].ctrlCode;
02237         }else{
02238           fprintf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]);
02239           testctrl = -1;
02240           break;
02241         }
02242       }
02243     }
02244     if( testctrl<0 ) testctrl = atoi(azArg[1]);
02245     if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){
02246       fprintf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]);
02247     }else{
02248       switch(testctrl){
02249 
02250         /* sqlite3_test_control(int, db, int) */
02251         case SQLITE_TESTCTRL_OPTIMIZATIONS:
02252         case SQLITE_TESTCTRL_RESERVE:             
02253           if( nArg==3 ){
02254             int opt = (int)strtol(azArg[2], 0, 0);        
02255             rc = sqlite3_test_control(testctrl, p->db, opt);
02256             printf("%d (0x%08x)\n", rc, rc);
02257           } else {
02258             fprintf(stderr,"Error: testctrl %s takes a single int option\n",
02259                     azArg[1]);
02260           }
02261           break;
02262 
02263         /* sqlite3_test_control(int) */
02264         case SQLITE_TESTCTRL_PRNG_SAVE:           
02265         case SQLITE_TESTCTRL_PRNG_RESTORE:        
02266         case SQLITE_TESTCTRL_PRNG_RESET:
02267         case SQLITE_TESTCTRL_PGHDRSZ:             
02268           if( nArg==2 ){
02269             rc = sqlite3_test_control(testctrl);
02270             printf("%d (0x%08x)\n", rc, rc);
02271           } else {
02272             fprintf(stderr,"Error: testctrl %s takes no options\n", azArg[1]);
02273           }
02274           break;
02275 
02276         /* sqlite3_test_control(int, uint) */
02277         case SQLITE_TESTCTRL_PENDING_BYTE:        
02278           if( nArg==3 ){
02279             unsigned int opt = (unsigned int)atoi(azArg[2]);        
02280             rc = sqlite3_test_control(testctrl, opt);
02281             printf("%d (0x%08x)\n", rc, rc);
02282           } else {
02283             fprintf(stderr,"Error: testctrl %s takes a single unsigned"
02284                            " int option\n", azArg[1]);
02285           }
02286           break;
02287           
02288         /* sqlite3_test_control(int, int) */
02289         case SQLITE_TESTCTRL_ASSERT:              
02290         case SQLITE_TESTCTRL_ALWAYS:              
02291           if( nArg==3 ){
02292             int opt = atoi(azArg[2]);        
02293             rc = sqlite3_test_control(testctrl, opt);
02294             printf("%d (0x%08x)\n", rc, rc);
02295           } else {
02296             fprintf(stderr,"Error: testctrl %s takes a single int option\n",
02297                             azArg[1]);
02298           }
02299           break;
02300 
02301         /* sqlite3_test_control(int, char *) */
02302 #ifdef SQLITE_N_KEYWORD
02303         case SQLITE_TESTCTRL_ISKEYWORD:           
02304           if( nArg==3 ){
02305             const char *opt = azArg[2];        
02306             rc = sqlite3_test_control(testctrl, opt);
02307             printf("%d (0x%08x)\n", rc, rc);
02308           } else {
02309             fprintf(stderr,"Error: testctrl %s takes a single char * option\n",
02310                             azArg[1]);
02311           }
02312           break;
02313 #endif
02314 
02315         case SQLITE_TESTCTRL_BITVEC_TEST:         
02316         case SQLITE_TESTCTRL_FAULT_INSTALL:       
02317         case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: 
02318         case SQLITE_TESTCTRL_SCRATCHMALLOC:       
02319         default:
02320           fprintf(stderr,"Error: CLI support for testctrl %s not implemented\n",
02321                   azArg[1]);
02322           break;
02323       }
02324     }
02325   }else
02326 
02327   if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 && nArg==2 ){
02328     open_db(p);
02329     sqlite3_busy_timeout(p->db, atoi(azArg[1]));
02330   }else
02331     
02332   if( HAS_TIMER && c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0
02333    && nArg==2
02334   ){
02335     enableTimer = booleanValue(azArg[1]);
02336   }else
02337   
02338   if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
02339     printf("SQLite %s %s\n",
02340         sqlite3_libversion(), sqlite3_sourceid());
02341   }else
02342 
02343   if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){
02344     int j;
02345     assert( nArg<=ArraySize(azArg) );
02346     for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
02347       p->colWidth[j-1] = atoi(azArg[j]);
02348     }
02349   }else
02350 
02351   {
02352     fprintf(stderr, "Error: unknown command or invalid arguments: "
02353       " \"%s\". Enter \".help\" for help\n", azArg[0]);
02354     rc = 1;
02355   }
02356 
02357   return rc;
02358 }

static int dump_callback ( void *  pArg,
int  nArg,
char **  azArg,
char **  azCol 
) [static]

Definition at line 1217 of file shell.c.

References appendText(), callback_data::db, callback_data::out, run_table_dump_query(), sqlite3_column_text, sqlite3_finalize, sqlite3_free, sqlite3_mprintf, sqlite3_prepare, sqlite3_step, SQLITE_CORRUPT, SQLITE_OK, SQLITE_ROW, UNUSED_PARAMETER, and callback_data::writableSchema.

Referenced by run_schema_dump_query().

01217                                                                           {
01218   int rc;
01219   const char *zTable;
01220   const char *zType;
01221   const char *zSql;
01222   const char *zPrepStmt = 0;
01223   struct callback_data *p = (struct callback_data *)pArg;
01224 
01225   UNUSED_PARAMETER(azCol);
01226   if( nArg!=3 ) return 1;
01227   zTable = azArg[0];
01228   zType = azArg[1];
01229   zSql = azArg[2];
01230   
01231   if( strcmp(zTable, "sqlite_sequence")==0 ){
01232     zPrepStmt = "DELETE FROM sqlite_sequence;\n";
01233   }else if( strcmp(zTable, "sqlite_stat1")==0 ){
01234     fprintf(p->out, "ANALYZE sqlite_master;\n");
01235   }else if( strncmp(zTable, "sqlite_", 7)==0 ){
01236     return 0;
01237   }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
01238     char *zIns;
01239     if( !p->writableSchema ){
01240       fprintf(p->out, "PRAGMA writable_schema=ON;\n");
01241       p->writableSchema = 1;
01242     }
01243     zIns = sqlite3_mprintf(
01244        "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
01245        "VALUES('table','%q','%q',0,'%q');",
01246        zTable, zTable, zSql);
01247     fprintf(p->out, "%s\n", zIns);
01248     sqlite3_free(zIns);
01249     return 0;
01250   }else{
01251     fprintf(p->out, "%s;\n", zSql);
01252   }
01253 
01254   if( strcmp(zType, "table")==0 ){
01255     sqlite3_stmt *pTableInfo = 0;
01256     char *zSelect = 0;
01257     char *zTableInfo = 0;
01258     char *zTmp = 0;
01259     int nRow = 0;
01260    
01261     zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
01262     zTableInfo = appendText(zTableInfo, zTable, '"');
01263     zTableInfo = appendText(zTableInfo, ");", 0);
01264 
01265     rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0);
01266     free(zTableInfo);
01267     if( rc!=SQLITE_OK || !pTableInfo ){
01268       return 1;
01269     }
01270 
01271     zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
01272     zTmp = appendText(zTmp, zTable, '"');
01273     if( zTmp ){
01274       zSelect = appendText(zSelect, zTmp, '\'');
01275     }
01276     zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
01277     rc = sqlite3_step(pTableInfo);
01278     while( rc==SQLITE_ROW ){
01279       const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1);
01280       zSelect = appendText(zSelect, "quote(", 0);
01281       zSelect = appendText(zSelect, zText, '"');
01282       rc = sqlite3_step(pTableInfo);
01283       if( rc==SQLITE_ROW ){
01284         zSelect = appendText(zSelect, ") || ',' || ", 0);
01285       }else{
01286         zSelect = appendText(zSelect, ") ", 0);
01287       }
01288       nRow++;
01289     }
01290     rc = sqlite3_finalize(pTableInfo);
01291     if( rc!=SQLITE_OK || nRow==0 ){
01292       free(zSelect);
01293       return 1;
01294     }
01295     zSelect = appendText(zSelect, "|| ')' FROM  ", 0);
01296     zSelect = appendText(zSelect, zTable, '"');
01297 
01298     rc = run_table_dump_query(p, zSelect, zPrepStmt);
01299     if( rc==SQLITE_CORRUPT ){
01300       zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
01301       run_table_dump_query(p, zSelect, 0);
01302     }
01303     if( zSelect ) free(zSelect);
01304   }
01305   return 0;
01306 }

static void endTimer ( void   )  [static]

Definition at line 118 of file shell.c.

References enableTimer, sBegin, and timeDiff().

00118                           {
00119   if( enableTimer ){
00120     struct rusage sEnd;
00121     getrusage(RUSAGE_SELF, &sEnd);
00122     printf("CPU Time: user %f sys %f\n",
00123        timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
00124        timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
00125   }
00126 }

static char* find_home_dir ( void   )  [static]

Definition at line 2541 of file shell.c.

References memcpy(), NULL, sqlite3_snprintf, and strlen30().

Referenced by main(), and process_sqliterc().

02541                                 {
02542   char *home_dir = NULL;
02543 
02544 #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL)
02545   struct passwd *pwent;
02546   uid_t uid = getuid();
02547   if( (pwent=getpwuid(uid)) != NULL) {
02548     home_dir = pwent->pw_dir;
02549   }
02550 #endif
02551 
02552 #if defined(_WIN32_WCE)
02553   /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
02554    */
02555   home_dir = strdup("/");
02556 #else
02557 
02558 #if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
02559   if (!home_dir) {
02560     home_dir = getenv("USERPROFILE");
02561   }
02562 #endif
02563 
02564   if (!home_dir) {
02565     home_dir = getenv("HOME");
02566   }
02567 
02568 #if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
02569   if (!home_dir) {
02570     char *zDrive, *zPath;
02571     int n;
02572     zDrive = getenv("HOMEDRIVE");
02573     zPath = getenv("HOMEPATH");
02574     if( zDrive && zPath ){
02575       n = strlen30(zDrive) + strlen30(zPath) + 1;
02576       home_dir = malloc( n );
02577       if( home_dir==0 ) return 0;
02578       sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
02579       return home_dir;
02580     }
02581     home_dir = "c:\\";
02582   }
02583 #endif
02584 
02585 #endif /* !_WIN32_WCE */
02586 
02587   if( home_dir ){
02588     int n = strlen30(home_dir) + 1;
02589     char *z = malloc( n );
02590     if( z ) memcpy(z, home_dir, n);
02591     home_dir = z;
02592   }
02593 
02594   return home_dir;
02595 }

int isatty ( int   ) 

Referenced by main().

static int isNumber ( const char *  z,
int *  realnum 
) [static]

Definition at line 282 of file shell.c.

References IsDigit.

Referenced by shell_callback().

00282                                                 {
00283   if( *z=='-' || *z=='+' ) z++;
00284   if( !IsDigit(*z) ){
00285     return 0;
00286   }
00287   z++;
00288   if( realnum ) *realnum = 0;
00289   while( IsDigit(*z) ){ z++; }
00290   if( *z=='.' ){
00291     z++;
00292     if( !IsDigit(*z) ) return 0;
00293     while( IsDigit(*z) ){ z++; }
00294     if( realnum ) *realnum = 1;
00295   }
00296   if( *z=='e' || *z=='E' ){
00297     z++;
00298     if( *z=='+' || *z=='-' ) z++;
00299     if( !IsDigit(*z) ) return 0;
00300     while( IsDigit(*z) ){ z++; }
00301     if( realnum ) *realnum = 1;
00302   }
00303   return *z==0;
00304 }

static char* local_getline ( char *  zPrompt,
FILE *  in 
) [static]

Definition at line 337 of file shell.c.

Referenced by do_meta_command(), and one_input_line().

00337                                                    {
00338   char *zLine;
00339   int nLine;
00340   int n;
00341 
00342   if( zPrompt && *zPrompt ){
00343     printf("%s",zPrompt);
00344     fflush(stdout);
00345   }
00346   nLine = 100;
00347   zLine = malloc( nLine );
00348   if( zLine==0 ) return 0;
00349   n = 0;
00350   while( 1 ){
00351     if( n+100>nLine ){
00352       nLine = nLine*2 + 100;
00353       zLine = realloc(zLine, nLine);
00354       if( zLine==0 ) return 0;
00355     }
00356     if( fgets(&zLine[n], nLine - n, in)==0 ){
00357       if( n==0 ){
00358         free(zLine);
00359         return 0;
00360       }
00361       zLine[n] = 0;
00362       break;
00363     }
00364     while( zLine[n] ){ n++; }
00365     if( n>0 && zLine[n-1]=='\n' ){
00366       n--;
00367       if( n>0 && zLine[n-1]=='\r' ) n--;
00368       zLine[n] = 0;
00369       break;
00370     }
00371   }
00372   zLine = realloc( zLine, n+1 );
00373   return zLine;
00374 }

int main ( int  argc,
char **  argv 
)

Definition at line 2700 of file shell.c.

References callback_data::db, do_meta_command(), callback_data::echoOn, find_home_dir(), isatty(), main_init(), memcpy(), callback_data::mode, MODE_Column, MODE_Csv, MODE_Html, MODE_Line, MODE_List, callback_data::nullvalue, open_db(), callback_data::out, process_input(), process_sqliterc(), read_history, callback_data::separator, set_table_name(), shell_callback(), shell_exec(), callback_data::showHeader, sqlite3_close, sqlite3_config(), sqlite3_libversion, sqlite3_snprintf, sqlite3_sourceid, sqlite3_vfs_find, sqlite3_vfs_register, SQLITE_CONFIG_HEAP, SQLITE_SOURCE_ID, callback_data::statsOn, stifle_history, strlen30(), write_history, and callback_data::zDbFilename.

02700                                {
02701   char *zErrMsg = 0;
02702   struct callback_data data;
02703   const char *zInitFile = 0;
02704   char *zFirstCmd = 0;
02705   int i;
02706   int rc = 0;
02707 
02708   if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){
02709     fprintf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
02710             sqlite3_sourceid(), SQLITE_SOURCE_ID);
02711     exit(1);
02712   }
02713   Argv0 = argv[0];
02714   main_init(&data);
02715   stdin_is_interactive = isatty(0);
02716 
02717   /* Make sure we have a valid signal handler early, before anything
02718   ** else is done.
02719   */
02720 #ifdef SIGINT
02721   signal(SIGINT, interrupt_handler);
02722 #endif
02723 
02724   /* Do an initial pass through the command-line argument to locate
02725   ** the name of the database file, the name of the initialization file,
02726   ** the size of the alternative malloc heap,
02727   ** and the first command to execute.
02728   */
02729   for(i=1; i<argc-1; i++){
02730     char *z;
02731     if( argv[i][0]!='-' ) break;
02732     z = argv[i];
02733     if( z[0]=='-' && z[1]=='-' ) z++;
02734     if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){
02735       i++;
02736     }else if( strcmp(argv[i],"-init")==0 ){
02737       i++;
02738       zInitFile = argv[i];
02739     /* Need to check for batch mode here to so we can avoid printing
02740     ** informational messages (like from process_sqliterc) before 
02741     ** we do the actual processing of arguments later in a second pass.
02742     */
02743     }else if( strcmp(argv[i],"-batch")==0 ){
02744       stdin_is_interactive = 0;
02745     }else if( strcmp(argv[i],"-heap")==0 ){
02746 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
02747       int j, c;
02748       const char *zSize;
02749       sqlite3_int64 szHeap;
02750 
02751       zSize = argv[++i];
02752       szHeap = atoi(zSize);
02753       for(j=0; (c = zSize[j])!=0; j++){
02754         if( c=='M' ){ szHeap *= 1000000; break; }
02755         if( c=='K' ){ szHeap *= 1000; break; }
02756         if( c=='G' ){ szHeap *= 1000000000; break; }
02757       }
02758       if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
02759       sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
02760 #endif
02761 #ifdef SQLITE_ENABLE_VFSTRACE
02762     }else if( strcmp(argv[i],"-vfstrace")==0 ){
02763       extern int vfstrace_register(
02764          const char *zTraceName,
02765          const char *zOldVfsName,
02766          int (*xOut)(const char*,void*),
02767          void *pOutArg,
02768          int makeDefault
02769       );
02770       vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
02771 #endif
02772 #ifdef SQLITE_ENABLE_MULTIPLEX
02773     }else if( strcmp(argv[i],"-multiplex")==0 ){
02774       extern int sqlite3_multiple_initialize(const char*,int);
02775       sqlite3_multiplex_initialize(0, 1);
02776 #endif
02777     }else if( strcmp(argv[i],"-vfs")==0 ){
02778       sqlite3_vfs *pVfs = sqlite3_vfs_find(argv[++i]);
02779       if( pVfs ){
02780         sqlite3_vfs_register(pVfs, 1);
02781       }else{
02782         fprintf(stderr, "no such VFS: \"%s\"\n", argv[i]);
02783         exit(1);
02784       }
02785     }
02786   }
02787   if( i<argc ){
02788 #if defined(SQLITE_OS_OS2) && SQLITE_OS_OS2
02789     data.zDbFilename = (const char *)convertCpPathToUtf8( argv[i++] );
02790 #else
02791     data.zDbFilename = argv[i++];
02792 #endif
02793   }else{
02794 #ifndef SQLITE_OMIT_MEMORYDB
02795     data.zDbFilename = ":memory:";
02796 #else
02797     data.zDbFilename = 0;
02798 #endif
02799   }
02800   if( i<argc ){
02801     zFirstCmd = argv[i++];
02802   }
02803   if( i<argc ){
02804     fprintf(stderr,"%s: Error: too many options: \"%s\"\n", Argv0, argv[i]);
02805     fprintf(stderr,"Use -help for a list of options.\n");
02806     return 1;
02807   }
02808   data.out = stdout;
02809 
02810 #ifdef SQLITE_OMIT_MEMORYDB
02811   if( data.zDbFilename==0 ){
02812     fprintf(stderr,"%s: Error: no database filename specified\n", Argv0);
02813     return 1;
02814   }
02815 #endif
02816 
02817   /* Go ahead and open the database file if it already exists.  If the
02818   ** file does not exist, delay opening it.  This prevents empty database
02819   ** files from being created if a user mistypes the database name argument
02820   ** to the sqlite command-line tool.
02821   */
02822   if( access(data.zDbFilename, 0)==0 ){
02823     open_db(&data);
02824   }
02825 
02826   /* Process the initialization file if there is one.  If no -init option
02827   ** is given on the command line, look for a file named ~/.sqliterc and
02828   ** try to process it.
02829   */
02830   rc = process_sqliterc(&data,zInitFile);
02831   if( rc>0 ){
02832     return rc;
02833   }
02834 
02835   /* Make a second pass through the command-line argument and set
02836   ** options.  This second pass is delayed until after the initialization
02837   ** file is processed so that the command-line arguments will override
02838   ** settings in the initialization file.
02839   */
02840   for(i=1; i<argc && argv[i][0]=='-'; i++){
02841     char *z = argv[i];
02842     if( z[1]=='-' ){ z++; }
02843     if( strcmp(z,"-init")==0 ){
02844       i++;
02845     }else if( strcmp(z,"-html")==0 ){
02846       data.mode = MODE_Html;
02847     }else if( strcmp(z,"-list")==0 ){
02848       data.mode = MODE_List;
02849     }else if( strcmp(z,"-line")==0 ){
02850       data.mode = MODE_Line;
02851     }else if( strcmp(z,"-column")==0 ){
02852       data.mode = MODE_Column;
02853     }else if( strcmp(z,"-csv")==0 ){
02854       data.mode = MODE_Csv;
02855       memcpy(data.separator,",",2);
02856     }else if( strcmp(z,"-separator")==0 ){
02857       i++;
02858       if(i>=argc){
02859         fprintf(stderr,"%s: Error: missing argument for option: %s\n", Argv0, z);
02860         fprintf(stderr,"Use -help for a list of options.\n");
02861         return 1;
02862       }
02863       sqlite3_snprintf(sizeof(data.separator), data.separator,
02864                        "%.*s",(int)sizeof(data.separator)-1,argv[i]);
02865     }else if( strcmp(z,"-nullvalue")==0 ){
02866       i++;
02867       if(i>=argc){
02868         fprintf(stderr,"%s: Error: missing argument for option: %s\n", Argv0, z);
02869         fprintf(stderr,"Use -help for a list of options.\n");
02870         return 1;
02871       }
02872       sqlite3_snprintf(sizeof(data.nullvalue), data.nullvalue,
02873                        "%.*s",(int)sizeof(data.nullvalue)-1,argv[i]);
02874     }else if( strcmp(z,"-header")==0 ){
02875       data.showHeader = 1;
02876     }else if( strcmp(z,"-noheader")==0 ){
02877       data.showHeader = 0;
02878     }else if( strcmp(z,"-echo")==0 ){
02879       data.echoOn = 1;
02880     }else if( strcmp(z,"-stats")==0 ){
02881       data.statsOn = 1;
02882     }else if( strcmp(z,"-bail")==0 ){
02883       bail_on_error = 1;
02884     }else if( strcmp(z,"-version")==0 ){
02885       printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
02886       return 0;
02887     }else if( strcmp(z,"-interactive")==0 ){
02888       stdin_is_interactive = 1;
02889     }else if( strcmp(z,"-batch")==0 ){
02890       stdin_is_interactive = 0;
02891     }else if( strcmp(z,"-heap")==0 ){
02892       i++;
02893     }else if( strcmp(z,"-vfs")==0 ){
02894       i++;
02895 #ifdef SQLITE_ENABLE_VFSTRACE
02896     }else if( strcmp(z,"-vfstrace")==0 ){
02897       i++;
02898 #endif
02899 #ifdef SQLITE_ENABLE_MULTIPLEX
02900     }else if( strcmp(z,"-multiplex")==0 ){
02901       i++;
02902 #endif
02903     }else if( strcmp(z,"-help")==0 || strcmp(z, "--help")==0 ){
02904       usage(1);
02905     }else{
02906       fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
02907       fprintf(stderr,"Use -help for a list of options.\n");
02908       return 1;
02909     }
02910   }
02911 
02912   if( zFirstCmd ){
02913     /* Run just the command that follows the database name
02914     */
02915     if( zFirstCmd[0]=='.' ){
02916       rc = do_meta_command(zFirstCmd, &data);
02917     }else{
02918       open_db(&data);
02919       rc = shell_exec(data.db, zFirstCmd, shell_callback, &data, &zErrMsg);
02920       if( zErrMsg!=0 ){
02921         fprintf(stderr,"Error: %s\n", zErrMsg);
02922         return rc!=0 ? rc : 1;
02923       }else if( rc!=0 ){
02924         fprintf(stderr,"Error: unable to process SQL \"%s\"\n", zFirstCmd);
02925         return rc;
02926       }
02927     }
02928   }else{
02929     /* Run commands received from standard input
02930     */
02931     if( stdin_is_interactive ){
02932       char *zHome;
02933       char *zHistory = 0;
02934       int nHistory;
02935       printf(
02936         "SQLite version %s %.19s\n"
02937         "Enter \".help\" for instructions\n"
02938         "Enter SQL statements terminated with a \";\"\n",
02939         sqlite3_libversion(), sqlite3_sourceid()
02940       );
02941       zHome = find_home_dir();
02942       if( zHome ){
02943         nHistory = strlen30(zHome) + 20;
02944         if( (zHistory = malloc(nHistory))!=0 ){
02945           sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
02946         }
02947       }
02948 #if defined(HAVE_READLINE) && HAVE_READLINE==1
02949       if( zHistory ) read_history(zHistory);
02950 #endif
02951       rc = process_input(&data, 0);
02952       if( zHistory ){
02953         stifle_history(100);
02954         write_history(zHistory);
02955         free(zHistory);
02956       }
02957       free(zHome);
02958     }else{
02959       rc = process_input(&data, stdin);
02960     }
02961   }
02962   set_table_name(&data, 0);
02963   if( data.db ){
02964     sqlite3_close(data.db);
02965   }
02966   return rc;
02967 }

static void main_init ( struct callback_data data  )  [static]

Definition at line 2688 of file shell.c.

References memcpy(), callback_data::mode, MODE_List, callback_data::separator, shellLog(), callback_data::showHeader, sqlite3_config(), sqlite3_snprintf, SQLITE_CONFIG_LOG, SQLITE_CONFIG_SINGLETHREAD, and SQLITE_CONFIG_URI.

Referenced by main().

02688                                                   {
02689   memset(data, 0, sizeof(*data));
02690   data->mode = MODE_List;
02691   memcpy(data->separator,"|", 2);
02692   data->showHeader = 0;
02693   sqlite3_config(SQLITE_CONFIG_URI, 1);
02694   sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
02695   sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
02696   sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
02697   sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
02698 }

static char* one_input_line ( const char *  zPrior,
FILE *  in 
) [static]

Definition at line 382 of file shell.c.

References add_history, local_getline(), and readline.

Referenced by process_input().

00382                                                          {
00383   char *zPrompt;
00384   char *zResult;
00385   if( in!=0 ){
00386     return local_getline(0, in);
00387   }
00388   if( zPrior && zPrior[0] ){
00389     zPrompt = continuePrompt;
00390   }else{
00391     zPrompt = mainPrompt;
00392   }
00393   zResult = readline(zPrompt);
00394 #if defined(HAVE_READLINE) && HAVE_READLINE==1
00395   if( zResult && *zResult ) add_history(zResult);
00396 #endif
00397   return zResult;
00398 }

static void open_db ( struct callback_data p  )  [static]

Definition at line 1413 of file shell.c.

References callback_data::db, shellstaticFunc(), sqlite3_create_function, sqlite3_enable_load_extension(), sqlite3_errcode, sqlite3_errmsg, sqlite3_open, SQLITE_OK, SQLITE_UTF8, and callback_data::zDbFilename.

Referenced by do_meta_command(), main(), and process_input().

01413                                             {
01414   if( p->db==0 ){
01415     sqlite3_open(p->zDbFilename, &p->db);
01416     db = p->db;
01417     if( db && sqlite3_errcode(db)==SQLITE_OK ){
01418       sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0,
01419           shellstaticFunc, 0, 0);
01420     }
01421     if( db==0 || SQLITE_OK!=sqlite3_errcode(db) ){
01422       fprintf(stderr,"Error: unable to open database \"%s\": %s\n", 
01423           p->zDbFilename, sqlite3_errmsg(db));
01424       exit(1);
01425     }
01426 #ifndef SQLITE_OMIT_LOAD_EXTENSION
01427     sqlite3_enable_load_extension(p->db, 1);
01428 #endif
01429   }
01430 }

static void output_c_string ( FILE *  out,
const char *  z 
) [static]

Definition at line 532 of file shell.c.

Referenced by do_meta_command(), and shell_callback().

00532                                                      {
00533   unsigned int c;
00534   fputc('"', out);
00535   while( (c = *(z++))!=0 ){
00536     if( c=='\\' ){
00537       fputc(c, out);
00538       fputc(c, out);
00539     }else if( c=='\t' ){
00540       fputc('\\', out);
00541       fputc('t', out);
00542     }else if( c=='\n' ){
00543       fputc('\\', out);
00544       fputc('n', out);
00545     }else if( c=='\r' ){
00546       fputc('\\', out);
00547       fputc('r', out);
00548     }else if( !isprint(c) ){
00549       fprintf(out, "\\%03o", c&0xff);
00550     }else{
00551       fputc(c, out);
00552     }
00553   }
00554   fputc('"', out);
00555 }

static void output_csv ( struct callback_data p,
const char *  z,
int  bSep 
) [static]

Definition at line 620 of file shell.c.

References callback_data::nullvalue, callback_data::out, callback_data::separator, and strlen30().

Referenced by shell_callback().

00620                                                                         {
00621   FILE *out = p->out;
00622   if( z==0 ){
00623     fprintf(out,"%s",p->nullvalue);
00624   }else{
00625     int i;
00626     int nSep = strlen30(p->separator);
00627     for(i=0; z[i]; i++){
00628       if( needCsvQuote[((unsigned char*)z)[i]] 
00629          || (z[i]==p->separator[0] && 
00630              (nSep==1 || memcmp(z, p->separator, nSep)==0)) ){
00631         i = 0;
00632         break;
00633       }
00634     }
00635     if( i==0 ){
00636       putc('"', out);
00637       for(i=0; z[i]; i++){
00638         if( z[i]=='"' ) putc('"', out);
00639         putc(z[i], out);
00640       }
00641       putc('"', out);
00642     }else{
00643       fprintf(out, "%s", z);
00644     }
00645   }
00646   if( bSep ){
00647     fprintf(p->out, "%s", p->separator);
00648   }
00649 }

static void output_hex_blob ( FILE *  out,
const void *  pBlob,
int  nBlob 
) [static]

Definition at line 491 of file shell.c.

Referenced by shell_callback().

00491                                                                     {
00492   int i;
00493   char *zBlob = (char *)pBlob;
00494   fprintf(out,"X'");
00495   for(i=0; i<nBlob; i++){ fprintf(out,"%02x",zBlob[i]); }
00496   fprintf(out,"'");
00497 }

static void output_html_string ( FILE *  out,
const char *  z 
) [static]

Definition at line 561 of file shell.c.

Referenced by shell_callback().

00561                                                         {
00562   int i;
00563   while( *z ){
00564     for(i=0;   z[i] 
00565             && z[i]!='<' 
00566             && z[i]!='&' 
00567             && z[i]!='>' 
00568             && z[i]!='\"' 
00569             && z[i]!='\'';
00570         i++){}
00571     if( i>0 ){
00572       fprintf(out,"%.*s",i,z);
00573     }
00574     if( z[i]=='<' ){
00575       fprintf(out,"&lt;");
00576     }else if( z[i]=='&' ){
00577       fprintf(out,"&amp;");
00578     }else if( z[i]=='>' ){
00579       fprintf(out,"&gt;");
00580     }else if( z[i]=='\"' ){
00581       fprintf(out,"&quot;");
00582     }else if( z[i]=='\'' ){
00583       fprintf(out,"&#39;");
00584     }else{
00585       break;
00586     }
00587     z += i + 1;
00588   }
00589 }

static void output_quoted_string ( FILE *  out,
const char *  z 
) [static]

Definition at line 502 of file shell.c.

Referenced by shell_callback().

00502                                                           {
00503   int i;
00504   int nSingle = 0;
00505   for(i=0; z[i]; i++){
00506     if( z[i]=='\'' ) nSingle++;
00507   }
00508   if( nSingle==0 ){
00509     fprintf(out,"'%s'",z);
00510   }else{
00511     fprintf(out,"'");
00512     while( *z ){
00513       for(i=0; z[i] && z[i]!='\''; i++){}
00514       if( i==0 ){
00515         fprintf(out,"''");
00516         z++;
00517       }else if( z[i]=='\'' ){
00518         fprintf(out,"%.*s''",i,z);
00519         z += i+1;
00520       }else{
00521         fprintf(out,"%s",z);
00522         break;
00523       }
00524     }
00525     fprintf(out,"'");
00526   }
00527 }

static int process_input ( struct callback_data p,
FILE *  in 
) [static]

Definition at line 2434 of file shell.c.

References _all_whitespace(), _contains_semicolon(), _is_command_terminator(), _is_complete(), BEGIN_TIMER, callback_data::cnt, callback_data::db, do_meta_command(), callback_data::echoOn, END_TIMER, IsSpace, lineno, memcpy(), one_input_line(), open_db(), callback_data::out, shell_callback(), shell_exec(), sqlite3_complete, sqlite3_errmsg, sqlite3_free, sqlite3_snprintf, and strlen30().

Referenced by do_meta_command(), main(), and process_sqliterc().

02434                                                            {
02435   char *zLine = 0;
02436   char *zSql = 0;
02437   int nSql = 0;
02438   int nSqlPrior = 0;
02439   char *zErrMsg;
02440   int rc;
02441   int errCnt = 0;
02442   int lineno = 0;
02443   int startline = 0;
02444 
02445   while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
02446     fflush(p->out);
02447     free(zLine);
02448     zLine = one_input_line(zSql, in);
02449     if( zLine==0 ){
02450       break;  /* We have reached EOF */
02451     }
02452     if( seenInterrupt ){
02453       if( in!=0 ) break;
02454       seenInterrupt = 0;
02455     }
02456     lineno++;
02457     if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
02458     if( zLine && zLine[0]=='.' && nSql==0 ){
02459       if( p->echoOn ) printf("%s\n", zLine);
02460       rc = do_meta_command(zLine, p);
02461       if( rc==2 ){ /* exit requested */
02462         break;
02463       }else if( rc ){
02464         errCnt++;
02465       }
02466       continue;
02467     }
02468     if( _is_command_terminator(zLine) && _is_complete(zSql, nSql) ){
02469       memcpy(zLine,";",2);
02470     }
02471     nSqlPrior = nSql;
02472     if( zSql==0 ){
02473       int i;
02474       for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
02475       if( zLine[i]!=0 ){
02476         nSql = strlen30(zLine);
02477         zSql = malloc( nSql+3 );
02478         if( zSql==0 ){
02479           fprintf(stderr, "Error: out of memory\n");
02480           exit(1);
02481         }
02482         memcpy(zSql, zLine, nSql+1);
02483         startline = lineno;
02484       }
02485     }else{
02486       int len = strlen30(zLine);
02487       zSql = realloc( zSql, nSql + len + 4 );
02488       if( zSql==0 ){
02489         fprintf(stderr,"Error: out of memory\n");
02490         exit(1);
02491       }
02492       zSql[nSql++] = '\n';
02493       memcpy(&zSql[nSql], zLine, len+1);
02494       nSql += len;
02495     }
02496     if( zSql && _contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
02497                 && sqlite3_complete(zSql) ){
02498       p->cnt = 0;
02499       open_db(p);
02500       BEGIN_TIMER;
02501       rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
02502       END_TIMER;
02503       if( rc || zErrMsg ){
02504         char zPrefix[100];
02505         if( in!=0 || !stdin_is_interactive ){
02506           sqlite3_snprintf(sizeof(zPrefix), zPrefix, 
02507                            "Error: near line %d:", startline);
02508         }else{
02509           sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
02510         }
02511         if( zErrMsg!=0 ){
02512           fprintf(stderr, "%s %s\n", zPrefix, zErrMsg);
02513           sqlite3_free(zErrMsg);
02514           zErrMsg = 0;
02515         }else{
02516           fprintf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
02517         }
02518         errCnt++;
02519       }
02520       free(zSql);
02521       zSql = 0;
02522       nSql = 0;
02523     }
02524   }
02525   if( zSql ){
02526     if( !_all_whitespace(zSql) ){
02527       fprintf(stderr, "Error: incomplete SQL: %s\n", zSql);
02528     }
02529     free(zSql);
02530   }
02531   free(zLine);
02532   return errCnt;
02533 }

static int process_sqliterc ( struct callback_data p,
const char *  sqliterc_override 
) [static]

Definition at line 2603 of file shell.c.

References find_home_dir(), NULL, process_input(), sqlite3_snprintf, and strlen30().

Referenced by main().

02606  {
02607   char *home_dir = NULL;
02608   const char *sqliterc = sqliterc_override;
02609   char *zBuf = 0;
02610   FILE *in = NULL;
02611   int nBuf;
02612   int rc = 0;
02613 
02614   if (sqliterc == NULL) {
02615     home_dir = find_home_dir();
02616     if( home_dir==0 ){
02617 #if !defined(__RTP__) && !defined(_WRS_KERNEL)
02618       fprintf(stderr,"%s: Error: cannot locate your home directory\n", Argv0);
02619 #endif
02620       return 1;
02621     }
02622     nBuf = strlen30(home_dir) + 16;
02623     zBuf = malloc( nBuf );
02624     if( zBuf==0 ){
02625       fprintf(stderr,"%s: Error: out of memory\n",Argv0);
02626       return 1;
02627     }
02628     sqlite3_snprintf(nBuf, zBuf,"%s/.sqliterc",home_dir);
02629     free(home_dir);
02630     sqliterc = (const char*)zBuf;
02631   }
02632   in = fopen(sqliterc,"rb");
02633   if( in ){
02634     if( stdin_is_interactive ){
02635       fprintf(stderr,"-- Loading resources from %s\n",sqliterc);
02636     }
02637     rc = process_input(p,in);
02638     fclose(in);
02639   }
02640   free(zBuf);
02641   return rc;
02642 }

static void resolve_backslashes ( char *  z  )  [static]

Definition at line 1441 of file shell.c.

Referenced by do_meta_command().

01441                                         {
01442   int i, j;
01443   char c;
01444   for(i=j=0; (c = z[i])!=0; i++, j++){
01445     if( c=='\\' ){
01446       c = z[++i];
01447       if( c=='n' ){
01448         c = '\n';
01449       }else if( c=='t' ){
01450         c = '\t';
01451       }else if( c=='r' ){
01452         c = '\r';
01453       }else if( c>='0' && c<='7' ){
01454         c -= '0';
01455         if( z[i+1]>='0' && z[i+1]<='7' ){
01456           i++;
01457           c = (c<<3) + z[i] - '0';
01458           if( z[i+1]>='0' && z[i+1]<='7' ){
01459             i++;
01460             c = (c<<3) + z[i] - '0';
01461           }
01462         }
01463       }
01464     }
01465     z[j] = c;
01466   }
01467   z[j] = 0;
01468 }

static int run_schema_dump_query ( struct callback_data p,
const char *  zQuery 
) [static]

Definition at line 1315 of file shell.c.

References callback_data::db, dump_callback(), callback_data::out, sqlite3_exec, sqlite3_free, sqlite3_snprintf, SQLITE_CORRUPT, and strlen30().

Referenced by do_meta_command().

01318  {
01319   int rc;
01320   char *zErr = 0;
01321   rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
01322   if( rc==SQLITE_CORRUPT ){
01323     char *zQ2;
01324     int len = strlen30(zQuery);
01325     fprintf(p->out, "/****** CORRUPTION ERROR *******/\n");
01326     if( zErr ){
01327       fprintf(p->out, "/****** %s ******/\n", zErr);
01328       sqlite3_free(zErr);
01329       zErr = 0;
01330     }
01331     zQ2 = malloc( len+100 );
01332     if( zQ2==0 ) return rc;
01333     sqlite3_snprintf(sizeof(zQ2), zQ2, "%s ORDER BY rowid DESC", zQuery);
01334     rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
01335     if( rc ){
01336       fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
01337     }else{
01338       rc = SQLITE_CORRUPT;
01339     }
01340     sqlite3_free(zErr);
01341     free(zQ2);
01342   }
01343   return rc;
01344 }

static int run_table_dump_query ( struct callback_data p,
const char *  zSelect,
const char *  zFirstRow 
) [static]

Definition at line 943 of file shell.c.

References callback_data::db, callback_data::nErr, callback_data::out, sqlite3_column_text, sqlite3_errmsg, sqlite3_finalize, sqlite3_prepare, sqlite3_step, SQLITE_OK, and SQLITE_ROW.

Referenced by do_meta_command(), and dump_callback().

00947  {
00948   sqlite3_stmt *pSelect;
00949   int rc;
00950   rc = sqlite3_prepare(p->db, zSelect, -1, &pSelect, 0);
00951   if( rc!=SQLITE_OK || !pSelect ){
00952     fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
00953     p->nErr++;
00954     return rc;
00955   }
00956   rc = sqlite3_step(pSelect);
00957   while( rc==SQLITE_ROW ){
00958     if( zFirstRow ){
00959       fprintf(p->out, "%s", zFirstRow);
00960       zFirstRow = 0;
00961     }
00962     fprintf(p->out, "%s;\n", sqlite3_column_text(pSelect, 0));
00963     rc = sqlite3_step(pSelect);
00964   }
00965   rc = sqlite3_finalize(pSelect);
00966   if( rc!=SQLITE_OK ){
00967     fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
00968     p->nErr++;
00969   }
00970   return rc;
00971 }

static char* save_err_msg ( sqlite3 db  )  [static]

Definition at line 976 of file shell.c.

References memcpy(), sqlite3_errmsg, sqlite3_malloc, and strlen30().

Referenced by shell_exec().

00978  {
00979   int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
00980   char *zErrMsg = sqlite3_malloc(nErrMsg);
00981   if( zErrMsg ){
00982     memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
00983   }
00984   return zErrMsg;
00985 }

static void set_table_name ( struct callback_data p,
const char *  zName 
) [static]

Definition at line 857 of file shell.c.

References callback_data::zDestTable.

Referenced by do_meta_command(), and main().

00857                                                                       {
00858   int i, n;
00859   int needQuote;
00860   char *z;
00861 
00862   if( p->zDestTable ){
00863     free(p->zDestTable);
00864     p->zDestTable = 0;
00865   }
00866   if( zName==0 ) return;
00867   needQuote = !isalpha((unsigned char)*zName) && *zName!='_';
00868   for(i=n=0; zName[i]; i++, n++){
00869     if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){
00870       needQuote = 1;
00871       if( zName[i]=='\'' ) n++;
00872     }
00873   }
00874   if( needQuote ) n += 2;
00875   z = p->zDestTable = malloc( n+1 );
00876   if( z==0 ){
00877     fprintf(stderr,"Error: out of memory\n");
00878     exit(1);
00879   }
00880   n = 0;
00881   if( needQuote ) z[n++] = '\'';
00882   for(i=0; zName[i]; i++){
00883     z[n++] = zName[i];
00884     if( zName[i]=='\'' ) z[n++] = '\'';
00885   }
00886   if( needQuote ) z[n++] = '\'';
00887   z[n] = 0;
00888 }

static int shell_callback ( void *  pArg,
int  nArg,
char **  azArg,
char **  azCol,
int *  aiType 
) [static]

Definition at line 666 of file shell.c.

References callback_data::actualWidth, ArraySize, callback_data::cnt, callback_data::colWidth, isNumber(), callback_data::mode, MODE_Column, MODE_Csv, MODE_Explain, MODE_Html, MODE_Insert, MODE_Line, MODE_List, MODE_Semi, MODE_Tcl, callback_data::nullvalue, callback_data::out, output_c_string(), output_csv(), output_hex_blob(), output_html_string(), output_quoted_string(), callback_data::pStmt, callback_data::separator, callback_data::showHeader, sqlite3_column_blob, sqlite3_column_bytes, SQLITE_BLOB, SQLITE_FLOAT, SQLITE_INTEGER, SQLITE_NULL, SQLITE_TEXT, strlen30(), and callback_data::zDestTable.

Referenced by callback(), main(), and process_input().

00666                                                                                         {
00667   int i;
00668   struct callback_data *p = (struct callback_data*)pArg;
00669 
00670   switch( p->mode ){
00671     case MODE_Line: {
00672       int w = 5;
00673       if( azArg==0 ) break;
00674       for(i=0; i<nArg; i++){
00675         int len = strlen30(azCol[i] ? azCol[i] : "");
00676         if( len>w ) w = len;
00677       }
00678       if( p->cnt++>0 ) fprintf(p->out,"\n");
00679       for(i=0; i<nArg; i++){
00680         fprintf(p->out,"%*s = %s\n", w, azCol[i],
00681                 azArg[i] ? azArg[i] : p->nullvalue);
00682       }
00683       break;
00684     }
00685     case MODE_Explain:
00686     case MODE_Column: {
00687       if( p->cnt++==0 ){
00688         for(i=0; i<nArg; i++){
00689           int w, n;
00690           if( i<ArraySize(p->colWidth) ){
00691             w = p->colWidth[i];
00692           }else{
00693             w = 0;
00694           }
00695           if( w<=0 ){
00696             w = strlen30(azCol[i] ? azCol[i] : "");
00697             if( w<10 ) w = 10;
00698             n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullvalue);
00699             if( w<n ) w = n;
00700           }
00701           if( i<ArraySize(p->actualWidth) ){
00702             p->actualWidth[i] = w;
00703           }
00704           if( p->showHeader ){
00705             fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": "  ");
00706           }
00707         }
00708         if( p->showHeader ){
00709           for(i=0; i<nArg; i++){
00710             int w;
00711             if( i<ArraySize(p->actualWidth) ){
00712                w = p->actualWidth[i];
00713             }else{
00714                w = 10;
00715             }
00716             fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
00717                    "----------------------------------------------------------",
00718                     i==nArg-1 ? "\n": "  ");
00719           }
00720         }
00721       }
00722       if( azArg==0 ) break;
00723       for(i=0; i<nArg; i++){
00724         int w;
00725         if( i<ArraySize(p->actualWidth) ){
00726            w = p->actualWidth[i];
00727         }else{
00728            w = 10;
00729         }
00730         if( p->mode==MODE_Explain && azArg[i] && 
00731            strlen30(azArg[i])>w ){
00732           w = strlen30(azArg[i]);
00733         }
00734         fprintf(p->out,"%-*.*s%s",w,w,
00735             azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": "  ");
00736       }
00737       break;
00738     }
00739     case MODE_Semi:
00740     case MODE_List: {
00741       if( p->cnt++==0 && p->showHeader ){
00742         for(i=0; i<nArg; i++){
00743           fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
00744         }
00745       }
00746       if( azArg==0 ) break;
00747       for(i=0; i<nArg; i++){
00748         char *z = azArg[i];
00749         if( z==0 ) z = p->nullvalue;
00750         fprintf(p->out, "%s", z);
00751         if( i<nArg-1 ){
00752           fprintf(p->out, "%s", p->separator);
00753         }else if( p->mode==MODE_Semi ){
00754           fprintf(p->out, ";\n");
00755         }else{
00756           fprintf(p->out, "\n");
00757         }
00758       }
00759       break;
00760     }
00761     case MODE_Html: {
00762       if( p->cnt++==0 && p->showHeader ){
00763         fprintf(p->out,"<TR>");
00764         for(i=0; i<nArg; i++){
00765           fprintf(p->out,"<TH>");
00766           output_html_string(p->out, azCol[i]);
00767           fprintf(p->out,"</TH>\n");
00768         }
00769         fprintf(p->out,"</TR>\n");
00770       }
00771       if( azArg==0 ) break;
00772       fprintf(p->out,"<TR>");
00773       for(i=0; i<nArg; i++){
00774         fprintf(p->out,"<TD>");
00775         output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
00776         fprintf(p->out,"</TD>\n");
00777       }
00778       fprintf(p->out,"</TR>\n");
00779       break;
00780     }
00781     case MODE_Tcl: {
00782       if( p->cnt++==0 && p->showHeader ){
00783         for(i=0; i<nArg; i++){
00784           output_c_string(p->out,azCol[i] ? azCol[i] : "");
00785           fprintf(p->out, "%s", p->separator);
00786         }
00787         fprintf(p->out,"\n");
00788       }
00789       if( azArg==0 ) break;
00790       for(i=0; i<nArg; i++){
00791         output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
00792         fprintf(p->out, "%s", p->separator);
00793       }
00794       fprintf(p->out,"\n");
00795       break;
00796     }
00797     case MODE_Csv: {
00798       if( p->cnt++==0 && p->showHeader ){
00799         for(i=0; i<nArg; i++){
00800           output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
00801         }
00802         fprintf(p->out,"\n");
00803       }
00804       if( azArg==0 ) break;
00805       for(i=0; i<nArg; i++){
00806         output_csv(p, azArg[i], i<nArg-1);
00807       }
00808       fprintf(p->out,"\n");
00809       break;
00810     }
00811     case MODE_Insert: {
00812       p->cnt++;
00813       if( azArg==0 ) break;
00814       fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
00815       for(i=0; i<nArg; i++){
00816         char *zSep = i>0 ? ",": "";
00817         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
00818           fprintf(p->out,"%sNULL",zSep);
00819         }else if( aiType && aiType[i]==SQLITE_TEXT ){
00820           if( zSep[0] ) fprintf(p->out,"%s",zSep);
00821           output_quoted_string(p->out, azArg[i]);
00822         }else if( aiType && (aiType[i]==SQLITE_INTEGER || aiType[i]==SQLITE_FLOAT) ){
00823           fprintf(p->out,"%s%s",zSep, azArg[i]);
00824         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
00825           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
00826           int nBlob = sqlite3_column_bytes(p->pStmt, i);
00827           if( zSep[0] ) fprintf(p->out,"%s",zSep);
00828           output_hex_blob(p->out, pBlob, nBlob);
00829         }else if( isNumber(azArg[i], 0) ){
00830           fprintf(p->out,"%s%s",zSep, azArg[i]);
00831         }else{
00832           if( zSep[0] ) fprintf(p->out,"%s",zSep);
00833           output_quoted_string(p->out, azArg[i]);
00834         }
00835       }
00836       fprintf(p->out,");\n");
00837       break;
00838     }
00839   }
00840   return 0;
00841 }

static int shell_exec ( sqlite3 db,
const char *  zSql,
int(*)(void *, int, char **, char **, int *)  xCallback,
struct callback_data pArg,
char **  pzErrMsg 
) [static]

Definition at line 1087 of file shell.c.

References callback_data::cnt, display_stats(), callback_data::echoOn, IsSpace, NULL, callback_data::out, callback_data::pStmt, save_err_msg(), sqlite3_column_count, sqlite3_column_name, sqlite3_column_text, sqlite3_column_type, sqlite3_finalize, sqlite3_free, sqlite3_malloc, sqlite3_prepare_v2, sqlite3_sql, sqlite3_step, SQLITE_ABORT, SQLITE_NOMEM, SQLITE_NULL, SQLITE_OK, SQLITE_ROW, and callback_data::statsOn.

Referenced by main(), and process_input().

01094  {
01095   sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
01096   int rc = SQLITE_OK;             /* Return Code */
01097   int rc2;
01098   const char *zLeftover;          /* Tail of unprocessed SQL */
01099 
01100   if( pzErrMsg ){
01101     *pzErrMsg = NULL;
01102   }
01103 
01104   while( zSql[0] && (SQLITE_OK == rc) ){
01105     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
01106     if( SQLITE_OK != rc ){
01107       if( pzErrMsg ){
01108         *pzErrMsg = save_err_msg(db);
01109       }
01110     }else{
01111       if( !pStmt ){
01112         /* this happens for a comment or white-space */
01113         zSql = zLeftover;
01114         while( IsSpace(zSql[0]) ) zSql++;
01115         continue;
01116       }
01117 
01118       /* save off the prepared statment handle and reset row count */
01119       if( pArg ){
01120         pArg->pStmt = pStmt;
01121         pArg->cnt = 0;
01122       }
01123 
01124       /* echo the sql statement if echo on */
01125       if( pArg && pArg->echoOn ){
01126         const char *zStmtSql = sqlite3_sql(pStmt);
01127         fprintf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
01128       }
01129 
01130       /* perform the first step.  this will tell us if we
01131       ** have a result set or not and how wide it is.
01132       */
01133       rc = sqlite3_step(pStmt);
01134       /* if we have a result set... */
01135       if( SQLITE_ROW == rc ){
01136         /* if we have a callback... */
01137         if( xCallback ){
01138           /* allocate space for col name ptr, value ptr, and type */
01139           int nCol = sqlite3_column_count(pStmt);
01140           void *pData = sqlite3_malloc(3*nCol*sizeof(const char*) + 1);
01141           if( !pData ){
01142             rc = SQLITE_NOMEM;
01143           }else{
01144             char **azCols = (char **)pData;      /* Names of result columns */
01145             char **azVals = &azCols[nCol];       /* Results */
01146             int *aiTypes = (int *)&azVals[nCol]; /* Result types */
01147             int i;
01148             assert(sizeof(int) <= sizeof(char *)); 
01149             /* save off ptrs to column names */
01150             for(i=0; i<nCol; i++){
01151               azCols[i] = (char *)sqlite3_column_name(pStmt, i);
01152             }
01153             do{
01154               /* extract the data and data types */
01155               for(i=0; i<nCol; i++){
01156                 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
01157                 aiTypes[i] = sqlite3_column_type(pStmt, i);
01158                 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
01159                   rc = SQLITE_NOMEM;
01160                   break; /* from for */
01161                 }
01162               } /* end for */
01163 
01164               /* if data and types extracted successfully... */
01165               if( SQLITE_ROW == rc ){ 
01166                 /* call the supplied callback with the result row data */
01167                 if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
01168                   rc = SQLITE_ABORT;
01169                 }else{
01170                   rc = sqlite3_step(pStmt);
01171                 }
01172               }
01173             } while( SQLITE_ROW == rc );
01174             sqlite3_free(pData);
01175           }
01176         }else{
01177           do{
01178             rc = sqlite3_step(pStmt);
01179           } while( rc == SQLITE_ROW );
01180         }
01181       }
01182 
01183       /* print usage stats if stats on */
01184       if( pArg && pArg->statsOn ){
01185         display_stats(db, pArg, 0);
01186       }
01187 
01188       /* Finalize the statement just executed. If this fails, save a 
01189       ** copy of the error message. Otherwise, set zSql to point to the
01190       ** next statement to execute. */
01191       rc2 = sqlite3_finalize(pStmt);
01192       if( rc!=SQLITE_NOMEM ) rc = rc2;
01193       if( rc==SQLITE_OK ){
01194         zSql = zLeftover;
01195         while( IsSpace(zSql[0]) ) zSql++;
01196       }else if( pzErrMsg ){
01197         *pzErrMsg = save_err_msg(db);
01198       }
01199 
01200       /* clear saved stmt handle */
01201       if( pArg ){
01202         pArg->pStmt = NULL;
01203       }
01204     }
01205   } /* end while */
01206 
01207   return rc;
01208 }

static void shellLog ( void *  pArg,
int  iErrCode,
const char *  zMsg 
) [static]

Definition at line 481 of file shell.c.

References callback_data::pLog.

Referenced by main_init().

00481                                                                 {
00482   struct callback_data *p = (struct callback_data*)pArg;
00483   if( p->pLog==0 ) return;
00484   fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
00485   fflush(p->pLog);
00486 }

static void shellstaticFunc ( sqlite3_context context,
int  argc,
sqlite3_value **  argv 
) [static]

Definition at line 315 of file shell.c.

References sqlite3_result_text, SQLITE_STATIC, and UNUSED_PARAMETER.

Referenced by open_db().

00319  {
00320   assert( 0==argc );
00321   assert( zShellStatic );
00322   UNUSED_PARAMETER(argc);
00323   UNUSED_PARAMETER(argv);
00324   sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC);
00325 }

static int strlen30 ( const char *  z  )  [static]

Definition at line 472 of file shell.c.

Referenced by appendText(), do_meta_command(), find_home_dir(), main(), output_csv(), process_input(), process_sqliterc(), run_schema_dump_query(), save_err_msg(), and shell_callback().

00472                                   {
00473   const char *z2 = z;
00474   while( *z2 ){ z2++; }
00475   return 0x3fffffff & (int)(z2 - z);
00476 }

static double timeDiff ( struct timeval *  pStart,
struct timeval *  pEnd 
) [static]

Definition at line 110 of file shell.c.

Referenced by endTimer().

00110                                                                     {
00111   return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + 
00112          (double)(pEnd->tv_sec - pStart->tv_sec);
00113 }

static void usage ( int  showDetail  )  [static]

Definition at line 2672 of file shell.c.

02672                                  {
02673   fprintf(stderr,
02674       "Usage: %s [OPTIONS] FILENAME [SQL]\n"  
02675       "FILENAME is the name of an SQLite database. A new database is created\n"
02676       "if the file does not previously exist.\n", Argv0);
02677   if( showDetail ){
02678     fprintf(stderr, "OPTIONS include:\n%s", zOptions);
02679   }else{
02680     fprintf(stderr, "Use the -help option for additional information\n");
02681   }
02682   exit(1);
02683 }


Variable Documentation

char* Argv0 [static]

Definition at line 243 of file shell.c.

int bail_on_error = 0 [static]

Definition at line 219 of file shell.c.

char continuePrompt[20] [static]

Definition at line 250 of file shell.c.

sqlite3* db = 0 [static]

Examples:
addGeomRepresentation.cpp, copyLayer.cpp, createDatabase.cpp, createLayer.cpp, createProximityMatrix.cpp, createSTElementSetFromLayer.cpp, createSTElementSetFromTheme.cpp, createTable.cpp, createTheme.cpp, databaseQuery.cpp, databaseSQLQuery.cpp, importDBF.cpp, importGeoTab.cpp, importGridData.cpp, importJPEG.cpp, importMIDMIF.cpp, importShape.cpp, proxMatrixAndSpatialStatistics.cpp, rasterSlicing.cpp, spatialQuery.cpp, spatialQueryAndBuffer.cpp, and themeGrouping.cpp.

Definition at line 232 of file shell.c.

Referenced by TeTemporalSeries::buildFrameIntervals(), TeExternalTheme::buildGrouping(), countNumGeometries(), TeConnectionPool::createNewConnection(), TeTheme::deleteGrouping(), deletePointedObjects(), deleteTextVisual(), TeGTM::exportGTM(), fillCoverageMetadataTable(), generateCountLegends(), generateItemsInClauseVec(), TeTheme::generateLabelPositions(), generateTextUsingCentroid(), getColors(), TeDBConnectionsPool::getDatabase(), TeConnectionPool::getFreeConnection(), getItems(), TeTheme::getItemVector(), TeAppTheme::getLocalDatabase(), getObject2ItemsMap(), getObjects(), TeTheme::getObjects(), TeAppTheme::getTextVisual(), getVisual(), getVisualRemote(), TeQuerierDBStr1::initGeomPortal(), TeConnectionPool::initialize(), TeQuerierDBStr3::initPortal(), TeQuerierDBStr2::initPortal(), TeQuerierDBStr1::initPortal(), insertObjectIntoCollection(), TeTheme::loadAttrLists(), TeLayer::loadLayerTables(), TeTheme::loadThemeTables(), main(), objBoxInCanvas(), plotPieBars(), plotTexts(), plotTextWV(), TeTheme::populateCollection(), TeTheme::populateCollectionAux(), populateDatabase(), TeAppTheme::removeAlias(), TeAppTheme::removeAllAlias(), TeDBConnectionsPool::removeDatabase(), TeQtGrid::removePointingColorSlot(), TeQtGrid::removeQueryColorSlot(), TeTheme::save(), saveObjectStatus(), TeAppTheme::setAlias(), TeQtGridSource::setCell(), TeTheme::setLegendsForObjects(), TeQtGridSource::setPortal(), TeTheme::sqlWhereRestrictions(), TeQuerierDB::sqlWhereRestrictions(), TeCreateCells(), TeExportLinesToOGR(), TeExportPointsToOGR(), TeExportPolygonsToOGR(), TeGeoOpAggregation(), TeGeoOpAssignByLocationCollect(), TeImportGeometriesToLayer(), TePlotFrame(), TePlotObjects(), TePlotSelectedObjects(), TePlotTexts(), TePlotTextWV(), TeUpdateDBFromSet(), and TeQtThemeItem::updateAlias().

int enableTimer = 0 [static]

Definition at line 86 of file shell.c.

Referenced by beginTimer(), do_meta_command(), and endTimer().

char mainPrompt[20] [static]

Definition at line 249 of file shell.c.

const char* modeDescr[] [static]

Initial value:

 {
  "line",
  "column",
  "list",
  "semi",
  "html",
  "insert",
  "tcl",
  "csv",
  "explain",
}

Definition at line 451 of file shell.c.

const char needCsvQuote[] [static]

Initial value:

 {
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,   
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,   
  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1, 
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,   
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,   
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,   
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,   
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,   
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,   
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,   
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,   
}

Definition at line 595 of file shell.c.

struct rusage sBegin [static]

Definition at line 98 of file shell.c.

Referenced by beginTimer(), and endTimer().

volatile int seenInterrupt = 0 [static]

Definition at line 237 of file shell.c.

int stdin_is_interactive = 1 [static]

Definition at line 225 of file shell.c.

char zHelp[] [static]

Definition at line 1349 of file shell.c.

const char zOptions[] [static]

Initial value:

 
  "   -help                show this message\n"
  "   -init filename       read/process named file\n"
  "   -echo                print commands before execution\n"
  "   -[no]header          turn headers on or off\n"
  "   -bail                stop after hitting an error\n"
  "   -interactive         force interactive I/O\n"
  "   -batch               force batch I/O\n"
  "   -column              set output mode to 'column'\n"
  "   -csv                 set output mode to 'csv'\n"
  "   -html                set output mode to HTML\n"
  "   -line                set output mode to 'line'\n"
  "   -list                set output mode to 'list'\n"
  "   -separator 'x'       set output field separator (|)\n"
  "   -stats               print memory stats before each finalize\n"
  "   -nullvalue 'text'    set text string for NULL values\n"
  "   -version             show SQLite version\n"
  "   -vfs NAME            use NAME as the default VFS\n"

Definition at line 2647 of file shell.c.

const char* zShellStatic = 0 [static]

Definition at line 314 of file shell.c.

char zTimerHelp[] [static]

Initial value:

  ".timer ON|OFF          Turn the CPU timer measurement on or off\n"

Definition at line 1402 of file shell.c.


Generated on Sun Jul 29 04:02:12 2012 for TerraLib - Development Source by  doxygen 1.5.3