/*
** 2001 September 15
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains code to implement the "sqlite3" command line
** utility for accessing SQLite databases.
*/
#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
/* This needs to come before any includes for MSVC compiler */
#define _CRT_SECURE_NO_WARNINGS
#endif
typedef unsigned int u32;
typedef unsigned short int u16;

/*
** Limit input nesting via .read or any other input redirect.
** It's not too expensive, so a generous allowance can be made.
*/
#define MAX_INPUT_NESTING 25

/*
** Used to prevent warnings about unused parameters
*/
#define UNUSED_PARAMETER(x) (void)(x)

/*
** Number of elements in an array
*/
#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))

/*
** Optionally #include a user-defined header, whereby compilation options
** may be set prior to where they take effect, but after platform setup.
** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
** file. Note that this macro has a like effect on sqlite3.c compilation.
*/
# define SHELL_STRINGIFY_(f) #f
# define SHELL_STRINGIFY(f) SHELL_STRINGIFY_(f)
#ifdef SQLITE_CUSTOM_INCLUDE
# include SHELL_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
#endif

/*
** If SQLITE_SHELL_FIDDLE is defined then the shell is modified
** somewhat for use as a WASM module in a web browser. This flag
** should only be used when building the "fiddle" web application, as
** the browser-mode build has much different user input requirements
** and this build mode rewires the user input subsystem to account for
** that.
*/
#if defined(SQLITE_SHELL_FIDDLE)
# undef SQLITE_OMIT_LOAD_EXTENSION
# define SQLITE_OMIT_LOAD_EXTENSION 1
#endif

/*
** Warning pragmas copied from msvc.h in the core.
*/
#if defined(_MSC_VER)
#pragma warning(disable : 4054)
#pragma warning(disable : 4055)
#pragma warning(disable : 4100)
#pragma warning(disable : 4127)
#pragma warning(disable : 4130)
#pragma warning(disable : 4152)
#pragma warning(disable : 4189)
#pragma warning(disable : 4206)
#pragma warning(disable : 4210)
#pragma warning(disable : 4232)
#pragma warning(disable : 4244)
#pragma warning(disable : 4305)
#pragma warning(disable : 4306)
#pragma warning(disable : 4702)
#pragma warning(disable : 4706)
#endif /* defined(_MSC_VER) */

/*
** No support for loadable extensions in VxWorks.
*/
#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
# define SQLITE_OMIT_LOAD_EXTENSION 1
#endif

/*
** Enable large-file support for fopen() and friends on unix.
*/
#ifndef SQLITE_DISABLE_LFS
# define _LARGE_FILE       1
# ifndef _FILE_OFFSET_BITS
#   define _FILE_OFFSET_BITS 64
# endif
# define _LARGEFILE_SOURCE 1
#endif

#if defined(SQLITE_SHELL_FIDDLE) && !defined(_POSIX_SOURCE)
/*
** emcc requires _POSIX_SOURCE (or one of several similar defines)
** to expose strdup().
*/
# define _POSIX_SOURCE
#endif

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <stdint.h>
#include <time.h>
#include "sqlite3.h"
typedef sqlite3_int64 i64;
typedef sqlite3_uint64 u64;
typedef unsigned char u8;
#include <ctype.h>
#include <stdarg.h>
#ifndef _WIN32
# include <sys/time.h>
# include <sys/ioctl.h>
#endif

#if !defined(_WIN32) && !defined(WIN32)
# include <signal.h>
# if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
#  include <pwd.h>
# endif
#endif
#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
# include <unistd.h>
# include <dirent.h>
# define GETPID getpid
# if defined(__MINGW32__)
#  define DIRENT dirent
#  ifndef S_ISLNK
#   define S_ISLNK(mode) (0)
#  endif
# endif
#else
# define GETPID (int)GetCurrentProcessId
#endif
#include <sys/types.h>
#include <sys/stat.h>

#if HAVE_READLINE
# include <readline/readline.h>
# include <readline/history.h>
#elif HAVE_EDITLINE
/* If both HAVE_READLINE and HAVE_EDITLINE are true, assume that this
** libedit installation does not have its own headers, instead using
** those from libreadline. */
# include <editline/readline.h>
#endif

#if HAVE_EDITLINE || HAVE_READLINE

# define shell_add_history(X) add_history(X)
# define shell_read_history(X) read_history(X)
# define shell_write_history(X) write_history(X)
# define shell_stifle_history(X) stifle_history(X)
# define shell_readline(X) readline(X)

#elif HAVE_LINENOISE

# include "linenoise.h"
# define shell_add_history(X) linenoiseHistoryAdd(X)
# define shell_read_history(X) linenoiseHistoryLoad(X)
# define shell_write_history(X) linenoiseHistorySave(X)
# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
# define shell_readline(X) linenoise(X)

#else

# define shell_read_history(X)
# define shell_write_history(X)
# define shell_stifle_history(X)

# define SHELL_USE_LOCAL_GETLINE 1
#endif

#ifndef deliberate_fall_through
/* Quiet some compilers about some of our intentional code. */
# if defined(GCC_VERSION) && GCC_VERSION>=7000000
#  define deliberate_fall_through __attribute__((fallthrough));
# else
#  define deliberate_fall_through
# endif
#endif

#if defined(_WIN32) || defined(WIN32)
#  include <io.h>
#  include <fcntl.h>
#  define isatty(h) _isatty(h)
#  ifndef access
#   define access(f,m) _access((f),(m))
#  endif
#  ifndef unlink
#   define unlink _unlink
#  endif
#  ifndef strdup
#   define strdup _strdup
#  endif
#  undef pclose
#  define pclose _pclose
#else
 /* Make sure isatty() has a prototype. */
 extern int isatty(int);

# if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
  /* popen and pclose are not C89 functions and so are
  ** sometimes omitted from the <stdio.h> header */
   extern FILE *popen(const char*,const char*);
   extern int pclose(FILE*);
# else
#  define SQLITE_OMIT_POPEN 1
# endif
#endif

#if defined(_WIN32_WCE)
/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
 * thus we always assume that we have a console. That can be
 * overridden with the -batch command line option.
 */
#define isatty(x) 1
#endif

/* ctype macros that work with signed characters */
#define IsSpace(X)  isspace((unsigned char)X)
#define IsDigit(X)  isdigit((unsigned char)X)
#define ToLower(X)  (char)tolower((unsigned char)X)
#define IsAlnum(X)  isalnum((unsigned char)X)
#define IsAlpha(X)  isalpha((unsigned char)X)

#if defined(_WIN32) || defined(WIN32)
#undef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

/* string conversion routines only needed on Win32 */
extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
#endif

INCLUDE ../ext/misc/sqlite3_stdio.h
INCLUDE ../ext/misc/sqlite3_stdio.c
INCLUDE ../ext/qrf/qrf.h
INCLUDE ../ext/qrf/qrf.c

/* Use console I/O package as a direct INCLUDE. */
#define SQLITE_INTERNAL_LINKAGE static

#ifdef SQLITE_SHELL_FIDDLE
/* Deselect most features from the console I/O package for Fiddle. */
# define SQLITE_CIO_NO_REDIRECT
# define SQLITE_CIO_NO_CLASSIFY
# define SQLITE_CIO_NO_TRANSLATE
# define SQLITE_CIO_NO_SETMODE
# define SQLITE_CIO_NO_FLUSH
#endif

/*
** The source code for several run-time loadable extensions is inserted
** below by the ../tool/mkshellc.tcl script.  Before processing that included
** code, we need to override some macros to make the included program code
** work here in the middle of this regular program.
*/
#define SQLITE_EXTENSION_INIT1
#define SQLITE_EXTENSION_INIT2(X) (void)(X)

INCLUDE ../ext/misc/windirent.h
INCLUDE ../ext/misc/memtrace.c
INCLUDE ../ext/misc/pcachetrace.c
INCLUDE ../ext/misc/shathree.c
INCLUDE ../ext/misc/sha1.c
INCLUDE ../ext/misc/uint.c
INCLUDE ../ext/misc/decimal.c
INCLUDE ../ext/misc/base64.c
INCLUDE ../ext/misc/base85.c
INCLUDE ../ext/misc/ieee754.c
INCLUDE ../ext/misc/series.c
INCLUDE ../ext/misc/regexp.c
#ifndef SQLITE_SHELL_FIDDLE
INCLUDE ../ext/misc/fileio.c
INCLUDE ../ext/misc/completion.c
INCLUDE ../ext/misc/appendvfs.c
#endif
#ifdef SQLITE_HAVE_ZLIB
INCLUDE ../ext/misc/zipfile.c
INCLUDE ../ext/misc/sqlar.c
#endif
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
INCLUDE ../ext/expert/sqlite3expert.h
INCLUDE ../ext/expert/sqlite3expert.c
#endif
INCLUDE ../ext/intck/sqlite3intck.h
INCLUDE ../ext/intck/sqlite3intck.c
INCLUDE ../ext/misc/stmtrand.c
INCLUDE ../ext/misc/vfstrace.c
INCLUDE ../ext/misc/diskused.c

#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
#define SQLITE_SHELL_HAVE_RECOVER 1
#else
#define SQLITE_SHELL_HAVE_RECOVER 0
#endif
#if SQLITE_SHELL_HAVE_RECOVER
INCLUDE ../ext/recover/sqlite3recover.h
# ifndef SQLITE_HAVE_SQLITE3R
INCLUDE ../ext/recover/dbdata.c
INCLUDE ../ext/recover/sqlite3recover.c
# endif /* SQLITE_HAVE_SQLITE3R */
#endif
#ifdef SQLITE_SHELL_EXTSRC
# include SHELL_STRINGIFY(SQLITE_SHELL_EXTSRC)
#endif

#if defined(SQLITE_ENABLE_SESSION)
/*
** State information for a single open session
*/
typedef struct OpenSession OpenSession;
struct OpenSession {
  char *zName;             /* Symbolic name for this session */
  int nFilter;             /* Number of xFilter rejection GLOB patterns */
  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
  sqlite3_session *p;      /* The open session */
};
#endif

#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
typedef struct ExpertInfo ExpertInfo;
struct ExpertInfo {
  sqlite3expert *pExpert;
  int bVerbose;
};
#endif

/* All the parameters that determine how to render query results.
*/
typedef struct Mode {
  u8 autoExplain;        /* Automatically turn on .explain mode */
  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to each SQL stmt */
  u8 autoEQPtrace;       /* autoEQP is in trace mode */
  u8 scanstatsOn;        /* True to display scan stats before each finalize */
  u8 bAutoScreenWidth;   /* Using the TTY to determine screen width */
  u8 mFlags;             /* MFLG_ECHO, MFLG_CRLF, etc. */
  u8 eMode;              /* One of the MODE_ values */
  sqlite3_qrf_spec spec; /* Spec to be passed into QRF */
} Mode;

/* Flags for Mode.mFlags */
#define MFLG_ECHO  0x01  /* Echo inputs to output */
#define MFLG_CRLF  0x02  /* Use CR/LF output line endings */
#define MFLG_HDR   0x04  /* .header used to change headers on/off */

/* A file that needs to be deleted, but only after a delay.
*/
typedef struct Unlink {
  sqlite3_int64 tm;      /* Unlink after this time */
  char *zFN;             /* Name of file.  Space from sqlite3_malloc() */
} Unlink;

/*
** State information about the database connection is contained in an
** instance of the following structure.
*/
typedef struct ShellState ShellState;
struct ShellState {
  sqlite3 *db;           /* The database */
  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
  u8 nEqpLevel;          /* Depth of the EQP output graph */
  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
  u8 bSafeMode;          /* True to prohibit unsafe operations */
  u8 bSafeModePersist;   /* The long-term value of bSafeMode */
  u8 eRestoreState;      /* See comments above doAutoDetectRestore() */
  unsigned statsOn;      /* True to display memory stats before each finalize */
  unsigned mEqpLines;    /* Mask of vertical lines in the EQP output graph */
  u8 nPopOutput;         /* Revert .output settings when reaching zero */
  u8 nPopMode;           /* Revert .mode settings when reaching zero */
  u8 enableTimer;        /* Enable the timer.  2: permanently 1: only once */
  u8 bDelimitNonprint;   /* Add \001...\002 around non-printing in prompts */
  int inputNesting;      /* Track nesting level of .read and other redirects */
  double prevTimer;      /* Last reported timer value */
  double tmProgress;     /* --timeout option for .progress */
  i64 lineno;            /* Line number of last line read from in */
  const char *zInFile;   /* Name of the input file */
  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
  FILE *in;              /* Read commands from this stream */
  FILE *out;             /* Write results here */
  FILE *traceOut;        /* Output for sqlite3_trace() */
  int nErr;              /* Number of errors seen */
  int writableSchema;    /* True if PRAGMA writable_schema=ON */
  int nCheck;            /* Number of ".check" commands run */
  unsigned nProgress;    /* Number of progress callbacks encountered */
  unsigned mxProgress;   /* Maximum progress callbacks before failing */
  unsigned flgProgress;  /* Flags for the progress callback */
  unsigned shellFlgs;    /* Various flags */
  unsigned nTestRun;     /* Number of test cases run */
  unsigned nTestErr;     /* Number of test cases that failed */
  sqlite3_int64 szMax;   /* --maxsize argument to .open */
  char *zDestTable;      /* Name of destination table when MODE_Insert */
  char *zTempFile;       /* Temporary file that might need deleting */
  char *zErrPrefix;      /* Alternative error message prefix */
  sqlite3_stmt *pStmt;   /* Current statement if any. */
  FILE *pLog;            /* Write log output here */
  char *azPrompt[2];     /* Main and continuation prompt strings */
  Mode mode;             /* Current display mode */
  Mode *aModeStack;      /* Backups */
  unsigned nModeStack;   /* Number of entries in aModeStack[] */
  unsigned nUnlink;      /* Number of entries in aDelayUnlink[] */
  Unlink *aUnlink;       /* Temp files to unlink after a delay */
  struct SavedMode {     /* Ability to define custom mode configurations */
    char *zTag;            /* Name of this saved mode */
    Mode mode;              /* The saved mode */
  } *aSavedModes;        /* Array of saved .mode settings. system malloc() */
  int nSavedModes;       /* Number of saved .mode settings */
  struct AuxDb {         /* Storage space for auxiliary database connections */
    sqlite3 *db;               /* Connection pointer */
    const char *zDbFilename;   /* Filename used to open the connection */
    char *zFreeOnClose;        /* Free this memory allocation on close */
    int mFlgs;                 /* 0x001: Use zDbFilenaem for prompt */
#if defined(SQLITE_ENABLE_SESSION)
    int nSession;              /* Number of active sessions */
    OpenSession aSession[4];   /* Array of sessions.  [0] is in focus. */
#endif
  } aAuxDb[5],           /* Array of all database connections */
    *pAuxDb;             /* Currently active database connection */
  char *zNonce;          /* Nonce for temporary safe-mode escapes */
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
  ExpertInfo expert;     /* Valid if previous command was ".expert OPT..." */
#endif
  struct DotCmdLine {    /* Info about arguments to a dot-command */
    const char *zOrig;      /* Original text of the dot-command */
    char *zCopy;            /* Copy of zOrig, from malloc() */
    int nAlloc;             /* Size of allocates for arrays below */
    int nArg;               /* Number of argument slots actually used */
    char **azArg;           /* Pointer to each argument, dequoted */
    int *aiOfst;            /* Offset into zOrig[] for start of each arg */
    char *abQuot;           /* True if the argment was originally quoted */
  } dot;
#ifdef SQLITE_SHELL_FIDDLE
  struct {
    const char * zInput; /* Input string from wasm/JS proxy */
    const char * zPos;   /* Cursor pos into zInput */
    const char * zDefaultDbName; /* Default name for db file */
  } wasm;
#endif
  char zTestcase[30];           /* Name of current test case */
  char outfile[FILENAME_MAX];   /* Filename for *out */
};

#ifdef SQLITE_SHELL_FIDDLE
static ShellState shellState;
#endif


/* Allowed values for ShellState.mode.autoEQP
*/
#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
#define AUTOEQP_on       1           /* Automatic EQP is on */
#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
#define AUTOEQP_full     3           /* Show full EXPLAIN */

/* Allowed values for ShellState.openMode
*/
#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
#define SHELL_OPEN_NORMAL      1      /* Normal database file */
#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
#define SHELL_OPEN_DESERIALIZE 4      /* Open using sqlite3_deserialize() */
#define SHELL_OPEN_HEXDB       5      /* Use "dbtotxt" output as data source */

/* Allowed values for ShellState.eTraceType
*/
#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */

/* Bits in the ShellState.flgProgress variable */
#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progress
                                   ** callback limit is reached, and for each
                                   ** top-level SQL statement */
#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
#define SHELL_PROGRESS_TMOUT 0x08  /* Stop after tmProgress seconds */

/* Names of values for Mode.spec.eEsc and Mode.spec.eText
*/
static const char *qrfEscNames[] = { "auto", "off", "ascii", "symbol" };
static const char *qrfQuoteNames[] = 
      { "off","off","sql","hex","csv","tcl","json","relaxed"};

/*
** These are the allowed shellFlgs values
*/
#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
#define SHFLG_NoErrLineno    0x00000010 /* Omit line numbers from error msgs */
#define SHFLG_CountChanges   0x00000020 /* .changes setting */
#define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
#define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
#define SHFLG_TestingMode    0x00000400 /* allow unsafe testing features */

/*
** Macros for testing and setting shellFlgs
*/
#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))

/*
** These are the allowed values for Mode.eMode.  There is a lot of overlap
** between these values and the Mode.spec.eStyle values, but they are not
** one-to-one, and thus need to be tracked separately.
*/
#define MODE_Ascii     0  /* Use ASCII unit and record separators (0x1F/0x1E) */
#define MODE_Box       1  /* Unicode box-drawing characters */
#define MODE_C         2  /* Comma-separated list of C-strings */
#define MODE_Column    3  /* One record per line in neat columns */
#define MODE_Count     4  /* Output only a count of the rows of output */
#define MODE_Csv       5  /* Quote strings, numbers are plain */
#define MODE_Html      6  /* Generate an XHTML table */
#define MODE_Insert    7  /* Generate SQL "insert" statements */
#define MODE_JAtom     8  /* Comma-separated list of JSON atoms */
#define MODE_JObject   9  /* One JSON object per row */
#define MODE_Json     10  /* Output JSON */
#define MODE_Line     11  /* One column per line.  Blank line between records */
#define MODE_List     12  /* One record per line with a separator */
#define MODE_Markdown 13  /* Markdown formatting */
#define MODE_Off      14  /* No query output shown */
#define MODE_Psql     15  /* Similar to psql */
#define MODE_QBox     16  /* BOX with SQL-quoted content */
#define MODE_Quote    17  /* Quote values as for SQL */
#define MODE_Split    18  /* Split-column mode */
#define MODE_Table    19  /* MySQL-style table formatting */
#define MODE_Tabs     20  /* Tab-separated values */
#define MODE_Tcl      21  /* Space-separated list of TCL strings */
#define MODE_Www      22  /* Full web-page output */

#define MODE_BUILTIN  22  /* Maximum built-in mode */
#define MODE_BATCH    50  /* Default mode for batch processing */
#define MODE_TTY      51  /* Default mode for interactive processing */
#define MODE_USER     75  /* First user-defined mode */
#define MODE_N_USER   25  /* Maximum number of user-defined modes */

/*
** Information about built-in display modes
*/
typedef struct ModeInfo ModeInfo;
struct ModeInfo {
  char zName[9];         /* Symbolic name of the mode */
  unsigned char eCSep;   /* Column separator */
  unsigned char eRSep;   /* Row separator */
  unsigned char eNull;   /* Null representation */
  unsigned char eText;   /* Default text encoding */
  unsigned char eHdr;    /* Default header encoding. */
  unsigned char eBlob;   /* Default blob encoding. */
  unsigned char bHdr;    /* Show headers by default.  0: n/a, 1: no 2: yes */
  unsigned char eStyle;  /* Underlying QRF style */
  unsigned char eCx;     /* 0: other, 1: line, 2: columnar */
  unsigned char mFlg;    /* Flags. 1=border-off 2=split-column */
};

/* String constants used by built-in modes */
static const char *aModeStr[] = 
  /* 0    1       2       3       4     5        6        7        8    */
   { 0,   "\n",   "|",    " ",    ",",  "\r\n",  "\036",  "\037",  "\t",
     "",  "NULL", "null", "\"\"", ": ",                                   };
  /* 9    10      11      12      13                                    */

static const ModeInfo aModeInfo[] = {
/*   zName      eCSep  eRSep eNull eText eHdr eBlob bHdr eStyle eCx mFlg */
  { "ascii",    7,     6,    9,    1,    1,    0,   1,   12,    0,  0 },
  { "box",      0,     0,    9,    1,    1,    0,   2,   1,     2,  0 },
  { "c",        4,     1,    10,   5,    5,    4,   1,   12,    0,  0 },
  { "column",   0,     0,    9,    1,    1,    0,   2,   2,     2,  0 },
  { "count",    0,     0,    0,    0,    0,    0,   0,   3,     0,  0 },
  { "csv",      4,     5,    9,    3,    3,    0,   1,   12,    0,  0 },
  { "html",     0,     0,    9,    4,    4,    0,   2,   7,     0,  0 },
  { "insert",   0,     0,    10,   2,    2,    0,   1,   8,     0,  0 },
  { "jatom",    4,     1,    11,   6,    6,    0,   1,   12,    0,  0 },
  { "jobject",  0,     1,    11,   6,    6,    0,   0,   10,    0,  0 },
  { "json",     0,     0,    11,   6,    6,    0,   0,   9,     0,  0 },
  { "line",     13,    1,    9,    1,    1,    0,   0,   11,    1,  0 },
  { "list",     2,     1,    9,    1,    1,    0,   1,   12,    0,  0 },
  { "markdown", 0,     0,    9,    1,    1,    0,   2,   13,    2,  0 },
  { "off",      0,     0,    0,    0,    0,    0,   0,   14,    0,  0 },
  { "psql",     0,     0,    9,    1,    1,    0,   2,   19,    2,  1 },
  { "qbox",     0,     0,    10,   2,    1,    0,   2,   1,     2,  0 },
  { "quote",    4,     1,    10,   2,    2,    0,   1,   12,    0,  0 },
  { "split",    0,     0,    9,    1,    1,    0,   1,   2,     2,  2 },
  { "table",    0,     0,    9,    1,    1,    0,   2,   19,    2,  0 },
  { "tabs",     8,     1,    9,    3,    3,    0,   1,   12,    0,  0 },
  { "tcl",      3,     1,    12,   5,    5,    4,   1,   12,    0,  0 },
  { "www",      0,     0,    9,    4,    4,    0,   2,   7,     0,  0 }
};     /*       |     /     /      |     /    /     |    |       \
       **       |    /     /       |    /    /      |    |        \_ 2: columnar
       ** Index into aModeStr[]    |   /    /       |    |           1: line
       **                          |  /    /        |    |           0: other
       **                          | /    /         |     \
       **           text encoding  |/     |    show |      \
       **      v-------------------'      |   hdrs? |       The QRF style
       **      0: n/a                blob |   v-----'
       **      1: plain        v----------'   0: n/a
       **      2: sql          0: auto        1: no         
       **      3: csv          1: as-text     2: yes
       **      4: html         2: sql
       **      5: c            3: hex
       **      6: json         4: c
       **                      5: json
       **                      6: size
       ******************************************************************/

/*
** Default values for the various QRF limits
*/
#ifndef DFLT_CHAR_LIMIT
# define DFLT_CHAR_LIMIT  300
#endif
#ifndef DFLT_LINE_LIMIT
# define DFLT_LINE_LIMIT  5
#endif
#ifndef DFLT_TITLE_LIMIT
# define DFLT_TITLE_LIMIT 20
#endif
#ifndef DFLT_MULTI_INSERT
# define DFLT_MULTI_INSERT 3000
#endif

/*
** If the following flag is set, then command execution stops
** at an error if we are not interactive.
*/
static int bail_on_error = 0;

/*
** Treat stdin as an interactive input if the following variable
** is true.  Otherwise, assume stdin is connected to a file or pipe.
*/
static int stdin_is_interactive = 1;

/*
** Treat stdout like a TTY if true.
*/
static int stdout_is_console = 1;

/*
** Use this value as the width of the output device.  Or, figure it
** out at runtime if the value is negative.  Or use a default width
** if this value is zero.
*/
static int stdout_tty_width = -1;

/*
** The following is the open SQLite database.  We make a pointer
** to this database a static variable so that it can be accessed
** by the SIGINT handler to interrupt database processing.
*/
static sqlite3 *globalDb = 0;

/*
** This is a global pointer to the main ShellState variable.  This
** exists so that the atexit() callback can access the ShellState
** object to do some cleanup.
*/
static struct ShellState *globalShellState = 0;

/*
** True if an interrupt (Control-C) has been received.
*/
static volatile int seenInterrupt = 0;

/*
** This is the name of our program. It is set in main(), used
** in a number of other places, mostly for error messages.
*/
static char *Argv0;

/*
** Write I/O traces to the following stream.
*/
#ifdef SQLITE_ENABLE_IOTRACE
static FILE *iotrace = 0;
#endif

/*
** Output routines that are able to redirect to memory rather than
** doing actually I/O.
**                                                Works like.
**                                                --------------
**   cli_printf(FILE*, const char*, ...);         fprintf()
**   cli_write(FILE*, const char*, int);          write()
**   cli_puts(const char*, FILE*);                fputs()
**   cli_vprintf(FILE*, const char*, va_list);    vfprintf()
**
** These are just thin wrappers with the following added semantics:
** If the file-scope variable cli_output_capture is not NULL, and
** if the FILE* argument is stdout or stderr, then rather than
** writing to stdout/stdout, append the text to the cli_output_capture
** variable.
**
** The cli_exit(int) routine works like exit() except that it
** first dumps any capture output to stdout.
*/
static sqlite3_str *cli_output_capture = 0; 
static int cli_printf(FILE *out, const char *zFormat, ...){
  va_list ap;
  int rc;
  va_start(ap,zFormat);
  if( cli_output_capture && (out==stdout || out==stderr) ){
    sqlite3_str_vappendf(cli_output_capture, zFormat, ap);
    rc = 1;
  }else{
    rc = sqlite3_vfprintf(out, zFormat, ap);
  }
  va_end(ap);
  return rc;
}
static int cli_write(FILE *out, const char *zData, int nData){
  if( cli_output_capture && (out==stdout || out==stderr) ){
    sqlite3_str_append(cli_output_capture, zData, nData);
#ifdef _WIN32
  }else if( out==stdout || out==stderr ){
    nData = sqlite3_fprintf(out, "%.*s", nData, zData);
#endif
  }else{
    nData = (int)fwrite(zData, 1, nData, out);
  }
  return nData;
}
static int cli_puts(const char *zText, FILE *out){
  if( cli_output_capture && (out==stdout || out==stderr) ){
    sqlite3_str_appendall(cli_output_capture, zText);
    return 1;
  }
  return sqlite3_fputs(zText, out);
}
#if 0 /* Not currently used - available if we need it later */
static int cli_vprintf(FILE *out, const char *zFormat, va_list ap){
  if( cli_output_capture && (out==stdout || out==stderr) ){
    sqlite3_str_vappendf(cli_output_capture, zFormat, ap);
    return 1;
  }else{
    return sqlite3_vfprintf(out, zFormat, ap);
  }
}
#endif
static void cli_exit(int rc){
  if( cli_output_capture ){
    char *z = sqlite3_str_finish(cli_output_capture);
    sqlite3_fputs(z, stdout);
    fflush(stdout);
  }
  exit(rc);
}


#define eputz(z) cli_puts(z,stderr)
#define sputz(fp,z) cli_puts(z,fp)

/* A version of strcmp() that works with NULL values */
static int cli_strcmp(const char *a, const char *b){
  if( a==0 ) a = "";
  if( b==0 ) b = "";
  return strcmp(a,b);
}
static int cli_strncmp(const char *a, const char *b, size_t n){
  if( a==0 ) a = "";
  if( b==0 ) b = "";
  return strncmp(a,b,n);
}

/* Return the current wall-clock time in microseconds since the
** Unix epoch (1970-01-01T00:00:00Z)
*/
static sqlite3_int64 timeOfDay(void){
#if defined(_WIN64) && _WIN32_WINNT >= _WIN32_WINNT_WIN8
  sqlite3_uint64 t;
  FILETIME tm;
  GetSystemTimePreciseAsFileTime(&tm);
  t =  ((u64)tm.dwHighDateTime<<32) | (u64)tm.dwLowDateTime;
  t += 116444736000000000LL;
  t /= 10;
  return t;
#elif defined(_WIN32)
  static sqlite3_vfs *clockVfs = 0;
  sqlite3_int64 t;
  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
  if( clockVfs==0 ) return 0;  /* Never actually happens */
  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
    clockVfs->xCurrentTimeInt64(clockVfs, &t);
  }else{
    double r;
    clockVfs->xCurrentTime(clockVfs, &r);
    t = (sqlite3_int64)(r*86400000.0);
  }
  return t*1000;
#else
  struct timeval sNow;
  (void)gettimeofday(&sNow,0);
  return ((i64)sNow.tv_sec)*1000000 + sNow.tv_usec;
#endif
}

/* Indicate out-of-memory and exit. */
static void shell_out_of_memory(void){
  eputz("Error: out of memory\n");
  cli_exit(1);
}

/* Check a pointer to see if it is NULL.  If it is NULL, exit with an
** out-of-memory error.
*/
static void shell_check_oom(const void *p){
  if( p==0 ) shell_out_of_memory();
}

/*
** This routine works like printf in that its first argument is a
** format string and subsequent arguments are values to be substituted
** in place of % fields.  The result of formatting this string
** is written to iotrace.
*/
#ifdef SQLITE_ENABLE_IOTRACE
static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
  va_list ap;
  char *z;
  if( iotrace==0 ) return;
  va_start(ap, zFormat);
  z = sqlite3_vmprintf(zFormat, ap);
  va_end(ap);
  cli_printf(iotrace, "%s", z);
  sqlite3_free(z);
}
#endif

/*
** Compute a string length that is limited to what can be stored in
** lower 30 bits of a 32-bit signed integer.
*/
static int strlen30(const char *z){
  size_t n;
  if( z==0 ) return 0;
  n = strlen(z);
  return n>0x3fffffff ? 0x3fffffff : (int)n;
}

/*
** Return open FILE * if zFile exists, can be opened for read
** and is an ordinary file or a character stream source.
** Otherwise return 0.
*/
static FILE * openChrSource(const char *zFile){
#if defined(_WIN32) || defined(WIN32)
  struct __stat64 x = {0};
# define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
  /* On Windows, open first, then check the stream nature. This order
  ** is necessary because _stat() and sibs, when checking a named pipe,
  ** effectively break the pipe as its supplier sees it. */
  FILE *rv = sqlite3_fopen(zFile, "rb");
  if( rv==0 ) return 0;
  if( _fstat64(_fileno(rv), &x) != 0
      || !STAT_CHR_SRC(x.st_mode)){
    fclose(rv);
    rv = 0;
  }
  return rv;
#else
  struct stat x = {0};
  int rc = stat(zFile, &x);
# define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
  if( rc!=0 ) return 0;
  if( STAT_CHR_SRC(x.st_mode) ){
    return sqlite3_fopen(zFile, "rb");
  }else{
    return 0;
  }
#endif
#undef STAT_CHR_SRC
}

/*
** This routine reads a line of text from FILE in, stores
** the text in memory obtained from malloc() and returns a pointer
** to the text.  NULL is returned at end of file, or if malloc()
** fails, or if the length of the line is longer than about a gigabyte.
**
** If zLine is not NULL then it is a malloced buffer returned from
** a previous call to this routine that may be reused.
*/
static char *local_getline(char *zLine, FILE *in){
  int nLine = zLine==0 ? 0 : 100;
  int n = 0;

  while( 1 ){
    if( n+100>nLine ){
      if( nLine>=1073741773 ){
        free(zLine);
        return 0;
      }
      nLine = nLine*2 + 100;
      zLine = realloc(zLine, nLine);
      shell_check_oom(zLine);
    }
    if( sqlite3_fgets(&zLine[n], nLine - n, in)==0 ){
      if( n==0 ){
        free(zLine);
        return 0;
      }
      zLine[n] = 0;
      break;
    }
    while( zLine[n] ) n++;
    if( n>0 && zLine[n-1]=='\n' ){
      n--;
      if( n>0 && zLine[n-1]=='\r' ) n--;
      zLine[n] = 0;
      break;
    }
  }
  return zLine;
}

/*
** Return true if either the SQLITE_NO_COLOR compile-time option is used
** or if the NO_COLOR environment variable exists
*/
static int shellNoColor(void){
#if defined(SQLITE_NO_COLOR) || defined(SQLITE_SHELL_FIDDLE)
  return 1;
#else
  return getenv("NO_COLOR")!=0;
#endif
}

/*
** The SQLITE_PS_APPDEF macro should be set to the name of a function
** that accepts a single "int" argument and returns a "const char *"
** that is guaranteed to be non-NULL.  The value returned depends on the
** argument:
**
**    1      Default main prompt.
**    2      Default continuation prompt.
**    3      Environment variable to override main prompt ("SQLITE_PS1")
**    4      Environment variable to override continuatio ("SQLITE_PS2")
**    'A'    The name of the application.  (Nominally "SQLite")
**    'V'    Version number including patch.  (ex: "3.54.1")
**    'v'    Version number without patch.  (ex: "3.54")
**
*/
#ifdef SQLITE_PS_APPDEF
extern const char *SQLITE_PS_APPDEF(int);
#else
# define SQLITE_PS_APPDEF shellPromptAppDef
static const char *shellPromptAppDef(int c){
  switch( c ){
    /* The default main prompt string */
    case 1:
#if   defined(SQLITE_PS1)
      return SQLITE_PS1;
#else
      if( shellNoColor() ){
        return "/A-/v /f-> ";
      }else{
        return "/e[1;32m/A-/v /e[1;/x33/:36/;m/m/e[3m/;/f/;/e[0m-> ";
      }
#endif

    /* The default continuation prompt string */
    case 2:
#if   defined(SQLITE_PS2)
      return SQLITE_PS2;
#else
      if( shellNoColor() ){
        return "/B/C-> ";
      }else{
        return "/B/e[1;/x33/:36/;m/C/e[0m-> ";
      }
#endif

    /* Name of environment variables that override the prompt strings
    ** of cases 1 and 2 */
    case 3:   return "SQLITE_PS1";
    case 4:   return "SQLITE_PS2";

    /* Name of the application */
#ifndef SQLITE_CLI_APPNAME
    case 'A': return "SQLite";
#else
    case 'A': return SHELL_STRINGIFY(SQLITE_CLI_APPNAME);
#endif

    /* Full version number of the application, including patch level */
    case 'V': return sqlite3_libversion();

    /* Version number without the patch level */
    case 'v': {
      static char zRel[16];
      const char *zF = sqlite3_libversion();
      const char *zD = strrchr(zF,'.');
      if( zD && (size_t)(zD-zF)<sizeof(zRel)-1 ){
        memcpy(zRel,zF,(size_t)(zD-zF));
        zRel[(size_t)(zD-zF)] = 0;
        return zRel;
      }else{
        return zF;
      }
    }
  }
  return "";
}
#endif /* !defined(SQLITE_PS_APPDEF) */



/*
** Return the raw (unexpanded) prompt string.  This will be the
** first of the following that exist:
**
**    *  The prompt string specified by the ".prompt" command.
**    *  The value of the SQLITE_PS1 (or SQLITE_PS2) environment variable.
**    *  The default prompt string.
**
** The main prompt is returned if the argument is zero and the
** continuation prompt is returned if the argument is 1.
*/
static const char *prompt_string(ShellState *p, int bContinue){
  const char *zPS;
  assert( bContinue==0 || bContinue==1 );
  if( p->azPrompt[bContinue] ){
    return p->azPrompt[bContinue];
  }
#ifndef SQLITE_SHELL_FIDDLE
  zPS = getenv(SQLITE_PS_APPDEF(3+bContinue));
  if( zPS ) return zPS;
#endif
  return SQLITE_PS_APPDEF(1+bContinue);
}

/*
** Return the name of the open database file, to be used for prompt
** expansion purposes.
*/
static const char *prompt_filename(ShellState *p, const char *zMemoryName){
  sqlite3_filename pFN;
  const char *zFN = 0;
  if( p->pAuxDb->mFlgs & 0x01 ){
    zFN = p->pAuxDb->zDbFilename;
  }else if( p->db && (pFN = sqlite3_db_filename(p->db,0))!=0 ){
    zFN = sqlite3_filename_database(pFN);
  }
  if( zFN==0 || zFN[0]==0 ){
    zFN = p->pAuxDb->zDbFilename;
  }
  if( zFN==0 || zFN[0]==0 || cli_strcmp(zFN,":memory:")==0 ){
    zFN = zMemoryName;
  }
  return zFN;
}

/*
** Return the name of the computer on which we are running.
*/
static const char *prompt_hostname(int bFull){
#ifdef _WIN32
  const char *z = getenv("COMPUTERNAME");
  if( z==0 || z[0]==0 ) z = "?";
  return z;
#else
  static char zHost[256];
  if( gethostname(zHost, sizeof(zHost)) ){
    zHost[0] = '?';
    zHost[1] = 0;
  }
  if( !bFull ){
    char *p = strchr(zHost,'.');
    if( p ) p[0] = 0;
  }
  return zHost;
#endif
}

/*
** Return the username.  This is taken from an environment variable
** and can thus be forged.  Do not depend on it.
*/
static const char *prompt_user(void){
  const char *z;
#ifdef _WIN32
  z = getenv("USERNAME");
  if( z==0 || z[0]==0 ) z = "?";
#else
  z = getenv("USER");
  if( z==0 || z[0]==0 ) z = getenv("LOGNAME");
  if( z==0 || z[0]==0 ) z = "?";
#endif
  return z;
}

/* If z[] begins with one or more ANSI X3.64 (VT100) escape sequences
** return the number of bytes in all such escape sequences.  Return
** zero if there are no valid escape sequences.
*/
static int nAnsiEscape(const char *z){
  int i = 0;
  while( z[i]=='\033' && z[i+1]=='[' ){
    int k = i+2;
    while( z[k]>=0x30 && z[k]<=0x3f ){ k++; }
    while( z[k]>=0x20 && z[k]<=0x2f ){ k++; }
    if( z[k]<0x40 || z[k]>0x7e ) break;
    i = k+1;
  }
  return i;
}

/*
** Expand escapes in the given input prompt string.  Return the
** expanded prompt in memory obtained from sqlite3_malloc().  The
** caller is responsible for freeing the memory.
**
** Early prototypes use U+005c '\\' as the escape character.  But
** that is an escape character for shells and C and many other languages,
** which can lead to nested quoting problems and confusion.  The
** U+0025 '%' character was also tried, and that works pretty well on
** unix, but % is special to many formats on Windows.  So now we
** use a forward-slash, U+002f '/', which seems to pass through
** every shell and "make" without issue.
*/
static char *expand_prompt(
  ShellState *p,        /* The current shell state */
  const char *zPrior,   /* Prior input lines not yet processed */
  const char *zPrompt   /* Prompt string to be expanded */
){
  sqlite3_str *pOut = sqlite3_str_new(0);
  int i;
  char c;
  unsigned int mOff = 0;    /* Bitmask of FALSE for if/then/else */
  int idxSpace = -1;
  int iDate = -1;
  for(i=0; zPrompt[i]; i++){
    if( zPrompt[i]!='/' ) continue;
    if( i>0 ){
      if( !mOff ) sqlite3_str_append(pOut, zPrompt, i);
      zPrompt += i;
      i = 0;
    }
    /* At this point zPrompt[0] is a / character and all prior
    ** characters have already been loaded into pOut.  Process the
    ** escape sequence that zPrompt points to. */
    c = zPrompt[1];
    if( c==0 ){
      /* / at the end of a line is silently ignored */
      break;
    }
    if( c=='/' ){
      /* // maps into a single /  */
      zPrompt++;
      continue;
    }
    if( c>='0' && c<='7' ){
      /* /nnn becomes a single byte given by octal nnn */
      int v = c - '0';
      i++;
      while( i<=2 && zPrompt[i+1]>='0' && zPrompt[i+1]<='7' ){
        v = v*8 + zPrompt[++i] - '0';
      }
      if( !mOff ) sqlite3_str_appendchar(pOut, 1, v);
      zPrompt += i+1;
      i = -1;
      continue;
    }
    if( c=='e' ){
      /* /e is shorthand for /033 which is U+001B "Escape" */
      if( !mOff ) sqlite3_str_append(pOut, "\033", 1);
      zPrompt += 2;
      i = -1;
      continue;
    }
    if( c=='A' || c=='V' || c=='v' ){
      /* /A expands to the application name */
      /* /V expands to the version number with patch level */
      /* /v expands to the version number without the patch level */
      if( !mOff ) sqlite3_str_appendall(pOut, SQLITE_PS_APPDEF(c));
      zPrompt += 2;
      i = -1;
      continue;
    }

    /* The intent of the following codes it to provide alternative
    ** text displays depending on whether or not the connection is
    ** currently in a transaction.  Example 1:  Show a "*" before the ">"
    ** like psql:  (Note:  We encode the "*" as /052 to avoid closing
    ** out this comment.)
    **
    **         .prompt 'sqlite/x/052/;> '
    **
    ** Example 2: Show database filename is blue if not in a transaction,
    ** or red if within a transaction:
    **
    **         .prompt '/e[1;/x31/:34/;m~f>/e[0m '
    */
    if( c==':' ){
      /* ELSE: toggle display on/off */
      mOff ^= 1;
      zPrompt += 2;
      i = -1;
      continue;
    }
    if( c==';' ){
      /* ENDIF: Turn display on */
      mOff >>= 1;
      zPrompt += 2;
      i = -1;
      continue;
    }
    if( c=='x' ){
      /* /x turns display off not in a transaction, on if in txn */
      mOff = (mOff<<1) | (p->db==0 || sqlite3_get_autocommit(p->db)!=0);
      zPrompt += 2;
      i = -1;
      continue;
    }
    if( c=='r' ){
      /* /r turns display off if database is read/write, on if read-only */
      mOff = (mOff<<1) | (p->db==0 || sqlite3_db_readonly(p->db,0)==0);
      zPrompt += 2;
      i = -1;
      continue;
    }
    if( c=='m' ){
      /* /m turns display on for an in-memory database and off for persistent */
      mOff = (mOff<<1) | (p->db && prompt_filename(p,0)!=0);
      zPrompt += 2;
      i = -1;
      continue;
    }     

    if( c=='f' || c=='F' || c=='~' ){
      /* /f becomes the tail of the database filename */
      /* /F becomes the full pathname */
      /* /~ becomes the full pathname relative to $HOME */
      if( !mOff ){
        const char *zFN = prompt_filename(p,"memory");
        if( c=='f' ){
#ifdef _WIN32
          const char *zTail = strrchr(zFN,'\\');
#else
          const char *zTail = strrchr(zFN,'/');
#endif
          if( zTail && zTail[1] ) zFN = &zTail[1];
        }else if( c=='~' ){
          const char *zHOME = getenv("HOME");
          size_t nHOME = zHOME ? strlen(zHOME) : 0;
          if( nHOME<strlen(zFN)
           && zHOME!=0 && zFN!=0  /* <--- these silence compiler warnings */
           && memcmp(zHOME,zFN,nHOME)==0
          ){
            sqlite3_str_append(pOut,"~",1);
            zFN += nHOME;
          }
        }
        sqlite3_str_appendall(pOut, zFN);
      }
      zPrompt += 2;
      i = -1;
      continue;
    }

    if( c=='h' || c=='H' ){
      /* /h becomes hostname up to the first '.' */
      /* /H is the full hostname */
      if( !mOff ){
        sqlite3_str_appendall(pOut, prompt_hostname(c=='H'));
      }
      zPrompt += 2;
      i = -1;
      continue;
    }
    if( c=='u' ){
      /* /u becomes the username (taken from environment variables) */
      if( !mOff ){
        sqlite3_str_appendall(pOut, prompt_user());
      }
      zPrompt += 2;
      i = -1;
      continue;
    }

    if( c=='C' ){
      /* /C becomes text needed to terminate current input */
      if( !mOff ){
        sqlite3_int64 R = zPrior ? sqlite3_incomplete(zPrior) : 0;
        int cc = (R>>16)&0xff;
        int nParen = R>>32;
        int eSemi = (R>>8)&0xff;
        if( cc==0 ){
          /* no-op */
        }else if( cc=='-' ){
          sqlite3_str_append(pOut,"\\n",3);
        }else if( cc=='/' ){
          sqlite3_str_append(pOut,"*/",2);
        }else{
          sqlite3_str_appendchar(pOut, 1, cc);
        }
        if( nParen>0 ){
          sqlite3_str_appendf(pOut, "%.*c",nParen,')');
        }
        if( eSemi==1 ){
          sqlite3_str_append(pOut, ";", 1);
        }else if( eSemi==2 ){
          sqlite3_str_append(pOut, "END;", 4);
        }else if( eSemi==3 ){
          sqlite3_str_append(pOut, ";END;", 5);
        }
      }
      zPrompt += 2;
      i = -1;
      continue;
    }

    if( c=='B' ){
      /* /B is a no-op for the main prompt.  For the continuation prompt,
      ** /B expands to zero or more spaces to make the continuation prompt
      ** at least as wide as the main prompt. */
      if( !mOff ) idxSpace = sqlite3_str_length(pOut);
      zPrompt += 2;
      i = -1;
      continue;
    }

    if( c=='D' ){
      /* /D.../D replaces all of the text between the two /D escapes with
      ** the result from strftime(). */
      if( iDate<0 ){
        iDate = sqlite3_str_length(pOut);
      }else{
        if( !mOff ){
          time_t now;
          char zBuf[200];
          zBuf[0] = 0;
          time(&now);
          strftime(zBuf, sizeof(zBuf)-1, sqlite3_str_value(pOut)+iDate, 
                   localtime(&now));
          zBuf[199] = 0;
          sqlite3_str_truncate(pOut, iDate);
          sqlite3_str_appendall(pOut, zBuf);
        }
        iDate = -1;
      }
      zPrompt += 2;
      i = -1;
      continue;
    }

    /* No match to a known escape.  Generate an error. The mOff flag
    ** is ignored for this output, so that errors appear even if they
    ** are in an unused branch. */
    sqlite3_str_appendf(pOut,"UNKNOWN(\"/%c\")",c);
    zPrompt += 2;
    i = -1;
  }
  if( i>0 && !mOff ){
    sqlite3_str_append(pOut, zPrompt, i);
  }

  /* Expand the /B, if there is one and if this is a continuation prompt */
  if( idxSpace>=0 && zPrior!=0 && zPrior[0]!=0 ){
    char *zOther = expand_prompt(p, 0, prompt_string(p,0));
    size_t wOther = sqlite3_qrf_wcswidth(zOther);
    size_t wThis = sqlite3_qrf_wcswidth(sqlite3_str_value(pOut));
    sqlite3_free(zOther);
    if( wOther>wThis ){
      char *z;
      int len;
      size_t nNew = wOther - wThis;
      assert( nNew>0 && nNew<0x7fffffff );
      sqlite3_str_appendchar(pOut, (int)nNew, ' ');
      len = sqlite3_str_length(pOut);
      if( len>idxSpace ){
        z = sqlite3_str_value(pOut);
        memmove(z+idxSpace+nNew, z+idxSpace, len-nNew-idxSpace);
        memset(z+idxSpace, ' ', nNew);
      }
    }
  }
  if( 0==sqlite3_str_length(pOut) ){
    /* Avoid a bogus OOM */
    sqlite3_str_appendchar(pOut, 1, '\0');
  }

  /* Editline does not recognize ANSI X3.64 escape sequences.  So we have
  ** to find them all and enclose them inside '\001'...'\002' delimiters.
  ** Some versions of readline also require this, but others do not.
  */
  if( p->bDelimitNonprint && strstr(sqlite3_str_value(pOut),"\033[")!=0 ){
    char *zOrig = sqlite3_str_finish(pOut);
    int n;
    pOut = sqlite3_str_new(0);
    for(i=0; zOrig[i]; i++){
      if( zOrig[i]=='\033' && (n = nAnsiEscape(zOrig+i))>0 ){
        sqlite3_str_appendchar(pOut, 1, 1);
        sqlite3_str_append(pOut, &zOrig[i], n);
        sqlite3_str_appendchar(pOut, 1, 2);
        i += n-1;
      }else{
        sqlite3_str_appendchar(pOut, 1, zOrig[i]);
      }
    }
    sqlite3_free(zOrig);
  }

  return sqlite3_str_finish(pOut);
}

/*
** Retrieve a single line of input text.
**
** If in==0 then read from standard input and prompt before each line.
** If bContinue is true, then a continuation prompt is appropriate.
** If bContinue is zero, then the main prompt should be used.
**
** If zPrior is not NULL then it is a buffer from a prior call to this
** routine that can be reused.
**
** The result is stored in space obtained from malloc() and must either
** be freed by the caller or else passed back into this routine via the
** zPrior argument for reuse.
*/
#ifndef SQLITE_SHELL_FIDDLE
static char *one_input_line(
  ShellState *p,        /* Shell state */
  char *zPrior,         /* Just the previous line */
  const char *zAll,     /* All statement text accumulated so far */
  int bContinue         /* True for a continuation prompt */
){
  char *zResult;
  FILE *in = p->in;
  if( in!=0 ){
    zResult = local_getline(zPrior, in);
  }else{
    const char *zBase = prompt_string(p, bContinue!=0);
    char *zPrompt = expand_prompt(p, zAll, zBase);
    shell_check_oom(zPrompt);
#if SHELL_USE_LOCAL_GETLINE
    sputz(stdout, zPrompt);
    fflush(stdout);
    do{
      zResult = local_getline(zPrior, stdin);
      zPrior = 0;
      /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
      if( zResult==0 ) sqlite3_sleep(50);
    }while( zResult==0 && seenInterrupt>0 );
#else
    free(zPrior);
    zResult = shell_readline(zPrompt);
    while( zResult==0 ){
      /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
      sqlite3_sleep(50);
      if( seenInterrupt==0 ) break;
      zResult = shell_readline("");
    }
    if( zResult && *zResult ) shell_add_history(zResult);
#endif
    sqlite3_free(zPrompt);
  }
  return zResult;
}
#endif /* !SQLITE_SHELL_FIDDLE */

/*
** Return the value of a hexadecimal digit.  Return -1 if the input
** is not a hex digit.
*/
static int hexDigitValue(char c){
  if( c>='0' && c<='9' ) return c - '0';
  if( c>='a' && c<='f' ) return c - 'a' + 10;
  if( c>='A' && c<='F' ) return c - 'A' + 10;
  return -1;
}

/*
** Interpret zArg as an integer value, possibly with suffixes.
**
** If the value specified by zArg is outside the range of values that
** can be represented using a 64-bit twos-complement integer, then return
** the nearest representable value.
*/
static sqlite3_int64 integerValue(const char *zArg){
  sqlite3_uint64 v = 0;
  static const struct { char *zSuffix; unsigned int iMult; } aMult[] = {
    { "KiB", 1024 },
    { "MiB", 1024*1024 },
    { "GiB", 1024*1024*1024 },
    { "KB",  1000 },
    { "MB",  1000000 },
    { "GB",  1000000000 },
    { "K",   1000 },
    { "M",   1000000 },
    { "G",   1000000000 },
  };
  int i;
  int isNeg = 0;
  if( zArg[0]=='-' ){
    isNeg = 1;
    zArg++;
  }else if( zArg[0]=='+' ){
    zArg++;
  }
  if( zArg[0]=='0' && zArg[1]=='x' ){
    int x;
    zArg += 2;
    while( (x = hexDigitValue(zArg[0]))>=0 ){
      if( v > 0x0fffffffffffffffULL ) goto integer_overflow;
      v = (v<<4) + x;
      zArg++;
    }
  }else{
    while( IsDigit(zArg[0]) ){
      if( v>=922337203685477580LL ){
        if( v>922337203685477580LL || zArg[0]>='8' ) goto integer_overflow;
      }
      v = v*10 + (zArg[0] - '0');
      zArg++;
    }
  }
  for(i=0; i<ArraySize(aMult); i++){
    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
      if( 0x7fffffffffffffffULL/aMult[i].iMult < v ) goto integer_overflow;
      v *= aMult[i].iMult;
      break;
    }
  }
  if( isNeg && v>0x7fffffffffffffffULL ) goto integer_overflow;
  return isNeg? -(sqlite3_int64)v : (sqlite3_int64)v;
integer_overflow:
  return isNeg ? (i64)0x8000000000000000LL : 0x7fffffffffffffffLL;
}

/*
** A variable length string to which one can append text.
*/
typedef struct ShellText ShellText;
struct ShellText {
  char *zTxt;       /* The text */
  i64 n;            /* Number of bytes of zTxt[] actually used */
  i64 nAlloc;       /* Number of bytes allocated for zTxt[] */
};

/*
** Initialize and destroy a ShellText object
*/
static void initText(ShellText *p){
  memset(p, 0, sizeof(*p));
}
static void freeText(ShellText *p){
  sqlite3_free(p->zTxt);
  initText(p);
}

/* zIn is either a pointer to a NULL-terminated string in memory obtained
** from malloc(), or a NULL pointer. The string pointed to by zAppend is
** added to zIn, and the result returned in memory obtained from malloc().
** zIn, if it was not NULL, is freed.
**
** If the third argument, quote, is not '\0', then it is used as a
** quote character for zAppend.
*/
static void appendText(ShellText *p, const char *zAppend, char quote){
  i64 len;
  i64 i;
  i64 nAppend = strlen30(zAppend);

  len = nAppend+p->n+1;
  if( quote ){
    len += 2;
    for(i=0; i<nAppend; i++){
      if( zAppend[i]==quote ) len++;
    }
  }

  if( p->zTxt==0 || p->n+len>=p->nAlloc ){
    p->nAlloc = p->nAlloc*2 + len + 20;
    p->zTxt = sqlite3_realloc64(p->zTxt, p->nAlloc);
    shell_check_oom(p->zTxt);
  }

  if( quote ){
    char *zCsr = p->zTxt+p->n;
    *zCsr++ = quote;
    for(i=0; i<nAppend; i++){
      *zCsr++ = zAppend[i];
      if( zAppend[i]==quote ) *zCsr++ = quote;
    }
    *zCsr++ = quote;
    p->n = (i64)(zCsr - p->zTxt);
    *zCsr = '\0';
  }else{
    memcpy(p->zTxt+p->n, zAppend, nAppend);
    p->n += nAppend;
    p->zTxt[p->n] = '\0';
  }
}

/*
** Attempt to determine if identifier zName needs to be quoted, either
** because it contains non-alphanumeric characters, or because it is an
** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
** that quoting is required.
**
** Return '"' if quoting is required.  Return 0 if no quoting is required.
*/
static char quoteChar(const char *zName){
  int i;
  if( zName==0 ) return '"';
  if( !IsAlpha(zName[0]) && zName[0]!='_' ) return '"';
  for(i=0; zName[i]; i++){
    if( !IsAlnum(zName[i]) && zName[i]!='_' ) return '"';
  }
  return sqlite3_keyword_check(zName, i) ? '"' : 0;
}

/*
** Construct a fake object name and column list to describe the structure
** of the view, virtual table, or table valued function zSchema.zName.
**
** The returned string comes from sqlite3_mprintf() and should be freed
** by the caller using sqlite3_free().
*/
static char *shellFakeSchema(
  sqlite3 *db,            /* The database connection containing the vtab */
  const char *zSchema,    /* Schema of the database holding the vtab */
  const char *zName       /* The name of the virtual table */
){
  sqlite3_stmt *pStmt = 0;
  char *zSql;
  ShellText s;
  char cQuote;
  char *zDiv = "(";
  int nRow = 0;

  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
                         zSchema ? zSchema : "main", zName);
  shell_check_oom(zSql);
  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  sqlite3_free(zSql);
  initText(&s);
  if( zSchema ){
    cQuote = quoteChar(zSchema);
    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
    appendText(&s, zSchema, cQuote);
    appendText(&s, ".", 0);
  }
  cQuote = quoteChar(zName);
  appendText(&s, zName, cQuote);
  while( sqlite3_step(pStmt)==SQLITE_ROW ){
    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
    nRow++;
    appendText(&s, zDiv, 0);
    zDiv = ",";
    if( zCol==0 ) zCol = "";
    cQuote = quoteChar(zCol);
    appendText(&s, zCol, cQuote);
  }
  appendText(&s, ")", 0);
  sqlite3_finalize(pStmt);
  if( nRow==0 ){
    freeText(&s);
    s.zTxt = 0;
  }
  return s.zTxt;
}

/*
** SQL function:  strtod(X)
**
** Use the C-library strtod() function to convert string X into a double.
** Used for comparing the accuracy of SQLite's internal text-to-float conversion
** routines against the C-library.
*/
static void shellStrtod(
  sqlite3_context *pCtx,
  int nVal,
  sqlite3_value **apVal
){
  char *z = (char*)sqlite3_value_text(apVal[0]);
  UNUSED_PARAMETER(nVal);
  if( z==0 ) return;
  sqlite3_result_double(pCtx, strtod(z,0));
}

/*
** SQL function:  dtostr(X)
**
** Use the C-library printf() function to convert real value X into a string.
** Used for comparing the accuracy of SQLite's internal float-to-text conversion
** routines against the C-library.
*/
static void shellDtostr(
  sqlite3_context *pCtx,
  int nVal,
  sqlite3_value **apVal
){
  double r = sqlite3_value_double(apVal[0]);
  int n = nVal>=2 ? sqlite3_value_int(apVal[1]) : 26;
  char z[400];
  if( n<1 ) n = 1;
  if( n>350 ) n = 350;
  sprintf(z, "%#+.*e", n, r);
  sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
}

/*
** SQL function:  shell_add_schema(S,X)
**
** Add the schema name X to the CREATE statement in S and return the result.
** Examples:
**
**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
**
** Also works on
**
**    CREATE INDEX
**    CREATE UNIQUE INDEX
**    CREATE VIEW
**    CREATE TRIGGER
**    CREATE VIRTUAL TABLE
**
** This UDF is used by the .schema command to insert the schema name of
** attached databases into the middle of the sqlite_schema.sql field.
*/
static void shellAddSchemaName(
  sqlite3_context *pCtx,
  int nVal,
  sqlite3_value **apVal
){
  static const char *aPrefix[] = {
     "TABLE",
     "INDEX",
     "UNIQUE INDEX",
     "VIEW",
     "TRIGGER",
     "VIRTUAL TABLE"
  };
  int i = 0;
  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
  sqlite3 *db = sqlite3_context_db_handle(pCtx);
  UNUSED_PARAMETER(nVal);
  if( zIn!=0 && cli_strncmp(zIn, "CREATE ", 7)==0 ){
    for(i=0; i<ArraySize(aPrefix); i++){
      int n = strlen30(aPrefix[i]);
      if( cli_strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
        char *z = 0;
        char *zFake = 0;
        if( zSchema ){
          char cQuote = quoteChar(zSchema);
          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
          }else{
            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
          }
        }
        if( zName
         && aPrefix[i][0]=='V'
         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
        ){
          if( z==0 ){
            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
          }else{
            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
          }
          sqlite3_free(zFake);
        }
        if( z ){
          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
          return;
        }
      }
    }
  }
  sqlite3_result_value(pCtx, apVal[0]);
}

/*
** SQL function:  shell_prompt_test(PROMPT,PRIOR,FILENAME,FLAGS)
**
** Return the shell prompt, with escapes expanded, for testing purposes.
** The first argument is the raw (unexpanded) prompt string.  Or if the
** first argument is NULL, then use whatever prompt string is currently
** configured.  If the second argument exists and is not NULL, then the
** second argument is understood to be prior incomplete text and a
** continuation prompt is generated.  If a third argument is provided,
** it is assumed to be the full pathname of the database file.  The
** fourth argument, if provided, is an integer of flags:
**
**      0x0001       Always insert \001..\002 delimiters around ANSI escapes
**      0x0002       Never insert \001..\002 delimiters
**
** This function is for testing purposes only.  The interface may change.
** The function itself might be renamed or removed in future releases.  Do
** not use this function in applications.
*/
static void shellExpandPrompt(
  sqlite3_context *pCtx,
  int nVal,
  sqlite3_value **apVal
){
  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
  const char *zPrompt;
  const char *zPrior;
  const char *zSavedDbFile;
  int mSavedFlgs;
  const char *zFName;
  char *zRes;
  int mFlags;
  char bSavedDelimit = p->bDelimitNonprint;

  if( nVal<1 ) return;
  if( nVal<2 
   || (zPrior = (const char*)sqlite3_value_text(apVal[1]))==0
   || zPrior[0]==0
  ){
    zPrior = 0;
  }
  zPrompt = (const char*)sqlite3_value_text(apVal[0]);
  if( zPrompt==0 ){
    zPrompt = prompt_string(p, zPrior!=0);
  }
  zSavedDbFile = p->pAuxDb->zDbFilename;
  mSavedFlgs = p->pAuxDb->mFlgs;
  if( nVal>=3 && (zFName = (const char*)sqlite3_value_text(apVal[2]))!=0 ){
    p->pAuxDb->zDbFilename = zFName;
    p->pAuxDb->mFlgs |= 0x001;
  }
  mFlags = nVal>=4 ? sqlite3_value_int(apVal[3]) : 0;
  if( mFlags & 0x0001 ){
    p->bDelimitNonprint = 1;
  }else if( mFlags & 0x0002 ){
    p->bDelimitNonprint = 0;
  }
  zRes = expand_prompt(p, zPrior, zPrompt);
  p->bDelimitNonprint = bSavedDelimit;
  p->pAuxDb->zDbFilename = zSavedDbFile;
  p->pAuxDb->mFlgs = mSavedFlgs;
  sqlite3_result_text(pCtx, zRes, -1, SQLITE_TRANSIENT);
  sqlite3_free(zRes);
}


/************************* BEGIN PERFORMANCE TIMER *****************************/
#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
#include <sys/time.h>
#include <sys/resource.h>
/* VxWorks does not support getrusage() as far as we can determine */
#if defined(_WRS_KERNEL) || defined(__RTP__)
struct rusage {
  struct timeval ru_utime; /* user CPU time used */
  struct timeval ru_stime; /* system CPU time used */
};
#define getrusage(A,B) memset(B,0,sizeof(*B))
#endif

/* Saved resource information for the beginning of an operation */
static struct rusage sBegin;  /* CPU time at start */
static sqlite3_int64 iBegin;  /* Wall-clock time at start */

/*
** Begin timing an operation
*/
static void beginTimer(ShellState *p){
  if( p->enableTimer || (p->flgProgress & SHELL_PROGRESS_TMOUT)!=0 ){
    getrusage(RUSAGE_SELF, &sBegin);
    iBegin = timeOfDay();
  }
}

/* Return the difference of two time_structs in seconds */
static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
         (double)(pEnd->tv_sec - pStart->tv_sec);
}

#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
/* Return the time since the start of the timer in
** seconds. */
static double elapseTime(ShellState *NotUsed){
  (void)NotUsed;
  if( iBegin==0 ) return 0.0;
  return (timeOfDay() - iBegin)*0.000001;
}
#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */

/*
** Print the timing results.
*/
static void endTimer(ShellState *p){
  if( p->enableTimer ){
    sqlite3_int64 iEnd = timeOfDay();
    struct rusage sEnd;
    getrusage(RUSAGE_SELF, &sEnd);
    p->prevTimer = (iEnd - iBegin)*0.000001;
    cli_printf(p->out, "Run Time: real %.6f user %.6f sys %.6f\n",
          p->prevTimer,
          timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
          timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
    if( p->enableTimer==1 ) p->enableTimer = 0;
    iBegin = 0;
  }
}

#define BEGIN_TIMER(X) beginTimer(X)
#define END_TIMER(X)   endTimer(X)
#define ELAPSE_TIME(X) elapseTime(X)
#define HAS_TIMER 1

#elif (defined(_WIN32) || defined(WIN32))

/* Saved resource information for the beginning of an operation */
static HANDLE hProcess;
static FILETIME ftKernelBegin;
static FILETIME ftUserBegin;
static sqlite3_int64 ftWallBegin;
typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
                                    LPFILETIME, LPFILETIME);
static GETPROCTIMES getProcessTimesAddr = NULL;

/*
** Check to see if we have timer support.  Return 1 if necessary
** support found (or found previously).
*/
static int hasTimer(void){
  if( getProcessTimesAddr ){
    return 1;
  } else {
    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
    ** versions. See if the version we are running on has it, and if it
    ** does, save off a pointer to it and the current process handle.
    */
    hProcess = GetCurrentProcess();
    if( hProcess ){
      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
      if( NULL != hinstLib ){
        getProcessTimesAddr =
            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
        if( NULL != getProcessTimesAddr ){
          return 1;
        }
        FreeLibrary(hinstLib);
      }
    }
  }
  return 0;
}

/*
** Begin timing an operation
*/
static void beginTimer(ShellState *p){
  if( (p->enableTimer || (p->flgProgress & SHELL_PROGRESS_TMOUT)!=0)
   && getProcessTimesAddr
  ){
    FILETIME ftCreation, ftExit;
    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
                        &ftKernelBegin,&ftUserBegin);
    ftWallBegin = timeOfDay();
  }
}

/* Return the difference of two FILETIME structs in seconds */
static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
  return (double) ((i64End - i64Start) / 10000000.0);
}

#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
/* Return the time since the start of the timer in
** seconds. */
static double elapseTime(ShellState *NotUsed){
  (void)NotUsed;
  if( ftWallBegin==0 ) return 0.0;
  return (timeOfDay() - ftWallBegin)*0.000001;
}
#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */

/*
** Print the timing results.
*/
static void endTimer(ShellState *p){
  if( p->enableTimer && getProcessTimesAddr){
    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
    sqlite3_int64 ftWallEnd = timeOfDay();
    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
    p->prevTimer = (ftWallEnd - ftWallBegin)*0.000001;
#ifdef _WIN64
    /* microsecond precision on 64-bit windows */
    cli_printf(p->out, "Run Time: real %.6f user %f sys %f\n",
          p->prevTimer,
          timeDiff(&ftUserBegin, &ftUserEnd),
          timeDiff(&ftKernelBegin, &ftKernelEnd));
#else
    /* millisecond precisino on 32-bit windows */
    cli_printf(p->out, "Run Time: real %.3f user %.3f sys %.3f\n",
          p->prevTimer,
          timeDiff(&ftUserBegin, &ftUserEnd),
          timeDiff(&ftKernelBegin, &ftKernelEnd));
#endif
    if( p->enableTimer==1 ) p->enableTimer = 0;
    ftWallBegin = 0;
  }
}

#define BEGIN_TIMER(X) beginTimer(X)
#define ELAPSE_TIME(X) elapseTime(X)
#define END_TIMER(X)   endTimer(X)
#define HAS_TIMER      hasTimer()

#else
#define BEGIN_TIMER(X) /* no-op */
#define ELAPSE_TIME(X) 0.0
#define END_TIMER(X)   /*no-op*/
#define HAS_TIMER 0
#endif
/************************* END PERFORMANCE TIMER ******************************/

/*
** Clear a display mode, freeing any allocated memory that it
** contains.
*/
static void modeFree(Mode *p){
  u8 autoExplain = p->autoExplain;
  free(p->spec.aWidth);
  free(p->spec.aAlign);
  free(p->spec.zColumnSep);
  free(p->spec.zRowSep);
  free(p->spec.zTableName);
  free(p->spec.zNull);
  memset(p, 0, sizeof(*p));
  p->spec.iVersion = 1;
  p->autoExplain = autoExplain;
}

/*
** Duplicate Mode pSrc into pDest.  pDest is assumed to be
** uninitialized prior to invoking this routine.
*/
static void modeDup(Mode *pDest, Mode *pSrc){
  memcpy(pDest, pSrc, sizeof(*pDest));
  if( pDest->spec.aWidth ){
    size_t sz = sizeof(pSrc->spec.aWidth[0]) * pSrc->spec.nWidth;
    pDest->spec.aWidth = malloc( sz );
    if( pDest->spec.aWidth ){
      memcpy(pDest->spec.aWidth, pSrc->spec.aWidth, sz);
    }else{
      pDest->spec.nWidth = 0;
    }
  }
  if( pDest->spec.aAlign ){
    size_t sz = sizeof(pSrc->spec.aAlign[0]) * pSrc->spec.nAlign;
    pDest->spec.aAlign = malloc( sz );
    if( pDest->spec.aAlign ){
      memcpy(pDest->spec.aAlign, pSrc->spec.aAlign, sz);
    }else{
      pDest->spec.nAlign = 0;
    }
  }
  if( pDest->spec.zColumnSep ){
    pDest->spec.zColumnSep = strdup(pSrc->spec.zColumnSep);
  }
  if( pDest->spec.zRowSep ){
    pDest->spec.zRowSep = strdup(pSrc->spec.zRowSep);
  }
  if( pDest->spec.zTableName ){
    pDest->spec.zTableName = strdup(pSrc->spec.zTableName);
  }
  if( pDest->spec.zNull ){
    pDest->spec.zNull = strdup(pSrc->spec.zNull);
  }
}

/*
** Set a string value to a copy of the zNew string in memory
** obtained from system malloc().
*/
static void modeSetStr(char **az, const char *zNew){
  free(*az);
  if( zNew==0 ){
    *az = 0;
  }else{
    size_t n = strlen(zNew);
    *az = malloc( n+1 );
    if( *az ){
      memcpy(*az, zNew, n+1 );
    }
  }
}

/* Forward reference */
static int pickStr(const char *zArg, char **pzErr, ...);

/*
** Change the limits on the display mode.  Return 0 on
** success.  Return non-zero if zArg is mis-formatted.
**
** Valid arguments:
**
**    "on"             Default limits
**    "off"            All limits turned off
**    L,C              Line and Characters limits set
**    L,C,T            Line, Character, and Title limits set
**
** Anything else returns non-zero
*/
static int modeSetLimit(ShellState *p, const char *zArg){
  int k = zArg==0 ? 1 : pickStr(zArg,0,"on","off","");
  if( k==0 ){
    p->mode.spec.nLineLimit = DFLT_LINE_LIMIT;
    p->mode.spec.nCharLimit = DFLT_CHAR_LIMIT;
    p->mode.spec.nTitleLimit = DFLT_TITLE_LIMIT;
  }else if( k==1 ){
    p->mode.spec.nLineLimit = 0;
    p->mode.spec.nCharLimit = 0;
    p->mode.spec.nTitleLimit = 0;
  }else{
    int L, C, T = 0;
    int nNum = sscanf(zArg, "%d,%d,%d", &L, &C, &T);
    if( nNum<2 || L<0 || C<0 || T<0) return 1;
    p->mode.spec.nLineLimit = L;
    p->mode.spec.nCharLimit = C;
    if( nNum==3 ) p->mode.spec.nTitleLimit = T;
  }
  return 0;
}

/*
** Change the mode to eMode
*/
static void modeChange(ShellState *p, unsigned char eMode){
  const ModeInfo *pI;
  if( eMode<ArraySize(aModeInfo) ){
    Mode *pM = &p->mode;
    pI = &aModeInfo[eMode];
    pM->eMode = eMode;
    if( pI->eCSep ) modeSetStr(&pM->spec.zColumnSep, aModeStr[pI->eCSep]);
    if( pI->eRSep ) modeSetStr(&pM->spec.zRowSep, aModeStr[pI->eRSep]);
    if( pI->eNull ) modeSetStr(&pM->spec.zNull, aModeStr[pI->eNull]);
    pM->spec.eText = pI->eText;
    pM->spec.eBlob = pI->eBlob;
    if( (pM->mFlags & MFLG_HDR)==0 ){
      pM->spec.bTitles = pI->bHdr;
    }
    pM->spec.eTitle = pI->eHdr;
    if( pI->mFlg & 0x01 ){
      pM->spec.bBorder = QRF_No;
    }else{
      pM->spec.bBorder = QRF_Auto;
    }
    if( pI->mFlg & 0x02 ){
      pM->spec.bSplitColumn = QRF_Yes;
      pM->bAutoScreenWidth = 1;
    }else{
      pM->spec.bSplitColumn = QRF_No;
    }
  }else if( eMode>=MODE_USER && eMode-MODE_USER<p->nSavedModes ){
    modeFree(&p->mode);
    modeDup(&p->mode, &p->aSavedModes[eMode-MODE_USER].mode);
  }else if( eMode==MODE_BATCH ){
    u8 mFlags = p->mode.mFlags;
    modeFree(&p->mode);
    modeChange(p, MODE_List);
    modeSetLimit(p, "off");
    p->mode.mFlags = mFlags;
  }else if( eMode==MODE_TTY ){
    u8 mFlags = p->mode.mFlags;
    modeFree(&p->mode);
    modeChange(p, MODE_QBox);
    p->mode.bAutoScreenWidth = 1;
    p->mode.spec.eText = QRF_TEXT_Relaxed;
    p->mode.spec.nCharLimit = DFLT_CHAR_LIMIT;
    p->mode.spec.nLineLimit = DFLT_LINE_LIMIT;
    p->mode.spec.bTextJsonb = QRF_Yes;
    p->mode.spec.nTitleLimit = DFLT_TITLE_LIMIT;
    p->mode.spec.nMultiInsert = DFLT_MULTI_INSERT;
    p->mode.mFlags = mFlags;
  }
}

/*
** Set the mode to the default.  It assumed that the mode has
** already been freed and zeroed prior to calling this routine.
*/
static void modeDefault(ShellState *p){
  p->mode.spec.iVersion = 1;
  p->mode.autoExplain = 1;
  if( stdin_is_interactive || stdout_is_console ){
    modeChange(p, MODE_TTY);
  }else{
    modeChange(p, MODE_BATCH);
  }
}

/*
** Find the number of a display mode given its name.  Return -1 if
** the name does not match any mode.
**
** Saved modes are also searched if p!=NULL.  The number returned
** for a saved mode is the index into the p->aSavedModes[] array 
** plus MODE_USER.
**
** Two special mode names are also available: "batch" and "tty".
** evaluate to the default mode for batch operation and interactive
** operation on a TTY, respectively.
*/
static int modeFind(ShellState *p, const char *zName){
  int i;
  for(i=0; i<ArraySize(aModeInfo); i++){
    if( cli_strcmp(aModeInfo[i].zName,zName)==0 ) return i;
  }
  for(i=0; i<p->nSavedModes; i++){
    if( cli_strcmp(p->aSavedModes[i].zTag,zName)==0 ) return i+MODE_USER;
  }
  if( strcmp(zName,"batch")==0 ) return MODE_BATCH;
  if( strcmp(zName,"tty")==0 ) return MODE_TTY;
  return -1;
}

/*
** Save or restore the current output mode
*/
static void modePush(ShellState *p){
  p->aModeStack = realloc(p->aModeStack, sizeof(Mode)*(1+p->nModeStack));
  shell_check_oom(p->aModeStack);
  modeDup(&p->aModeStack[p->nModeStack], &p->mode);
  p->nModeStack++;
}
static void modePop(ShellState *p){
  if( p->nModeStack ){
    modeFree(&p->mode);
    p->nModeStack--;
    memcpy(&p->mode, &p->aModeStack[p->nModeStack], sizeof(Mode));
    memset(&p->aModeStack[p->nModeStack], 0, sizeof(Mode));
  }
}

/*
** A callback for the sqlite3_log() interface.
*/
static void shellLog(void *pArg, int iErrCode, const char *zMsg){
  ShellState *p = (ShellState*)pArg;
  if( p->pLog==0 ) return;
  cli_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
  fflush(p->pLog);
}

/*
** SQL function:  shell_putsnl(X)
**
** Write the text X to the screen (or whatever output is being directed)
** adding a newline at the end, and then return X.
*/
static void shellPutsFunc(
  sqlite3_context *pCtx,
  int nVal,
  sqlite3_value **apVal
){
  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
  (void)nVal;
  cli_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
  sqlite3_result_value(pCtx, apVal[0]);
}

/*
** Compute the name of the location of an input error in memory
** obtained from sqlite3_malloc().
*/
static char *shellErrorLocation(ShellState *p){
  char *zLoc;
  if( p->zErrPrefix ){
    zLoc = sqlite3_mprintf("%s", p->zErrPrefix);
  }else if( p->zInFile==0 || strcmp(p->zInFile,"<stdin>")==0){
    zLoc = sqlite3_mprintf("line %lld:", p->lineno);
  }else{
    zLoc = sqlite3_mprintf("%s:%lld:", p->zInFile, p->lineno);
  }
  return zLoc;
}

/*
** If in safe mode, print an error message described by the arguments
** and exit immediately.
*/
static void failIfSafeMode(
  ShellState *p,
  const char *zErrMsg,
  ...
){
  if( p->bSafeMode ){
    va_list ap;
    char *zMsg;
    char *zLoc = shellErrorLocation(p);
    va_start(ap, zErrMsg);
    zMsg = sqlite3_vmprintf(zErrMsg, ap);
    va_end(ap);
    cli_printf(stderr, "%s %s\n", zLoc, zMsg);
    cli_exit(1);
  }
}

/*
** Issue an error message from a dot-command.
*/
static void dotCmdError(
  ShellState *p,       /* Shell state */
  int iArg,            /* Index of argument on which error occurred */
  const char *zBrief,  /* Brief (<20 character) error description */
  const char *zDetail, /* Error details */
  ...
){
  FILE *out = stderr;
  char *zLoc = shellErrorLocation(p);
  if( zBrief!=0 && iArg>=0 && iArg<p->dot.nArg ){
    int i = p->dot.aiOfst[iArg];
    int nPrompt = strlen30(zBrief) + 5;
    cli_printf(out, "%s %s\n", zLoc, p->dot.zOrig);
    if( i > nPrompt ){
      cli_printf(out, "%s %*s%s ---^\n", zLoc, 1+i-nPrompt, "", zBrief);
    }else{
      cli_printf(out, "%s %*s^--- %s\n", zLoc, i, "", zBrief);
    }
  }
  if( zDetail ){
    char *zMsg;
    va_list ap;
    va_start(ap, zDetail);
    zMsg = sqlite3_vmprintf(zDetail,ap);
    va_end(ap);
    cli_printf(out,"%s %s\n", zLoc, zMsg);
    sqlite3_free(zMsg);
  }
  sqlite3_free(zLoc);
}
  

/*
** SQL function:   edit(VALUE)
**                 edit(VALUE,EDITOR)
**
** These steps:
**
**     (1) Write VALUE into a temporary file.
**     (2) Run program EDITOR on that temporary file.
**     (3) Read the temporary file back and return its content as the result.
**     (4) Delete the temporary file
**
** If the EDITOR argument is omitted, use the value in the VISUAL
** environment variable.  If still there is no EDITOR, throw an error.
**
** Also throw an error if the EDITOR program returns a non-zero exit code.
*/
#ifndef SQLITE_NOHAVE_SYSTEM
static void editFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  const char *zEditor;
  char *zTempFile = 0;
  sqlite3 *db;
  char *zCmd = 0;
  int bBin;
  int rc;
  int hasCRLF = 0;
  FILE *f = 0;
  sqlite3_int64 sz;
  sqlite3_int64 x;
  unsigned char *p = 0;

  if( argc==2 ){
    zEditor = (const char*)sqlite3_value_text(argv[1]);
  }else{
    zEditor = getenv("VISUAL");
  }
  if( zEditor==0 ){
    sqlite3_result_error(context, "no editor for edit()", -1);
    return;
  }
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
    sqlite3_result_error(context, "NULL input to edit()", -1);
    return;
  }
  db = sqlite3_context_db_handle(context);
  zTempFile = 0;
  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
  if( zTempFile==0 ){
    sqlite3_uint64 r = 0;
    sqlite3_randomness(sizeof(r), &r);
    zTempFile = sqlite3_mprintf("temp%llx", r);
    if( zTempFile==0 ){
      sqlite3_result_error_nomem(context);
      return;
    }
  }
  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
  /* When writing the file to be edited, do \n to \r\n conversions on systems
  ** that want \r\n line endings */
  f = sqlite3_fopen(zTempFile, bBin ? "wb" : "w");
  if( f==0 ){
    sqlite3_result_error(context, "edit() cannot open temp file", -1);
    goto edit_func_end;
  }
  sz = sqlite3_value_bytes(argv[0]);
  if( bBin ){
    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
  }else{
    const char *z = (const char*)sqlite3_value_text(argv[0]);
    /* Remember whether or not the value originally contained \r\n */
    if( z && strstr(z,"\r\n")!=0 ) hasCRLF = 1;
    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
  }
  fclose(f);
  f = 0;
  if( x!=sz ){
    sqlite3_result_error(context, "edit() could not write the whole file", -1);
    goto edit_func_end;
  }
  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
  if( zCmd==0 ){
    sqlite3_result_error_nomem(context);
    goto edit_func_end;
  }
  rc = system(zCmd);
  sqlite3_free(zCmd);
  if( rc ){
    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
    goto edit_func_end;
  }
  f = sqlite3_fopen(zTempFile, "rb");
  if( f==0 ){
    sqlite3_result_error(context,
      "edit() cannot reopen temp file after edit", -1);
    goto edit_func_end;
  }
  fseek(f, 0, SEEK_END);
  sz = ftell(f);
  rewind(f);
  p = sqlite3_malloc64( sz+1 );
  if( p==0 ){
    sqlite3_result_error_nomem(context);
    goto edit_func_end;
  }
  x = fread(p, 1, (size_t)sz, f);
  fclose(f);
  f = 0;
  if( x!=sz ){
    sqlite3_result_error(context, "could not read back the whole file", -1);
    goto edit_func_end;
  }
  if( bBin ){
    sqlite3_result_blob64(context, p, sz, sqlite3_free);
  }else{
    sqlite3_int64 i, j;
    if( hasCRLF ){
      /* If the original contains \r\n then do no conversions back to \n */
    }else{
      /* If the file did not originally contain \r\n then convert any new
      ** \r\n back into \n */
      p[sz] = 0;
      for(i=j=0; i<sz; i++){
        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
        p[j++] = p[i];
      }
      sz = j;
      p[sz] = 0;
    }
    sqlite3_result_text64(context, (const char*)p, sz,
                          sqlite3_free, SQLITE_UTF8);
  }
  p = 0;

edit_func_end:
  if( f ) fclose(f);
  unlink(zTempFile);
  sqlite3_free(zTempFile);
  sqlite3_free(p);
}
#endif /* SQLITE_NOHAVE_SYSTEM */

/*
** Set output mode to text or binary for Windows.
*/
static void setCrlfMode(ShellState *p){
#ifdef _WIN32
  if( p->mode.mFlags & MFLG_CRLF ){
    sqlite3_fsetmode(p->out, _O_TEXT);
  }else{
    sqlite3_fsetmode(p->out, _O_BINARY);
  }
#else
  UNUSED_PARAMETER(p);
#endif    
}

/*
** Find earliest of chars within s specified in zAny.
** With ns == ~0, is like strpbrk(s,zAny) and s must be 0-terminated.
*/
static const char *anyOfInStr(const char *s, const char *zAny, size_t ns){
  const char *pcFirst = 0;
  if( ns == ~(size_t)0 ) ns = strlen(s);
  while(*zAny){
    const char *pc = (const char*)memchr(s, *zAny&0xff, ns);
    if( pc ){
      pcFirst = pc;
      ns = pcFirst - s;
    }
    ++zAny;
  }
  return pcFirst;
}

/* Skip over as much z[] input char sequence as is valid UTF-8,
** limited per nAccept char's or whole characters and containing
** no char cn such that ((1<<cn) & ccm)!=0. On return, the
** sequence z:return (inclusive:exclusive) is validated UTF-8.
** Limit: nAccept>=0 => char count, nAccept<0 => character
 */
const char *zSkipValidUtf8(const char *z, int nAccept, long ccm){
  int ng = (nAccept<0)? -nAccept : 0;
  const char *pcLimit = (nAccept>=0)? z+nAccept : 0;
  assert(z!=0);
  while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){
    unsigned char c = *(u8*)z;
    if( c<0x7f ){
      if( ccm != 0L && c < 0x20 && ((1L<<c) & ccm) != 0 ) return z;
      ++z; /* ASCII */
    }else if( (c & 0xC0) != 0xC0 ) return z; /* not a lead byte */
    else{
      const char *zt = z+1; /* Got lead byte, look at trail bytes.*/
      do{
        if( pcLimit && zt >= pcLimit ) return z;
        else{
          char ct = *zt++;
          if( ct==0 || (zt-z)>4 || (ct & 0xC0)!=0x80 ){
            /* Trailing bytes are too few, too many, or invalid. */
            return z;
          }
        }
      } while( ((c <<= 1) & 0x40) == 0x40 ); /* Eat lead byte's count. */
      z = zt;
    }
  }
  return z;
}


/*
** Output the given string as a quoted according to C or TCL quoting rules.
*/
static void output_c_string(FILE *out, const char *z){
  char c;
  static const char *zq = "\"";
  static long ctrlMask = ~0L;
  static const char *zDQBSRO = "\"\\\x7f"; /* double-quote, backslash, rubout */
  char ace[3] = "\\?";
  char cbsSay;
  cli_puts(zq, out);
  if( z==0 ) z = "";
  while( *z!=0 ){
    const char *pcDQBSRO = anyOfInStr(z, zDQBSRO, ~(size_t)0);
    const char *pcPast = zSkipValidUtf8(z, INT_MAX, ctrlMask);
    const char *pcEnd = (pcDQBSRO && pcDQBSRO < pcPast)? pcDQBSRO : pcPast;
    if( pcEnd > z ){
      cli_printf(out, "%.*s", (int)(pcEnd-z), z);
    }
    if( (c = *pcEnd)==0 ) break;
    ++pcEnd;
    switch( c ){
    case '\\': case '"':
      cbsSay = (char)c;
      break;
    case '\t': cbsSay = 't'; break;
    case '\n': cbsSay = 'n'; break;
    case '\r': cbsSay = 'r'; break;
    case '\f': cbsSay = 'f'; break;
    default: cbsSay = 0; break;
    }
    if( cbsSay ){
      ace[1] = cbsSay;
      cli_puts(ace, out);
    }else if( !isprint(c&0xff) ){
      cli_printf(out, "\\%03o", c&0xff);
    }else{
      ace[1] = (char)c;
      cli_puts(ace+1, out);
    }
    z = pcEnd;
  }
  cli_puts(zq, out);
}

/* Encode input string z[] as a C-language string literal and
** append it to the sqlite3_str.  If z is NULL render and empty string.
*/
static void append_c_string(sqlite3_str *out, const char *z){
  char c;
  static const char *zq = "\"";
  static long ctrlMask = ~0L;
  static const char *zDQBSRO = "\"\\\x7f"; /* double-quote, backslash, rubout */
  char ace[3] = "\\?";
  char cbsSay;
  if( z==0 ) z = "";
  sqlite3_str_appendall(out,zq);
  while( *z!=0 ){
    const char *pcDQBSRO = anyOfInStr(z, zDQBSRO, ~(size_t)0);
    const char *pcPast = zSkipValidUtf8(z, INT_MAX, ctrlMask);
    const char *pcEnd = (pcDQBSRO && pcDQBSRO < pcPast)? pcDQBSRO : pcPast;
    if( pcEnd > z ){
      sqlite3_str_appendf(out, "%.*s", (int)(pcEnd-z), z);
    }
    if( (c = *pcEnd)==0 ) break;
    ++pcEnd;
    switch( c ){
    case '\\': case '"':
      cbsSay = (char)c;
      break;
    case '\t': cbsSay = 't'; break;
    case '\n': cbsSay = 'n'; break;
    case '\r': cbsSay = 'r'; break;
    case '\f': cbsSay = 'f'; break;
    default: cbsSay = 0; break;
    }
    if( cbsSay ){
      ace[1] = cbsSay;
      sqlite3_str_appendall(out,ace);
    }else if( !isprint(c&0xff) ){
      sqlite3_str_appendf(out, "\\%03o", c&0xff);
    }else{
      ace[1] = (char)c;
      sqlite3_str_appendall(out, ace+1);
    }
    z = pcEnd;
  }
  sqlite3_str_appendall(out, zq);
}

/*
** This routine runs when the user presses Ctrl-C
*/
static void interrupt_handler(int NotUsed){
  UNUSED_PARAMETER(NotUsed);
  if( ++seenInterrupt>1 ) cli_exit(1);
  if( globalDb ) sqlite3_interrupt(globalDb);
}
/* No-masking interrupts (SIGTERM or SIGHUP) */
static void sigterm_handler(int NotUsed){
  UNUSED_PARAMETER(NotUsed);
  cli_exit(1);
}

/* Try to determine the screen width.  Use the default if unable.
*/
int shellScreenWidth(void){
  if( stdout_tty_width>0 ){
    return stdout_tty_width;
  }else{
#if defined(TIOCGSIZE)
    struct ttysize ts;
    if( ioctl(STDIN_FILENO, TIOCGSIZE, &ts)>=0
     || ioctl(STDOUT_FILENO, TIOCGSIZE, &ts)>=0
     || ioctl(STDERR_FILENO, TIOCGSIZE, &ts)>=0
    ){
      return ts.ts_cols;
    }
#elif defined(TIOCGWINSZ)
    struct winsize ws;
    if( ioctl(STDIN_FILENO, TIOCGWINSZ, &ws)>=0
     || ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws)>=0
     || ioctl(STDERR_FILENO, TIOCGWINSZ, &ws)>=0
    ){
      return ws.ws_col;
    }
#elif defined(_WIN32)
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)
     || GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi)
     || GetConsoleScreenBufferInfo(GetStdHandle(STD_INPUT_HANDLE), &csbi)
    ){
      return csbi.srWindow.Right - csbi.srWindow.Left + 1;
    }
#endif
#define DEFAULT_SCREEN_WIDTH 80
    return DEFAULT_SCREEN_WIDTH;
  }
}

#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
/*
** This routine runs for console events (e.g. Ctrl-C) on Win32
*/
static BOOL WINAPI ConsoleCtrlHandler(
  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
){
  if( dwCtrlType==CTRL_C_EVENT ){
    interrupt_handler(0);
    return TRUE;
  }
  return FALSE;
}
#endif

#ifndef SQLITE_OMIT_AUTHORIZATION
/*
** This authorizer runs in safe mode.
*/
static int safeModeAuth(
  void *pClientData,
  int op,
  const char *zA1,
  const char *zA2,
  const char *zA3,
  const char *zA4
){
  ShellState *p = (ShellState*)pClientData;
  static const char *azProhibitedFunctions[] = {
    "edit",
    "fts3_tokenizer",
    "load_extension",
    "readfile",
    "realpath",
    "writefile",
    "zipfile",
    "zipfile_cds",
  };
  UNUSED_PARAMETER(zA1);
  UNUSED_PARAMETER(zA3);
  UNUSED_PARAMETER(zA4);
  switch( op ){
    case SQLITE_ATTACH: {
#ifndef SQLITE_SHELL_FIDDLE
      /* In WASM builds the filesystem is a virtual sandbox, so
      ** there's no harm in using ATTACH. */
      failIfSafeMode(p, "cannot run ATTACH in safe mode");
#endif
      break;
    }
    case SQLITE_FUNCTION: {
      int i;
      for(i=0; i<ArraySize(azProhibitedFunctions); i++){
        if( sqlite3_stricmp(zA2, azProhibitedFunctions[i])==0 ){
          failIfSafeMode(p, "cannot use the %s() function in safe mode",
                         azProhibitedFunctions[i]);
        }
      }
      break;
    }
  }
  return SQLITE_OK;
}

/*
** When the ".auth ON" is set, the following authorizer callback is
** invoked.  It always returns SQLITE_OK.
*/
static int shellAuth(
  void *pClientData,
  int op,
  const char *zA1,
  const char *zA2,
  const char *zA3,
  const char *zA4
){
  ShellState *p = (ShellState*)pClientData;
  static const char *azAction[] = { 0,
     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
     "PRAGMA",               "READ",                 "SELECT",
     "TRANSACTION",          "UPDATE",               "ATTACH",
     "DETACH",               "ALTER_TABLE",          "REINDEX",
     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
  };
  int i;
  const char *az[4];
  az[0] = zA1;
  az[1] = zA2;
  az[2] = zA3;
  az[3] = zA4;
  cli_printf(p->out, "authorizer: %s", azAction[op]);
  for(i=0; i<4; i++){
    cli_puts(" ", p->out);
    if( az[i] ){
      output_c_string(p->out, az[i]);
    }else{
      cli_puts("NULL", p->out);
    }
  }
  cli_puts("\n", p->out);
  if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
  return SQLITE_OK;
}
#endif

/*
** Print a schema statement.  This is a helper routine to dump_callback().
**
** This routine converts some CREATE TABLE statements for shadow tables
** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
**
** If the schema statement in z[] contains a start-of-comment and if
** sqlite3_complete() returns false, try to terminate the comment before
** printing the result.  https://sqlite.org/forum/forumpost/d7be961c5c
*/
static void printSchemaLine(FILE *out, const char *z, const char *zTail){
  char *zToFree = 0;
  if( z==0 ) return;
  if( zTail==0 ) return;
  if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
    const char *zOrig = z;
    static const char *azTerm[] = { "", "*/", "\n" };
    int i;
    for(i=0; i<ArraySize(azTerm); i++){
      char *zNew = sqlite3_mprintf("%s%s;", zOrig, azTerm[i]);
      shell_check_oom(zNew);
      if( sqlite3_complete(zNew) ){
        size_t n = strlen(zNew);
        zNew[n-1] = 0;
        zToFree = zNew;
        z = zNew;
        break;
      }
      sqlite3_free(zNew);
    }
  }
  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
    cli_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
  }else{
    cli_printf(out, "%s%s", z, zTail);
  }
  sqlite3_free(zToFree);
}

/*
** Return true if string z[] has nothing but whitespace and comments to the
** end of the first line.
*/
static int wsToEol(const char *z){
  int i;
  for(i=0; z[i]; i++){
    if( z[i]=='\n' ) return 1;
    if( IsSpace(z[i]) ) continue;
    if( z[i]=='-' && z[i+1]=='-' ) return 1;
    return 0;
  }
  return 1;
}

/*
** SQL Function:  shell_format_schema(SQL,FLAGS)
**
** This function is internally by the CLI to assist with the
** ".schema", ".fullschema", and ".dump" commands.  The first
** argument is the value from sqlite_schema.sql.  The value returned
** is a modification of the input that can actually be run as SQL
** to recreate the schema object.
**
** When FLAGS is zero, the only changes is to append ";".  If the
** 0x01 bit of FLAGS is set, then transformations are made to implement
** ".schema --indent".
*/
static void shellFormatSchema(
  sqlite3_context *pCtx,
  int nVal,
  sqlite3_value **apVal
){
  int flags;          /* Value of 2nd parameter */
  const char *zSql;   /* Value of 1st parameter */
  int nSql;           /* Bytes of text in zSql[] */
  sqlite3_str *pOut;  /* Output buffer */
  char *z;            /* Writable copy of zSql */
  int i, j;           /* Loop counters */
  int nParen = 0;
  char cEnd = 0;
  char c;
  int nLine = 0;
  int isIndex;
  int isWhere = 0;

  assert( nVal==2 );
  pOut = sqlite3_str_new(sqlite3_context_db_handle(pCtx));
  nSql = sqlite3_value_bytes(apVal[0]);
  zSql = (const char*)sqlite3_value_text(apVal[0]);
  if( zSql==0 || zSql[0]==0 ) goto shellFormatSchema_finish;
  flags = sqlite3_value_int(apVal[1]);
  if( (flags & 0x01)==0 ){
    sqlite3_str_append(pOut, zSql, nSql);
    sqlite3_str_append(pOut, ";", 1);
    goto shellFormatSchema_finish;
  }
  if( sqlite3_strlike("CREATE VIEW%", zSql, 0)==0
   || sqlite3_strlike("CREATE TRIG%", zSql, 0)==0
  ){
    sqlite3_str_append(pOut, zSql, nSql);
    sqlite3_str_append(pOut, ";", 1);
    goto shellFormatSchema_finish;
  }
  isIndex = sqlite3_strlike("CREATE INDEX%", zSql, 0)==0
         || sqlite3_strlike("CREATE UNIQUE INDEX%", zSql, 0)==0;
  z = sqlite3_mprintf("%s", zSql);
  if( z==0 ){
    sqlite3_str_free(pOut);
    sqlite3_result_error_nomem(pCtx);
    return;
  }
  j = 0;
  for(i=0; IsSpace(z[i]); i++){}
  for(; (c = z[i])!=0; i++){
    if( IsSpace(c) ){
      if( z[j-1]=='\r' ) z[j-1] = '\n';
      if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
    }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
      j--;
    }
    z[j++] = c;
  }
  while( j>0 && IsSpace(z[j-1]) ){ j--; }
  z[j] = 0;
  if( strlen30(z)>=79 ){
    for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
      if( c==cEnd ){
        cEnd = 0;
      }else if( cEnd!=0){
        /* No-op */
      }else if( c=='"' || c=='\'' || c=='`' ){
        cEnd = c;
      }else if( c=='[' ){
        cEnd = ']';
      }else if( c=='-' && z[i+1]=='-' ){
        cEnd = '\n';
      }else if( c=='(' ){
        nParen++;
      }else if( c==')' ){
        nParen--;
        if( nLine>0 && nParen==0 && j>0 && !isWhere ){
          sqlite3_str_append(pOut, z, j);
          sqlite3_str_append(pOut, "\n", 1);
          j = 0;
        }
      }else if( (c=='w' || c=='W')
             && nParen==0 && isIndex
             && sqlite3_strnicmp("WHERE",&z[i],5)==0
             && !IsAlnum(z[i+5]) && z[i+5]!='_' ){
        isWhere = 1;
      }else if( isWhere && (c=='A' || c=='a')
             && nParen==0
             && sqlite3_strnicmp("AND",&z[i],3)==0
             && !IsAlnum(z[i+3]) && z[i+3]!='_' ){
        sqlite3_str_append(pOut, z, j);
        sqlite3_str_append(pOut, "\n    ", 5);
        j = 0;
      }
      z[j++] = c;
      if( nParen==1 && cEnd==0
       && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
       && !isWhere
      ){
        if( c=='\n' ) j--;
        sqlite3_str_append(pOut, z, j);
        sqlite3_str_append(pOut, "\n  ", 3);
        j = 0;
        nLine++;
        while( IsSpace(z[i+1]) ){ i++; }
      }
    }
    z[j] = 0;
  }
  sqlite3_str_appendall(pOut, z);
  sqlite3_str_append(pOut, ";", 1);
  sqlite3_free(z);

shellFormatSchema_finish:
  sqlite3_result_text(pCtx, sqlite3_str_finish(pOut), -1, sqlite3_free);
}

#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
/*
** Progress handler callback.
*/
static int progress_handler(void *pClientData) {
  ShellState *p = (ShellState*)pClientData;
  p->nProgress++;
  if( (p->flgProgress & SHELL_PROGRESS_TMOUT)!=0
   && ELAPSE_TIME(p)>=p->tmProgress
  ){
    cli_printf(p->out, "Progress timeout after %.6f seconds\n", 
               ELAPSE_TIME(p));
    return 1;
  }
  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
    cli_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
    return 1;
  }
  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
    cli_printf(p->out, "Progress %u\n", p->nProgress);
  }
  return 0;
}
#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */

/*
** This is the callback routine from sqlite3_exec() that appends all
** output onto the end of a ShellText object.
*/
static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
  ShellText *p = (ShellText*)pArg;
  int i;
  UNUSED_PARAMETER(az);
  if( azArg==0 ) return 0;
  if( p->n ) appendText(p, "|", 0);
  for(i=0; i<nArg; i++){
    if( i ) appendText(p, ",", 0);
    if( azArg[i] ) appendText(p, azArg[i], 0);
  }
  return 0;
}

/*
** Generate an appropriate SELFTEST table in the main database.
*/
static void createSelftestTable(ShellState *p){
  char *zErrMsg = 0;
  sqlite3_exec(p->db,
    "SAVEPOINT selftest_init;\n"
    "CREATE TABLE IF NOT EXISTS selftest(\n"
    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
    "  op TEXT,\n"                   /* Operator:  memo run */
    "  cmd TEXT,\n"                  /* Command text */
    "  ans TEXT\n"                   /* Desired answer */
    ");"
    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
    "         'memo','Tests generated by --init');\n"
    "INSERT INTO [_shell$self]\n"
    "  SELECT 'run',\n"
    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
                          "FROM sqlite_schema ORDER BY 2',224));\n"
    "INSERT INTO [_shell$self]\n"
    "  SELECT 'run',"
    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
    "  FROM (\n"
    "    SELECT name FROM sqlite_schema\n"
    "     WHERE type='table'\n"
    "       AND name<>'selftest'\n"
    "       AND coalesce(rootpage,0)>0\n"
    "  )\n"
    " ORDER BY name;\n"
    "INSERT INTO [_shell$self]\n"
    "  VALUES('run','PRAGMA integrity_check','ok');\n"
    "INSERT INTO selftest(tno,op,cmd,ans)"
    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
    "DROP TABLE [_shell$self];"
    ,0,0,&zErrMsg);
  if( zErrMsg ){
    cli_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
    sqlite3_free(zErrMsg);
  }
  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
}


/*
** Set the destination table field of the ShellState structure to
** the name of the table given.  Escape any quote characters in the
** table name.
*/
static void set_table_name(ShellState *p, const char *zName){
  if( p->zDestTable ){
    sqlite3_free(p->zDestTable);
    p->zDestTable = 0;
  }
  if( zName==0 ) return;
  p->zDestTable = sqlite3_mprintf("%s", zName);
  shell_check_oom(p->zDestTable);
}

/*
** Maybe construct two lines of text that point out the position of a
** syntax error.  Return a pointer to the text, in memory obtained from
** sqlite3_malloc().  Or, if the most recent error does not involve a
** specific token that we can point to, return an empty string.
**
** In all cases, the memory returned is obtained from sqlite3_malloc64()
** and should be released by the caller invoking sqlite3_free().
*/
static char *shell_error_context(const char *zSql, sqlite3 *db){
  int iOffset;
  size_t len;
  char *zCode;
  char *zMsg;
  int i;
  if( db==0
   || zSql==0
   || (iOffset = sqlite3_error_offset(db))<0
   || iOffset>=(int)strlen(zSql)
  ){
    return sqlite3_mprintf("");
  }
  while( iOffset>50 ){
    iOffset--;
    zSql++;
    while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; }
  }
  len = strlen(zSql);
  if( len>78 ){
    len = 78;
    while( len>0 && (zSql[len]&0xc0)==0x80 ) len--;
  }
  zCode = sqlite3_mprintf("%.*s", len, zSql);
  shell_check_oom(zCode);
  for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
  if( iOffset<25 ){
    zMsg = sqlite3_mprintf("\n  %z\n  %*s^--- error here", zCode,iOffset,"");
  }else{
    zMsg = sqlite3_mprintf("\n  %z\n  %*serror here ---^", zCode,iOffset-14,"");
  }
  return zMsg;
}


/*
** Execute a query statement that will generate SQL output.  Print
** the result columns, comma-separated, on a line and then add a
** semicolon terminator to the end of that line.
**
** If the number of columns is 1 and that column contains text "--"
** then write the semicolon on a separate line.  That way, if a
** "--" comment occurs at the end of the statement, the comment
** won't consume the semicolon terminator.
*/
static int run_table_dump_query(
  ShellState *p,           /* Query context */
  const char *zSelect      /* SELECT statement to extract content */
){
  sqlite3_stmt *pSelect;
  int rc;
  int nResult;
  int i;
  const char *z;
  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
  if( rc!=SQLITE_OK || !pSelect ){
    char *zContext = shell_error_context(zSelect, p->db);
    cli_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s",
          rc, sqlite3_errmsg(p->db), zContext);
    sqlite3_free(zContext);
    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
    return rc;
  }
  rc = sqlite3_step(pSelect);
  nResult = sqlite3_column_count(pSelect);
  while( rc==SQLITE_ROW ){
    z = (const char*)sqlite3_column_text(pSelect, 0);
    cli_printf(p->out, "%s", z);
    for(i=1; i<nResult; i++){
      cli_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
    }
    if( z==0 ) z = "";
    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
    if( z[0] ){
      cli_puts("\n;\n", p->out);
    }else{
      cli_puts(";\n", p->out);
    }
    rc = sqlite3_step(pSelect);
  }
  rc = sqlite3_finalize(pSelect);
  if( rc!=SQLITE_OK ){
    cli_printf(p->out, "/**** ERROR: (%d) %s *****/\n",
                    rc, sqlite3_errmsg(p->db));
    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
  }
  return rc;
}

/*
** Allocate space and save off string indicating current error.
*/
static char *save_err_msg(
  sqlite3 *db,           /* Database to query */
  const char *zPhase,    /* When the error occurs */
  int rc,                /* Error code returned from API */
  const char *zSql       /* SQL string, or NULL */
){
  char *zErr;
  char *zContext;
  sqlite3_str *pStr = sqlite3_str_new(0);
  sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db));
  if( rc>1 ){
    sqlite3_str_appendf(pStr, " (%d)", rc);
  }
  zContext = shell_error_context(zSql, db);
  if( zContext ){
    sqlite3_str_appendall(pStr, zContext);
    sqlite3_free(zContext);
  }
  zErr = sqlite3_str_finish(pStr);
  shell_check_oom(zErr);
  return zErr;
}

#ifdef __linux__
/*
** Attempt to display I/O stats on Linux using /proc/PID/io
*/
static void displayLinuxIoStats(FILE *out){
  FILE *in;
  char z[200];
  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
  in = sqlite3_fopen(z, "rb");
  if( in==0 ) return;
  while( sqlite3_fgets(z, sizeof(z), in)!=0 ){
    static const struct {
      const char *zPattern;
      const char *zDesc;
    } aTrans[] = {
      { "rchar: ",                  "Bytes received by read():" },
      { "wchar: ",                  "Bytes sent to write():"    },
      { "syscr: ",                  "Read() system calls:"      },
      { "syscw: ",                  "Write() system calls:"     },
      { "read_bytes: ",             "Bytes read from storage:"  },
      { "write_bytes: ",            "Bytes written to storage:" },
      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
    };
    int i;
    for(i=0; i<ArraySize(aTrans); i++){
      int n = strlen30(aTrans[i].zPattern);
      if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
        cli_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
        break;
      }
    }
  }
  fclose(in);
}
#endif

/*
** Display a single line of status using 64-bit values.
*/
static void displayStatLine(
  FILE *out,                /* Write to this channel */
  char *zLabel,             /* Label for this one line */
  char *zFormat,            /* Format for the result */
  int iStatusCtrl,          /* Which status to display */
  int bReset                /* True to reset the stats */
){
  sqlite3_int64 iCur = -1;
  sqlite3_int64 iHiwtr = -1;
  int i, nPercent;
  char zLine[200];
  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
  for(i=0, nPercent=0; zFormat[i]; i++){
    if( zFormat[i]=='%' ) nPercent++;
  }
  if( nPercent>1 ){
    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
  }else{
    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
  }
  cli_printf(out, "%-36s %s\n", zLabel, zLine);
}

/*
** Display memory stats.
*/
static int display_stats(
  sqlite3 *db,                /* Database to query */
  ShellState *pArg,           /* Pointer to ShellState */
  int bReset                  /* True to reset the stats */
){
  int iCur, iHiwtr;
  sqlite3_int64 iCur64, iHiwtr64;
  FILE *out;
  if( pArg==0 || pArg->out==0 ) return 0;
  out = pArg->out;

  if( pArg->pStmt && pArg->statsOn==2 ){
    int nCol, i, x;
    sqlite3_stmt *pStmt = pArg->pStmt;
    char z[100];
    nCol = sqlite3_column_count(pStmt);
    cli_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
    for(i=0; i<nCol; i++){
      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
      cli_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
#ifndef SQLITE_OMIT_DECLTYPE
      sqlite3_snprintf(30, z+x, "declared type:");
      cli_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
#endif
#ifdef SQLITE_ENABLE_COLUMN_METADATA
      sqlite3_snprintf(30, z+x, "database name:");
      cli_printf(out, "%-36s %s\n", z,
                           sqlite3_column_database_name(pStmt,i));
      sqlite3_snprintf(30, z+x, "table name:");
      cli_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
      sqlite3_snprintf(30, z+x, "origin name:");
      cli_printf(out, "%-36s %s\n", z,sqlite3_column_origin_name(pStmt,i));
#endif
    }
  }

  if( pArg->statsOn==3 ){
    if( pArg->pStmt ){
      iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP,bReset);
      cli_printf(out, "VM-steps: %d\n", iCur);
    }
    return 0;
  }

  displayStatLine(out, "Memory Used:",
     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
  displayStatLine(out, "Number of Outstanding Allocations:",
     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
  if( pArg->shellFlgs & SHFLG_Pagecache ){
    displayStatLine(out, "Number of Pcache Pages Used:",
       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
  }
  displayStatLine(out, "Number of Pcache Overflow Bytes:",
     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
  displayStatLine(out, "Largest Allocation:",
     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
  displayStatLine(out, "Largest Pcache Allocation:",
     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
#ifdef YYTRACKMAXSTACKDEPTH
  displayStatLine(out, "Deepest Parser Stack:",
     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
#endif

  if( db ){
    if( pArg->shellFlgs & SHFLG_Lookaside ){
      iHiwtr = iCur = -1;
      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
                        &iCur, &iHiwtr, bReset);
      cli_printf(out, 
           "Lookaside Slots Used:                %d (max %d)\n", iCur, iHiwtr);
      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
                        &iCur, &iHiwtr, bReset);
      cli_printf(out,
           "Successful lookaside attempts:       %d\n", iHiwtr);
      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
                        &iCur, &iHiwtr, bReset);
      cli_printf(out,
           "Lookaside failures due to size:      %d\n", iHiwtr);
      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
                        &iCur, &iHiwtr, bReset);
      cli_printf(out,
           "Lookaside failures due to OOM:       %d\n", iHiwtr);
    }
    iHiwtr = iCur = -1;
    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
    cli_printf(out,
           "Pager Heap Usage:                    %d bytes\n", iCur);
    iHiwtr = iCur = -1;
    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
    cli_printf(out,
           "Page cache hits:                     %d\n", iCur);
    iHiwtr = iCur = -1;
    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
    cli_printf(out,
           "Page cache misses:                   %d\n", iCur);
    iHiwtr64 = iCur64 = -1;
    sqlite3_db_status64(db, SQLITE_DBSTATUS_TEMPBUF_SPILL, &iCur64, &iHiwtr64,
                        0);
    iHiwtr = iCur = -1;
    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
    cli_printf(out,
           "Page cache writes:                   %d\n", iCur);
    iHiwtr = iCur = -1;
    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
    cli_printf(out,
           "Page cache spills:                   %d\n", iCur);
    cli_printf(out,
           "Temporary data spilled to disk:      %lld\n", iCur64);
    sqlite3_db_status64(db, SQLITE_DBSTATUS_TEMPBUF_SPILL, &iCur64, &iHiwtr64,
                        1);
    iHiwtr = iCur = -1;
    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
    cli_printf(out,
           "Schema Heap Usage:                   %d bytes\n", iCur);
    iHiwtr = iCur = -1;
    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
    cli_printf(out,
           "Statement Heap/Lookaside Usage:      %d bytes\n", iCur);
  }

  if( pArg->pStmt ){
    int iHit, iMiss;
    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
                               bReset);
    cli_printf(out,
           "Fullscan Steps:                      %d\n", iCur);
    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
    cli_printf(out,
           "Sort Operations:                     %d\n", iCur);
    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
    cli_printf(out,
           "Autoindex Inserts:                   %d\n", iCur);
    iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT,
                               bReset);
    iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS,
                                bReset);
    if( iHit || iMiss ){
      cli_printf(out,
           "Bloom filter bypass taken:           %d/%d\n", iHit, iHit+iMiss);
    }
    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
    cli_printf(out,
           "Virtual Machine Steps:               %d\n", iCur);
    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
    cli_printf(out,
           "Reprepare operations:                %d\n", iCur);
    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
    cli_printf(out,
           "Number of times run:                 %d\n", iCur);
    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
    cli_printf(out,
           "Memory used by prepared stmt:        %d\n", iCur);
  }

#ifdef __linux__
  displayLinuxIoStats(pArg->out);
#endif

  /* Do not remove this machine readable comment: extra-stats-output-here */

  return 0;
}

/*
** Disable and restore .wheretrace and .treetrace/.selecttrace settings.
*/
static unsigned int savedSelectTrace;
static unsigned int savedWhereTrace;
static void disable_debug_trace_modes(void){
  unsigned int zero = 0;
  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
}
static void restore_debug_trace_modes(void){
  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
}

/* Create the TEMP table used to store parameter bindings */
static void bind_table_init(ShellState *p){
  int wrSchema = 0;
  int defensiveMode = 0;
  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
  sqlite3_exec(p->db,
    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
    "  key TEXT PRIMARY KEY,\n"
    "  value\n"
    ") WITHOUT ROWID;",
    0, 0, 0);
  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
}

/*
** Bind parameters on a prepared statement.
**
** Parameter bindings are taken from a TEMP table of the form:
**
**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
**    WITHOUT ROWID;
**
** No bindings occur if this table does not exist.  The name of the table
** begins with "sqlite_" so that it will not collide with ordinary application
** tables.  The table must be in the TEMP schema.
*/
static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
  int nVar;
  int i;
  int rc;
  sqlite3_stmt *pQ = 0;

  nVar = sqlite3_bind_parameter_count(pStmt);
  if( nVar==0 ) return;  /* Nothing to do */
  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
    rc = SQLITE_NOTFOUND;
    pQ = 0;
  }else{
    rc = sqlite3_prepare_v2(pArg->db,
            "SELECT value FROM temp.sqlite_parameters"
            " WHERE key=?1", -1, &pQ, 0);
  }
  for(i=1; i<=nVar; i++){
    char zNum[30];
    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
    if( zVar==0 ){
      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
      zVar = zNum;
    }
    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
    if( rc==SQLITE_OK && pQ && sqlite3_step(pQ)==SQLITE_ROW ){
      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
#ifdef NAN
    }else if( sqlite3_strlike("_NAN", zVar, 0)==0 ){
      sqlite3_bind_double(pStmt, i, NAN);
#endif
#ifdef INFINITY
    }else if( sqlite3_strlike("_INF", zVar, 0)==0 ){
      sqlite3_bind_double(pStmt, i, INFINITY);
#endif
    }else if( strncmp(zVar, "$int_", 5)==0 ){
      sqlite3_bind_int(pStmt, i, atoi(&zVar[5]));
    }else if( strncmp(zVar, "$text_", 6)==0 ){
      size_t szVar = strlen(zVar);
      char *zBuf = sqlite3_malloc64( szVar-5 );
      if( zBuf ){
        memcpy(zBuf, &zVar[6], szVar-5);
        sqlite3_bind_text64(pStmt, i, zBuf, szVar-6, sqlite3_free, SQLITE_UTF8);
      }
    }else if( strcmp(zVar, "$TIMER")==0 ){
      sqlite3_bind_double(pStmt, i, pArg->prevTimer);
#ifdef SQLITE_ENABLE_CARRAY
    }else if( strncmp(zVar, "$carray_", 8)==0 ){
      static char *azColorNames[] = {
        "azure", "black", "blue",   "brown", "cyan",   "fuchsia", "gold",
        "gray",  "green", "indigo", "khaki", "lime",   "magenta", "maroon",
        "navy",  "olive", "orange", "pink",  "purple", "red",     "silver",
        "tan",   "teal",  "violet", "white", "yellow"
      };
      static int aPrimes[] = {
        1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
       53, 59, 61, 67, 71, 73, 79, 83, 89, 97
      };
      /* Special bindings:  carray($carray_clr), carray($carray_primes)
      ** with --unsafe-testing:  carray($carray_clr_p,26,'char*'),
      **                         carray($carray_primes_p,26,'int32')
      */
      if( strcmp(zVar+8,"clr")==0 ){
        sqlite3_carray_bind(pStmt,i,azColorNames,26,SQLITE_CARRAY_TEXT,0);
      }else if( strcmp(zVar+8,"primes")==0 ){
        sqlite3_carray_bind(pStmt,i,aPrimes,26,SQLITE_CARRAY_INT32,0);
      }else if( strcmp(zVar+8,"clr_p")==0
             && ShellHasFlag(pArg,SHFLG_TestingMode) ){
        sqlite3_bind_pointer(pStmt,i,azColorNames,"carray",0);
      }else if( strcmp(zVar+8,"primes_p")==0
             && ShellHasFlag(pArg,SHFLG_TestingMode) ){
        sqlite3_bind_pointer(pStmt,i,aPrimes,"carray",0);
      }else{
        sqlite3_bind_null(pStmt, i);
      }
#endif  
    }else{
      sqlite3_bind_null(pStmt, i);
    }
    sqlite3_reset(pQ);
  }
  sqlite3_finalize(pQ);
}

#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
/*
** This function is called to process SQL if the previous shell command
** was ".expert". It passes the SQL in the second argument directly to
** the sqlite3expert object.
**
** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
** code. In this case, (*pzErr) may be set to point to a buffer containing
** an English language error message. It is the responsibility of the
** caller to eventually free this buffer using sqlite3_free().
*/
static int expertHandleSQL(
  ShellState *pState,
  const char *zSql,
  char **pzErr
){
  assert( pState->expert.pExpert );
  assert( pzErr==0 || *pzErr==0 );
  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
}

/*
** This function is called either to silently clean up the object
** created by the ".expert" command (if bCancel==1), or to generate a
** report from it and then clean it up (if bCancel==0).
**
** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
** code. In this case, (*pzErr) may be set to point to a buffer containing
** an English language error message. It is the responsibility of the
** caller to eventually free this buffer using sqlite3_free().
*/
static int expertFinish(
  ShellState *pState,
  int bCancel,
  char **pzErr
){
  int rc = SQLITE_OK;
  sqlite3expert *p = pState->expert.pExpert;
  FILE *out = pState->out;
  assert( p );
  assert( bCancel || pzErr==0 || *pzErr==0 );
  if( bCancel==0 ){
    int bVerbose = pState->expert.bVerbose;

    rc = sqlite3_expert_analyze(p, pzErr);
    if( rc==SQLITE_OK ){
      int nQuery = sqlite3_expert_count(p);
      int i;

      if( bVerbose ){
        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
        cli_puts("-- Candidates -----------------------------\n", out);
        cli_printf(out, "%s\n", zCand);
      }
      for(i=0; i<nQuery; i++){
        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
        if( zIdx==0 ) zIdx = "(no new indexes)\n";
        if( bVerbose ){
          cli_printf(out,
              "-- Query %d --------------------------------\n"
              "%s\n\n"
              ,i+1, zSql);
        }
        cli_printf(out, "%s\n%s\n", zIdx, zEQP);
      }
    }
  }
  sqlite3_expert_destroy(p);
  pState->expert.pExpert = 0;
  return rc;
}

/*
** Implementation of ".expert" dot command.
*/
static int expertDotCommand(
  ShellState *pState,             /* Current shell tool state */
  char **azArg,                   /* Array of arguments passed to dot command */
  int nArg                        /* Number of entries in azArg[] */
){
  int rc = SQLITE_OK;
  char *zErr = 0;
  int i;
  int iSample = 0;

  assert( pState->expert.pExpert==0 );
  memset(&pState->expert, 0, sizeof(ExpertInfo));

  for(i=1; rc==SQLITE_OK && i<nArg; i++){
    char *z = azArg[i];
    int n;
    if( z[0]=='-' && z[1]=='-' ) z++;
    n = strlen30(z);
    if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){
      pState->expert.bVerbose = 1;
    }
    else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
      if( i==(nArg-1) ){
        cli_printf(stderr, "option requires an argument: %s\n", z);
        rc = SQLITE_ERROR;
      }else{
        iSample = (int)integerValue(azArg[++i]);
        if( iSample<0 || iSample>100 ){
          cli_printf(stderr,"value out of range: %s\n", azArg[i]);
          rc = SQLITE_ERROR;
        }
      }
    }
    else{
      cli_printf(stderr,"unknown option: %s\n", z);
      rc = SQLITE_ERROR;
    }
  }

  if( rc==SQLITE_OK ){
    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
    if( pState->expert.pExpert==0 ){
      cli_printf(stderr,
          "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
      rc = SQLITE_ERROR;
    }else{
      sqlite3_expert_config(
          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
      );
    }
  }
  sqlite3_free(zErr);

  return rc;
}
#endif /* !SQLITE_OMIT_VIRTUALTABLE && !SQLITE_OMIT_AUTHORIZATION */

/*
** QRF write callback
*/
static int shellWriteQR(void *pX, const char *z, sqlite3_int64 n){
  ShellState *pArg = (ShellState*)pX;
  cli_write(pArg->out, z, (int)n);
  return SQLITE_OK;
}

/*
** Execute a statement or set of statements.  Print
** any result rows/columns depending on the current mode
** set via the supplied callback.
**
** This is very similar to SQLite's built-in sqlite3_exec()
** function except it takes a slightly different callback
** and callback data argument.
*/
static int shell_exec(
  ShellState *pArg,                         /* Pointer to ShellState */
  const char *zSql,                         /* SQL to be evaluated */
  char **pzErrMsg                           /* Error msg written here */
){
  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
  int rc = SQLITE_OK;             /* Return Code */
  int rc2;
  const char *zLeftover;          /* Tail of unprocessed SQL */
  sqlite3 *db = pArg->db;
  unsigned char eStyle;
  sqlite3_qrf_spec spec;

  if( pzErrMsg ){
    *pzErrMsg = NULL;
  }
  memcpy(&spec, &pArg->mode.spec, sizeof(spec));
  spec.xWrite = shellWriteQR;
  spec.pWriteArg = (void*)pArg;
  if( pArg->mode.eMode==MODE_Insert && ShellHasFlag(pArg,SHFLG_PreserveRowid) ){
    spec.bTitles = QRF_SW_On;
  }
         /*                        ,- This is true, but it is omitted
         ** vvvvvvvvvvvvvvvvvvv ----- to avoid compiler warnings.         */
  assert( /*pArg->mode.eMode>=0 &&*/ pArg->mode.eMode<ArraySize(aModeInfo) );
  eStyle = aModeInfo[pArg->mode.eMode].eStyle;
  if( pArg->mode.bAutoScreenWidth ){
    spec.nScreenWidth = shellScreenWidth();
  }
  if( spec.eBlob==QRF_BLOB_Auto ){
    switch( spec.eText ){
      case QRF_TEXT_Relaxed: /* fall through */
      case QRF_TEXT_Sql:  spec.eBlob = QRF_BLOB_Sql;   break;
      case QRF_TEXT_Json: spec.eBlob = QRF_BLOB_Json;  break;
      default:            spec.eBlob = QRF_BLOB_Text;  break;
    }
  }

#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
  if( pArg->expert.pExpert ){
    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
  }
#endif

  while( zSql && zSql[0] && (SQLITE_OK == rc) ){
    static const char *zStmtSql;
    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
    if( SQLITE_OK != rc ){
      if( pzErrMsg ){
        *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql);
      }
    }else{
      int isExplain;
      if( !pStmt ){
        /* this happens for a comment or white-space */
        zSql = zLeftover;
        while( IsSpace(zSql[0]) ) zSql++;
        continue;
      }
      zStmtSql = sqlite3_sql(pStmt);
      if( zStmtSql==0 ) zStmtSql = "";
      while( IsSpace(zStmtSql[0]) ) zStmtSql++;

      /* save off the prepared statement handle */
      if( pArg ){
        pArg->pStmt = pStmt;
      }
     
      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
      isExplain = sqlite3_stmt_isexplain(pStmt);
      if( pArg && pArg->mode.autoEQP && isExplain==0 && pArg->dot.nArg==0 ){
        int triggerEQP = 0;
        u8 savedEnableTimer = pArg->enableTimer;
        pArg->enableTimer = 0;
        disable_debug_trace_modes();
        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
        if( pArg->mode.autoEQP>=AUTOEQP_trigger ){
          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
        }
        sqlite3_reset(pStmt);
        spec.eStyle = QRF_STYLE_Auto;
        sqlite3_stmt_explain(pStmt, 2);
        sqlite3_format_query_result(pStmt, &spec, 0);
        if( pArg->mode.autoEQP>=AUTOEQP_full ){
          sqlite3_reset(pStmt);
          sqlite3_stmt_explain(pStmt, 1);
          sqlite3_format_query_result(pStmt, &spec, 0);
        }

        if( pArg->mode.autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
        }
        sqlite3_reset(pStmt);
        sqlite3_stmt_explain(pStmt, 0);
        restore_debug_trace_modes();
        pArg->enableTimer = savedEnableTimer;
      }

      bind_prepared_stmt(pArg, pStmt);
      if( isExplain && pArg->mode.autoExplain ){
        spec.eStyle = isExplain==1 ? QRF_STYLE_Explain : QRF_STYLE_Eqp;
        sqlite3_format_query_result(pStmt, &spec, pzErrMsg);
      }else if( pArg->mode.eMode==MODE_Www ){
        cli_printf(pArg->out,
              "</PRE>\n"
              "<TABLE border='1' cellspacing='0' cellpadding='2'>\n");
        spec.eStyle = QRF_STYLE_Html;
        sqlite3_format_query_result(pStmt, &spec, pzErrMsg);
        cli_printf(pArg->out,
              "</TABLE>\n"
              "<PRE>");
      }else{
        spec.eStyle = eStyle;
        sqlite3_format_query_result(pStmt, &spec, pzErrMsg);
      }

      /* print usage stats if stats on */
      if( pArg && pArg->statsOn ){
        display_stats(db, pArg, 0);
      }

      /* print loop-counters if required */
      if( pArg && pArg->mode.scanstatsOn ){
        char *zErr = 0;
        switch( pArg->mode.scanstatsOn ){
          case 1:   spec.eStyle = QRF_STYLE_Stats;     break;
          case 2:   spec.eStyle = QRF_STYLE_StatsEst;  break;
          default:  spec.eStyle = QRF_STYLE_StatsVm;   break;
        }
        sqlite3_reset(pStmt);
        rc = sqlite3_format_query_result(pStmt, &spec, &zErr);
        if( rc ){
          cli_printf(stderr, "Stats query failed: %s\n", zErr);
          sqlite3_free(zErr);
        }          
      }

      /* Finalize the statement just executed. If this fails, save a
      ** copy of the error message. Otherwise, set zSql to point to the
      ** next statement to execute. */
      rc2 = sqlite3_finalize(pStmt);
      if( rc!=SQLITE_NOMEM ) rc = rc2;
      if( rc==SQLITE_OK ){
        zSql = zLeftover;
        while( IsSpace(zSql[0]) ) zSql++;
      }else if( pzErrMsg ){
        *pzErrMsg = save_err_msg(db, "stepping", rc, 0);
      }

      /* clear saved stmt handle */
      if( pArg ){
        pArg->pStmt = NULL;
      }
    }
  } /* end while */

  return rc;
}

/*
** Release memory previously allocated by tableColumnList().
*/
static void freeColumnList(char **azCol){
  int i;
  for(i=1; azCol[i]; i++){
    sqlite3_free(azCol[i]);
  }
  /* azCol[0] is a static string */
  sqlite3_free(azCol);
}

/*
** Return a list of pointers to strings which are the names of all
** columns in table zTab.   The memory to hold the names is dynamically
** allocated and must be released by the caller using a subsequent call
** to freeColumnList().
**
** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
** value that needs to be preserved, then azCol[0] is filled in with the
** name of the rowid column.
**
** The first regular column in the table is azCol[1].  The list is terminated
** by an entry with azCol[i]==0.
*/
static char **tableColumnList(ShellState *p, const char *zTab){
  char **azCol = 0;
  sqlite3_stmt *pStmt;
  char *zSql;
  int nCol = 0;
  i64 nAlloc = 0;
  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
  int rc;

  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
  shell_check_oom(zSql);
  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
  sqlite3_free(zSql);
  if( rc ) return 0;
  while( sqlite3_step(pStmt)==SQLITE_ROW ){
    if( nCol>=nAlloc-2 ){
      nAlloc = nAlloc*2 + nCol + 10;
      azCol = sqlite3_realloc64(azCol, nAlloc*sizeof(azCol[0]));
      shell_check_oom(azCol);
    }
    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
    shell_check_oom(azCol[nCol]);
    if( sqlite3_column_int(pStmt, 5) ){
      nPK++;
      if( nPK==1
       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
                          "INTEGER")==0
      ){
        isIPK = 1;
      }else{
        isIPK = 0;
      }
    }
  }
  sqlite3_finalize(pStmt);
  if( azCol==0 ) return 0;
  azCol[0] = 0;
  azCol[nCol+1] = 0;

  /* The decision of whether or not a rowid really needs to be preserved
  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
  ** rowids on tables where the rowid is inaccessible because there are other
  ** columns in the table named "rowid", "_rowid_", and "oid".
  */
  if( preserveRowid && isIPK ){
    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
    ** might be an alias for the ROWID.  But it might also be a WITHOUT ROWID
    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
    ** ROWID aliases.  To distinguish these cases, check to see if
    ** there is a "pk" entry in "PRAGMA index_list".  There will be
    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
    */
    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
                           " WHERE origin='pk'", zTab);
    shell_check_oom(zSql);
    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
    sqlite3_free(zSql);
    if( rc ){
      freeColumnList(azCol);
      return 0;
    }
    rc = sqlite3_step(pStmt);
    sqlite3_finalize(pStmt);
    preserveRowid = rc==SQLITE_ROW;
  }
  if( preserveRowid ){
    /* Only preserve the rowid if we can find a name to use for the
    ** rowid */
    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
    int i, j;
    for(j=0; j<3; j++){
      for(i=1; i<=nCol; i++){
        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
      }
      if( i>nCol ){
        /* At this point, we know that azRowid[j] is not the name of any
        ** ordinary column in the table.  Verify that azRowid[j] is a valid
        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
        ** tables will fail this last check */
        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
        break;
      }
    }
  }
  return azCol;
}

/*
** Toggle the reverse_unordered_selects setting.
*/
static void toggleSelectOrder(sqlite3 *db){
  sqlite3_stmt *pStmt = 0;
  int iSetting = 0;
  char zStmt[100];
  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
  if( sqlite3_step(pStmt)==SQLITE_ROW ){
    iSetting = sqlite3_column_int(pStmt, 0);
  }
  sqlite3_finalize(pStmt);
  sqlite3_snprintf(sizeof(zStmt), zStmt,
       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
  sqlite3_exec(db, zStmt, 0, 0, 0);
}

/* Forward reference */
static int db_int(sqlite3 *db, const char *zSql, ...);

/*
** This is a different callback routine used for dumping the database.
** Each row received by this callback consists of a table name,
** the table type ("index" or "table") and SQL to create the table.
** This routine should print text sufficient to recreate the table.
*/
static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
  int rc;
  const char *zTable;
  const char *zType;
  const char *zSql;
  ShellState *p = (ShellState *)pArg;
  int dataOnly;
  int noSys;

  UNUSED_PARAMETER(azNotUsed);
  if( nArg!=3 || azArg==0 ) return 0;
  zTable = azArg[0];
  zType = azArg[1];
  zSql = azArg[2];
  if( zTable==0 ) return 0;
  if( zType==0 ) return 0;
  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;

  if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
    /* The sqlite_sequence table is repopulated last.  Delete content
    ** in the sqlite_sequence table added by prior repopulations prior to
    ** repopulating sqlite_sequence itself.  But only do this if the
    ** table is non-empty, because if it is empty the table might not
    ** have been recreated by prior repopulations. See forum posts:
    ** 2024-10-13T17:10:01z and 2025-10-29T19:38:43z
    */
    if( db_int(p->db, "SELECT count(*) FROM sqlite_sequence")>0 ){
      if( !p->writableSchema ){
        cli_puts("PRAGMA writable_schema=ON;\n", p->out);
        p->writableSchema = 1;
      }
      cli_puts("CREATE TABLE IF NOT EXISTS sqlite_sequence(name,seq);\n"
                    "DELETE FROM sqlite_sequence;\n", p->out);
    }
  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
    if( !dataOnly ) cli_puts("ANALYZE sqlite_schema;\n", p->out);
  }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
    return 0;
  }else if( dataOnly ){
    /* no-op */
  }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
    char *zIns;
    if( !p->writableSchema ){
      cli_puts("PRAGMA writable_schema=ON;\n", p->out);
      p->writableSchema = 1;
    }
    zIns = sqlite3_mprintf(
       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
       "VALUES('table','%q','%q',0,'%q');",
       zTable, zTable, zSql);
    shell_check_oom(zIns);
    cli_printf(p->out, "%s\n", zIns);
    sqlite3_free(zIns);
    return 0;
  }else{
    printSchemaLine(p->out, zSql, ";\n");
  }

  if( cli_strcmp(zType, "table")==0 ){
    ShellText sSelect;
    ShellText sTable;
    char **azCol;
    int i;
    Mode savedMode;

    azCol = tableColumnList(p, zTable);
    if( azCol==0 ){
      p->nErr++;
      return 0;
    }

    /* Always quote the table name, even if it appears to be pure ascii,
    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
    initText(&sTable);
    appendText(&sTable, zTable, quoteChar(zTable));
    /* If preserving the rowid, add a column list after the table name.
    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
    ** instead of the usual "INSERT INTO tab VALUES(...)".
    */
    if( azCol[0] ){
      appendText(&sTable, "(", 0);
      appendText(&sTable, azCol[0], 0);
      for(i=1; azCol[i]; i++){
        appendText(&sTable, ",", 0);
        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
      }
      appendText(&sTable, ")", 0);
    }

    /* Build an appropriate SELECT statement */
    initText(&sSelect);
    appendText(&sSelect, "SELECT ", 0);
    if( azCol[0] ){
      appendText(&sSelect, azCol[0], 0);
      appendText(&sSelect, ",", 0);
    }
    for(i=1; azCol[i]; i++){
      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
      if( azCol[i+1] ){
        appendText(&sSelect, ",", 0);
      }
    }
    freeColumnList(azCol);
    appendText(&sSelect, " FROM ", 0);
    appendText(&sSelect, zTable, quoteChar(zTable));


    savedMode = p->mode;
    p->mode.spec.zTableName = (char*)zTable;
    p->mode.eMode = MODE_Insert;
    p->mode.spec.eText = QRF_TEXT_Sql;
    p->mode.spec.eBlob = QRF_BLOB_Sql;
    p->mode.spec.bTitles = QRF_No;
    p->mode.spec.nCharLimit = 0;
    rc = shell_exec(p, sSelect.zTxt, 0);
    if( (rc&0xff)==SQLITE_CORRUPT ){
      cli_puts("/****** CORRUPTION ERROR *******/\n", p->out);
      toggleSelectOrder(p->db);
      shell_exec(p, sSelect.zTxt, 0);
      toggleSelectOrder(p->db);
    }
    p->mode = savedMode;
    freeText(&sTable);
    freeText(&sSelect);
    if( rc ) p->nErr++;
  }
  return 0;
}

/*
** Run zQuery.  Use dump_callback() as the callback routine so that
** the contents of the query are output as SQL statements.
**
** If we get a SQLITE_CORRUPT error, rerun the query after appending
** "ORDER BY rowid DESC" to the end.
*/
static int run_schema_dump_query(
  ShellState *p,
  const char *zQuery
){
  int rc;
  char *zErr = 0;
  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
  if( rc==SQLITE_CORRUPT ){
    char *zQ2;
    int len = strlen30(zQuery);
    cli_puts("/****** CORRUPTION ERROR *******/\n", p->out);
    if( zErr ){
      cli_printf(p->out, "/****** %s ******/\n", zErr);
      sqlite3_free(zErr);
      zErr = 0;
    }
    zQ2 = malloc( len+100 );
    if( zQ2==0 ) return rc;
    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
    if( rc ){
      cli_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
    }else{
      rc = SQLITE_CORRUPT;
    }
    free(zQ2);
  }
  sqlite3_free(zErr);
  return rc;
}

/*
** Text of help messages.
**
** The help text for each individual command begins with a line that starts
** with ".".  Subsequent lines are supplemental information.
**
** There must be two or more spaces between the end of the command and the
** start of the description of what that command does.
*/
static const char *(azHelp[]) = {
#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
  && !defined(SQLITE_SHELL_FIDDLE)
  ".archive ...             Manage SQL archives",
  "   Each command must have exactly one of the following options:",
  "     -c, --create               Create a new archive",
  "     -u, --update               Add or update files with changed mtime",
  "     -i, --insert               Like -u but always add even if unchanged",
  "     -r, --remove               Remove files from archive",
  "     -t, --list                 List contents of archive",
  "     -x, --extract              Extract files from archive",
  "   Optional arguments:",
  "     -v, --verbose              Print each filename as it is processed",
  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
  "     -g, --glob                 Use glob matching for names in archive",
  "     -n, --dryrun               Show the SQL that would have occurred",
  "   Examples:",
  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
  "   See also:",
  "      http://sqlite.org/cli.html#sqlite_archive_support",
#endif
#ifndef SQLITE_OMIT_AUTHORIZATION
  ".auth ON|OFF             Show authorizer callbacks",
#endif
#ifndef SQLITE_SHELL_FIDDLE
  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
  "   Options:",
  "       --append            Use the appendvfs",
  "       --async             Write to FILE without journal and fsync()",
#endif
  ".bail on|off             Stop after hitting an error.  Default OFF",
#ifndef SQLITE_SHELL_FIDDLE
  ".cd DIRECTORY            Change the working directory to DIRECTORY",
#endif
  ".changes on|off          Show number of rows changed by SQL",
  ".check OPTIONS ...       Verify the results of a .testcase",
#ifndef SQLITE_SHELL_FIDDLE
  ".clone NEWDB             Clone data into NEWDB from the existing database",
#endif
  ".connection [close] [#]  Open or close an auxiliary database connection",
  ".crlf ?on|off?           Whether or not to use \\r\\n line endings",
  ".databases               List names and files of attached databases",
  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
#if SQLITE_SHELL_HAVE_RECOVER
  ".dbinfo ?DB?             Show status information about the database",
#endif
  ".dbtotxt                 Hex dump of the database file",
  ".diskused ?SCHEMA?       Report database space and size stats",
  ".dump ?OBJECTS?          Render database content as SQL",
  "   Options:",
  "     --data-only            Output only INSERT statements",
  "     --newlines             Allow unescaped newline characters in output",
  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
  "     --preserve-rowids      Include ROWID values in the output",
  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
  "   Additional LIKE patterns can be given in subsequent arguments",
  ".echo on|off             Turn command echo on or off",
  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
  "   Other Modes:",
#ifdef SQLITE_DEBUG
  "      test                  Show raw EXPLAIN QUERY PLAN output",
  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
#endif
  "      trigger               Like \"full\" but also show trigger bytecode",
#ifndef SQLITE_SHELL_FIDDLE
  ".excel                   Display the output of next command in spreadsheet",
  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
#endif
#ifndef SQLITE_SHELL_FIDDLE
  ".exit ?CODE?             Exit this program with return-code CODE",
#endif
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
#endif
  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
  "   --help                  Show CMD details",
  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
  ".headers on|off          Turn display of headers on or off",
  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
#ifndef SQLITE_SHELL_FIDDLE
  ".import FILE TABLE       Import data from FILE into TABLE",
#endif
#ifndef SQLITE_OMIT_TEST_CONTROL
  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
#endif
  ".indexes ?PATTERN?       Show names of indexes matching PATTERN",
  "   -a|--all                Also show system-generated indexes",
  "   --expr                  Show only expression indexes",
  "   --sys                   Show only system-generated indexes",
  ".intck ?STEPS?           Run an incremental integrity check on the db",
#ifdef SQLITE_ENABLE_IOTRACE
  ",iotrace FILE            Enable I/O diagnostic logging to FILE",
#endif
  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
  ".lint OPTIONS            Report potential schema issues.",
  "     Options:",
  "        fkey-indexes     Find missing foreign key indexes",
#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
  ".load FILE ?ENTRY?       Load an extension library",
#endif
#if !defined(SQLITE_SHELL_FIDDLE)
  ".log FILE|on|off         Turn logging on or off.  FILE can be stderr/stdout",
#else
  ".log on|off              Turn logging on or off.",
#endif
  ".mode ?MODE? ?OPTIONS?   Set output mode",
#ifndef SQLITE_SHELL_FIDDLE
  ".nonce STRING            Suspend safe mode for one command if nonce matches",
#endif
  ".nullvalue STRING        Use STRING in place of NULL values",
#ifndef SQLITE_SHELL_FIDDLE
  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
  /* Note that .open is (partially) available in WASM builds but is
  ** currently only intended to be used by the fiddle tool, not
  ** end users, so is "undocumented." */
  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
  "     Options:",
  "        --append        Use appendvfs to append database to the end of FILE",
#endif
#ifndef SQLITE_OMIT_DESERIALIZE
  "        --deserialize   Load into memory using sqlite3_deserialize()",
#endif
/*"        --exclusive     Set the SQLITE_OPEN_EXCLUSIVE flag", UNDOCUMENTED */
#ifndef SQLITE_OMIT_DESERIALIZE
  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
#endif
  "        --ifexist       Only open if FILE already exists",
#ifndef SQLITE_OMIT_DESERIALIZE
  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
#endif
  "        --new           Initialize FILE to an empty database",
  "        --normal        FILE is an ordinary SQLite database",
  "        --nofollow      Do not follow symbolic links",
  "        --readonly      Open FILE readonly",
  "        --zip           FILE is a ZIP archive",
#ifndef SQLITE_SHELL_FIDDLE
  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
#endif
  ".parameter CMD ...       Manage SQL parameter bindings",
  "   clear                   Erase all bindings",
  "   init                    Initialize the TEMP table that holds bindings",
  "   list                    List the current parameter bindings",
  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
  "                           PARAMETER should start with one of: $ : @ ?",
  "   unset PARAMETER         Remove PARAMETER from the binding table",
  ".print STRING...         Print literal STRING",
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
  ".progress N              Invoke progress handler after every N opcodes",
  "   --limit N                 Interrupt after N progress callbacks",
  "   --once                    Do no more than one progress interrupt",
  "   --quiet|-q                No output except at interrupts",
  "   --reset                   Reset the count for each input and interrupt",
  "   --timeout S               Halt after running for S seconds",
#endif
  ".prompt MAIN CONTINUE    Replace the standard prompts",
  "   --hard-reset              Unset SQLITE_PS1/2 and then --reset",
#ifndef SQLITE_NO_COLOR
  "   --no-color                Disable color prompts. Use --color to re-enable",
#endif
  "   --reset                   Revert to default prompts",
  "   --show                    Show the current prompt strings",
  "   --                        No more options. Subsequent args are prompts",
#ifndef SQLITE_SHELL_FIDDLE
  ".quit                    Stop interpreting input stream, exit if primary.",
  ".read FILE               Read input from FILE or command output",
  "    If FILE begins with \"|\", it is a command that generates the input.",
#endif
#if SQLITE_SHELL_HAVE_RECOVER
  ".recover                 Recover as much data as possible from corrupt db.",
  "   --ignore-freelist        Ignore pages that appear to be on db freelist",
  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
  "   --no-rowids              Do not attempt to recover rowid values",
  "                            that are not also INTEGER PRIMARY KEYs",
#endif
#ifndef SQLITE_SHELL_FIDDLE
  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
  ".save ?OPTIONS? FILE     Write database to FILE (an alias for .backup ...)",
#endif
  ".scanstats on|off|est    Turn sqlite3_stmt_scanstatus() metrics on or off",
  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
  "   Options:",
  "      --indent             Try to pretty-print the schema",
  "      --nosys              Omit objects whose names start with \"sqlite_\"",
  ",selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
  "    Options:",
  "       --init               Create a new SELFTEST table",
  "       -v                   Verbose output",
  ",separator COL ?ROW?     Change the column and row separators",
#if defined(SQLITE_ENABLE_SESSION)
  ".session ?NAME? CMD ...  Create or control sessions",
  "   Subcommands:",
  "     attach TABLE             Attach TABLE",
  "     changeset FILE           Write a changeset into FILE",
  "     close                    Close one session",
  "     enable ?BOOLEAN?         Set or query the enable bit",
  "     filter GLOB...           Reject tables matching GLOBs",
  "     indirect ?BOOLEAN?       Mark or query the indirect status",
  "     isempty                  Query whether the session is empty",
  "     list                     List currently open session names",
  "     open DB NAME             Open a new session on DB",
  "     patchset FILE            Write a patchset into FILE",
  "   If ?NAME? is omitted, the first defined session is used.",
#endif
  ".sha3sum ...             Compute a SHA3 hash of database content",
  "    Options:",
  "      --schema              Also hash the sqlite_schema table",
  "      --sha3-224            Use the sha3-224 algorithm",
  "      --sha3-256            Use the sha3-256 algorithm (default)",
  "      --sha3-384            Use the sha3-384 algorithm",
  "      --sha3-512            Use the sha3-512 algorithm",
  "    Any other argument is a LIKE pattern for tables to hash",
#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
#endif
  ",show                    Show the current values for various settings",
  ".stats ?ARG?             Show stats or turn stats on or off",
  "   off                      Turn off automatic stat display",
  "   on                       Turn on automatic stat display",
  "   stmt                     Show statement stats",
  "   vmstep                   Show the virtual machine step count only",
#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
#endif
  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
  ".testcase NAME           Begin a test case.",
  ",testctrl CMD ...        Run various sqlite3_test_control() operations",
  "                           Run \".testctrl\" with no arguments for details",
  ".timeout MS              Try opening locked tables for MS milliseconds",
  ".timer on|off|once       Turn SQL timer on or off.",
#ifndef SQLITE_OMIT_TRACE
  ".trace ?OPTIONS?         Output each SQL statement as it is run",
  "    FILE                    Send output to FILE",
  "    stdout                  Send output to stdout",
  "    stderr                  Send output to stderr",
  "    off                     Disable tracing",
  "    --expanded              Expand query parameters",
#ifdef SQLITE_ENABLE_NORMALIZE
  "    --normalized            Normal the SQL statements",
#endif
  "    --plain                 Show SQL as it is input",
  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
#endif /* SQLITE_OMIT_TRACE */
#ifdef SQLITE_DEBUG
  ".unmodule NAME ...       Unregister virtual table modules",
  "    --allexcept             Unregister everything except those named",
#endif
  ".version                 Show source, library and compiler versions",
  ".vfsinfo ?AUX?           Information about the top-level VFS",
  ".vfslist                 List all available VFSes",
  ".vfsname ?AUX?           Print the name of the VFS stack",
  ",width NUM1 NUM2 ...     Set minimum column widths for columnar output",
  "     Negative values right-justify",
#ifndef SQLITE_SHELL_FIDDLE
  ".www                     Display output of the next command in web browser",
  "    --plain                 Show results as text/plain, not as HTML",
#endif
};

INSERT-USAGE-TEXT-HERE

/*
** Return a pointer to usage text for zCmd, or NULL if none exists.
*/
static const char *findUsage(const char *zCmd){
  int i;
  for(i=0; i<ArraySize(aUsage); i++){
    if( sqlite3_strglob(zCmd, aUsage[i].zCmd)==0 ) return aUsage[i].zUsage;
  }
  return 0;
}

/*
** Output help text for commands that match zPattern.
**
**    *   If zPattern is NULL, then show all documented commands, but
**        only give a one-line summary of each.
**
**    *   If zPattern is "-a" or "-all" or "--all" then show all help text
**        for all commands except undocumented commands.
**
**    *   If zPattern is "0" then show all help for undocumented commands.
**        Undocumented commands begin with "," instead of "." in the azHelp[]
**        array.
**
**    *   If zPattern is a prefix for one or more documented commands, then
**        show help for those commands.  If only a single command matches the
**        prefix, show the full text of the help.  If multiple commands match,
**        Only show just the first line of each.
**
**    *   Otherwise, show the complete text of any documented command for which
**        zPattern is a LIKE match for any text within that command help
**        text.
**
** Return the number commands that match zPattern.
*/
static int showHelp(FILE *out, const char *zPattern){
  int i = 0;
  int j = 0;
  int n = 0;
  char *zPat;
  const char *zHit = 0;
  if( zPattern==0 ){
    /* Show just the first line for all help topics */
    zPattern = "[a-z]";
  }else if( cli_strcmp(zPattern,"-a")==0
         || cli_strcmp(zPattern,"-all")==0
         || cli_strcmp(zPattern,"--all")==0
  ){
    /* Show everything except undocumented commands */
    zPattern = ".";
  }else if( cli_strcmp(zPattern,"0")==0 ){
    /* Show complete help text of undocumented commands */
    int show = 0;
    for(i=0; i<ArraySize(azHelp); i++){
      if( azHelp[i][0]=='.' ){
        show = 0;
      }else if( azHelp[i][0]==',' ){
        show = 1;
        cli_printf(out, ".%s\n", &azHelp[i][1]);
        n++;
      }else if( show ){
        cli_printf(out, "%s\n", azHelp[i]);
      }
    }
    return n;
  }

  /* Seek documented commands for which zPattern is an exact prefix */
  zPat = sqlite3_mprintf(".%s*", zPattern[0]=='.' ? &zPattern[1] : zPattern);
  shell_check_oom(zPat);
  for(i=0; i<ArraySize(azHelp); i++){
    if( sqlite3_strglob(zPat, azHelp[i])==0 ){
      if( zHit ) cli_printf(out, "%s\n", zHit);
      zHit = azHelp[i];
      j = i+1;
      n++;
    }
  }
  if( n ){
    if( n==1 ){
      const char *zUsage = findUsage(zPat);
      if( zUsage ){
        cli_puts(zUsage, out);
      }else{
        /* when zPattern is a prefix of exactly one command, then include
        ** the details of that command, which should begin at offset j */
        cli_printf(out, "%s\n", zHit);
        while( j<ArraySize(azHelp)-1 && azHelp[j][0]==' ' ){
          cli_printf(out, "%s\n", azHelp[j]);
          j++;
        }
      }
    }else{
      cli_printf(out, "%s\n", zHit);
    }
  }
  sqlite3_free(zPat);
  if( n ) return n;

  /* Look for documented commands that contain zPattern anywhere.
  ** Show complete text of all documented commands that match. */
  zPat = sqlite3_mprintf("%%%s%%", zPattern);
  shell_check_oom(zPat);
  for(i=0; i<ArraySize(azHelp); i++){
    if( azHelp[i][0]==',' ){
      while( i<ArraySize(azHelp)-1 && azHelp[i+1][0]==' ' ) ++i;
      continue;
    }
    if( azHelp[i][0]=='.' ) j = i;
    if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
      cli_printf(out, "%s\n", azHelp[j]);
      while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]==' ' ){
        j++;
        cli_printf(out, "%s\n", azHelp[j]);
      }
      i = j;
      n++;
    }
  }
  sqlite3_free(zPat);
  return n;
}

/* Forward reference */
static int process_input(ShellState *p, const char*);

/*
** Read the content of file zName into memory obtained from sqlite3_malloc64()
** and return a pointer to the buffer. The caller is responsible for freeing
** the memory.
**
** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
** read.
**
** For convenience, a nul-terminator byte is always appended to the data read
** from the file before the buffer is returned. This byte is not included in
** the final value of (*pnByte), if applicable.
**
** NULL is returned if any error is encountered. The final value of *pnByte
** is undefined in this case.
*/
static char *readFile(const char *zName, int *pnByte){
  FILE *in = sqlite3_fopen(zName, "rb");
  long nIn;
  size_t nRead;
  char *pBuf;
  int rc;
  if( in==0 ) return 0;
  rc = fseek(in, 0, SEEK_END);
  if( rc!=0 ){
    cli_printf(stderr,"Error: '%s' not seekable\n", zName);
    fclose(in);
    return 0;
  }
  nIn = ftell(in);
  rewind(in);
  pBuf = sqlite3_malloc64( nIn+1 );
  if( pBuf==0 ){
    cli_puts("Error: out of memory\n", stderr);
    fclose(in);
    return 0;
  }
  nRead = fread(pBuf, nIn, 1, in);
  fclose(in);
  if( nRead!=1 ){
    sqlite3_free(pBuf);
    cli_printf(stderr,"Error: cannot read '%s'\n", zName);
    return 0;
  }
  pBuf[nIn] = 0;
  if( pnByte ) *pnByte = nIn;
  return pBuf;
}

#if defined(SQLITE_ENABLE_SESSION)
/*
** Close a single OpenSession object and release all of its associated
** resources.
*/
static void session_close(OpenSession *pSession){
  int i;
  sqlite3session_delete(pSession->p);
  sqlite3_free(pSession->zName);
  for(i=0; i<pSession->nFilter; i++){
    sqlite3_free(pSession->azFilter[i]);
  }
  sqlite3_free(pSession->azFilter);
  memset(pSession, 0, sizeof(OpenSession));
}
#endif

/*
** Close all OpenSession objects and release all associated resources.
*/
#if defined(SQLITE_ENABLE_SESSION)
static void session_close_all(ShellState *p, int i){
  int j;
  struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
  for(j=0; j<pAuxDb->nSession; j++){
    session_close(&pAuxDb->aSession[j]);
  }
  pAuxDb->nSession = 0;
}
#else
# define session_close_all(X,Y)
#endif

/*
** Implementation of the xFilter function for an open session.  Omit
** any tables named by ".session filter" but let all other table through.
*/
#if defined(SQLITE_ENABLE_SESSION)
static int session_filter(void *pCtx, const char *zTab){
  OpenSession *pSession = (OpenSession*)pCtx;
  int i;
  for(i=0; i<pSession->nFilter; i++){
    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
  }
  return 1;
}
#endif

/*
** Return the size of the named file in bytes.  Or return a negative
** number if the file does not exist.
*/
static sqlite3_int64 fileSize(const char *zFile){
#if defined(_WIN32) || defined(WIN32)
  struct _stat64 x;
  if( _stat64(zFile, &x)!=0 ) return -1;
  return (sqlite3_int64)x.st_size;
#else
  struct stat x;
  if( stat(zFile, &x)!=0 ) return -1;
  return (sqlite3_int64)x.st_size;
#endif
}

/*
** Return true if zFile is an SQLite database.
**
** Algorithm:
**    * If the file does not exist -> return false
**    * If the size of the file is not a multiple of 512 -> return false
**    * If sqlite3_open() fails -> return false
**    * if sqlite3_prepare() or sqlite3_step() fails -> return false
**    * Otherwise -> return true
*/
static int isDatabaseFile(const char *zFile, int openFlags){
  sqlite3 *db = 0;
  sqlite3_stmt *pStmt = 0;
  int rc;
  sqlite3_int64 sz = fileSize(zFile);
  if( sz<512 || (sz%512)!=0 ) return 0;
  if( sqlite3_open_v2(zFile, &db, openFlags, 0)==SQLITE_OK
   && sqlite3_prepare_v2(db,"SELECT count(*) FROM sqlite_schema",-1,&pStmt,0)
           ==SQLITE_OK
   && sqlite3_step(pStmt)==SQLITE_ROW
  ){
    rc = 1;
  }else{
    rc = 0;
  }
  sqlite3_finalize(pStmt);
  sqlite3_close(db);
  return rc;
}

/*
** Try to deduce the type of file for zName based on its content.  Return
** one of the SHELL_OPEN_* constants.
**
** If the file does not exist or is empty but its name looks like a ZIP
** archive and the dfltZip flag is true, then assume it is a ZIP archive.
** Otherwise, assume an ordinary database regardless of the filename if
** the type cannot be determined from content.
*/
int deduceDatabaseType(const char *zName, int dfltZip, int openFlags){
  FILE *f;
  size_t n;
  int rc = SHELL_OPEN_UNSPEC;
  char zBuf[100];
  if( access(zName,0)!=0 ) goto database_type_by_name;
  if( isDatabaseFile(zName, openFlags) ){
    rc = SHELL_OPEN_NORMAL;
  }
  if( rc==SHELL_OPEN_NORMAL ) return SHELL_OPEN_NORMAL;
  f = sqlite3_fopen(zName, "rb");
  if( f==0 ) goto database_type_by_name;
  n = fread(zBuf, 16, 1, f);
  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
    fclose(f);
    return SHELL_OPEN_NORMAL;
  }
  fseek(f, -25, SEEK_END);
  n = fread(zBuf, 25, 1, f);
  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
    rc = SHELL_OPEN_APPENDVFS;
  }else{
    fseek(f, -22, SEEK_END);
    n = fread(zBuf, 22, 1, f);
    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
       && zBuf[3]==0x06 ){
      rc = SHELL_OPEN_ZIPFILE;
    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
      rc = SHELL_OPEN_ZIPFILE;
    }
  }
  fclose(f);
  return rc;

database_type_by_name:
  if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
    rc = SHELL_OPEN_ZIPFILE;
  }else{
    rc = SHELL_OPEN_NORMAL;
  }
  return rc;
}

/*
** If the text in z[] is the name of a readable file and that file appears
** to contain SQL text and/or dot-commands, then return true.  If z[] is
** not a file, or if the file is unreadable, or if the file is a database
** or anything else that is not SQL text and dot-commands, then return false.
**
** If the bLeaveUninit flag is set, then be sure to leave SQLite in an
** uninitialized state.  This means invoking sqlite3_shutdown() after any
** SQLite API is used.
**
** Some amount of guesswork is involved in this decision.
*/
static int isScriptFile(const char *z, int bLeaveUninit){
  sqlite3_int64 sz = fileSize(z);
  if( sz<=0 ) return 0;
  if( (sz%512)==0 ){
    int rc;
    sqlite3_initialize();
    rc = isDatabaseFile(z, SQLITE_OPEN_READONLY);
    if( bLeaveUninit ){
      sqlite3_shutdown();
    }
    if( rc ) return 0;  /* Is a database */
  }
  if( sqlite3_strlike("%.sql",z,0)==0 ) return 1;
  if( sqlite3_strlike("%.txt",z,0)==0 ) return 1;
  return 0;
}

#ifndef SQLITE_OMIT_DESERIALIZE
/*
** Reconstruct an in-memory database using the output from the "dbtotxt"
** program.  Read content from the file in p->aAuxDb[].zDbFilename.
** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
*/
static unsigned char *readHexDb(ShellState *p, int *pnData){
  unsigned char *a = 0;
  i64 nLine;
  int n = 0;                      /* Size of db per first line of hex dump */
  i64 sz = 0;                     /* n rounded up to nearest page boundary */
  int pgsz = 0;
  i64 iOffset = 0;
  int rc;
  FILE *in;
  const char *zDbFilename = p->pAuxDb->zDbFilename;
  unsigned int x[16];
  char zLine[1000];
  if( zDbFilename ){
    in = sqlite3_fopen(zDbFilename, "r");
    if( in==0 ){
      cli_printf(stderr,"cannot open \"%s\" for reading\n", zDbFilename);
      return 0;
    }
    nLine = 0;
  }else{
    in = p->in;
    nLine = p->lineno;
    if( in==0 ) in = stdin;
  }
  *pnData = 0;
  nLine++;
  if( sqlite3_fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
  if( rc!=2 ) goto readHexDb_error;
  if( n<0 ) goto readHexDb_error;
  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
    cli_puts("invalid pagesize\n", stderr);
    goto readHexDb_error;
  }
  sz = ((i64)n+pgsz-1)&~(pgsz-1); /* Round up to nearest multiple of pgsz */
  a = sqlite3_malloc64( sz ? sz : 1 );
  shell_check_oom(a);
  memset(a, 0, sz);
  for(nLine++; sqlite3_fgets(zLine, sizeof(zLine), in)!=0; nLine++){
    int j = 0;                    /* Page number from "| page" line */
    int k = 0;                    /* Offset from "| page" line */
    if( nLine>=2000000000 ){
      cli_printf(stderr, "input too big\n");
      goto readHexDb_error;
    }
    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
    if( rc==2 ){
      iOffset = k;
      continue;
    }
    if( cli_strncmp(zLine, "| end ", 6)==0 ){
      break;
    }
    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
    if( rc==17 ){
      i64 iOff = iOffset+j;
      if( iOff+16<=sz && iOff>=0 ){
        int ii;
        for(ii=0; ii<16; ii++) a[iOff+ii] = x[ii]&0xff;
      }
    }
  }
  *pnData = sz;
  if( in!=p->in ){
    fclose(in);
  }else{
    p->lineno = nLine;
  }
  return a;

readHexDb_error:
  if( in!=p->in ){
    fclose(in);
  }else{
    while( sqlite3_fgets(zLine, sizeof(zLine), p->in)!=0 ){
      nLine++;
      if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
    }
    p->lineno = nLine;
  }
  sqlite3_free(a);
  cli_printf(stderr,"Error on line %lld of --hexdb input\n", nLine);
  return 0;
}
#endif /* SQLITE_OMIT_DESERIALIZE */

/*
** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
*/
static void shellUSleepFunc(
  sqlite3_context *context,
  int argcUnused,
  sqlite3_value **argv
){
  int sleep = sqlite3_value_int(argv[0]);
  (void)argcUnused;
  sqlite3_sleep(sleep/1000);
  sqlite3_result_int(context, sleep);
}

/*
** SQL function:  shell_module_schema(X)
**
** Return a fake schema for the table-valued function or eponymous virtual
** table X.
*/
static void shellModuleSchema(
  sqlite3_context *pCtx,
  int nVal,
  sqlite3_value **apVal
){
  const char *zName;
  char *zFake;
  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
  FILE *pSavedLog = p->pLog;
  UNUSED_PARAMETER(nVal);
  zName = (const char*)sqlite3_value_text(apVal[0]);

  /* Temporarily disable the ".log" when calling shellFakeSchema() because
  ** shellFakeSchema() might generate failures for some ephemeral virtual
  ** tables due to missing arguments.  Example: fts4aux.
  ** https://sqlite.org/forum/forumpost/42fe6520b803be51 */
  p->pLog = 0;
  zFake = zName? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;
  p->pLog = pSavedLog;

  if( zFake ){
    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
                        -1, sqlite3_free);
    sqlite3_free(zFake);
  }
}

/* Flags for open_db().
**
** The default behavior of open_db() is to exit(1) if the database fails to
** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
** but still returns without calling exit.
**
** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
** ZIP archive if the file does not exist or is empty and its name matches
** the *.zip pattern.
*/
#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */

/* Forward reference */
static void shellTempFilenameFunc(sqlite3_context*,int,sqlite3_value**);

/*
** Make sure the database is open.  If it is not, then open it.  If
** the database fails to open, print an error message and exit.
*/
static void open_db(ShellState *p, int openFlags){
  if( p->db==0 ){
    const char *zDbFilename = p->pAuxDb->zDbFilename;
    const char *zHOME;    /* Value of HOME environment variable */
    char *zToFree = 0;    /* Filename with ~/ prefix expansion */

    /* If the zDbFilename does not exist and begins with ~/ then replace
    ** the ~ with the home directory.
    */
    if( zDbFilename && zDbFilename[0]=='~'
#ifdef _WIN32
     && (zDbFilename[1]=='/' || zDbFilename[1]=='\\')
#else
     && zDbFilename[1]=='/'
#endif
     && access(zDbFilename,0)!=0
     && (zHOME = getenv("HOME"))!=0 && zHOME[0]!=0
    ){
      size_t nHOME = strlen(zHOME);
      size_t nFN = strlen(zDbFilename);
      zToFree = malloc(nHOME+nFN+1);
      if( zToFree ){
        memcpy(zToFree, zHOME, nHOME);
        memcpy(zToFree+nHOME, zDbFilename+1, nFN);
        zDbFilename = zToFree;
      }
    }

    if( p->openMode==SHELL_OPEN_UNSPEC ){
      if( zDbFilename==0 || zDbFilename[0]==0 ){
        p->openMode = SHELL_OPEN_NORMAL;
      }else{
        p->openMode = (u8)deduceDatabaseType(zDbFilename,
                             (openFlags & OPEN_DB_ZIPFILE)!=0, p->openFlags);
      }
    }
    if( (p->openFlags & (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE))==0 ){
      if( p->openFlags==0 ) p->openFlags = SQLITE_OPEN_CREATE;
      p->openFlags |= SQLITE_OPEN_READWRITE;
    }
    switch( p->openMode ){
      case SHELL_OPEN_APPENDVFS: {
        sqlite3_open_v2(zDbFilename, &p->db, p->openFlags, "apndvfs");
        break;
      }
      case SHELL_OPEN_HEXDB:
      case SHELL_OPEN_DESERIALIZE: {
        sqlite3_open(0, &p->db);
        break;
      }
      case SHELL_OPEN_ZIPFILE: {
        sqlite3_open(":memory:", &p->db);
        break;
      }
      case SHELL_OPEN_UNSPEC:
      case SHELL_OPEN_NORMAL: {
        sqlite3_open_v2(zDbFilename, &p->db, p->openFlags, 0);
        break;
      }
    }
    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
      cli_printf(stderr,"Error: unable to open database \"%s\": %s\n",
            zDbFilename, sqlite3_errmsg(p->db));
      if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
        cli_exit(1);
      }
      sqlite3_close(p->db);
      sqlite3_open(":memory:", &p->db);
      if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
        cli_puts("Also: unable to open substitute in-memory database.\n",
                      stderr);
        cli_exit(1);
      }else{
        cli_printf(stderr,
              "Notice: using substitute in-memory database instead of \"%s\"\n",
              zDbFilename);
      }
    }
    globalDb = p->db;
    sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0);

    /* Reflect the use or absence of --unsafe-testing invocation. */
    {
      int testmode_on = ShellHasFlag(p,SHFLG_TestingMode);
      sqlite3_db_config(p->db, SQLITE_DBCONFIG_TRUSTED_SCHEMA, testmode_on,0);
      sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, !testmode_on,0);
    }

#ifndef SQLITE_OMIT_LOAD_EXTENSION
    sqlite3_enable_load_extension(p->db, 1);
#endif
    sqlite3_sha_init(p->db, 0, 0);
    sqlite3_shathree_init(p->db, 0, 0);
    sqlite3_uint_init(p->db, 0, 0);
    sqlite3_stmtrand_init(p->db, 0, 0);
    sqlite3_decimal_init(p->db, 0, 0);
    sqlite3_base64_init(p->db, 0, 0);
    sqlite3_base85_init(p->db, 0, 0);
    sqlite3_regexp_init(p->db, 0, 0);
    sqlite3_ieee_init(p->db, 0, 0);
    sqlite3_series_init(p->db, 0, 0);
    sqlite3_diskused_init(p->db, 0, 0);
#ifndef SQLITE_SHELL_FIDDLE
    sqlite3_fileio_init(p->db, 0, 0);
    sqlite3_completion_init(p->db, 0, 0);
#endif
#ifdef SQLITE_HAVE_ZLIB
    if( !p->bSafeModePersist ){
      sqlite3_zipfile_init(p->db, 0, 0);
      sqlite3_sqlar_init(p->db, 0, 0);
    }
#endif
#ifdef SQLITE_SHELL_EXTFUNCS
    /* Create a preprocessing mechanism for extensions to make
     * their own provisions for being built into the shell.
     * This is a short-span macro. See further below for usage.
     */
#define SHELL_SUB_MACRO(base, variant) base ## _ ## variant
#define SHELL_SUBMACRO(base, variant) SHELL_SUB_MACRO(base, variant)
    /* Let custom-included extensions get their ..._init() called.
     * The WHATEVER_INIT( db, pzErrorMsg, pApi ) macro should cause
     * the extension's sqlite3_*_init( db, pzErrorMsg, pApi )
     * initialization routine to be called.
     */
    {
      int irc = SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, INIT)(p->db);
    /* Let custom-included extensions expose their functionality.
     * The WHATEVER_EXPOSE( db, pzErrorMsg ) macro should cause
     * the SQL functions, virtual tables, collating sequences or
     * VFS's implemented by the extension to be registered.
     */
      if( irc==SQLITE_OK
          || irc==SQLITE_OK_LOAD_PERMANENTLY ){
        SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, EXPOSE)(p->db, 0);
      }
#undef SHELL_SUB_MACRO
#undef SHELL_SUBMACRO
    }
#endif

    sqlite3_create_function(p->db, "strtod", 1, SQLITE_UTF8, 0,
                            shellStrtod, 0, 0);
    sqlite3_create_function(p->db, "dtostr", 1, SQLITE_UTF8, 0,
                            shellDtostr, 0, 0);
    sqlite3_create_function(p->db, "dtostr", 2, SQLITE_UTF8, 0,
                            shellDtostr, 0, 0);
    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
                            shellAddSchemaName, 0, 0);
    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, p,
                            shellModuleSchema, 0, 0);
    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
                            shellPutsFunc, 0, 0);
    sqlite3_create_function(p->db, "shell_format_schema", 2, SQLITE_UTF8, p,
                            shellFormatSchema, 0, 0);
    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
                            shellUSleepFunc, 0, 0);
#ifndef SQLITE_NOHAVE_SYSTEM
    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
                            editFunc, 0, 0);
    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
                            editFunc, 0, 0);
#endif
    sqlite3_create_function(p->db, "shell_prompt_test", -1, SQLITE_UTF8,
                            p, shellExpandPrompt, 0, 0);
    sqlite3_create_function(p->db, "shell_temp_filename", 1, SQLITE_UTF8,
                            p, shellTempFilenameFunc, 0, 0);


    if( p->openMode==SHELL_OPEN_ZIPFILE ){
      char *zSql = sqlite3_mprintf(
         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
      shell_check_oom(zSql);
      sqlite3_exec(p->db, zSql, 0, 0, 0);
      sqlite3_free(zSql);
    }
#ifndef SQLITE_OMIT_DESERIALIZE
    else
    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
      int rc;
      int nData = 0;
      unsigned char *aData;
      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
        aData = (unsigned char*)readFile(zDbFilename, &nData);
      }else{
        aData = readHexDb(p, &nData);
      }
      if( aData==0 ){
        return;
      }
      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
                   SQLITE_DESERIALIZE_RESIZEABLE |
                   SQLITE_DESERIALIZE_FREEONCLOSE);
      if( rc ){
        cli_printf(stderr,"Error: sqlite3_deserialize() returns %d\n", rc);
      }
      if( p->szMax>0 ){
        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
      }
    }
#endif
    free(zToFree);
  }
  if( p->db!=0 ){
#ifndef SQLITE_OMIT_AUTHORIZATION
    if( p->bSafeModePersist ){
      sqlite3_set_authorizer(p->db, safeModeAuth, p);
    }
#endif
    sqlite3_db_config(
        p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->mode.scanstatsOn, (int*)0
    );
  }
}

/*
** Attempt to close the database connection.  Report errors.
*/
void close_db(sqlite3 *db){
  int rc = sqlite3_close(db);
  if( rc ){
    cli_printf(stderr,
        "Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
  }
}

#if (HAVE_READLINE || HAVE_EDITLINE) \
  && !defined(SQLITE_OMIT_READLINE_COMPLETION)
/*
** Readline completion callbacks
*/
static char *readline_completion_generator(const char *text, int state){
  static sqlite3_stmt *pStmt = 0;
  char *zRet;
  if( state==0 ){
    char *zSql;
    sqlite3_finalize(pStmt);
    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
                           "  FROM completion(%Q) ORDER BY 1", text);
    shell_check_oom(zSql);
    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
    sqlite3_free(zSql);
  }
  if( sqlite3_step(pStmt)==SQLITE_ROW ){
    const char *z = (const char*)sqlite3_column_text(pStmt,0);
    zRet = z ? strdup(z) : 0;
  }else{
    sqlite3_finalize(pStmt);
    pStmt = 0;
    zRet = 0;
  }
  return zRet;
}
static char **readline_completion(const char *zText, int iStart, int iEnd){
  (void)iStart;
  (void)iEnd;
  rl_attempted_completion_over = 1;
  return rl_completion_matches(zText, readline_completion_generator);
}

#elif HAVE_LINENOISE
/*
** Linenoise completion callback. Note that the 3rd argument is from
** the "msteveb" version of linenoise, not the "antirez" version.
*/
static void linenoise_completion(
  const char *zLine,
  linenoiseCompletions *lc
#if HAVE_LINENOISE==2
  ,void *pUserData
#endif
){
  i64 nLine = strlen(zLine);
  i64 i, iStart;
  sqlite3_stmt *pStmt = 0;
  char *zSql;
  char zBuf[1000];

#if HAVE_LINENOISE==2
  UNUSED_PARAMETER(pUserData);
#endif
  if( nLine>(i64)sizeof(zBuf)-30 ) return;
  if( zLine[0]=='.' || zLine[0]=='#') return;
  for(i=nLine-1; i>=0 && (IsAlnum(zLine[i]) || zLine[i]=='_'); i--){}
  if( i==nLine-1 ) return;
  iStart = i+1;
  memcpy(zBuf, zLine, iStart);
  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
                         "  FROM completion(%Q,%Q) ORDER BY 1",
                         &zLine[iStart], zLine);
  shell_check_oom(zSql);
  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
  sqlite3_free(zSql);
  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
  while( sqlite3_step(pStmt)==SQLITE_ROW ){
    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
    int nCompletion = sqlite3_column_bytes(pStmt, 0);
    if( iStart+nCompletion < (i64)sizeof(zBuf)-1 && zCompletion ){
      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
      linenoiseAddCompletion(lc, zBuf);
    }
  }
  sqlite3_finalize(pStmt);
}
#endif

/*
** Do C-language style dequoting.
**
**    \a    -> alarm
**    \b    -> backspace
**    \t    -> tab
**    \n    -> newline
**    \v    -> vertical tab
**    \f    -> form feed
**    \r    -> carriage return
**    \s    -> space
**    \"    -> "
**    \'    -> '
**    \\    -> backslash
**    \NNN  -> ascii character NNN in octal
**    \xHH  -> ascii character HH in hexadecimal
*/
static void resolve_backslashes(char *z){
  int i, j;
  char c;
  while( *z && *z!='\\' ) z++;
  for(i=j=0; (c = z[i])!=0; i++, j++){
    if( c=='\\' && z[i+1]!=0 ){
      c = z[++i];
      if( c=='a' ){
        c = '\a';
      }else if( c=='b' ){
        c = '\b';
      }else if( c=='t' ){
        c = '\t';
      }else if( c=='n' ){
        c = '\n';
      }else if( c=='v' ){
        c = '\v';
      }else if( c=='f' ){
        c = '\f';
      }else if( c=='r' ){
        c = '\r';
      }else if( c=='"' ){
        c = '"';
      }else if( c=='\'' ){
        c = '\'';
      }else if( c=='\\' ){
        c = '\\';
      }else if( c=='x' ){
        int nhd = 0, hdv;
        u8 hv = 0;
        while( nhd<2 && (c=z[i+1+nhd])!=0 && (hdv=hexDigitValue(c))>=0 ){
          hv = (u8)((hv<<4)|hdv);
          ++nhd;
        }
        i += nhd;
        c = (u8)hv;
      }else if( c>='0' && c<='7' ){
        c -= '0';
        if( z[i+1]>='0' && z[i+1]<='7' ){
          i++;
          c = (c<<3) + z[i] - '0';
          if( z[i+1]>='0' && z[i+1]<='7' ){
            i++;
            c = (c<<3) + z[i] - '0';
          }
        }
      }
    }
    z[j] = c;
  }
  if( j<i ) z[j] = 0;
}

/*
** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
** for TRUE and FALSE.  Return the integer value if appropriate.
*/
static int booleanValue(const char *zArg){
  int i;
  if( zArg[0]=='0' && zArg[1]=='x' ){
    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
  }else{
    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
  }
  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
    return 1;
  }
  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
    return 0;
  }
  cli_printf(stderr,
       "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", zArg);
  return 0;
}

/*
** Set or clear a shell flag according to a boolean value.
*/
static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
  if( booleanValue(zArg) ){
    ShellSetFlag(p, mFlag);
  }else{
    ShellClearFlag(p, mFlag);
  }
}

/*
** Close an output file, assuming it is not stderr or stdout
*/
static void output_file_close(FILE *f){
  if( f && f!=stdout && f!=stderr ) fclose(f);
}

/*
** Try to open an output file.   The names "stdout" and "stderr" are
** recognized and do the right thing.  NULL is returned if the output
** filename is "off".
*/
static FILE *output_file_open(ShellState *p, const char *zFile){
  FILE *f;
  if( cli_strcmp(zFile,"stdout")==0 ){
    f = stdout;
  }else if( cli_strcmp(zFile, "stderr")==0 ){
    f = stderr;
  }else if( cli_strcmp(zFile, "off")==0 || p->bSafeMode ){
    f = 0;
  }else{
    f = sqlite3_fopen(zFile, "w");
    if( f==0 ){
      cli_printf(stderr,"Error: cannot open \"%s\"\n", zFile);
    }
  }
  return f;
}

#ifndef SQLITE_OMIT_TRACE
/*
** A routine for handling output from sqlite3_trace().
*/
static int sql_trace_callback(
  unsigned mType,         /* The trace type */
  void *pArg,             /* The ShellState pointer */
  void *pP,               /* Usually a pointer to sqlite_stmt */
  void *pX                /* Auxiliary output */
){
  ShellState *p = (ShellState*)pArg;
  sqlite3_stmt *pStmt;
  const char *zSql;
  i64 nSql;
  if( p->traceOut==0 ) return 0;
  if( mType==SQLITE_TRACE_CLOSE ){
    sputz(p->traceOut, "-- closing database connection\n");
    return 0;
  }
  if( mType!=SQLITE_TRACE_ROW && pX!=0 && ((const char*)pX)[0]=='-' ){
    zSql = (const char*)pX;
  }else{
    pStmt = (sqlite3_stmt*)pP;
    switch( p->eTraceType ){
      case SHELL_TRACE_EXPANDED: {
        zSql = sqlite3_expanded_sql(pStmt);
        break;
      }
#ifdef SQLITE_ENABLE_NORMALIZE
      case SHELL_TRACE_NORMALIZED: {
        zSql = sqlite3_normalized_sql(pStmt);
        break;
      }
#endif
      default: {
        zSql = sqlite3_sql(pStmt);
        break;
      }
    }
  }
  if( zSql==0 ) return 0;
  nSql = strlen(zSql);
  if( nSql>1000000000 ) nSql = 1000000000;
  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
  switch( mType ){
    case SQLITE_TRACE_ROW:
    case SQLITE_TRACE_STMT: {
      cli_printf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
      break;
    }
    case SQLITE_TRACE_PROFILE: {
      sqlite3_int64 nNanosec = pX ? *(sqlite3_int64*)pX : 0;
      cli_printf(p->traceOut,
                      "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
      break;
    }
  }
  return 0;
}
#endif

/*
** A no-op routine that runs with the ".breakpoint" doc-command.  This is
** a useful spot to set a debugger breakpoint.
**
** This routine does not do anything practical.  The code are there simply
** to prevent the compiler from optimizing this routine out.
*/
static void test_breakpoint(void){
  static unsigned int nCall = 0;
  if( (nCall++)==0xffffffff ) printf("Many .breakpoints have run\n");
}

/*
** An object used to read a CSV and other files for import.
*/
typedef struct ImportCtx ImportCtx;
struct ImportCtx {
  const char *zFile;  /* Name of the input file */
  FILE *in;           /* Read the CSV text from this input stream */
  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
  char *zIn;          /* Input text */
  char *z;            /* Accumulated text for a field */
  i64 nUsed;          /* Bytes of zIn[] used so far */
  i64 n;              /* Number of bytes in z */
  i64 nAlloc;         /* Space allocated for z[] */
  int nLine;          /* Current line number */
  int nRow;           /* Number of rows imported */
  int nErr;           /* Number of errors encountered */
  int bNotFirst;      /* True if one or more bytes already read */
  int cTerm;          /* Character that terminated the most recent field */
  int cColSep;        /* The column separator character.  (Usually ",") */
  int cRowSep;        /* The row separator character.  (Usually "\n") */
  int cQEscape;       /* Escape character with "...".  0 for none */
  int cUQEscape;      /* Escape character not with "...".  0 for none */
};

/* Clean up resourced used by an ImportCtx */
static void import_cleanup(ImportCtx *p){
  if( p->in!=0 && p->xCloser!=0 ){
    p->xCloser(p->in);
    p->in = 0;
  }
  sqlite3_free(p->z);
  p->z = 0;
  if( p->zIn ){
    sqlite3_free(p->zIn);
    p->zIn = 0;
  }
}

/* Read a single character of the .import input text.  Return EOF
** at end-of-file.
*/
static int import_getc(ImportCtx *p){
  if( p->in ){
    return fgetc(p->in);
  }else if( p->zIn && p->zIn[p->nUsed]!=0 ){
    return p->zIn[p->nUsed++];
  }else{
    return EOF;
  }
}

/* Append a single byte to the field value begin constructed
** in the p->z[] buffer
*/
static void import_append_char(ImportCtx *p, int c){
  if( p->n+1>=p->nAlloc ){
    p->nAlloc += p->nAlloc + 100;
    p->z = sqlite3_realloc64(p->z, p->nAlloc);
    shell_check_oom(p->z);
  }
  p->z[p->n++] = (char)c;
}

/* Read a single field of CSV text.  Compatible with rfc4180 and extended
** with the option of having a separator other than ",".
**
**   +  Input comes from p->in.
**   +  Store results in p->z of length p->n.  Space to hold p->z comes
**      from sqlite3_malloc64().
**   +  Use p->cColSep as the column separator.  The default is ",".
**   +  Use p->cRowSep as the row separator.  The default is "\n".
**   +  Keep track of the line number in p->nLine.
**   +  Store the character that terminates the field in p->cTerm.  Store
**      EOF on end-of-file.
**   +  Report syntax errors on stderr
*/
static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
  int c;
  int cSep = (u8)p->cColSep;
  int rSep = (u8)p->cRowSep;
  p->n = 0;
  c = import_getc(p);
  if( c==EOF || seenInterrupt ){
    p->cTerm = EOF;
    return 0;
  }
  if( c=='"' ){
    int pc, ppc;
    int startLine = p->nLine;
    int cQuote = c;
    int cEsc = (u8)p->cQEscape;
    pc = ppc = 0;
    while( 1 ){
      c = import_getc(p);
      if( c==rSep ) p->nLine++;
      if( c==cEsc && cEsc!=0 ){
        c = import_getc(p);
        import_append_char(p, c);
        ppc = pc = 0;
        continue;
      }
      if( c==cQuote ){
        if( pc==cQuote ){
          pc = 0;
          continue;
        }
      }
      if( (c==cSep && pc==cQuote)
       || (c==rSep && pc==cQuote)
       || (c==rSep && pc=='\r' && ppc==cQuote)
       || (c==EOF && pc==cQuote)
      ){
        do{ p->n--; }while( p->z[p->n]!=cQuote );
        p->cTerm = c;
        break;
      }
      if( pc==cQuote && c!='\r' ){
        cli_printf(stderr,"%s:%d: unescaped %c character\n", 
                   p->zFile, p->nLine, cQuote);
      }
      if( c==EOF ){
        cli_printf(stderr,"%s:%d: unterminated %c-quoted field\n",
              p->zFile, startLine, cQuote);
        p->cTerm = c;
        break;
      }
      import_append_char(p, c);
      ppc = pc;
      pc = c;
    }
  }else{
    /* If this is the first field being parsed and it begins with the
    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
    int cEsc = p->cUQEscape;
    if( (c&0xff)==0xef && p->bNotFirst==0 ){
      import_append_char(p, c);
      c = import_getc(p);
      if( (c&0xff)==0xbb ){
        import_append_char(p, c);
        c = import_getc(p);
        if( (c&0xff)==0xbf ){
          p->bNotFirst = 1;
          p->n = 0;
          return csv_read_one_field(p);
        }
      }
    }
    while( c!=EOF && c!=cSep && c!=rSep ){
      if( c==cEsc && cEsc!=0 ) c = import_getc(p);
      import_append_char(p, c);
      c = import_getc(p);
    }
    if( c==rSep ){
      p->nLine++;
      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
    }
    p->cTerm = c;
  }
  if( p->z ) p->z[p->n] = 0;
  p->bNotFirst = 1;
  return p->z;
}

/* Read a single field of ASCII delimited text.
**
**   +  Input comes from p->in.
**   +  Store results in p->z of length p->n.  Space to hold p->z comes
**      from sqlite3_malloc64().
**   +  Use p->cColSep as the column separator.  The default is "\x1F".
**   +  Use p->cRowSep as the row separator.  The default is "\x1E".
**   +  Keep track of the row number in p->nLine.
**   +  Store the character that terminates the field in p->cTerm.  Store
**      EOF on end-of-file.
**   +  Report syntax errors on stderr
*/
static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
  int c;
  int cSep = (u8)p->cColSep;
  int rSep = (u8)p->cRowSep;
  p->n = 0;
  c = import_getc(p);
  if( c==EOF || seenInterrupt ){
    p->cTerm = EOF;
    return 0;
  }
  while( c!=EOF && c!=cSep && c!=rSep ){
    import_append_char(p, c);
    c = import_getc(p);
  }
  if( c==rSep ){
    p->nLine++;
  }
  p->cTerm = c;
  if( p->z ) p->z[p->n] = 0;
  return p->z;
}

/*
** Try to transfer data for table zTable.  If an error is seen while
** moving forward, try to go backwards.  The backwards movement won't
** work for WITHOUT ROWID tables.
*/
static void tryToCloneData(
  ShellState *p,
  sqlite3 *newDb,
  const char *zTable
){
  sqlite3_stmt *pQuery = 0;
  sqlite3_stmt *pInsert = 0;
  char *zQuery = 0;
  char *zInsert = 0;
  int rc;
  int i, j, n;
  int nTable = strlen30(zTable);
  int k = 0;
  int cnt = 0;
  const int spinRate = 10000;

  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
  shell_check_oom(zQuery);
  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
  if( rc ){
    cli_printf(stderr,"Error %d: %s on [%s]\n",
          sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
    goto end_data_xfer;
  }
  n = sqlite3_column_count(pQuery);
  zInsert = sqlite3_malloc64(200 + nTable + n*3);
  shell_check_oom(zInsert);
  sqlite3_snprintf(200+nTable,zInsert,
                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
  i = strlen30(zInsert);
  for(j=1; j<n; j++){
    memcpy(zInsert+i, ",?", 2);
    i += 2;
  }
  memcpy(zInsert+i, ");", 3);
  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
  if( rc ){
    cli_printf(stderr,"Error %d: %s on [%s]\n",
          sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), zInsert);
    goto end_data_xfer;
  }
  for(k=0; k<2; k++){
    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
      for(i=0; i<n; i++){
        switch( sqlite3_column_type(pQuery, i) ){
          case SQLITE_NULL: {
            sqlite3_bind_null(pInsert, i+1);
            break;
          }
          case SQLITE_INTEGER: {
            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
            break;
          }
          case SQLITE_FLOAT: {
            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
            break;
          }
          case SQLITE_TEXT: {
            sqlite3_bind_text(pInsert, i+1,
                             (const char*)sqlite3_column_text(pQuery,i),
                             -1, SQLITE_STATIC);
            break;
          }
          case SQLITE_BLOB: {
            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
                                            sqlite3_column_bytes(pQuery,i),
                                            SQLITE_STATIC);
            break;
          }
        }
      } /* End for */
      rc = sqlite3_step(pInsert);
      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
        cli_printf(stderr,"Error %d: %s\n",
              sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb));
      }
      sqlite3_reset(pInsert);
      cnt++;
      if( (cnt%spinRate)==0 ){
        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
        fflush(stdout);
      }
    } /* End while */
    if( rc==SQLITE_DONE ) break;
    sqlite3_finalize(pQuery);
    sqlite3_free(zQuery);
    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
                             zTable);
    shell_check_oom(zQuery);
    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
    if( rc ){
      cli_printf(stderr,"Warning: cannot step \"%s\" backwards", zTable);
      break;
    }
  } /* End for(k=0...) */

end_data_xfer:
  sqlite3_finalize(pQuery);
  sqlite3_finalize(pInsert);
  sqlite3_free(zQuery);
  sqlite3_free(zInsert);
}


/*
** Try to transfer all rows of the schema that match zWhere.  For
** each row, invoke xForEach() on the object defined by that row.
** If an error is encountered while moving forward through the
** sqlite_schema table, try again moving backwards.
*/
static void tryToCloneSchema(
  ShellState *p,
  sqlite3 *newDb,
  const char *zWhere,
  void (*xForEach)(ShellState*,sqlite3*,const char*)
){
  sqlite3_stmt *pQuery = 0;
  char *zQuery = 0;
  int rc;
  const unsigned char *zName;
  const unsigned char *zSql;
  char *zErrMsg = 0;

  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
                           " WHERE %s ORDER BY rowid ASC", zWhere);
  shell_check_oom(zQuery);
  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
  if( rc ){
    cli_printf(stderr,
          "Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db),
          sqlite3_errmsg(p->db), zQuery);
    goto end_schema_xfer;
  }
  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
    zName = sqlite3_column_text(pQuery, 0);
    zSql = sqlite3_column_text(pQuery, 1);
    if( zName==0 || zSql==0 ) continue;
    if( sqlite3_stricmp((char*)zName, "sqlite_sequence")!=0 ){
      cli_printf(stdout, "%s... ", zName); fflush(stdout);
      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
      if( zErrMsg ){
        cli_printf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
        sqlite3_free(zErrMsg);
        zErrMsg = 0;
      }
    }
    if( xForEach ){
      xForEach(p, newDb, (const char*)zName);
    }
    sputz(stdout, "done\n");
  }
  if( rc!=SQLITE_DONE ){
    sqlite3_finalize(pQuery);
    sqlite3_free(zQuery);
    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
                             " WHERE %s ORDER BY rowid DESC", zWhere);
    shell_check_oom(zQuery);
    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
    if( rc ){
      cli_printf(stderr,"Error: (%d) %s on [%s]\n",
            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
      goto end_schema_xfer;
    }
    while( sqlite3_step(pQuery)==SQLITE_ROW ){
      zName = sqlite3_column_text(pQuery, 0);
      zSql = sqlite3_column_text(pQuery, 1);
      if( zName==0 || zSql==0 ) continue;
      if( sqlite3_stricmp((char*)zName, "sqlite_sequence")==0 ) continue;
      cli_printf(stdout, "%s... ", zName); fflush(stdout);
      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
      if( zErrMsg ){
        cli_printf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
        sqlite3_free(zErrMsg);
        zErrMsg = 0;
      }
      if( xForEach ){
        xForEach(p, newDb, (const char*)zName);
      }
      sputz(stdout, "done\n");
    }
  }
end_schema_xfer:
  sqlite3_finalize(pQuery);
  sqlite3_free(zQuery);
}

/*
** Open a new database file named "zNewDb".  Try to recover as much information
** as possible out of the main database (which might be corrupt) and write it
** into zNewDb.
*/
static void tryToClone(ShellState *p, const char *zNewDb){
  int rc;
  sqlite3 *newDb = 0;
  if( access(zNewDb,0)==0 ){
    cli_printf(stderr,"File \"%s\" already exists.\n", zNewDb);
    return;
  }
  rc = sqlite3_open(zNewDb, &newDb);
  if( rc ){
    cli_printf(stderr,
        "Cannot create output database: %s\n", sqlite3_errmsg(newDb));
  }else{
    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
    tryToCloneSchema(p, newDb, "type!='table'", 0);
    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
  }
  close_db(newDb);
}

#ifndef SQLITE_SHELL_FIDDLE
/*
** Change the output stream (file or pipe or console) to something else.
*/
static void output_redir(ShellState *p, FILE *pfNew){
  if( p->out != stdout ){
    cli_puts("Output already redirected.\n", stderr);
  }else{
    p->out = pfNew;
    setCrlfMode(p);
    if( p->mode.eMode==MODE_Www ){
      cli_puts(
        "<!DOCTYPE html>\n"
        "<HTML><BODY><PRE>\n",
        p->out
      );
    }
  }
}

/*
** Change the output file back to stdout.
**
** If the p->doXdgOpen flag is set, that means the output was being
** redirected to a temporary file named by p->zTempFile.  In that case,
** launch start/open/xdg-open on that temporary file.
*/
static void output_reset(ShellState *p){
  if( p->outfile[0]=='|' ){
#ifndef SQLITE_OMIT_POPEN
    pclose(p->out);
#endif
  }else{
    if( p->mode.eMode==MODE_Www ){
      cli_puts("</PRE></BODY></HTML>\n", p->out);
    }
    output_file_close(p->out);
#ifndef SQLITE_NOHAVE_SYSTEM
    if( p->doXdgOpen ){
      const char *zXdgOpenCmd =
#if defined(_WIN32)
      "start";
#elif defined(__APPLE__)
      "open";
#else
      "xdg-open";
#endif
      char *zCmd;
      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
      if( system(zCmd) ){
        cli_printf(stderr,"Failed: [%s]\n", zCmd);
      }else{
        /* Give the start/open/xdg-open command some time to get
        ** going before we continue and potentially delete the
        ** p->zTempFile data file out from under it */
        p->aUnlink = realloc(p->aUnlink, sizeof(Unlink)*(1+p->nUnlink));
        shell_check_oom(p->aUnlink);
        p->aUnlink[p->nUnlink].tm = timeOfDay()+10000;
        p->aUnlink[p->nUnlink].zFN = p->zTempFile;
        p->nUnlink++;
        p->zTempFile = 0;
      }
      sqlite3_free(zCmd);
      modePop(p);
      p->doXdgOpen = 0;
    }
#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
  }
  p->outfile[0] = 0;
  p->out = stdout;
  setCrlfMode(p);
  if( cli_output_capture ){
    sqlite3_str_free(cli_output_capture);
    cli_output_capture = 0;
  }
}
#else
# define output_redir(SS,pfO)
# define output_reset(SS)
#endif

/*
** Run an SQL command and return the single integer result.
*/
static int db_int(sqlite3 *db, const char *zSql, ...){
  sqlite3_stmt *pStmt;
  int res = 0;
  char *z;
  va_list ap;
  va_start(ap, zSql);
  z = sqlite3_vmprintf(zSql, ap);
  va_end(ap);
  sqlite3_prepare_v2(db, z, -1, &pStmt, 0);
  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
    res = sqlite3_column_int(pStmt,0);
  }
  sqlite3_finalize(pStmt);
  sqlite3_free(z);
  return res;
}

#if SQLITE_SHELL_HAVE_RECOVER
/*
** Convert a 2-byte or 4-byte big-endian integer into a native integer
*/
static unsigned int get2byteInt(unsigned char *a){
  return ((unsigned int)a[0]<<8) + (unsigned int)a[1];
}
static unsigned int get4byteInt(unsigned char *a){
  return ((unsigned int)a[0]<<24)
       + ((unsigned int)a[1]<<16)
       + ((unsigned int)a[2]<<8)
       + (unsigned int)a[3];
}

/*
** Implementation of the ".dbinfo" command.
**
** Return 1 on error, 2 to exit, and 0 otherwise.
*/
static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
  static const struct { const char *zName; int ofst; } aField[] = {
     { "file change counter:",  24  },
     { "database page count:",  28  },
     { "freelist page count:",  36  },
     { "schema cookie:",        40  },
     { "schema format:",        44  },
     { "default cache size:",   48  },
     { "autovacuum top root:",  52  },
     { "incremental vacuum:",   64  },
     { "text encoding:",        56  },
     { "user version:",         60  },
     { "application id:",       68  },
     { "software version:",     96  },
  };
  static const struct { const char *zName; const char *zSql; } aQuery[] = {
     { "number of tables:",
       "SELECT count(*) FROM %s WHERE type='table'" },
     { "number of indexes:",
       "SELECT count(*) FROM %s WHERE type='index'" },
     { "number of triggers:",
       "SELECT count(*) FROM %s WHERE type='trigger'" },
     { "number of views:",
       "SELECT count(*) FROM %s WHERE type='view'" },
     { "schema size:",
       "SELECT total(length(sql)) FROM %s" },
  };
  int i, rc;
  unsigned iDataVersion;
  char *zSchemaTab;
  char *zDb = nArg>=2 ? azArg[1] : "main";
  sqlite3_stmt *pStmt = 0;
  unsigned char aHdr[100];
  open_db(p, 0);
  if( p->db==0 ) return 1;
  rc = sqlite3_prepare_v2(p->db,
             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
             -1, &pStmt, 0);
  if( rc ){
    cli_printf(stderr,"error: %s\n", sqlite3_errmsg(p->db));
    sqlite3_finalize(pStmt);
    return 1;
  }
  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
  if( sqlite3_step(pStmt)==SQLITE_ROW
   && sqlite3_column_bytes(pStmt,0)>100
  ){
    const u8 *pb = sqlite3_column_blob(pStmt,0);
    shell_check_oom(pb);
    memcpy(aHdr, pb, 100);
    sqlite3_finalize(pStmt);
  }else{
    cli_puts("unable to read database header\n", stderr);
    sqlite3_finalize(pStmt);
    return 1;
  }
  i = get2byteInt(aHdr+16);
  if( i==1 ) i = 65536;
  cli_printf(p->out, "%-20s %d\n", "database page size:", i);
  cli_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
  cli_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
  cli_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
  for(i=0; i<ArraySize(aField); i++){
    int ofst = aField[i].ofst;
    unsigned int val = get4byteInt(aHdr + ofst);
    cli_printf(p->out, "%-20s %u", aField[i].zName, val);
    switch( ofst ){
      case 56: {
        if( val==1 ) cli_puts(" (utf8)", p->out);
        if( val==2 ) cli_puts(" (utf16le)", p->out);
        if( val==3 ) cli_puts(" (utf16be)", p->out);
      }
    }
    cli_puts("\n", p->out);
  }
  if( zDb==0 ){
    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
  }else if( cli_strcmp(zDb,"temp")==0 ){
    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
  }else{
    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
  }
  for(i=0; i<ArraySize(aQuery); i++){
    int val = db_int(p->db, aQuery[i].zSql, zSchemaTab);
    cli_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
  }
  sqlite3_free(zSchemaTab);
  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
  cli_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
  return 0;
}
#endif /* SQLITE_SHELL_HAVE_RECOVER */

/*
** Implementation of the ".dbtotxt" command.
**
** Return 1 on error, 2 to exit, and 0 otherwise.
*/
static int shell_dbtotxt_command(ShellState *p, int nArg, char **azArg){
  sqlite3_stmt *pStmt = 0;
  sqlite3_int64 nPage = 0;
  int pgSz = 0;
  const char *zTail;
  char *zName = 0;
  int rc, i, j;
  unsigned char bShow[256];   /* Characters ok to display */

  UNUSED_PARAMETER(nArg);
  UNUSED_PARAMETER(azArg);
  memset(bShow, '.', sizeof(bShow));
  for(i=' '; i<='~'; i++){
    if( i!='{' && i!='}' && i!='"' && i!='\\' ) bShow[i] = (unsigned char)i;
  }
  rc = sqlite3_prepare_v2(p->db, "PRAGMA page_size", -1, &pStmt, 0);
  if( rc ) goto dbtotxt_error;
  rc = 0;
  if( sqlite3_step(pStmt)!=SQLITE_ROW ) goto dbtotxt_error;
  pgSz = sqlite3_column_int(pStmt, 0);
  sqlite3_finalize(pStmt);
  pStmt = 0;
  if( pgSz<512 || pgSz>65536 || (pgSz&(pgSz-1))!=0 ) goto dbtotxt_error;
  rc = sqlite3_prepare_v2(p->db, "PRAGMA page_count", -1, &pStmt, 0);
  if( rc ) goto dbtotxt_error;
  rc = 0;
  if( sqlite3_step(pStmt)!=SQLITE_ROW ) goto dbtotxt_error;
  nPage = sqlite3_column_int64(pStmt, 0);
  sqlite3_finalize(pStmt);
  pStmt = 0;
  if( nPage<1 ) goto dbtotxt_error;
  rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
  if( rc ) goto dbtotxt_error;
  if( sqlite3_step(pStmt)!=SQLITE_ROW ){
    zTail = "unk.db";
  }else{
    const char *zFilename = (const char*)sqlite3_column_text(pStmt, 2);
    if( zFilename==0 || zFilename[0]==0 ) zFilename = "unk.db";
    zTail = strrchr(zFilename, '/');
#if defined(_WIN32)
    if( zTail==0 ) zTail = strrchr(zFilename, '\\');
#endif
    if( zTail==0 ){
      zTail = zFilename;
    }else if( zTail[1]!=0 ){
      zTail++;
    }
  }
  zName = strdup(zTail);
  shell_check_oom(zName);
  cli_printf(p->out, "| size %lld pagesize %d filename %s\n",
                  nPage*pgSz, pgSz, zName);
  sqlite3_finalize(pStmt);
  pStmt = 0;
  rc = sqlite3_prepare_v2(p->db,
           "SELECT pgno, data FROM sqlite_dbpage ORDER BY pgno", -1, &pStmt, 0);
  if( rc ) goto dbtotxt_error;
  while( sqlite3_step(pStmt)==SQLITE_ROW ){
    sqlite3_int64 pgno = sqlite3_column_int64(pStmt, 0);
    const u8 *aData = sqlite3_column_blob(pStmt, 1);
    int seenPageLabel = 0;
    for(i=0; i<pgSz; i+=16){
      const u8 *aLine = aData+i;
      for(j=0; j<16 && aLine[j]==0; j++){}
      if( j==16 ) continue;
      if( !seenPageLabel ){
        cli_printf(p->out, "| page %lld offset %lld\n",pgno,(pgno-1)*pgSz);
        seenPageLabel = 1;
      }
      cli_printf(p->out, "|  %5d:", i);
      for(j=0; j<16; j++) cli_printf(p->out, " %02x", aLine[j]);
      cli_printf(p->out, "   ");
      for(j=0; j<16; j++){
        unsigned char c = (unsigned char)aLine[j];
        cli_printf(p->out, "%c", bShow[c]);
      }
      cli_printf(p->out, "\n");
    }
  }
  sqlite3_finalize(pStmt);
  cli_printf(p->out, "| end %s\n", zName);
  free(zName);
  return 0;

dbtotxt_error:
  if( rc ){
    cli_printf(stderr, "ERROR: %s\n", sqlite3_errmsg(p->db));
  }
  sqlite3_finalize(pStmt);
  free(zName);
  return 1;
}

/*
** Print the given string as an error message.
*/
static void shellEmitError(const char *zErr){
  cli_printf(stderr,"Error: %s\n", zErr);
}
/*
** Print the current sqlite3_errmsg() value to stderr and return 1.
*/
static int shellDatabaseError(sqlite3 *db){
  shellEmitError(sqlite3_errmsg(db));
  return 1;
}

/*
** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
** if they match and FALSE (0) if they do not match.
**
** Globbing rules:
**
**      '*'       Matches any sequence of zero or more characters.
**
**      '?'       Matches exactly one character.
**
**     [...]      Matches one character from the enclosed list of
**                characters.
**
**     [^...]     Matches one character not in the enclosed list.
**
**      '#'       Matches any sequence of one or more digits with an
**                optional + or - sign in front
**
**      ' '       Any span of whitespace matches any other span of
**                whitespace.
**
** Extra whitespace at the end of z[] is ignored.
*/
static int testcase_glob(const char *zGlob, const char *z){
  int c, c2;
  int invert;
  int seen;

  while( (c = (*(zGlob++)))!=0 ){
    if( IsSpace(c) ){
      if( !IsSpace(*z) ) return 0;
      while( IsSpace(*zGlob) ) zGlob++;
      while( IsSpace(*z) ) z++;
    }else if( c=='*' ){
      while( (c=(*(zGlob++))) == '*' || c=='?' ){
        if( c=='?' && (*(z++))==0 ) return 0;
      }
      if( c==0 ){
        return 1;
      }else if( c=='[' ){
        while( *z && testcase_glob(zGlob-1,z)==0 ){
          z++;
        }
        return (*z)!=0;
      }
      while( (c2 = (*(z++)))!=0 ){
        while( c2!=c ){
          c2 = *(z++);
          if( c2==0 ) return 0;
        }
        if( testcase_glob(zGlob,z) ) return 1;
      }
      return 0;
    }else if( c=='?' ){
      if( (*(z++))==0 ) return 0;
    }else if( c=='[' ){
      int prior_c = 0;
      seen = 0;
      invert = 0;
      c = *(z++);
      if( c==0 ) return 0;
      c2 = *(zGlob++);
      if( c2=='^' ){
        invert = 1;
        c2 = *(zGlob++);
      }
      if( c2==']' ){
        if( c==']' ) seen = 1;
        c2 = *(zGlob++);
      }
      while( c2 && c2!=']' ){
        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
          c2 = *(zGlob++);
          if( c>=prior_c && c<=c2 ) seen = 1;
          prior_c = 0;
        }else{
          if( c==c2 ){
            seen = 1;
          }
          prior_c = c2;
        }
        c2 = *(zGlob++);
      }
      if( c2==0 || (seen ^ invert)==0 ) return 0;
    }else if( c=='#' ){
      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
      if( !IsDigit(z[0]) ) return 0;
      z++;
      while( IsDigit(z[0]) ){ z++; }
    }else{
      if( c!=(*(z++)) ) return 0;
    }
  }
  while( IsSpace(*z) ){ z++; }
  return *z==0;
}


/*
** Compare the string as a command-line option with either one or two
** initial "-" characters.
*/
static int optionMatch(const char *zStr, const char *zOpt){
  if( zStr[0]!='-' ) return 0;
  zStr++;
  if( zStr[0]=='-' ) zStr++;
  return cli_strcmp(zStr, zOpt)==0;
}

/*
** The input zFN is guaranteed to start with "file:" and is thus a URI
** filename.  Extract the actual filename and return a pointer to that
** filename in spaced obtained from sqlite3_malloc().
**
** The caller is responsible for freeing space using sqlite3_free() when
** it has finished with the filename.
*/
static char *shellFilenameFromUri(const char *zFN){
  char *zOut;
  int i, j, d1, d2;

  assert( cli_strncmp(zFN,"file:",5)==0 );
  zOut = sqlite3_mprintf("%s", zFN+5);
  shell_check_oom(zOut);
  for(i=j=0; zOut[i]!=0 && zOut[i]!='?'; i++){
    if( zOut[i]!='%' ){
      zOut[j++] = zOut[i];
      continue;
    }
    d1 = hexDigitValue(zOut[i+1]);
    if( d1<0 ){
      zOut[j] = 0;
      break;
    }
    d2 = hexDigitValue(zOut[i+2]);
    if( d2<0 ){
      zOut[j] = 0;
      break;
    }
    zOut[j++] = d1*16 + d2;
    i += 2;
  }
  zOut[j] = 0;
  return zOut;
}

/*
** Delete a file.
**
** If unsuccessful on the first attempt and if pRetry!=NULL and pRetry[0]
** is positive, then delay for pRetry[0] milliseconds and try again.  On
** a retry, pRetry[0] is set to zero.  Unsuccessful unlinks usually only
** happen on Windows.  It is possible (in theory) for unlink() to fail
** on Unix, but it rarely happens.
**
** Return 0 on success and non-zero if unable.
*/
int shellDeleteFile(const char *zFilename, int *pRetry){
  int rc;
  for(;;){
#ifdef _WIN32
    wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
    rc = _wunlink(z);
    if( rc && _waccess(z,0)!=0 ) rc = 0;
    sqlite3_free(z);
#else
    rc = unlink(zFilename);
    if( rc && access(zFilename, 0)!=0 ) rc = 0;
#endif
    if( rc && pRetry && pRetry[0] ){
      sqlite3_sleep(pRetry[0]);
      pRetry[0] = 0;
      continue;
    }
    break;
  }
  return rc;
}

/*
** This routine does two things:
**
**    (1)  Delete (unlink) temporary files that are no longer needed.
**    (2)  Free memory used to hold the names of those temporary files.
**
** Temp filenames are stored in p->zTempName and in the p->aUnlink[]
** array.  The p->zTempFile is always subject to immediate deletion.  The
** temp files named in p->aUnlink[] are usually not deleted until after
** the timestamp stored in the p->aUnlink[].tm field, except if
** bForce is true, the p->aUnlink[] files will be deleted even if they
** have not reached their expiration, as long as a delay of at least
** nDelay milliseconds occurs first.
**
** If nDelay is non-zero and a deletion attempt is not successful,
** wait for nDelay milliseconds and try again before giving up.  Only
** a single wait occurs, even if problems are encountered with multiple
** temp files.  Only the first problem seen takes the delay.  Thus
** the total delay never exceeds nDelay milliseconds.  When nDelay
** is non-zero, memory used to hold the filename is reclaimed regardless
** of whether or not the temporary files were successfully deleted.
**
** If bForce is true, deletion is attempted on p->aUnlink[] files
** even if their time has not expired.  However, there is a pause
** of up to nDelay milliseconds before doing the deletion.
**
** When nDelay is zero and the deletion attempt fails, memory used to
** store temporary filesnames is not reclaimed.
**
** The bForce flag is used only when the process is about to exit.  When
** bForce is set, that indicates that this is our last opportunity to
** clean up temporary files.
*/
static void clearTempFile(ShellState *p, int nDelay, int bForce){
  int alwaysFree = (nDelay>0) || bForce;
  int rc = 0;
  if( p->zTempFile && (!p->doXdgOpen || bForce) ){
    rc = shellDeleteFile(p->zTempFile, &nDelay);
    if( rc==0 || alwaysFree ){
      sqlite3_free(p->zTempFile);
      p->zTempFile = 0;
    }
  }
  if( p->nUnlink ){
    unsigned int i;
    for(i=0; i<p->nUnlink; i++){
      sqlite3_int64 tmToGo = p->aUnlink[i].tm - timeOfDay();
      int doDelete =  tmToGo<=0;
      if( !doDelete && alwaysFree ){
        int tmSleep = nDelay;
        if( tmSleep > tmToGo ) tmSleep = (int)tmToGo;
        sqlite3_sleep(tmSleep);
        nDelay -= tmSleep;
        doDelete = 1;
      }
      if( doDelete ){
        char *zFN = p->aUnlink[i].zFN;
        rc = shellDeleteFile(zFN, &nDelay);
        if( rc==0 || alwaysFree ){
          sqlite3_free(p->aUnlink[i].zFN);
          p->nUnlink--;
          if( i<p->nUnlink ){
            p->aUnlink[i] = p->aUnlink[p->nUnlink];
            memset(&p->aUnlink[p->nUnlink], 0, sizeof(Unlink));
          }
          i--;
        }
      }
    }
    if( p->nUnlink==0 ){
      free(p->aUnlink);
      p->aUnlink = 0;
    }
  }  
}

/* Forward reference */
static char *find_home_dir(int clearFlag);

/*
** Create a new temp file name with the given suffix.
**
** Because the classic temp folders like /tmp are no longer
** accessible to web browsers, for security reasons, create the
** temp file in the user's home directory.
*/
static void newTempFile(ShellState *p, const char *zSuffix){
  char *zHome;          /* Home directory */
  int i;                /* Loop counter */
  sqlite3_uint64 r = 0; /* Integer with 64 bits of randomness */
  char zRand[32];       /* Text string with 160 bits of randomness */
#ifdef _WIN32
  const char cDirSep = '\\';
#else
  const char cDirSep = '/';
#endif

  for(i=0; i<31; i++){
    if( (i%12)==0 ) sqlite3_randomness(sizeof(r),&r);
    zRand[i] = "0123456789abcdefghijklmnopqrstuvwxyz"[r%36];
    r /= 36;
  }
  zRand[i] = 0;
  clearTempFile(p,10,0);
  sqlite3_free(p->zTempFile);
  p->zTempFile = 0;
  zHome = find_home_dir(0);
  p->zTempFile = sqlite3_mprintf("%s%ctemp-%s.%s",
                                 zHome,cDirSep,zRand,zSuffix);
  shell_check_oom(p->zTempFile);
}

/*
** SQL function:  shell_temp_filename(SUFFIX)
**
** Return a randomly generated temporary filename.
*/
static void shellTempFilenameFunc(
  sqlite3_context *pCtx,
  int nVal,
  sqlite3_value **apVal
){
  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
  const char *zSuffix = (const char*)sqlite3_value_text(apVal[0]);
  (void)nVal;
  if( zSuffix==0 ) zSuffix = "";
  newTempFile(p, zSuffix);
  sqlite3_result_text(pCtx, p->zTempFile, -1, SQLITE_TRANSIENT);
  sqlite3_free(p->zTempFile);
  p->zTempFile = 0;
}

/*
** The implementation of SQL scalar function fkey_collate_clause(), used
** by the ".lint fkey-indexes" command. This scalar function is always
** called with four arguments - the parent table name, the parent column name,
** the child table name and the child column name.
**
**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
**
** If either of the named tables or columns do not exist, this function
** returns an empty string. An empty string is also returned if both tables
** and columns exist but have the same default collation sequence. Or,
** if both exist but the default collation sequences are different, this
** function returns the string " COLLATE <parent-collation>", where
** <parent-collation> is the default collation sequence of the parent column.
*/
static void shellFkeyCollateClause(
  sqlite3_context *pCtx,
  int nVal,
  sqlite3_value **apVal
){
  sqlite3 *db = sqlite3_context_db_handle(pCtx);
  const char *zParent;
  const char *zParentCol;
  const char *zParentSeq;
  const char *zChild;
  const char *zChildCol;
  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
  int rc;

  assert( nVal==4 );
  zParent = (const char*)sqlite3_value_text(apVal[0]);
  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
  zChild = (const char*)sqlite3_value_text(apVal[2]);
  zChildCol = (const char*)sqlite3_value_text(apVal[3]);

  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
  rc = sqlite3_table_column_metadata(
      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
  );
  if( rc==SQLITE_OK ){
    rc = sqlite3_table_column_metadata(
        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
    );
  }

  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
    sqlite3_free(z);
  }
}


/*
** The implementation of dot-command ".lint fkey-indexes".
*/
static int lintFkeyIndexes(
  ShellState *pState,             /* Current shell tool state */
  char **azArg,                   /* Array of arguments passed to dot command */
  int nArg                        /* Number of entries in azArg[] */
){
  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
  int bVerbose = 0;               /* If -verbose is present */
  int bGroupByParent = 0;         /* If -groupbyparent is present */
  int i;                          /* To iterate through azArg[] */
  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
  int rc;                         /* Return code */
  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
  FILE *out = pState->out;        /* Send output here */

  /*
  ** This SELECT statement returns one row for each foreign key constraint
  ** in the schema of the main database. The column values are:
  **
  ** 0. The text of an SQL statement similar to:
  **
  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
  **
  **    This SELECT is similar to the one that the foreign keys implementation
  **    needs to run internally on child tables. If there is an index that can
  **    be used to optimize this query, then it can also be used by the FK
  **    implementation to optimize DELETE or UPDATE statements on the parent
  **    table.
  **
  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
  **    contains an index that can be used to optimize the query.
  **
  ** 2. Human readable text that describes the child table and columns. e.g.
  **
  **       "child_table(child_key1, child_key2)"
  **
  ** 3. Human readable text that describes the parent table and columns. e.g.
  **
  **       "parent_table(parent_key1, parent_key2)"
  **
  ** 4. A full CREATE INDEX statement for an index that could be used to
  **    optimize DELETE or UPDATE statements on the parent table. e.g.
  **
  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
  **
  ** 5. The name of the parent table.
  **
  ** These six values are used by the C logic below to generate the report.
  */
  const char *zSql =
  "SELECT "
    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
    "  || fkey_collate_clause("
    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
    ", "
    "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
    "  || group_concat('*=?', ' AND ') || ')'"
    ", "
    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
    ", "
    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
    ", "
    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
    "  || ' ON ' || quote(s.name) || '('"
    "  || group_concat(quote(f.[from]) ||"
    "        fkey_collate_clause("
    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
    "  || ');'"
    ", "
    "     f.[table] "
    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
    "GROUP BY s.name, f.id "
    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
  ;
  const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";

  for(i=2; i<nArg; i++){
    int n = strlen30(azArg[i]);
    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
      bVerbose = 1;
    }
    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
      bGroupByParent = 1;
      zIndent = "    ";
    }
    else{
      cli_printf(stderr,
           "Usage: %s %s ?-verbose? ?-groupbyparent?\n", azArg[0], azArg[1]);
      return SQLITE_ERROR;
    }
  }

  /* Register the fkey_collate_clause() SQL function */
  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
      0, shellFkeyCollateClause, 0, 0
  );


  if( rc==SQLITE_OK ){
    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
  }
  if( rc==SQLITE_OK ){
    sqlite3_bind_int(pSql, 1, bGroupByParent);
  }

  if( rc==SQLITE_OK ){
    int rc2;
    char *zPrev = 0;
    while( SQLITE_ROW==sqlite3_step(pSql) ){
      int res = -1;
      sqlite3_stmt *pExplain = 0;
      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);

      if( zEQP==0 ) continue;
      if( zGlob==0 ) continue;
      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
      if( rc!=SQLITE_OK ) break;
      if( SQLITE_ROW==sqlite3_step(pExplain) ){
        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
        res = zPlan!=0 && (  0==sqlite3_strglob(zGlob, zPlan)
                          || 0==sqlite3_strglob(zGlobIPK, zPlan));
      }
      rc = sqlite3_finalize(pExplain);
      if( rc!=SQLITE_OK ) break;

      if( res<0 ){
        cli_puts("Error: internal error", stderr);
        break;
      }else{
        if( bGroupByParent
        && (bVerbose || res==0)
        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
        ){
          cli_printf(out, "-- Parent table %s\n", zParent);
          sqlite3_free(zPrev);
          zPrev = sqlite3_mprintf("%s", zParent);
        }

        if( res==0 ){
          cli_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
        }else if( bVerbose ){
          cli_printf(out,
                "%s/* no extra indexes required for %s -> %s */\n",
                zIndent, zFrom, zTarget
          );
        }
      }
    }
    sqlite3_free(zPrev);

    if( rc!=SQLITE_OK ){
      cli_printf(stderr,"%s\n", sqlite3_errmsg(db));
    }

    rc2 = sqlite3_finalize(pSql);
    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
      rc = rc2;
      cli_printf(stderr,"%s\n", sqlite3_errmsg(db));
    }
  }else{
    cli_printf(stderr,"%s\n", sqlite3_errmsg(db));
  }

  return rc;
}

/*
** Implementation of ".lint" dot command.
*/
static int lintDotCommand(
  ShellState *pState,             /* Current shell tool state */
  char **azArg,                   /* Array of arguments passed to dot command */
  int nArg                        /* Number of entries in azArg[] */
){
  int n;
  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
  return lintFkeyIndexes(pState, azArg, nArg);

 usage:
  cli_printf(stderr,"Usage %s sub-command ?switches...?\n", azArg[0]);
  cli_printf(stderr, "Where sub-commands are:\n");
  cli_printf(stderr, "    fkey-indexes\n");
  return SQLITE_ERROR;
}

static void shellPrepare(
  sqlite3 *db,
  int *pRc,
  const char *zSql,
  sqlite3_stmt **ppStmt
){
  *ppStmt = 0;
  if( *pRc==SQLITE_OK ){
    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
    if( rc!=SQLITE_OK ){
      cli_printf(stderr,
         "sql error: %s (%d)\n", sqlite3_errmsg(db), sqlite3_errcode(db));
      *pRc = rc;
    }
  }
}

/*
** Create a prepared statement using printf-style arguments for the SQL.
*/
static void shellPreparePrintf(
  sqlite3 *db,
  int *pRc,
  sqlite3_stmt **ppStmt,
  const char *zFmt,
  ...
){
  *ppStmt = 0;
  if( *pRc==SQLITE_OK ){
    va_list ap;
    char *z;
    va_start(ap, zFmt);
    z = sqlite3_vmprintf(zFmt, ap);
    va_end(ap);
    if( z==0 ){
      *pRc = SQLITE_NOMEM;
    }else{
      shellPrepare(db, pRc, z, ppStmt);
      sqlite3_free(z);
    }
  }
}

/* 
** Finalize the prepared statement created using shellPreparePrintf().
*/
static void shellFinalize(
  int *pRc,
  sqlite3_stmt *pStmt
){
  if( pStmt ){
    sqlite3 *db = sqlite3_db_handle(pStmt);
    int rc = sqlite3_finalize(pStmt);
    if( *pRc==SQLITE_OK ){
      if( rc!=SQLITE_OK ){
        cli_printf(stderr,"SQL error: %s\n", sqlite3_errmsg(db));
      }
      *pRc = rc;
    }
  }
}

#if !defined SQLITE_OMIT_VIRTUALTABLE
/* Reset the prepared statement created using shellPreparePrintf().
**
** This routine is could be marked "static".  But it is not always used,
** depending on compile-time options.  By omitting the "static", we avoid
** nuisance compiler warnings about "defined but not used".
*/
void shellReset(
  int *pRc,
  sqlite3_stmt *pStmt
){
  int rc = sqlite3_reset(pStmt);
  if( *pRc==SQLITE_OK ){
    if( rc!=SQLITE_OK ){
      sqlite3 *db = sqlite3_db_handle(pStmt);
      cli_printf(stderr,"SQL error: %s\n", sqlite3_errmsg(db));
    }
    *pRc = rc;
  }
}
#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */

#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
/******************************************************************************
** The ".archive" or ".ar" command.
*/
/*
** Structure representing a single ".ar" command.
*/
typedef struct ArCommand ArCommand;
struct ArCommand {
  u8 eCmd;                        /* An AR_CMD_* value */
  u8 bVerbose;                    /* True if --verbose */
  u8 bZip;                        /* True if the archive is a ZIP */
  u8 bDryRun;                     /* 1 for --dry-run, 2 for --debug */
  u8 bAppend;                     /* True if --append */
  u8 bGlob;                       /* True if --glob */
  u8 fromCmdLine;                 /* Run from -A instead of .archive */
  int nArg;                       /* Number of command arguments */
  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
  const char *zFile;              /* --file argument, or NULL */
  const char *zDir;               /* --directory argument, or NULL */
  char **azArg;                   /* Array of command arguments */
  ShellState *p;                  /* Shell state */
  FILE *out;                      /* Output to this stream */
  sqlite3 *db;                    /* Database containing the archive */
};

/*
** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
*/
static int arUsage(FILE *f){
  showHelp(f,"archive");
  return SQLITE_ERROR;
}

/*
** Print an error message for the .ar command to stderr and return
** SQLITE_ERROR.
*/
static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
  va_list ap;
  char *z;
  va_start(ap, zFmt);
  z = sqlite3_vmprintf(zFmt, ap);
  va_end(ap);
  shellEmitError(z);
  if( pAr->fromCmdLine ){
    cli_puts("Use \"-A\" for more help\n", stderr);
  }else{
    cli_puts("Use \".archive --help\" for more help\n", stderr);
  }
  sqlite3_free(z);
  return SQLITE_ERROR;
}

/*
** Values for ArCommand.eCmd.
*/
#define AR_CMD_CREATE       1
#define AR_CMD_UPDATE       2
#define AR_CMD_INSERT       3
#define AR_CMD_EXTRACT      4
#define AR_CMD_LIST         5
#define AR_CMD_HELP         6
#define AR_CMD_REMOVE       7

/*
** Other (non-command) switches.
*/
#define AR_SWITCH_VERBOSE     8
#define AR_SWITCH_FILE        9
#define AR_SWITCH_DIRECTORY  10
#define AR_SWITCH_APPEND     11
#define AR_SWITCH_DRYRUN     12
#define AR_SWITCH_GLOB       13
#define AR_SWITCH_DEBUG      14

static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
  switch( eSwitch ){
    case AR_CMD_CREATE:
    case AR_CMD_EXTRACT:
    case AR_CMD_LIST:
    case AR_CMD_REMOVE:
    case AR_CMD_UPDATE:
    case AR_CMD_INSERT:
    case AR_CMD_HELP:
      if( pAr->eCmd ){
        return arErrorMsg(pAr, "multiple command options");
      }
      pAr->eCmd = eSwitch;
      break;

    case AR_SWITCH_DRYRUN:
      if( pAr->bDryRun<2 ) pAr->bDryRun = 1;
      break;
    case AR_SWITCH_DEBUG:
      pAr->bDryRun = 2;
      break;
    case AR_SWITCH_GLOB:
      pAr->bGlob = 1;
      break;
    case AR_SWITCH_VERBOSE:
      pAr->bVerbose = 1;
      break;
    case AR_SWITCH_APPEND:
      pAr->bAppend = 1;
      deliberate_fall_through; /* FALLTHRU */
    case AR_SWITCH_FILE:
      pAr->zFile = zArg;
      break;
    case AR_SWITCH_DIRECTORY:
      pAr->zDir = zArg;
      break;
  }

  return SQLITE_OK;
}

/*
** Parse the command line for an ".ar" command. The results are written into
** structure (*pAr). SQLITE_OK is returned if the command line is parsed
** successfully, otherwise an error message is written to stderr and
** SQLITE_ERROR returned.
*/
static int arParseCommand(
  char **azArg,                   /* Array of arguments passed to dot command */
  int nArg,                       /* Number of entries in azArg[] */
  ArCommand *pAr                  /* Populate this object */
){
  struct ArSwitch {
    const char *zLong;
    char cShort;
    u8 eSwitch;
    u8 bArg;
  } aSwitch[] = {
    { "create",    'c', AR_CMD_CREATE,       0 },
    { "extract",   'x', AR_CMD_EXTRACT,      0 },
    { "insert",    'i', AR_CMD_INSERT,       0 },
    { "list",      't', AR_CMD_LIST,         0 },
    { "remove",    'r', AR_CMD_REMOVE,       0 },
    { "update",    'u', AR_CMD_UPDATE,       0 },
    { "help",      'h', AR_CMD_HELP,         0 },
    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
    { "file",      'f', AR_SWITCH_FILE,      1 },
    { "append",    'a', AR_SWITCH_APPEND,    1 },
    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
    { "debug",      0,  AR_SWITCH_DEBUG,     0 },
    { "glob",      'g', AR_SWITCH_GLOB,      0 },
  };
  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
  struct ArSwitch *pEnd = &aSwitch[nSwitch];

  if( nArg<=1 ){
    cli_printf(stderr, "Wrong number of arguments.  Usage:\n");
    return arUsage(stderr);
  }else{
    char *z = azArg[1];
    if( z[0]!='-' ){
      /* Traditional style [tar] invocation */
      int i;
      int iArg = 2;
      for(i=0; z[i]; i++){
        const char *zArg = 0;
        struct ArSwitch *pOpt;
        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
          if( z[i]==pOpt->cShort ) break;
        }
        if( pOpt==pEnd ){
          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
        }
        if( pOpt->bArg ){
          if( iArg>=nArg ){
            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
          }
          zArg = azArg[iArg++];
        }
        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
      }
      pAr->nArg = nArg-iArg;
      if( pAr->nArg>0 ){
        pAr->azArg = &azArg[iArg];
      }
    }else{
      /* Non-traditional invocation */
      int iArg;
      for(iArg=1; iArg<nArg; iArg++){
        int n;
        z = azArg[iArg];
        if( z[0]!='-' ){
          /* All remaining command line words are command arguments. */
          pAr->azArg = &azArg[iArg];
          pAr->nArg = nArg-iArg;
          break;
        }
        n = strlen30(z);

        if( z[1]!='-' ){
          int i;
          /* One or more short options */
          for(i=1; i<n; i++){
            const char *zArg = 0;
            struct ArSwitch *pOpt;
            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
              if( z[i]==pOpt->cShort ) break;
            }
            if( pOpt==pEnd ){
              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
            }
            if( pOpt->bArg ){
              if( i<(n-1) ){
                zArg = &z[i+1];
                i = n;
              }else{
                if( iArg>=(nArg-1) ){
                  return arErrorMsg(pAr, "option requires an argument: %c",
                                    z[i]);
                }
                zArg = azArg[++iArg];
              }
            }
            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
          }
        }else if( z[2]=='\0' ){
          /* A -- option, indicating that all remaining command line words
          ** are command arguments.  */
          pAr->azArg = &azArg[iArg+1];
          pAr->nArg = nArg-iArg-1;
          break;
        }else{
          /* A long option */
          const char *zArg = 0;             /* Argument for option, if any */
          struct ArSwitch *pMatch = 0;      /* Matching option */
          struct ArSwitch *pOpt;            /* Iterator */
          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
            const char *zLong = pOpt->zLong;
            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
              if( pMatch ){
                return arErrorMsg(pAr, "ambiguous option: %s",z);
              }else{
                pMatch = pOpt;
              }
            }
          }

          if( pMatch==0 ){
            return arErrorMsg(pAr, "unrecognized option: %s", z);
          }
          if( pMatch->bArg ){
            if( iArg>=(nArg-1) ){
              return arErrorMsg(pAr, "option requires an argument: %s", z);
            }
            zArg = azArg[++iArg];
          }
          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
        }
      }
    }
  }
  if( pAr->eCmd==0 ){
    cli_printf(stderr, "Required argument missing.  Usage:\n");
    return arUsage(stderr);
  }
  return SQLITE_OK;
}

/*
** This function assumes that all arguments within the ArCommand.azArg[]
** array refer to archive members, as for the --extract, --list or --remove
** commands. It checks that each of them are "present". If any specified
** file is not present in the archive, an error is printed to stderr and an
** error code returned. Otherwise, if all specified arguments are present
** in the archive, SQLITE_OK is returned. Here, "present" means either an
** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
** when pAr->bGlob is true.
**
** This function strips any trailing '/' characters from each argument.
** This is consistent with the way the [tar] command seems to work on
** Linux.
*/
static int arCheckEntries(ArCommand *pAr){
  int rc = SQLITE_OK;
  if( pAr->nArg ){
    int i, j;
    sqlite3_stmt *pTest = 0;
    const char *zSel = (pAr->bGlob)
      ? "SELECT name FROM %s WHERE glob($name,name)"
      : "SELECT name FROM %s WHERE name=$name";

    shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
    j = sqlite3_bind_parameter_index(pTest, "$name");
    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
      char *z = pAr->azArg[i];
      int n = strlen30(z);
      int bOk = 0;
      while( n>0 && z[n-1]=='/' ) n--;
      z[n] = '\0';
      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
      if( SQLITE_ROW==sqlite3_step(pTest) ){
        bOk = 1;
      }
      shellReset(&rc, pTest);
      if( rc==SQLITE_OK && bOk==0 ){
        cli_printf(stderr,"not found in archive: %s\n", z);
        rc = SQLITE_ERROR;
      }
    }
    shellFinalize(&rc, pTest);
  }
  return rc;
}

/*
** Format a WHERE clause that can be used against the "sqlar" table to
** identify all archive members that match the command arguments held
** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
** The caller is responsible for eventually calling sqlite3_free() on
** any non-NULL (*pzWhere) value. Here, "match" means strict equality
** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
*/
static void arWhereClause(
  int *pRc,
  ArCommand *pAr,
  char **pzWhere                  /* OUT: New WHERE clause */
){
  char *zWhere = 0;
  if( *pRc==SQLITE_OK ){
    if( pAr->nArg==0 ){
      zWhere = sqlite3_mprintf("1");
    }else{
      char *z1 = sqlite3_mprintf(pAr->bGlob ? "" : "name IN(");
      char *z2 = sqlite3_mprintf("");
      const char *zSep1 = "";
      const char *zSep2 = "";

      int i;
      for(i=0; i<pAr->nArg && z1 && z2; i++){
        const char *z = pAr->azArg[i];
        int n = strlen30(z);

        if( pAr->bGlob ){
          z1 = sqlite3_mprintf("%z%sname GLOB '%q'", z1, zSep2, z);
          z2 = sqlite3_mprintf(
              "%z%ssubstr(name,1,%d) GLOB '%q/'", z2, zSep2, n+1,z
          );
        }else{
          z1 = sqlite3_mprintf("%z%s'%q'", z1, zSep1, z);
          z2 = sqlite3_mprintf("%z%ssubstr(name,1,%d) = '%q/'",z2,zSep2,n+1,z);
        }
        zSep1 = ", ";
        zSep2 = " OR ";
      }
      if( z1==0 || z2==0 ){
        *pRc = SQLITE_NOMEM;
      }else{
        zWhere = sqlite3_mprintf("(%s%s OR (name GLOB '*/*' AND (%s))) ",
            z1, pAr->bGlob==0 ? ")" : "", z2
        );
      }
      sqlite3_free(z1);
      sqlite3_free(z2);
    }
  }
  *pzWhere = zWhere;
}

/*
** Implementation of .ar "lisT" command.
*/
static int arListCommand(ArCommand *pAr){
  const char *zSql = "SELECT %s FROM %s WHERE %s";
  const char *azCols[] = {
    "name",
    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
  };

  char *zWhere = 0;
  sqlite3_stmt *pSql = 0;
  int rc;

  rc = arCheckEntries(pAr);
  arWhereClause(&rc, pAr, &zWhere);

  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
                     pAr->zSrcTable, zWhere);
  if( pAr->bDryRun ){
    cli_printf(pAr->out, "%s\n", sqlite3_sql(pSql));
  }else{
    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
      if( pAr->bVerbose ){
        cli_printf(pAr->out, "%s % 10d  %s  %s\n",
              sqlite3_column_text(pSql, 0), sqlite3_column_int(pSql, 1),
              sqlite3_column_text(pSql, 2),sqlite3_column_text(pSql, 3));
      }else{
        cli_printf(pAr->out, "%s\n", sqlite3_column_text(pSql, 0));
      }
    }
  }
  shellFinalize(&rc, pSql);
  sqlite3_free(zWhere);
  return rc;
}

/*
** Implementation of .ar "Remove" command.
*/
static int arRemoveCommand(ArCommand *pAr){
  int rc = 0;
  char *zSql = 0;
  char *zWhere = 0;

  if( pAr->nArg ){
    /* Verify that args actually exist within the archive before proceeding.
    ** And formulate a WHERE clause to match them.  */
    rc = arCheckEntries(pAr);
    arWhereClause(&rc, pAr, &zWhere);
  }
  if( rc==SQLITE_OK ){
    zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
                           pAr->zSrcTable, zWhere);
    if( pAr->bDryRun ){
      cli_printf(pAr->out, "%s\n", zSql);
    }else{
      char *zErr = 0;
      rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
      if( rc==SQLITE_OK ){
        rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
        if( rc!=SQLITE_OK ){
          sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
        }else{
          rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
        }
      }
      if( zErr ){
        cli_printf(stdout, "ERROR: %s\n", zErr); /* stdout? */
        sqlite3_free(zErr);
      }
    }
  }
  sqlite3_free(zWhere);
  sqlite3_free(zSql);
  return rc;
}

/*
** Implementation of .ar "eXtract" command.
*/
static int arExtractCommand(ArCommand *pAr){
  /* The zSql1[] string is a template for the query that does the
  ** extraction.  Notes:
  **
  **   *  $dir is the directory into which the archive is to be extracted
  **   *  $pass is the integer pass number: 0, 1, or 2
  **   *  The dest CTE is created so that realpath($dir) only needs
  **      to be called once.
  */
  const char *zSql1 =
    "WITH dest(dpath,dlen) AS (\n"
#ifdef _WIN32
    "  SELECT realpath($dir) || '\\',\n"
#else
    "  SELECT realpath($dir) || '/',\n"
#endif
    "  1+length(realpath($dir))\n"
    ")\n"
    "SELECT\n"
    "    ($dir || name),\n"
    "    CASE $dryrun\n"                /*   vv--- azExtraArg */
    "      WHEN 0 THEN writefile($dir||name, %s, mode, mtime)\n"
    "      WHEN 1 THEN 0\n"
    "      ELSE shell_putsnl(format('writefile(%%Q,%%s,%%0o,%%d)',"
                           "$dir||name,quote(%s),mode,mtime)) IS NULL\n"
    "      END\n"                       /*   ^^--- azExtraArg */
    "  FROM dest CROSS JOIN %s\n"
    " WHERE (%s)\n"      /* ^^-- pAr->zSrcTable */
         /*  ^^--- zWhere */
    "   AND (CASE $pass WHEN 0 THEN (mode&0xf000)<>0xa000\n"
    "                   WHEN 1 THEN (mode&0xf000)=0xa000\n"
    "                   ELSE data IS NULL END)\n"
    "   AND dpath=substr(realpath($dir||name),1,dlen)\n" /* No escapes */
    "   AND name NOT GLOB '*..[/\\]*'\n";                /* No /../ in paths */

  const char *azExtraArg[] = {
    "sqlar_uncompress(data, sz)",
    "data"
  };

  sqlite3_stmt *pSql = 0;
  int rc = SQLITE_OK;
  char *zDir = 0;
  char *zWhere = 0;
  int i, j;

  /* If arguments are specified, check that they actually exist within
  ** the archive before proceeding. And formulate a WHERE clause to
  ** match them.  */
  rc = arCheckEntries(pAr);
  arWhereClause(&rc, pAr, &zWhere);

  if( rc==SQLITE_OK ){
    if( pAr->zDir ){
      zDir = sqlite3_mprintf("%s/", pAr->zDir);
    }else{
      zDir = sqlite3_mprintf("");
    }
    if( zDir==0 ) rc = SQLITE_NOMEM;
  }

  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
      azExtraArg[pAr->bZip],
      azExtraArg[pAr->bZip],
      pAr->zSrcTable,
      zWhere
  );

  if( rc==SQLITE_OK ){
    j = sqlite3_bind_parameter_index(pSql, "$dir");
    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
    j = sqlite3_bind_parameter_index(pSql, "$dryrun");
    sqlite3_bind_int(pSql, j, pAr->bDryRun);

    /* Run the SELECT statement twice
    **   (0) writefile() files and directories
    **   (1) writefile() symlinks
    **   (2) writefile() for directory again
    ** The third pass is so that the timestamps for extracted directories
    ** will be reset to the value in the archive, since populating them
    ** in the first pass will have changed the timestamp. */
    for(i=0; i<3; i++){
      if( pAr->bDryRun>=2 ){
        cli_printf(pAr->out, "*** BEGIN PASS %d ***\n", i+1);
      }
      j = sqlite3_bind_parameter_index(pSql, "$pass");
      sqlite3_bind_int(pSql, j, i);
      if( pAr->bDryRun && i==0 ){
        cli_printf(pAr->out, "%s\n", sqlite3_sql(pSql));
      }
      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
        if( i==0 && pAr->bVerbose ){
          cli_printf(pAr->out, "%s\n", sqlite3_column_text(pSql, 0));
        }
      }
      if( pAr->bDryRun==1 ) break;
      shellReset(&rc, pSql);
      if( pAr->bDryRun>=2 ){
        cli_printf(pAr->out, "*** END PASS %d ***\n", i+1);
      }
    }
    shellFinalize(&rc, pSql);
  }

  sqlite3_free(zDir);
  sqlite3_free(zWhere);
  return rc;
}

/*
** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
*/
static int arExecSql(ArCommand *pAr, const char *zSql){
  int rc;
  if( pAr->bDryRun ){
    cli_printf(pAr->out, "%s\n", zSql);
    rc = SQLITE_OK;
  }else{
    char *zErr = 0;
    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
    if( zErr ){
      cli_printf(stdout, "ERROR: %s\n", zErr);
      sqlite3_free(zErr);
    }
  }
  return rc;
}


/*
** Implementation of .ar "create", "insert", and "update" commands.
**
**     create    ->     Create a new SQL archive
**     insert    ->     Insert or reinsert all files listed
**     update    ->     Insert files that have changed or that were not
**                      previously in the archive
**
** Create the "sqlar" table in the database if it does not already exist.
** Then add each file in the azFile[] array to the archive. Directories
** are added recursively. If argument bVerbose is non-zero, a message is
** printed on stdout for each file archived.
**
** The create command is the same as update, except that it drops
** any existing "sqlar" table before beginning.  The "insert" command
** always overwrites every file named on the command-line, where as
** "update" only overwrites if the size or mtime or mode has changed.
*/
static int arCreateOrUpdateCommand(
  ArCommand *pAr,                 /* Command arguments and options */
  int bUpdate,                    /* true for a --create. */
  int bOnlyIfChanged              /* Only update if file has changed */
){
  const char *zCreate =
      "CREATE TABLE IF NOT EXISTS sqlar(\n"
      "  name TEXT PRIMARY KEY,  -- name of the file\n"
      "  mode INT,               -- access permissions\n"
      "  mtime INT,              -- last modification time\n"
      "  sz INT,                 -- original file size\n"
      "  data BLOB               -- compressed content\n"
      ")";
  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
  const char *zInsertFmt[2] = {
     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
     "  SELECT\n"
     "    %s,\n"
     "    mode,\n"
     "    mtime,\n"
     "    CASE substr(lsmode(mode),1,1)\n"
     "      WHEN '-' THEN length(data)\n"
     "      WHEN 'd' THEN 0\n"
     "      ELSE -1 END,\n"
     "    sqlar_compress(data)\n"
     "  FROM fsdir(%Q,%Q) AS disk\n"
     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
     ,
     "REPLACE INTO %s(name,mode,mtime,data)\n"
     "  SELECT\n"
     "    %s,\n"
     "    mode,\n"
     "    mtime,\n"
     "    data\n"
     "  FROM fsdir(%Q,%Q) AS disk\n"
     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
  };
  int i;                          /* For iterating through azFile[] */
  int rc;                         /* Return code */
  const char *zTab = 0;           /* SQL table into which to insert */
  char *zSql;
  char zTemp[50];
  char *zExists = 0;

  arExecSql(pAr, "PRAGMA page_size=512");
  rc = arExecSql(pAr, "SAVEPOINT ar;");
  if( rc!=SQLITE_OK ) return rc;
  zTemp[0] = 0;
  if( pAr->bZip ){
    /* Initialize the zipfile virtual table, if necessary */
    if( pAr->zFile ){
      sqlite3_uint64 r;
      sqlite3_randomness(sizeof(r),&r);
      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
      zTab = zTemp;
      zSql = sqlite3_mprintf(
         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
         zTab, pAr->zFile
      );
      rc = arExecSql(pAr, zSql);
      sqlite3_free(zSql);
    }else{
      zTab = "zip";
    }
  }else{
    /* Initialize the table for an SQLAR */
    zTab = "sqlar";
    if( bUpdate==0 ){
      rc = arExecSql(pAr, zDrop);
      if( rc!=SQLITE_OK ) goto end_ar_transaction;
    }
    rc = arExecSql(pAr, zCreate);
  }
  if( bOnlyIfChanged ){
    zExists = sqlite3_mprintf(
      " AND NOT EXISTS("
          "SELECT 1 FROM %s AS mem"
          " WHERE mem.name=disk.name"
          " AND mem.mtime=disk.mtime"
          " AND mem.mode=disk.mode)", zTab);
  }else{
    zExists = sqlite3_mprintf("");
  }
  if( zExists==0 ) rc = SQLITE_NOMEM;
  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
        pAr->bVerbose ? "shell_putsnl(name)" : "name",
        pAr->azArg[i], pAr->zDir, zExists);
    rc = arExecSql(pAr, zSql2);
    sqlite3_free(zSql2);
  }
end_ar_transaction:
  if( rc!=SQLITE_OK ){
    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
  }else{
    rc = arExecSql(pAr, "RELEASE ar;");
    if( pAr->bZip && pAr->zFile ){
      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
      arExecSql(pAr, zSql);
      sqlite3_free(zSql);
    }
  }
  sqlite3_free(zExists);
  return rc;
}

/*
** Implementation of ".ar" dot command.
*/
static int arDotCommand(
  ShellState *pState,          /* Current shell tool state */
  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
  char **azArg,                /* Array of arguments passed to dot command */
  int nArg                     /* Number of entries in azArg[] */
){
  ArCommand cmd;
  int rc;
  memset(&cmd, 0, sizeof(cmd));
  cmd.fromCmdLine = fromCmdLine;
  rc = arParseCommand(azArg, nArg, &cmd);
  if( rc==SQLITE_OK ){
    int eDbType = SHELL_OPEN_UNSPEC;
    cmd.p = pState;
    cmd.out = pState->out;
    cmd.db = pState->db;
    if( cmd.zFile ){
      eDbType = deduceDatabaseType(cmd.zFile, 1, 0);
    }else{
      eDbType = pState->openMode;
    }
    if( eDbType==SHELL_OPEN_ZIPFILE ){
      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
        if( cmd.zFile==0 ){
          cmd.zSrcTable = sqlite3_mprintf("zip");
        }else{
          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
        }
      }
      cmd.bZip = 1;
    }else if( cmd.zFile ){
      int flags;
      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
           || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
      }else{
        flags = SQLITE_OPEN_READONLY;
      }
      cmd.db = 0;
      if( cmd.bDryRun ){
        cli_printf(cmd.out, "-- open database '%s'%s\n", cmd.zFile,
              eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
      }
      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
      if( rc!=SQLITE_OK ){
        cli_printf(stderr, "cannot open file: %s (%s)\n",
                        cmd.zFile, sqlite3_errmsg(cmd.db));
        goto end_ar_command;
      }
      sqlite3_fileio_init(cmd.db, 0, 0);
      sqlite3_sqlar_init(cmd.db, 0, 0);
      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
                              shellPutsFunc, 0, 0);

    }
    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
      if( cmd.eCmd!=AR_CMD_CREATE
       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
      ){
        cli_printf(stderr, "database does not contain an 'sqlar' table\n");
        rc = SQLITE_ERROR;
        goto end_ar_command;
      }
      cmd.zSrcTable = sqlite3_mprintf("sqlar");
    }

    switch( cmd.eCmd ){
      case AR_CMD_CREATE:
        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
        break;

      case AR_CMD_EXTRACT:
        rc = arExtractCommand(&cmd);
        break;

      case AR_CMD_LIST:
        rc = arListCommand(&cmd);
        break;

      case AR_CMD_HELP:
        arUsage(pState->out);
        break;

      case AR_CMD_INSERT:
        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
        break;

      case AR_CMD_REMOVE:
        rc = arRemoveCommand(&cmd);
        break;

      default:
        assert( cmd.eCmd==AR_CMD_UPDATE );
        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
        break;
    }
  }
end_ar_command:
  if( cmd.db!=pState->db ){
    close_db(cmd.db);
  }
  sqlite3_free(cmd.zSrcTable);

  return rc;
}
/* End of the ".archive" or ".ar" command logic
*******************************************************************************/
#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */

#if SQLITE_SHELL_HAVE_RECOVER

/*
** This function is used as a callback by the recover extension. Simply
** print the supplied SQL statement to stdout.
*/
static int recoverSqlCb(void *pCtx, const char *zSql){
  ShellState *pState = (ShellState*)pCtx;
  cli_printf(pState->out, "%s;\n", zSql);
  return SQLITE_OK;
}

/*
** This function is called to recover data from the database. A script
** to construct a new database containing all recovered data is output
** on stream pState->out.
*/
static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
  int rc = SQLITE_OK;
  const char *zRecoveryDb = "";   /* Name of "recovery" database.  Debug only */
  const char *zLAF = "lost_and_found";
  int bFreelist = 1;              /* 0 if --ignore-freelist is specified */
  int bRowids = 1;                /* 0 if --no-rowids */
  sqlite3_recover *p = 0;
  int i = 0;

  for(i=1; i<nArg; i++){
    char *z = azArg[i];
    int n;
    if( z[0]=='-' && z[1]=='-' ) z++;
    n = strlen30(z);
    if( n<=17 && memcmp("-ignore-freelist", z, n)==0 ){
      bFreelist = 0;
    }else
    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
      /* This option determines the name of the ATTACH-ed database used
      ** internally by the recovery extension.  The default is "" which
      ** means to use a temporary database that is automatically deleted
      ** when closed.  This option is undocumented and might disappear at
      ** any moment. */
      i++;
      zRecoveryDb = azArg[i];
    }else
    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
      i++;
      zLAF = azArg[i];
    }else
    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
      bRowids = 0;
    }
    else{
      cli_printf(stderr,"unexpected option: %s\n", azArg[i]);
      showHelp(pState->out, azArg[0]);
      return 1;
    }
  }

  p = sqlite3_recover_init_sql(
      pState->db, "main", recoverSqlCb, (void*)pState
  );

  if( !pState->bSafeMode ){
    sqlite3_recover_config(p, 789, (void*)zRecoveryDb);  /* Debug use only */
  }
  sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
  sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
  sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);

  cli_printf(pState->out, ".dbconfig defensive off\n");
  sqlite3_recover_run(p);
  if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
    const char *zErr = sqlite3_recover_errmsg(p);
    int errCode = sqlite3_recover_errcode(p);
    cli_printf(stderr,"sql error: %s (%d)\n", zErr, errCode);
  }
  rc = sqlite3_recover_finish(p);
  return rc;
}
#endif /* SQLITE_SHELL_HAVE_RECOVER */

/*
** Implementation of ".intck STEPS_PER_UNLOCK" command.
*/
static int intckDatabaseCmd(ShellState *pState, i64 nStepPerUnlock){
  sqlite3_intck *p = 0;
  int rc = SQLITE_OK;

  rc = sqlite3_intck_open(pState->db, "main", &p);
  if( rc==SQLITE_OK ){
    i64 nStep = 0;
    i64 nError = 0;
    const char *zErr = 0;
    while( SQLITE_OK==sqlite3_intck_step(p) ){
      const char *zMsg = sqlite3_intck_message(p);
      if( zMsg ){
        cli_printf(pState->out, "%s\n", zMsg);
        nError++;
      }
      nStep++;
      if( nStepPerUnlock && (nStep % nStepPerUnlock)==0 ){
        sqlite3_intck_unlock(p);
      }
    }
    rc = sqlite3_intck_error(p, &zErr);
    if( zErr ){
      cli_printf(stderr,"%s\n", zErr);
    }
    sqlite3_intck_close(p);

    cli_printf(pState->out, "%lld steps, %lld errors\n", nStep, nError);
  }

  return rc;
}

/*
 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
 *   close db and set it to 0, and return the columns spec, to later
 *   be sqlite3_free()'ed by the caller.
 * The return is 0 when either:
 *   (a) The db was not initialized and zCol==0 (There are no columns.)
 *   (b) zCol!=0  (Column was added, db initialized as needed.)
 * The 3rd argument, pRenamed, references an out parameter. If the
 * pointer is non-zero, its referent will be set to a summary of renames
 * done if renaming was necessary, or set to 0 if none was done. The out
 * string (if any) must be sqlite3_free()'ed by the caller.
 */
#ifdef SHELL_DEBUG
#define rc_err_oom_die(rc) \
  if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
  else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
    cli_printf(stderr,"E:%d\n",rc), assert(0)
#else
static void rc_err_oom_die(int rc){
  if( rc==SQLITE_NOMEM ) shell_check_oom(0);
  assert(rc==SQLITE_OK||rc==SQLITE_DONE);
}
#endif

#ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB);
#else  /* Otherwise, memory is faster/better for the transient DB. */
static const char *zCOL_DB = ":memory:";
#endif

/* Define character (as C string) to separate generated column ordinal
 * from protected part of incoming column names. This defaults to "_"
 * so that incoming column identifiers that did not need not be quoted
 * remain usable without being quoted. It must be one character.
 */
#ifndef SHELL_AUTOCOLUMN_SEP
# define AUTOCOLUMN_SEP "_"
#else
# define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)
#endif

static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){
  /* Queries and D{D,M}L used here */
  static const char * const zTabMake = "\
CREATE TABLE ColNames(\
 cpos INTEGER PRIMARY KEY,\
 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
CREATE VIEW RepeatedNames AS \
SELECT DISTINCT t.name FROM ColNames t \
WHERE t.name COLLATE NOCASE IN (\
 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
);\
";
  static const char * const zTabFill = "\
INSERT INTO ColNames(name,nlen,chop,reps,suff)\
 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
";
  static const char * const zHasDupes = "\
SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
 <count(name) FROM ColNames\
";
#ifdef SHELL_COLUMN_RENAME_CLEAN
  static const char * const zDedoctor = "\
UPDATE ColNames SET chop=iif(\
  (substring(name,nlen,1) BETWEEN '0' AND '9')\
  AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\
 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\
 0\
)\
";
#endif
  static const char * const zSetReps = "\
UPDATE ColNames AS t SET reps=\
(SELECT count(*) FROM ColNames d \
 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
 COLLATE NOCASE\
)\
";
#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
  static const char * const zColDigits = "\
SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
";
#else
  /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */
  static const char * const zColDigits = "\
SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \
 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \
 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \
";
#endif
  static const char * const zRenameRank =
#ifdef SHELL_COLUMN_RENAME_CLEAN
    "UPDATE ColNames AS t SET suff="
    "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')"
#else /* ...RENAME_MINIMAL_ONE_PASS */
"WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
"  SELECT 0 AS nlz"
"  UNION"
"  SELECT nlz+1 AS nlz FROM Lzn"
"  WHERE EXISTS("
"   SELECT 1"
"   FROM ColNames t, ColNames o"
"   WHERE"
"    iif(t.name IN (SELECT * FROM RepeatedNames),"
"     printf('%s"AUTOCOLUMN_SEP"%s',"
"      t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),"
"     t.name"
"    )"
"    ="
"    iif(o.name IN (SELECT * FROM RepeatedNames),"
"     printf('%s"AUTOCOLUMN_SEP"%s',"
"      o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),"
"     o.name"
"    )"
"    COLLATE NOCASE"
"    AND o.cpos<>t.cpos"
"   GROUP BY t.cpos"
"  )"
") UPDATE Colnames AS t SET"
" chop = 0," /* No chopping, never touch incoming names. */
" suff = iif(name IN (SELECT * FROM RepeatedNames),"
"  printf('"AUTOCOLUMN_SEP"%s', substring("
"   printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),"
"  ''"
" )"
#endif
    ;
  static const char * const zCollectVar = "\
SELECT\
 '('||x'0a'\
 || group_concat(\
  cname,\
  ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
 ||')' AS ColsSpec \
FROM (\
 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
 FROM ColNames ORDER BY cpos\
)";
  static const char * const zRenamesDone =
    "SELECT group_concat("
    " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"
    " ','||x'0a')"
    "FROM ColNames WHERE suff<>'' OR chop!=0"
    ;
  int rc;
  sqlite3_stmt *pStmt = 0;
  assert(pDb!=0);
  if( zColNew ){
    /* Add initial or additional column. Init db if necessary. */
    if( *pDb==0 ){
      if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0;
#ifdef SHELL_COLFIX_DB
      if(*zCOL_DB!=':')
        sqlite3_exec(*pDb,"drop table if exists ColNames;"
                     "drop view if exists RepeatedNames;",0,0,0);
#endif
#undef SHELL_COLFIX_DB
      rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
      rc_err_oom_die(rc);
    }
    assert(*pDb!=0);
    rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
    rc_err_oom_die(rc);
    rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0);
    rc_err_oom_die(rc);
    rc = sqlite3_step(pStmt);
    rc_err_oom_die(rc);
    sqlite3_finalize(pStmt);
    return 0;
  }else if( *pDb==0 ){
    return 0;
  }else{
    /* Formulate the columns spec, close the DB, zero *pDb. */
    char *zColsSpec = 0;
    int hasDupes = db_int(*pDb, "%s", zHasDupes);
    int nDigits = (hasDupes)? db_int(*pDb, "%s", zColDigits) : 0;
    if( hasDupes ){
#ifdef SHELL_COLUMN_RENAME_CLEAN
      rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
      rc_err_oom_die(rc);
#endif
      rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0);
      rc_err_oom_die(rc);
      rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0);
      rc_err_oom_die(rc);
      sqlite3_bind_int(pStmt, 1, nDigits);
      rc = sqlite3_step(pStmt);
      sqlite3_finalize(pStmt);
      if( rc!=SQLITE_DONE ) rc_err_oom_die(SQLITE_NOMEM);
    }
    assert(db_int(*pDb, "%s", zHasDupes)==0); /* Consider: remove this */
    rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
    rc_err_oom_die(rc);
    rc = sqlite3_step(pStmt);
    if( rc==SQLITE_ROW ){
      zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
    }else{
      zColsSpec = 0;
    }
    if( pzRenamed!=0 ){
      if( !hasDupes ) *pzRenamed = 0;
      else{
        sqlite3_finalize(pStmt);
        if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0)
            && SQLITE_ROW==sqlite3_step(pStmt) ){
          *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
        }else
          *pzRenamed = 0;
      }
    }
    sqlite3_finalize(pStmt);
    sqlite3_close(*pDb);
    *pDb = 0;
    return zColsSpec;
  }
}

/*
** Check if the sqlite_schema table contains one or more virtual tables. If
** parameter zLike is not NULL, then it is an SQL expression that the
** sqlite_schema row must also match. If one or more such rows are found,
** print the following warning to the output:
**
** WARNING: Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled
*/
static int outputDumpWarning(ShellState *p, const char *zLike){
  int rc = SQLITE_OK;
  sqlite3_stmt *pStmt = 0;
  shellPreparePrintf(p->db, &rc, &pStmt,
    "SELECT 1 FROM sqlite_schema o WHERE "
    "sql LIKE 'CREATE VIRTUAL TABLE%%' AND (%s)", zLike ? zLike : "true"
  );
  if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
    cli_puts("/* WARNING: "
          "Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled */\n",
          p->out
    );
  }
  shellFinalize(&rc, pStmt);
  return rc;
}

/*
** Fault-Simulator state and logic.
*/
static struct {
  int iId;           /* ID that triggers a simulated fault.  -1 means "any" */
  int iErr;          /* The error code to return on a fault */
  int iCnt;          /* Trigger the fault only if iCnt is already zero */
  int iInterval;     /* Reset iCnt to this value after each fault */
  int eVerbose;      /* When to print output */
  int nHit;          /* Number of hits seen so far */
  int nRepeat;       /* Turn off after this many hits.  0 for never */
  int nSkip;         /* Skip this many before first fault */
} faultsim_state = {-1, 0, 0, 0, 0, 0, 0, 0};

/*
** This is the fault-sim callback
*/
static int faultsim_callback(int iArg){
  if( faultsim_state.iId>0 && faultsim_state.iId!=iArg ){
    return SQLITE_OK;
  }
  if( faultsim_state.iCnt ){
    if( faultsim_state.iCnt>0 ) faultsim_state.iCnt--;
    if( faultsim_state.eVerbose>=2 ){
      cli_printf(stdout, 
         "FAULT-SIM id=%d no-fault (cnt=%d)\n", iArg, faultsim_state.iCnt);
    }
    return SQLITE_OK;
  }
  if( faultsim_state.eVerbose>=1 ){
    cli_printf(stdout, 
         "FAULT-SIM id=%d returns %d\n", iArg, faultsim_state.iErr);
  }
  faultsim_state.iCnt = faultsim_state.iInterval;
  faultsim_state.nHit++;
  if( faultsim_state.nRepeat>0 && faultsim_state.nRepeat<=faultsim_state.nHit ){
    faultsim_state.iCnt = -1;
  }
  return faultsim_state.iErr;
}

/*
** pickStr(zArg, &zErr, zS1, zS2, ..., "");
**
** Try to match zArg against zS1, zS2, and so forth until the first
** emptry string.  Return the index of the match or -1 if none is found.
** If no match is found, and &zErr is not NULL, then write into
** zErr a message describing the valid choices.
*/
static int pickStr(const char *zArg, char **pzErr, ...){
  int i, n;
  const char *z;
  sqlite3_str *pMsg;
  va_list ap;
  va_start(ap, pzErr);
  i = 0;
  while( (z = va_arg(ap,const char*))!=0 && z[0]!=0 ){
    if( cli_strcmp(zArg, z)==0 ) return i;
    i++;
  }
  va_end(ap);
  if( pzErr==0 ) return -1;
  n = i;
  pMsg = sqlite3_str_new(0);
  va_start(ap, pzErr);
  sqlite3_str_appendall(pMsg, "should be");
  i = 0;
  while( (z = va_arg(ap, const char*))!=0 && z[0]!=0 ){
    if( i==n-1 ){
      sqlite3_str_append(pMsg,", or",4);
    }else if( i>0 ){
      sqlite3_str_append(pMsg, ",", 1);
    }
    sqlite3_str_appendf(pMsg, " %s", z);
    i++;
  }
  va_end(ap);
  *pzErr = sqlite3_str_finish(pMsg);
  return -1;
}

/*
** DOT-COMMAND: .import
**
** USAGE: .import [OPTIONS] FILE TABLE
**
** Import CSV or similar text from FILE into TABLE.  If TABLE does
** not exist, it is created using the first row of FILE as the column
** names.  If FILE begins with "|" then it is a command that is run
** and the output from the command is used as the input data.  If
** FILE begins with "<<" followed by a label, then content is read from
** the script until the first line that matches the label.
**
** The content of FILE is interpreted using RFC-4180 ("CSV") quoting
** rules unless the current mode is "ascii" or "tabs" or unless one
** the --ascii option is used.
**
** The column and row separators must be single ASCII characters.  If
** multiple characters or a Unicode character are specified for the
** separators, then only the first byte of the separator is used.  Except,
** if the row separator is \n and the mode is not --ascii, then \r\n is
** understood as a row separator too.
**
** Options:
**   --ascii         Do not use RFC-4180 quoting.  Use \037 and \036
**                   as column and row separators on input, unless other
**                   delimiters are specified using --colsep and/or --rowsep
**   --colsep CHAR   Use CHAR as the column separator.
**   --csv           Input is standard RFC-4180 CSV.
**   --esc CHAR      Use CHAR as an escape character in unquoted CSV inputs.
**   --qesc CHAR     Use CHAR as an escape character in quoted CSV inputs.
**   --rowsep CHAR   Use CHAR as the row separator.
**   --schema S      When creating TABLE, put it in schema S
**   --skip N        Ignore the first N rows of input
**   -v              Verbose mode
*/
static int dotCmdImport(ShellState *p){
  int nArg = p->dot.nArg;     /* Number of arguments */
  char **azArg = p->dot.azArg;/* Argument list */
  char *zTable = 0;           /* Insert data into this table */
  char *zSchema = 0;          /* Schema of zTable */
  char *zFile = 0;            /* Name of file to extra content from */
  sqlite3_stmt *pStmt = NULL; /* A statement */
  int nCol;                   /* Number of columns in the table */
  i64 nByte;                  /* Number of bytes in an SQL string */
  int i, j;                   /* Loop counters */
  int needCommit;             /* True to COMMIT or ROLLBACK at end */
  char *zSql = 0;             /* An SQL statement */
  ImportCtx sCtx;             /* Reader context */
  char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
  int eVerbose = 0;           /* Larger for more console output */
  i64 nSkip = 0;              /* Initial lines to skip */
  i64 iLineOffset = 0;        /* Offset to the first line of input */
  char *zCreate = 0;          /* CREATE TABLE statement text */
  int rc;                     /* Result code */

  failIfSafeMode(p, "cannot run .import in safe mode");
  memset(&sCtx, 0, sizeof(sCtx));
  if( p->mode.eMode==MODE_Ascii ){
    xRead = ascii_read_one_field;
  }else{
    xRead = csv_read_one_field;
  }
  for(i=1; i<nArg; i++){
    char *z = azArg[i];
    if( z[0]=='-' && z[1]=='-' ) z++;
    if( z[0]!='-' ){
      if( zFile==0 ){
        zFile = z;
      }else if( zTable==0 ){
        zTable = z;
      }else{
        dotCmdError(p, i, "unknown argument", 0);
        return 1;
      }
    }else if( cli_strcmp(z,"-v")==0 ){
      eVerbose++;
    }else if( cli_strcmp(z,"-schema")==0 && i<nArg-1 ){
      zSchema = azArg[++i];
    }else if( cli_strcmp(z,"-skip")==0 && i<nArg-1 ){
      nSkip = integerValue(azArg[++i]);
    }else if( cli_strcmp(z,"-ascii")==0 ){
      if( sCtx.cColSep==0 ) sCtx.cColSep = '\037';
      if( sCtx.cRowSep==0 ) sCtx.cRowSep = '\036';
      xRead = ascii_read_one_field;
    }else if( cli_strcmp(z,"-csv")==0 ){
      if( sCtx.cColSep==0 ) sCtx.cColSep = ',';
      if( sCtx.cRowSep==0 ) sCtx.cRowSep = '\n';
      xRead = csv_read_one_field;
    }else if( cli_strcmp(z,"-esc")==0 ){
      sCtx.cUQEscape = azArg[++i][0];
    }else if( cli_strcmp(z,"-qesc")==0 ){
      sCtx.cQEscape = azArg[++i][0];
    }else if( cli_strcmp(z,"-colsep")==0 ){
      if( i==nArg-1 ){
        dotCmdError(p, i, "missing argument", 0);
        return 1;
      }
      i++;
      sCtx.cColSep = azArg[i][0];
    }else if( cli_strcmp(z,"-rowsep")==0 ){
      if( i==nArg-1 ){
        dotCmdError(p, i, "missing argument", 0);
        return 1;
      }
      i++;
      sCtx.cRowSep = azArg[i][0];
    }else{
      dotCmdError(p, i, "unknown option", 0);
      return 1;
    }
  }
  if( zTable==0 ){
    dotCmdError(p, nArg, 0, "Missing %s argument\n",
          zFile==0 ? "FILE" : "TABLE");
    return 1;
  }
  seenInterrupt = 0;
  open_db(p, 0);
  if( sCtx.cColSep==0 ){
    if( p->mode.spec.zColumnSep && p->mode.spec.zColumnSep[0]!=0 ){
      sCtx.cColSep = p->mode.spec.zColumnSep[0];
    }else{
      sCtx.cColSep = ',';
    }
  }
  if( (sCtx.cColSep & 0x80)!=0 ){
    eputz("Error: .import column separator must be ASCII\n");
    return 1;
  }
  if( sCtx.cRowSep==0 ){
    if( p->mode.spec.zRowSep && p->mode.spec.zRowSep[0]!=0 ){
      sCtx.cRowSep = p->mode.spec.zRowSep[0];
    }else{
      sCtx.cRowSep = '\n';
    }
  }
  if( sCtx.cRowSep=='\r' && xRead!=ascii_read_one_field ){
    sCtx.cRowSep = '\n';
  }
  if( (sCtx.cRowSep & 0x80)!=0 ){
    eputz("Error: .import row separator must be ASCII\n");
    return 1;
  }
  sCtx.zFile = zFile;
  sCtx.nLine = 1;
  if( sCtx.zFile[0]=='|' ){
#ifdef SQLITE_OMIT_POPEN
    eputz("Error: pipes are not supported in this OS\n");
    return 1;
#else
    sCtx.in = sqlite3_popen(sCtx.zFile+1, "r");
    sCtx.zFile = "<pipe>";
    sCtx.xCloser = pclose;
#endif
  }else if( sCtx.zFile[0]=='<' && sCtx.zFile[1]=='<' && sCtx.zFile[2]!=0 ){
    /* Input text comes from subsequent lines of script until the zFile
    ** delimiter */
    int nEndMark = strlen30(zFile)-2;
    char *zEndMark = &zFile[2];
    sqlite3_str *pContent = sqlite3_str_new(p->db);
    int ckEnd = 1;
    i64 iStart = p->lineno;
    char zLine[2000];
    sCtx.zFile = p->zInFile;
    sCtx.nLine = p->lineno+1;
    iLineOffset = p->lineno;
    while( sqlite3_fgets(zLine,sizeof(zLine),p->in) ){
      if( ckEnd && cli_strncmp(zLine,zEndMark,nEndMark)==0 ){
        ckEnd = 2;
        if( strchr(zLine,'\n') ) p->lineno++;
        break;
      }
      if( strchr(zLine,'\n') ){
        p->lineno++;
        ckEnd = 1;
      }else{
        ckEnd = 0;
      }
      sqlite3_str_appendall(pContent, zLine);
    }
    sCtx.zIn = sqlite3_str_finish(pContent);
    if( sCtx.zIn==0 ){
      sCtx.zIn = sqlite3_mprintf("");
    }
    if( ckEnd<2 ){
      i64 savedLn = p->lineno;
      p->lineno = iStart;
      dotCmdError(p, 0, 0,"Content terminator \"%s\" not found.",zEndMark);
      p->lineno = savedLn;
      import_cleanup(&sCtx);
      return 1;
    }
  }else{
    sCtx.in = sqlite3_fopen(sCtx.zFile, "rb");
    sCtx.xCloser = fclose;
  }
  if( sCtx.in==0 && sCtx.zIn==0 ){
    dotCmdError(p, 0, 0, "cannot open \"%s\"", zFile);
    import_cleanup(&sCtx);
    return 1;
  }
  if( eVerbose>=1 ){
    char zSep[2];
    zSep[1] = 0;
    zSep[0] = sCtx.cColSep;
    cli_puts("Column separator ", p->out);
    output_c_string(p->out, zSep);
    cli_puts(", row separator ", p->out);
    zSep[0] = sCtx.cRowSep;
    output_c_string(p->out, zSep);
    cli_puts("\n", p->out);
  }
  sCtx.z = sqlite3_malloc64(120);
  if( sCtx.z==0 ){
    import_cleanup(&sCtx);
    shell_out_of_memory();
  }
  /* Below, resources must be freed before exit. */
  while( nSkip>0 ){
    nSkip--;
    while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
  }
  import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
  if( sqlite3_table_column_metadata(p->db, zSchema, zTable,0,0,0,0,0,0) 
   && 0==db_int(p->db, "SELECT count(*) FROM \"%w\".sqlite_schema"
                       " WHERE name=%Q AND type='view'",
                       zSchema ? zSchema : "main", zTable)
  ){
    /* Table does not exist.  Create it. */
    sqlite3 *dbCols = 0;
    char *zRenames = 0;
    char *zColDefs;
    zCreate = sqlite3_mprintf("CREATE TABLE \"%w\".\"%w\"", 
                  zSchema ? zSchema : "main", zTable);
    while( xRead(&sCtx) ){
      zAutoColumn(sCtx.z, &dbCols, 0);
      if( sCtx.cTerm!=sCtx.cColSep ) break;
    }
    zColDefs = zAutoColumn(0, &dbCols, &zRenames);
    if( zRenames!=0 ){
      cli_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
            "Columns renamed during .import %s due to duplicates:\n"
            "%s\n", sCtx.zFile, zRenames);
      sqlite3_free(zRenames);
    }
    assert(dbCols==0);
    if( zColDefs==0 ){
      cli_printf(stderr,"%s: empty file\n", sCtx.zFile);
      import_cleanup(&sCtx);
      sqlite3_free(zCreate);
      return 1;
    }
    zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
    if( zCreate==0 ){
      import_cleanup(&sCtx);
      shell_out_of_memory();
    }
    if( eVerbose>=1 ){
      cli_printf(p->out, "%s\n", zCreate);
    }
    rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
    if( rc ){
      cli_printf(stderr,
           "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
    }
    sqlite3_free(zCreate);
    zCreate = 0;
    if( rc ){
      import_cleanup(&sCtx);
      return 1;
    }
  }
  zSql = sqlite3_mprintf("SELECT count(*) FROM pragma_table_info(%Q,%Q);",
                         zTable, zSchema);
  if( zSql==0 ){
    import_cleanup(&sCtx);
    shell_out_of_memory();
  }
  rc =  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
  sqlite3_free(zSql);
  zSql = 0;
  if( rc ){
    if (pStmt) sqlite3_finalize(pStmt);
    shellDatabaseError(p->db);
    import_cleanup(&sCtx);
    return 1;
  }
  if( sqlite3_step(pStmt)==SQLITE_ROW ){
    nCol = sqlite3_column_int(pStmt, 0);
  }else{
    nCol = 0;
  }
  sqlite3_finalize(pStmt);
  pStmt = 0;
  if( nCol==0 ) return 0; /* no columns, no error */

  nByte = 64                 /* space for "INSERT INTO", "VALUES(", ")\0" */
        + (zSchema ? strlen(zSchema)*2 + 2: 0)  /* Quoted schema name */
        + strlen(zTable)*2 + 2                  /* Quoted table name */
        + nCol*2;            /* Space for ",?" for each column */
  zSql = sqlite3_malloc64( nByte );
  if( zSql==0 ){
    import_cleanup(&sCtx);
    shell_out_of_memory();
  }
  if( zSchema ){
    sqlite3_snprintf(nByte, zSql, "INSERT INTO \"%w\".\"%w\" VALUES(?", 
                     zSchema, zTable);
  }else{
    sqlite3_snprintf(nByte, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
  }
  j = strlen30(zSql);
  for(i=1; i<nCol; i++){
    zSql[j++] = ',';
    zSql[j++] = '?';
  }
  zSql[j++] = ')';
  zSql[j] = 0;
  assert( j<nByte );
  if( eVerbose>=2 ){
    cli_printf(p->out, "Insert using: %s\n", zSql);
  }
  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
  sqlite3_free(zSql);
  zSql = 0;
  if( rc ){
    shellDatabaseError(p->db);
    if (pStmt) sqlite3_finalize(pStmt);
    import_cleanup(&sCtx);
    return 1;
  }
  needCommit = sqlite3_get_autocommit(p->db);
  if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
  do{
    int startLine = sCtx.nLine;
    for(i=0; i<nCol; i++){
      char *z = xRead(&sCtx);
      /*
      ** Did we reach end-of-file before finding any columns?
      ** If so, stop instead of NULL filling the remaining columns.
      */
      if( z==0 && i==0 ) break;
      /*
      ** Did we reach end-of-file OR end-of-line before finding any
      ** columns in ASCII mode?  If so, stop instead of NULL filling
      ** the remaining columns.
      */
      if( p->mode.eMode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
      /*
      ** For CSV mode, per RFC 4180, accept EOF in lieu of final
      ** record terminator but only for last field of multi-field row.
      ** (If there are too few fields, it's not valid CSV anyway.)
      */
      if( z==0 && (xRead==csv_read_one_field) && i==nCol-1 && i>0 ){
        z = "";
      }
      sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
      if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
        if( i==0 && (strcmp(z,"\n")==0 || strcmp(z,"\r\n")==0) ){
          /* Ignore trailing \n or \r\n when some other row separator */
          break;
        }
        cli_printf(stderr,"%s:%d: expected %d columns but found %d"
              " - filling the rest with NULL\n",
              sCtx.zFile, startLine, nCol, i+1);
        i += 2;
        while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
      }
    }
    if( sCtx.cTerm==sCtx.cColSep ){
      do{
        xRead(&sCtx);
        i++;
      }while( sCtx.cTerm==sCtx.cColSep );
      cli_printf(stderr,
            "%s:%d: expected %d columns but found %d - extras ignored\n",
            sCtx.zFile, startLine, nCol, i);
    }
    if( i>=nCol ){
      sqlite3_step(pStmt);
      rc = sqlite3_reset(pStmt);
      if( rc!=SQLITE_OK ){
        cli_printf(stderr,"%s:%d: INSERT failed: %s\n",
              sCtx.zFile, startLine, sqlite3_errmsg(p->db));
        sCtx.nErr++;
        if( bail_on_error ) break;
      }else{
        sCtx.nRow++;
      }
    }
  }while( sCtx.cTerm!=EOF );

  import_cleanup(&sCtx);
  sqlite3_finalize(pStmt);
  if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
  if( eVerbose>0 ){
    cli_printf(p->out,
          "Added %d rows with %d errors using %d lines of input\n",
          sCtx.nRow, sCtx.nErr, sCtx.nLine-1-iLineOffset);
  }
  return sCtx.nErr ? 1 : 0;
}


/*
** This function computes what to show the user about the configured
** titles (or column-names).  Output is an integer between 0 and 3:
**
**    0:     The titles do not matter.  Never show anything.
**    1:     Show  "--titles off"
**    2:     Show  "--titles on"
**    3:     Show  "--title VALUE"  where VALUE is an encoding method
**             to use, one of: plain sql csv html tcl json
**
** Inputs are:
**
**    spec.bTitles   (bT)     Whether or not to show the titles
**    spec.eTitle    (eT)     The actual encoding to be used for titles
**    ModeInfo.bHdr  (bH)     Default value for spec.bTitles
**    ModeInfo.eHdr  (eH)     Default value for spec.eTitle
**    bAll                    Whether the -v option is used
*/
static int modeTitleDsply(ShellState *p, int bAll){
  int eMode = p->mode.eMode;
  const ModeInfo *pI = &aModeInfo[eMode];
  int bT = p->mode.spec.bTitles;
  int eT = p->mode.spec.eTitle;
  int bH = pI->bHdr;
  int eH = pI->eHdr;

  /* Variable "v" is the truth table that will determine the answer
  **
  **                   Actual encoding is different from default 
  **                            vvvvvvvv                                    */
  sqlite3_uint64 v = UINT64_C(0x0133013311220102);
  /*                            ^^^^    ^^^^
  **                   Upper 2-byte groups for when ON/OFF disagrees with
  **                   the default.                                         */

  if( bH==0 ) return 0;  /* Header not appliable.  Ex: off, count */

  if( eT==0 ) eT = eH;   /* Fill in missing spec.eTitle */
  if( bT==0 ) bT = bH;   /* Fill in missing spec.bTitles */

  if( eT!=eH ) v >>= 32; /* Encoding disagree in upper 4-bytes */
  if( bT!=bH ) v >>= 16; /* ON/OFF disagree in upper 2-byte pairs */
  if( bT<2 ) v >>= 8;    /* ON in even bytes, OFF in odd bytes (1st byte 0) */
  if( !bAll ) v >>= 4;   /* bAll values are in the lower half-byte */

  return v & 3;          /* Return the selected truth-table entry */
}

/*
** DOT-COMMAND:  .mode
**
** USAGE: .mode [MODE] [OPTIONS]
**
** Change the output mode to MODE and/or apply OPTIONS to the output mode.
** Arguments are processed from left to right.  If no arguments, show the
** current output mode and relevant options.
**
** Options:
**   --align STRING           Set the alignment of text in columnar modes
**                            String consists of characters 'L', 'C', 'R'
**                            meaning "left", "centered", and "right", with
**                            one letter per column starting from the left.
**                            Unspecified alignment defaults to 'L'.
**   --blob-quote ARG         ARG can be "auto", "text", "sql", "hex", "tcl",
**                            "json", or "size".  Default is "auto".
**   --border on|off          Show outer border on "box" and "table" modes.
**   --charlimit N            Set the maximum number of output characters to
**                            show for any single SQL value to N. Longer values
**                            truncated. Zero means "no limit".
**   --colsep STRING          Use STRING as the column separator
**   --escape ESC             Enable/disable escaping of control characters
**                            found in the output. ESC can be "off", "ascii",
**                            or "symbol".
**   --linelimit N            Set the maximum number of output lines to show for
**                            any single SQL value to N. Longer values are
**                            truncated. Zero means "no limit". Only works
**                            in "line" mode and in columnar modes.
**   --limits L,C,T           Shorthand for "--linelimit L --charlimit C
**                            --titlelimit T". The ",T" can be omitted in which
**                            case the --titlelimit is unchanged.  The argument
**                            can also be "off" to mean "0,0,0" or "on" to
**                            mean "5,300,20".
**   --list                   List available modes
**   --multiinsert N          In "insert" mode, put multiple rows on a single
**                            INSERT statement until the size exceeds N bytes.
**   --null STRING            Render SQL NULL values as the given string
**   --once                   Setting changes to the right are reverted after
**                            the next SQL command.
**   --quote ARG              Enable/disable quoting of text. ARG can be
**                            "off", "on", "sql", "relaxed", "csv", "html",
**                            "tcl", or "json". "off" means show the text as-is.
**                            "on" is an alias for "sql".
**   --reset                  Changes all mode settings back to their default.
**   --rowsep STRING          Use STRING as the row separator
**   --sw|--screenwidth N     Declare the screen width of the output device
**                            to be N characters.  An attempt may be made to
**                            wrap output text to fit within this limit. Zero
**                            means "no limit".  Or N can be "auto" to set the
**                            width automatically.
**   --tablename NAME         Set the name of the table for "insert" mode.
**   --tag NAME               Save mode to the left as NAME.
**   --textjsonb BOOLEAN      If enabled, JSONB text is displayed as text JSON.
**   --title ARG              Whether or not to show column headers, and if so
**                            how to encode them.  ARG can be "off", "on",
**                            "sql", "csv", "html", "tcl", or "json".
**   --titlelimit N           Limit the length of column titles to N characters.
**   -v|--verbose             Verbose output
**   --widths LIST            Set the columns widths for columnar modes. The
**                            argument is a list of integers, one for each
**                            column. A "0" width means use a dynamic width
**                            based on the actual width of data. If there are
**                            fewer entries in LIST than columns, "0" is used
**                            for the unspecified widths.
**   --wordwrap BOOLEAN       Enable/disable word wrapping
**   --wrap N                 Wrap columns wider than N characters
**   --ww                     Shorthand for "--wordwrap on"
*/
static int dotCmdMode(ShellState *p){
  int nArg = p->dot.nArg;     /* Number of arguments */
  char **azArg = p->dot.azArg;/* Argument list */
  int eMode = -1;             /* New mode value, or -1 for none */
  int iMode = -1;             /* Index of the argument that is the mode name */
  int i;                      /* Loop counter */
  int k;                      /* Misc index variable */
  int chng = 0;               /* True if anything has changed */
  int bAll = 0;               /* Show all details of the mode */

  for(i=1; i<nArg; i++){
    const char *z = azArg[i];
    if( z[0]=='-' && z[1]=='-' ) z++;
    if( z[0]!='-'
     && iMode<0
     && (eMode = modeFind(p, azArg[i]))>=0
     && eMode!=MODE_Www
    ){
      iMode = i;
      modeChange(p, eMode);
      /* (Legacy) If the mode is MODE_Insert and the next argument
      ** is not an option, then the next argument must be the table
      ** name.
      */
      if( i+1<nArg && azArg[i+1][0]!='-' ){
        i++;
        modeSetStr(&p->mode.spec.zTableName, azArg[i]);
      }
      chng = 1;
    }else if( optionMatch(z,"align") ){
      char *zAlign;
      int nAlign;
      int nErr = 0;
      if( i+1>=nArg ){
        dotCmdError(p, i, "missing argument", 0);
        return 1;
      }
      i++;
      zAlign = azArg[i];
      nAlign = 0x3fff & strlen(zAlign);
      free(p->mode.spec.aAlign);
      p->mode.spec.aAlign = malloc(nAlign);
      shell_check_oom(p->mode.spec.aAlign);
      for(k=0; k<nAlign; k++){
        unsigned char c = 0;
        switch( zAlign[k] ){
          case 'l': case 'L':   c = QRF_ALIGN_Left;   break;
          case 'c': case 'C':   c = QRF_ALIGN_Center; break;
          case 'r': case 'R':   c = QRF_ALIGN_Right;  break;
          default:  nErr++; break;
        }
        p->mode.spec.aAlign[k] = c;
      }
      p->mode.spec.nAlign = nAlign;
      chng = 1;
      if( nErr ){
        dotCmdError(p, i, "bad alignment string",
             "Should contain only characters L, C, and R.");
        return 1;
      }
    }else if( pickStr(z,0,"-blob","-blob-quote","")>=0 ){
      if( (++i)>=nArg ){
        dotCmdError(p, i-1, "missing argument", 0);
        return 1;
      }
      k = pickStr(azArg[i], 0,
                    "auto", "text", "sql", "hex", "tcl", "json", "size", "");
        /*           0       1       2      3      4      5       6
        ** Must match QRF_BLOB_xxxx values.  See also tag-20251124a */
      if( k>=0 ){
        p->mode.spec.eBlob = k & 0xff;
      }
      chng = 1;
    }else if( optionMatch(z,"border") ){
      if( (++i)>=nArg ){
        dotCmdError(p, i-1, "missing argument", 0);
        return 1;
      }
      k = pickStr(azArg[i], 0, "auto", "off", "on", "");
      if( k>=0 ){
        p->mode.spec.bBorder = k & 0x3;
      }
      chng = 1;
    }else if( 0<=(k=pickStr(z,0,
                  "-charlimit","-linelimit","-titlelimit","-multiinsert","")) ){
      int w;   /*  0            1            2             3 */
      if( i+1>=nArg ){
        dotCmdError(p, i, "missing argument", 0);
        return 1;
      }
      w = integerValue(azArg[++i]);
      switch( k ){
        case 0:   p->mode.spec.nCharLimit = w;    break;
        case 1:   p->mode.spec.nLineLimit = w;    break;
        case 2:   p->mode.spec.nTitleLimit = w;   break;
        default:  p->mode.spec.nMultiInsert = w;  break;
      }
      chng = 1;
    }else if( 0<=(k=pickStr(z,0,"-tablename","-rowsep","-colsep","-null","")) ){
                            /*   0            1         2         3 */
      if( i+1>=nArg ){
        dotCmdError(p, i, "missing argument", 0);
        return 1;
      }
      i++;
      switch( k ){
        case 0: modeSetStr(&p->mode.spec.zTableName, azArg[i]); break;
        case 1: modeSetStr(&p->mode.spec.zRowSep, azArg[i]);    break;
        case 2: modeSetStr(&p->mode.spec.zColumnSep, azArg[i]); break;
        case 3: modeSetStr(&p->mode.spec.zNull, azArg[i]);      break;
      }
      chng = 1;
    }else if( optionMatch(z,"escape") ){
      /* See similar code at tag-20250224-1 */
      char *zErr = 0;
      if( (++i)>=nArg ){
        dotCmdError(p, i-1, "missing argument", 0);
        return 1;
      }                       /*  0     1       2  <-- One less than QRF_ESC_ */
      k = pickStr(azArg[i],&zErr,"off","ascii","symbol","");
      if( k<0 ){
        dotCmdError(p, i, "unknown escape type", "%s", zErr);
        sqlite3_free(zErr);
        return 1;
      }
      p->mode.spec.eEsc = k+1;
      chng = 1;
    }else if( optionMatch(z,"limits") ){
      if( (++i)>=nArg ){
        dotCmdError(p, i-1, "missing argument", 0);
        return 1;
      }
      if( modeSetLimit(p, azArg[i]) ){
        dotCmdError(p, i, "bad argument", "Should be \"on\" or \"off\" or "
                    "\"L,C,T\" where L, C, and T are unsigned integers");
        return 1;
      }
      chng = 1;
    }else if( optionMatch(z,"list") ){
      int ii;
      cli_puts("available modes:", p->out);
      for(ii=0; ii<ArraySize(aModeInfo); ii++){
        if( ii==MODE_Www ) continue;
        cli_printf(p->out, " %s", aModeInfo[ii].zName);
      }
      for(ii=0; ii<p->nSavedModes; ii++){
        cli_printf(p->out, " %s", p->aSavedModes[ii].zTag);
      }
      cli_puts(" batch tty\n", p->out);
      chng = 1;  /* Not really a change, but we still want to suppress the
                 ** "current mode" output */
    }else if( optionMatch(z,"once") ){
      modePush(p);
      p->nPopMode = 1;
    }else if( optionMatch(z,"noquote") ){
      /* (undocumented legacy) --noquote always turns quoting off */
      p->mode.spec.eText = QRF_TEXT_Plain;
      p->mode.spec.eBlob = QRF_BLOB_Auto;
      chng = 1;
    }else if( optionMatch(z,"quote") ){
      if( i+1<nArg
       && azArg[i+1][0]!='-'
       && (iMode>0 || strcmp(azArg[i+1],"off")==0 || modeFind(p, azArg[i+1])<0)
      ){
        /* --quote is followed by an argument other that is not an option
        ** or a mode name.  See it must be a boolean or a keyword to describe
        ** how to set quoting. */
        i++;
        if( (k = pickStr(azArg[i],0,"no","yes","0","1",""))>=0 ){
          k &= 1;   /* 0 for "off".  1 for "on". */
        }else{
          char *zErr = 0;
          k = pickStr(azArg[i],&zErr,
                 "off","on","sql","csv","html","tcl","json","relaxed","");
              /*  0     1    2     3     4      5     6      7   */
          if( k<0 ){
            dotCmdError(p, i, "unknown", "%z", zErr);
            return 1;
          }
        }
      }else{
        /* (Legacy) no following boolean argument.  Turn quoting on */ 
        k = 1;
      }
      switch( k ){
        case 1:  /* on */
          modeSetStr(&p->mode.spec.zNull, "NULL");
          /* Fall through */
        case 2:  /* sql */
          p->mode.spec.eText = QRF_TEXT_Sql;
          break;
        case 3:  /* csv */
          p->mode.spec.eText = QRF_TEXT_Csv;
          break;
        case 4:  /* html */
          p->mode.spec.eText = QRF_TEXT_Html;
          break;
        case 5:  /* tcl */
          p->mode.spec.eText = QRF_TEXT_Tcl;
          break;
        case 6:  /* json */
          p->mode.spec.eText = QRF_TEXT_Json;
          break;
        case 7:  /* relaxed */
          p->mode.spec.eText = QRF_TEXT_Relaxed;
          break;
        default: /* off */
          p->mode.spec.eText = QRF_TEXT_Plain;
          break;
      }
      chng = 1;
    }else if( optionMatch(z,"reset") ){
      int saved_eMode = p->mode.eMode;
      modeFree(&p->mode);
      modeChange(p, saved_eMode);
    }else if( optionMatch(z,"screenwidth") || optionMatch(z,"sw") ){
      if( (++i)>=nArg ){
        dotCmdError(p, i-1, "missing argument", 0);
        return 1;
      }
      k = pickStr(azArg[i],0,"off","auto","");
      if( k==0 ){
        p->mode.bAutoScreenWidth = 0;
        p->mode.spec.nScreenWidth = 0;
      }else if( k==1 ){
        p->mode.bAutoScreenWidth = 1;
      }else{
        i64 w = integerValue(azArg[i]);
        p->mode.bAutoScreenWidth = 0;
        if( w<0 ) w = 0;
        if( w>QRF_MAX_WIDTH ) w = QRF_MAX_WIDTH;
        p->mode.spec.nScreenWidth = w;
      }
      chng = 1;
    }else if( optionMatch(z,"tag") ){
      size_t nByte;
      int n;
      const char *zTag;
      if( i+1>=nArg ){
        dotCmdError(p, i, "missing argument", 0);
        return 1;
      }
      zTag = azArg[++i];
      if( modeFind(p, zTag)>=0 ){
        dotCmdError(p, i, "mode already exists", 0);
        return 1;
      }
      if( p->nSavedModes > MODE_N_USER ){
        dotCmdError(p, i-1, "cannot add more modes", 0);
        return 1;
      }
      n = p->nSavedModes++;
      nByte = sizeof(p->aSavedModes[0]);
      nByte *= n+1;
      p->aSavedModes = realloc( p->aSavedModes, nByte );
      shell_check_oom(p->aSavedModes);
      p->aSavedModes[n].zTag = strdup(zTag);
      shell_check_oom(p->aSavedModes[n].zTag);
      modeDup(&p->aSavedModes[n].mode, &p->mode);
      chng = 1;
    }else if( optionMatch(z,"textjsonb") ){
      if( i+1>=nArg ){
        dotCmdError(p, i, "missing argument", 0);
        return 1;
      }
      p->mode.spec.bTextJsonb = booleanValue(azArg[++i]) ? QRF_Yes : QRF_No;
      chng = 1;
    }else if( optionMatch(z,"titles") || optionMatch(z,"title") ){
      char *zErr = 0;
      if( i+1>=nArg ){
        dotCmdError(p, i, "missing argument", 0);
        return 1;
      }
      k = pickStr(azArg[++i],&zErr,
              "off","on","plain","sql","csv","html","tcl","json","");
          /*   0     1    2       3     4     5     6      7 */
      if( k<0 ){
        dotCmdError(p, i, "bad --titles value","%z", zErr);
        return 1;
      }
      p->mode.spec.bTitles = k>=1 ? QRF_Yes : QRF_No;
      p->mode.mFlags &= ~MFLG_HDR;
      p->mode.spec.eTitle = k>1 ? k-1 : aModeInfo[p->mode.eMode].eHdr;
      chng = 1;
    }else if( optionMatch(z,"widths") || optionMatch(z,"width") ){
      int nWidth = 0;
      short int *aWidth;
      const char *zW;
      if( i+1>=nArg ){
        dotCmdError(p, i, "missing argument", 0);
        return 1;
      }
      zW = azArg[++i];
      /* Every width value takes at least 2 bytes in the input string to
      ** specify, so strlen(zW) bytes should be plenty of space to hold the
      ** result. */
      aWidth = malloc( strlen(zW) );
      while( IsSpace(zW[0]) ) zW++;
      while( zW[0] ){
        int w = 0;
        int nDigit = 0;
        k = zW[0]=='-' && IsDigit(zW[1]);
        while( IsDigit(zW[k]) ){
          w = w*10 + zW[k] - '0';
          if( w>QRF_MAX_WIDTH ){
            dotCmdError(p,i+1,"width too big",
                        "Maximum column width is %d", QRF_MAX_WIDTH);
            free(aWidth);
            return 1;
          }
          nDigit++;
          k++;
        }
        if( nDigit==0 ){
          dotCmdError(p,i+1,"syntax error",
                      "should be a comma-separated list if integers");
          free(aWidth);
          return 1;
        }
        if( zW[0]=='-' ) w = -w;
        aWidth[nWidth++] = w;
        zW += k;
        if( zW[0]==',' ) zW++;
        while( IsSpace(zW[0]) ) zW++;
      }
      free(p->mode.spec.aWidth);
      p->mode.spec.aWidth = aWidth;
      p->mode.spec.nWidth = nWidth;
      chng = 1;
    }else if( optionMatch(z,"wrap") ){
      int w;
      if( i+1>=nArg ){
        dotCmdError(p, i, "missing argument", 0);
        return 1;
      }
      w = integerValue(azArg[++i]);
      if( w<(-QRF_MAX_WIDTH) ) w = -QRF_MAX_WIDTH;
      if( w>QRF_MAX_WIDTH ) w = QRF_MAX_WIDTH;
      p->mode.spec.nWrap = w;
      chng = 1;
    }else if( optionMatch(z,"ww") ){
      p->mode.spec.bWordWrap = QRF_Yes;
      chng = 1;
    }else if( optionMatch(z,"wordwrap") ){
      if( i+1>=nArg ){
        dotCmdError(p, i, "missing argument", 0);
        return 1;
      }
      p->mode.spec.bWordWrap = (u8)booleanValue(azArg[++i]) ? QRF_Yes : QRF_No;
      chng = 1;
    }else if( optionMatch(z,"v") || optionMatch(z,"verbose") ){
      bAll = 1;
    }else if( z[0]=='-' ){
      dotCmdError(p, i, "bad option", "Use \".help .mode\" for more info");
      return 1;
    }else{
      dotCmdError(p, i, iMode>0?"bad argument":"unknown mode",
                  "Use \".help .mode\" for more info");
      return 1;
    }
  }
  if( !chng || bAll ){
    const ModeInfo *pI = aModeInfo + p->mode.eMode;
    sqlite3_str *pDesc = sqlite3_str_new(p->db);
    char *zDesc;
    const char *zSetting;

    if( p->nPopMode ) sqlite3_str_appendall(pDesc, "--once ");
    sqlite3_str_appendall(pDesc,pI->zName);
    if( bAll || (p->mode.spec.nAlign && pI->eCx==2) ){
      int ii;
      sqlite3_str_appendall(pDesc, " --align \"");
      for(ii=0; ii<p->mode.spec.nAlign; ii++){
        unsigned char a = p->mode.spec.aAlign[ii];
        sqlite3_str_appendchar(pDesc, 1, "LLCR"[a&3]);
      }
      sqlite3_str_append(pDesc, "\"", 1);
    }
    if( bAll
     || (p->mode.spec.bBorder==QRF_No) != ((pI->mFlg&1)!=0)
    ){
      sqlite3_str_appendf(pDesc," --border %s",
             p->mode.spec.bBorder==QRF_No ? "off" : "on");
    }
    if( bAll || p->mode.spec.eBlob!=QRF_BLOB_Auto ){
      const char *azBQuote[] =
           { "auto", "text", "sql", "hex", "tcl", "json", "size" };
      /*      0       1       2      3      4      5       6
      ** Must match QRF_BLOB_xxxx values.  See all instances of tag-20251124a */
      u8 e = p->mode.spec.eBlob;
      sqlite3_str_appendf(pDesc, " --blob-quote %s", azBQuote[e]);
    }
    zSetting = aModeStr[pI->eCSep];
    if( bAll || (zSetting && cli_strcmp(zSetting,p->mode.spec.zColumnSep)!=0) ){
      sqlite3_str_appendf(pDesc, " --colsep ");
      append_c_string(pDesc, p->mode.spec.zColumnSep);
    }
    if( bAll || p->mode.spec.eEsc!=QRF_Auto ){
      sqlite3_str_appendf(pDesc, " --escape %s",qrfEscNames[p->mode.spec.eEsc]);
    }
    if( bAll
     || (p->mode.spec.nLineLimit>0 && pI->eCx>0)
     || p->mode.spec.nCharLimit>0
     || (p->mode.spec.nTitleLimit>0 && pI->eCx>0)
    ){
      if( p->mode.spec.nLineLimit==0
       && p->mode.spec.nCharLimit==0
       && p->mode.spec.nTitleLimit==0
      ){
        sqlite3_str_appendf(pDesc, " --limits off");
      }else if( p->mode.spec.nLineLimit==DFLT_LINE_LIMIT
       && p->mode.spec.nCharLimit==DFLT_CHAR_LIMIT
       && p->mode.spec.nTitleLimit==DFLT_TITLE_LIMIT
      ){
        sqlite3_str_appendf(pDesc, " --limits on");
      }else{
        sqlite3_str_appendf(pDesc, " --limits %d,%d,%d",
           p->mode.spec.nLineLimit, p->mode.spec.nCharLimit,
           p->mode.spec.nTitleLimit);
      }
    }
    if( bAll
     || (p->mode.spec.nMultiInsert && p->mode.spec.eStyle==QRF_STYLE_Insert)
    ){
      sqlite3_str_appendf(pDesc, " --multiinsert %u",
                          p->mode.spec.nMultiInsert);
    }
    zSetting = aModeStr[pI->eNull];
    if( bAll || (zSetting && cli_strcmp(zSetting,p->mode.spec.zNull)!=0) ){
      sqlite3_str_appendf(pDesc, " --null ");
      append_c_string(pDesc, p->mode.spec.zNull);
    }
    if( bAll 
     || (pI->eText!=p->mode.spec.eText && (pI->eText>1 || p->mode.spec.eText>1))
    ){
      sqlite3_str_appendf(pDesc," --quote %s",qrfQuoteNames[p->mode.spec.eText]);
    }
    zSetting = aModeStr[pI->eRSep];
    if( bAll || (zSetting && cli_strcmp(zSetting,p->mode.spec.zRowSep)!=0) ){
      sqlite3_str_appendf(pDesc, " --rowsep ");
      append_c_string(pDesc, p->mode.spec.zRowSep);
    }
    if( bAll
     || (pI->eCx && (p->mode.spec.nScreenWidth>0 || p->mode.bAutoScreenWidth))
    ){
      if( p->mode.bAutoScreenWidth ){
        sqlite3_str_appendall(pDesc, " --sw auto");
      }else{
        sqlite3_str_appendf(pDesc," --sw %d",
                            p->mode.spec.nScreenWidth);
      }
    }
    if( bAll || p->mode.eMode==MODE_Insert ){
      sqlite3_str_appendf(pDesc," --tablename ");
      append_c_string(pDesc, p->mode.spec.zTableName);
    }
    if( bAll || p->mode.spec.bTextJsonb ){
      sqlite3_str_appendf(pDesc," --textjsonb %s",
             p->mode.spec.bTextJsonb==QRF_Yes ? "on" : "off");
    }
    k = modeTitleDsply(p, bAll);
    if( k==1 ){
      sqlite3_str_appendall(pDesc, " --titles off");
    }else if( k==2 ){
      sqlite3_str_appendall(pDesc, " --titles on");
    }else if( k==3 ){
      static const char *azTitle[] =
          { "plain", "sql", "csv", "html", "tcl", "json"};
      sqlite3_str_appendf(pDesc, " --titles %s",
                   azTitle[p->mode.spec.eTitle-1]);
    }    
    if( p->mode.spec.nWidth>0 && (bAll || pI->eCx==2) ){
      int ii;
      const char *zSep = " --widths ";
      for(ii=0; ii<p->mode.spec.nWidth; ii++){
        sqlite3_str_appendf(pDesc, "%s%d", zSep, (int)p->mode.spec.aWidth[ii]);
        zSep = ",";
      }
    }else if( bAll ){
      sqlite3_str_appendall(pDesc, " --widths \"\"");
    }
    if( bAll || (pI->eCx>0 && p->mode.spec.bWordWrap) ){
      if( bAll ){
        sqlite3_str_appendf(pDesc, " --wordwrap %s",
          p->mode.spec.bWordWrap==QRF_Yes ? "on" : "off");
      }
      if( p->mode.spec.nWrap ){
        sqlite3_str_appendf(pDesc, " --wrap %d", p->mode.spec.nWrap);
      }
      if( !bAll ) sqlite3_str_append(pDesc, " --ww", 5);
    }
    zDesc = sqlite3_str_finish(pDesc);
    cli_printf(p->out, ".mode %s\n", zDesc);
    fflush(p->out);
    sqlite3_free(zDesc);
  }
  return 0;
}

/*
** DOT-COMMAND: .output
** USAGE: .output [OPTIONS] [FILE]
**
** Begin redirecting output to FILE.  Or if FILE is omitted, revert
** to sending output to the console.  If FILE begins with "|" then
** the remainder of file is taken as a pipe and output is directed
** into that pipe.  If FILE is "memory" then output is captured in an
** internal memory buffer.  If FILE is "off" then output is redirected
** into /dev/null or the equivalent.
**
** Options:
**   --bom             Prepend a byte-order mark to the output
**   -e                Accumulate output in a temporary text file then
**                     launch a text editor when the redirection ends.
**   --error-prefix X  Use X as the left-margin prefix for error messages.
**                     Set to an empty string to restore the default.
**   --keep            Keep redirecting output to its current destination.
**                     Use this option in combination with --show or
**                     with --error-prefix when you do not want to stop
**                     a current redirection.
**   --plain           Use plain text rather than HTML tables with -w
**   --show            Show output text captured by .testcase or by
**                     redirecting to "memory".
**   -w                Show the output in a web browser.  Output is
**                     written into a temporary HTML file until the
**                     redirect ends, then the web browser is launched.
**                     Query results  are shown as HTML tables, unless
**                     the --plain is used too.
**   -x                Show the output in a spreadsheet.  Output is
**                     written to a temp file as CSV then the spreadsheet
**                     is launched when
**
** DOT-COMMAND: .once
** USAGE: .once [OPTIONS] FILE ...
**
** Write the output for the next line of SQL or the next dot-command into
** FILE.  If FILE begins with "|" then it is a program into which output
** is written. The FILE argument should be omitted if one of the -e, -w,
** or -x options is used.
**
** Options:
**   -e                Capture output into a temporary file then bring up
**                     a text editor on that temporary file.
**   --plain           Use plain text rather than HTML tables with -w
**   -w                Capture output into an HTML file then bring up that
**                     file in a web browser
**   -x                Show the output in a spreadsheet.  Output is
**                     written to a temp file as CSV then the spreadsheet
**                     is launched when
**
** DOT-COMMAND: .excel
** Shorthand for ".once -x"
**
** DOT-COMMAND: .www [--plain]
** Shorthand for ".once -w" or ".once --plain -w"
*/
static int dotCmdOutput(ShellState *p){
  int nArg = p->dot.nArg;        /* Number of arguments */
  char **azArg = p->dot.azArg;   /* Text of the arguments */
  char *zFile = 0;               /* The FILE argument */
  int i;                  /* Loop counter */
  int eMode = 0;          /* 0: .outout/.once, 'x'=.excel, 'w'=.www */
  int bOnce = 0;          /* 0: .output, 1: .once, 2: .excel/.www */
  int bPlain = 0;         /* --plain option */
  int bKeep = 0;          /* Keep redirecting */
  static const char *zBomUtf8 = "\357\273\277";
  const char *zBom = 0;
  char c = azArg[0][0];
  int n = strlen30(azArg[0]);

  failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
  if( c=='e' ){
    eMode = 'x';
    bOnce = 2;
  }else if( c=='w' ){
    eMode = 'w';
    bOnce = 2;
  }else if( n>=2 && cli_strncmp(azArg[0],"once",n)==0 ){
    bOnce = 1;
  }
  for(i=1; i<nArg; i++){
    char *z = azArg[i];
    if( z[0]=='-' ){
      if( z[1]=='-' ) z++;
      if( cli_strcmp(z,"-bom")==0 ){
        zBom = zBomUtf8;
      }else if( cli_strcmp(z,"-plain")==0 ){
        bPlain = 1;
      }else if( c=='o' && sqlite3_strglob("-[ewx]",z)==0 ){
        if( bKeep || eMode ){
          dotCmdError(p, i, "incompatible with prior options",0);
          goto dotCmdOutput_error;
        }
        eMode = z[1];
      }else if( cli_strcmp(z,"-show")==0 ){
        if( cli_output_capture ){
          sqlite3_fprintf(stdout, "%s", sqlite3_str_value(cli_output_capture));
        }
      }else if( cli_strcmp(z,"-keep")==0 ){
        bKeep = 1;
      }else if( optionMatch(z,"error-prefix") ){
        if( i+1>=nArg ){
          dotCmdError(p, i, "missing argument", 0);
          return 1;
        }
        free(p->zErrPrefix);
        i++;
        p->zErrPrefix = azArg[i][0]==0 ? 0 : strdup(azArg[i]);
      }else{
        dotCmdError(p, i, "unknown option", 0);
        sqlite3_free(zFile);
        return 1;
      }
    }else if( zFile==0 && eMode==0 ){
      if( bKeep ){
        dotCmdError(p, i, "incompatible with prior options",0);
        goto dotCmdOutput_error;
      }
      if( cli_strcmp(z, "memory")==0 && bOnce ){
        dotCmdError(p, 0, "cannot redirect to \"memory\"", 0);
        goto dotCmdOutput_error;
      }
      if( cli_strcmp(z, "off")==0 ){
#ifdef _WIN32
        zFile = sqlite3_mprintf("nul");
#else
        zFile = sqlite3_mprintf("/dev/null");
#endif
      }else{
        zFile = sqlite3_mprintf("%s", z);
      }
      if( zFile && zFile[0]=='|' ){
        while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
        break;
      }
    }else{
      dotCmdError(p, i, "surplus argument", 0);
      sqlite3_free(zFile);
      return 1;
    }
  }
  if( zFile==0 && !bKeep ){
    zFile = sqlite3_mprintf("stdout");
    shell_check_oom(zFile);
  }
  if( bOnce ){
    p->nPopOutput = 2;
  }else{
    p->nPopOutput = 0;
  }
  if( !bKeep ) output_reset(p);
#ifndef SQLITE_NOHAVE_SYSTEM
  if( eMode=='e' || eMode=='x' || eMode=='w' ){
    p->doXdgOpen = 1;
    modePush(p);
    if( eMode=='x' ){
      /* spreadsheet mode.  Output as CSV. */
      newTempFile(p, "csv");
      p->mode.mFlags &= ~MFLG_ECHO;
      p->mode.eMode = MODE_Csv;
      modeSetStr(&p->mode.spec.zColumnSep, ",");
      modeSetStr(&p->mode.spec.zRowSep, "\r\n");
#ifdef _WIN32
      zBom = zBomUtf8;  /* Always include the BOM on Windows, as Excel does
                        ** not work without it. */
#endif
    }else if( eMode=='w' ){
      /* web-browser mode. */
      newTempFile(p, "html");
      if( !bPlain ) p->mode.eMode = MODE_Www;
    }else{
      /* text editor mode */
      newTempFile(p, "txt");
    }
    sqlite3_free(zFile);
    zFile = sqlite3_mprintf("%s", p->zTempFile);
  }
#endif /* SQLITE_NOHAVE_SYSTEM */
  if( !bKeep ) shell_check_oom(zFile);
  if( bKeep ){
    /* no-op */
  }else if( cli_strcmp(zFile,"memory")==0 ){
    if( cli_output_capture ){
      sqlite3_str_free(cli_output_capture);
    }
    cli_output_capture = sqlite3_str_new(0);
  }else if( zFile[0]=='|' ){
#ifdef SQLITE_OMIT_POPEN
    eputz("Error: pipes are not supported in this OS\n");
    output_redir(p, stdout);
    goto dotCmdOutput_error;
#else
    FILE *pfPipe = sqlite3_popen(zFile + 1, "w");
    if( pfPipe==0 ){
      assert( stderr!=NULL );
      cli_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
      goto dotCmdOutput_error;
    }else{
      output_redir(p, pfPipe);
      if( zBom ) cli_puts(zBom, pfPipe);
      sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
    }
#endif
  }else{
    FILE *pfFile = output_file_open(p, zFile);
    if( pfFile==0 ){
      if( cli_strcmp(zFile,"off")!=0 ){
       assert( stderr!=NULL );
       cli_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
      }
      goto dotCmdOutput_error;
    } else {
      output_redir(p, pfFile);
      if( zBom ) cli_puts(zBom, pfFile);
      if( bPlain && eMode=='w' ){
        cli_puts(
          "<!DOCTYPE html>\n<BODY>\n<PLAINTEXT>\n",
          pfFile
        );
      }
      sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
    }
  }
  sqlite3_free(zFile);
  return 0;

dotCmdOutput_error:
  sqlite3_free(zFile);
  return 1;
}

/*
** DOT-COMMAND: .check
** USAGE: .check [OPTIONS] PATTERN
**
** Verify results of commands since the most recent .testcase command.
** Restore output to the console, unless --keep is used.
**
** If PATTERN starts with "<<ENDMARK" then the actual pattern is taken from
** subsequent lines of text up to the first line that begins with ENDMARK.
** All pattern lines and the ENDMARK are discarded.
**
** Options:
**   --exact                Do an exact comparison including leading and
**                          trailing whitespace.
**   --glob                 Treat PATTERN as a GLOB
**   --keep                 Do not reset the testcase.  More .check commands
**                          will follow.
**   --notglob              Output should not match PATTERN
**   --show                 Write testcase output to the screen, for debugging.
*/
static int dotCmdCheck(ShellState *p){
  int nArg = p->dot.nArg;            /* Number of arguments */
  char **azArg = p->dot.azArg;       /* Text of the arguments */
  int i;                             /* Loop counter */
  int k;                             /* Result of pickStr() */
  char *zTest;                       /* Textcase result */
  int bKeep = 0;                     /* --keep option */
  char *zCheck = 0;                  /* PATTERN argument */
  char *zPattern = 0;                /* Actual test pattern */
  int eCheck = 0;                    /* 1: --glob,  2: --notglob,  3: --exact */
  int isOk;                          /* True if results are OK */
  sqlite3_int64 iStart = p->lineno;  /* Line number of .check statement */

  if( p->zTestcase[0]==0 ){
    dotCmdError(p, 0, "no .testcase is active", 0);
    return 1;
  }
  for(i=1; i<nArg; i++){
    char *z = azArg[i];
    if( z[0]=='-' && z[1]=='-' && z[2]!=0 ) z++;
    if( cli_strcmp(z,"-keep")==0 ){
      bKeep = 1;
    }else if( cli_strcmp(z,"-show")==0 ){
      if( cli_output_capture ){
        sqlite3_fprintf(stdout, "%s", sqlite3_str_value(cli_output_capture));
      }
      bKeep = 1;
    }else if( z[0]=='-'
          && (k = pickStr(&z[1],0,"glob","notglob","exact",""))>=0
    ){
      if( eCheck && eCheck!=k+1 ){
        dotCmdError(p, i, "incompatible with prior options",0);
        return 1;
      }
      eCheck = k+1;
    }else if( zCheck ){
      dotCmdError(p, i, "unknown option", 0);
      return 1;
    }else{
      zCheck = azArg[i];
    }
  }
  if( zCheck==0 ){
    dotCmdError(p, 0, "no PATTERN specified", 0);
    return 1;
  }
  if( cli_output_capture && sqlite3_str_length(cli_output_capture) ){
    zTest = sqlite3_str_value(cli_output_capture);
    shell_check_oom(zTest);
  }else{
    zTest = "";
  }
  p->nTestRun++;
  if( zCheck[0]=='<' && zCheck[1]=='<' && zCheck[2]!=0 ){
    int nCheck = strlen30(zCheck);
    sqlite3_str *pPattern = sqlite3_str_new(p->db);
    char zLine[2000];
    while( sqlite3_fgets(zLine,sizeof(zLine),p->in) ){
      if( strchr(zLine,'\n') ) p->lineno++;
      if( cli_strncmp(&zCheck[2],zLine,nCheck-2)==0 ) break;
      sqlite3_str_appendall(pPattern, zLine);
    }
    zPattern = sqlite3_str_finish(pPattern);
    if( zPattern==0 ){
      zPattern = sqlite3_mprintf("");
    }
  }else{
    zPattern = zCheck;
  }
  shell_check_oom(zPattern);
  switch( eCheck ){
    case 1: {
      char *zGlob = sqlite3_mprintf("*%s*", zPattern);
      isOk = testcase_glob(zGlob, zTest)!=0;
      sqlite3_free(zGlob);
      break;
    }
    case 2: {
      char *zGlob = sqlite3_mprintf("*%s*", zPattern);
      isOk = testcase_glob(zGlob, zTest)==0;
      sqlite3_free(zGlob);
      break;
    }
    case 3: {
      isOk = cli_strcmp(zTest,zPattern)==0;
      break;
    }
    default: {
      /* Skip leading and trailing \n and \r on both pattern and test output */
      const char *z1 = zPattern;
      const char *z2 = zTest;
      size_t n1, n2;
      while( z1[0]=='\n' || z1[0]=='\r' ) z1++;
      n1 = strlen(z1);
      while( n1>0 && (z1[n1-1]=='\n' || z1[n1-1]=='\r') ) n1--;
      while( z2[0]=='\n' || z2[0]=='\r' ) z2++;
      n2 = strlen(z2);
      while( n2>0 && (z2[n2-1]=='\n' || z2[n2-1]=='\r') ) n2--;
      isOk = n1==n2 && memcmp(z1,z2,n1)==0;
      break;
    }
  }
  if( !isOk ){
    sqlite3_fprintf(stderr,
          "%s:%lld: .check failed for testcase %s\n",
        p->zInFile, iStart, p->zTestcase);
    p->nTestErr++;
    sqlite3_fprintf(stderr, "Expected: [%s]\n", zPattern);
    sqlite3_fprintf(stderr, "Got:      [%s]\n", zTest);
  }
  if( zPattern!=zCheck ){
    sqlite3_free(zPattern);
  }
  if( !bKeep ){
    output_reset(p);
    p->zTestcase[0] = 0;
  }
  return 0;
}

/*
** DOT-COMMAND: .testcase
** USAGE: .testcase [OPTIONS] NAME
**
** Start a new test case identified by NAME.  All output
** through the next ".check" command is captured for comparison. See the
** ".check" command for additional informatioon.
**
** Options:
**   --error-prefix TEXT       Change error message prefix text to TEXT
*/
static int dotCmdTestcase(ShellState *p){
  int nArg = p->dot.nArg;        /* Number of arguments */
  char **azArg = p->dot.azArg;   /* Text of the arguments */
  int i;                         /* Loop counter */
  const char *zName = 0;         /* Testcase name */

  for(i=1; i<nArg; i++){
    char *z = azArg[i];
    if( z[0]=='-' && z[1]=='-' && z[2]!=0 ) z++;
    if( optionMatch(z,"error-prefix") ){
      if( i+1>=nArg ){
        dotCmdError(p, i, "missing argument", 0);
        return 1;
      }
      free(p->zErrPrefix);
      i++;
      p->zErrPrefix = azArg[i][0]==0 ? 0 : strdup(azArg[i]);
    }else if( zName ){
      dotCmdError(p, i, "unknown option", 0);
      return 1;
    }else{
      zName = azArg[i];
    }
  }
  output_reset(p);
  if( cli_output_capture ){
    sqlite3_str_free(cli_output_capture);
  }
  cli_output_capture = sqlite3_str_new(0);
  if( zName ){
    sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", zName);
  }else{
    sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s:%lld",
                     p->zInFile, p->lineno);
  }
  return 0;
}

/*
** Enlarge the space allocated in p->dot so that it can hold more
** than nArg parsed command-line arguments.
*/
static void parseDotRealloc(ShellState *p, int nArg){
  p->dot.nAlloc = nArg+22;
  p->dot.azArg = realloc(p->dot.azArg,p->dot.nAlloc*sizeof(char*));
  shell_check_oom(p->dot.azArg);
  p->dot.aiOfst = realloc(p->dot.aiOfst,p->dot.nAlloc*sizeof(int));
  shell_check_oom(p->dot.aiOfst);
  p->dot.abQuot = realloc(p->dot.abQuot,p->dot.nAlloc);
  shell_check_oom(p->dot.abQuot);
}


/*
** Parse input line zLine up into individual arguments.  Retain the
** parse in the p->dot substructure.
*/
static void parseDotCmdArgs(const char *zLine, ShellState *p){
  char *z;
  int h = 1;
  int nArg = 0;
  size_t szLine;

  p->dot.zOrig = zLine;
  free(p->dot.zCopy);
  z = p->dot.zCopy = strdup(zLine);
  shell_check_oom(z);
  szLine = strlen(z);
  while( szLine>0 && IsSpace(z[szLine-1]) ) szLine--;
  if( szLine>0 && z[szLine-1]==';' ){
    szLine--;
    while( szLine>0 && IsSpace(z[szLine-1]) ) szLine--;
  }
  z[szLine] = 0;
  parseDotRealloc(p, 2);
  while( z[h] ){
    while( IsSpace(z[h]) ){ h++; }
    if( z[h]==0 ) break;
    if( nArg+2>p->dot.nAlloc ){
      parseDotRealloc(p, nArg);
    }
    if( z[h]=='\'' || z[h]=='"' ){
      int delim = z[h++];
      p->dot.abQuot[nArg] = 1;
      p->dot.azArg[nArg] = &z[h];
      p->dot.aiOfst[nArg] = h;
      while( z[h] && z[h]!=delim ){
        if( z[h]=='\\' && delim=='"' && z[h+1]!=0 ) h++;
        h++;
      }
      if( z[h]==delim ){
        z[h++] = 0;
      }
      if( delim=='"' ) resolve_backslashes(p->dot.azArg[nArg]);
    }else{
      p->dot.abQuot[nArg] = 0;
      p->dot.azArg[nArg] = &z[h];
      p->dot.aiOfst[nArg] = h;
      while( z[h] && !IsSpace(z[h]) ){ h++; }
      if( z[h] ) z[h++] = 0;
    }
    nArg++;
  }
  p->dot.nArg = nArg;
  p->dot.azArg[nArg] = 0;
}

/*
** If an input line begins with "." then invoke this routine to
** process that line.
**
** Return 1 on error, 2 to exit, and 0 otherwise.
*/
static int do_meta_command(const char *zLine, ShellState *p){
  int nArg;
  int n, c;
  int rc = 0;
  char **azArg;

#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
  if( p->expert.pExpert ){
    expertFinish(p, 1, 0);
  }
#endif

  /* Parse the input line into tokens stored in p->dot.
  */
  parseDotCmdArgs(zLine, p);
  nArg = p->dot.nArg;
  azArg = p->dot.azArg;

  /* Process the input line.
  */
  if( nArg==0 ) return 0; /* no tokens, no error */
  n = strlen30(azArg[0]);
  c = azArg[0][0];
  clearTempFile(p,0,0);

#ifndef SQLITE_OMIT_AUTHORIZATION
  if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
    if( nArg!=2 ){
      cli_printf(stderr, "Usage: .auth ON|OFF\n");
      rc = 1;
      goto meta_command_exit;
    }
    open_db(p, 0);
    if( booleanValue(azArg[1]) ){
      sqlite3_set_authorizer(p->db, shellAuth, p);
    }else if( p->bSafeModePersist ){
      sqlite3_set_authorizer(p->db, safeModeAuth, p);
    }else{
      sqlite3_set_authorizer(p->db, 0, 0);
    }
  }else
#endif

#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
  && !defined(SQLITE_SHELL_FIDDLE)
  if( c=='a' && cli_strncmp(azArg[0], "archive", n)==0 ){
    open_db(p, 0);
    failIfSafeMode(p, "cannot run .archive in safe mode");
    rc = arDotCommand(p, 0, azArg, nArg);
  }else
#endif

#ifndef SQLITE_SHELL_FIDDLE
  if( (c=='b' && n>=3 && cli_strncmp(azArg[0], "backup", n)==0)
   || (c=='s' && n>=3 && cli_strncmp(azArg[0], "save", n)==0)
  ){
    const char *zDestFile = 0;
    const char *zDb = 0;
    sqlite3 *pDest;
    sqlite3_backup *pBackup;
    int j;
    int bAsync = 0;
    const char *zVfs = 0;
    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
    for(j=1; j<nArg; j++){
      const char *z = azArg[j];
      if( z[0]=='-' ){
        if( z[1]=='-' ) z++;
        if( cli_strcmp(z, "-append")==0 ){
          zVfs = "apndvfs";
        }else
        if( cli_strcmp(z, "-async")==0 ){
          bAsync = 1;
        }else
        {
          dotCmdError(p, j, "unknown option", "should be -append or -async");
          return 1;
        }
      }else if( zDestFile==0 ){
        zDestFile = azArg[j];
      }else if( zDb==0 ){
        zDb = zDestFile;
        zDestFile = azArg[j];
      }else{
        cli_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
        return 1;
      }
    }
    if( zDestFile==0 ){
      cli_printf(stderr, "missing FILENAME argument on .backup\n");
      return 1;
    }
    if( zDb==0 ) zDb = "main";
    rc = sqlite3_open_v2(zDestFile, &pDest,
                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
    if( rc!=SQLITE_OK ){
      cli_printf(stderr,"Error: cannot open \"%s\"\n", zDestFile);
      close_db(pDest);
      return 1;
    }
    if( bAsync ){
      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
                   0, 0, 0);
    }
    open_db(p, 0);
    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
    if( pBackup==0 ){
      shellDatabaseError(pDest);
      close_db(pDest);
      return 1;
    }
    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
    sqlite3_backup_finish(pBackup);
    if( rc==SQLITE_DONE ){
      rc = 0;
    }else{
      shellDatabaseError(pDest);
      rc = 1;
    }
    close_db(pDest);
  }else
#endif /* !defined(SQLITE_SHELL_FIDDLE) */

  if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){
    if( nArg==2 ){
      bail_on_error = booleanValue(azArg[1]);
    }else{
      eputz("Usage: .bail on|off\n");
      rc = 1;
    }
  }else

  /* Undocumented.  Legacy only.  See "crlf" below */
  if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){
    eputz("The \".binary\" command is deprecated.\n");
    rc = 1;
  }else

  /* The undocumented ".breakpoint" command causes a call to the no-op
  ** routine named test_breakpoint().
  */
  if( c=='b' && n>=3 && cli_strncmp(azArg[0], "breakpoint", n)==0 ){
    test_breakpoint();
  }else

#ifndef SQLITE_SHELL_FIDDLE
  if( c=='c' && cli_strcmp(azArg[0],"cd")==0 ){
    failIfSafeMode(p, "cannot run .cd in safe mode");
    if( nArg==2 ){
#if defined(_WIN32) || defined(WIN32)
      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
      rc = !SetCurrentDirectoryW(z);
      sqlite3_free(z);
#else
      rc = chdir(azArg[1]);
#endif
      if( rc ){
        cli_printf(stderr,"Cannot change to directory \"%s\"\n", azArg[1]);
        rc = 1;
      }
    }else{
      eputz("Usage: .cd DIRECTORY\n");
      rc = 1;
    }
  }else
#endif /* !defined(SQLITE_SHELL_FIDDLE) */

  if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){
    if( nArg==2 ){
      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
    }else{
      eputz("Usage: .changes on|off\n");
      rc = 1;
    }
  }else

  /* Cancel output redirection, if it is currently set (by .testcase)
  ** Then read the content of the testcase-out.txt file and compare against
  ** azArg[1].  If there are differences, report an error and exit.
  */
  if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){
    rc = dotCmdCheck(p);
  }else

#ifndef SQLITE_SHELL_FIDDLE
  if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
    failIfSafeMode(p, "cannot run .clone in safe mode");
    if( nArg==2 ){
      tryToClone(p, azArg[1]);
    }else{
      eputz("Usage: .clone FILENAME\n");
      rc = 1;
    }
  }else
#endif /* !defined(SQLITE_SHELL_FIDDLE) */

  if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){
    if( nArg==1 ){
      /* List available connections */
      int i;
      for(i=0; i<ArraySize(p->aAuxDb); i++){
        const char *zFile = p->aAuxDb[i].zDbFilename;
        if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
          zFile = "(not open)";
        }else if( zFile==0 ){
          zFile = "(memory)";
        }else if( zFile[0]==0 ){
          zFile = "(temporary-file)";
        }
        if( p->pAuxDb == &p->aAuxDb[i] ){
          cli_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
        }else if( p->aAuxDb[i].db!=0 ){
          cli_printf(stdout, "       %d: %s\n", i, zFile);
        }
      }
    }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
      int i = azArg[1][0] - '0';
      if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
        p->pAuxDb->db = p->db;
        p->pAuxDb = &p->aAuxDb[i];
        globalDb = p->db = p->pAuxDb->db;
        p->pAuxDb->db = 0;
      }
    }else if( nArg==3 && cli_strcmp(azArg[1], "close")==0
           && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
      int i = azArg[2][0] - '0';
      if( i<0 || i>=ArraySize(p->aAuxDb) ){
        /* No-op */
      }else if( p->pAuxDb == &p->aAuxDb[i] ){
        eputz("cannot close the active database connection\n");
        rc = 1;
      }else if( p->aAuxDb[i].db ){
        session_close_all(p, i);
        close_db(p->aAuxDb[i].db);
        p->aAuxDb[i].db = 0;
      }
    }else{
      eputz("Usage: .connection [close] [CONNECTION-NUMBER]\n");
      rc = 1;
    }
  }else

  if( c=='c' && n==4
   && (cli_strncmp(azArg[0], "crlf", n)==0
       || cli_strncmp(azArg[0], "crnl",n)==0)
  ){
    if( nArg==2 ){
#ifdef _WIN32
      if( booleanValue(azArg[1]) ){
        p->mode.mFlags |= MFLG_CRLF;
      }else{
        p->mode.mFlags &= ~MFLG_CRLF;
      }
#else
      p->mode.mFlags &= ~MFLG_CRLF;
#endif
    }
    cli_printf(stderr, "crlf is %s\n", 
       (p->mode.mFlags & MFLG_CRLF)!=0 ? "ON" : "OFF");
  }else

  if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
    char **azName = 0;
    int nName = 0;
    sqlite3_stmt *pStmt;
    int i;
    open_db(p, 0);
    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
    if( rc ){
      shellDatabaseError(p->db);
      rc = 1;
    }else{
      while( sqlite3_step(pStmt)==SQLITE_ROW ){
        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
        if( zSchema==0 || zFile==0 ) continue;
        azName = sqlite3_realloc64(azName, (nName+1)*2*sizeof(char*));
        shell_check_oom(azName);
        azName[nName*2] = strdup(zSchema);
        azName[nName*2+1] = strdup(zFile);
        nName++;
      }
    }
    sqlite3_finalize(pStmt);
    for(i=0; i<nName; i++){
      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
      const char *z = azName[i*2+1];
      cli_printf(p->out, "%s: %s %s%s\n",
            azName[i*2], z && z[0] ? z : "\"\"", bRdonly ? "r/o" : "r/w",
            eTxn==SQLITE_TXN_NONE ? "" :
            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
      free(azName[i*2]);
      free(azName[i*2+1]);
    }
    sqlite3_free(azName);
  }else

  if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbconfig", n)==0 ){
    static const struct DbConfigChoices {
      const char *zName;
      int op;
    } aDbConfig[] = {
        { "attach_create",      SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE  },
        { "attach_write",       SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE   },
        { "comments",           SQLITE_DBCONFIG_ENABLE_COMMENTS       },
        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
        { "fp_digits",          SQLITE_DBCONFIG_FP_DIGITS             },
        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
        { "reverse_scanorder",  SQLITE_DBCONFIG_REVERSE_SCANORDER     },
        { "stmt_scanstatus",    SQLITE_DBCONFIG_STMT_SCANSTATUS       },
        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
    };
    int ii, v;
    open_db(p, 0);
    for(ii=0; ii<ArraySize(aDbConfig); ii++){
      if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
      if( nArg>=3 ){
        if( aDbConfig[ii].op==SQLITE_DBCONFIG_FP_DIGITS ){
          sqlite3_db_config(p->db, aDbConfig[ii].op, atoi(azArg[2]), 0);
        }else{
          sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
        }
      }
      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
      if( aDbConfig[ii].op==SQLITE_DBCONFIG_FP_DIGITS ){
        cli_printf(p->out, "%19s %d\n", aDbConfig[ii].zName, v);
      }else{
        cli_printf(p->out, "%19s %s\n",
                        aDbConfig[ii].zName, v ? "on" : "off");
      }
      if( nArg>1 ) break;
    }
    if( nArg>1 && ii==ArraySize(aDbConfig) ){
      dotCmdError(p, 1, "unknown dbconfig",
         "Enter \".dbconfig\" with no arguments for a list");
    }
  }else

#if SQLITE_SHELL_HAVE_RECOVER
  if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
    rc = shell_dbinfo_command(p, nArg, azArg);
  }else
#endif /* SQLITE_SHELL_HAVE_RECOVER */

  if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbtotxt", n)==0 ){
    open_db(p, 0);
    rc = shell_dbtotxt_command(p, nArg, azArg);
  }else

  if( c=='d' && n==8 && cli_strncmp(azArg[0], "diskused", n)==0 ){
    const char *zSchema = 0;
    int ii;
    char *zSql;
    open_db(p, 0);
    for(ii=1; ii<nArg; ii++){
      const char *z = azArg[ii];
      if( z[0]=='-' ){
        dotCmdError(p, ii, "unknown option", 0);
        rc = 1;
        goto meta_command_exit;
      }
      if( zSchema ){
        dotCmdError(p, ii, "unknown argument", 0);
        rc = 1;
        goto meta_command_exit;
      }
      zSchema = z;
    }
    zSql = sqlite3_mprintf("SELECT diskused(%Q)", zSchema);
    shell_check_oom(zSql);
    modePush(p);
    modeChange(p, MODE_BATCH);
    shell_exec(p, zSql, 0);
    modePop(p);
    sqlite3_free(zSql);
  }else

  if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){
    char *zLike = 0;
    char *zSql;
    int i;
    int savedShellFlags = p->shellFlgs;
    Mode saved_mode;
    ShellClearFlag(p,
       SHFLG_PreserveRowid|SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
    for(i=1; i<nArg; i++){
      if( azArg[i][0]=='-' ){
        const char *z = azArg[i]+1;
        if( z[0]=='-' ) z++;
        if( cli_strcmp(z,"preserve-rowids")==0 ){
#ifdef SQLITE_OMIT_VIRTUALTABLE
          dotCmdError(p, i, "unable",
            "The --preserve-rowids option is not compatible"
                " with SQLITE_OMIT_VIRTUALTABLE");
          rc = 1;
          sqlite3_free(zLike);
          goto meta_command_exit;
#else
          ShellSetFlag(p, SHFLG_PreserveRowid);
#endif
        }else
        if( cli_strcmp(z,"newlines")==0 ){
          /*ShellSetFlag(p, SHFLG_Newlines);*/
        }else
        if( cli_strcmp(z,"data-only")==0 ){
          ShellSetFlag(p, SHFLG_DumpDataOnly);
        }else
        if( cli_strcmp(z,"nosys")==0 ){
          ShellSetFlag(p, SHFLG_DumpNoSys);
        }else
        {
          dotCmdError(p, i, "unknown option", 0);
          rc = 1;
          sqlite3_free(zLike);
          goto meta_command_exit;
        }
      }else{
        /* azArg[i] contains a LIKE pattern. This ".dump" request should
        ** only dump data for tables for which either the table name matches
        ** the LIKE pattern, or the table appears to be a shadow table of
        ** a virtual table for which the name matches the LIKE pattern.
        */
        char *zExpr = sqlite3_mprintf(
            "name LIKE %Q ESCAPE '\\' OR EXISTS ("
            "  SELECT 1 FROM sqlite_schema WHERE "
            "    name LIKE %Q ESCAPE '\\' AND"
            "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
            "    substr(o.name, 1, length(name)+1) == (name||'_')"
            ")", azArg[i], azArg[i]
        );

        if( zLike ){
          zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
        }else{
          zLike = zExpr;
        }
      }
    }

    open_db(p, 0);

    modeDup(&saved_mode, &p->mode);
    outputDumpWarning(p, zLike);
    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
      /* When playing back a "dump", the content might appear in an order
      ** which causes immediate foreign key constraints to be violated.
      ** So disable foreign-key constraint enforcement to prevent problems. */
      cli_puts("PRAGMA foreign_keys=OFF;\n", p->out);
      cli_puts("BEGIN TRANSACTION;\n", p->out);
    }
    p->writableSchema = 0;
    p->mode.spec.bTitles = QRF_No;
    /* Set writable_schema=ON since doing so forces SQLite to initialize
    ** as much of the schema as it can even if the sqlite_schema table is
    ** corrupt. */
    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
    p->nErr = 0;
    if( zLike==0 ) zLike = sqlite3_mprintf("true");
    zSql = sqlite3_mprintf(
      "SELECT name, type, sql FROM sqlite_schema AS o "
      "WHERE (%s) AND type=='table'"
      "  AND sql NOT NULL"
      " ORDER BY tbl_name='sqlite_sequence', rowid",
      zLike
    );
    run_schema_dump_query(p,zSql);
    sqlite3_free(zSql);
    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
      zSql = sqlite3_mprintf(
        "SELECT sql FROM sqlite_schema AS o "
        "WHERE (%s) AND sql NOT NULL"
        "  AND type IN ('index','trigger','view') "
        "ORDER BY type COLLATE NOCASE DESC",
        zLike
      );
      run_table_dump_query(p, zSql);
      sqlite3_free(zSql);
    }
    sqlite3_free(zLike);
    if( p->writableSchema ){
      cli_puts("PRAGMA writable_schema=OFF;\n", p->out);
      p->writableSchema = 0;
    }
    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
      cli_puts(p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n", p->out);
    }
    p->shellFlgs = savedShellFlags;
    modeFree(&p->mode);
    p->mode = saved_mode;
    rc = p->nErr>0;
  }else

  if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
    if( nArg==2 ){
      if( booleanValue(azArg[1]) ){
        p->mode.mFlags |= MFLG_ECHO;
      }else{
        p->mode.mFlags &= ~MFLG_ECHO;
      }
    }else{
      eputz("Usage: .echo on|off\n");
      rc = 1;
    }
  }else

  if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
    if( nArg==2 ){
      if( p->mode.autoEQPtrace ){
        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
        p->mode.autoEQPtrace = 0;
      }
      if( cli_strcmp(azArg[1],"full")==0 ){
        p->mode.autoEQP = AUTOEQP_full;
      }else if( cli_strcmp(azArg[1],"trigger")==0 ){
        p->mode.autoEQP = AUTOEQP_trigger;
#ifdef SQLITE_DEBUG
      }else if( cli_strcmp(azArg[1],"trace")==0 ){
        p->mode.autoEQP = AUTOEQP_full;
        p->mode.autoEQPtrace = 1;
        open_db(p, 0);
        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
#endif
      }else{
        p->mode.autoEQP = (u8)booleanValue(azArg[1]);
      }
    }else{
      eputz("Usage: .eqp off|on|trace|trigger|full\n");
      rc = 1;
    }
  }else

#ifndef SQLITE_SHELL_FIDDLE
  if( c=='e' && cli_strncmp(azArg[0], "exit", n)==0 ){
    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) cli_exit(rc);
    rc = 2;
  }else
#endif

  /* The ".explain" command is automatic now.  It is largely pointless.  It
  ** retained purely for backwards compatibility */
  if( c=='e' && cli_strncmp(azArg[0], "explain", n)==0 ){
    if( nArg>=2 ){
      if( cli_strcmp(azArg[1],"auto")==0 ){
        p->mode.autoExplain = 1;
      }else{
        p->mode.autoExplain = booleanValue(azArg[1]);
      }
    }
  }else

#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
  if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
    if( p->bSafeMode ){
      cli_printf(stderr,
            "Cannot run experimental commands such as \"%s\" in safe mode\n",
            azArg[0]);
      rc = 1;
    }else{
      open_db(p, 0);
      expertDotCommand(p, azArg, nArg);
    }
  }else
#endif

  if( c=='f' && cli_strncmp(azArg[0], "filectrl", n)==0 ){
    static const struct {
       const char *zCtrlName;   /* Name of a test-control option */
       int ctrlCode;            /* Integer code for that option */
       const char *zUsage;      /* Usage notes */
    } aCtrl[] = {
      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
      { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
    };
    int filectrl = -1;
    int iCtrl = -1;
    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
    int n2, i;
    const char *zCmd = 0;
    const char *zSchema = 0;

    open_db(p, 0);
    zCmd = nArg>=2 ? azArg[1] : "help";

    if( zCmd[0]=='-'
     && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0)
     && nArg>=4
    ){
      zSchema = azArg[2];
      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
      nArg -= 2;
      zCmd = azArg[1];
    }

    /* The argument can optionally begin with "-" or "--" */
    if( zCmd[0]=='-' && zCmd[1] ){
      zCmd++;
      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
    }

    /* --help lists all file-controls */
    if( cli_strcmp(zCmd,"help")==0 ){
      cli_puts("Available file-controls:\n", p->out);
      for(i=0; i<ArraySize(aCtrl); i++){
        cli_printf(p->out,
               "  .filectrl %s %s\n", aCtrl[i].zCtrlName, aCtrl[i].zUsage);
      }
      rc = 1;
      goto meta_command_exit;
    }

    /* convert filectrl text option to value. allow any unique prefix
    ** of the option name, or a numerical value. */
    n2 = strlen30(zCmd);
    for(i=0; i<ArraySize(aCtrl); i++){
      if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
        if( filectrl<0 ){
          filectrl = aCtrl[i].ctrlCode;
          iCtrl = i;
        }else{
          cli_printf(stderr,"Error: ambiguous file-control: \"%s\"\n"
                "Use \".filectrl --help\" for help\n", zCmd);
          rc = 1;
          goto meta_command_exit;
        }
      }
    }
    if( filectrl<0 ){
      cli_printf(stderr,"Error: unknown file-control: %s\n"
            "Use \".filectrl --help\" for help\n", zCmd);
    }else{
      switch(filectrl){
        case SQLITE_FCNTL_SIZE_LIMIT: {
          if( nArg!=2 && nArg!=3 ) break;
          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
          isOk = 1;
          break;
        }
        case SQLITE_FCNTL_LOCK_TIMEOUT:
        case SQLITE_FCNTL_CHUNK_SIZE: {
          int x;
          if( nArg!=3 ) break;
          x = (int)integerValue(azArg[2]);
          sqlite3_file_control(p->db, zSchema, filectrl, &x);
          isOk = 2;
          break;
        }
        case SQLITE_FCNTL_PERSIST_WAL:
        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
          int x;
          if( nArg!=2 && nArg!=3 ) break;
          x = nArg==3 ? booleanValue(azArg[2]) : -1;
          sqlite3_file_control(p->db, zSchema, filectrl, &x);
          iRes = x;
          isOk = 1;
          break;
        }
        case SQLITE_FCNTL_DATA_VERSION:
        case SQLITE_FCNTL_HAS_MOVED: {
          int x;
          if( nArg!=2 ) break;
          sqlite3_file_control(p->db, zSchema, filectrl, &x);
          iRes = x;
          isOk = 1;
          break;
        }
        case SQLITE_FCNTL_TEMPFILENAME: {
          char *z = 0;
          if( nArg!=2 ) break;
          sqlite3_file_control(p->db, zSchema, filectrl, &z);
          if( z ){
            cli_printf(p->out, "%s\n", z);
            sqlite3_free(z);
          }
          isOk = 2;
          break;
        }
        case SQLITE_FCNTL_RESERVE_BYTES: {
          int x;
          if( nArg>=3 ){
            x = atoi(azArg[2]);
            sqlite3_file_control(p->db, zSchema, filectrl, &x);
          }
          x = -1;
          sqlite3_file_control(p->db, zSchema, filectrl, &x);
          cli_printf(p->out, "%d\n", x);
          isOk = 2;
          break;
        }
      }
    }
    if( isOk==0 && iCtrl>=0 ){
      cli_printf(p->out, "Usage: .filectrl %s %s\n",
                      zCmd, aCtrl[iCtrl].zUsage);
      rc = 1;
    }else if( isOk==1 ){
      char zBuf[100];
      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
      cli_printf(p->out, "%s\n", zBuf);
    }
  }else

  if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
    ShellState data;
    int doStats = 0;
    int hasStat[5];
    int flgs = 0;
    char *zSql;
    if( nArg==2 && optionMatch(azArg[1], "indent") ){
      nArg = 1;
    }
    if( nArg!=1 ){
      eputz("Usage: .fullschema ?--indent?\n");
      rc = 1;
      goto meta_command_exit;
    }
    open_db(p, 0);
    zSql = sqlite3_mprintf(
       "SELECT shell_format_schema(sql,%d) FROM"
       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
       "     FROM sqlite_schema UNION ALL"
       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
       "WHERE type!='meta' AND sql NOTNULL"
       "  AND name NOT LIKE 'sqlite__%%' ESCAPE '_' "
       "ORDER BY x", flgs);
    memcpy(&data, p, sizeof(data));
    data.mode.spec.bTitles = QRF_No;
    data.mode.eMode = MODE_List;
    data.mode.spec.eText = QRF_TEXT_Plain;
    data.mode.spec.nCharLimit = 0;
    data.mode.spec.zRowSep = "\n";
    rc = shell_exec(&data,zSql,0);
    sqlite3_free(zSql);
    if( rc==SQLITE_OK ){
      sqlite3_stmt *pStmt;
      memset(hasStat, 0, sizeof(hasStat));
      rc = sqlite3_prepare_v2(p->db,
               "SELECT substr(name,12,1) FROM sqlite_schema"
               " WHERE name GLOB 'sqlite_stat[134]'",
               -1, &pStmt, 0);
      if( rc==SQLITE_OK ){
        while( sqlite3_step(pStmt)==SQLITE_ROW ){
          int k = sqlite3_column_int(pStmt,0);
          assert( k==1 || k==3 || k==4 );
          hasStat[k] = 1;
          doStats = 1;
        }
      }
      sqlite3_finalize(pStmt);
    }
    if( doStats==0 ){
      cli_puts("/* No STAT tables available */\n", p->out);
    }else{
      cli_puts("ANALYZE sqlite_schema;\n", p->out);
      data.mode.eMode = MODE_Insert;
      if( hasStat[1] ){
        data.mode.spec.zTableName = "sqlite_stat1";
        shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
      }
      if( hasStat[4] ){
        data.mode.spec.zTableName = "sqlite_stat4";
        shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
      }
      cli_puts("ANALYZE sqlite_schema;\n", p->out);
    }
  }else

  if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
    if( nArg==2 ){
      p->mode.spec.bTitles = booleanValue(azArg[1]) ? QRF_Yes : QRF_No;
      p->mode.mFlags |= MFLG_HDR;
      p->mode.spec.eTitle = aModeInfo[p->mode.eMode].eHdr;
    }else{
      eputz("Usage: .headers on|off\n");
      rc = 1;
    }
  }else

  if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
    if( nArg>=2 ){
      n = showHelp(p->out, azArg[1]);
      if( n==0 ){
        cli_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
      }
    }else{
      showHelp(p->out, 0);
    }
  }else

#ifndef SQLITE_SHELL_FIDDLE
  if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){
    rc = dotCmdImport(p);
  }else
#endif /* !defined(SQLITE_SHELL_FIDDLE) */

#ifndef SQLITE_UNTESTABLE
  if( c=='i' && cli_strncmp(azArg[0], "imposter", n)==0 ){
    char *zSql;
    char *zCollist = 0;
    sqlite3_stmt *pStmt;
    int tnum = 0;
    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
    int i;
    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
      eputz("Usage: .imposter INDEX IMPOSTER\n"
            "       .imposter off\n");
      /* Also allowed, but not documented:
      **
      **    .imposter TABLE IMPOSTER
      **
      ** where TABLE is a WITHOUT ROWID table.  In that case, the
      ** imposter is another WITHOUT ROWID table with the columns in
      ** storage order. */
      rc = 1;
      goto meta_command_exit;
    }
    open_db(p, 0);
    if( nArg==2 ){
      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
      goto meta_command_exit;
    }
    zSql = sqlite3_mprintf(
      "SELECT rootpage, 0 FROM sqlite_schema"
      " WHERE type='index' AND lower(name)=lower('%q')"
      "UNION ALL "
      "SELECT rootpage, 1 FROM sqlite_schema"
      " WHERE type='table' AND lower(name)=lower('%q')"
      "   AND sql LIKE '%%without%%rowid%%'",
      azArg[1], azArg[1]
    );
    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
    sqlite3_free(zSql);
    if( sqlite3_step(pStmt)==SQLITE_ROW ){
      tnum = sqlite3_column_int(pStmt, 0);
      isWO = sqlite3_column_int(pStmt, 1);
    }
    sqlite3_finalize(pStmt);
    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
    sqlite3_free(zSql);
    i = 0;
    while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
      char zLabel[20];
      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
      i++;
      if( zCol==0 ){
        if( sqlite3_column_int(pStmt,1)==-1 ){
          zCol = "_ROWID_";
        }else{
          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
          zCol = zLabel;
        }
      }
      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
        lenPK = (int)strlen(zCollist);
      }
      if( zCollist==0 ){
        zCollist = sqlite3_mprintf("\"%w\"", zCol);
      }else{
        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
      }
    }
    sqlite3_finalize(pStmt);
    if( i==0 || tnum==0 ){
      cli_printf(stderr,"no such index: \"%s\"\n", azArg[1]);
      rc = 1;
      sqlite3_free(zCollist);
      goto meta_command_exit;
    }
    if( lenPK==0 ) lenPK = 100000;
    zSql = sqlite3_mprintf(
          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
          azArg[2], zCollist, lenPK, zCollist);
    sqlite3_free(zCollist);
    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 2, tnum);
    if( rc==SQLITE_OK ){
      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
      if( rc ){
        cli_printf(stderr,
              "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
      }else{
        cli_printf(stdout, "%s;\n", zSql);
      }
    }else{
      cli_printf(stderr,"SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
      rc = 1;
    }
    sqlite3_free(zSql);
  }else
#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */

  if( c=='i' && (cli_strncmp(azArg[0], "indices", n)==0
                 || cli_strncmp(azArg[0], "indexes", n)==0)
  ){
    sqlite3_str *pSql;
    int i;
    int allFlag = 0;
    int sysFlag = 0;
    int exprFlag = 0;
    int debugFlag = 0;  /* Undocument --debug flag */
    const char *zPattern = 0;
    const char *zSep = "WHERE";

    for(i=1; i<nArg; i++){
      if( azArg[i][0]=='-' ){
        const char *z = azArg[i]+1;
        if( z[0]=='-' ) z++;
        if( cli_strcmp(z,"all")==0 || cli_strcmp(z,"a")==0 ){
          allFlag = 1;
        }else
        if( cli_strcmp(z,"sys")==0 ){
          sysFlag = 1;
          allFlag = 0;
        }else
        if( cli_strcmp(z,"expr")==0 ){
          exprFlag = 1;
          allFlag = 0;
        }else
        if( cli_strcmp(z,"debug")==0 ){
          debugFlag = 1;
        }else
        {
          dotCmdError(p, i, "unknown option", 0);
          rc = 1;
          goto meta_command_exit;
        }
      }else if( zPattern==0 ){
        zPattern = azArg[i];
      }else{
        dotCmdError(p, i, "unknown argument", 0);
        rc = 1;
        goto meta_command_exit;
      }
    }

    open_db(p, 0);
    pSql = sqlite3_str_new(p->db);
    sqlite3_str_appendf(pSql,
      "SELECT if(t.schema='main',i.name,t.schema||'.'||i.name)\n"
      "FROM pragma_table_list t, pragma_index_list(t.name,t.schema) i\n"
    );
    if( exprFlag ){
      allFlag = 0;
      sqlite3_str_appendf(pSql,
        "%s (EXISTS(SELECT 1 FROM pragma_index_xinfo(i.name) WHERE cid=-2)\n"
        "       OR\n"
        "       EXISTS(SELECT cid FROM pragma_table_xinfo(t.name) WHERE hidden=2"
        " INTERSECT "
        " SELECT cid FROM pragma_index_info(i.name)))\n", zSep);
      zSep = "AND";
    }
    if( sysFlag ){
      sqlite3_str_appendf(pSql,
           "%s i.name LIKE 'sqlite__autoindex__%%' ESCAPE '_'\n", zSep);
      zSep = "AND";
    }else if( !allFlag ){
      sqlite3_str_appendf(pSql,
           "%s i.name NOT LIKE 'sqlite__%%' ESCAPE '_'\n", zSep);
      zSep = "AND";
    }
    if( zPattern ){
      sqlite3_str_appendf(pSql, "%s i.name LIKE '%%%q%%'\n", zSep, zPattern);
    }
    sqlite3_str_appendf(pSql, "ORDER BY 1");

    /* Run the SQL statement in "split" mode. */
    if( debugFlag ){
      cli_printf(stdout,"%s;\n", sqlite3_str_value(pSql));
    }else{
      modePush(p);
      modeChange(p, MODE_Split);
      shell_exec(p, sqlite3_str_value(pSql), 0);
      modePop(p);
    }
    sqlite3_str_free(pSql);
  }else

  if( c=='i' && cli_strncmp(azArg[0], "intck", n)==0 ){
    i64 iArg = 0;
    if( nArg==2 ){
      iArg = integerValue(azArg[1]);
      if( iArg==0 ) iArg = -1;
    }
    if( (nArg!=1 && nArg!=2) || iArg<0 ){
      cli_printf(stderr,"%s","Usage: .intck STEPS_PER_UNLOCK\n");
      rc = 1;
      goto meta_command_exit;
    }
    open_db(p, 0);
    rc = intckDatabaseCmd(p, iArg);
  }else

#ifdef SQLITE_ENABLE_IOTRACE
  if( c=='i' && cli_strncmp(azArg[0], "iotrace", n)==0 ){
    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
    if( iotrace && iotrace!=stdout ) fclose(iotrace);
    iotrace = 0;
    if( nArg<2 ){
      sqlite3IoTrace = 0;
    }else if( cli_strcmp(azArg[1], "-")==0 ){
      sqlite3IoTrace = iotracePrintf;
      iotrace = stdout;
    }else{
      iotrace = sqlite3_fopen(azArg[1], "w");
      if( iotrace==0 ){
        cli_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
        sqlite3IoTrace = 0;
        rc = 1;
      }else{
        sqlite3IoTrace = iotracePrintf;
      }
    }
  }else
#endif

  if( c=='l' && n>=5 && cli_strncmp(azArg[0], "limits", n)==0 ){
    static const struct {
       const char *zLimitName;   /* Name of a limit */
       int limitCode;            /* Integer code for that limit */
    } aLimit[] = {
      { "length",                SQLITE_LIMIT_LENGTH                    },
      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
      { "column",                SQLITE_LIMIT_COLUMN                    },
      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
      { "parser_depth",          SQLITE_LIMIT_PARSER_DEPTH              },
      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
      { "attached",              SQLITE_LIMIT_ATTACHED                  },
      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
    };
    int i, n2;
    open_db(p, 0);
    if( nArg==1 ){
      for(i=0; i<ArraySize(aLimit); i++){
        cli_printf(stdout, "%20s %d\n", aLimit[i].zLimitName,
              sqlite3_limit(p->db, aLimit[i].limitCode, -1));
      }
    }else if( nArg>3 ){
      eputz("Usage: .limit NAME ?NEW-VALUE?\n");
      rc = 1;
      goto meta_command_exit;
    }else{
      int iLimit = -1;
      n2 = strlen30(azArg[1]);
      for(i=0; i<ArraySize(aLimit); i++){
        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
          if( iLimit<0 ){
            iLimit = i;
          }else{
            cli_printf(stderr,"ambiguous limit: \"%s\"\n", azArg[1]);
            rc = 1;
            goto meta_command_exit;
          }
        }
      }
      if( iLimit<0 ){
        cli_printf(stderr,"unknown limit: \"%s\"\n"
              "enter \".limits\" with no arguments for a list.\n",
              azArg[1]);
        rc = 1;
        goto meta_command_exit;
      }
      if( nArg==3 ){
        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
                      (int)integerValue(azArg[2]));
      }else{
        cli_printf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName,
              sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
      }
    }
  }else

  if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
    open_db(p, 0);
    lintDotCommand(p, azArg, nArg);
  }else

#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
  if( c=='l' && cli_strncmp(azArg[0], "load", n)==0 ){
    const char *zFile, *zProc;
    char *zErrMsg = 0;
    failIfSafeMode(p, "cannot run .load in safe mode");
    if( nArg<2 || azArg[1][0]==0 ){
      /* Must have a non-empty FILE. (Will not load self.) */
      eputz("Usage: .load FILE ?ENTRYPOINT?\n");
      rc = 1;
      goto meta_command_exit;
    }
    zFile = azArg[1];
    zProc = nArg>=3 ? azArg[2] : 0;
    open_db(p, 0);
    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
    if( rc!=SQLITE_OK ){
      shellEmitError(zErrMsg);
      sqlite3_free(zErrMsg);
      rc = 1;
    }
  }else
#endif

  if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){
    if( nArg!=2 ){
      eputz("Usage: .log FILENAME\n");
      rc = 1;
    }else{
      const char *zFile = azArg[1];
      if( p->bSafeMode
       && cli_strcmp(zFile,"on")!=0
       && cli_strcmp(zFile,"off")!=0
      ){
        sputz(stdout, "cannot set .log to anything other"
              " than \"on\" or \"off\"\n");
        zFile = "off";
      }
      output_file_close(p->pLog);
      if( cli_strcmp(zFile,"on")==0 ) zFile = "stdout";
      p->pLog = output_file_open(p, zFile);
    }
  }else

  if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){
    rc = dotCmdMode(p);
  }else

#ifndef SQLITE_SHELL_FIDDLE
  if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
    if( nArg!=2 ){
      eputz("Usage: .nonce NONCE\n");
      rc = 1;
    }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
      cli_printf(stderr,"line %lld: incorrect nonce: \"%s\"\n",
            p->lineno, azArg[1]);
      cli_exit(1);
    }else{
      p->bSafeMode = 0;
      return 0;  /* Return immediately to bypass the safe mode reset
                 ** at the end of this procedure */
    }
  }else
#endif /* !defined(SQLITE_SHELL_FIDDLE) */

  if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){
    if( nArg==2 ){
      modeSetStr(&p->mode.spec.zNull, azArg[1]);
    }else{
      eputz("Usage: .nullvalue STRING\n");
      rc = 1;
    }
  }else

  if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){
    const char *zFN = 0;     /* Pointer to constant filename */
    char *zNewFilename = 0;  /* Name of the database file to open */
    int iName = 1;           /* Index in azArg[] of the filename */
    int newFlag = 0;         /* True to delete file before opening */
    int openMode = SHELL_OPEN_UNSPEC;
    int openFlags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;

    if( p->bSafeMode ) openFlags = SQLITE_OPEN_READONLY;

    /* Check for command-line arguments */
    for(iName=1; iName<nArg; iName++){
      const char *z = azArg[iName];
#ifndef SQLITE_SHELL_FIDDLE
      if( optionMatch(z,"new") ){
        newFlag = 1;
#ifdef SQLITE_HAVE_ZLIB
      }else if( optionMatch(z, "zip") && !p->bSafeMode ){
        openMode = SHELL_OPEN_ZIPFILE;
#endif
      }else if( optionMatch(z, "append") && !p->bSafeMode ){
        openMode = SHELL_OPEN_APPENDVFS;
      }else if( optionMatch(z, "readonly") ){
        openFlags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
        openFlags |= SQLITE_OPEN_READONLY;
      }else if( optionMatch(z, "exclusive") ){
        openFlags |= SQLITE_OPEN_EXCLUSIVE;
      }else if( optionMatch(z, "ifexists") ){
        openFlags &= ~(SQLITE_OPEN_CREATE);
      }else if( optionMatch(z, "nofollow") ){
        openFlags |= SQLITE_OPEN_NOFOLLOW;
#ifndef SQLITE_OMIT_DESERIALIZE
      }else if( optionMatch(z, "deserialize") ){
        openMode = SHELL_OPEN_DESERIALIZE;
      }else if( optionMatch(z, "hexdb") ){
        openMode = SHELL_OPEN_HEXDB;
      }else if( optionMatch(z, "normal") ){
        openMode = SHELL_OPEN_NORMAL;
      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
        p->szMax = integerValue(azArg[++iName]);
#endif /* SQLITE_OMIT_DESERIALIZE */
      }else
#endif /* !SQLITE_SHELL_FIDDLE */
      if( z[0]=='-' ){
        cli_printf(stderr,"unknown option: %s\n", z);
        rc = 1;
        goto meta_command_exit;
      }else if( zFN ){
        cli_printf(stderr,"extra argument: \"%s\"\n", z);
        rc = 1;
        goto meta_command_exit;
      }else{
        zFN = z;
      }
    }

    /* Close the existing database */
    session_close_all(p, -1);
    close_db(p->db);
    p->db = 0;
    p->pAuxDb->zDbFilename = 0;
    sqlite3_free(p->pAuxDb->zFreeOnClose);
    p->pAuxDb->zFreeOnClose = 0;
    p->openMode = openMode;
    p->openFlags = openFlags;
    p->szMax = 0;

    /* If a filename is specified, try to open it first */
    if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
      if( newFlag && zFN && !p->bSafeMode ){
        if( cli_strncmp(zFN,"file:",5)==0 ){
          char *zDel = shellFilenameFromUri(zFN);
          shell_check_oom(zDel);
          shellDeleteFile(zDel, 0);
          sqlite3_free(zDel);
        }else{
          shellDeleteFile(zFN, 0);
        }
      }
#ifndef SQLITE_SHELL_FIDDLE
      if( p->bSafeMode
       && p->openMode!=SHELL_OPEN_HEXDB
       && zFN
       && cli_strcmp(zFN,":memory:")!=0
      ){
        failIfSafeMode(p, "cannot open disk-based database files in safe mode");
      }
#else
      /* WASM mode has its own sandboxed pseudo-filesystem. */
#endif
      if( zFN ){
        zNewFilename = sqlite3_mprintf("%s", zFN);
        shell_check_oom(zNewFilename);
      }else{
        zNewFilename = 0;
      }
      p->pAuxDb->zDbFilename = zNewFilename;
      open_db(p, OPEN_DB_KEEPALIVE);
      if( p->db==0 ){
        cli_printf(stderr,"Error: cannot open '%s'\n", zNewFilename);
        sqlite3_free(zNewFilename);
      }else{
        p->pAuxDb->zFreeOnClose = zNewFilename;
      }
    }
    if( p->db==0 ){
      /* As a fall-back open a TEMP database */
      p->pAuxDb->zDbFilename = 0;
      open_db(p, 0);
    }
  }else

#ifndef SQLITE_SHELL_FIDDLE
  if( (c=='o'
        && (cli_strncmp(azArg[0], "output", n)==0
            || cli_strncmp(azArg[0], "once", n)==0))
   || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0)
   || (c=='w' && n==3 && cli_strcmp(azArg[0],"www")==0)
  ){
    rc = dotCmdOutput(p);
  }else
#endif /* !defined(SQLITE_SHELL_FIDDLE) */

  if( c=='p' && n>=3 && cli_strncmp(azArg[0], "parameter", n)==0 ){
    open_db(p,0);
    if( nArg<=1 ) goto parameter_syntax_error;

    /* .parameter clear
    ** Clear all bind parameters by dropping the TEMP table that holds them.
    */
    if( nArg==2 && cli_strcmp(azArg[1],"clear")==0 ){
      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
                   0, 0, 0);
    }else

    /* .parameter list
    ** List all bind parameters.
    */
    if( nArg==2 && cli_strcmp(azArg[1],"list")==0 ){
      sqlite3_stmt *pStmt = 0;
      int rx;
      int len = 0;
      rx = sqlite3_prepare_v2(p->db,
             "SELECT max(length(key)) "
             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
        len = sqlite3_column_int(pStmt, 0);
        if( len>40 ) len = 40;
      }
      sqlite3_finalize(pStmt);
      pStmt = 0;
      if( len ){
        rx = sqlite3_prepare_v2(p->db,
             "SELECT key, quote(value) "
             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
        while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
          cli_printf(p->out,
                "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
                sqlite3_column_text(pStmt,1));
        }
        sqlite3_finalize(pStmt);
      }
    }else

    /* .parameter init
    ** Make sure the TEMP table used to hold bind parameters exists.
    ** Create it if necessary.
    */
    if( nArg==2 && cli_strcmp(azArg[1],"init")==0 ){
      bind_table_init(p);
    }else

    /* .parameter set NAME VALUE
    ** Set or reset a bind parameter.  NAME should be the full parameter
    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
    ** VALUE can be in either SQL literal notation, or if not it will be
    ** understood to be a text string.
    */
    if( nArg==4 && cli_strcmp(azArg[1],"set")==0 ){
      int rx;
      char *zSql;
      sqlite3_stmt *pStmt;
      const char *zKey = azArg[2];
      const char *zValue = azArg[3];
      bind_table_init(p);
      zSql = sqlite3_mprintf(
                  "REPLACE INTO temp.sqlite_parameters(key,value)"
                  "VALUES(%Q,%s);", zKey, zValue);
      shell_check_oom(zSql);
      pStmt = 0;
      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
      sqlite3_free(zSql);
      if( rx!=SQLITE_OK ){
        sqlite3_finalize(pStmt);
        pStmt = 0;
        zSql = sqlite3_mprintf(
                   "REPLACE INTO temp.sqlite_parameters(key,value)"
                   "VALUES(%Q,%Q);", zKey, zValue);
        shell_check_oom(zSql);
        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
        sqlite3_free(zSql);
        if( rx!=SQLITE_OK ){
          cli_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
          sqlite3_finalize(pStmt);
          pStmt = 0;
          rc = 1;
        }
      }
      bind_prepared_stmt(p, pStmt);
      sqlite3_step(pStmt);
      sqlite3_finalize(pStmt);
    }else

    /* .parameter unset NAME
    ** Remove the NAME binding from the parameter binding table, if it
    ** exists.
    */
    if( nArg==3 && cli_strcmp(azArg[1],"unset")==0 ){
      char *zSql = sqlite3_mprintf(
          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
      shell_check_oom(zSql);
      sqlite3_exec(p->db, zSql, 0, 0, 0);
      sqlite3_free(zSql);
    }else
    /* If no command name matches, show a syntax error */
    parameter_syntax_error:
    showHelp(p->out, "parameter");
  }else

  if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
    int i;
    for(i=1; i<nArg; i++){
      if( i>1 ) cli_puts(" ", p->out);
      cli_puts(azArg[i], p->out);
    }
    cli_puts("\n", p->out);
  }else

#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
  if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
    int i;
    int nn = 0;
    p->flgProgress = 0;
    p->mxProgress = 0;
    p->nProgress = 0;
    for(i=1; i<nArg; i++){
      const char *z = azArg[i];
      if( z[0]=='-' ){
        z++;
        if( z[0]=='-' ) z++;
        if( cli_strcmp(z,"quiet")==0 || cli_strcmp(z,"q")==0 ){
          p->flgProgress |= SHELL_PROGRESS_QUIET;
          continue;
        }
        if( cli_strcmp(z,"reset")==0 ){
          p->flgProgress |= SHELL_PROGRESS_RESET;
          continue;
        }
        if( cli_strcmp(z,"once")==0 ){
          p->flgProgress |= SHELL_PROGRESS_ONCE;
          continue;
        }
        if( cli_strcmp(z,"timeout")==0 ){
          if( i==nArg-1 ){
            dotCmdError(p, i, "missing argument", 0);
            return 1;
          }
          i++;
          p->tmProgress = atof(azArg[i]);
          if( p->tmProgress>0.0 ){
            p->flgProgress = SHELL_PROGRESS_QUIET|SHELL_PROGRESS_TMOUT;
            if( nn==0 ) nn = 100;
          }
          continue;
        }
        if( cli_strcmp(z,"limit")==0 ){
          if( i+1>=nArg ){
            eputz("Error: missing argument on --limit\n");
            rc = 1;
            goto meta_command_exit;
          }else{
            p->mxProgress = (int)integerValue(azArg[++i]);
          }
          continue;
        }
        cli_printf(stderr,"Error: unknown option: \"%s\"\n", azArg[i]);
        rc = 1;
        goto meta_command_exit;
      }else{
        nn = (int)integerValue(z);
      }
    }
    open_db(p, 0);
    sqlite3_progress_handler(p->db, nn, progress_handler, p);
  }else
#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */

  if( c=='p' && cli_strncmp(azArg[0], "prompt", n)==0 ){
    int i;
    int cnt = 0;
    int noOpt = 0;
    for(i=1; i<nArg; i++){
      const char *z = azArg[i];
      if( z[0]=='-' && !noOpt ){
        if( z[1]=='-' ) z++;
        if( strcmp(z,"-hard-reset")==0 ){
#ifdef _WIN32
          _putenv("SQLITE_PS1=");
          _putenv("SQLITE_PS2=");
#else
          unsetenv("SQLITE_PS1");
          unsetenv("SQLITE_PS2");
#endif
          z += 5;
        }
        if( strcmp(z,"-reset")==0 ){
          free(p->azPrompt[0]);
          free(p->azPrompt[1]);
          memset(p->azPrompt, 0, sizeof(p->azPrompt));
        }else
        if( strcmp(z,"-show")==0 ){
          cli_printf(stdout,"Main prompt:  '%s'\n", prompt_string(p, 0));
          cli_printf(stdout,"Continuation: '%s'\n", prompt_string(p, 1));
        }else
#ifndef SQLITE_NO_COLOR
        if( strcmp(z,"-color")==0 ){
#ifdef _WIN32
          _putenv("NO_COLOR=");
#else
          unsetenv("NO_COLOR");
#endif
        }else
        if( strcmp(z,"-no-color")==0 ){
#ifdef _WIN32
          _putenv("NO_COLOR=1");
#else
          setenv("NO_COLOR","1",1);
#endif
        }else
#endif
        if( strcmp(z,"-")==0 ){
          noOpt = 1;
        }else
        {
          dotCmdError(p, i, "unknown option", 0);
          rc = 1;
          goto meta_command_exit;
        }
      }else if( cnt>1 ){
        dotCmdError(p, i, "extra argument", 0);
        rc = 1;
        goto meta_command_exit;
      }else if( !p->dot.abQuot[i] && sqlite3_strglob("*[^a-z]*",z)!=0 ){
        dotCmdError(p, i, "use quotes around the prompt string", 0);
        rc = 1;
      }else{
        free(p->azPrompt[cnt]);
        p->azPrompt[cnt] = strdup(z);
        cnt++;
      }
    } 
  }else

#ifndef SQLITE_SHELL_FIDDLE
  if( c=='q' && cli_strncmp(azArg[0], "quit", n)==0 ){
    rc = 2;
  }else
#endif

#ifndef SQLITE_SHELL_FIDDLE
  if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){
    FILE *inSaved = p->in;
    i64 savedLineno = p->lineno;
    failIfSafeMode(p, "cannot run .read in safe mode");
    if( nArg!=2 ){
      eputz("Usage: .read FILE\n");
      rc = 1;
      goto meta_command_exit;
    }
    if( azArg[1][0]=='|' ){
#ifdef SQLITE_OMIT_POPEN
      eputz("Error: pipes are not supported in this OS\n");
      rc = 1;
#else
      p->in = sqlite3_popen(azArg[1]+1, "r");
      if( p->in==0 ){
        cli_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
        rc = 1;
      }else{
        rc = process_input(p, "<pipe>");
        pclose(p->in);
      }
#endif
    }else if( (p->in = openChrSource(azArg[1]))==0 ){
      cli_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
      rc = 1;
    }else{
      char *zFilename = strdup(azArg[1]);
      rc = process_input(p, zFilename);
      free(zFilename);
      fclose(p->in);
    }
    p->in = inSaved;
    p->lineno = savedLineno;
  }else
#endif /* !defined(SQLITE_SHELL_FIDDLE) */

#if SQLITE_SHELL_HAVE_RECOVER
  if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
    open_db(p, 0);
    rc = recoverDatabaseCmd(p, nArg, azArg);
  }else
#endif /* SQLITE_SHELL_HAVE_RECOVER */

#ifndef SQLITE_SHELL_FIDDLE
  if( c=='r' && n>=3 && cli_strncmp(azArg[0], "restore", n)==0 ){
    const char *zSrcFile;
    const char *zDb;
    sqlite3 *pSrc;
    sqlite3_backup *pBackup;
    int nTimeout = 0;

    failIfSafeMode(p, "cannot run .restore in safe mode");
    if( nArg==2 ){
      zSrcFile = azArg[1];
      zDb = "main";
    }else if( nArg==3 ){
      zSrcFile = azArg[2];
      zDb = azArg[1];
    }else{
      eputz("Usage: .restore ?DB? FILE\n");
      rc = 1;
      goto meta_command_exit;
    }
    rc = sqlite3_open(zSrcFile, &pSrc);
    if( rc!=SQLITE_OK ){
      cli_printf(stderr,"Error: cannot open \"%s\"\n", zSrcFile);
      close_db(pSrc);
      return 1;
    }
    open_db(p, 0);
    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
    if( pBackup==0 ){
      shellDatabaseError(p->db);
      close_db(pSrc);
      return 1;
    }
    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
          || rc==SQLITE_BUSY  ){
      if( rc==SQLITE_BUSY ){
        if( nTimeout++ >= 3 ) break;
        sqlite3_sleep(100);
      }
    }
    sqlite3_backup_finish(pBackup);
    if( rc==SQLITE_DONE ){
      rc = 0;
    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
      eputz("Error: source database is busy\n");
      rc = 1;
    }else{
      shellDatabaseError(p->db);
      rc = 1;
    }
    close_db(pSrc);
  }else
#endif /* !defined(SQLITE_SHELL_FIDDLE) */

  if( c=='s' &&
     (cli_strncmp(azArg[0], "scanstats",  n)==0 ||
      cli_strncmp(azArg[0], "scanstatus", n)==0)
  ){
    if( nArg==2 ){
      if( cli_strcmp(azArg[1], "vm")==0 ){
        p->mode.scanstatsOn = 3;
      }else
      if( cli_strcmp(azArg[1], "est")==0 ){
        p->mode.scanstatsOn = 2;
      }else{
        p->mode.scanstatsOn = (u8)booleanValue(azArg[1]);
      }
      open_db(p, 0);
      sqlite3_db_config(
          p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->mode.scanstatsOn, (int*)0
      );
#if !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
      eputz("Warning: .scanstats not available in this build.\n");
#elif !defined(SQLITE_ENABLE_BYTECODE_VTAB)
      if( p->mode.scanstatsOn==3 ){
        eputz("Warning: \".scanstats vm\" not available in this build.\n");
      }
#endif
    }else{
      eputz("Usage: .scanstats on|off|est\n");
      rc = 1;
    }
  }else

  if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){
    ShellState data;
    char *zErrMsg = 0;
    const char *zDiv = "(";
    const char *zName = 0;
    int iSchema = 0;
    int bDebug = 0;
    int bNoSystemTabs = 0;
    int bIndent = 0;
    int ii;
    sqlite3_str *pSql;
    sqlite3_stmt *pStmt = 0;
 
    open_db(p, 0);
    memcpy(&data, p, sizeof(data));
    data.mode.spec.bTitles = QRF_No;
    data.mode.eMode = MODE_List;
    data.mode.spec.eText = QRF_TEXT_Plain;
    data.mode.spec.nCharLimit = 0;
    data.mode.spec.zRowSep = "\n";
    for(ii=1; ii<nArg; ii++){
      if( optionMatch(azArg[ii],"indent") ){
        bIndent = 1;
      }else if( optionMatch(azArg[ii],"debug") ){
        bDebug = 1;
      }else if( optionMatch(azArg[ii],"nosys") ){
        bNoSystemTabs = 1;
      }else if( azArg[ii][0]=='-' ){
        cli_printf(stderr,"Unknown option: \"%s\"\n", azArg[ii]);
        rc = 1;
        goto meta_command_exit;
      }else if( zName==0 ){
        zName = azArg[ii];
      }else{
        eputz("Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
        rc = 1;
        goto meta_command_exit;
      }
    }
    if( zName!=0 ){
      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
      if( isSchema ){
        cli_printf(p->out,
                      "CREATE TABLE %ssqlite_schema (\n"
                      "  type text,\n"
                      "  name text,\n"
                      "  tbl_name text,\n"
                      "  rootpage integer,\n"
                      "  sql text\n"
                      ");\n",
               sqlite3_strlike("sqlite_t%",zName,0)==0 ? "temp." : ""
        );
      }
    }
    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
    if( rc ){
      shellDatabaseError(p->db);
      sqlite3_finalize(pStmt);
      
      rc = 1;
      goto meta_command_exit;
    }
    pSql = sqlite3_str_new(p->db);
    sqlite3_str_appendf(pSql, "SELECT sql FROM", 0);
    iSchema = 0;
    while( sqlite3_step(pStmt)==SQLITE_ROW ){
      const char *zDb = (const char*)sqlite3_column_text(pStmt, 1);
      char zScNum[30];
      sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
      sqlite3_str_appendall(pSql, zDiv);
      zDiv = " UNION ALL ";
      if( sqlite3_stricmp(zDb, "main")==0 ){
        sqlite3_str_appendf(pSql,
            "SELECT shell_format_schema(shell_add_schema(sql,NULL,name),%d)",
            bIndent);
      }else{
        sqlite3_str_appendf(pSql,
            "SELECT shell_format_schema(shell_add_schema(sql,%Q,name),%d)",
            zDb, bIndent);
      }
      sqlite3_str_appendf(pSql,
         " AS sql, type, tbl_name, name, rowid, %d AS snum, %Q as sname",
         ++iSchema, zDb);
      sqlite3_str_appendf(pSql," FROM \"%w\".sqlite_schema", zDb);
    }
    sqlite3_finalize(pStmt);
#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS) \
 && !defined(SQLITE_OMIT_VIRTUALTABLE)
    if( zName ){
      sqlite3_str_appendall(pSql,
         " UNION ALL SELECT shell_module_schema(name),"
         " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list");
    }
#endif
    sqlite3_str_appendf(pSql, ") WHERE ", 0);
    if( zName ){
      int bGlob;
      bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
              strchr(zName, '[') != 0;
      if( strchr(zName, '.') ){
        sqlite3_str_appendall(pSql, "lower(format('%%s.%%s',sname,tbl_name))");
      }else{
        sqlite3_str_appendall(pSql, "lower(tbl_name)");
      }
      if( bGlob ){
        sqlite3_str_appendf(pSql, " GLOB %Q AND ", zName);
      }else{
        sqlite3_str_appendf(pSql, " LIKE %Q ESCAPE '\\' AND ", zName);
      }
    }
    if( bNoSystemTabs ){
      sqlite3_str_appendf(pSql, " name NOT LIKE 'sqlite__%%' ESCAPE '_' AND ");
    }
    sqlite3_str_appendf(pSql, "sql IS NOT NULL ORDER BY snum, rowid");
    if( bDebug ){
      cli_printf(p->out, "SQL: %s;\n", sqlite3_str_value(pSql));
    }else{
      rc = shell_exec(&data, sqlite3_str_value(pSql), &zErrMsg);
    }
    sqlite3_str_free(pSql);

    if( zErrMsg ){
      shellEmitError(zErrMsg);
      sqlite3_free(zErrMsg);
      rc = 1;
    }else if( rc != SQLITE_OK ){
      eputz("Error: querying schema information\n");
      rc = 1;
    }else{
      rc = 0;
    }
  }else

  if( (c=='s' && n==11 && cli_strncmp(azArg[0], "selecttrace", n)==0)
   || (c=='t' && n==9  && cli_strncmp(azArg[0], "treetrace", n)==0)
  ){
    unsigned int x = nArg>=2? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
  }else

#if defined(SQLITE_ENABLE_SESSION)
  if( c=='s' && cli_strncmp(azArg[0],"session",n)==0 && n>=3 ){
    struct AuxDb *pAuxDb = p->pAuxDb;
    OpenSession *pSession = &pAuxDb->aSession[0];
    char **azCmd = &azArg[1];
    int iSes = 0;
    int nCmd = nArg - 1;
    int i;
    if( nArg<=1 ) goto session_syntax_error;
    open_db(p, 0);
    if( nArg>=3 ){
      for(iSes=0; iSes<pAuxDb->nSession; iSes++){
        if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
      }
      if( iSes<pAuxDb->nSession ){
        pSession = &pAuxDb->aSession[iSes];
        azCmd++;
        nCmd--;
      }else{
        pSession = &pAuxDb->aSession[0];
        iSes = 0;
      }
    }

    /* .session attach TABLE
    ** Invoke the sqlite3session_attach() interface to attach a particular
    ** table so that it is never filtered.
    */
    if( cli_strcmp(azCmd[0],"attach")==0 ){
      if( nCmd!=2 ) goto session_syntax_error;
      if( pSession->p==0 ){
        session_not_open:
        eputz("ERROR: No sessions are open\n");
      }else{
        rc = sqlite3session_attach(pSession->p, azCmd[1]);
        if( rc ){
          cli_printf(stderr,
               "ERROR: sqlite3session_attach() returns %d\n",rc);
          rc = 0;
        }
      }
    }else

    /* .session changeset FILE
    ** .session patchset FILE
    ** Write a changeset or patchset into a file.  The file is overwritten.
    */
    if( cli_strcmp(azCmd[0],"changeset")==0
     || cli_strcmp(azCmd[0],"patchset")==0
    ){
      FILE *out = 0;
      failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
      if( nCmd!=2 ) goto session_syntax_error;
      if( pSession->p==0 ) goto session_not_open;
      out = sqlite3_fopen(azCmd[1], "wb");
      if( out==0 ){
        cli_printf(stderr,"ERROR: cannot open \"%s\" for writing\n",
              azCmd[1]);
      }else{
        int szChng;
        void *pChng;
        if( azCmd[0][0]=='c' ){
          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
        }else{
          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
        }
        if( rc ){
          cli_printf(stdout, "Error: error code %d\n", rc);
          rc = 0;
        }
        if( pChng
          && fwrite(pChng, szChng, 1, out)!=1 ){
          cli_printf(stderr,
              "ERROR: Failed to write entire %d-byte output\n", szChng);
        }
        sqlite3_free(pChng);
        fclose(out);
      }
    }else

    /* .session close
    ** Close the identified session
    */
    if( cli_strcmp(azCmd[0], "close")==0 ){
      if( nCmd!=1 ) goto session_syntax_error;
      if( pAuxDb->nSession ){
        session_close(pSession);
        pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
      }
    }else

    /* .session enable ?BOOLEAN?
    ** Query or set the enable flag
    */
    if( cli_strcmp(azCmd[0], "enable")==0 ){
      int ii;
      if( nCmd>2 ) goto session_syntax_error;
      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
      if( pAuxDb->nSession ){
        ii = sqlite3session_enable(pSession->p, ii);
        cli_printf(p->out,
            "session %s enable flag = %d\n", pSession->zName, ii);
      }
    }else

    /* .session filter GLOB ....
    ** Set a list of GLOB patterns of table names to be excluded.
    */
    if( cli_strcmp(azCmd[0], "filter")==0 ){
      int ii;
      i64 nByte;
      if( nCmd<2 ) goto session_syntax_error;
      if( pAuxDb->nSession ){
        for(ii=0; ii<pSession->nFilter; ii++){
          sqlite3_free(pSession->azFilter[ii]);
        }
        sqlite3_free(pSession->azFilter);
        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
        pSession->azFilter = sqlite3_malloc64( nByte );
        shell_check_oom( pSession->azFilter );
        for(ii=1; ii<nCmd; ii++){
          char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
          shell_check_oom(x);
        }
        pSession->nFilter = ii-1;
      }
    }else

    /* .session indirect ?BOOLEAN?
    ** Query or set the indirect flag
    */
    if( cli_strcmp(azCmd[0], "indirect")==0 ){
      int ii;
      if( nCmd>2 ) goto session_syntax_error;
      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
      if( pAuxDb->nSession ){
        ii = sqlite3session_indirect(pSession->p, ii);
        cli_printf(p->out,
            "session %s indirect flag = %d\n", pSession->zName, ii);
      }
    }else

    /* .session isempty
    ** Determine if the session is empty
    */
    if( cli_strcmp(azCmd[0], "isempty")==0 ){
      int ii;
      if( nCmd!=1 ) goto session_syntax_error;
      if( pAuxDb->nSession ){
        ii = sqlite3session_isempty(pSession->p);
        cli_printf(p->out,
             "session %s isempty flag = %d\n", pSession->zName, ii);
      }
    }else

    /* .session list
    ** List all currently open sessions
    */
    if( cli_strcmp(azCmd[0],"list")==0 ){
      for(i=0; i<pAuxDb->nSession; i++){
        cli_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
      }
    }else

    /* .session open DB NAME
    ** Open a new session called NAME on the attached database DB.
    ** DB is normally "main".
    */
    if( cli_strcmp(azCmd[0],"open")==0 ){
      char *zName;
      if( nCmd!=3 ) goto session_syntax_error;
      zName = azCmd[2];
      if( zName[0]==0 ) goto session_syntax_error;
      for(i=0; i<pAuxDb->nSession; i++){
        if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
          cli_printf(stderr,"Session \"%s\" already exists\n", zName);
          goto meta_command_exit;
        }
      }
      if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
        cli_printf(stderr,
           "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
        goto meta_command_exit;
      }
      pSession = &pAuxDb->aSession[pAuxDb->nSession];
      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
      if( rc ){
        cli_printf(stderr,"Cannot open session: error code=%d\n", rc);
        rc = 0;
        goto meta_command_exit;
      }
      pSession->nFilter = 0;
      sqlite3session_table_filter(pSession->p, session_filter, pSession);
      pAuxDb->nSession++;
      pSession->zName = sqlite3_mprintf("%s", zName);
      shell_check_oom(pSession->zName);
    }else
    /* If no command name matches, show a syntax error */
    session_syntax_error:
    showHelp(p->out, "session");
  }else
#endif

#ifdef SQLITE_DEBUG
  /* Undocumented commands for internal testing.  Subject to change
  ** without notice. */
  if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
    if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
      int i, v;
      for(i=1; i<nArg; i++){
        v = booleanValue(azArg[i]);
        cli_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
      }
    }
    if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
      int i; sqlite3_int64 v;
      for(i=1; i<nArg; i++){
        char zBuf[200];
        v = integerValue(azArg[i]);
        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
        cli_puts(zBuf, p->out);
      }
    }
  }else
#endif

  if( c=='s' && n>=4 && cli_strncmp(azArg[0],"selftest",n)==0 ){
    int bIsInit = 0;         /* True to initialize the SELFTEST table */
    int bVerbose = 0;        /* Verbose output */
    int bSelftestExists;     /* True if SELFTEST already exists */
    int i, k;                /* Loop counters */
    int nTest = 0;           /* Number of tests runs */
    int nErr = 0;            /* Number of errors seen */
    ShellText str;           /* Answer for a query */
    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */

    open_db(p,0);
    for(i=1; i<nArg; i++){
      const char *z = azArg[i];
      if( z[0]=='-' && z[1]=='-' ) z++;
      if( cli_strcmp(z,"-init")==0 ){
        bIsInit = 1;
      }else
      if( cli_strcmp(z,"-v")==0 ){
        bVerbose++;
      }else
      {
        cli_printf(stderr,
              "Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
        cli_puts("Should be one of: --init -v\n", stderr);
        rc = 1;
        goto meta_command_exit;
      }
    }
    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
           != SQLITE_OK ){
      bSelftestExists = 0;
    }else{
      bSelftestExists = 1;
    }
    if( bIsInit ){
      createSelftestTable(p);
      bSelftestExists = 1;
    }
    initText(&str);
    appendText(&str, "x", 0);
    for(k=bSelftestExists; k>=0; k--){
      if( k==1 ){
        rc = sqlite3_prepare_v2(p->db,
            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
            -1, &pStmt, 0);
      }else{
        rc = sqlite3_prepare_v2(p->db,
          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
          "      (1,'run','PRAGMA integrity_check','ok')",
          -1, &pStmt, 0);
      }
      if( rc ){
        eputz("Error querying the selftest table\n");
        rc = 1;
        sqlite3_finalize(pStmt);
        goto meta_command_exit;
      }
      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
        int tno = sqlite3_column_int(pStmt, 0);
        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);

        if( zOp==0 ) continue;
        if( zSql==0 ) continue;
        if( zAns==0 ) continue;
        k = 0;
        if( bVerbose>0 ){
          cli_printf(stdout, "%d: %s %s\n", tno, zOp, zSql);
        }
        if( cli_strcmp(zOp,"memo")==0 ){
          cli_printf(p->out, "%s\n", zSql);
        }else
        if( cli_strcmp(zOp,"run")==0 ){
          char *zErrMsg = 0;
          str.n = 0;
          str.zTxt[0] = 0;
          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
          nTest++;
          if( bVerbose ){
            cli_printf(p->out, "Result: %s\n", str.zTxt);
          }
          if( rc || zErrMsg ){
            nErr++;
            rc = 1;
            cli_printf(p->out, "%d: error-code-%d: %s\n", tno, rc,zErrMsg);
            sqlite3_free(zErrMsg);
          }else if( cli_strcmp(zAns,str.zTxt)!=0 ){
            nErr++;
            rc = 1;
            cli_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
            cli_printf(p->out, "%d:      Got: [%s]\n", tno, str.zTxt);
          }
        }
        else{
          cli_printf(stderr,
                "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
          rc = 1;
          break;
        }
      } /* End loop over rows of content from SELFTEST */
      sqlite3_finalize(pStmt);
    } /* End loop over k */
    freeText(&str);
    cli_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
  }else

  if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
    if( nArg<2 || nArg>3 ){
      eputz("Usage: .separator COL ?ROW?\n");
      rc = 1;
    }
    if( nArg>=2 ){
      modeSetStr(&p->mode.spec.zColumnSep, azArg[1]);
    }
    if( nArg>=3 ){
      modeSetStr(&p->mode.spec.zRowSep,azArg[2]);
    }
  }else

  if( c=='s' && n>=4 && cli_strncmp(azArg[0],"sha3sum",n)==0 ){
    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
    int i;                   /* Loop counter */
    int bSchema = 0;         /* Also hash the schema */
    int bSeparate = 0;       /* Hash each table separately */
    int iSize = 224;         /* Hash algorithm to use */
    int bDebug = 0;          /* Only show the query that would have run */
    sqlite3_stmt *pStmt;     /* For querying tables names */
    char *zSql;              /* SQL to be run */
    char *zSep;              /* Separator */
    ShellText sSql;          /* Complete SQL for the query to run the hash */
    ShellText sQuery;        /* Set of queries used to read all content */
    open_db(p, 0);
    for(i=1; i<nArg; i++){
      const char *z = azArg[i];
      if( z[0]=='-' ){
        z++;
        if( z[0]=='-' ) z++;
        if( cli_strcmp(z,"schema")==0 ){
          bSchema = 1;
        }else
        if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0
         || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0
        ){
          iSize = atoi(&z[5]);
        }else
        if( cli_strcmp(z,"debug")==0 ){
          bDebug = 1;
        }else
        {
          cli_printf(stderr,
                  "Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
          showHelp(p->out, azArg[0]);
          rc = 1;
          goto meta_command_exit;
        }
      }else if( zLike ){
        eputz("Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
        rc = 1;
        goto meta_command_exit;
      }else{
        zLike = z;
        bSeparate = 1;
        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
      }
    }
    if( bSchema ){
      zSql = "SELECT lower(name) as tname FROM sqlite_schema"
             " WHERE type='table' AND coalesce(rootpage,0)>1"
             " UNION ALL SELECT 'sqlite_schema'"
             " ORDER BY 1 collate nocase";
    }else{
      zSql = "SELECT lower(name) as tname FROM sqlite_schema"
             " WHERE type='table' AND coalesce(rootpage,0)>1"
             " AND name NOT LIKE 'sqlite__%' ESCAPE '_'"
             " ORDER BY 1 collate nocase";
    }
    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
    initText(&sQuery);
    initText(&sSql);
    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
    zSep = "VALUES(";
    while( SQLITE_ROW==sqlite3_step(pStmt) ){
      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
      if( zTab==0 ) continue;
      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
      if( cli_strncmp(zTab, "sqlite_",7)!=0 ){
        appendText(&sQuery,"SELECT * FROM ", 0);
        appendText(&sQuery,zTab,'"');
        appendText(&sQuery," NOT INDEXED;", 0);
      }else if( cli_strcmp(zTab, "sqlite_schema")==0 ){
        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
                           " ORDER BY name;", 0);
      }else if( cli_strcmp(zTab, "sqlite_sequence")==0 ){
        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
                           " ORDER BY name;", 0);
      }else if( cli_strcmp(zTab, "sqlite_stat1")==0 ){
        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
                           " ORDER BY tbl,idx;", 0);
      }else if( cli_strcmp(zTab, "sqlite_stat4")==0 ){
        appendText(&sQuery, "SELECT * FROM ", 0);
        appendText(&sQuery, zTab, 0);
        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
      }
      appendText(&sSql, zSep, 0);
      appendText(&sSql, sQuery.zTxt, '\'');
      sQuery.n = 0;
      appendText(&sSql, ",", 0);
      appendText(&sSql, zTab, '\'');
      zSep = "),(";
    }
    sqlite3_finalize(pStmt);
    if( bSeparate ){
      zSql = sqlite3_mprintf(
          "%s))"
          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
          "   FROM [sha3sum$query]",
          sSql.zTxt, iSize);
    }else{
      zSql = sqlite3_mprintf(
          "%s))"
          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
          "   FROM [sha3sum$query]",
          sSql.zTxt, iSize);
    }
    shell_check_oom(zSql);
    freeText(&sQuery);
    freeText(&sSql);
    if( bDebug ){
      cli_printf(p->out, "%s\n", zSql);
    }else{
      shell_exec(p, zSql, 0);
    }
#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && !defined(SQLITE_OMIT_VIRTUALTABLE)
    {
      int lrc;
      char *zRevText = /* Query for reversible to-blob-to-text check */
        "SELECT lower(name) as tname FROM sqlite_schema\n"
        "WHERE type='table' AND coalesce(rootpage,0)>1\n"
        "AND name NOT LIKE 'sqlite__%%' ESCAPE '_'%s\n"
        "ORDER BY 1 collate nocase";
      zRevText = sqlite3_mprintf(zRevText, zLike? " AND name LIKE $tspec" : "");
      zRevText = sqlite3_mprintf(
          /* lower-case query is first run, producing upper-case query. */
          "with tabcols as materialized(\n"
          "select tname, cname\n"
          "from ("
          " select printf('\"%%w\"',ss.tname) as tname,"
          " printf('\"%%w\"',ti.name) as cname\n"
          " from (%z) ss\n inner join pragma_table_info(tname) ti))\n"
          "select 'SELECT total(bad_text_count) AS bad_text_count\n"
          "FROM ('||group_concat(query, ' UNION ALL ')||')' as btc_query\n"
          " from (select 'SELECT COUNT(*) AS bad_text_count\n"
          "FROM '||tname||' WHERE '\n"
          "||group_concat('CAST(CAST('||cname||' AS BLOB) AS TEXT)<>'||cname\n"
          "|| ' AND typeof('||cname||')=''text'' ',\n"
          "' OR ') as query, tname from tabcols group by tname)"
          , zRevText);
      shell_check_oom(zRevText);
      if( bDebug ) cli_printf(p->out, "%s\n", zRevText);
      lrc = sqlite3_prepare_v2(p->db, zRevText, -1, &pStmt, 0);
      if( lrc!=SQLITE_OK ){
        /* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the
        ** user does cruel and unnatural things like ".limit expr_depth 0". */
        rc = 1;
      }else{
        if( zLike ) sqlite3_bind_text(pStmt,1,zLike,-1,SQLITE_STATIC);
        lrc = SQLITE_ROW==sqlite3_step(pStmt);
        if( lrc ){
          const char *zGenQuery = (char*)sqlite3_column_text(pStmt,0);
          sqlite3_stmt *pCheckStmt;
          lrc = sqlite3_prepare_v2(p->db, zGenQuery, -1, &pCheckStmt, 0);
          if( bDebug ) cli_printf(p->out, "%s\n", zGenQuery);
          if( lrc!=SQLITE_OK ){
            rc = 1;
          }else{
            if( SQLITE_ROW==sqlite3_step(pCheckStmt) ){
              double countIrreversible = sqlite3_column_double(pCheckStmt, 0);
              if( countIrreversible>0 ){
                int sz = (int)(countIrreversible + 0.5);
                cli_printf(stderr,
                      "Digest includes %d invalidly encoded text field%s.\n",
                      sz, (sz>1)? "s": "");
              }
            }
            sqlite3_finalize(pCheckStmt);
          }
          sqlite3_finalize(pStmt);
        }
      }
      if( rc ) eputz(".sha3sum failed.\n");
      sqlite3_free(zRevText);
    }
#endif /* !defined(*_OMIT_SCHEMA_PRAGMAS) && !defined(*_OMIT_VIRTUALTABLE) */
    sqlite3_free(zSql);
  }else

#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
  if( c=='s'
   && (cli_strncmp(azArg[0], "shell", n)==0
       || cli_strncmp(azArg[0],"system",n)==0)
  ){
    char *zCmd;
    int i, x;
    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
    if( nArg<2 ){
      eputz("Usage: .system COMMAND\n");
      rc = 1;
      goto meta_command_exit;
    }
    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
    for(i=2; i<nArg && zCmd!=0; i++){
      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
                             zCmd, azArg[i]);
    }
    /*consoleRestore();*/
    x = zCmd!=0 ? system(zCmd) : 1;
    /*consoleRenewSetup();*/
    sqlite3_free(zCmd);
    if( x ) cli_printf(stderr,"System command returns %d\n", x);
  }else
#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */

  if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
    static const char *azBool[] = { "off", "on", "trigger", "full"};
    const char *zOut;
    int i;
    if( nArg!=1 ){
      eputz("Usage: .show\n");
      rc = 1;
      goto meta_command_exit;
    }
    cli_printf(p->out, "%12.12s: %s\n","echo", 
            azBool[(p->mode.mFlags & MFLG_ECHO)!=0]);
    cli_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->mode.autoEQP&3]);
    cli_printf(p->out, "%12.12s: %s\n","explain",
                             p->mode.autoExplain ? "auto" : "off");
    cli_printf(p->out, "%12.12s: %s\n","headers",
          azBool[p->mode.spec.bTitles==QRF_Yes]);
    if( p->mode.spec.eStyle==QRF_STYLE_Column
     || p->mode.spec.eStyle==QRF_STYLE_Box
     || p->mode.spec.eStyle==QRF_STYLE_Table
     || p->mode.spec.eStyle==QRF_STYLE_Markdown
    ){
      cli_printf(p->out,
            "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
            aModeInfo[p->mode.eMode].zName, p->mode.spec.nWrap,
            p->mode.spec.bWordWrap==QRF_Yes ? "on" : "off",
            p->mode.spec.eText==QRF_TEXT_Sql ? "" : "no");
    }else{
      cli_printf(p->out, "%12.12s: %s\n","mode", 
                 aModeInfo[p->mode.eMode].zName);
    }
    cli_printf(p->out, "%12.12s: ", "nullvalue");
    output_c_string(p->out, p->mode.spec.zNull);
    cli_puts("\n", p->out);
    cli_printf(p->out, "%12.12s: %s\n","output",
          strlen30(p->outfile) ? p->outfile : "stdout");
    cli_printf(p->out, "%12.12s: ", "colseparator");
    output_c_string(p->out, p->mode.spec.zColumnSep);
    cli_puts("\n", p->out);
    cli_printf(p->out, "%12.12s: ", "rowseparator");
    output_c_string(p->out, p->mode.spec.zRowSep);
    cli_puts("\n", p->out);
    switch( p->statsOn ){
      case 0:  zOut = "off";     break;
      default: zOut = "on";      break;
      case 2:  zOut = "stmt";    break;
      case 3:  zOut = "vmstep";  break;
    }
    cli_printf(p->out, "%12.12s: %s\n","stats", zOut);
    cli_printf(p->out, "%12.12s: ", "width");
    for(i=0; i<p->mode.spec.nWidth; i++){
      cli_printf(p->out, "%d ", (int)p->mode.spec.aWidth[i]);
    }
    cli_puts("\n", p->out);
    cli_printf(p->out, "%12.12s: %s\n", "filename",
          p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
  }else

  if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
    if( nArg==2 ){
      if( cli_strcmp(azArg[1],"stmt")==0 ){
        p->statsOn = 2;
      }else if( cli_strcmp(azArg[1],"vmstep")==0 ){
        p->statsOn = 3;
      }else{
        p->statsOn = (u8)booleanValue(azArg[1]);
      }
    }else if( nArg==1 ){
      display_stats(p->db, p, 0);
    }else{
      eputz("Usage: .stats ?on|off|stmt|vmstep?\n");
      rc = 1;
    }
  }else

  if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0) ){
    sqlite3_stmt *pStmt;
    sqlite3_str *pSql;
    const char *zPattern = nArg>1 ? azArg[1] : 0;

    open_db(p, 0);
    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
    if( rc ){
      sqlite3_finalize(pStmt);
      return shellDatabaseError(p->db);
    }

    pSql = sqlite3_str_new(p->db);
    while( sqlite3_step(pStmt)==SQLITE_ROW ){
      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
      if( zDbName==0 ) continue;
      if( sqlite3_str_length(pSql) ){
        sqlite3_str_appendall(pSql, " UNION ALL ");
      }
      if( sqlite3_stricmp(zDbName, "main")==0 ){
        sqlite3_str_appendall(pSql, "SELECT name FROM ");
      }else{
        sqlite3_str_appendf(pSql, "SELECT %Q||'.'||name FROM ", zDbName);
      }
      sqlite3_str_appendf(pSql, "\"%w\".sqlite_schema", zDbName);
      sqlite3_str_appendf(pSql,
          " WHERE type IN ('table','view')"
          "   AND name NOT LIKE 'sqlite__%%' ESCAPE '_'"
      );
      if( zPattern ){
        sqlite3_str_appendf(pSql," AND name LIKE %Q", zPattern);
      }
    }
    rc = sqlite3_finalize(pStmt);
    if( rc==SQLITE_OK ){
      sqlite3_str_appendall(pSql, " ORDER BY 1");
    }

    /* Run the SQL statement in "split" mode. */
    modePush(p);
    modeChange(p, MODE_Split);
    shell_exec(p, sqlite3_str_value(pSql), 0);
    sqlite3_str_free(pSql);
    modePop(p);
    if( rc ) return shellDatabaseError(p->db);
  }else

  /* Set the p->zTestcase name and begin redirecting output into
  ** the cli_output_capture sqlite3_str */
  if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){
    rc = dotCmdTestcase(p);
  }else

#ifndef SQLITE_UNTESTABLE
  if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){
    static const struct {
       const char *zCtrlName;   /* Name of a test-control option */
       int ctrlCode;            /* Integer code for that option */
       int unSafe;              /* Not valid unless --unsafe-testing */
       const char *zUsage;      /* Usage notes */
    } aCtrl[] = {
    {"always",             SQLITE_TESTCTRL_ALWAYS, 1,     "BOOLEAN"         },
    {"assert",             SQLITE_TESTCTRL_ASSERT, 1,     "BOOLEAN"         },
  /*{"benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, ""        },*/
    {"bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST, 1, "SIZE INT-ARRAY"},
    {"byteorder",          SQLITE_TESTCTRL_BYTEORDER, 0,  ""                },
    {"extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN"  },
    {"fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, 1,"args..."       },
    {"fk_no_action",       SQLITE_TESTCTRL_FK_NO_ACTION, 0, "BOOLEAN"       },
    {"imposter",         SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"},
    {"internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,""          },
    {"json_selfcheck",     SQLITE_TESTCTRL_JSON_SELFCHECK ,0,"BOOLEAN"      },
    {"localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN"      },
    {"never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN"       },
    {"optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK ..."},
#ifdef YYCOVERAGE
    {"parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE,0,""             },
#endif
    {"pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,1, "OFFSET  "       },
    {"prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,0, ""               },
    {"prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,   0, ""               },
    {"prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,   0, "SEED ?db?"      },
    {"seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,  0, ""               },
    {"sorter_mmap",        SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX"           },
    {"tune",               SQLITE_TESTCTRL_TUNE,        1, "ID VALUE"       },
    };
    int testctrl = -1;
    int iCtrl = -1;
    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
    int isOk = 0;
    int i, n2;
    const char *zCmd = 0;

    open_db(p, 0);
    zCmd = nArg>=2 ? azArg[1] : "help";

    /* The argument can optionally begin with "-" or "--" */
    if( zCmd[0]=='-' && zCmd[1] ){
      zCmd++;
      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
    }

    /* --help lists all test-controls */
    if( cli_strcmp(zCmd,"help")==0 ){
      cli_puts("Available test-controls:\n", p->out);
      for(i=0; i<ArraySize(aCtrl); i++){
        if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
        cli_printf(p->out, "  .testctrl %s %s\n",
              aCtrl[i].zCtrlName, aCtrl[i].zUsage);
      }
      rc = 1;
      goto meta_command_exit;
    }

    /* convert testctrl text option to value. allow any unique prefix
    ** of the option name, or a numerical value. */
    n2 = strlen30(zCmd);
    for(i=0; i<ArraySize(aCtrl); i++){
      if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
      if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
        if( testctrl<0 ){
          testctrl = aCtrl[i].ctrlCode;
          iCtrl = i;
        }else{
          cli_printf(stderr,"Error: ambiguous test-control: \"%s\"\n"
                "Use \".testctrl --help\" for help\n", zCmd);
          rc = 1;
          goto meta_command_exit;
        }
      }
    }
    if( testctrl<0 ){
      cli_printf(stderr,"Error: unknown test-control: %s\n"
            "Use \".testctrl --help\" for help\n", zCmd);
    }else{
      switch(testctrl){

        /* Special processing for .testctrl opt MASK ...
        ** Each MASK argument can be one of:
        **
        **      +LABEL       Enable the named optimization 
        **
        **      -LABEL       Disable the named optimization
        **
        **      INTEGER      Mask of optimizations to disable
        */
        case SQLITE_TESTCTRL_OPTIMIZATIONS: {
          static const struct {
             unsigned int mask;    /* Mask for this optimization */
             unsigned int bDsply;  /* Display this on output */
             const char *zLabel;   /* Name of optimization */
          } aLabel[] = {
            { 0x00000001, 1, "QueryFlattener" },
            { 0x00000001, 0, "Flatten" },
            { 0x00000002, 1, "WindowFunc" },
            { 0x00000004, 1, "GroupByOrder" },
            { 0x00000008, 1, "FactorOutConst" },
            { 0x00000010, 1, "DistinctOpt" },
            { 0x00000020, 1, "CoverIdxScan" },
            { 0x00000040, 1, "OrderByIdxJoin" },
            { 0x00000080, 1, "Transitive" },
            { 0x00000100, 1, "OmitNoopJoin" },
            { 0x00000200, 1, "CountOfView" },
            { 0x00000400, 1, "CursorHints" },
            { 0x00000800, 1, "Stat4" },
            { 0x00001000, 1, "PushDown" },
            { 0x00002000, 1, "SimplifyJoin" },
            { 0x00004000, 1, "SkipScan" },
            { 0x00008000, 1, "PropagateConst" },
            { 0x00010000, 1, "MinMaxOpt" },
            { 0x00020000, 1, "SeekScan" },
            { 0x00040000, 1, "OmitOrderBy" },
            { 0x00080000, 1, "BloomFilter" },
            { 0x00100000, 1, "BloomPulldown" },
            { 0x00200000, 1, "BalancedMerge" },
            { 0x00400000, 1, "ReleaseReg" },
            { 0x00800000, 1, "FlttnUnionAll" },
            { 0x01000000, 1, "IndexedEXpr" },
            { 0x02000000, 1, "Coroutines" },
            { 0x04000000, 1, "NullUnusedCols" },
            { 0x08000000, 1, "OnePass" },
            { 0x10000000, 1, "OrderBySubq" },
            { 0x20000000, 1, "StarQuery" },
            { 0x40000000, 1, "ExistsToJoin" },
            { 0xffffffff, 0, "All" },
          };
          unsigned int curOpt;
          unsigned int newOpt;
          unsigned int m;
          int ii;
          int nOff;
          sqlite3_test_control(SQLITE_TESTCTRL_GETOPT, p->db, &curOpt);
          newOpt = curOpt;
          for(ii=2; ii<nArg; ii++){
            const char *z = azArg[ii];
            int useLabel = 0;
            const char *zLabel = 0;
            if( (z[0]=='+'|| z[0]=='-') && !IsDigit(z[1]) ){
              useLabel = z[0];
              zLabel = &z[1];
            }else if( !IsDigit(z[0]) && z[0]!=0 && !IsDigit(z[1]) ){
              useLabel = '+';
              zLabel = z;
            }else{
              newOpt = (unsigned int)strtol(z,0,0);
            }
            if( useLabel ){
              int jj;
              for(jj=0; jj<ArraySize(aLabel); jj++){
                if( sqlite3_stricmp(zLabel, aLabel[jj].zLabel)==0 ) break;
              }
              if( jj>=ArraySize(aLabel) ){
                cli_printf(stderr,
                    "Error: no such optimization: \"%s\"\n", zLabel);
                cli_puts("Should be one of:", stderr);
                for(jj=0; jj<ArraySize(aLabel); jj++){
                  cli_printf(stderr," %s", aLabel[jj].zLabel);
                }
                cli_puts("\n", stderr);
                rc = 1;
                goto meta_command_exit;
              }
              if( useLabel=='+' ){
                newOpt &= ~aLabel[jj].mask;
              }else{
                newOpt |= aLabel[jj].mask;
              }
            }
          }
          if( curOpt!=newOpt ){
            sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,p->db,newOpt);
          }
          for(ii=nOff=0, m=1; ii<32; ii++, m <<= 1){
            if( m & newOpt ) nOff++;
          }
          if( nOff<12 ){
            cli_puts("+All", p->out);
            for(ii=0; ii<ArraySize(aLabel); ii++){
              if( !aLabel[ii].bDsply  ) continue;
              if( (newOpt & aLabel[ii].mask)!=0 ){
                cli_printf(p->out, " -%s", aLabel[ii].zLabel);
              }
            }
          }else{
            cli_puts("-All", p->out);
            for(ii=0; ii<ArraySize(aLabel); ii++){
              if( !aLabel[ii].bDsply  ) continue;
              if( (newOpt & aLabel[ii].mask)==0 ){
                cli_printf(p->out, " +%s", aLabel[ii].zLabel);
              }
            }
          }
          cli_puts("\n", p->out);
          rc2 = isOk = 3;
          break;
        }

        /* sqlite3_test_control(int, db, int) */
        case SQLITE_TESTCTRL_FK_NO_ACTION:
          if( nArg==3 ){
            unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
            rc2 = sqlite3_test_control(testctrl, p->db, opt);
            isOk = 3;
          }
          break;

        /* sqlite3_test_control(int) */
        case SQLITE_TESTCTRL_PRNG_SAVE:
        case SQLITE_TESTCTRL_PRNG_RESTORE:
        case SQLITE_TESTCTRL_BYTEORDER:
          if( nArg==2 ){
            rc2 = sqlite3_test_control(testctrl);
            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
          }
          break;

        /* sqlite3_test_control(int, uint) */
        case SQLITE_TESTCTRL_PENDING_BYTE:
          if( nArg==3 ){
            unsigned int opt = (unsigned int)integerValue(azArg[2]);
            rc2 = sqlite3_test_control(testctrl, opt);
            isOk = 3;
          }
          break;

        /* sqlite3_test_control(int, int, sqlite3*) */
        case SQLITE_TESTCTRL_PRNG_SEED:
          if( nArg==3 || nArg==4 ){
            int ii = (int)integerValue(azArg[2]);
            sqlite3 *db;
            if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
              sqlite3_randomness(sizeof(ii),&ii);
              cli_printf(stdout, "-- random seed: %d\n", ii);
            }
            if( nArg==3 ){
              db = 0;
            }else{
              db = p->db;
              /* Make sure the schema has been loaded */
              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
            }
            rc2 = sqlite3_test_control(testctrl, ii, db);
            isOk = 3;
          }
          break;

        /* sqlite3_test_control(int, int) */
        case SQLITE_TESTCTRL_ASSERT:
        case SQLITE_TESTCTRL_ALWAYS:
          if( nArg==3 ){
            int opt = booleanValue(azArg[2]);
            rc2 = sqlite3_test_control(testctrl, opt);
            isOk = 1;
          }
          break;

        /* sqlite3_test_control(int, int) */
        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
        case SQLITE_TESTCTRL_NEVER_CORRUPT:
          if( nArg==3 ){
            int opt = booleanValue(azArg[2]);
            rc2 = sqlite3_test_control(testctrl, opt);
            isOk = 3;
          }
          break;

        /* sqlite3_test_control(sqlite3*) */
        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
          rc2 = sqlite3_test_control(testctrl, p->db);
          isOk = 3;
          break;

        case SQLITE_TESTCTRL_IMPOSTER:
          if( nArg==5 ){
            rc2 = sqlite3_test_control(testctrl, p->db,
                          azArg[2],
                          integerValue(azArg[3]),
                          integerValue(azArg[4]));
            isOk = 3;
          }
          break;

        case SQLITE_TESTCTRL_SEEK_COUNT: {
          u64 x = 0;
          rc2 = sqlite3_test_control(testctrl, p->db, &x);
          cli_printf(p->out, "%llu\n", x);
          isOk = 3;
          break;
        }

#ifdef YYCOVERAGE
        case SQLITE_TESTCTRL_PARSER_COVERAGE: {
          if( nArg==2 ){
            sqlite3_test_control(testctrl, p->out);
            isOk = 3;
          }
          break;
        }
#endif
#ifdef SQLITE_DEBUG
        case SQLITE_TESTCTRL_TUNE: {
          if( nArg==4 ){
            int id = (int)integerValue(azArg[2]);
            int val = (int)integerValue(azArg[3]);
            sqlite3_test_control(testctrl, id, &val);
            isOk = 3;
          }else if( nArg==3 ){
            int id = (int)integerValue(azArg[2]);
            sqlite3_test_control(testctrl, -id, &rc2);
            isOk = 1;
          }else if( nArg==2 ){
            int id = 1;
            while(1){
              int val = 0;
              rc2 = sqlite3_test_control(testctrl, -id, &val);
              if( rc2!=SQLITE_OK ) break;
              if( id>1 ) cli_puts("  ", p->out);
              cli_printf(p->out, "%d: %d", id, val);
              id++;
            }
            if( id>1 ) cli_puts("\n", p->out);
            isOk = 3;
          }
          break;
        }
#endif
        case SQLITE_TESTCTRL_SORTER_MMAP:
          if( nArg==3 ){
            int opt = (unsigned int)integerValue(azArg[2]);
            rc2 = sqlite3_test_control(testctrl, p->db, opt);
            isOk = 3;
          }
          break;
        case SQLITE_TESTCTRL_JSON_SELFCHECK:
          if( nArg==2 ){
            rc2 = -1;
            isOk = 1;
          }else{
            rc2 = booleanValue(azArg[2]);
            isOk = 3;
          }
          sqlite3_test_control(testctrl, &rc2);
          break;
        case SQLITE_TESTCTRL_BITVEC_TEST: {
          /* Examples:
          **   .testctrl bitvec_test 100   6,1       -- Show BITVEC constants
          **   .testctrl bitvec_test 1000  1,12,7,3  -- Simple test
          **                         ----  --------
          **      size of Bitvec -----^        ^---  aOp array. 0 added at end.
          **
          ** See comments on sqlite3BitvecBuiltinTest() for more information
          ** about the aOp[] array.
          */
          int iSize;
          const char *zTestArg;
          int nOp;
          int ii, jj, x;
          int *aOp;
          if( nArg!=4 ){
            cli_printf(stderr,
              "ERROR - should be:  \".testctrl bitvec_test SIZE  INT-ARRAY\"\n"
            );
            rc = 1;
            goto meta_command_exit;
          }
          isOk = 3;
          iSize = (int)integerValue(azArg[2]);
          zTestArg = azArg[3];
          nOp = (int)strlen(zTestArg)+1;
          aOp = malloc( sizeof(int)*(nOp+1) );
          shell_check_oom(aOp);
          memset(aOp, 0, sizeof(int)*(nOp+1) );
          for(ii = jj = x = 0; zTestArg[ii]!=0; ii++){
            if( IsDigit(zTestArg[ii]) ){
              x = x*10 + zTestArg[ii] - '0';
            }else{
              aOp[jj++] = x;
              x = 0;
            }
          }
          aOp[jj] = x;
          x = sqlite3_test_control(testctrl, iSize, aOp);
          cli_printf(p->out, "result: %d\n", x);
          free(aOp);
          break;
        }
        case SQLITE_TESTCTRL_FAULT_INSTALL: {
          int kk;
          int bShowHelp = nArg<=2;
          isOk = 3;
          for(kk=2; kk<nArg; kk++){
            const char *z = azArg[kk];
            if( z[0]=='-' && z[1]=='-' ) z++;
            if( cli_strcmp(z,"off")==0 ){
              sqlite3_test_control(testctrl, 0);
            }else if( cli_strcmp(z,"on")==0 ){
              faultsim_state.iCnt = faultsim_state.nSkip;
              if( faultsim_state.iErr==0 ) faultsim_state.iErr = 1;
              faultsim_state.nHit = 0;
              sqlite3_test_control(testctrl, faultsim_callback);
            }else if( cli_strcmp(z,"reset")==0 ){
              faultsim_state.iCnt = faultsim_state.nSkip;
              faultsim_state.nHit = 0;
              sqlite3_test_control(testctrl, faultsim_callback);
            }else if( cli_strcmp(z,"status")==0 ){
              cli_printf(p->out, "faultsim.iId:       %d\n",
                              faultsim_state.iId);
              cli_printf(p->out, "faultsim.iErr:      %d\n",
                              faultsim_state.iErr);
              cli_printf(p->out, "faultsim.iCnt:      %d\n",
                              faultsim_state.iCnt);
              cli_printf(p->out, "faultsim.nHit:      %d\n",
                              faultsim_state.nHit);
              cli_printf(p->out, "faultsim.iInterval: %d\n",
                              faultsim_state.iInterval);
              cli_printf(p->out, "faultsim.eVerbose:  %d\n",
                              faultsim_state.eVerbose);
              cli_printf(p->out, "faultsim.nRepeat:   %d\n",
                              faultsim_state.nRepeat);
              cli_printf(p->out, "faultsim.nSkip:     %d\n",
                              faultsim_state.nSkip);
            }else if( cli_strcmp(z,"-v")==0 ){
              if( faultsim_state.eVerbose<2 ) faultsim_state.eVerbose++;
            }else if( cli_strcmp(z,"-q")==0 ){
              if( faultsim_state.eVerbose>0 ) faultsim_state.eVerbose--;
            }else if( cli_strcmp(z,"-id")==0 && kk+1<nArg ){
              faultsim_state.iId = atoi(azArg[++kk]);
            }else if( cli_strcmp(z,"-errcode")==0 && kk+1<nArg ){
              faultsim_state.iErr = atoi(azArg[++kk]);
            }else if( cli_strcmp(z,"-interval")==0 && kk+1<nArg ){
              faultsim_state.iInterval = atoi(azArg[++kk]);
            }else if( cli_strcmp(z,"-repeat")==0 && kk+1<nArg ){
              faultsim_state.nRepeat = atoi(azArg[++kk]);
           }else if( cli_strcmp(z,"-skip")==0 && kk+1<nArg ){
              faultsim_state.nSkip = atoi(azArg[++kk]);
            }else if( cli_strcmp(z,"-?")==0 || sqlite3_strglob("*help*",z)==0){
              bShowHelp = 1;
            }else{
              cli_printf(stderr,
                  "Unrecognized fault_install argument: \"%s\"\n",
                  azArg[kk]);
              rc = 1;
              bShowHelp = 1;
              break;
            }
          }
          if( bShowHelp ){
            cli_puts(
               "Usage: .testctrl fault_install ARGS\n"
               "Possible arguments:\n"
               "   off               Disable faultsim\n"
               "   on                Activate faultsim\n"
               "   reset             Reset the trigger counter\n"
               "   status            Show current status\n"
               "   -v                Increase verbosity\n"
               "   -q                Decrease verbosity\n"
               "   --errcode N       When triggered, return N as error code\n"
               "   --id ID           Trigger only for the ID specified\n"
               "   --interval N      Trigger only after every N-th call\n"
               "   --repeat N        Turn off after N hits.  0 means never\n"
               "   --skip N          Skip the first N encounters\n"
               ,p->out
            );
          }
          break;
        }
      }
    }
    if( isOk==0 && iCtrl>=0 ){
      cli_printf(p->out,
          "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
      rc = 1;
    }else if( isOk==1 ){
      cli_printf(p->out, "%d\n", rc2);
    }else if( isOk==2 ){
      cli_printf(p->out, "0x%08x\n", rc2);
    }
  }else
#endif /* !defined(SQLITE_UNTESTABLE) */

  if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
    open_db(p, 0);
    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
  }else

  if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
    if( nArg==2 ){
      if( cli_strcmp(azArg[1],"once")==0 ){
        p->enableTimer = 1;
      }else{
        p->enableTimer = 2*booleanValue(azArg[1]);
      }
      if( p->enableTimer && !HAS_TIMER ){
        eputz("Error: timer not available on this system.\n");
        p->enableTimer = 0;
      }
    }else{
      eputz("Usage: .timer on|off|once\n");
      rc = 1;
    }
  }else

#ifndef SQLITE_OMIT_TRACE
  if( c=='t' && cli_strncmp(azArg[0], "trace", n)==0 ){
    int mType = 0;
    int jj;
    open_db(p, 0);
    for(jj=1; jj<nArg; jj++){
      const char *z = azArg[jj];
      if( z[0]=='-' ){
        if( optionMatch(z, "expanded") ){
          p->eTraceType = SHELL_TRACE_EXPANDED;
        }
#ifdef SQLITE_ENABLE_NORMALIZE
        else if( optionMatch(z, "normalized") ){
          p->eTraceType = SHELL_TRACE_NORMALIZED;
        }
#endif
        else if( optionMatch(z, "plain") ){
          p->eTraceType = SHELL_TRACE_PLAIN;
        }
        else if( optionMatch(z, "profile") ){
          mType |= SQLITE_TRACE_PROFILE;
        }
        else if( optionMatch(z, "row") ){
          mType |= SQLITE_TRACE_ROW;
        }
        else if( optionMatch(z, "stmt") ){
          mType |= SQLITE_TRACE_STMT;
        }
        else if( optionMatch(z, "close") ){
          mType |= SQLITE_TRACE_CLOSE;
        }
        else {
          cli_printf(stderr,"Unknown option \"%s\" on \".trace\"\n", z);
          rc = 1;
          goto meta_command_exit;
        }
      }else{
        output_file_close(p->traceOut);
        p->traceOut = output_file_open(p, z);
      }
    }
    if( p->traceOut==0 ){
      sqlite3_trace_v2(p->db, 0, 0, 0);
    }else{
      if( mType==0 ) mType = SQLITE_TRACE_STMT;
      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
    }
  }else
#endif /* !defined(SQLITE_OMIT_TRACE) */

#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
  if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){
    int ii;
    int lenOpt;
    char *zOpt;
    if( nArg<2 ){
      eputz("Usage: .unmodule [--allexcept] NAME ...\n");
      rc = 1;
      goto meta_command_exit;
    }
    open_db(p, 0);
    zOpt = azArg[1];
    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
    lenOpt = (int)strlen(zOpt);
    if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){
      assert( azArg[nArg]==0 );
      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
    }else{
      for(ii=1; ii<nArg; ii++){
        sqlite3_create_module(p->db, azArg[ii], 0, 0);
      }
    }
  }else
#endif

  if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
    char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit";
    cli_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
          sqlite3_libversion(), sqlite3_sourceid());
#if SQLITE_HAVE_ZLIB
    cli_printf(p->out, "zlib version %s\n", zlibVersion());
#endif
#define CTIMEOPT_VAL_(opt) #opt
#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
#if defined(__clang__) && defined(__clang_major__)
    cli_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
          CTIMEOPT_VAL(__clang_minor__) "."
          CTIMEOPT_VAL(__clang_patchlevel__) " (%s)\n", zPtrSz);
#elif defined(_MSC_VER)
    cli_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz);
#elif defined(__GNUC__) && defined(__VERSION__)
    cli_printf(p->out, "gcc-" __VERSION__ " (%s)\n", zPtrSz);
#endif
  }else

  if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
    const char *zDbName = nArg==2 ? azArg[1] : "main";
    sqlite3_vfs *pVfs = 0;
    if( p->db ){
      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
      if( pVfs ){
        cli_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
        cli_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
        cli_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
        cli_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
      }
    }
  }else

  if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
    sqlite3_vfs *pVfs;
    sqlite3_vfs *pCurrent = 0;
    if( p->db ){
      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
    }
    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
      cli_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
            pVfs==pCurrent ? "  <--- CURRENT" : "");
      cli_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
      cli_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
      cli_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
      if( pVfs->pNext ){
        cli_puts("-----------------------------------\n", p->out);
      }
    }
  }else

  if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
    const char *zDbName = nArg==2 ? azArg[1] : "main";
    char *zVfsName = 0;
    if( p->db ){
      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
      if( zVfsName ){
        cli_printf(p->out, "%s\n", zVfsName);
        sqlite3_free(zVfsName);
      }
    }
  }else

  if( c=='w' && cli_strncmp(azArg[0], "wheretrace", n)==0 ){
    unsigned int x = nArg>=2? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
  }else

  if( c=='w' && cli_strncmp(azArg[0], "width", n)==0 ){
    int j;
    p->mode.spec.nWidth = nArg-1;
    p->mode.spec.aWidth = realloc(p->mode.spec.aWidth,
                                  (p->mode.spec.nWidth+1)*sizeof(short int));
    shell_check_oom(p->mode.spec.aWidth);
    for(j=1; j<nArg; j++){
      i64 w = integerValue(azArg[j]);
      if( w < -QRF_MAX_WIDTH ) w = -QRF_MAX_WIDTH;
      if( w > QRF_MAX_WIDTH ) w = QRF_MAX_WIDTH;
      p->mode.spec.aWidth[j-1] = (short int)w;
    }
  }else

  {
    cli_printf(stderr,"Error: unknown command or invalid arguments: "
          " \"%s\". Enter \".help\" for help\n", azArg[0]);
    rc = 1;
  }

meta_command_exit:
  if( p->nPopOutput ){
    p->nPopOutput--;
    if( p->nPopOutput==0 ) output_reset(p);
  }
  p->bSafeMode = p->bSafeModePersist;
  p->dot.nArg = 0;
  return rc;
}

/*
** Test to see if a line consists entirely of whitespace.
*/
static int line_is_all_whitespace(const char *z){
  for(; *z; z++){
    if( IsSpace(z[0]) ) continue;
    if( *z=='/' && z[1]=='*' ){
      z += 2;
      while( *z && (*z!='*' || z[1]!='/') ){ z++; }
      if( *z==0 ) return 0;
      z++;
      continue;
    }
    if( *z=='-' && z[1]=='-' ){
      z += 2;
      while( *z && *z!='\n' ){ z++; }
      if( *z==0 ) return 1;
      continue;
    }
    return 0;
  }
  return 1;
}

/*
** Return TRUE if the line typed in is an SQL command terminator other
** than a semi-colon.  The SQL Server style "go" command is understood
** as is the Oracle "/".
*/
static int line_is_command_terminator(const char *zLine){
  while( IsSpace(zLine[0]) ){ zLine++; };
  if( zLine[0]=='/' && line_is_all_whitespace(&zLine[1]) ){
    return 1;  /* Oracle */
  }
  if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
         && line_is_all_whitespace(&zLine[2]) ){
    return 1;  /* SQL Server */
  }
  return 0;
}

/*
** The CLI needs a working sqlite3_complete() to work properly.  So error
** out of the build if compiling with SQLITE_OMIT_COMPLETE.
*/
#ifdef SQLITE_OMIT_COMPLETE
# error the CLI application is incompatible with SQLITE_OMIT_COMPLETE.
#endif

/*
** Return true if zSql is a complete SQL statement.  Return false if it
** ends in the middle of a string literal or C-style comment.
*/
static int line_is_complete(char *zSql, int nSql){
  int rc;
  if( zSql==0 ) return 1;
  zSql[nSql] = ';';
  zSql[nSql+1] = 0;
  rc = sqlite3_complete(zSql);
  zSql[nSql] = 0;
  return rc;
}

/*
** This function is called after processing each line of SQL in the
** runOneSqlLine() function. Its purpose is to detect scenarios where
** defensive mode should be automatically turned off. Specifically, when
**
**   1. The first line of input is "PRAGMA foreign_keys=OFF;",
**   2. The second line of input is "BEGIN TRANSACTION;",
**   3. The database is empty, and
**   4. The shell is not running in --safe mode.
** 
** The implementation uses the ShellState.eRestoreState to maintain state:
**
**    0: Have not seen any SQL.
**    1: Have seen "PRAGMA foreign_keys=OFF;".
**    2-6: Currently running .dump transaction. If the "2" bit is set,
**         disable DEFENSIVE when done. If "4" is set, disable DQS_DDL.
**    7: Nothing left to do. This function becomes a no-op.
*/
static int doAutoDetectRestore(ShellState *p, const char *zSql){
  int rc = SQLITE_OK;

  if( p->eRestoreState<7 ){
    switch( p->eRestoreState ){
      case 0: {
        const char *zExpect = "PRAGMA foreign_keys=OFF;";
        assert( strlen(zExpect)==24 );
        if( p->bSafeMode==0
         && strlen(zSql)>=24
         && memcmp(zSql, zExpect, 25)==0
        ){
          p->eRestoreState = 1;
        }else{
          p->eRestoreState = 7;
        }
        break;
      };
  
      case 1: {
        int bIsDump = 0;
        const char *zExpect = "BEGIN TRANSACTION;";
        assert( strlen(zExpect)==18 );
        if( memcmp(zSql, zExpect, 19)==0 ){
          /* Now check if the database is empty. */
          const char *zQuery = "SELECT 1 FROM sqlite_schema LIMIT 1";
          sqlite3_stmt *pStmt = 0;
  
          bIsDump = 1;
          shellPrepare(p->db, &rc, zQuery, &pStmt);
          if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
            bIsDump = 0;
          }
          shellFinalize(&rc, pStmt);
        }
        if( bIsDump && rc==SQLITE_OK ){
          int bDefense = 0;
          int bDqsDdl = 0;
          sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &bDefense);
          sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, -1, &bDqsDdl);
          sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
          sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 1, 0);
          p->eRestoreState = (bDefense ? 2 : 0) + (bDqsDdl ? 4 : 0);
        }else{
          p->eRestoreState = 7;
        }
        break;
      }
  
      default: {
        if( sqlite3_get_autocommit(p->db) ){
          if( (p->eRestoreState & 2) ){
            sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 1, 0);
          }
          if( (p->eRestoreState & 4) ){
            sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 0, 0);
          }
          p->eRestoreState = 7;
        }
        break;
      }
    }
  }

  return rc;
}

/*
** Run a single line of SQL.  Return the number of errors.
*/
static int runOneSqlLine(
  ShellState *p,            /* Execution context */
  char *zSql,               /* SQL to be run */
  const char *zFilename,    /* Source file of the sql */
  int startline             /* linenumber */
){
  int rc;
  char *zErrMsg = 0;

  open_db(p, 0);
  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
  BEGIN_TIMER(p);
  rc = shell_exec(p, zSql, &zErrMsg);
  END_TIMER(p);
  if( rc || zErrMsg ){
    sqlite3_str *pPrefix = sqlite3_str_new(p->db);
    const char *zErrorTail;
    const char *zErrorType;
    if( zErrMsg==0 ){
      zErrorType = "Error";
      zErrorTail = sqlite3_errmsg(p->db);
    }else if( cli_strncmp(zErrMsg, "in prepare, ",12)==0 ){
      zErrorType = "Parse error";
      zErrorTail = &zErrMsg[12];
    }else if( cli_strncmp(zErrMsg, "stepping, ", 10)==0 ){
      zErrorType = "Runtime error";
      zErrorTail = &zErrMsg[10];
    }else{
      zErrorType = "Error";
      zErrorTail = zErrMsg;
    }
    if( zFilename || !stdin_is_interactive ){
      if( cli_strcmp(zFilename,"cmdline")==0 ){
        sqlite3_str_appendf(pPrefix,
                  "%s in %r command line argument:", zErrorType, startline);
      }else if( cli_strcmp(zFilename,"<stdin>")==0 ){
        sqlite3_str_appendf(pPrefix,
                  "%s near line %d:", zErrorType, startline);
      }else{
        sqlite3_str_appendf(pPrefix,
                  "%s near line %d of %s:", zErrorType, startline, zFilename);
      }
    }else{
      sqlite3_str_appendf(pPrefix, "%s:", zErrorType);
    }
    cli_printf(stderr,"%s %s\n", sqlite3_str_value(pPrefix), zErrorTail);
    sqlite3_str_free(pPrefix);
    sqlite3_free(zErrMsg);
    zErrMsg = 0;
    return 1;
  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
    cli_printf(p->out, 
            "changes: %lld   total_changes: %lld\n",
            sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
  }

  if( doAutoDetectRestore(p, zSql) ) return 1;
  return 0;
}

static void echo_group_input(ShellState *p, const char *zDo){
  if( p->mode.mFlags & MFLG_ECHO ){
    cli_printf(p->out, "%s\n", zDo);
    fflush(p->out);
  }
}

#ifdef SQLITE_SHELL_FIDDLE
/*
** Alternate one_input_line() impl for wasm mode. This is not in the primary
** impl because we need the global shellState and cannot access it from that
** function without moving lots of code around (creating a larger/messier diff).
*/
static char *one_input_line(
  ShellState *p,     /* Shell state */
  char *zPrior,      /* Just the prior line of text */
  const char *zAll,  /* All accumulated statement text */
  int bContinue      /* True if this is an accumulation */
){
  /* Parse the next line from shellState.wasm.zInput. */
  const char *zBegin = shellState.wasm.zPos;
  const char *z = zBegin;
  char *zLine = 0;
  i64 nZ = 0;
  FILE *in = p->in;

  UNUSED_PARAMETER(in);
  UNUSED_PARAMETER(bContinue);
  if(!z || !*z){
    return 0;
  }
  while(*z && IsSpace(*z)) ++z;
  zBegin = z;
  for(; *z && '\n'!=*z; ++nZ, ++z){}
  if(nZ>0 && '\r'==zBegin[nZ-1]){
    --nZ;
  }
  shellState.wasm.zPos = z;
  zLine = realloc(zPrior, nZ+1);
  shell_check_oom(zLine);
  memcpy(zLine, zBegin, nZ);
  zLine[nZ] = 0;
  return zLine;
}
#endif /* SQLITE_SHELL_FIDDLE */

/*
** Read input from *in and process it.  If *in==0 then input
** is interactive - the user is typing it it.  Otherwise, input
** is coming from a file or device.  A prompt is issued and history
** is saved only if input is interactive.  An interrupt signal will
** cause this routine to exit immediately, unless input is interactive.
**
** Return the number of errors.
*/
static int process_input(ShellState *p, const char *zSrc){
  char *zLine = 0;          /* A single input line */
  char *zSql = 0;           /* Accumulated SQL text */
  i64 nLine;                /* Length of current line */
  i64 nSql = 0;             /* Bytes of zSql[] used */
  i64 nAlloc = 0;           /* Allocated zSql[] space */
  int rc;                   /* Error code */
  int errCnt = 0;           /* Number of errors seen */
  int hasSemi = 0;          /* Input line contains a semicolon */
  i64 startline = 0;        /* Line number for start of current input */
  const char *saved_zInFile;      /* Prior value of p->zInFile */
  i64 saved_lineno;               /* Prior value of p->lineno */

  if( p->inputNesting==MAX_INPUT_NESTING ){
    /* This will be more informative in a later version. */
    cli_printf(stderr,"%s: Input nesting limit (%d) reached at line %lld."
          " Check recursion.\n", zSrc, MAX_INPUT_NESTING, p->lineno);
    return 1;
  }
  ++p->inputNesting;
  saved_zInFile = p->zInFile;
  p->zInFile = zSrc;
  saved_lineno = p->lineno;
  p->lineno = 0;
  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
    fflush(p->out);
    zLine = one_input_line(p, zLine, zSql, nSql>0);
    if( zLine==0 ){
      /* End of input */
      if( p->in==0 && stdin_is_interactive ) cli_puts("\n", p->out);
      break;
    }
    if( seenInterrupt ){
      if( p->in!=0 ) break;
      seenInterrupt = 0;
    }
    p->lineno++;
    if( nSql==0 && line_is_all_whitespace(zLine) ){
      /* Just swallow single-line whitespace */
      echo_group_input(p, zLine);
      continue;
    }
    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
      echo_group_input(p, zLine);
      if( zLine[0]=='.' ){
        rc = do_meta_command(zLine, p);
        if( rc==2 ){ /* exit requested */
          break;
        }else if( rc ){
          errCnt++;
        }
      }
      continue;
    }
    if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
      memcpy(zLine,";",2);
    }
    hasSemi = strchr(zLine,';')!=0;
    nLine = strlen(zLine);
    if( nSql+nLine+2>=nAlloc ){
      /* Grow buffer by half-again increments when big. */
      nAlloc = nSql+(nSql>>1)+nLine+100;
      zSql = realloc(zSql, nAlloc);
      shell_check_oom(zSql);
    }
    if( nSql==0 ){
      i64 i;
      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
      assert( nAlloc>0 && zSql!=0 );
      memcpy(zSql, zLine+i, nLine+1-i);
      startline = p->lineno;
      nSql = nLine-i;
    }else{
      zSql[nSql++] = '\n';
      memcpy(zSql+nSql, zLine, nLine+1);
      nSql += nLine;
    }
    if( nSql>0x7fff0000 ){
      char zSize[100];
      sqlite3_snprintf(sizeof(zSize),zSize,"%,lld",nSql);
      cli_printf(stderr, "%s:%lld: Input SQL is too big: %s bytes\n",
                      zSrc, startline, zSize);
      nSql = 0;
      errCnt++;
      break;
    }
    if( nSql && hasSemi && sqlite3_complete(zSql) ){
      echo_group_input(p, zSql);
      errCnt += runOneSqlLine(p, zSql, p->zInFile, startline);
      nSql = 0;
      if( p->nPopOutput ){
        output_reset(p);
        p->nPopOutput = 0;
      }else{
        clearTempFile(p,0,0);
      }
      if( p->nPopMode ){
        modePop(p);
        p->nPopMode = 0;
      }
      p->bSafeMode = p->bSafeModePersist;
    }else if( nSql && line_is_all_whitespace(zSql) ){
      echo_group_input(p, zSql);
      nSql = 0;
    }
  }
  if( nSql ){
    /* This may be incomplete. Let the SQL parser deal with that. */
    echo_group_input(p, zSql);
    errCnt += runOneSqlLine(p, zSql, p->zInFile, startline);
  }
  free(zSql);
  free(zLine);
  --p->inputNesting;
  p->zInFile = saved_zInFile;
  p->lineno = saved_lineno;
  return errCnt>0;
}

/*
** Return a pathname which is the user's home directory.  A
** 0 return indicates an error of some kind.
*/
static char *find_home_dir(int clearFlag){
  static char *home_dir = NULL;
  if( clearFlag ){
    free(home_dir);
    home_dir = 0;
    return 0;
  }
  if( home_dir ) return home_dir;

#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
     && !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
  {
    struct passwd *pwent;
    uid_t uid = getuid();
    if( (pwent=getpwuid(uid)) != NULL) {
      home_dir = pwent->pw_dir;
    }
  }
#endif

#if defined(_WIN32_WCE)
  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
   */
  home_dir = "/";
#else

#if defined(_WIN32) || defined(WIN32)
  if (!home_dir) {
    home_dir = getenv("USERPROFILE");
  }
#endif

  if (!home_dir) {
    home_dir = getenv("HOME");
  }

#if defined(_WIN32) || defined(WIN32)
  if (!home_dir) {
    char *zDrive, *zPath;
    int n;
    zDrive = getenv("HOMEDRIVE");
    zPath = getenv("HOMEPATH");
    if( zDrive && zPath ){
      n = strlen30(zDrive) + strlen30(zPath) + 1;
      home_dir = malloc( n );
      if( home_dir==0 ) return 0;
      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
      return home_dir;
    }
    home_dir = "c:\\";
  }
#endif

#endif /* !_WIN32_WCE */

  if( home_dir ){
    i64 n = strlen(home_dir) + 1;
    char *z = malloc( n );
    if( z ) memcpy(z, home_dir, n);
    home_dir = z;
  }

  return home_dir;
}

/*
** On non-Windows platforms, look for:
**
** - ${zEnvVar}/${zBaseName}
** - ${HOME}/${zSubdir}/${zBaseName}
**
** $zEnvVar is intended to be the name of an XDG_... environment
** variable, e.g. XDG_CONFIG_HOME or XDG_STATE_HOME.  If zEnvVar is
** NULL or getenv(zEnvVar) is NULL then fall back to the second
** option. If the selected option is not found in the filesystem,
** return 0.
**
** zSubdir may be NULL or empty, in which case ${HOME}/${zBaseName}
** becomes the fallback.
**
** Both zSubdir and zBaseName may contain subdirectory parts. zSubdir
** will conventionally be ".config" or ".local/state", which, not
** coincidentally, is the typical subdir of the corresponding XDG_...
** var with the XDG var's $HOME prefix.
**
** The returned string is obtained from sqlite3_malloc() and should be
** sqlite3_free()'d by the caller.
*/
static char *find_xdg_file(const char *zEnvVar, const char *zSubdir,
                           const char *zBaseName){
#if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \
     || defined(__RTP__) || defined(_WRS_KERNEL)
  return 0;
#else
  char *zConfigFile = 0;
  const char *zXdgDir;

  zXdgDir = zEnvVar ? getenv(zEnvVar) : 0;
  if( zXdgDir ){
    zConfigFile = sqlite3_mprintf("%s/%s", zXdgDir, zBaseName);
  }else{
    const char * zHome = find_home_dir(0);
    if( zHome==0 ) return 0;
    zConfigFile = (zSubdir && *zSubdir)
      ? sqlite3_mprintf("%s/%s/%s", zHome, zSubdir, zBaseName)
      : sqlite3_mprintf("%s/%s", zHome, zBaseName);
  }
  shell_check_oom(zConfigFile);
  if( access(zConfigFile,0)!=0 ){
    sqlite3_free(zConfigFile);
    zConfigFile = 0;
  }
  return zConfigFile;
#endif
}

/*
** Read input from the file sqliterc_override.  If that parameter is
** NULL, take it from find_xdg_file(), if found, or fall back to
** ~/.sqliterc.
**
** Failure to read the config is only considered a failure if
** sqliterc_override is not NULL, in which case this function may emit
** a warning or, if ::bail_on_error is true, fail fatally if the file
** named by sqliterc_override is not found.
*/
static void process_sqliterc(
  ShellState *p,                  /* Configuration data */
  const char *sqliterc_override   /* Name of config file. NULL to use default */
){
  char *home_dir = NULL;
  char *sqliterc = (char*)sqliterc_override;
  FILE *inSaved = p->in;
  i64 savedLineno = p->lineno;

  if( sqliterc == NULL ){
    sqliterc = find_xdg_file("XDG_CONFIG_HOME",
                             ".config",
                             "sqlite3/sqliterc");
  }
  if( sqliterc == NULL ){
    home_dir = find_home_dir(0);
    if( home_dir==0 ){
      eputz("-- warning: cannot find home directory;"
            " cannot read ~/.sqliterc\n");
      return;
    }
    sqliterc = sqlite3_mprintf("%s/.sqliterc",home_dir);
    shell_check_oom(sqliterc);
  }
  p->in = sqliterc ? sqlite3_fopen(sqliterc,"rb") : 0;
  if( p->in ){
    if( stdin_is_interactive ){
      cli_printf(stderr,"-- Loading resources from %s\n", sqliterc);
    }
    if( process_input(p, sqliterc) && bail_on_error ) cli_exit(1);
    fclose(p->in);
  }else if( sqliterc_override!=0 ){
    cli_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
    if( bail_on_error ) cli_exit(1);
  }
  p->in = inSaved;
  p->lineno = savedLineno;
  if( sqliterc != sqliterc_override ){
    sqlite3_free(sqliterc);
  }
}

/*
** Show available command line options
*/
static const char zOptions[] =
  "   --                   treat no subsequent arguments as options\n"
#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
  "   -A ARGS...           run \".archive ARGS\" and exit\n"
#endif
  "   -append              append the database to the end of the file\n"
  "   -ascii               set '.mode ascii'\n"
  "   -bail                stop after hitting an error\n"
  "   -batch               force batch I/O\n"
  "   -box                 set '.mode box'\n"
  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
  "   -column              set '.mode column'\n"
  "   -csv                 set '.mode csv -limits off'\n"
#if !defined(SQLITE_OMIT_DESERIALIZE)
  "   -deserialize         open the database using sqlite3_deserialize()\n"
#endif
  "   -echo                print inputs before execution\n"
  "   -escape T            ctrl-char escape; T is one of: symbol, ascii, off\n"
  "   -init FILENAME       read/process named file\n"
  "   -[no]header          turn headers on or off\n"
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
#endif
  "   -help                show this message\n"
  "   -html                set '.mode html'\n"
  "   -ifexists            only open if database already exists\n"
  "   -interactive         force interactive I/O\n"
  "   -json                set '.mode json'\n"
  "   -line                set '.mode line'\n"
  "   -list                set '.mode list'\n"
  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
  "   -markdown            set '.mode markdown'\n"
#if !defined(SQLITE_OMIT_DESERIALIZE)
  "   -maxsize N           maximum size for a --deserialize database\n"
#endif
  "   -memtrace            trace all memory allocations and deallocations\n"
  "   -mmap N              default mmap size set to N\n"
#ifdef SQLITE_ENABLE_MULTIPLEX
  "   -multiplex           enable the multiplexor VFS\n"
#endif
  "   -newline SEP         set output row separator. Default: '\\n'\n"
  "   -nofollow            refuse to open symbolic links to database files\n"
  "   -noinit              Do not read the ~/.sqliterc file at startup\n"
  "   -nonce STRING        set the safe-mode escape nonce\n"
  "   -no-rowid-in-view    Disable rowid-in-view using sqlite3_config()\n"
  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
  "   -pcachetrace         trace all page cache operations\n"
  "   -quote               set '.mode quote'\n"
  "   -readonly            open the database read-only\n"
  "   -safe                enable safe-mode\n"
  "   -screenwidth N       use N as the default screenwidth \n"
  "   -separator SEP       set output column separator. Default: '|'\n"
#ifdef SQLITE_ENABLE_SORTER_REFERENCES
  "   -sorterref SIZE      sorter references threshold size\n"
#endif
  "   -stats               print memory stats before each finalize\n"
  "   -table               set '.mode table'\n"
  "   -tabs                set '.mode tabs'\n"
  "   -unsafe-testing      allow unsafe commands and modes for testing\n"
  "   -version             show SQLite version\n"
  "   -vfs NAME            use NAME as the default VFS\n"
  "   -vfstrace            enable tracing of all VFS calls\n"
#ifdef SQLITE_HAVE_ZLIB
  "   -zip                 open the file as a ZIP Archive\n"
#endif
;
static void usage(int showDetail){
  cli_printf(stderr,"Usage: %s [OPTIONS] [FILENAME [SQL...]]\n"
       "FILENAME is the name of an SQLite database. A new database is created\n"
       "if the file does not previously exist. Defaults to :memory:.\n", Argv0);
  if( showDetail ){
    cli_printf(stderr,"OPTIONS include:\n%s", zOptions);
  }else{
    eputz("Use the -help option for additional information\n");
  }
  exit(0);
}

/*
** Internal check:  Verify that the SQLite is uninitialized.  Print a
** error message if it is initialized.
*/
static void verify_uninitialized(void){
  if( sqlite3_config(-1)==SQLITE_MISUSE ){
    sputz(stdout, "WARNING: attempt to configure SQLite after"
          " initialization.\n");
  }
}

#if HAVE_EDITLINE
/*
** https://sqlite.org/forum/forumpost/aad7a634916ff050:
**
** Calling setlocale(LC_...,"") is required to get libedit to accept
** non-ASCII input.
*/
#define DO_SET_LOCALE 1
#include <locale.h>
#else
#define DO_SET_LOCALE 0
#endif

/*
** Initialize the state information in data
*/
static void main_init(ShellState *p) {
  memset(p, 0, sizeof(*p));
  p->pAuxDb = &p->aAuxDb[0];
  p->shellFlgs = SHFLG_Lookaside;
  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, p);
#if !defined(SQLITE_SHELL_FIDDLE)
  verify_uninitialized();
#endif
  sqlite3_config(SQLITE_CONFIG_URI, 1);
  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
  globalShellState = p;
#if HAVE_EDITLINE || HAVE_READLINE
  /* Editline requires \001...\002 delimiters around ANSI x3.64 escapes in
  ** prompt strings.  Readline does sometimes, depending on how it is
  ** compiled and installed. */
  p->bDelimitNonprint = 1;
#else
  /* No \001...\002 escapes required for linenoise or when not using a
  ** command-line editing library */
  p->bDelimitNonprint = 0;
#endif
#if DO_SET_LOCALE
  setlocale(LC_CTYPE,"");
#endif
#undef DO_SET_LOCALE
}

/*
** Output text to the console in a font that attracts extra attention.
*/
static void printBold(const char *zText){
  if( shellNoColor() ){
    cli_printf(stdout, "%s", zText);
  }else{
    cli_printf(stdout, "\033[1;36m\033[3m%s\033[0m", zText);
  }
}

/*
** Get the argument to an --option.  Throw an error and die if no argument
** is available.
*/
static char *cmdline_option_value(int argc, char **argv, int i){
  if( i==argc ){
    cli_printf(stderr,
            "%s: Error: missing argument to %s\n", argv[0], argv[argc-1]);
    cli_exit(1);
  }
  return argv[i];
}

/*
** The callback from atexit().
*/
static void SQLITE_CDECL abnormalExit(void){
  if( seenInterrupt ) eputz("Program interrupted.\n");
  if( globalShellState ){
    clearTempFile(globalShellState, 1, 1);
  }
}

/* Routine to output from vfstrace
*/
static int vfstraceOut(const char *z, void *pArg){
  ShellState *p = (ShellState*)pArg;
  cli_puts(z, p->out);
  fflush(p->out);
  return 1;
}

#if defined(SQLITE_DEBUG) && !defined(SQLITE_SHELL_FIDDLE)
/* Ensure that sqlite3_reset_auto_extension() clears auto-extension
** memory. https://sqlite.org/forum/forumpost/310cb231b07c80d1.
** Testing this: if we (inadvertently) remove the
** sqlite3_reset_auto_extension() call from main(), most shell tests
** will fail because of a leak message in their output. */
static int auto_ext_leak_tester(
  sqlite3 *db,
  char **pzErrMsg,
  const struct sqlite3_api_routines *pThunk
){
  (void)db; (void)pzErrMsg; (void)pThunk;
  return SQLITE_OK;
}
#endif

/* Alternative name to the entry point for Fiddle */
#ifdef SQLITE_SHELL_FIDDLE
#  define main fiddle_main
#endif

/* Use the wmain() entry point on Windows.  Translate arguments to
** UTF8, then invoke the traditional main() entry point which is
** renamed using a #define to utf8_main() .
*/
#if defined(_WIN32) && !defined(__MINGW32__) && !defined(main)
#  define main utf8_main                 /* Rename entry point to utf_main() */
int SQLITE_CDECL utf8_main(int,char**);  /* Forward declaration */  
int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
  int rc, i;
  DWORD mode;
  HANDLE hOut;
  char **argv = malloc( sizeof(char*) * (argc+1) );
  char **orig = argv;
  if( argv==0 ){
    fprintf(stderr, "malloc failed\n");
    exit(1);
  }
  for(i=0; i<argc; i++){
    int nByte = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, 0, 0, 0, 0);
    if( nByte==0 ){
      argv[i] = 0;
    }else{
      argv[i] = malloc( nByte );
      if( argv[i]==0 ){
        fprintf(stderr, "malloc failed\n");
        exit(1);
      }
      nByte = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, argv[i],nByte,0,0);
      if( nByte==0 ){
        free(argv[i]);
        argv[i] = 0;
      }
    }
  }
  argv[argc] = 0;
  hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  GetConsoleMode(hOut, &mode);
  SetConsoleMode(hOut, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
  rc = utf8_main(argc, argv);
  for(i=0; i<argc; i++) free(orig[i]);
  free(argv);
  return rc;
}
#endif /* WIN32 */

/*
** This is the main entry point for the process.  Everything starts here.
**
** The "main" identifier may have been #defined to something else:
**
**     utf8_main            On Windows
**     fiddle_main          In Fiddle
**     sqlite3_shell        Other projects that use shell.c as a subroutine
*/
int SQLITE_CDECL main(int argc, char **argv){
#ifdef SQLITE_DEBUG
  sqlite3_int64 mem_main_enter = 0;
#endif
#ifdef SQLITE_SHELL_FIDDLE
#  define data shellState
#else
  ShellState data;
#endif
  const char *zInitFile = 0;
  int i;
  int rc = 0;
  int warnInmemoryDb = 0;
  int readStdin = 1;
  int noInit = 0;                 /* Do not read ~/.sqliterc if true */
  int nCmd = 0;
  int nOptsEnd = argc;
  int bEnableVfstrace = 0;
  char **azCmd = 0;
  int *aiCmd = 0;
  const char *zVfs = 0;           /* Value of -vfs command-line option */
  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */

#ifdef SQLITE_SHELL_FIDDLE
  stdin_is_interactive = 0;
  stdout_is_console = 1;
  data.wasm.zDefaultDbName = "/fiddle.sqlite3";
#else
  stdin_is_interactive = isatty(0);
  stdout_is_console = isatty(1);
#endif
  atexit(abnormalExit);
#ifdef SQLITE_DEBUG
  mem_main_enter = sqlite3_memory_used();
#endif
#if !defined(_WIN32_WCE)
  if( getenv("SQLITE_DEBUG_BREAK") ){
    if( isatty(0) && isatty(2) ){
      char zLine[100];
      cli_printf(stderr,
            "attach debugger to process %d and press ENTER to continue...",
            GETPID());
      if( sqlite3_fgets(zLine, sizeof(zLine), stdin)!=0
       && cli_strcmp(zLine,"stop")==0
      ){
        exit(1);
      }
    }else{
#if defined(_WIN32) || defined(WIN32)
      DebugBreak();
#elif defined(SIGTRAP)
      raise(SIGTRAP);
#endif
    }
  }
#endif
  /* Register a valid signal handler early, before much else is done. */
#ifdef SIGINT
  signal(SIGINT, interrupt_handler);
#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
  if( !SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE) ){
    eputz("No ^C handler.\n");
  }
#endif
#ifdef SIGTERM
  signal(SIGTERM, sigterm_handler);
#endif
#ifdef SIGHUP
  signal(SIGHUP, sigterm_handler);
#endif


#if USE_SYSTEM_SQLITE+0!=1
  if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
    cli_printf(stderr,
          "SQLite header and source version mismatch\n%s\n%s\n",
          sqlite3_sourceid(), SQLITE_SOURCE_ID);
    exit(1);
  }
#endif
  main_init(&data);

  assert( argc>=1 && argv && argv[0] );
  Argv0 = argv[0];

#ifdef SQLITE_SHELL_DBNAME_PROC
  {
    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
    ** of a C-function that will provide the name of the database file.  Use
    ** this compile-time option to embed this shell program in larger
    ** applications. */
    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
    SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
    warnInmemoryDb = 0;
  }
#endif

  /* Do an initial pass through the command-line arguments to locate
  ** the name of the database file, the name of the initialization file,
  ** the size of the alternative malloc heap, options affecting commands
  ** or SQL run from the command line, and the first command to execute.
  */
#ifndef SQLITE_SHELL_FIDDLE
  verify_uninitialized();
#endif
  for(i=1; i<argc; i++){
    char *z;
    z = argv[i];
    if( z[0]!='-' || i>nOptsEnd ){
      if( data.aAuxDb->zDbFilename==0 && !isScriptFile(z,1) ){
        data.aAuxDb->zDbFilename = z;
      }else{
        /* Excess arguments are interpreted as SQL (or dot-commands) and
        ** mean that nothing is read from stdin */
        readStdin = 0;
        stdin_is_interactive = 0;
        nCmd++;
        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
        shell_check_oom(azCmd);
        aiCmd = realloc(aiCmd, sizeof(aiCmd[0])*nCmd);
        shell_check_oom(aiCmd);
        azCmd[nCmd-1] = z;
        aiCmd[nCmd-1] = i;
      }
      continue;
    }
    if( z[1]=='-' ) z++;
    if( cli_strcmp(z, "-")==0 ){
      nOptsEnd = i;
      continue;
    }else if( cli_strcmp(z,"-separator")==0
     || cli_strcmp(z,"-nullvalue")==0
     || cli_strcmp(z,"-newline")==0
     || cli_strcmp(z,"-cmd")==0
    ){
      (void)cmdline_option_value(argc, argv, ++i);
    }else if( cli_strcmp(z,"-init")==0 ){
      zInitFile = cmdline_option_value(argc, argv, ++i);
    }else if( cli_strcmp(z,"-interactive")==0 ){
    }else if( cli_strcmp(z,"-batch")==0 ){
      /* Need to check for batch mode here to so we can avoid printing
      ** informational messages (like from process_sqliterc) before
      ** we do the actual processing of arguments later in a second pass.
      */
      stdin_is_interactive = 0;
      stdout_is_console = 0;
      modeChange(&data, MODE_BATCH);
    }else if( cli_strcmp(z,"-screenwidth")==0 ){
      int n = atoi(cmdline_option_value(argc, argv, ++i));
      if( n<2 ){
        sqlite3_fprintf(stderr,"minimum --screenwidth is 2\n");
        exit(1);
      }
      stdout_tty_width = n;
    }else if( cli_strcmp(z,"-utf8")==0 ){
    }else if( cli_strcmp(z,"-no-utf8")==0 ){
    }else if( cli_strcmp(z,"-no-rowid-in-view")==0 ){
      int val = 0;
      sqlite3_config(SQLITE_CONFIG_ROWID_IN_VIEW, &val);
      assert( val==0 );
    }else if( cli_strcmp(z,"-heap")==0 ){
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
      const char *zSize;
      sqlite3_int64 szHeap;

      zSize = cmdline_option_value(argc, argv, ++i);
      szHeap = integerValue(zSize);
      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
      verify_uninitialized();
      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
#else
      (void)cmdline_option_value(argc, argv, ++i);
#endif
    }else if( cli_strcmp(z,"-pagecache")==0 ){
      sqlite3_int64 n, sz;
      sz = integerValue(cmdline_option_value(argc,argv,++i));
      if( sz>65536 ) sz = 65536;
      if( sz<0 ) sz = 0;
      n = integerValue(cmdline_option_value(argc,argv,++i));
      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
        n = 0xffffffffffffLL/sz;
      }
      if( sz>0 && (sz & (sz-1))==0 ){
        /* If SIZE is a power of two, round it up by the PCACHE_HDRSZ */
        int szHdr = 0;
        sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &szHdr);
        sz += szHdr;
        cli_printf(stdout, "Page cache size increased to %d to accommodate"
                        " the %d-byte headers\n", (int)sz, szHdr);
      }
      verify_uninitialized();
      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
      data.shellFlgs |= SHFLG_Pagecache;
    }else if( cli_strcmp(z,"-lookaside")==0 ){
      int n, sz;
      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
      if( sz<0 ) sz = 0;
      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
      if( n<0 ) n = 0;
      verify_uninitialized();
      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
      if( (i64)sz*(i64)n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
    }else if( cli_strcmp(z,"-threadsafe")==0 ){
      int n;
      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
      verify_uninitialized();
      switch( n ){
         case 0:  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);  break;
         case 2:  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);   break;
         default: sqlite3_config(SQLITE_CONFIG_SERIALIZED);    break;
      }
    }else if( cli_strcmp(z,"-vfstrace")==0 ){
      bEnableVfstrace = 1;
#ifdef SQLITE_ENABLE_MULTIPLEX
    }else if( cli_strcmp(z,"-multiplex")==0 ){
      extern int sqlite3_multiplex_initialize(const char*,int);
      sqlite3_multiplex_initialize(0, 1);
#endif
    }else if( cli_strcmp(z,"-mmap")==0 ){
      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
      verify_uninitialized();
      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
#if defined(SQLITE_ENABLE_SORTER_REFERENCES)
    }else if( cli_strcmp(z,"-sorterref")==0 ){
      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
      verify_uninitialized();
      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
#endif
    }else if( cli_strcmp(z,"-vfs")==0 ){
      zVfs = cmdline_option_value(argc, argv, ++i);
#ifdef SQLITE_HAVE_ZLIB
    }else if( cli_strcmp(z,"-zip")==0 ){
      data.openMode = SHELL_OPEN_ZIPFILE;
#endif
    }else if( cli_strcmp(z,"-append")==0 ){
      data.openMode = SHELL_OPEN_APPENDVFS;
#ifndef SQLITE_OMIT_DESERIALIZE
    }else if( cli_strcmp(z,"-deserialize")==0 ){
      data.openMode = SHELL_OPEN_DESERIALIZE;
    }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
      data.szMax = integerValue(argv[++i]);
#endif
    }else if( cli_strcmp(z,"-readonly")==0 ){
      data.openFlags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
      data.openFlags |= SQLITE_OPEN_READONLY;
    }else if( cli_strcmp(z,"-nofollow")==0 ){
      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
    }else if( cli_strcmp(z,"-noinit")==0 ){
      noInit = 1;
    }else if( cli_strcmp(z,"-exclusive")==0 ){  /* UNDOCUMENTED */
      data.openFlags |= SQLITE_OPEN_EXCLUSIVE;
    }else if( cli_strcmp(z,"-ifexists")==0 ){
      data.openFlags &= ~(SQLITE_OPEN_CREATE);
      if( data.openFlags==0 ) data.openFlags = SQLITE_OPEN_READWRITE;
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
    }else if( cli_strncmp(z, "-A",2)==0 ){
      /* All remaining command-line arguments are passed to the ".archive"
      ** command, so ignore them */
      break;
#endif
    }else if( cli_strcmp(z, "-memtrace")==0 ){
      sqlite3MemTraceActivate(stderr);
    }else if( cli_strcmp(z, "-pcachetrace")==0 ){
      sqlite3PcacheTraceActivate(stderr);
    }else if( cli_strcmp(z,"-bail")==0 ){
      bail_on_error = 1;
    }else if( cli_strcmp(z,"-nonce")==0 ){
      free(data.zNonce);
      data.zNonce = strdup(cmdline_option_value(argc, argv, ++i));
    }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
      ShellSetFlag(&data,SHFLG_TestingMode);
    }else if( cli_strcmp(z,"-safe")==0 ){
      /* no-op - catch this on the second pass */
    }else if( cli_strcmp(z,"-escape")==0 && i+1<argc ){
      /* skip over the argument */
      i++;
    }else if( cli_strcmp(z,"-test-argv")==0 ){
      /* Undocumented test option.  Print the values in argv[] and exit.
      ** Use this to verify that any translation of the argv[], for example
      ** on Windows that receives wargv[] from the OS and must convert
      ** to UTF8 prior to calling this routine. */
      int kk;
      for(kk=0; kk<argc; kk++){
        sqlite3_fprintf(stdout,"argv[%d] = \"%s\"\n", kk, argv[kk]);
      }
      return 0;
    }
  }
#ifndef SQLITE_SHELL_FIDDLE
  if( !bEnableVfstrace ) verify_uninitialized();
#endif


#ifdef SQLITE_SHELL_INIT_PROC
  {
    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
    ** of a C-function that will perform initialization actions on SQLite that
    ** occur just before or after sqlite3_initialize(). Use this compile-time
    ** option to embed this shell program in larger applications. */
    extern void SQLITE_SHELL_INIT_PROC(void);
    SQLITE_SHELL_INIT_PROC();
  }
#else
  /* All the sqlite3_config() calls have now been made. So it is safe
  ** to call sqlite3_initialize() and process any command line -vfs option. */
  sqlite3_initialize();
#endif

  if( zVfs ){
    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
    if( pVfs ){
      sqlite3_vfs_register(pVfs, 1);
    }
#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
    else if( access(zVfs,0)==0 ){
      /* If the VFS name is not the name of an existing VFS, but it is
      ** the name of a file, then try to load that file as an extension.
      ** Presumably the extension implements the desired VFS. */
      sqlite3 *db = 0;
      char *zErr = 0;
      sqlite3_open(":memory:", &db);
      sqlite3_enable_load_extension(db, 1);
      rc = sqlite3_load_extension(db, zVfs, 0, &zErr);
      sqlite3_close(db);
      if( (rc&0xff)!=SQLITE_OK ){
        cli_printf(stderr, "could not load extension VFS \"%s\": %s\n",
                   zVfs, zErr);
        exit(1);
      }
    }
#endif
    else{
      cli_printf(stderr,"no such VFS: \"%s\"\n", zVfs);
      exit(1);
    }
  }

  if( data.pAuxDb->zDbFilename==0 ){
#ifndef SQLITE_OMIT_MEMORYDB
    data.pAuxDb->zDbFilename = ":memory:";
    warnInmemoryDb = argc==1;
#else
    cli_printf(stderr,
                    "%s: Error: no database filename specified\n", Argv0);
    rc = 1;
    goto shell_main_exit;
#endif
  }
  data.out = stdout;
  if( bEnableVfstrace ){
    vfstrace_register("trace",0,vfstraceOut, &data, 1);
  }
#ifndef SQLITE_SHELL_FIDDLE
  sqlite3_appendvfs_init(0,0,0);
#ifdef SQLITE_DEBUG
  sqlite3_auto_extension( (void(*)(void))auto_ext_leak_tester );
#endif
#endif
  modeDefault(&data);

  /* Go ahead and open the database file if it already exists.  If the
  ** file does not exist, delay opening it.  This prevents empty database
  ** files from being created if a user mistypes the database name argument
  ** to the sqlite command-line tool.
  */
  if( access(data.pAuxDb->zDbFilename, 0)==0 ){
    open_db(&data, 0);
  }

  /* Process the initialization file if there is one.  If no -init option
  ** is given on the command line, look for a file named ~/.sqliterc and
  ** try to process it.
  */
  if( !noInit ) process_sqliterc(&data,zInitFile);

  /* Make a second pass through the command-line arguments and set
  ** options.  This second pass is delayed until after the initialization
  ** file is processed so that the command-line arguments will override
  ** settings in the initialization file.
  */
  for(i=1; i<argc; i++){
    char *z = argv[i];
    if( z[0]!='-' || i>=nOptsEnd ) continue;
    if( z[1]=='-' ){ z++; }
    if( cli_strcmp(z,"-init")==0 ){
      i++;
    }else if( cli_strcmp(z,"-html")==0 ){
      modeChange(&data, MODE_Html);
    }else if( cli_strcmp(z,"-list")==0 ){
      modeChange(&data, MODE_List);
    }else if( cli_strcmp(z,"-quote")==0 ){
      modeChange(&data, MODE_Quote);
    }else if( cli_strcmp(z,"-line")==0 ){
      modeChange(&data, MODE_Line);
    }else if( cli_strcmp(z,"-column")==0 ){
      modeChange(&data, MODE_Column);
    }else if( cli_strcmp(z,"-json")==0 ){
      modeChange(&data, MODE_Json);
    }else if( cli_strcmp(z,"-markdown")==0 ){
      modeChange(&data, MODE_Markdown);
    }else if( cli_strcmp(z,"-table")==0 ){
      modeChange(&data, MODE_Table);
    }else if( cli_strcmp(z,"-psql")==0 ){
      modeChange(&data, MODE_Psql);
    }else if( cli_strcmp(z,"-box")==0 ){
      modeChange(&data, MODE_Box);
    }else if( cli_strcmp(z,"-csv")==0 ){
      modeChange(&data, MODE_Csv);
      modeSetLimit(&data, "off");
    }else if( cli_strcmp(z,"-escape")==0 && i+1<argc ){
      /* See similar code at tag-20250224-1 */
      const char *zEsc = argv[++i];
      int k;
      for(k=0; k<ArraySize(qrfEscNames); k++){
        if( sqlite3_stricmp(zEsc,qrfEscNames[k])==0 ){
          data.mode.spec.eEsc = k;
          break;
        }
      }
      if( k>=ArraySize(qrfEscNames) ){
        cli_printf(stderr, "unknown control character escape mode \"%s\""
                                " - choices:", zEsc);
        for(k=0; k<ArraySize(qrfEscNames); k++){
          cli_printf(stderr, " %s", qrfEscNames[k]);
        }
        cli_printf(stderr, "\n");
        exit(1);
      }
#ifdef SQLITE_HAVE_ZLIB
    }else if( cli_strcmp(z,"-zip")==0 ){
      data.openMode = SHELL_OPEN_ZIPFILE;
#endif
    }else if( cli_strcmp(z,"-append")==0 ){
      data.openMode = SHELL_OPEN_APPENDVFS;
#ifndef SQLITE_OMIT_DESERIALIZE
    }else if( cli_strcmp(z,"-deserialize")==0 ){
      data.openMode = SHELL_OPEN_DESERIALIZE;
    }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
      data.szMax = integerValue(argv[++i]);
#endif
    }else if( cli_strcmp(z,"-readonly")==0 ){
      data.openFlags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
      data.openFlags |= SQLITE_OPEN_READONLY;
    }else if( cli_strcmp(z,"-nofollow")==0 ){
      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
    }else if( cli_strcmp(z,"-noinit")==0 ){
      /* No-op */
    }else if( cli_strcmp(z,"-exclusive")==0 ){  /* UNDOCUMENTED */
      data.openFlags |= SQLITE_OPEN_EXCLUSIVE;
    }else if( cli_strcmp(z,"-ifexists")==0 ){
      data.openFlags &= ~(SQLITE_OPEN_CREATE);
      if( data.openFlags==0 ) data.openFlags = SQLITE_OPEN_READWRITE;
    }else if( cli_strcmp(z,"-ascii")==0 ){
      modeChange(&data, MODE_Ascii);
    }else if( cli_strcmp(z,"-tabs")==0 ){
      modeChange(&data, MODE_Tabs);
    }else if( cli_strcmp(z,"-separator")==0 ){
      modeSetStr(&data.mode.spec.zColumnSep, 
                       cmdline_option_value(argc,argv,++i));
    }else if( cli_strcmp(z,"-newline")==0 ){
      modeSetStr(&data.mode.spec.zRowSep, 
                       cmdline_option_value(argc,argv,++i));
    }else if( cli_strcmp(z,"-nullvalue")==0 ){
     modeSetStr(&data.mode.spec.zNull, 
                       cmdline_option_value(argc,argv,++i));
    }else if( cli_strcmp(z,"-header")==0 ){
      data.mode.spec.bTitles = QRF_Yes;
      data.mode.mFlags |= MFLG_HDR;
     }else if( cli_strcmp(z,"-noheader")==0 ){
      data.mode.spec.bTitles = QRF_No;
      data.mode.mFlags |= MFLG_HDR;
    }else if( cli_strcmp(z,"-echo")==0 ){
      data.mode.mFlags |= MFLG_ECHO;
    }else if( cli_strcmp(z,"-eqp")==0 ){
      data.mode.autoEQP = AUTOEQP_on;
    }else if( cli_strcmp(z,"-eqpfull")==0 ){
      data.mode.autoEQP = AUTOEQP_full;
    }else if( cli_strcmp(z,"-stats")==0 ){
      data.statsOn = 1;
    }else if( cli_strcmp(z,"-scanstats")==0 ){
      data.mode.scanstatsOn = 1;
    }else if( cli_strcmp(z,"-backslash")==0 ){
      /* Undocumented command-line option: -backslash
      ** Causes C-style backslash escapes to be evaluated in SQL statements
      ** prior to sending the SQL into SQLite.  Useful for injecting
      ** crazy bytes in the middle of SQL statements for testing and debugging.
      */
      ShellSetFlag(&data, SHFLG_Backslash);
    }else if( cli_strcmp(z,"-bail")==0 ){
      /* No-op.  The bail_on_error flag should already be set. */
    }else if( cli_strcmp(z,"-version")==0 ){
      cli_printf(stdout, "%s %s (%d-bit)\n",
            sqlite3_libversion(), sqlite3_sourceid(), 8*(int)sizeof(char*));
      rc = 0;
      goto shell_main_exit;
    }else if( cli_strcmp(z,"-interactive")==0 ){
      /* Need to check for interactive override here to so that it can
      ** affect console setup (for Windows only) and testing thereof.
      */
      stdin_is_interactive = 1;
    }else if( cli_strcmp(z,"-batch")==0 ){
      /* already handled */
    }else if( cli_strcmp(z,"-screenwidth")==0 ){
      i++;      
    }else if( cli_strcmp(z,"-utf8")==0 ){
      /* already handled */
    }else if( cli_strcmp(z,"-no-utf8")==0 ){
      /* already handled */
    }else if( cli_strcmp(z,"-no-rowid-in-view")==0 ){
      /* already handled */
    }else if( cli_strcmp(z,"-heap")==0 ){
      i++;
    }else if( cli_strcmp(z,"-pagecache")==0 ){
      i+=2;
    }else if( cli_strcmp(z,"-lookaside")==0 ){
      i+=2;
    }else if( cli_strcmp(z,"-threadsafe")==0 ){
      i+=2;
    }else if( cli_strcmp(z,"-nonce")==0 ){
      i += 2;
    }else if( cli_strcmp(z,"-mmap")==0 ){
      i++;
    }else if( cli_strcmp(z,"-memtrace")==0 ){
      i++;
    }else if( cli_strcmp(z,"-pcachetrace")==0 ){
      i++;
#ifdef SQLITE_ENABLE_SORTER_REFERENCES
    }else if( cli_strcmp(z,"-sorterref")==0 ){
      i++;
#endif
    }else if( cli_strcmp(z,"-vfs")==0 ){
      i++;
    }else if( cli_strcmp(z,"-vfstrace")==0 ){
      i++;
#ifdef SQLITE_ENABLE_MULTIPLEX
    }else if( cli_strcmp(z,"-multiplex")==0 ){
      i++;
#endif
    }else if( cli_strcmp(z,"-help")==0 ){
      usage(1);
    }else if( cli_strcmp(z,"-cmd")==0 ){
      /* Run commands that follow -cmd first and separately from commands
      ** that simply appear on the command-line.  This seems goofy.  It would
      ** be better if all commands ran in the order that they appear.  But
      ** we retain the goofy behavior for historical compatibility. */
      if( i==argc-1 ) break;
      z = cmdline_option_value(argc,argv,++i);
      if( z[0]=='.' ){
        rc = do_meta_command(z, &data);
        if( rc && (bail_on_error || rc==2) ){
          if( rc==2 ) rc = 0;
          goto shell_main_exit;
        }
      }else{
        rc = runOneSqlLine(&data, z, "cmdline", i);
        if( bail_on_error ) goto shell_main_exit;
      }
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
    }else if( cli_strncmp(z, "-A", 2)==0 ){
      if( nCmd>0 ){
        cli_printf(stderr,"Error: cannot mix regular SQL or dot-commands"
              " with \"%s\"\n", z);
        rc = 1;
        goto shell_main_exit;
      }
      open_db(&data, OPEN_DB_ZIPFILE);
      if( z[2] ){
        argv[i] = &z[2];
        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
      }else{
        arDotCommand(&data, 1, argv+i, argc-i);
      }
      readStdin = 0;
      stdin_is_interactive = 0;
      break;
#endif
    }else if( cli_strcmp(z,"-safe")==0 ){
      data.bSafeMode = data.bSafeModePersist = 1;
    }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
      /* Acted upon in first pass. */
    }else{
      cli_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
      eputz("Use -help for a list of options.\n");
      rc = 1;
      goto shell_main_exit;
    }
  }

  if( !readStdin ){
    /* Run all arguments that do not begin with '-' as if they were separate
    ** command-line inputs, except for the argToSkip argument which contains
    ** the database filename.
    */
    for(i=0; i<nCmd; i++){
      echo_group_input(&data, azCmd[i]);
      if( isScriptFile(azCmd[i],0) ){
        FILE *inSaved = data.in;
        i64 savedLineno = data.lineno;
        int res = 1;
        if( (data.in = openChrSource(azCmd[i]))!=0 ){
          res = process_input(&data, azCmd[i]);
          fclose(data.in);
        }
        data.in = inSaved;
        data.lineno = savedLineno;
        if( res ) i = nCmd;
      }else if( azCmd[i][0]=='.' ){
        char *zErrCtx = malloc( 64 );
        shell_check_oom(zErrCtx);
        sqlite3_snprintf(64,zErrCtx,"argv[%i]:",aiCmd[i]);
        data.zInFile = "<cmdline>";
        data.zErrPrefix = zErrCtx;
        rc = do_meta_command(azCmd[i], &data);
        free(data.zErrPrefix);
        data.zErrPrefix = 0;
        if( rc ){
          if( rc==2 ) rc = 0;
          goto shell_main_exit;
        }
      }else{
        rc = runOneSqlLine(&data, azCmd[i], "cmdline", aiCmd[i]);
        if( data.nPopMode ){
          modePop(&data);
          data.nPopMode = 0;
        }
        if( rc ){
          goto shell_main_exit;
        }
      }
      if( data.nPopOutput && azCmd[i][0]!='.' ){
        output_reset(&data);
        data.nPopOutput = 0;
      }else{
        clearTempFile(&data,0,0);
      }
    }
  }else{
    /* Run commands received from standard input
    */
    if( stdin_is_interactive ){
      char *zHome;
      char *zHistory;
      cli_printf(stdout,
            "SQLite version %s %.19s\n" /*extra-version-info*/
            "Enter \".help\" for usage hints.\n",
            sqlite3_libversion(), sqlite3_sourceid());
      if( warnInmemoryDb ){
        sputz(stdout, "Connected to a ");
        printBold("transient in-memory database");
        sputz(stdout, ".\nUse \".open FILENAME\" to reopen on a"
              " persistent database.\n");
      }
      zHistory = getenv("SQLITE_HISTORY");
      if( zHistory ){
        zHistory = sqlite3_mprintf("%s", zHistory);
        shell_check_oom(zHistory);
      }else{
        zHistory = find_xdg_file("XDG_STATE_HOME",
                                 ".local/state",
                                 "sqlite_history");
        if( 0==zHistory && (zHome = find_home_dir(0))!=0 ){
          zHistory = sqlite3_mprintf("%s/.sqlite_history", zHome);
          shell_check_oom(zHistory);
        }
      }
      if( zHistory ){ shell_read_history(zHistory); }
#if (HAVE_READLINE || HAVE_EDITLINE) && !defined(SQLITE_OMIT_READLINE_COMPLETION)
      rl_attempted_completion_function = readline_completion;
#elif HAVE_LINENOISE==1
      linenoiseSetCompletionCallback(linenoise_completion);
#elif HAVE_LINENOISE==2
      linenoiseSetCompletionCallback(linenoise_completion, NULL);
#endif
      data.in = 0;
      open_db(&data, 0);
      rc = process_input(&data, "<stdin>");
      if( zHistory ){
        shell_stifle_history(2000);
        shell_write_history(zHistory);
        sqlite3_free(zHistory);
      }
    }else{
      data.in = stdin;
      rc = process_input(&data, "<stdin>");
    }
  }
#ifndef SQLITE_SHELL_FIDDLE
  /* In WASM mode we have to leave the db state in place so that
  ** client code can "push" SQL into it after this call returns. */
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
  if( data.expert.pExpert ){
    expertFinish(&data, 1, 0);
  }
#endif
 shell_main_exit:
  free(azCmd);
  free(aiCmd);
  set_table_name(&data, 0);
  if( data.db ){
    session_close_all(&data, -1);
    close_db(data.db);
  }
  for(i=0; i<ArraySize(data.aAuxDb); i++){
    sqlite3_free(data.aAuxDb[i].zFreeOnClose);
    if( data.aAuxDb[i].db ){
      session_close_all(&data, i);
      close_db(data.aAuxDb[i].db);
    }
  }
  find_home_dir(1);
  output_reset(&data);
  data.doXdgOpen = 0;
  clearTempFile(&data,2500,1);
  globalShellState = 0;
  while( data.nModeStack ) modePop(&data);
  free(data.aModeStack);
  modeFree(&data.mode);
  if( data.nSavedModes ){
    int ii;
    for(ii=0; ii<data.nSavedModes; ii++){
      modeFree(&data.aSavedModes[ii].mode);
      free(data.aSavedModes[ii].zTag);
    }
    free(data.aSavedModes);
  }
  free(data.zErrPrefix);
  free(data.zNonce);
  free(data.dot.zCopy);
  free(data.dot.azArg);
  free(data.dot.aiOfst);
  free(data.dot.abQuot);
  free(data.azPrompt[0]);
  free(data.azPrompt[1]);
  if( data.nTestRun ){
    sqlite3_fprintf(stdout, "%d test%s run with %d error%s\n",
       data.nTestRun, data.nTestRun==1 ? "" : "s",
       data.nTestErr, data.nTestErr==1 ? "" : "s");
    fflush(stdout);
    rc = data.nTestErr>0;
  }
  /* Clear the global data structure so that valgrind will detect memory
  ** leaks */
  memset(&data, 0, sizeof(data));
  if( bEnableVfstrace ){
    vfstrace_unregister("trace");
  }
  sqlite3_reset_auto_extension();
#ifdef SQLITE_DEBUG
  if( sqlite3_memory_used()>mem_main_enter ){
    cli_printf(stderr,"Memory leaked: %u bytes\n",
          (unsigned int)(sqlite3_memory_used()-mem_main_enter));
  }
#endif
#else /* SQLITE_SHELL_FIDDLE... */
  shell_main_exit:
#endif
  return rc;
}


#ifdef SQLITE_SHELL_FIDDLE
/* Only for emcc experimentation purposes. */
int fiddle_experiment(int a,int b){
  return a + b;
}

/*
** Returns a pointer to the current DB handle.
*/
sqlite3 * fiddle_db_handle(){
  return globalDb;
}

/*
** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
** "main" is assumed. Returns 0 if no db with the given name is
** open.
*/
sqlite3_vfs * fiddle_db_vfs(const char *zDbName){
  sqlite3_vfs * pVfs = 0;
  if(globalDb){
    sqlite3_file_control(globalDb, zDbName ? zDbName : "main",
                         SQLITE_FCNTL_VFS_POINTER, &pVfs);
  }
  return pVfs;
}

/* Only for emcc experimentation purposes. */
sqlite3 * fiddle_db_arg(sqlite3 *arg){
    cli_printf(stdout, "fiddle_db_arg(%p)\n", (const void*)arg);
    return arg;
}

/*
** Intended to be called via a SharedWorker() while a separate
** SharedWorker() (which manages the wasm module) is performing work
** which should be interrupted. Unfortunately, SharedWorker is not
** portable enough to make real use of.
*/
void fiddle_interrupt(void){
  if( globalDb ) sqlite3_interrupt(globalDb);
}

/*
** Returns the filename of the given db name, assuming "main" if
** zDbName is NULL. Returns NULL if globalDb is not opened.
*/
const char * fiddle_db_filename(const char * zDbName){
    return globalDb
      ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
      : NULL;
}

/*
** Completely wipes out the contents of the currently-opened database
** but leaves its storage intact for reuse. If any transactions are
** active, they are forcibly rolled back.
*/
void fiddle_reset_db(void){
  if( globalDb ){
    int rc;
    while( sqlite3_txn_state(globalDb,0)>0 ){
      /*
      ** Resolve problem reported in
      ** https://sqlite.org/forum/forumpost/0b41a25d65
      */
      cli_puts("Rolling back in-progress transaction.\n", stdout);
      sqlite3_exec(globalDb,"ROLLBACK", 0, 0, 0);
    }
    rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
    if( 0==rc ) sqlite3_exec(globalDb, "VACUUM", 0, 0, 0);
    sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
  }
}

/*
** Uses the current database's VFS xRead to stream the db file's
** contents out to the given callback. The callback gets a single
** chunk of size n (its 2nd argument) on each call and must return 0
** on success, non-0 on error. This function returns 0 on success,
** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
** code from the callback. Note that this is not thread-friendly: it
** expects that it will be the only thread reading the db file and
** takes no measures to ensure that is the case.
*/
int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){
  sqlite3_int64 nSize = 0;
  sqlite3_int64 nPos = 0;
  sqlite3_file * pFile = 0;
  unsigned char buf[1024 * 8];
  int nBuf = (int)sizeof(buf);
  int rc = shellState.db
    ? sqlite3_file_control(shellState.db, "main",
                           SQLITE_FCNTL_FILE_POINTER, &pFile)
    : SQLITE_NOTFOUND;
  if( rc ) return rc;
  rc = pFile->pMethods->xFileSize(pFile, &nSize);
  if( rc ) return rc;
  if(nSize % nBuf){
    /* DB size is not an even multiple of the buffer size. Reduce
    ** buffer size so that we do not unduly inflate the db size when
    ** exporting. */
    if(0 == nSize % 4096) nBuf = 4096;
    else if(0 == nSize % 2048) nBuf = 2048;
    else if(0 == nSize % 1024) nBuf = 1024;
    else nBuf = 512;
  }
  for( ; 0==rc && nPos<nSize; nPos += nBuf ){
    rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);
    if(SQLITE_IOERR_SHORT_READ == rc){
      rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/;
    }
    if( 0==rc ) rc = xCallback(buf, nBuf);
  }
  return rc;
}

/*
** Trivial exportable function for emscripten. It processes zSql as if
** it were input to the sqlite3 shell and redirects all output to the
** wasm binding. fiddle_main() must have been called before this
** is called, or results are undefined.
*/
void fiddle_exec(const char * zSql){
  if(zSql && *zSql){
    if('.'==*zSql) puts(zSql);
    shellState.wasm.zInput = zSql;
    shellState.wasm.zPos = zSql;
    process_input(&shellState, "<stdin>");
    shellState.wasm.zInput = shellState.wasm.zPos = 0;
  }
}

char *fiddle_get_prompt(void){
  return expand_prompt(&shellState, 0, prompt_string(&shellState,0));
}
#endif /* SQLITE_SHELL_FIDDLE */
