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
39 static char sccsid[] = "@(#)output.c 8.2 (Berkeley) 5/4/95";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD: src/bin/sh/output.c,v 1.10.2.2 2002/07/19 04:38:52 tjr Exp $");
46 * Shell output routines. We use our own output routines because:
47 * When a builtin command is interrupted we have to discard
49 * When a builtin command appears in back quotes, we want to
50 * save the output of the command in a region obtained
51 * via malloc, rather than doing a fork and reading the
52 * output of the command via a pipe.
53 * Our output routines may be smaller than the stdio routines.
56 #include <sys/types.h> /* quad_t */
57 #include <sys/ioctl.h>
59 #include <stdio.h> /* defines BUFSIZ */
74 #define OUTBUFSIZ BUFSIZ
75 #define BLOCK_OUT -2 /* output to a fixed block of memory */
76 #define MEM_OUT -3 /* output to dynamically allocated memory */
77 #define OUTPUT_ERR 01 /* error occurred on output */
80 struct output output = {NULL, 0, NULL, OUTBUFSIZ, 1, 0};
81 struct output errout = {NULL, 0, NULL, 100, 2, 0};
82 struct output memout = {NULL, 0, NULL, 0, MEM_OUT, 0};
83 struct output *out1 = &output;
84 struct output *out2 = &errout;
96 if (memout.buf != NULL) {
106 out1str(const char *p)
112 out1qstr(const char *p)
118 out2str(const char *p)
124 out2qstr(const char *p)
130 outstr(const char *p, struct output *file)
138 /* Like outstr(), but quote for re-input into the shell. */
140 outqstr(const char *p, struct output *file)
144 if (p[strcspn(p, "|&;<>()$`\\\"'")] == '\0' && (!ifsset() ||
145 p[strcspn(p, ifsval())] == '\0')) {
151 while ((ch = *p++) != '\0') {
155 * Can't quote single quotes inside single quotes;
156 * close them, write escaped single quote, open again.
158 outstr("'\\''", file);
170 emptyoutbuf(struct output *dest)
174 if (dest->fd == BLOCK_OUT) {
175 dest->nextc = out_junk;
176 dest->nleft = sizeof out_junk;
177 dest->flags |= OUTPUT_ERR;
178 } else if (dest->buf == NULL) {
180 dest->buf = ckmalloc(dest->bufsize);
181 dest->nextc = dest->buf;
182 dest->nleft = dest->bufsize;
184 } else if (dest->fd == MEM_OUT) {
185 offset = dest->bufsize;
188 dest->buf = ckrealloc(dest->buf, dest->bufsize);
189 dest->nleft = dest->bufsize - offset;
190 dest->nextc = dest->buf + offset;
208 flushout(struct output *dest)
211 if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
213 if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
214 dest->flags |= OUTPUT_ERR;
215 dest->nextc = dest->buf;
216 dest->nleft = dest->bufsize;
234 outfmt(struct output *file, const char *fmt, ...)
239 doformat(file, fmt, ap);
245 out1fmt(const char *fmt, ...)
250 doformat(out1, fmt, ap);
255 dprintf(const char *fmt, ...)
260 doformat(out2, fmt, ap);
266 fmtstr(char *outbuf, int length, const char *fmt, ...)
269 struct output strout;
272 strout.nextc = outbuf;
273 strout.nleft = length;
274 strout.fd = BLOCK_OUT;
276 doformat(&strout, fmt, ap);
278 if (strout.flags & OUTPUT_ERR)
279 outbuf[length - 1] = '\0';
283 * Formatted output. This routine handles a subset of the printf formats:
284 * - Formats supported: d, u, o, X, s, and c.
285 * - The x format is also accepted but is treated like X.
286 * - The l and q modifiers are accepted.
287 * - The - and # flags are accepted; # only works with the o format.
288 * - Width and precision may be specified with any format except c.
289 * - An * may be given for the width or precision.
290 * - The obsolete practice of preceding the width with a zero to get
291 * zero padding is not supported; use the precision field.
292 * - A % may be printed by writing %% in the format string.
297 static const char digit[] = "0123456789ABCDEF";
301 doformat(struct output *dest, const char *f, va_list ap)
320 while ((c = *f++) != '\0') {
341 width = va_arg(ap, int);
344 while (is_digit(*f)) {
345 width = 10 * width + digit_val(*f++);
350 prec = va_arg(ap, int);
354 while (is_digit(*f)) {
355 prec = 10 * prec + digit_val(*f++);
362 } else if (*f == 'q') {
369 l = va_arg(ap, quad_t);
371 l = va_arg(ap, long);
389 /* we don't implement 'x'; treat like 'X' */
392 uns_number: /* an unsigned number */
395 num = va_arg(ap, u_quad_t);
397 num = va_arg(ap, unsigned long);
399 num = va_arg(ap, unsigned int);
400 number: /* process a number */
401 p = temp + TEMPSIZE - 1;
404 *--p = digit[num % base];
407 len = (temp + TEMPSIZE - 1) - p;
410 if (sharp && *f == 'o' && prec <= len)
419 if (flushleft == 0) {
435 p = va_arg(ap, char *);
439 if (prec >= 0 && len > prec)
442 if (flushleft == 0) {
448 while (--prec != 0 && *p)
468 * Version of write which resumes after a signal is caught.
472 xwrite(int fd, char *buf, int nbytes)
481 i = write(fd, buf, n);
490 } else if (errno != EINTR) {