output.c revision 1.28 1 1.28 agc /* $NetBSD: output.c,v 1.28 2003/08/07 09:05:36 agc Exp $ */
2 1.13 cgd
3 1.1 cgd /*-
4 1.7 jtc * Copyright (c) 1991, 1993
5 1.7 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Kenneth Almquist.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.28 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.19 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.13 cgd #if 0
38 1.14 christos static char sccsid[] = "@(#)output.c 8.2 (Berkeley) 5/4/95";
39 1.13 cgd #else
40 1.28 agc __RCSID("$NetBSD: output.c,v 1.28 2003/08/07 09:05:36 agc Exp $");
41 1.13 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd /*
45 1.1 cgd * Shell output routines. We use our own output routines because:
46 1.1 cgd * When a builtin command is interrupted we have to discard
47 1.1 cgd * any pending output.
48 1.1 cgd * When a builtin command appears in back quotes, we want to
49 1.1 cgd * save the output of the command in a region obtained
50 1.1 cgd * via malloc, rather than doing a fork and reading the
51 1.1 cgd * output of the command via a pipe.
52 1.1 cgd * Our output routines may be smaller than the stdio routines.
53 1.1 cgd */
54 1.1 cgd
55 1.20 christos #include <sys/types.h> /* quad_t */
56 1.20 christos #include <sys/param.h> /* BSD4_4 */
57 1.11 cgd #include <sys/ioctl.h>
58 1.11 cgd
59 1.1 cgd #include <stdio.h> /* defines BUFSIZ */
60 1.12 cgd #include <string.h>
61 1.1 cgd #include <errno.h>
62 1.9 jtc #include <unistd.h>
63 1.14 christos #include <stdlib.h>
64 1.14 christos
65 1.14 christos #include "shell.h"
66 1.14 christos #include "syntax.h"
67 1.14 christos #include "output.h"
68 1.14 christos #include "memalloc.h"
69 1.14 christos #include "error.h"
70 1.1 cgd
71 1.1 cgd
72 1.1 cgd #define OUTBUFSIZ BUFSIZ
73 1.1 cgd #define BLOCK_OUT -2 /* output to a fixed block of memory */
74 1.1 cgd #define MEM_OUT -3 /* output to dynamically allocated memory */
75 1.1 cgd #define OUTPUT_ERR 01 /* error occurred on output */
76 1.1 cgd
77 1.1 cgd
78 1.1 cgd struct output output = {NULL, 0, NULL, OUTBUFSIZ, 1, 0};
79 1.18 christos struct output errout = {NULL, 0, NULL, 100, 2, 0};
80 1.1 cgd struct output memout = {NULL, 0, NULL, 0, MEM_OUT, 0};
81 1.1 cgd struct output *out1 = &output;
82 1.1 cgd struct output *out2 = &errout;
83 1.1 cgd
84 1.1 cgd
85 1.1 cgd
86 1.1 cgd #ifdef mkinit
87 1.1 cgd
88 1.1 cgd INCLUDE "output.h"
89 1.1 cgd INCLUDE "memalloc.h"
90 1.1 cgd
91 1.1 cgd RESET {
92 1.1 cgd out1 = &output;
93 1.1 cgd out2 = &errout;
94 1.1 cgd if (memout.buf != NULL) {
95 1.1 cgd ckfree(memout.buf);
96 1.1 cgd memout.buf = NULL;
97 1.1 cgd }
98 1.1 cgd }
99 1.1 cgd
100 1.1 cgd #endif
101 1.1 cgd
102 1.1 cgd
103 1.1 cgd #ifdef notdef /* no longer used */
104 1.1 cgd /*
105 1.1 cgd * Set up an output file to write to memory rather than a file.
106 1.1 cgd */
107 1.1 cgd
108 1.1 cgd void
109 1.27 christos open_mem(char *block, int length, struct output *file)
110 1.27 christos {
111 1.1 cgd file->nextc = block;
112 1.1 cgd file->nleft = --length;
113 1.1 cgd file->fd = BLOCK_OUT;
114 1.1 cgd file->flags = 0;
115 1.1 cgd }
116 1.1 cgd #endif
117 1.1 cgd
118 1.1 cgd
119 1.1 cgd void
120 1.27 christos out1str(const char *p)
121 1.27 christos {
122 1.1 cgd outstr(p, out1);
123 1.1 cgd }
124 1.1 cgd
125 1.1 cgd
126 1.1 cgd void
127 1.27 christos out2str(const char *p)
128 1.27 christos {
129 1.1 cgd outstr(p, out2);
130 1.1 cgd }
131 1.1 cgd
132 1.1 cgd
133 1.1 cgd void
134 1.27 christos outstr(const char *p, struct output *file)
135 1.27 christos {
136 1.1 cgd while (*p)
137 1.1 cgd outc(*p++, file);
138 1.7 jtc if (file == out2)
139 1.7 jtc flushout(file);
140 1.1 cgd }
141 1.1 cgd
142 1.1 cgd
143 1.1 cgd char out_junk[16];
144 1.1 cgd
145 1.1 cgd
146 1.1 cgd void
147 1.27 christos emptyoutbuf(struct output *dest)
148 1.27 christos {
149 1.1 cgd int offset;
150 1.1 cgd
151 1.1 cgd if (dest->fd == BLOCK_OUT) {
152 1.1 cgd dest->nextc = out_junk;
153 1.1 cgd dest->nleft = sizeof out_junk;
154 1.1 cgd dest->flags |= OUTPUT_ERR;
155 1.1 cgd } else if (dest->buf == NULL) {
156 1.1 cgd INTOFF;
157 1.1 cgd dest->buf = ckmalloc(dest->bufsize);
158 1.1 cgd dest->nextc = dest->buf;
159 1.1 cgd dest->nleft = dest->bufsize;
160 1.1 cgd INTON;
161 1.1 cgd } else if (dest->fd == MEM_OUT) {
162 1.1 cgd offset = dest->bufsize;
163 1.1 cgd INTOFF;
164 1.1 cgd dest->bufsize <<= 1;
165 1.1 cgd dest->buf = ckrealloc(dest->buf, dest->bufsize);
166 1.1 cgd dest->nleft = dest->bufsize - offset;
167 1.1 cgd dest->nextc = dest->buf + offset;
168 1.1 cgd INTON;
169 1.1 cgd } else {
170 1.1 cgd flushout(dest);
171 1.1 cgd }
172 1.1 cgd dest->nleft--;
173 1.1 cgd }
174 1.1 cgd
175 1.1 cgd
176 1.1 cgd void
177 1.27 christos flushall(void)
178 1.27 christos {
179 1.1 cgd flushout(&output);
180 1.1 cgd flushout(&errout);
181 1.1 cgd }
182 1.1 cgd
183 1.1 cgd
184 1.1 cgd void
185 1.27 christos flushout(struct output *dest)
186 1.27 christos {
187 1.1 cgd
188 1.1 cgd if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
189 1.1 cgd return;
190 1.1 cgd if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
191 1.1 cgd dest->flags |= OUTPUT_ERR;
192 1.1 cgd dest->nextc = dest->buf;
193 1.1 cgd dest->nleft = dest->bufsize;
194 1.1 cgd }
195 1.1 cgd
196 1.1 cgd
197 1.1 cgd void
198 1.27 christos freestdout(void)
199 1.27 christos {
200 1.1 cgd INTOFF;
201 1.1 cgd if (output.buf) {
202 1.1 cgd ckfree(output.buf);
203 1.1 cgd output.buf = NULL;
204 1.1 cgd output.nleft = 0;
205 1.1 cgd }
206 1.1 cgd INTON;
207 1.1 cgd }
208 1.1 cgd
209 1.1 cgd
210 1.21 christos void
211 1.21 christos outfmt(struct output *file, const char *fmt, ...)
212 1.21 christos {
213 1.1 cgd va_list ap;
214 1.1 cgd
215 1.21 christos va_start(ap, fmt);
216 1.1 cgd doformat(file, fmt, ap);
217 1.1 cgd va_end(ap);
218 1.1 cgd }
219 1.1 cgd
220 1.1 cgd
221 1.1 cgd void
222 1.21 christos out1fmt(const char *fmt, ...)
223 1.21 christos {
224 1.1 cgd va_list ap;
225 1.1 cgd
226 1.21 christos va_start(ap, fmt);
227 1.1 cgd doformat(out1, fmt, ap);
228 1.1 cgd va_end(ap);
229 1.1 cgd }
230 1.1 cgd
231 1.7 jtc void
232 1.21 christos dprintf(const char *fmt, ...)
233 1.21 christos {
234 1.7 jtc va_list ap;
235 1.7 jtc
236 1.21 christos va_start(ap, fmt);
237 1.7 jtc doformat(out2, fmt, ap);
238 1.7 jtc va_end(ap);
239 1.7 jtc flushout(out2);
240 1.7 jtc }
241 1.1 cgd
242 1.1 cgd void
243 1.21 christos fmtstr(char *outbuf, size_t length, const char *fmt, ...)
244 1.21 christos {
245 1.1 cgd va_list ap;
246 1.1 cgd struct output strout;
247 1.26 wiz
248 1.21 christos va_start(ap, fmt);
249 1.1 cgd strout.nextc = outbuf;
250 1.1 cgd strout.nleft = length;
251 1.1 cgd strout.fd = BLOCK_OUT;
252 1.1 cgd strout.flags = 0;
253 1.1 cgd doformat(&strout, fmt, ap);
254 1.1 cgd outc('\0', &strout);
255 1.1 cgd if (strout.flags & OUTPUT_ERR)
256 1.1 cgd outbuf[length - 1] = '\0';
257 1.24 wiz va_end(ap);
258 1.1 cgd }
259 1.1 cgd
260 1.1 cgd /*
261 1.1 cgd * Formatted output. This routine handles a subset of the printf formats:
262 1.21 christos * - Formats supported: d, u, o, p, X, s, and c.
263 1.1 cgd * - The x format is also accepted but is treated like X.
264 1.22 lukem * - The l, ll and q modifiers are accepted.
265 1.1 cgd * - The - and # flags are accepted; # only works with the o format.
266 1.1 cgd * - Width and precision may be specified with any format except c.
267 1.1 cgd * - An * may be given for the width or precision.
268 1.1 cgd * - The obsolete practice of preceding the width with a zero to get
269 1.1 cgd * zero padding is not supported; use the precision field.
270 1.1 cgd * - A % may be printed by writing %% in the format string.
271 1.1 cgd */
272 1.1 cgd
273 1.1 cgd #define TEMPSIZE 24
274 1.1 cgd
275 1.23 lukem #ifdef BSD4_4
276 1.23 lukem #define HAVE_VASPRINTF 1
277 1.23 lukem #endif
278 1.23 lukem
279 1.1 cgd void
280 1.27 christos doformat(struct output *dest, const char *f, va_list ap)
281 1.23 lukem {
282 1.23 lukem #if HAVE_VASPRINTF
283 1.23 lukem char *s;
284 1.23 lukem
285 1.23 lukem vasprintf(&s, f, ap);
286 1.23 lukem outstr(s, dest);
287 1.23 lukem free(s);
288 1.23 lukem #else /* !HAVE_VASPRINTF */
289 1.25 thorpej static const char digit[] = "0123456789ABCDEF";
290 1.17 tls char c;
291 1.1 cgd char temp[TEMPSIZE];
292 1.1 cgd int flushleft;
293 1.1 cgd int sharp;
294 1.1 cgd int width;
295 1.1 cgd int prec;
296 1.1 cgd int islong;
297 1.15 jtc int isquad;
298 1.1 cgd char *p;
299 1.1 cgd int sign;
300 1.18 christos #ifdef BSD4_4
301 1.15 jtc quad_t l;
302 1.15 jtc u_quad_t num;
303 1.18 christos #else
304 1.18 christos long l;
305 1.18 christos u_long num;
306 1.18 christos #endif
307 1.1 cgd unsigned base;
308 1.1 cgd int len;
309 1.1 cgd int size;
310 1.1 cgd int pad;
311 1.1 cgd
312 1.1 cgd while ((c = *f++) != '\0') {
313 1.1 cgd if (c != '%') {
314 1.1 cgd outc(c, dest);
315 1.1 cgd continue;
316 1.1 cgd }
317 1.1 cgd flushleft = 0;
318 1.1 cgd sharp = 0;
319 1.1 cgd width = 0;
320 1.1 cgd prec = -1;
321 1.1 cgd islong = 0;
322 1.15 jtc isquad = 0;
323 1.1 cgd for (;;) {
324 1.1 cgd if (*f == '-')
325 1.1 cgd flushleft++;
326 1.1 cgd else if (*f == '#')
327 1.1 cgd sharp++;
328 1.1 cgd else
329 1.1 cgd break;
330 1.1 cgd f++;
331 1.1 cgd }
332 1.1 cgd if (*f == '*') {
333 1.1 cgd width = va_arg(ap, int);
334 1.1 cgd f++;
335 1.1 cgd } else {
336 1.1 cgd while (is_digit(*f)) {
337 1.1 cgd width = 10 * width + digit_val(*f++);
338 1.1 cgd }
339 1.1 cgd }
340 1.1 cgd if (*f == '.') {
341 1.1 cgd if (*++f == '*') {
342 1.1 cgd prec = va_arg(ap, int);
343 1.1 cgd f++;
344 1.1 cgd } else {
345 1.1 cgd prec = 0;
346 1.1 cgd while (is_digit(*f)) {
347 1.1 cgd prec = 10 * prec + digit_val(*f++);
348 1.1 cgd }
349 1.1 cgd }
350 1.1 cgd }
351 1.1 cgd if (*f == 'l') {
352 1.1 cgd f++;
353 1.22 lukem if (*f == 'l') {
354 1.22 lukem isquad++;
355 1.22 lukem f++;
356 1.22 lukem } else
357 1.22 lukem islong++;
358 1.15 jtc } else if (*f == 'q') {
359 1.15 jtc isquad++;
360 1.15 jtc f++;
361 1.1 cgd }
362 1.1 cgd switch (*f) {
363 1.1 cgd case 'd':
364 1.18 christos #ifdef BSD4_4
365 1.15 jtc if (isquad)
366 1.15 jtc l = va_arg(ap, quad_t);
367 1.18 christos else
368 1.18 christos #endif
369 1.18 christos if (islong)
370 1.1 cgd l = va_arg(ap, long);
371 1.1 cgd else
372 1.1 cgd l = va_arg(ap, int);
373 1.1 cgd sign = 0;
374 1.1 cgd num = l;
375 1.1 cgd if (l < 0) {
376 1.1 cgd num = -l;
377 1.1 cgd sign = 1;
378 1.1 cgd }
379 1.1 cgd base = 10;
380 1.1 cgd goto number;
381 1.1 cgd case 'u':
382 1.1 cgd base = 10;
383 1.1 cgd goto uns_number;
384 1.1 cgd case 'o':
385 1.1 cgd base = 8;
386 1.1 cgd goto uns_number;
387 1.21 christos case 'p':
388 1.21 christos outc('0', dest);
389 1.21 christos outc('x', dest);
390 1.21 christos /*FALLTHROUGH*/
391 1.1 cgd case 'x':
392 1.1 cgd /* we don't implement 'x'; treat like 'X' */
393 1.1 cgd case 'X':
394 1.1 cgd base = 16;
395 1.1 cgd uns_number: /* an unsigned number */
396 1.1 cgd sign = 0;
397 1.18 christos #ifdef BSD4_4
398 1.15 jtc if (isquad)
399 1.15 jtc num = va_arg(ap, u_quad_t);
400 1.18 christos else
401 1.18 christos #endif
402 1.18 christos if (islong)
403 1.1 cgd num = va_arg(ap, unsigned long);
404 1.1 cgd else
405 1.1 cgd num = va_arg(ap, unsigned int);
406 1.1 cgd number: /* process a number */
407 1.1 cgd p = temp + TEMPSIZE - 1;
408 1.1 cgd *p = '\0';
409 1.1 cgd while (num) {
410 1.1 cgd *--p = digit[num % base];
411 1.1 cgd num /= base;
412 1.1 cgd }
413 1.1 cgd len = (temp + TEMPSIZE - 1) - p;
414 1.1 cgd if (prec < 0)
415 1.1 cgd prec = 1;
416 1.1 cgd if (sharp && *f == 'o' && prec <= len)
417 1.1 cgd prec = len + 1;
418 1.1 cgd pad = 0;
419 1.1 cgd if (width) {
420 1.1 cgd size = len;
421 1.1 cgd if (size < prec)
422 1.1 cgd size = prec;
423 1.1 cgd size += sign;
424 1.1 cgd pad = width - size;
425 1.1 cgd if (flushleft == 0) {
426 1.1 cgd while (--pad >= 0)
427 1.1 cgd outc(' ', dest);
428 1.1 cgd }
429 1.1 cgd }
430 1.1 cgd if (sign)
431 1.1 cgd outc('-', dest);
432 1.1 cgd prec -= len;
433 1.1 cgd while (--prec >= 0)
434 1.1 cgd outc('0', dest);
435 1.1 cgd while (*p)
436 1.1 cgd outc(*p++, dest);
437 1.1 cgd while (--pad >= 0)
438 1.1 cgd outc(' ', dest);
439 1.1 cgd break;
440 1.1 cgd case 's':
441 1.1 cgd p = va_arg(ap, char *);
442 1.1 cgd pad = 0;
443 1.1 cgd if (width) {
444 1.1 cgd len = strlen(p);
445 1.1 cgd if (prec >= 0 && len > prec)
446 1.1 cgd len = prec;
447 1.1 cgd pad = width - len;
448 1.1 cgd if (flushleft == 0) {
449 1.1 cgd while (--pad >= 0)
450 1.1 cgd outc(' ', dest);
451 1.1 cgd }
452 1.1 cgd }
453 1.1 cgd prec++;
454 1.1 cgd while (--prec != 0 && *p)
455 1.1 cgd outc(*p++, dest);
456 1.1 cgd while (--pad >= 0)
457 1.1 cgd outc(' ', dest);
458 1.1 cgd break;
459 1.1 cgd case 'c':
460 1.1 cgd c = va_arg(ap, int);
461 1.1 cgd outc(c, dest);
462 1.1 cgd break;
463 1.1 cgd default:
464 1.1 cgd outc(*f, dest);
465 1.1 cgd break;
466 1.1 cgd }
467 1.1 cgd f++;
468 1.1 cgd }
469 1.23 lukem #endif /* !HAVE_VASPRINTF */
470 1.1 cgd }
471 1.1 cgd
472 1.1 cgd
473 1.1 cgd
474 1.1 cgd /*
475 1.1 cgd * Version of write which resumes after a signal is caught.
476 1.1 cgd */
477 1.1 cgd
478 1.1 cgd int
479 1.27 christos xwrite(int fd, char *buf, int nbytes)
480 1.27 christos {
481 1.1 cgd int ntry;
482 1.1 cgd int i;
483 1.1 cgd int n;
484 1.1 cgd
485 1.1 cgd n = nbytes;
486 1.1 cgd ntry = 0;
487 1.1 cgd for (;;) {
488 1.1 cgd i = write(fd, buf, n);
489 1.1 cgd if (i > 0) {
490 1.1 cgd if ((n -= i) <= 0)
491 1.1 cgd return nbytes;
492 1.1 cgd buf += i;
493 1.1 cgd ntry = 0;
494 1.1 cgd } else if (i == 0) {
495 1.1 cgd if (++ntry > 10)
496 1.1 cgd return nbytes - n;
497 1.1 cgd } else if (errno != EINTR) {
498 1.1 cgd return -1;
499 1.1 cgd }
500 1.1 cgd }
501 1.1 cgd }
502 1.1 cgd
503 1.1 cgd
504 1.1 cgd /*
505 1.1 cgd * Version of ioctl that retries after a signal is caught.
506 1.11 cgd * XXX unused function
507 1.1 cgd */
508 1.1 cgd
509 1.1 cgd int
510 1.27 christos xioctl(int fd, unsigned long request, char *arg)
511 1.11 cgd {
512 1.1 cgd int i;
513 1.1 cgd
514 1.1 cgd while ((i = ioctl(fd, request, arg)) == -1 && errno == EINTR);
515 1.1 cgd return i;
516 1.1 cgd }
517