aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlarrybr <larrybr@noemail.net>2022-12-06 05:31:20 +0000
committerlarrybr <larrybr@noemail.net>2022-12-06 05:31:20 +0000
commitf9d40801c33f65e463ab2110c737e7fc1942dddc (patch)
tree3b6840296391e53e8435ec05b3a4c978e89298d0
parentd2db31c971cf0abb4dbe0ab51bfe0e8dbee232f2 (diff)
parent9ab1598e52d70a6b7ae823804144af33a80bbbc0 (diff)
downloadsqlite-f9d40801c33f65e463ab2110c737e7fc1942dddc.tar.gz
sqlite-f9d40801c33f65e463ab2110c737e7fc1942dddc.zip
Add optional feature: A CLI continuation prompt which reflects open lexemes and parens, similarly to PG shell.
FossilOrigin-Name: f41f18b1c3c950565ee3c237aebb51cfc3241813c6425813a8217e07aa0153a6
-rw-r--r--manifest14
-rw-r--r--manifest.uuid2
-rw-r--r--src/shell.c.in112
3 files changed, 114 insertions, 14 deletions
diff --git a/manifest b/manifest
index 9b85c8788..eb01d8ca7 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Enhance\sthe\ssqlite3_stmt_scanstatus()\sAPI\sand\sadd\ssqlite3_stmt_scanstatus_v2().\sFor\screation\sof\senhanced\squery\sperformance\sreports.
-D 2022-12-05T19:16:23.509
+C Add\soptional\sfeature:\sA\sCLI\scontinuation\sprompt\swhich\sreflects\sopen\slexemes\sand\sparens,\ssimilarly\sto\sPG\sshell.
+D 2022-12-06T05:31:20.264
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -646,7 +646,7 @@ F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c
F src/resolve.c efea4e5fbecfd6d0a9071b0be0d952620991673391b6ffaaf4c277b0bb674633
F src/rowset.c ba9515a922af32abe1f7d39406b9d35730ed65efab9443dc5702693b60854c92
F src/select.c 321f29e431fbb71e594cc7026391a827a0270237597d2e2401244e7602dea8bd
-F src/shell.c.in e7fc8db64df14d1893a86dce488770efdbb1007e44ec8cd5549769533bb419f2
+F src/shell.c.in f4ef4c6bca35bcab91041c2e10ac9b53440c37d5751563713cc7feaa8c1ac880
F src/sqlite.h.in 1fe1836879ecbb2e28f00f44eb6092db09a2a06bf072af351c6c2466bd515496
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
F src/sqlite3ext.h c4b9fa7a7e2bcdf850cfeb4b8a91d5ec47b7a00033bc996fd2ee96cbf2741f5f
@@ -2067,8 +2067,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P dc7dd2d3e50e7cc474b22f1b5b219da32bcd7aa1ba56864d1dbcf0d3a6fa06f2 009462f2344b1f468cf9440343a47fec68d783a2bfb4fa6168bb227ec910b918
-R 6f58eb8b4613cbdc0da9cabf0de9170c
-U dan
-Z dd8e28132ec997579f72120c72f627ae
+P 4893b4e3eafc7c9c22b24717f90a585862203f987cf108b079ce6e946093e675 dac2ddc287db7a68d0cd49b785060f62290868fbb1aa2ee09e54d3b1acfbf55f
+R 4e6f769112d568da8b23927a4847535b
+U larrybr
+Z 19f7409ace4582d7233d6e9f94f36495
# Remove this line to create a well-formed Fossil manifest.
diff --git a/manifest.uuid b/manifest.uuid
index e55b14b4b..02dae66a8 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-4893b4e3eafc7c9c22b24717f90a585862203f987cf108b079ce6e946093e675 \ No newline at end of file
+f41f18b1c3c950565ee3c237aebb51cfc3241813c6425813a8217e07aa0153a6 \ No newline at end of file
diff --git a/src/shell.c.in b/src/shell.c.in
index 7fc777f05..78e681c02 100644
--- a/src/shell.c.in
+++ b/src/shell.c.in
@@ -467,8 +467,93 @@ static char *Argv0;
** Prompt strings. Initialized in main. Settable with
** .prompt main continue
*/
-static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
-static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
+#define PROMPT_LEN_MAX 20
+/* First line prompt. default: "sqlite> " */
+static char mainPrompt[PROMPT_LEN_MAX];
+/* Continuation prompt. default: " ...> " */
+static char continuePrompt[PROMPT_LEN_MAX];
+
+/*
+** Optionally disable dynamic continuation prompt.
+** Unless disabled, the continuation prompt shows open SQL lexemes if any,
+** or open parentheses level if non-zero, or continuation prompt as set.
+** This facility interacts with the scanner and process_input() where the
+** below 5 macros are used.
+*/
+#ifdef SQLITE_OMIT_DYNAPROMPT
+# define CONTINUATION_PROMPT continuePrompt
+# define CONTINUE_PROMPT_RESET(p)
+# define CONTINUE_PROMPT_AWAITS(p,s)
+# define CONTINUE_PROMPT_AWAITC(p,c)
+# define CONTINUE_PAREN_INCR(p,n)
+# define CONTINUE_PROMPT_STATE 0
+# define SCAN_TRACKER_REFTYPE void*
+#else
+# define CONTINUATION_PROMPT dynamicContinuePrompt()
+# define CONTINUE_PROMPT_RESET(p) \
+ if(p) (setLexemeOpen(p,0,0), trackParenLevel(p,0))
+# define CONTINUE_PROMPT_AWAITS(p,s) \
+ if(p && stdin_is_interactive) setLexemeOpen(p, s, 0)
+# define CONTINUE_PROMPT_AWAITC(p,c) \
+ if(p && stdin_is_interactive) setLexemeOpen(p, 0, c)
+# define CONTINUE_PAREN_INCR(p,n) \
+ if(p && stdin_is_interactive) (trackParenLevel(p,n))
+# define SCAN_TRACKER_REFTYPE struct DynaPrompt *
+# define CONTINUE_PROMPT_STATE ((SCAN_TRACKER_REFTYPE)&dynPrompt)
+
+static struct DynaPrompt {
+ char dynamicPrompt[PROMPT_LEN_MAX];
+ char acAwait[2];
+ int inParenLevel;
+ char *zScannerAwaits;
+} dynPrompt = { {0}, {0}, 0, 0 };
+
+/* Record parenthesis nesting level change, or force level to 0. */
+static void trackParenLevel(struct DynaPrompt *p, int ni){
+ p->inParenLevel += ni;
+ if( ni==0 ) p->inParenLevel = 0;
+ p->zScannerAwaits = 0;
+}
+
+/* Record that a lexeme is opened, or closed with args==0. */
+static void setLexemeOpen(struct DynaPrompt *p, char *s, char c){
+ if( s!=0 || c==0 ){
+ p->zScannerAwaits = s;
+ p->acAwait[0] = 0;
+ }else{
+ p->acAwait[0] = c;
+ p->zScannerAwaits = p->acAwait;
+ }
+}
+
+/* Upon demand, derive the continuation prompt to display. */
+static char *dynamicContinuePrompt(void){
+ if( continuePrompt[0]==0
+ || (dynPrompt.zScannerAwaits==0 && dynPrompt.inParenLevel == 0) ){
+ return continuePrompt;
+ }else{
+ if( dynPrompt.zScannerAwaits ){
+ int ncp = strlen(continuePrompt), ndp = strlen(dynPrompt.zScannerAwaits);
+ if( ndp > ncp-3 ) return continuePrompt;
+ strncpy(dynPrompt.dynamicPrompt, dynPrompt.zScannerAwaits, ndp);
+ while( ndp<3 ) dynPrompt.dynamicPrompt[ndp++] = ' ';
+ strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3,
+ PROMPT_LEN_MAX-4);
+ }else{
+ if( dynPrompt.inParenLevel>9 ){
+ strncpy(dynPrompt.dynamicPrompt, "(..", 3);
+ }else if( dynPrompt.inParenLevel<0 ){
+ strncpy(dynPrompt.dynamicPrompt, ")x!", 3);
+ }else{
+ strncpy(dynPrompt.dynamicPrompt, "(x.", 3);
+ dynPrompt.dynamicPrompt[2] = (char)('0'+dynPrompt.inParenLevel);
+ }
+ strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3, PROMPT_LEN_MAX-4);
+ }
+ }
+ return dynPrompt.dynamicPrompt;
+}
+#endif /* !defined(SQLITE_OMIT_DYNAPROMPT) */
/*
** Render output like fprintf(). Except, if the output is going to the
@@ -729,7 +814,7 @@ static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
if( in!=0 ){
zResult = local_getline(zPrior, in);
}else{
- zPrompt = isContinuation ? continuePrompt : mainPrompt;
+ zPrompt = isContinuation ? CONTINUATION_PROMPT : mainPrompt;
#if SHELL_USE_LOCAL_GETLINE
printf("%s", zPrompt);
fflush(stdout);
@@ -10968,7 +11053,8 @@ typedef enum {
** The scan is resumable for subsequent lines when prior
** return values are passed as the 2nd argument.
*/
-static QuickScanState quickscan(char *zLine, QuickScanState qss){
+static QuickScanState quickscan(char *zLine, QuickScanState qss,
+ SCAN_TRACKER_REFTYPE pst){
char cin;
char cWait = (char)qss; /* intentional narrowing loss */
if( cWait==0 ){
@@ -10992,6 +11078,7 @@ static QuickScanState quickscan(char *zLine, QuickScanState qss){
if( *zLine=='*' ){
++zLine;
cWait = '*';
+ CONTINUE_PROMPT_AWAITS(pst, "/*");
qss = QSS_SETV(qss, cWait);
goto TermScan;
}
@@ -11002,7 +11089,14 @@ static QuickScanState quickscan(char *zLine, QuickScanState qss){
case '`': case '\'': case '"':
cWait = cin;
qss = QSS_HasDark | cWait;
+ CONTINUE_PROMPT_AWAITC(pst, cin);
goto TermScan;
+ case '(':
+ CONTINUE_PAREN_INCR(pst, 1);
+ break;
+ case ')':
+ CONTINUE_PAREN_INCR(pst, -1);
+ break;
default:
break;
}
@@ -11018,6 +11112,7 @@ static QuickScanState quickscan(char *zLine, QuickScanState qss){
continue;
++zLine;
cWait = 0;
+ CONTINUE_PROMPT_AWAITC(pst, 0);
qss = QSS_SETV(qss, 0);
goto PlainScan;
case '`': case '\'': case '"':
@@ -11028,6 +11123,7 @@ static QuickScanState quickscan(char *zLine, QuickScanState qss){
/* fall thru */
case ']':
cWait = 0;
+ CONTINUE_PROMPT_AWAITC(pst, 0);
qss = QSS_SETV(qss, 0);
goto PlainScan;
default: assert(0);
@@ -11051,7 +11147,7 @@ static int line_is_command_terminator(char *zLine){
zLine += 2; /* SQL Server */
else
return 0;
- return quickscan(zLine, QSS_Start)==QSS_Start;
+ return quickscan(zLine, QSS_Start, 0)==QSS_Start;
}
/*
@@ -11191,6 +11287,7 @@ static int process_input(ShellState *p){
}
++p->inputNesting;
p->lineno = 0;
+ CONTINUE_PROMPT_RESET(CONTINUE_PROMPT_STATE);
while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
fflush(p->out);
zLine = one_input_line(p->in, zLine, nSql>0);
@@ -11209,7 +11306,7 @@ static int process_input(ShellState *p){
&& line_is_complete(zSql, nSql) ){
memcpy(zLine,";",2);
}
- qss = quickscan(zLine, qss);
+ qss = quickscan(zLine, qss, CONTINUE_PROMPT_STATE);
if( QSS_PLAINWHITE(qss) && nSql==0 ){
/* Just swallow single-line whitespace */
echo_group_input(p, zLine);
@@ -11217,6 +11314,7 @@ static int process_input(ShellState *p){
continue;
}
if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
+ CONTINUE_PROMPT_RESET(CONTINUE_PROMPT_STATE);
echo_group_input(p, zLine);
if( zLine[0]=='.' ){
rc = do_meta_command(zLine, p);
@@ -11252,6 +11350,7 @@ static int process_input(ShellState *p){
if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
echo_group_input(p, zSql);
errCnt += runOneSqlLine(p, zSql, p->in, startline);
+ CONTINUE_PROMPT_RESET(CONTINUE_PROMPT_STATE);
nSql = 0;
if( p->outCount ){
output_reset(p);
@@ -11271,6 +11370,7 @@ static int process_input(ShellState *p){
/* This may be incomplete. Let the SQL parser deal with that. */
echo_group_input(p, zSql);
errCnt += runOneSqlLine(p, zSql, p->in, startline);
+ CONTINUE_PROMPT_RESET(CONTINUE_PROMPT_STATE);
}
free(zSql);
free(zLine);