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#include "libbb.h"
64#include "xregex.h"
65
66#if ENABLE_EXPR_MATH_SUPPORT_64
67typedef int64_t arith_t;
68
69#define PF_REZ "ll"
70#define PF_REZ_TYPE (long long)
71#define STRTOL(s, e, b) strtoll(s, e, b)
72#else
73typedef long arith_t;
74
75#define PF_REZ "l"
76#define PF_REZ_TYPE (long)
77#define STRTOL(s, e, b) strtol(s, e, b)
78#endif
79
80
81
82
83enum {
84 INTEGER,
85 STRING
86};
87
88
89struct valinfo {
90 smallint type;
91 union {
92 arith_t i;
93 char *s;
94 } u;
95};
96typedef struct valinfo VALUE;
97
98
99struct globals {
100 char **args;
101} FIX_ALIASING;
102#define G (*(struct globals*)&bb_common_bufsiz1)
103#define INIT_G() do { } while (0)
104
105
106static VALUE *eval(void);
107
108
109
110
111static VALUE *int_value(arith_t i)
112{
113 VALUE *v;
114
115 v = xzalloc(sizeof(VALUE));
116 if (INTEGER)
117 v->type = INTEGER;
118 v->u.i = i;
119 return v;
120}
121
122
123
124static VALUE *str_value(const char *s)
125{
126 VALUE *v;
127
128 v = xzalloc(sizeof(VALUE));
129 if (STRING)
130 v->type = STRING;
131 v->u.s = xstrdup(s);
132 return v;
133}
134
135
136
137static void freev(VALUE *v)
138{
139 if (v->type == STRING)
140 free(v->u.s);
141 free(v);
142}
143
144
145
146static int null(VALUE *v)
147{
148 if (v->type == INTEGER)
149 return v->u.i == 0;
150
151 return v->u.s[0] == '\0' || LONE_CHAR(v->u.s, '0');
152}
153
154
155
156static void tostring(VALUE *v)
157{
158 if (v->type == INTEGER) {
159 v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
160 v->type = STRING;
161 }
162}
163
164
165
166static bool toarith(VALUE *v)
167{
168 if (v->type == STRING) {
169 arith_t i;
170 char *e;
171
172
173
174 i = STRTOL(v->u.s, &e, 10);
175 if ((v->u.s == e) || *e)
176 return 0;
177 free(v->u.s);
178 v->u.i = i;
179 v->type = INTEGER;
180 }
181 return 1;
182}
183
184
185
186
187static int nextarg(const char *str)
188{
189 if (*G.args == NULL || strcmp(*G.args, str) != 0)
190 return 0;
191 return (unsigned char)str[0] + (unsigned char)str[1];
192}
193
194
195
196static int cmp_common(VALUE *l, VALUE *r, int op)
197{
198 arith_t ll, rr;
199
200 ll = l->u.i;
201 rr = r->u.i;
202 if (l->type == STRING || r->type == STRING) {
203 tostring(l);
204 tostring(r);
205 ll = strcmp(l->u.s, r->u.s);
206 rr = 0;
207 }
208
209
210 if (op == '<')
211 return ll < rr;
212 if (op == ('<' + '='))
213 return ll <= rr;
214 if (op == '=' || (op == '=' + '='))
215 return ll == rr;
216 if (op == '!' + '=')
217 return ll != rr;
218 if (op == '>')
219 return ll > rr;
220
221 return ll >= rr;
222}
223
224
225
226static arith_t arithmetic_common(VALUE *l, VALUE *r, int op)
227{
228 arith_t li, ri;
229
230 if (!toarith(l) || !toarith(r))
231 bb_error_msg_and_die("non-numeric argument");
232 li = l->u.i;
233 ri = r->u.i;
234 if (op == '+')
235 return li + ri;
236 if (op == '-')
237 return li - ri;
238 if (op == '*')
239 return li * ri;
240 if (ri == 0)
241 bb_error_msg_and_die("division by zero");
242 if (op == '/')
243 return li / ri;
244 return li % ri;
245}
246
247
248
249
250
251static VALUE *docolon(VALUE *sv, VALUE *pv)
252{
253 enum { NMATCH = 2 };
254 VALUE *v;
255 regex_t re_buffer;
256 regmatch_t re_regs[NMATCH];
257
258 tostring(sv);
259 tostring(pv);
260
261 if (pv->u.s[0] == '^') {
262 bb_error_msg(
263"warning: '%s': using '^' as the first character\n"
264"of a basic regular expression is not portable; it is ignored", pv->u.s);
265 }
266
267 memset(&re_buffer, 0, sizeof(re_buffer));
268 memset(re_regs, 0, sizeof(re_regs));
269 xregcomp(&re_buffer, pv->u.s, 0);
270
271
272
273 if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH
274 && re_regs[0].rm_so == 0
275 ) {
276
277 if (re_buffer.re_nsub > 0 && re_regs[1].rm_so >= 0) {
278 sv->u.s[re_regs[1].rm_eo] = '\0';
279 v = str_value(sv->u.s + re_regs[1].rm_so);
280 } else {
281 v = int_value(re_regs[0].rm_eo);
282 }
283 } else {
284
285 if (re_buffer.re_nsub > 0)
286 v = str_value("");
287 else
288 v = int_value(0);
289 }
290 regfree(&re_buffer);
291 return v;
292}
293
294
295
296static VALUE *eval7(void)
297{
298 VALUE *v;
299
300 if (!*G.args)
301 bb_error_msg_and_die("syntax error");
302
303 if (nextarg("(")) {
304 G.args++;
305 v = eval();
306 if (!nextarg(")"))
307 bb_error_msg_and_die("syntax error");
308 G.args++;
309 return v;
310 }
311
312 if (nextarg(")"))
313 bb_error_msg_and_die("syntax error");
314
315 return str_value(*G.args++);
316}
317
318
319
320static VALUE *eval6(void)
321{
322 static const char keywords[] ALIGN1 =
323 "quote\0""length\0""match\0""index\0""substr\0";
324
325 VALUE *r, *i1, *i2;
326 VALUE *l = l;
327 VALUE *v = v;
328 int key = *G.args ? index_in_strings(keywords, *G.args) + 1 : 0;
329
330 if (key == 0)
331 return eval7();
332 G.args++;
333 if (key == 1) {
334 if (!*G.args)
335 bb_error_msg_and_die("syntax error");
336 return str_value(*G.args++);
337 }
338 if (key == 2) {
339 r = eval6();
340 tostring(r);
341 v = int_value(strlen(r->u.s));
342 freev(r);
343 } else
344 l = eval6();
345
346 if (key == 3) {
347 r = eval6();
348 v = docolon(l, r);
349 freev(l);
350 freev(r);
351 }
352 if (key == 4) {
353 r = eval6();
354 tostring(l);
355 tostring(r);
356 v = int_value(strcspn(l->u.s, r->u.s) + 1);
357 if (v->u.i == (arith_t) strlen(l->u.s) + 1)
358 v->u.i = 0;
359 freev(l);
360 freev(r);
361 }
362 if (key == 5) {
363 i1 = eval6();
364 i2 = eval6();
365 tostring(l);
366 if (!toarith(i1) || !toarith(i2)
367 || i1->u.i > (arith_t) strlen(l->u.s)
368 || i1->u.i <= 0 || i2->u.i <= 0)
369 v = str_value("");
370 else {
371 v = xmalloc(sizeof(VALUE));
372 v->type = STRING;
373 v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
374 }
375 freev(l);
376 freev(i1);
377 freev(i2);
378 }
379 return v;
380}
381
382
383
384
385static VALUE *eval5(void)
386{
387 VALUE *l, *r, *v;
388
389 l = eval6();
390 while (nextarg(":")) {
391 G.args++;
392 r = eval6();
393 v = docolon(l, r);
394 freev(l);
395 freev(r);
396 l = v;
397 }
398 return l;
399}
400
401
402
403static VALUE *eval4(void)
404{
405 VALUE *l, *r;
406 int op;
407 arith_t val;
408
409 l = eval5();
410 while (1) {
411 op = nextarg("*");
412 if (!op) { op = nextarg("/");
413 if (!op) { op = nextarg("%");
414 if (!op) return l;
415 }}
416 G.args++;
417 r = eval5();
418 val = arithmetic_common(l, r, op);
419 freev(l);
420 freev(r);
421 l = int_value(val);
422 }
423}
424
425
426
427static VALUE *eval3(void)
428{
429 VALUE *l, *r;
430 int op;
431 arith_t val;
432
433 l = eval4();
434 while (1) {
435 op = nextarg("+");
436 if (!op) {
437 op = nextarg("-");
438 if (!op) return l;
439 }
440 G.args++;
441 r = eval4();
442 val = arithmetic_common(l, r, op);
443 freev(l);
444 freev(r);
445 l = int_value(val);
446 }
447}
448
449
450
451static VALUE *eval2(void)
452{
453 VALUE *l, *r;
454 int op;
455 arith_t val;
456
457 l = eval3();
458 while (1) {
459 op = nextarg("<");
460 if (!op) { op = nextarg("<=");
461 if (!op) { op = nextarg("=");
462 if (!op) { op = nextarg("==");
463 if (!op) { op = nextarg("!=");
464 if (!op) { op = nextarg(">=");
465 if (!op) { op = nextarg(">");
466 if (!op) return l;
467 }}}}}}
468 G.args++;
469 r = eval3();
470 toarith(l);
471 toarith(r);
472 val = cmp_common(l, r, op);
473 freev(l);
474 freev(r);
475 l = int_value(val);
476 }
477}
478
479
480
481static VALUE *eval1(void)
482{
483 VALUE *l, *r;
484
485 l = eval2();
486 while (nextarg("&")) {
487 G.args++;
488 r = eval2();
489 if (null(l) || null(r)) {
490 freev(l);
491 freev(r);
492 l = int_value(0);
493 } else
494 freev(r);
495 }
496 return l;
497}
498
499
500
501static VALUE *eval(void)
502{
503 VALUE *l, *r;
504
505 l = eval1();
506 while (nextarg("|")) {
507 G.args++;
508 r = eval1();
509 if (null(l)) {
510 freev(l);
511 l = r;
512 } else
513 freev(r);
514 }
515 return l;
516}
517
518int expr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
519int expr_main(int argc UNUSED_PARAM, char **argv)
520{
521 VALUE *v;
522
523 INIT_G();
524
525 xfunc_error_retval = 2;
526 G.args = argv + 1;
527 if (*G.args == NULL) {
528 bb_error_msg_and_die("too few arguments");
529 }
530 v = eval();
531 if (*G.args)
532 bb_error_msg_and_die("syntax error");
533 if (v->type == INTEGER)
534 printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
535 else
536 puts(v->u.s);
537 fflush_stdout_and_exit(null(v));
538}
539