xref: /dragonfly/bin/sh/output.c (revision 8f2ce533)
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 __FBSDID("$FreeBSD: head/bin/sh/output.c 344306 2019-02-19 21:27:30Z jilles $");
42 
43 /*
44  * Shell output routines.  We use our own output routines because:
45  *	When a builtin command is interrupted we have to discard
46  *		any pending output.
47  *	When a builtin command appears in back quotes, we want to
48  *		save the output of the command in a region obtained
49  *		via malloc, rather than doing a fork and reading the
50  *		output of the command via a pipe.
51  */
52 
53 #include <stdio.h>	/* defines BUFSIZ */
54 #include <string.h>
55 #include <stdarg.h>
56 #include <errno.h>
57 #include <unistd.h>
58 #include <stdlib.h>
59 #include <poll.h>
60 #include <wchar.h>
61 #include <wctype.h>
62 
63 #include "shell.h"
64 #include "syntax.h"
65 #include "output.h"
66 #include "memalloc.h"
67 #include "error.h"
68 #include "var.h"
69 
70 
71 #define OUTBUFSIZ BUFSIZ
72 #define MEM_OUT -2		/* output to dynamically allocated memory */
73 #define OUTPUT_ERR 01		/* error occurred on output */
74 
75 static int doformat_wr(void *, const char *, int);
76 
77 struct output output = {NULL, NULL, NULL, OUTBUFSIZ, 1, 0};
78 struct output errout = {NULL, NULL, NULL, 256, 2, 0};
79 struct output memout = {NULL, NULL, NULL, 64, MEM_OUT, 0};
80 struct output *out1 = &output;
81 struct output *out2 = &errout;
82 
83 void
outcslow(int c,struct output * file)84 outcslow(int c, struct output *file)
85 {
86 	outc(c, file);
87 }
88 
89 void
out1str(const char * p)90 out1str(const char *p)
91 {
92 	outstr(p, out1);
93 }
94 
95 void
out1qstr(const char * p)96 out1qstr(const char *p)
97 {
98 	outqstr(p, out1);
99 }
100 
101 void
out2str(const char * p)102 out2str(const char *p)
103 {
104 	outstr(p, out2);
105 }
106 
107 void
out2qstr(const char * p)108 out2qstr(const char *p)
109 {
110 	outqstr(p, out2);
111 }
112 
113 void
outstr(const char * p,struct output * file)114 outstr(const char *p, struct output *file)
115 {
116 	outbin(p, strlen(p), file);
117 }
118 
119 static void
byteseq(int ch,struct output * file)120 byteseq(int ch, struct output *file)
121 {
122 	char seq[4];
123 
124 	seq[0] = '\\';
125 	seq[1] = (ch >> 6 & 0x3) + '0';
126 	seq[2] = (ch >> 3 & 0x7) + '0';
127 	seq[3] = (ch & 0x7) + '0';
128 	outbin(seq, 4, file);
129 }
130 
131 static void
outdqstr(const char * p,struct output * file)132 outdqstr(const char *p, struct output *file)
133 {
134 	const char *end;
135 	mbstate_t mbs;
136 	size_t clen;
137 	wchar_t wc;
138 
139 	memset(&mbs, '\0', sizeof(mbs));
140 	end = p + strlen(p);
141 	outstr("$'", file);
142 	while ((clen = mbrtowc(&wc, p, end - p + 1, &mbs)) != 0) {
143 		if (clen == (size_t)-2) {
144 			while (p < end)
145 				byteseq(*p++, file);
146 			break;
147 		}
148 		if (clen == (size_t)-1) {
149 			memset(&mbs, '\0', sizeof(mbs));
150 			byteseq(*p++, file);
151 			continue;
152 		}
153 		if (wc == L'\n')
154 			outcslow('\n', file), p++;
155 		else if (wc == L'\r')
156 			outstr("\\r", file), p++;
157 		else if (wc == L'\t')
158 			outstr("\\t", file), p++;
159 		else if (!iswprint(wc)) {
160 			for (; clen > 0; clen--)
161 				byteseq(*p++, file);
162 		} else {
163 			if (wc == L'\'' || wc == L'\\')
164 				outcslow('\\', file);
165 			outbin(p, clen, file);
166 			p += clen;
167 		}
168 	}
169 	outcslow('\'', file);
170 }
171 
172 /* Like outstr(), but quote for re-input into the shell. */
173 void
outqstr(const char * p,struct output * file)174 outqstr(const char *p, struct output *file)
175 {
176 	int i;
177 
178 	if (p[0] == '\0') {
179 		outstr("''", file);
180 		return;
181 	}
182 	for (i = 0; p[i] != '\0'; i++) {
183 		if ((p[i] > '\0' && p[i] < ' ' && p[i] != '\n') ||
184 		    (p[i] & 0x80) != 0 || p[i] == '\'') {
185 			outdqstr(p, file);
186 			return;
187 		}
188 	}
189 
190 	if (p[strcspn(p, "|&;<>()$`\\\" \n*?[~#=")] == '\0' ||
191 			strcmp(p, "[") == 0) {
192 		outstr(p, file);
193 		return;
194 	}
195 
196 	outcslow('\'', file);
197 	outstr(p, file);
198 	outcslow('\'', file);
199 }
200 
201 void
outbin(const void * data,size_t len,struct output * file)202 outbin(const void *data, size_t len, struct output *file)
203 {
204 	const char *p;
205 
206 	p = data;
207 	while (len-- > 0)
208 		outc(*p++, file);
209 }
210 
211 void
emptyoutbuf(struct output * dest)212 emptyoutbuf(struct output *dest)
213 {
214 	int offset, newsize;
215 
216 	if (dest->buf == NULL) {
217 		INTOFF;
218 		dest->buf = ckmalloc(dest->bufsize);
219 		dest->nextc = dest->buf;
220 		dest->bufend = dest->buf + dest->bufsize;
221 		INTON;
222 	} else if (dest->fd == MEM_OUT) {
223 		offset = dest->nextc - dest->buf;
224 		newsize = dest->bufsize << 1;
225 		INTOFF;
226 		dest->buf = ckrealloc(dest->buf, newsize);
227 		dest->bufsize = newsize;
228 		dest->bufend = dest->buf + newsize;
229 		dest->nextc = dest->buf + offset;
230 		INTON;
231 	} else {
232 		flushout(dest);
233 	}
234 }
235 
236 
237 void
flushall(void)238 flushall(void)
239 {
240 	flushout(&output);
241 	flushout(&errout);
242 }
243 
244 
245 void
flushout(struct output * dest)246 flushout(struct output *dest)
247 {
248 
249 	if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
250 		return;
251 	if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0) {
252 		dest->flags |= OUTPUT_ERR;
253 		dest->error = errno;
254 	}
255 	dest->nextc = dest->buf;
256 }
257 
258 
259 void
freestdout(void)260 freestdout(void)
261 {
262 	output.nextc = output.buf;
263 }
264 
265 
266 int
outiserror(struct output * file)267 outiserror(struct output *file)
268 {
269 	return (file->flags & OUTPUT_ERR);
270 }
271 
272 
273 void
outclearerror(struct output * file)274 outclearerror(struct output *file)
275 {
276 	file->flags &= ~OUTPUT_ERR;
277 }
278 
279 
280 void
outfmt(struct output * file,const char * fmt,...)281 outfmt(struct output *file, const char *fmt, ...)
282 {
283 	va_list ap;
284 
285 	va_start(ap, fmt);
286 	doformat(file, fmt, ap);
287 	va_end(ap);
288 }
289 
290 
291 void
out1fmt(const char * fmt,...)292 out1fmt(const char *fmt, ...)
293 {
294 	va_list ap;
295 
296 	va_start(ap, fmt);
297 	doformat(out1, fmt, ap);
298 	va_end(ap);
299 }
300 
301 void
out2fmt_flush(const char * fmt,...)302 out2fmt_flush(const char *fmt, ...)
303 {
304 	va_list ap;
305 
306 	va_start(ap, fmt);
307 	doformat(out2, fmt, ap);
308 	va_end(ap);
309 	flushout(out2);
310 }
311 
312 void
fmtstr(char * outbuf,int length,const char * fmt,...)313 fmtstr(char *outbuf, int length, const char *fmt, ...)
314 {
315 	va_list ap;
316 
317 	INTOFF;
318 	va_start(ap, fmt);
319 	vsnprintf(outbuf, length, fmt, ap);
320 	va_end(ap);
321 	INTON;
322 }
323 
324 static int
doformat_wr(void * cookie,const char * buf,int len)325 doformat_wr(void *cookie, const char *buf, int len)
326 {
327 	struct output *o;
328 
329 	o = (struct output *)cookie;
330 	outbin(buf, len, o);
331 
332 	return (len);
333 }
334 
335 void
doformat(struct output * dest,const char * f,va_list ap)336 doformat(struct output *dest, const char *f, va_list ap)
337 {
338 	FILE *fp;
339 
340 	if ((fp = fwopen(dest, doformat_wr)) != NULL) {
341 		vfprintf(fp, f, ap);
342 		fclose(fp);
343 	}
344 }
345 
346 FILE *
out1fp(void)347 out1fp(void)
348 {
349 	return fwopen(out1, doformat_wr);
350 }
351 
352 /*
353  * Version of write which resumes after a signal is caught.
354  */
355 
356 int
xwrite(int fd,const char * buf,int nbytes)357 xwrite(int fd, const char *buf, int nbytes)
358 {
359 	int ntry;
360 	int i;
361 	int n;
362 
363 	n = nbytes;
364 	ntry = 0;
365 	for (;;) {
366 		i = write(fd, buf, n);
367 		if (i > 0) {
368 			if ((n -= i) <= 0)
369 				return nbytes;
370 			buf += i;
371 			ntry = 0;
372 		} else if (i == 0) {
373 			if (++ntry > 10)
374 				return nbytes - n;
375 		} else {
376 			struct pollfd pfd;
377 
378 			switch(errno) {
379 			case EINTR:
380 				/* retry */
381 				break;
382 			case EAGAIN:
383 				/*
384 				 * If stdout is non-blocking, use poll
385 				 * to wait, then retry.
386 				 */
387 				pfd.fd = fd;
388 				pfd.events = POLLOUT;
389 				pfd.revents = 0;
390 				poll(&pfd, 1, -1);
391 				if (pfd.revents & (POLLERR | POLLNVAL))
392 					return -1;
393 				/* retry */
394 				break;
395 			default:
396 				return -1;
397 				/* not reached */
398 			}
399 		}
400 	}
401 }
402