output.c revision 1.22 1 /* $NetBSD: output.c,v 1.22 2001/01/07 22:19:53 lukem 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.22 2001/01/07 22:19:53 lukem 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, ll 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 f++;
408 if (*f == 'l') {
409 isquad++;
410 f++;
411 } else
412 islong++;
413 } else if (*f == 'q') {
414 isquad++;
415 f++;
416 }
417 switch (*f) {
418 case 'd':
419 #ifdef BSD4_4
420 if (isquad)
421 l = va_arg(ap, quad_t);
422 else
423 #endif
424 if (islong)
425 l = va_arg(ap, long);
426 else
427 l = va_arg(ap, int);
428 sign = 0;
429 num = l;
430 if (l < 0) {
431 num = -l;
432 sign = 1;
433 }
434 base = 10;
435 goto number;
436 case 'u':
437 base = 10;
438 goto uns_number;
439 case 'o':
440 base = 8;
441 goto uns_number;
442 case 'p':
443 outc('0', dest);
444 outc('x', dest);
445 /*FALLTHROUGH*/
446 case 'x':
447 /* we don't implement 'x'; treat like 'X' */
448 case 'X':
449 base = 16;
450 uns_number: /* an unsigned number */
451 sign = 0;
452 #ifdef BSD4_4
453 if (isquad)
454 num = va_arg(ap, u_quad_t);
455 else
456 #endif
457 if (islong)
458 num = va_arg(ap, unsigned long);
459 else
460 num = va_arg(ap, unsigned int);
461 number: /* process a number */
462 p = temp + TEMPSIZE - 1;
463 *p = '\0';
464 while (num) {
465 *--p = digit[num % base];
466 num /= base;
467 }
468 len = (temp + TEMPSIZE - 1) - p;
469 if (prec < 0)
470 prec = 1;
471 if (sharp && *f == 'o' && prec <= len)
472 prec = len + 1;
473 pad = 0;
474 if (width) {
475 size = len;
476 if (size < prec)
477 size = prec;
478 size += sign;
479 pad = width - size;
480 if (flushleft == 0) {
481 while (--pad >= 0)
482 outc(' ', dest);
483 }
484 }
485 if (sign)
486 outc('-', dest);
487 prec -= len;
488 while (--prec >= 0)
489 outc('0', dest);
490 while (*p)
491 outc(*p++, dest);
492 while (--pad >= 0)
493 outc(' ', dest);
494 break;
495 case 's':
496 p = va_arg(ap, char *);
497 pad = 0;
498 if (width) {
499 len = strlen(p);
500 if (prec >= 0 && len > prec)
501 len = prec;
502 pad = width - len;
503 if (flushleft == 0) {
504 while (--pad >= 0)
505 outc(' ', dest);
506 }
507 }
508 prec++;
509 while (--prec != 0 && *p)
510 outc(*p++, dest);
511 while (--pad >= 0)
512 outc(' ', dest);
513 break;
514 case 'c':
515 c = va_arg(ap, int);
516 outc(c, dest);
517 break;
518 default:
519 outc(*f, dest);
520 break;
521 }
522 f++;
523 }
524 }
525
526
527
528 /*
529 * Version of write which resumes after a signal is caught.
530 */
531
532 int
533 xwrite(fd, buf, nbytes)
534 int fd;
535 char *buf;
536 int nbytes;
537 {
538 int ntry;
539 int i;
540 int n;
541
542 n = nbytes;
543 ntry = 0;
544 for (;;) {
545 i = write(fd, buf, n);
546 if (i > 0) {
547 if ((n -= i) <= 0)
548 return nbytes;
549 buf += i;
550 ntry = 0;
551 } else if (i == 0) {
552 if (++ntry > 10)
553 return nbytes - n;
554 } else if (errno != EINTR) {
555 return -1;
556 }
557 }
558 }
559
560
561 /*
562 * Version of ioctl that retries after a signal is caught.
563 * XXX unused function
564 */
565
566 int
567 xioctl(fd, request, arg)
568 int fd;
569 unsigned long request;
570 char * arg;
571 {
572 int i;
573
574 while ((i = ioctl(fd, request, arg)) == -1 && errno == EINTR);
575 return i;
576 }
577