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, 100, 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 if (file == out2) 128 flushout(file); 129 } 130 131 /* Like outstr(), but quote for re-input into the shell. */ 132 void 133 outqstr(const char *p, struct output *file) 134 { 135 char ch; 136 137 if (p[strcspn(p, "|&;<>()$`\\\"'")] == '\0' && (!ifsset() || 138 p[strcspn(p, ifsval())] == '\0')) { 139 outstr(p, file); 140 return; 141 } 142 143 out1c('\''); 144 while ((ch = *p++) != '\0') { 145 switch (ch) { 146 case '\'': 147 /* 148 * Can't quote single quotes inside single quotes; 149 * close them, write escaped single quote, open again. 150 */ 151 outstr("'\\''", file); 152 break; 153 default: 154 outc(ch, file); 155 } 156 } 157 out1c('\''); 158 } 159 160 STATIC char out_junk[16]; 161 162 void 163 emptyoutbuf(struct output *dest) 164 { 165 int offset; 166 167 if (dest->fd == BLOCK_OUT) { 168 dest->nextc = out_junk; 169 dest->nleft = sizeof out_junk; 170 dest->flags |= OUTPUT_ERR; 171 } else if (dest->buf == NULL) { 172 INTOFF; 173 dest->buf = ckmalloc(dest->bufsize); 174 dest->nextc = dest->buf; 175 dest->nleft = dest->bufsize; 176 INTON; 177 } else if (dest->fd == MEM_OUT) { 178 offset = dest->bufsize; 179 INTOFF; 180 dest->bufsize <<= 1; 181 dest->buf = ckrealloc(dest->buf, dest->bufsize); 182 dest->nleft = dest->bufsize - offset; 183 dest->nextc = dest->buf + offset; 184 INTON; 185 } else { 186 flushout(dest); 187 } 188 dest->nleft--; 189 } 190 191 192 void 193 flushall(void) 194 { 195 flushout(&output); 196 flushout(&errout); 197 } 198 199 200 void 201 flushout(struct output *dest) 202 { 203 204 if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0) 205 return; 206 if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0) 207 dest->flags |= OUTPUT_ERR; 208 dest->nextc = dest->buf; 209 dest->nleft = dest->bufsize; 210 } 211 212 213 void 214 freestdout(void) 215 { 216 INTOFF; 217 if (output.buf) { 218 ckfree(output.buf); 219 output.buf = NULL; 220 output.nleft = 0; 221 } 222 INTON; 223 } 224 225 226 void 227 outfmt(struct output *file, const char *fmt, ...) 228 { 229 va_list ap; 230 231 va_start(ap, fmt); 232 doformat(file, fmt, ap); 233 va_end(ap); 234 } 235 236 237 void 238 out1fmt(const char *fmt, ...) 239 { 240 va_list ap; 241 242 va_start(ap, fmt); 243 doformat(out1, fmt, ap); 244 va_end(ap); 245 } 246 247 void 248 dprintf(const char *fmt, ...) 249 { 250 va_list ap; 251 252 va_start(ap, fmt); 253 doformat(out2, fmt, ap); 254 va_end(ap); 255 flushout(out2); 256 } 257 258 void 259 fmtstr(char *outbuf, int length, const char *fmt, ...) 260 { 261 va_list ap; 262 struct output strout; 263 264 strout.nextc = outbuf; 265 strout.nleft = length; 266 strout.fd = BLOCK_OUT; 267 strout.flags = 0; 268 va_start(ap, fmt); 269 doformat(&strout, fmt, ap); 270 va_end(ap); 271 outc('\0', &strout); 272 if (strout.flags & OUTPUT_ERR) 273 outbuf[length - 1] = '\0'; 274 } 275 276 static int 277 doformat_wr(void *cookie, const char *buf, int len) 278 { 279 struct output *o; 280 int origlen; 281 unsigned char c; 282 283 o = (struct output *)cookie; 284 origlen = len; 285 while (len-- != 0) { 286 c = (unsigned char)*buf++; 287 outc(c, o); 288 } 289 290 return (origlen); 291 } 292 293 void 294 doformat(struct output *dest, const char *f, va_list ap) 295 { 296 FILE *fp; 297 298 if ((fp = fwopen(dest, doformat_wr)) != NULL) { 299 vfprintf(fp, f, ap); 300 fclose(fp); 301 } 302 } 303 304 /* 305 * Version of write which resumes after a signal is caught. 306 */ 307 308 int 309 xwrite(int fd, char *buf, int nbytes) 310 { 311 int ntry; 312 int i; 313 int n; 314 315 n = nbytes; 316 ntry = 0; 317 for (;;) { 318 i = write(fd, buf, n); 319 if (i > 0) { 320 if ((n -= i) <= 0) 321 return nbytes; 322 buf += i; 323 ntry = 0; 324 } else if (i == 0) { 325 if (++ntry > 10) 326 return nbytes - n; 327 } else if (errno != EINTR) { 328 return -1; 329 } 330 } 331 } 332