msh: shrink, mainly by using smallints

function                                             old     new   delta
synio                                                251     263     +12
filechar                                             310     315      +5
subgetc                                              781     785      +4
brkcontin                                            106     109      +3
...
yylex                                                981     976      -5
execute                                             2513    2508      -5
waitfor                                              321     315      -6
onintr                                               103      97      -6
doset                                                332     326      -6
doexec                                               186     180      -6
yyparse                                               65      57      -8
thenpart                                             135     126      -9
forkexec                                            1379    1370      -9
onecommand                                           457     433     -24
newtp                                                 52      28     -24
command                                             1086    1061     -25
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 7/23 up/down: 27/-163)         Total: -136 bytes
This commit is contained in:
Denis Vlasenko 2008-02-12 06:04:06 +00:00
parent a5f2cd30c9
commit 648b44fd78

View File

@ -158,7 +158,7 @@ int mshdbg_rc = 0;
/* /*
* library and system definitions * library and system definitions
*/ */
typedef void xint; /* base type of jmp_buf, for not broken compilers */ typedef void xint; /* base type of jmp_buf, for not broken compilers */
/* /*
* shell components * shell components
@ -172,20 +172,20 @@ typedef void xint; /* base type of jmp_buf, for not broken compilers */
* redirection * redirection
*/ */
struct ioword { struct ioword {
short io_unit; /* unit affected */ short io_unit; /* unit affected */
short io_flag; /* action (below) */ short io_flag; /* action (below) */
char *io_name; /* file name */ char *io_name; /* file name */
}; };
#define IOREAD 1 /* < */ #define IOREAD 1 /* < */
#define IOHERE 2 /* << (here file) */ #define IOHERE 2 /* << (here file) */
#define IOWRITE 4 /* > */ #define IOWRITE 4 /* > */
#define IOCAT 8 /* >> */ #define IOCAT 8 /* >> */
#define IOXHERE 16 /* ${}, ` in << */ #define IOXHERE 16 /* ${}, ` in << */
#define IODUP 32 /* >&digit */ #define IODUP 32 /* >&digit */
#define IOCLOSE 64 /* >&- */ #define IOCLOSE 64 /* >&- */
#define IODEFAULT (-1) /* token for default IO unit */ #define IODEFAULT (-1) /* token for default IO unit */
/* /*
@ -193,12 +193,12 @@ struct ioword {
* Might eventually use a union. * Might eventually use a union.
*/ */
struct op { struct op {
int type; /* operation type, see below */ smallint type; /* operation type, see Txxxx below */
char **words; /* arguments to a command */ char **words; /* arguments to a command */
struct ioword **ioact; /* IO actions (eg, < > >>) */ struct ioword **ioact; /* IO actions (eg, < > >>) */
struct op *left; struct op *left;
struct op *right; struct op *right;
char *str; /* identifier for case and for */ char *str; /* identifier for case and for */
}; };
#define TCOM 1 /* command */ #define TCOM 1 /* command */
@ -269,9 +269,9 @@ struct brkcon {
}; };
static int trapset; /* trap pending */ static smallint trapset; /* trap pending (signal number) */
static int yynerrs; /* yacc */ static smallint yynerrs; /* yacc (flag) */
/* moved to G: static char line[LINELIM]; */ /* moved to G: static char line[LINELIM]; */
@ -420,7 +420,7 @@ struct ioarg {
char **awordlist; char **awordlist;
int afile; /* file descriptor */ int afile; /* file descriptor */
unsigned afid; /* buffer id */ unsigned afid; /* buffer id */
long afpos; /* file position */ off_t afpos; /* file position */
struct iobuf *afbuf; /* buffer for this file */ struct iobuf *afbuf; /* buffer for this file */
}; };
@ -539,7 +539,7 @@ static int xxchar(struct ioarg *ap);
struct here { struct here {
char *h_tag; char *h_tag;
int h_dosub; char h_dosub;
struct ioword *h_iop; struct ioword *h_iop;
struct here *h_next; struct here *h_next;
}; };
@ -600,15 +600,15 @@ static struct op *dowholefile(int, int);
static char **dolv; static char **dolv;
static int dolc; static int dolc;
static int exstat; static int exstat;
static char gflg; static smallint gflg; /* (seems to be a parse error indicator) */
static int interactive; /* Is this an interactive shell */ static smallint interactive; /* Is this an interactive shell */
static int execflg; static smallint execflg;
static int multiline; /* \n changed to ; */ static smallint isbreak; /* "break" statement was seen */
static struct op *outtree; /* result from parser */ static int multiline; /* '\n' changed to ';' (counter) */
static struct op *outtree; /* result from parser */
static xint *failpt; static xint *failpt;
static xint *errpt; static xint *errpt;
static struct brkcon *brklist; static struct brkcon *brklist;
static int isbreak;
static struct wdblock *wdlist; static struct wdblock *wdlist;
static struct wdblock *iolist; static struct wdblock *iolist;
@ -624,10 +624,10 @@ static struct var *shell; /* shell to interpret command files */
static struct var *ifs; /* field separators */ static struct var *ifs; /* field separators */
static int areanum; /* current allocation area */ static int areanum; /* current allocation area */
static int intr; /* interrupt pending */ static smallint intr; /* interrupt pending (bool) */
static smallint heedint = 1; /* heed interrupt signals (bool) */
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 void (*qflag)(int) = SIG_IGN; static void (*qflag)(int) = SIG_IGN;
static int startl; static int startl;
static int peeksym; static int peeksym;
@ -1560,10 +1560,11 @@ static int gmatch(const char *s, const char *p)
static void yyerror(const char *s) ATTRIBUTE_NORETURN; static void yyerror(const char *s) ATTRIBUTE_NORETURN;
static void yyerror(const char *s) static void yyerror(const char *s)
{ {
yynerrs++; yynerrs = 1;
if (interactive && global_env.iop <= iostack) { if (interactive && global_env.iop <= iostack) {
multiline = 0; multiline = 0;
while (eofc() == 0 && yylex(0) != '\n'); while (eofc() == 0 && yylex(0) != '\n')
continue;
} }
err(s); err(s);
fail(); fail();
@ -1584,7 +1585,7 @@ int yyparse(void)
yynerrs = 0; yynerrs = 0;
outtree = c_list(); outtree = c_list();
musthave('\n', 0); musthave('\n', 0);
return (yynerrs != 0); return yynerrs; /* 0/1 */
} }
static struct op *pipeline(int cf) static struct op *pipeline(int cf)
@ -1819,7 +1820,7 @@ static struct op *command(int cf)
case UNTIL: case UNTIL:
multiline++; multiline++;
t = newtp(); t = newtp();
t->type = c == WHILE ? TWHILE : TUNTIL; t->type = (c == WHILE ? TWHILE : TUNTIL);
t->left = c_list(); t->left = c_list();
t->right = dogroup(1); t->right = dogroup(1);
t->words = NULL; t->words = NULL;
@ -1916,7 +1917,7 @@ static struct op *thenpart(void)
return NULL; return NULL;
} }
t = newtp(); t = newtp();
t->type = 0; /*t->type = 0; - newtp() did this */
t->left = c_list(); t->left = c_list();
if (t->left == NULL) if (t->left == NULL)
zzerr(); zzerr();
@ -2044,8 +2045,7 @@ static struct op *block(int type, struct op *t1, struct op *t2, char **wp)
t->right = t2; t->right = t2;
t->words = wp; t->words = wp;
DBGPRINTF7(("BLOCK: inserted %p between %p and %p\n", t, t1, DBGPRINTF7(("BLOCK: inserted %p between %p and %p\n", t, t1, t2));
t2));
return t; return t;
} }
@ -2099,12 +2099,7 @@ static struct op *newtp(void)
struct op *t; struct op *t;
t = (struct op *) tree(sizeof(*t)); t = (struct op *) tree(sizeof(*t));
t->type = 0; memset(t, 0, sizeof(*t));
t->words = NULL;
t->ioact = NULL;
t->left = NULL;
t->right = NULL;
t->str = NULL;
DBGPRINTF3(("NEWTP: allocated %p\n", t)); DBGPRINTF3(("NEWTP: allocated %p\n", t));
@ -2481,7 +2476,8 @@ static int execute(struct op *t, int *pin, int *pout, int act)
rv = 0; rv = 0;
a = areanum++; a = areanum++;
wp = (wp2 = t->words) != NULL wp2 = t->words;
wp = (wp2 != NULL)
? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY) ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY)
: NULL; : NULL;
@ -2533,7 +2529,7 @@ static int execute(struct op *t, int *pin, int *pout, int act)
case TASYNC: case TASYNC:
{ {
int hinteractive = interactive; smallint hinteractive = interactive;
DBGPRINTF7(("EXECUTE: TASYNC clause, calling vfork()...\n")); DBGPRINTF7(("EXECUTE: TASYNC clause, calling vfork()...\n"));
@ -2700,10 +2696,10 @@ static int forkexec(struct op *t, int *pin, int *pout, int act, char **wp)
int *hpin = pin; int *hpin = pin;
int *hpout = pout; int *hpout = pout;
char *hwp; char *hwp;
int hinteractive; smallint hinteractive;
int hintr; smallint hintr;
smallint hexecflg;
struct brkcon *hbrklist; struct brkcon *hbrklist;
int hexecflg;
#if __GNUC__ #if __GNUC__
/* Avoid longjmp clobbering */ /* Avoid longjmp clobbering */
@ -2993,7 +2989,7 @@ static int waitfor(int lastpid, int canintr)
{ {
int pid, rv; int pid, rv;
int s; int s;
int oheedint = heedint; smallint oheedint = heedint;
heedint = 0; heedint = 0;
rv = 0; rv = 0;
@ -3521,7 +3517,7 @@ static int brkcontin(char *cp, int val)
err("bad break/continue level"); err("bad break/continue level");
return 1; return 1;
} }
isbreak = val; isbreak = (val != 0);
longjmp(bc->brkpt, 1); longjmp(bc->brkpt, 1);
/* NOTREACHED */ /* NOTREACHED */
} }
@ -3699,7 +3695,7 @@ static char **eval(char **ap, int f)
expand(*wf, &wb, f & ~DOGLOB); expand(*wf, &wb, f & ~DOGLOB);
} }
} }
for (wb = addword((char *) 0, wb); *ap; ap++) { for (wb = addword((char *) NULL, wb); *ap; ap++) {
if (!FLAG['k'] || !isassign(*ap)) if (!FLAG['k'] || !isassign(*ap))
expand(*ap, &wb, f & ~DOKEY); expand(*ap, &wb, f & ~DOKEY);
} }
@ -3935,13 +3931,13 @@ static int dollar(int quoted)
global_env.iop->task = otask; global_env.iop->task = otask;
if (c != '}') { if (c != '}') {
err("unclosed ${"); err("unclosed ${");
gflg++; gflg = 1;
return c; return c;
} }
} }
if (global_env.linep >= elinep) { if (global_env.linep >= elinep) {
err("string in ${} too long"); err("string in ${} too long");
gflg++; gflg = 1;
global_env.linep -= 10; global_env.linep -= 10;
} }
*global_env.linep = 0; *global_env.linep = 0;
@ -3971,7 +3967,7 @@ static int dollar(int quoted)
case '=': case '=':
if (isdigit(*s)) { if (isdigit(*s)) {
err("cannot use ${...=...} with $n"); err("cannot use ${...=...} with $n");
gflg++; gflg = 1;
break; break;
} }
setval(vp, cp); setval(vp, cp);
@ -3988,7 +3984,7 @@ static int dollar(int quoted)
err(s); err(s);
} else } else
err(cp); err(cp);
gflg++; gflg = 1;
break; break;
} }
} else if (c == '+') } else if (c == '+')
@ -3996,7 +3992,7 @@ static int dollar(int quoted)
if (FLAG['u'] && dolp == null) { if (FLAG['u'] && dolp == null) {
prs("unset variable: "); prs("unset variable: ");
err(s); err(s);
gflg++; gflg = 1;
} }
global_env.linep = s; global_env.linep = s;
PUSHIO(aword, dolp, quoted ? qstrchar : strchar); PUSHIO(aword, dolp, quoted ? qstrchar : strchar);
@ -4539,7 +4535,7 @@ static int my_getc(int ec)
if (global_env.linep > elinep) { if (global_env.linep > elinep) {
while ((c = readc()) != '\n' && c); while ((c = readc()) != '\n' && c);
err("input line too long"); err("input line too long");
gflg++; gflg = 1;
return c; return c;
} }
c = readc(); c = readc();
@ -4638,7 +4634,7 @@ static void pushio(struct ioarg *argp, int (*fn) (struct ioarg *))
if (++global_env.iop >= &iostack[NPUSH]) { if (++global_env.iop >= &iostack[NPUSH]) {
global_env.iop--; global_env.iop--;
err("Shell input nested too deeply"); err("Shell input nested too deeply");
gflg++; gflg = 1;
return; return;
} }
@ -5012,7 +5008,7 @@ static void markhere(char *s, struct ioword *iop)
*s &= ~QUOTE; *s &= ~QUOTE;
} }
} }
h->h_dosub = iop->io_flag & IOXHERE; h->h_dosub = ((iop->io_flag & IOXHERE) ? '\0' : '\'');
} }
static void gethere(void) static void gethere(void)
@ -5023,7 +5019,7 @@ static void gethere(void)
/* Scan here files first leaving inhere list in place */ /* Scan here files first leaving inhere list in place */
for (hp = h = inhere; h != NULL; hp = h, h = h->h_next) for (hp = h = inhere; h != NULL; hp = h, h = h->h_next)
readhere(&h->h_iop->io_name, h->h_tag, h->h_dosub ? 0 : '\''); readhere(&h->h_iop->io_name, h->h_tag, h->h_dosub /* NUL or ' */);
/* Make inhere list active - keep list intact for scraphere */ /* Make inhere list active - keep list intact for scraphere */
if (hp != NULL) { if (hp != NULL) {
@ -5288,7 +5284,7 @@ int msh_main(int argc, char **argv)
break; break;
case 'i': case 'i':
interactive++; interactive = 1;
default: default:
if (*s >= 'a' && *s <= 'z') if (*s >= 'a' && *s <= 'z')
FLAG[(int) *s]++; FLAG[(int) *s]++;
@ -5324,7 +5320,7 @@ int msh_main(int argc, char **argv)
if (global_env.iop < iostack) { if (global_env.iop < iostack) {
PUSHIO(afile, 0, iof); PUSHIO(afile, 0, iof);
if (isatty(0) && isatty(1) && !cflag) { if (isatty(0) && isatty(1) && !cflag) {
interactive++; interactive = 1;
#if !ENABLE_FEATURE_SH_EXTRA_QUIET #if !ENABLE_FEATURE_SH_EXTRA_QUIET
#ifdef MSHDEBUG #ifdef MSHDEBUG
printf("\n\n%s built-in shell (msh with debug)\n", bb_banner); printf("\n\n%s built-in shell (msh with debug)\n", bb_banner);
@ -5338,7 +5334,7 @@ int msh_main(int argc, char **argv)
signal(SIGQUIT, qflag); signal(SIGQUIT, qflag);
if (name && name[0] == '-') { if (name && name[0] == '-') {
interactive++; interactive = 1;
f = open(".profile", O_RDONLY); f = open(".profile", O_RDONLY);
if (f >= 0) if (f >= 0)
next(remap(f)); next(remap(f));