output.c revision 1.36 1 /* $NetBSD: output.c,v 1.36 2017/05/18 13:31:10 kre 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. 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.
21 *
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.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)output.c 8.2 (Berkeley) 5/4/95";
39 #else
40 __RCSID("$NetBSD: output.c,v 1.36 2017/05/18 13:31:10 kre Exp $");
41 #endif
42 #endif /* not lint */
43
44 /*
45 * Shell output routines. We use our own output routines because:
46 * When a builtin command is interrupted we have to discard
47 * any pending output.
48 * When a builtin command appears in back quotes, we want to
49 * save the output of the command in a region obtained
50 * via malloc, rather than doing a fork and reading the
51 * output of the command via a pipe.
52 * Our output routines may be smaller than the stdio routines.
53 */
54
55 #include <sys/types.h> /* quad_t */
56 #include <sys/param.h> /* BSD4_4 */
57 #include <sys/ioctl.h>
58
59 #include <stdio.h> /* defines BUFSIZ */
60 #include <string.h>
61 #include <errno.h>
62 #include <unistd.h>
63 #include <stdlib.h>
64
65 #include "shell.h"
66 #include "syntax.h"
67 #include "output.h"
68 #include "memalloc.h"
69 #include "error.h"
70
71
72 #define OUTBUFSIZ BUFSIZ
73 #define BLOCK_OUT -2 /* output to a fixed block of memory */
74 #define MEM_OUT -3 /* output to dynamically allocated memory */
75
76
77 struct output output = {NULL, 0, NULL, OUTBUFSIZ, 1, 0};
78 struct output errout = {NULL, 0, NULL, 100, 2, 0};
79 struct output memout = {NULL, 0, NULL, 0, MEM_OUT, 0};
80 struct output *out1 = &output;
81 struct output *out2 = &errout;
82
83
84
85 #ifdef mkinit
86
87 INCLUDE "output.h"
88 INCLUDE "memalloc.h"
89
90 RESET {
91 out1 = &output;
92 out2 = &errout;
93 if (memout.buf != NULL) {
94 ckfree(memout.buf);
95 memout.buf = NULL;
96 }
97 }
98
99 #endif
100
101
102 #ifdef notdef /* no longer used */
103 /*
104 * Set up an output file to write to memory rather than a file.
105 */
106
107 void
108 open_mem(char *block, int length, struct output *file)
109 {
110 file->nextc = block;
111 file->nleft = --length;
112 file->fd = BLOCK_OUT;
113 file->flags = 0;
114 }
115 #endif
116
117
118 void
119 out1str(const char *p)
120 {
121 outstr(p, out1);
122 }
123
124
125 void
126 out2str(const char *p)
127 {
128 outstr(p, out2);
129 }
130
131
132 void
133 outstr(const char *p, struct output *file)
134 {
135 while (*p)
136 outc(*p++, file);
137 if (file == out2)
138 flushout(file);
139 }
140
141
142 void
143 out2shstr(const char *p)
144 {
145 outshstr(p, out2);
146 }
147
148
149 static const char norm_chars [] = \
150 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/+-=_,.'";
151
152 static int
153 inquote(const char *p)
154 {
155 size_t l = strspn(p, norm_chars);
156 char *s = strchr(p, '\'');
157
158 return s == NULL ? p[l] != '\0' : s - p > (off_t)l;
159 }
160
161
162 void
163 outshstr(const char *p, struct output *file)
164 {
165 /*
166 * ' is in this list, not because it does not require quoting
167 * (which applies to all the others) but because '' quoting cannot
168 * be used to quote it.
169 */
170 int need_q = p[0] == 0 || p[strspn(p, norm_chars)] != 0;
171 int inq;
172 char c;
173
174 /*
175 * Don't emit ' unless something needs quoting before closing '
176 */
177 if (need_q) {
178 if ((inq = inquote(p)) != 0)
179 outc('\'', file);
180 } else
181 inq = 0;
182
183 while ((c = *p++) != '\0') {
184 if (c != '\'') {
185 outc(c, file);
186 continue;
187 }
188
189 if (inq)
190 outc('\'', file); /* inq = 0, implicit */
191 outc('\\', file);
192 outc(c, file);
193 if (need_q && *p != '\0') {
194 if ((inq = inquote(p)) != 0)
195 outc('\'', file);
196 } else
197 inq = 0;
198 }
199
200 if (inq)
201 outc('\'', file);
202
203 if (file == out2)
204 flushout(file);
205 }
206
207
208 char out_junk[16];
209
210
211 void
212 emptyoutbuf(struct output *dest)
213 {
214 int offset;
215
216 if (dest->fd == BLOCK_OUT) {
217 dest->nextc = out_junk;
218 dest->nleft = sizeof out_junk;
219 dest->flags |= OUTPUT_ERR;
220 } else if (dest->buf == NULL) {
221 INTOFF;
222 dest->buf = ckmalloc(dest->bufsize);
223 dest->nextc = dest->buf;
224 dest->nleft = dest->bufsize;
225 INTON;
226 } else if (dest->fd == MEM_OUT) {
227 offset = dest->bufsize;
228 INTOFF;
229 dest->bufsize <<= 1;
230 dest->buf = ckrealloc(dest->buf, dest->bufsize);
231 dest->nleft = dest->bufsize - offset;
232 dest->nextc = dest->buf + offset;
233 INTON;
234 } else {
235 flushout(dest);
236 }
237 dest->nleft--;
238 }
239
240
241 void
242 flushall(void)
243 {
244 flushout(&output);
245 flushout(&errout);
246 }
247
248
249 void
250 flushout(struct output *dest)
251 {
252
253 if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
254 return;
255 if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
256 dest->flags |= OUTPUT_ERR;
257 dest->nextc = dest->buf;
258 dest->nleft = dest->bufsize;
259 }
260
261
262 void
263 freestdout(void)
264 {
265 INTOFF;
266 if (output.buf) {
267 ckfree(output.buf);
268 output.buf = NULL;
269 output.nleft = 0;
270 }
271 INTON;
272 }
273
274
275 void
276 outfmt(struct output *file, const char *fmt, ...)
277 {
278 va_list ap;
279
280 va_start(ap, fmt);
281 doformat(file, fmt, ap);
282 va_end(ap);
283 }
284
285
286 void
287 out1fmt(const char *fmt, ...)
288 {
289 va_list ap;
290
291 va_start(ap, fmt);
292 doformat(out1, fmt, ap);
293 va_end(ap);
294 }
295
296 #ifdef DEBUG
297 void
298 debugprintf(const char *fmt, ...)
299 {
300 va_list ap;
301
302 va_start(ap, fmt);
303 doformat(out2, fmt, ap);
304 va_end(ap);
305 flushout(out2);
306 }
307 #endif
308
309 void
310 fmtstr(char *outbuf, size_t length, const char *fmt, ...)
311 {
312 va_list ap;
313 struct output strout;
314
315 va_start(ap, fmt);
316 strout.nextc = outbuf;
317 strout.nleft = length;
318 strout.fd = BLOCK_OUT;
319 strout.flags = 0;
320 doformat(&strout, fmt, ap);
321 outc('\0', &strout);
322 if (strout.flags & OUTPUT_ERR)
323 outbuf[length - 1] = '\0';
324 va_end(ap);
325 }
326
327 /*
328 * Formatted output. This routine handles a subset of the printf formats:
329 * - Formats supported: d, u, o, p, X, s, and c.
330 * - The x format is also accepted but is treated like X.
331 * - The l, ll and q modifiers are accepted.
332 * - The - and # flags are accepted; # only works with the o format.
333 * - Width and precision may be specified with any format except c.
334 * - An * may be given for the width or precision.
335 * - The obsolete practice of preceding the width with a zero to get
336 * zero padding is not supported; use the precision field.
337 * - A % may be printed by writing %% in the format string.
338 */
339
340 #define TEMPSIZE 24
341
342 #ifdef BSD4_4
343 #define HAVE_VASPRINTF 1
344 #endif
345
346 void
347 doformat(struct output *dest, const char *f, va_list ap)
348 {
349 #if HAVE_VASPRINTF
350 char *s;
351
352 vasprintf(&s, f, ap);
353 if (s == NULL)
354 error("Could not allocate formatted output buffer");
355 outstr(s, dest);
356 free(s);
357 #else /* !HAVE_VASPRINTF */
358 static const char digit[] = "0123456789ABCDEF";
359 char c;
360 char temp[TEMPSIZE];
361 int flushleft;
362 int sharp;
363 int width;
364 int prec;
365 int islong;
366 int isquad;
367 char *p;
368 int sign;
369 #ifdef BSD4_4
370 quad_t l;
371 u_quad_t num;
372 #else
373 long l;
374 u_long num;
375 #endif
376 unsigned base;
377 int len;
378 int size;
379 int pad;
380
381 while ((c = *f++) != '\0') {
382 if (c != '%') {
383 outc(c, dest);
384 continue;
385 }
386 flushleft = 0;
387 sharp = 0;
388 width = 0;
389 prec = -1;
390 islong = 0;
391 isquad = 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 f++;
422 if (*f == 'l') {
423 isquad++;
424 f++;
425 } else
426 islong++;
427 } else if (*f == 'q') {
428 isquad++;
429 f++;
430 }
431 switch (*f) {
432 case 'd':
433 #ifdef BSD4_4
434 if (isquad)
435 l = va_arg(ap, quad_t);
436 else
437 #endif
438 if (islong)
439 l = va_arg(ap, long);
440 else
441 l = va_arg(ap, int);
442 sign = 0;
443 num = l;
444 if (l < 0) {
445 num = -l;
446 sign = 1;
447 }
448 base = 10;
449 goto number;
450 case 'u':
451 base = 10;
452 goto uns_number;
453 case 'o':
454 base = 8;
455 goto uns_number;
456 case 'p':
457 outc('0', dest);
458 outc('x', dest);
459 /*FALLTHROUGH*/
460 case 'x':
461 /* we don't implement 'x'; treat like 'X' */
462 case 'X':
463 base = 16;
464 uns_number: /* an unsigned number */
465 sign = 0;
466 #ifdef BSD4_4
467 if (isquad)
468 num = va_arg(ap, u_quad_t);
469 else
470 #endif
471 if (islong)
472 num = va_arg(ap, unsigned long);
473 else
474 num = va_arg(ap, unsigned int);
475 number: /* process a number */
476 p = temp + TEMPSIZE - 1;
477 *p = '\0';
478 while (num) {
479 *--p = digit[num % base];
480 num /= base;
481 }
482 len = (temp + TEMPSIZE - 1) - p;
483 if (prec < 0)
484 prec = 1;
485 if (sharp && *f == 'o' && prec <= len)
486 prec = len + 1;
487 pad = 0;
488 if (width) {
489 size = len;
490 if (size < prec)
491 size = prec;
492 size += sign;
493 pad = width - size;
494 if (flushleft == 0) {
495 while (--pad >= 0)
496 outc(' ', dest);
497 }
498 }
499 if (sign)
500 outc('-', dest);
501 prec -= len;
502 while (--prec >= 0)
503 outc('0', dest);
504 while (*p)
505 outc(*p++, dest);
506 while (--pad >= 0)
507 outc(' ', dest);
508 break;
509 case 's':
510 p = va_arg(ap, char *);
511 pad = 0;
512 if (width) {
513 len = strlen(p);
514 if (prec >= 0 && len > prec)
515 len = prec;
516 pad = width - len;
517 if (flushleft == 0) {
518 while (--pad >= 0)
519 outc(' ', dest);
520 }
521 }
522 prec++;
523 while (--prec != 0 && *p)
524 outc(*p++, dest);
525 while (--pad >= 0)
526 outc(' ', dest);
527 break;
528 case 'c':
529 c = va_arg(ap, int);
530 outc(c, dest);
531 break;
532 default:
533 outc(*f, dest);
534 break;
535 }
536 f++;
537 }
538 #endif /* !HAVE_VASPRINTF */
539 }
540
541
542
543 /*
544 * Version of write which resumes after a signal is caught.
545 */
546
547 int
548 xwrite(int fd, char *buf, int nbytes)
549 {
550 int ntry;
551 int i;
552 int n;
553
554 n = nbytes;
555 ntry = 0;
556 while (n > 0) {
557 i = write(fd, buf, n);
558 if (i > 0) {
559 if ((n -= i) <= 0)
560 return nbytes;
561 buf += i;
562 ntry = 0;
563 } else if (i == 0) {
564 if (++ntry > 10)
565 return nbytes - n;
566 } else if (errno != EINTR) {
567 return -1;
568 }
569 }
570 return nbytes;
571 }
572
573
574 /*
575 * Version of ioctl that retries after a signal is caught.
576 * XXX unused function
577 */
578
579 int
580 xioctl(int fd, unsigned long request, char *arg)
581 {
582 int i;
583
584 while ((i = ioctl(fd, request, arg)) == -1 && errno == EINTR);
585 return i;
586 }
587