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