diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2009-04-09 20:41:34 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2009-04-09 20:41:34 +0000 |
commit | d68ae08cde969985e3bfcfc21a64f9c78d736fe1 (patch) | |
tree | bd415a04c247e2c88049d3fa09456c52bee867a4 | |
parent | 05d3b7cc0de2283f149b07196f1ca0557c062323 (diff) | |
download | busybox-w32-d68ae08cde969985e3bfcfc21a64f9c78d736fe1.tar.gz busybox-w32-d68ae08cde969985e3bfcfc21a64f9c78d736fe1.tar.bz2 busybox-w32-d68ae08cde969985e3bfcfc21a64f9c78d736fe1.zip |
hush: shrink and make more versatile error-reporting machinery
function old new delta
syntax_error_unterm_ch - 31 +31
syntax_error_unterm_str - 14 +14
parse_stream 2356 2361 +5
syntax_error_at 12 14 +2
syntax_error 25 27 +2
syntax_error_unterminated 28 - -28
expand_variables 2063 2031 -32
------------------------------------------------------------------------------
(add/remove: 2/1 grow/shrink: 3/1 up/down: 54/-60) Total: -6 bytes
text data bss dec hex filename
67278 197 3184 70659 11403 busybox_old
67228 197 3184 70609 113d1 busybox_unstripped
-rw-r--r-- | shell/hush.c | 85 |
1 files changed, 46 insertions, 39 deletions
diff --git a/shell/hush.c b/shell/hush.c index 41d65ff69..d6f765d6b 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -663,23 +663,25 @@ static void xxfree(void *ptr) | |||
663 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. | 663 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
664 | */ | 664 | */ |
665 | #if HUSH_DEBUG < 2 | 665 | #if HUSH_DEBUG < 2 |
666 | # define die_if_script(lineno, fmt, msg) die_if_script(fmt, msg) | 666 | # define die_if_script(lineno, fmt...) die_if_script(fmt) |
667 | # define syntax_error(lineno, msg) syntax_error(msg) | 667 | # define syntax_error(lineno, msg) syntax_error(msg) |
668 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) | 668 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
669 | # define syntax_error_unterminated(lineno, ch) syntax_error_unterminated(ch) | 669 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
670 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) | ||
670 | #endif | 671 | #endif |
671 | 672 | ||
672 | static void die_if_script(unsigned lineno, const char *fmt, const char *msg) | 673 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
673 | { | 674 | { |
674 | void FAST_FUNC (*fp)(const char *s, ...) = bb_error_msg_and_die; | 675 | va_list p; |
675 | #if ENABLE_HUSH_INTERACTIVE | 676 | |
676 | if (G_interactive_fd) | ||
677 | fp = bb_error_msg; | ||
678 | #endif | ||
679 | #if HUSH_DEBUG >= 2 | 677 | #if HUSH_DEBUG >= 2 |
680 | bb_error_msg("hush.c:%u", lineno); | 678 | bb_error_msg("hush.c:%u", lineno); |
681 | #endif | 679 | #endif |
682 | fp(fmt, msg); | 680 | va_start(p, fmt); |
681 | bb_verror_msg(fmt, p, NULL); | ||
682 | va_end(p); | ||
683 | if (!G_interactive_fd) | ||
684 | xfunc_die(); | ||
683 | } | 685 | } |
684 | 686 | ||
685 | static void syntax_error(unsigned lineno, const char *msg) | 687 | static void syntax_error(unsigned lineno, const char *msg) |
@@ -695,7 +697,7 @@ static void syntax_error_at(unsigned lineno, const char *msg) | |||
695 | die_if_script(lineno, "syntax error at '%s'", msg); | 697 | die_if_script(lineno, "syntax error at '%s'", msg); |
696 | } | 698 | } |
697 | 699 | ||
698 | static void syntax_error_unterminated(unsigned lineno, char ch) | 700 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
699 | { | 701 | { |
700 | char msg[2]; | 702 | char msg[2]; |
701 | msg[0] = ch; | 703 | msg[0] = ch; |
@@ -703,16 +705,23 @@ static void syntax_error_unterminated(unsigned lineno, char ch) | |||
703 | die_if_script(lineno, "syntax error: unterminated %s", msg); | 705 | die_if_script(lineno, "syntax error: unterminated %s", msg); |
704 | } | 706 | } |
705 | 707 | ||
708 | static void syntax_error_unterm_str(unsigned lineno, const char *s) | ||
709 | { | ||
710 | die_if_script(lineno, "syntax error: unterminated %s", s); | ||
711 | } | ||
712 | |||
706 | #if HUSH_DEBUG < 2 | 713 | #if HUSH_DEBUG < 2 |
707 | # undef die_if_script | 714 | # undef die_if_script |
708 | # undef syntax_error | 715 | # undef syntax_error |
709 | # undef syntax_error_at | 716 | # undef syntax_error_at |
710 | # undef syntax_error_unterminated | 717 | # undef syntax_error_unterm_ch |
718 | # undef syntax_error_unterm_str | ||
711 | #else | 719 | #else |
712 | # define die_if_script(fmt, msg) die_if_script(__LINE__, fmt, msg) | 720 | # define die_if_script(fmt...) die_if_script(__LINE__, fmt) |
713 | # define syntax_error(msg) syntax_error(__LINE__, msg) | 721 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
714 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) | 722 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
715 | # define syntax_error_unterminated(ch) syntax_error_unterminated(__LINE__, ch) | 723 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
724 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) | ||
716 | #endif | 725 | #endif |
717 | 726 | ||
718 | 727 | ||
@@ -1957,7 +1966,7 @@ static int expand_vars_to_list(o_string *output, int n, char *arg, char or_mask) | |||
1957 | msg = "expression recursion loop detected"; | 1966 | msg = "expression recursion loop detected"; |
1958 | break; | 1967 | break; |
1959 | } | 1968 | } |
1960 | die_if_script(msg, NULL); | 1969 | die_if_script(msg); |
1961 | } | 1970 | } |
1962 | debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res); | 1971 | debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res); |
1963 | sprintf(arith_buf, arith_t_fmt, res); | 1972 | sprintf(arith_buf, arith_t_fmt, res); |
@@ -2036,20 +2045,18 @@ static int expand_vars_to_list(o_string *output, int n, char *arg, char or_mask) | |||
2036 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, | 2045 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
2037 | exp_null ? "true" : "false", exp_test); | 2046 | exp_null ? "true" : "false", exp_test); |
2038 | if (exp_test) { | 2047 | if (exp_test) { |
2039 | if (exp_op == '?') | 2048 | if (exp_op == '?') { |
2040 | //TODO: what does interactive bash | 2049 | //TODO: how interactive bash aborts expansion mid-command? |
2041 | /* ${var?[error_msg_if_unset]} */ | 2050 | /* ${var?[error_msg_if_unset]} */ |
2042 | /* ${var:?[error_msg_if_unset_or_null]} */ | 2051 | /* ${var:?[error_msg_if_unset_or_null]} */ |
2043 | /* mimic bash message */ | 2052 | /* mimic bash message */ |
2044 | if (*exp_word) { | 2053 | die_if_script("%s: %s", |
2045 | char *msg = xasprintf("%s: %s", var, exp_word); | 2054 | var, |
2046 | die_if_script("%s", msg); | 2055 | exp_word[0] ? exp_word : "parameter null or not set" |
2047 | free(msg); | 2056 | ); |
2048 | } else { | 2057 | } else { |
2049 | die_if_script("%s: parameter null or not set", var); | ||
2050 | } | ||
2051 | else | ||
2052 | val = exp_word; | 2058 | val = exp_word; |
2059 | } | ||
2053 | 2060 | ||
2054 | if (exp_op == '=') { | 2061 | if (exp_op == '=') { |
2055 | /* ${var=[word]} or ${var:=[word]} */ | 2062 | /* ${var=[word]} or ${var:=[word]} */ |
@@ -4487,7 +4494,7 @@ static int add_till_single_quote(o_string *dest, struct in_str *input) | |||
4487 | while (1) { | 4494 | while (1) { |
4488 | int ch = i_getch(input); | 4495 | int ch = i_getch(input); |
4489 | if (ch == EOF) { | 4496 | if (ch == EOF) { |
4490 | syntax_error_unterminated('\''); | 4497 | syntax_error_unterm_ch('\''); |
4491 | return 1; | 4498 | return 1; |
4492 | } | 4499 | } |
4493 | if (ch == '\'') | 4500 | if (ch == '\'') |
@@ -4501,7 +4508,7 @@ static int add_till_double_quote(o_string *dest, struct in_str *input) | |||
4501 | while (1) { | 4508 | while (1) { |
4502 | int ch = i_getch(input); | 4509 | int ch = i_getch(input); |
4503 | if (ch == EOF) { | 4510 | if (ch == EOF) { |
4504 | syntax_error_unterminated('"'); | 4511 | syntax_error_unterm_ch('"'); |
4505 | return 1; | 4512 | return 1; |
4506 | } | 4513 | } |
4507 | if (ch == '"') | 4514 | if (ch == '"') |
@@ -4539,7 +4546,7 @@ static int add_till_backquote(o_string *dest, struct in_str *input) | |||
4539 | while (1) { | 4546 | while (1) { |
4540 | int ch = i_getch(input); | 4547 | int ch = i_getch(input); |
4541 | if (ch == EOF) { | 4548 | if (ch == EOF) { |
4542 | syntax_error_unterminated('`'); | 4549 | syntax_error_unterm_ch('`'); |
4543 | return 1; | 4550 | return 1; |
4544 | } | 4551 | } |
4545 | if (ch == '`') | 4552 | if (ch == '`') |
@@ -4548,7 +4555,7 @@ static int add_till_backquote(o_string *dest, struct in_str *input) | |||
4548 | /* \x. Copy both chars unless it is \` */ | 4555 | /* \x. Copy both chars unless it is \` */ |
4549 | int ch2 = i_getch(input); | 4556 | int ch2 = i_getch(input); |
4550 | if (ch2 == EOF) { | 4557 | if (ch2 == EOF) { |
4551 | syntax_error_unterminated('`'); | 4558 | syntax_error_unterm_ch('`'); |
4552 | return 1; | 4559 | return 1; |
4553 | } | 4560 | } |
4554 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') | 4561 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
@@ -4576,7 +4583,7 @@ static int add_till_closing_paren(o_string *dest, struct in_str *input, bool dbl | |||
4576 | while (1) { | 4583 | while (1) { |
4577 | int ch = i_getch(input); | 4584 | int ch = i_getch(input); |
4578 | if (ch == EOF) { | 4585 | if (ch == EOF) { |
4579 | syntax_error_unterminated(')'); | 4586 | syntax_error_unterm_ch(')'); |
4580 | return 1; | 4587 | return 1; |
4581 | } | 4588 | } |
4582 | if (ch == '(') | 4589 | if (ch == '(') |
@@ -4608,7 +4615,7 @@ static int add_till_closing_paren(o_string *dest, struct in_str *input, bool dbl | |||
4608 | /* \x. Copy verbatim. Important for \(, \) */ | 4615 | /* \x. Copy verbatim. Important for \(, \) */ |
4609 | ch = i_getch(input); | 4616 | ch = i_getch(input); |
4610 | if (ch == EOF) { | 4617 | if (ch == EOF) { |
4611 | syntax_error_unterminated(')'); | 4618 | syntax_error_unterm_ch(')'); |
4612 | return 1; | 4619 | return 1; |
4613 | } | 4620 | } |
4614 | o_addchr(dest, ch); | 4621 | o_addchr(dest, ch); |
@@ -4726,7 +4733,7 @@ static int handle_dollar(o_string *as_string, | |||
4726 | break; | 4733 | break; |
4727 | default: | 4734 | default: |
4728 | case_default: | 4735 | case_default: |
4729 | syntax_error("unterminated ${name}"); | 4736 | syntax_error_unterm_str("${name}"); |
4730 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); | 4737 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); |
4731 | return 1; | 4738 | return 1; |
4732 | } | 4739 | } |
@@ -4831,7 +4838,7 @@ static int parse_stream_dquoted(o_string *as_string, | |||
4831 | } | 4838 | } |
4832 | /* note: can't move it above ch == dquote_end check! */ | 4839 | /* note: can't move it above ch == dquote_end check! */ |
4833 | if (ch == EOF) { | 4840 | if (ch == EOF) { |
4834 | syntax_error_unterminated('"'); | 4841 | syntax_error_unterm_ch('"'); |
4835 | debug_printf_parse("parse_stream_dquoted return 1: unterminated \"\n"); | 4842 | debug_printf_parse("parse_stream_dquoted return 1: unterminated \"\n"); |
4836 | return 1; | 4843 | return 1; |
4837 | } | 4844 | } |
@@ -4954,7 +4961,7 @@ static struct pipe *parse_stream(char **pstring, | |||
4954 | struct pipe *pi; | 4961 | struct pipe *pi; |
4955 | 4962 | ||
4956 | if (heredoc_cnt) { | 4963 | if (heredoc_cnt) { |
4957 | syntax_error("unterminated here document"); | 4964 | syntax_error_unterm_str("here document"); |
4958 | goto parse_error; | 4965 | goto parse_error; |
4959 | } | 4966 | } |
4960 | if (done_word(&dest, &ctx)) { | 4967 | if (done_word(&dest, &ctx)) { |
@@ -5043,7 +5050,7 @@ static struct pipe *parse_stream(char **pstring, | |||
5043 | * We require heredoc to be in enclosing {}/(), | 5050 | * We require heredoc to be in enclosing {}/(), |
5044 | * if any. | 5051 | * if any. |
5045 | */ | 5052 | */ |
5046 | syntax_error("unterminated here document"); | 5053 | syntax_error_unterm_str("here document"); |
5047 | goto parse_error; | 5054 | goto parse_error; |
5048 | } | 5055 | } |
5049 | if (done_word(&dest, &ctx)) { | 5056 | if (done_word(&dest, &ctx)) { |
@@ -5123,7 +5130,7 @@ static struct pipe *parse_stream(char **pstring, | |||
5123 | while (1) { | 5130 | while (1) { |
5124 | ch = i_getch(input); | 5131 | ch = i_getch(input); |
5125 | if (ch == EOF) { | 5132 | if (ch == EOF) { |
5126 | syntax_error_unterminated('\''); | 5133 | syntax_error_unterm_ch('\''); |
5127 | goto parse_error; | 5134 | goto parse_error; |
5128 | } | 5135 | } |
5129 | nommu_addchr(&ctx.as_string, ch); | 5136 | nommu_addchr(&ctx.as_string, ch); |