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. 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
32 * @(#)output.c 8.2 (Berkeley) 5/4/95
33 * $FreeBSD: head/bin/sh/output.c 244162 2012-12-12 22:01:10Z jilles $
37 * Shell output routines. We use our own output routines because:
38 * When a builtin command is interrupted we have to discard
40 * When a builtin command appears in back quotes, we want to
41 * save the output of the command in a region obtained
42 * via malloc, rather than doing a fork and reading the
43 * output of the command via a pipe.
46 #include <stdio.h> /* defines BUFSIZ */
61 #define OUTBUFSIZ BUFSIZ
62 #define MEM_OUT -2 /* output to dynamically allocated memory */
63 #define OUTPUT_ERR 01 /* error occurred on output */
65 static int doformat_wr(void *, const char *, int);
67 struct output output = {NULL, 0, NULL, OUTBUFSIZ, 1, 0};
68 struct output errout = {NULL, 0, NULL, 256, 2, 0};
69 struct output memout = {NULL, 0, NULL, 0, MEM_OUT, 0};
70 struct output *out1 = &output;
71 struct output *out2 = &errout;
83 if (memout.buf != NULL) {
93 outcslow(int c, struct output *file)
99 out1str(const char *p)
105 out1qstr(const char *p)
111 out2str(const char *p)
117 out2qstr(const char *p)
123 outstr(const char *p, struct output *file)
125 outbin(p, strlen(p), file);
128 /* Like outstr(), but quote for re-input into the shell. */
130 outqstr(const char *p, struct output *file)
139 /* Caller will handle '=' if necessary */
140 if (p[strcspn(p, "|&;<>()$`\\\"' \t\n*?[~#")] == '\0' ||
141 strcmp(p, "[") == 0) {
147 while ((ch = *p++) != '\0') {
150 /* Can't quote single quotes inside single quotes. */
152 outcslow('\'', file);
158 outcslow('\'', file);
164 outcslow('\'', file);
168 outbin(const void *data, size_t len, struct output *file)
178 emptyoutbuf(struct output *dest)
182 if (dest->buf == NULL) {
184 dest->buf = ckmalloc(dest->bufsize);
185 dest->nextc = dest->buf;
186 dest->nleft = dest->bufsize;
188 } else if (dest->fd == MEM_OUT) {
189 offset = dest->bufsize;
192 dest->buf = ckrealloc(dest->buf, dest->bufsize);
193 dest->nleft = dest->bufsize - offset;
194 dest->nextc = dest->buf + offset;
212 flushout(struct output *dest)
215 if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
217 if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
218 dest->flags |= OUTPUT_ERR;
219 dest->nextc = dest->buf;
220 dest->nleft = dest->bufsize;
238 outiserror(struct output *file)
240 return (file->flags & OUTPUT_ERR);
245 outclearerror(struct output *file)
247 file->flags &= ~OUTPUT_ERR;
252 outfmt(struct output *file, const char *fmt, ...)
257 doformat(file, fmt, ap);
263 out1fmt(const char *fmt, ...)
268 doformat(out1, fmt, ap);
273 out2fmt_flush(const char *fmt, ...)
278 doformat(out2, fmt, ap);
284 fmtstr(char *outbuf, int length, const char *fmt, ...)
290 vsnprintf(outbuf, length, fmt, ap);
296 doformat_wr(void *cookie, const char *buf, int len)
300 o = (struct output *)cookie;
307 doformat(struct output *dest, const char *f, va_list ap)
311 if ((fp = fwopen(dest, doformat_wr)) != NULL) {
318 * Version of write which resumes after a signal is caught.
322 xwrite(int fd, const char *buf, int nbytes)
331 i = write(fd, buf, n);
340 } else if (errno != EINTR) {