diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2015-10-07 15:44:36 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2015-10-07 15:44:36 +0200 |
commit | 9c5410023a9d1920c336ed4d9ceaad586ce43328 (patch) | |
tree | 4abde80ca1b0e5d3e089f0e20c00225115089b94 | |
parent | 2007ef5c3cd351ab15b9cb70dd65bf9105403822 (diff) | |
download | busybox-w32-9c5410023a9d1920c336ed4d9ceaad586ce43328.tar.gz busybox-w32-9c5410023a9d1920c336ed4d9ceaad586ce43328.tar.bz2 busybox-w32-9c5410023a9d1920c336ed4d9ceaad586ce43328.zip |
ash: a bunch of trivial simplifications
Also, in a few places made code more reliable wrt large sizeof(int)
and sizeof(arith_t)..
function old new delta
sprint_status48 - 143 +143
newline_and_flush - 56 +56
showjob 365 382 +17
parse_command 1440 1443 +3
cmdputs 334 332 -2
cmdloop 434 429 -5
showjobs 70 64 -6
fg_bgcmd 296 290 -6
ash_vmsg 159 153 -6
ash_main 1487 1481 -6
jobscmd 94 82 -12
getoptscmd 687 632 -55
outcslow 56 - -56
sprint_status 156 - -156
------------------------------------------------------------------------------
(add/remove: 2/2 grow/shrink: 2/8 up/down: 219/-310) Total: -91 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | shell/ash.c | 72 |
1 files changed, 37 insertions, 35 deletions
diff --git a/shell/ash.c b/shell/ash.c index 42c91257e..b34dcc149 100644 --- a/shell/ash.c +++ b/shell/ash.c | |||
@@ -537,11 +537,12 @@ flush_stdout_stderr(void) | |||
537 | INT_ON; | 537 | INT_ON; |
538 | } | 538 | } |
539 | 539 | ||
540 | /* Was called outcslow(c,FILE*), but c was always '\n' */ | ||
540 | static void | 541 | static void |
541 | outcslow(int c, FILE *dest) | 542 | newline_and_flush(FILE *dest) |
542 | { | 543 | { |
543 | INT_OFF; | 544 | INT_OFF; |
544 | putc(c, dest); | 545 | putc('\n', dest); |
545 | fflush(dest); | 546 | fflush(dest); |
546 | INT_ON; | 547 | INT_ON; |
547 | } | 548 | } |
@@ -1202,7 +1203,7 @@ ash_vmsg(const char *msg, va_list ap) | |||
1202 | fprintf(stderr, "line %d: ", startlinno); | 1203 | fprintf(stderr, "line %d: ", startlinno); |
1203 | } | 1204 | } |
1204 | vfprintf(stderr, msg, ap); | 1205 | vfprintf(stderr, msg, ap); |
1205 | outcslow('\n', stderr); | 1206 | newline_and_flush(stderr); |
1206 | } | 1207 | } |
1207 | 1208 | ||
1208 | /* | 1209 | /* |
@@ -3336,6 +3337,7 @@ unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) | |||
3336 | #define SHOW_ONLY_PGID 0x01 /* show only pgid (jobs -p) */ | 3337 | #define SHOW_ONLY_PGID 0x01 /* show only pgid (jobs -p) */ |
3337 | #define SHOW_PIDS 0x02 /* show individual pids, not just one line per job */ | 3338 | #define SHOW_PIDS 0x02 /* show individual pids, not just one line per job */ |
3338 | #define SHOW_CHANGED 0x04 /* only jobs whose state has changed */ | 3339 | #define SHOW_CHANGED 0x04 /* only jobs whose state has changed */ |
3340 | #define SHOW_STDERR 0x08 /* print to stderr (else stdout) */ | ||
3339 | 3341 | ||
3340 | /* | 3342 | /* |
3341 | * A job structure contains information about a job. A job is either a | 3343 | * A job structure contains information about a job. A job is either a |
@@ -3850,7 +3852,7 @@ showpipe(struct job *jp /*, FILE *out*/) | |||
3850 | psend = jp->ps + jp->nprocs; | 3852 | psend = jp->ps + jp->nprocs; |
3851 | for (ps = jp->ps + 1; ps < psend; ps++) | 3853 | for (ps = jp->ps + 1; ps < psend; ps++) |
3852 | printf(" | %s", ps->ps_cmd); | 3854 | printf(" | %s", ps->ps_cmd); |
3853 | outcslow('\n', stdout); | 3855 | newline_and_flush(stdout); |
3854 | flush_stdout_stderr(); | 3856 | flush_stdout_stderr(); |
3855 | } | 3857 | } |
3856 | 3858 | ||
@@ -3910,7 +3912,7 @@ fg_bgcmd(int argc UNUSED_PARAM, char **argv) | |||
3910 | #endif | 3912 | #endif |
3911 | 3913 | ||
3912 | static int | 3914 | static int |
3913 | sprint_status(char *s, int status, int sigonly) | 3915 | sprint_status48(char *s, int status, int sigonly) |
3914 | { | 3916 | { |
3915 | int col; | 3917 | int col; |
3916 | int st; | 3918 | int st; |
@@ -3935,14 +3937,12 @@ sprint_status(char *s, int status, int sigonly) | |||
3935 | //TODO: use bbox's get_signame? strsignal adds ~600 bytes to text+rodata | 3937 | //TODO: use bbox's get_signame? strsignal adds ~600 bytes to text+rodata |
3936 | col = fmtstr(s, 32, strsignal(st)); | 3938 | col = fmtstr(s, 32, strsignal(st)); |
3937 | if (WCOREDUMP(status)) { | 3939 | if (WCOREDUMP(status)) { |
3938 | col += fmtstr(s + col, 16, " (core dumped)"); | 3940 | strcpy(s + col, " (core dumped)"); |
3941 | col += sizeof(" (core dumped)")-1; | ||
3939 | } | 3942 | } |
3940 | } else if (!sigonly) { | 3943 | } else if (!sigonly) { |
3941 | st = WEXITSTATUS(status); | 3944 | st = WEXITSTATUS(status); |
3942 | if (st) | 3945 | col = fmtstr(s, 16, (st ? "Done(%d)" : "Done"), st); |
3943 | col = fmtstr(s, 16, "Done(%d)", st); | ||
3944 | else | ||
3945 | col = fmtstr(s, 16, "Done"); | ||
3946 | } | 3946 | } |
3947 | out: | 3947 | out: |
3948 | return col; | 3948 | return col; |
@@ -4031,7 +4031,7 @@ dowait(int wait_flags, struct job *job) | |||
4031 | char s[48 + 1]; | 4031 | char s[48 + 1]; |
4032 | int len; | 4032 | int len; |
4033 | 4033 | ||
4034 | len = sprint_status(s, status, 1); | 4034 | len = sprint_status48(s, status, 1); |
4035 | if (len) { | 4035 | if (len) { |
4036 | s[len] = '\n'; | 4036 | s[len] = '\n'; |
4037 | s[len + 1] = '\0'; | 4037 | s[len + 1] = '\0'; |
@@ -4052,13 +4052,14 @@ blocking_wait_with_raise_on_sig(void) | |||
4052 | 4052 | ||
4053 | #if JOBS | 4053 | #if JOBS |
4054 | static void | 4054 | static void |
4055 | showjob(FILE *out, struct job *jp, int mode) | 4055 | showjob(struct job *jp, int mode) |
4056 | { | 4056 | { |
4057 | struct procstat *ps; | 4057 | struct procstat *ps; |
4058 | struct procstat *psend; | 4058 | struct procstat *psend; |
4059 | int col; | 4059 | int col; |
4060 | int indent_col; | 4060 | int indent_col; |
4061 | char s[80]; | 4061 | char s[16 + 16 + 48]; |
4062 | FILE *out = (mode & SHOW_STDERR ? stderr : stdout); | ||
4062 | 4063 | ||
4063 | ps = jp->ps; | 4064 | ps = jp->ps; |
4064 | 4065 | ||
@@ -4088,7 +4089,7 @@ showjob(FILE *out, struct job *jp, int mode) | |||
4088 | int status = psend[-1].ps_status; | 4089 | int status = psend[-1].ps_status; |
4089 | if (jp->state == JOBSTOPPED) | 4090 | if (jp->state == JOBSTOPPED) |
4090 | status = jp->stopstatus; | 4091 | status = jp->stopstatus; |
4091 | col += sprint_status(s + col, status, 0); | 4092 | col += sprint_status48(s + col, status, 0); |
4092 | } | 4093 | } |
4093 | /* By now, "[JOBID]* [maybe PID] STATUS" is printed */ | 4094 | /* By now, "[JOBID]* [maybe PID] STATUS" is printed */ |
4094 | 4095 | ||
@@ -4115,7 +4116,7 @@ showjob(FILE *out, struct job *jp, int mode) | |||
4115 | ps->ps_cmd | 4116 | ps->ps_cmd |
4116 | ); | 4117 | ); |
4117 | } while (++ps != psend); | 4118 | } while (++ps != psend); |
4118 | outcslow('\n', out); | 4119 | newline_and_flush(out); |
4119 | 4120 | ||
4120 | jp->changed = 0; | 4121 | jp->changed = 0; |
4121 | 4122 | ||
@@ -4130,7 +4131,7 @@ showjob(FILE *out, struct job *jp, int mode) | |||
4130 | * statuses have changed since the last call to showjobs. | 4131 | * statuses have changed since the last call to showjobs. |
4131 | */ | 4132 | */ |
4132 | static void | 4133 | static void |
4133 | showjobs(FILE *out, int mode) | 4134 | showjobs(int mode) |
4134 | { | 4135 | { |
4135 | struct job *jp; | 4136 | struct job *jp; |
4136 | 4137 | ||
@@ -4142,7 +4143,7 @@ showjobs(FILE *out, int mode) | |||
4142 | 4143 | ||
4143 | for (jp = curjob; jp; jp = jp->prev_job) { | 4144 | for (jp = curjob; jp; jp = jp->prev_job) { |
4144 | if (!(mode & SHOW_CHANGED) || jp->changed) { | 4145 | if (!(mode & SHOW_CHANGED) || jp->changed) { |
4145 | showjob(out, jp, mode); | 4146 | showjob(jp, mode); |
4146 | } | 4147 | } |
4147 | } | 4148 | } |
4148 | } | 4149 | } |
@@ -4163,10 +4164,10 @@ jobscmd(int argc UNUSED_PARAM, char **argv) | |||
4163 | argv = argptr; | 4164 | argv = argptr; |
4164 | if (*argv) { | 4165 | if (*argv) { |
4165 | do | 4166 | do |
4166 | showjob(stdout, getjob(*argv, 0), mode); | 4167 | showjob(getjob(*argv, 0), mode); |
4167 | while (*++argv); | 4168 | while (*++argv); |
4168 | } else { | 4169 | } else { |
4169 | showjobs(stdout, mode); | 4170 | showjobs(mode); |
4170 | } | 4171 | } |
4171 | 4172 | ||
4172 | return 0; | 4173 | return 0; |
@@ -5572,8 +5573,8 @@ cvtnum(arith_t num) | |||
5572 | { | 5573 | { |
5573 | int len; | 5574 | int len; |
5574 | 5575 | ||
5575 | expdest = makestrspace(32, expdest); | 5576 | expdest = makestrspace(sizeof(arith_t)*3 + 2, expdest); |
5576 | len = fmtstr(expdest, 32, ARITH_FMT, num); | 5577 | len = fmtstr(expdest, sizeof(arith_t)*3 + 2, ARITH_FMT, num); |
5577 | STADJUST(len, expdest); | 5578 | STADJUST(len, expdest); |
5578 | return len; | 5579 | return len; |
5579 | } | 5580 | } |
@@ -10352,9 +10353,11 @@ getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *opt | |||
10352 | char c = '?'; | 10353 | char c = '?'; |
10353 | int done = 0; | 10354 | int done = 0; |
10354 | int err = 0; | 10355 | int err = 0; |
10355 | char s[12]; | 10356 | char sbuf[2]; |
10356 | char **optnext; | 10357 | char **optnext; |
10357 | 10358 | ||
10359 | sbuf[1] = '\0'; | ||
10360 | |||
10358 | if (*param_optind < 1) | 10361 | if (*param_optind < 1) |
10359 | return 1; | 10362 | return 1; |
10360 | optnext = optfirst + *param_optind - 1; | 10363 | optnext = optfirst + *param_optind - 1; |
@@ -10381,9 +10384,9 @@ getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *opt | |||
10381 | for (q = optstr; *q != c;) { | 10384 | for (q = optstr; *q != c;) { |
10382 | if (*q == '\0') { | 10385 | if (*q == '\0') { |
10383 | if (optstr[0] == ':') { | 10386 | if (optstr[0] == ':') { |
10384 | s[0] = c; | 10387 | sbuf[0] = c; |
10385 | s[1] = '\0'; | 10388 | /*sbuf[1] = '\0'; - already is */ |
10386 | err |= setvarsafe("OPTARG", s, 0); | 10389 | err |= setvarsafe("OPTARG", sbuf, 0); |
10387 | } else { | 10390 | } else { |
10388 | fprintf(stderr, "Illegal option -%c\n", c); | 10391 | fprintf(stderr, "Illegal option -%c\n", c); |
10389 | unsetvar("OPTARG"); | 10392 | unsetvar("OPTARG"); |
@@ -10398,9 +10401,9 @@ getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *opt | |||
10398 | if (*++q == ':') { | 10401 | if (*++q == ':') { |
10399 | if (*p == '\0' && (p = *optnext) == NULL) { | 10402 | if (*p == '\0' && (p = *optnext) == NULL) { |
10400 | if (optstr[0] == ':') { | 10403 | if (optstr[0] == ':') { |
10401 | s[0] = c; | 10404 | sbuf[0] = c; |
10402 | s[1] = '\0'; | 10405 | /*sbuf[1] = '\0'; - already is */ |
10403 | err |= setvarsafe("OPTARG", s, 0); | 10406 | err |= setvarsafe("OPTARG", sbuf, 0); |
10404 | c = ':'; | 10407 | c = ':'; |
10405 | } else { | 10408 | } else { |
10406 | fprintf(stderr, "No arg for -%c option\n", c); | 10409 | fprintf(stderr, "No arg for -%c option\n", c); |
@@ -10419,11 +10422,10 @@ getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *opt | |||
10419 | out: | 10422 | out: |
10420 | *optoff = p ? p - *(optnext - 1) : -1; | 10423 | *optoff = p ? p - *(optnext - 1) : -1; |
10421 | *param_optind = optnext - optfirst + 1; | 10424 | *param_optind = optnext - optfirst + 1; |
10422 | fmtstr(s, sizeof(s), "%d", *param_optind); | 10425 | err |= setvarsafe("OPTIND", itoa(*param_optind), VNOFUNC); |
10423 | err |= setvarsafe("OPTIND", s, VNOFUNC); | 10426 | sbuf[0] = c; |
10424 | s[0] = c; | 10427 | /*sbuf[1] = '\0'; - already is */ |
10425 | s[1] = '\0'; | 10428 | err |= setvarsafe(optvar, sbuf, 0); |
10426 | err |= setvarsafe(optvar, s, 0); | ||
10427 | if (err) { | 10429 | if (err) { |
10428 | *param_optind = 1; | 10430 | *param_optind = 1; |
10429 | *optoff = -1; | 10431 | *optoff = -1; |
@@ -12119,7 +12121,7 @@ cmdloop(int top) | |||
12119 | setstackmark(&smark); | 12121 | setstackmark(&smark); |
12120 | #if JOBS | 12122 | #if JOBS |
12121 | if (doing_jobctl) | 12123 | if (doing_jobctl) |
12122 | showjobs(stderr, SHOW_CHANGED); | 12124 | showjobs(SHOW_CHANGED|SHOW_STDERR); |
12123 | #endif | 12125 | #endif |
12124 | inter = 0; | 12126 | inter = 0; |
12125 | if (iflag && top) { | 12127 | if (iflag && top) { |
@@ -13143,7 +13145,7 @@ int ash_main(int argc UNUSED_PARAM, char **argv) | |||
13143 | exitshell(); | 13145 | exitshell(); |
13144 | } | 13146 | } |
13145 | if (e == EXINT) { | 13147 | if (e == EXINT) { |
13146 | outcslow('\n', stderr); | 13148 | newline_and_flush(stderr); |
13147 | } | 13149 | } |
13148 | 13150 | ||
13149 | popstackmark(&smark); | 13151 | popstackmark(&smark); |