2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)output.c 8.2 (Berkeley) 5/4/95";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * Shell output routines. We use our own output routines because:
43 * When a builtin command is interrupted we have to discard
45 * When a builtin command appears in back quotes, we want to
46 * save the output of the command in a region obtained
47 * via malloc, rather than doing a fork and reading the
48 * output of the command via a pipe.
51 #include <stdio.h> /* defines BUFSIZ */
66 #define OUTBUFSIZ BUFSIZ
67 #define MEM_OUT -2 /* output to dynamically allocated memory */
68 #define OUTPUT_ERR 01 /* error occurred on output */
70 static int doformat_wr(void *, const char *, int);
72 struct output output = {NULL, 0, NULL, OUTBUFSIZ, 1, 0};
73 struct output errout = {NULL, 0, NULL, 256, 2, 0};
74 struct output memout = {NULL, 0, NULL, 0, MEM_OUT, 0};
75 struct output *out1 = &output;
76 struct output *out2 = &errout;
88 if (memout.buf != NULL) {
98 outcslow(int c, struct output *file)
104 out1str(const char *p)
110 out1qstr(const char *p)
116 out2str(const char *p)
122 out2qstr(const char *p)
128 outstr(const char *p, struct output *file)
130 outbin(p, strlen(p), file);
133 /* Like outstr(), but quote for re-input into the shell. */
135 outqstr(const char *p, struct output *file)
144 /* Caller will handle '=' if necessary */
145 if (p[strcspn(p, "|&;<>()$`\\\"' \t\n*?[~#")] == '\0' ||
146 strcmp(p, "[") == 0) {
152 while ((ch = *p++) != '\0') {
155 /* Can't quote single quotes inside single quotes. */
157 outcslow('\'', file);
163 outcslow('\'', file);
169 outcslow('\'', file);
173 outbin(const void *data, size_t len, struct output *file)
183 emptyoutbuf(struct output *dest)
187 if (dest->buf == NULL) {
189 dest->buf = ckmalloc(dest->bufsize);
190 dest->nextc = dest->buf;
191 dest->nleft = dest->bufsize;
193 } else if (dest->fd == MEM_OUT) {
194 offset = dest->bufsize;
197 dest->buf = ckrealloc(dest->buf, dest->bufsize);
198 dest->nleft = dest->bufsize - offset;
199 dest->nextc = dest->buf + offset;
217 flushout(struct output *dest)
220 if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
222 if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
223 dest->flags |= OUTPUT_ERR;
224 dest->nextc = dest->buf;
225 dest->nleft = dest->bufsize;
243 outfmt(struct output *file, const char *fmt, ...)
248 doformat(file, fmt, ap);
254 out1fmt(const char *fmt, ...)
259 doformat(out1, fmt, ap);
264 out2fmt_flush(const char *fmt, ...)
269 doformat(out2, fmt, ap);
275 fmtstr(char *outbuf, int length, const char *fmt, ...)
281 vsnprintf(outbuf, length, fmt, ap);
287 doformat_wr(void *cookie, const char *buf, int len)
291 o = (struct output *)cookie;
298 doformat(struct output *dest, const char *f, va_list ap)
302 if ((fp = fwopen(dest, doformat_wr)) != NULL) {
309 * Version of write which resumes after a signal is caught.
313 xwrite(int fd, const char *buf, int nbytes)
322 i = write(fd, buf, n);
331 } else if (errno != EINTR) {