Skip to content

Commit 67877f6

Browse files
author
Peter Stephenson
committed
37364: "test" and "[" handling of parentheses.
If three arguments, need to prefer binary operators if possible. Need to look for full string for parentheses.
1 parent 7f5b2f5 commit 67877f6

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2015-12-09 Peter Stephenson <p.stephenson@samsung.com>
2+
3+
* 37364: Src/builtin.c, Src/text.c, Test/C02cond.ztst: "test"
4+
and "[" need to prefer binary operators to parentheses in
5+
three-argument expressions.
6+
17
2015-12-08 Peter Stephenson <p.stephenson@samsung.com>
28

39
* 37348: Src/utils.c, Test/D04parameter.ztst,

Src/builtin.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6463,7 +6463,13 @@ bin_test(char *name, char **argv, UNUSED(Options ops), int func)
64636463
nargs = arrlen(argv);
64646464
if (nargs == 3 || nargs == 4)
64656465
{
6466-
if (*argv[0] == '(' && *argv[nargs-1] == ')') {
6466+
/*
6467+
* As parentheses are an extension, we need to be careful ---
6468+
* if this is a three-argument expression that could
6469+
* be a binary operator, prefer that.
6470+
*/
6471+
if (!strcmp(argv[0], "(") && !strcmp(argv[nargs-1],")") &&
6472+
(nargs != 3 || !is_cond_binary_op(argv[1]))) {
64676473
argv[nargs-1] = NULL;
64686474
argv++;
64696475
}

Src/text.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,32 @@
4040
/**/
4141
int text_expand_tabs;
4242

43+
/*
44+
* Binary operators in conditions.
45+
* There order is tied to the order of the definitions COND_STREQ
46+
* et seq. in zsh.h.
47+
*/
48+
static const char *cond_binary_ops[] = {
49+
"=", "!=", "<", ">", "-nt", "-ot", "-ef", "-eq",
50+
"-ne", "-lt", "-gt", "-le", "-ge", "=~"
51+
};
52+
4353
static char *tptr, *tbuf, *tlim, *tpending;
4454
static int tsiz, tindent, tnewlins, tjob;
4555

56+
/**/
57+
int
58+
is_cond_binary_op(const char *str)
59+
{
60+
const char **op;
61+
for (op = cond_binary_ops; *op; op++)
62+
{
63+
if (!strcmp(str, *op))
64+
return 1;
65+
}
66+
return 0;
67+
}
68+
4669
static void
4770
dec_tindent(void)
4871
{
@@ -120,7 +143,7 @@ taddchr(int c)
120143

121144
/**/
122145
static void
123-
taddstr(char *s)
146+
taddstr(const char *s)
124147
{
125148
int sl = strlen(s);
126149
char c;
@@ -822,11 +845,6 @@ gettext2(Estate state)
822845
break;
823846
case WC_COND:
824847
{
825-
static char *c1[] = {
826-
"=", "!=", "<", ">", "-nt", "-ot", "-ef", "-eq",
827-
"-ne", "-lt", "-gt", "-le", "-ge", "=~"
828-
};
829-
830848
int ctype;
831849

832850
if (!s) {
@@ -912,7 +930,7 @@ gettext2(Estate state)
912930
/* Binary test: `a = b' etc. */
913931
taddstr(ecgetstr(state, EC_NODUP, NULL));
914932
taddstr(" ");
915-
taddstr(c1[ctype - COND_STREQ]);
933+
taddstr(cond_binary_ops[ctype - COND_STREQ]);
916934
taddstr(" ");
917935
taddstr(ecgetstr(state, EC_NODUP, NULL));
918936
if (ctype == COND_STREQ ||

Test/C02cond.ztst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,18 @@ F:Failures in these cases do not indicate a problem in the shell.
389389
>Not zero 5
390390
>Not zero 6
391391

392+
[ '(' = ')' ] || print OK 1
393+
[ '((' = '))' ] || print OK 2
394+
[ '(' = '(' ] && print OK 3
395+
[ '(' non-empty-string ')' ] && echo OK 4
396+
[ '(' '' ')' ] || echo OK 5
397+
0:yet more old-fashioned test fix ups: prefer comparison to parentheses
398+
>OK 1
399+
>OK 2
400+
>OK 3
401+
>OK 4
402+
>OK 5
403+
392404
%clean
393405
# This works around a bug in rm -f in some versions of Cygwin
394406
chmod 644 unmodish

0 commit comments

Comments
 (0)