aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2019-05-13 15:20:24 +0300
committerSergey Poznyakoff <gray@gnu.org>2019-05-13 15:20:24 +0300
commit65f41a742e025487f8ec7f2e7ca2a3af3283fc96 (patch)
treecef728d35f7683f7721659a58fdb2aa1f61387bc
parent07930c5ee74b8d2645fc55465833ab84e2f4444a (diff)
downloadgrecs-65f41a742e025487f8ec7f2e7ca2a3af3283fc96.tar.gz
grecs-65f41a742e025487f8ec7f2e7ca2a3af3283fc96.tar.bz2
wordsplit: optionally disable splitting of unexpandable variable and command refs
* include/wordsplit.h (WRDSO_NOVARSPLIT) (WRDSO_NOCMDSPLIT): New options. * src/wordsplit.c (scan_word): Treat any variable reference, even containing whitespace, as a single word if WRDSO_NOVARSPLIT is set. Ditto for commands and WRDSO_NOCMDSPLIT. * tests/wordsplit.at: Add new tests. * tests/wsp.c: Recognize novarsplit and nocmdsplit options. For future use: recognize bskeep_words, bskeep_quote, bskeep.
-rw-r--r--include/wordsplit.h11
-rw-r--r--src/wordsplit.c6
-rw-r--r--tests/wordsplit.at18
-rw-r--r--tests/wsp.c5
4 files changed, 35 insertions, 5 deletions
diff --git a/include/wordsplit.h b/include/wordsplit.h
index 3a7ab25..a175275 100644
--- a/include/wordsplit.h
+++ b/include/wordsplit.h
@@ -203,5 +203,3 @@ struct wordsplit
#define WRDSO_DOTGLOB 0x00000004
-#if 0 /* Unused value */
-#define WRDSO_ARGV 0x00000008
-#endif
+/* Unused value: 0x00000008 */
/* Keep backslash in unrecognized escape sequences in words */
@@ -222,2 +220,9 @@ struct wordsplit
#define WRDSO_XESC_QUOTE 0x00000400
+/* Unused: 0x00000800 */
+/* Don't split variable references, even if they contain whitespace
+ (e.g. ${VAR:-foo bar}) */
+#define WRDSO_NOVARSPLIT 0x00001000
+/* Don't split commands, even containing whitespace, e.g.
+ $(echo foo bar) */
+#define WRDSO_NOCMDSPLIT 0x00002000
diff --git a/src/wordsplit.c b/src/wordsplit.c
index e979f27..521a1eb 100644
--- a/src/wordsplit.c
+++ b/src/wordsplit.c
@@ -2060,3 +2060,4 @@ scan_word (struct wordsplit *wsp, size_t start, int consume_all)
{
- if (!(wsp->ws_flags & WRDSF_NOVAR)
+ if ((!(wsp->ws_flags & WRDSF_NOVAR)
+ || (wsp->ws_options & WRDSO_NOVARSPLIT))
&& command[i+1] == '{'
@@ -2064,3 +2065,4 @@ scan_word (struct wordsplit *wsp, size_t start, int consume_all)
continue;
- if (!(wsp->ws_flags & WRDSF_NOCMD)
+ if ((!(wsp->ws_flags & WRDSF_NOCMD)
+ || (wsp->ws_options & WRDSO_NOCMDSPLIT))
&& command[i+1] == '('
diff --git a/tests/wordsplit.at b/tests/wordsplit.at
index e3af703..1f2e80d 100644
--- a/tests/wordsplit.at
+++ b/tests/wordsplit.at
@@ -952,2 +952,20 @@ TOTAL: 3
+TESTWSP([variable nosplit],[],[novar novarsplit],
+[begin ${VAR:- a b} end],
+[NF: 3
+0: begin
+1: "${VAR:- a b}"
+2: end
+TOTAL: 3
+])
+
+TESTWSP([command nosplit],[],[nocmd nocmdsplit],
+[begin $(words a b) end],
+[NF: 3
+0: begin
+1: "$(words a b)"
+2: end
+TOTAL: 3
+])
+
m4_popdef([TESTWSP])
diff --git a/tests/wsp.c b/tests/wsp.c
index cca3a36..bd13e63 100644
--- a/tests/wsp.c
+++ b/tests/wsp.c
@@ -66,2 +66,7 @@ struct kwd opt_keytab[] = {
{ "dotglob", WRDSO_DOTGLOB },
+ { "bskeep_words", WRDSO_BSKEEP_WORD },
+ { "bskeep_quote", WRDSO_BSKEEP_QUOTE },
+ { "bskeep", WRDSO_BSKEEP_WORD|WRDSO_BSKEEP_QUOTE },
+ { "novarsplit", WRDSO_NOVARSPLIT },
+ { "nocmdsplit", WRDSO_NOCMDSPLIT },
{ NULL, 0 }

Return to:

Send suggestions and report system problems to the System administrator.