VirtualBox

source: kBuild/trunk/src/kash/output.c@ 1214

Last change on this file since 1214 was 1214, checked in by bird, 17 years ago

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