Home | History | Annotate | Line # | Download | only in sh
output.c revision 1.13
      1 /*	$NetBSD: output.c,v 1.13 1995/03/21 09:09:55 cgd 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 #ifndef lint
     40 #if 0
     41 static char sccsid[] = "@(#)output.c	8.1 (Berkeley) 5/31/93";
     42 #else
     43 static char rcsid[] = "$NetBSD: output.c,v 1.13 1995/03/21 09:09:55 cgd Exp $";
     44 #endif
     45 #endif /* not lint */
     46 
     47 /*
     48  * Shell output routines.  We use our own output routines because:
     49  *	When a builtin command is interrupted we have to discard
     50  *		any pending output.
     51  *	When a builtin command appears in back quotes, we want to
     52  *		save the output of the command in a region obtained
     53  *		via malloc, rather than doing a fork and reading the
     54  *		output of the command via a pipe.
     55  *	Our output routines may be smaller than the stdio routines.
     56  */
     57 
     58 #include <sys/ioctl.h>
     59 
     60 #include <stdio.h>	/* defines BUFSIZ */
     61 #include <string.h>
     62 #include "shell.h"
     63 #include "syntax.h"
     64 #include "output.h"
     65 #include "memalloc.h"
     66 #include "error.h"
     67 #ifdef __STDC__
     68 #include "stdarg.h"
     69 #else
     70 #include <varargs.h>
     71 #endif
     72 #include <errno.h>
     73 #include <unistd.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 	register const char *p;
    145 	register 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 #ifdef __STDC__
    222 void
    223 outfmt(struct output *file, char *fmt, ...) {
    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(char *fmt, ...) {
    234 	va_list ap;
    235 
    236 	va_start(ap, fmt);
    237 	doformat(out1, fmt, ap);
    238 	va_end(ap);
    239 }
    240 
    241 void
    242 dprintf(char *fmt, ...) {
    243 	va_list ap;
    244 
    245 	va_start(ap, fmt);
    246 	doformat(out2, fmt, ap);
    247 	va_end(ap);
    248 	flushout(out2);
    249 }
    250 
    251 void
    252 fmtstr(char *outbuf, int length, char *fmt, ...) {
    253 	va_list ap;
    254 	struct output strout;
    255 
    256 	va_start(ap, fmt);
    257 	strout.nextc = outbuf;
    258 	strout.nleft = length;
    259 	strout.fd = BLOCK_OUT;
    260 	strout.flags = 0;
    261 	doformat(&strout, fmt, ap);
    262 	outc('\0', &strout);
    263 	if (strout.flags & OUTPUT_ERR)
    264 		outbuf[length - 1] = '\0';
    265 }
    266 
    267 #else /* not __STDC__ */
    268 
    269 void
    270 outfmt(va_alist)
    271 	va_dcl
    272 	{
    273 	va_list ap;
    274 	struct output *file;
    275 	char *fmt;
    276 
    277 	va_start(ap);
    278 	file = va_arg(ap, struct output *);
    279 	fmt = va_arg(ap, char *);
    280 	doformat(file, fmt, ap);
    281 	va_end(ap);
    282 }
    283 
    284 
    285 void
    286 out1fmt(va_alist)
    287 	va_dcl
    288 	{
    289 	va_list ap;
    290 	char *fmt;
    291 
    292 	va_start(ap);
    293 	fmt = va_arg(ap, char *);
    294 	doformat(out1, fmt, ap);
    295 	va_end(ap);
    296 }
    297 
    298 void
    299 dprintf(va_alist)
    300 	va_dcl
    301 	{
    302 	va_list ap;
    303 	char *fmt;
    304 
    305 	va_start(ap);
    306 	fmt = va_arg(ap, char *);
    307 	doformat(out2, fmt, ap);
    308 	va_end(ap);
    309 	flushout(out2);
    310 }
    311 
    312 void
    313 fmtstr(va_alist)
    314 	va_dcl
    315 	{
    316 	va_list ap;
    317 	struct output strout;
    318 	char *outbuf;
    319 	int length;
    320 	char *fmt;
    321 
    322 	va_start(ap);
    323 	outbuf = va_arg(ap, char *);
    324 	length = va_arg(ap, int);
    325 	fmt = va_arg(ap, char *);
    326 	strout.nextc = outbuf;
    327 	strout.nleft = length;
    328 	strout.fd = BLOCK_OUT;
    329 	strout.flags = 0;
    330 	doformat(&strout, fmt, ap);
    331 	outc('\0', &strout);
    332 	if (strout.flags & OUTPUT_ERR)
    333 		outbuf[length - 1] = '\0';
    334 }
    335 #endif /* __STDC__ */
    336 
    337 
    338 /*
    339  * Formatted output.  This routine handles a subset of the printf formats:
    340  * - Formats supported: d, u, o, X, s, and c.
    341  * - The x format is also accepted but is treated like X.
    342  * - The l modifier is accepted.
    343  * - The - and # flags are accepted; # only works with the o format.
    344  * - Width and precision may be specified with any format except c.
    345  * - An * may be given for the width or precision.
    346  * - The obsolete practice of preceding the width with a zero to get
    347  *   zero padding is not supported; use the precision field.
    348  * - A % may be printed by writing %% in the format string.
    349  */
    350 
    351 #define TEMPSIZE 24
    352 
    353 #ifdef __STDC__
    354 static const char digit[16] = "0123456789ABCDEF";
    355 #else
    356 static const char digit[17] = "0123456789ABCDEF";
    357 #endif
    358 
    359 
    360 void
    361 doformat(dest, f, ap)
    362 	register struct output *dest;
    363 	register char *f;		/* format string */
    364 	va_list ap;
    365 	{
    366 	register char c;
    367 	char temp[TEMPSIZE];
    368 	int flushleft;
    369 	int sharp;
    370 	int width;
    371 	int prec;
    372 	int islong;
    373 	char *p;
    374 	int sign;
    375 	long l;
    376 	unsigned long num;
    377 	unsigned base;
    378 	int len;
    379 	int size;
    380 	int pad;
    381 
    382 	while ((c = *f++) != '\0') {
    383 		if (c != '%') {
    384 			outc(c, dest);
    385 			continue;
    386 		}
    387 		flushleft = 0;
    388 		sharp = 0;
    389 		width = 0;
    390 		prec = -1;
    391 		islong = 0;
    392 		for (;;) {
    393 			if (*f == '-')
    394 				flushleft++;
    395 			else if (*f == '#')
    396 				sharp++;
    397 			else
    398 				break;
    399 			f++;
    400 		}
    401 		if (*f == '*') {
    402 			width = va_arg(ap, int);
    403 			f++;
    404 		} else {
    405 			while (is_digit(*f)) {
    406 				width = 10 * width + digit_val(*f++);
    407 			}
    408 		}
    409 		if (*f == '.') {
    410 			if (*++f == '*') {
    411 				prec = va_arg(ap, int);
    412 				f++;
    413 			} else {
    414 				prec = 0;
    415 				while (is_digit(*f)) {
    416 					prec = 10 * prec + digit_val(*f++);
    417 				}
    418 			}
    419 		}
    420 		if (*f == 'l') {
    421 			islong++;
    422 			f++;
    423 		}
    424 		switch (*f) {
    425 		case 'd':
    426 			if (islong)
    427 				l = va_arg(ap, long);
    428 			else
    429 				l = va_arg(ap, int);
    430 			sign = 0;
    431 			num = l;
    432 			if (l < 0) {
    433 				num = -l;
    434 				sign = 1;
    435 			}
    436 			base = 10;
    437 			goto number;
    438 		case 'u':
    439 			base = 10;
    440 			goto uns_number;
    441 		case 'o':
    442 			base = 8;
    443 			goto uns_number;
    444 		case 'x':
    445 			/* we don't implement 'x'; treat like 'X' */
    446 		case 'X':
    447 			base = 16;
    448 uns_number:	  /* an unsigned number */
    449 			sign = 0;
    450 			if (islong)
    451 				num = va_arg(ap, unsigned long);
    452 			else
    453 				num = va_arg(ap, unsigned int);
    454 number:		  /* process a number */
    455 			p = temp + TEMPSIZE - 1;
    456 			*p = '\0';
    457 			while (num) {
    458 				*--p = digit[num % base];
    459 				num /= base;
    460 			}
    461 			len = (temp + TEMPSIZE - 1) - p;
    462 			if (prec < 0)
    463 				prec = 1;
    464 			if (sharp && *f == 'o' && prec <= len)
    465 				prec = len + 1;
    466 			pad = 0;
    467 			if (width) {
    468 				size = len;
    469 				if (size < prec)
    470 					size = prec;
    471 				size += sign;
    472 				pad = width - size;
    473 				if (flushleft == 0) {
    474 					while (--pad >= 0)
    475 						outc(' ', dest);
    476 				}
    477 			}
    478 			if (sign)
    479 				outc('-', dest);
    480 			prec -= len;
    481 			while (--prec >= 0)
    482 				outc('0', dest);
    483 			while (*p)
    484 				outc(*p++, dest);
    485 			while (--pad >= 0)
    486 				outc(' ', dest);
    487 			break;
    488 		case 's':
    489 			p = va_arg(ap, char *);
    490 			pad = 0;
    491 			if (width) {
    492 				len = strlen(p);
    493 				if (prec >= 0 && len > prec)
    494 					len = prec;
    495 				pad = width - len;
    496 				if (flushleft == 0) {
    497 					while (--pad >= 0)
    498 						outc(' ', dest);
    499 				}
    500 			}
    501 			prec++;
    502 			while (--prec != 0 && *p)
    503 				outc(*p++, dest);
    504 			while (--pad >= 0)
    505 				outc(' ', dest);
    506 			break;
    507 		case 'c':
    508 			c = va_arg(ap, int);
    509 			outc(c, dest);
    510 			break;
    511 		default:
    512 			outc(*f, dest);
    513 			break;
    514 		}
    515 		f++;
    516 	}
    517 }
    518 
    519 
    520 
    521 /*
    522  * Version of write which resumes after a signal is caught.
    523  */
    524 
    525 int
    526 xwrite(fd, buf, nbytes)
    527 	int fd;
    528 	char *buf;
    529 	int nbytes;
    530 	{
    531 	int ntry;
    532 	int i;
    533 	int n;
    534 
    535 	n = nbytes;
    536 	ntry = 0;
    537 	for (;;) {
    538 		i = write(fd, buf, n);
    539 		if (i > 0) {
    540 			if ((n -= i) <= 0)
    541 				return nbytes;
    542 			buf += i;
    543 			ntry = 0;
    544 		} else if (i == 0) {
    545 			if (++ntry > 10)
    546 				return nbytes - n;
    547 		} else if (errno != EINTR) {
    548 			return -1;
    549 		}
    550 	}
    551 }
    552 
    553 
    554 /*
    555  * Version of ioctl that retries after a signal is caught.
    556  * XXX unused function
    557  */
    558 
    559 int
    560 xioctl(fd, request, arg)
    561 	int fd;
    562 	unsigned long request;
    563 	char * arg;
    564 {
    565 	int i;
    566 
    567 	while ((i = ioctl(fd, request, arg)) == -1 && errno == EINTR);
    568 	return i;
    569 }
    570