xref: /illumos-gate/usr/src/cmd/sqlite/shell.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate ** 2001 September 15
87c478bd9Sstevel@tonic-gate **
97c478bd9Sstevel@tonic-gate ** The author disclaims copyright to this source code.  In place of
107c478bd9Sstevel@tonic-gate ** a legal notice, here is a blessing:
117c478bd9Sstevel@tonic-gate **
127c478bd9Sstevel@tonic-gate **    May you do good and not evil.
137c478bd9Sstevel@tonic-gate **    May you find forgiveness for yourself and forgive others.
147c478bd9Sstevel@tonic-gate **    May you share freely, never taking more than you give.
157c478bd9Sstevel@tonic-gate **
167c478bd9Sstevel@tonic-gate *************************************************************************
177c478bd9Sstevel@tonic-gate ** This file contains code to implement the "sqlite" command line
187c478bd9Sstevel@tonic-gate ** utility for accessing SQLite databases.
197c478bd9Sstevel@tonic-gate **
207c478bd9Sstevel@tonic-gate ** $Id: shell.c,v 1.93 2004/03/17 23:42:13 drh Exp $
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate #include <stdlib.h>
237c478bd9Sstevel@tonic-gate #include <string.h>
247c478bd9Sstevel@tonic-gate #include <stdio.h>
257c478bd9Sstevel@tonic-gate #include "sqlite.h"
267c478bd9Sstevel@tonic-gate #include "sqlite-misc.h"				/* SUNW addition */
277c478bd9Sstevel@tonic-gate #include <ctype.h>
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
307c478bd9Sstevel@tonic-gate # include <signal.h>
317c478bd9Sstevel@tonic-gate # include <pwd.h>
327c478bd9Sstevel@tonic-gate # include <unistd.h>
337c478bd9Sstevel@tonic-gate # include <sys/types.h>
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #ifdef __MACOS__
377c478bd9Sstevel@tonic-gate # include <console.h>
387c478bd9Sstevel@tonic-gate # include <signal.h>
397c478bd9Sstevel@tonic-gate # include <unistd.h>
407c478bd9Sstevel@tonic-gate # include <extras.h>
417c478bd9Sstevel@tonic-gate # include <Files.h>
427c478bd9Sstevel@tonic-gate # include <Folders.h>
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #if defined(HAVE_READLINE) && HAVE_READLINE==1
467c478bd9Sstevel@tonic-gate # include <readline/readline.h>
477c478bd9Sstevel@tonic-gate # include <readline/history.h>
487c478bd9Sstevel@tonic-gate #else
497c478bd9Sstevel@tonic-gate # define readline(p) local_getline(p,stdin)
507c478bd9Sstevel@tonic-gate # define add_history(X)
517c478bd9Sstevel@tonic-gate # define read_history(X)
527c478bd9Sstevel@tonic-gate # define write_history(X)
537c478bd9Sstevel@tonic-gate # define stifle_history(X)
547c478bd9Sstevel@tonic-gate #endif
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* Make sure isatty() has a prototype.
577c478bd9Sstevel@tonic-gate */
587c478bd9Sstevel@tonic-gate extern int isatty();
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate ** The following is the open SQLite database.  We make a pointer
627c478bd9Sstevel@tonic-gate ** to this database a static variable so that it can be accessed
637c478bd9Sstevel@tonic-gate ** by the SIGINT handler to interrupt database processing.
647c478bd9Sstevel@tonic-gate */
657c478bd9Sstevel@tonic-gate static sqlite *db = 0;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate ** True if an interrupt (Control-C) has been received.
697c478bd9Sstevel@tonic-gate */
707c478bd9Sstevel@tonic-gate static int seenInterrupt = 0;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate ** This is the name of our program. It is set in main(), used
747c478bd9Sstevel@tonic-gate ** in a number of other places, mostly for error messages.
757c478bd9Sstevel@tonic-gate */
767c478bd9Sstevel@tonic-gate static char *Argv0;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate ** Prompt strings. Initialized in main. Settable with
807c478bd9Sstevel@tonic-gate **   .prompt main continue
817c478bd9Sstevel@tonic-gate */
827c478bd9Sstevel@tonic-gate static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
837c478bd9Sstevel@tonic-gate static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate ** Determines if a string is a number of not.
887c478bd9Sstevel@tonic-gate */
897c478bd9Sstevel@tonic-gate extern int sqliteIsNumber(const char*);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate ** This routine reads a line of text from standard input, stores
937c478bd9Sstevel@tonic-gate ** the text in memory obtained from malloc() and returns a pointer
947c478bd9Sstevel@tonic-gate ** to the text.  NULL is returned at end of file, or if malloc()
957c478bd9Sstevel@tonic-gate ** fails.
967c478bd9Sstevel@tonic-gate **
977c478bd9Sstevel@tonic-gate ** The interface is like "readline" but no command-line editing
987c478bd9Sstevel@tonic-gate ** is done.
997c478bd9Sstevel@tonic-gate */
local_getline(char * zPrompt,FILE * in)1007c478bd9Sstevel@tonic-gate static char *local_getline(char *zPrompt, FILE *in){
1017c478bd9Sstevel@tonic-gate   char *zLine;
1027c478bd9Sstevel@tonic-gate   int nLine;
1037c478bd9Sstevel@tonic-gate   int n;
1047c478bd9Sstevel@tonic-gate   int eol;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate   if( zPrompt && *zPrompt ){
1077c478bd9Sstevel@tonic-gate     printf("%s",zPrompt);
1087c478bd9Sstevel@tonic-gate     fflush(stdout);
1097c478bd9Sstevel@tonic-gate   }
1107c478bd9Sstevel@tonic-gate   nLine = 100;
1117c478bd9Sstevel@tonic-gate   zLine = malloc( nLine );
1127c478bd9Sstevel@tonic-gate   if( zLine==0 ) return 0;
1137c478bd9Sstevel@tonic-gate   n = 0;
1147c478bd9Sstevel@tonic-gate   eol = 0;
1157c478bd9Sstevel@tonic-gate   while( !eol ){
1167c478bd9Sstevel@tonic-gate     if( n+100>nLine ){
1177c478bd9Sstevel@tonic-gate       nLine = nLine*2 + 100;
1187c478bd9Sstevel@tonic-gate       zLine = realloc(zLine, nLine);
1197c478bd9Sstevel@tonic-gate       if( zLine==0 ) return 0;
1207c478bd9Sstevel@tonic-gate     }
1217c478bd9Sstevel@tonic-gate     if( fgets(&zLine[n], nLine - n, in)==0 ){
1227c478bd9Sstevel@tonic-gate       if( n==0 ){
1237c478bd9Sstevel@tonic-gate         free(zLine);
1247c478bd9Sstevel@tonic-gate         return 0;
1257c478bd9Sstevel@tonic-gate       }
1267c478bd9Sstevel@tonic-gate       zLine[n] = 0;
1277c478bd9Sstevel@tonic-gate       eol = 1;
1287c478bd9Sstevel@tonic-gate       break;
1297c478bd9Sstevel@tonic-gate     }
1307c478bd9Sstevel@tonic-gate     while( zLine[n] ){ n++; }
1317c478bd9Sstevel@tonic-gate     if( n>0 && zLine[n-1]=='\n' ){
1327c478bd9Sstevel@tonic-gate       n--;
1337c478bd9Sstevel@tonic-gate       zLine[n] = 0;
1347c478bd9Sstevel@tonic-gate       eol = 1;
1357c478bd9Sstevel@tonic-gate     }
1367c478bd9Sstevel@tonic-gate   }
1377c478bd9Sstevel@tonic-gate   zLine = realloc( zLine, n+1 );
1387c478bd9Sstevel@tonic-gate   return zLine;
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate ** Retrieve a single line of input text.  "isatty" is true if text
1437c478bd9Sstevel@tonic-gate ** is coming from a terminal.  In that case, we issue a prompt and
1447c478bd9Sstevel@tonic-gate ** attempt to use "readline" for command-line editing.  If "isatty"
1457c478bd9Sstevel@tonic-gate ** is false, use "local_getline" instead of "readline" and issue no prompt.
1467c478bd9Sstevel@tonic-gate **
1477c478bd9Sstevel@tonic-gate ** zPrior is a string of prior text retrieved.  If not the empty
1487c478bd9Sstevel@tonic-gate ** string, then issue a continuation prompt.
1497c478bd9Sstevel@tonic-gate */
one_input_line(const char * zPrior,FILE * in)1507c478bd9Sstevel@tonic-gate static char *one_input_line(const char *zPrior, FILE *in){
1517c478bd9Sstevel@tonic-gate   char *zPrompt;
1527c478bd9Sstevel@tonic-gate   char *zResult;
1537c478bd9Sstevel@tonic-gate   if( in!=0 ){
1547c478bd9Sstevel@tonic-gate     return local_getline(0, in);
1557c478bd9Sstevel@tonic-gate   }
1567c478bd9Sstevel@tonic-gate   if( zPrior && zPrior[0] ){
1577c478bd9Sstevel@tonic-gate     zPrompt = continuePrompt;
1587c478bd9Sstevel@tonic-gate   }else{
1597c478bd9Sstevel@tonic-gate     zPrompt = mainPrompt;
1607c478bd9Sstevel@tonic-gate   }
1617c478bd9Sstevel@tonic-gate   zResult = readline(zPrompt);
1627c478bd9Sstevel@tonic-gate   if( zResult ) add_history(zResult);
1637c478bd9Sstevel@tonic-gate   return zResult;
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate struct previous_mode_data {
1677c478bd9Sstevel@tonic-gate   int valid;        /* Is there legit data in here? */
1687c478bd9Sstevel@tonic-gate   int mode;
1697c478bd9Sstevel@tonic-gate   int showHeader;
1707c478bd9Sstevel@tonic-gate   int colWidth[100];
1717c478bd9Sstevel@tonic-gate };
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate ** An pointer to an instance of this structure is passed from
1747c478bd9Sstevel@tonic-gate ** the main program to the callback.  This is used to communicate
1757c478bd9Sstevel@tonic-gate ** state and mode information.
1767c478bd9Sstevel@tonic-gate */
1777c478bd9Sstevel@tonic-gate struct callback_data {
1787c478bd9Sstevel@tonic-gate   sqlite *db;            /* The database */
1797c478bd9Sstevel@tonic-gate   int echoOn;            /* True to echo input commands */
1807c478bd9Sstevel@tonic-gate   int cnt;               /* Number of records displayed so far */
1817c478bd9Sstevel@tonic-gate   FILE *out;             /* Write results here */
1827c478bd9Sstevel@tonic-gate   int mode;              /* An output mode setting */
1837c478bd9Sstevel@tonic-gate   int showHeader;        /* True to show column names in List or Column mode */
1847c478bd9Sstevel@tonic-gate   char *zDestTable;      /* Name of destination table when MODE_Insert */
1857c478bd9Sstevel@tonic-gate   char separator[20];    /* Separator character for MODE_List */
1867c478bd9Sstevel@tonic-gate   int colWidth[100];     /* Requested width of each column when in column mode*/
1877c478bd9Sstevel@tonic-gate   int actualWidth[100];  /* Actual width of each column */
1887c478bd9Sstevel@tonic-gate   char nullvalue[20];    /* The text to print when a NULL comes back from
1897c478bd9Sstevel@tonic-gate                          ** the database */
1907c478bd9Sstevel@tonic-gate   struct previous_mode_data explainPrev;
1917c478bd9Sstevel@tonic-gate                          /* Holds the mode information just before
1927c478bd9Sstevel@tonic-gate                          ** .explain ON */
1937c478bd9Sstevel@tonic-gate   char outfile[FILENAME_MAX]; /* Filename for *out */
1947c478bd9Sstevel@tonic-gate   const char *zDbFilename;    /* name of the database file */
1957c478bd9Sstevel@tonic-gate   char *zKey;                 /* Encryption key */
1967c478bd9Sstevel@tonic-gate };
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate ** These are the allowed modes.
2007c478bd9Sstevel@tonic-gate */
2017c478bd9Sstevel@tonic-gate #define MODE_Line     0  /* One column per line.  Blank line between records */
2027c478bd9Sstevel@tonic-gate #define MODE_Column   1  /* One record per line in neat columns */
2037c478bd9Sstevel@tonic-gate #define MODE_List     2  /* One record per line with a separator */
2047c478bd9Sstevel@tonic-gate #define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
2057c478bd9Sstevel@tonic-gate #define MODE_Html     4  /* Generate an XHTML table */
2067c478bd9Sstevel@tonic-gate #define MODE_Insert   5  /* Generate SQL "insert" statements */
2077c478bd9Sstevel@tonic-gate #define MODE_NUM_OF   6  /* The number of modes (not a mode itself) */
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate char *modeDescr[MODE_NUM_OF] = {
2107c478bd9Sstevel@tonic-gate   "line",
2117c478bd9Sstevel@tonic-gate   "column",
2127c478bd9Sstevel@tonic-gate   "list",
2137c478bd9Sstevel@tonic-gate   "semi",
2147c478bd9Sstevel@tonic-gate   "html",
2157c478bd9Sstevel@tonic-gate   "insert"
2167c478bd9Sstevel@tonic-gate };
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate ** Number of elements in an array
2207c478bd9Sstevel@tonic-gate */
2217c478bd9Sstevel@tonic-gate #define ArraySize(X)  (sizeof(X)/sizeof(X[0]))
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate ** Output the given string as a quoted string using SQL quoting conventions.
2257c478bd9Sstevel@tonic-gate */
output_quoted_string(FILE * out,const char * z)2267c478bd9Sstevel@tonic-gate static void output_quoted_string(FILE *out, const char *z){
2277c478bd9Sstevel@tonic-gate   int i;
2287c478bd9Sstevel@tonic-gate   int nSingle = 0;
2297c478bd9Sstevel@tonic-gate   for(i=0; z[i]; i++){
2307c478bd9Sstevel@tonic-gate     if( z[i]=='\'' ) nSingle++;
2317c478bd9Sstevel@tonic-gate   }
2327c478bd9Sstevel@tonic-gate   if( nSingle==0 ){
2337c478bd9Sstevel@tonic-gate     fprintf(out,"'%s'",z);
2347c478bd9Sstevel@tonic-gate   }else{
2357c478bd9Sstevel@tonic-gate     fprintf(out,"'");
2367c478bd9Sstevel@tonic-gate     while( *z ){
2377c478bd9Sstevel@tonic-gate       for(i=0; z[i] && z[i]!='\''; i++){}
2387c478bd9Sstevel@tonic-gate       if( i==0 ){
2397c478bd9Sstevel@tonic-gate         fprintf(out,"''");
2407c478bd9Sstevel@tonic-gate         z++;
2417c478bd9Sstevel@tonic-gate       }else if( z[i]=='\'' ){
2427c478bd9Sstevel@tonic-gate         fprintf(out,"%.*s''",i,z);
2437c478bd9Sstevel@tonic-gate         z += i+1;
2447c478bd9Sstevel@tonic-gate       }else{
2457c478bd9Sstevel@tonic-gate         fprintf(out,"%s",z);
2467c478bd9Sstevel@tonic-gate         break;
2477c478bd9Sstevel@tonic-gate       }
2487c478bd9Sstevel@tonic-gate     }
2497c478bd9Sstevel@tonic-gate     fprintf(out,"'");
2507c478bd9Sstevel@tonic-gate   }
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate ** Output the given string with characters that are special to
2557c478bd9Sstevel@tonic-gate ** HTML escaped.
2567c478bd9Sstevel@tonic-gate */
output_html_string(FILE * out,const char * z)2577c478bd9Sstevel@tonic-gate static void output_html_string(FILE *out, const char *z){
2587c478bd9Sstevel@tonic-gate   int i;
2597c478bd9Sstevel@tonic-gate   while( *z ){
2607c478bd9Sstevel@tonic-gate     for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){}
2617c478bd9Sstevel@tonic-gate     if( i>0 ){
2627c478bd9Sstevel@tonic-gate       fprintf(out,"%.*s",i,z);
2637c478bd9Sstevel@tonic-gate     }
2647c478bd9Sstevel@tonic-gate     if( z[i]=='<' ){
2657c478bd9Sstevel@tonic-gate       fprintf(out,"&lt;");
2667c478bd9Sstevel@tonic-gate     }else if( z[i]=='&' ){
2677c478bd9Sstevel@tonic-gate       fprintf(out,"&amp;");
2687c478bd9Sstevel@tonic-gate     }else{
2697c478bd9Sstevel@tonic-gate       break;
2707c478bd9Sstevel@tonic-gate     }
2717c478bd9Sstevel@tonic-gate     z += i + 1;
2727c478bd9Sstevel@tonic-gate   }
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate ** This routine runs when the user presses Ctrl-C
2777c478bd9Sstevel@tonic-gate */
interrupt_handler(int NotUsed)2787c478bd9Sstevel@tonic-gate static void interrupt_handler(int NotUsed){
2797c478bd9Sstevel@tonic-gate   seenInterrupt = 1;
2807c478bd9Sstevel@tonic-gate   if( db ) sqlite_interrupt(db);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate ** This is the callback routine that the SQLite library
2857c478bd9Sstevel@tonic-gate ** invokes for each row of a query result.
2867c478bd9Sstevel@tonic-gate */
callback(void * pArg,int nArg,char ** azArg,char ** azCol)2877c478bd9Sstevel@tonic-gate static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2887c478bd9Sstevel@tonic-gate   int i;
2897c478bd9Sstevel@tonic-gate   struct callback_data *p = (struct callback_data*)pArg;
2907c478bd9Sstevel@tonic-gate   switch( p->mode ){
2917c478bd9Sstevel@tonic-gate     case MODE_Line: {
2927c478bd9Sstevel@tonic-gate       int w = 5;
2937c478bd9Sstevel@tonic-gate       if( azArg==0 ) break;
2947c478bd9Sstevel@tonic-gate       for(i=0; i<nArg; i++){
2957c478bd9Sstevel@tonic-gate         int len = strlen(azCol[i]);
2967c478bd9Sstevel@tonic-gate         if( len>w ) w = len;
2977c478bd9Sstevel@tonic-gate       }
2987c478bd9Sstevel@tonic-gate       if( p->cnt++>0 ) fprintf(p->out,"\n");
2997c478bd9Sstevel@tonic-gate       for(i=0; i<nArg; i++){
300*2a8bcb4eSToomas Soome         fprintf(p->out,"%*s = %s\n", w, azCol[i],
3017c478bd9Sstevel@tonic-gate                 azArg[i] ? azArg[i] : p->nullvalue);
3027c478bd9Sstevel@tonic-gate       }
3037c478bd9Sstevel@tonic-gate       break;
3047c478bd9Sstevel@tonic-gate     }
3057c478bd9Sstevel@tonic-gate     case MODE_Column: {
3067c478bd9Sstevel@tonic-gate       if( p->cnt++==0 ){
3077c478bd9Sstevel@tonic-gate         for(i=0; i<nArg; i++){
3087c478bd9Sstevel@tonic-gate           int w, n;
3097c478bd9Sstevel@tonic-gate           if( i<ArraySize(p->colWidth) ){
3107c478bd9Sstevel@tonic-gate              w = p->colWidth[i];
3117c478bd9Sstevel@tonic-gate           }else{
3127c478bd9Sstevel@tonic-gate              w = 0;
3137c478bd9Sstevel@tonic-gate           }
3147c478bd9Sstevel@tonic-gate           if( w<=0 ){
3157c478bd9Sstevel@tonic-gate             w = strlen(azCol[i] ? azCol[i] : "");
3167c478bd9Sstevel@tonic-gate             if( w<10 ) w = 10;
3177c478bd9Sstevel@tonic-gate             n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue);
3187c478bd9Sstevel@tonic-gate             if( w<n ) w = n;
3197c478bd9Sstevel@tonic-gate           }
3207c478bd9Sstevel@tonic-gate           if( i<ArraySize(p->actualWidth) ){
3217c478bd9Sstevel@tonic-gate             p->actualWidth[i] = w;
3227c478bd9Sstevel@tonic-gate           }
3237c478bd9Sstevel@tonic-gate           if( p->showHeader ){
3247c478bd9Sstevel@tonic-gate             fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": "  ");
3257c478bd9Sstevel@tonic-gate           }
3267c478bd9Sstevel@tonic-gate         }
3277c478bd9Sstevel@tonic-gate         if( p->showHeader ){
3287c478bd9Sstevel@tonic-gate           for(i=0; i<nArg; i++){
3297c478bd9Sstevel@tonic-gate             int w;
3307c478bd9Sstevel@tonic-gate             if( i<ArraySize(p->actualWidth) ){
3317c478bd9Sstevel@tonic-gate                w = p->actualWidth[i];
3327c478bd9Sstevel@tonic-gate             }else{
3337c478bd9Sstevel@tonic-gate                w = 10;
3347c478bd9Sstevel@tonic-gate             }
3357c478bd9Sstevel@tonic-gate             fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
3367c478bd9Sstevel@tonic-gate                    "----------------------------------------------------------",
3377c478bd9Sstevel@tonic-gate                     i==nArg-1 ? "\n": "  ");
3387c478bd9Sstevel@tonic-gate           }
3397c478bd9Sstevel@tonic-gate         }
3407c478bd9Sstevel@tonic-gate       }
3417c478bd9Sstevel@tonic-gate       if( azArg==0 ) break;
3427c478bd9Sstevel@tonic-gate       for(i=0; i<nArg; i++){
3437c478bd9Sstevel@tonic-gate         int w;
3447c478bd9Sstevel@tonic-gate         if( i<ArraySize(p->actualWidth) ){
3457c478bd9Sstevel@tonic-gate            w = p->actualWidth[i];
3467c478bd9Sstevel@tonic-gate         }else{
3477c478bd9Sstevel@tonic-gate            w = 10;
3487c478bd9Sstevel@tonic-gate         }
3497c478bd9Sstevel@tonic-gate         fprintf(p->out,"%-*.*s%s",w,w,
3507c478bd9Sstevel@tonic-gate             azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": "  ");
3517c478bd9Sstevel@tonic-gate       }
3527c478bd9Sstevel@tonic-gate       break;
3537c478bd9Sstevel@tonic-gate     }
3547c478bd9Sstevel@tonic-gate     case MODE_Semi:
3557c478bd9Sstevel@tonic-gate     case MODE_List: {
3567c478bd9Sstevel@tonic-gate       if( p->cnt++==0 && p->showHeader ){
3577c478bd9Sstevel@tonic-gate         for(i=0; i<nArg; i++){
3587c478bd9Sstevel@tonic-gate           fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
3597c478bd9Sstevel@tonic-gate         }
3607c478bd9Sstevel@tonic-gate       }
3617c478bd9Sstevel@tonic-gate       if( azArg==0 ) break;
3627c478bd9Sstevel@tonic-gate       for(i=0; i<nArg; i++){
3637c478bd9Sstevel@tonic-gate         char *z = azArg[i];
3647c478bd9Sstevel@tonic-gate         if( z==0 ) z = p->nullvalue;
3657c478bd9Sstevel@tonic-gate         fprintf(p->out, "%s", z);
3667c478bd9Sstevel@tonic-gate         if( i<nArg-1 ){
3677c478bd9Sstevel@tonic-gate           fprintf(p->out, "%s", p->separator);
3687c478bd9Sstevel@tonic-gate         }else if( p->mode==MODE_Semi ){
3697c478bd9Sstevel@tonic-gate           fprintf(p->out, ";\n");
3707c478bd9Sstevel@tonic-gate         }else{
3717c478bd9Sstevel@tonic-gate           fprintf(p->out, "\n");
3727c478bd9Sstevel@tonic-gate         }
3737c478bd9Sstevel@tonic-gate       }
3747c478bd9Sstevel@tonic-gate       break;
3757c478bd9Sstevel@tonic-gate     }
3767c478bd9Sstevel@tonic-gate     case MODE_Html: {
3777c478bd9Sstevel@tonic-gate       if( p->cnt++==0 && p->showHeader ){
3787c478bd9Sstevel@tonic-gate         fprintf(p->out,"<TR>");
3797c478bd9Sstevel@tonic-gate         for(i=0; i<nArg; i++){
3807c478bd9Sstevel@tonic-gate           fprintf(p->out,"<TH>%s</TH>",azCol[i]);
3817c478bd9Sstevel@tonic-gate         }
3827c478bd9Sstevel@tonic-gate         fprintf(p->out,"</TR>\n");
3837c478bd9Sstevel@tonic-gate       }
3847c478bd9Sstevel@tonic-gate       if( azArg==0 ) break;
3857c478bd9Sstevel@tonic-gate       fprintf(p->out,"<TR>");
3867c478bd9Sstevel@tonic-gate       for(i=0; i<nArg; i++){
3877c478bd9Sstevel@tonic-gate         fprintf(p->out,"<TD>");
3887c478bd9Sstevel@tonic-gate         output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
3897c478bd9Sstevel@tonic-gate         fprintf(p->out,"</TD>\n");
3907c478bd9Sstevel@tonic-gate       }
3917c478bd9Sstevel@tonic-gate       fprintf(p->out,"</TR>\n");
3927c478bd9Sstevel@tonic-gate       break;
3937c478bd9Sstevel@tonic-gate     }
3947c478bd9Sstevel@tonic-gate     case MODE_Insert: {
3957c478bd9Sstevel@tonic-gate       if( azArg==0 ) break;
3967c478bd9Sstevel@tonic-gate       fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
3977c478bd9Sstevel@tonic-gate       for(i=0; i<nArg; i++){
3987c478bd9Sstevel@tonic-gate         char *zSep = i>0 ? ",": "";
3997c478bd9Sstevel@tonic-gate         if( azArg[i]==0 ){
4007c478bd9Sstevel@tonic-gate           fprintf(p->out,"%sNULL",zSep);
4017c478bd9Sstevel@tonic-gate         }else if( sqliteIsNumber(azArg[i]) ){
4027c478bd9Sstevel@tonic-gate           fprintf(p->out,"%s%s",zSep, azArg[i]);
4037c478bd9Sstevel@tonic-gate         }else{
4047c478bd9Sstevel@tonic-gate           if( zSep[0] ) fprintf(p->out,"%s",zSep);
4057c478bd9Sstevel@tonic-gate           output_quoted_string(p->out, azArg[i]);
4067c478bd9Sstevel@tonic-gate         }
4077c478bd9Sstevel@tonic-gate       }
4087c478bd9Sstevel@tonic-gate       fprintf(p->out,");\n");
4097c478bd9Sstevel@tonic-gate       break;
4107c478bd9Sstevel@tonic-gate     }
4117c478bd9Sstevel@tonic-gate   }
4127c478bd9Sstevel@tonic-gate   return 0;
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate ** Set the destination table field of the callback_data structure to
4177c478bd9Sstevel@tonic-gate ** the name of the table given.  Escape any quote characters in the
4187c478bd9Sstevel@tonic-gate ** table name.
4197c478bd9Sstevel@tonic-gate */
set_table_name(struct callback_data * p,const char * zName)4207c478bd9Sstevel@tonic-gate static void set_table_name(struct callback_data *p, const char *zName){
4217c478bd9Sstevel@tonic-gate   int i, n;
4227c478bd9Sstevel@tonic-gate   int needQuote;
4237c478bd9Sstevel@tonic-gate   char *z;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate   if( p->zDestTable ){
4267c478bd9Sstevel@tonic-gate     free(p->zDestTable);
4277c478bd9Sstevel@tonic-gate     p->zDestTable = 0;
4287c478bd9Sstevel@tonic-gate   }
4297c478bd9Sstevel@tonic-gate   if( zName==0 ) return;
4307c478bd9Sstevel@tonic-gate   needQuote = !isalpha(*zName) && *zName!='_';
4317c478bd9Sstevel@tonic-gate   for(i=n=0; zName[i]; i++, n++){
4327c478bd9Sstevel@tonic-gate     if( !isalnum(zName[i]) && zName[i]!='_' ){
4337c478bd9Sstevel@tonic-gate       needQuote = 1;
4347c478bd9Sstevel@tonic-gate       if( zName[i]=='\'' ) n++;
4357c478bd9Sstevel@tonic-gate     }
4367c478bd9Sstevel@tonic-gate   }
4377c478bd9Sstevel@tonic-gate   if( needQuote ) n += 2;
4387c478bd9Sstevel@tonic-gate   z = p->zDestTable = malloc( n+1 );
4397c478bd9Sstevel@tonic-gate   if( z==0 ){
4407c478bd9Sstevel@tonic-gate     fprintf(stderr,"Out of memory!\n");
4417c478bd9Sstevel@tonic-gate     exit(1);
4427c478bd9Sstevel@tonic-gate   }
4437c478bd9Sstevel@tonic-gate   n = 0;
4447c478bd9Sstevel@tonic-gate   if( needQuote ) z[n++] = '\'';
4457c478bd9Sstevel@tonic-gate   for(i=0; zName[i]; i++){
4467c478bd9Sstevel@tonic-gate     z[n++] = zName[i];
4477c478bd9Sstevel@tonic-gate     if( zName[i]=='\'' ) z[n++] = '\'';
4487c478bd9Sstevel@tonic-gate   }
4497c478bd9Sstevel@tonic-gate   if( needQuote ) z[n++] = '\'';
4507c478bd9Sstevel@tonic-gate   z[n] = 0;
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate /*
4547c478bd9Sstevel@tonic-gate ** This is a different callback routine used for dumping the database.
4557c478bd9Sstevel@tonic-gate ** Each row received by this callback consists of a table name,
4567c478bd9Sstevel@tonic-gate ** the table type ("index" or "table") and SQL to create the table.
4577c478bd9Sstevel@tonic-gate ** This routine should print text sufficient to recreate the table.
4587c478bd9Sstevel@tonic-gate */
dump_callback(void * pArg,int nArg,char ** azArg,char ** azCol)4597c478bd9Sstevel@tonic-gate static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
4607c478bd9Sstevel@tonic-gate   struct callback_data *p = (struct callback_data *)pArg;
4617c478bd9Sstevel@tonic-gate   if( nArg!=3 ) return 1;
4627c478bd9Sstevel@tonic-gate   fprintf(p->out, "%s;\n", azArg[2]);
4637c478bd9Sstevel@tonic-gate   if( strcmp(azArg[1],"table")==0 ){
4647c478bd9Sstevel@tonic-gate     struct callback_data d2;
4657c478bd9Sstevel@tonic-gate     d2 = *p;
4667c478bd9Sstevel@tonic-gate     d2.mode = MODE_Insert;
4677c478bd9Sstevel@tonic-gate     d2.zDestTable = 0;
4687c478bd9Sstevel@tonic-gate     set_table_name(&d2, azArg[0]);
4697c478bd9Sstevel@tonic-gate     sqlite_exec_printf(p->db,
4707c478bd9Sstevel@tonic-gate        "SELECT * FROM '%q'",
4717c478bd9Sstevel@tonic-gate        callback, &d2, 0, azArg[0]
4727c478bd9Sstevel@tonic-gate     );
4737c478bd9Sstevel@tonic-gate     set_table_name(&d2, 0);
4747c478bd9Sstevel@tonic-gate   }
4757c478bd9Sstevel@tonic-gate   return 0;
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate /*
4797c478bd9Sstevel@tonic-gate ** Text of a help message
4807c478bd9Sstevel@tonic-gate */
4817c478bd9Sstevel@tonic-gate static char zHelp[] =
4827c478bd9Sstevel@tonic-gate   ".databases             List names and files of attached databases\n"
4837c478bd9Sstevel@tonic-gate   ".dump ?TABLE? ...      Dump the database in a text format\n"
4847c478bd9Sstevel@tonic-gate   ".echo ON|OFF           Turn command echo on or off\n"
4857c478bd9Sstevel@tonic-gate   ".exit                  Exit this program\n"
4867c478bd9Sstevel@tonic-gate   ".explain ON|OFF        Turn output mode suitable for EXPLAIN on or off.\n"
4877c478bd9Sstevel@tonic-gate   ".header(s) ON|OFF      Turn display of headers on or off\n"
4887c478bd9Sstevel@tonic-gate   ".help                  Show this message\n"
4897c478bd9Sstevel@tonic-gate   ".indices TABLE         Show names of all indices on TABLE\n"
4907c478bd9Sstevel@tonic-gate   ".mode MODE             Set mode to one of \"line(s)\", \"column(s)\", \n"
4917c478bd9Sstevel@tonic-gate   "                       \"insert\", \"list\", or \"html\"\n"
4927c478bd9Sstevel@tonic-gate   ".mode insert TABLE     Generate SQL insert statements for TABLE\n"
4937c478bd9Sstevel@tonic-gate   ".nullvalue STRING      Print STRING instead of nothing for NULL data\n"
4947c478bd9Sstevel@tonic-gate   ".output FILENAME       Send output to FILENAME\n"
4957c478bd9Sstevel@tonic-gate   ".output stdout         Send output to the screen\n"
4967c478bd9Sstevel@tonic-gate   ".prompt MAIN CONTINUE  Replace the standard prompts\n"
4977c478bd9Sstevel@tonic-gate   ".quit                  Exit this program\n"
4987c478bd9Sstevel@tonic-gate   ".read FILENAME         Execute SQL in FILENAME\n"
4997c478bd9Sstevel@tonic-gate #ifdef SQLITE_HAS_CODEC
5007c478bd9Sstevel@tonic-gate   ".rekey OLD NEW NEW     Change the encryption key\n"
5017c478bd9Sstevel@tonic-gate #endif
5027c478bd9Sstevel@tonic-gate   ".schema ?TABLE?        Show the CREATE statements\n"
5037c478bd9Sstevel@tonic-gate   ".separator STRING      Change separator string for \"list\" mode\n"
5047c478bd9Sstevel@tonic-gate   ".show                  Show the current values for various settings\n"
5057c478bd9Sstevel@tonic-gate   ".tables ?PATTERN?      List names of tables matching a pattern\n"
5067c478bd9Sstevel@tonic-gate   ".timeout MS            Try opening locked tables for MS milliseconds\n"
5077c478bd9Sstevel@tonic-gate   ".width NUM NUM ...     Set column widths for \"column\" mode\n"
5087c478bd9Sstevel@tonic-gate ;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate /* Forward reference */
5117c478bd9Sstevel@tonic-gate static void process_input(struct callback_data *p, FILE *in);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate /*
5147c478bd9Sstevel@tonic-gate ** Make sure the database is open.  If it is not, then open it.  If
5157c478bd9Sstevel@tonic-gate ** the database fails to open, print an error message and exit.
5167c478bd9Sstevel@tonic-gate */
open_db(struct callback_data * p)5177c478bd9Sstevel@tonic-gate static void open_db(struct callback_data *p){
5187c478bd9Sstevel@tonic-gate   if( p->db==0 ){
5197c478bd9Sstevel@tonic-gate     char *zErrMsg = 0;
5207c478bd9Sstevel@tonic-gate #ifdef SQLITE_HAS_CODEC
5217c478bd9Sstevel@tonic-gate     int n = p->zKey ? strlen(p->zKey) : 0;
5227c478bd9Sstevel@tonic-gate     db = p->db = sqlite_open_encrypted(p->zDbFilename, p->zKey, n, 0, &zErrMsg);
5237c478bd9Sstevel@tonic-gate #else
5247c478bd9Sstevel@tonic-gate     db = p->db = sqlite_open(p->zDbFilename, 0, &zErrMsg);
5257c478bd9Sstevel@tonic-gate #endif
5267c478bd9Sstevel@tonic-gate     if( p->db==0 ){
5277c478bd9Sstevel@tonic-gate       if( zErrMsg ){
528*2a8bcb4eSToomas Soome         fprintf(stderr,"Unable to open database \"%s\": %s\n",
5297c478bd9Sstevel@tonic-gate            p->zDbFilename, zErrMsg);
5307c478bd9Sstevel@tonic-gate       }else{
5317c478bd9Sstevel@tonic-gate         fprintf(stderr,"Unable to open database %s\n", p->zDbFilename);
5327c478bd9Sstevel@tonic-gate       }
5337c478bd9Sstevel@tonic-gate       exit(1);
5347c478bd9Sstevel@tonic-gate     }
5357c478bd9Sstevel@tonic-gate   }
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate ** If an input line begins with "." then invoke this routine to
5407c478bd9Sstevel@tonic-gate ** process that line.
5417c478bd9Sstevel@tonic-gate **
5427c478bd9Sstevel@tonic-gate ** Return 1 to exit and 0 to continue.
5437c478bd9Sstevel@tonic-gate */
do_meta_command(char * zLine,struct callback_data * p)5447c478bd9Sstevel@tonic-gate static int do_meta_command(char *zLine, struct callback_data *p){
5457c478bd9Sstevel@tonic-gate   int i = 1;
5467c478bd9Sstevel@tonic-gate   int nArg = 0;
5477c478bd9Sstevel@tonic-gate   int n, c;
5487c478bd9Sstevel@tonic-gate   int rc = 0;
5497c478bd9Sstevel@tonic-gate   char *azArg[50];
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate   /* Parse the input line into tokens.
5527c478bd9Sstevel@tonic-gate   */
5537c478bd9Sstevel@tonic-gate   while( zLine[i] && nArg<ArraySize(azArg) ){
5547c478bd9Sstevel@tonic-gate     while( isspace(zLine[i]) ){ i++; }
5557c478bd9Sstevel@tonic-gate     if( zLine[i]==0 ) break;
5567c478bd9Sstevel@tonic-gate     if( zLine[i]=='\'' || zLine[i]=='"' ){
5577c478bd9Sstevel@tonic-gate       int delim = zLine[i++];
5587c478bd9Sstevel@tonic-gate       azArg[nArg++] = &zLine[i];
5597c478bd9Sstevel@tonic-gate       while( zLine[i] && zLine[i]!=delim ){ i++; }
5607c478bd9Sstevel@tonic-gate       if( zLine[i]==delim ){
5617c478bd9Sstevel@tonic-gate         zLine[i++] = 0;
5627c478bd9Sstevel@tonic-gate       }
5637c478bd9Sstevel@tonic-gate     }else{
5647c478bd9Sstevel@tonic-gate       azArg[nArg++] = &zLine[i];
5657c478bd9Sstevel@tonic-gate       while( zLine[i] && !isspace(zLine[i]) ){ i++; }
5667c478bd9Sstevel@tonic-gate       if( zLine[i] ) zLine[i++] = 0;
5677c478bd9Sstevel@tonic-gate     }
5687c478bd9Sstevel@tonic-gate   }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate   /* Process the input line.
5717c478bd9Sstevel@tonic-gate   */
5727c478bd9Sstevel@tonic-gate   if( nArg==0 ) return rc;
5737c478bd9Sstevel@tonic-gate   n = strlen(azArg[0]);
5747c478bd9Sstevel@tonic-gate   c = azArg[0][0];
5757c478bd9Sstevel@tonic-gate   if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
5767c478bd9Sstevel@tonic-gate     struct callback_data data;
5777c478bd9Sstevel@tonic-gate     char *zErrMsg = 0;
5787c478bd9Sstevel@tonic-gate     open_db(p);
5797c478bd9Sstevel@tonic-gate     memcpy(&data, p, sizeof(data));
5807c478bd9Sstevel@tonic-gate     data.showHeader = 1;
5817c478bd9Sstevel@tonic-gate     data.mode = MODE_Column;
5827c478bd9Sstevel@tonic-gate     data.colWidth[0] = 3;
5837c478bd9Sstevel@tonic-gate     data.colWidth[1] = 15;
5847c478bd9Sstevel@tonic-gate     data.colWidth[2] = 58;
5857c478bd9Sstevel@tonic-gate     sqlite_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
5867c478bd9Sstevel@tonic-gate     if( zErrMsg ){
5877c478bd9Sstevel@tonic-gate       fprintf(stderr,"Error: %s\n", zErrMsg);
5887c478bd9Sstevel@tonic-gate       sqlite_freemem(zErrMsg);
5897c478bd9Sstevel@tonic-gate     }
5907c478bd9Sstevel@tonic-gate   }else
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate   if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
5937c478bd9Sstevel@tonic-gate     char *zErrMsg = 0;
5947c478bd9Sstevel@tonic-gate     open_db(p);
5957c478bd9Sstevel@tonic-gate     fprintf(p->out, "BEGIN TRANSACTION;\n");
5967c478bd9Sstevel@tonic-gate     if( nArg==1 ){
5977c478bd9Sstevel@tonic-gate       sqlite_exec(p->db,
5987c478bd9Sstevel@tonic-gate         "SELECT name, type, sql FROM sqlite_master "
5997c478bd9Sstevel@tonic-gate         "WHERE type!='meta' AND sql NOT NULL "
6007c478bd9Sstevel@tonic-gate         "ORDER BY substr(type,2,1), name",
6017c478bd9Sstevel@tonic-gate         dump_callback, p, &zErrMsg
6027c478bd9Sstevel@tonic-gate       );
6037c478bd9Sstevel@tonic-gate     }else{
6047c478bd9Sstevel@tonic-gate       int i;
6057c478bd9Sstevel@tonic-gate       for(i=1; i<nArg && zErrMsg==0; i++){
6067c478bd9Sstevel@tonic-gate         sqlite_exec_printf(p->db,
6077c478bd9Sstevel@tonic-gate           "SELECT name, type, sql FROM sqlite_master "
6087c478bd9Sstevel@tonic-gate           "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOT NULL "
6097c478bd9Sstevel@tonic-gate           "ORDER BY substr(type,2,1), name",
6107c478bd9Sstevel@tonic-gate           dump_callback, p, &zErrMsg, azArg[i]
6117c478bd9Sstevel@tonic-gate         );
6127c478bd9Sstevel@tonic-gate       }
6137c478bd9Sstevel@tonic-gate     }
6147c478bd9Sstevel@tonic-gate     if( zErrMsg ){
6157c478bd9Sstevel@tonic-gate       fprintf(stderr,"Error: %s\n", zErrMsg);
6167c478bd9Sstevel@tonic-gate       sqlite_freemem(zErrMsg);
6177c478bd9Sstevel@tonic-gate     }else{
6187c478bd9Sstevel@tonic-gate       fprintf(p->out, "COMMIT;\n");
6197c478bd9Sstevel@tonic-gate     }
6207c478bd9Sstevel@tonic-gate   }else
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate   if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
6237c478bd9Sstevel@tonic-gate     int j;
6247c478bd9Sstevel@tonic-gate     char *z = azArg[1];
6257c478bd9Sstevel@tonic-gate     int val = atoi(azArg[1]);
6267c478bd9Sstevel@tonic-gate     for(j=0; z[j]; j++){
6277c478bd9Sstevel@tonic-gate       if( isupper(z[j]) ) z[j] = tolower(z[j]);
6287c478bd9Sstevel@tonic-gate     }
6297c478bd9Sstevel@tonic-gate     if( strcmp(z,"on")==0 ){
6307c478bd9Sstevel@tonic-gate       val = 1;
6317c478bd9Sstevel@tonic-gate     }else if( strcmp(z,"yes")==0 ){
6327c478bd9Sstevel@tonic-gate       val = 1;
6337c478bd9Sstevel@tonic-gate     }
6347c478bd9Sstevel@tonic-gate     p->echoOn = val;
6357c478bd9Sstevel@tonic-gate   }else
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate   if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
6387c478bd9Sstevel@tonic-gate     rc = 1;
6397c478bd9Sstevel@tonic-gate   }else
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate   if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
6427c478bd9Sstevel@tonic-gate     int j;
6437c478bd9Sstevel@tonic-gate     char *z = nArg>=2 ? azArg[1] : "1";
6447c478bd9Sstevel@tonic-gate     int val = atoi(z);
6457c478bd9Sstevel@tonic-gate     for(j=0; z[j]; j++){
6467c478bd9Sstevel@tonic-gate       if( isupper(z[j]) ) z[j] = tolower(z[j]);
6477c478bd9Sstevel@tonic-gate     }
6487c478bd9Sstevel@tonic-gate     if( strcmp(z,"on")==0 ){
6497c478bd9Sstevel@tonic-gate       val = 1;
6507c478bd9Sstevel@tonic-gate     }else if( strcmp(z,"yes")==0 ){
6517c478bd9Sstevel@tonic-gate       val = 1;
6527c478bd9Sstevel@tonic-gate     }
6537c478bd9Sstevel@tonic-gate     if(val == 1) {
6547c478bd9Sstevel@tonic-gate       if(!p->explainPrev.valid) {
6557c478bd9Sstevel@tonic-gate         p->explainPrev.valid = 1;
6567c478bd9Sstevel@tonic-gate         p->explainPrev.mode = p->mode;
6577c478bd9Sstevel@tonic-gate         p->explainPrev.showHeader = p->showHeader;
6587c478bd9Sstevel@tonic-gate         memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
6597c478bd9Sstevel@tonic-gate       }
6607c478bd9Sstevel@tonic-gate       /* We could put this code under the !p->explainValid
6617c478bd9Sstevel@tonic-gate       ** condition so that it does not execute if we are already in
6627c478bd9Sstevel@tonic-gate       ** explain mode. However, always executing it allows us an easy
6637c478bd9Sstevel@tonic-gate       ** was to reset to explain mode in case the user previously
6647c478bd9Sstevel@tonic-gate       ** did an .explain followed by a .width, .mode or .header
6657c478bd9Sstevel@tonic-gate       ** command.
6667c478bd9Sstevel@tonic-gate       */
6677c478bd9Sstevel@tonic-gate       p->mode = MODE_Column;
6687c478bd9Sstevel@tonic-gate       p->showHeader = 1;
6697c478bd9Sstevel@tonic-gate       memset(p->colWidth,0,ArraySize(p->colWidth));
6707c478bd9Sstevel@tonic-gate       p->colWidth[0] = 4;
6717c478bd9Sstevel@tonic-gate       p->colWidth[1] = 12;
6727c478bd9Sstevel@tonic-gate       p->colWidth[2] = 10;
6737c478bd9Sstevel@tonic-gate       p->colWidth[3] = 10;
6747c478bd9Sstevel@tonic-gate       p->colWidth[4] = 35;
6757c478bd9Sstevel@tonic-gate     }else if (p->explainPrev.valid) {
6767c478bd9Sstevel@tonic-gate       p->explainPrev.valid = 0;
6777c478bd9Sstevel@tonic-gate       p->mode = p->explainPrev.mode;
6787c478bd9Sstevel@tonic-gate       p->showHeader = p->explainPrev.showHeader;
6797c478bd9Sstevel@tonic-gate       memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
6807c478bd9Sstevel@tonic-gate     }
6817c478bd9Sstevel@tonic-gate   }else
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate   if( c=='h' && (strncmp(azArg[0], "header", n)==0
6847c478bd9Sstevel@tonic-gate                  ||
6857c478bd9Sstevel@tonic-gate                  strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){
6867c478bd9Sstevel@tonic-gate     int j;
6877c478bd9Sstevel@tonic-gate     char *z = azArg[1];
6887c478bd9Sstevel@tonic-gate     int val = atoi(azArg[1]);
6897c478bd9Sstevel@tonic-gate     for(j=0; z[j]; j++){
6907c478bd9Sstevel@tonic-gate       if( isupper(z[j]) ) z[j] = tolower(z[j]);
6917c478bd9Sstevel@tonic-gate     }
6927c478bd9Sstevel@tonic-gate     if( strcmp(z,"on")==0 ){
6937c478bd9Sstevel@tonic-gate       val = 1;
6947c478bd9Sstevel@tonic-gate     }else if( strcmp(z,"yes")==0 ){
6957c478bd9Sstevel@tonic-gate       val = 1;
6967c478bd9Sstevel@tonic-gate     }
6977c478bd9Sstevel@tonic-gate     p->showHeader = val;
6987c478bd9Sstevel@tonic-gate   }else
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate   if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
7017c478bd9Sstevel@tonic-gate     fprintf(stderr,zHelp);
7027c478bd9Sstevel@tonic-gate   }else
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate   if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
7057c478bd9Sstevel@tonic-gate     struct callback_data data;
7067c478bd9Sstevel@tonic-gate     char *zErrMsg = 0;
7077c478bd9Sstevel@tonic-gate     open_db(p);
7087c478bd9Sstevel@tonic-gate     memcpy(&data, p, sizeof(data));
7097c478bd9Sstevel@tonic-gate     data.showHeader = 0;
7107c478bd9Sstevel@tonic-gate     data.mode = MODE_List;
7117c478bd9Sstevel@tonic-gate     sqlite_exec_printf(p->db,
7127c478bd9Sstevel@tonic-gate       "SELECT name FROM sqlite_master "
7137c478bd9Sstevel@tonic-gate       "WHERE type='index' AND tbl_name LIKE '%q' "
7147c478bd9Sstevel@tonic-gate       "UNION ALL "
7157c478bd9Sstevel@tonic-gate       "SELECT name FROM sqlite_temp_master "
7167c478bd9Sstevel@tonic-gate       "WHERE type='index' AND tbl_name LIKE '%q' "
7177c478bd9Sstevel@tonic-gate       "ORDER BY 1",
7187c478bd9Sstevel@tonic-gate       callback, &data, &zErrMsg, azArg[1], azArg[1]
7197c478bd9Sstevel@tonic-gate     );
7207c478bd9Sstevel@tonic-gate     if( zErrMsg ){
7217c478bd9Sstevel@tonic-gate       fprintf(stderr,"Error: %s\n", zErrMsg);
7227c478bd9Sstevel@tonic-gate       sqlite_freemem(zErrMsg);
7237c478bd9Sstevel@tonic-gate     }
7247c478bd9Sstevel@tonic-gate   }else
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate   if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
7277c478bd9Sstevel@tonic-gate     int n2 = strlen(azArg[1]);
7287c478bd9Sstevel@tonic-gate     if( strncmp(azArg[1],"line",n2)==0
7297c478bd9Sstevel@tonic-gate         ||
7307c478bd9Sstevel@tonic-gate         strncmp(azArg[1],"lines",n2)==0 ){
7317c478bd9Sstevel@tonic-gate       p->mode = MODE_Line;
7327c478bd9Sstevel@tonic-gate     }else if( strncmp(azArg[1],"column",n2)==0
7337c478bd9Sstevel@tonic-gate               ||
7347c478bd9Sstevel@tonic-gate               strncmp(azArg[1],"columns",n2)==0 ){
7357c478bd9Sstevel@tonic-gate       p->mode = MODE_Column;
7367c478bd9Sstevel@tonic-gate     }else if( strncmp(azArg[1],"list",n2)==0 ){
7377c478bd9Sstevel@tonic-gate       p->mode = MODE_List;
7387c478bd9Sstevel@tonic-gate     }else if( strncmp(azArg[1],"html",n2)==0 ){
7397c478bd9Sstevel@tonic-gate       p->mode = MODE_Html;
7407c478bd9Sstevel@tonic-gate     }else if( strncmp(azArg[1],"insert",n2)==0 ){
7417c478bd9Sstevel@tonic-gate       p->mode = MODE_Insert;
7427c478bd9Sstevel@tonic-gate       if( nArg>=3 ){
7437c478bd9Sstevel@tonic-gate         set_table_name(p, azArg[2]);
7447c478bd9Sstevel@tonic-gate       }else{
7457c478bd9Sstevel@tonic-gate         set_table_name(p, "table");
7467c478bd9Sstevel@tonic-gate       }
7477c478bd9Sstevel@tonic-gate     }else {
7487c478bd9Sstevel@tonic-gate       fprintf(stderr,"mode should be on of: column html insert line list\n");
7497c478bd9Sstevel@tonic-gate     }
7507c478bd9Sstevel@tonic-gate   }else
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate   if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
7537c478bd9Sstevel@tonic-gate     sprintf(p->nullvalue, "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
7547c478bd9Sstevel@tonic-gate   }else
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate   if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
7577c478bd9Sstevel@tonic-gate     if( p->out!=stdout ){
7587c478bd9Sstevel@tonic-gate       fclose(p->out);
7597c478bd9Sstevel@tonic-gate     }
7607c478bd9Sstevel@tonic-gate     if( strcmp(azArg[1],"stdout")==0 ){
7617c478bd9Sstevel@tonic-gate       p->out = stdout;
7627c478bd9Sstevel@tonic-gate       strcpy(p->outfile,"stdout");
7637c478bd9Sstevel@tonic-gate     }else{
7647c478bd9Sstevel@tonic-gate       p->out = fopen(azArg[1], "wb");
7657c478bd9Sstevel@tonic-gate       if( p->out==0 ){
7667c478bd9Sstevel@tonic-gate         fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
7677c478bd9Sstevel@tonic-gate         p->out = stdout;
7687c478bd9Sstevel@tonic-gate       } else {
7697c478bd9Sstevel@tonic-gate          strcpy(p->outfile,azArg[1]);
7707c478bd9Sstevel@tonic-gate       }
7717c478bd9Sstevel@tonic-gate     }
7727c478bd9Sstevel@tonic-gate   }else
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate   if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
7757c478bd9Sstevel@tonic-gate     if( nArg >= 2) {
7767c478bd9Sstevel@tonic-gate       strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
7777c478bd9Sstevel@tonic-gate     }
7787c478bd9Sstevel@tonic-gate     if( nArg >= 3) {
7797c478bd9Sstevel@tonic-gate       strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
7807c478bd9Sstevel@tonic-gate     }
7817c478bd9Sstevel@tonic-gate   }else
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate   if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
7847c478bd9Sstevel@tonic-gate     rc = 1;
7857c478bd9Sstevel@tonic-gate   }else
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate   if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
7887c478bd9Sstevel@tonic-gate     FILE *alt = fopen(azArg[1], "rb");
7897c478bd9Sstevel@tonic-gate     if( alt==0 ){
7907c478bd9Sstevel@tonic-gate       fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
7917c478bd9Sstevel@tonic-gate     }else{
7927c478bd9Sstevel@tonic-gate       process_input(p, alt);
7937c478bd9Sstevel@tonic-gate       fclose(alt);
7947c478bd9Sstevel@tonic-gate     }
7957c478bd9Sstevel@tonic-gate   }else
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate #ifdef SQLITE_HAS_CODEC
7987c478bd9Sstevel@tonic-gate   if( c=='r' && strncmp(azArg[0],"rekey", n)==0 && nArg==4 ){
7997c478bd9Sstevel@tonic-gate     char *zOld = p->zKey;
8007c478bd9Sstevel@tonic-gate     if( zOld==0 ) zOld = "";
8017c478bd9Sstevel@tonic-gate     if( strcmp(azArg[1],zOld) ){
8027c478bd9Sstevel@tonic-gate       fprintf(stderr,"old key is incorrect\n");
8037c478bd9Sstevel@tonic-gate     }else if( strcmp(azArg[2], azArg[3]) ){
8047c478bd9Sstevel@tonic-gate       fprintf(stderr,"2nd copy of new key does not match the 1st\n");
8057c478bd9Sstevel@tonic-gate     }else{
8067c478bd9Sstevel@tonic-gate       sqlite_freemem(p->zKey);
8077c478bd9Sstevel@tonic-gate       p->zKey = sqlite_mprintf("%s", azArg[2]);
8087c478bd9Sstevel@tonic-gate       sqlite_rekey(p->db, p->zKey, strlen(p->zKey));
8097c478bd9Sstevel@tonic-gate     }
8107c478bd9Sstevel@tonic-gate   }else
8117c478bd9Sstevel@tonic-gate #endif
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate   if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
8147c478bd9Sstevel@tonic-gate     struct callback_data data;
8157c478bd9Sstevel@tonic-gate     char *zErrMsg = 0;
8167c478bd9Sstevel@tonic-gate     open_db(p);
8177c478bd9Sstevel@tonic-gate     memcpy(&data, p, sizeof(data));
8187c478bd9Sstevel@tonic-gate     data.showHeader = 0;
8197c478bd9Sstevel@tonic-gate     data.mode = MODE_Semi;
8207c478bd9Sstevel@tonic-gate     if( nArg>1 ){
8217c478bd9Sstevel@tonic-gate       extern int sqliteStrICmp(const char*,const char*);
8227c478bd9Sstevel@tonic-gate       if( sqliteStrICmp(azArg[1],"sqlite_master")==0 ){
8237c478bd9Sstevel@tonic-gate         char *new_argv[2], *new_colv[2];
8247c478bd9Sstevel@tonic-gate         new_argv[0] = "CREATE TABLE sqlite_master (\n"
8257c478bd9Sstevel@tonic-gate                       "  type text,\n"
8267c478bd9Sstevel@tonic-gate                       "  name text,\n"
8277c478bd9Sstevel@tonic-gate                       "  tbl_name text,\n"
8287c478bd9Sstevel@tonic-gate                       "  rootpage integer,\n"
8297c478bd9Sstevel@tonic-gate                       "  sql text\n"
8307c478bd9Sstevel@tonic-gate                       ")";
8317c478bd9Sstevel@tonic-gate         new_argv[1] = 0;
8327c478bd9Sstevel@tonic-gate         new_colv[0] = "sql";
8337c478bd9Sstevel@tonic-gate         new_colv[1] = 0;
8347c478bd9Sstevel@tonic-gate         callback(&data, 1, new_argv, new_colv);
8357c478bd9Sstevel@tonic-gate       }else if( sqliteStrICmp(azArg[1],"sqlite_temp_master")==0 ){
8367c478bd9Sstevel@tonic-gate         char *new_argv[2], *new_colv[2];
8377c478bd9Sstevel@tonic-gate         new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
8387c478bd9Sstevel@tonic-gate                       "  type text,\n"
8397c478bd9Sstevel@tonic-gate                       "  name text,\n"
8407c478bd9Sstevel@tonic-gate                       "  tbl_name text,\n"
8417c478bd9Sstevel@tonic-gate                       "  rootpage integer,\n"
8427c478bd9Sstevel@tonic-gate                       "  sql text\n"
8437c478bd9Sstevel@tonic-gate                       ")";
8447c478bd9Sstevel@tonic-gate         new_argv[1] = 0;
8457c478bd9Sstevel@tonic-gate         new_colv[0] = "sql";
8467c478bd9Sstevel@tonic-gate         new_colv[1] = 0;
8477c478bd9Sstevel@tonic-gate         callback(&data, 1, new_argv, new_colv);
8487c478bd9Sstevel@tonic-gate       }else{
8497c478bd9Sstevel@tonic-gate         sqlite_exec_printf(p->db,
8507c478bd9Sstevel@tonic-gate           "SELECT sql FROM "
8517c478bd9Sstevel@tonic-gate           "  (SELECT * FROM sqlite_master UNION ALL"
8527c478bd9Sstevel@tonic-gate           "   SELECT * FROM sqlite_temp_master) "
8537c478bd9Sstevel@tonic-gate           "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOTNULL "
8547c478bd9Sstevel@tonic-gate           "ORDER BY substr(type,2,1), name",
8557c478bd9Sstevel@tonic-gate           callback, &data, &zErrMsg, azArg[1]);
8567c478bd9Sstevel@tonic-gate       }
8577c478bd9Sstevel@tonic-gate     }else{
8587c478bd9Sstevel@tonic-gate       sqlite_exec(p->db,
8597c478bd9Sstevel@tonic-gate          "SELECT sql FROM "
8607c478bd9Sstevel@tonic-gate          "  (SELECT * FROM sqlite_master UNION ALL"
8617c478bd9Sstevel@tonic-gate          "   SELECT * FROM sqlite_temp_master) "
8627c478bd9Sstevel@tonic-gate          "WHERE type!='meta' AND sql NOTNULL "
8637c478bd9Sstevel@tonic-gate          "ORDER BY substr(type,2,1), name",
8647c478bd9Sstevel@tonic-gate          callback, &data, &zErrMsg
8657c478bd9Sstevel@tonic-gate       );
8667c478bd9Sstevel@tonic-gate     }
8677c478bd9Sstevel@tonic-gate     if( zErrMsg ){
8687c478bd9Sstevel@tonic-gate       fprintf(stderr,"Error: %s\n", zErrMsg);
8697c478bd9Sstevel@tonic-gate       sqlite_freemem(zErrMsg);
8707c478bd9Sstevel@tonic-gate     }
8717c478bd9Sstevel@tonic-gate   }else
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate   if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
8747c478bd9Sstevel@tonic-gate     sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
8757c478bd9Sstevel@tonic-gate   }else
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate   if( c=='s' && strncmp(azArg[0], "show", n)==0){
8787c478bd9Sstevel@tonic-gate     int i;
8797c478bd9Sstevel@tonic-gate     fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
8807c478bd9Sstevel@tonic-gate     fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
8817c478bd9Sstevel@tonic-gate     fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
8827c478bd9Sstevel@tonic-gate     fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
8837c478bd9Sstevel@tonic-gate     fprintf(p->out,"%9.9s: %s\n","nullvalue", p->nullvalue);
8847c478bd9Sstevel@tonic-gate     fprintf(p->out,"%9.9s: %s\n","output",
8857c478bd9Sstevel@tonic-gate                                  strlen(p->outfile) ? p->outfile : "stdout");
8867c478bd9Sstevel@tonic-gate     fprintf(p->out,"%9.9s: %s\n","separator", p->separator);
8877c478bd9Sstevel@tonic-gate     fprintf(p->out,"%9.9s: ","width");
8887c478bd9Sstevel@tonic-gate     for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
8897c478bd9Sstevel@tonic-gate         fprintf(p->out,"%d ",p->colWidth[i]);
8907c478bd9Sstevel@tonic-gate     }
8917c478bd9Sstevel@tonic-gate     fprintf(p->out,"\n\n");
8927c478bd9Sstevel@tonic-gate   }else
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate   if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
8957c478bd9Sstevel@tonic-gate     char **azResult;
8967c478bd9Sstevel@tonic-gate     int nRow, rc;
8977c478bd9Sstevel@tonic-gate     char *zErrMsg;
8987c478bd9Sstevel@tonic-gate     open_db(p);
8997c478bd9Sstevel@tonic-gate     if( nArg==1 ){
9007c478bd9Sstevel@tonic-gate       rc = sqlite_get_table(p->db,
9017c478bd9Sstevel@tonic-gate         "SELECT name FROM sqlite_master "
9027c478bd9Sstevel@tonic-gate         "WHERE type IN ('table','view') "
9037c478bd9Sstevel@tonic-gate         "UNION ALL "
9047c478bd9Sstevel@tonic-gate         "SELECT name FROM sqlite_temp_master "
9057c478bd9Sstevel@tonic-gate         "WHERE type IN ('table','view') "
9067c478bd9Sstevel@tonic-gate         "ORDER BY 1",
9077c478bd9Sstevel@tonic-gate         &azResult, &nRow, 0, &zErrMsg
9087c478bd9Sstevel@tonic-gate       );
9097c478bd9Sstevel@tonic-gate     }else{
9107c478bd9Sstevel@tonic-gate       rc = sqlite_get_table_printf(p->db,
9117c478bd9Sstevel@tonic-gate         "SELECT name FROM sqlite_master "
9127c478bd9Sstevel@tonic-gate         "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
9137c478bd9Sstevel@tonic-gate         "UNION ALL "
9147c478bd9Sstevel@tonic-gate         "SELECT name FROM sqlite_temp_master "
9157c478bd9Sstevel@tonic-gate         "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
9167c478bd9Sstevel@tonic-gate         "ORDER BY 1",
9177c478bd9Sstevel@tonic-gate         &azResult, &nRow, 0, &zErrMsg, azArg[1], azArg[1]
9187c478bd9Sstevel@tonic-gate       );
9197c478bd9Sstevel@tonic-gate     }
9207c478bd9Sstevel@tonic-gate     if( zErrMsg ){
9217c478bd9Sstevel@tonic-gate       fprintf(stderr,"Error: %s\n", zErrMsg);
9227c478bd9Sstevel@tonic-gate       sqlite_freemem(zErrMsg);
9237c478bd9Sstevel@tonic-gate     }
9247c478bd9Sstevel@tonic-gate     if( rc==SQLITE_OK ){
9257c478bd9Sstevel@tonic-gate       int len, maxlen = 0;
9267c478bd9Sstevel@tonic-gate       int i, j;
9277c478bd9Sstevel@tonic-gate       int nPrintCol, nPrintRow;
9287c478bd9Sstevel@tonic-gate       for(i=1; i<=nRow; i++){
9297c478bd9Sstevel@tonic-gate         if( azResult[i]==0 ) continue;
9307c478bd9Sstevel@tonic-gate         len = strlen(azResult[i]);
9317c478bd9Sstevel@tonic-gate         if( len>maxlen ) maxlen = len;
9327c478bd9Sstevel@tonic-gate       }
9337c478bd9Sstevel@tonic-gate       nPrintCol = 80/(maxlen+2);
9347c478bd9Sstevel@tonic-gate       if( nPrintCol<1 ) nPrintCol = 1;
9357c478bd9Sstevel@tonic-gate       nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
9367c478bd9Sstevel@tonic-gate       for(i=0; i<nPrintRow; i++){
9377c478bd9Sstevel@tonic-gate         for(j=i+1; j<=nRow; j+=nPrintRow){
9387c478bd9Sstevel@tonic-gate           char *zSp = j<=nPrintRow ? "" : "  ";
9397c478bd9Sstevel@tonic-gate           printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
9407c478bd9Sstevel@tonic-gate         }
9417c478bd9Sstevel@tonic-gate         printf("\n");
9427c478bd9Sstevel@tonic-gate       }
9437c478bd9Sstevel@tonic-gate     }
9447c478bd9Sstevel@tonic-gate     sqlite_free_table(azResult);
9457c478bd9Sstevel@tonic-gate   }else
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate   if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
9487c478bd9Sstevel@tonic-gate     open_db(p);
9497c478bd9Sstevel@tonic-gate     sqlite_busy_timeout(p->db, atoi(azArg[1]));
9507c478bd9Sstevel@tonic-gate   }else
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate   if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
9537c478bd9Sstevel@tonic-gate     int j;
9547c478bd9Sstevel@tonic-gate     for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
9557c478bd9Sstevel@tonic-gate       p->colWidth[j-1] = atoi(azArg[j]);
9567c478bd9Sstevel@tonic-gate     }
9577c478bd9Sstevel@tonic-gate   }else
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate   {
9607c478bd9Sstevel@tonic-gate     fprintf(stderr, "unknown command or invalid arguments: "
9617c478bd9Sstevel@tonic-gate       " \"%s\". Enter \".help\" for help\n", azArg[0]);
9627c478bd9Sstevel@tonic-gate   }
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate   return rc;
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate /*
9687c478bd9Sstevel@tonic-gate ** Return TRUE if the last non-whitespace character in z[] is a semicolon.
9697c478bd9Sstevel@tonic-gate ** z[] is N characters long.
9707c478bd9Sstevel@tonic-gate */
_ends_with_semicolon(const char * z,int N)9717c478bd9Sstevel@tonic-gate static int _ends_with_semicolon(const char *z, int N){
9727c478bd9Sstevel@tonic-gate   while( N>0 && isspace(z[N-1]) ){ N--; }
9737c478bd9Sstevel@tonic-gate   return N>0 && z[N-1]==';';
9747c478bd9Sstevel@tonic-gate }
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate /*
9777c478bd9Sstevel@tonic-gate ** Test to see if a line consists entirely of whitespace.
9787c478bd9Sstevel@tonic-gate */
_all_whitespace(const char * z)9797c478bd9Sstevel@tonic-gate static int _all_whitespace(const char *z){
9807c478bd9Sstevel@tonic-gate   for(; *z; z++){
9817c478bd9Sstevel@tonic-gate     if( isspace(*z) ) continue;
9827c478bd9Sstevel@tonic-gate     if( *z=='/' && z[1]=='*' ){
9837c478bd9Sstevel@tonic-gate       z += 2;
9847c478bd9Sstevel@tonic-gate       while( *z && (*z!='*' || z[1]!='/') ){ z++; }
9857c478bd9Sstevel@tonic-gate       if( *z==0 ) return 0;
9867c478bd9Sstevel@tonic-gate       z++;
9877c478bd9Sstevel@tonic-gate       continue;
9887c478bd9Sstevel@tonic-gate     }
9897c478bd9Sstevel@tonic-gate     if( *z=='-' && z[1]=='-' ){
9907c478bd9Sstevel@tonic-gate       z += 2;
9917c478bd9Sstevel@tonic-gate       while( *z && *z!='\n' ){ z++; }
9927c478bd9Sstevel@tonic-gate       if( *z==0 ) return 1;
9937c478bd9Sstevel@tonic-gate       continue;
9947c478bd9Sstevel@tonic-gate     }
9957c478bd9Sstevel@tonic-gate     return 0;
9967c478bd9Sstevel@tonic-gate   }
9977c478bd9Sstevel@tonic-gate   return 1;
9987c478bd9Sstevel@tonic-gate }
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate /*
10017c478bd9Sstevel@tonic-gate ** Return TRUE if the line typed in is an SQL command terminator other
10027c478bd9Sstevel@tonic-gate ** than a semi-colon.  The SQL Server style "go" command is understood
10037c478bd9Sstevel@tonic-gate ** as is the Oracle "/".
10047c478bd9Sstevel@tonic-gate */
_is_command_terminator(const char * zLine)10057c478bd9Sstevel@tonic-gate static int _is_command_terminator(const char *zLine){
10067c478bd9Sstevel@tonic-gate   extern int sqliteStrNICmp(const char*,const char*,int);
10077c478bd9Sstevel@tonic-gate   while( isspace(*zLine) ){ zLine++; };
10087c478bd9Sstevel@tonic-gate   if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ) return 1;  /* Oracle */
10097c478bd9Sstevel@tonic-gate   if( sqliteStrNICmp(zLine,"go",2)==0 && _all_whitespace(&zLine[2]) ){
10107c478bd9Sstevel@tonic-gate     return 1;  /* SQL Server */
10117c478bd9Sstevel@tonic-gate   }
10127c478bd9Sstevel@tonic-gate   return 0;
10137c478bd9Sstevel@tonic-gate }
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate /*
10167c478bd9Sstevel@tonic-gate ** Read input from *in and process it.  If *in==0 then input
10177c478bd9Sstevel@tonic-gate ** is interactive - the user is typing it it.  Otherwise, input
10187c478bd9Sstevel@tonic-gate ** is coming from a file or device.  A prompt is issued and history
10197c478bd9Sstevel@tonic-gate ** is saved only if input is interactive.  An interrupt signal will
10207c478bd9Sstevel@tonic-gate ** cause this routine to exit immediately, unless input is interactive.
10217c478bd9Sstevel@tonic-gate */
process_input(struct callback_data * p,FILE * in)10227c478bd9Sstevel@tonic-gate static void process_input(struct callback_data *p, FILE *in){
10237c478bd9Sstevel@tonic-gate   char *zLine;
10247c478bd9Sstevel@tonic-gate   char *zSql = 0;
10257c478bd9Sstevel@tonic-gate   int nSql = 0;
10267c478bd9Sstevel@tonic-gate   char *zErrMsg;
10277c478bd9Sstevel@tonic-gate   int rc;
10287c478bd9Sstevel@tonic-gate   while( fflush(p->out), (zLine = one_input_line(zSql, in))!=0 ){
10297c478bd9Sstevel@tonic-gate     if( seenInterrupt ){
10307c478bd9Sstevel@tonic-gate       if( in!=0 ) break;
10317c478bd9Sstevel@tonic-gate       seenInterrupt = 0;
10327c478bd9Sstevel@tonic-gate     }
10337c478bd9Sstevel@tonic-gate     if( p->echoOn ) printf("%s\n", zLine);
10347c478bd9Sstevel@tonic-gate     if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
10357c478bd9Sstevel@tonic-gate     if( zLine && zLine[0]=='.' && nSql==0 ){
10367c478bd9Sstevel@tonic-gate       int rc = do_meta_command(zLine, p);
10377c478bd9Sstevel@tonic-gate       free(zLine);
10387c478bd9Sstevel@tonic-gate       if( rc ) break;
10397c478bd9Sstevel@tonic-gate       continue;
10407c478bd9Sstevel@tonic-gate     }
10417c478bd9Sstevel@tonic-gate     if( _is_command_terminator(zLine) ){
10427c478bd9Sstevel@tonic-gate       strcpy(zLine,";");
10437c478bd9Sstevel@tonic-gate     }
10447c478bd9Sstevel@tonic-gate     if( zSql==0 ){
10457c478bd9Sstevel@tonic-gate       int i;
10467c478bd9Sstevel@tonic-gate       for(i=0; zLine[i] && isspace(zLine[i]); i++){}
10477c478bd9Sstevel@tonic-gate       if( zLine[i]!=0 ){
10487c478bd9Sstevel@tonic-gate         nSql = strlen(zLine);
10497c478bd9Sstevel@tonic-gate         zSql = malloc( nSql+1 );
10507c478bd9Sstevel@tonic-gate         strcpy(zSql, zLine);
10517c478bd9Sstevel@tonic-gate       }
10527c478bd9Sstevel@tonic-gate     }else{
10537c478bd9Sstevel@tonic-gate       int len = strlen(zLine);
10547c478bd9Sstevel@tonic-gate       zSql = realloc( zSql, nSql + len + 2 );
10557c478bd9Sstevel@tonic-gate       if( zSql==0 ){
10567c478bd9Sstevel@tonic-gate         fprintf(stderr,"%s: out of memory!\n", Argv0);
10577c478bd9Sstevel@tonic-gate         exit(1);
10587c478bd9Sstevel@tonic-gate       }
10597c478bd9Sstevel@tonic-gate       strcpy(&zSql[nSql++], "\n");
10607c478bd9Sstevel@tonic-gate       strcpy(&zSql[nSql], zLine);
10617c478bd9Sstevel@tonic-gate       nSql += len;
10627c478bd9Sstevel@tonic-gate     }
10637c478bd9Sstevel@tonic-gate     free(zLine);
10647c478bd9Sstevel@tonic-gate     if( zSql && _ends_with_semicolon(zSql, nSql) && sqlite_complete(zSql) ){
10657c478bd9Sstevel@tonic-gate       p->cnt = 0;
10667c478bd9Sstevel@tonic-gate       open_db(p);
10677c478bd9Sstevel@tonic-gate       rc = sqlite_exec(p->db, zSql, callback, p, &zErrMsg);
10687c478bd9Sstevel@tonic-gate       if( rc || zErrMsg ){
10697c478bd9Sstevel@tonic-gate         if( in!=0 && !p->echoOn ) printf("%s\n",zSql);
10707c478bd9Sstevel@tonic-gate         if( zErrMsg!=0 ){
10717c478bd9Sstevel@tonic-gate           printf("SQL error: %s\n", zErrMsg);
10727c478bd9Sstevel@tonic-gate           sqlite_freemem(zErrMsg);
10737c478bd9Sstevel@tonic-gate           zErrMsg = 0;
10747c478bd9Sstevel@tonic-gate         }else{
10757c478bd9Sstevel@tonic-gate           printf("SQL error: %s\n", sqlite_error_string(rc));
10767c478bd9Sstevel@tonic-gate         }
10777c478bd9Sstevel@tonic-gate       }
10787c478bd9Sstevel@tonic-gate       free(zSql);
10797c478bd9Sstevel@tonic-gate       zSql = 0;
10807c478bd9Sstevel@tonic-gate       nSql = 0;
10817c478bd9Sstevel@tonic-gate     }
10827c478bd9Sstevel@tonic-gate   }
10837c478bd9Sstevel@tonic-gate   if( zSql ){
10847c478bd9Sstevel@tonic-gate     if( !_all_whitespace(zSql) ) printf("Incomplete SQL: %s\n", zSql);
10857c478bd9Sstevel@tonic-gate     free(zSql);
10867c478bd9Sstevel@tonic-gate   }
10877c478bd9Sstevel@tonic-gate }
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate /*
10907c478bd9Sstevel@tonic-gate ** Return a pathname which is the user's home directory.  A
10917c478bd9Sstevel@tonic-gate ** 0 return indicates an error of some kind.  Space to hold the
10927c478bd9Sstevel@tonic-gate ** resulting string is obtained from malloc().  The calling
10937c478bd9Sstevel@tonic-gate ** function should free the result.
10947c478bd9Sstevel@tonic-gate */
find_home_dir(void)10957c478bd9Sstevel@tonic-gate static char *find_home_dir(void){
10967c478bd9Sstevel@tonic-gate   char *home_dir = NULL;
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate #if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
10997c478bd9Sstevel@tonic-gate   struct passwd *pwent;
11007c478bd9Sstevel@tonic-gate   uid_t uid = getuid();
11017c478bd9Sstevel@tonic-gate   if( (pwent=getpwuid(uid)) != NULL) {
11027c478bd9Sstevel@tonic-gate     home_dir = pwent->pw_dir;
11037c478bd9Sstevel@tonic-gate   }
11047c478bd9Sstevel@tonic-gate #endif
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate #ifdef __MACOS__
11077c478bd9Sstevel@tonic-gate   char home_path[_MAX_PATH+1];
11087c478bd9Sstevel@tonic-gate   home_dir = getcwd(home_path, _MAX_PATH);
11097c478bd9Sstevel@tonic-gate #endif
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate   if (!home_dir) {
11127c478bd9Sstevel@tonic-gate     home_dir = getenv("HOME");
11137c478bd9Sstevel@tonic-gate     if (!home_dir) {
11147c478bd9Sstevel@tonic-gate       home_dir = getenv("HOMEPATH"); /* Windows? */
11157c478bd9Sstevel@tonic-gate     }
11167c478bd9Sstevel@tonic-gate   }
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate #if defined(_WIN32) || defined(WIN32)
11197c478bd9Sstevel@tonic-gate   if (!home_dir) {
11207c478bd9Sstevel@tonic-gate     home_dir = "c:";
11217c478bd9Sstevel@tonic-gate   }
11227c478bd9Sstevel@tonic-gate #endif
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate   if( home_dir ){
11257c478bd9Sstevel@tonic-gate     char *z = malloc( strlen(home_dir)+1 );
11267c478bd9Sstevel@tonic-gate     if( z ) strcpy(z, home_dir);
11277c478bd9Sstevel@tonic-gate     home_dir = z;
11287c478bd9Sstevel@tonic-gate   }
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate   return home_dir;
11317c478bd9Sstevel@tonic-gate }
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate /*
11347c478bd9Sstevel@tonic-gate ** Read input from the file given by sqliterc_override.  Or if that
11357c478bd9Sstevel@tonic-gate ** parameter is NULL, take input from ~/.sqliterc
11367c478bd9Sstevel@tonic-gate */
process_sqliterc(struct callback_data * p,const char * sqliterc_override)11377c478bd9Sstevel@tonic-gate static void process_sqliterc(
11387c478bd9Sstevel@tonic-gate   struct callback_data *p,        /* Configuration data */
11397c478bd9Sstevel@tonic-gate   const char *sqliterc_override   /* Name of config file. NULL to use default */
11407c478bd9Sstevel@tonic-gate ){
11417c478bd9Sstevel@tonic-gate   char *home_dir = NULL;
11427c478bd9Sstevel@tonic-gate   const char *sqliterc = sqliterc_override;
11437c478bd9Sstevel@tonic-gate   char *zBuf;
11447c478bd9Sstevel@tonic-gate   FILE *in = NULL;
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate   if (sqliterc == NULL) {
11477c478bd9Sstevel@tonic-gate     home_dir = find_home_dir();
11487c478bd9Sstevel@tonic-gate     if( home_dir==0 ){
11497c478bd9Sstevel@tonic-gate       fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0);
11507c478bd9Sstevel@tonic-gate       return;
11517c478bd9Sstevel@tonic-gate     }
11527c478bd9Sstevel@tonic-gate     zBuf = malloc(strlen(home_dir) + 15);
11537c478bd9Sstevel@tonic-gate     if( zBuf==0 ){
11547c478bd9Sstevel@tonic-gate       fprintf(stderr,"%s: out of memory!\n", Argv0);
11557c478bd9Sstevel@tonic-gate       exit(1);
11567c478bd9Sstevel@tonic-gate     }
11577c478bd9Sstevel@tonic-gate     sprintf(zBuf,"%s/.sqliterc",home_dir);
11587c478bd9Sstevel@tonic-gate     free(home_dir);
11597c478bd9Sstevel@tonic-gate     sqliterc = (const char*)zBuf;
11607c478bd9Sstevel@tonic-gate   }
11617c478bd9Sstevel@tonic-gate   in = fopen(sqliterc,"rb");
11627c478bd9Sstevel@tonic-gate   if( in ){
11637c478bd9Sstevel@tonic-gate     if( isatty(fileno(stdout)) ){
11647c478bd9Sstevel@tonic-gate       printf("Loading resources from %s\n",sqliterc);
11657c478bd9Sstevel@tonic-gate     }
11667c478bd9Sstevel@tonic-gate     process_input(p,in);
11677c478bd9Sstevel@tonic-gate     fclose(in);
11687c478bd9Sstevel@tonic-gate   }
11697c478bd9Sstevel@tonic-gate   return;
11707c478bd9Sstevel@tonic-gate }
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate /*
11737c478bd9Sstevel@tonic-gate ** Show available command line options
11747c478bd9Sstevel@tonic-gate */
1175*2a8bcb4eSToomas Soome static const char zOptions[] =
11767c478bd9Sstevel@tonic-gate   "   -init filename       read/process named file\n"
11777c478bd9Sstevel@tonic-gate   "   -echo                print commands before execution\n"
11787c478bd9Sstevel@tonic-gate   "   -[no]header          turn headers on or off\n"
11797c478bd9Sstevel@tonic-gate   "   -column              set output mode to 'column'\n"
11807c478bd9Sstevel@tonic-gate   "   -html                set output mode to HTML\n"
11817c478bd9Sstevel@tonic-gate #ifdef SQLITE_HAS_CODEC
11827c478bd9Sstevel@tonic-gate   "   -key KEY             encryption key\n"
1183*2a8bcb4eSToomas Soome #endif
11847c478bd9Sstevel@tonic-gate   "   -line                set output mode to 'line'\n"
11857c478bd9Sstevel@tonic-gate   "   -list                set output mode to 'list'\n"
11867c478bd9Sstevel@tonic-gate   "   -separator 'x'       set output field separator (|)\n"
11877c478bd9Sstevel@tonic-gate   "   -nullvalue 'text'    set text string for NULL values\n"
11887c478bd9Sstevel@tonic-gate   "   -version             show SQLite version\n"
11897c478bd9Sstevel@tonic-gate   "   -help                show this text, also show dot-commands\n"
11907c478bd9Sstevel@tonic-gate ;
usage(int showDetail)11917c478bd9Sstevel@tonic-gate static void usage(int showDetail){
11927c478bd9Sstevel@tonic-gate   fprintf(stderr, "Usage: %s [OPTIONS] FILENAME [SQL]\n", Argv0);
11937c478bd9Sstevel@tonic-gate   if( showDetail ){
11947c478bd9Sstevel@tonic-gate     fprintf(stderr, "Options are:\n%s", zOptions);
11957c478bd9Sstevel@tonic-gate   }else{
11967c478bd9Sstevel@tonic-gate     fprintf(stderr, "Use the -help option for additional information\n");
11977c478bd9Sstevel@tonic-gate   }
11987c478bd9Sstevel@tonic-gate   exit(1);
11997c478bd9Sstevel@tonic-gate }
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate /*
12027c478bd9Sstevel@tonic-gate ** Initialize the state information in data
12037c478bd9Sstevel@tonic-gate */
main_init(struct callback_data * data)12047c478bd9Sstevel@tonic-gate void main_init(struct callback_data *data) {
12057c478bd9Sstevel@tonic-gate   memset(data, 0, sizeof(*data));
12067c478bd9Sstevel@tonic-gate   data->mode = MODE_List;
12077c478bd9Sstevel@tonic-gate   strcpy(data->separator,"|");
12087c478bd9Sstevel@tonic-gate   data->showHeader = 0;
12097c478bd9Sstevel@tonic-gate   strcpy(mainPrompt,"sqlite> ");
12107c478bd9Sstevel@tonic-gate   strcpy(continuePrompt,"   ...> ");
12117c478bd9Sstevel@tonic-gate }
12127c478bd9Sstevel@tonic-gate 
main(int argc,char ** argv)12137c478bd9Sstevel@tonic-gate int main(int argc, char **argv){
12147c478bd9Sstevel@tonic-gate   char *zErrMsg = 0;
12157c478bd9Sstevel@tonic-gate   struct callback_data data;
12167c478bd9Sstevel@tonic-gate   const char *zInitFile = 0;
12177c478bd9Sstevel@tonic-gate   char *zFirstCmd = 0;
12187c478bd9Sstevel@tonic-gate   int i;
12197c478bd9Sstevel@tonic-gate   extern int sqliteOsFileExists(const char*);
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate   sqlite_temp_directory = "/etc/svc/volatile";		/* SUNW addition */
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate #ifdef __MACOS__
12247c478bd9Sstevel@tonic-gate   argc = ccommand(&argv);
12257c478bd9Sstevel@tonic-gate #endif
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate   Argv0 = argv[0];
12287c478bd9Sstevel@tonic-gate   main_init(&data);
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate   /* Make sure we have a valid signal handler early, before anything
12317c478bd9Sstevel@tonic-gate   ** else is done.
12327c478bd9Sstevel@tonic-gate   */
12337c478bd9Sstevel@tonic-gate #ifdef SIGINT
12347c478bd9Sstevel@tonic-gate   signal(SIGINT, interrupt_handler);
12357c478bd9Sstevel@tonic-gate #endif
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate   /* Do an initial pass through the command-line argument to locate
12387c478bd9Sstevel@tonic-gate   ** the name of the database file, the name of the initialization file,
12397c478bd9Sstevel@tonic-gate   ** and the first command to execute.
12407c478bd9Sstevel@tonic-gate   */
12417c478bd9Sstevel@tonic-gate   for(i=1; i<argc-1; i++){
12427c478bd9Sstevel@tonic-gate     if( argv[i][0]!='-' ) break;
12437c478bd9Sstevel@tonic-gate     if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){
12447c478bd9Sstevel@tonic-gate       i++;
12457c478bd9Sstevel@tonic-gate     }else if( strcmp(argv[i],"-init")==0 ){
12467c478bd9Sstevel@tonic-gate       i++;
12477c478bd9Sstevel@tonic-gate       zInitFile = argv[i];
12487c478bd9Sstevel@tonic-gate     }else if( strcmp(argv[i],"-key")==0 ){
12497c478bd9Sstevel@tonic-gate       i++;
12507c478bd9Sstevel@tonic-gate       data.zKey = sqlite_mprintf("%s",argv[i]);
12517c478bd9Sstevel@tonic-gate     }
12527c478bd9Sstevel@tonic-gate   }
12537c478bd9Sstevel@tonic-gate   if( i<argc ){
12547c478bd9Sstevel@tonic-gate     data.zDbFilename = argv[i++];
12557c478bd9Sstevel@tonic-gate   }else{
12567c478bd9Sstevel@tonic-gate     data.zDbFilename = ":memory:";
12577c478bd9Sstevel@tonic-gate   }
12587c478bd9Sstevel@tonic-gate   if( i<argc ){
12597c478bd9Sstevel@tonic-gate     zFirstCmd = argv[i++];
12607c478bd9Sstevel@tonic-gate   }
12617c478bd9Sstevel@tonic-gate   data.out = stdout;
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate   /* Go ahead and open the database file if it already exists.  If the
12647c478bd9Sstevel@tonic-gate   ** file does not exist, delay opening it.  This prevents empty database
12657c478bd9Sstevel@tonic-gate   ** files from being created if a user mistypes the database name argument
12667c478bd9Sstevel@tonic-gate   ** to the sqlite command-line tool.
12677c478bd9Sstevel@tonic-gate   */
12687c478bd9Sstevel@tonic-gate   if( sqliteOsFileExists(data.zDbFilename) ){
12697c478bd9Sstevel@tonic-gate     open_db(&data);
12707c478bd9Sstevel@tonic-gate   }
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate   /* Process the initialization file if there is one.  If no -init option
12737c478bd9Sstevel@tonic-gate   ** is given on the command line, look for a file named ~/.sqliterc and
12747c478bd9Sstevel@tonic-gate   ** try to process it.
12757c478bd9Sstevel@tonic-gate   */
12767c478bd9Sstevel@tonic-gate   process_sqliterc(&data,zInitFile);
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate   /* Make a second pass through the command-line argument and set
12797c478bd9Sstevel@tonic-gate   ** options.  This second pass is delayed until after the initialization
12807c478bd9Sstevel@tonic-gate   ** file is processed so that the command-line arguments will override
12817c478bd9Sstevel@tonic-gate   ** settings in the initialization file.
12827c478bd9Sstevel@tonic-gate   */
12837c478bd9Sstevel@tonic-gate   for(i=1; i<argc && argv[i][0]=='-'; i++){
12847c478bd9Sstevel@tonic-gate     char *z = argv[i];
12857c478bd9Sstevel@tonic-gate     if( strcmp(z,"-init")==0 || strcmp(z,"-key")==0 ){
12867c478bd9Sstevel@tonic-gate       i++;
12877c478bd9Sstevel@tonic-gate     }else if( strcmp(z,"-html")==0 ){
12887c478bd9Sstevel@tonic-gate       data.mode = MODE_Html;
12897c478bd9Sstevel@tonic-gate     }else if( strcmp(z,"-list")==0 ){
12907c478bd9Sstevel@tonic-gate       data.mode = MODE_List;
12917c478bd9Sstevel@tonic-gate     }else if( strcmp(z,"-line")==0 ){
12927c478bd9Sstevel@tonic-gate       data.mode = MODE_Line;
12937c478bd9Sstevel@tonic-gate     }else if( strcmp(z,"-column")==0 ){
12947c478bd9Sstevel@tonic-gate       data.mode = MODE_Column;
12957c478bd9Sstevel@tonic-gate     }else if( strcmp(z,"-separator")==0 ){
12967c478bd9Sstevel@tonic-gate       i++;
12977c478bd9Sstevel@tonic-gate       sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[i]);
12987c478bd9Sstevel@tonic-gate     }else if( strcmp(z,"-nullvalue")==0 ){
12997c478bd9Sstevel@tonic-gate       i++;
13007c478bd9Sstevel@tonic-gate       sprintf(data.nullvalue,"%.*s",(int)sizeof(data.nullvalue)-1,argv[i]);
13017c478bd9Sstevel@tonic-gate     }else if( strcmp(z,"-header")==0 ){
13027c478bd9Sstevel@tonic-gate       data.showHeader = 1;
13037c478bd9Sstevel@tonic-gate     }else if( strcmp(z,"-noheader")==0 ){
13047c478bd9Sstevel@tonic-gate       data.showHeader = 0;
13057c478bd9Sstevel@tonic-gate     }else if( strcmp(z,"-echo")==0 ){
13067c478bd9Sstevel@tonic-gate       data.echoOn = 1;
13077c478bd9Sstevel@tonic-gate     }else if( strcmp(z,"-version")==0 ){
13087c478bd9Sstevel@tonic-gate       printf("%s\n", sqlite_version);
13097c478bd9Sstevel@tonic-gate       return 1;
13107c478bd9Sstevel@tonic-gate     }else if( strcmp(z,"-help")==0 ){
13117c478bd9Sstevel@tonic-gate       usage(1);
13127c478bd9Sstevel@tonic-gate     }else{
13137c478bd9Sstevel@tonic-gate       fprintf(stderr,"%s: unknown option: %s\n", Argv0, z);
13147c478bd9Sstevel@tonic-gate       fprintf(stderr,"Use -help for a list of options.\n");
13157c478bd9Sstevel@tonic-gate       return 1;
13167c478bd9Sstevel@tonic-gate     }
13177c478bd9Sstevel@tonic-gate   }
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate   if( zFirstCmd ){
13207c478bd9Sstevel@tonic-gate     /* Run just the command that follows the database name
13217c478bd9Sstevel@tonic-gate     */
13227c478bd9Sstevel@tonic-gate     if( zFirstCmd[0]=='.' ){
13237c478bd9Sstevel@tonic-gate       do_meta_command(zFirstCmd, &data);
13247c478bd9Sstevel@tonic-gate       exit(0);
13257c478bd9Sstevel@tonic-gate     }else{
13267c478bd9Sstevel@tonic-gate       int rc;
13277c478bd9Sstevel@tonic-gate       open_db(&data);
13287c478bd9Sstevel@tonic-gate       rc = sqlite_exec(data.db, zFirstCmd, callback, &data, &zErrMsg);
13297c478bd9Sstevel@tonic-gate       if( rc!=0 && zErrMsg!=0 ){
13307c478bd9Sstevel@tonic-gate         fprintf(stderr,"SQL error: %s\n", zErrMsg);
13317c478bd9Sstevel@tonic-gate         exit(1);
13327c478bd9Sstevel@tonic-gate       }
13337c478bd9Sstevel@tonic-gate     }
13347c478bd9Sstevel@tonic-gate   }else{
13357c478bd9Sstevel@tonic-gate     /* Run commands received from standard input
13367c478bd9Sstevel@tonic-gate     */
13377c478bd9Sstevel@tonic-gate     if( isatty(fileno(stdout)) && isatty(fileno(stdin)) ){
13387c478bd9Sstevel@tonic-gate       char *zHome;
13397c478bd9Sstevel@tonic-gate       char *zHistory = 0;
13407c478bd9Sstevel@tonic-gate       printf(
13417c478bd9Sstevel@tonic-gate         "SQLite version %s\n"
13427c478bd9Sstevel@tonic-gate         "Enter \".help\" for instructions\n",
13437c478bd9Sstevel@tonic-gate         sqlite_version
13447c478bd9Sstevel@tonic-gate       );
13457c478bd9Sstevel@tonic-gate       zHome = find_home_dir();
13467c478bd9Sstevel@tonic-gate       if( zHome && (zHistory = malloc(strlen(zHome)+20))!=0 ){
13477c478bd9Sstevel@tonic-gate         sprintf(zHistory,"%s/.sqlite_history", zHome);
13487c478bd9Sstevel@tonic-gate       }
13497c478bd9Sstevel@tonic-gate       if( zHistory ) read_history(zHistory);
13507c478bd9Sstevel@tonic-gate       process_input(&data, 0);
13517c478bd9Sstevel@tonic-gate       if( zHistory ){
13527c478bd9Sstevel@tonic-gate         stifle_history(100);
13537c478bd9Sstevel@tonic-gate         write_history(zHistory);
13547c478bd9Sstevel@tonic-gate       }
13557c478bd9Sstevel@tonic-gate     }else{
13567c478bd9Sstevel@tonic-gate       process_input(&data, stdin);
13577c478bd9Sstevel@tonic-gate     }
13587c478bd9Sstevel@tonic-gate   }
13597c478bd9Sstevel@tonic-gate   set_table_name(&data, 0);
13607c478bd9Sstevel@tonic-gate   if( db ) sqlite_close(db);
13617c478bd9Sstevel@tonic-gate   return 0;
13627c478bd9Sstevel@tonic-gate }
1363