xref: /freebsd/bin/sh/output.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 #ifndef lint
36 #if 0
37 static char sccsid[] = "@(#)output.c	8.2 (Berkeley) 5/4/95";
38 #endif
39 #endif /* not lint */
40 #include <sys/cdefs.h>
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 #include <wchar.h>
58 #include <wctype.h>
59 
60 #include "shell.h"
61 #include "syntax.h"
62 #include "output.h"
63 #include "memalloc.h"
64 #include "error.h"
65 #include "var.h"
66 
67 
68 #define OUTBUFSIZ BUFSIZ
69 #define MEM_OUT -2		/* output to dynamically allocated memory */
70 #define OUTPUT_ERR 01		/* error occurred on output */
71 
72 static int doformat_wr(void *, const char *, int);
73 
74 struct output output = {NULL, NULL, NULL, OUTBUFSIZ, 1, 0};
75 struct output errout = {NULL, NULL, NULL, 256, 2, 0};
76 struct output memout = {NULL, NULL, NULL, 64, MEM_OUT, 0};
77 struct output *out1 = &output;
78 struct output *out2 = &errout;
79 
80 void
81 outcslow(int c, struct output *file)
82 {
83 	outc(c, file);
84 }
85 
86 void
87 out1str(const char *p)
88 {
89 	outstr(p, out1);
90 }
91 
92 void
93 out1qstr(const char *p)
94 {
95 	outqstr(p, out1);
96 }
97 
98 void
99 out2str(const char *p)
100 {
101 	outstr(p, out2);
102 }
103 
104 void
105 out2qstr(const char *p)
106 {
107 	outqstr(p, out2);
108 }
109 
110 void
111 outstr(const char *p, struct output *file)
112 {
113 	outbin(p, strlen(p), file);
114 }
115 
116 static void
117 byteseq(int ch, struct output *file)
118 {
119 	char seq[4];
120 
121 	seq[0] = '\\';
122 	seq[1] = (ch >> 6 & 0x3) + '0';
123 	seq[2] = (ch >> 3 & 0x7) + '0';
124 	seq[3] = (ch & 0x7) + '0';
125 	outbin(seq, 4, file);
126 }
127 
128 static void
129 outdqstr(const char *p, struct output *file)
130 {
131 	const char *end;
132 	mbstate_t mbs;
133 	size_t clen;
134 	wchar_t wc;
135 
136 	memset(&mbs, '\0', sizeof(mbs));
137 	end = p + strlen(p);
138 	outstr("$'", file);
139 	while ((clen = mbrtowc(&wc, p, end - p + 1, &mbs)) != 0) {
140 		if (clen == (size_t)-2) {
141 			while (p < end)
142 				byteseq(*p++, file);
143 			break;
144 		}
145 		if (clen == (size_t)-1) {
146 			memset(&mbs, '\0', sizeof(mbs));
147 			byteseq(*p++, file);
148 			continue;
149 		}
150 		if (wc == L'\n')
151 			outcslow('\n', file), p++;
152 		else if (wc == L'\r')
153 			outstr("\\r", file), p++;
154 		else if (wc == L'\t')
155 			outstr("\\t", file), p++;
156 		else if (!iswprint(wc)) {
157 			for (; clen > 0; clen--)
158 				byteseq(*p++, file);
159 		} else {
160 			if (wc == L'\'' || wc == L'\\')
161 				outcslow('\\', file);
162 			outbin(p, clen, file);
163 			p += clen;
164 		}
165 	}
166 	outcslow('\'', file);
167 }
168 
169 /* Like outstr(), but quote for re-input into the shell. */
170 void
171 outqstr(const char *p, struct output *file)
172 {
173 	int i;
174 
175 	if (p[0] == '\0') {
176 		outstr("''", file);
177 		return;
178 	}
179 	for (i = 0; p[i] != '\0'; i++) {
180 		if ((p[i] > '\0' && p[i] < ' ' && p[i] != '\n') ||
181 		    (p[i] & 0x80) != 0 || p[i] == '\'') {
182 			outdqstr(p, file);
183 			return;
184 		}
185 	}
186 
187 	if (p[strcspn(p, "|&;<>()$`\\\" \n*?[~#=")] == '\0' ||
188 			strcmp(p, "[") == 0) {
189 		outstr(p, file);
190 		return;
191 	}
192 
193 	outcslow('\'', file);
194 	outstr(p, file);
195 	outcslow('\'', file);
196 }
197 
198 void
199 outbin(const void *data, size_t len, struct output *file)
200 {
201 	const char *p;
202 
203 	p = data;
204 	while (len-- > 0)
205 		outc(*p++, file);
206 }
207 
208 void
209 emptyoutbuf(struct output *dest)
210 {
211 	int offset, newsize;
212 
213 	if (dest->buf == NULL) {
214 		INTOFF;
215 		dest->buf = ckmalloc(dest->bufsize);
216 		dest->nextc = dest->buf;
217 		dest->bufend = dest->buf + dest->bufsize;
218 		INTON;
219 	} else if (dest->fd == MEM_OUT) {
220 		offset = dest->nextc - dest->buf;
221 		newsize = dest->bufsize << 1;
222 		INTOFF;
223 		dest->buf = ckrealloc(dest->buf, newsize);
224 		dest->bufsize = newsize;
225 		dest->bufend = dest->buf + newsize;
226 		dest->nextc = dest->buf + offset;
227 		INTON;
228 	} else {
229 		flushout(dest);
230 	}
231 }
232 
233 
234 void
235 flushall(void)
236 {
237 	flushout(&output);
238 	flushout(&errout);
239 }
240 
241 
242 void
243 flushout(struct output *dest)
244 {
245 
246 	if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
247 		return;
248 	if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
249 		dest->flags |= OUTPUT_ERR;
250 	dest->nextc = dest->buf;
251 }
252 
253 
254 void
255 freestdout(void)
256 {
257 	output.nextc = output.buf;
258 }
259 
260 
261 int
262 outiserror(struct output *file)
263 {
264 	return (file->flags & OUTPUT_ERR);
265 }
266 
267 
268 void
269 outclearerror(struct output *file)
270 {
271 	file->flags &= ~OUTPUT_ERR;
272 }
273 
274 
275 void
276 outfmt(struct output *file, const char *fmt, ...)
277 {
278 	va_list ap;
279 
280 	va_start(ap, fmt);
281 	doformat(file, fmt, ap);
282 	va_end(ap);
283 }
284 
285 
286 void
287 out1fmt(const char *fmt, ...)
288 {
289 	va_list ap;
290 
291 	va_start(ap, fmt);
292 	doformat(out1, fmt, ap);
293 	va_end(ap);
294 }
295 
296 void
297 out2fmt_flush(const char *fmt, ...)
298 {
299 	va_list ap;
300 
301 	va_start(ap, fmt);
302 	doformat(out2, fmt, ap);
303 	va_end(ap);
304 	flushout(out2);
305 }
306 
307 void
308 fmtstr(char *outbuf, int length, const char *fmt, ...)
309 {
310 	va_list ap;
311 
312 	INTOFF;
313 	va_start(ap, fmt);
314 	vsnprintf(outbuf, length, fmt, ap);
315 	va_end(ap);
316 	INTON;
317 }
318 
319 static int
320 doformat_wr(void *cookie, const char *buf, int len)
321 {
322 	struct output *o;
323 
324 	o = (struct output *)cookie;
325 	outbin(buf, len, o);
326 
327 	return (len);
328 }
329 
330 void
331 doformat(struct output *dest, const char *f, va_list ap)
332 {
333 	FILE *fp;
334 
335 	if ((fp = fwopen(dest, doformat_wr)) != NULL) {
336 		vfprintf(fp, f, ap);
337 		fclose(fp);
338 	}
339 }
340 
341 FILE *
342 out1fp(void)
343 {
344 	return fwopen(out1, doformat_wr);
345 }
346 
347 /*
348  * Version of write which resumes after a signal is caught.
349  */
350 
351 int
352 xwrite(int fd, const char *buf, int nbytes)
353 {
354 	int ntry;
355 	int i;
356 	int n;
357 
358 	n = nbytes;
359 	ntry = 0;
360 	for (;;) {
361 		i = write(fd, buf, n);
362 		if (i > 0) {
363 			if ((n -= i) <= 0)
364 				return nbytes;
365 			buf += i;
366 			ntry = 0;
367 		} else if (i == 0) {
368 			if (++ntry > 10)
369 				return nbytes - n;
370 		} else if (errno != EINTR) {
371 			return -1;
372 		}
373 	}
374 }
375