HAMMER VFS - Major retooling of the refcount mechanics, and fix a deadlock
[dragonfly.git] / bin / sh / output.c
blobcc1d728bc3f24b8b33ac09587f4bf5d3ee12c111
1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 * @(#)output.c 8.2 (Berkeley) 5/4/95
37 * $FreeBSD: src/bin/sh/output.c,v 1.20 2005/12/08 21:00:39 stefanf Exp $
38 * $DragonFly: src/bin/sh/output.c,v 1.4 2007/01/14 05:12:40 pavalos Exp $
42 * Shell output routines. We use our own output routines because:
43 * When a builtin command is interrupted we have to discard
44 * any pending output.
45 * When a builtin command appears in back quotes, we want to
46 * save the output of the command in a region obtained
47 * via malloc, rather than doing a fork and reading the
48 * output of the command via a pipe.
51 #include <stdio.h> /* defines BUFSIZ */
52 #include <string.h>
53 #include <stdarg.h>
54 #include <errno.h>
55 #include <unistd.h>
56 #include <stdlib.h>
58 #include "shell.h"
59 #include "syntax.h"
60 #include "output.h"
61 #include "memalloc.h"
62 #include "error.h"
63 #include "var.h"
66 #define OUTBUFSIZ BUFSIZ
67 #define BLOCK_OUT -2 /* output to a fixed block of memory */
68 #define MEM_OUT -3 /* output to dynamically allocated memory */
69 #define OUTPUT_ERR 01 /* error occurred on output */
71 static int doformat_wr(void *, const char *, int);
73 struct output output = {NULL, 0, NULL, OUTBUFSIZ, 1, 0};
74 struct output errout = {NULL, 0, NULL, 100, 2, 0};
75 struct output memout = {NULL, 0, NULL, 0, MEM_OUT, 0};
76 struct output *out1 = &output;
77 struct output *out2 = &errout;
81 #ifdef mkinit
83 INCLUDE "output.h"
84 INCLUDE "memalloc.h"
86 RESET {
87 out1 = &output;
88 out2 = &errout;
89 if (memout.buf != NULL) {
90 ckfree(memout.buf);
91 memout.buf = NULL;
95 #endif
98 void
99 out1str(const char *p)
101 outstr(p, out1);
104 void
105 out1qstr(const char *p)
107 outqstr(p, out1);
110 void
111 out2str(const char *p)
113 outstr(p, out2);
116 void
117 out2qstr(const char *p)
119 outqstr(p, out2);
122 void
123 outstr(const char *p, struct output *file)
125 while (*p)
126 outc(*p++, file);
127 if (file == out2)
128 flushout(file);
131 /* Like outstr(), but quote for re-input into the shell. */
132 void
133 outqstr(const char *p, struct output *file)
135 char ch;
137 if (p[0] == '\0') {
138 outstr("''", file);
139 return;
141 if (p[strcspn(p, "|&;<>()$`\\\"'")] == '\0' && (!ifsset() ||
142 p[strcspn(p, ifsval())] == '\0')) {
143 outstr(p, file);
144 return;
147 out1c('\'');
148 while ((ch = *p++) != '\0') {
149 switch (ch) {
150 case '\'':
152 * Can't quote single quotes inside single quotes;
153 * close them, write escaped single quote, open again.
155 outstr("'\\''", file);
156 break;
157 default:
158 outc(ch, file);
161 out1c('\'');
164 STATIC char out_junk[16];
166 void
167 emptyoutbuf(struct output *dest)
169 int offset;
171 if (dest->fd == BLOCK_OUT) {
172 dest->nextc = out_junk;
173 dest->nleft = sizeof out_junk;
174 dest->flags |= OUTPUT_ERR;
175 } else if (dest->buf == NULL) {
176 INTOFF;
177 dest->buf = ckmalloc(dest->bufsize);
178 dest->nextc = dest->buf;
179 dest->nleft = dest->bufsize;
180 INTON;
181 } else if (dest->fd == MEM_OUT) {
182 offset = dest->bufsize;
183 INTOFF;
184 dest->bufsize <<= 1;
185 dest->buf = ckrealloc(dest->buf, dest->bufsize);
186 dest->nleft = dest->bufsize - offset;
187 dest->nextc = dest->buf + offset;
188 INTON;
189 } else {
190 flushout(dest);
192 dest->nleft--;
196 void
197 flushall(void)
199 flushout(&output);
200 flushout(&errout);
204 void
205 flushout(struct output *dest)
208 if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
209 return;
210 if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
211 dest->flags |= OUTPUT_ERR;
212 dest->nextc = dest->buf;
213 dest->nleft = dest->bufsize;
217 void
218 freestdout(void)
220 INTOFF;
221 if (output.buf) {
222 ckfree(output.buf);
223 output.buf = NULL;
224 output.nleft = 0;
226 INTON;
230 void
231 outfmt(struct output *file, const char *fmt, ...)
233 va_list ap;
235 va_start(ap, fmt);
236 doformat(file, fmt, ap);
237 va_end(ap);
241 void
242 out1fmt(const char *fmt, ...)
244 va_list ap;
246 va_start(ap, fmt);
247 doformat(out1, fmt, ap);
248 va_end(ap);
251 void
252 dprintf(const char *fmt, ...)
254 va_list ap;
256 va_start(ap, fmt);
257 doformat(out2, fmt, ap);
258 va_end(ap);
259 flushout(out2);
262 void
263 fmtstr(char *outbuf, int length, const char *fmt, ...)
265 va_list ap;
266 struct output strout;
268 strout.nextc = outbuf;
269 strout.nleft = length;
270 strout.fd = BLOCK_OUT;
271 strout.flags = 0;
272 va_start(ap, fmt);
273 doformat(&strout, fmt, ap);
274 va_end(ap);
275 outc('\0', &strout);
276 if (strout.flags & OUTPUT_ERR)
277 outbuf[length - 1] = '\0';
280 static int
281 doformat_wr(void *cookie, const char *buf, int len)
283 struct output *o;
284 int origlen;
285 unsigned char c;
287 o = (struct output *)cookie;
288 origlen = len;
289 while (len-- != 0) {
290 c = (unsigned char)*buf++;
291 outc(c, o);
294 return (origlen);
297 void
298 doformat(struct output *dest, const char *f, va_list ap)
300 FILE *fp;
302 if ((fp = fwopen(dest, doformat_wr)) != NULL) {
303 vfprintf(fp, f, ap);
304 fclose(fp);
309 * Version of write which resumes after a signal is caught.
313 xwrite(int fd, char *buf, int nbytes)
315 int ntry;
316 int i;
317 int n;
319 n = nbytes;
320 ntry = 0;
321 for (;;) {
322 i = write(fd, buf, n);
323 if (i > 0) {
324 if ((n -= i) <= 0)
325 return nbytes;
326 buf += i;
327 ntry = 0;
328 } else if (i == 0) {
329 if (++ntry > 10)
330 return nbytes - n;
331 } else if (errno != EINTR) {
332 return -1;