mirror of
https://github.com/sheumann/hush.git
synced 2024-12-22 14:30:31 +00:00
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
This commit is contained in:
parent
ac678ec2f1
commit
7d4c44e1b1
34
shell/hush.c
34
shell/hush.c
@ -20,7 +20,6 @@
|
|||||||
* rewrites.
|
* rewrites.
|
||||||
*
|
*
|
||||||
* Other credits:
|
* Other credits:
|
||||||
* simple_itoa() was lifted from boa-0.93.15
|
|
||||||
* b_addchr() derived from similar w_addchar function in glibc-2.2
|
* b_addchr() derived from similar w_addchar function in glibc-2.2
|
||||||
* setup_redirect(), redirect_opt_num(), and big chunks of main()
|
* setup_redirect(), redirect_opt_num(), and big chunks of main()
|
||||||
* and many builtins derived from contributions by Erik Andersen
|
* and many builtins derived from contributions by Erik Andersen
|
||||||
@ -280,7 +279,7 @@ struct built_in_command {
|
|||||||
/* belongs in busybox.h */
|
/* belongs in busybox.h */
|
||||||
static int max(int a, int b)
|
static int max(int a, int b)
|
||||||
{
|
{
|
||||||
return (a>b)?a:b;
|
return (a > b) ? a : b;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This should be in utility.c */
|
/* This should be in utility.c */
|
||||||
@ -811,23 +810,12 @@ static int b_addqchr(o_string *o, int ch, int quote)
|
|||||||
return b_addchr(o, ch);
|
return b_addchr(o, ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* belongs in utility.c */
|
|
||||||
static char *simple_itoa(unsigned i)
|
|
||||||
{
|
|
||||||
static char local[sizeof(int)*3 + 2];
|
|
||||||
char *p = &local[sizeof(int)*3 + 2 - 1];
|
|
||||||
*p-- = '\0';
|
|
||||||
do {
|
|
||||||
*p-- = '0' + i % 10;
|
|
||||||
i /= 10;
|
|
||||||
} while (i > 0);
|
|
||||||
return p + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int b_adduint(o_string *o, unsigned i)
|
static int b_adduint(o_string *o, unsigned i)
|
||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
char *p = simple_itoa(i);
|
char buf[sizeof(unsigned)*3 + 1];
|
||||||
|
char *p = buf;
|
||||||
|
*(utoa_to_buf(i, buf, sizeof(buf))) = '\0';
|
||||||
/* no escape checking necessary */
|
/* no escape checking necessary */
|
||||||
do r = b_addchr(o, *p++); while (r == 0 && *p);
|
do r = b_addchr(o, *p++); while (r == 0 && *p);
|
||||||
return r;
|
return r;
|
||||||
@ -2008,8 +1996,8 @@ static int reserved_word(o_string *dest, struct p_context *ctx)
|
|||||||
{ "do", RES_DO, FLAG_DONE },
|
{ "do", RES_DO, FLAG_DONE },
|
||||||
{ "done", RES_DONE, FLAG_END }
|
{ "done", RES_DONE, FLAG_END }
|
||||||
};
|
};
|
||||||
|
enum { NRES = sizeof(reserved_list)/sizeof(reserved_list[0]) };
|
||||||
const struct reserved_combo *r;
|
const struct reserved_combo *r;
|
||||||
#define NRES sizeof(reserved_list)/sizeof(reserved_list[0])
|
|
||||||
|
|
||||||
for (r = reserved_list; r < reserved_list+NRES; r++) {
|
for (r = reserved_list; r < reserved_list+NRES; r++) {
|
||||||
if (strcmp(dest->data, r->literal) == 0) {
|
if (strcmp(dest->data, r->literal) == 0) {
|
||||||
@ -2113,11 +2101,13 @@ static int done_command(struct p_context *ctx)
|
|||||||
struct child_prog *prog = ctx->child;
|
struct child_prog *prog = ctx->child;
|
||||||
|
|
||||||
if (prog && prog->group == NULL
|
if (prog && prog->group == NULL
|
||||||
&& prog->argv == NULL
|
&& prog->argv == NULL
|
||||||
&& prog->redirects == NULL) {
|
&& prog->redirects == NULL
|
||||||
|
) {
|
||||||
debug_printf("done_command: skipping null command\n");
|
debug_printf("done_command: skipping null command\n");
|
||||||
return 0;
|
return 0;
|
||||||
} else if (prog) {
|
}
|
||||||
|
if (prog) {
|
||||||
pi->num_progs++;
|
pi->num_progs++;
|
||||||
debug_printf("done_command: num_progs incremented to %d\n", pi->num_progs);
|
debug_printf("done_command: num_progs incremented to %d\n", pi->num_progs);
|
||||||
} else {
|
} else {
|
||||||
@ -2172,7 +2162,7 @@ static int redirect_dup_num(struct in_str *input)
|
|||||||
return -3; /* "-" represents "close me" */
|
return -3; /* "-" represents "close me" */
|
||||||
}
|
}
|
||||||
while (isdigit(ch)) {
|
while (isdigit(ch)) {
|
||||||
d = d*10+(ch-'0');
|
d = d*10 + (ch-'0');
|
||||||
ok = 1;
|
ok = 1;
|
||||||
b_getch(input);
|
b_getch(input);
|
||||||
ch = b_peek(input);
|
ch = b_peek(input);
|
||||||
@ -2226,7 +2216,7 @@ static FILE *generate_stream_from_list(struct pipe *head)
|
|||||||
} else if (pid == 0) {
|
} else if (pid == 0) {
|
||||||
close(channel[0]);
|
close(channel[0]);
|
||||||
if (channel[1] != 1) {
|
if (channel[1] != 1) {
|
||||||
dup2(channel[1],1);
|
dup2(channel[1], 1);
|
||||||
close(channel[1]);
|
close(channel[1]);
|
||||||
}
|
}
|
||||||
_exit(run_list_real(head)); /* leaks memory */
|
_exit(run_list_real(head)); /* leaks memory */
|
||||||
|
36
shell/msh.c
36
shell/msh.c
@ -264,10 +264,6 @@ static const char *const T_CMD_NAMES[] = {
|
|||||||
#define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM)
|
#define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM)
|
||||||
|
|
||||||
|
|
||||||
/* PROTOTYPES */
|
|
||||||
static int newfile(char *s);
|
|
||||||
|
|
||||||
|
|
||||||
struct brkcon {
|
struct brkcon {
|
||||||
jmp_buf brkpt;
|
jmp_buf brkpt;
|
||||||
struct brkcon *nextlev;
|
struct brkcon *nextlev;
|
||||||
@ -285,8 +281,8 @@ struct brkcon {
|
|||||||
* -u: unset variables net diagnostic
|
* -u: unset variables net diagnostic
|
||||||
*/
|
*/
|
||||||
static char flags['z' - 'a' + 1];
|
static char flags['z' - 'a' + 1];
|
||||||
/* this looks weird, but is OK ... we index flag with 'a'...'z' */
|
/* this looks weird, but is OK ... we index FLAG with 'a'...'z' */
|
||||||
static char *flag = flags - 'a';
|
#define FLAG (flags - 'a')
|
||||||
|
|
||||||
/* moved to G: static char *trap[_NSIG + 1]; */
|
/* moved to G: static char *trap[_NSIG + 1]; */
|
||||||
/* moved to G: static char ourtrap[_NSIG + 1]; */
|
/* moved to G: static char ourtrap[_NSIG + 1]; */
|
||||||
@ -693,7 +689,7 @@ static int intr; /* interrupt pending */
|
|||||||
static int inparse;
|
static int inparse;
|
||||||
static char *null = (char*)""; /* null value for variable */
|
static char *null = (char*)""; /* null value for variable */
|
||||||
static int heedint = 1; /* heed interrupt signals */
|
static int heedint = 1; /* heed interrupt signals */
|
||||||
static void (*qflag) (int) = SIG_IGN;
|
static void (*qflag)(int) = SIG_IGN;
|
||||||
static int startl;
|
static int startl;
|
||||||
static int peeksym;
|
static int peeksym;
|
||||||
static int nlseen;
|
static int nlseen;
|
||||||
@ -855,14 +851,14 @@ static void warn(const char *s)
|
|||||||
exstat = -1;
|
exstat = -1;
|
||||||
}
|
}
|
||||||
prs("\n");
|
prs("\n");
|
||||||
if (flag['e'])
|
if (FLAG['e'])
|
||||||
leave();
|
leave();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void err(const char *s)
|
static void err(const char *s)
|
||||||
{
|
{
|
||||||
warn(s);
|
warn(s);
|
||||||
if (flag['n'])
|
if (FLAG['n'])
|
||||||
return;
|
return;
|
||||||
if (!interactive)
|
if (!interactive)
|
||||||
leave();
|
leave();
|
||||||
@ -1295,7 +1291,7 @@ static void setdash(void)
|
|||||||
|
|
||||||
cp = m;
|
cp = m;
|
||||||
for (c = 'a'; c <= 'z'; c++)
|
for (c = 'a'; c <= 'z'; c++)
|
||||||
if (flag[c])
|
if (FLAG[c])
|
||||||
*cp++ = c;
|
*cp++ = c;
|
||||||
*cp = '\0';
|
*cp = '\0';
|
||||||
setval(lookup("-"), m);
|
setval(lookup("-"), m);
|
||||||
@ -1401,7 +1397,7 @@ static void onecommand(void)
|
|||||||
intr = 0;
|
intr = 0;
|
||||||
execflg = 0;
|
execflg = 0;
|
||||||
|
|
||||||
if (!flag['n']) {
|
if (!FLAG['n']) {
|
||||||
DBGPRINTF(("ONECOMMAND: calling execute, t=outtree=%p\n",
|
DBGPRINTF(("ONECOMMAND: calling execute, t=outtree=%p\n",
|
||||||
outtree));
|
outtree));
|
||||||
execute(outtree, NOPIPE, NOPIPE, 0);
|
execute(outtree, NOPIPE, NOPIPE, 0);
|
||||||
@ -2736,7 +2732,7 @@ static int forkexec(struct op *t, int *pin, int *pout, int act, char **wp)
|
|||||||
|
|
||||||
/* strip all initial assignments */
|
/* strip all initial assignments */
|
||||||
/* not correct wrt PATH=yyy command etc */
|
/* not correct wrt PATH=yyy command etc */
|
||||||
if (flag['x']) {
|
if (FLAG['x']) {
|
||||||
DBGPRINTF9(("FORKEXEC: echo'ing, cp=%p, wp=%p, owp=%p\n",
|
DBGPRINTF9(("FORKEXEC: echo'ing, cp=%p, wp=%p, owp=%p\n",
|
||||||
cp, wp, owp));
|
cp, wp, owp));
|
||||||
echo(cp ? wp : owp);
|
echo(cp ? wp : owp);
|
||||||
@ -3598,18 +3594,18 @@ static int doset(struct op *t)
|
|||||||
/* bad: t->words++; */
|
/* bad: t->words++; */
|
||||||
for (n = 0; (t->words[n] = t->words[n + 1]) != NULL; n++);
|
for (n = 0; (t->words[n] = t->words[n + 1]) != NULL; n++);
|
||||||
if (*++cp == 0)
|
if (*++cp == 0)
|
||||||
flag['x'] = flag['v'] = 0;
|
FLAG['x'] = FLAG['v'] = 0;
|
||||||
else {
|
else {
|
||||||
for (; *cp; cp++) {
|
for (; *cp; cp++) {
|
||||||
switch (*cp) {
|
switch (*cp) {
|
||||||
case 'e':
|
case 'e':
|
||||||
if (!interactive)
|
if (!interactive)
|
||||||
flag['e']++;
|
FLAG['e']++;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if (*cp >= 'a' && *cp <= 'z')
|
if (*cp >= 'a' && *cp <= 'z')
|
||||||
flag[(int) *cp]++;
|
FLAG[(int) *cp]++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -3692,14 +3688,14 @@ static char **eval(char **ap, int f)
|
|||||||
if (newenv(setjmp(errpt)) == 0) {
|
if (newenv(setjmp(errpt)) == 0) {
|
||||||
while (*ap && isassign(*ap))
|
while (*ap && isassign(*ap))
|
||||||
expand(*ap++, &wb, f & ~DOGLOB);
|
expand(*ap++, &wb, f & ~DOGLOB);
|
||||||
if (flag['k']) {
|
if (FLAG['k']) {
|
||||||
for (wf = ap; *wf; wf++) {
|
for (wf = ap; *wf; wf++) {
|
||||||
if (isassign(*wf))
|
if (isassign(*wf))
|
||||||
expand(*wf, &wb, f & ~DOGLOB);
|
expand(*wf, &wb, f & ~DOGLOB);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (wb = addword((char *) 0, wb); *ap; ap++) {
|
for (wb = addword((char *) 0, wb); *ap; ap++) {
|
||||||
if (!flag['k'] || !isassign(*ap))
|
if (!FLAG['k'] || !isassign(*ap))
|
||||||
expand(*ap, &wb, f & ~DOKEY);
|
expand(*ap, &wb, f & ~DOKEY);
|
||||||
}
|
}
|
||||||
wb = addword((char *) 0, wb);
|
wb = addword((char *) 0, wb);
|
||||||
@ -3992,7 +3988,7 @@ static int dollar(int quoted)
|
|||||||
}
|
}
|
||||||
} else if (c == '+')
|
} else if (c == '+')
|
||||||
dolp = strsave(cp, areanum);
|
dolp = strsave(cp, areanum);
|
||||||
if (flag['u'] && dolp == null) {
|
if (FLAG['u'] && dolp == null) {
|
||||||
prs("unset variable: ");
|
prs("unset variable: ");
|
||||||
err(s);
|
err(s);
|
||||||
gflg++;
|
gflg++;
|
||||||
@ -4613,7 +4609,7 @@ static int readc(void)
|
|||||||
|
|
||||||
static void ioecho(char c)
|
static void ioecho(char c)
|
||||||
{
|
{
|
||||||
if (flag['v'])
|
if (FLAG['v'])
|
||||||
write(2, &c, sizeof c);
|
write(2, &c, sizeof c);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -5278,7 +5274,7 @@ int msh_main(int argc, char **argv)
|
|||||||
interactive++;
|
interactive++;
|
||||||
default:
|
default:
|
||||||
if (*s >= 'a' && *s <= 'z')
|
if (*s >= 'a' && *s <= 'z')
|
||||||
flag[(int) *s]++;
|
FLAG[(int) *s]++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
argv--;
|
argv--;
|
||||||
|
Loading…
Reference in New Issue
Block a user