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
@@ -201,9 +201,7 @@ struct wordsplit
201#define WRDSO_FAILGLOB 0x00000002 201#define WRDSO_FAILGLOB 0x00000002
202/* Allow a leading period to be matched by metacharacters. */ 202/* Allow a leading period to be matched by metacharacters. */
203#define WRDSO_DOTGLOB 0x00000004 203#define WRDSO_DOTGLOB 0x00000004
204#if 0 /* Unused value */ 204/* Unused value: 0x00000008 */
205#define WRDSO_ARGV 0x00000008
206#endif
207/* Keep backslash in unrecognized escape sequences in words */ 205/* Keep backslash in unrecognized escape sequences in words */
208#define WRDSO_BSKEEP_WORD 0x00000010 206#define WRDSO_BSKEEP_WORD 0x00000010
209/* Handle octal escapes in words */ 207/* Handle octal escapes in words */
@@ -220,6 +218,13 @@ struct wordsplit
220#define WRDSO_OESC_QUOTE 0x00000200 218#define WRDSO_OESC_QUOTE 0x00000200
221/* Handle hex escapes in quoted strings */ 219/* Handle hex escapes in quoted strings */
222#define WRDSO_XESC_QUOTE 0x00000400 220#define WRDSO_XESC_QUOTE 0x00000400
221/* Unused: 0x00000800 */
222/* Don't split variable references, even if they contain whitespace
223 (e.g. ${VAR:-foo bar}) */
224#define WRDSO_NOVARSPLIT 0x00001000
225/* Don't split commands, even containing whitespace, e.g.
226 $(echo foo bar) */
227#define WRDSO_NOCMDSPLIT 0x00002000
223 228
224#define WRDSO_BSKEEP WRDSO_BSKEEP_WORD 229#define WRDSO_BSKEEP WRDSO_BSKEEP_WORD
225#define WRDSO_OESC WRDSO_OESC_WORD 230#define WRDSO_OESC WRDSO_OESC_WORD
diff --git a/src/wordsplit.c b/src/wordsplit.c
index e979f27..521a1eb 100644
--- a/src/wordsplit.c
+++ b/src/wordsplit.c
@@ -2058,11 +2058,13 @@ scan_word (struct wordsplit *wsp, size_t start, int consume_all)
2058 2058
2059 if (command[i] == '$') 2059 if (command[i] == '$')
2060 { 2060 {
2061 if (!(wsp->ws_flags & WRDSF_NOVAR) 2061 if ((!(wsp->ws_flags & WRDSF_NOVAR)
2062 || (wsp->ws_options & WRDSO_NOVARSPLIT))
2062 && command[i+1] == '{' 2063 && command[i+1] == '{'
2063 && find_closing_paren (command, i + 2, len, &i, "{}") == 0) 2064 && find_closing_paren (command, i + 2, len, &i, "{}") == 0)
2064 continue; 2065 continue;
2065 if (!(wsp->ws_flags & WRDSF_NOCMD) 2066 if ((!(wsp->ws_flags & WRDSF_NOCMD)
2067 || (wsp->ws_options & WRDSO_NOCMDSPLIT))
2066 && command[i+1] == '(' 2068 && command[i+1] == '('
2067 && find_closing_paren (command, i + 2, len, &i, "()") == 0) 2069 && find_closing_paren (command, i + 2, len, &i, "()") == 0)
2068 continue; 2070 continue;
diff --git a/tests/wordsplit.at b/tests/wordsplit.at
index e3af703..1f2e80d 100644
--- a/tests/wordsplit.at
+++ b/tests/wordsplit.at
@@ -950,6 +950,24 @@ TOTAL: 3
950[input exhausted 950[input exhausted
951])) 951]))
952 952
953TESTWSP([variable nosplit],[],[novar novarsplit],
954[begin ${VAR:- a b} end],
955[NF: 3
9560: begin
9571: "${VAR:- a b}"
9582: end
959TOTAL: 3
960])
961
962TESTWSP([command nosplit],[],[nocmd nocmdsplit],
963[begin $(words a b) end],
964[NF: 3
9650: begin
9661: "$(words a b)"
9672: end
968TOTAL: 3
969])
970
953m4_popdef([TESTWSP]) 971m4_popdef([TESTWSP])
954m4_popdef([wspnum]) 972m4_popdef([wspnum])
955m4_popdef([wspid]) 973m4_popdef([wspid])
diff --git a/tests/wsp.c b/tests/wsp.c
index cca3a36..bd13e63 100644
--- a/tests/wsp.c
+++ b/tests/wsp.c
@@ -64,6 +64,11 @@ struct kwd opt_keytab[] = {
64 { "nullglob", WRDSO_NULLGLOB }, 64 { "nullglob", WRDSO_NULLGLOB },
65 { "failglob", WRDSO_FAILGLOB }, 65 { "failglob", WRDSO_FAILGLOB },
66 { "dotglob", WRDSO_DOTGLOB }, 66 { "dotglob", WRDSO_DOTGLOB },
67 { "bskeep_words", WRDSO_BSKEEP_WORD },
68 { "bskeep_quote", WRDSO_BSKEEP_QUOTE },
69 { "bskeep", WRDSO_BSKEEP_WORD|WRDSO_BSKEEP_QUOTE },
70 { "novarsplit", WRDSO_NOVARSPLIT },
71 { "nocmdsplit", WRDSO_NOCMDSPLIT },
67 { NULL, 0 } 72 { NULL, 0 }
68}; 73};
69 74

Return to:

Send suggestions and report system problems to the System administrator.