diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-04-16 22:34:39 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-04-16 22:34:39 +0000 |
commit | 7d4c44e1b102c0fb72603d96278bfa8e7aef73bc (patch) | |
tree | 7b0d54cc6fa9cd93fdd53593d8324e7c86f705c8 | |
parent | ac678ec2f127fc02a93fff2c164e0b13a94a6abf (diff) | |
download | busybox-w32-7d4c44e1b102c0fb72603d96278bfa8e7aef73bc.tar.gz busybox-w32-7d4c44e1b102c0fb72603d96278bfa8e7aef73bc.tar.bz2 busybox-w32-7d4c44e1b102c0fb72603d96278bfa8e7aef73bc.zip |
shells: remove few statics and duplicated code
(much more of the same remains, alas)
function old new delta
doset 330 332 +2
warn 53 51 -2
onecommand 463 461 -2
ioecho 40 38 -2
forkexec 1412 1410 -2
err 81 79 -2
setdash 59 56 -3
flag 4 - -4
msh_main 1389 1384 -5
eval 388 381 -7
subgetc 759 747 -12
static.local 14 - -14
b_adduint 70 52 -18
------------------------------------------------------------------------------
(add/remove: 0/2 grow/shrink: 1/10 up/down: 2/-73) Total: -71 bytes
-rw-r--r-- | shell/hush.c | 34 | ||||
-rw-r--r-- | shell/msh.c | 36 |
2 files changed, 28 insertions, 42 deletions
diff --git a/shell/hush.c b/shell/hush.c index 9489fb276..6173afc80 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -20,7 +20,6 @@ | |||
20 | * rewrites. | 20 | * rewrites. |
21 | * | 21 | * |
22 | * Other credits: | 22 | * Other credits: |
23 | * simple_itoa() was lifted from boa-0.93.15 | ||
24 | * b_addchr() derived from similar w_addchar function in glibc-2.2 | 23 | * b_addchr() derived from similar w_addchar function in glibc-2.2 |
25 | * setup_redirect(), redirect_opt_num(), and big chunks of main() | 24 | * setup_redirect(), redirect_opt_num(), and big chunks of main() |
26 | * and many builtins derived from contributions by Erik Andersen | 25 | * and many builtins derived from contributions by Erik Andersen |
@@ -280,7 +279,7 @@ struct built_in_command { | |||
280 | /* belongs in busybox.h */ | 279 | /* belongs in busybox.h */ |
281 | static int max(int a, int b) | 280 | static int max(int a, int b) |
282 | { | 281 | { |
283 | return (a>b)?a:b; | 282 | return (a > b) ? a : b; |
284 | } | 283 | } |
285 | 284 | ||
286 | /* This should be in utility.c */ | 285 | /* This should be in utility.c */ |
@@ -811,23 +810,12 @@ static int b_addqchr(o_string *o, int ch, int quote) | |||
811 | return b_addchr(o, ch); | 810 | return b_addchr(o, ch); |
812 | } | 811 | } |
813 | 812 | ||
814 | /* belongs in utility.c */ | ||
815 | static char *simple_itoa(unsigned i) | ||
816 | { | ||
817 | static char local[sizeof(int)*3 + 2]; | ||
818 | char *p = &local[sizeof(int)*3 + 2 - 1]; | ||
819 | *p-- = '\0'; | ||
820 | do { | ||
821 | *p-- = '0' + i % 10; | ||
822 | i /= 10; | ||
823 | } while (i > 0); | ||
824 | return p + 1; | ||
825 | } | ||
826 | |||
827 | static int b_adduint(o_string *o, unsigned i) | 813 | static int b_adduint(o_string *o, unsigned i) |
828 | { | 814 | { |
829 | int r; | 815 | int r; |
830 | char *p = simple_itoa(i); | 816 | char buf[sizeof(unsigned)*3 + 1]; |
817 | char *p = buf; | ||
818 | *(utoa_to_buf(i, buf, sizeof(buf))) = '\0'; | ||
831 | /* no escape checking necessary */ | 819 | /* no escape checking necessary */ |
832 | do r = b_addchr(o, *p++); while (r == 0 && *p); | 820 | do r = b_addchr(o, *p++); while (r == 0 && *p); |
833 | return r; | 821 | return r; |
@@ -2008,8 +1996,8 @@ static int reserved_word(o_string *dest, struct p_context *ctx) | |||
2008 | { "do", RES_DO, FLAG_DONE }, | 1996 | { "do", RES_DO, FLAG_DONE }, |
2009 | { "done", RES_DONE, FLAG_END } | 1997 | { "done", RES_DONE, FLAG_END } |
2010 | }; | 1998 | }; |
1999 | enum { NRES = sizeof(reserved_list)/sizeof(reserved_list[0]) }; | ||
2011 | const struct reserved_combo *r; | 2000 | const struct reserved_combo *r; |
2012 | #define NRES sizeof(reserved_list)/sizeof(reserved_list[0]) | ||
2013 | 2001 | ||
2014 | for (r = reserved_list; r < reserved_list+NRES; r++) { | 2002 | for (r = reserved_list; r < reserved_list+NRES; r++) { |
2015 | if (strcmp(dest->data, r->literal) == 0) { | 2003 | if (strcmp(dest->data, r->literal) == 0) { |
@@ -2113,11 +2101,13 @@ static int done_command(struct p_context *ctx) | |||
2113 | struct child_prog *prog = ctx->child; | 2101 | struct child_prog *prog = ctx->child; |
2114 | 2102 | ||
2115 | if (prog && prog->group == NULL | 2103 | if (prog && prog->group == NULL |
2116 | && prog->argv == NULL | 2104 | && prog->argv == NULL |
2117 | && prog->redirects == NULL) { | 2105 | && prog->redirects == NULL |
2106 | ) { | ||
2118 | debug_printf("done_command: skipping null command\n"); | 2107 | debug_printf("done_command: skipping null command\n"); |
2119 | return 0; | 2108 | return 0; |
2120 | } else if (prog) { | 2109 | } |
2110 | if (prog) { | ||
2121 | pi->num_progs++; | 2111 | pi->num_progs++; |
2122 | debug_printf("done_command: num_progs incremented to %d\n", pi->num_progs); | 2112 | debug_printf("done_command: num_progs incremented to %d\n", pi->num_progs); |
2123 | } else { | 2113 | } else { |
@@ -2172,7 +2162,7 @@ static int redirect_dup_num(struct in_str *input) | |||
2172 | return -3; /* "-" represents "close me" */ | 2162 | return -3; /* "-" represents "close me" */ |
2173 | } | 2163 | } |
2174 | while (isdigit(ch)) { | 2164 | while (isdigit(ch)) { |
2175 | d = d*10+(ch-'0'); | 2165 | d = d*10 + (ch-'0'); |
2176 | ok = 1; | 2166 | ok = 1; |
2177 | b_getch(input); | 2167 | b_getch(input); |
2178 | ch = b_peek(input); | 2168 | ch = b_peek(input); |
@@ -2226,7 +2216,7 @@ static FILE *generate_stream_from_list(struct pipe *head) | |||
2226 | } else if (pid == 0) { | 2216 | } else if (pid == 0) { |
2227 | close(channel[0]); | 2217 | close(channel[0]); |
2228 | if (channel[1] != 1) { | 2218 | if (channel[1] != 1) { |
2229 | dup2(channel[1],1); | 2219 | dup2(channel[1], 1); |
2230 | close(channel[1]); | 2220 | close(channel[1]); |
2231 | } | 2221 | } |
2232 | _exit(run_list_real(head)); /* leaks memory */ | 2222 | _exit(run_list_real(head)); /* leaks memory */ |
diff --git a/shell/msh.c b/shell/msh.c index 91e302fac..4feede6fa 100644 --- a/shell/msh.c +++ b/shell/msh.c | |||
@@ -264,10 +264,6 @@ static const char *const T_CMD_NAMES[] = { | |||
264 | #define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM) | 264 | #define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM) |
265 | 265 | ||
266 | 266 | ||
267 | /* PROTOTYPES */ | ||
268 | static int newfile(char *s); | ||
269 | |||
270 | |||
271 | struct brkcon { | 267 | struct brkcon { |
272 | jmp_buf brkpt; | 268 | jmp_buf brkpt; |
273 | struct brkcon *nextlev; | 269 | struct brkcon *nextlev; |
@@ -285,8 +281,8 @@ struct brkcon { | |||
285 | * -u: unset variables net diagnostic | 281 | * -u: unset variables net diagnostic |
286 | */ | 282 | */ |
287 | static char flags['z' - 'a' + 1]; | 283 | static char flags['z' - 'a' + 1]; |
288 | /* this looks weird, but is OK ... we index flag with 'a'...'z' */ | 284 | /* this looks weird, but is OK ... we index FLAG with 'a'...'z' */ |
289 | static char *flag = flags - 'a'; | 285 | #define FLAG (flags - 'a') |
290 | 286 | ||
291 | /* moved to G: static char *trap[_NSIG + 1]; */ | 287 | /* moved to G: static char *trap[_NSIG + 1]; */ |
292 | /* moved to G: static char ourtrap[_NSIG + 1]; */ | 288 | /* moved to G: static char ourtrap[_NSIG + 1]; */ |
@@ -693,7 +689,7 @@ static int intr; /* interrupt pending */ | |||
693 | static int inparse; | 689 | static int inparse; |
694 | static char *null = (char*)""; /* null value for variable */ | 690 | static char *null = (char*)""; /* null value for variable */ |
695 | static int heedint = 1; /* heed interrupt signals */ | 691 | static int heedint = 1; /* heed interrupt signals */ |
696 | static void (*qflag) (int) = SIG_IGN; | 692 | static void (*qflag)(int) = SIG_IGN; |
697 | static int startl; | 693 | static int startl; |
698 | static int peeksym; | 694 | static int peeksym; |
699 | static int nlseen; | 695 | static int nlseen; |
@@ -855,14 +851,14 @@ static void warn(const char *s) | |||
855 | exstat = -1; | 851 | exstat = -1; |
856 | } | 852 | } |
857 | prs("\n"); | 853 | prs("\n"); |
858 | if (flag['e']) | 854 | if (FLAG['e']) |
859 | leave(); | 855 | leave(); |
860 | } | 856 | } |
861 | 857 | ||
862 | static void err(const char *s) | 858 | static void err(const char *s) |
863 | { | 859 | { |
864 | warn(s); | 860 | warn(s); |
865 | if (flag['n']) | 861 | if (FLAG['n']) |
866 | return; | 862 | return; |
867 | if (!interactive) | 863 | if (!interactive) |
868 | leave(); | 864 | leave(); |
@@ -1295,7 +1291,7 @@ static void setdash(void) | |||
1295 | 1291 | ||
1296 | cp = m; | 1292 | cp = m; |
1297 | for (c = 'a'; c <= 'z'; c++) | 1293 | for (c = 'a'; c <= 'z'; c++) |
1298 | if (flag[c]) | 1294 | if (FLAG[c]) |
1299 | *cp++ = c; | 1295 | *cp++ = c; |
1300 | *cp = '\0'; | 1296 | *cp = '\0'; |
1301 | setval(lookup("-"), m); | 1297 | setval(lookup("-"), m); |
@@ -1401,7 +1397,7 @@ static void onecommand(void) | |||
1401 | intr = 0; | 1397 | intr = 0; |
1402 | execflg = 0; | 1398 | execflg = 0; |
1403 | 1399 | ||
1404 | if (!flag['n']) { | 1400 | if (!FLAG['n']) { |
1405 | DBGPRINTF(("ONECOMMAND: calling execute, t=outtree=%p\n", | 1401 | DBGPRINTF(("ONECOMMAND: calling execute, t=outtree=%p\n", |
1406 | outtree)); | 1402 | outtree)); |
1407 | execute(outtree, NOPIPE, NOPIPE, 0); | 1403 | execute(outtree, NOPIPE, NOPIPE, 0); |
@@ -2736,7 +2732,7 @@ static int forkexec(struct op *t, int *pin, int *pout, int act, char **wp) | |||
2736 | 2732 | ||
2737 | /* strip all initial assignments */ | 2733 | /* strip all initial assignments */ |
2738 | /* not correct wrt PATH=yyy command etc */ | 2734 | /* not correct wrt PATH=yyy command etc */ |
2739 | if (flag['x']) { | 2735 | if (FLAG['x']) { |
2740 | DBGPRINTF9(("FORKEXEC: echo'ing, cp=%p, wp=%p, owp=%p\n", | 2736 | DBGPRINTF9(("FORKEXEC: echo'ing, cp=%p, wp=%p, owp=%p\n", |
2741 | cp, wp, owp)); | 2737 | cp, wp, owp)); |
2742 | echo(cp ? wp : owp); | 2738 | echo(cp ? wp : owp); |
@@ -3598,18 +3594,18 @@ static int doset(struct op *t) | |||
3598 | /* bad: t->words++; */ | 3594 | /* bad: t->words++; */ |
3599 | for (n = 0; (t->words[n] = t->words[n + 1]) != NULL; n++); | 3595 | for (n = 0; (t->words[n] = t->words[n + 1]) != NULL; n++); |
3600 | if (*++cp == 0) | 3596 | if (*++cp == 0) |
3601 | flag['x'] = flag['v'] = 0; | 3597 | FLAG['x'] = FLAG['v'] = 0; |
3602 | else { | 3598 | else { |
3603 | for (; *cp; cp++) { | 3599 | for (; *cp; cp++) { |
3604 | switch (*cp) { | 3600 | switch (*cp) { |
3605 | case 'e': | 3601 | case 'e': |
3606 | if (!interactive) | 3602 | if (!interactive) |
3607 | flag['e']++; | 3603 | FLAG['e']++; |
3608 | break; | 3604 | break; |
3609 | 3605 | ||
3610 | default: | 3606 | default: |
3611 | if (*cp >= 'a' && *cp <= 'z') | 3607 | if (*cp >= 'a' && *cp <= 'z') |
3612 | flag[(int) *cp]++; | 3608 | FLAG[(int) *cp]++; |
3613 | break; | 3609 | break; |
3614 | } | 3610 | } |
3615 | } | 3611 | } |
@@ -3692,14 +3688,14 @@ static char **eval(char **ap, int f) | |||
3692 | if (newenv(setjmp(errpt)) == 0) { | 3688 | if (newenv(setjmp(errpt)) == 0) { |
3693 | while (*ap && isassign(*ap)) | 3689 | while (*ap && isassign(*ap)) |
3694 | expand(*ap++, &wb, f & ~DOGLOB); | 3690 | expand(*ap++, &wb, f & ~DOGLOB); |
3695 | if (flag['k']) { | 3691 | if (FLAG['k']) { |
3696 | for (wf = ap; *wf; wf++) { | 3692 | for (wf = ap; *wf; wf++) { |
3697 | if (isassign(*wf)) | 3693 | if (isassign(*wf)) |
3698 | expand(*wf, &wb, f & ~DOGLOB); | 3694 | expand(*wf, &wb, f & ~DOGLOB); |
3699 | } | 3695 | } |
3700 | } | 3696 | } |
3701 | for (wb = addword((char *) 0, wb); *ap; ap++) { | 3697 | for (wb = addword((char *) 0, wb); *ap; ap++) { |
3702 | if (!flag['k'] || !isassign(*ap)) | 3698 | if (!FLAG['k'] || !isassign(*ap)) |
3703 | expand(*ap, &wb, f & ~DOKEY); | 3699 | expand(*ap, &wb, f & ~DOKEY); |
3704 | } | 3700 | } |
3705 | wb = addword((char *) 0, wb); | 3701 | wb = addword((char *) 0, wb); |
@@ -3992,7 +3988,7 @@ static int dollar(int quoted) | |||
3992 | } | 3988 | } |
3993 | } else if (c == '+') | 3989 | } else if (c == '+') |
3994 | dolp = strsave(cp, areanum); | 3990 | dolp = strsave(cp, areanum); |
3995 | if (flag['u'] && dolp == null) { | 3991 | if (FLAG['u'] && dolp == null) { |
3996 | prs("unset variable: "); | 3992 | prs("unset variable: "); |
3997 | err(s); | 3993 | err(s); |
3998 | gflg++; | 3994 | gflg++; |
@@ -4613,7 +4609,7 @@ static int readc(void) | |||
4613 | 4609 | ||
4614 | static void ioecho(char c) | 4610 | static void ioecho(char c) |
4615 | { | 4611 | { |
4616 | if (flag['v']) | 4612 | if (FLAG['v']) |
4617 | write(2, &c, sizeof c); | 4613 | write(2, &c, sizeof c); |
4618 | } | 4614 | } |
4619 | 4615 | ||
@@ -5278,7 +5274,7 @@ int msh_main(int argc, char **argv) | |||
5278 | interactive++; | 5274 | interactive++; |
5279 | default: | 5275 | default: |
5280 | if (*s >= 'a' && *s <= 'z') | 5276 | if (*s >= 'a' && *s <= 'z') |
5281 | flag[(int) *s]++; | 5277 | FLAG[(int) *s]++; |
5282 | } | 5278 | } |
5283 | } else { | 5279 | } else { |
5284 | argv--; | 5280 | argv--; |