Home | History | Annotate | Line # | Download | only in sh
output.c revision 1.26
      1 /*	$NetBSD: output.c,v 1.26 2002/05/25 23:09:06 wiz 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.26 2002/05/25 23:09:06 wiz 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 outfmt(struct output *file, const char *fmt, ...)
    223 {
    224 	va_list ap;
    225 
    226 	va_start(ap, fmt);
    227 	doformat(file, fmt, ap);
    228 	va_end(ap);
    229 }
    230 
    231 
    232 void
    233 out1fmt(const char *fmt, ...)
    234 {
    235 	va_list ap;
    236 
    237 	va_start(ap, fmt);
    238 	doformat(out1, fmt, ap);
    239 	va_end(ap);
    240 }
    241 
    242 void
    243 dprintf(const char *fmt, ...)
    244 {
    245 	va_list ap;
    246 
    247 	va_start(ap, fmt);
    248 	doformat(out2, fmt, ap);
    249 	va_end(ap);
    250 	flushout(out2);
    251 }
    252 
    253 void
    254 fmtstr(char *outbuf, size_t length, const char *fmt, ...)
    255 {
    256 	va_list ap;
    257 	struct output strout;
    258 
    259 	va_start(ap, fmt);
    260 	strout.nextc = outbuf;
    261 	strout.nleft = length;
    262 	strout.fd = BLOCK_OUT;
    263 	strout.flags = 0;
    264 	doformat(&strout, fmt, ap);
    265 	outc('\0', &strout);
    266 	if (strout.flags & OUTPUT_ERR)
    267 		outbuf[length - 1] = '\0';
    268 	va_end(ap);
    269 }
    270 
    271 /*
    272  * Formatted output.  This routine handles a subset of the printf formats:
    273  * - Formats supported: d, u, o, p, X, s, and c.
    274  * - The x format is also accepted but is treated like X.
    275  * - The l, ll and q modifiers are accepted.
    276  * - The - and # flags are accepted; # only works with the o format.
    277  * - Width and precision may be specified with any format except c.
    278  * - An * may be given for the width or precision.
    279  * - The obsolete practice of preceding the width with a zero to get
    280  *   zero padding is not supported; use the precision field.
    281  * - A % may be printed by writing %% in the format string.
    282  */
    283 
    284 #define TEMPSIZE 24
    285 
    286 #ifdef BSD4_4
    287 #define HAVE_VASPRINTF 1
    288 #endif
    289 
    290 void
    291 doformat(dest, f, ap)
    292 	struct output *dest;
    293 	const char *f;		/* format string */
    294 	va_list ap;
    295 {
    296 #if	HAVE_VASPRINTF
    297 	char *s;
    298 
    299 	vasprintf(&s, f, ap);
    300 	outstr(s, dest);
    301 	free(s);
    302 #else	/* !HAVE_VASPRINTF */
    303 	static const char digit[] = "0123456789ABCDEF";
    304 	char c;
    305 	char temp[TEMPSIZE];
    306 	int flushleft;
    307 	int sharp;
    308 	int width;
    309 	int prec;
    310 	int islong;
    311 	int isquad;
    312 	char *p;
    313 	int sign;
    314 #ifdef BSD4_4
    315 	quad_t l;
    316 	u_quad_t num;
    317 #else
    318 	long l;
    319 	u_long num;
    320 #endif
    321 	unsigned base;
    322 	int len;
    323 	int size;
    324 	int pad;
    325 
    326 	while ((c = *f++) != '\0') {
    327 		if (c != '%') {
    328 			outc(c, dest);
    329 			continue;
    330 		}
    331 		flushleft = 0;
    332 		sharp = 0;
    333 		width = 0;
    334 		prec = -1;
    335 		islong = 0;
    336 		isquad = 0;
    337 		for (;;) {
    338 			if (*f == '-')
    339 				flushleft++;
    340 			else if (*f == '#')
    341 				sharp++;
    342 			else
    343 				break;
    344 			f++;
    345 		}
    346 		if (*f == '*') {
    347 			width = va_arg(ap, int);
    348 			f++;
    349 		} else {
    350 			while (is_digit(*f)) {
    351 				width = 10 * width + digit_val(*f++);
    352 			}
    353 		}
    354 		if (*f == '.') {
    355 			if (*++f == '*') {
    356 				prec = va_arg(ap, int);
    357 				f++;
    358 			} else {
    359 				prec = 0;
    360 				while (is_digit(*f)) {
    361 					prec = 10 * prec + digit_val(*f++);
    362 				}
    363 			}
    364 		}
    365 		if (*f == 'l') {
    366 			f++;
    367 			if (*f == 'l') {
    368 				isquad++;
    369 				f++;
    370 			} else
    371 				islong++;
    372 		} else if (*f == 'q') {
    373 			isquad++;
    374 			f++;
    375 		}
    376 		switch (*f) {
    377 		case 'd':
    378 #ifdef BSD4_4
    379 			if (isquad)
    380 				l = va_arg(ap, quad_t);
    381 			else
    382 #endif
    383 			if (islong)
    384 				l = va_arg(ap, long);
    385 			else
    386 				l = va_arg(ap, int);
    387 			sign = 0;
    388 			num = l;
    389 			if (l < 0) {
    390 				num = -l;
    391 				sign = 1;
    392 			}
    393 			base = 10;
    394 			goto number;
    395 		case 'u':
    396 			base = 10;
    397 			goto uns_number;
    398 		case 'o':
    399 			base = 8;
    400 			goto uns_number;
    401 		case 'p':
    402 			outc('0', dest);
    403 			outc('x', dest);
    404 			/*FALLTHROUGH*/
    405 		case 'x':
    406 			/* we don't implement 'x'; treat like 'X' */
    407 		case 'X':
    408 			base = 16;
    409 uns_number:	  /* an unsigned number */
    410 			sign = 0;
    411 #ifdef BSD4_4
    412 			if (isquad)
    413 				num = va_arg(ap, u_quad_t);
    414 			else
    415 #endif
    416 			if (islong)
    417 				num = va_arg(ap, unsigned long);
    418 			else
    419 				num = va_arg(ap, unsigned int);
    420 number:		  /* process a number */
    421 			p = temp + TEMPSIZE - 1;
    422 			*p = '\0';
    423 			while (num) {
    424 				*--p = digit[num % base];
    425 				num /= base;
    426 			}
    427 			len = (temp + TEMPSIZE - 1) - p;
    428 			if (prec < 0)
    429 				prec = 1;
    430 			if (sharp && *f == 'o' && prec <= len)
    431 				prec = len + 1;
    432 			pad = 0;
    433 			if (width) {
    434 				size = len;
    435 				if (size < prec)
    436 					size = prec;
    437 				size += sign;
    438 				pad = width - size;
    439 				if (flushleft == 0) {
    440 					while (--pad >= 0)
    441 						outc(' ', dest);
    442 				}
    443 			}
    444 			if (sign)
    445 				outc('-', dest);
    446 			prec -= len;
    447 			while (--prec >= 0)
    448 				outc('0', dest);
    449 			while (*p)
    450 				outc(*p++, dest);
    451 			while (--pad >= 0)
    452 				outc(' ', dest);
    453 			break;
    454 		case 's':
    455 			p = va_arg(ap, char *);
    456 			pad = 0;
    457 			if (width) {
    458 				len = strlen(p);
    459 				if (prec >= 0 && len > prec)
    460 					len = prec;
    461 				pad = width - len;
    462 				if (flushleft == 0) {
    463 					while (--pad >= 0)
    464 						outc(' ', dest);
    465 				}
    466 			}
    467 			prec++;
    468 			while (--prec != 0 && *p)
    469 				outc(*p++, dest);
    470 			while (--pad >= 0)
    471 				outc(' ', dest);
    472 			break;
    473 		case 'c':
    474 			c = va_arg(ap, int);
    475 			outc(c, dest);
    476 			break;
    477 		default:
    478 			outc(*f, dest);
    479 			break;
    480 		}
    481 		f++;
    482 	}
    483 #endif	/* !HAVE_VASPRINTF */
    484 }
    485 
    486 
    487 
    488 /*
    489  * Version of write which resumes after a signal is caught.
    490  */
    491 
    492 int
    493 xwrite(fd, buf, nbytes)
    494 	int fd;
    495 	char *buf;
    496 	int nbytes;
    497 	{
    498 	int ntry;
    499 	int i;
    500 	int n;
    501 
    502 	n = nbytes;
    503 	ntry = 0;
    504 	for (;;) {
    505 		i = write(fd, buf, n);
    506 		if (i > 0) {
    507 			if ((n -= i) <= 0)
    508 				return nbytes;
    509 			buf += i;
    510 			ntry = 0;
    511 		} else if (i == 0) {
    512 			if (++ntry > 10)
    513 				return nbytes - n;
    514 		} else if (errno != EINTR) {
    515 			return -1;
    516 		}
    517 	}
    518 }
    519 
    520 
    521 /*
    522  * Version of ioctl that retries after a signal is caught.
    523  * XXX unused function
    524  */
    525 
    526 int
    527 xioctl(fd, request, arg)
    528 	int fd;
    529 	unsigned long request;
    530 	char * arg;
    531 {
    532 	int i;
    533 
    534 	while ((i = ioctl(fd, request, arg)) == -1 && errno == EINTR);
    535 	return i;
    536 }
    537