aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2007-04-02 16:38:13 +0000
committerBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2007-04-02 16:38:13 +0000
commitadb01b1583199c5563cb729b9485d024cda62b30 (patch)
tree54db790b802c05b74cd2220e0833d197ef8c5f59
parent729bd9e0b0d5977e9c2c2a4eff7e2f0ca2ad4b9e (diff)
downloadbusybox-w32-adb01b1583199c5563cb729b9485d024cda62b30.tar.gz
busybox-w32-adb01b1583199c5563cb729b9485d024cda62b30.tar.bz2
busybox-w32-adb01b1583199c5563cb729b9485d024cda62b30.zip
- remove args from bss, minor misc shrinkage while at it.
text data bss dec hex filename 2577 0 4 2581 a15 expr.o 2522 0 0 2522 9da expr.o
-rw-r--r--coreutils/expr.c85
1 files changed, 45 insertions, 40 deletions
diff --git a/coreutils/expr.c b/coreutils/expr.c
index b7e33de49..b082de1e9 100644
--- a/coreutils/expr.c
+++ b/coreutils/expr.c
@@ -62,15 +62,18 @@ struct valinfo {
62typedef struct valinfo VALUE; 62typedef struct valinfo VALUE;
63 63
64/* The arguments given to the program, minus the program name. */ 64/* The arguments given to the program, minus the program name. */
65static char **args; 65struct globals {
66 char **args;
67};
68#define G (*(struct globals*)&bb_common_bufsiz1)
66 69
67static VALUE *docolon(VALUE * sv, VALUE * pv); 70static VALUE *docolon(VALUE * sv, VALUE * pv);
68static VALUE *eval(void); 71static VALUE *eval(void);
69static VALUE *int_value(arith_t i); 72static VALUE *int_value(arith_t i);
70static VALUE *str_value(const char *s); 73static VALUE *str_value(const char *s);
71static int nextarg(const char *str); 74static bool nextarg(const char *str);
72static int null(VALUE * v); 75static int null(VALUE * v);
73static int toarith(VALUE * v); 76static bool toarith(VALUE * v);
74static void freev(VALUE * v); 77static void freev(VALUE * v);
75static void tostring(VALUE * v); 78static void tostring(VALUE * v);
76 79
@@ -83,10 +86,10 @@ int expr_main(int argc, char **argv)
83 bb_error_msg_and_die("too few arguments"); 86 bb_error_msg_and_die("too few arguments");
84 } 87 }
85 88
86 args = argv + 1; 89 G.args = argv + 1;
87 90
88 v = eval(); 91 v = eval();
89 if (*args) 92 if (*G.args)
90 bb_error_msg_and_die("syntax error"); 93 bb_error_msg_and_die("syntax error");
91 94
92 if (v->type == integer) 95 if (v->type == integer)
@@ -152,7 +155,7 @@ static void tostring(VALUE * v)
152 155
153/* Coerce V to an integer value. Return 1 on success, 0 on failure. */ 156/* Coerce V to an integer value. Return 1 on success, 0 on failure. */
154 157
155static int toarith(VALUE * v) 158static bool toarith(VALUE * v)
156{ 159{
157 if (v->type == string) { 160 if (v->type == string) {
158 arith_t i; 161 arith_t i;
@@ -173,11 +176,11 @@ static int toarith(VALUE * v)
173/* Return nonzero if the next token matches STR exactly. 176/* Return nonzero if the next token matches STR exactly.
174 STR must not be NULL. */ 177 STR must not be NULL. */
175 178
176static int nextarg(const char *str) 179static bool nextarg(const char *str)
177{ 180{
178 if (*args == NULL) 181 if (*G.args == NULL)
179 return 0; 182 return 0;
180 return strcmp(*args, str) == 0; 183 return strcmp(*G.args, str) == 0;
181} 184}
182 185
183/* The comparison operator handling functions. */ 186/* The comparison operator handling functions. */
@@ -281,53 +284,57 @@ static VALUE *eval7(void)
281{ 284{
282 VALUE *v; 285 VALUE *v;
283 286
284 if (!*args) 287 if (!*G.args)
285 bb_error_msg_and_die("syntax error"); 288 bb_error_msg_and_die("syntax error");
286 289
287 if (nextarg("(")) { 290 if (nextarg("(")) {
288 args++; 291 G.args++;
289 v = eval(); 292 v = eval();
290 if (!nextarg(")")) 293 if (!nextarg(")"))
291 bb_error_msg_and_die("syntax error"); 294 bb_error_msg_and_die("syntax error");
292 args++; 295 G.args++;
293 return v; 296 return v;
294 } 297 }
295 298
296 if (nextarg(")")) 299 if (nextarg(")"))
297 bb_error_msg_and_die("syntax error"); 300 bb_error_msg_and_die("syntax error");
298 301
299 return str_value(*args++); 302 return str_value(*G.args++);
300} 303}
301 304
302/* Handle match, substr, index, length, and quote keywords. */ 305/* Handle match, substr, index, length, and quote keywords. */
303 306
304static VALUE *eval6(void) 307static VALUE *eval6(void)
305{ 308{
306 VALUE *l, *r, *v, *i1, *i2; 309 VALUE *l, *r, *v = NULL /* silence gcc */, *i1, *i2;
310 const char * const keywords[] = {
311 "quote", "length", "match", "index", "substr", NULL
312 };
307 313
308 if (nextarg("quote")) { 314 smalluint key = *G.args ? index_in_str_array(keywords, *G.args) + 1 : 0;
309 args++; 315 if (key == 0) /* not a keyword */
310 if (!*args) 316 return eval7();
317 G.args++; /* We have a valid token, so get the next argument. */
318 if (key == 1) { /* quote */
319 if (!*G.args)
311 bb_error_msg_and_die("syntax error"); 320 bb_error_msg_and_die("syntax error");
312 return str_value(*args++); 321 return str_value(*G.args++);
313 } else if (nextarg("length")) { 322 }
314 args++; 323 if (key == 2) { /* length */
315 r = eval6(); 324 r = eval6();
316 tostring(r); 325 tostring(r);
317 v = int_value(strlen(r->u.s)); 326 v = int_value(strlen(r->u.s));
318 freev(r); 327 freev(r);
319 return v; 328 } else
320 } else if (nextarg("match")) {
321 args++;
322 l = eval6(); 329 l = eval6();
330
331 if (key == 3) { /* match */
323 r = eval6(); 332 r = eval6();
324 v = docolon(l, r); 333 v = docolon(l, r);
325 freev(l); 334 freev(l);
326 freev(r); 335 freev(r);
327 return v; 336 }
328 } else if (nextarg("index")) { 337 if (key == 4) { /* index */
329 args++;
330 l = eval6();
331 r = eval6(); 338 r = eval6();
332 tostring(l); 339 tostring(l);
333 tostring(r); 340 tostring(r);
@@ -336,10 +343,8 @@ static VALUE *eval6(void)
336 v->u.i = 0; 343 v->u.i = 0;
337 freev(l); 344 freev(l);
338 freev(r); 345 freev(r);
339 return v; 346 }
340 } else if (nextarg("substr")) { 347 if (key == 5) { /* substr */
341 args++;
342 l = eval6();
343 i1 = eval6(); 348 i1 = eval6();
344 i2 = eval6(); 349 i2 = eval6();
345 tostring(l); 350 tostring(l);
@@ -355,9 +360,9 @@ static VALUE *eval6(void)
355 freev(l); 360 freev(l);
356 freev(i1); 361 freev(i1);
357 freev(i2); 362 freev(i2);
358 return v; 363 }
359 } else 364 return v;
360 return eval7(); 365
361} 366}
362 367
363/* Handle : operator (pattern matching). 368/* Handle : operator (pattern matching).
@@ -369,7 +374,7 @@ static VALUE *eval5(void)
369 374
370 l = eval6(); 375 l = eval6();
371 while (nextarg(":")) { 376 while (nextarg(":")) {
372 args++; 377 G.args++;
373 r = eval6(); 378 r = eval6();
374 v = docolon(l, r); 379 v = docolon(l, r);
375 freev(l); 380 freev(l);
@@ -397,7 +402,7 @@ static VALUE *eval4(void)
397 op = '%'; 402 op = '%';
398 else 403 else
399 return l; 404 return l;
400 args++; 405 G.args++;
401 r = eval5(); 406 r = eval5();
402 val = arithmetic_common(l, r, op); 407 val = arithmetic_common(l, r, op);
403 freev(l); 408 freev(l);
@@ -422,7 +427,7 @@ static VALUE *eval3(void)
422 op = '-'; 427 op = '-';
423 else 428 else
424 return l; 429 return l;
425 args++; 430 G.args++;
426 r = eval4(); 431 r = eval4();
427 val = arithmetic_common(l, r, op); 432 val = arithmetic_common(l, r, op);
428 freev(l); 433 freev(l);
@@ -455,7 +460,7 @@ static VALUE *eval2(void)
455 op = '>'; 460 op = '>';
456 else 461 else
457 return l; 462 return l;
458 args++; 463 G.args++;
459 r = eval3(); 464 r = eval3();
460 toarith(l); 465 toarith(l);
461 toarith(r); 466 toarith(r);
@@ -474,7 +479,7 @@ static VALUE *eval1(void)
474 479
475 l = eval2(); 480 l = eval2();
476 while (nextarg("&")) { 481 while (nextarg("&")) {
477 args++; 482 G.args++;
478 r = eval2(); 483 r = eval2();
479 if (null(l) || null(r)) { 484 if (null(l) || null(r)) {
480 freev(l); 485 freev(l);
@@ -494,7 +499,7 @@ static VALUE *eval(void)
494 499
495 l = eval1(); 500 l = eval1();
496 while (nextarg("|")) { 501 while (nextarg("|")) {
497 args++; 502 G.args++;
498 r = eval1(); 503 r = eval1();
499 if (null(l)) { 504 if (null(l)) {
500 freev(l); 505 freev(l);