VirtualBox

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

Last change on this file was 3573, checked in by bird, 2 years ago

kash: addressed some pedantic warnings

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