diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2009-11-30 01:14:16 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2009-11-30 01:14:16 +0100 |
commit | cdeda16ee4acb78569598e848a491ad595af3abb (patch) | |
tree | ad65f2a91520e7235f0686b5aec49e263ebcdc08 | |
parent | dcbfaba264df2f9f07e53f77e8178f5bfc7ae88e (diff) | |
download | busybox-w32-cdeda16ee4acb78569598e848a491ad595af3abb.tar.gz busybox-w32-cdeda16ee4acb78569598e848a491ad595af3abb.tar.bz2 busybox-w32-cdeda16ee4acb78569598e848a491ad595af3abb.zip |
awk: preparatory patch, no essential code changes. -13 bytes.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | editors/awk.c | 73 |
1 files changed, 43 insertions, 30 deletions
diff --git a/editors/awk.c b/editors/awk.c index ce6c9b726..e987bc868 100644 --- a/editors/awk.c +++ b/editors/awk.c | |||
@@ -516,7 +516,7 @@ static const char EMSG_UNDEF_FUNC[] ALIGN1 = "Call to undefined function"; | |||
516 | static const char EMSG_NO_MATH[] ALIGN1 = "Math support is not compiled in"; | 516 | static const char EMSG_NO_MATH[] ALIGN1 = "Math support is not compiled in"; |
517 | #endif | 517 | #endif |
518 | 518 | ||
519 | static void zero_out_var(var * vp) | 519 | static void zero_out_var(var *vp) |
520 | { | 520 | { |
521 | memset(vp, 0, sizeof(*vp)); | 521 | memset(vp, 0, sizeof(*vp)); |
522 | } | 522 | } |
@@ -533,7 +533,8 @@ static unsigned hashidx(const char *name) | |||
533 | { | 533 | { |
534 | unsigned idx = 0; | 534 | unsigned idx = 0; |
535 | 535 | ||
536 | while (*name) idx = *name++ + (idx << 6) - idx; | 536 | while (*name) |
537 | idx = *name++ + (idx << 6) - idx; | ||
537 | return idx; | 538 | return idx; |
538 | } | 539 | } |
539 | 540 | ||
@@ -542,9 +543,9 @@ static xhash *hash_init(void) | |||
542 | { | 543 | { |
543 | xhash *newhash; | 544 | xhash *newhash; |
544 | 545 | ||
545 | newhash = xzalloc(sizeof(xhash)); | 546 | newhash = xzalloc(sizeof(*newhash)); |
546 | newhash->csize = FIRST_PRIME; | 547 | newhash->csize = FIRST_PRIME; |
547 | newhash->items = xzalloc(newhash->csize * sizeof(hash_item *)); | 548 | newhash->items = xzalloc(FIRST_PRIME * sizeof(newhash->items[0])); |
548 | 549 | ||
549 | return newhash; | 550 | return newhash; |
550 | } | 551 | } |
@@ -554,7 +555,7 @@ static void *hash_search(xhash *hash, const char *name) | |||
554 | { | 555 | { |
555 | hash_item *hi; | 556 | hash_item *hi; |
556 | 557 | ||
557 | hi = hash->items [ hashidx(name) % hash->csize ]; | 558 | hi = hash->items[hashidx(name) % hash->csize]; |
558 | while (hi) { | 559 | while (hi) { |
559 | if (strcmp(hi->name, name) == 0) | 560 | if (strcmp(hi->name, name) == 0) |
560 | return &(hi->data); | 561 | return &(hi->data); |
@@ -573,7 +574,7 @@ static void hash_rebuild(xhash *hash) | |||
573 | return; | 574 | return; |
574 | 575 | ||
575 | newsize = PRIMES[hash->nprime++]; | 576 | newsize = PRIMES[hash->nprime++]; |
576 | newitems = xzalloc(newsize * sizeof(hash_item *)); | 577 | newitems = xzalloc(newsize * sizeof(newitems[0])); |
577 | 578 | ||
578 | for (i = 0; i < hash->csize; i++) { | 579 | for (i = 0; i < hash->csize; i++) { |
579 | hi = hash->items[i]; | 580 | hi = hash->items[i]; |
@@ -659,9 +660,8 @@ static void skip_spaces(char **s) | |||
659 | static char *nextword(char **s) | 660 | static char *nextword(char **s) |
660 | { | 661 | { |
661 | char *p = *s; | 662 | char *p = *s; |
662 | 663 | while (*(*s)++) | |
663 | while (*(*s)++) /* */; | 664 | continue; |
664 | |||
665 | return p; | 665 | return p; |
666 | } | 666 | } |
667 | 667 | ||
@@ -671,8 +671,10 @@ static char nextchar(char **s) | |||
671 | 671 | ||
672 | c = *((*s)++); | 672 | c = *((*s)++); |
673 | pps = *s; | 673 | pps = *s; |
674 | if (c == '\\') c = bb_process_escape_sequence((const char**)s); | 674 | if (c == '\\') |
675 | if (c == '\\' && *s == pps) c = *((*s)++); | 675 | c = bb_process_escape_sequence((const char**)s); |
676 | if (c == '\\' && *s == pps) | ||
677 | c = *((*s)++); | ||
676 | return c; | 678 | return c; |
677 | } | 679 | } |
678 | 680 | ||
@@ -754,10 +756,10 @@ static var *setvar_s(var *v, const char *value) | |||
754 | return setvar_p(v, (value && *value) ? xstrdup(value) : NULL); | 756 | return setvar_p(v, (value && *value) ? xstrdup(value) : NULL); |
755 | } | 757 | } |
756 | 758 | ||
757 | /* same as setvar_s but set USER flag */ | 759 | /* same as setvar_s but sets USER flag */ |
758 | static var *setvar_u(var *v, const char *value) | 760 | static var *setvar_u(var *v, const char *value) |
759 | { | 761 | { |
760 | setvar_s(v, value); | 762 | v = setvar_s(v, value); |
761 | v->type |= VF_USER; | 763 | v->type |= VF_USER; |
762 | return v; | 764 | return v; |
763 | } | 765 | } |
@@ -842,7 +844,7 @@ static var *copyvar(var *dest, const var *src) | |||
842 | 844 | ||
843 | static var *incvar(var *v) | 845 | static var *incvar(var *v) |
844 | { | 846 | { |
845 | return setvar_i(v, getvar_i(v) + 1.); | 847 | return setvar_i(v, getvar_i(v) + 1.0); |
846 | } | 848 | } |
847 | 849 | ||
848 | /* return true if v is number or numeric string */ | 850 | /* return true if v is number or numeric string */ |
@@ -856,8 +858,8 @@ static int is_numeric(var *v) | |||
856 | static int istrue(var *v) | 858 | static int istrue(var *v) |
857 | { | 859 | { |
858 | if (is_numeric(v)) | 860 | if (is_numeric(v)) |
859 | return (v->number == 0) ? 0 : 1; | 861 | return (v->number != 0); |
860 | return (v->string && *(v->string)) ? 1 : 0; | 862 | return (v->string && v->string[0]); |
861 | } | 863 | } |
862 | 864 | ||
863 | /* temporary variables allocator. Last allocated should be first freed */ | 865 | /* temporary variables allocator. Last allocated should be first freed */ |
@@ -869,7 +871,8 @@ static var *nvalloc(int n) | |||
869 | 871 | ||
870 | while (g_cb) { | 872 | while (g_cb) { |
871 | pb = g_cb; | 873 | pb = g_cb; |
872 | if ((g_cb->pos - g_cb->nv) + n <= g_cb->size) break; | 874 | if ((g_cb->pos - g_cb->nv) + n <= g_cb->size) |
875 | break; | ||
873 | g_cb = g_cb->next; | 876 | g_cb = g_cb->next; |
874 | } | 877 | } |
875 | 878 | ||
@@ -880,7 +883,8 @@ static var *nvalloc(int n) | |||
880 | g_cb->pos = g_cb->nv; | 883 | g_cb->pos = g_cb->nv; |
881 | g_cb->prev = pb; | 884 | g_cb->prev = pb; |
882 | /*g_cb->next = NULL; - xzalloc did it */ | 885 | /*g_cb->next = NULL; - xzalloc did it */ |
883 | if (pb) pb->next = g_cb; | 886 | if (pb) |
887 | pb->next = g_cb; | ||
884 | } | 888 | } |
885 | 889 | ||
886 | v = r = g_cb->pos; | 890 | v = r = g_cb->pos; |
@@ -1143,9 +1147,11 @@ static node *parse_expr(uint32_t iexp) | |||
1143 | /* for binary and postfix-unary operators, jump back over | 1147 | /* for binary and postfix-unary operators, jump back over |
1144 | * previous operators with higher priority */ | 1148 | * previous operators with higher priority */ |
1145 | vn = cn; | 1149 | vn = cn; |
1146 | while ( ((t_info & PRIMASK) > (vn->a.n->info & PRIMASK2)) | 1150 | while (((t_info & PRIMASK) > (vn->a.n->info & PRIMASK2)) |
1147 | || ((t_info == vn->info) && ((t_info & OPCLSMASK) == OC_COLON)) ) | 1151 | || ((t_info == vn->info) && ((t_info & OPCLSMASK) == OC_COLON)) |
1152 | ) { | ||
1148 | vn = vn->a.n; | 1153 | vn = vn->a.n; |
1154 | } | ||
1149 | if ((t_info & OPCLSMASK) == OC_TERNARY) | 1155 | if ((t_info & OPCLSMASK) == OC_TERNARY) |
1150 | t_info += P(6); | 1156 | t_info += P(6); |
1151 | cn = vn->a.n->r.n = new_node(t_info); | 1157 | cn = vn->a.n->r.n = new_node(t_info); |
@@ -1985,17 +1991,20 @@ static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int | |||
1985 | } | 1991 | } |
1986 | 1992 | ||
1987 | sp += eo; | 1993 | sp += eo; |
1988 | if (i == nm) break; | 1994 | if (i == nm) |
1995 | break; | ||
1989 | if (eo == so) { | 1996 | if (eo == so) { |
1990 | ds[di] = *sp++; | 1997 | ds[di] = *sp++; |
1991 | if (!ds[di++]) break; | 1998 | if (!ds[di++]) |
1999 | break; | ||
1992 | } | 2000 | } |
1993 | } | 2001 | } |
1994 | 2002 | ||
1995 | qrealloc(&ds, di + strlen(sp), &dssize); | 2003 | qrealloc(&ds, di + strlen(sp), &dssize); |
1996 | strcpy(ds + di, sp); | 2004 | strcpy(ds + di, sp); |
1997 | setvar_p(dest, ds); | 2005 | setvar_p(dest, ds); |
1998 | if (re == &sreg) regfree(re); | 2006 | if (re == &sreg) |
2007 | regfree(re); | ||
1999 | return i; | 2008 | return i; |
2000 | } | 2009 | } |
2001 | 2010 | ||
@@ -2438,7 +2447,7 @@ static var *evaluate(node *op, var *res) | |||
2438 | if (!op->r.f->body.first) | 2447 | if (!op->r.f->body.first) |
2439 | syntax_error(EMSG_UNDEF_FUNC); | 2448 | syntax_error(EMSG_UNDEF_FUNC); |
2440 | 2449 | ||
2441 | X.v = R.v = nvalloc(op->r.f->nargs+1); | 2450 | X.v = R.v = nvalloc(op->r.f->nargs + 1); |
2442 | while (op1) { | 2451 | while (op1) { |
2443 | L.v = evaluate(nextarg(&op1), v1); | 2452 | L.v = evaluate(nextarg(&op1), v1); |
2444 | copyvar(R.v, L.v); | 2453 | copyvar(R.v, L.v); |
@@ -2610,7 +2619,7 @@ static var *evaluate(node *op, var *res) | |||
2610 | R.d--; | 2619 | R.d--; |
2611 | goto r_op_change; | 2620 | goto r_op_change; |
2612 | case '!': | 2621 | case '!': |
2613 | L.d = istrue(X.v) ? 0 : 1; | 2622 | L.d = !istrue(X.v); |
2614 | break; | 2623 | break; |
2615 | case '-': | 2624 | case '-': |
2616 | L.d = -R.d; | 2625 | L.d = -R.d; |
@@ -2670,7 +2679,8 @@ static var *evaluate(node *op, var *res) | |||
2670 | L.d *= R.d; | 2679 | L.d *= R.d; |
2671 | break; | 2680 | break; |
2672 | case '/': | 2681 | case '/': |
2673 | if (R.d == 0) syntax_error(EMSG_DIV_BY_ZERO); | 2682 | if (R.d == 0) |
2683 | syntax_error(EMSG_DIV_BY_ZERO); | ||
2674 | L.d /= R.d; | 2684 | L.d /= R.d; |
2675 | break; | 2685 | break; |
2676 | case '&': | 2686 | case '&': |
@@ -2681,7 +2691,8 @@ static var *evaluate(node *op, var *res) | |||
2681 | #endif | 2691 | #endif |
2682 | break; | 2692 | break; |
2683 | case '%': | 2693 | case '%': |
2684 | if (R.d == 0) syntax_error(EMSG_DIV_BY_ZERO); | 2694 | if (R.d == 0) |
2695 | syntax_error(EMSG_DIV_BY_ZERO); | ||
2685 | L.d -= (int)(L.d / R.d) * R.d; | 2696 | L.d -= (int)(L.d / R.d) * R.d; |
2686 | break; | 2697 | break; |
2687 | } | 2698 | } |
@@ -2707,7 +2718,7 @@ static var *evaluate(node *op, var *res) | |||
2707 | R.i = (L.d == 0); | 2718 | R.i = (L.d == 0); |
2708 | break; | 2719 | break; |
2709 | } | 2720 | } |
2710 | setvar_i(res, (opn & 0x1 ? R.i : !R.i) ? 1 : 0); | 2721 | setvar_i(res, (opn & 1 ? R.i : !R.i) ? 1 : 0); |
2711 | break; | 2722 | break; |
2712 | 2723 | ||
2713 | default: | 2724 | default: |
@@ -2789,7 +2800,8 @@ static rstream *next_input_file(void) | |||
2789 | FILE *F = NULL; | 2800 | FILE *F = NULL; |
2790 | const char *fname, *ind; | 2801 | const char *fname, *ind; |
2791 | 2802 | ||
2792 | if (rsm.F) fclose(rsm.F); | 2803 | if (rsm.F) |
2804 | fclose(rsm.F); | ||
2793 | rsm.F = NULL; | 2805 | rsm.F = NULL; |
2794 | rsm.pos = rsm.adv = 0; | 2806 | rsm.pos = rsm.adv = 0; |
2795 | 2807 | ||
@@ -2929,7 +2941,8 @@ int awk_main(int argc, char **argv) | |||
2929 | awk_exit(EXIT_SUCCESS); | 2941 | awk_exit(EXIT_SUCCESS); |
2930 | 2942 | ||
2931 | /* input file could already be opened in BEGIN block */ | 2943 | /* input file could already be opened in BEGIN block */ |
2932 | if (!iF) iF = next_input_file(); | 2944 | if (!iF) |
2945 | iF = next_input_file(); | ||
2933 | 2946 | ||
2934 | /* passing through input files */ | 2947 | /* passing through input files */ |
2935 | while (iF) { | 2948 | while (iF) { |