1 /* $NetBSD: output.c,v 1.28 2003/08/07 09:05:36 agc 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 #if 0
36 #ifndef lint
37 static char sccsid[] = "@(#)output.c 8.2 (Berkeley) 5/4/95";
38 #else
39 __RCSID("$NetBSD: output.c,v 1.28 2003/08/07 09:05:36 agc Exp $");
40 #endif /* not lint */
41 #endif
42
43 /*
44 * Shell output routines. We use our own output routines because:
45 * When a builtin command is interrupted we have to discard
46 * any pending output.
47 * When a builtin command appears in back quotes, we want to
48 * save the output of the command in a region obtained
49 * via malloc, rather than doing a fork and reading the
50 * output of the command via a pipe.
51 * Our output routines may be smaller than the stdio routines.
52 */
53
54 #include <sys/types.h>
55
56 #include <stdio.h> /* defines BUFSIZ */
57 #include <string.h>
58 #include <errno.h>
59 #include <stdlib.h>
60
61 #include "shell.h"
62 #include "syntax.h"
63 #include "output.h"
64 #include "memalloc.h"
65 #include "error.h"
66 #include "shinstance.h"
67
68 //#define OUTBUFSIZ BUFSIZ
69 #define BLOCK_OUT -2 /* output to a fixed block of memory */
70 //#define MEM_OUT -3 /* output to dynamically allocated memory */
71 #define OUTPUT_ERR 01 /* error occurred on output */
72
73
74 //struct output output = {NULL, 0, NULL, OUTBUFSIZ, 1, 0};
75 //struct output errout = {NULL, 0, NULL, 100, 2, 0};
76 //struct output memout = {NULL, 0, NULL, 0, MEM_OUT, 0};
77 //struct output *out1 = &output;
78 //struct output *out2 = &errout;
79
80
81
82 #ifdef mkinit
83
84 INCLUDE "output.h"
85 INCLUDE "memalloc.h"
86
87 RESET {
88 psh->out1 = &psh->output;
89 psh->out2 = &psh->errout;
90 if (psh->memout.buf != NULL) {
91 ckfree(psh, psh->memout.buf);
92 psh->memout.buf = NULL;
93 }
94 }
95
96 #endif
97
98
99 #ifdef notdef /* no longer used */
100 /*
101 * Set up an output file to write to memory rather than a file.
102 */
103
104 void
open_mem(char * block,int length,struct output * file)105 open_mem(char *block, int length, struct output *file)
106 {
107 file->nextc = block;
108 file->nleft = --length;
109 file->fd = BLOCK_OUT;
110 file->flags = 0;
111 file->psh = psh;
112 }
113 #endif
114
115
116 void
out1str(shinstance * psh,const char * p)117 out1str(shinstance *psh, const char *p)
118 {
119 outstr(p, psh->out1);
120 }
121
122
123 void
out2str(shinstance * psh,const char * p)124 out2str(shinstance *psh, const char *p)
125 {
126 outstr(p, psh->out2);
127 }
128
129
130 void
outstr(const char * p,struct output * file)131 outstr(const char *p, struct output *file)
132 {
133 while (*p)
134 outc(*p++, file);
135 if (file->psh && file == file->psh->out2)
136 flushout(file);
137 }
138
139
140 char out_junk[16];
141
142
143 void
emptyoutbuf(struct output * dest)144 emptyoutbuf(struct output *dest)
145 {
146 int offset;
147 shinstance *psh = dest->psh;
148
149 if (dest->fd == BLOCK_OUT) {
150 dest->nextc = out_junk;
151 dest->nleft = sizeof out_junk;
152 dest->flags |= OUTPUT_ERR;
153 } else if (dest->buf == NULL) {
154 INTOFF;
155 dest->buf = ckmalloc(psh, dest->bufsize);
156 dest->nextc = dest->buf;
157 dest->nleft = dest->bufsize;
158 INTON;
159 } else if (dest->fd == MEM_OUT) {
160 offset = dest->bufsize;
161 INTOFF;
162 dest->bufsize <<= 1;
163 dest->buf = ckrealloc(psh, dest->buf, dest->bufsize);
164 dest->nleft = dest->bufsize - offset;
165 dest->nextc = dest->buf + offset;
166 INTON;
167 } else {
168 flushout(dest);
169 }
170 dest->nleft--;
171 }
172
173
174 void
output_flushall(shinstance * psh)175 output_flushall(shinstance *psh)
176 {
177 flushout(&psh->output);
178 flushout(&psh->errout);
179 }
180
181
182 void
flushout(struct output * dest)183 flushout(struct output *dest)
184 {
185
186 if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
187 return;
188 if (xwrite(dest->psh, dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
189 dest->flags |= OUTPUT_ERR;
190 dest->nextc = dest->buf;
191 dest->nleft = dest->bufsize;
192 }
193
194
195 void
freestdout(shinstance * psh)196 freestdout(shinstance *psh)
197 {
198 INTOFF;
199 if (psh->output.buf) {
200 ckfree(psh, psh->output.buf);
201 psh->output.buf = NULL;
202 psh->output.nleft = 0;
203 }
204 INTON;
205 }
206
207
208 void
outfmt(struct output * file,const char * fmt,...)209 outfmt(struct output *file, const char *fmt, ...)
210 {
211 va_list ap;
212
213 va_start(ap, fmt);
214 doformat(file, fmt, ap);
215 va_end(ap);
216 }
217
218
219 void
out1fmt(shinstance * psh,const char * fmt,...)220 out1fmt(shinstance *psh, const char *fmt, ...)
221 {
222 va_list ap;
223
224 va_start(ap, fmt);
225 doformat(psh->out1, fmt, ap);
226 va_end(ap);
227 }
228
229 void
dprintf(shinstance * psh,const char * fmt,...)230 dprintf(shinstance *psh, const char *fmt, ...)
231 {
232 va_list ap;
233
234 va_start(ap, fmt);
235 doformat(psh->out2, fmt, ap);
236 va_end(ap);
237 flushout(psh->out2);
238 }
239
240 void
fmtstr(char * outbuf,size_t length,const char * fmt,...)241 fmtstr(char *outbuf, size_t length, const char *fmt, ...)
242 {
243 va_list ap;
244 struct output strout;
245
246 va_start(ap, fmt);
247 strout.nextc = outbuf;
248 strout.nleft = (int)length;
249 strout.fd = BLOCK_OUT;
250 strout.flags = 0;
251 strout.psh = NULL;
252 doformat(&strout, fmt, ap);
253 outc('\0', &strout);
254 if (strout.flags & OUTPUT_ERR)
255 outbuf[length - 1] = '\0';
256 va_end(ap);
257 }
258
259 /*
260 * Formatted output. This routine handles a subset of the printf formats:
261 * - Formats supported: d, u, o, p, X, s, and c.
262 * - The x format is also accepted but is treated like X.
263 * - The l, ll and q modifiers are accepted.
264 * - The - and # flags are accepted; # only works with the o format.
265 * - Width and precision may be specified with any format except c.
266 * - An * may be given for the width or precision.
267 * - The obsolete practice of preceding the width with a zero to get
268 * zero padding is not supported; use the precision field.
269 * - A % may be printed by writing %% in the format string.
270 */
271
272 #define TEMPSIZE 32
273
274 #ifdef BSD4_4
275 #define HAVE_VASPRINTF 1
276 #endif
277
278 void
doformat(struct output * dest,const char * f,va_list ap)279 doformat(struct output *dest, const char *f, va_list ap)
280 {
281 #ifdef HAVE_VASPRINTF
282 char *s;
283
284 vasprintf(&s, f, ap);
285 outstr(s, dest);
286 free(s);
287 #else /* !HAVE_VASPRINTF */
288 static const char digit_lower[] = "0123456789abcdef";
289 static const char digit_upper[] = "0123456789ABCDEF";
290 const char *digit;
291 char c;
292 char temp[TEMPSIZE];
293 int flushleft;
294 int sharp;
295 int width;
296 int prec;
297 int islong;
298 int isquad;
299 char *p;
300 int sign;
301 int64_t l;
302 uint64_t num;
303 unsigned base;
304 int len;
305 int size;
306 int pad;
307
308 while ((c = *f++) != '\0') {
309 if (c != '%') {
310 outc(c, dest);
311 continue;
312 }
313 flushleft = 0;
314 sharp = 0;
315 width = 0;
316 prec = -1;
317 islong = 0;
318 isquad = 0;
319 for (;;) {
320 if (*f == '-')
321 flushleft++;
322 else if (*f == '#')
323 sharp++;
324 else
325 break;
326 f++;
327 }
328 if (*f == '*') {
329 width = va_arg(ap, int);
330 f++;
331 } else {
332 while (is_digit(*f)) {
333 width = 10 * width + digit_val(*f++);
334 }
335 }
336 if (*f == '.') {
337 if (*++f == '*') {
338 prec = va_arg(ap, int);
339 f++;
340 } else {
341 prec = 0;
342 while (is_digit(*f)) {
343 prec = 10 * prec + digit_val(*f++);
344 }
345 }
346 }
347 if (*f == 'l') {
348 f++;
349 if (*f == 'l') {
350 isquad++;
351 f++;
352 } else
353 islong++;
354 } else if (*f == 'q') {
355 isquad++;
356 f++;
357 }
358 digit = digit_upper;
359 switch (*f) {
360 case 'd':
361 if (isquad)
362 l = va_arg(ap, int64_t);
363 else if (islong)
364 l = va_arg(ap, long);
365 else
366 l = va_arg(ap, int);
367 sign = 0;
368 num = l;
369 if (l < 0) {
370 num = -l;
371 sign = 1;
372 }
373 base = 10;
374 goto number;
375 case 'u':
376 base = 10;
377 goto uns_number;
378 case 'o':
379 base = 8;
380 goto uns_number;
381 case 'p':
382 outc('0', dest);
383 outc('x', dest);
384 /*FALLTHROUGH*/
385 case 'x':
386 /* we don't implement 'x'; treat like 'X' */
387 digit = digit_lower;
388 case 'X':
389 base = 16;
390 uns_number: /* an unsigned number */
391 sign = 0;
392 if (isquad)
393 num = va_arg(ap, uint64_t);
394 else if (islong)
395 num = va_arg(ap, unsigned long);
396 else
397 num = va_arg(ap, unsigned int);
398 number: /* process a number */
399 p = temp + TEMPSIZE - 1;
400 *p = '\0';
401 while (num) {
402 *--p = digit[num % base];
403 num /= base;
404 }
405 len = (int)((temp + TEMPSIZE - 1) - p);
406 if (prec < 0)
407 prec = 1;
408 if (sharp && *f == 'o' && prec <= len)
409 prec = len + 1;
410 pad = 0;
411 if (width) {
412 size = len;
413 if (size < prec)
414 size = prec;
415 size += sign;
416 pad = width - size;
417 if (flushleft == 0) {
418 while (--pad >= 0)
419 outc(' ', dest);
420 }
421 }
422 if (sign)
423 outc('-', dest);
424 prec -= len;
425 while (--prec >= 0)
426 outc('0', dest);
427 while (*p)
428 outc(*p++, dest);
429 while (--pad >= 0)
430 outc(' ', dest);
431 break;
432 case 's':
433 p = va_arg(ap, char *);
434 pad = 0;
435 if (width) {
436 len = (int)strlen(p);
437 if (prec >= 0 && len > prec)
438 len = prec;
439 pad = width - len;
440 if (flushleft == 0) {
441 while (--pad >= 0)
442 outc(' ', dest);
443 }
444 }
445 prec++;
446 while (--prec != 0 && *p)
447 outc(*p++, dest);
448 while (--pad >= 0)
449 outc(' ', dest);
450 break;
451 case 'c':
452 c = va_arg(ap, int);
453 outc(c, dest);
454 break;
455 default:
456 outc(*f, dest);
457 break;
458 }
459 f++;
460 }
461 #endif /* !HAVE_VASPRINTF */
462 }
463
464
465
466 /*
467 * Version of write which resumes after a signal is caught.
468 */
469
470 int
xwrite(shinstance * psh,int fd,char * buf,size_t nbytes)471 xwrite(shinstance *psh, int fd, char *buf, size_t nbytes)
472 {
473 int ntry;
474 long i;
475 size_t n;
476
477 n = nbytes;
478 ntry = 0;
479 for (;;) {
480 i = shfile_write(&psh->fdtab, fd, buf, n);
481 if (i > 0) {
482 if ((n -= i) <= 0)
483 return (int)nbytes;
484 buf += i;
485 ntry = 0;
486 } else if (i == 0) {
487 if (++ntry > 10)
488 return (int)(nbytes - n);
489 } else if (errno != EINTR) {
490 return -1;
491 }
492 }
493 }
494