1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/**
* @file cond.c
* @brief Evaluating `[[ expression ]]`, the Korn shell conditional
* command.
*
* The expression was parsed into a tree (see parse_cond_command()), so
* evaluating it needs no argument vector, no builtin lookup and no
* re-parsing. Operands are expanded only when they are reached, which
* makes `&&` and `||` short-circuit expansions and command substitutions
* too, and they are never split into fields or globbed.
*
* | Expression | True when |
* |-------------------|---------------------------------------------------|
* | `s` | s is not empty |
* | `-n s`, `-z s` | s is not empty / is empty |
* | `-f file` ... | the file tests of `test`. `-a file` is `-e` |
* | `-o option` | the shell option is on (`-o errexit`) |
* | `-v name` | the variable is set |
* | `s == p`, `s = p` | s matches the pattern p (quote p to compare text) |
* | `s != p` | s does not match p |
* | `s =~ re` | s matches the extended regular expression re |
* | `s < t`, `s > t` | s sorts before / after t (byte order) |
* | `x -eq y` ... | arithmetic comparison. X and y are expressions |
* | `f -nt g` ... | file comparisons, as in `test` |
*
* The status is 0 for true, 1 for false and 2 for a bad regular
* expression. An invalid arithmetic operand is an error, as in `$(( ))`.
*/
#include <regex.h>
#include <string.h>
#include "exec.h"
#include "expand.h"
/**
* @brief Expand an operand: parameters, command substitution, arithmetic,
* tilde, quote removal. No splitting or globbing.
*/
static const char *operand(const word *w) {
if (w->plain)
return w->plain;
return expand_string(w->parts, XS_TILDE);
}
/**
* @brief Is this a character with a meaning in an extended regular
* expression?
*/
static bool is_ere_special(char c) {
return strchr("\\.[]()*+?{}|^$", c) != nullptr;
}
/**
* @brief Expand the right-hand side of `=~` into a regular expression.
*
* Unquoted parts keep their meaning as regular-expression text. Quoted
* parts are escaped so that they match literally, as in bash and ksh93:
* `[[ $x =~ "a.b" ]]` looks for the three characters `a.b`.
*
* @param w The operand.
* @return The expression, in scratch memory.
*/
static const char *regex_text(const word *w) {
strbuf sb;
sb_init(&sb, &scratch);
for (const wpart *p = w->parts; p; p = p->next) {
wpart one = *p;
one.next = nullptr;
const char *s = expand_string(&one, 0);
if (!p->quoted) {
sb_puts(&sb, s);
continue;
}
for (; *s; s++) {
if (is_ere_special(*s))
sb_putc(&sb, '\\');
sb_putc(&sb, *s);
}
}
return sb_cstr(&sb);
}
/**
* @brief Evaluate `s =~ re`.
* @return 0 for a match, 1 for none, 2 if `re` is not a valid
* expression.
*/
static int regex_match(const char *s, const char *re) {
regex_t rx;
int rc = regcomp(&rx, re, REG_EXTENDED | REG_NOSUB);
if (rc != 0) {
char msg[128];
regerror(rc, &rx, msg, sizeof msg);
sh_warn("[[: %s: %s", re, msg);
return 2;
}
rc = regexec(&rx, s, 0, nullptr, 0);
regfree(&rx);
return rc == 0 ? 0 : 1;
}
/**
* @brief Evaluate a unary test.
* @param op The operator's letter.
* @param arg The expanded operand.
*/
static bool unary(char op, const char *arg) {
switch (op) {
case 'a':
return test_unary('e', arg);
case 'o':
return option_is_set(arg) == 1;
case 'v':
return var_get(arg) != nullptr;
default:
return test_unary(op, arg);
}
}
/**
* @brief Evaluate a binary test.
* @param c The node.
* @return 0, 1, or 2 for a bad regular expression.
*/
static int binary(const cond *c) {
const char *a = operand(c->a);
switch (c->op) {
case CO_MATCH:
case CO_NOMATCH: {
/* A quoted literal is plain too, but must match only itself. */
const char *pat = c->b->plain && !c->b->parts->quoted
? c->b->plain
: expand_pattern(c->b->parts);
bool m = pat_match(pat, a, a + strlen(a));
return (c->op == CO_MATCH) == m ? 0 : 1;
}
case CO_REGEX:
return regex_match(a, regex_text(c->b));
case CO_LESS:
return strcmp(a, operand(c->b)) < 0 ? 0 : 1;
case CO_GREATER:
return strcmp(a, operand(c->b)) > 0 ? 0 : 1;
case CO_NEWER:
return test_files(a, 'n', operand(c->b)) ? 0 : 1;
case CO_OLDER:
return test_files(a, 'o', operand(c->b)) ? 0 : 1;
case CO_SAMEFILE:
return test_files(a, 'e', operand(c->b)) ? 0 : 1;
default:
break;
}
/* -eq and friends: both sides are arithmetic expressions. */
intmax_t x = arith_eval(a);
intmax_t y = arith_eval(operand(c->b));
bool r;
switch (c->op) {
case CO_EQ:
r = x == y;
break;
case CO_NE:
r = x != y;
break;
case CO_LT:
r = x < y;
break;
case CO_LE:
r = x <= y;
break;
case CO_GT:
r = x > y;
break;
default:
r = x >= y;
break;
}
return r ? 0 : 1;
}
/**
* @brief Evaluate a `[[ ]]` expression.
* @param c The expression.
* @return 0 if it is true, 1 if false, 2 on a bad regular expression.
*/
int eval_cond(const cond *c) {
int r;
switch (c->kind) {
case CN_AND:
r = eval_cond(c->left);
return r ? r : eval_cond(c->right);
case CN_OR:
r = eval_cond(c->left);
return r == 1 ? eval_cond(c->right) : r;
case CN_NOT:
r = eval_cond(c->left);
return r == 2 ? 2 : !r;
case CN_UNARY:
return unary(c->uop, operand(c->a)) ? 0 : 1;
case CN_BINARY:
return binary(c);
case CN_STRING:
return *operand(c->a) ? 0 : 1;
}
UNREACHABLE();
}
|