xref: /freebsd/bin/sh/output.c (revision 8832864298b522bb2b4b3d2fdb1fa0443bedd681)
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 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 */
70 
71 static int doformat_wr(void *, const char *, int);
72 
73 struct output output = {NULL, 0, NULL, OUTBUFSIZ, 1, 0};
74 struct output errout = {NULL, 0, NULL, 256, 2, 0};
75 struct output memout = {NULL, 0, NULL, 0, MEM_OUT, 0};
76 struct output *out1 = &output;
77 struct output *out2 = &errout;
78 
79 
80 
81 #ifdef mkinit
82 
83 INCLUDE "output.h"
84 INCLUDE "memalloc.h"
85 
86 RESET {
87 	out1 = &output;
88 	out2 = &errout;
89 	if (memout.buf != NULL) {
90 		ckfree(memout.buf);
91 		memout.buf = NULL;
92 	}
93 }
94 
95 #endif
96 
97 
98 void
99 out1str(const char *p)
100 {
101 	outstr(p, out1);
102 }
103 
104 void
105 out1qstr(const char *p)
106 {
107 	outqstr(p, out1);
108 }
109 
110 void
111 out2str(const char *p)
112 {
113 	outstr(p, out2);
114 }
115 
116 void
117 out2qstr(const char *p)
118 {
119 	outqstr(p, out2);
120 }
121 
122 void
123 outstr(const char *p, struct output *file)
124 {
125 	while (*p)
126 		outc(*p++, file);
127 }
128 
129 /* Like outstr(), but quote for re-input into the shell. */
130 void
131 outqstr(const char *p, struct output *file)
132 {
133 	char ch;
134 	int inquotes;
135 
136 	if (p[0] == '\0') {
137 		outstr("''", file);
138 		return;
139 	}
140 	/* Caller will handle '=' if necessary */
141 	if (p[strcspn(p, "|&;<>()$`\\\"' \t\n*?[~#")] == '\0' ||
142 			strcmp(p, "[") == 0) {
143 		outstr(p, file);
144 		return;
145 	}
146 
147 	inquotes = 0;
148 	while ((ch = *p++) != '\0') {
149 		switch (ch) {
150 		case '\'':
151 			/* Can't quote single quotes inside single quotes. */
152 			if (inquotes)
153 				outc('\'', file);
154 			inquotes = 0;
155 			outstr("\\'", file);
156 			break;
157 		default:
158 			if (!inquotes)
159 				outc('\'', file);
160 			inquotes = 1;
161 			outc(ch, file);
162 		}
163 	}
164 	if (inquotes)
165 		outc('\'', file);
166 }
167 
168 static char out_junk[16];
169 
170 void
171 emptyoutbuf(struct output *dest)
172 {
173 	int offset;
174 
175 	if (dest->fd == BLOCK_OUT) {
176 		dest->nextc = out_junk;
177 		dest->nleft = sizeof out_junk;
178 		dest->flags |= OUTPUT_ERR;
179 	} else if (dest->buf == NULL) {
180 		INTOFF;
181 		dest->buf = ckmalloc(dest->bufsize);
182 		dest->nextc = dest->buf;
183 		dest->nleft = dest->bufsize;
184 		INTON;
185 	} else if (dest->fd == MEM_OUT) {
186 		offset = dest->bufsize;
187 		INTOFF;
188 		dest->bufsize <<= 1;
189 		dest->buf = ckrealloc(dest->buf, dest->bufsize);
190 		dest->nleft = dest->bufsize - offset;
191 		dest->nextc = dest->buf + offset;
192 		INTON;
193 	} else {
194 		flushout(dest);
195 	}
196 	dest->nleft--;
197 }
198 
199 
200 void
201 flushall(void)
202 {
203 	flushout(&output);
204 	flushout(&errout);
205 }
206 
207 
208 void
209 flushout(struct output *dest)
210 {
211 
212 	if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
213 		return;
214 	if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
215 		dest->flags |= OUTPUT_ERR;
216 	dest->nextc = dest->buf;
217 	dest->nleft = dest->bufsize;
218 }
219 
220 
221 void
222 freestdout(void)
223 {
224 	INTOFF;
225 	if (output.buf) {
226 		ckfree(output.buf);
227 		output.buf = NULL;
228 		output.nleft = 0;
229 	}
230 	INTON;
231 }
232 
233 
234 void
235 outfmt(struct output *file, const char *fmt, ...)
236 {
237 	va_list ap;
238 
239 	va_start(ap, fmt);
240 	doformat(file, fmt, ap);
241 	va_end(ap);
242 }
243 
244 
245 void
246 out1fmt(const char *fmt, ...)
247 {
248 	va_list ap;
249 
250 	va_start(ap, fmt);
251 	doformat(out1, fmt, ap);
252 	va_end(ap);
253 }
254 
255 void
256 out2fmt_flush(const char *fmt, ...)
257 {
258 	va_list ap;
259 
260 	va_start(ap, fmt);
261 	doformat(out2, fmt, ap);
262 	va_end(ap);
263 	flushout(out2);
264 }
265 
266 void
267 fmtstr(char *outbuf, int length, const char *fmt, ...)
268 {
269 	va_list ap;
270 	struct output strout;
271 
272 	strout.nextc = outbuf;
273 	strout.nleft = length;
274 	strout.fd = BLOCK_OUT;
275 	strout.flags = 0;
276 	va_start(ap, fmt);
277 	doformat(&strout, fmt, ap);
278 	va_end(ap);
279 	outc('\0', &strout);
280 	if (strout.flags & OUTPUT_ERR)
281 		outbuf[length - 1] = '\0';
282 }
283 
284 static int
285 doformat_wr(void *cookie, const char *buf, int len)
286 {
287 	struct output *o;
288 	int origlen;
289 	unsigned char c;
290 
291 	o = (struct output *)cookie;
292 	origlen = len;
293 	while (len-- != 0) {
294 		c = (unsigned char)*buf++;
295 		outc(c, o);
296 	}
297 
298 	return (origlen);
299 }
300 
301 void
302 doformat(struct output *dest, const char *f, va_list ap)
303 {
304 	FILE *fp;
305 
306 	if ((fp = fwopen(dest, doformat_wr)) != NULL) {
307 		vfprintf(fp, f, ap);
308 		fclose(fp);
309 	}
310 }
311 
312 /*
313  * Version of write which resumes after a signal is caught.
314  */
315 
316 int
317 xwrite(int fd, const char *buf, int nbytes)
318 {
319 	int ntry;
320 	int i;
321 	int n;
322 
323 	n = nbytes;
324 	ntry = 0;
325 	for (;;) {
326 		i = write(fd, buf, n);
327 		if (i > 0) {
328 			if ((n -= i) <= 0)
329 				return nbytes;
330 			buf += i;
331 			ntry = 0;
332 		} else if (i == 0) {
333 			if (++ntry > 10)
334 				return nbytes - n;
335 		} else if (errno != EINTR) {
336 			return -1;
337 		}
338 	}
339 }
340