diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2021-07-02 14:27:40 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-07-02 14:27:40 +0200 |
commit | 40573556f2a67b11319785e0479b7087d02c060e (patch) | |
tree | 5fd59daf789789a6e90724368ddc87e0f301da5a | |
parent | 8b4c429025c233640bd5c5838552f34683a06fc0 (diff) | |
download | busybox-w32-40573556f2a67b11319785e0479b7087d02c060e.tar.gz busybox-w32-40573556f2a67b11319785e0479b7087d02c060e.tar.bz2 busybox-w32-40573556f2a67b11319785e0479b7087d02c060e.zip |
awk: shuffle functions to reduce forward declarations, no code changes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | editors/awk.c | 192 |
1 files changed, 94 insertions, 98 deletions
diff --git a/editors/awk.c b/editors/awk.c index 0be044eef..6833c2f0d 100644 --- a/editors/awk.c +++ b/editors/awk.c | |||
@@ -619,18 +619,6 @@ struct globals2 { | |||
619 | G.evaluate__seed = 1; \ | 619 | G.evaluate__seed = 1; \ |
620 | } while (0) | 620 | } while (0) |
621 | 621 | ||
622 | |||
623 | /* function prototypes */ | ||
624 | static void handle_special(var *); | ||
625 | static node *parse_expr(uint32_t); | ||
626 | static void chain_group(void); | ||
627 | static var *evaluate(node *, var *); | ||
628 | static rstream *next_input_file(void); | ||
629 | static int fmt_num(char *, int, const char *, double, int); | ||
630 | static int awk_exit(int) NORETURN; | ||
631 | |||
632 | /* ---- error handling ---- */ | ||
633 | |||
634 | static const char EMSG_UNEXP_EOS[] ALIGN1 = "Unexpected end of string"; | 622 | static const char EMSG_UNEXP_EOS[] ALIGN1 = "Unexpected end of string"; |
635 | static const char EMSG_UNEXP_TOKEN[] ALIGN1 = "Unexpected token"; | 623 | static const char EMSG_UNEXP_TOKEN[] ALIGN1 = "Unexpected token"; |
636 | static const char EMSG_DIV_BY_ZERO[] ALIGN1 = "Division by zero"; | 624 | static const char EMSG_DIV_BY_ZERO[] ALIGN1 = "Division by zero"; |
@@ -642,10 +630,7 @@ static const char EMSG_UNDEF_FUNC[] ALIGN1 = "Call to undefined function"; | |||
642 | static const char EMSG_NO_MATH[] ALIGN1 = "Math support is not compiled in"; | 630 | static const char EMSG_NO_MATH[] ALIGN1 = "Math support is not compiled in"; |
643 | static const char EMSG_NEGATIVE_FIELD[] ALIGN1 = "Access to negative field"; | 631 | static const char EMSG_NEGATIVE_FIELD[] ALIGN1 = "Access to negative field"; |
644 | 632 | ||
645 | static void zero_out_var(var *vp) | 633 | static int awk_exit(int) NORETURN; |
646 | { | ||
647 | memset(vp, 0, sizeof(*vp)); | ||
648 | } | ||
649 | 634 | ||
650 | static void syntax_error(const char *message) NORETURN; | 635 | static void syntax_error(const char *message) NORETURN; |
651 | static void syntax_error(const char *message) | 636 | static void syntax_error(const char *message) |
@@ -653,6 +638,11 @@ static void syntax_error(const char *message) | |||
653 | bb_error_msg_and_die("%s:%i: %s", g_progname, g_lineno, message); | 638 | bb_error_msg_and_die("%s:%i: %s", g_progname, g_lineno, message); |
654 | } | 639 | } |
655 | 640 | ||
641 | static void zero_out_var(var *vp) | ||
642 | { | ||
643 | memset(vp, 0, sizeof(*vp)); | ||
644 | } | ||
645 | |||
656 | /* ---- hash stuff ---- */ | 646 | /* ---- hash stuff ---- */ |
657 | 647 | ||
658 | static unsigned hashidx(const char *name) | 648 | static unsigned hashidx(const char *name) |
@@ -885,10 +875,29 @@ static double my_strtod(char **pp) | |||
885 | 875 | ||
886 | /* -------- working with variables (set/get/copy/etc) -------- */ | 876 | /* -------- working with variables (set/get/copy/etc) -------- */ |
887 | 877 | ||
888 | static xhash *iamarray(var *v) | 878 | static int fmt_num(char *b, int size, const char *format, double n, int int_as_int) |
889 | { | 879 | { |
890 | var *a = v; | 880 | int r = 0; |
881 | char c; | ||
882 | const char *s = format; | ||
883 | |||
884 | if (int_as_int && n == (long long)n) { | ||
885 | r = snprintf(b, size, "%lld", (long long)n); | ||
886 | } else { | ||
887 | do { c = *s; } while (c && *++s); | ||
888 | if (strchr("diouxX", c)) { | ||
889 | r = snprintf(b, size, format, (int)n); | ||
890 | } else if (strchr("eEfgG", c)) { | ||
891 | r = snprintf(b, size, format, n); | ||
892 | } else { | ||
893 | syntax_error(EMSG_INV_FMT); | ||
894 | } | ||
895 | } | ||
896 | return r; | ||
897 | } | ||
891 | 898 | ||
899 | static xhash *iamarray(var *a) | ||
900 | { | ||
892 | while (a->type & VF_CHILD) | 901 | while (a->type & VF_CHILD) |
893 | a = a->x.parent; | 902 | a = a->x.parent; |
894 | 903 | ||
@@ -913,6 +922,8 @@ static var *clrvar(var *v) | |||
913 | return v; | 922 | return v; |
914 | } | 923 | } |
915 | 924 | ||
925 | static void handle_special(var *); | ||
926 | |||
916 | /* assign string value to variable */ | 927 | /* assign string value to variable */ |
917 | static var *setvar_p(var *v, char *value) | 928 | static var *setvar_p(var *v, char *value) |
918 | { | 929 | { |
@@ -1284,6 +1295,8 @@ static void mk_re_node(const char *s, node *n, regex_t *re) | |||
1284 | xregcomp(re + 1, s, REG_EXTENDED | REG_ICASE); | 1295 | xregcomp(re + 1, s, REG_EXTENDED | REG_ICASE); |
1285 | } | 1296 | } |
1286 | 1297 | ||
1298 | static node *parse_expr(uint32_t); | ||
1299 | |||
1287 | static node *parse_lrparen_list(void) | 1300 | static node *parse_lrparen_list(void) |
1288 | { | 1301 | { |
1289 | next_token(TC_LPAREN); | 1302 | next_token(TC_LPAREN); |
@@ -1488,6 +1501,8 @@ static void chain_expr(uint32_t info) | |||
1488 | rollback_token(); | 1501 | rollback_token(); |
1489 | } | 1502 | } |
1490 | 1503 | ||
1504 | static void chain_group(void); | ||
1505 | |||
1491 | static node *chain_loop(node *nn) | 1506 | static node *chain_loop(node *nn) |
1492 | { | 1507 | { |
1493 | node *n, *n2, *save_brk, *save_cont; | 1508 | node *n, *n2, *save_brk, *save_cont; |
@@ -1770,6 +1785,8 @@ static node *mk_splitter(const char *s, tsplitter *spl) | |||
1770 | return n; | 1785 | return n; |
1771 | } | 1786 | } |
1772 | 1787 | ||
1788 | static var *evaluate(node *, var *); | ||
1789 | |||
1773 | /* Use node as a regular expression. Supplied with node ptr and regex_t | 1790 | /* Use node as a regular expression. Supplied with node ptr and regex_t |
1774 | * storage space. Return ptr to regex (if result points to preg, it should | 1791 | * storage space. Return ptr to regex (if result points to preg, it should |
1775 | * be later regfree'd manually). | 1792 | * be later regfree'd manually). |
@@ -2222,27 +2239,6 @@ static int awk_getline(rstream *rsm, var *v) | |||
2222 | return r; | 2239 | return r; |
2223 | } | 2240 | } |
2224 | 2241 | ||
2225 | static int fmt_num(char *b, int size, const char *format, double n, int int_as_int) | ||
2226 | { | ||
2227 | int r = 0; | ||
2228 | char c; | ||
2229 | const char *s = format; | ||
2230 | |||
2231 | if (int_as_int && n == (long long)n) { | ||
2232 | r = snprintf(b, size, "%lld", (long long)n); | ||
2233 | } else { | ||
2234 | do { c = *s; } while (c && *++s); | ||
2235 | if (strchr("diouxX", c)) { | ||
2236 | r = snprintf(b, size, format, (int)n); | ||
2237 | } else if (strchr("eEfgG", c)) { | ||
2238 | r = snprintf(b, size, format, n); | ||
2239 | } else { | ||
2240 | syntax_error(EMSG_INV_FMT); | ||
2241 | } | ||
2242 | } | ||
2243 | return r; | ||
2244 | } | ||
2245 | |||
2246 | /* formatted output into an allocated buffer, return ptr to buffer */ | 2242 | /* formatted output into an allocated buffer, return ptr to buffer */ |
2247 | #if !ENABLE_FEATURE_AWK_GNU_EXTENSIONS | 2243 | #if !ENABLE_FEATURE_AWK_GNU_EXTENSIONS |
2248 | # define awk_printf(a, b) awk_printf(a) | 2244 | # define awk_printf(a, b) awk_printf(a) |
@@ -2306,7 +2302,7 @@ static char *awk_printf(node *n, int *len) | |||
2306 | } | 2302 | } |
2307 | 2303 | ||
2308 | free(fmt); | 2304 | free(fmt); |
2309 | // nvfree(tmpvar, 1); | 2305 | //nvfree(tmpvar, 1); |
2310 | #undef TMPVAR | 2306 | #undef TMPVAR |
2311 | 2307 | ||
2312 | b = xrealloc(b, i + 1); | 2308 | b = xrealloc(b, i + 1); |
@@ -2652,6 +2648,64 @@ static NOINLINE var *exec_builtin(node *op, var *res) | |||
2652 | #undef tspl | 2648 | #undef tspl |
2653 | } | 2649 | } |
2654 | 2650 | ||
2651 | /* if expr looks like "var=value", perform assignment and return 1, | ||
2652 | * otherwise return 0 */ | ||
2653 | static int is_assignment(const char *expr) | ||
2654 | { | ||
2655 | char *exprc, *val; | ||
2656 | |||
2657 | if (!isalnum_(*expr) || (val = strchr(expr, '=')) == NULL) { | ||
2658 | return FALSE; | ||
2659 | } | ||
2660 | |||
2661 | exprc = xstrdup(expr); | ||
2662 | val = exprc + (val - expr); | ||
2663 | *val++ = '\0'; | ||
2664 | |||
2665 | unescape_string_in_place(val); | ||
2666 | setvar_u(newvar(exprc), val); | ||
2667 | free(exprc); | ||
2668 | return TRUE; | ||
2669 | } | ||
2670 | |||
2671 | /* switch to next input file */ | ||
2672 | static rstream *next_input_file(void) | ||
2673 | { | ||
2674 | #define rsm (G.next_input_file__rsm) | ||
2675 | #define files_happen (G.next_input_file__files_happen) | ||
2676 | |||
2677 | FILE *F; | ||
2678 | const char *fname, *ind; | ||
2679 | |||
2680 | if (rsm.F) | ||
2681 | fclose(rsm.F); | ||
2682 | rsm.F = NULL; | ||
2683 | rsm.pos = rsm.adv = 0; | ||
2684 | |||
2685 | for (;;) { | ||
2686 | if (getvar_i(intvar[ARGIND])+1 >= getvar_i(intvar[ARGC])) { | ||
2687 | if (files_happen) | ||
2688 | return NULL; | ||
2689 | fname = "-"; | ||
2690 | F = stdin; | ||
2691 | break; | ||
2692 | } | ||
2693 | ind = getvar_s(incvar(intvar[ARGIND])); | ||
2694 | fname = getvar_s(findvar(iamarray(intvar[ARGV]), ind)); | ||
2695 | if (fname && *fname && !is_assignment(fname)) { | ||
2696 | F = xfopen_stdin(fname); | ||
2697 | break; | ||
2698 | } | ||
2699 | } | ||
2700 | |||
2701 | files_happen = TRUE; | ||
2702 | setvar_s(intvar[FILENAME], fname); | ||
2703 | rsm.F = F; | ||
2704 | return &rsm; | ||
2705 | #undef rsm | ||
2706 | #undef files_happen | ||
2707 | } | ||
2708 | |||
2655 | /* | 2709 | /* |
2656 | * Evaluate node - the heart of the program. Supplied with subtree | 2710 | * Evaluate node - the heart of the program. Supplied with subtree |
2657 | * and place where to store result. Returns ptr to result. | 2711 | * and place where to store result. Returns ptr to result. |
@@ -3338,64 +3392,6 @@ static int awk_exit(int r) | |||
3338 | exit(r); | 3392 | exit(r); |
3339 | } | 3393 | } |
3340 | 3394 | ||
3341 | /* if expr looks like "var=value", perform assignment and return 1, | ||
3342 | * otherwise return 0 */ | ||
3343 | static int is_assignment(const char *expr) | ||
3344 | { | ||
3345 | char *exprc, *val; | ||
3346 | |||
3347 | if (!isalnum_(*expr) || (val = strchr(expr, '=')) == NULL) { | ||
3348 | return FALSE; | ||
3349 | } | ||
3350 | |||
3351 | exprc = xstrdup(expr); | ||
3352 | val = exprc + (val - expr); | ||
3353 | *val++ = '\0'; | ||
3354 | |||
3355 | unescape_string_in_place(val); | ||
3356 | setvar_u(newvar(exprc), val); | ||
3357 | free(exprc); | ||
3358 | return TRUE; | ||
3359 | } | ||
3360 | |||
3361 | /* switch to next input file */ | ||
3362 | static rstream *next_input_file(void) | ||
3363 | { | ||
3364 | #define rsm (G.next_input_file__rsm) | ||
3365 | #define files_happen (G.next_input_file__files_happen) | ||
3366 | |||
3367 | FILE *F; | ||
3368 | const char *fname, *ind; | ||
3369 | |||
3370 | if (rsm.F) | ||
3371 | fclose(rsm.F); | ||
3372 | rsm.F = NULL; | ||
3373 | rsm.pos = rsm.adv = 0; | ||
3374 | |||
3375 | for (;;) { | ||
3376 | if (getvar_i(intvar[ARGIND])+1 >= getvar_i(intvar[ARGC])) { | ||
3377 | if (files_happen) | ||
3378 | return NULL; | ||
3379 | fname = "-"; | ||
3380 | F = stdin; | ||
3381 | break; | ||
3382 | } | ||
3383 | ind = getvar_s(incvar(intvar[ARGIND])); | ||
3384 | fname = getvar_s(findvar(iamarray(intvar[ARGV]), ind)); | ||
3385 | if (fname && *fname && !is_assignment(fname)) { | ||
3386 | F = xfopen_stdin(fname); | ||
3387 | break; | ||
3388 | } | ||
3389 | } | ||
3390 | |||
3391 | files_happen = TRUE; | ||
3392 | setvar_s(intvar[FILENAME], fname); | ||
3393 | rsm.F = F; | ||
3394 | return &rsm; | ||
3395 | #undef rsm | ||
3396 | #undef files_happen | ||
3397 | } | ||
3398 | |||
3399 | int awk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 3395 | int awk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
3400 | int awk_main(int argc UNUSED_PARAM, char **argv) | 3396 | int awk_main(int argc UNUSED_PARAM, char **argv) |
3401 | { | 3397 | { |