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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)output.c 8.2 (Berkeley) 5/4/95
37 * $FreeBSD: src/bin/sh/output.c,v 1.28 2010/12/11 17:47:27 jilles Exp $
41 * Shell output routines. We use our own output routines because:
42 * When a builtin command is interrupted we have to discard
44 * When a builtin command appears in back quotes, we want to
45 * save the output of the command in a region obtained
46 * via malloc, rather than doing a fork and reading the
47 * output of the command via a pipe.
50 #include <stdio.h> /* defines BUFSIZ */
65 #define OUTBUFSIZ BUFSIZ
66 #define MEM_OUT -2 /* output to dynamically allocated memory */
67 #define OUTPUT_ERR 01 /* error occurred on output */
69 static int doformat_wr(void *, const char *, int);
71 struct output output = {NULL, 0, NULL, OUTBUFSIZ, 1, 0};
72 struct output errout = {NULL, 0, NULL, 256, 2, 0};
73 struct output memout = {NULL, 0, NULL, 0, MEM_OUT, 0};
74 struct output *out1 = &output;
75 struct output *out2 = &errout;
87 if (memout.buf != NULL) {
97 outcslow(int c, struct output *file)
103 out1str(const char *p)
109 out1qstr(const char *p)
115 out2str(const char *p)
121 out2qstr(const char *p)
127 outstr(const char *p, struct output *file)
129 outbin(p, strlen(p), file);
132 /* Like outstr(), but quote for re-input into the shell. */
134 outqstr(const char *p, struct output *file)
143 /* Caller will handle '=' if necessary */
144 if (p[strcspn(p, "|&;<>()$`\\\"' \t\n*?[~#")] == '\0' ||
145 strcmp(p, "[") == 0) {
151 while ((ch = *p++) != '\0') {
154 /* Can't quote single quotes inside single quotes. */
156 outcslow('\'', file);
162 outcslow('\'', file);
168 outcslow('\'', file);
172 outbin(const void *data, size_t len, struct output *file)
182 emptyoutbuf(struct output *dest)
186 if (dest->buf == NULL) {
188 dest->buf = ckmalloc(dest->bufsize);
189 dest->nextc = dest->buf;
190 dest->nleft = dest->bufsize;
192 } else if (dest->fd == MEM_OUT) {
193 offset = dest->bufsize;
196 dest->buf = ckrealloc(dest->buf, dest->bufsize);
197 dest->nleft = dest->bufsize - offset;
198 dest->nextc = dest->buf + offset;
216 flushout(struct output *dest)
219 if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
221 if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
222 dest->flags |= OUTPUT_ERR;
223 dest->nextc = dest->buf;
224 dest->nleft = dest->bufsize;
242 outfmt(struct output *file, const char *fmt, ...)
247 doformat(file, fmt, ap);
253 out1fmt(const char *fmt, ...)
258 doformat(out1, fmt, ap);
263 out2fmt_flush(const char *fmt, ...)
268 doformat(out2, fmt, ap);
274 fmtstr(char *outbuf, int length, const char *fmt, ...)
280 vsnprintf(outbuf, length, fmt, ap);
286 doformat_wr(void *cookie, const char *buf, int len)
290 o = (struct output *)cookie;
297 doformat(struct output *dest, const char *f, va_list ap)
301 if ((fp = fwopen(dest, doformat_wr)) != NULL) {
308 * Version of write which resumes after a signal is caught.
312 xwrite(int fd, const char *buf, int nbytes)
321 i = write(fd, buf, n);
330 } else if (errno != EINTR) {