]>
git.saurik.com Git - apple/shell_cmds.git/blob - sh/output.c
39b722fdba234adc583429ca248644f9971e5d60
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
35 static char sccsid
[] = "@(#)output.c 8.2 (Berkeley) 5/4/95";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * Shell output routines. We use our own output routines because:
43 * When a builtin command is interrupted we have to discard
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.
51 #include <stdio.h> /* defines BUFSIZ */
68 #define OUTBUFSIZ BUFSIZ
69 #define MEM_OUT -2 /* output to dynamically allocated memory */
70 #define OUTPUT_ERR 01 /* error occurred on output */
72 static int doformat_wr(void *, const char *, int);
74 struct output output
= {NULL
, 0, NULL
, OUTBUFSIZ
, 1, 0};
75 struct output errout
= {NULL
, 0, NULL
, 256, 2, 0};
76 struct output memout
= {NULL
, 0, NULL
, 0, MEM_OUT
, 0};
77 struct output
*out1
= &output
;
78 struct output
*out2
= &errout
;
81 outcslow(int c
, struct output
*file
)
87 out1str(const char *p
)
93 out1qstr(const char *p
)
99 out2str(const char *p
)
105 out2qstr(const char *p
)
111 outstr(const char *p
, struct output
*file
)
113 outbin(p
, strlen(p
), file
);
117 byteseq(int ch
, struct output
*file
)
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
);
129 outdqstr(const char *p
, struct output
*file
)
136 memset(&mbs
, '\0', sizeof(mbs
));
139 while ((clen
= mbrtowc(&wc
, p
, end
- p
+ 1, &mbs
)) != 0) {
140 if (clen
== (size_t)-2) {
145 if (clen
== (size_t)-1) {
146 memset(&mbs
, '\0', sizeof(mbs
));
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
--)
160 if (wc
== L
'\'' || wc
== L
'\\')
161 outcslow('\\', file
);
162 outbin(p
, clen
, file
);
166 outcslow('\'', file
);
169 /* Like outstr(), but quote for re-input into the shell. */
171 outqstr(const char *p
, struct output
*file
)
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
] == '\'') {
187 if (p
[strcspn(p
, "|&;<>()$`\\\" \n*?[~#=")] == '\0' ||
188 strcmp(p
, "[") == 0) {
193 outcslow('\'', file
);
195 outcslow('\'', file
);
199 outbin(const void *data
, size_t len
, struct output
*file
)
209 emptyoutbuf(struct output
*dest
)
213 if (dest
->buf
== NULL
) {
215 dest
->buf
= ckmalloc(dest
->bufsize
);
216 dest
->nextc
= dest
->buf
;
217 dest
->nleft
= dest
->bufsize
;
219 } else if (dest
->fd
== MEM_OUT
) {
220 offset
= dest
->bufsize
;
223 dest
->buf
= ckrealloc(dest
->buf
, dest
->bufsize
);
224 dest
->nleft
= dest
->bufsize
- offset
;
225 dest
->nextc
= dest
->buf
+ offset
;
243 flushout(struct output
*dest
)
246 if (dest
->buf
== NULL
|| dest
->nextc
== dest
->buf
|| dest
->fd
< 0)
248 if (xwrite(dest
->fd
, dest
->buf
, dest
->nextc
- dest
->buf
) < 0)
249 dest
->flags
|= OUTPUT_ERR
;
250 dest
->nextc
= dest
->buf
;
251 dest
->nleft
= dest
->bufsize
;
269 outiserror(struct output
*file
)
271 return (file
->flags
& OUTPUT_ERR
);
276 outclearerror(struct output
*file
)
278 file
->flags
&= ~OUTPUT_ERR
;
283 outfmt(struct output
*file
, const char *fmt
, ...)
288 doformat(file
, fmt
, ap
);
294 out1fmt(const char *fmt
, ...)
299 doformat(out1
, fmt
, ap
);
304 out2fmt_flush(const char *fmt
, ...)
309 doformat(out2
, fmt
, ap
);
315 fmtstr(char *outbuf
, int length
, const char *fmt
, ...)
321 vsnprintf(outbuf
, length
, fmt
, ap
);
327 doformat_wr(void *cookie
, const char *buf
, int len
)
331 o
= (struct output
*)cookie
;
338 doformat(struct output
*dest
, const char *f
, va_list ap
)
342 if ((fp
= fwopen(dest
, doformat_wr
)) != NULL
) {
349 * Version of write which resumes after a signal is caught.
353 xwrite(int fd
, const char *buf
, int nbytes
)
362 i
= write(fd
, buf
, n
);
371 } else if (errno
!= EINTR
) {