Home | History | Annotate | Line # | Download | only in sh
output.c revision 1.25
      1 /*	$NetBSD: output.c,v 1.25 2002/04/09 00:52:05 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Kenneth Almquist.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 #if 0
     42 static char sccsid[] = "@(#)output.c	8.2 (Berkeley) 5/4/95";
     43 #else
     44 __RCSID("$NetBSD: output.c,v 1.25 2002/04/09 00:52:05 thorpej Exp $");
     45 #endif
     46 #endif /* not lint */
     47 
     48 /*
     49  * Shell output routines.  We use our own output routines because:
     50  *	When a builtin command is interrupted we have to discard
     51  *		any pending output.
     52  *	When a builtin command appears in back quotes, we want to
     53  *		save the output of the command in a region obtained
     54  *		via malloc, rather than doing a fork and reading the
     55  *		output of the command via a pipe.
     56  *	Our output routines may be smaller than the stdio routines.
     57  */
     58 
     59 #include <sys/types.h>		/* quad_t */
     60 #include <sys/param.h>		/* BSD4_4 */
     61 #include <sys/ioctl.h>
     62 
     63 #include <stdio.h>	/* defines BUFSIZ */
     64 #include <string.h>
     65 #include <errno.h>
     66 #include <unistd.h>
     67 #include <stdlib.h>
     68 
     69 #include "shell.h"
     70 #include "syntax.h"
     71 #include "output.h"
     72 #include "memalloc.h"
     73 #include "error.h"
     74 
     75 
     76 #define OUTBUFSIZ BUFSIZ
     77 #define BLOCK_OUT -2		/* output to a fixed block of memory */
     78 #define MEM_OUT -3		/* output to dynamically allocated memory */
     79 #define OUTPUT_ERR 01		/* error occurred on output */
     80 
     81 
     82 struct output output = {NULL, 0, NULL, OUTBUFSIZ, 1, 0};
     83 struct output errout = {NULL, 0, NULL, 100, 2, 0};
     84 struct output memout = {NULL, 0, NULL, 0, MEM_OUT, 0};
     85 struct output *out1 = &output;
     86 struct output *out2 = &errout;
     87 
     88 
     89 
     90 #ifdef mkinit
     91 
     92 INCLUDE "output.h"
     93 INCLUDE "memalloc.h"
     94 
     95 RESET {
     96 	out1 = &output;
     97 	out2 = &errout;
     98 	if (memout.buf != NULL) {
     99 		ckfree(memout.buf);
    100 		memout.buf = NULL;
    101 	}
    102 }
    103 
    104 #endif
    105 
    106 
    107 #ifdef notdef	/* no longer used */
    108 /*
    109  * Set up an output file to write to memory rather than a file.
    110  */
    111 
    112 void
    113 open_mem(block, length, file)
    114 	char *block;
    115 	int length;
    116 	struct output *file;
    117 	{
    118 	file->nextc = block;
    119 	file->nleft = --length;
    120 	file->fd = BLOCK_OUT;
    121 	file->flags = 0;
    122 }
    123 #endif
    124 
    125 
    126 void
    127 out1str(p)
    128 	const char *p;
    129 	{
    130 	outstr(p, out1);
    131 }
    132 
    133 
    134 void
    135 out2str(p)
    136 	const char *p;
    137 	{
    138 	outstr(p, out2);
    139 }
    140 
    141 
    142 void
    143 outstr(p, file)
    144 	const char *p;
    145 	struct output *file;
    146 	{
    147 	while (*p)
    148 		outc(*p++, file);
    149 	if (file == out2)
    150 		flushout(file);
    151 }
    152 
    153 
    154 char out_junk[16];
    155 
    156 
    157 void
    158 emptyoutbuf(dest)
    159 	struct output *dest;
    160 	{
    161 	int offset;
    162 
    163 	if (dest->fd == BLOCK_OUT) {
    164 		dest->nextc = out_junk;
    165 		dest->nleft = sizeof out_junk;
    166 		dest->flags |= OUTPUT_ERR;
    167 	} else if (dest->buf == NULL) {
    168 		INTOFF;
    169 		dest->buf = ckmalloc(dest->bufsize);
    170 		dest->nextc = dest->buf;
    171 		dest->nleft = dest->bufsize;
    172 		INTON;
    173 	} else if (dest->fd == MEM_OUT) {
    174 		offset = dest->bufsize;
    175 		INTOFF;
    176 		dest->bufsize <<= 1;
    177 		dest->buf = ckrealloc(dest->buf, dest->bufsize);
    178 		dest->nleft = dest->bufsize - offset;
    179 		dest->nextc = dest->buf + offset;
    180 		INTON;
    181 	} else {
    182 		flushout(dest);
    183 	}
    184 	dest->nleft--;
    185 }
    186 
    187 
    188 void
    189 flushall() {
    190 	flushout(&output);
    191 	flushout(&errout);
    192 }
    193 
    194 
    195 void
    196 flushout(dest)
    197 	struct output *dest;
    198 	{
    199 
    200 	if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
    201 		return;
    202 	if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
    203 		dest->flags |= OUTPUT_ERR;
    204 	dest->nextc = dest->buf;
    205 	dest->nleft = dest->bufsize;
    206 }
    207 
    208 
    209 void
    210 freestdout() {
    211 	INTOFF;
    212 	if (output.buf) {
    213 		ckfree(output.buf);
    214 		output.buf = NULL;
    215 		output.nleft = 0;
    216 	}
    217 	INTON;
    218 }
    219 
    220 
    221 void
    222 #ifdef __STDC__
    223 outfmt(struct output *file, const char *fmt, ...)
    224 #else
    225 void
    226 outfmt(va_alist)
    227 	va_dcl
    228 #endif
    229 {
    230 	va_list ap;
    231 #ifndef __STDC__
    232 	struct output *file;
    233 	const char *fmt;
    234 
    235 	va_start(ap);
    236 	file = va_arg(ap, struct output *);
    237 	fmt = va_arg(ap, const char *);
    238 #else
    239 	va_start(ap, fmt);
    240 #endif
    241 	doformat(file, fmt, ap);
    242 	va_end(ap);
    243 }
    244 
    245 
    246 void
    247 #ifdef __STDC__
    248 out1fmt(const char *fmt, ...)
    249 #else
    250 out1fmt(va_alist)
    251 	va_dcl
    252 #endif
    253 {
    254 	va_list ap;
    255 #ifndef __STDC__
    256 	const char *fmt;
    257 
    258 	va_start(ap);
    259 	fmt = va_arg(ap, const char *);
    260 #else
    261 	va_start(ap, fmt);
    262 #endif
    263 	doformat(out1, fmt, ap);
    264 	va_end(ap);
    265 }
    266 
    267 void
    268 #ifdef __STDC__
    269 dprintf(const char *fmt, ...)
    270 #else
    271 dprintf(va_alist)
    272 	va_dcl
    273 #endif
    274 {
    275 	va_list ap;
    276 #ifndef __STDC__
    277 	const char *fmt;
    278 
    279 	va_start(ap);
    280 	fmt = va_arg(ap, const char *);
    281 #else
    282 	va_start(ap, fmt);
    283 #endif
    284 	doformat(out2, fmt, ap);
    285 	va_end(ap);
    286 	flushout(out2);
    287 }
    288 
    289 void
    290 #ifdef __STDC__
    291 fmtstr(char *outbuf, size_t length, const char *fmt, ...)
    292 #else
    293 fmtstr(va_alist)
    294 	va_dcl
    295 #endif
    296 {
    297 	va_list ap;
    298 	struct output strout;
    299 #ifndef __STDC__
    300 	char *outbuf;
    301 	size_t length;
    302 	const char *fmt;
    303 
    304 	va_start(ap);
    305 	outbuf = va_arg(ap, char *);
    306 	length = va_arg(ap, size_t);
    307 	fmt = va_arg(ap, const char *);
    308 #else
    309 	va_start(ap, fmt);
    310 #endif
    311 	strout.nextc = outbuf;
    312 	strout.nleft = length;
    313 	strout.fd = BLOCK_OUT;
    314 	strout.flags = 0;
    315 	doformat(&strout, fmt, ap);
    316 	outc('\0', &strout);
    317 	if (strout.flags & OUTPUT_ERR)
    318 		outbuf[length - 1] = '\0';
    319 	va_end(ap);
    320 }
    321 
    322 /*
    323  * Formatted output.  This routine handles a subset of the printf formats:
    324  * - Formats supported: d, u, o, p, X, s, and c.
    325  * - The x format is also accepted but is treated like X.
    326  * - The l, ll and q modifiers are accepted.
    327  * - The - and # flags are accepted; # only works with the o format.
    328  * - Width and precision may be specified with any format except c.
    329  * - An * may be given for the width or precision.
    330  * - The obsolete practice of preceding the width with a zero to get
    331  *   zero padding is not supported; use the precision field.
    332  * - A % may be printed by writing %% in the format string.
    333  */
    334 
    335 #define TEMPSIZE 24
    336 
    337 #ifdef BSD4_4
    338 #define HAVE_VASPRINTF 1
    339 #endif
    340 
    341 void
    342 doformat(dest, f, ap)
    343 	struct output *dest;
    344 	const char *f;		/* format string */
    345 	va_list ap;
    346 {
    347 #if	HAVE_VASPRINTF
    348 	char *s;
    349 
    350 	vasprintf(&s, f, ap);
    351 	outstr(s, dest);
    352 	free(s);
    353 #else	/* !HAVE_VASPRINTF */
    354 	static const char digit[] = "0123456789ABCDEF";
    355 	char c;
    356 	char temp[TEMPSIZE];
    357 	int flushleft;
    358 	int sharp;
    359 	int width;
    360 	int prec;
    361 	int islong;
    362 	int isquad;
    363 	char *p;
    364 	int sign;
    365 #ifdef BSD4_4
    366 	quad_t l;
    367 	u_quad_t num;
    368 #else
    369 	long l;
    370 	u_long num;
    371 #endif
    372 	unsigned base;
    373 	int len;
    374 	int size;
    375 	int pad;
    376 
    377 	while ((c = *f++) != '\0') {
    378 		if (c != '%') {
    379 			outc(c, dest);
    380 			continue;
    381 		}
    382 		flushleft = 0;
    383 		sharp = 0;
    384 		width = 0;
    385 		prec = -1;
    386 		islong = 0;
    387 		isquad = 0;
    388 		for (;;) {
    389 			if (*f == '-')
    390 				flushleft++;
    391 			else if (*f == '#')
    392 				sharp++;
    393 			else
    394 				break;
    395 			f++;
    396 		}
    397 		if (*f == '*') {
    398 			width = va_arg(ap, int);
    399 			f++;
    400 		} else {
    401 			while (is_digit(*f)) {
    402 				width = 10 * width + digit_val(*f++);
    403 			}
    404 		}
    405 		if (*f == '.') {
    406 			if (*++f == '*') {
    407 				prec = va_arg(ap, int);
    408 				f++;
    409 			} else {
    410 				prec = 0;
    411 				while (is_digit(*f)) {
    412 					prec = 10 * prec + digit_val(*f++);
    413 				}
    414 			}
    415 		}
    416 		if (*f == 'l') {
    417 			f++;
    418 			if (*f == 'l') {
    419 				isquad++;
    420 				f++;
    421 			} else
    422 				islong++;
    423 		} else if (*f == 'q') {
    424 			isquad++;
    425 			f++;
    426 		}
    427 		switch (*f) {
    428 		case 'd':
    429 #ifdef BSD4_4
    430 			if (isquad)
    431 				l = va_arg(ap, quad_t);
    432 			else
    433 #endif
    434 			if (islong)
    435 				l = va_arg(ap, long);
    436 			else
    437 				l = va_arg(ap, int);
    438 			sign = 0;
    439 			num = l;
    440 			if (l < 0) {
    441 				num = -l;
    442 				sign = 1;
    443 			}
    444 			base = 10;
    445 			goto number;
    446 		case 'u':
    447 			base = 10;
    448 			goto uns_number;
    449 		case 'o':
    450 			base = 8;
    451 			goto uns_number;
    452 		case 'p':
    453 			outc('0', dest);
    454 			outc('x', dest);
    455 			/*FALLTHROUGH*/
    456 		case 'x':
    457 			/* we don't implement 'x'; treat like 'X' */
    458 		case 'X':
    459 			base = 16;
    460 uns_number:	  /* an unsigned number */
    461 			sign = 0;
    462 #ifdef BSD4_4
    463 			if (isquad)
    464 				num = va_arg(ap, u_quad_t);
    465 			else
    466 #endif
    467 			if (islong)
    468 				num = va_arg(ap, unsigned long);
    469 			else
    470 				num = va_arg(ap, unsigned int);
    471 number:		  /* process a number */
    472 			p = temp + TEMPSIZE - 1;
    473 			*p = '\0';
    474 			while (num) {
    475 				*--p = digit[num % base];
    476 				num /= base;
    477 			}
    478 			len = (temp + TEMPSIZE - 1) - p;
    479 			if (prec < 0)
    480 				prec = 1;
    481 			if (sharp && *f == 'o' && prec <= len)
    482 				prec = len + 1;
    483 			pad = 0;
    484 			if (width) {
    485 				size = len;
    486 				if (size < prec)
    487 					size = prec;
    488 				size += sign;
    489 				pad = width - size;
    490 				if (flushleft == 0) {
    491 					while (--pad >= 0)
    492 						outc(' ', dest);
    493 				}
    494 			}
    495 			if (sign)
    496 				outc('-', dest);
    497 			prec -= len;
    498 			while (--prec >= 0)
    499 				outc('0', dest);
    500 			while (*p)
    501 				outc(*p++, dest);
    502 			while (--pad >= 0)
    503 				outc(' ', dest);
    504 			break;
    505 		case 's':
    506 			p = va_arg(ap, char *);
    507 			pad = 0;
    508 			if (width) {
    509 				len = strlen(p);
    510 				if (prec >= 0 && len > prec)
    511 					len = prec;
    512 				pad = width - len;
    513 				if (flushleft == 0) {
    514 					while (--pad >= 0)
    515 						outc(' ', dest);
    516 				}
    517 			}
    518 			prec++;
    519 			while (--prec != 0 && *p)
    520 				outc(*p++, dest);
    521 			while (--pad >= 0)
    522 				outc(' ', dest);
    523 			break;
    524 		case 'c':
    525 			c = va_arg(ap, int);
    526 			outc(c, dest);
    527 			break;
    528 		default:
    529 			outc(*f, dest);
    530 			break;
    531 		}
    532 		f++;
    533 	}
    534 #endif	/* !HAVE_VASPRINTF */
    535 }
    536 
    537 
    538 
    539 /*
    540  * Version of write which resumes after a signal is caught.
    541  */
    542 
    543 int
    544 xwrite(fd, buf, nbytes)
    545 	int fd;
    546 	char *buf;
    547 	int nbytes;
    548 	{
    549 	int ntry;
    550 	int i;
    551 	int n;
    552 
    553 	n = nbytes;
    554 	ntry = 0;
    555 	for (;;) {
    556 		i = write(fd, buf, n);
    557 		if (i > 0) {
    558 			if ((n -= i) <= 0)
    559 				return nbytes;
    560 			buf += i;
    561 			ntry = 0;
    562 		} else if (i == 0) {
    563 			if (++ntry > 10)
    564 				return nbytes - n;
    565 		} else if (errno != EINTR) {
    566 			return -1;
    567 		}
    568 	}
    569 }
    570 
    571 
    572 /*
    573  * Version of ioctl that retries after a signal is caught.
    574  * XXX unused function
    575  */
    576 
    577 int
    578 xioctl(fd, request, arg)
    579 	int fd;
    580 	unsigned long request;
    581 	char * arg;
    582 {
    583 	int i;
    584 
    585 	while ((i = ioctl(fd, request, arg)) == -1 && errno == EINTR);
    586 	return i;
    587 }
    588