Home | History | Annotate | Line # | Download | only in sh
output.c revision 1.21
      1 /*	$NetBSD: output.c,v 1.21 1998/01/31 12:37:55 christos 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.21 1998/01/31 12:37:55 christos 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 }
    320 
    321 /*
    322  * Formatted output.  This routine handles a subset of the printf formats:
    323  * - Formats supported: d, u, o, p, X, s, and c.
    324  * - The x format is also accepted but is treated like X.
    325  * - The l and q modifiers are accepted.
    326  * - The - and # flags are accepted; # only works with the o format.
    327  * - Width and precision may be specified with any format except c.
    328  * - An * may be given for the width or precision.
    329  * - The obsolete practice of preceding the width with a zero to get
    330  *   zero padding is not supported; use the precision field.
    331  * - A % may be printed by writing %% in the format string.
    332  */
    333 
    334 #define TEMPSIZE 24
    335 
    336 static const char digit[] = "0123456789ABCDEF";
    337 
    338 
    339 void
    340 doformat(dest, f, ap)
    341 	struct output *dest;
    342 	const char *f;		/* format string */
    343 	va_list ap;
    344 	{
    345 	char c;
    346 	char temp[TEMPSIZE];
    347 	int flushleft;
    348 	int sharp;
    349 	int width;
    350 	int prec;
    351 	int islong;
    352 	int isquad;
    353 	char *p;
    354 	int sign;
    355 #ifdef BSD4_4
    356 	quad_t l;
    357 	u_quad_t num;
    358 #else
    359 	long l;
    360 	u_long num;
    361 #endif
    362 	unsigned base;
    363 	int len;
    364 	int size;
    365 	int pad;
    366 
    367 	while ((c = *f++) != '\0') {
    368 		if (c != '%') {
    369 			outc(c, dest);
    370 			continue;
    371 		}
    372 		flushleft = 0;
    373 		sharp = 0;
    374 		width = 0;
    375 		prec = -1;
    376 		islong = 0;
    377 		isquad = 0;
    378 		for (;;) {
    379 			if (*f == '-')
    380 				flushleft++;
    381 			else if (*f == '#')
    382 				sharp++;
    383 			else
    384 				break;
    385 			f++;
    386 		}
    387 		if (*f == '*') {
    388 			width = va_arg(ap, int);
    389 			f++;
    390 		} else {
    391 			while (is_digit(*f)) {
    392 				width = 10 * width + digit_val(*f++);
    393 			}
    394 		}
    395 		if (*f == '.') {
    396 			if (*++f == '*') {
    397 				prec = va_arg(ap, int);
    398 				f++;
    399 			} else {
    400 				prec = 0;
    401 				while (is_digit(*f)) {
    402 					prec = 10 * prec + digit_val(*f++);
    403 				}
    404 			}
    405 		}
    406 		if (*f == 'l') {
    407 			islong++;
    408 			f++;
    409 		} else if (*f == 'q') {
    410 			isquad++;
    411 			f++;
    412 		}
    413 		switch (*f) {
    414 		case 'd':
    415 #ifdef BSD4_4
    416 			if (isquad)
    417 				l = va_arg(ap, quad_t);
    418 			else
    419 #endif
    420 			if (islong)
    421 				l = va_arg(ap, long);
    422 			else
    423 				l = va_arg(ap, int);
    424 			sign = 0;
    425 			num = l;
    426 			if (l < 0) {
    427 				num = -l;
    428 				sign = 1;
    429 			}
    430 			base = 10;
    431 			goto number;
    432 		case 'u':
    433 			base = 10;
    434 			goto uns_number;
    435 		case 'o':
    436 			base = 8;
    437 			goto uns_number;
    438 		case 'p':
    439 			outc('0', dest);
    440 			outc('x', dest);
    441 			/*FALLTHROUGH*/
    442 		case 'x':
    443 			/* we don't implement 'x'; treat like 'X' */
    444 		case 'X':
    445 			base = 16;
    446 uns_number:	  /* an unsigned number */
    447 			sign = 0;
    448 #ifdef BSD4_4
    449 			if (isquad)
    450 				num = va_arg(ap, u_quad_t);
    451 			else
    452 #endif
    453 			if (islong)
    454 				num = va_arg(ap, unsigned long);
    455 			else
    456 				num = va_arg(ap, unsigned int);
    457 number:		  /* process a number */
    458 			p = temp + TEMPSIZE - 1;
    459 			*p = '\0';
    460 			while (num) {
    461 				*--p = digit[num % base];
    462 				num /= base;
    463 			}
    464 			len = (temp + TEMPSIZE - 1) - p;
    465 			if (prec < 0)
    466 				prec = 1;
    467 			if (sharp && *f == 'o' && prec <= len)
    468 				prec = len + 1;
    469 			pad = 0;
    470 			if (width) {
    471 				size = len;
    472 				if (size < prec)
    473 					size = prec;
    474 				size += sign;
    475 				pad = width - size;
    476 				if (flushleft == 0) {
    477 					while (--pad >= 0)
    478 						outc(' ', dest);
    479 				}
    480 			}
    481 			if (sign)
    482 				outc('-', dest);
    483 			prec -= len;
    484 			while (--prec >= 0)
    485 				outc('0', dest);
    486 			while (*p)
    487 				outc(*p++, dest);
    488 			while (--pad >= 0)
    489 				outc(' ', dest);
    490 			break;
    491 		case 's':
    492 			p = va_arg(ap, char *);
    493 			pad = 0;
    494 			if (width) {
    495 				len = strlen(p);
    496 				if (prec >= 0 && len > prec)
    497 					len = prec;
    498 				pad = width - len;
    499 				if (flushleft == 0) {
    500 					while (--pad >= 0)
    501 						outc(' ', dest);
    502 				}
    503 			}
    504 			prec++;
    505 			while (--prec != 0 && *p)
    506 				outc(*p++, dest);
    507 			while (--pad >= 0)
    508 				outc(' ', dest);
    509 			break;
    510 		case 'c':
    511 			c = va_arg(ap, int);
    512 			outc(c, dest);
    513 			break;
    514 		default:
    515 			outc(*f, dest);
    516 			break;
    517 		}
    518 		f++;
    519 	}
    520 }
    521 
    522 
    523 
    524 /*
    525  * Version of write which resumes after a signal is caught.
    526  */
    527 
    528 int
    529 xwrite(fd, buf, nbytes)
    530 	int fd;
    531 	char *buf;
    532 	int nbytes;
    533 	{
    534 	int ntry;
    535 	int i;
    536 	int n;
    537 
    538 	n = nbytes;
    539 	ntry = 0;
    540 	for (;;) {
    541 		i = write(fd, buf, n);
    542 		if (i > 0) {
    543 			if ((n -= i) <= 0)
    544 				return nbytes;
    545 			buf += i;
    546 			ntry = 0;
    547 		} else if (i == 0) {
    548 			if (++ntry > 10)
    549 				return nbytes - n;
    550 		} else if (errno != EINTR) {
    551 			return -1;
    552 		}
    553 	}
    554 }
    555 
    556 
    557 /*
    558  * Version of ioctl that retries after a signal is caught.
    559  * XXX unused function
    560  */
    561 
    562 int
    563 xioctl(fd, request, arg)
    564 	int fd;
    565 	unsigned long request;
    566 	char * arg;
    567 {
    568 	int i;
    569 
    570 	while ((i = ioctl(fd, request, arg)) == -1 && errno == EINTR);
    571 	return i;
    572 }
    573