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