From aeb5d065044ee6733a7fee14edb52959a28c1ab4 Mon Sep 17 00:00:00 2001 From: Jilles Tjoelker Date: Sat, 20 Nov 2010 14:14:52 +0000 Subject: [PATCH] sh: Code size optimizations to buffered output. This is mainly less use of the outc macro. No functional change is intended, but code size is about 2K less on i386. --- bin/sh/eval.c | 12 ++++++------ bin/sh/expand.c | 6 ++---- bin/sh/main.c | 6 ++---- bin/sh/options.c | 13 ++++++------- bin/sh/output.c | 12 +++++++++--- bin/sh/output.h | 3 ++- bin/sh/var.c | 21 ++++++++++++--------- 7 files changed, 39 insertions(+), 34 deletions(-) diff --git a/bin/sh/eval.c b/bin/sh/eval.c index 415b9eba0120..c306e731f316 100644 --- a/bin/sh/eval.c +++ b/bin/sh/eval.c @@ -699,13 +699,13 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd) for (sp = varlist.list ; sp ; sp = sp->next) { if (sep != 0) out2c(' '); - p = sp->text; - while (*p != '=' && *p != '\0') - out2c(*p++); - if (*p != '\0') { - out2c(*p++); + p = strchr(sp->text, '='); + if (p != NULL) { + p++; + outbin(sp->text, p - sp->text, out2); out2qstr(p); - } + } else + out2qstr(sp->text); sep = ' '; } for (sp = arglist.list ; sp ; sp = sp->next) { diff --git a/bin/sh/expand.c b/bin/sh/expand.c index 200da3f046d4..d251ed07bd08 100644 --- a/bin/sh/expand.c +++ b/bin/sh/expand.c @@ -1592,9 +1592,7 @@ wordexpcmd(int argc, char **argv) for (i = 1, len = 0; i < argc; i++) len += strlen(argv[i]); out1fmt("%08x", (int)len); - for (i = 1; i < argc; i++) { - out1str(argv[i]); - out1c('\0'); - } + for (i = 1; i < argc; i++) + outbin(argv[i], strlen(argv[i]) + 1, out1); return (0); } diff --git a/bin/sh/main.c b/bin/sh/main.c index 83a62a7a7e50..9298f696545d 100644 --- a/bin/sh/main.c +++ b/bin/sh/main.c @@ -128,10 +128,8 @@ main(int argc, char *argv[]) exitshell(exitstatus); } reset(); - if (exception == EXINT) { - out2c('\n'); - flushout(&errout); - } + if (exception == EXINT) + out2fmt_flush("\n"); popstackmark(&smark); FORCEINTON; /* enable interrupts */ if (state == 1) diff --git a/bin/sh/options.c b/bin/sh/options.c index af80036283b0..389e55506c5e 100644 --- a/bin/sh/options.c +++ b/bin/sh/options.c @@ -261,13 +261,12 @@ minus_o(char *name, int val) optlist[i].val ? "on" : "off"); } else { /* Output suitable for re-input to shell. */ - for (i = 0; i < NOPTS; i++) { - if (i % 6 == 0) - out1str(i == 0 ? "set" : "\nset"); - out1fmt(" %co %s", optlist[i].val ? '-' : '+', - optlist[i].name); - } - out1c('\n'); + for (i = 0; i < NOPTS; i++) + out1fmt("%s %co %s%s", + i % 6 == 0 ? "set" : "", + optlist[i].val ? '-' : '+', + optlist[i].name, + i % 6 == 5 || i == NOPTS - 1 ? "\n" : ""); } } else { for (i = 0; i < NOPTS; i++) diff --git a/bin/sh/output.c b/bin/sh/output.c index d7fc534d7013..8442a22ec62a 100644 --- a/bin/sh/output.c +++ b/bin/sh/output.c @@ -95,6 +95,12 @@ RESET { #endif +void +outcslow(int c, struct output *file) +{ + outc(c, file); +} + void out1str(const char *p) { @@ -149,19 +155,19 @@ outqstr(const char *p, struct output *file) case '\'': /* Can't quote single quotes inside single quotes. */ if (inquotes) - outc('\'', file); + outcslow('\'', file); inquotes = 0; outstr("\\'", file); break; default: if (!inquotes) - outc('\'', file); + outcslow('\'', file); inquotes = 1; outc(ch, file); } } if (inquotes) - outc('\'', file); + outcslow('\'', file); } void diff --git a/bin/sh/output.h b/bin/sh/output.h index a7c748e72b8b..5e3b04886a89 100644 --- a/bin/sh/output.h +++ b/bin/sh/output.h @@ -54,6 +54,7 @@ extern struct output *out1; /* &memout if backquote, otherwise &output */ extern struct output *out2; /* &memout if backquote with 2>&1, otherwise &errout */ +void outcslow(int, struct output *); void out1str(const char *); void out1qstr(const char *); void out2str(const char *); @@ -74,7 +75,7 @@ int xwrite(int, const char *, int); #define outc(c, file) (--(file)->nleft < 0? (emptyoutbuf(file), *(file)->nextc++ = (c)) : (*(file)->nextc++ = (c))) #define out1c(c) outc(c, out1); -#define out2c(c) outc(c, out2); +#define out2c(c) outcslow(c, out2); #define OUTPUT_INCL #endif diff --git a/bin/sh/var.c b/bin/sh/var.c index 3bba368ab56b..6c0618f3e583 100644 --- a/bin/sh/var.c +++ b/bin/sh/var.c @@ -633,10 +633,10 @@ showvarscmd(int argc __unused, char **argv __unused) qsort(vars, n, sizeof(*vars), var_compare); for (i = 0; i < n; i++) { - for (s = vars[i]; *s != '='; s++) - out1c(*s); - out1c('='); - out1qstr(s + 1); + s = strchr(vars[i], '='); + s++; + outbin(vars[i], s - vars[i], out1); + out1qstr(s); out1c('\n'); } ckfree(vars); @@ -710,12 +710,15 @@ found:; out1str(cmdname); out1c(' '); } - for (p = vp->text ; *p != '=' ; p++) - out1c(*p); + p = strchr(vp->text, '='); if (values && !(vp->flags & VUNSET)) { - out1c('='); - out1qstr(p + 1); - } + p++; + outbin(vp->text, p - vp->text, + out1); + out1qstr(p); + } else + outbin(vp->text, p - vp->text, + out1); out1c('\n'); } }