mirror of
git://git.gnu.org.ua/wordsplit.git
synced 2025-04-26 00:29:54 +03:00
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.
This commit is contained in:
parent
65d0759a8b
commit
c01a4a61e8
4 changed files with 38 additions and 8 deletions
|
@ -201,9 +201,7 @@ struct wordsplit
|
||||||
#define WRDSO_FAILGLOB 0x00000002
|
#define WRDSO_FAILGLOB 0x00000002
|
||||||
/* Allow a leading period to be matched by metacharacters. */
|
/* Allow a leading period to be matched by metacharacters. */
|
||||||
#define WRDSO_DOTGLOB 0x00000004
|
#define WRDSO_DOTGLOB 0x00000004
|
||||||
#if 0 /* Unused value */
|
/* Unused value: 0x00000008 */
|
||||||
#define WRDSO_ARGV 0x00000008
|
|
||||||
#endif
|
|
||||||
/* Keep backslash in unrecognized escape sequences in words */
|
/* Keep backslash in unrecognized escape sequences in words */
|
||||||
#define WRDSO_BSKEEP_WORD 0x00000010
|
#define WRDSO_BSKEEP_WORD 0x00000010
|
||||||
/* Handle octal escapes in words */
|
/* Handle octal escapes in words */
|
||||||
|
@ -220,6 +218,13 @@ struct wordsplit
|
||||||
#define WRDSO_OESC_QUOTE 0x00000200
|
#define WRDSO_OESC_QUOTE 0x00000200
|
||||||
/* Handle hex escapes in quoted strings */
|
/* Handle hex escapes in quoted strings */
|
||||||
#define WRDSO_XESC_QUOTE 0x00000400
|
#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
|
||||||
|
|
||||||
#define WRDSO_BSKEEP WRDSO_BSKEEP_WORD
|
#define WRDSO_BSKEEP WRDSO_BSKEEP_WORD
|
||||||
#define WRDSO_OESC WRDSO_OESC_WORD
|
#define WRDSO_OESC WRDSO_OESC_WORD
|
||||||
|
|
|
@ -2058,11 +2058,13 @@ scan_word (struct wordsplit *wsp, size_t start, int consume_all)
|
||||||
|
|
||||||
if (command[i] == '$')
|
if (command[i] == '$')
|
||||||
{
|
{
|
||||||
if (!(wsp->ws_flags & WRDSF_NOVAR)
|
if ((!(wsp->ws_flags & WRDSF_NOVAR)
|
||||||
|
|| (wsp->ws_options & WRDSO_NOVARSPLIT))
|
||||||
&& command[i+1] == '{'
|
&& command[i+1] == '{'
|
||||||
&& find_closing_paren (command, i + 2, len, &i, "{}") == 0)
|
&& find_closing_paren (command, i + 2, len, &i, "{}") == 0)
|
||||||
continue;
|
continue;
|
||||||
if (!(wsp->ws_flags & WRDSF_NOCMD)
|
if ((!(wsp->ws_flags & WRDSF_NOCMD)
|
||||||
|
|| (wsp->ws_options & WRDSO_NOCMDSPLIT))
|
||||||
&& command[i+1] == '('
|
&& command[i+1] == '('
|
||||||
&& find_closing_paren (command, i + 2, len, &i, "()") == 0)
|
&& find_closing_paren (command, i + 2, len, &i, "()") == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -950,6 +950,24 @@ TOTAL: 3
|
||||||
[input exhausted
|
[input exhausted
|
||||||
]))
|
]))
|
||||||
|
|
||||||
|
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])
|
m4_popdef([TESTWSP])
|
||||||
m4_popdef([wspnum])
|
m4_popdef([wspnum])
|
||||||
m4_popdef([wspid])
|
m4_popdef([wspid])
|
||||||
|
|
11
tests/wsp.c
11
tests/wsp.c
|
@ -61,9 +61,14 @@ struct kwd bool_keytab[] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct kwd opt_keytab[] = {
|
struct kwd opt_keytab[] = {
|
||||||
{ "nullglob", WRDSO_NULLGLOB },
|
{ "nullglob", WRDSO_NULLGLOB },
|
||||||
{ "failglob", WRDSO_FAILGLOB },
|
{ "failglob", WRDSO_FAILGLOB },
|
||||||
{ "dotglob", WRDSO_DOTGLOB },
|
{ "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 }
|
{ NULL, 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue