diff options
author | Denys Vlasenko <dvlasenk@redhat.com> | 2010-10-05 16:49:03 +0200 |
---|---|---|
committer | Denys Vlasenko <dvlasenk@redhat.com> | 2010-10-05 16:49:03 +0200 |
commit | 28458c64db779c27ecd63f400ea9af0e3e6b555a (patch) | |
tree | 8e7dc9dd5a8743b4968859ed4c3afa6239566aad | |
parent | d527e0c81d2efe98f258f1e1516a0e9cbe879154 (diff) | |
download | busybox-w32-28458c64db779c27ecd63f400ea9af0e3e6b555a.tar.gz busybox-w32-28458c64db779c27ecd63f400ea9af0e3e6b555a.tar.bz2 busybox-w32-28458c64db779c27ecd63f400ea9af0e3e6b555a.zip |
awk: code shrink
function old new delta
fsrealloc 112 107 -5
next_token 862 844 -18
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
-rw-r--r-- | editors/awk.c | 84 |
1 files changed, 41 insertions, 43 deletions
diff --git a/editors/awk.c b/editors/awk.c index d9f9e0f6e..2245cad03 100644 --- a/editors/awk.c +++ b/editors/awk.c | |||
@@ -987,7 +987,6 @@ static uint32_t next_token(uint32_t expected) | |||
987 | const char *tl; | 987 | const char *tl; |
988 | uint32_t tc; | 988 | uint32_t tc; |
989 | const uint32_t *ti; | 989 | const uint32_t *ti; |
990 | int l; | ||
991 | 990 | ||
992 | if (t_rollback) { | 991 | if (t_rollback) { |
993 | t_rollback = FALSE; | 992 | t_rollback = FALSE; |
@@ -1053,7 +1052,7 @@ static uint32_t next_token(uint32_t expected) | |||
1053 | char *pp = p; | 1052 | char *pp = p; |
1054 | t_double = my_strtod(&pp); | 1053 | t_double = my_strtod(&pp); |
1055 | p = pp; | 1054 | p = pp; |
1056 | if (*pp == '.') | 1055 | if (*p == '.') |
1057 | syntax_error(EMSG_UNEXP_TOKEN); | 1056 | syntax_error(EMSG_UNEXP_TOKEN); |
1058 | tc = TC_NUMBER; | 1057 | tc = TC_NUMBER; |
1059 | 1058 | ||
@@ -1063,52 +1062,51 @@ static uint32_t next_token(uint32_t expected) | |||
1063 | tc = 0x00000001; | 1062 | tc = 0x00000001; |
1064 | ti = tokeninfo; | 1063 | ti = tokeninfo; |
1065 | while (*tl) { | 1064 | while (*tl) { |
1066 | l = *tl++; | 1065 | int l = (unsigned char) *tl++; |
1067 | if (l == NTCC) { | 1066 | if (l == (unsigned char) NTCC) { |
1068 | tc <<= 1; | 1067 | tc <<= 1; |
1069 | continue; | 1068 | continue; |
1070 | } | 1069 | } |
1071 | /* if token class is expected, token | 1070 | /* if token class is expected, |
1072 | * matches and it's not a longer word, | 1071 | * token matches, |
1073 | * then this is what we are looking for | 1072 | * and it's not a longer word, |
1074 | */ | 1073 | */ |
1075 | if ((tc & (expected | TC_WORD | TC_NEWLINE)) | 1074 | if ((tc & (expected | TC_WORD | TC_NEWLINE)) |
1076 | && *tl == *p && strncmp(p, tl, l) == 0 | 1075 | && strncmp(p, tl, l) == 0 |
1077 | && !((tc & TC_WORD) && isalnum_(p[l])) | 1076 | && !((tc & TC_WORD) && isalnum_(p[l])) |
1078 | ) { | 1077 | ) { |
1078 | /* then this is what we are looking for */ | ||
1079 | t_info = *ti; | 1079 | t_info = *ti; |
1080 | p += l; | 1080 | p += l; |
1081 | break; | 1081 | goto token_found; |
1082 | } | 1082 | } |
1083 | ti++; | 1083 | ti++; |
1084 | tl += l; | 1084 | tl += l; |
1085 | } | 1085 | } |
1086 | 1086 | /* not a known token */ | |
1087 | if (!*tl) { | 1087 | |
1088 | /* it's a name (var/array/function), | 1088 | /* is it a name? (var/array/function) */ |
1089 | * otherwise it's something wrong | 1089 | if (!isalnum_(*p)) |
1090 | */ | 1090 | syntax_error(EMSG_UNEXP_TOKEN); /* no */ |
1091 | if (!isalnum_(*p)) | 1091 | /* yes */ |
1092 | syntax_error(EMSG_UNEXP_TOKEN); | 1092 | t_string = --p; |
1093 | 1093 | while (isalnum_(*++p)) { | |
1094 | t_string = --p; | 1094 | p[-1] = *p; |
1095 | while (isalnum_(*++p)) { | 1095 | } |
1096 | p[-1] = *p; | 1096 | p[-1] = '\0'; |
1097 | } | 1097 | tc = TC_VARIABLE; |
1098 | p[-1] = '\0'; | 1098 | /* also consume whitespace between functionname and bracket */ |
1099 | tc = TC_VARIABLE; | 1099 | if (!(expected & TC_VARIABLE) || (expected & TC_ARRAY)) |
1100 | /* also consume whitespace between functionname and bracket */ | 1100 | p = skip_spaces(p); |
1101 | if (!(expected & TC_VARIABLE) || (expected & TC_ARRAY)) | 1101 | if (*p == '(') { |
1102 | p = skip_spaces(p); | 1102 | tc = TC_FUNCTION; |
1103 | if (*p == '(') { | 1103 | } else { |
1104 | tc = TC_FUNCTION; | 1104 | if (*p == '[') { |
1105 | } else { | 1105 | p++; |
1106 | if (*p == '[') { | 1106 | tc = TC_ARRAY; |
1107 | p++; | ||
1108 | tc = TC_ARRAY; | ||
1109 | } | ||
1110 | } | 1107 | } |
1111 | } | 1108 | } |
1109 | token_found: ; | ||
1112 | } | 1110 | } |
1113 | g_pos = p; | 1111 | g_pos = p; |
1114 | 1112 | ||
@@ -1186,6 +1184,7 @@ static node *parse_expr(uint32_t iexp) | |||
1186 | xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP | iexp; | 1184 | xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP | iexp; |
1187 | 1185 | ||
1188 | while (!((tc = next_token(xtc)) & iexp)) { | 1186 | while (!((tc = next_token(xtc)) & iexp)) { |
1187 | |||
1189 | if (glptr && (t_info == (OC_COMPARE | VV | P(39) | 2))) { | 1188 | if (glptr && (t_info == (OC_COMPARE | VV | P(39) | 2))) { |
1190 | /* input redirection (<) attached to glptr node */ | 1189 | /* input redirection (<) attached to glptr node */ |
1191 | cn = glptr->l.n = new_node(OC_CONCAT | SS | P(37)); | 1190 | cn = glptr->l.n = new_node(OC_CONCAT | SS | P(37)); |
@@ -1522,10 +1521,10 @@ static node *mk_splitter(const char *s, tsplitter *spl) | |||
1522 | regfree(re); | 1521 | regfree(re); |
1523 | regfree(ire); // TODO: nuke ire, use re+1? | 1522 | regfree(ire); // TODO: nuke ire, use re+1? |
1524 | } | 1523 | } |
1525 | if (strlen(s) > 1) { | 1524 | if (s[0] && s[1]) { /* strlen(s) > 1 */ |
1526 | mk_re_node(s, n, re); | 1525 | mk_re_node(s, n, re); |
1527 | } else { | 1526 | } else { |
1528 | n->info = (uint32_t) *s; | 1527 | n->info = (uint32_t) s[0]; |
1529 | } | 1528 | } |
1530 | 1529 | ||
1531 | return n; | 1530 | return n; |
@@ -1582,24 +1581,22 @@ static void fsrealloc(int size) | |||
1582 | if (size >= maxfields) { | 1581 | if (size >= maxfields) { |
1583 | i = maxfields; | 1582 | i = maxfields; |
1584 | maxfields = size + 16; | 1583 | maxfields = size + 16; |
1585 | Fields = xrealloc(Fields, maxfields * sizeof(var)); | 1584 | Fields = xrealloc(Fields, maxfields * sizeof(Fields[0])); |
1586 | for (; i < maxfields; i++) { | 1585 | for (; i < maxfields; i++) { |
1587 | Fields[i].type = VF_SPECIAL; | 1586 | Fields[i].type = VF_SPECIAL; |
1588 | Fields[i].string = NULL; | 1587 | Fields[i].string = NULL; |
1589 | } | 1588 | } |
1590 | } | 1589 | } |
1591 | 1590 | /* if size < nfields, clear extra field variables */ | |
1592 | if (size < nfields) { | 1591 | for (i = size; i < nfields; i++) { |
1593 | for (i = size; i < nfields; i++) { | 1592 | clrvar(Fields + i); |
1594 | clrvar(Fields + i); | ||
1595 | } | ||
1596 | } | 1593 | } |
1597 | nfields = size; | 1594 | nfields = size; |
1598 | } | 1595 | } |
1599 | 1596 | ||
1600 | static int awk_split(const char *s, node *spl, char **slist) | 1597 | static int awk_split(const char *s, node *spl, char **slist) |
1601 | { | 1598 | { |
1602 | int l, n = 0; | 1599 | int l, n; |
1603 | char c[4]; | 1600 | char c[4]; |
1604 | char *s1; | 1601 | char *s1; |
1605 | regmatch_t pmatch[2]; // TODO: why [2]? [1] is enough... | 1602 | regmatch_t pmatch[2]; // TODO: why [2]? [1] is enough... |
@@ -1613,6 +1610,7 @@ static int awk_split(const char *s, node *spl, char **slist) | |||
1613 | if (*getvar_s(intvar[RS]) == '\0') | 1610 | if (*getvar_s(intvar[RS]) == '\0') |
1614 | c[2] = '\n'; | 1611 | c[2] = '\n'; |
1615 | 1612 | ||
1613 | n = 0; | ||
1616 | if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */ | 1614 | if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */ |
1617 | if (!*s) | 1615 | if (!*s) |
1618 | return n; /* "": zero fields */ | 1616 | return n; /* "": zero fields */ |
@@ -1658,7 +1656,7 @@ static int awk_split(const char *s, node *spl, char **slist) | |||
1658 | } | 1656 | } |
1659 | if (*s1) | 1657 | if (*s1) |
1660 | n++; | 1658 | n++; |
1661 | while ((s1 = strpbrk(s1, c))) { | 1659 | while ((s1 = strpbrk(s1, c)) != NULL) { |
1662 | *s1++ = '\0'; | 1660 | *s1++ = '\0'; |
1663 | n++; | 1661 | n++; |
1664 | } | 1662 | } |