1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-06 15:49:35 +03:00

Update the implementation of ".testctrl" in the command-line shell to use

a look-up table rather than a long sequence of if-elses.  Shorten source
code lines of shell.c to 80 characters or less.

FossilOrigin-Name: 54bacb95dd6e2d6ac4971391a40484ccb9126d29
This commit is contained in:
drh
2011-03-17 16:45:50 +00:00
parent ac8eb11375
commit d416fe7995
3 changed files with 61 additions and 36 deletions

View File

@@ -1,8 +1,8 @@
-----BEGIN PGP SIGNED MESSAGE----- -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 Hash: SHA1
C Comment\senhancement\sto\sbetter\sexplain\sthe\slogic\sin\sthe\s"x\sIS\sNULL"\s\noptimization. C Update\sthe\simplementation\sof\s".testctrl"\sin\sthe\scommand-line\sshell\sto\suse\na\slook-up\stable\srather\sthan\sa\slong\ssequence\sof\sif-elses.\s\sShorten\ssource\ncode\slines\sof\sshell.c\sto\s80\scharacters\sor\sless.
D 2011-03-17T01:58:21.662 D 2011-03-17T16:45:50.769
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in 27701a1653595a1f2187dc61c8117e00a6c1d50f F Makefile.in 27701a1653595a1f2187dc61c8117e00a6c1d50f
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -180,7 +180,7 @@ F src/random.c cd4a67b3953b88019f8cd4ccd81394a8ddfaba50
F src/resolve.c 1c0f32b64f8e3f555fe1f732f9d6f501a7f05706 F src/resolve.c 1c0f32b64f8e3f555fe1f732f9d6f501a7f05706
F src/rowset.c 69afa95a97c524ba6faf3805e717b5b7ae85a697 F src/rowset.c 69afa95a97c524ba6faf3805e717b5b7ae85a697
F src/select.c d24406c45dd2442eb2eeaac413439066b149c944 F src/select.c d24406c45dd2442eb2eeaac413439066b149c944
F src/shell.c 75ec746b9661e9b5cf956ef4cd9833c0545ef037 F src/shell.c 4a5e0ad845475c84881f0b25b1abba2ddaab0a72
F src/sqlite.h.in 369c767e6b9f101d63d8e4c5e40279f975ccec08 F src/sqlite.h.in 369c767e6b9f101d63d8e4c5e40279f975ccec08
F src/sqlite3ext.h c90bd5507099f62043832d73f6425d8d5c5da754 F src/sqlite3ext.h c90bd5507099f62043832d73f6425d8d5c5da754
F src/sqliteInt.h 2cea3e47997e3f4d9b4f1ce62f99c35be1b5a586 F src/sqliteInt.h 2cea3e47997e3f4d9b4f1ce62f99c35be1b5a586
@@ -917,14 +917,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
P 2353176811f752a16c1f2351a3d3431919b062a9 P 869f894798a65f8bc0e0b083866a784fa0189f68
R ceaf122e76347e0b045461abe9cbfd0f R a8def7fc552253bddf0337de8f57667b
U drh U drh
Z 6e216a7f32c457388fcc438c6cd929ce Z f32baca0676f04528da8db7e35d99aa0
-----BEGIN PGP SIGNATURE----- -----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux) Version: GnuPG v1.4.6 (GNU/Linux)
iD8DBQFNgWrBoxKgR168RlERAg4rAJ0ZbgbEbrWUz8iIL7EI7v8AcMLRpQCfZka3 iD8DBQFNgjx3oxKgR168RlERAkZlAJ9g0EAzYOwFcF/NGcxGCGlF65eaGACfcZkt
v7cQP/zk4VcNw9IakEHnWtw= bA9BP3Fet3XmbiRxAgQtxR0=
=fknb =Yzeg
-----END PGP SIGNATURE----- -----END PGP SIGNATURE-----

View File

@@ -1 +1 @@
869f894798a65f8bc0e0b083866a784fa0189f68 54bacb95dd6e2d6ac4971391a40484ccb9126d29

View File

@@ -2173,29 +2173,45 @@ static int do_meta_command(char *zLine, struct callback_data *p){
}else }else
if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){ if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
static const struct {
const char *zCtrlName; /* Name of a test-control option */
int ctrlCode; /* Integer code for that option */
} aCtrl[] = {
{ "prng_save", SQLITE_TESTCTRL_PRNG_SAVE },
{ "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE },
{ "prng_reset", SQLITE_TESTCTRL_PRNG_RESET },
{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST },
{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL },
{ "benign_malloc_hooks", SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS },
{ "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE },
{ "assert", SQLITE_TESTCTRL_ASSERT },
{ "always", SQLITE_TESTCTRL_ALWAYS },
{ "reserve", SQLITE_TESTCTRL_RESERVE },
{ "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS },
{ "iskeyword", SQLITE_TESTCTRL_ISKEYWORD },
{ "pghdrsz", SQLITE_TESTCTRL_PGHDRSZ },
{ "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC },
};
int testctrl = -1; int testctrl = -1;
int rc = 0; int rc = 0;
int i, n;
open_db(p); open_db(p);
/* convert testctrl text option to value. allow only the first /* convert testctrl text option to value. allow any unique prefix
** three characters of the option to be used or the numerical ** of the option name, or a numerical value. */
** value. */ n = strlen(azArg[1]);
if( strncmp( azArg[1], "prng_save", 6 )==0 ) testctrl = SQLITE_TESTCTRL_PRNG_SAVE; for(i=0; i<sizeof(aCtrl)/sizeof(aCtrl[0]); i++){
else if( strncmp( azArg[1], "prng_restore", 10 )==0 ) testctrl = SQLITE_TESTCTRL_PRNG_RESTORE; if( strncmp(azArg[1], aCtrl[i].zCtrlName, n)==0 ){
else if( strncmp( azArg[1], "prng_reset", 10 )==0 ) testctrl = SQLITE_TESTCTRL_PRNG_RESET; if( testctrl<0 ){
else if( strncmp( azArg[1], "bitvec_test", 6 )==3 ) testctrl = SQLITE_TESTCTRL_BITVEC_TEST; testctrl = aCtrl[i].ctrlCode;
else if( strncmp( azArg[1], "fault_install", 6 )==3 ) testctrl = SQLITE_TESTCTRL_FAULT_INSTALL; }else{
else if( strncmp( azArg[1], "benign_malloc_hooks", 3 )==0 ) testctrl = SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS; fprintf(stderr, "ambiguous option name: \"%s\"\n", azArg[i]);
else if( strncmp( azArg[1], "pending_byte", 3 )==0 ) testctrl = SQLITE_TESTCTRL_PENDING_BYTE; testctrl = -1;
else if( strncmp( azArg[1], "assert", 3 )==0 ) testctrl = SQLITE_TESTCTRL_ASSERT; break;
else if( strncmp( azArg[1], "always", 3 )==0 ) testctrl = SQLITE_TESTCTRL_ALWAYS; }
else if( strncmp( azArg[1], "reserve", 3 )==0 ) testctrl = SQLITE_TESTCTRL_RESERVE; }
else if( strncmp( azArg[1], "optimizations", 3 )==0 ) testctrl = SQLITE_TESTCTRL_OPTIMIZATIONS; }
else if( strncmp( azArg[1], "iskeyword", 3 )==0 ) testctrl = SQLITE_TESTCTRL_ISKEYWORD; if( testctrl<0 ) testctrl = atoi(azArg[1]);
else if( strncmp( azArg[1], "pghdrsz", 3 )==0 ) testctrl = SQLITE_TESTCTRL_PGHDRSZ;
else if( strncmp( azArg[1], "scratchmalloc", 3 )==0 ) testctrl = SQLITE_TESTCTRL_SCRATCHMALLOC;
else testctrl = atoi(azArg[1]);
if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){ if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){
fprintf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]); fprintf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]);
}else{ }else{
@@ -2209,7 +2225,8 @@ static int do_meta_command(char *zLine, struct callback_data *p){
rc = sqlite3_test_control(testctrl, p->db, opt); rc = sqlite3_test_control(testctrl, p->db, opt);
printf("%d (0x%08x)\n", rc, rc); printf("%d (0x%08x)\n", rc, rc);
} else { } else {
fprintf(stderr,"Error: testctrl %s takes a single int option\n", azArg[1]); fprintf(stderr,"Error: testctrl %s takes a single int option\n",
azArg[1]);
} }
break; break;
@@ -2233,7 +2250,8 @@ static int do_meta_command(char *zLine, struct callback_data *p){
rc = sqlite3_test_control(testctrl, opt); rc = sqlite3_test_control(testctrl, opt);
printf("%d (0x%08x)\n", rc, rc); printf("%d (0x%08x)\n", rc, rc);
} else { } else {
fprintf(stderr,"Error: testctrl %s takes a single unsigned int option\n", azArg[1]); fprintf(stderr,"Error: testctrl %s takes a single unsigned"
" int option\n", azArg[1]);
} }
break; break;
@@ -2245,7 +2263,8 @@ static int do_meta_command(char *zLine, struct callback_data *p){
rc = sqlite3_test_control(testctrl, opt); rc = sqlite3_test_control(testctrl, opt);
printf("%d (0x%08x)\n", rc, rc); printf("%d (0x%08x)\n", rc, rc);
} else { } else {
fprintf(stderr,"Error: testctrl %s takes a single int option\n", azArg[1]); fprintf(stderr,"Error: testctrl %s takes a single int option\n",
azArg[1]);
} }
break; break;
@@ -2257,7 +2276,8 @@ static int do_meta_command(char *zLine, struct callback_data *p){
rc = sqlite3_test_control(testctrl, opt); rc = sqlite3_test_control(testctrl, opt);
printf("%d (0x%08x)\n", rc, rc); printf("%d (0x%08x)\n", rc, rc);
} else { } else {
fprintf(stderr,"Error: testctrl %s takes a single char * option\n", azArg[1]); fprintf(stderr,"Error: testctrl %s takes a single char * option\n",
azArg[1]);
} }
break; break;
#endif #endif
@@ -2267,7 +2287,8 @@ static int do_meta_command(char *zLine, struct callback_data *p){
case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS:
case SQLITE_TESTCTRL_SCRATCHMALLOC: case SQLITE_TESTCTRL_SCRATCHMALLOC:
default: default:
fprintf(stderr,"Error: CLI support for testctrl %s not implemented\n", azArg[1]); fprintf(stderr,"Error: CLI support for testctrl %s not implemented\n",
azArg[1]);
break; break;
} }
} }
@@ -2278,7 +2299,9 @@ static int do_meta_command(char *zLine, struct callback_data *p){
sqlite3_busy_timeout(p->db, atoi(azArg[1])); sqlite3_busy_timeout(p->db, atoi(azArg[1]));
}else }else
if( HAS_TIMER && c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 && nArg==2 ){ if( HAS_TIMER && c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0
&& nArg==2
){
enableTimer = booleanValue(azArg[1]); enableTimer = booleanValue(azArg[1]);
}else }else
@@ -2465,7 +2488,9 @@ static int process_input(struct callback_data *p, FILE *in){
} }
} }
if( zSql ){ if( zSql ){
if( !_all_whitespace(zSql) ) fprintf(stderr, "Error: incomplete SQL: %s\n", zSql); if( !_all_whitespace(zSql) ){
fprintf(stderr, "Error: incomplete SQL: %s\n", zSql);
}
free(zSql); free(zSql);
} }
free(zLine); free(zLine);