0.1.3
[kbuild-mirror.git] / src / ash / output.c
blobe667812480bc289781ef57301fd5ec1c083bf74e
1 /* $NetBSD: output.c,v 1.28 2003/08/07 09:05:36 agc Exp $ */
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #ifdef HAVE_SYS_CDEFS_H
36 #include <sys/cdefs.h>
37 #endif
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)output.c 8.2 (Berkeley) 5/4/95";
41 #else
42 __RCSID("$NetBSD: output.c,v 1.28 2003/08/07 09:05:36 agc Exp $");
43 #endif
44 #endif /* not lint */
47 * Shell output routines. We use our own output routines because:
48 * When a builtin command is interrupted we have to discard
49 * any pending output.
50 * When a builtin command appears in back quotes, we want to
51 * save the output of the command in a region obtained
52 * via malloc, rather than doing a fork and reading the
53 * output of the command via a pipe.
54 * Our output routines may be smaller than the stdio routines.
57 #include <sys/types.h> /* quad_t */
58 #include <sys/param.h> /* BSD4_4 */
59 #include <sys/ioctl.h>
61 #include <stdio.h> /* defines BUFSIZ */
62 #include <string.h>
63 #include <errno.h>
64 #include <unistd.h>
65 #include <stdlib.h>
67 #include "shell.h"
68 #include "syntax.h"
69 #include "output.h"
70 #include "memalloc.h"
71 #include "error.h"
74 #define OUTBUFSIZ BUFSIZ
75 #define BLOCK_OUT -2 /* output to a fixed block of memory */
76 #define MEM_OUT -3 /* output to dynamically allocated memory */
77 #define OUTPUT_ERR 01 /* error occurred on output */
80 struct output output = {NULL, 0, NULL, OUTBUFSIZ, 1, 0};
81 struct output errout = {NULL, 0, NULL, 100, 2, 0};
82 struct output memout = {NULL, 0, NULL, 0, MEM_OUT, 0};
83 struct output *out1 = &output;
84 struct output *out2 = &errout;
88 #ifdef mkinit
90 INCLUDE "output.h"
91 INCLUDE "memalloc.h"
93 RESET {
94 out1 = &output;
95 out2 = &errout;
96 if (memout.buf != NULL) {
97 ckfree(memout.buf);
98 memout.buf = NULL;
102 #endif
105 #ifdef notdef /* no longer used */
107 * Set up an output file to write to memory rather than a file.
110 void
111 open_mem(char *block, int length, struct output *file)
113 file->nextc = block;
114 file->nleft = --length;
115 file->fd = BLOCK_OUT;
116 file->flags = 0;
118 #endif
121 void
122 out1str(const char *p)
124 outstr(p, out1);
128 void
129 out2str(const char *p)
131 outstr(p, out2);
135 void
136 outstr(const char *p, struct output *file)
138 while (*p)
139 outc(*p++, file);
140 if (file == out2)
141 flushout(file);
145 char out_junk[16];
148 void
149 emptyoutbuf(struct output *dest)
151 int offset;
153 if (dest->fd == BLOCK_OUT) {
154 dest->nextc = out_junk;
155 dest->nleft = sizeof out_junk;
156 dest->flags |= OUTPUT_ERR;
157 } else if (dest->buf == NULL) {
158 INTOFF;
159 dest->buf = ckmalloc(dest->bufsize);
160 dest->nextc = dest->buf;
161 dest->nleft = dest->bufsize;
162 INTON;
163 } else if (dest->fd == MEM_OUT) {
164 offset = dest->bufsize;
165 INTOFF;
166 dest->bufsize <<= 1;
167 dest->buf = ckrealloc(dest->buf, dest->bufsize);
168 dest->nleft = dest->bufsize - offset;
169 dest->nextc = dest->buf + offset;
170 INTON;
171 } else {
172 flushout(dest);
174 dest->nleft--;
178 void
179 output_flushall(void)
181 flushout(&output);
182 flushout(&errout);
186 void
187 flushout(struct output *dest)
190 if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
191 return;
192 if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
193 dest->flags |= OUTPUT_ERR;
194 dest->nextc = dest->buf;
195 dest->nleft = dest->bufsize;
199 void
200 freestdout(void)
202 INTOFF;
203 if (output.buf) {
204 ckfree(output.buf);
205 output.buf = NULL;
206 output.nleft = 0;
208 INTON;
212 void
213 outfmt(struct output *file, const char *fmt, ...)
215 va_list ap;
217 va_start(ap, fmt);
218 doformat(file, fmt, ap);
219 va_end(ap);
223 void
224 out1fmt(const char *fmt, ...)
226 va_list ap;
228 va_start(ap, fmt);
229 doformat(out1, fmt, ap);
230 va_end(ap);
233 void
234 dprintf(const char *fmt, ...)
236 va_list ap;
238 va_start(ap, fmt);
239 doformat(out2, fmt, ap);
240 va_end(ap);
241 flushout(out2);
244 void
245 fmtstr(char *outbuf, size_t length, const char *fmt, ...)
247 va_list ap;
248 struct output strout;
250 va_start(ap, fmt);
251 strout.nextc = outbuf;
252 strout.nleft = length;
253 strout.fd = BLOCK_OUT;
254 strout.flags = 0;
255 doformat(&strout, fmt, ap);
256 outc('\0', &strout);
257 if (strout.flags & OUTPUT_ERR)
258 outbuf[length - 1] = '\0';
259 va_end(ap);
263 * Formatted output. This routine handles a subset of the printf formats:
264 * - Formats supported: d, u, o, p, X, s, and c.
265 * - The x format is also accepted but is treated like X.
266 * - The l, ll and q modifiers are accepted.
267 * - The - and # flags are accepted; # only works with the o format.
268 * - Width and precision may be specified with any format except c.
269 * - An * may be given for the width or precision.
270 * - The obsolete practice of preceding the width with a zero to get
271 * zero padding is not supported; use the precision field.
272 * - A % may be printed by writing %% in the format string.
275 #define TEMPSIZE 24
277 #ifdef BSD4_4
278 #define HAVE_VASPRINTF 1
279 #endif
281 void
282 doformat(struct output *dest, const char *f, va_list ap)
284 #if HAVE_VASPRINTF
285 char *s;
287 vasprintf(&s, f, ap);
288 outstr(s, dest);
289 free(s);
290 #else /* !HAVE_VASPRINTF */
291 static const char digit[] = "0123456789ABCDEF";
292 char c;
293 char temp[TEMPSIZE];
294 int flushleft;
295 int sharp;
296 int width;
297 int prec;
298 int islong;
299 int isquad;
300 char *p;
301 int sign;
302 #ifdef BSD4_4
303 quad_t l;
304 u_quad_t num;
305 #else
306 long l;
307 u_long num;
308 #endif
309 unsigned base;
310 int len;
311 int size;
312 int pad;
314 while ((c = *f++) != '\0') {
315 if (c != '%') {
316 outc(c, dest);
317 continue;
319 flushleft = 0;
320 sharp = 0;
321 width = 0;
322 prec = -1;
323 islong = 0;
324 isquad = 0;
325 for (;;) {
326 if (*f == '-')
327 flushleft++;
328 else if (*f == '#')
329 sharp++;
330 else
331 break;
332 f++;
334 if (*f == '*') {
335 width = va_arg(ap, int);
336 f++;
337 } else {
338 while (is_digit(*f)) {
339 width = 10 * width + digit_val(*f++);
342 if (*f == '.') {
343 if (*++f == '*') {
344 prec = va_arg(ap, int);
345 f++;
346 } else {
347 prec = 0;
348 while (is_digit(*f)) {
349 prec = 10 * prec + digit_val(*f++);
353 if (*f == 'l') {
354 f++;
355 if (*f == 'l') {
356 isquad++;
357 f++;
358 } else
359 islong++;
360 } else if (*f == 'q') {
361 isquad++;
362 f++;
364 switch (*f) {
365 case 'd':
366 #ifdef BSD4_4
367 if (isquad)
368 l = va_arg(ap, quad_t);
369 else
370 #endif
371 if (islong)
372 l = va_arg(ap, long);
373 else
374 l = va_arg(ap, int);
375 sign = 0;
376 num = l;
377 if (l < 0) {
378 num = -l;
379 sign = 1;
381 base = 10;
382 goto number;
383 case 'u':
384 base = 10;
385 goto uns_number;
386 case 'o':
387 base = 8;
388 goto uns_number;
389 case 'p':
390 outc('0', dest);
391 outc('x', dest);
392 /*FALLTHROUGH*/
393 case 'x':
394 /* we don't implement 'x'; treat like 'X' */
395 case 'X':
396 base = 16;
397 uns_number: /* an unsigned number */
398 sign = 0;
399 #ifdef BSD4_4
400 if (isquad)
401 num = va_arg(ap, u_quad_t);
402 else
403 #endif
404 if (islong)
405 num = va_arg(ap, unsigned long);
406 else
407 num = va_arg(ap, unsigned int);
408 number: /* process a number */
409 p = temp + TEMPSIZE - 1;
410 *p = '\0';
411 while (num) {
412 *--p = digit[num % base];
413 num /= base;
415 len = (temp + TEMPSIZE - 1) - p;
416 if (prec < 0)
417 prec = 1;
418 if (sharp && *f == 'o' && prec <= len)
419 prec = len + 1;
420 pad = 0;
421 if (width) {
422 size = len;
423 if (size < prec)
424 size = prec;
425 size += sign;
426 pad = width - size;
427 if (flushleft == 0) {
428 while (--pad >= 0)
429 outc(' ', dest);
432 if (sign)
433 outc('-', dest);
434 prec -= len;
435 while (--prec >= 0)
436 outc('0', dest);
437 while (*p)
438 outc(*p++, dest);
439 while (--pad >= 0)
440 outc(' ', dest);
441 break;
442 case 's':
443 p = va_arg(ap, char *);
444 pad = 0;
445 if (width) {
446 len = strlen(p);
447 if (prec >= 0 && len > prec)
448 len = prec;
449 pad = width - len;
450 if (flushleft == 0) {
451 while (--pad >= 0)
452 outc(' ', dest);
455 prec++;
456 while (--prec != 0 && *p)
457 outc(*p++, dest);
458 while (--pad >= 0)
459 outc(' ', dest);
460 break;
461 case 'c':
462 c = va_arg(ap, int);
463 outc(c, dest);
464 break;
465 default:
466 outc(*f, dest);
467 break;
469 f++;
471 #endif /* !HAVE_VASPRINTF */
477 * Version of write which resumes after a signal is caught.
481 xwrite(int fd, char *buf, int nbytes)
483 int ntry;
484 int i;
485 int n;
487 n = nbytes;
488 ntry = 0;
489 for (;;) {
490 i = write(fd, buf, n);
491 if (i > 0) {
492 if ((n -= i) <= 0)
493 return nbytes;
494 buf += i;
495 ntry = 0;
496 } else if (i == 0) {
497 if (++ntry > 10)
498 return nbytes - n;
499 } else if (errno != EINTR) {
500 return -1;
507 * Version of ioctl that retries after a signal is caught.
508 * XXX unused function
512 xioctl(int fd, unsigned long request, char *arg)
514 int i;
516 while ((i = ioctl(fd, request, arg)) == -1 && errno == EINTR);
517 return i;