VirtualBox

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

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

hacking...

  • 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 strout.psh = NULL;
256 doformat(&strout, fmt, ap);
257 outc('\0', &strout);
258 if (strout.flags & OUTPUT_ERR)
259 outbuf[length - 1] = '\0';
260 va_end(ap);
261}
262
263/*
264 * Formatted output. This routine handles a subset of the printf formats:
265 * - Formats supported: d, u, o, p, X, s, and c.
266 * - The x format is also accepted but is treated like X.
267 * - The l, ll and q modifiers are accepted.
268 * - The - and # flags are accepted; # only works with the o format.
269 * - Width and precision may be specified with any format except c.
270 * - An * may be given for the width or precision.
271 * - The obsolete practice of preceding the width with a zero to get
272 * zero padding is not supported; use the precision field.
273 * - A % may be printed by writing %% in the format string.
274 */
275
276#define TEMPSIZE 24
277
278#ifdef BSD4_4
279#define HAVE_VASPRINTF 1
280#endif
281
282void
283doformat(struct output *dest, const char *f, va_list ap)
284{
285#if HAVE_VASPRINTF
286 char *s;
287
288 vasprintf(&s, f, ap);
289 outstr(s, dest);
290 free(s);
291#else /* !HAVE_VASPRINTF */
292 static const char digit[] = "0123456789ABCDEF";
293 char c;
294 char temp[TEMPSIZE];
295 int flushleft;
296 int sharp;
297 int width;
298 int prec;
299 int islong;
300 int isquad;
301 char *p;
302 int sign;
303#ifdef BSD4_4
304 quad_t l;
305 u_quad_t num;
306#else
307 long l;
308 u_long num;
309#endif
310 unsigned base;
311 int len;
312 int size;
313 int pad;
314
315 while ((c = *f++) != '\0') {
316 if (c != '%') {
317 outc(c, dest);
318 continue;
319 }
320 flushleft = 0;
321 sharp = 0;
322 width = 0;
323 prec = -1;
324 islong = 0;
325 isquad = 0;
326 for (;;) {
327 if (*f == '-')
328 flushleft++;
329 else if (*f == '#')
330 sharp++;
331 else
332 break;
333 f++;
334 }
335 if (*f == '*') {
336 width = va_arg(ap, int);
337 f++;
338 } else {
339 while (is_digit(*f)) {
340 width = 10 * width + digit_val(*f++);
341 }
342 }
343 if (*f == '.') {
344 if (*++f == '*') {
345 prec = va_arg(ap, int);
346 f++;
347 } else {
348 prec = 0;
349 while (is_digit(*f)) {
350 prec = 10 * prec + digit_val(*f++);
351 }
352 }
353 }
354 if (*f == 'l') {
355 f++;
356 if (*f == 'l') {
357 isquad++;
358 f++;
359 } else
360 islong++;
361 } else if (*f == 'q') {
362 isquad++;
363 f++;
364 }
365 switch (*f) {
366 case 'd':
367#ifdef BSD4_4
368 if (isquad)
369 l = va_arg(ap, quad_t);
370 else
371#endif
372 if (islong)
373 l = va_arg(ap, long);
374 else
375 l = va_arg(ap, int);
376 sign = 0;
377 num = l;
378 if (l < 0) {
379 num = -l;
380 sign = 1;
381 }
382 base = 10;
383 goto number;
384 case 'u':
385 base = 10;
386 goto uns_number;
387 case 'o':
388 base = 8;
389 goto uns_number;
390 case 'p':
391 outc('0', dest);
392 outc('x', dest);
393 /*FALLTHROUGH*/
394 case 'x':
395 /* we don't implement 'x'; treat like 'X' */
396 case 'X':
397 base = 16;
398uns_number: /* an unsigned number */
399 sign = 0;
400#ifdef BSD4_4
401 if (isquad)
402 num = va_arg(ap, u_quad_t);
403 else
404#endif
405 if (islong)
406 num = va_arg(ap, unsigned long);
407 else
408 num = va_arg(ap, unsigned int);
409number: /* process a number */
410 p = temp + TEMPSIZE - 1;
411 *p = '\0';
412 while (num) {
413 *--p = digit[num % base];
414 num /= base;
415 }
416 len = (temp + TEMPSIZE - 1) - p;
417 if (prec < 0)
418 prec = 1;
419 if (sharp && *f == 'o' && prec <= len)
420 prec = len + 1;
421 pad = 0;
422 if (width) {
423 size = len;
424 if (size < prec)
425 size = prec;
426 size += sign;
427 pad = width - size;
428 if (flushleft == 0) {
429 while (--pad >= 0)
430 outc(' ', dest);
431 }
432 }
433 if (sign)
434 outc('-', dest);
435 prec -= len;
436 while (--prec >= 0)
437 outc('0', dest);
438 while (*p)
439 outc(*p++, dest);
440 while (--pad >= 0)
441 outc(' ', dest);
442 break;
443 case 's':
444 p = va_arg(ap, char *);
445 pad = 0;
446 if (width) {
447 len = strlen(p);
448 if (prec >= 0 && len > prec)
449 len = prec;
450 pad = width - len;
451 if (flushleft == 0) {
452 while (--pad >= 0)
453 outc(' ', dest);
454 }
455 }
456 prec++;
457 while (--prec != 0 && *p)
458 outc(*p++, dest);
459 while (--pad >= 0)
460 outc(' ', dest);
461 break;
462 case 'c':
463 c = va_arg(ap, int);
464 outc(c, dest);
465 break;
466 default:
467 outc(*f, dest);
468 break;
469 }
470 f++;
471 }
472#endif /* !HAVE_VASPRINTF */
473}
474
475
476
477/*
478 * Version of write which resumes after a signal is caught.
479 */
480
481int
482xwrite(int fd, char *buf, int nbytes)
483{
484 int ntry;
485 int i;
486 int n;
487
488 n = nbytes;
489 ntry = 0;
490 for (;;) {
491 i = write(fd, buf, n);
492 if (i > 0) {
493 if ((n -= i) <= 0)
494 return nbytes;
495 buf += i;
496 ntry = 0;
497 } else if (i == 0) {
498 if (++ntry > 10)
499 return nbytes - n;
500 } else if (errno != EINTR) {
501 return -1;
502 }
503 }
504}
505
506
507/*
508 * Version of ioctl that retries after a signal is caught.
509 * XXX unused function
510 */
511
512int
513xioctl(int fd, unsigned long request, char *arg)
514{
515 int i;
516
517 while ((i = ioctl(fd, request, arg)) == -1 && errno == EINTR);
518 return i;
519}
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