Skip to content

Commit

Permalink
37364: "test" and "[" handling of parentheses.
Browse files Browse the repository at this point in the history
If three arguments, need to prefer binary operators if possible.

Need to look for full string for parentheses.
  • Loading branch information
Peter Stephenson committed Dec 9, 2015
1 parent 7f5b2f5 commit 67877f6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
@@ -1,3 +1,9 @@
2015-12-09 Peter Stephenson <p.stephenson@samsung.com>

* 37364: Src/builtin.c, Src/text.c, Test/C02cond.ztst: "test"
and "[" need to prefer binary operators to parentheses in
three-argument expressions.

2015-12-08 Peter Stephenson <p.stephenson@samsung.com>

* 37348: Src/utils.c, Test/D04parameter.ztst,
Expand Down
8 changes: 7 additions & 1 deletion Src/builtin.c
Expand Up @@ -6463,7 +6463,13 @@ bin_test(char *name, char **argv, UNUSED(Options ops), int func)
nargs = arrlen(argv);
if (nargs == 3 || nargs == 4)
{
if (*argv[0] == '(' && *argv[nargs-1] == ')') {
/*
* As parentheses are an extension, we need to be careful ---
* if this is a three-argument expression that could
* be a binary operator, prefer that.
*/
if (!strcmp(argv[0], "(") && !strcmp(argv[nargs-1],")") &&
(nargs != 3 || !is_cond_binary_op(argv[1]))) {
argv[nargs-1] = NULL;
argv++;
}
Expand Down
32 changes: 25 additions & 7 deletions Src/text.c
Expand Up @@ -40,9 +40,32 @@
/**/
int text_expand_tabs;

/*
* Binary operators in conditions.
* There order is tied to the order of the definitions COND_STREQ
* et seq. in zsh.h.
*/
static const char *cond_binary_ops[] = {
"=", "!=", "<", ">", "-nt", "-ot", "-ef", "-eq",
"-ne", "-lt", "-gt", "-le", "-ge", "=~"
};

static char *tptr, *tbuf, *tlim, *tpending;
static int tsiz, tindent, tnewlins, tjob;

/**/
int
is_cond_binary_op(const char *str)
{
const char **op;
for (op = cond_binary_ops; *op; op++)
{
if (!strcmp(str, *op))
return 1;
}
return 0;
}

static void
dec_tindent(void)
{
Expand Down Expand Up @@ -120,7 +143,7 @@ taddchr(int c)

/**/
static void
taddstr(char *s)
taddstr(const char *s)
{
int sl = strlen(s);
char c;
Expand Down Expand Up @@ -822,11 +845,6 @@ gettext2(Estate state)
break;
case WC_COND:
{
static char *c1[] = {
"=", "!=", "<", ">", "-nt", "-ot", "-ef", "-eq",
"-ne", "-lt", "-gt", "-le", "-ge", "=~"
};

int ctype;

if (!s) {
Expand Down Expand Up @@ -912,7 +930,7 @@ gettext2(Estate state)
/* Binary test: `a = b' etc. */
taddstr(ecgetstr(state, EC_NODUP, NULL));
taddstr(" ");
taddstr(c1[ctype - COND_STREQ]);
taddstr(cond_binary_ops[ctype - COND_STREQ]);
taddstr(" ");
taddstr(ecgetstr(state, EC_NODUP, NULL));
if (ctype == COND_STREQ ||
Expand Down
12 changes: 12 additions & 0 deletions Test/C02cond.ztst
Expand Up @@ -389,6 +389,18 @@ F:Failures in these cases do not indicate a problem in the shell.
>Not zero 5
>Not zero 6

[ '(' = ')' ] || print OK 1
[ '((' = '))' ] || print OK 2
[ '(' = '(' ] && print OK 3
[ '(' non-empty-string ')' ] && echo OK 4
[ '(' '' ')' ] || echo OK 5
0:yet more old-fashioned test fix ups: prefer comparison to parentheses
>OK 1
>OK 2
>OK 3
>OK 4
>OK 5

%clean
# This works around a bug in rm -f in some versions of Cygwin
chmod 644 unmodish
Expand Down

0 comments on commit 67877f6

Please sign in to comment.