diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-06-06 17:01:00 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-06-06 17:01:00 +0000 |
| commit | ae5a8aac26d0fdd77218bb2ce336aa5f5ce82f9d (patch) | |
| tree | e60be1a0e110d178a3ac36b3bf478e2b656de7e4 | |
| parent | c084d2f205469358d594e89365e3e184f416f005 (diff) | |
| download | busybox-w32-ae5a8aac26d0fdd77218bb2ce336aa5f5ce82f9d.tar.gz busybox-w32-ae5a8aac26d0fdd77218bb2ce336aa5f5ce82f9d.tar.bz2 busybox-w32-ae5a8aac26d0fdd77218bb2ce336aa5f5ce82f9d.zip | |
awk: move all data to malloc space
function old new delta
evaluate 6448 6728 +280
awk_getline 676 705 +29
parse_expr 726 752 +26
next_token 917 943 +26
next_input_file 237 252 +15
awk_split 498 510 +12
awk_sub 632 643 +11
split_f0 160 170 +10
getvar_s 98 108 +10
...
chain_loop 128 121 -7
nvalloc 179 171 -8
chain_node 107 99 -8
mainseq 12 - -12
endseq 12 - -12
chain_group 640 628 -12
beginseq 12 - -12
awk_exit 112 100 -12
fsrealloc 127 110 -17
static.v 20 - -20
static.rsm 24 - -24
ttt 28 - -28
parse_program 339 311 -28
static.sreg 32 - -32
intvar 76 - -76
static.tspl 84 - -84
rsplitter 84 - -84
fsplitter 84 - -84
------------------------------------------------------------------------------
(add/remove: 0/39 grow/shrink: 16/11 up/down: 439/-685) Total: -246 bytes
| -rw-r--r-- | coreutils/diff.c | 3 | ||||
| -rw-r--r-- | editors/awk.c | 390 |
2 files changed, 234 insertions, 159 deletions
diff --git a/coreutils/diff.c b/coreutils/diff.c index faab287f8..b39a301b9 100644 --- a/coreutils/diff.c +++ b/coreutils/diff.c | |||
| @@ -155,8 +155,6 @@ struct globals { | |||
| 155 | } while (0) | 155 | } while (0) |
| 156 | 156 | ||
| 157 | 157 | ||
| 158 | |||
| 159 | |||
| 160 | static void print_only(const char *path, size_t dirlen, const char *entry) | 158 | static void print_only(const char *path, size_t dirlen, const char *entry) |
| 161 | { | 159 | { |
| 162 | if (dirlen > 1) | 160 | if (dirlen > 1) |
| @@ -164,7 +162,6 @@ static void print_only(const char *path, size_t dirlen, const char *entry) | |||
| 164 | printf("Only in %.*s: %s\n", (int) dirlen, path, entry); | 162 | printf("Only in %.*s: %s\n", (int) dirlen, path, entry); |
| 165 | } | 163 | } |
| 166 | 164 | ||
| 167 | |||
| 168 | static void print_status(int val, char *path1, char *path2, char *entry) | 165 | static void print_status(int val, char *path1, char *path2, char *entry) |
| 169 | { | 166 | { |
| 170 | const char * const _entry = entry ? entry : ""; | 167 | const char * const _entry = entry ? entry : ""; |
diff --git a/editors/awk.c b/editors/awk.c index ea326ecdf..b5bab16af 100644 --- a/editors/awk.c +++ b/editors/awk.c | |||
| @@ -35,7 +35,7 @@ extern char **environ; | |||
| 35 | 35 | ||
| 36 | /* Variable */ | 36 | /* Variable */ |
| 37 | typedef struct var_s { | 37 | typedef struct var_s { |
| 38 | unsigned short type; /* flags */ | 38 | unsigned type; /* flags */ |
| 39 | double number; | 39 | double number; |
| 40 | char *string; | 40 | char *string; |
| 41 | union { | 41 | union { |
| @@ -55,7 +55,7 @@ typedef struct chain_s { | |||
| 55 | 55 | ||
| 56 | /* Function */ | 56 | /* Function */ |
| 57 | typedef struct func_s { | 57 | typedef struct func_s { |
| 58 | unsigned short nargs; | 58 | unsigned nargs; |
| 59 | struct chain_s body; | 59 | struct chain_s body; |
| 60 | } func; | 60 | } func; |
| 61 | 61 | ||
| @@ -66,7 +66,7 @@ typedef struct rstream_s { | |||
| 66 | int adv; | 66 | int adv; |
| 67 | int size; | 67 | int size; |
| 68 | int pos; | 68 | int pos; |
| 69 | unsigned short is_pipe; | 69 | smallint is_pipe; |
| 70 | } rstream; | 70 | } rstream; |
| 71 | 71 | ||
| 72 | typedef struct hash_item_s { | 72 | typedef struct hash_item_s { |
| @@ -394,34 +394,101 @@ enum { NPRIMES = sizeof(PRIMES) / sizeof(PRIMES[0]) }; | |||
| 394 | 394 | ||
| 395 | /* globals */ | 395 | /* globals */ |
| 396 | 396 | ||
| 397 | static var *intvar[NUM_INTERNAL_VARS]; | 397 | struct globals { |
| 398 | static chain beginseq, mainseq, endseq, *seq; | 398 | /* former 'struct t' */ |
| 399 | static int nextrec, nextfile; | 399 | uint32_t t_info; /* often used */ |
| 400 | static node *break_ptr, *continue_ptr; | 400 | uint32_t t_tclass; |
| 401 | static rstream *iF; | 401 | char *t_string; |
| 402 | static xhash *vhash, *ahash, *fdhash, *fnhash; | 402 | double t_double; |
| 403 | static const char *programname; | 403 | int t_lineno; |
| 404 | static int lineno; | 404 | int t_rollback; |
| 405 | static int is_f0_split; | 405 | |
| 406 | static int nfields; | 406 | /* the rest */ |
| 407 | static var *Fields; | 407 | smallint icase; |
| 408 | static tsplitter fsplitter, rsplitter; | 408 | smallint exiting; |
| 409 | static nvblock *cb; | 409 | smallint nextrec; |
| 410 | static char *pos; | 410 | smallint nextfile; |
| 411 | static char *buf; | 411 | smallint is_f0_split; |
| 412 | static int icase; | 412 | chain beginseq, mainseq, endseq, *seq; |
| 413 | static int exiting; | 413 | node *break_ptr, *continue_ptr; |
| 414 | 414 | rstream *iF; | |
| 415 | static struct { | 415 | xhash *vhash, *ahash, *fdhash, *fnhash; |
| 416 | uint32_t tclass; | 416 | const char *g_progname; |
| 417 | uint32_t info; | 417 | int g_lineno; |
| 418 | char *string; | 418 | int nfields; |
| 419 | double number; | 419 | int maxfields; /* used in fsrealloc() only */ |
| 420 | int lineno; | 420 | var *Fields; |
| 421 | int rollback; | 421 | nvblock *g_cb; |
| 422 | } ttt; | 422 | char *g_pos; |
| 423 | /* It had even better name: 't'. Whoever knows what is it, please rename! */ | 423 | char *g_buf; |
| 424 | /* (actually it looks like unrelated stuff lumped together...) */ | 424 | |
| 425 | /* former statics from various functions */ | ||
| 426 | char *split_f0__fstrings; | ||
| 427 | |||
| 428 | rstream next_input_file__rsm; | ||
| 429 | smallint next_input_file__files_happen; | ||
| 430 | |||
| 431 | smallint next_token__concat_inserted; | ||
| 432 | uint32_t next_token__save_tclass; | ||
| 433 | uint32_t next_token__save_info; | ||
| 434 | uint32_t next_token__ltclass; | ||
| 435 | |||
| 436 | var *evaluate__fnargs; | ||
| 437 | unsigned evaluate__seed; | ||
| 438 | regex_t evaluate__sreg; | ||
| 439 | |||
| 440 | var ptest__v; | ||
| 441 | |||
| 442 | tsplitter exec_builtin__tspl; | ||
| 443 | |||
| 444 | /* biggest members go last */ | ||
| 445 | var *intvar[NUM_INTERNAL_VARS]; | ||
| 446 | tsplitter fsplitter, rsplitter; | ||
| 447 | }; | ||
| 448 | #define G (*ptr_to_globals) | ||
| 449 | /* for debug */ | ||
| 450 | /* char Gsize[sizeof(G)]; ~0x240 */ | ||
| 451 | /* Trying to keep most of members accessible with short offsets: */ | ||
| 452 | /* char Gofs_seed[offsetof(struct globals, evaluate__seed)]; ~0xc0 */ | ||
| 453 | #define t_info (G.t_info ) | ||
| 454 | #define t_tclass (G.t_tclass ) | ||
| 455 | #define t_string (G.t_string ) | ||
| 456 | #define t_double (G.t_double ) | ||
| 457 | #define t_lineno (G.t_lineno ) | ||
| 458 | #define t_rollback (G.t_rollback ) | ||
| 459 | #define icase (G.icase ) | ||
| 460 | #define exiting (G.exiting ) | ||
| 461 | #define nextrec (G.nextrec ) | ||
| 462 | #define nextfile (G.nextfile ) | ||
| 463 | #define is_f0_split (G.is_f0_split ) | ||
| 464 | #define beginseq (G.beginseq ) | ||
| 465 | #define mainseq (G.mainseq ) | ||
| 466 | #define endseq (G.endseq ) | ||
| 467 | #define seq (G.seq ) | ||
| 468 | #define break_ptr (G.break_ptr ) | ||
| 469 | #define continue_ptr (G.continue_ptr) | ||
| 470 | #define iF (G.iF ) | ||
| 471 | #define vhash (G.vhash ) | ||
| 472 | #define ahash (G.ahash ) | ||
| 473 | #define fdhash (G.fdhash ) | ||
| 474 | #define fnhash (G.fnhash ) | ||
| 475 | #define g_progname (G.g_progname ) | ||
| 476 | #define g_lineno (G.g_lineno ) | ||
| 477 | #define nfields (G.nfields ) | ||
| 478 | #define maxfields (G.maxfields ) | ||
| 479 | #define Fields (G.Fields ) | ||
| 480 | #define g_cb (G.g_cb ) | ||
| 481 | #define g_pos (G.g_pos ) | ||
| 482 | #define g_buf (G.g_buf ) | ||
| 483 | #define intvar (G.intvar ) | ||
| 484 | #define fsplitter (G.fsplitter ) | ||
| 485 | #define rsplitter (G.rsplitter ) | ||
| 486 | #define INIT_G() do { \ | ||
| 487 | PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ | ||
| 488 | G.next_token__ltclass = TC_OPTERM; \ | ||
| 489 | G.evaluate__seed = 1; \ | ||
| 490 | } while (0) | ||
| 491 | |||
| 425 | 492 | ||
| 426 | /* function prototypes */ | 493 | /* function prototypes */ |
| 427 | static void handle_special(var *); | 494 | static void handle_special(var *); |
| @@ -455,12 +522,9 @@ static void zero_out_var(var * vp) | |||
| 455 | static void syntax_error(const char * const message) ATTRIBUTE_NORETURN; | 522 | static void syntax_error(const char * const message) ATTRIBUTE_NORETURN; |
| 456 | static void syntax_error(const char * const message) | 523 | static void syntax_error(const char * const message) |
| 457 | { | 524 | { |
| 458 | bb_error_msg_and_die("%s:%i: %s", programname, lineno, message); | 525 | bb_error_msg_and_die("%s:%i: %s", g_progname, g_lineno, message); |
| 459 | } | 526 | } |
| 460 | 527 | ||
| 461 | #define runtime_error(x) syntax_error(x) | ||
| 462 | |||
| 463 | |||
| 464 | /* ---- hash stuff ---- */ | 528 | /* ---- hash stuff ---- */ |
| 465 | 529 | ||
| 466 | static unsigned hashidx(const char *name) | 530 | static unsigned hashidx(const char *name) |
| @@ -581,7 +645,7 @@ static void skip_spaces(char **s) | |||
| 581 | while (1) { | 645 | while (1) { |
| 582 | if (*p == '\\' && p[1] == '\n') { | 646 | if (*p == '\\' && p[1] == '\n') { |
| 583 | p++; | 647 | p++; |
| 584 | ttt.lineno++; | 648 | t_lineno++; |
| 585 | } else if (*p != ' ' && *p != '\t') { | 649 | } else if (*p != ' ' && *p != '\t') { |
| 586 | break; | 650 | break; |
| 587 | } | 651 | } |
| @@ -714,8 +778,8 @@ static const char *getvar_s(var *v) | |||
| 714 | { | 778 | { |
| 715 | /* if v is numeric and has no cached string, convert it to string */ | 779 | /* if v is numeric and has no cached string, convert it to string */ |
| 716 | if ((v->type & (VF_NUMBER | VF_CACHED)) == VF_NUMBER) { | 780 | if ((v->type & (VF_NUMBER | VF_CACHED)) == VF_NUMBER) { |
| 717 | fmt_num(buf, MAXVARFMT, getvar_s(intvar[CONVFMT]), v->number, TRUE); | 781 | fmt_num(g_buf, MAXVARFMT, getvar_s(intvar[CONVFMT]), v->number, TRUE); |
| 718 | v->string = xstrdup(buf); | 782 | v->string = xstrdup(g_buf); |
| 719 | v->type |= VF_CACHED; | 783 | v->type |= VF_CACHED; |
| 720 | } | 784 | } |
| 721 | return (v->string == NULL) ? "" : v->string; | 785 | return (v->string == NULL) ? "" : v->string; |
| @@ -773,8 +837,7 @@ static int istrue(var *v) | |||
| 773 | { | 837 | { |
| 774 | if (is_numeric(v)) | 838 | if (is_numeric(v)) |
| 775 | return (v->number == 0) ? 0 : 1; | 839 | return (v->number == 0) ? 0 : 1; |
| 776 | else | 840 | return (v->string && *(v->string)) ? 1 : 0; |
| 777 | return (v->string && *(v->string)) ? 1 : 0; | ||
| 778 | } | 841 | } |
| 779 | 842 | ||
| 780 | /* temporary variables allocator. Last allocated should be first freed */ | 843 | /* temporary variables allocator. Last allocated should be first freed */ |
| @@ -784,26 +847,26 @@ static var *nvalloc(int n) | |||
| 784 | var *v, *r; | 847 | var *v, *r; |
| 785 | int size; | 848 | int size; |
| 786 | 849 | ||
| 787 | while (cb) { | 850 | while (g_cb) { |
| 788 | pb = cb; | 851 | pb = g_cb; |
| 789 | if ((cb->pos - cb->nv) + n <= cb->size) break; | 852 | if ((g_cb->pos - g_cb->nv) + n <= g_cb->size) break; |
| 790 | cb = cb->next; | 853 | g_cb = g_cb->next; |
| 791 | } | 854 | } |
| 792 | 855 | ||
| 793 | if (! cb) { | 856 | if (!g_cb) { |
| 794 | size = (n <= MINNVBLOCK) ? MINNVBLOCK : n; | 857 | size = (n <= MINNVBLOCK) ? MINNVBLOCK : n; |
| 795 | cb = xmalloc(sizeof(nvblock) + size * sizeof(var)); | 858 | g_cb = xmalloc(sizeof(nvblock) + size * sizeof(var)); |
| 796 | cb->size = size; | 859 | g_cb->size = size; |
| 797 | cb->pos = cb->nv; | 860 | g_cb->pos = g_cb->nv; |
| 798 | cb->prev = pb; | 861 | g_cb->prev = pb; |
| 799 | cb->next = NULL; | 862 | g_cb->next = NULL; |
| 800 | if (pb) pb->next = cb; | 863 | if (pb) pb->next = g_cb; |
| 801 | } | 864 | } |
| 802 | 865 | ||
| 803 | v = r = cb->pos; | 866 | v = r = g_cb->pos; |
| 804 | cb->pos += n; | 867 | g_cb->pos += n; |
| 805 | 868 | ||
| 806 | while (v < cb->pos) { | 869 | while (v < g_cb->pos) { |
| 807 | v->type = 0; | 870 | v->type = 0; |
| 808 | v->string = NULL; | 871 | v->string = NULL; |
| 809 | v++; | 872 | v++; |
| @@ -816,10 +879,10 @@ static void nvfree(var *v) | |||
| 816 | { | 879 | { |
| 817 | var *p; | 880 | var *p; |
| 818 | 881 | ||
| 819 | if (v < cb->nv || v >= cb->pos) | 882 | if (v < g_cb->nv || v >= g_cb->pos) |
| 820 | runtime_error(EMSG_INTERNAL_ERROR); | 883 | syntax_error(EMSG_INTERNAL_ERROR); |
| 821 | 884 | ||
| 822 | for (p = v; p < cb->pos; p++) { | 885 | for (p = v; p < g_cb->pos; p++) { |
| 823 | if ((p->type & (VF_ARRAY | VF_CHILD)) == VF_ARRAY) { | 886 | if ((p->type & (VF_ARRAY | VF_CHILD)) == VF_ARRAY) { |
| 824 | clear_array(iamarray(p)); | 887 | clear_array(iamarray(p)); |
| 825 | free(p->x.array->items); | 888 | free(p->x.array->items); |
| @@ -831,9 +894,9 @@ static void nvfree(var *v) | |||
| 831 | clrvar(p); | 894 | clrvar(p); |
| 832 | } | 895 | } |
| 833 | 896 | ||
| 834 | cb->pos = v; | 897 | g_cb->pos = v; |
| 835 | while (cb->prev && cb->pos == cb->nv) { | 898 | while (g_cb->prev && g_cb->pos == g_cb->nv) { |
| 836 | cb = cb->prev; | 899 | g_cb = g_cb->prev; |
| 837 | } | 900 | } |
| 838 | } | 901 | } |
| 839 | 902 | ||
| @@ -844,9 +907,11 @@ static void nvfree(var *v) | |||
| 844 | */ | 907 | */ |
| 845 | static uint32_t next_token(uint32_t expected) | 908 | static uint32_t next_token(uint32_t expected) |
| 846 | { | 909 | { |
| 847 | static int concat_inserted; | 910 | #define concat_inserted (G.next_token__concat_inserted) |
| 848 | static uint32_t save_tclass, save_info; | 911 | #define save_tclass (G.next_token__save_tclass) |
| 849 | static uint32_t ltclass = TC_OPTERM; | 912 | #define save_info (G.next_token__save_info) |
| 913 | /* Initialized to TC_OPTERM: */ | ||
| 914 | #define ltclass (G.next_token__ltclass) | ||
| 850 | 915 | ||
| 851 | char *p, *pp, *s; | 916 | char *p, *pp, *s; |
| 852 | const char *tl; | 917 | const char *tl; |
| @@ -854,32 +919,32 @@ static uint32_t next_token(uint32_t expected) | |||
| 854 | const uint32_t *ti; | 919 | const uint32_t *ti; |
| 855 | int l; | 920 | int l; |
| 856 | 921 | ||
| 857 | if (ttt.rollback) { | 922 | if (t_rollback) { |
| 858 | ttt.rollback = FALSE; | 923 | t_rollback = FALSE; |
| 859 | 924 | ||
| 860 | } else if (concat_inserted) { | 925 | } else if (concat_inserted) { |
| 861 | concat_inserted = FALSE; | 926 | concat_inserted = FALSE; |
| 862 | ttt.tclass = save_tclass; | 927 | t_tclass = save_tclass; |
| 863 | ttt.info = save_info; | 928 | t_info = save_info; |
| 864 | 929 | ||
| 865 | } else { | 930 | } else { |
| 866 | p = pos; | 931 | p = g_pos; |
| 867 | readnext: | 932 | readnext: |
| 868 | skip_spaces(&p); | 933 | skip_spaces(&p); |
| 869 | lineno = ttt.lineno; | 934 | g_lineno = t_lineno; |
| 870 | if (*p == '#') | 935 | if (*p == '#') |
| 871 | while (*p != '\n' && *p != '\0') | 936 | while (*p != '\n' && *p != '\0') |
| 872 | p++; | 937 | p++; |
| 873 | 938 | ||
| 874 | if (*p == '\n') | 939 | if (*p == '\n') |
| 875 | ttt.lineno++; | 940 | t_lineno++; |
| 876 | 941 | ||
| 877 | if (*p == '\0') { | 942 | if (*p == '\0') { |
| 878 | tc = TC_EOF; | 943 | tc = TC_EOF; |
| 879 | 944 | ||
| 880 | } else if (*p == '\"') { | 945 | } else if (*p == '\"') { |
| 881 | /* it's a string */ | 946 | /* it's a string */ |
| 882 | ttt.string = s = ++p; | 947 | t_string = s = ++p; |
| 883 | while (*p != '\"') { | 948 | while (*p != '\"') { |
| 884 | if (*p == '\0' || *p == '\n') | 949 | if (*p == '\0' || *p == '\n') |
| 885 | syntax_error(EMSG_UNEXP_EOS); | 950 | syntax_error(EMSG_UNEXP_EOS); |
| @@ -891,7 +956,7 @@ static uint32_t next_token(uint32_t expected) | |||
| 891 | 956 | ||
| 892 | } else if ((expected & TC_REGEXP) && *p == '/') { | 957 | } else if ((expected & TC_REGEXP) && *p == '/') { |
| 893 | /* it's regexp */ | 958 | /* it's regexp */ |
| 894 | ttt.string = s = ++p; | 959 | t_string = s = ++p; |
| 895 | while (*p != '/') { | 960 | while (*p != '/') { |
| 896 | if (*p == '\0' || *p == '\n') | 961 | if (*p == '\0' || *p == '\n') |
| 897 | syntax_error(EMSG_UNEXP_EOS); | 962 | syntax_error(EMSG_UNEXP_EOS); |
| @@ -911,7 +976,7 @@ static uint32_t next_token(uint32_t expected) | |||
| 911 | 976 | ||
| 912 | } else if (*p == '.' || isdigit(*p)) { | 977 | } else if (*p == '.' || isdigit(*p)) { |
| 913 | /* it's a number */ | 978 | /* it's a number */ |
| 914 | ttt.number = strtod(p, &p); | 979 | t_double = strtod(p, &p); |
| 915 | if (*p == '.') | 980 | if (*p == '.') |
| 916 | syntax_error(EMSG_UNEXP_TOKEN); | 981 | syntax_error(EMSG_UNEXP_TOKEN); |
| 917 | tc = TC_NUMBER; | 982 | tc = TC_NUMBER; |
| @@ -935,7 +1000,7 @@ static uint32_t next_token(uint32_t expected) | |||
| 935 | && *tl == *p && strncmp(p, tl, l) == 0 | 1000 | && *tl == *p && strncmp(p, tl, l) == 0 |
| 936 | && !((tc & TC_WORD) && isalnum_(p[l])) | 1001 | && !((tc & TC_WORD) && isalnum_(p[l])) |
| 937 | ) { | 1002 | ) { |
| 938 | ttt.info = *ti; | 1003 | t_info = *ti; |
| 939 | p += l; | 1004 | p += l; |
| 940 | break; | 1005 | break; |
| 941 | } | 1006 | } |
| @@ -950,7 +1015,7 @@ static uint32_t next_token(uint32_t expected) | |||
| 950 | if (!isalnum_(*p)) | 1015 | if (!isalnum_(*p)) |
| 951 | syntax_error(EMSG_UNEXP_TOKEN); | 1016 | syntax_error(EMSG_UNEXP_TOKEN); |
| 952 | 1017 | ||
| 953 | ttt.string = --p; | 1018 | t_string = --p; |
| 954 | while (isalnum_(*(++p))) { | 1019 | while (isalnum_(*(++p))) { |
| 955 | *(p-1) = *p; | 1020 | *(p-1) = *p; |
| 956 | } | 1021 | } |
| @@ -969,7 +1034,7 @@ static uint32_t next_token(uint32_t expected) | |||
| 969 | } | 1034 | } |
| 970 | } | 1035 | } |
| 971 | } | 1036 | } |
| 972 | pos = p; | 1037 | g_pos = p; |
| 973 | 1038 | ||
| 974 | /* skipping newlines in some cases */ | 1039 | /* skipping newlines in some cases */ |
| 975 | if ((ltclass & TC_NOTERM) && (tc & TC_NEWLINE)) | 1040 | if ((ltclass & TC_NOTERM) && (tc & TC_NEWLINE)) |
| @@ -979,14 +1044,14 @@ static uint32_t next_token(uint32_t expected) | |||
| 979 | if ((ltclass & TC_CONCAT1) && (tc & TC_CONCAT2) && (expected & TC_BINOP)) { | 1044 | if ((ltclass & TC_CONCAT1) && (tc & TC_CONCAT2) && (expected & TC_BINOP)) { |
| 980 | concat_inserted = TRUE; | 1045 | concat_inserted = TRUE; |
| 981 | save_tclass = tc; | 1046 | save_tclass = tc; |
| 982 | save_info = ttt.info; | 1047 | save_info = t_info; |
| 983 | tc = TC_BINOP; | 1048 | tc = TC_BINOP; |
| 984 | ttt.info = OC_CONCAT | SS | P(35); | 1049 | t_info = OC_CONCAT | SS | P(35); |
| 985 | } | 1050 | } |
| 986 | 1051 | ||
| 987 | ttt.tclass = tc; | 1052 | t_tclass = tc; |
| 988 | } | 1053 | } |
| 989 | ltclass = ttt.tclass; | 1054 | ltclass = t_tclass; |
| 990 | 1055 | ||
| 991 | /* Are we ready for this? */ | 1056 | /* Are we ready for this? */ |
| 992 | if (!(ltclass & expected)) | 1057 | if (!(ltclass & expected)) |
| @@ -994,11 +1059,15 @@ static uint32_t next_token(uint32_t expected) | |||
| 994 | EMSG_UNEXP_EOS : EMSG_UNEXP_TOKEN); | 1059 | EMSG_UNEXP_EOS : EMSG_UNEXP_TOKEN); |
| 995 | 1060 | ||
| 996 | return ltclass; | 1061 | return ltclass; |
| 1062 | #undef concat_inserted | ||
| 1063 | #undef save_tclass | ||
| 1064 | #undef save_info | ||
| 1065 | #undef ltclass | ||
| 997 | } | 1066 | } |
| 998 | 1067 | ||
| 999 | static void rollback_token(void) | 1068 | static void rollback_token(void) |
| 1000 | { | 1069 | { |
| 1001 | ttt.rollback = TRUE; | 1070 | t_rollback = TRUE; |
| 1002 | } | 1071 | } |
| 1003 | 1072 | ||
| 1004 | static node *new_node(uint32_t info) | 1073 | static node *new_node(uint32_t info) |
| @@ -1007,7 +1076,7 @@ static node *new_node(uint32_t info) | |||
| 1007 | 1076 | ||
| 1008 | n = xzalloc(sizeof(node)); | 1077 | n = xzalloc(sizeof(node)); |
| 1009 | n->info = info; | 1078 | n->info = info; |
| 1010 | n->lineno = lineno; | 1079 | n->lineno = g_lineno; |
| 1011 | return n; | 1080 | return n; |
| 1012 | } | 1081 | } |
| 1013 | 1082 | ||
| @@ -1043,7 +1112,7 @@ static node *parse_expr(uint32_t iexp) | |||
| 1043 | xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP | iexp; | 1112 | xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP | iexp; |
| 1044 | 1113 | ||
| 1045 | while (!((tc = next_token(xtc)) & iexp)) { | 1114 | while (!((tc = next_token(xtc)) & iexp)) { |
| 1046 | if (glptr && (ttt.info == (OC_COMPARE | VV | P(39) | 2))) { | 1115 | if (glptr && (t_info == (OC_COMPARE | VV | P(39) | 2))) { |
| 1047 | /* input redirection (<) attached to glptr node */ | 1116 | /* input redirection (<) attached to glptr node */ |
| 1048 | cn = glptr->l.n = new_node(OC_CONCAT | SS | P(37)); | 1117 | cn = glptr->l.n = new_node(OC_CONCAT | SS | P(37)); |
| 1049 | cn->a.n = glptr; | 1118 | cn->a.n = glptr; |
| @@ -1054,17 +1123,17 @@ static node *parse_expr(uint32_t iexp) | |||
| 1054 | /* for binary and postfix-unary operators, jump back over | 1123 | /* for binary and postfix-unary operators, jump back over |
| 1055 | * previous operators with higher priority */ | 1124 | * previous operators with higher priority */ |
| 1056 | vn = cn; | 1125 | vn = cn; |
| 1057 | while ( ((ttt.info & PRIMASK) > (vn->a.n->info & PRIMASK2)) | 1126 | while ( ((t_info & PRIMASK) > (vn->a.n->info & PRIMASK2)) |
| 1058 | || ((ttt.info == vn->info) && ((ttt.info & OPCLSMASK) == OC_COLON)) ) | 1127 | || ((t_info == vn->info) && ((t_info & OPCLSMASK) == OC_COLON)) ) |
| 1059 | vn = vn->a.n; | 1128 | vn = vn->a.n; |
| 1060 | if ((ttt.info & OPCLSMASK) == OC_TERNARY) | 1129 | if ((t_info & OPCLSMASK) == OC_TERNARY) |
| 1061 | ttt.info += P(6); | 1130 | t_info += P(6); |
| 1062 | cn = vn->a.n->r.n = new_node(ttt.info); | 1131 | cn = vn->a.n->r.n = new_node(t_info); |
| 1063 | cn->a.n = vn->a.n; | 1132 | cn->a.n = vn->a.n; |
| 1064 | if (tc & TC_BINOP) { | 1133 | if (tc & TC_BINOP) { |
| 1065 | cn->l.n = vn; | 1134 | cn->l.n = vn; |
| 1066 | xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP; | 1135 | xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP; |
| 1067 | if ((ttt.info & OPCLSMASK) == OC_PGETLINE) { | 1136 | if ((t_info & OPCLSMASK) == OC_PGETLINE) { |
| 1068 | /* it's a pipe */ | 1137 | /* it's a pipe */ |
| 1069 | next_token(TC_GETLINE); | 1138 | next_token(TC_GETLINE); |
| 1070 | /* give maximum priority to this pipe */ | 1139 | /* give maximum priority to this pipe */ |
| @@ -1081,7 +1150,7 @@ static node *parse_expr(uint32_t iexp) | |||
| 1081 | /* for operands and prefix-unary operators, attach them | 1150 | /* for operands and prefix-unary operators, attach them |
| 1082 | * to last node */ | 1151 | * to last node */ |
| 1083 | vn = cn; | 1152 | vn = cn; |
| 1084 | cn = vn->r.n = new_node(ttt.info); | 1153 | cn = vn->r.n = new_node(t_info); |
| 1085 | cn->a.n = vn; | 1154 | cn->a.n = vn; |
| 1086 | xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP; | 1155 | xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP; |
| 1087 | if (tc & (TC_OPERAND | TC_REGEXP)) { | 1156 | if (tc & (TC_OPERAND | TC_REGEXP)) { |
| @@ -1092,12 +1161,12 @@ static node *parse_expr(uint32_t iexp) | |||
| 1092 | case TC_VARIABLE: | 1161 | case TC_VARIABLE: |
| 1093 | case TC_ARRAY: | 1162 | case TC_ARRAY: |
| 1094 | cn->info = OC_VAR; | 1163 | cn->info = OC_VAR; |
| 1095 | v = hash_search(ahash, ttt.string); | 1164 | v = hash_search(ahash, t_string); |
| 1096 | if (v != NULL) { | 1165 | if (v != NULL) { |
| 1097 | cn->info = OC_FNARG; | 1166 | cn->info = OC_FNARG; |
| 1098 | cn->l.i = v->x.aidx; | 1167 | cn->l.i = v->x.aidx; |
| 1099 | } else { | 1168 | } else { |
| 1100 | cn->l.v = newvar(ttt.string); | 1169 | cn->l.v = newvar(t_string); |
| 1101 | } | 1170 | } |
| 1102 | if (tc & TC_ARRAY) { | 1171 | if (tc & TC_ARRAY) { |
| 1103 | cn->info |= xS; | 1172 | cn->info |= xS; |
| @@ -1110,18 +1179,18 @@ static node *parse_expr(uint32_t iexp) | |||
| 1110 | cn->info = OC_VAR; | 1179 | cn->info = OC_VAR; |
| 1111 | v = cn->l.v = xzalloc(sizeof(var)); | 1180 | v = cn->l.v = xzalloc(sizeof(var)); |
| 1112 | if (tc & TC_NUMBER) | 1181 | if (tc & TC_NUMBER) |
| 1113 | setvar_i(v, ttt.number); | 1182 | setvar_i(v, t_double); |
| 1114 | else | 1183 | else |
| 1115 | setvar_s(v, ttt.string); | 1184 | setvar_s(v, t_string); |
| 1116 | break; | 1185 | break; |
| 1117 | 1186 | ||
| 1118 | case TC_REGEXP: | 1187 | case TC_REGEXP: |
| 1119 | mk_re_node(ttt.string, cn, xzalloc(sizeof(regex_t)*2)); | 1188 | mk_re_node(t_string, cn, xzalloc(sizeof(regex_t)*2)); |
| 1120 | break; | 1189 | break; |
| 1121 | 1190 | ||
| 1122 | case TC_FUNCTION: | 1191 | case TC_FUNCTION: |
| 1123 | cn->info = OC_FUNC; | 1192 | cn->info = OC_FUNC; |
| 1124 | cn->r.f = newfunc(ttt.string); | 1193 | cn->r.f = newfunc(t_string); |
| 1125 | cn->l.n = condition(); | 1194 | cn->l.n = condition(); |
| 1126 | break; | 1195 | break; |
| 1127 | 1196 | ||
| @@ -1153,10 +1222,10 @@ static node *chain_node(uint32_t info) | |||
| 1153 | if (!seq->first) | 1222 | if (!seq->first) |
| 1154 | seq->first = seq->last = new_node(0); | 1223 | seq->first = seq->last = new_node(0); |
| 1155 | 1224 | ||
| 1156 | if (seq->programname != programname) { | 1225 | if (seq->programname != g_progname) { |
| 1157 | seq->programname = programname; | 1226 | seq->programname = g_progname; |
| 1158 | n = chain_node(OC_NEWSOURCE); | 1227 | n = chain_node(OC_NEWSOURCE); |
| 1159 | n->l.s = xstrdup(programname); | 1228 | n->l.s = xstrdup(g_progname); |
| 1160 | } | 1229 | } |
| 1161 | 1230 | ||
| 1162 | n = seq->last; | 1231 | n = seq->last; |
| @@ -1172,7 +1241,7 @@ static void chain_expr(uint32_t info) | |||
| 1172 | 1241 | ||
| 1173 | n = chain_node(info); | 1242 | n = chain_node(info); |
| 1174 | n->l.n = parse_expr(TC_OPTERM | TC_GRPTERM); | 1243 | n->l.n = parse_expr(TC_OPTERM | TC_GRPTERM); |
| 1175 | if (ttt.tclass & TC_GRPTERM) | 1244 | if (t_tclass & TC_GRPTERM) |
| 1176 | rollback_token(); | 1245 | rollback_token(); |
| 1177 | } | 1246 | } |
| 1178 | 1247 | ||
| @@ -1211,7 +1280,7 @@ static void chain_group(void) | |||
| 1211 | 1280 | ||
| 1212 | if (c & TC_GRPSTART) { | 1281 | if (c & TC_GRPSTART) { |
| 1213 | while (next_token(TC_GRPSEQ | TC_GRPTERM) != TC_GRPTERM) { | 1282 | while (next_token(TC_GRPSEQ | TC_GRPTERM) != TC_GRPTERM) { |
| 1214 | if (ttt.tclass & TC_NEWLINE) continue; | 1283 | if (t_tclass & TC_NEWLINE) continue; |
| 1215 | rollback_token(); | 1284 | rollback_token(); |
| 1216 | chain_group(); | 1285 | chain_group(); |
| 1217 | } | 1286 | } |
| @@ -1219,7 +1288,7 @@ static void chain_group(void) | |||
| 1219 | rollback_token(); | 1288 | rollback_token(); |
| 1220 | chain_expr(OC_EXEC | Vx); | 1289 | chain_expr(OC_EXEC | Vx); |
| 1221 | } else { /* TC_STATEMNT */ | 1290 | } else { /* TC_STATEMNT */ |
| 1222 | switch (ttt.info & OPCLSMASK) { | 1291 | switch (t_info & OPCLSMASK) { |
| 1223 | case ST_IF: | 1292 | case ST_IF: |
| 1224 | n = chain_node(OC_BR | Vx); | 1293 | n = chain_node(OC_BR | Vx); |
| 1225 | n->l.n = condition(); | 1294 | n->l.n = condition(); |
| @@ -1251,7 +1320,7 @@ static void chain_group(void) | |||
| 1251 | case ST_FOR: | 1320 | case ST_FOR: |
| 1252 | next_token(TC_SEQSTART); | 1321 | next_token(TC_SEQSTART); |
| 1253 | n2 = parse_expr(TC_SEMICOL | TC_SEQTERM); | 1322 | n2 = parse_expr(TC_SEMICOL | TC_SEQTERM); |
| 1254 | if (ttt.tclass & TC_SEQTERM) { /* for-in */ | 1323 | if (t_tclass & TC_SEQTERM) { /* for-in */ |
| 1255 | if ((n2->info & OPCLSMASK) != OC_IN) | 1324 | if ((n2->info & OPCLSMASK) != OC_IN) |
| 1256 | syntax_error(EMSG_UNEXP_TOKEN); | 1325 | syntax_error(EMSG_UNEXP_TOKEN); |
| 1257 | n = chain_node(OC_WALKINIT | VV); | 1326 | n = chain_node(OC_WALKINIT | VV); |
| @@ -1274,13 +1343,13 @@ static void chain_group(void) | |||
| 1274 | 1343 | ||
| 1275 | case OC_PRINT: | 1344 | case OC_PRINT: |
| 1276 | case OC_PRINTF: | 1345 | case OC_PRINTF: |
| 1277 | n = chain_node(ttt.info); | 1346 | n = chain_node(t_info); |
| 1278 | n->l.n = parse_expr(TC_OPTERM | TC_OUTRDR | TC_GRPTERM); | 1347 | n->l.n = parse_expr(TC_OPTERM | TC_OUTRDR | TC_GRPTERM); |
| 1279 | if (ttt.tclass & TC_OUTRDR) { | 1348 | if (t_tclass & TC_OUTRDR) { |
| 1280 | n->info |= ttt.info; | 1349 | n->info |= t_info; |
| 1281 | n->r.n = parse_expr(TC_OPTERM | TC_GRPTERM); | 1350 | n->r.n = parse_expr(TC_OPTERM | TC_GRPTERM); |
| 1282 | } | 1351 | } |
| 1283 | if (ttt.tclass & TC_GRPTERM) | 1352 | if (t_tclass & TC_GRPTERM) |
| 1284 | rollback_token(); | 1353 | rollback_token(); |
| 1285 | break; | 1354 | break; |
| 1286 | 1355 | ||
| @@ -1296,7 +1365,7 @@ static void chain_group(void) | |||
| 1296 | 1365 | ||
| 1297 | /* delete, next, nextfile, return, exit */ | 1366 | /* delete, next, nextfile, return, exit */ |
| 1298 | default: | 1367 | default: |
| 1299 | chain_expr(ttt.info); | 1368 | chain_expr(t_info); |
| 1300 | } | 1369 | } |
| 1301 | } | 1370 | } |
| 1302 | } | 1371 | } |
| @@ -1308,8 +1377,8 @@ static void parse_program(char *p) | |||
| 1308 | func *f; | 1377 | func *f; |
| 1309 | var *v; | 1378 | var *v; |
| 1310 | 1379 | ||
| 1311 | pos = p; | 1380 | g_pos = p; |
| 1312 | ttt.lineno = 1; | 1381 | t_lineno = 1; |
| 1313 | while ((tclass = next_token(TC_EOF | TC_OPSEQ | TC_GRPSTART | | 1382 | while ((tclass = next_token(TC_EOF | TC_OPSEQ | TC_GRPSTART | |
| 1314 | TC_OPTERM | TC_BEGIN | TC_END | TC_FUNCDECL)) != TC_EOF) { | 1383 | TC_OPTERM | TC_BEGIN | TC_END | TC_FUNCDECL)) != TC_EOF) { |
| 1315 | 1384 | ||
| @@ -1327,12 +1396,12 @@ static void parse_program(char *p) | |||
| 1327 | 1396 | ||
| 1328 | } else if (tclass & TC_FUNCDECL) { | 1397 | } else if (tclass & TC_FUNCDECL) { |
| 1329 | next_token(TC_FUNCTION); | 1398 | next_token(TC_FUNCTION); |
| 1330 | pos++; | 1399 | g_pos++; |
| 1331 | f = newfunc(ttt.string); | 1400 | f = newfunc(t_string); |
| 1332 | f->body.first = NULL; | 1401 | f->body.first = NULL; |
| 1333 | f->nargs = 0; | 1402 | f->nargs = 0; |
| 1334 | while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) { | 1403 | while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) { |
| 1335 | v = findvar(ahash, ttt.string); | 1404 | v = findvar(ahash, t_string); |
| 1336 | v->x.aidx = (f->nargs)++; | 1405 | v->x.aidx = (f->nargs)++; |
| 1337 | 1406 | ||
| 1338 | if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM) | 1407 | if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM) |
| @@ -1346,7 +1415,7 @@ static void parse_program(char *p) | |||
| 1346 | rollback_token(); | 1415 | rollback_token(); |
| 1347 | cn = chain_node(OC_TEST); | 1416 | cn = chain_node(OC_TEST); |
| 1348 | cn->l.n = parse_expr(TC_OPTERM | TC_EOF | TC_GRPSTART); | 1417 | cn->l.n = parse_expr(TC_OPTERM | TC_EOF | TC_GRPSTART); |
| 1349 | if (ttt.tclass & TC_GRPSTART) { | 1418 | if (t_tclass & TC_GRPSTART) { |
| 1350 | rollback_token(); | 1419 | rollback_token(); |
| 1351 | chain_group(); | 1420 | chain_group(); |
| 1352 | } else { | 1421 | } else { |
| @@ -1414,7 +1483,6 @@ static void qrealloc(char **b, int n, int *size) | |||
| 1414 | /* resize field storage space */ | 1483 | /* resize field storage space */ |
| 1415 | static void fsrealloc(int size) | 1484 | static void fsrealloc(int size) |
| 1416 | { | 1485 | { |
| 1417 | static int maxfields; /* = 0;*/ | ||
| 1418 | int i; | 1486 | int i; |
| 1419 | 1487 | ||
| 1420 | if (size >= maxfields) { | 1488 | if (size >= maxfields) { |
| @@ -1504,7 +1572,7 @@ static int awk_split(const char *s, node *spl, char **slist) | |||
| 1504 | 1572 | ||
| 1505 | static void split_f0(void) | 1573 | static void split_f0(void) |
| 1506 | { | 1574 | { |
| 1507 | static char *fstrings = NULL; | 1575 | #define fstrings (G.split_f0__fstrings) |
| 1508 | 1576 | ||
| 1509 | int i, n; | 1577 | int i, n; |
| 1510 | char *s; | 1578 | char *s; |
| @@ -1527,6 +1595,7 @@ static void split_f0(void) | |||
| 1527 | clrvar(intvar[NF]); | 1595 | clrvar(intvar[NF]); |
| 1528 | intvar[NF]->type = VF_NUMBER | VF_SPECIAL; | 1596 | intvar[NF]->type = VF_NUMBER | VF_SPECIAL; |
| 1529 | intvar[NF]->number = nfields; | 1597 | intvar[NF]->number = nfields; |
| 1598 | #undef fstrings | ||
| 1530 | } | 1599 | } |
| 1531 | 1600 | ||
| 1532 | /* perform additional actions when some internal variables changed */ | 1601 | /* perform additional actions when some internal variables changed */ |
| @@ -1636,9 +1705,8 @@ static int hashwalk_next(var *v) | |||
| 1636 | /* evaluate node, return 1 when result is true, 0 otherwise */ | 1705 | /* evaluate node, return 1 when result is true, 0 otherwise */ |
| 1637 | static int ptest(node *pattern) | 1706 | static int ptest(node *pattern) |
| 1638 | { | 1707 | { |
| 1639 | static var v; /* static: to save stack space? */ | 1708 | /* ptest__v is "static": to save stack space? */ |
| 1640 | 1709 | return istrue(evaluate(pattern, &G.ptest__v)); | |
| 1641 | return istrue(evaluate(pattern, &v)); | ||
| 1642 | } | 1710 | } |
| 1643 | 1711 | ||
| 1644 | /* read next record from stream rsm into a variable v */ | 1712 | /* read next record from stream rsm into a variable v */ |
| @@ -1750,7 +1818,7 @@ static int fmt_num(char *b, int size, const char *format, double n, int int_as_i | |||
| 1750 | } else if (strchr("eEfgG", c)) { | 1818 | } else if (strchr("eEfgG", c)) { |
| 1751 | r = snprintf(b, size, format, n); | 1819 | r = snprintf(b, size, format, n); |
| 1752 | } else { | 1820 | } else { |
| 1753 | runtime_error(EMSG_INV_FMT); | 1821 | syntax_error(EMSG_INV_FMT); |
| 1754 | } | 1822 | } |
| 1755 | } | 1823 | } |
| 1756 | return r; | 1824 | return r; |
| @@ -1888,6 +1956,8 @@ static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int | |||
| 1888 | 1956 | ||
| 1889 | static var *exec_builtin(node *op, var *res) | 1957 | static var *exec_builtin(node *op, var *res) |
| 1890 | { | 1958 | { |
| 1959 | #define tspl (G.exec_builtin__tspl) | ||
| 1960 | |||
| 1891 | int (*to_xxx)(int); | 1961 | int (*to_xxx)(int); |
| 1892 | var *tv; | 1962 | var *tv; |
| 1893 | node *an[4]; | 1963 | node *an[4]; |
| @@ -1895,7 +1965,6 @@ static var *exec_builtin(node *op, var *res) | |||
| 1895 | const char *as[4]; | 1965 | const char *as[4]; |
| 1896 | regmatch_t pmatch[2]; | 1966 | regmatch_t pmatch[2]; |
| 1897 | regex_t sreg, *re; | 1967 | regex_t sreg, *re; |
| 1898 | static tsplitter tspl; | ||
| 1899 | node *spl; | 1968 | node *spl; |
| 1900 | uint32_t isr, info; | 1969 | uint32_t isr, info; |
| 1901 | int nargs; | 1970 | int nargs; |
| @@ -1917,7 +1986,7 @@ static var *exec_builtin(node *op, var *res) | |||
| 1917 | 1986 | ||
| 1918 | nargs = i; | 1987 | nargs = i; |
| 1919 | if (nargs < (info >> 30)) | 1988 | if (nargs < (info >> 30)) |
| 1920 | runtime_error(EMSG_TOO_FEW_ARGS); | 1989 | syntax_error(EMSG_TOO_FEW_ARGS); |
| 1921 | 1990 | ||
| 1922 | switch (info & OPNMASK) { | 1991 | switch (info & OPNMASK) { |
| 1923 | 1992 | ||
| @@ -1925,7 +1994,7 @@ static var *exec_builtin(node *op, var *res) | |||
| 1925 | #if ENABLE_FEATURE_AWK_MATH | 1994 | #if ENABLE_FEATURE_AWK_MATH |
| 1926 | setvar_i(res, atan2(getvar_i(av[i]), getvar_i(av[1]))); | 1995 | setvar_i(res, atan2(getvar_i(av[i]), getvar_i(av[1]))); |
| 1927 | #else | 1996 | #else |
| 1928 | runtime_error(EMSG_NO_MATH); | 1997 | syntax_error(EMSG_NO_MATH); |
| 1929 | #endif | 1998 | #endif |
| 1930 | break; | 1999 | break; |
| 1931 | 2000 | ||
| @@ -2003,7 +2072,7 @@ static var *exec_builtin(node *op, var *res) | |||
| 2003 | ll = strlen(as[1]); | 2072 | ll = strlen(as[1]); |
| 2004 | l = strlen(as[0]) - ll; | 2073 | l = strlen(as[0]) - ll; |
| 2005 | if (ll > 0 && l >= 0) { | 2074 | if (ll > 0 && l >= 0) { |
| 2006 | if (! icase) { | 2075 | if (!icase) { |
| 2007 | s = strstr(as[0], as[1]); | 2076 | s = strstr(as[0], as[1]); |
| 2008 | if (s) n = (s - as[0]) + 1; | 2077 | if (s) n = (s - as[0]) + 1; |
| 2009 | } else { | 2078 | } else { |
| @@ -2027,11 +2096,11 @@ static var *exec_builtin(node *op, var *res) | |||
| 2027 | else | 2096 | else |
| 2028 | time(&tt); | 2097 | time(&tt); |
| 2029 | //s = (nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y"; | 2098 | //s = (nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y"; |
| 2030 | i = strftime(buf, MAXVARFMT, | 2099 | i = strftime(g_buf, MAXVARFMT, |
| 2031 | ((nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y"), | 2100 | ((nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y"), |
| 2032 | localtime(&tt)); | 2101 | localtime(&tt)); |
| 2033 | buf[i] = '\0'; | 2102 | g_buf[i] = '\0'; |
| 2034 | setvar_s(res, buf); | 2103 | setvar_s(res, g_buf); |
| 2035 | break; | 2104 | break; |
| 2036 | 2105 | ||
| 2037 | case B_ma: | 2106 | case B_ma: |
| @@ -2065,6 +2134,7 @@ static var *exec_builtin(node *op, var *res) | |||
| 2065 | 2134 | ||
| 2066 | nvfree(tv); | 2135 | nvfree(tv); |
| 2067 | return res; | 2136 | return res; |
| 2137 | #undef tspl | ||
| 2068 | } | 2138 | } |
| 2069 | 2139 | ||
| 2070 | /* | 2140 | /* |
| @@ -2075,10 +2145,11 @@ static var *exec_builtin(node *op, var *res) | |||
| 2075 | 2145 | ||
| 2076 | static var *evaluate(node *op, var *res) | 2146 | static var *evaluate(node *op, var *res) |
| 2077 | { | 2147 | { |
| 2078 | /* This procedure is recursive so we should count every byte */ | 2148 | /* This procedure is recursive so we should count every byte */ |
| 2079 | static var *fnargs = NULL; | 2149 | #define fnargs (G.evaluate__fnargs) |
| 2080 | static unsigned seed = 1; | 2150 | /* seed is initialized to 1 */ |
| 2081 | static regex_t sreg; | 2151 | #define seed (G.evaluate__seed) |
| 2152 | #define sreg (G.evaluate__sreg) | ||
| 2082 | 2153 | ||
| 2083 | node *op1; | 2154 | node *op1; |
| 2084 | var *v1; | 2155 | var *v1; |
| @@ -2089,7 +2160,7 @@ static var *evaluate(node *op, var *res) | |||
| 2089 | int i; | 2160 | int i; |
| 2090 | } L, R; | 2161 | } L, R; |
| 2091 | uint32_t opinfo; | 2162 | uint32_t opinfo; |
| 2092 | short opn; | 2163 | int opn; |
| 2093 | union { | 2164 | union { |
| 2094 | char *s; | 2165 | char *s; |
| 2095 | rstream *rsm; | 2166 | rstream *rsm; |
| @@ -2106,8 +2177,8 @@ static var *evaluate(node *op, var *res) | |||
| 2106 | 2177 | ||
| 2107 | while (op) { | 2178 | while (op) { |
| 2108 | opinfo = op->info; | 2179 | opinfo = op->info; |
| 2109 | opn = (short)(opinfo & OPNMASK); | 2180 | opn = (opinfo & OPNMASK); |
| 2110 | lineno = op->lineno; | 2181 | g_lineno = op->lineno; |
| 2111 | 2182 | ||
| 2112 | /* execute inevitable things */ | 2183 | /* execute inevitable things */ |
| 2113 | op1 = op->l.n; | 2184 | op1 = op->l.n; |
| @@ -2163,7 +2234,7 @@ static var *evaluate(node *op, var *res) | |||
| 2163 | X.F = stdout; | 2234 | X.F = stdout; |
| 2164 | if (op->r.n) { | 2235 | if (op->r.n) { |
| 2165 | X.rsm = newfile(R.s); | 2236 | X.rsm = newfile(R.s); |
| 2166 | if (! X.rsm->F) { | 2237 | if (!X.rsm->F) { |
| 2167 | if (opn == '|') { | 2238 | if (opn == '|') { |
| 2168 | X.rsm->F = popen(R.s, "w"); | 2239 | X.rsm->F = popen(R.s, "w"); |
| 2169 | if (X.rsm->F == NULL) | 2240 | if (X.rsm->F == NULL) |
| @@ -2177,15 +2248,15 @@ static var *evaluate(node *op, var *res) | |||
| 2177 | } | 2248 | } |
| 2178 | 2249 | ||
| 2179 | if ((opinfo & OPCLSMASK) == OC_PRINT) { | 2250 | if ((opinfo & OPCLSMASK) == OC_PRINT) { |
| 2180 | if (! op1) { | 2251 | if (!op1) { |
| 2181 | fputs(getvar_s(intvar[F0]), X.F); | 2252 | fputs(getvar_s(intvar[F0]), X.F); |
| 2182 | } else { | 2253 | } else { |
| 2183 | while (op1) { | 2254 | while (op1) { |
| 2184 | L.v = evaluate(nextarg(&op1), v1); | 2255 | L.v = evaluate(nextarg(&op1), v1); |
| 2185 | if (L.v->type & VF_NUMBER) { | 2256 | if (L.v->type & VF_NUMBER) { |
| 2186 | fmt_num(buf, MAXVARFMT, getvar_s(intvar[OFMT]), | 2257 | fmt_num(g_buf, MAXVARFMT, getvar_s(intvar[OFMT]), |
| 2187 | getvar_i(L.v), TRUE); | 2258 | getvar_i(L.v), TRUE); |
| 2188 | fputs(buf, X.F); | 2259 | fputs(g_buf, X.F); |
| 2189 | } else { | 2260 | } else { |
| 2190 | fputs(getvar_s(L.v), X.F); | 2261 | fputs(getvar_s(L.v), X.F); |
| 2191 | } | 2262 | } |
| @@ -2210,7 +2281,7 @@ static var *evaluate(node *op, var *res) | |||
| 2210 | } else if (X.info == OC_FNARG) { | 2281 | } else if (X.info == OC_FNARG) { |
| 2211 | R.v = &fnargs[op1->l.i]; | 2282 | R.v = &fnargs[op1->l.i]; |
| 2212 | } else { | 2283 | } else { |
| 2213 | runtime_error(EMSG_NOT_ARRAY); | 2284 | syntax_error(EMSG_NOT_ARRAY); |
| 2214 | } | 2285 | } |
| 2215 | 2286 | ||
| 2216 | if (op1->r.n) { | 2287 | if (op1->r.n) { |
| @@ -2223,7 +2294,7 @@ static var *evaluate(node *op, var *res) | |||
| 2223 | break; | 2294 | break; |
| 2224 | 2295 | ||
| 2225 | case XC( OC_NEWSOURCE ): | 2296 | case XC( OC_NEWSOURCE ): |
| 2226 | programname = op->l.s; | 2297 | g_progname = op->l.s; |
| 2227 | break; | 2298 | break; |
| 2228 | 2299 | ||
| 2229 | case XC( OC_RETURN ): | 2300 | case XC( OC_RETURN ): |
| @@ -2285,13 +2356,13 @@ static var *evaluate(node *op, var *res) | |||
| 2285 | 2356 | ||
| 2286 | case XC( OC_TERNARY ): | 2357 | case XC( OC_TERNARY ): |
| 2287 | if ((op->r.n->info & OPCLSMASK) != OC_COLON) | 2358 | if ((op->r.n->info & OPCLSMASK) != OC_COLON) |
| 2288 | runtime_error(EMSG_POSSIBLE_ERROR); | 2359 | syntax_error(EMSG_POSSIBLE_ERROR); |
| 2289 | res = evaluate(istrue(L.v) ? op->r.n->l.n : op->r.n->r.n, res); | 2360 | res = evaluate(istrue(L.v) ? op->r.n->l.n : op->r.n->r.n, res); |
| 2290 | break; | 2361 | break; |
| 2291 | 2362 | ||
| 2292 | case XC( OC_FUNC ): | 2363 | case XC( OC_FUNC ): |
| 2293 | if (!op->r.f->body.first) | 2364 | if (!op->r.f->body.first) |
| 2294 | runtime_error(EMSG_UNDEF_FUNC); | 2365 | syntax_error(EMSG_UNDEF_FUNC); |
| 2295 | 2366 | ||
| 2296 | X.v = R.v = nvalloc(op->r.f->nargs+1); | 2367 | X.v = R.v = nvalloc(op->r.f->nargs+1); |
| 2297 | while (op1) { | 2368 | while (op1) { |
| @@ -2306,9 +2377,9 @@ static var *evaluate(node *op, var *res) | |||
| 2306 | R.v = fnargs; | 2377 | R.v = fnargs; |
| 2307 | fnargs = X.v; | 2378 | fnargs = X.v; |
| 2308 | 2379 | ||
| 2309 | L.s = programname; | 2380 | L.s = g_progname; |
| 2310 | res = evaluate(op->r.f->body.first, res); | 2381 | res = evaluate(op->r.f->body.first, res); |
| 2311 | programname = L.s; | 2382 | g_progname = L.s; |
| 2312 | 2383 | ||
| 2313 | nvfree(fnargs); | 2384 | nvfree(fnargs); |
| 2314 | fnargs = R.v; | 2385 | fnargs = R.v; |
| @@ -2387,7 +2458,7 @@ static var *evaluate(node *op, var *res) | |||
| 2387 | case F_lg: | 2458 | case F_lg: |
| 2388 | case F_si: | 2459 | case F_si: |
| 2389 | case F_sq: | 2460 | case F_sq: |
| 2390 | runtime_error(EMSG_NO_MATH); | 2461 | syntax_error(EMSG_NO_MATH); |
| 2391 | break; | 2462 | break; |
| 2392 | #endif | 2463 | #endif |
| 2393 | case F_sr: | 2464 | case F_sr: |
| @@ -2525,18 +2596,18 @@ static var *evaluate(node *op, var *res) | |||
| 2525 | L.d *= R.d; | 2596 | L.d *= R.d; |
| 2526 | break; | 2597 | break; |
| 2527 | case '/': | 2598 | case '/': |
| 2528 | if (R.d == 0) runtime_error(EMSG_DIV_BY_ZERO); | 2599 | if (R.d == 0) syntax_error(EMSG_DIV_BY_ZERO); |
| 2529 | L.d /= R.d; | 2600 | L.d /= R.d; |
| 2530 | break; | 2601 | break; |
| 2531 | case '&': | 2602 | case '&': |
| 2532 | #if ENABLE_FEATURE_AWK_MATH | 2603 | #if ENABLE_FEATURE_AWK_MATH |
| 2533 | L.d = pow(L.d, R.d); | 2604 | L.d = pow(L.d, R.d); |
| 2534 | #else | 2605 | #else |
| 2535 | runtime_error(EMSG_NO_MATH); | 2606 | syntax_error(EMSG_NO_MATH); |
| 2536 | #endif | 2607 | #endif |
| 2537 | break; | 2608 | break; |
| 2538 | case '%': | 2609 | case '%': |
| 2539 | if (R.d == 0) runtime_error(EMSG_DIV_BY_ZERO); | 2610 | if (R.d == 0) syntax_error(EMSG_DIV_BY_ZERO); |
| 2540 | L.d -= (int)(L.d / R.d) * R.d; | 2611 | L.d -= (int)(L.d / R.d) * R.d; |
| 2541 | break; | 2612 | break; |
| 2542 | } | 2613 | } |
| @@ -2566,7 +2637,7 @@ static var *evaluate(node *op, var *res) | |||
| 2566 | break; | 2637 | break; |
| 2567 | 2638 | ||
| 2568 | default: | 2639 | default: |
| 2569 | runtime_error(EMSG_POSSIBLE_ERROR); | 2640 | syntax_error(EMSG_POSSIBLE_ERROR); |
| 2570 | } | 2641 | } |
| 2571 | if ((opinfo & OPCLSMASK) <= SHIFT_TIL_THIS) | 2642 | if ((opinfo & OPCLSMASK) <= SHIFT_TIL_THIS) |
| 2572 | op = op->a.n; | 2643 | op = op->a.n; |
| @@ -2577,6 +2648,9 @@ static var *evaluate(node *op, var *res) | |||
| 2577 | } | 2648 | } |
| 2578 | nvfree(v1); | 2649 | nvfree(v1); |
| 2579 | return res; | 2650 | return res; |
| 2651 | #undef fnargs | ||
| 2652 | #undef seed | ||
| 2653 | #undef sreg | ||
| 2580 | } | 2654 | } |
| 2581 | 2655 | ||
| 2582 | 2656 | ||
| @@ -2635,8 +2709,8 @@ static int is_assignment(const char *expr) | |||
| 2635 | /* switch to next input file */ | 2709 | /* switch to next input file */ |
| 2636 | static rstream *next_input_file(void) | 2710 | static rstream *next_input_file(void) |
| 2637 | { | 2711 | { |
| 2638 | static rstream rsm; | 2712 | #define rsm (G.next_input_file__rsm) |
| 2639 | static int files_happen = FALSE; | 2713 | #define files_happen (G.next_input_file__files_happen) |
| 2640 | 2714 | ||
| 2641 | FILE *F = NULL; | 2715 | FILE *F = NULL; |
| 2642 | const char *fname, *ind; | 2716 | const char *fname, *ind; |
| @@ -2663,6 +2737,8 @@ static rstream *next_input_file(void) | |||
| 2663 | setvar_s(intvar[FILENAME], fname); | 2737 | setvar_s(intvar[FILENAME], fname); |
| 2664 | rsm.F = F; | 2738 | rsm.F = F; |
| 2665 | return &rsm; | 2739 | return &rsm; |
| 2740 | #undef rsm | ||
| 2741 | #undef files_happen | ||
| 2666 | } | 2742 | } |
| 2667 | 2743 | ||
| 2668 | int awk_main(int argc, char **argv); | 2744 | int awk_main(int argc, char **argv); |
| @@ -2678,6 +2754,8 @@ int awk_main(int argc, char **argv) | |||
| 2678 | char *vnames = (char *)vNames; /* cheat */ | 2754 | char *vnames = (char *)vNames; /* cheat */ |
| 2679 | char *vvalues = (char *)vValues; | 2755 | char *vvalues = (char *)vValues; |
| 2680 | 2756 | ||
| 2757 | INIT_G(); | ||
| 2758 | |||
| 2681 | /* Undo busybox.c, or else strtod may eat ','! This breaks parsing: | 2759 | /* Undo busybox.c, or else strtod may eat ','! This breaks parsing: |
| 2682 | * $1,$2 == '$1,' '$2', NOT '$1' ',' '$2' */ | 2760 | * $1,$2 == '$1,' '$2', NOT '$1' ',' '$2' */ |
| 2683 | if (ENABLE_LOCALE_SUPPORT) | 2761 | if (ENABLE_LOCALE_SUPPORT) |
| @@ -2686,7 +2764,7 @@ int awk_main(int argc, char **argv) | |||
| 2686 | zero_out_var(&tv); | 2764 | zero_out_var(&tv); |
| 2687 | 2765 | ||
| 2688 | /* allocate global buffer */ | 2766 | /* allocate global buffer */ |
| 2689 | buf = xmalloc(MAXVARFMT + 1); | 2767 | g_buf = xmalloc(MAXVARFMT + 1); |
| 2690 | 2768 | ||
| 2691 | vhash = hash_init(); | 2769 | vhash = hash_init(); |
| 2692 | ahash = hash_init(); | 2770 | ahash = hash_init(); |
| @@ -2725,7 +2803,7 @@ int awk_main(int argc, char **argv) | |||
| 2725 | free(s); | 2803 | free(s); |
| 2726 | } | 2804 | } |
| 2727 | opt_complementary = "v::"; | 2805 | opt_complementary = "v::"; |
| 2728 | opt = getopt32(argc, argv, "F:v:f:W:", &opt_F, &opt_v, &programname, &opt_W); | 2806 | opt = getopt32(argc, argv, "F:v:f:W:", &opt_F, &opt_v, &g_progname, &opt_W); |
| 2729 | argv += optind; | 2807 | argv += optind; |
| 2730 | argc -= optind; | 2808 | argc -= optind; |
| 2731 | if (opt & 0x1) | 2809 | if (opt & 0x1) |
| @@ -2736,7 +2814,7 @@ int awk_main(int argc, char **argv) | |||
| 2736 | } | 2814 | } |
| 2737 | if (opt & 0x4) { // -f | 2815 | if (opt & 0x4) { // -f |
| 2738 | char *s = s; /* die, gcc, die */ | 2816 | char *s = s; /* die, gcc, die */ |
| 2739 | FILE *from_file = afopen(programname, "r"); | 2817 | FILE *from_file = afopen(g_progname, "r"); |
| 2740 | /* one byte is reserved for some trick in next_token */ | 2818 | /* one byte is reserved for some trick in next_token */ |
| 2741 | if (fseek(from_file, 0, SEEK_END) == 0) { | 2819 | if (fseek(from_file, 0, SEEK_END) == 0) { |
| 2742 | flen = ftell(from_file); | 2820 | flen = ftell(from_file); |
| @@ -2756,7 +2834,7 @@ int awk_main(int argc, char **argv) | |||
| 2756 | } else { // no -f: take program from 1st parameter | 2834 | } else { // no -f: take program from 1st parameter |
| 2757 | if (!argc) | 2835 | if (!argc) |
| 2758 | bb_show_usage(); | 2836 | bb_show_usage(); |
| 2759 | programname = "cmd. line"; | 2837 | g_progname = "cmd. line"; |
| 2760 | parse_program(*argv++); | 2838 | parse_program(*argv++); |
| 2761 | argc--; | 2839 | argc--; |
| 2762 | } | 2840 | } |
| @@ -2793,7 +2871,7 @@ int awk_main(int argc, char **argv) | |||
| 2793 | } | 2871 | } |
| 2794 | 2872 | ||
| 2795 | if (i < 0) | 2873 | if (i < 0) |
| 2796 | runtime_error(strerror(errno)); | 2874 | syntax_error(strerror(errno)); |
| 2797 | 2875 | ||
| 2798 | iF = next_input_file(); | 2876 | iF = next_input_file(); |
| 2799 | } | 2877 | } |
