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.10.2.2 2002/07/19 04:38:52 tjr Exp $
38 * $DragonFly: src/bin/sh/output.c,v 1.3 2003/08/24 16:26:00 drhodus Exp $
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.
49 * Our output routines may be smaller than the stdio routines.
52 #include <sys/types.h> /* quad_t */
53 #include <sys/ioctl.h>
55 #include <stdio.h> /* defines BUFSIZ */
70 #define OUTBUFSIZ BUFSIZ
71 #define BLOCK_OUT -2 /* output to a fixed block of memory */
72 #define MEM_OUT -3 /* output to dynamically allocated memory */
73 #define OUTPUT_ERR 01 /* error occurred on output */
76 struct output output = {NULL, 0, NULL, OUTBUFSIZ, 1, 0};
77 struct output errout = {NULL, 0, NULL, 100, 2, 0};
78 struct output memout = {NULL, 0, NULL, 0, MEM_OUT, 0};
79 struct output *out1 = &output;
80 struct output *out2 = &errout;
92 if (memout.buf != NULL) {
102 out1str(const char *p)
108 out1qstr(const char *p)
114 out2str(const char *p)
120 out2qstr(const char *p)
126 outstr(const char *p, struct output *file)
134 /* Like outstr(), but quote for re-input into the shell. */
136 outqstr(const char *p, struct output *file)
140 if (p[strcspn(p, "|&;<>()$`\\\"'")] == '\0' && (!ifsset() ||
141 p[strcspn(p, ifsval())] == '\0')) {
147 while ((ch = *p++) != '\0') {
151 * Can't quote single quotes inside single quotes;
152 * close them, write escaped single quote, open again.
154 outstr("'\\''", file);
163 STATIC char out_junk[16];
166 emptyoutbuf(struct output *dest)
170 if (dest->fd == BLOCK_OUT) {
171 dest->nextc = out_junk;
172 dest->nleft = sizeof out_junk;
173 dest->flags |= OUTPUT_ERR;
174 } else if (dest->buf == NULL) {
176 dest->buf = ckmalloc(dest->bufsize);
177 dest->nextc = dest->buf;
178 dest->nleft = dest->bufsize;
180 } else if (dest->fd == MEM_OUT) {
181 offset = dest->bufsize;
184 dest->buf = ckrealloc(dest->buf, dest->bufsize);
185 dest->nleft = dest->bufsize - offset;
186 dest->nextc = dest->buf + offset;
204 flushout(struct output *dest)
207 if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
209 if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
210 dest->flags |= OUTPUT_ERR;
211 dest->nextc = dest->buf;
212 dest->nleft = dest->bufsize;
230 outfmt(struct output *file, const char *fmt, ...)
235 doformat(file, fmt, ap);
241 out1fmt(const char *fmt, ...)
246 doformat(out1, fmt, ap);
251 dprintf(const char *fmt, ...)
256 doformat(out2, fmt, ap);
262 fmtstr(char *outbuf, int length, const char *fmt, ...)
265 struct output strout;
268 strout.nextc = outbuf;
269 strout.nleft = length;
270 strout.fd = BLOCK_OUT;
272 doformat(&strout, fmt, ap);
274 if (strout.flags & OUTPUT_ERR)
275 outbuf[length - 1] = '\0';
279 * Formatted output. This routine handles a subset of the printf formats:
280 * - Formats supported: d, u, o, X, s, and c.
281 * - The x format is also accepted but is treated like X.
282 * - The l and q modifiers are accepted.
283 * - The - and # flags are accepted; # only works with the o format.
284 * - Width and precision may be specified with any format except c.
285 * - An * may be given for the width or precision.
286 * - The obsolete practice of preceding the width with a zero to get
287 * zero padding is not supported; use the precision field.
288 * - A % may be printed by writing %% in the format string.
293 static const char digit[] = "0123456789ABCDEF";
297 doformat(struct output *dest, const char *f, va_list ap)
316 while ((c = *f++) != '\0') {
337 width = va_arg(ap, int);
340 while (is_digit(*f)) {
341 width = 10 * width + digit_val(*f++);
346 prec = va_arg(ap, int);
350 while (is_digit(*f)) {
351 prec = 10 * prec + digit_val(*f++);
358 } else if (*f == 'q') {
365 l = va_arg(ap, quad_t);
367 l = va_arg(ap, long);
385 /* we don't implement 'x'; treat like 'X' */
388 uns_number: /* an unsigned number */
391 num = va_arg(ap, u_quad_t);
393 num = va_arg(ap, unsigned long);
395 num = va_arg(ap, unsigned int);
396 number: /* process a number */
397 p = temp + TEMPSIZE - 1;
400 *--p = digit[num % base];
403 len = (temp + TEMPSIZE - 1) - p;
406 if (sharp && *f == 'o' && prec <= len)
415 if (flushleft == 0) {
431 p = va_arg(ap, char *);
435 if (prec >= 0 && len > prec)
438 if (flushleft == 0) {
444 while (--prec != 0 && *p)
464 * Version of write which resumes after a signal is caught.
468 xwrite(int fd, char *buf, int nbytes)
477 i = write(fd, buf, n);
486 } else if (errno != EINTR) {