VirtualBox

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

Last change on this file since 3438 was 3438, checked in by bird, 4 years ago

kash: Hammering on threaded mode.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 10.1 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, 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 file->psh = psh;
112}
113#endif
114
115
116void
117out1str(shinstance *psh, const char *p)
118{
119 outstr(p, psh->out1);
120}
121
122
123void
124out2str(shinstance *psh, const char *p)
125{
126 outstr(p, psh->out2);
127}
128
129
130void
131outstr(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
140char out_junk[16];
141
142
143void
144emptyoutbuf(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
174void
175output_flushall(shinstance *psh)
176{
177 flushout(&psh->output);
178 flushout(&psh->errout);
179}
180
181
182void
183flushout(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
195void
196freestdout(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
208void
209outfmt(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
219void
220out1fmt(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
229void
230dprintf(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
240void
241fmtstr(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
278void
279doformat(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#ifdef _MSC_VER /* for SHPID_PRI / KI64_PRI */
359 else if (*f == 'I' && f[1] == '6' && f[2] == '4') {
360 isquad++;
361 f += 3;
362 }
363#endif
364 digit = digit_upper;
365 switch (*f) {
366 case 'd':
367 if (isquad)
368 l = va_arg(ap, int64_t);
369 else if (islong)
370 l = va_arg(ap, long);
371 else
372 l = va_arg(ap, int);
373 sign = 0;
374 num = l;
375 if (l < 0) {
376 num = -l;
377 sign = 1;
378 }
379 base = 10;
380 goto number;
381 case 'u':
382 base = 10;
383 goto uns_number;
384 case 'o':
385 base = 8;
386 goto uns_number;
387 case 'p':
388 outc('0', dest);
389 outc('x', dest);
390 /*FALLTHROUGH*/
391 case 'x':
392 /* we don't implement 'x'; treat like 'X' */
393 digit = digit_lower;
394 case 'X':
395 base = 16;
396uns_number: /* an unsigned number */
397 sign = 0;
398 if (isquad)
399 num = va_arg(ap, uint64_t);
400 else if (islong)
401 num = va_arg(ap, unsigned long);
402 else
403 num = va_arg(ap, unsigned int);
404number: /* process a number */
405 p = temp + TEMPSIZE - 1;
406 *p = '\0';
407 while (num) {
408 *--p = digit[num % base];
409 num /= base;
410 }
411 len = (int)((temp + TEMPSIZE - 1) - p);
412 if (prec < 0)
413 prec = 1;
414 if (sharp && *f == 'o' && prec <= len)
415 prec = len + 1;
416 pad = 0;
417 if (width) {
418 size = len;
419 if (size < prec)
420 size = prec;
421 size += sign;
422 pad = width - size;
423 if (flushleft == 0) {
424 while (--pad >= 0)
425 outc(' ', dest);
426 }
427 }
428 if (sign)
429 outc('-', dest);
430 prec -= len;
431 while (--prec >= 0)
432 outc('0', dest);
433 while (*p)
434 outc(*p++, dest);
435 while (--pad >= 0)
436 outc(' ', dest);
437 break;
438 case 's':
439 p = va_arg(ap, char *);
440 pad = 0;
441 if (width) {
442 len = (int)strlen(p);
443 if (prec >= 0 && len > prec)
444 len = prec;
445 pad = width - len;
446 if (flushleft == 0) {
447 while (--pad >= 0)
448 outc(' ', dest);
449 }
450 }
451 prec++;
452 while (--prec != 0 && *p)
453 outc(*p++, dest);
454 while (--pad >= 0)
455 outc(' ', dest);
456 break;
457 case 'c':
458 c = va_arg(ap, int);
459 outc(c, dest);
460 break;
461 default:
462 outc(*f, dest);
463 break;
464 }
465 f++;
466 }
467#endif /* !HAVE_VASPRINTF */
468}
469
470
471
472/*
473 * Version of write which resumes after a signal is caught.
474 */
475
476int
477xwrite(shinstance *psh, int fd, char *buf, size_t nbytes)
478{
479 int ntry;
480 long i;
481 size_t n;
482
483 n = nbytes;
484 ntry = 0;
485 for (;;) {
486 i = shfile_write(&psh->fdtab, fd, buf, n);
487 if (i > 0) {
488 if ((n -= i) <= 0)
489 return (int)nbytes;
490 buf += i;
491 ntry = 0;
492 } else if (i == 0) {
493 if (++ntry > 10)
494 return (int)(nbytes - n);
495 } else if (errno != EINTR) {
496 return -1;
497 }
498 }
499}
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