sh: Simplify output buffering.
Similarly to how STPUTC was changed, change struct output to store the pointer just past the end of the available space instead of the size of the available space, so after writing a character it is only necessary to increment a pointer and not to decrement a counter.
This commit is contained in:
parent
7a377fbeb1
commit
5183ddf2ed
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=318385
@ -1080,8 +1080,8 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
|
|||||||
#endif
|
#endif
|
||||||
mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
|
mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
|
||||||
if (flags == EV_BACKCMD) {
|
if (flags == EV_BACKCMD) {
|
||||||
memout.nleft = 0;
|
|
||||||
memout.nextc = memout.buf;
|
memout.nextc = memout.buf;
|
||||||
|
memout.bufend = memout.buf;
|
||||||
memout.bufsize = 64;
|
memout.bufsize = 64;
|
||||||
mode |= REDIR_BACKQ;
|
mode |= REDIR_BACKQ;
|
||||||
}
|
}
|
||||||
@ -1134,8 +1134,11 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
|
|||||||
exitshell(exitstatus);
|
exitshell(exitstatus);
|
||||||
if (flags == EV_BACKCMD) {
|
if (flags == EV_BACKCMD) {
|
||||||
backcmd->buf = memout.buf;
|
backcmd->buf = memout.buf;
|
||||||
backcmd->nleft = memout.nextc - memout.buf;
|
backcmd->nleft = memout.buf != NULL ?
|
||||||
|
memout.nextc - memout.buf : 0;
|
||||||
memout.buf = NULL;
|
memout.buf = NULL;
|
||||||
|
memout.nextc = NULL;
|
||||||
|
memout.bufend = NULL;
|
||||||
}
|
}
|
||||||
if (cmdentry.u.index != EXECCMD)
|
if (cmdentry.u.index != EXECCMD)
|
||||||
popredir();
|
popredir();
|
||||||
|
@ -71,9 +71,9 @@ __FBSDID("$FreeBSD$");
|
|||||||
|
|
||||||
static int doformat_wr(void *, const char *, int);
|
static int doformat_wr(void *, const char *, int);
|
||||||
|
|
||||||
struct output output = {NULL, 0, NULL, OUTBUFSIZ, 1, 0};
|
struct output output = {NULL, NULL, NULL, OUTBUFSIZ, 1, 0};
|
||||||
struct output errout = {NULL, 0, NULL, 256, 2, 0};
|
struct output errout = {NULL, NULL, NULL, 256, 2, 0};
|
||||||
struct output memout = {NULL, 0, NULL, 0, MEM_OUT, 0};
|
struct output memout = {NULL, NULL, NULL, 0, MEM_OUT, 0};
|
||||||
struct output *out1 = &output;
|
struct output *out1 = &output;
|
||||||
struct output *out2 = &errout;
|
struct output *out2 = &errout;
|
||||||
|
|
||||||
@ -214,20 +214,19 @@ emptyoutbuf(struct output *dest)
|
|||||||
INTOFF;
|
INTOFF;
|
||||||
dest->buf = ckmalloc(dest->bufsize);
|
dest->buf = ckmalloc(dest->bufsize);
|
||||||
dest->nextc = dest->buf;
|
dest->nextc = dest->buf;
|
||||||
dest->nleft = dest->bufsize;
|
dest->bufend = dest->buf + dest->bufsize;
|
||||||
INTON;
|
INTON;
|
||||||
} else if (dest->fd == MEM_OUT) {
|
} else if (dest->fd == MEM_OUT) {
|
||||||
offset = dest->bufsize;
|
offset = dest->nextc - dest->buf;
|
||||||
INTOFF;
|
INTOFF;
|
||||||
dest->bufsize <<= 1;
|
dest->bufsize <<= 1;
|
||||||
dest->buf = ckrealloc(dest->buf, dest->bufsize);
|
dest->buf = ckrealloc(dest->buf, dest->bufsize);
|
||||||
dest->nleft = dest->bufsize - offset;
|
dest->bufend = dest->buf + dest->bufsize;
|
||||||
dest->nextc = dest->buf + offset;
|
dest->nextc = dest->buf + offset;
|
||||||
INTON;
|
INTON;
|
||||||
} else {
|
} else {
|
||||||
flushout(dest);
|
flushout(dest);
|
||||||
}
|
}
|
||||||
dest->nleft--;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -248,7 +247,6 @@ flushout(struct output *dest)
|
|||||||
if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
|
if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
|
||||||
dest->flags |= OUTPUT_ERR;
|
dest->flags |= OUTPUT_ERR;
|
||||||
dest->nextc = dest->buf;
|
dest->nextc = dest->buf;
|
||||||
dest->nleft = dest->bufsize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -258,8 +256,9 @@ freestdout(void)
|
|||||||
INTOFF;
|
INTOFF;
|
||||||
if (output.buf) {
|
if (output.buf) {
|
||||||
ckfree(output.buf);
|
ckfree(output.buf);
|
||||||
|
output.nextc = NULL;
|
||||||
output.buf = NULL;
|
output.buf = NULL;
|
||||||
output.nleft = 0;
|
output.bufend = NULL;
|
||||||
}
|
}
|
||||||
INTON;
|
INTON;
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@
|
|||||||
|
|
||||||
struct output {
|
struct output {
|
||||||
char *nextc;
|
char *nextc;
|
||||||
int nleft;
|
char *bufend;
|
||||||
char *buf;
|
char *buf;
|
||||||
int bufsize;
|
int bufsize;
|
||||||
short fd;
|
short fd;
|
||||||
@ -75,7 +75,7 @@ void fmtstr(char *, int, const char *, ...) __printflike(3, 4);
|
|||||||
void doformat(struct output *, const char *, va_list) __printflike(2, 0);
|
void doformat(struct output *, const char *, va_list) __printflike(2, 0);
|
||||||
int xwrite(int, const char *, int);
|
int xwrite(int, const char *, int);
|
||||||
|
|
||||||
#define outc(c, file) (--(file)->nleft < 0? (emptyoutbuf(file), *(file)->nextc++ = (c)) : (*(file)->nextc++ = (c)))
|
#define outc(c, file) ((file)->nextc == (file)->bufend ? (emptyoutbuf(file), *(file)->nextc++ = (c)) : (*(file)->nextc++ = (c)))
|
||||||
#define out1c(c) outc(c, out1);
|
#define out1c(c) outc(c, out1);
|
||||||
#define out2c(c) outcslow(c, out2);
|
#define out2c(c) outcslow(c, out2);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user