VirtualBox

source: kBuild/trunk/src/ash/output.c@ 1158

Last change on this file since 1158 was 809, checked in by bird, 18 years ago

Solaris + cleanup.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette