aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-03-11 12:41:55 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2010-03-11 12:41:55 +0100
commit6ebdf7aa7bc2eac2b43601d5b9ec70af22e38af3 (patch)
tree7e4fda7c650f71ba2ac3d225cb208b35b211f544
parentda62b09ab1def06ed8844c4df7b7eacf05474284 (diff)
downloadbusybox-w32-6ebdf7aa7bc2eac2b43601d5b9ec70af22e38af3.tar.gz
busybox-w32-6ebdf7aa7bc2eac2b43601d5b9ec70af22e38af3.tar.bz2
busybox-w32-6ebdf7aa7bc2eac2b43601d5b9ec70af22e38af3.zip
awk: code shrink; style fixes
function old new delta next_token 932 862 -70 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--editors/awk.c112
1 files changed, 71 insertions, 41 deletions
diff --git a/editors/awk.c b/editors/awk.c
index 397d70f4c..829d8a425 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -674,10 +674,11 @@ static void skip_spaces(char **s)
674 *s = p; 674 *s = p;
675} 675}
676 676
677/* returns old *s, advances *s past word and terminating NUL */
677static char *nextword(char **s) 678static char *nextword(char **s)
678{ 679{
679 char *p = *s; 680 char *p = *s;
680 while (*(*s)++) 681 while (*(*s)++ != '\0')
681 continue; 682 continue;
682 return p; 683 return p;
683} 684}
@@ -686,12 +687,12 @@ static char nextchar(char **s)
686{ 687{
687 char c, *pps; 688 char c, *pps;
688 689
689 c = *((*s)++); 690 c = *(*s)++;
690 pps = *s; 691 pps = *s;
691 if (c == '\\') 692 if (c == '\\')
692 c = bb_process_escape_sequence((const char**)s); 693 c = bb_process_escape_sequence((const char**)s);
693 if (c == '\\' && *s == pps) 694 if (c == '\\' && *s == pps)
694 c = *((*s)++); 695 c = *(*s)++;
695 return c; 696 return c;
696} 697}
697 698
@@ -963,7 +964,7 @@ static uint32_t next_token(uint32_t expected)
963/* Initialized to TC_OPTERM: */ 964/* Initialized to TC_OPTERM: */
964#define ltclass (G.next_token__ltclass) 965#define ltclass (G.next_token__ltclass)
965 966
966 char *p, *pp, *s; 967 char *p, *s;
967 const char *tl; 968 const char *tl;
968 uint32_t tc; 969 uint32_t tc;
969 const uint32_t *ti; 970 const uint32_t *ti;
@@ -996,9 +997,11 @@ static uint32_t next_token(uint32_t expected)
996 /* it's a string */ 997 /* it's a string */
997 t_string = s = ++p; 998 t_string = s = ++p;
998 while (*p != '\"') { 999 while (*p != '\"') {
1000 char *pp = p;
999 if (*p == '\0' || *p == '\n') 1001 if (*p == '\0' || *p == '\n')
1000 syntax_error(EMSG_UNEXP_EOS); 1002 syntax_error(EMSG_UNEXP_EOS);
1001 *(s++) = nextchar(&p); 1003 *s++ = nextchar(&pp);
1004 p = pp;
1002 } 1005 }
1003 p++; 1006 p++;
1004 *s = '\0'; 1007 *s = '\0';
@@ -1012,12 +1015,14 @@ static uint32_t next_token(uint32_t expected)
1012 syntax_error(EMSG_UNEXP_EOS); 1015 syntax_error(EMSG_UNEXP_EOS);
1013 *s = *p++; 1016 *s = *p++;
1014 if (*s++ == '\\') { 1017 if (*s++ == '\\') {
1015 pp = p; 1018 char *pp = p;
1016 *(s-1) = bb_process_escape_sequence((const char **)&p); 1019 s[-1] = bb_process_escape_sequence((const char **)&pp);
1017 if (*pp == '\\') 1020 if (*p == '\\')
1018 *s++ = '\\'; 1021 *s++ = '\\';
1019 if (p == pp) 1022 if (pp == p)
1020 *s++ = *p++; 1023 *s++ = *p++;
1024 else
1025 p = pp;
1021 } 1026 }
1022 } 1027 }
1023 p++; 1028 p++;
@@ -1026,8 +1031,10 @@ static uint32_t next_token(uint32_t expected)
1026 1031
1027 } else if (*p == '.' || isdigit(*p)) { 1032 } else if (*p == '.' || isdigit(*p)) {
1028 /* it's a number */ 1033 /* it's a number */
1029 t_double = my_strtod(&p); 1034 char *pp = p;
1030 if (*p == '.') 1035 t_double = my_strtod(&pp);
1036 p = pp;
1037 if (*pp == '.')
1031 syntax_error(EMSG_UNEXP_TOKEN); 1038 syntax_error(EMSG_UNEXP_TOKEN);
1032 tc = TC_NUMBER; 1039 tc = TC_NUMBER;
1033 1040
@@ -1037,7 +1044,7 @@ static uint32_t next_token(uint32_t expected)
1037 tc = 0x00000001; 1044 tc = 0x00000001;
1038 ti = tokeninfo; 1045 ti = tokeninfo;
1039 while (*tl) { 1046 while (*tl) {
1040 l = *(tl++); 1047 l = *tl++;
1041 if (l == NTCC) { 1048 if (l == NTCC) {
1042 tc <<= 1; 1049 tc <<= 1;
1043 continue; 1050 continue;
@@ -1066,10 +1073,10 @@ static uint32_t next_token(uint32_t expected)
1066 syntax_error(EMSG_UNEXP_TOKEN); 1073 syntax_error(EMSG_UNEXP_TOKEN);
1067 1074
1068 t_string = --p; 1075 t_string = --p;
1069 while (isalnum_(*(++p))) { 1076 while (isalnum_(*++p)) {
1070 *(p-1) = *p; 1077 p[-1] = *p;
1071 } 1078 }
1072 *(p-1) = '\0'; 1079 p[-1] = '\0';
1073 tc = TC_VARIABLE; 1080 tc = TC_VARIABLE;
1074 /* also consume whitespace between functionname and bracket */ 1081 /* also consume whitespace between functionname and bracket */
1075 if (!(expected & TC_VARIABLE) || (expected & TC_ARRAY)) 1082 if (!(expected & TC_VARIABLE) || (expected & TC_ARRAY))
@@ -1332,7 +1339,8 @@ static void chain_group(void)
1332 1339
1333 if (c & TC_GRPSTART) { 1340 if (c & TC_GRPSTART) {
1334 while (next_token(TC_GRPSEQ | TC_GRPTERM) != TC_GRPTERM) { 1341 while (next_token(TC_GRPSEQ | TC_GRPTERM) != TC_GRPTERM) {
1335 if (t_tclass & TC_NEWLINE) continue; 1342 if (t_tclass & TC_NEWLINE)
1343 continue;
1336 rollback_token(); 1344 rollback_token();
1337 chain_group(); 1345 chain_group();
1338 } 1346 }
@@ -1628,7 +1636,8 @@ static int awk_split(const char *s, node *spl, char **slist)
1628 c[0] = toupper(c[0]); 1636 c[0] = toupper(c[0]);
1629 c[1] = tolower(c[1]); 1637 c[1] = tolower(c[1]);
1630 } 1638 }
1631 if (*s1) n++; 1639 if (*s1)
1640 n++;
1632 while ((s1 = strpbrk(s1, c))) { 1641 while ((s1 = strpbrk(s1, c))) {
1633 *s1++ = '\0'; 1642 *s1++ = '\0';
1634 n++; 1643 n++;
@@ -1638,7 +1647,8 @@ static int awk_split(const char *s, node *spl, char **slist)
1638 /* space split */ 1647 /* space split */
1639 while (*s) { 1648 while (*s) {
1640 s = skip_whitespace(s); 1649 s = skip_whitespace(s);
1641 if (!*s) break; 1650 if (!*s)
1651 break;
1642 n++; 1652 n++;
1643 while (*s && !isspace(*s)) 1653 while (*s && !isspace(*s))
1644 *s1++ = *s++; 1654 *s1++ = *s++;
@@ -1836,7 +1846,8 @@ static int awk_getline(rstream *rsm, var *v)
1836 } 1846 }
1837 } else if (c != '\0') { 1847 } else if (c != '\0') {
1838 s = strchr(b+pp, c); 1848 s = strchr(b+pp, c);
1839 if (!s) s = memchr(b+pp, '\0', p - pp); 1849 if (!s)
1850 s = memchr(b+pp, '\0', p - pp);
1840 if (s) { 1851 if (s) {
1841 so = eo = s-b; 1852 so = eo = s-b;
1842 eo++; 1853 eo++;
@@ -1931,7 +1942,7 @@ static char *awk_printf(node *n)
1931 i = 0; 1942 i = 0;
1932 while (*f) { 1943 while (*f) {
1933 s = f; 1944 s = f;
1934 while (*f && (*f != '%' || *(++f) == '%')) 1945 while (*f && (*f != '%' || *++f == '%'))
1935 f++; 1946 f++;
1936 while (*f && !isalpha(*f)) { 1947 while (*f && !isalpha(*f)) {
1937 if (*f == '*') 1948 if (*f == '*')
@@ -1942,7 +1953,8 @@ static char *awk_printf(node *n)
1942 incr = (f - s) + MAXVARFMT; 1953 incr = (f - s) + MAXVARFMT;
1943 b = qrealloc(b, incr + i, &bsize); 1954 b = qrealloc(b, incr + i, &bsize);
1944 c = *f; 1955 c = *f;
1945 if (c != '\0') f++; 1956 if (c != '\0')
1957 f++;
1946 c1 = *f; 1958 c1 = *f;
1947 *f = '\0'; 1959 *f = '\0';
1948 arg = evaluate(nextarg(&n), v); 1960 arg = evaluate(nextarg(&n), v);
@@ -1961,7 +1973,8 @@ static char *awk_printf(node *n)
1961 *f = c1; 1973 *f = c1;
1962 1974
1963 /* if there was an error while sprintf, return value is negative */ 1975 /* if there was an error while sprintf, return value is negative */
1964 if (i < j) i = j; 1976 if (i < j)
1977 i = j;
1965 } 1978 }
1966 1979
1967 b = xrealloc(b, i + 1); 1980 b = xrealloc(b, i + 1);
@@ -1987,8 +2000,10 @@ static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int
1987 regex_t sreg, *re; 2000 regex_t sreg, *re;
1988 2001
1989 re = as_regex(rn, &sreg); 2002 re = as_regex(rn, &sreg);
1990 if (!src) src = intvar[F0]; 2003 if (!src)
1991 if (!dest) dest = intvar[F0]; 2004 src = intvar[F0];
2005 if (!dest)
2006 dest = intvar[F0];
1992 2007
1993 i = di = 0; 2008 i = di = 0;
1994 sp = getvar_s(src); 2009 sp = getvar_s(src);
@@ -2100,8 +2115,10 @@ static NOINLINE var *exec_builtin(node *op, var *res)
2100 av[2] = av[3] = NULL; 2115 av[2] = av[3] = NULL;
2101 for (i = 0; i < 4 && op; i++) { 2116 for (i = 0; i < 4 && op; i++) {
2102 an[i] = nextarg(&op); 2117 an[i] = nextarg(&op);
2103 if (isr & 0x09000000) av[i] = evaluate(an[i], &tv[i]); 2118 if (isr & 0x09000000)
2104 if (isr & 0x08000000) as[i] = getvar_s(av[i]); 2119 av[i] = evaluate(an[i], &tv[i]);
2120 if (isr & 0x08000000)
2121 as[i] = getvar_s(av[i]);
2105 isr >>= 1; 2122 isr >>= 1;
2106 } 2123 }
2107 2124
@@ -2140,10 +2157,13 @@ static NOINLINE var *exec_builtin(node *op, var *res)
2140 case B_ss: 2157 case B_ss:
2141 l = strlen(as[0]); 2158 l = strlen(as[0]);
2142 i = getvar_i(av[1]) - 1; 2159 i = getvar_i(av[1]) - 1;
2143 if (i > l) i = l; 2160 if (i > l)
2144 if (i < 0) i = 0; 2161 i = l;
2162 if (i < 0)
2163 i = 0;
2145 n = (nargs > 2) ? getvar_i(av[2]) : l-i; 2164 n = (nargs > 2) ? getvar_i(av[2]) : l-i;
2146 if (n < 0) n = 0; 2165 if (n < 0)
2166 n = 0;
2147 s = xstrndup(as[0]+i, n); 2167 s = xstrndup(as[0]+i, n);
2148 setvar_p(res, s); 2168 setvar_p(res, s);
2149 break; 2169 break;
@@ -2193,7 +2213,8 @@ static NOINLINE var *exec_builtin(node *op, var *res)
2193 if (ll > 0 && l >= 0) { 2213 if (ll > 0 && l >= 0) {
2194 if (!icase) { 2214 if (!icase) {
2195 s = strstr(as[0], as[1]); 2215 s = strstr(as[0], as[1]);
2196 if (s) n = (s - as[0]) + 1; 2216 if (s)
2217 n = (s - as[0]) + 1;
2197 } else { 2218 } else {
2198 /* this piece of code is terribly slow and 2219 /* this piece of code is terribly slow and
2199 * really should be rewritten 2220 * really should be rewritten
@@ -2239,7 +2260,8 @@ static NOINLINE var *exec_builtin(node *op, var *res)
2239 setvar_i(newvar("RSTART"), pmatch[0].rm_so); 2260 setvar_i(newvar("RSTART"), pmatch[0].rm_so);
2240 setvar_i(newvar("RLENGTH"), pmatch[0].rm_eo - pmatch[0].rm_so); 2261 setvar_i(newvar("RLENGTH"), pmatch[0].rm_eo - pmatch[0].rm_so);
2241 setvar_i(res, pmatch[0].rm_so); 2262 setvar_i(res, pmatch[0].rm_so);
2242 if (re == &sreg) regfree(re); 2263 if (re == &sreg)
2264 regfree(re);
2243 break; 2265 break;
2244 2266
2245 case B_ge: 2267 case B_ge:
@@ -2305,11 +2327,16 @@ static var *evaluate(node *op, var *res)
2305 2327
2306 /* execute inevitable things */ 2328 /* execute inevitable things */
2307 op1 = op->l.n; 2329 op1 = op->l.n;
2308 if (opinfo & OF_RES1) X.v = L.v = evaluate(op1, v1); 2330 if (opinfo & OF_RES1)
2309 if (opinfo & OF_RES2) R.v = evaluate(op->r.n, v1+1); 2331 X.v = L.v = evaluate(op1, v1);
2310 if (opinfo & OF_STR1) L.s = getvar_s(L.v); 2332 if (opinfo & OF_RES2)
2311 if (opinfo & OF_STR2) R.s = getvar_s(R.v); 2333 R.v = evaluate(op->r.n, v1+1);
2312 if (opinfo & OF_NUM1) L.d = getvar_i(L.v); 2334 if (opinfo & OF_STR1)
2335 L.s = getvar_s(L.v);
2336 if (opinfo & OF_STR2)
2337 R.s = getvar_s(R.v);
2338 if (opinfo & OF_NUM1)
2339 L.d = getvar_i(L.v);
2313 2340
2314 switch (XC(opinfo & OPCLSMASK)) { 2341 switch (XC(opinfo & OPCLSMASK)) {
2315 2342
@@ -2384,7 +2411,8 @@ static var *evaluate(node *op, var *res)
2384 fputs(getvar_s(L.v), X.F); 2411 fputs(getvar_s(L.v), X.F);
2385 } 2412 }
2386 2413
2387 if (op1) fputs(getvar_s(intvar[OFS]), X.F); 2414 if (op1)
2415 fputs(getvar_s(intvar[OFS]), X.F);
2388 } 2416 }
2389 } 2417 }
2390 fputs(getvar_s(intvar[ORS]), X.F); 2418 fputs(getvar_s(intvar[ORS]), X.F);
@@ -2463,7 +2491,8 @@ static var *evaluate(node *op, var *res)
2463 re_cont: 2491 re_cont:
2464 X.re = as_regex(op1, &sreg); 2492 X.re = as_regex(op1, &sreg);
2465 R.i = regexec(X.re, L.s, 0, NULL, 0); 2493 R.i = regexec(X.re, L.s, 0, NULL, 0);
2466 if (X.re == &sreg) regfree(X.re); 2494 if (X.re == &sreg)
2495 regfree(X.re);
2467 setvar_i(res, (R.i == 0) ^ (opn == '!')); 2496 setvar_i(res, (R.i == 0) ^ (opn == '!'));
2468 break; 2497 break;
2469 2498
@@ -2523,7 +2552,8 @@ static var *evaluate(node *op, var *res)
2523 } 2552 }
2524 } 2553 }
2525 } else { 2554 } else {
2526 if (!iF) iF = next_input_file(); 2555 if (!iF)
2556 iF = next_input_file();
2527 X.rsm = iF; 2557 X.rsm = iF;
2528 } 2558 }
2529 2559
@@ -2822,10 +2852,10 @@ static int is_assignment(const char *expr)
2822 return FALSE; 2852 return FALSE;
2823 } 2853 }
2824 2854
2825 *(s++) = '\0'; 2855 *s++ = '\0';
2826 s0 = s1 = s; 2856 s0 = s1 = s;
2827 while (*s) 2857 while (*s)
2828 *(s1++) = nextchar(&s); 2858 *s1++ = nextchar(&s);
2829 2859
2830 *s1 = '\0'; 2860 *s1 = '\0';
2831 setvar_u(newvar(exprc), s0); 2861 setvar_u(newvar(exprc), s0);