output.c revision 1.20 1 /* $NetBSD: output.c,v 1.20 1998/01/21 10:47:40 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.20 1998/01/21 10:47:40 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 #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 and q modifiers are 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 static const char digit[] = "0123456789ABCDEF";
354
355
356 void
357 doformat(dest, f, ap)
358 struct output *dest;
359 char *f; /* format string */
360 va_list ap;
361 {
362 char c;
363 char temp[TEMPSIZE];
364 int flushleft;
365 int sharp;
366 int width;
367 int prec;
368 int islong;
369 int isquad;
370 char *p;
371 int sign;
372 #ifdef BSD4_4
373 quad_t l;
374 u_quad_t num;
375 #else
376 long l;
377 u_long num;
378 #endif
379 unsigned base;
380 int len;
381 int size;
382 int pad;
383
384 while ((c = *f++) != '\0') {
385 if (c != '%') {
386 outc(c, dest);
387 continue;
388 }
389 flushleft = 0;
390 sharp = 0;
391 width = 0;
392 prec = -1;
393 islong = 0;
394 isquad = 0;
395 for (;;) {
396 if (*f == '-')
397 flushleft++;
398 else if (*f == '#')
399 sharp++;
400 else
401 break;
402 f++;
403 }
404 if (*f == '*') {
405 width = va_arg(ap, int);
406 f++;
407 } else {
408 while (is_digit(*f)) {
409 width = 10 * width + digit_val(*f++);
410 }
411 }
412 if (*f == '.') {
413 if (*++f == '*') {
414 prec = va_arg(ap, int);
415 f++;
416 } else {
417 prec = 0;
418 while (is_digit(*f)) {
419 prec = 10 * prec + digit_val(*f++);
420 }
421 }
422 }
423 if (*f == 'l') {
424 islong++;
425 f++;
426 } else if (*f == 'q') {
427 isquad++;
428 f++;
429 }
430 switch (*f) {
431 case 'd':
432 #ifdef BSD4_4
433 if (isquad)
434 l = va_arg(ap, quad_t);
435 else
436 #endif
437 if (islong)
438 l = va_arg(ap, long);
439 else
440 l = va_arg(ap, int);
441 sign = 0;
442 num = l;
443 if (l < 0) {
444 num = -l;
445 sign = 1;
446 }
447 base = 10;
448 goto number;
449 case 'u':
450 base = 10;
451 goto uns_number;
452 case 'o':
453 base = 8;
454 goto uns_number;
455 case 'x':
456 /* we don't implement 'x'; treat like 'X' */
457 case 'X':
458 base = 16;
459 uns_number: /* an unsigned number */
460 sign = 0;
461 #ifdef BSD4_4
462 if (isquad)
463 num = va_arg(ap, u_quad_t);
464 else
465 #endif
466 if (islong)
467 num = va_arg(ap, unsigned long);
468 else
469 num = va_arg(ap, unsigned int);
470 number: /* process a number */
471 p = temp + TEMPSIZE - 1;
472 *p = '\0';
473 while (num) {
474 *--p = digit[num % base];
475 num /= base;
476 }
477 len = (temp + TEMPSIZE - 1) - p;
478 if (prec < 0)
479 prec = 1;
480 if (sharp && *f == 'o' && prec <= len)
481 prec = len + 1;
482 pad = 0;
483 if (width) {
484 size = len;
485 if (size < prec)
486 size = prec;
487 size += sign;
488 pad = width - size;
489 if (flushleft == 0) {
490 while (--pad >= 0)
491 outc(' ', dest);
492 }
493 }
494 if (sign)
495 outc('-', dest);
496 prec -= len;
497 while (--prec >= 0)
498 outc('0', dest);
499 while (*p)
500 outc(*p++, dest);
501 while (--pad >= 0)
502 outc(' ', dest);
503 break;
504 case 's':
505 p = va_arg(ap, char *);
506 pad = 0;
507 if (width) {
508 len = strlen(p);
509 if (prec >= 0 && len > prec)
510 len = prec;
511 pad = width - len;
512 if (flushleft == 0) {
513 while (--pad >= 0)
514 outc(' ', dest);
515 }
516 }
517 prec++;
518 while (--prec != 0 && *p)
519 outc(*p++, dest);
520 while (--pad >= 0)
521 outc(' ', dest);
522 break;
523 case 'c':
524 c = va_arg(ap, int);
525 outc(c, dest);
526 break;
527 default:
528 outc(*f, dest);
529 break;
530 }
531 f++;
532 }
533 }
534
535
536
537 /*
538 * Version of write which resumes after a signal is caught.
539 */
540
541 int
542 xwrite(fd, buf, nbytes)
543 int fd;
544 char *buf;
545 int nbytes;
546 {
547 int ntry;
548 int i;
549 int n;
550
551 n = nbytes;
552 ntry = 0;
553 for (;;) {
554 i = write(fd, buf, n);
555 if (i > 0) {
556 if ((n -= i) <= 0)
557 return nbytes;
558 buf += i;
559 ntry = 0;
560 } else if (i == 0) {
561 if (++ntry > 10)
562 return nbytes - n;
563 } else if (errno != EINTR) {
564 return -1;
565 }
566 }
567 }
568
569
570 /*
571 * Version of ioctl that retries after a signal is caught.
572 * XXX unused function
573 */
574
575 int
576 xioctl(fd, request, arg)
577 int fd;
578 unsigned long request;
579 char * arg;
580 {
581 int i;
582
583 while ((i = ioctl(fd, request, arg)) == -1 && errno == EINTR);
584 return i;
585 }
586