io.c revision 1.2 1 1.2 tls /* $NetBSD: io.c,v 1.2 1997/01/12 19:11:53 tls Exp $ */
2 1.2 tls
3 1.1 jtc /*
4 1.1 jtc * shell buffered IO and formatted output
5 1.1 jtc */
6 1.1 jtc
7 1.1 jtc #include <ctype.h>
8 1.1 jtc #include "sh.h"
9 1.1 jtc #include "ksh_stat.h"
10 1.1 jtc
11 1.1 jtc /*
12 1.1 jtc * formatted output functions
13 1.1 jtc */
14 1.1 jtc
15 1.1 jtc
16 1.1 jtc /* A shell error occured (eg, syntax error, etc.) */
17 1.1 jtc void
18 1.1 jtc #ifdef HAVE_PROTOTYPES
19 1.1 jtc errorf(const char *fmt, ...)
20 1.1 jtc #else
21 1.1 jtc errorf(fmt, va_alist)
22 1.1 jtc const char *fmt;
23 1.1 jtc va_dcl
24 1.1 jtc #endif
25 1.1 jtc {
26 1.1 jtc va_list va;
27 1.1 jtc
28 1.1 jtc shl_stdout_ok = 0; /* debugging: note that stdout not valid */
29 1.1 jtc exstat = 1;
30 1.1 jtc if (*fmt) {
31 1.1 jtc error_prefix(TRUE);
32 1.1 jtc SH_VA_START(va, fmt);
33 1.1 jtc shf_vfprintf(shl_out, fmt, va);
34 1.1 jtc va_end(va);
35 1.1 jtc shf_putchar('\n', shl_out);
36 1.1 jtc }
37 1.1 jtc shf_flush(shl_out);
38 1.1 jtc unwind(LERROR);
39 1.1 jtc }
40 1.1 jtc
41 1.1 jtc /* like errorf(), but no unwind is done */
42 1.1 jtc void
43 1.1 jtc #ifdef HAVE_PROTOTYPES
44 1.1 jtc warningf(int fileline, const char *fmt, ...)
45 1.1 jtc #else
46 1.1 jtc warningf(fileline, fmt, va_alist)
47 1.1 jtc int fileline;
48 1.1 jtc const char *fmt;
49 1.1 jtc va_dcl
50 1.1 jtc #endif
51 1.1 jtc {
52 1.1 jtc va_list va;
53 1.1 jtc
54 1.1 jtc error_prefix(fileline);
55 1.1 jtc SH_VA_START(va, fmt);
56 1.1 jtc shf_vfprintf(shl_out, fmt, va);
57 1.1 jtc va_end(va);
58 1.1 jtc shf_putchar('\n', shl_out);
59 1.1 jtc shf_flush(shl_out);
60 1.1 jtc }
61 1.1 jtc
62 1.1 jtc /* Used by built-in utilities to prefix shell and utility name to message
63 1.1 jtc * (also unwinds environments for special builtins).
64 1.1 jtc */
65 1.1 jtc void
66 1.1 jtc #ifdef HAVE_PROTOTYPES
67 1.1 jtc bi_errorf(const char *fmt, ...)
68 1.1 jtc #else
69 1.1 jtc bi_errorf(fmt, va_alist)
70 1.1 jtc const char *fmt;
71 1.1 jtc va_dcl
72 1.1 jtc #endif
73 1.1 jtc {
74 1.1 jtc va_list va;
75 1.1 jtc
76 1.1 jtc shl_stdout_ok = 0; /* debugging: note that stdout not valid */
77 1.1 jtc exstat = 1;
78 1.1 jtc if (*fmt) {
79 1.1 jtc error_prefix(TRUE);
80 1.1 jtc /* not set when main() calls parse_args() */
81 1.1 jtc if (builtin_argv0)
82 1.1 jtc shf_fprintf(shl_out, "%s: ", builtin_argv0);
83 1.1 jtc SH_VA_START(va, fmt);
84 1.1 jtc shf_vfprintf(shl_out, fmt, va);
85 1.1 jtc va_end(va);
86 1.1 jtc shf_putchar('\n', shl_out);
87 1.1 jtc }
88 1.1 jtc shf_flush(shl_out);
89 1.1 jtc /* POSIX special builtins and ksh special builtins cause
90 1.1 jtc * non-interactive shells to exit.
91 1.1 jtc * XXX odd use of KEEPASN; also may not want LERROR here
92 1.1 jtc */
93 1.1 jtc if ((builtin_flag & SPEC_BI)
94 1.1 jtc || (Flag(FPOSIX) && (builtin_flag & KEEPASN)))
95 1.1 jtc {
96 1.1 jtc builtin_argv0 = (char *) 0;
97 1.1 jtc unwind(LERROR);
98 1.1 jtc }
99 1.1 jtc }
100 1.1 jtc
101 1.1 jtc /* Called when something that shouldn't happen does */
102 1.1 jtc void
103 1.1 jtc #ifdef HAVE_PROTOTYPES
104 1.1 jtc internal_errorf(int jump, const char *fmt, ...)
105 1.1 jtc #else
106 1.1 jtc internal_errorf(jump, fmt, va_alist)
107 1.1 jtc int jump;
108 1.1 jtc const char *fmt;
109 1.1 jtc va_dcl
110 1.1 jtc #endif
111 1.1 jtc {
112 1.1 jtc va_list va;
113 1.1 jtc
114 1.1 jtc error_prefix(TRUE);
115 1.1 jtc shf_fprintf(shl_out, "internal error: ");
116 1.1 jtc SH_VA_START(va, fmt);
117 1.1 jtc shf_vfprintf(shl_out, fmt, va);
118 1.1 jtc va_end(va);
119 1.1 jtc shf_putchar('\n', shl_out);
120 1.1 jtc shf_flush(shl_out);
121 1.1 jtc if (jump)
122 1.1 jtc unwind(LERROR);
123 1.1 jtc }
124 1.1 jtc
125 1.1 jtc /* used by error reporting functions to print "ksh: .kshrc[25]: " */
126 1.1 jtc void
127 1.1 jtc error_prefix(fileline)
128 1.1 jtc int fileline;
129 1.1 jtc {
130 1.1 jtc shf_fprintf(shl_out, "%s: ", kshname + (*kshname == '-'));
131 1.1 jtc if (fileline && source && source->file != NULL) {
132 1.1 jtc shf_fprintf(shl_out, "%s[%d]: ", source->file,
133 1.1 jtc source->errline > 0 ? source->errline : source->line);
134 1.1 jtc source->errline = 0;
135 1.1 jtc }
136 1.1 jtc }
137 1.1 jtc
138 1.1 jtc /* printf to shl_out (stderr) with flush */
139 1.1 jtc void
140 1.1 jtc #ifdef HAVE_PROTOTYPES
141 1.1 jtc shellf(const char *fmt, ...)
142 1.1 jtc #else
143 1.1 jtc shellf(fmt, va_alist)
144 1.1 jtc const char *fmt;
145 1.1 jtc va_dcl
146 1.1 jtc #endif
147 1.1 jtc {
148 1.1 jtc va_list va;
149 1.1 jtc
150 1.1 jtc SH_VA_START(va, fmt);
151 1.1 jtc shf_vfprintf(shl_out, fmt, va);
152 1.1 jtc va_end(va);
153 1.1 jtc shf_flush(shl_out);
154 1.1 jtc }
155 1.1 jtc
156 1.1 jtc /* printf to shl_stdout (stdout) */
157 1.1 jtc void
158 1.1 jtc #ifdef HAVE_PROTOTYPES
159 1.1 jtc shprintf(const char *fmt, ...)
160 1.1 jtc #else
161 1.1 jtc shprintf(fmt, va_alist)
162 1.1 jtc const char *fmt;
163 1.1 jtc va_dcl
164 1.1 jtc #endif
165 1.1 jtc {
166 1.1 jtc va_list va;
167 1.1 jtc
168 1.1 jtc if (!shl_stdout_ok)
169 1.1 jtc internal_errorf(1, "shl_stdout not valid");
170 1.1 jtc SH_VA_START(va, fmt);
171 1.1 jtc shf_vfprintf(shl_stdout, fmt, va);
172 1.1 jtc va_end(va);
173 1.1 jtc }
174 1.1 jtc
175 1.1 jtc /* test if we can seek backwards fd (returns 0 or SHF_UNBUF) */
176 1.1 jtc int
177 1.1 jtc can_seek(fd)
178 1.1 jtc int fd;
179 1.1 jtc {
180 1.1 jtc struct stat statb;
181 1.1 jtc
182 1.1 jtc return fstat(fd, &statb) == 0 && !S_ISREG(statb.st_mode) ?
183 1.1 jtc SHF_UNBUF : 0;
184 1.1 jtc }
185 1.1 jtc
186 1.1 jtc struct shf shf_iob[3];
187 1.1 jtc
188 1.1 jtc void
189 1.1 jtc initio()
190 1.1 jtc {
191 1.1 jtc shf_fdopen(1, SHF_WR, shl_stdout); /* force buffer allocation */
192 1.1 jtc shf_fdopen(2, SHF_WR, shl_out);
193 1.1 jtc shf_fdopen(2, SHF_WR, shl_spare); /* force buffer allocation */
194 1.1 jtc }
195 1.1 jtc
196 1.1 jtc /* A dup2() with error checking */
197 1.1 jtc int
198 1.1 jtc ksh_dup2(ofd, nfd, errok)
199 1.1 jtc int ofd;
200 1.1 jtc int nfd;
201 1.1 jtc int errok;
202 1.1 jtc {
203 1.1 jtc int ret = dup2(ofd, nfd);
204 1.1 jtc
205 1.1 jtc if (ret < 0 && errno != EBADF && !errok)
206 1.1 jtc errorf("too many files open in shell");
207 1.1 jtc
208 1.1 jtc #ifdef DUP2_BROKEN
209 1.1 jtc /* Ultrix systems like to preserve the close-on-exec flag */
210 1.1 jtc if (ret >= 0)
211 1.1 jtc (void) fcntl(nfd, F_SETFD, 0);
212 1.1 jtc #endif /* DUP2_BROKEN */
213 1.1 jtc
214 1.1 jtc return ret;
215 1.1 jtc }
216 1.1 jtc
217 1.1 jtc /*
218 1.1 jtc * move fd from user space (0<=fd<10) to shell space (fd>=10),
219 1.1 jtc * set close-on-exec flag.
220 1.1 jtc */
221 1.1 jtc int
222 1.1 jtc savefd(fd, noclose)
223 1.1 jtc int fd;
224 1.1 jtc int noclose;
225 1.1 jtc {
226 1.1 jtc int nfd;
227 1.1 jtc
228 1.1 jtc if (fd < FDBASE) {
229 1.1 jtc nfd = ksh_dupbase(fd, FDBASE);
230 1.1 jtc if (nfd < 0)
231 1.1 jtc if (errno == EBADF)
232 1.1 jtc return -1;
233 1.1 jtc else
234 1.1 jtc errorf("too many files open in shell");
235 1.1 jtc if (!noclose)
236 1.1 jtc close(fd);
237 1.1 jtc } else
238 1.1 jtc nfd = fd;
239 1.1 jtc fd_clexec(nfd);
240 1.1 jtc return nfd;
241 1.1 jtc }
242 1.1 jtc
243 1.1 jtc void
244 1.1 jtc restfd(fd, ofd)
245 1.1 jtc int fd, ofd;
246 1.1 jtc {
247 1.1 jtc if (fd == 2)
248 1.1 jtc shf_flush(&shf_iob[fd]);
249 1.1 jtc if (ofd < 0) /* original fd closed */
250 1.1 jtc close(fd);
251 1.1 jtc else {
252 1.1 jtc ksh_dup2(ofd, fd, TRUE); /* XXX: what to do if this fails? */
253 1.1 jtc close(ofd);
254 1.1 jtc }
255 1.1 jtc }
256 1.1 jtc
257 1.1 jtc void
258 1.1 jtc openpipe(pv)
259 1.1 jtc register int *pv;
260 1.1 jtc {
261 1.1 jtc if (pipe(pv) < 0)
262 1.1 jtc errorf("can't create pipe - try again");
263 1.1 jtc pv[0] = savefd(pv[0], 0);
264 1.1 jtc pv[1] = savefd(pv[1], 0);
265 1.1 jtc }
266 1.1 jtc
267 1.1 jtc void
268 1.1 jtc closepipe(pv)
269 1.1 jtc register int *pv;
270 1.1 jtc {
271 1.1 jtc close(pv[0]);
272 1.1 jtc close(pv[1]);
273 1.1 jtc }
274 1.1 jtc
275 1.1 jtc /* Called by iosetup() (deals with 2>&4, etc.), c_read, c_print to turn
276 1.1 jtc * a string (the X in 2>&X, read -uX, print -uX) into a file descriptor.
277 1.1 jtc */
278 1.1 jtc int
279 1.1 jtc check_fd(name, mode, emsgp)
280 1.1 jtc char *name;
281 1.1 jtc int mode;
282 1.1 jtc const char **emsgp;
283 1.1 jtc {
284 1.1 jtc int fd, fl;
285 1.1 jtc
286 1.1 jtc if (isdigit(name[0]) && !name[1]) {
287 1.1 jtc fd = name[0] - '0';
288 1.1 jtc if ((fl = fcntl(fd = name[0] - '0', F_GETFL, 0)) < 0) {
289 1.1 jtc if (emsgp)
290 1.1 jtc *emsgp = "bad file descriptor";
291 1.1 jtc return -1;
292 1.1 jtc }
293 1.1 jtc fl &= O_ACCMODE;
294 1.1 jtc #ifdef OS2
295 1.1 jtc if (mode == W_OK ) {
296 1.1 jtc if (setmode(fd, O_TEXT) == -1) {
297 1.1 jtc if (emsgp)
298 1.1 jtc *emsgp = "couldn't set write mode";
299 1.1 jtc return -1;
300 1.1 jtc }
301 1.1 jtc } else if (mode == R_OK)
302 1.1 jtc if (setmode(fd, O_BINARY) == -1) {
303 1.1 jtc if (emsgp)
304 1.1 jtc *emsgp = "couldn't set read mode";
305 1.1 jtc return -1;
306 1.1 jtc }
307 1.1 jtc #else /* OS2 */
308 1.1 jtc /* X_OK is a kludge to disable this check for dups (x<&1):
309 1.1 jtc * historical shells never did this check (XXX don't know what
310 1.1 jtc * posix has to say).
311 1.1 jtc */
312 1.1 jtc if (!(mode & X_OK) && fl != O_RDWR
313 1.1 jtc && (((mode & R_OK) && fl != O_RDONLY)
314 1.1 jtc || ((mode & W_OK) && fl != O_WRONLY)))
315 1.1 jtc {
316 1.1 jtc if (emsgp)
317 1.1 jtc *emsgp = (fl == O_WRONLY) ?
318 1.1 jtc "fd not open for reading"
319 1.1 jtc : "fd not open for writing";
320 1.1 jtc return -1;
321 1.1 jtc }
322 1.1 jtc #endif /* OS2 */
323 1.1 jtc return fd;
324 1.1 jtc }
325 1.1 jtc #ifdef KSH
326 1.1 jtc else if (name[0] == 'p' && !name[1])
327 1.1 jtc return coproc_getfd(mode, emsgp);
328 1.1 jtc #endif /* KSH */
329 1.1 jtc if (emsgp)
330 1.1 jtc *emsgp = "illegal file descriptor name";
331 1.1 jtc return -1;
332 1.1 jtc }
333 1.1 jtc
334 1.1 jtc #ifdef KSH
335 1.1 jtc /* Called once from main */
336 1.1 jtc void
337 1.1 jtc coproc_init()
338 1.1 jtc {
339 1.1 jtc coproc.read = coproc.readw = coproc.write = -1;
340 1.1 jtc coproc.njobs = 0;
341 1.1 jtc coproc.id = 0;
342 1.1 jtc }
343 1.1 jtc
344 1.1 jtc /* Called by c_read() when eof is read - close fd if it is the co-process fd */
345 1.1 jtc void
346 1.1 jtc coproc_read_close(fd)
347 1.1 jtc int fd;
348 1.1 jtc {
349 1.1 jtc if (coproc.read >= 0 && fd == coproc.read) {
350 1.1 jtc coproc_readw_close(fd);
351 1.1 jtc close(coproc.read);
352 1.1 jtc coproc.read = -1;
353 1.1 jtc }
354 1.1 jtc }
355 1.1 jtc
356 1.1 jtc /* Called by c_read() and by iosetup() to close the other side of the
357 1.1 jtc * read pipe, so reads will actually terminate.
358 1.1 jtc */
359 1.1 jtc void
360 1.1 jtc coproc_readw_close(fd)
361 1.1 jtc int fd;
362 1.1 jtc {
363 1.1 jtc if (coproc.readw >= 0 && coproc.read >= 0 && fd == coproc.read) {
364 1.1 jtc close(coproc.readw);
365 1.1 jtc coproc.readw = -1;
366 1.1 jtc }
367 1.1 jtc }
368 1.1 jtc
369 1.1 jtc /* Called by c_print when a write to a fd fails with EPIPE and by iosetup
370 1.1 jtc * when co-process input is dup'd
371 1.1 jtc */
372 1.1 jtc void
373 1.1 jtc coproc_write_close(fd)
374 1.1 jtc int fd;
375 1.1 jtc {
376 1.1 jtc if (coproc.write >= 0 && fd == coproc.write) {
377 1.1 jtc close(coproc.write);
378 1.1 jtc coproc.write = -1;
379 1.1 jtc }
380 1.1 jtc }
381 1.1 jtc
382 1.1 jtc /* Called to check for existance of/value of the co-process file descriptor.
383 1.1 jtc * (Used by check_fd() and by c_read/c_print to deal with -p option).
384 1.1 jtc */
385 1.1 jtc int
386 1.1 jtc coproc_getfd(mode, emsgp)
387 1.1 jtc int mode;
388 1.1 jtc const char **emsgp;
389 1.1 jtc {
390 1.1 jtc int fd = (mode & R_OK) ? coproc.read : coproc.write;
391 1.1 jtc
392 1.1 jtc if (fd >= 0)
393 1.1 jtc return fd;
394 1.1 jtc if (emsgp)
395 1.1 jtc *emsgp = "no coprocess";
396 1.1 jtc return -1;
397 1.1 jtc }
398 1.1 jtc
399 1.1 jtc /* called to close file descriptors related to the coprocess (if any)
400 1.1 jtc * Should be called with SIGCHLD blocked.
401 1.1 jtc */
402 1.1 jtc void
403 1.1 jtc coproc_cleanup(reuse)
404 1.1 jtc int reuse;
405 1.1 jtc {
406 1.1 jtc /* This to allow co-processes to share output pipe */
407 1.1 jtc if (!reuse || coproc.readw < 0 || coproc.read < 0) {
408 1.1 jtc if (coproc.read >= 0) {
409 1.1 jtc close(coproc.read);
410 1.1 jtc coproc.read = -1;
411 1.1 jtc }
412 1.1 jtc if (coproc.readw >= 0) {
413 1.1 jtc close(coproc.readw);
414 1.1 jtc coproc.readw = -1;
415 1.1 jtc }
416 1.1 jtc }
417 1.1 jtc if (coproc.write >= 0) {
418 1.1 jtc close(coproc.write);
419 1.1 jtc coproc.write = -1;
420 1.1 jtc }
421 1.1 jtc }
422 1.1 jtc #endif /* KSH */
423 1.1 jtc
424 1.1 jtc /*
425 1.1 jtc * temporary files
426 1.1 jtc */
427 1.1 jtc
428 1.1 jtc struct temp *
429 1.1 jtc maketemp(ap)
430 1.1 jtc Area *ap;
431 1.1 jtc {
432 1.1 jtc static unsigned int inc;
433 1.1 jtc struct temp *tp;
434 1.1 jtc int len;
435 1.1 jtc int fd;
436 1.1 jtc char *path;
437 1.1 jtc const char *tmp;
438 1.1 jtc
439 1.1 jtc tmp = tmpdir ? tmpdir : "/tmp";
440 1.1 jtc /* The 20 + 20 is a paranoid worst case for pid/inc */
441 1.1 jtc len = strlen(tmp) + 3 + 20 + 20 + 1;
442 1.1 jtc tp = (struct temp *) alloc(sizeof(struct temp) + len, ap);
443 1.1 jtc tp->name = path = (char *) &tp[1];
444 1.1 jtc tp->shf = (struct shf *) 0;
445 1.1 jtc while (1) {
446 1.1 jtc /* Note that temp files need to fit 8.3 DOS limits */
447 1.1 jtc shf_snprintf(path, len, "%s/sh%05u.%03x",
448 1.1 jtc tmp, (unsigned) procpid, inc++);
449 1.1 jtc /* Mode 0600 to be paranoid, O_TRUNC in case O_EXCL isn't
450 1.1 jtc * really there.
451 1.1 jtc */
452 1.1 jtc fd = open(path, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0600);
453 1.1 jtc if (fd >= 0) {
454 1.1 jtc tp->shf = shf_fdopen(fd, SHF_WR, (struct shf *) 0);
455 1.1 jtc break;
456 1.1 jtc }
457 1.1 jtc if (errno != EINTR
458 1.1 jtc #ifdef EEXIST
459 1.1 jtc && errno != EEXIST
460 1.1 jtc #endif /* EEXIST */
461 1.1 jtc #ifdef EISDIR
462 1.1 jtc && errno != EISDIR
463 1.1 jtc #endif /* EISDIR */
464 1.1 jtc )
465 1.1 jtc /* Error must be printed by called: don't know here if
466 1.1 jtc * errorf() or bi_errorf() should be used.
467 1.1 jtc */
468 1.1 jtc break;
469 1.1 jtc }
470 1.1 jtc tp->next = NULL;
471 1.1 jtc tp->pid = procpid;
472 1.1 jtc return tp;
473 1.1 jtc }
474