xref: /freebsd/bin/sh/output.c (revision d184218c)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Kenneth Almquist.
7  *
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  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)output.c	8.2 (Berkeley) 5/4/95";
36 #endif
37 #endif /* not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 /*
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.
49  */
50 
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>
57 
58 #include "shell.h"
59 #include "syntax.h"
60 #include "output.h"
61 #include "memalloc.h"
62 #include "error.h"
63 #include "var.h"
64 
65 
66 #define OUTBUFSIZ BUFSIZ
67 #define MEM_OUT -2		/* output to dynamically allocated memory */
68 #define OUTPUT_ERR 01		/* error occurred on output */
69 
70 static int doformat_wr(void *, const char *, int);
71 
72 struct output output = {NULL, 0, NULL, OUTBUFSIZ, 1, 0};
73 struct output errout = {NULL, 0, NULL, 256, 2, 0};
74 struct output memout = {NULL, 0, NULL, 0, MEM_OUT, 0};
75 struct output *out1 = &output;
76 struct output *out2 = &errout;
77 
78 
79 
80 #ifdef mkinit
81 
82 INCLUDE "output.h"
83 INCLUDE "memalloc.h"
84 
85 RESET {
86 	out1 = &output;
87 	out2 = &errout;
88 	if (memout.buf != NULL) {
89 		ckfree(memout.buf);
90 		memout.buf = NULL;
91 	}
92 }
93 
94 #endif
95 
96 
97 void
98 outcslow(int c, struct output *file)
99 {
100 	outc(c, file);
101 }
102 
103 void
104 out1str(const char *p)
105 {
106 	outstr(p, out1);
107 }
108 
109 void
110 out1qstr(const char *p)
111 {
112 	outqstr(p, out1);
113 }
114 
115 void
116 out2str(const char *p)
117 {
118 	outstr(p, out2);
119 }
120 
121 void
122 out2qstr(const char *p)
123 {
124 	outqstr(p, out2);
125 }
126 
127 void
128 outstr(const char *p, struct output *file)
129 {
130 	outbin(p, strlen(p), file);
131 }
132 
133 /* Like outstr(), but quote for re-input into the shell. */
134 void
135 outqstr(const char *p, struct output *file)
136 {
137 	char ch;
138 	int inquotes;
139 
140 	if (p[0] == '\0') {
141 		outstr("''", file);
142 		return;
143 	}
144 	/* Caller will handle '=' if necessary */
145 	if (p[strcspn(p, "|&;<>()$`\\\"' \t\n*?[~#")] == '\0' ||
146 			strcmp(p, "[") == 0) {
147 		outstr(p, file);
148 		return;
149 	}
150 
151 	inquotes = 0;
152 	while ((ch = *p++) != '\0') {
153 		switch (ch) {
154 		case '\'':
155 			/* Can't quote single quotes inside single quotes. */
156 			if (inquotes)
157 				outcslow('\'', file);
158 			inquotes = 0;
159 			outstr("\\'", file);
160 			break;
161 		default:
162 			if (!inquotes)
163 				outcslow('\'', file);
164 			inquotes = 1;
165 			outc(ch, file);
166 		}
167 	}
168 	if (inquotes)
169 		outcslow('\'', file);
170 }
171 
172 void
173 outbin(const void *data, size_t len, struct output *file)
174 {
175 	const char *p;
176 
177 	p = data;
178 	while (len-- > 0)
179 		outc(*p++, file);
180 }
181 
182 void
183 emptyoutbuf(struct output *dest)
184 {
185 	int offset;
186 
187 	if (dest->buf == NULL) {
188 		INTOFF;
189 		dest->buf = ckmalloc(dest->bufsize);
190 		dest->nextc = dest->buf;
191 		dest->nleft = dest->bufsize;
192 		INTON;
193 	} else if (dest->fd == MEM_OUT) {
194 		offset = dest->bufsize;
195 		INTOFF;
196 		dest->bufsize <<= 1;
197 		dest->buf = ckrealloc(dest->buf, dest->bufsize);
198 		dest->nleft = dest->bufsize - offset;
199 		dest->nextc = dest->buf + offset;
200 		INTON;
201 	} else {
202 		flushout(dest);
203 	}
204 	dest->nleft--;
205 }
206 
207 
208 void
209 flushall(void)
210 {
211 	flushout(&output);
212 	flushout(&errout);
213 }
214 
215 
216 void
217 flushout(struct output *dest)
218 {
219 
220 	if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
221 		return;
222 	if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
223 		dest->flags |= OUTPUT_ERR;
224 	dest->nextc = dest->buf;
225 	dest->nleft = dest->bufsize;
226 }
227 
228 
229 void
230 freestdout(void)
231 {
232 	INTOFF;
233 	if (output.buf) {
234 		ckfree(output.buf);
235 		output.buf = NULL;
236 		output.nleft = 0;
237 	}
238 	INTON;
239 }
240 
241 
242 int
243 outiserror(struct output *file)
244 {
245 	return (file->flags & OUTPUT_ERR);
246 }
247 
248 
249 void
250 outclearerror(struct output *file)
251 {
252 	file->flags &= ~OUTPUT_ERR;
253 }
254 
255 
256 void
257 outfmt(struct output *file, const char *fmt, ...)
258 {
259 	va_list ap;
260 
261 	va_start(ap, fmt);
262 	doformat(file, fmt, ap);
263 	va_end(ap);
264 }
265 
266 
267 void
268 out1fmt(const char *fmt, ...)
269 {
270 	va_list ap;
271 
272 	va_start(ap, fmt);
273 	doformat(out1, fmt, ap);
274 	va_end(ap);
275 }
276 
277 void
278 out2fmt_flush(const char *fmt, ...)
279 {
280 	va_list ap;
281 
282 	va_start(ap, fmt);
283 	doformat(out2, fmt, ap);
284 	va_end(ap);
285 	flushout(out2);
286 }
287 
288 void
289 fmtstr(char *outbuf, int length, const char *fmt, ...)
290 {
291 	va_list ap;
292 
293 	INTOFF;
294 	va_start(ap, fmt);
295 	vsnprintf(outbuf, length, fmt, ap);
296 	va_end(ap);
297 	INTON;
298 }
299 
300 static int
301 doformat_wr(void *cookie, const char *buf, int len)
302 {
303 	struct output *o;
304 
305 	o = (struct output *)cookie;
306 	outbin(buf, len, o);
307 
308 	return (len);
309 }
310 
311 void
312 doformat(struct output *dest, const char *f, va_list ap)
313 {
314 	FILE *fp;
315 
316 	if ((fp = fwopen(dest, doformat_wr)) != NULL) {
317 		vfprintf(fp, f, ap);
318 		fclose(fp);
319 	}
320 }
321 
322 /*
323  * Version of write which resumes after a signal is caught.
324  */
325 
326 int
327 xwrite(int fd, const char *buf, int nbytes)
328 {
329 	int ntry;
330 	int i;
331 	int n;
332 
333 	n = nbytes;
334 	ntry = 0;
335 	for (;;) {
336 		i = write(fd, buf, n);
337 		if (i > 0) {
338 			if ((n -= i) <= 0)
339 				return nbytes;
340 			buf += i;
341 			ntry = 0;
342 		} else if (i == 0) {
343 			if (++ntry > 10)
344 				return nbytes - n;
345 		} else if (errno != EINTR) {
346 			return -1;
347 		}
348 	}
349 }
350