]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Sync with trunk
authorlarrybr <larrybr@noemail.net>
Mon, 7 Mar 2022 19:00:25 +0000 (19:00 +0000)
committerlarrybr <larrybr@noemail.net>
Mon, 7 Mar 2022 19:00:25 +0000 (19:00 +0000)
FossilOrigin-Name: 1194093a58978b99c7afee08483d0590ce9aff4647a46dc9a338f022647fdb42

1  2 
manifest
manifest.uuid
src/shell.c.in
test/shell5.test

diff --cc manifest
index c1d97fc1e518cbdca02106e88d434ad68db13498,4d2ba895f9142a69419856e3cf1670f21cb92068..29bc1cb75ccf9a2ea011da0639c6a4b4281f1321
+++ b/manifest
@@@ -1,5 -1,5 +1,5 @@@
- C (WIP)\sShell\smeta-command\sobjectification\sdone,\sready\sfor\sdbShell\sto\sbe\sloaded\sand\sused\sfor\sextension\smeta-commands
- D 2022-03-06T17:53:09.997
 -C In\sthe\sstay-on-last-page\soptimization\s\sfor\ssqlite3BtreeIndexMoveto()\n(check-in\s[0057bbb508e7662b]\sabout\s16\shours\sago),\sbe\ssure\nto\sclear\sthe\sBTCF_ValidOvfl\sflag,\ssince\sthe\soverflow\scache\sis\sinvalidated\nby\sthe\ssearch\son\sthe\slast\spage.\s\sOSSFuzz\sissue\s45329.
 -D 2022-03-07T17:19:40.649
++C Sync\swith\strunk
++D 2022-03-07T19:00:25.488
  F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
  F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
  F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@@ -554,13 -552,12 +554,13 @@@ F src/printf.c 05d8dfd2018bc4fc3ddb8b37
  F src/random.c 097dc8b31b8fba5a9aca1697aeb9fd82078ec91be734c16bffda620ced7ab83c
  F src/resolve.c ea935b87d6fb36c78b70cdc7b28561dc8f33f2ef37048389549c7b5ef9b0ba5e
  F src/rowset.c ba9515a922af32abe1f7d39406b9d35730ed65efab9443dc5702693b60854c92
- F src/select.c 3baa9dd8cf240654773c7974e2bcce398ac9dd24419c36684156963defe43b35
- F src/shell.c.in b5ac2daac19a40b0359ef1c55a8916e385854a46813dd3fa41895f6b39773cc8
+ F src/select.c 4890a3cfee0bc60ff231c3a44db37968859ab0be156983dbcc0c096109832cdd
 -F src/shell.c.in 0a9c4b9226143365c8aea42eb9a62e055e9fffffc5e4559da5c9dadc0bedaa25
++F src/shell.c.in 92f8c98677e6e9b192ee77927ecd2ab1af2c8e3e5daca06e52f06d451037f705
 +F src/shext_linkage.h bff86090e82d1df3a6dd0c566324f38ac67cb75d6e72697d9a8a3065545b4f0c
- F src/sqlite.h.in e30cedf008d9c51511f4027a3739b727a588da553424748b48d2393f85dbde41
+ F src/sqlite.h.in e82ac380b307659d0892f502b742f825504e78729f4edaadce946003b9c00816
  F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
  F src/sqlite3ext.h a95cb9ed106e3d39e2118e4dcc15a14faec3fa50d0093425083d340d9dfd96e6
- F src/sqliteInt.h f8814239fb1f95056555e2d7fa475750e64681cac4221fb03610d1fde0b79d53
+ F src/sqliteInt.h 4a8b7d685781df55ce546b6c72bdbd72bb23ca93f59b6d70d81ed6df22607d75
  F src/sqliteLimit.h d7323ffea5208c6af2734574bae933ca8ed2ab728083caa117c9738581a31657
  F src/status.c 4b8bc2a6905163a38b739854a35b826c737333fab5b1f8e03fa7eb9a4799c4c1
  F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1
@@@ -1392,11 -1388,11 +1392,11 @@@ F test/sharedA.test 49d87ec54ab640fbbc3
  F test/sharedB.test 16cc7178e20965d75278f410943109b77b2e645e
  F test/shared_err.test 32634e404a3317eeb94abc7a099c556a346fdb8fb3858dbe222a4cbb8926a939
  F test/sharedlock.test 5ede3c37439067c43b0198f580fd374ebf15d304
 -F test/shell1.test b224e0793c5f48aa3749e65d8c64b93a30731bd206f2e41e6c5f1bee1bdb16c6
 -F test/shell2.test 89e4b2db062d52baed75022227b462d085cff495809de1699652779d8e0257d6
 -F test/shell3.test a50628ab1d78d90889d9d3f32fb2c084ee15674771e96afe954aaa0accd1de3c
 -F test/shell4.test 8f6c0fce4abed19a8a7f7262517149812a04caa905d01bdc8f5e92573504b759
 -F test/shell5.test 0a9920d81fae28c45cd5dbd1deb809487a23c5f4b422a49f9d31c85f926d4a9c
 +F test/shell1.test 2a8d73538f672c5b5b238ba2fa15e4fbaea47454f372dd5b683be51727af33cc
 +F test/shell2.test aed8773dd236517149a6abaeccf94f066e5a0c871108e8d21889122132dd51ba
 +F test/shell3.test 4ddea2bd182e7e03249911b23ae249e7cb8a91cdc86e695198725affabe8ecd3
 +F test/shell4.test 867e0675d7b096d6b93de534541e07c7f5ffafa3e6612695ecf55180802e1115
- F test/shell5.test 839a4489bc97d6f2028ffd036d500d2b8cab1657ff477bbcd635184f8e8b3887
++F test/shell5.test 21f3d54a0bc73588efe023c2af846d48d04420d8df7f4ee0fc465719fcbad756
  F test/shell6.test 1ceb51b2678c472ba6cf1e5da96679ce8347889fe2c3bf93a0e0fa73f00b00d3
  F test/shell7.test 115132f66d0463417f408562cc2cf534f6bbc6d83a6d50f0072a9eb171bae97f
  F test/shell8.test 388471d16e4de767333107e30653983f186232c0e863f4490bb230419e830aae
@@@ -1880,10 -1875,10 +1880,10 @@@ F tool/mkctimec.tcl 3147e9dfc4ad774e94f
  F tool/mkkeywordhash.c 35bfc41adacc4aa6ef6fca7fd0c63e0ec0534b78daf4d0cfdebe398216bbffc3
  F tool/mkmsvcmin.tcl 6ecab9fe22c2c8de4d82d4c46797bda3d2deac8e763885f5a38d0c44a895ab33
  F tool/mkopcodec.tcl 33d20791e191df43209b77d37f0ff0904620b28465cca6990cf8d60da61a07ef
- F tool/mkopcodeh.tcl 130b88697da6ec5b89b41844d955d08fb62c2552e889dec8c7bcecb28d8f50bd
+ F tool/mkopcodeh.tcl 5dab48c49a25452257494e9601702ab63adaba6bd54a9b382615fa52661c8f8c
  F tool/mkopts.tcl 680f785fdb09729fd9ac50632413da4eadbdf9071535e3f26d03795828ab07fa
  F tool/mkpragmatab.tcl bd07bd59d45d0f3448e123d6937e9811195f9908a51e09d774609883055bfd3d
 -F tool/mkshellc.tcl df5d249617f9cc94d5c48eb0401673eb3f31f383ecbc54e8a13ca3dd97e89450
 +F tool/mkshellc.tcl 8b59d0312c708acb00b9242a2eb79cdf3a84d972c06bb09447599ebadc1d5cbf
  F tool/mksourceid.c 36aa8020014aed0836fd13c51d6dc9219b0df1761d6b5f58ff5b616211b079b9
  F tool/mkspeedsql.tcl a1a334d288f7adfe6e996f2e712becf076745c97
  F tool/mksqlite3c-noext.tcl 4f7cfef5152b0c91920355cbfc1d608a4ad242cb819f1aea07f6d0274f584a7f
@@@ -1949,8 -1944,8 +1949,8 @@@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a9
  F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
  F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
  F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
- P a85679d3e9e1edf71ed6308377783cdaa677c51290f644a28da9a69d429e7be9
- R ba61495e364ca7be34de1f25f5104c9e
 -P 5e0ed49b3d739d292f5df3e498449ae8f4357cbb83394181fb34f98ed8372707
 -R f716eefd212005e4b7e38ae3bd823c39
 -U drh
 -Z e8342be86d82f1f58876f0baf6aae742
++P a88983ecb7e2905219898e17d11de3598ee801c57b8ba32c51cb2790456a0e5b 0021bebc162e001b788786703ce634e7b8fcd3976f7047a5956e82140791e765
++R 2e72e0cc424e9311255d66424a9824eb
 +U larrybr
- Z 9c5099ee4121c6747f3b8f1539d8df34
++Z d510d7fd6f6e96bbcf87e6ed60b7095d
  # Remove this line to create a well-formed Fossil manifest.
diff --cc manifest.uuid
index c4e9b231e1242f76a524e88bc9d026e4c93850c4,9f01f7ac6634d29f6d2af57c92e64155a5304652..2b431c2ffb7e1ae8b41a24fdd45ddf16941e35f7
@@@ -1,1 -1,1 +1,1 @@@
- a88983ecb7e2905219898e17d11de3598ee801c57b8ba32c51cb2790456a0e5b
 -0021bebc162e001b788786703ce634e7b8fcd3976f7047a5956e82140791e765
++1194093a58978b99c7afee08483d0590ce9aff4647a46dc9a338f022647fdb42
diff --cc src/shell.c.in
index 9d5d9d3011859ec3b3cc1ed299c264ca0bac0aa3,b0f3b02dc47b20c03aa987adb6a12bd8cec30e42..5cd6093e2bb9fc310ba097ab9737323de9676d8a
@@@ -8024,3368 -8249,1614 +8024,3373 @@@ DISPATCHABLE_COMMAND( check 3 0 0 )
    ** 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 && strncmp(azArg[0], "check", n)==0 ){
 -    char *zRes = 0;
 -    output_reset(p);
 -    if( nArg!=2 ){
 -      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
 -      rc = 2;
 -    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
 -      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
 -      rc = 2;
 -    }else if( testcase_glob(azArg[1],zRes)==0 ){
 -      utf8_printf(stderr,
 -                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
 -                 p->zTestcase, azArg[1], zRes);
 -      rc = 1;
 -    }else{
 -      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
 -      p->nCheck++;
 +  char *zRes = 0;
 +  int rc=0;
 +  output_reset(ISS(p));
 +  if( nArg!=2 ){
 +    return SHELL_INVALID_ARGS;
 +  }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
 +    *pzErr = shellMPrintf(&rc, "Error: cannot read 'testcase-out.txt'");
 +    rc = 2;
 +  }else if( testcase_glob(azArg[1],zRes)==0 ){
 +    *pzErr =
 +      shellMPrintf(&rc,
 +                   "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
 +                   ISS(p)->zTestcase, azArg[1], zRes);
 +    rc = 1;
 +  }else{
 +    utf8_printf(STD_OUT, "testcase-%s ok\n", ISS(p)->zTestcase);
 +    ISS(p)->nCheck++;
 +  }
 +  sqlite3_free(zRes);
 +  return rc;
 +}
 +DISPATCHABLE_COMMAND( clone ? 2 2 ){
 +  if( ISS(p)->bSafeMode ) return SHELL_FORBIDDEN_OP;
 +  tryToClone(p, azArg[1]);
 +  return 0;
 +}
 +DISPATCHABLE_COMMAND( connection ? 1 4 ){
 +  ShellInState *psi = ISS(p);
 +  if( nArg==1 ){
 +    /* List available connections */
 +    int i;
 +    for(i=0; i<ArraySize(psi->aAuxDb); i++){
 +      const char *zFile = psi->aAuxDb[i].zDbFilename;
 +      if( psi->aAuxDb[i].db==0 && psi->pAuxDb!=&psi->aAuxDb[i] ){
 +        zFile = "(not open)";
 +      }else if( zFile==0 ){
 +        zFile = "(memory)";
 +      }else if( zFile[0]==0 ){
 +        zFile = "(temporary-file)";
 +      }
 +      if( psi->pAuxDb == &psi->aAuxDb[i] ){
 +        utf8_printf(STD_OUT, "ACTIVE %d: %s\n", i, zFile);
 +      }else if( psi->aAuxDb[i].db!=0 ){
 +        utf8_printf(STD_OUT, "       %d: %s\n", i, zFile);
 +      }
 +    }
 +  }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
 +    int i = azArg[1][0] - '0';
 +    if( psi->pAuxDb != &psi->aAuxDb[i] && i>=0 && i<ArraySize(psi->aAuxDb) ){
 +      psi->pAuxDb->db = DBI(psi);
 +      psi->pAuxDb = &psi->aAuxDb[i];
 +      globalDb = DBI(psi) = psi->pAuxDb->db;
 +      psi->pAuxDb->db = 0;
 +    }
 +  }else if( nArg==3 && strcmp(azArg[1], "close")==0
 +            && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
 +    int i = azArg[2][0] - '0';
 +    if( i<0 || i>=ArraySize(psi->aAuxDb) ){
 +      /* No-op */
 +    }else if( psi->pAuxDb == &psi->aAuxDb[i] ){
 +      raw_printf(STD_ERR, "cannot close the active database connection\n");
 +      return 1;
 +    }else if( psi->aAuxDb[i].db ){
 +      session_close_all(psi, i);
 +      close_db(psi->aAuxDb[i].db);
 +      psi->aAuxDb[i].db = 0;
      }
 -    sqlite3_free(zRes);
 -  }else
 +  }else{
 +    return SHELL_INVALID_ARGS;
 +  }
 +  return 0;
 +}
  
 -  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
 -    failIfSafeMode(p, "cannot run .clone in safe mode");
 -    if( nArg==2 ){
 -      tryToClone(p, azArg[1]);
 -    }else{
 -      raw_printf(stderr, "Usage: .clone FILENAME\n");
 -      rc = 1;
 +/*****************
 + * The .databases, .dbconfig and .dbinfo commands
 + */
 +COLLECT_HELP_TEXT[
 +  ".databases               List names and files of attached databases",
 +  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
 +  ".dbinfo ?DB?             Show status information about the database",
 +];
 +/* Allow garbage arguments on this, to be ignored. */
 +DISPATCHABLE_COMMAND( databases 2 1 0 ){
 +  int rc;
 +  char **azName = 0;
 +  int nName = 0;
 +  sqlite3_stmt *pStmt;
 +  sqlite3 *db;
 +  int i;
 +  open_db(p, 0);
 +  db = DBX(p);
 +  rc = sqlite3_prepare_v2(db, "PRAGMA database_list", -1, &pStmt, 0);
 +  if( rc ){
 +    *pzErr = shellMPrintf(0,"Error: %s\n", sqlite3_errmsg(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_realloc(azName, (nName+1)*2*sizeof(char*));
 +      shell_check_oom(azName);
 +      azName[nName*2] = strdup(zSchema);
 +      shell_check_oom(azName[nName*2]);
 +      azName[nName*2+1] = strdup(zFile);
 +      shell_check_oom(azName[nName*2+1]);
 +      nName++;
      }
 -  }else
 +  }
 +  sqlite3_finalize(pStmt);
 +  for(i=0; i<nName; i++){
 +    int eTxn = sqlite3_txn_state(db, azName[i*2]);
 +    int bRdonly = sqlite3_db_readonly(db, azName[i*2]);
 +    const char *z = azName[i*2+1];
 +    utf8_printf(ISS(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);
 +  return rc;
 +}
 +DISPATCHABLE_COMMAND( dbconfig 3 1 3 ){
 +  static const struct DbConfigChoices {
 +    const char *zName;
 +    int op;
 +  } aDbConfig[] = {
 +    { "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 },
 +    { "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        },
 +    { "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 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
 +    if( nArg>=3 ){
 +      sqlite3_db_config(DBX(p), aDbConfig[ii].op, booleanValue(azArg[2]), 0);
 +    }
 +    sqlite3_db_config(DBX(p), aDbConfig[ii].op, -1, &v);
 +    utf8_printf(ISS(p)->out, "%19s %s\n",
 +                aDbConfig[ii].zName, v ? "on" : "off");
 +    if( nArg>1 ) break;
 +  }
 +  if( nArg>1 && ii==ArraySize(aDbConfig) ){
 +    *pzErr = sqlite3_mprintf
 +      ("Error: unknown dbconfig \"%s\"\n"
 +       "Enter \".dbconfig\" with no arguments for a list\n",
 +       azArg[1]);
 +    return 1;
 +  }
 +  return 0;
 +}
 +DISPATCHABLE_COMMAND( dbinfo 3 1 2 ){
 +  return shell_dbinfo_command(p, nArg, azArg);
 +}
  
 -  if( c=='c' && 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] ){
 -          utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
 -        }else if( p->aAuxDb[i].db!=0 ){
 -          utf8_printf(stdout, "       %d: %s\n", i, zFile);
 +/*****************
 + * The .dump, .echo and .eqp commands
 + */
 +COLLECT_HELP_TEXT[
 +  ".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",
 +  "     --schema SCHEMA        Dump table(s) from given SCHEMA",
 +  "   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",
 +];
 +DISPATCHABLE_COMMAND( dump ? 1 2 ){
 +  ShellInState *psi = ISS(p);
 +  char *zLike = 0;
 +  char *zSchema = "main";
 +  char *zSql;
 +  int i;
 +  int savedShowHeader = psi->showHeader;
 +  int savedShellFlags = psi->shellFlgs;
 +  ShellClearFlag(p,
 +     SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
 +     |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( strcmp(z,"preserve-rowids")==0 ){
 +#ifdef SQLITE_OMIT_VIRTUALTABLE
 +        *pzErr = sqlite3_mprintf
 +          ("The --preserve-rowids option is not compatible"
 +                   " with SQLITE_OMIT_VIRTUALTABLE\n");
 +        sqlite3_free(zLike);
 +        return 1;
 +#else
 +        ShellSetFlag(p, SHFLG_PreserveRowid);
 +#endif
 +      }else{
 +        if( strcmp(z,"newlines")==0 ){
 +          ShellSetFlag(p, SHFLG_Newlines);
 +        }else if( strcmp(z,"data-only")==0 ){
 +          ShellSetFlag(p, SHFLG_DumpDataOnly);
 +        }else if( strcmp(z,"nosys")==0 ){
 +          ShellSetFlag(p, SHFLG_DumpNoSys);
 +        }else if( strcmp(z,"schema")==0 && ++i<nArg ){
 +          zSchema = azArg[i];
 +        }else{
 +          *pzErr = sqlite3_mprintf
 +            ("Unknown option \"%s\" on \".dump\"\n", azArg[i]);
 +          sqlite3_free(zLike);
 +          return 1;
          }
        }
 -    }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 && 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] ){
 -        raw_printf(stderr, "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{
 -      raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
 -      rc = 1;
 +      /* 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 %w.sqlite_schema WHERE "
 +                    "    name LIKE %Q ESCAPE '\\' AND"
 +                    "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
 +                    "    substr(o.name, 1, length(name)+1) == (name||'_')"
 +                    ")", azArg[i], zSchema, azArg[i]
 +                    );
 +
 +      if( zLike ){
 +        zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
 +      }else{
 +        zLike = zExpr;
 +      }
      }
 -  }else
 +  }
  
 -  if( c=='d' && n>1 && 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 ){
 -      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(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_realloc(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];
 -      utf8_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 && strncmp(azArg[0], "dbconfig", n)==0 ){
 -    static const struct DbConfigChoices {
 -      const char *zName;
 -      int op;
 -    } aDbConfig[] = {
 -        { "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 },
 -        { "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        },
 -        { "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 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
 -      if( nArg>=3 ){
 -        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
 -      }
 -      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
 -      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
 -      if( nArg>1 ) break;
 -    }
 -    if( nArg>1 && ii==ArraySize(aDbConfig) ){
 -      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
 -      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
 -    }   
 -  }else
 -
 -  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
 -    rc = shell_dbinfo_command(p, nArg, azArg);
 -  }else
 -
 -#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
 -  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
 -    open_db(p, 0);
 -    rc = recoverDatabaseCmd(p, nArg, azArg);
 -  }else
 -#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
 -
 -  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
 -    char *zLike = 0;
 -    char *zSql;
 -    int i;
 -    int savedShowHeader = p->showHeader;
 -    int savedShellFlags = p->shellFlgs;
 -    ShellClearFlag(p, 
 -       SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
 -       |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( strcmp(z,"preserve-rowids")==0 ){
 -#ifdef SQLITE_OMIT_VIRTUALTABLE
 -          raw_printf(stderr, "The --preserve-rowids option is not compatible"
 -                             " with SQLITE_OMIT_VIRTUALTABLE\n");
 -          rc = 1;
 -          sqlite3_free(zLike);
 -          goto meta_command_exit;
 -#else
 -          ShellSetFlag(p, SHFLG_PreserveRowid);
 -#endif
 -        }else
 -        if( strcmp(z,"newlines")==0 ){
 -          ShellSetFlag(p, SHFLG_Newlines);
 -        }else
 -        if( strcmp(z,"data-only")==0 ){
 -          ShellSetFlag(p, SHFLG_DumpDataOnly);
 -        }else
 -        if( strcmp(z,"nosys")==0 ){
 -          ShellSetFlag(p, SHFLG_DumpNoSys);
 -        }else
 -        {
 -          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
 -          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);
 +  open_db(p, 0);
  
 -    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. */
 -      raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
 -      raw_printf(p->out, "BEGIN TRANSACTION;\n");
 -    }
 -    p->writableSchema = 0;
 -    p->showHeader = 0;
 -    /* 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");
 +  if( (psi->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. */
 +    raw_printf(psi->out, "PRAGMA foreign_keys=OFF;\n");
 +    raw_printf(psi->out, "BEGIN TRANSACTION;\n");
 +  }
 +  psi->writableSchema = 0;
 +  psi->showHeader = 0;
 +  /* 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(DBX(p), "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
 +  psi->nErr = 0;
 +  if( zLike==0 ) zLike = sqlite3_mprintf("true");
 +  zSql = sqlite3_mprintf(
 +           "SELECT name, type, sql FROM %w.sqlite_schema AS o "
 +           "WHERE (%s) AND type=='table'"
 +           "  AND sql NOT NULL"
 +           " ORDER BY tbl_name='sqlite_sequence', rowid",
 +           zSchema, zLike
 +         );
 +  run_schema_dump_query(psi,zSql);
 +  sqlite3_free(zSql);
 +  if( (psi->shellFlgs & SHFLG_DumpDataOnly)==0 ){
      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);
 +             "SELECT sql FROM sqlite_schema AS o "
 +             "WHERE (%s) AND sql NOT NULL"
 +             "  AND type IN ('index','trigger','view')",
 +             zLike
 +           );
 +    run_table_dump_query(psi, 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')",
 -        zLike
 -      );
 -      run_table_dump_query(p, zSql);
 -      sqlite3_free(zSql);
 -    }
 -    sqlite3_free(zLike);
 -    if( p->writableSchema ){
 -      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
 -      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 ){
 -      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
 -    }
 -    p->showHeader = savedShowHeader;
 -    p->shellFlgs = savedShellFlags;
 -  }else
 +  }
 +  sqlite3_free(zLike);
 +  if( psi->writableSchema ){
 +    raw_printf(psi->out, "PRAGMA writable_schema=OFF;\n");
 +    psi->writableSchema = 0;
 +  }
 +  sqlite3_exec(DBX(p), "PRAGMA writable_schema=OFF;", 0, 0, 0);
 +  sqlite3_exec(DBX(p), "RELEASE dump;", 0, 0, 0);
 +  if( (psi->shellFlgs & SHFLG_DumpDataOnly)==0 ){
 +    raw_printf(psi->out, psi->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
 +  }
 +  psi->showHeader = savedShowHeader;
 +  psi->shellFlgs = savedShellFlags;
  
 -  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
 -    if( nArg==2 ){
 -      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
 +  return 0;
 +}
 +DISPATCHABLE_COMMAND( echo ? 2 2 ){
 +  setOrClearFlag(p, SHFLG_Echo, azArg[1]);
 +  return 0;
 +}
 +DISPATCHABLE_COMMAND( eqp ? 0 0 ){
 +  ShellInState *psi = ISS(p);
 +  if( nArg==2 ){
 +    psi->autoEQPtest = 0;
 +    if( psi->autoEQPtrace ){
 +      if( DBX(p) ) sqlite3_exec(DBX(p), "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
 +      psi->autoEQPtrace = 0;
 +    }
 +    if( strcmp(azArg[1],"full")==0 ){
 +      psi->autoEQP = AUTOEQP_full;
 +    }else if( strcmp(azArg[1],"trigger")==0 ){
 +      psi->autoEQP = AUTOEQP_trigger;
 +#ifdef SQLITE_DEBUG
 +    }else if( strcmp(azArg[1],"test")==0 ){
 +      psi->autoEQP = AUTOEQP_on;
 +      psi->autoEQPtest = 1;
 +    }else if( strcmp(azArg[1],"trace")==0 ){
 +      psi->autoEQP = AUTOEQP_full;
 +      psi->autoEQPtrace = 1;
 +      open_db(p, 0);
 +      sqlite3_exec(DBX(p), "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
 +      sqlite3_exec(DBX(p), "PRAGMA vdbe_trace=ON;", 0, 0, 0);
 +#endif
      }else{
 -      raw_printf(stderr, "Usage: .echo on|off\n");
 -      rc = 1;
 +      psi->autoEQP = (u8)booleanValue(azArg[1]);
      }
 -  }else
 +  }else{
 +    return SHELL_INVALID_ARGS;
 +  }
 +}
  
 -  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
 -    if( nArg==2 ){
 -      p->autoEQPtest = 0;
 -      if( p->autoEQPtrace ){
 -        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
 -        p->autoEQPtrace = 0;
 -      }
 -      if( strcmp(azArg[1],"full")==0 ){
 -        p->autoEQP = AUTOEQP_full;
 -      }else if( strcmp(azArg[1],"trigger")==0 ){
 -        p->autoEQP = AUTOEQP_trigger;
 -#ifdef SQLITE_DEBUG
 -      }else if( strcmp(azArg[1],"test")==0 ){
 -        p->autoEQP = AUTOEQP_on;
 -        p->autoEQPtest = 1;
 -      }else if( strcmp(azArg[1],"trace")==0 ){
 -        p->autoEQP = AUTOEQP_full;
 -        p->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->autoEQP = (u8)booleanValue(azArg[1]);
 -      }
 +/*****************
 + * The .expert and .explain commands
 + */
 +CONDITION_COMMAND( expert !defined(SQLITE_OMIT_VIRTUALTABLE) );
 +COLLECT_HELP_TEXT[
 +  ".expert                  Suggest indexes for queries",
 +  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode. Default: auto",
 +];
 +DISPATCHABLE_COMMAND( expert ? 1 1 ){
 +  open_db(p, 0);
 +  expertDotCommand(ISS(p), azArg, nArg);
 +  return 0;
 +}
 +DISPATCHABLE_COMMAND( explain ? 1 2 ){
 +  /* The ".explain" command is automatic now.  It is largely
 +  ** pointless, retained purely for backwards compatibility */
 +  ShellInState *psi = ISS(p);
 +  int val = 1;
 +  if( nArg>1 ){
 +    if( strcmp(azArg[1],"auto")==0 ){
 +      val = 99;
      }else{
 -      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
 -      rc = 1;
 +      val = booleanValue(azArg[1]);
      }
 -  }else
 +  }
 +  if( val==1 && psi->mode!=MODE_Explain ){
 +    psi->normalMode = psi->mode;
 +    psi->mode = MODE_Explain;
 +    psi->autoExplain = 0;
 +  }else if( val==0 ){
 +    if( psi->mode==MODE_Explain ) psi->mode = psi->normalMode;
 +    psi->autoExplain = 0;
 +  }else if( val==99 ){
 +    if( psi->mode==MODE_Explain ) psi->mode = psi->normalMode;
 +    psi->autoExplain = 1;
 +  }
 +  return 0;
 +}
  
 -  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
 -    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
 -    rc = 2;
 -  }else
 +/*****************
 + * The .excel, .once and .output commands
 + * These share much implementation, so they stick together.
 + */
 +COLLECT_HELP_TEXT[
 +  ".excel                   Display the output of next command in spreadsheet",
 +  "   --bom                   Prefix the file with a UTF8 byte-order mark",
 +  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
 +  "   If FILE begins with '|' then open it as a command to be piped into.",
 +  "   Options:",
 +  "     --bom                 Prefix output with a UTF8 byte-order mark",
 +  "     -e                    Send output to the system text editor",
 +  "     -x       Send output as CSV to a spreadsheet (same as \".excel\")",
 +  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
 +  "   If FILE begins with '|' then open it as a command to be piped into.",
 +  "   Options:",
 +  "     --bom                 Prefix output with a UTF8 byte-order mark",
 +  "     -e                    Send output to the system text editor",
 +  "     -x       Send output as CSV to a spreadsheet (same as \".excel\")",
 +];
 +static int outputRedirs(char *[], int, ShellInState *,
 +                        char **pzErr, int bOnce, int eMode);
 +DISPATCHABLE_COMMAND( excel ? 1 2 ){
 +  return outputRedirs(azArg, nArg, ISS(p), pzErr, 2, 'x');
 +}
 +DISPATCHABLE_COMMAND( once ? 1 6 ){
 +  return outputRedirs(azArg, nArg, ISS(p), pzErr, 1, 0);
 +}
 +DISPATCHABLE_COMMAND( output ? 1 6 ){
 +  return outputRedirs(azArg, nArg, ISS(p), pzErr, 0, 0);
 +}
  
 -  /* The ".explain" command is automatic now.  It is largely pointless.  It
 -  ** retained purely for backwards compatibility */
 -  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
 -    int val = 1;
 -    if( nArg>=2 ){
 -      if( strcmp(azArg[1],"auto")==0 ){
 -        val = 99;
 +static int outputRedirs(char *azArg[], int nArg, ShellInState *psi,
 +                        char **pzErr, int bOnce, int eMode){
 +  /* bOnce => 0: .output, 1: .once, 2: .excel */
 +  /* eMode => 'x' for excel, else 0 */
 +  int rc = 0;
 +  char *zFile = 0;
 +  int bTxtMode = 0;
 +  int i;
 +  int bBOM = 0;
 +  if( psi->bSafeMode ) return SHELL_FORBIDDEN_OP;
 +  for(i=1; i<nArg; i++){
 +    char *z = azArg[i];
 +    if( z[0]=='-' ){
 +      if( z[1]=='-' ) z++;
 +      if( strcmp(z,"-bom")==0 ){
 +        bBOM = 1;
 +      }else if( bOnce!=2 && strcmp(z,"-x")==0 ){
 +        eMode = 'x';  /* spreadsheet */
 +      }else if( bOnce!=2 && strcmp(z,"-e")==0 ){
 +        eMode = 'e';  /* text editor */
        }else{
 -        val =  booleanValue(azArg[1]);
 +        *pzErr = shellMPrintf(0,"       unknown option: \"%s\"\n",azArg[i]);
 +        return SHELL_INVALID_ARGS;
 +      }
 +    }else if( zFile==0 && eMode!='e' && eMode!='x' ){
 +      zFile = sqlite3_mprintf("%s", z);
 +      shell_check_oom(zFile);
 +      if( zFile[0]=='|' ){
 +        while( i+1<nArg ){
 +          zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
 +          shell_check_oom(zFile);
 +        }
 +        break;
        }
 +    }else{
 +      *pzErr = shellMPrintf(0,"       excess argument: \"%s\"\n", azArg[i]);
 +      sqlite3_free(zFile);
 +      return SHELL_INVALID_ARGS;
      }
 -    if( val==1 && p->mode!=MODE_Explain ){
 -      p->normalMode = p->mode;
 -      p->mode = MODE_Explain;
 -      p->autoExplain = 0;
 -    }else if( val==0 ){
 -      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
 -      p->autoExplain = 0;
 -    }else if( val==99 ){
 -      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
 -      p->autoExplain = 1;
 +  }
 +  if( zFile==0 ){
 +    zFile = sqlite3_mprintf("stdout");
 +    shell_check_oom(zFile);
 +  }
 +  if( bOnce ){
 +    psi->outCount = 2;
 +  }else{
 +    psi->outCount = 0;
 +  }
 +  output_reset(psi);
 +#ifndef SQLITE_NOHAVE_SYSTEM
 +  if( eMode=='e' || eMode=='x' ){
 +    psi->doXdgOpen = 1;
 +    outputModePush(psi);
 +    if( eMode=='x' ){
 +      /* spreadsheet mode.  Output as CSV. */
 +      newTempFile(psi, "csv");
 +      psi->shellFlgs &= ~SHFLG_Echo;
 +      psi->mode = MODE_Csv;
 +      sqlite3_snprintf(sizeof(psi->colSeparator), psi->colSeparator, SEP_Comma);
 +      sqlite3_snprintf(sizeof(psi->rowSeparator), psi->rowSeparator, SEP_CrLf);
 +    }else{
 +      /* text editor mode */
 +      newTempFile(psi, "txt");
 +      bTxtMode = 1;
      }
 -  }else
 -
 -#ifndef SQLITE_OMIT_VIRTUALTABLE
 -  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
 -    if( p->bSafeMode ){
 -      raw_printf(stderr, 
 -        "Cannot run experimental commands such as \"%s\" in safe mode\n",
 -        azArg[0]);
 +    sqlite3_free(zFile);
 +    zFile = sqlite3_mprintf("%s", psi->zTempFile);
 +  }
 +#endif /* SQLITE_NOHAVE_SYSTEM */
 +  shell_check_oom(zFile);
 +  if( zFile[0]=='|' ){
 +#ifdef SQLITE_OMIT_POPEN
 +    *pzErr = shellMPrintf(&rc, "Error: pipes are not supported in this OS\n");
 +    rc = 1;
 +    psi->out = STD_OUT;
 +#else
 +    psi->out = popen(zFile + 1, "w");
 +    if( psi->out==0 ){
 +      *pzErr = shellMPrintf(&rc, "Error: cannot open pipe \"%s\"\n", zFile + 1);
 +      psi->out = STD_OUT;
        rc = 1;
      }else{
 -      open_db(p, 0);
 -      expertDotCommand(p, azArg, nArg);
 +      if( bBOM ) fprintf(psi->out,"\357\273\277");
 +      sqlite3_snprintf(sizeof(psi->outfile), psi->outfile, "%s", zFile);
      }
 -  }else
  #endif
 +  }else{
 +    psi->out = output_file_open(zFile, bTxtMode);
 +    if( psi->out==0 ){
 +      if( strcmp(zFile,"off")!=0 ){
 +        *pzErr = shellMPrintf
 +          (&rc, "Error: cannot write to \"%s\"\n", zFile);
 +      }
 +      psi->out = STD_OUT;
 +      rc = 1;
 +    } else {
 +      if( bBOM ) fprintf(psi->out,"\357\273\277");
 +      sqlite3_snprintf(sizeof(psi->outfile), psi->outfile, "%s", zFile);
 +    }
 +  }
 +  sqlite3_free(zFile);
 +  return rc;
 +}
  
 -  if( c=='f' && 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";
 +/*****************
 + * The .filectrl and fullschema commands
 + */
 +COLLECT_HELP_TEXT[
 +  ".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",
 +];
 +DISPATCHABLE_COMMAND( filectrl ? 2 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"    },*/
 +  };
 +  ShellInState *psi = ISS(p);
 +  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;
  
 -    if( zCmd[0]=='-' 
 -     && (strcmp(zCmd,"--schema")==0 || 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];
 -    }
 +  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++;
 -    }
 +  if( zCmd[0]=='-'
 +      && (strcmp(zCmd,"--schema")==0 || 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];
 +  }
  
 -    /* --help lists all file-controls */
 -    if( strcmp(zCmd,"help")==0 ){
 -      utf8_printf(p->out, "Available file-controls:\n");
 -      for(i=0; i<ArraySize(aCtrl); i++){
 -        utf8_printf(p->out, "  .filectrl %s %s\n",
 -                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
 -      }
 -      rc = 1;
 -      goto meta_command_exit;
 -    }
 +  /* The argument can optionally begin with "-" or "--" */
 +  if( zCmd[0]=='-' && zCmd[1] ){
 +    zCmd++;
 +    if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
 +  }
  
 -    /* convert filectrl text option to value. allow any unique prefix
 -    ** of the option name, or a numerical value. */
 -    n2 = strlen30(zCmd);
 +  /* --help lists all file-controls */
 +  if( strcmp(zCmd,"help")==0 ){
 +    utf8_printf(psi->out, "Available file-controls:\n");
      for(i=0; i<ArraySize(aCtrl); i++){
 -      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
 -        if( filectrl<0 ){
 -          filectrl = aCtrl[i].ctrlCode;
 -          iCtrl = i;
 -        }else{
 -          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
 -                              "Use \".filectrl --help\" for help\n", zCmd);
 -          rc = 1;
 -          goto meta_command_exit;
 -        }
 -      }
 +      utf8_printf(psi->out, "  .filectrl %s %s\n",
 +                  aCtrl[i].zCtrlName, aCtrl[i].zUsage);
      }
 -    if( filectrl<0 ){
 -      utf8_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 ){
 -            utf8_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);
 -          utf8_printf(p->out,"%d\n", x);
 -          isOk = 2;
 -          break;
 -        }
 +    return 1;
 +  }
 +
 +  /* 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( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
 +      if( filectrl<0 ){
 +        filectrl = aCtrl[i].ctrlCode;
 +        iCtrl = i;
 +      }else{
 +        utf8_printf(STD_ERR, "Error: ambiguous file-control: \"%s\"\n"
 +                    "Use \".filectrl --help\" for help\n", zCmd);
 +        return 1;
        }
      }
 -    if( isOk==0 && iCtrl>=0 ){
 -      utf8_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);
 -      raw_printf(p->out, "%s\n", zBuf);
 -    }
 -  }else
 -
 -  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
 -    ShellState data;
 -    int doStats = 0;
 -    memcpy(&data, p, sizeof(data));
 -    data.showHeader = 0;
 -    data.cMode = data.mode = MODE_Semi;
 -    if( nArg==2 && optionMatch(azArg[1], "indent") ){
 -      data.cMode = data.mode = MODE_Pretty;
 -      nArg = 1;
 +  }
 +  if( filectrl<0 ){
 +    utf8_printf(STD_ERR,"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(DBX(p), zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
 +      isOk = 1;
 +      break;
      }
 -    if( nArg!=1 ){
 -      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
 -      rc = 1;
 -      goto meta_command_exit;
 +    case SQLITE_FCNTL_LOCK_TIMEOUT:
 +    case SQLITE_FCNTL_CHUNK_SIZE: {
 +      int x;
 +      if( nArg!=3 ) break;
 +      x = (int)integerValue(azArg[2]);
 +      sqlite3_file_control(DBX(p), zSchema, filectrl, &x);
 +      isOk = 2;
 +      break;
      }
 -    open_db(p, 0);
 -    rc = sqlite3_exec(p->db,
 -       "SELECT sql 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_%' "
 -       "ORDER BY x",
 -       callback, &data, 0
 -    );
 -    if( rc==SQLITE_OK ){
 -      sqlite3_stmt *pStmt;
 -      rc = sqlite3_prepare_v2(p->db,
 -               "SELECT rowid FROM sqlite_schema"
 -               " WHERE name GLOB 'sqlite_stat[134]'",
 -               -1, &pStmt, 0);
 -      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
 -      sqlite3_finalize(pStmt);
 +    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(DBX(p), zSchema, filectrl, &x);
 +      iRes = x;
 +      isOk = 1;
 +      break;
      }
 -    if( doStats==0 ){
 -      raw_printf(p->out, "/* No STAT tables available */\n");
 -    }else{
 -      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
 -      data.cMode = data.mode = MODE_Insert;
 -      data.zDestTable = "sqlite_stat1";
 -      shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
 -      data.zDestTable = "sqlite_stat4";
 -      shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
 -      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
 +    case SQLITE_FCNTL_DATA_VERSION:
 +    case SQLITE_FCNTL_HAS_MOVED: {
 +      int x;
 +      if( nArg!=2 ) break;
 +      sqlite3_file_control(DBX(p), zSchema, filectrl, &x);
 +      iRes = x;
 +      isOk = 1;
 +      break;
      }
 -  }else
 -
 -  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
 -    if( nArg==2 ){
 -      p->showHeader = booleanValue(azArg[1]);
 -      p->shellFlgs |= SHFLG_HeaderSet;
 -    }else{
 -      raw_printf(stderr, "Usage: .headers on|off\n");
 -      rc = 1;
 +    case SQLITE_FCNTL_TEMPFILENAME: {
 +      char *z = 0;
 +      if( nArg!=2 ) break;
 +      sqlite3_file_control(DBX(p), zSchema, filectrl, &z);
 +      if( z ){
 +        utf8_printf(psi->out, "%s\n", z);
 +        sqlite3_free(z);
 +      }
 +      isOk = 2;
 +      break;
      }
 -  }else
 -
 -  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
 -    if( nArg>=2 ){
 -      n = showHelp(p->out, azArg[1]);
 -      if( n==0 ){
 -        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
 +    case SQLITE_FCNTL_RESERVE_BYTES: {
 +      int x;
 +      if( nArg>=3 ){
 +        x = atoi(azArg[2]);
 +        sqlite3_file_control(DBX(p), zSchema, filectrl, &x);
        }
 -    }else{
 -      showHelp(p->out, 0);
 +      x = -1;
 +      sqlite3_file_control(DBX(p), zSchema, filectrl, &x);
 +      utf8_printf(psi->out,"%d\n", x);
 +      isOk = 2;
 +      break;
      }
 -  }else
 +   }
 +  }
 +  if( isOk==0 && iCtrl>=0 ){
 +    utf8_printf(psi->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
 +    return 1;
 +  }else if( isOk==1 ){
 +    char zBuf[100];
 +    sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
 +    raw_printf(psi->out, "%s\n", zBuf);
 +  }
 +  return 0;
 +}
  
 -  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
 -    char *zTable = 0;           /* Insert data into this table */
 -    char *zSchema = 0;          /* within this schema (may default to "main") */
 -    char *zFile = 0;            /* Name of file to extra content from */
 -    sqlite3_stmt *pStmt = NULL; /* A statement */
 -    int nCol;                   /* Number of columns in the table */
 -    int nByte;                  /* Number of bytes in an SQL string */
 -    int i, j;                   /* Loop counters */
 -    int needCommit;             /* True to COMMIT or ROLLBACK at end */
 -    int nSep;                   /* Number of bytes in p->colSeparator[] */
 -    char *zSql;                 /* An SQL statement */
 -    char *zFullTabName;         /* Table name with schema if applicable */
 -    ImportCtx sCtx;             /* Reader context */
 -    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
 -    int eVerbose = 0;           /* Larger for more console output */
 -    int nSkip = 0;              /* Initial lines to skip */
 -    int useOutputMode = 1;      /* Use output mode to determine separators */
 -
 -    failIfSafeMode(p, "cannot run .import in safe mode");
 -    memset(&sCtx, 0, sizeof(sCtx));
 -    sCtx.z = sqlite3_malloc64(120);
 -    if( sCtx.z==0 ){
 -      import_cleanup(&sCtx);
 -      shell_out_of_memory();
 -    }
 -    if( p->mode==MODE_Ascii ){
 -      xRead = ascii_read_one_field;
 +DISPATCHABLE_COMMAND( fullschema ? 1 2 ){
 +  int rc;
 +  ShellInState data;
 +  ShellExState datax;
 +  int doStats = 0;
 +  /* Consider some refactoring to avoid this wholesale copying. */
 +  memcpy(&data, ISS(p), sizeof(data));
 +  memcpy(&datax, p, sizeof(datax));
 +  data.pSXS = &datax;
 +  datax.pSIS = &data;
 +  data.showHeader = 0;
 +  data.cMode = data.mode = MODE_Semi;
 +  if( nArg==2 && optionMatch(azArg[1], "indent") ){
 +    data.cMode = data.mode = MODE_Pretty;
 +    nArg = 1;
 +  }
 +  if( nArg!=1 ){
 +    return SHELL_INVALID_ARGS;
 +  }
 +  open_db(p, 0);
 +  rc = sqlite3_exec(datax.dbUser,
 +    "SELECT sql 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_%' "
 +    "ORDER BY x",
 +    callback, &datax, 0
 +  );
 +  if( rc==SQLITE_OK ){
 +    sqlite3_stmt *pStmt;
 +    rc = sqlite3_prepare_v2(datax.dbUser,
 +                            "SELECT rowid FROM sqlite_schema"
 +                            " WHERE name GLOB 'sqlite_stat[134]'",
 +                            -1, &pStmt, 0);
 +    doStats = sqlite3_step(pStmt)==SQLITE_ROW;
 +    sqlite3_finalize(pStmt);
 +  }
 +  if( doStats==0 ){
 +    raw_printf(data.out, "/* No STAT tables available */\n");
 +  }else{
 +    raw_printf(data.out, "ANALYZE sqlite_schema;\n");
 +    data.cMode = data.mode = MODE_Insert;
 +    datax.zDestTable = "sqlite_stat1";
 +    shell_exec(&datax, "SELECT * FROM sqlite_stat1", 0);
 +    datax.zDestTable = "sqlite_stat4";
 +    shell_exec(&datax, "SELECT * FROM sqlite_stat4", 0);
 +    raw_printf(data.out, "ANALYZE sqlite_schema;\n");
 +  }
 +  return rc > 0;
 +}
 +
 +/*****************
 + * The .headers command
 + */
 +COLLECT_HELP_TEXT[
 +  ".headers on|off          Turn display of headers on or off",
 +];
 +DISPATCHABLE_COMMAND( headers 6 2 2 ){
 +  ISS(p)->showHeader = booleanValue(azArg[1]);
 +  ISS(p)->shellFlgs |= SHFLG_HeaderSet;
 +  return 0;
 +}
 +
 +/*****************
 + * The .help command
 + */
 +COLLECT_HELP_TEXT[
 +  ".help ?PATTERN?|?-all?   Show help for PATTERN or everything, or summarize",
 +  "                           Repeat -all to see undocumented commands",
 +];
 +DISPATCHABLE_COMMAND( help 3 1 2 ){
 +  const char *zPat = 0;
 +  if( nArg>1 ){
 +    char *z = azArg[1];
 +    if( strcmp(z,"-a")==0
 +        || strcmp(z,"-all")==0
 +        || strcmp(z,"--all")==0 ){
 +      zPat = "";
      }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{
 -          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
 -          showHelp(p->out, "import");
 -          rc = 1;
 -          goto meta_command_exit;
 -        }
 -      }else if( strcmp(z,"-v")==0 ){
 -        eVerbose++;
 -      }else if( strcmp(z,"-schema")==0 && i<nArg-1 ){
 -        zSchema = azArg[++i];
 -      }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
 -        nSkip = integerValue(azArg[++i]);
 -      }else if( strcmp(z,"-ascii")==0 ){
 -        sCtx.cColSep = SEP_Unit[0];
 -        sCtx.cRowSep = SEP_Record[0];
 -        xRead = ascii_read_one_field;
 -        useOutputMode = 0;
 -      }else if( strcmp(z,"-csv")==0 ){
 -        sCtx.cColSep = ',';
 -        sCtx.cRowSep = '\n';
 -        xRead = csv_read_one_field;
 -        useOutputMode = 0;
 -      }else{
 -        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
 -        showHelp(p->out, "import");
 -        rc = 1;
 -        goto meta_command_exit;
 -      }
 +      zPat = z;
      }
 -    if( zTable==0 ){
 -      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
 -                  zFile==0 ? "FILE" : "TABLE");
 -      showHelp(p->out, "import");
 -      rc = 1;
 -      goto meta_command_exit;
 -    }
 -    seenInterrupt = 0;
 -    open_db(p, 0);
 -    if( useOutputMode ){
 -      /* If neither the --csv or --ascii options are specified, then set
 -      ** the column and row separator characters from the output mode. */
 -      nSep = strlen30(p->colSeparator);
 -      if( nSep==0 ){
 -        raw_printf(stderr,
 -                   "Error: non-null column separator required for import\n");
 -        rc = 1;
 -        goto meta_command_exit;
 -      }
 -      if( nSep>1 ){
 -        raw_printf(stderr, 
 -              "Error: multi-character column separators not allowed"
 -              " for import\n");
 -        rc = 1;
 -        goto meta_command_exit;
 -      }
 -      nSep = strlen30(p->rowSeparator);
 -      if( nSep==0 ){
 -        raw_printf(stderr,
 -            "Error: non-null row separator required for import\n");
 -        rc = 1;
 -        goto meta_command_exit;
 -      }
 -      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
 -        /* When importing CSV (only), if the row separator is set to the
 -        ** default output row separator, change it to the default input
 -        ** row separator.  This avoids having to maintain different input
 -        ** and output row separators. */
 -        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
 -        nSep = strlen30(p->rowSeparator);
 -      }
 -      if( nSep>1 ){
 -        raw_printf(stderr, "Error: multi-character row separators not allowed"
 -                           " for import\n");
 -        rc = 1;
 -        goto meta_command_exit;
 -      }
 -      sCtx.cColSep = p->colSeparator[0];
 -      sCtx.cRowSep = p->rowSeparator[0];
 +  }
 +  if( showHelp(ISS(p)->out, zPat, p)==0 ){
 +    utf8_printf(ISS(p)->out, "Nothing matches '%s'\n", azArg[1]);
 +  }
 +  /* Help pleas never fail! */
 +  return 0;
 +}
 +
 +/*****************
 + * The .import command
 + */
 +COLLECT_HELP_TEXT[
 +  ".import FILE TABLE       Import data from FILE into TABLE",
 +  "   Options:",
 +  "     --ascii               Use \\037 and \\036 as column and row separators",
 +  "     --csv                 Use , and \\n as column and row separators",
 +  "     --skip N              Skip the first N rows of input",
 +  "     --schema S            Target table to be S.TABLE",
 +  "     -v                    \"Verbose\" - increase auxiliary output",
 +  "   Notes:",
 +  "     *  If TABLE does not exist, it is created.  The first row of input",
 +  "        determines the column names.",
 +  "     *  If neither --csv or --ascii are used, the input mode is derived",
 +  "        from the \".mode\" output mode",
 +  "     *  If FILE begins with \"|\" then it is a command that generates the",
 +  "        input text.",
 +];
 +DISPATCHABLE_COMMAND( import ? 3 7 ){
 +  char *zTable = 0;           /* Insert data into this table */
-   char *zSchema = "main";     /* within this schema */
++  char *zSchema = 0;          /* within this schema (may default to "main") */
 +  char *zFile = 0;            /* Name of file to extra content from */
 +  sqlite3_stmt *pStmt = NULL; /* A statement */
 +  int nCol;                   /* Number of columns in the table */
 +  int nByte;                  /* Number of bytes in an SQL string */
 +  int i, j;                   /* Loop counters */
 +  int needCommit;             /* True to COMMIT or ROLLBACK at end */
 +  int nSep;                   /* Number of bytes in psi->colSeparator[] */
 +  char *zSql;                 /* An SQL statement */
++  char *zFullTabName;         /* Table name with schema if applicable */
 +  ImportCtx sCtx;             /* Reader context */
 +  char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
 +  int eVerbose = 0;           /* Larger for more console output */
 +  int nSkip = 0;              /* Initial lines to skip */
 +  int useOutputMode = 1;      /* Use output mode to determine separators */
 +  FILE *out = ISS(p)->out;    /* output stream */
 +  ShellInState *psi = ISS(p);
 +  int rc = 0;
 +
 +  if(psi->bSafeMode) return SHELL_FORBIDDEN_OP;
 +  memset(&sCtx, 0, sizeof(sCtx));
 +  if( 0==(sCtx.z = sqlite3_malloc64(120)) ){
 +    shell_out_of_memory();
 +  }
 +
 +  if( psi->mode==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{
 +        *pzErr = shellMPrintf(0,"       surplus argument: \"%s\"\n", z);
 +        return SHELL_INVALID_ARGS;
 +      }
 +    }else if( strcmp(z,"-v")==0 ){
 +      eVerbose++;
 +    }else if( strcmp(z,"-schema")==0 && i<nArg-1 ){
 +      zSchema = azArg[++i];
 +    }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
 +      nSkip = integerValue(azArg[++i]);
 +    }else if( strcmp(z,"-ascii")==0 ){
 +      sCtx.cColSep = SEP_Unit[0];
 +      sCtx.cRowSep = SEP_Record[0];
 +      xRead = ascii_read_one_field;
 +      useOutputMode = 0;
 +    }else if( strcmp(z,"-csv")==0 ){
 +      sCtx.cColSep = ',';
 +      sCtx.cRowSep = '\n';
 +      xRead = csv_read_one_field;
 +      useOutputMode = 0;
 +    }else{
 +      *pzErr = shellMPrintf(0,"       unknown option: \"%s\"", z);
 +      return SHELL_INVALID_ARGS;
 +    }
 +  }
 +  if( zTable==0 ){
 +    *pzErr = shellMPrintf(0,"       missing %s argument.\n",
 +                zFile==0 ? "FILE" : "TABLE");
 +    return SHELL_INVALID_ARGS;
 +  }
 +  seenInterrupt = 0;
 +  open_db(p, 0);
 +  if( useOutputMode ){
 +    const char *zYap = 0;
 +    /* If neither the --csv or --ascii options are specified, then set
 +    ** the column and row separator characters from the output mode. */
 +    nSep = strlen30(psi->colSeparator);
 +    if( nSep==0 ){
 +      zYap = "Error: non-null column separator required for import";
 +    }
 +    if( nSep>1 ){
 +      zYap = "Error: multi-character or multi-byte column separators"
 +        " not allowed for import";
 +    }
 +    nSep = strlen30(psi->rowSeparator);
 +    if( nSep==0 ){
 +      zYap = "Error: non-null row separator required for import";
 +    }
 +    if( zYap!=0 ){
 +      *pzErr = shellMPrintf(0,"%s\n", zYap);
 +      return 1;
 +    }
 +    if( nSep==2 && psi->mode==MODE_Csv
 +        && strcmp(psi->rowSeparator,SEP_CrLf)==0 ){
 +      /* When importing CSV (only), if the row separator is set to the
 +      ** default output row separator, change it to the default input
 +      ** row separator.  This avoids having to maintain different input
 +      ** and output row separators. */
 +      sqlite3_snprintf(sizeof(psi->rowSeparator), psi->rowSeparator, SEP_Row);
 +      nSep = strlen30(psi->rowSeparator);
 +    }
 +    if( nSep>1 ){
 +      *pzErr = sqlite3_mprintf
 +        ("Error: multi-character row separators not allowed for import\n");
 +      return 1;
      }
 -    sCtx.zFile = zFile;
 -    sCtx.nLine = 1;
 -    if( sCtx.zFile[0]=='|' ){
 +    sCtx.cColSep = psi->colSeparator[0];
 +    sCtx.cRowSep = psi->rowSeparator[0];
 +  }
 +  sCtx.zFile = zFile;
 +  sCtx.nLine = 1;
 +  if( sCtx.zFile[0]=='|' ){
  #ifdef SQLITE_OMIT_POPEN
 -      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
 -      rc = 1;
 -      goto meta_command_exit;
 +    *pzErr = shellMPrintf(0,"Error: pipes are not supported in this OS\n");
 +    return 1;
  #else
 -      sCtx.in = popen(sCtx.zFile+1, "r");
 -      sCtx.zFile = "<pipe>";
 -      sCtx.xCloser = pclose;
 +    sCtx.in = popen(sCtx.zFile+1, "r");
 +    sCtx.zFile = "<pipe>";
 +    sCtx.xCloser = pclose;
  #endif
 -    }else{
 -      sCtx.in = fopen(sCtx.zFile, "rb");
 -      sCtx.xCloser = fclose;
 -    }
 -    if( sCtx.in==0 ){
 -      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
 -      rc = 1;
 -      import_cleanup(&sCtx);
 -      goto meta_command_exit;
 -    }
 -    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
 -      char zSep[2];
 -      zSep[1] = 0;
 -      zSep[0] = sCtx.cColSep;
 -      utf8_printf(p->out, "Column separator ");
 -      output_c_string(p->out, zSep);
 -      utf8_printf(p->out, ", row separator ");
 -      zSep[0] = sCtx.cRowSep;
 -      output_c_string(p->out, zSep);
 -      utf8_printf(p->out, "\n");
 -    }
 -    /* Below, resources must be freed before exit. */
 -    while( (nSkip--)>0 ){
 -      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
 -    }
 -    if( zSchema!=0 ){
 -      zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable);
 -    }else{
 -      zFullTabName = sqlite3_mprintf("\"%w\"", zTable);
 -    }
 -    zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName);
 -    if( zSql==0 || zFullTabName==0 ){
 +  }else{
 +    sCtx.in = fopen(sCtx.zFile, "rb");
 +    sCtx.xCloser = fclose;
 +  }
 +  if( sCtx.in==0 ){
 +        *pzErr = shellMPrintf(0,"Error: cannot open \"%s\"\n", zFile);
 +    import_cleanup(&sCtx);
 +    return 1;
 +  }
-   /* Below, resources must be freed before exit. */
++  /* Here and below, resources must be freed before exit. */
 +  if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
 +    char zSep[2];
 +    zSep[1] = 0;
 +    zSep[0] = sCtx.cColSep;
 +    utf8_printf(out, "Column separator ");
 +    output_c_string(out, zSep);
 +    utf8_printf(out, ", row separator ");
 +    zSep[0] = sCtx.cRowSep;
 +    output_c_string(out, zSep);
 +    utf8_printf(out, "\n");
 +  }
 +  while( (nSkip--)>0 ){
 +    while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
 +  }
-   zSql = sqlite3_mprintf("SELECT * FROM \"%w\".\"%w\"", zSchema, zTable);
-   if( zSql==0 ){
++  if( zSchema!=0 ){
++    zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable);
++  }else{
++    zFullTabName = sqlite3_mprintf("\"%w\"", zTable);
++  }
++  zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName);
++  if( zSql==0 || zFullTabName==0 ){
 +    import_cleanup(&sCtx);
 +    shell_out_of_memory();
 +  }
 +  nByte = strlen30(zSql);
 +  rc = sqlite3_prepare_v2(DBX(p), zSql, -1, &pStmt, 0);
 +  import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
 +  if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(DBX(p)))==0 ){
-     char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\".\"%w\"",
-                                     zSchema, zTable);
++    char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName);
 +    sqlite3 *dbCols = 0;
 +    char *zRenames = 0;
 +    char *zColDefs;
 +    while( xRead(&sCtx) ){
 +      zAutoColumn(sCtx.z, &dbCols, 0);
 +      if( sCtx.cTerm!=sCtx.cColSep ) break;
 +    }
 +    zColDefs = zAutoColumn(0, &dbCols, &zRenames);
 +    if( zRenames!=0 ){
 +      FILE *fh = INSOURCE_IS_INTERACTIVE(psi->pInSource)?  out : STD_ERR;
 +      utf8_printf(fh, "Columns renamed during .import %s due to duplicates:\n"
 +                  "%s\n", sCtx.zFile, zRenames);
 +      sqlite3_free(zRenames);
 +    }
 +    assert(dbCols==0);
 +    if( zColDefs==0 ){
++      utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
++    import_fail:
 +      sqlite3_free(zCreate);
++      sqlite3_free(zSql);
++      sqlite3_free(zFullTabName);
        import_cleanup(&sCtx);
-       *pzErr = shellMPrintf(0,"%s: empty file\n", sCtx.zFile);
 -      shell_out_of_memory();
 +      return 1;
      }
 -    nByte = strlen30(zSql);
 -    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
 -    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
 -    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
 -      char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName);
 -      sqlite3 *dbCols = 0;
 -      char *zRenames = 0;
 -      char *zColDefs;
 -      while( xRead(&sCtx) ){
 -        zAutoColumn(sCtx.z, &dbCols, 0);
 -        if( sCtx.cTerm!=sCtx.cColSep ) break;
 -      }
 -      zColDefs = zAutoColumn(0, &dbCols, &zRenames);
 -      if( zRenames!=0 ){
 -        utf8_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 ){
 -        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
 -      import_fail:
 -        sqlite3_free(zCreate);
 -        sqlite3_free(zSql);
 -        sqlite3_free(zFullTabName);
 -        import_cleanup(&sCtx);
 -        rc = 1;
 -        goto meta_command_exit;
 -      }
 -      zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
 -      if( eVerbose>=1 ){
 -        utf8_printf(p->out, "%s\n", zCreate);
 -      }
 -      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
 -      if( rc ){
 -        utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
 -        goto import_fail;
 -      }
 -      sqlite3_free(zCreate);
 -      zCreate = 0;
 -      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
 +    zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
 +    if( eVerbose>=1 ){
 +      utf8_printf(out, "%s\n", zCreate);
      }
 +    rc = sqlite3_exec(DBX(p), zCreate, 0, 0, 0);
      if( rc ){
 -      if (pStmt) sqlite3_finalize(pStmt);
 -      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
 +      utf8_printf(STD_ERR, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(DBX(p)));
-       sqlite3_free(zCreate);
-       import_cleanup(&sCtx);
-       return 1;
+       goto import_fail;
      }
 -    sqlite3_free(zSql);
 -    nCol = sqlite3_column_count(pStmt);
 -    sqlite3_finalize(pStmt);
 -    pStmt = 0;
 -    if( nCol==0 ) return 0; /* no columns, no error */
 -    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
 -    if( zSql==0 ){
 -      import_cleanup(&sCtx);
 -      shell_out_of_memory();
 -    }
 -    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName);
 -    j = strlen30(zSql);
 -    for(i=1; i<nCol; i++){
 -      zSql[j++] = ',';
 -      zSql[j++] = '?';
 -    }
 -    zSql[j++] = ')';
 -    zSql[j] = 0;
 -    if( eVerbose>=2 ){
 -      utf8_printf(p->out, "Insert using: %s\n", zSql);
 +    sqlite3_free(zCreate);
++    zCreate = 0;
 +    rc = sqlite3_prepare_v2(DBX(p), zSql, -1, &pStmt, 0);
 +  }
-   sqlite3_free(zSql);
 +  if( rc ){
 +    if (pStmt) sqlite3_finalize(pStmt);
 +    *pzErr = shellMPrintf(0,"Error: %s\n", sqlite3_errmsg(DBX(p)));
-     import_cleanup(&sCtx);
-     return 1;
++    goto import_fail;
 +  }
++  sqlite3_free(zSql);
 +  nCol = sqlite3_column_count(pStmt);
 +  sqlite3_finalize(pStmt);
 +  pStmt = 0;
 +  if( nCol==0 ) return 0; /* no columns, no error */
 +  zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
 +  if( zSql==0 ){
 +    import_cleanup(&sCtx);
 +    shell_out_of_memory();
 +  }
-   sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\".\"%w\" VALUES(?",
-                    zSchema, zTable);
++  sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName);
 +  j = strlen30(zSql);
 +  for(i=1; i<nCol; i++){
 +    zSql[j++] = ',';
 +    zSql[j++] = '?';
 +  }
 +  zSql[j++] = ')';
 +  zSql[j] = 0;
 +  if( eVerbose>=2 ){
 +    utf8_printf(psi->out, "Insert using: %s\n", zSql);
 +  }
 +  rc = sqlite3_prepare_v2(DBX(p), zSql, -1, &pStmt, 0);
-   sqlite3_free(zSql);
 +  if( rc ){
 +    *pzErr = shellMPrintf(0,"Error: %s\n", sqlite3_errmsg(DBX(p)));
 +    if (pStmt) sqlite3_finalize(pStmt);
-     import_cleanup(&sCtx);
-     return 1;
++    goto import_fail;
 +  }
++  sqlite3_free(zSql);
++  sqlite3_free(zFullTabName);
 +  needCommit = sqlite3_get_autocommit(DBX(p));
 +  if( needCommit ) sqlite3_exec(DBX(p), "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( psi->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
 +      sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
 +      if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
 +        utf8_printf(STD_ERR, "%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++; }
 +      }
      }
 -    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
 -    if( rc ){
 -      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
 -      if (pStmt) sqlite3_finalize(pStmt);
 -      goto import_fail;
 +    if( sCtx.cTerm==sCtx.cColSep ){
 +      do{
 +        xRead(&sCtx);
 +        i++;
 +      }while( sCtx.cTerm==sCtx.cColSep );
 +      utf8_printf(STD_ERR, "%s:%d: expected %d columns but found %d - "
 +                  "extras ignored\n",
 +                  sCtx.zFile, startLine, nCol, i);
      }
 -    sqlite3_free(zSql);
 -    sqlite3_free(zFullTabName);
 -    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==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
 -        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
 -        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
 -          utf8_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( i>=nCol ){
 +      sqlite3_step(pStmt);
 +      rc = sqlite3_reset(pStmt);
 +      if( rc!=SQLITE_OK ){
 +        utf8_printf(STD_ERR, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
 +                    startLine, sqlite3_errmsg(DBX(p)));
 +        sCtx.nErr++;
 +      }else{
 +        sCtx.nRow++;
        }
 -      if( sCtx.cTerm==sCtx.cColSep ){
 -        do{
 -          xRead(&sCtx);
 -          i++;
 -        }while( sCtx.cTerm==sCtx.cColSep );
 -        utf8_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 ){
 -          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
 -                      startLine, sqlite3_errmsg(p->db));
 -          sCtx.nErr++;
 -        }else{
 -          sCtx.nRow++;
 +    }
 +  }while( sCtx.cTerm!=EOF );
 +
 +  import_cleanup(&sCtx);
 +  sqlite3_finalize(pStmt);
 +  if( needCommit ) sqlite3_exec(DBX(p), "COMMIT", 0, 0, 0);
 +  if( eVerbose>0 ){
 +    utf8_printf(out,
 +      "Added %d rows with %d errors using %d lines of input\n",
 +      sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
 +  }
 +  return 0;
 +}
 +
 +/*****************
 + * The .keyword command
 + */
 +CONDITION_COMMAND( keyword !defined(NO_KEYWORD_COMMAND) );
 +COLLECT_HELP_TEXT[
 +  ".keyword ?KW?            List keywords, or say whether KW is one.",
 +];
 +DISPATCHABLE_COMMAND( keyword ? 1 2 ){
 +  FILE *out = ISS(p)->out;
 +  if( nArg<2 ){
 +    int i = 0;
 +    int nk = sqlite3_keyword_count();
 +    int nCol = 0;
 +    int szKW;
 +    while( i<nk ){
 +      const char *zKW = 0;
 +      if( SQLITE_OK==sqlite3_keyword_name(i++, &zKW, &szKW) ){
 +        char kwBuf[50];
 +        if( szKW < sizeof(kwBuf) ){
 +          const char *zSep = " ";
 +          if( (nCol += (1+szKW))>75){
 +            zSep = "\n";
 +            nCol = 0;
 +          }
 +          memcpy(kwBuf, zKW, szKW);
 +          kwBuf[szKW] = 0;
 +          utf8_printf(out, "%s%s", kwBuf, zSep);
          }
        }
 -    }while( sCtx.cTerm!=EOF );
 -
 -    import_cleanup(&sCtx);
 -    sqlite3_finalize(pStmt);
 -    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
 -    if( eVerbose>0 ){
 -      utf8_printf(p->out,
 -          "Added %d rows with %d errors using %d lines of input\n",
 -          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
      }
 -  }else
 +    if( nCol>0 ) utf8_printf(out, "\n");
 +  }else{
 +    int szKW = strlen30(azArg[1]);
 +    int isKeyword = sqlite3_keyword_check(azArg[1], szKW);
 +    utf8_printf(out, "%s is%s a keyword\n",
 +                azArg[1], (isKeyword)? "" : " not");
 +  }
 +  return 0;
 +}
  
 -#ifndef SQLITE_UNTESTABLE
 -  if( c=='i' && 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)) ){
 -      utf8_printf(stderr, "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;
 +/*****************
 + * The .imposter, .iotrace, limit, lint, .load and .log commands
 + */
 +CONDITION_COMMAND( imposter !defined(SQLITE_OMIT_TEST_CONTROL) );
 +CONDITION_COMMAND( iotrace defined(SQLITE_ENABLE_IOTRACE) );
 +CONDITION_COMMAND( load !defined(SQLITE_OMIT_LOAD_EXTENSION) );
 +COLLECT_HELP_TEXT[
 +  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
 +  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
 +  ".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",
 +  ".load FILE ?ENTRY?       Load an extension library",
 +#if SHELL_DYNAMIC_EXTENSION
 +  "   Option -shellext will load the library as a shell extension.",
 +#endif
 +  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
 +];
 +DISPATCHABLE_COMMAND( imposter ? 3 3 ){
 +  int rc = 0;
 +  char *zSql;
 +  char *zCollist = 0;
 +  sqlite3_stmt *pStmt;
 +  sqlite3 *db;
 +  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)) ){
 +    *pzErr = shellMPrintf(0,"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. */
 +    return 1;
 +  }
 +  open_db(p, 0);
 +  db = DBX(p);
 +  if( nArg==2 ){
 +    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, "main", 0, 1);
 +    return 0;
 +  }
 +  zSql = sqlite3_mprintf(
 +                         "SELECT rootpage, 0 FROM sqlite_schema"
 +                         " WHERE name='%q' AND type='index'"
 +                         "UNION ALL "
 +                         "SELECT rootpage, 1 FROM sqlite_schema"
 +                         " WHERE name='%q' AND type='table'"
 +                         "   AND sql LIKE '%%without%%rowid%%'",
 +                         azArg[1], azArg[1]
 +                         );
 +  sqlite3_prepare_v2(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(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;
 +      }
      }
 -    open_db(p, 0);
 -    if( nArg==2 ){
 -      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
 -      goto meta_command_exit;
 +    if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
 +      lenPK = (int)strlen(zCollist);
      }
 -    zSql = sqlite3_mprintf(
 -      "SELECT rootpage, 0 FROM sqlite_schema"
 -      " WHERE name='%q' AND type='index'"
 -      "UNION ALL "
 -      "SELECT rootpage, 1 FROM sqlite_schema"
 -      " WHERE name='%q' AND type='table'"
 -      "   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);
 +    if( zCollist==0 ){
 +      zCollist = sqlite3_mprintf("\"%w\"", zCol);
 +    }else{
 +      zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
      }
 -    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_";
 +  }
 +  sqlite3_finalize(pStmt);
 +  if( i==0 || tnum==0 ){
 +    *pzErr = shellMPrintf(0,"no such index: \"%s\"\n", azArg[1]);
 +    sqlite3_free(zCollist);
 +    return 1;
 +  }
 +  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, db, "main", 1, tnum);
 +  if( rc==SQLITE_OK ){
 +    rc = sqlite3_exec(db, zSql, 0, 0, 0);
 +    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, "main", 0, 0);
 +    if( rc ){
 +      *pzErr = shellMPrintf(0,"Error in [%s]: %s\n",
 +                               zSql, sqlite3_errmsg(db));
 +    }else{
 +      utf8_printf(STD_OUT, "%s;\n", zSql);
 +      raw_printf(STD_OUT, "WARNING: "
 +                 "writing to an imposter table will corrupt the \"%s\" %s!\n",
 +                 azArg[1], isWO ? "table" : "index"
 +                 );
 +    }
 +  }else{
 +    *pzErr = shellMPrintf(0,"SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
 +  }
 +  sqlite3_free(zSql);
 +  return rc != 0;
 +}
 +DISPATCHABLE_COMMAND( iotrace ? 2 2 ){
 +  SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
 +  if( iotrace && iotrace!=STD_OUT ) fclose(iotrace);
 +  iotrace = 0;
 +  if( nArg<2 ){
 +    sqlite3IoTrace = 0;
 +  }else if( strcmp(azArg[1], "-")==0 ){
 +    sqlite3IoTrace = iotracePrintf;
 +    iotrace = STD_OUT;
 +  }else{
 +    iotrace = fopen(azArg[1], "w");
 +    if( iotrace==0 ){
 +      *pzErr = shellMPrintf(0,"Error: cannot open \"%s\"\n", azArg[1]);
 +      sqlite3IoTrace = 0;
 +      return 1;
 +    }else{
 +      sqlite3IoTrace = iotracePrintf;
 +    }
 +  }
 +  return 0;
 +}
 +DISPATCHABLE_COMMAND( limits 5 1 3 ){
 +  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                },
 +    { "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++){
 +      fprintf(STD_OUT, "%20s %d\n", aLimit[i].zLimitName,
 +             sqlite3_limit(DBX(p), aLimit[i].limitCode, -1));
 +    }
 +  }else if( nArg>3 ){
 +    return SHELL_INVALID_ARGS;
 +  }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{
 -          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
 -          zCol = zLabel;
 +          *pzErr = shellMPrintf(0,"ambiguous limit: \"%s\"\n", azArg[1]);
 +          return 1;
          }
        }
 -      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 ){
 -      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
 -      rc = 1;
 -      sqlite3_free(zCollist);
 -      goto meta_command_exit;
 +    if( iLimit<0 ){
 +      *pzErr = sqlite3_mprintf
 +        ("unknown limit: \"%s\"\n"
 +         "enter \".limits\" with no arguments for a list.\n",
 +         azArg[1]);
 +      return 1;
      }
 -    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", 1, 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 ){
 -        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
 -      }else{
 -        utf8_printf(stdout, "%s;\n", zSql);
 -        raw_printf(stdout,
 -          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
 -          azArg[1], isWO ? "table" : "index"
 -        );
 +    if( nArg==3 ){
 +      sqlite3_limit(DBX(p), aLimit[iLimit].limitCode,
 +                    (int)integerValue(azArg[2]));
 +    }
 +    fprintf(STD_OUT, "%20s %d\n", aLimit[iLimit].zLimitName,
 +           sqlite3_limit(DBX(p), aLimit[iLimit].limitCode, -1));
 +  }
 +  return 0;
 +}
 +
 +DISPATCHABLE_COMMAND( lint 3 1 0 ){
 +  sqlite3 *db;                    /* Database handle to query "main" db of */
 +  FILE *out = ISS(p)->out;        /* Stream to write non-error output to */
 +  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 */
 +
 +  i = (nArg>=2 ? strlen30(azArg[1]) : 0);
 +  if( i==0 || 0!=sqlite3_strnicmp(azArg[1], "fkey-indexes", i) ){
 +    *pzErr = sqlite3_mprintf
 +      ("Usage %s sub-command ?switches...?\n"
 +       "Where sub-commands are:\n"
 +       "    fkey-indexes\n", azArg[0]);
 +    return 1;
 +  }
 +  open_db(p, 0);
 +  db = DBX(p);
 +
 +  /*
 +  ** 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{
 +      raw_printf(STD_ERR, "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 || 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 ){
 +        raw_printf(STD_ERR, "Error: internal error");
 +        break;
 +      }else{
 +        if( bGroupByParent
 +        && (bVerbose || res==0)
 +        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
 +        ){
 +          raw_printf(out, "-- Parent table %s\n", zParent);
 +          sqlite3_free(zPrev);
 +          zPrev = sqlite3_mprintf("%s", zParent);
 +        }
 +
 +        if( res==0 ){
 +          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
 +        }else if( bVerbose ){
 +          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
 +                     zIndent, zFrom, zTarget);
 +        }
 +      }
 +    }
 +    sqlite3_free(zPrev);
 +
 +    if( rc!=SQLITE_OK ){
 +      raw_printf(STD_ERR, "%s\n", sqlite3_errmsg(db));
 +    }
 +
 +    rc2 = sqlite3_finalize(pSql);
 +    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
 +      rc = rc2;
 +      raw_printf(STD_ERR, "%s\n", sqlite3_errmsg(db));
 +    }
 +  }else{
 +    raw_printf(STD_ERR, "%s\n", sqlite3_errmsg(db));
 +  }
 +
 +  return rc;
 +}
 +
 +DISPATCHABLE_COMMAND( load ? 2 4 ){
 +  const char *zFile = 0, *zProc = 0;
 +  char *zErrMsg = 0;
 +  int ai = 1, rc;
 +#if SHELL_DYNAMIC_EXTENSION
 +  u8 bLoadExt = 0;
 +#endif
 +  if( ISS(p)->bSafeMode ) return SHELL_FORBIDDEN_OP;
 +  while( ai<nArg ){
 +    const char *zA = azArg[ai++];
 +#if SHELL_DYNAMIC_EXTENSION
 +    if( optionMatch(zA, "shellext") ) bLoadExt = 1;
 +    else
 +#endif
 +    if( zFile==0 ) zFile = zA;
 +    else if( zProc==0 ) zProc = zA;
 +    else return SHELL_INVALID_ARGS;
 +  }
 +#if SHELL_DYNAMIC_EXTENSION
 +  if( bLoadExt ){
 +    rc = load_shell_extension(p, zFile, zProc, pzErr);
 +  }else
 +#endif
 +  {
 +    open_db(p, 0);
 +    rc = sqlite3_load_extension(DBX(p), zFile, zProc, pzErr);
 +  }
 +  return rc!=SQLITE_OK;
 +}
 +
 +DISPATCHABLE_COMMAND( log ? 2 2 ){
 +  const char *zFile = azArg[1];
 +  if( ISS(p)->bSafeMode ) return SHELL_FORBIDDEN_OP;
 +  output_file_close(ISS(p)->pLog);
 +  ISS(p)->pLog = output_file_open(zFile, 0);
 +  return 0;
 +}
 +
 +static void effectMode(ShellInState *psi, u8 modeRequest, u8 modeNominal){
 +  /* Effect the specified mode change. */
 +  const char *zColSep = 0, *zRowSep = 0;
 +  assert(modeNominal!=MODE_COUNT_OF);
 +  switch( modeRequest ){
 +  case MODE_Line:
 +    zRowSep = SEP_Row;
 +    break;
 +  case MODE_Column:
 +    if( (psi->shellFlgs & SHFLG_HeaderSet)==0 ){
 +      psi->showHeader = 1;
 +    }
 +    zRowSep = SEP_Row;
 +    break;
 +  case MODE_List:
 +    zColSep = SEP_Column;
 +    zRowSep = SEP_Row;
 +    break;
 +  case MODE_Html:
 +    break;
 +  case MODE_Tcl:
 +    zColSep = SEP_Space;
 +    zRowSep = SEP_Row;
 +    break;
 +  case MODE_Csv:
 +    zColSep = SEP_Comma;
 +    zRowSep = SEP_CrLf;
 +    break;
 +  case MODE_Tab:
 +    zColSep = SEP_Tab;
 +    break;
 +  case MODE_Insert:
 +    break;
 +  case MODE_Quote:
 +    zColSep = SEP_Comma;
 +    zRowSep = SEP_Row;
 +    break;
 +  case MODE_Ascii:
 +    zColSep = SEP_Unit;
 +    zRowSep = SEP_Record;
 +    break;
 +  case MODE_Markdown:
 +    /* fall-thru */
 +  case MODE_Table:
 +    /* fall-thru */
 +  case MODE_Box:
 +    break;
 +  case MODE_Count:
 +    /* fall-thru */
 +  case MODE_Off:
 +    /* fall-thru */
 +  case MODE_Json:
 +    break;
 +  case MODE_Explain: case MODE_Semi: case MODE_EQP: case MODE_Pretty: default:
 +    /* Modes used internally, not settable by .mode command. */
 +    return;
 +  }
 +  if( zRowSep!=0 ){
 +    sqlite3_snprintf(sizeof(psi->rowSeparator), psi->rowSeparator, zRowSep);
 +  }
 +  if( zColSep!=0 ){
 +    sqlite3_snprintf(sizeof(psi->colSeparator), psi->colSeparator, zColSep);
 +  }
 +  psi->mode = modeNominal;
 +}
 +
 +/*****************
 + * The .mode command
 + */
 +COLLECT_HELP_TEXT[
 +  ".mode MODE ?OPTIONS?     Set output mode",
 +  "   MODE is one of:",
 +  "     ascii       Columns/rows delimited by 0x1F and 0x1E",
 +  "     box         Tables using unicode box-drawing characters",
 +  "     csv         Comma-separated values",
 +  "     column      Output in columns.  (See .width)",
 +  "     html        HTML <table> code",
 +  "     insert      SQL insert statements for TABLE",
 +  "     json        Results in a JSON array",
 +  "     line        One value per line",
 +  "     list        Values delimited by \"|\"",
 +  "     markdown    Markdown table format",
 +  "     qbox        Shorthand for \"box --width 60 --quote\"",
 +  "     quote       Escape answers as for SQL",
 +  "     table       ASCII-art table",
 +  "     tabs        Tab-separated values",
 +  "     tcl         TCL list elements",
 +  "   OPTIONS: (for columnar modes or insert mode):",
 +  "     --wrap N       Wrap output lines to no longer than N characters",
 +  "     --wordwrap B   Wrap or not at word boundaries per B (on/off)",
 +  "     --ww           Shorthand for \"--wordwrap 1\"",
 +  "     --quote        Quote output text as SQL literals",
 +  "     --noquote      Do not quote output text",
 +  "     TABLE          The name of SQL table used for \"insert\" mode",
 +];
 +DISPATCHABLE_COMMAND( mode ? 1 0 ){
 +  ShellInState *psi = ISS(p);
 +  const char *zTabname = 0;
 +  const char *zArg;
 +  int i;
 +  u8 foundMode = MODE_COUNT_OF, setMode = MODE_COUNT_OF;
 +  ColModeOpts cmOpts = ColModeOpts_default;
 +  for(i=1; i<nArg; i++){
 +    zArg = azArg[i];
 +    if( optionMatch(zArg,"wrap") && i+1<nArg ){
 +      cmOpts.iWrap = integerValue(azArg[++i]);
 +    }else if( optionMatch(zArg,"ww") ){
 +      cmOpts.bWordWrap = 1;
 +    }else if( optionMatch(zArg,"wordwrap") && i+1<nArg ){
 +      cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
 +    }else if( optionMatch(zArg,"quote") ){
 +      cmOpts.bQuote = 1;
 +    }else if( optionMatch(zArg,"noquote") ){
 +      cmOpts.bQuote = 0;
 +    }else{
 +      /* Not a known option. Check for known mode, or possibly a table name. */
 +      if( foundMode==MODE_Insert && zTabname==0 ){
 +        zTabname = zArg;
 +      }else if( *zArg=='-' ){
 +        goto flag_unknown;
 +      }else{
 +        int im, nza = strlen30(zArg);
 +        if( foundMode!=MODE_COUNT_OF ) goto mode_badarg;
 +        for( im=0; im<MODE_COUNT_OF; ++im ){
 +          if( modeDescr[i].bUserBlocked ) continue;
 +          if( strncmp(zArg,modeDescr[im].zModeName,nza)==0 ){
 +            foundMode = (u8)im;
 +            setMode = modeDescr[im].iAliasFor;
 +            break;
 +          }
 +        }
 +        if( strcmp(zArg, "qbox")==0 ){
 +          ColModeOpts cmo = ColModeOpts_default_qbox;
 +          foundMode = setMode = MODE_Box;
 +          cmOpts = cmo;
 +        }else if( im==MODE_COUNT_OF ) goto mode_unknown;
 +      }
 +    }
 +  } /* Arg loop */
 +  if( foundMode==MODE_COUNT_OF ){
 +    FILE *out = psi->out;
 +    const char *zMode;
 +    int nms;
 +    i = psi->mode;
 +    assert(i>=0 && i<MODE_COUNT_OF);
 +    zMode = modeDescr[i].zModeName;
 +    nms = strlen30(zMode)-modeDescr[i].bDepluralize;
 +    /* Mode not specified. Show present mode (and toss any options set.) */
 +    if( MODE_IS_COLUMNAR(psi->mode) ){
 +      raw_printf
 +        (out, "current output mode: %.*s --wrap %d --wordwrap %s --%squote\n",
 +         nms, zMode, psi->cmOpts.iWrap,
 +         psi->cmOpts.bWordWrap ? "on" : "off",
 +         psi->cmOpts.bQuote ? "" : "no");
 +    }else{
 +      raw_printf(out, "current output mode: %.*s\n", nms, zMode);
 +    }
 +  }else{
 +    effectMode(psi, foundMode, setMode);
 +    if( MODE_IS_COLUMNAR(setMode) ) psi->cmOpts = cmOpts;
 +    else if( setMode==MODE_Insert ){
 +      set_table_name(p, zTabname ? zTabname : "table");
 +    }
 +  }
 +  psi->cMode = psi->mode;
 +  return 0;
 + flag_unknown:
 +  utf8_printf(STD_ERR, "Error: Unknown .mode option: %s\nValid options:\n%s",
 +              zArg,
 +              "  --noquote\n"
 +              "  --quote\n"
 +              "  --wordwrap on/off\n"
 +              "  --wrap N\n"
 +              "  --ww\n");
 +  return 1;
 + mode_unknown:
 +  raw_printf(STD_ERR, "Error: Mode should be one of: "
 +             "ascii box column csv html insert json line list markdown "
 +             "qbox quote table tabs tcl\n");
 +  return 1;
 + mode_badarg:
 +  utf8_printf(STD_ERR, "Error: Invalid .mode argument: %s\n", zArg);
 +  return 1;
 +}
 +
 +/*****************
 + * The .open, .nonce and .nullvalue commands
 + */
 +COLLECT_HELP_TEXT[
 +  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
 +  "     Options:",
 +  "        --append        Use appendvfs to append database to the end of FILE",
 +#ifndef SQLITE_OMIT_DESERIALIZE
 +  "        --deserialize   Load into memory using sqlite3_deserialize()",
 +  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
 +  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
 +#endif
 +  "        --new           Initialize FILE to an empty database",
 +  "        --nofollow      Do not follow symbolic links",
 +  "        --readonly      Open FILE readonly",
 +  "        --zip           FILE is a ZIP archive",
 +  ".nonce STRING            Suspend safe mode for one command if nonce matches",
 +  ".nullvalue STRING        Use STRING in place of NULL values",
 +];
 +DISPATCHABLE_COMMAND( open 3 1 0 ){
 +  ShellInState *psi = ISS(p);
 +  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 */
 +  u8 openMode = SHELL_OPEN_UNSPEC;
 +  int openFlags = 0;
 +  sqlite3_int64 szMax = 0;
 +  int rc = 0;
 +  /* Check for command-line arguments */
 +  for(iName=1; iName<nArg; iName++){
 +    const char *z = azArg[iName];
 +    if( optionMatch(z,"new") ){
 +      newFlag = 1;
 +#ifdef SQLITE_HAVE_ZLIB
 +    }else if( optionMatch(z, "zip") ){
 +      openMode = SHELL_OPEN_ZIPFILE;
 +#endif
 +    }else if( optionMatch(z, "append") ){
 +      openMode = SHELL_OPEN_APPENDVFS;
 +    }else if( optionMatch(z, "readonly") ){
 +      openMode = SHELL_OPEN_READONLY;
 +    }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, "maxsize") && iName+1<nArg ){
 +      szMax = integerValue(azArg[++iName]);
 +#endif /* SQLITE_OMIT_DESERIALIZE */
 +    }else if( z[0]=='-' ){
 +      *pzErr = shellMPrintf(0,"unknown option: %s\n", z);
 +      return SHELL_INVALID_ARGS;
 +    }else if( zFN ){
 +      *pzErr = shellMPrintf(0,"extra argument: \"%s\"\n", z);
 +      return SHELL_INVALID_ARGS;
 +    }else{
 +      zFN = z;
 +    }
 +  }
 +
 +  /* Close the existing database */
 +  session_close_all(psi, -1);
 +  close_db(DBX(p));
 +  DBX(p) = 0;
 +  psi->pAuxDb->zDbFilename = 0;
 +  sqlite3_free(psi->pAuxDb->zFreeOnClose);
 +  psi->pAuxDb->zFreeOnClose = 0;
 +  psi->openMode = openMode;
 +  psi->openFlags = 0;
 +  psi->szMax = 0;
 +
 +  /* If a filename is specified, try to open it first */
 +  if( zFN || psi->openMode==SHELL_OPEN_HEXDB ){
 +    if( newFlag && zFN && !psi->bSafeMode ) shellDeleteFile(zFN);
 +    if( psi->bSafeMode
 +        && psi->openMode!=SHELL_OPEN_HEXDB
 +        && zFN
 +        && strcmp(zFN,":memory:")!=0
 +        ){
 +      *pzErr = shellMPrintf(0,"cannot open database files in safe mode");
 +      return SHELL_FORBIDDEN_OP;
 +    }
 +    if( zFN ){
 +      zNewFilename = sqlite3_mprintf("%s", zFN);
 +      shell_check_oom(zNewFilename);
 +    }else{
 +      zNewFilename = 0;
 +    }
 +    psi->pAuxDb->zDbFilename = zNewFilename;
 +    psi->openFlags = openFlags;
 +    psi->szMax = szMax;
 +    open_db(p, OPEN_DB_KEEPALIVE);
 +    if( DBX(p)==0 ){
 +      *pzErr = shellMPrintf(0,"Error: cannot open '%s'\n", zNewFilename);
 +      sqlite3_free(zNewFilename);
 +      rc = 1;
 +    }else{
 +      psi->pAuxDb->zFreeOnClose = zNewFilename;
 +    }
 +  }
 +  if( DBX(p)==0 ){
 +    /* As a fall-back open a TEMP database */
 +    psi->pAuxDb->zDbFilename = 0;
 +    open_db(p, 0);
 +  }
 +  return rc;
 +}
 +
 +DISPATCHABLE_COMMAND( nonce ? 2 2 ){
 +  ShellInState *psi = ISS(p);
 +  if( psi->zNonce==0 || strcmp(azArg[1],psi->zNonce)!=0 ){
 +    raw_printf(STD_ERR, "line %d: incorrect nonce: \"%s\"\n",
 +               psi->pInSource->lineno, azArg[1]);
 +    exit(1);
 +  }
 +  /* Suspend safe mode for 1 meta-command after this. */
 +  psi->bSafeModeFuture = 2;
 +  return 0;
 +}
 +
 +DISPATCHABLE_COMMAND( nullvalue ? 2 2 ){
 +  sqlite3_snprintf(sizeof(ISS(p)->nullValue), ISS(p)->nullValue, "%.*s",
 +                   (int)ArraySize(ISS(p)->nullValue)-1, azArg[1]);
 +  return 0;
 +}
 +
 +/* Helper functions for .parameter command
 + */
 +
 +struct param_row { char * value; int uses; int hits; };
 +
 +static int param_find_callback(void *pData, int nc, char **pV, char **pC){
 +  assert(nc>=1);
 +  assert(strcmp(pC[0],"value")==0);
 +  struct param_row *pParam = (struct param_row *)pData;
 +  assert(pParam->value==0); /* key values are supposedly unique. */
 +  if( pParam->value!=0 ) sqlite3_free( pParam->value );
 +  pParam->value = sqlite3_mprintf("%s", pV[0]); /* source owned by statement */
 +  if( nc>1 ) pParam->uses = (int)integerValue(pV[1]);
 +  ++pParam->hits;
 +  return 0;
 +}
 +
 +static void append_in_clause(sqlite3_str *pStr,
 +                             const char **azBeg, const char **azLim);
 +static void append_glob_terms(sqlite3_str *pStr, const char *zColName,
 +                              const char **azBeg, const char **azLim);
 +static char *find_home_dir(int clearFlag);
 +
 +/* Create a home-relative pathname from ~ prefixed path.
 + * Return it, or 0 for any error.
 + * Caller must sqlite3_free() it.
 + */
 +static char *home_based_path( const char *zPath ){
 +  char *zHome = find_home_dir(0);
 +  char *zErr = 0;
 +  assert( zPath[0]=='~' );
 +  if( zHome==0 ){
 +    zErr = "Cannot find home directory.";
 +  }else if( zPath[0]==0 || (zPath[1]!='/'
 +#if defined(_WIN32) || defined(WIN32)
 +                            && zPath[1]!='\\'
 +#endif
 +                            ) ){
 +    zErr = "Malformed pathname";
 +  }else{
 +    return sqlite3_mprintf("%s%s", zHome, zPath+1);
 +  }
 +  utf8_printf(STD_ERR, "Error: %s\n", zErr);
 +  return 0;
 +}
 +
 +/* Transfer selected parameters between two parameter tables, for save/load.
 + * Argument bSaveNotLoad determines transfer direction and other actions.
 + * If it is true, the store DB will be created if not existent, and its
 + * table for keeping parameters will be created. Or failure is returned.
 + * If it is false, the store DB will be opened for read and its presumed
 + * table for keeping parameters will be read. Or failure is returned.
 + *
 + * Arguments azNames and nNames reference the ?NAMES? save/load arguments.
 + * If it is an empty list, all parameters will be saved or loaded.
 + * Otherwise, only the named parameters are transferred, if they exist.
 + * It is not an error to specify a name that cannot be transferred
 + * because it does not exist in the source table.
 + *
 + * Returns are SQLITE_OK for success, or other codes for failure.
 + */
 +static int param_xfr_table(sqlite3 *db, const char *zStoreDbName,
 +                           int bSaveNotLoad, const char *azNames[], int nNames){
 +  int rc = 0;
 +  char *zSql = 0; /* to be sqlite3_free()'ed */
 +  sqlite3_str *sbCopy = 0;
 +  const char *zHere = PARAM_TABLE_SNAME;
 +  const char *zThere = PARAM_STORE_SNAME;
 +  const char *zTo = (bSaveNotLoad)? zThere : zHere;
 +  const char *zFrom = (bSaveNotLoad)? zHere : zThere;
 +  sqlite3 *dbStore = 0;
 +  int openFlags = (bSaveNotLoad)
 +    ? SQLITE_OPEN_URI|SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE
 +    : SQLITE_OPEN_READONLY;
 +
 +  /* Ensure store DB can be opened and/or created appropriately. */
 +  rc = sqlite3_open_v2(zStoreDbName, &dbStore, openFlags, 0);
 +  if( rc!=SQLITE_OK ){
 +    utf8_printf(STD_ERR, "Error: Cannot %s parameter store DB %s\n",
 +                bSaveNotLoad? "open/create" : "read", zStoreDbName);
 +    return rc;
 +  }
 +  /* Ensure it has the parameter store table, or handle its absence. */
 +  assert(dbStore!=0);
 +  if( sqlite3_table_column_metadata
 +      (dbStore, "main", PARAM_STORE_NAME, 0, 0, 0, 0, 0, 0)!=SQLITE_OK ){
 +    if( !bSaveNotLoad ){
 +      utf8_printf(STD_ERR, "Error: No parameters ever stored in DB %s\n",
 +                  zStoreDbName);
 +      rc = 1;
 +    }else{
 +      /* The saved parameters table is not there yet; create it. */
 +      const char *zCT =
 +        "CREATE TABLE IF NOT EXISTS "PARAM_STORE_NAME"(\n"
 +        "  key TEXT PRIMARY KEY,\n"
 +        "  value,\n"
 +        "  uses INT\n"
 +        ") WITHOUT ROWID;";
 +      rc = sqlite3_exec(dbStore, zCT, 0, 0, 0);
 +      if( rc!=SQLITE_OK ){
 +        utf8_printf(STD_ERR, "Cannot create table %s. Nothing saved.", zThere);
 +      }
 +    }
 +  }
 +  sqlite3_close(dbStore);
 +  if( rc!=0 ) return rc;
 +
 +  zSql = sqlite3_mprintf("ATTACH %Q AS %s;", zStoreDbName, PARAM_STORE_SCHEMA);
 +  shell_check_oom(zSql);
 +  rc = sqlite3_exec(db, zSql, 0, 0, 0);
 +  sqlite3_free(zSql);
 +  if( rc!=SQLITE_OK ) return rc;
 +
 +  sbCopy = sqlite3_str_new(db);
 +  sqlite3_str_appendf
 +      (sbCopy, "INSERT OR REPLACE INTO %s(key,value,uses)"
 +       "SELECT key, value, uses FROM %s WHERE key ", zTo, zFrom);
 +  append_in_clause(sbCopy, azNames, azNames+nNames);
 +  zSql = sqlite3_str_finish(sbCopy);
 +  shell_check_oom(zSql);
 +  rc = sqlite3_exec(db, zSql, 0, 0, 0);
 +  sqlite3_free(zSql);
 +
 +  sqlite3_exec(db, "DETACH "PARAM_STORE_SCHEMA";", 0, 0, 0);
 +  return rc;
 +}
 +
 +/* Default location of parameters store DB for .parameters save/load. */
 +static const char *zDefaultParamStore = "~/sqlite_params.sdb";
 +
 +/* Possibly generate a derived path from input spec, with defaulting
 + * and conversion of leading (or only) tilde as home directory.
 + * The above-set default is used for zSpec NULL, "" or "~".
 + * When return is 0, there is an error; what needs doing cannnot be done.
 + * If the return is exactly the input, it must not be sqlite3_free()'ed.
 + * If the return differs from the input, it must be sqlite3_free()'ed.
 + */
 +static const char *params_store_path(const char *zSpec){
 +  if( zSpec==0 || zSpec[0]==0 || strcmp(zSpec,"~")==0 ){
 +    return home_based_path(zDefaultParamStore);
 +  }else if ( zSpec[0]=='~' ){
 +    return home_based_path(zSpec);
 +  }
 +  return zSpec;
 +}
 +
 +/* Load some or all parameters. Arguments are "load FILE ?NAMES?". */
 +static int parameters_load(sqlite3 *db, const char *azArg[], int nArg){
 +  const char *zStore = params_store_path((nArg>1)? azArg[1] : 0);
 +  if( zStore==0 ){
 +    utf8_printf(STD_ERR, "Cannot form parameter load path. Nothing loaded.\n");
 +    return 1;
 +  }else{
 +    const char **pzFirst = (nArg>2)? azArg+2 : 0;
 +    int nNames = (nArg>2)? nArg-2 : 0;
 +    int rc = param_xfr_table(db, zStore, 0, pzFirst, nNames);
 +    if( nArg>1 && zStore!=azArg[1] ) sqlite3_free((void*)zStore);
 +    return rc;
 +  }
 +}
 +
 +/* Save some or all parameters. Arguments are "save FILE ?NAMES?". */
 +static int parameters_save(sqlite3 *db, const char *azArg[], int nArg){
 +  const char *zStore = params_store_path((nArg>1)? azArg[1] : 0);
 +  if( zStore==0 ){
 +    utf8_printf(STD_ERR, "Cannot form parameter save path. Nothing saved.\n");
 +    return 1;
 +  }else{
 +    const char **pzFirst = (nArg>2)? azArg+2 : 0;
 +    int nNames = (nArg>2)? nArg-2 : 0;
 +    int rc = param_xfr_table(db, zStore, 1, pzFirst, nNames);
 +    if( nArg>1 && zStore!=azArg[1] ) sqlite3_free((void*)zStore);
 +    return rc;
 +  }
 +}
 +
 +#ifndef SQLITE_NOHAVE_SYSTEM
 +/*
 + * Edit one named parameter in the parameters table. If it does not
 + * yet exist, create it. If eval is true, the value is treated as a
 + * bare expression, otherwise it is a text value. The uses argument
 + * sets the 3rd column in the parameters table, and may also serve
 + * to partition the key namespace. (This is not done now.)
 + */
 +static int edit_one_param(sqlite3 *db, char *name, int eval,
 +                          ParamTableUse uses, const char * zEditor){
 +  struct param_row paramVU = {0,0,0};
 +  int rc;
 +  char * zVal = 0;
 +  char * zSql = sqlite3_mprintf
 +    ("SELECT value, uses FROM " PARAM_TABLE_SNAME " WHERE key=%Q", name);
 +  shell_check_oom(zSql);
 +  sqlite3_exec(db, zSql, param_find_callback, &paramVU, 0);
 +  sqlite3_free(zSql);
 +  assert(paramVU.hits<2);
 +  if( paramVU.hits==1 && paramVU.uses==uses){
 +    /* Editing an existing value of same kind. */
 +    sqlite3_free(paramVU.value);
 +    if( eval!=0 ){
 +      zSql = sqlite3_mprintf
 +        ("SELECT edit(value, %Q) FROM " PARAM_TABLE_SNAME
 +         " WHERE key=%Q AND uses=%d", zEditor, name, uses);
 +      zVal = db_text(db, zSql, 1);
 +      sqlite3_free(zSql);
 +      zSql = sqlite3_mprintf
 +        ("UPDATE "PARAM_TABLE_SNAME" SET value=(SELECT %s) WHERE"
 +         " key=%Q AND uses=%d", zVal, name, uses);
 +    }else{
 +      zSql = sqlite3_mprintf
 +        ("UPDATE "PARAM_TABLE_SNAME" SET value=edit(value, %Q) WHERE"
 +         " key=%Q AND uses=%d", zEditor, name, uses);
 +    }
 +  }else{
 +    /* Editing a new value of same kind. */
 +    assert(paramVU.value==0 || paramVU.uses!=uses);
 +    if( eval!=0 ){
 +      zSql = sqlite3_mprintf
 +        ("SELECT edit('-- %q%s', %Q)", name, "\n", zEditor);
 +      zVal = db_text(db, zSql, 1);
 +      sqlite3_free(zSql);
 +      zSql = sqlite3_mprintf
 +        ("INSERT INTO "PARAM_TABLE_SNAME"(key,value,uses)"
 +         " VALUES (%Q,(SELECT %s LIMIT 1),%d)",
 +         name, zVal, uses);
 +    }else{
 +      zSql = sqlite3_mprintf
 +        ("INSERT INTO "PARAM_TABLE_SNAME"(key,value,uses)"
 +         " VALUES (%Q,edit('-- %q%s', %Q),%d)",
 +         name, name, "\n", zEditor, uses);
 +    }
 +  }
 +  shell_check_oom(zSql);
 +  if( eval!=0 ){
 +  }
 +  rc = sqlite3_exec(db, zSql, 0, 0, 0);
 +  sqlite3_free(zSql);
 +  sqlite3_free(zVal);
 +  return rc!=SQLITE_OK;
 +}
 +#endif
 +
 +/* Space-join values in an argument list. *valLim is not included. */
 +char *values_join( char **valBeg, char **valLim ){
 +  char *z = 0;
 +  const char *zSep = 0;
 +  while( valBeg < valLim ){
 +    z = sqlite3_mprintf("%z%s%s", z, zSep, *valBeg);
 +    zSep = " ";
 +    ++valBeg;
 +  }
 +  return z;
 +}
 +
 +/* Get a named parameter value in form of stepped prepared statement,
 + * ready to have its value taken from the 0th column. If the name
 + * cannot be found for the given ParamTableUse, 0 is returned.
 + * The caller is responsible for calling sqlite3_finalize(pStmt),
 + * where pStmt is the return from this function.
 + */
 +static sqlite3_stmt *get_param_value(sqlite3 *db, char *name,
 +                                     ParamTableUse ptu){
 +  sqlite3_stmt *rv = 0;
 +  int rc;
 +  char *zSql = sqlite3_mprintf
 +    ( "SELECT value FROM "PARAM_TABLE_SNAME
 +      " WHERE key=%Q AND uses=%d", name, ptu );
 +  shell_check_oom(zSql);
 +  rc = sqlite3_prepare_v2(db, zSql, -1, &rv, 0);
 +  sqlite3_free(zSql);
 +  if( SQLITE_OK==rc ){
 +    if( SQLITE_ROW==sqlite3_step(rv) ) return rv;
 +    sqlite3_finalize(rv);
 +  }
 +  return 0;
 +}
 +
 +static struct ParamSetOpts {
 +  const char cCast;
 +  const char *zTypename;
 +  int evalKind;
 +} param_set_opts[] = {
 +  /* { 'q', 0, 2 }, */
 +  /* { 'x', 0, 1 }, */
 +  { 'i', "INT", 1 },
 +  { 'r', "REAL", 1 },
 +  { 'b', "BLOB", 1 },
 +  { 't', "TEXT", 0 },
 +  { 'n', "NUMERIC", 1 }
 +};
 +
 +/* Return an option character if it is single and prefixed by - or --,
 + * else return 0.
 + */
 +static char option_char(char *zArg){
 +  if( zArg[0]=='-' ){
 +    ++zArg;
 +    if( zArg[0]=='-' ) ++zArg;
 +    if( zArg[0]!=0 && zArg[1]==0 ) return zArg[0];
 +  }
 +  return 0;
 +}
 +
 +/* The set subcommand (per help text)
 + */
 +static int param_set(sqlite3 *db, char cCast,
 +                     char *name, char **valBeg, char **valLim,
 +                     ParamTableUse ptu){
 +  char *zSql = 0;
 +  int rc = SQLITE_OK, retries = 0, needsEval = 1;
 +  char *zValGlom = (valLim-valBeg>1)? values_join(valBeg, valLim) : 0;
 +  sqlite3_stmt *pStmtSet = 0;
 +  const char *zCastTo = 0;
 +  char *zValue = (zValGlom==0)? *valBeg : zValGlom;
 +  if( cCast ){
 +    struct ParamSetOpts *pSO = param_set_opts;
 +    for(; pSO-param_set_opts < ArraySize(param_set_opts); ++pSO ){
 +      if( cCast==pSO->cCast ){
 +        zCastTo = pSO->zTypename;
 +        needsEval = pSO->evalKind > 0;
 +        break;
 +      }
 +    }
 +  }
 +  if( needsEval ){
 +    if( zCastTo!=0 ){
 +      zSql = sqlite3_mprintf
 +        ( "REPLACE INTO "PARAM_TABLE_SNAME"(key,value,uses)"
 +          " VALUES(%Q,CAST((%s) AS %s),%d);", name, zValue, zCastTo, ptu );
 +    }else{
 +      zSql = sqlite3_mprintf
 +        ( "REPLACE INTO "PARAM_TABLE_SNAME"(key,value,uses)"
 +          "VALUES(%Q,(%s),%d);", name, zValue, ptu );
 +    }
 +    shell_check_oom(zSql);
 +    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmtSet, 0);
 +    sqlite3_free(zSql);
 +  }
 +  if( !needsEval || rc!=SQLITE_OK ){
 +    /* Reach here when value either requested to be cast to text, or must be. */
 +    sqlite3_finalize(pStmtSet);
 +    pStmtSet = 0;
 +    zSql = sqlite3_mprintf
 +      ( "REPLACE INTO "PARAM_TABLE_SNAME"(key,value,uses)"
 +        "VALUES(%Q,%Q,%d);", name, zValue, ptu );
 +    shell_check_oom(zSql);
 +    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmtSet, 0);
 +    assert(rc==SQLITE_OK);
 +    sqlite3_free(zSql);
 +  }
 +  sqlite3_step(pStmtSet);
 +  sqlite3_finalize(pStmtSet);
 +  sqlite3_free(zValGlom);
 +  return rc;
 +}
 +
 +/* list or ls subcommand for .parameter dot-command */
 +static void list_params(ShellExState *psx, ParamTableUse ptu, u8 bShort,
 +                        char **pzArgs, int nArg){
 +  sqlite3_stmt *pStmt = 0;
 +  sqlite3 *db = DBX(psx);
 +  sqlite3_str *sbList = sqlite3_str_new(db);
 +  int len = 0, rc;
 +  char *zFromWhere = 0;
 +  char *zSql = 0;
 +  sqlite3_str_appendf(sbList, "FROM "PARAM_TABLE_SNAME
 +                      " WHERE (?1=3 OR uses=?1) AND ");
 +  append_glob_terms(sbList, "key",
 +                    (const char **)pzArgs, (const char **)pzArgs+nArg);
 +  zFromWhere = sqlite3_str_finish(sbList);
 +  shell_check_oom(zFromWhere);
 +  zSql = sqlite3_mprintf("SELECT max(length(key)) %s", zFromWhere);
 +  shell_check_oom(zSql);
 +  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
 +  if( rc==SQLITE_OK ){
 +    sqlite3_bind_int(pStmt, 1, ptu);
 +    if( sqlite3_step(pStmt)==SQLITE_ROW ){
 +      len = sqlite3_column_int(pStmt, 0);
 +      if( len>40 ) len = 40;
 +      if( len<4 ) len = 4;
 +    }
 +  }
 +  sqlite3_finalize(pStmt);
 +  pStmt = 0;
 +  if( len ){
 +    FILE *out = ISS(psx)->out;
 +    sqlite3_free(zSql);
 +    if( !bShort ){
 +      int nBindings = 0, nScripts = 0;
 +      zSql = sqlite3_mprintf("SELECT key, uses, iif(uses, value, quote(value))"
 +                             " %z ORDER BY uses, key", zFromWhere);
 +      shell_check_oom(zSql);
 +      rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
 +      sqlite3_bind_int(pStmt, 1, ptu);
 +      while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
 +        ParamTableUse ptux = sqlite3_column_int(pStmt,1);
 +        switch( ptux ){
 +        case PTU_Binding:
 +          if( nBindings++ == 0 ){
 +            utf8_printf(out, "Binding Values:\n%-*s %s\n",
 +                        len, "name", "value");
 +          }
 +          utf8_printf(out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
 +                      sqlite3_column_text(pStmt,2));
 +          break;
 +        case PTU_Script:
 +          if( nScripts++ == 0 ){
 +            utf8_printf(out, "Scripts\n%-*s %s\n", len, "name", "value");
 +          }
 +          utf8_printf(out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
 +                      sqlite3_column_text(pStmt,2));
 +          break;
 +        default: break; /* Ignore */
 +        }
 +      }
 +    }else{
 +      int nc = 0, ncw = 78/(len+2);
 +      zSql = sqlite3_mprintf("SELECT key %z ORDER BY key", zFromWhere);
 +      shell_check_oom(zSql);
 +      rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
 +      sqlite3_bind_int(pStmt, 1, ptu);
 +      while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
 +        utf8_printf(out, "%s  %-*s", ((++nc%ncw==0)? "\n" : ""),
 +                    len, sqlite3_column_text(pStmt,0));
 +      }
 +      if( nc>0 ) utf8_printf(out, "\n");
 +    }
 +    sqlite3_finalize(pStmt);
 +  }else{
 +    sqlite3_free(zFromWhere);
 +  }
 +  sqlite3_free(zSql);
 +}
 +
 +/* Append an OR'ed series of GLOB terms comparing a given column
 + * name to a series of patterns. Result is an appended expression.
 + * For an empty pattern series, expression is true for non-NULL.
 + */
 +static void append_glob_terms(sqlite3_str *pStr, const char *zColName,
 +                              const char **azBeg, const char **azLim){
 +  if( azBeg==azLim ) sqlite3_str_appendf(pStr, "%s NOT NULL", zColName);
 +  else{
 +    char *zSep = "(";
 +    while( azBeg<azLim ){
 +      sqlite3_str_appendf(pStr, "%sglob(%Q,%s)", zSep, *azBeg, zColName);
 +      zSep = " OR ";
 +      ++azBeg;
 +    }
 +    sqlite3_str_appendf(pStr, ")");
 +  }
 +}
 +
 +/* Append either an IN clause or an always true test to some SQL.
 + *
 + * An empty IN list is the same as always true (for non-NULL LHS)
 + * for this clause, which assumes a trailing LHS operand and space.
 + * If that is not the right result, guard the call against it.
 + * This is used for ".parameter dostuff ?NAMES?" options,
 + * where a missing list means all the qualifying entries.
 + *
 + * The empty list may be signified by azBeg and azLim both 0.
 + */
 +static void append_in_clause(sqlite3_str *pStr,
 +                             const char **azBeg, const char **azLim){
 +  if( azBeg==azLim ) sqlite3_str_appendf(pStr, "NOT NULL");
 +  else{
 +    char cSep = '(';
 +    sqlite3_str_appendf(pStr, "IN");
 +    while( azBeg<azLim ){
 +      sqlite3_str_appendf(pStr, "%c%Q", cSep, *azBeg);
 +      cSep = ',';
 +      ++azBeg;
 +    }
 +    sqlite3_str_appendf(pStr, ")");
 +  }
 +}
 +
 +/*****************
 + * The .parameter command
 + */
 +COLLECT_HELP_TEXT[
 +  ".parameter CMD ...       Manage SQL parameter bindings and scripts table",
 +  "   clear ?NAMES?           Erase all or only given named parameters",
 +#ifndef SQLITE_NOHAVE_SYSTEM
 +  "   edit ?OPT? NAME ...     Use edit() to create or alter parameter NAME",
 +  "      OPT may be -t to use edited value as text or -e to evaluate it.",
 +#endif
 +  "   init                    Initialize TEMP table for bindings and scripts",
 +  "   list ?PATTERNS?         List parameters table binding and script values",
 +  "      Alternatively, to list just some or all names: ls ?PATTERNS?",
 +  "   load ?FILE? ?NAMES?     Load some or all named parameters from FILE",
 +  "      If FILE missing, empty or '~', it defaults to ~/sqlite_params.sdb",
 +  "   save ?FILE? ?NAMES?     Save some or all named parameters into FILE",
 +  "      If FILE missing, empty or '~', it defaults to ~/sqlite_params.sdb",
 +  "   set ?TOPT? NAME VALUE   Give SQL parameter NAME a value of VALUE",
 +  "      NAME must begin with one of $,:,@,? for bindings, or with a letter",
 +  "      to be executable; value is the space-joined argument list.",
 +  "      Option TOPT may be one of {-b -i -n -r -t} to cast effective value",
 +  "      to BLOB, INT, NUMERIC, REAL or TEXT respectively.",
 +  "   unset ?NAMES?           Remove named parameter(s) from parameters table",
 +];
 +DISPATCHABLE_COMMAND( parameter 2 2 0 ){
 +  int rc = 0;
 +  open_db(p,0);
 +  sqlite3 *db = DBX(p);
 +
 +  /* .parameter clear  and  .parameter unset ?NAMES?
 +  **  Delete some or all parameters from the TEMP table that holds them.
 +  **  Without any arguments, clear deletes them all and unset does nothing.
 +  */
 +  if( strcmp(azArg[1],"clear")==0 || strcmp(azArg[1],"unset")==0 ){
 +    if( param_table_exists(db) && (nArg>2 || azArg[1][0]=='c') ){
 +      sqlite3_str *sbZap = sqlite3_str_new(db);
 +      char *zSql;
 +      sqlite3_str_appendf
 +        (sbZap, "DELETE FROM "PARAM_TABLE_SNAME" WHERE key ");
 +      append_in_clause(sbZap,
 +                       (const char **)&azArg[2], (const char **)&azArg[nArg]);
 +      zSql = sqlite3_str_finish(sbZap);
 +      shell_check_oom(zSql);
 +      sqlite3_exec(db, zSql, 0, 0, 0);
 +      sqlite3_free(zSql);
 +    }
 +  }else
 +#ifndef SQLITE_NOHAVE_SYSTEM
 +  /* .parameter edit ?NAMES?
 +  ** Edit the named parameters. Any that do not exist are created.
 +  ** New ones get a uses tag auto-selected by their leading char.
 +  */
 +  if( strcmp(azArg[1],"edit")==0 ){
 +    ShellInState *psi = ISS(p);
 +    int ia = 2;
 +    int eval = 0;
 +    if( !INSOURCE_IS_INTERACTIVE(psi->pInSource) ){
 +      utf8_printf(STD_ERR, "Error: "
 +                  ".parameter edit can only be used interactively.\n");
 +      return 1;
 +    }
 +    param_table_init(db);
 +    if( psi->zEditor==0 ){
 +      const char *zE = getenv("VISUAL");
 +      if( zE!=0 ) psi->zEditor = sqlite3_mprintf("%s", zE);
 +    }
 +    if( nArg>=3 && azArg[2][0]=='-' ){
 +      char *zArg = (azArg[2][1]=='-')? azArg[2]+2 : azArg[2]+1;
 +      if( strncmp(zArg,"editor=",7)==0 ){
 +        sqlite3_free(psi->zEditor);
 +        /* Accept an initial -editor=? option. */
 +        psi->zEditor = sqlite3_mprintf("%s", zArg+7);
 +        ++ia;
 +      }
 +    }
 +    if( psi->zEditor==0 ){
 +      utf8_printf(STD_ERR,
 +                  "Either set env-var VISUAL to name an"
 +                  " editor and restart, or rerun\n "
 +                  ".parameter edit with an initial "
 +                  "edit option, --editor=EDITOR_COMMAND .\n");
 +      return 1;
 +    }
 +    /* Future: Allow an option whereby new value can be evaluated
 +     * the way that .parameter set ... does.
 +     */
 +    while( ia < nArg ){
 +      ParamTableUse ptu;
 +      char cf = (azArg[ia][0]=='-')? azArg[ia][1] : 0;
 +      if( cf!=0 && azArg[ia][2]==0 ){
 +        ++ia;
 +        switch( cf ){
 +        case 'e': eval = 1; continue;
 +        case 't': eval = 0; continue;
 +        default:
 +          utf8_printf(STD_ERR, "Error: bad .parameter name: %s\n",
 +                      azArg[--ia]);
 +          return 1;
 +        }
 +      }
 +      ptu = classify_param_name(azArg[ia]);
 +      if( ptu==PTU_Nil ){
 +        utf8_printf(STD_ERR, "Error: %s cannot be a binding or executable"
 +                    " parameter name.\n", azArg[ia]);
 +        return 1;
 +      }
 +      rc = edit_one_param(db, azArg[ia], eval, ptu, psi->zEditor);
 +      ++ia;
 +      if( rc!=0 ) return rc;
 +    }
 +  }else
 +#endif
 +
 +  /* .parameter init
 +  ** Make sure the TEMP table used to hold bind parameters exists.
 +  ** Create it if necessary.
 +  */
 +  if( nArg==2 && strcmp(azArg[1],"init")==0 ){
 +    param_table_init(db);
 +  }else
 +
 +  /* .parameter list|ls
 +  ** List all or selected bind parameters.
 +  ** list displays names, values and uses.
 +  ** ls displays just the names.
 +  */
 +  if( nArg>=2 && ((strcmp(azArg[1],"list")==0)
 +                  || (strcmp(azArg[1],"ls")==0)) ){
 +    list_params(p, PTU_Nil, azArg[1][1]=='s', azArg+2, nArg-2);
 +  }else
 +
 +  /* .parameter load
 +  ** Load all or named parameters from specified or default (DB) file.
 +  */
 +  if( strcmp(azArg[1],"load")==0 ){
 +    param_table_init(db);
 +    rc = parameters_load(db, (const char **)azArg+1, nArg-1);
 +  }else
 +
 +  /* .parameter save
 +  ** Save all or named parameters into specified or default (DB) file.
 +  */
 +  if( strcmp(azArg[1],"save")==0 ){
 +    rc = parameters_save(db, (const char **)azArg+1, nArg-1);
 +  }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 && strcmp(azArg[1],"set")==0 ){
 +    char cCast = option_char(azArg[2]);
 +    int inv = 2 + (cCast != 0);
 +    ParamTableUse ptu = classify_param_name(azArg[inv]);
 +    if( ptu==PTU_Nil ){
 +      utf8_printf(STD_ERR,
 +                  "Error: %s is not a usable parameter name.\n", azArg[inv]);
 +      rc = 1;
 +    }else{
 +      param_table_init(db);
 +      rc = param_set(db, cCast, azArg[inv],
 +                     &azArg[inv+1], &azArg[nArg], ptu);
 +      if( rc!=SQLITE_OK ){
 +        utf8_printf(STD_ERR, "Error: %s\n", sqlite3_errmsg(db));
 +        rc = 1;
 +      }
 +    }
 +  }else
 +
 +  {  /* If no command name and arg count matches, show a syntax error */
 +    showHelp(ISS(p)->out, "parameter", p);
 +    return 1;
 +  }
 +
 +  return rc;
 +}
 +
 +/*****************
 + * The .print, .progress and .prompt commands
 + */
 +CONDITION_COMMAND( progress !defined(SQLITE_OMIT_PROGRESS_CALLBACK) );
 +COLLECT_HELP_TEXT[
 +  ".print STRING...         Print literal STRING",
 +  ".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",
 +  ".prompt MAIN CONTINUE    Replace the standard prompts",
 +];
 +DISPATCHABLE_COMMAND( print 3 1 0 ){
 +  int i;
 +  for(i=1; i<nArg; i++){
 +    utf8_printf(ISS(p)->out, "%s%s", azArg[i], (i==nArg-1)? "\n" : " ");
 +  }
 +  return 0;
 +}
 +DISPATCHABLE_COMMAND( progress 3 2 0 ){
 +  ShellInState *psi = ISS(p);
 +  int i;
 +  int nn = 0;
 +  psi->flgProgress = 0;
 +  psi->mxProgress = 0;
 +  psi->nProgress = 0;
 +  for(i=1; i<nArg; i++){
 +    const char *z = azArg[i];
 +    if( z[0]=='-' ){
 +      z++;
 +      if( z[0]=='-' ) z++;
 +      if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
 +        psi->flgProgress |= SHELL_PROGRESS_QUIET;
 +        continue;
 +      }
 +      if( strcmp(z,"reset")==0 ){
 +        psi->flgProgress |= SHELL_PROGRESS_RESET;
 +        continue;
 +      }
 +      if( strcmp(z,"once")==0 ){
 +        psi->flgProgress |= SHELL_PROGRESS_ONCE;
 +        continue;
 +      }
 +      if( strcmp(z,"limit")==0 ){
 +        if( i+1>=nArg ){
 +          *pzErr = shellMPrintf(0,"Error: missing argument on --limit\n");
 +          return SHELL_INVALID_ARGS;
 +        }else{
 +          psi->mxProgress = (int)integerValue(azArg[++i]);
 +        }
 +        continue;
 +      }
 +      *pzErr = shellMPrintf(0, "Error: unknown option: \"%s\"\n", azArg[i]);
 +      return SHELL_INVALID_ARGS;
 +    }else{
 +      nn = (int)integerValue(z);
 +    }
 +  }
 +  open_db(p, 0);
 +  sqlite3_progress_handler(DBX(p), nn, progress_handler, psi);
 +  return 0;
 +}
 +/* Allow too few arguments by tradition, (a form of no-op.) */
 +DISPATCHABLE_COMMAND( prompt ? 1 3 ){
 +  if( nArg >= 2) {
 +    strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
 +  }
 +  if( nArg >= 3) {
 +    strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
 +  }
 +  return 0;
 +}
 +
 +/*****************
 + * The .read, .recover and .restore commands
 + */
 +CONDITION_COMMAND( recover !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) );
 +COLLECT_HELP_TEXT[
 +  ".read FILE               Read input from FILE",
 +  "   If FILE begins with \"|\", it is a command that generates the input.",
 +  ".recover                 Recover as much data as possible from corrupt db.",
 +  "   --freelist-corrupt       Assume the freelist is corrupt",
 +  "   --recovery-db NAME       Store recovery metadata in database file NAME",
 +  "   --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",
 +  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
 +];
 +DISPATCHABLE_COMMAND( read 3 2 2 ){
 +  int rc = 0;
 +  FILE *inUse = 0;
 +  int (*fCloser)(FILE *) = 0;
 +  if( ISS(p)->bSafeMode ) return SHELL_FORBIDDEN_OP;
 +  if( azArg[1][0]=='|' ){
 +#ifdef SQLITE_OMIT_POPEN
 +    *pzErr = shellMPrintf(0,"Error: pipes are not supported in this OS\n");
 +    rc = 1;
 +    /* p->out = STD_OUT; This was likely not needed. To be investigated. */
 +#else
 +    inUse = popen(azArg[1]+1, "r");
 +    if( inUse==0 ){
 +      utf8_printf(STD_ERR, "Error: cannot open \"%s\"\n", azArg[1]);
 +      rc = 1;
 +    }else{
 +      fCloser = pclose;
 +    }
 +#endif
 +  }else if( (inUse = openChrSource(azArg[1]))==0 ){
 +    *pzErr = shellMPrintf(0,"Error: cannot open \"%s\"\n", azArg[1]);
 +    rc = 1;
 +  }else{
 +    fCloser = fclose;
 +  }
 +  if( inUse!=0 ){
 +    InSource inSourceRedir
 +      = INSOURCE_FILE_REDIR(inUse, azArg[1], ISS(p)->pInSource);
 +    ISS(p)->pInSource = &inSourceRedir;
 +    rc = process_input(ISS(p));
 +    assert(fCloser!=0);
 +    fCloser(inUse);
 +    ISS(p)->pInSource = inSourceRedir.pFrom;
 +  }
 +  return rc;
 +}
 +
 +/*
 +** This command is invoked to recover data from the database. A script
 +** to construct a new database containing all recovered data is output
 +** on stream pState->out.
 +*/
 +DISPATCHABLE_COMMAND( recover ? 1 7 ){
 +  FILE *out = ISS(p)->out;
 +  sqlite3 *db;
 +  int rc = SQLITE_OK;
 +  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
 +  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
 +  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
 +  const char *zRecoveryDb = "";   /* Name of "recovery" database */
 +  const char *zLostAndFound = "lost_and_found";
 +  int i;
 +  int nOrphan = -1;
 +  RecoverTable *pOrphan = 0;
 +
 +  open_db(p, 0);
 +  db = DBX(p);
 +
 +  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
 +  int bRowids = 1;                /* 0 if --no-rowids */
 +  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("-freelist-corrupt", z, n)==0 ){
 +      bFreelist = 0;
 +    }else
 +    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
 +      i++;
 +      zRecoveryDb = azArg[i];
 +    }else
 +    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
 +      i++;
 +      zLostAndFound = azArg[i];
 +    }else
 +    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
 +      bRowids = 0;
 +    }
 +    else{
 +      *pzErr = shellMPrintf(0,"unexpected option: %s\n", azArg[i]);
 +      showHelp(out, azArg[0], p);
 +      return 1;
 +    }
 +  }
 +
 +  shellExecPrintf(db, &rc,
 +    /* Attach an in-memory database named 'recovery'. Create an indexed
 +    ** cache of the sqlite_dbptr virtual table. */
 +    "PRAGMA writable_schema = on;"
 +    "ATTACH %Q AS recovery;"
 +    "DROP TABLE IF EXISTS recovery.dbptr;"
 +    "DROP TABLE IF EXISTS recovery.freelist;"
 +    "DROP TABLE IF EXISTS recovery.map;"
 +    "DROP TABLE IF EXISTS recovery.schema;"
 +    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
 +  );
 +
 +  if( bFreelist ){
 +    shellExec(db, &rc,
 +      "WITH trunk(pgno) AS ("
 +      "  SELECT shell_int32("
 +      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
 +      "      WHERE x>0"
 +      "    UNION"
 +      "  SELECT shell_int32("
 +      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
 +      "      FROM trunk WHERE x>0"
 +      "),"
 +      "freelist(data, n, freepgno) AS ("
 +      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
 +      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
 +      "    UNION ALL"
 +      "  SELECT data, n-1, shell_int32(data, 2+n) "
 +      "      FROM freelist WHERE n>=0"
 +      ")"
 +      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
 +    );
 +  }
 +
 +  /* If this is an auto-vacuum database, add all pointer-map pages to
 +  ** the freelist table. Do this regardless of whether or not
 +  ** --freelist-corrupt was specified.  */
 +  shellExec(db, &rc,
 +    "WITH ptrmap(pgno) AS ("
 +    "  SELECT 2 WHERE shell_int32("
 +    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
 +    "  )"
 +    "    UNION ALL "
 +    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
 +    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
 +    ")"
 +    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
 +  );
 +
 +  shellExec(db, &rc,
 +    "CREATE TABLE recovery.dbptr("
 +    "      pgno, child, PRIMARY KEY(child, pgno)"
 +    ") WITHOUT ROWID;"
 +    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
 +    "    SELECT * FROM sqlite_dbptr"
 +    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
 +
 +    /* Delete any pointer to page 1. This ensures that page 1 is considered
 +    ** a root page, regardless of how corrupt the db is. */
 +    "DELETE FROM recovery.dbptr WHERE child = 1;"
 +
 +    /* Delete all pointers to any pages that have more than one pointer
 +    ** to them. Such pages will be treated as root pages when recovering
 +    ** data.  */
 +    "DELETE FROM recovery.dbptr WHERE child IN ("
 +    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
 +    ");"
 +
 +    /* Create the "map" table that will (eventually) contain instructions
 +    ** for dealing with each page in the db that contains one or more
 +    ** records. */
 +    "CREATE TABLE recovery.map("
 +      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
 +    ");"
 +
 +    /* Populate table [map]. If there are circular loops of pages in the
 +    ** database, the following adds all pages in such a loop to the map
 +    ** as individual root pages. This could be handled better.  */
 +    "WITH pages(i, maxlen) AS ("
 +    "  SELECT page_count, ("
 +    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
 +    "  ) FROM pragma_page_count WHERE page_count>0"
 +    "    UNION ALL"
 +    "  SELECT i-1, ("
 +    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
 +    "  ) FROM pages WHERE i>=2"
 +    ")"
 +    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
 +    "  SELECT i, maxlen, NULL, ("
 +    "    WITH p(orig, pgno, parent) AS ("
 +    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
 +    "        UNION "
 +    "      SELECT i, p.parent, "
 +    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
 +    "    )"
 +    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
 +    ") "
 +    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
 +    "UPDATE recovery.map AS o SET intkey = ("
 +    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
 +    ");"
 +
 +    /* Extract data from page 1 and any linked pages into table
 +    ** recovery.schema. With the same schema as an sqlite_schema table.  */
 +    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
 +    "INSERT INTO recovery.schema SELECT "
 +    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
 +    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
 +    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
 +    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
 +    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
 +    "FROM sqlite_dbdata WHERE pgno IN ("
 +    "  SELECT pgno FROM recovery.map WHERE root=1"
 +    ")"
 +    "GROUP BY pgno, cell;"
 +    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
 +  );
 +
 +  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
 +  ** CREATE TABLE statements that extracted from the existing schema.  */
 +  if( rc==SQLITE_OK ){
 +    sqlite3_stmt *pStmt = 0;
 +    /* ".recover" might output content in an order which causes immediate
 +    ** foreign key constraints to be violated. So disable foreign-key
 +    ** constraint enforcement to prevent problems when running the output
 +    ** script. */
 +    raw_printf(out, "PRAGMA foreign_keys=OFF;\n");
 +    raw_printf(out, "BEGIN;\n");
 +    raw_printf(out, "PRAGMA writable_schema = on;\n");
 +    shellPrepare(db, &rc,
 +        "SELECT sql FROM recovery.schema "
 +        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
 +    );
 +    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
 +      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
 +      raw_printf(out, "CREATE TABLE IF NOT EXISTS %s;\n", &zCreateTable[12]);
 +    }
 +    shellFinalize(&rc, pStmt);
 +  }
 +
 +  /* Figure out if an orphan table will be required. And if so, how many
 +  ** user columns it should contain */
 +  shellPrepare(db, &rc,
 +      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
 +      , &pLoop
 +  );
 +  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
 +    nOrphan = sqlite3_column_int(pLoop, 0);
 +  }
 +  shellFinalize(&rc, pLoop);
 +  pLoop = 0;
 +
 +  shellPrepare(db, &rc,
 +      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
 +  );
 +
 +  shellPrepare(db, &rc,
 +      "SELECT max(field), group_concat(shell_escape_crnl(quote"
 +      "(case when (? AND field<0) then NULL else value end)"
 +      "), ', ')"
 +      ", min(field) "
 +      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
 +      "GROUP BY cell", &pCells
 +  );
 +
 +  /* Loop through each root page. */
 +  shellPrepare(db, &rc,
 +      "SELECT root, intkey, max(maxlen) FROM recovery.map"
 +      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
 +      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
 +      ")", &pLoop
 +  );
 +  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
 +    int iRoot = sqlite3_column_int(pLoop, 0);
 +    int bIntkey = sqlite3_column_int(pLoop, 1);
 +    int nCol = sqlite3_column_int(pLoop, 2);
 +    int bNoop = 0;
 +    RecoverTable *pTab;
 +
 +    assert( bIntkey==0 || bIntkey==1 );
 +    pTab = recoverFindTable(db, &rc, iRoot, bIntkey, nCol, &bNoop);
 +    if( bNoop || rc ) continue;
 +    if( pTab==0 ){
 +      if( pOrphan==0 ){
 +        pOrphan = recoverOrphanTable(db, out, &rc, zLostAndFound, nOrphan);
        }
 -    }else{
 -      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
 -      rc = 1;
 +      pTab = pOrphan;
 +      if( pTab==0 ) break;
      }
 -    sqlite3_free(zSql);
 -  }else
 -#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
  
 -#ifdef SQLITE_ENABLE_IOTRACE
 -  if( c=='i' && 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( strcmp(azArg[1], "-")==0 ){
 -      sqlite3IoTrace = iotracePrintf;
 -      iotrace = stdout;
 +    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
 +      raw_printf(out, "DELETE FROM sqlite_sequence;\n");
 +    }
 +    sqlite3_bind_int(pPages, 1, iRoot);
 +    if( bRowids==0 && pTab->iPk<0 ){
 +      sqlite3_bind_int(pCells, 1, 1);
      }else{
 -      iotrace = fopen(azArg[1], "w");
 -      if( iotrace==0 ){
 -        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
 -        sqlite3IoTrace = 0;
 -        rc = 1;
 -      }else{
 -        sqlite3IoTrace = iotracePrintf;
 -      }
 +      sqlite3_bind_int(pCells, 1, 0);
      }
 -  }else
 -#endif
 +    sqlite3_bind_int(pCells, 3, pTab->iPk);
  
 -  if( c=='l' && n>=5 && 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                },
 -      { "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++){
 -        printf("%20s %d\n", aLimit[i].zLimitName,
 -               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
 -      }
 -    }else if( nArg>3 ){
 -      raw_printf(stderr, "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{
 -            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
 -            rc = 1;
 -            goto meta_command_exit;
 +    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
 +      int iPgno = sqlite3_column_int(pPages, 0);
 +      sqlite3_bind_int(pCells, 2, iPgno);
 +      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
 +        int nField = sqlite3_column_int(pCells, 0);
 +        int iMin = sqlite3_column_int(pCells, 2);
 +        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
 +
 +        RecoverTable *pTab2 = pTab;
 +        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
 +          if( pOrphan==0 ){
 +            pOrphan = recoverOrphanTable(db, out, &rc, zLostAndFound, nOrphan);
            }
 +          pTab2 = pOrphan;
 +          if( pTab2==0 ) break;
 +        }
 +
 +        nField = nField+1;
 +        if( pTab2==pOrphan ){
 +          raw_printf(out,
 +              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
 +              pTab2->zQuoted, iRoot, iPgno, nField,
 +              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
 +          );
 +        }else{
 +          raw_printf(out, "INSERT INTO %s(%s) VALUES( %s );\n",
 +              pTab2->zQuoted, pTab2->azlCol[nField], zVal
 +          );
          }
        }
 -      if( iLimit<0 ){
 -        utf8_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]));
 +      shellReset(&rc, pCells);
 +    }
 +    shellReset(&rc, pPages);
 +    if( pTab!=pOrphan ) recoverFreeTable(pTab);
 +  }
 +  shellFinalize(&rc, pLoop);
 +  shellFinalize(&rc, pPages);
 +  shellFinalize(&rc, pCells);
 +  recoverFreeTable(pOrphan);
 +
 +  /* The rest of the schema */
 +  if( rc==SQLITE_OK ){
 +    sqlite3_stmt *pStmt = 0;
 +    shellPrepare(db, &rc,
 +        "SELECT sql, name FROM recovery.schema "
 +        "WHERE sql NOT LIKE 'create table%'", &pStmt
 +    );
 +    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
 +      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
 +      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
 +        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
 +        char *zPrint = shellMPrintf(&rc,
 +          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
 +          zName, zName, zSql
 +        );
 +        raw_printf(out, "%s;\n", zPrint);
 +        sqlite3_free(zPrint);
 +      }else{
 +        raw_printf(out, "%s;\n", zSql);
        }
 -      printf("%20s %d\n", aLimit[iLimit].zLimitName,
 -             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
      }
 -  }else
 +    shellFinalize(&rc, pStmt);
 +  }
  
 -  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
 -    open_db(p, 0);
 -    lintDotCommand(p, azArg, nArg);
 -  }else
 +  if( rc==SQLITE_OK ){
 +    raw_printf(out, "PRAGMA writable_schema = off;\n");
 +    raw_printf(out, "COMMIT;\n");
 +  }
 +  sqlite3_exec(db, "DETACH recovery", 0, 0, 0);
 +  return rc;
 +}
  
 -#ifndef SQLITE_OMIT_LOAD_EXTENSION
 -  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
 -    const char *zFile, *zProc;
 -    char *zErrMsg = 0;
 -    failIfSafeMode(p, "cannot run .load in safe mode");
 -    if( nArg<2 ){
 -      raw_printf(stderr, "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 ){
 -      utf8_printf(stderr, "Error: %s\n", zErrMsg);
 -      sqlite3_free(zErrMsg);
 -      rc = 1;
 +DISPATCHABLE_COMMAND( restore ? 2 3 ){
 +  int rc;
 +  const char *zSrcFile;
 +  const char *zDb;
 +  sqlite3 *pSrc;
 +  sqlite3_backup *pBackup;
 +  int nTimeout = 0;
 +
 +  if( ISS(p)->bSafeMode ) return SHELL_FORBIDDEN_OP;
 +  if( nArg==2 ){
 +    zSrcFile = azArg[1];
 +    zDb = "main";
 +  }else if( nArg==3 ){
 +    zSrcFile = azArg[2];
 +    zDb = azArg[1];
 +  }else{
 +    return SHELL_INVALID_ARGS;
 +  }
 +  rc = sqlite3_open(zSrcFile, &pSrc);
 +  if( rc!=SQLITE_OK ){
 +    *pzErr = shellMPrintf(0,"Error: cannot open \"%s\"\n", zSrcFile);
 +    close_db(pSrc);
 +    return 1;
 +  }
 +  open_db(p, 0);
 +  pBackup = sqlite3_backup_init(DBX(p), zDb, pSrc, "main");
 +  if( pBackup==0 ){
 +    *pzErr = shellMPrintf(0,"Error: %s\n", sqlite3_errmsg(DBX(p)));
 +    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);
      }
 -  }else
 +  }
 +  sqlite3_backup_finish(pBackup);
 +  if( rc==SQLITE_DONE ){
 +    rc = 0;
 +  }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
 +    *pzErr = shellMPrintf(0,"Error: source database is busy\n");
 +    rc = 1;
 +  }else{
 +    *pzErr = shellMPrintf(0,"Error: %s\n", sqlite3_errmsg(DBX(p)));
 +    rc = 1;
 +  }
 +  close_db(pSrc);
 +  return rc;
 +}
 +
 +/*****************
 + * The .scanstats and .schema commands
 + */
 +COLLECT_HELP_TEXT[
 +  ".scanstats on|off        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_\"",
 +];
 +DISPATCHABLE_COMMAND( scanstats ? 2 2 ){
 +  ISS(p)->scanstatsOn = (u8)booleanValue(azArg[1]);
 +#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
 +  raw_printf(STD_ERR, "Warning: .scanstats not available in this build.\n");
  #endif
 +  return 0;
 +}
 +DISPATCHABLE_COMMAND( schema ? 1 2 ){
 +  int rc;
 +  ShellText sSelect;
 +  ShellInState data;
 +  ShellExState datax;
 +  char *zErrMsg = 0;
 +  const char *zDiv = "(";
 +  const char *zName = 0;
 +  int iSchema = 0;
 +  int bDebug = 0;
 +  int bNoSystemTabs = 0;
 +  int ii;
  
 -  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
 -    failIfSafeMode(p, "cannot run .log in safe mode");
 -    if( nArg!=2 ){
 -      raw_printf(stderr, "Usage: .log FILENAME\n");
 -      rc = 1;
 +  open_db(p, 0);
 +  /* Consider some refactoring to avoid duplicative wholesale copying. */
 +  memcpy(&data, ISS(p), sizeof(data));
 +  memcpy(&datax, p, sizeof(datax));
 +  data.pSXS = &datax;
 +  datax.pSIS = &data;
 +
 +  data.showHeader = 0;
 +  data.cMode = data.mode = MODE_Semi;
 +  initText(&sSelect);
 +  for(ii=1; ii<nArg; ii++){
 +    if( optionMatch(azArg[ii],"indent") ){
 +      data.cMode = data.mode = MODE_Pretty;
 +    }else if( optionMatch(azArg[ii],"debug") ){
 +      bDebug = 1;
 +    }else if( optionMatch(azArg[ii],"nosys") ){
 +      bNoSystemTabs = 1;
 +    }else if( azArg[ii][0]=='-' ){
 +      *pzErr = shellMPrintf(0,"Unknown option: \"%s\"\n", azArg[ii]);
 +      return SHELL_INVALID_ARGS;
 +    }else if( zName==0 ){
 +      zName = azArg[ii];
      }else{
 -      const char *zFile = azArg[1];
 -      output_file_close(p->pLog);
 -      p->pLog = output_file_open(zFile, 0);
 +      return SHELL_INVALID_ARGS;
 +    }
 +  }
 +  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 ){
 +      char *new_argv[2], *new_colv[2];
 +      new_argv[0] = sqlite3_mprintf(
 +                                    "CREATE TABLE %s (\n"
 +                                    "  type text,\n"
 +                                    "  name text,\n"
 +                                    "  tbl_name text,\n"
 +                                    "  rootpage integer,\n"
 +                                    "  sql text\n"
 +                                    ")", zName);
 +      shell_check_oom(new_argv[0]);
 +      new_argv[1] = 0;
 +      new_colv[0] = "sql";
 +      new_colv[1] = 0;
 +      callback(&datax, 1, new_argv, new_colv);
 +      sqlite3_free(new_argv[0]);
 +    }
 +  }
 +  if( zDiv ){
 +    sqlite3_stmt *pStmt = 0;
 +    rc = sqlite3_prepare_v2(datax.dbUser,
 +                            "SELECT name FROM pragma_database_list",
 +                            -1, &pStmt, 0);
 +    if( rc ){
 +      *pzErr = shellMPrintf(0,"Error: %s\n", sqlite3_errmsg(datax.dbUser));
 +      sqlite3_finalize(pStmt);
 +      return 1;
      }
 -  }else
 -
 -  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
 -    const char *zMode = 0;
 -    const char *zTabname = 0;
 -    int i, n2;
 -    ColModeOpts cmOpts = ColModeOpts_default;
 -    for(i=1; i<nArg; i++){
 -      const char *z = azArg[i];
 -      if( optionMatch(z,"wrap") && i+1<nArg ){
 -        cmOpts.iWrap = integerValue(azArg[++i]);
 -      }else if( optionMatch(z,"ww") ){
 -        cmOpts.bWordWrap = 1;
 -      }else if( optionMatch(z,"wordwrap") && i+1<nArg ){
 -        cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
 -      }else if( optionMatch(z,"quote") ){
 -        cmOpts.bQuote = 1;
 -      }else if( optionMatch(z,"noquote") ){
 -        cmOpts.bQuote = 0;
 -      }else if( zMode==0 ){
 -        zMode = z;
 -        /* Apply defaults for qbox pseudo-mods. If that
 -         * overwrites already-set values, user was informed of this.
 -         */
 -        if( strcmp(z, "qbox")==0 ){
 -          ColModeOpts cmo = ColModeOpts_default_qbox;
 -          zMode = "box";
 -          cmOpts = cmo;
 -        }
 -      }else if( zTabname==0 ){
 -        zTabname = z;
 -      }else if( z[0]=='-' ){
 -        utf8_printf(stderr, "unknown option: %s\n", z);
 -        utf8_printf(stderr, "options:\n"
 -                            "  --noquote\n"
 -                            "  --quote\n"
 -                            "  --wordwrap on/off\n"
 -                            "  --wrap N\n"
 -                            "  --ww\n");
 -        rc = 1;
 -        goto meta_command_exit;
 +    appendText(&sSelect, "SELECT sql FROM", 0);
 +    iSchema = 0;
 +    while( sqlite3_step(pStmt)==SQLITE_ROW ){
 +      const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
 +      char zScNum[30];
 +      sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
 +      appendText(&sSelect, zDiv, 0);
 +      zDiv = " UNION ALL ";
 +      appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
 +      if( sqlite3_stricmp(zDb, "main")!=0 ){
 +        appendText(&sSelect, zDb, '\'');
        }else{
 -        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
 -        rc = 1;
 -        goto meta_command_exit;
 +        appendText(&sSelect, "NULL", 0);
        }
 +      appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
 +      appendText(&sSelect, zScNum, 0);
 +      appendText(&sSelect, " AS snum, ", 0);
 +      appendText(&sSelect, zDb, '\'');
 +      appendText(&sSelect, " AS sname FROM ", 0);
 +      appendText(&sSelect, zDb, quoteChar(zDb));
 +      appendText(&sSelect, ".sqlite_schema", 0);
      }
 -    if( zMode==0 ){
 -      if( p->mode==MODE_Column
 -       || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
 -      ){
 -        raw_printf
 -          (p->out,
 -           "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
 -           modeDescr[p->mode], p->cmOpts.iWrap,
 -           p->cmOpts.bWordWrap ? "on" : "off",
 -           p->cmOpts.bQuote ? "" : "no");
 +    sqlite3_finalize(pStmt);
 +#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
 +    if( zName ){
 +      appendText(&sSelect,
 +                 " UNION ALL SELECT shell_module_schema(name),"
 +                 " 'table', name, name, name, 9e+99, 'main'"
 +                 " FROM pragma_module_list",
 +                 0);
 +    }
 +#endif
 +    appendText(&sSelect, ") WHERE ", 0);
 +    if( zName ){
 +      char *zQarg = sqlite3_mprintf("%Q", zName);
 +      int bGlob;
 +      shell_check_oom(zQarg);
 +      bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0
 +        || strchr(zName, '[') != 0;
 +      if( strchr(zName, '.') ){
 +        appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
        }else{
 -        raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
 -      }
 -      zMode = modeDescr[p->mode];
 -    }
 -    n2 = strlen30(zMode);
 -    if( strncmp(zMode,"lines",n2)==0 ){
 -      p->mode = MODE_Line;
 -      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
 -    }else if( strncmp(zMode,"columns",n2)==0 ){
 -      p->mode = MODE_Column;
 -      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
 -        p->showHeader = 1;
 -      }
 -      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
 -      p->cmOpts = cmOpts;
 -    }else if( strncmp(zMode,"list",n2)==0 ){
 -      p->mode = MODE_List;
 -      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
 -      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
 -    }else if( strncmp(zMode,"html",n2)==0 ){
 -      p->mode = MODE_Html;
 -    }else if( strncmp(zMode,"tcl",n2)==0 ){
 -      p->mode = MODE_Tcl;
 -      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
 -      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
 -    }else if( strncmp(zMode,"csv",n2)==0 ){
 -      p->mode = MODE_Csv;
 -      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
 -      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
 -    }else if( strncmp(zMode,"tabs",n2)==0 ){
 -      p->mode = MODE_List;
 -      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
 -    }else if( strncmp(zMode,"insert",n2)==0 ){
 -      p->mode = MODE_Insert;
 -      set_table_name(p, zTabname ? zTabname : "table");
 -    }else if( strncmp(zMode,"quote",n2)==0 ){
 -      p->mode = MODE_Quote;
 -      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
 -      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
 -    }else if( strncmp(zMode,"ascii",n2)==0 ){
 -      p->mode = MODE_Ascii;
 -      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
 -      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
 -    }else if( strncmp(zMode,"markdown",n2)==0 ){
 -      p->mode = MODE_Markdown;
 -      p->cmOpts = cmOpts;
 -    }else if( strncmp(zMode,"table",n2)==0 ){
 -      p->mode = MODE_Table;
 -      p->cmOpts = cmOpts;
 -    }else if( strncmp(zMode,"box",n2)==0 ){
 -      p->mode = MODE_Box;
 -      p->cmOpts = cmOpts;
 -    }else if( strncmp(zMode,"count",n2)==0 ){
 -      p->mode = MODE_Count;
 -    }else if( strncmp(zMode,"off",n2)==0 ){
 -      p->mode = MODE_Off;
 -    }else if( strncmp(zMode,"json",n2)==0 ){
 -      p->mode = MODE_Json;
 -    }else{
 -      raw_printf(stderr, "Error: mode should be one of: "
 -         "ascii box column csv html insert json line list markdown "
 -         "qbox quote table tabs tcl\n");
 -      rc = 1;
 +        appendText(&sSelect, "lower(tbl_name)", 0);
 +      }
 +      appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
 +      appendText(&sSelect, zQarg, 0);
 +      if( !bGlob ){
 +        appendText(&sSelect, " ESCAPE '\\' ", 0);
 +      }
 +      appendText(&sSelect, " AND ", 0);
 +      sqlite3_free(zQarg);
      }
 -    p->cMode = p->mode;
 -  }else
 -
 -  if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
 -    if( nArg!=2 ){
 -      raw_printf(stderr, "Usage: .nonce NONCE\n");
 -      rc = 1;
 -    }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
 -      raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n",
 -                 p->lineno, azArg[1]);
 -      exit(1);
 +    if( bNoSystemTabs ){
 +      appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
 +    }
 +    appendText(&sSelect, "sql IS NOT NULL"
 +               " ORDER BY snum, rowid", 0);
 +    if( bDebug ){
 +      utf8_printf(data.out, "SQL: %s;\n", sSelect.z);
      }else{
 -      p->bSafeMode = 0;
 -      return 0;  /* Return immediately to bypass the safe mode reset
 -                 ** at the end of this procedure */
 +      rc = sqlite3_exec(datax.dbUser, sSelect.z, callback, &datax, &zErrMsg);
      }
 -  }else
 +    freeText(&sSelect);
 +  }
 +  if( zErrMsg ){
 +    *pzErr = zErrMsg;
 +    return 1;
 +  }else if( rc != SQLITE_OK ){
 +    *pzErr = shellMPrintf(0,"Error: querying schema information\n");
 +    return 1;
 +  }else{
 +    return 0;
 +  }
 +}
  
 -  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
 -    if( nArg==2 ){
 -      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
 -                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
 +/*****************
 + * The .selecttrace, .separator, .session and .sha3sum commands
 + */
 +CONDITION_COMMAND( session defined(SQLITE_ENABLE_SESSION) );
 +COLLECT_HELP_TEXT[
 +  ".separator COL ?ROW?     Change the column and row separators",
 +  ".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.",
 +  ".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",
 +];
 +DISPATCHABLE_COMMAND( selecttrace ? 1 0 ){
 +  unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
 +  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
 +  return 0;
 +}
 +DISPATCHABLE_COMMAND( separator ? 2 3 ){
 +  if( nArg>=2 ){
 +    sqlite3_snprintf(sizeof(ISS(p)->colSeparator), ISS(p)->colSeparator,
 +                     "%.*s", (int)ArraySize(ISS(p)->colSeparator)-1, azArg[1]);
 +  }
 +  if( nArg>=3 ){
 +    sqlite3_snprintf(sizeof(ISS(p)->rowSeparator), ISS(p)->rowSeparator,
 +                     "%.*s", (int)ArraySize(ISS(p)->rowSeparator)-1, azArg[2]);
 +  }
 +  return 0;
 +}
 +DISPATCHABLE_COMMAND( session 3 2 0 ){
 +  int rc = 0;
 +  struct AuxDb *pAuxDb = ISS(p)->pAuxDb;
 +  OpenSession *pSession = &pAuxDb->aSession[0];
 +  FILE *out = ISS(p)->out;
 +  char **azCmd = &azArg[1];
 +  int iSes = 0;
 +  int nCmd = nArg - 1;
 +  int i;
 +  open_db(p, 0);
 +  if( nArg>=3 ){
 +    for(iSes=0; iSes<pAuxDb->nSession; iSes++){
 +      if( strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
 +    }
 +    if( iSes<pAuxDb->nSession ){
 +      pSession = &pAuxDb->aSession[iSes];
 +      azCmd++;
 +      nCmd--;
      }else{
 -      raw_printf(stderr, "Usage: .nullvalue STRING\n");
 -      rc = 1;
 +      pSession = &pAuxDb->aSession[0];
 +      iSes = 0;
      }
 -  }else
 +  }
  
 -  if( c=='o' && 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;
 -
 -    /* Check for command-line arguments */
 -    for(iName=1; iName<nArg; iName++){
 -      const char *z = azArg[iName];
 -      if( optionMatch(z,"new") ){
 -        newFlag = 1;
 -#ifdef SQLITE_HAVE_ZLIB
 -      }else if( optionMatch(z, "zip") ){
 -        openMode = SHELL_OPEN_ZIPFILE;
 -#endif
 -      }else if( optionMatch(z, "append") ){
 -        openMode = SHELL_OPEN_APPENDVFS;
 -      }else if( optionMatch(z, "readonly") ){
 -        openMode = SHELL_OPEN_READONLY;
 -      }else if( optionMatch(z, "nofollow") ){
 -        p->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, "maxsize") && iName+1<nArg ){
 -        p->szMax = integerValue(azArg[++iName]);
 -#endif /* SQLITE_OMIT_DESERIALIZE */
 -      }else if( z[0]=='-' ){
 -        utf8_printf(stderr, "unknown option: %s\n", z);
 -        rc = 1;
 -        goto meta_command_exit;
 -      }else if( zFN ){
 -        utf8_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 = 0;
 -    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 ) shellDeleteFile(zFN);
 -      if( p->bSafeMode
 -       && p->openMode!=SHELL_OPEN_HEXDB
 -       && zFN
 -       && strcmp(zFN,":memory:")!=0
 -      ){
 -        failIfSafeMode(p, "cannot open disk-based database files in safe mode");
 -      }
 -      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 ){
 -        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
 -        sqlite3_free(zNewFilename);
 -      }else{
 -        p->pAuxDb->zFreeOnClose = zNewFilename;
 +  /* .session attach TABLE
 +  ** Invoke the sqlite3session_attach() interface to attach a particular
 +  ** table so that it is never filtered.
 +  */
 +  if( strcmp(azCmd[0],"attach")==0 ){
 +    if( nCmd!=2 ) goto session_syntax_error;
 +    if( pSession->p==0 ){
 +    session_not_open:
 +      raw_printf(STD_ERR, "ERROR: No sessions are open\n");
 +    }else{
 +      rc = sqlite3session_attach(pSession->p, azCmd[1]);
 +      if( rc ){
 +        raw_printf(STD_ERR, "ERROR: sqlite3session_attach() returns %d\n", rc);
 +        rc = 0;
        }
      }
 -    if( p->db==0 ){
 -      /* As a fall-back open a TEMP database */
 -      p->pAuxDb->zDbFilename = 0;
 -      open_db(p, 0);
 -    }
    }else
  
 -  if( (c=='o'
 -        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
 -   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
 -  ){
 -    char *zFile = 0;
 -    int bTxtMode = 0;
 -    int i;
 -    int eMode = 0;
 -    int bBOM = 0;
 -    int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
 -
 -    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
 -    if( c=='e' ){
 -      eMode = 'x';
 -      bOnce = 2;
 -    }else if( 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( strcmp(z,"-bom")==0 ){
 -          bBOM = 1;
 -        }else if( c!='e' && strcmp(z,"-x")==0 ){
 -          eMode = 'x';  /* spreadsheet */
 -        }else if( c!='e' && strcmp(z,"-e")==0 ){
 -          eMode = 'e';  /* text editor */
 -        }else{
 -          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
 -                      azArg[i]);
 -          showHelp(p->out, azArg[0]);
 -          rc = 1;
 -          goto meta_command_exit;
 -        }
 -      }else if( zFile==0 && eMode!='e' && eMode!='x' ){
 -        zFile = sqlite3_mprintf("%s", z);
 -        if( zFile && zFile[0]=='|' ){
 -          while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
 -          break;
 -        }
 -      }else{
 -        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
 -                    azArg[i]);
 -        showHelp(p->out, azArg[0]);
 -        rc = 1;
 -        sqlite3_free(zFile);
 -        goto meta_command_exit;
 -      }
 -    }
 -    if( zFile==0 ){
 -      zFile = sqlite3_mprintf("stdout");
 -    }
 -    if( bOnce ){
 -      p->outCount = 2;
 +  /* .session changeset FILE
 +  ** .session patchset FILE
 +  ** Write a changeset or patchset into a file.  The file is overwritten.
 +  */
 +  if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
 +    FILE *cs_out = 0;
 +    if( failIfSafeMode
 +        (p, "cannot run \".session %s\" in safe mode", azCmd[0]) ){
 +      rc = SHELL_FORBIDDEN_OP;
      }else{
 -      p->outCount = 0;
 -    }
 -    output_reset(p);
 -#ifndef SQLITE_NOHAVE_SYSTEM
 -    if( eMode=='e' || eMode=='x' ){
 -      p->doXdgOpen = 1;
 -      outputModePush(p);
 -      if( eMode=='x' ){
 -        /* spreadsheet mode.  Output as CSV. */
 -        newTempFile(p, "csv");
 -        ShellClearFlag(p, SHFLG_Echo);
 -        p->mode = MODE_Csv;
 -        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
 -        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
 -      }else{
 -        /* text editor mode */
 -        newTempFile(p, "txt");
 -        bTxtMode = 1;
 -      }
 -      sqlite3_free(zFile);
 -      zFile = sqlite3_mprintf("%s", p->zTempFile);
 -    }
 -#endif /* SQLITE_NOHAVE_SYSTEM */
 -    shell_check_oom(zFile);
 -    if( zFile[0]=='|' ){
 -#ifdef SQLITE_OMIT_POPEN
 -      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
 -      rc = 1;
 -      p->out = stdout;
 -#else
 -      p->out = popen(zFile + 1, "w");
 -      if( p->out==0 ){
 -        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
 -        p->out = stdout;
 +      if( nCmd!=2 ) goto session_syntax_error;
 +      if( pSession->p==0 ) goto session_not_open;
 +      cs_out = fopen(azCmd[1], "wb");
 +      if( cs_out==0 ){
 +        *pzErr = sqlite3_mprintf
 +          ("ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
          rc = 1;
        }else{
 -        if( bBOM ) fprintf(p->out,"\357\273\277");
 -        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
 -      }
 -#endif
 -    }else{
 -      p->out = output_file_open(zFile, bTxtMode);
 -      if( p->out==0 ){
 -        if( strcmp(zFile,"off")!=0 ){
 -          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
 +        int szChng;
 +        void *pChng;
 +        if( azCmd[0][0]=='c' ){
 +          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
 +        }else{
 +          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
          }
 -        p->out = stdout;
 -        rc = 1;
 -      } else {
 -        if( bBOM ) fprintf(p->out,"\357\273\277");
 -        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
 +        if( rc ){
 +          fprintf(out, "Error: error code %d\n", rc);
 +          rc = 0;
 +        }
 +        if( pChng && fwrite(pChng, szChng, 1, cs_out)!=1 ){
 +          raw_printf(STD_ERR, "ERROR: Failed to write entire %d-byte output\n",
 +                     szChng);
 +        }
 +        sqlite3_free(pChng);
 +        fclose(cs_out);
        }
      }
 -    sqlite3_free(zFile);
    }else
  
 -  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
 -    open_db(p,0);
 -    if( nArg<=1 ) goto parameter_syntax_error;
 +  /* .session close
 +  ** Close the identified session
 +  */
 +  if( 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
  
 -    /* .parameter clear
 -    ** Clear all bind parameters by dropping the TEMP table that holds them.
 -    */
 -    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
 -      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
 -                   0, 0, 0);
 -    }else
 +  /* .session enable ?BOOLEAN?
 +  ** Query or set the enable flag
 +  */
 +  if( 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);
 +      utf8_printf(out, "session %s enable flag = %d\n",
 +                  pSession->zName, ii);
 +    }
 +  }else
  
 -    /* .parameter list
 -    ** List all bind parameters.
 -    */
 -    if( nArg==2 && 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;
 +  /* .session filter GLOB ....
 +  ** Set a list of GLOB patterns of table names to be excluded.
 +  */
 +  if( strcmp(azCmd[0], "filter")==0 ){
 +    int ii, nByte;
 +    if( nCmd<2 ) goto session_syntax_error;
 +    if( pAuxDb->nSession ){
 +      for(ii=0; ii<pSession->nFilter; ii++){
 +        sqlite3_free(pSession->azFilter[ii]);
        }
 -      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 ){
 -          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
 -                      sqlite3_column_text(pStmt,1));
 -        }
 -        sqlite3_finalize(pStmt);
 +      sqlite3_free(pSession->azFilter);
 +      nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
 +      pSession->azFilter = sqlite3_malloc( nByte );
 +      if( pSession->azFilter==0 ){
 +        shell_out_of_memory();
        }
 -    }else
 -
 -    /* .parameter init
 -    ** Make sure the TEMP table used to hold bind parameters exists.
 -    ** Create it if necessary.
 -    */
 -    if( nArg==2 && 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 && 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 ){
 -          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
 -          sqlite3_finalize(pStmt);
 -          pStmt = 0;
 -          rc = 1;
 -        }
 +      for(ii=1; ii<nCmd; ii++){
 +        pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
 +        shell_check_oom(pSession->azFilter[ii-1]);
        }
 -      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 && 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");
 +      pSession->nFilter = ii-1;
 +    }
    }else
  
 -  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
 -    int i;
 -    for(i=1; i<nArg; i++){
 -      if( i>1 ) raw_printf(p->out, " ");
 -      utf8_printf(p->out, "%s", azArg[i]);
 +  /* .session indirect ?BOOLEAN?
 +  ** Query or set the indirect flag
 +  */
 +  if( 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);
 +      utf8_printf(out, "session %s indirect flag = %d\n",
 +                  pSession->zName, ii);
      }
 -    raw_printf(p->out, "\n");
    }else
  
 -#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 -  if( c=='p' && n>=3 && 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( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
 -          p->flgProgress |= SHELL_PROGRESS_QUIET;
 -          continue;
 -        }
 -        if( strcmp(z,"reset")==0 ){
 -          p->flgProgress |= SHELL_PROGRESS_RESET;
 -          continue;
 -        }
 -        if( strcmp(z,"once")==0 ){
 -          p->flgProgress |= SHELL_PROGRESS_ONCE;
 -          continue;
 -        }
 -        if( strcmp(z,"limit")==0 ){
 -          if( i+1>=nArg ){
 -            utf8_printf(stderr, "Error: missing argument on --limit\n");
 -            rc = 1;
 -            goto meta_command_exit;
 -          }else{
 -            p->mxProgress = (int)integerValue(azArg[++i]);
 -          }
 -          continue;
 -        }
 -        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
 -        rc = 1;
 -        goto meta_command_exit;
 -      }else{
 -        nn = (int)integerValue(z);
 -      }
 +  /* .session isempty
 +  ** Determine if the session is empty
 +  */
 +  if( strcmp(azCmd[0], "isempty")==0 ){
 +    int ii;
 +    if( nCmd!=1 ) goto session_syntax_error;
 +    if( pAuxDb->nSession ){
 +      ii = sqlite3session_isempty(pSession->p);
 +      utf8_printf(out, "session %s isempty flag = %d\n",
 +                  pSession->zName, ii);
      }
 -    open_db(p, 0);
 -    sqlite3_progress_handler(p->db, nn, progress_handler, p);
    }else
 -#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
  
 -  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
 -    if( nArg >= 2) {
 -      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
 -    }
 -    if( nArg >= 3) {
 -      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
 +  /* .session list
 +  ** List all currently open sessions
 +  */
 +  if( strcmp(azCmd[0],"list")==0 ){
 +    for(i=0; i<pAuxDb->nSession; i++){
 +      utf8_printf(out, "%d %s\n", i, pAuxDb->aSession[i].zName);
      }
    }else
  
index fc3811b8a4b9d098102a4b961dae33f47e2ffb08,d63a0a20d3edd86086b4dc5eebd450757fa1532c..db0ead997f592f5bf02b8ea22f8edb961119e640
@@@ -476,6 -476,20 +476,20 @@@ CREATE TABLE t8(a, b, c)
    db eval { SELECT * FROM t8 }
  } {1 2 3}
  
 -SELECT * FROM temp.t8
+ do_test shell5-4.4 {
+   forcedelete shell5.csv
+   set fd [open shell5.csv w]
+   puts $fd "1,2,3"
+   close $fd
+   catchcmd test.db [string trim {
+ .mode csv
+ CREATE TEMP TABLE t8(a, b, c);
+ .import shell5.csv t8
+ .nullvalue #
++SELECT * FROM temp.t8;
+   }]
+ } {0 1,2,3}
  #----------------------------------------------------------------------------
  # Tests for the shell automatic column rename.
  #