output.c revision 1.39 1 1.39 kre /* $NetBSD: output.c,v 1.39 2017/11/19 03:23:01 kre Exp $ */
2 1.13 cgd
3 1.1 cgd /*-
4 1.7 jtc * Copyright (c) 1991, 1993
5 1.7 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Kenneth Almquist.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.28 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.19 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.13 cgd #if 0
38 1.14 christos static char sccsid[] = "@(#)output.c 8.2 (Berkeley) 5/4/95";
39 1.13 cgd #else
40 1.39 kre __RCSID("$NetBSD: output.c,v 1.39 2017/11/19 03:23:01 kre Exp $");
41 1.13 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd /*
45 1.1 cgd * Shell output routines. We use our own output routines because:
46 1.1 cgd * When a builtin command is interrupted we have to discard
47 1.1 cgd * any pending output.
48 1.1 cgd * When a builtin command appears in back quotes, we want to
49 1.1 cgd * save the output of the command in a region obtained
50 1.1 cgd * via malloc, rather than doing a fork and reading the
51 1.1 cgd * output of the command via a pipe.
52 1.1 cgd * Our output routines may be smaller than the stdio routines.
53 1.1 cgd */
54 1.1 cgd
55 1.20 christos #include <sys/types.h> /* quad_t */
56 1.20 christos #include <sys/param.h> /* BSD4_4 */
57 1.11 cgd #include <sys/ioctl.h>
58 1.11 cgd
59 1.1 cgd #include <stdio.h> /* defines BUFSIZ */
60 1.12 cgd #include <string.h>
61 1.1 cgd #include <errno.h>
62 1.9 jtc #include <unistd.h>
63 1.14 christos #include <stdlib.h>
64 1.14 christos
65 1.14 christos #include "shell.h"
66 1.14 christos #include "syntax.h"
67 1.14 christos #include "output.h"
68 1.14 christos #include "memalloc.h"
69 1.14 christos #include "error.h"
70 1.39 kre #include "redir.h"
71 1.39 kre #include "options.h"
72 1.39 kre #include "show.h"
73 1.1 cgd
74 1.1 cgd
75 1.1 cgd #define OUTBUFSIZ BUFSIZ
76 1.1 cgd #define BLOCK_OUT -2 /* output to a fixed block of memory */
77 1.1 cgd #define MEM_OUT -3 /* output to dynamically allocated memory */
78 1.1 cgd
79 1.1 cgd
80 1.39 kre /* nextc nleft bufsize buf fd flags chain */
81 1.39 kre struct output output = {NULL, 0, OUTBUFSIZ, NULL, 1, 0, NULL };
82 1.39 kre struct output errout = {NULL, 0, 100, NULL, 2, 0, NULL };
83 1.39 kre struct output memout = {NULL, 0, 0, NULL, MEM_OUT, 0, NULL };
84 1.1 cgd struct output *out1 = &output;
85 1.1 cgd struct output *out2 = &errout;
86 1.39 kre struct output *outx = &errout;
87 1.39 kre struct output *outxtop = NULL;
88 1.1 cgd
89 1.1 cgd
90 1.1 cgd #ifdef mkinit
91 1.1 cgd
92 1.1 cgd INCLUDE "output.h"
93 1.1 cgd INCLUDE "memalloc.h"
94 1.1 cgd
95 1.1 cgd RESET {
96 1.1 cgd out1 = &output;
97 1.1 cgd out2 = &errout;
98 1.1 cgd if (memout.buf != NULL) {
99 1.1 cgd ckfree(memout.buf);
100 1.1 cgd memout.buf = NULL;
101 1.1 cgd }
102 1.1 cgd }
103 1.1 cgd
104 1.1 cgd #endif
105 1.1 cgd
106 1.1 cgd
107 1.1 cgd #ifdef notdef /* no longer used */
108 1.1 cgd /*
109 1.1 cgd * Set up an output file to write to memory rather than a file.
110 1.1 cgd */
111 1.1 cgd
112 1.1 cgd void
113 1.27 christos open_mem(char *block, int length, struct output *file)
114 1.27 christos {
115 1.1 cgd file->nextc = block;
116 1.1 cgd file->nleft = --length;
117 1.1 cgd file->fd = BLOCK_OUT;
118 1.1 cgd file->flags = 0;
119 1.1 cgd }
120 1.1 cgd #endif
121 1.1 cgd
122 1.1 cgd
123 1.1 cgd void
124 1.27 christos out1str(const char *p)
125 1.27 christos {
126 1.1 cgd outstr(p, out1);
127 1.1 cgd }
128 1.1 cgd
129 1.1 cgd
130 1.1 cgd void
131 1.27 christos out2str(const char *p)
132 1.27 christos {
133 1.1 cgd outstr(p, out2);
134 1.1 cgd }
135 1.1 cgd
136 1.39 kre void
137 1.39 kre outxstr(const char *p)
138 1.39 kre {
139 1.39 kre outstr(p, outx);
140 1.39 kre }
141 1.39 kre
142 1.1 cgd
143 1.1 cgd void
144 1.27 christos outstr(const char *p, struct output *file)
145 1.27 christos {
146 1.39 kre char c = 0;
147 1.39 kre
148 1.1 cgd while (*p)
149 1.39 kre outc((c = *p++), file);
150 1.39 kre if (file == out2 || (file == outx && c == '\n'))
151 1.7 jtc flushout(file);
152 1.1 cgd }
153 1.1 cgd
154 1.1 cgd
155 1.31 christos void
156 1.31 christos out2shstr(const char *p)
157 1.31 christos {
158 1.31 christos outshstr(p, out2);
159 1.31 christos }
160 1.31 christos
161 1.39 kre void
162 1.39 kre outxshstr(const char *p)
163 1.39 kre {
164 1.39 kre outshstr(p, outx);
165 1.39 kre }
166 1.31 christos
167 1.37 kre /*
168 1.37 kre * ' is in this list, not because it does not require quoting
169 1.37 kre * (which applies to all the others) but because '' quoting cannot
170 1.37 kre * be used to quote it.
171 1.37 kre */
172 1.35 christos static const char norm_chars [] = \
173 1.36 kre "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/+-=_,.'";
174 1.35 christos
175 1.35 christos static int
176 1.35 christos inquote(const char *p)
177 1.35 christos {
178 1.35 christos size_t l = strspn(p, norm_chars);
179 1.35 christos char *s = strchr(p, '\'');
180 1.35 christos
181 1.35 christos return s == NULL ? p[l] != '\0' : s - p > (off_t)l;
182 1.35 christos }
183 1.35 christos
184 1.31 christos void
185 1.31 christos outshstr(const char *p, struct output *file)
186 1.31 christos {
187 1.37 kre int need_q;
188 1.35 christos int inq;
189 1.31 christos char c;
190 1.31 christos
191 1.37 kre if (strchr(p, '\'') != NULL && p[1] != '\0') {
192 1.37 kre /*
193 1.37 kre * string contains ' in it, and is not only the '
194 1.37 kre * see if " quoting will work
195 1.37 kre */
196 1.37 kre size_t i = strcspn(p, "\\\"$`");
197 1.37 kre
198 1.37 kre if (p[i] == '\0') {
199 1.37 kre /*
200 1.37 kre * string contains no $ ` \ or " chars, perfect...
201 1.37 kre *
202 1.37 kre * We know it contains ' so needs quoting, so
203 1.37 kre * this is easy...
204 1.37 kre */
205 1.37 kre outc('"', file);
206 1.37 kre outstr(p, file);
207 1.37 kre outc('"', file);
208 1.37 kre return;
209 1.37 kre }
210 1.37 kre }
211 1.37 kre
212 1.37 kre need_q = p[0] == 0 || p[strspn(p, norm_chars)] != 0;
213 1.37 kre
214 1.35 christos /*
215 1.35 christos * Don't emit ' unless something needs quoting before closing '
216 1.35 christos */
217 1.37 kre if (need_q && (p[0] == 0 || inquote(p) != 0)) {
218 1.37 kre outc('\'', file);
219 1.37 kre inq = 1;
220 1.35 christos } else
221 1.35 christos inq = 0;
222 1.31 christos
223 1.35 christos while ((c = *p++) != '\0') {
224 1.35 christos if (c != '\'') {
225 1.31 christos outc(c, file);
226 1.35 christos continue;
227 1.31 christos }
228 1.35 christos
229 1.35 christos if (inq)
230 1.35 christos outc('\'', file); /* inq = 0, implicit */
231 1.35 christos outc('\\', file);
232 1.35 christos outc(c, file);
233 1.35 christos if (need_q && *p != '\0') {
234 1.35 christos if ((inq = inquote(p)) != 0)
235 1.35 christos outc('\'', file);
236 1.35 christos } else
237 1.35 christos inq = 0;
238 1.31 christos }
239 1.31 christos
240 1.35 christos if (inq)
241 1.31 christos outc('\'', file);
242 1.31 christos
243 1.31 christos if (file == out2)
244 1.31 christos flushout(file);
245 1.31 christos }
246 1.31 christos
247 1.31 christos
248 1.1 cgd char out_junk[16];
249 1.1 cgd
250 1.1 cgd
251 1.1 cgd void
252 1.27 christos emptyoutbuf(struct output *dest)
253 1.27 christos {
254 1.1 cgd int offset;
255 1.1 cgd
256 1.1 cgd if (dest->fd == BLOCK_OUT) {
257 1.1 cgd dest->nextc = out_junk;
258 1.1 cgd dest->nleft = sizeof out_junk;
259 1.1 cgd dest->flags |= OUTPUT_ERR;
260 1.1 cgd } else if (dest->buf == NULL) {
261 1.1 cgd INTOFF;
262 1.1 cgd dest->buf = ckmalloc(dest->bufsize);
263 1.1 cgd dest->nextc = dest->buf;
264 1.1 cgd dest->nleft = dest->bufsize;
265 1.1 cgd INTON;
266 1.39 kre VTRACE(DBG_OUTPUT, ("emptyoutbuf now %d @%p for fd %d\n",
267 1.39 kre dest->nleft, dest->buf, dest->fd));
268 1.1 cgd } else if (dest->fd == MEM_OUT) {
269 1.1 cgd offset = dest->bufsize;
270 1.1 cgd INTOFF;
271 1.1 cgd dest->bufsize <<= 1;
272 1.1 cgd dest->buf = ckrealloc(dest->buf, dest->bufsize);
273 1.1 cgd dest->nleft = dest->bufsize - offset;
274 1.1 cgd dest->nextc = dest->buf + offset;
275 1.1 cgd INTON;
276 1.1 cgd } else {
277 1.1 cgd flushout(dest);
278 1.1 cgd }
279 1.1 cgd dest->nleft--;
280 1.1 cgd }
281 1.1 cgd
282 1.1 cgd
283 1.1 cgd void
284 1.27 christos flushall(void)
285 1.27 christos {
286 1.1 cgd flushout(&output);
287 1.1 cgd flushout(&errout);
288 1.1 cgd }
289 1.1 cgd
290 1.1 cgd
291 1.1 cgd void
292 1.27 christos flushout(struct output *dest)
293 1.27 christos {
294 1.1 cgd
295 1.1 cgd if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
296 1.1 cgd return;
297 1.39 kre VTRACE(DBG_OUTPUT, ("flushout fd=%d %zd to write\n", dest->fd,
298 1.39 kre (size_t)(dest->nextc - dest->buf)));
299 1.1 cgd if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
300 1.1 cgd dest->flags |= OUTPUT_ERR;
301 1.1 cgd dest->nextc = dest->buf;
302 1.1 cgd dest->nleft = dest->bufsize;
303 1.1 cgd }
304 1.1 cgd
305 1.1 cgd
306 1.1 cgd void
307 1.27 christos freestdout(void)
308 1.27 christos {
309 1.1 cgd INTOFF;
310 1.1 cgd if (output.buf) {
311 1.1 cgd ckfree(output.buf);
312 1.1 cgd output.buf = NULL;
313 1.1 cgd output.nleft = 0;
314 1.1 cgd }
315 1.1 cgd INTON;
316 1.1 cgd }
317 1.1 cgd
318 1.1 cgd
319 1.21 christos void
320 1.21 christos outfmt(struct output *file, const char *fmt, ...)
321 1.21 christos {
322 1.1 cgd va_list ap;
323 1.1 cgd
324 1.21 christos va_start(ap, fmt);
325 1.1 cgd doformat(file, fmt, ap);
326 1.1 cgd va_end(ap);
327 1.1 cgd }
328 1.1 cgd
329 1.1 cgd
330 1.1 cgd void
331 1.21 christos out1fmt(const char *fmt, ...)
332 1.21 christos {
333 1.1 cgd va_list ap;
334 1.1 cgd
335 1.21 christos va_start(ap, fmt);
336 1.1 cgd doformat(out1, fmt, ap);
337 1.1 cgd va_end(ap);
338 1.1 cgd }
339 1.1 cgd
340 1.33 christos #ifdef DEBUG
341 1.7 jtc void
342 1.33 christos debugprintf(const char *fmt, ...)
343 1.21 christos {
344 1.7 jtc va_list ap;
345 1.7 jtc
346 1.21 christos va_start(ap, fmt);
347 1.7 jtc doformat(out2, fmt, ap);
348 1.7 jtc va_end(ap);
349 1.7 jtc flushout(out2);
350 1.7 jtc }
351 1.33 christos #endif
352 1.1 cgd
353 1.1 cgd void
354 1.21 christos fmtstr(char *outbuf, size_t length, const char *fmt, ...)
355 1.21 christos {
356 1.1 cgd va_list ap;
357 1.1 cgd struct output strout;
358 1.26 wiz
359 1.21 christos va_start(ap, fmt);
360 1.1 cgd strout.nextc = outbuf;
361 1.1 cgd strout.nleft = length;
362 1.1 cgd strout.fd = BLOCK_OUT;
363 1.1 cgd strout.flags = 0;
364 1.1 cgd doformat(&strout, fmt, ap);
365 1.1 cgd outc('\0', &strout);
366 1.1 cgd if (strout.flags & OUTPUT_ERR)
367 1.1 cgd outbuf[length - 1] = '\0';
368 1.24 wiz va_end(ap);
369 1.1 cgd }
370 1.1 cgd
371 1.1 cgd /*
372 1.1 cgd * Formatted output. This routine handles a subset of the printf formats:
373 1.21 christos * - Formats supported: d, u, o, p, X, s, and c.
374 1.1 cgd * - The x format is also accepted but is treated like X.
375 1.22 lukem * - The l, ll and q modifiers are accepted.
376 1.1 cgd * - The - and # flags are accepted; # only works with the o format.
377 1.1 cgd * - Width and precision may be specified with any format except c.
378 1.1 cgd * - An * may be given for the width or precision.
379 1.1 cgd * - The obsolete practice of preceding the width with a zero to get
380 1.1 cgd * zero padding is not supported; use the precision field.
381 1.1 cgd * - A % may be printed by writing %% in the format string.
382 1.1 cgd */
383 1.1 cgd
384 1.1 cgd #define TEMPSIZE 24
385 1.1 cgd
386 1.23 lukem #ifdef BSD4_4
387 1.23 lukem #define HAVE_VASPRINTF 1
388 1.23 lukem #endif
389 1.23 lukem
390 1.1 cgd void
391 1.27 christos doformat(struct output *dest, const char *f, va_list ap)
392 1.23 lukem {
393 1.23 lukem #if HAVE_VASPRINTF
394 1.23 lukem char *s;
395 1.23 lukem
396 1.23 lukem vasprintf(&s, f, ap);
397 1.29 rumble if (s == NULL)
398 1.29 rumble error("Could not allocate formatted output buffer");
399 1.23 lukem outstr(s, dest);
400 1.23 lukem free(s);
401 1.23 lukem #else /* !HAVE_VASPRINTF */
402 1.25 thorpej static const char digit[] = "0123456789ABCDEF";
403 1.17 tls char c;
404 1.1 cgd char temp[TEMPSIZE];
405 1.1 cgd int flushleft;
406 1.1 cgd int sharp;
407 1.1 cgd int width;
408 1.1 cgd int prec;
409 1.1 cgd int islong;
410 1.15 jtc int isquad;
411 1.1 cgd char *p;
412 1.1 cgd int sign;
413 1.18 christos #ifdef BSD4_4
414 1.15 jtc quad_t l;
415 1.15 jtc u_quad_t num;
416 1.18 christos #else
417 1.18 christos long l;
418 1.18 christos u_long num;
419 1.18 christos #endif
420 1.1 cgd unsigned base;
421 1.1 cgd int len;
422 1.1 cgd int size;
423 1.1 cgd int pad;
424 1.1 cgd
425 1.1 cgd while ((c = *f++) != '\0') {
426 1.1 cgd if (c != '%') {
427 1.1 cgd outc(c, dest);
428 1.1 cgd continue;
429 1.1 cgd }
430 1.1 cgd flushleft = 0;
431 1.1 cgd sharp = 0;
432 1.1 cgd width = 0;
433 1.1 cgd prec = -1;
434 1.1 cgd islong = 0;
435 1.15 jtc isquad = 0;
436 1.1 cgd for (;;) {
437 1.1 cgd if (*f == '-')
438 1.1 cgd flushleft++;
439 1.1 cgd else if (*f == '#')
440 1.1 cgd sharp++;
441 1.1 cgd else
442 1.1 cgd break;
443 1.1 cgd f++;
444 1.1 cgd }
445 1.1 cgd if (*f == '*') {
446 1.1 cgd width = va_arg(ap, int);
447 1.1 cgd f++;
448 1.1 cgd } else {
449 1.1 cgd while (is_digit(*f)) {
450 1.1 cgd width = 10 * width + digit_val(*f++);
451 1.1 cgd }
452 1.1 cgd }
453 1.1 cgd if (*f == '.') {
454 1.1 cgd if (*++f == '*') {
455 1.1 cgd prec = va_arg(ap, int);
456 1.1 cgd f++;
457 1.1 cgd } else {
458 1.1 cgd prec = 0;
459 1.1 cgd while (is_digit(*f)) {
460 1.1 cgd prec = 10 * prec + digit_val(*f++);
461 1.1 cgd }
462 1.1 cgd }
463 1.1 cgd }
464 1.1 cgd if (*f == 'l') {
465 1.1 cgd f++;
466 1.22 lukem if (*f == 'l') {
467 1.22 lukem isquad++;
468 1.22 lukem f++;
469 1.22 lukem } else
470 1.22 lukem islong++;
471 1.15 jtc } else if (*f == 'q') {
472 1.15 jtc isquad++;
473 1.15 jtc f++;
474 1.1 cgd }
475 1.1 cgd switch (*f) {
476 1.1 cgd case 'd':
477 1.18 christos #ifdef BSD4_4
478 1.15 jtc if (isquad)
479 1.15 jtc l = va_arg(ap, quad_t);
480 1.18 christos else
481 1.18 christos #endif
482 1.18 christos if (islong)
483 1.1 cgd l = va_arg(ap, long);
484 1.1 cgd else
485 1.1 cgd l = va_arg(ap, int);
486 1.1 cgd sign = 0;
487 1.1 cgd num = l;
488 1.1 cgd if (l < 0) {
489 1.1 cgd num = -l;
490 1.1 cgd sign = 1;
491 1.1 cgd }
492 1.1 cgd base = 10;
493 1.1 cgd goto number;
494 1.1 cgd case 'u':
495 1.1 cgd base = 10;
496 1.1 cgd goto uns_number;
497 1.1 cgd case 'o':
498 1.1 cgd base = 8;
499 1.1 cgd goto uns_number;
500 1.21 christos case 'p':
501 1.21 christos outc('0', dest);
502 1.21 christos outc('x', dest);
503 1.21 christos /*FALLTHROUGH*/
504 1.1 cgd case 'x':
505 1.1 cgd /* we don't implement 'x'; treat like 'X' */
506 1.1 cgd case 'X':
507 1.1 cgd base = 16;
508 1.1 cgd uns_number: /* an unsigned number */
509 1.1 cgd sign = 0;
510 1.18 christos #ifdef BSD4_4
511 1.15 jtc if (isquad)
512 1.15 jtc num = va_arg(ap, u_quad_t);
513 1.18 christos else
514 1.18 christos #endif
515 1.18 christos if (islong)
516 1.1 cgd num = va_arg(ap, unsigned long);
517 1.1 cgd else
518 1.1 cgd num = va_arg(ap, unsigned int);
519 1.1 cgd number: /* process a number */
520 1.1 cgd p = temp + TEMPSIZE - 1;
521 1.1 cgd *p = '\0';
522 1.1 cgd while (num) {
523 1.1 cgd *--p = digit[num % base];
524 1.1 cgd num /= base;
525 1.1 cgd }
526 1.1 cgd len = (temp + TEMPSIZE - 1) - p;
527 1.1 cgd if (prec < 0)
528 1.1 cgd prec = 1;
529 1.1 cgd if (sharp && *f == 'o' && prec <= len)
530 1.1 cgd prec = len + 1;
531 1.1 cgd pad = 0;
532 1.1 cgd if (width) {
533 1.1 cgd size = len;
534 1.1 cgd if (size < prec)
535 1.1 cgd size = prec;
536 1.1 cgd size += sign;
537 1.1 cgd pad = width - size;
538 1.1 cgd if (flushleft == 0) {
539 1.1 cgd while (--pad >= 0)
540 1.1 cgd outc(' ', dest);
541 1.1 cgd }
542 1.1 cgd }
543 1.1 cgd if (sign)
544 1.1 cgd outc('-', dest);
545 1.1 cgd prec -= len;
546 1.1 cgd while (--prec >= 0)
547 1.1 cgd outc('0', dest);
548 1.1 cgd while (*p)
549 1.1 cgd outc(*p++, dest);
550 1.1 cgd while (--pad >= 0)
551 1.1 cgd outc(' ', dest);
552 1.1 cgd break;
553 1.1 cgd case 's':
554 1.1 cgd p = va_arg(ap, char *);
555 1.1 cgd pad = 0;
556 1.1 cgd if (width) {
557 1.1 cgd len = strlen(p);
558 1.1 cgd if (prec >= 0 && len > prec)
559 1.1 cgd len = prec;
560 1.1 cgd pad = width - len;
561 1.1 cgd if (flushleft == 0) {
562 1.1 cgd while (--pad >= 0)
563 1.1 cgd outc(' ', dest);
564 1.1 cgd }
565 1.1 cgd }
566 1.1 cgd prec++;
567 1.1 cgd while (--prec != 0 && *p)
568 1.1 cgd outc(*p++, dest);
569 1.1 cgd while (--pad >= 0)
570 1.1 cgd outc(' ', dest);
571 1.1 cgd break;
572 1.1 cgd case 'c':
573 1.1 cgd c = va_arg(ap, int);
574 1.1 cgd outc(c, dest);
575 1.1 cgd break;
576 1.1 cgd default:
577 1.1 cgd outc(*f, dest);
578 1.1 cgd break;
579 1.1 cgd }
580 1.1 cgd f++;
581 1.1 cgd }
582 1.23 lukem #endif /* !HAVE_VASPRINTF */
583 1.1 cgd }
584 1.1 cgd
585 1.1 cgd
586 1.1 cgd
587 1.1 cgd /*
588 1.1 cgd * Version of write which resumes after a signal is caught.
589 1.1 cgd */
590 1.1 cgd
591 1.1 cgd int
592 1.27 christos xwrite(int fd, char *buf, int nbytes)
593 1.27 christos {
594 1.1 cgd int ntry;
595 1.1 cgd int i;
596 1.1 cgd int n;
597 1.1 cgd
598 1.1 cgd n = nbytes;
599 1.1 cgd ntry = 0;
600 1.34 christos while (n > 0) {
601 1.1 cgd i = write(fd, buf, n);
602 1.1 cgd if (i > 0) {
603 1.1 cgd if ((n -= i) <= 0)
604 1.1 cgd return nbytes;
605 1.1 cgd buf += i;
606 1.1 cgd ntry = 0;
607 1.1 cgd } else if (i == 0) {
608 1.1 cgd if (++ntry > 10)
609 1.1 cgd return nbytes - n;
610 1.1 cgd } else if (errno != EINTR) {
611 1.1 cgd return -1;
612 1.1 cgd }
613 1.1 cgd }
614 1.34 christos return nbytes;
615 1.1 cgd }
616 1.1 cgd
617 1.1 cgd
618 1.1 cgd /*
619 1.1 cgd * Version of ioctl that retries after a signal is caught.
620 1.11 cgd * XXX unused function
621 1.1 cgd */
622 1.1 cgd
623 1.1 cgd int
624 1.27 christos xioctl(int fd, unsigned long request, char *arg)
625 1.11 cgd {
626 1.1 cgd int i;
627 1.1 cgd
628 1.1 cgd while ((i = ioctl(fd, request, arg)) == -1 && errno == EINTR);
629 1.1 cgd return i;
630 1.1 cgd }
631 1.39 kre
632 1.39 kre static void
633 1.39 kre xtrace_fd_swap(int from, int to)
634 1.39 kre {
635 1.39 kre struct output *o = outxtop;
636 1.39 kre
637 1.39 kre while (o != NULL) {
638 1.39 kre if (o->fd == from)
639 1.39 kre o->fd = to;
640 1.39 kre o = o->chain;
641 1.39 kre }
642 1.39 kre }
643 1.39 kre
644 1.39 kre /*
645 1.39 kre * the -X flag is to be set or reset (not necessarily changed)
646 1.39 kre * Do what is needed to make tracing go to where it should
647 1.39 kre *
648 1.39 kre * Note: Xflag has not yet been altered, "on" indicates what it will become
649 1.39 kre */
650 1.39 kre
651 1.39 kre void
652 1.39 kre xtracefdsetup(int on)
653 1.39 kre {
654 1.39 kre if (!on) {
655 1.39 kre flushout(outx);
656 1.39 kre
657 1.39 kre if (Xflag != 1) /* Was already +X */
658 1.39 kre return; /* so nothing to do */
659 1.39 kre
660 1.39 kre outx = out2;
661 1.39 kre CTRACE(DBG_OUTPUT, ("Tracing to stderr\n"));
662 1.39 kre return;
663 1.39 kre }
664 1.39 kre
665 1.39 kre if (Xflag == 1) { /* was already set */
666 1.39 kre /*
667 1.39 kre * This is a change of output file only
668 1.39 kre * Just close the current one, and reuse the struct output
669 1.39 kre */
670 1.39 kre if (!(outx->flags & OUTPUT_CLONE))
671 1.39 kre sh_close(outx->fd);
672 1.39 kre } else if (outxtop == NULL) {
673 1.39 kre /*
674 1.39 kre * -X is just turning on, for the forst time,
675 1.39 kre * need a new output struct to become outx
676 1.39 kre */
677 1.39 kre xtrace_clone(1);
678 1.39 kre } else
679 1.39 kre outx = outxtop;
680 1.39 kre
681 1.39 kre if (outx != out2) {
682 1.39 kre outx->flags &= ~OUTPUT_CLONE;
683 1.39 kre outx->fd = to_upper_fd(dup(out2->fd));
684 1.39 kre register_sh_fd(outx->fd, xtrace_fd_swap);
685 1.39 kre }
686 1.39 kre
687 1.39 kre CTRACE(DBG_OUTPUT, ("Tracing now to fd %d (from %d)\n",
688 1.39 kre outx->fd, out2->fd));
689 1.39 kre }
690 1.39 kre
691 1.39 kre void
692 1.39 kre xtrace_clone(int new)
693 1.39 kre {
694 1.39 kre struct output *o;
695 1.39 kre
696 1.39 kre CTRACE(DBG_OUTPUT,
697 1.39 kre ("xtrace_clone(%d): %sfd=%d buf=%p nleft=%d flags=%x ",
698 1.39 kre new, (outx == out2 ? "out2: " : ""),
699 1.39 kre outx->fd, outx->buf, outx->nleft, outx->flags));
700 1.39 kre
701 1.39 kre flushout(outx);
702 1.39 kre
703 1.39 kre if (!new && outxtop == NULL && Xflag == 0) {
704 1.39 kre /* following stderr, nothing to save */
705 1.39 kre CTRACE(DBG_OUTPUT, ("+X\n"));
706 1.39 kre return;
707 1.39 kre }
708 1.39 kre
709 1.39 kre o = ckmalloc(sizeof(*o));
710 1.39 kre o->fd = outx->fd;
711 1.39 kre o->flags = OUTPUT_CLONE;
712 1.39 kre o->bufsize = outx->bufsize;
713 1.39 kre o->nleft = 0;
714 1.39 kre o->buf = NULL;
715 1.39 kre o->nextc = NULL;
716 1.39 kre o->chain = outxtop;
717 1.39 kre outx = o;
718 1.39 kre outxtop = o;
719 1.39 kre
720 1.39 kre CTRACE(DBG_OUTPUT, ("-> fd=%d flags=%x[CLONE]\n", outx->fd, o->flags));
721 1.39 kre }
722 1.39 kre
723 1.39 kre void
724 1.39 kre xtrace_pop(void)
725 1.39 kre {
726 1.39 kre struct output *o;
727 1.39 kre
728 1.39 kre CTRACE(DBG_OUTPUT, ("trace_pop: fd=%d buf=%p nleft=%d flags=%x ",
729 1.39 kre outx->fd, outx->buf, outx->nleft, outx->flags));
730 1.39 kre
731 1.39 kre flushout(outx);
732 1.39 kre
733 1.39 kre if (outxtop == NULL) {
734 1.39 kre /*
735 1.39 kre * No -X has been used, so nothing much to do
736 1.39 kre */
737 1.39 kre CTRACE(DBG_OUTPUT, ("+X\n"));
738 1.39 kre return;
739 1.39 kre }
740 1.39 kre
741 1.39 kre o = outxtop;
742 1.39 kre outx = o->chain;
743 1.39 kre if (outx == NULL)
744 1.39 kre outx = &errout;
745 1.39 kre outxtop = o->chain;
746 1.39 kre if (o != &errout) {
747 1.39 kre if (o->fd >= 0 && !(o->flags & OUTPUT_CLONE))
748 1.39 kre sh_close(o->fd);
749 1.39 kre if (o->buf)
750 1.39 kre ckfree(o->buf);
751 1.39 kre ckfree(o);
752 1.39 kre }
753 1.39 kre
754 1.39 kre CTRACE(DBG_OUTPUT, ("-> fd=%d buf=%p nleft=%d flags=%x\n",
755 1.39 kre outx->fd, outx->buf, outx->nleft, outx->flags));
756 1.39 kre }
757