don't bother resolving onbld python module deps
[unleashed.git] / bin / ksh / io.c
blob5d8f1564c722745b3fc648f617cf624be51433a0
1 /* $OpenBSD: io.c,v 1.36 2018/01/16 22:52:32 jca Exp $ */
3 /*
4 * shell buffered IO and formatted output
5 */
7 #include <sys/stat.h>
9 #include <ctype.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <string.h>
13 #include <unistd.h>
15 #include "sh.h"
17 static int initio_done;
20 * formatted output functions
24 /* A shell error occurred (eg, syntax error, etc.) */
25 void
26 errorf(const char *fmt, ...)
28 va_list va;
30 shl_stdout_ok = 0; /* debugging: note that stdout not valid */
31 exstat = 1;
32 if (fmt != NULL && *fmt != '\0') {
33 error_prefix(true);
34 va_start(va, fmt);
35 shf_vfprintf(shl_out, fmt, va);
36 va_end(va);
37 shf_putchar('\n', shl_out);
39 shf_flush(shl_out);
40 unwind(LERROR);
43 /* like errorf(), but no unwind is done */
44 void
45 warningf(bool show_lineno, const char *fmt, ...)
47 va_list va;
49 error_prefix(show_lineno);
50 va_start(va, fmt);
51 shf_vfprintf(shl_out, fmt, va);
52 va_end(va);
53 shf_putchar('\n', shl_out);
54 shf_flush(shl_out);
57 /* Used by built-in utilities to prefix shell and utility name to message
58 * (also unwinds environments for special builtins).
60 void
61 bi_errorf(const char *fmt, ...)
63 va_list va;
65 shl_stdout_ok = 0; /* debugging: note that stdout not valid */
66 exstat = 1;
67 if (fmt != NULL && *fmt != '\0') {
68 error_prefix(true);
69 /* not set when main() calls parse_args() */
70 if (builtin_argv0)
71 shf_fprintf(shl_out, "%s: ", builtin_argv0);
72 va_start(va, fmt);
73 shf_vfprintf(shl_out, fmt, va);
74 va_end(va);
75 shf_putchar('\n', shl_out);
77 shf_flush(shl_out);
78 /* POSIX special builtins and ksh special builtins cause
79 * non-interactive shells to exit.
80 * XXX odd use of KEEPASN; also may not want LERROR here
82 if ((builtin_flag & SPEC_BI) ||
83 (Flag(FPOSIX) && (builtin_flag & KEEPASN))) {
84 builtin_argv0 = NULL;
85 unwind(LERROR);
89 static void
90 internal_error_vwarn(const char *fmt, va_list va)
92 error_prefix(true);
93 shf_fprintf(shl_out, "internal error: ");
94 shf_vfprintf(shl_out, fmt, va);
95 shf_putchar('\n', shl_out);
96 shf_flush(shl_out);
99 /* Warn when something that shouldn't happen does */
100 void
101 internal_warningf(const char *fmt, ...)
103 va_list va;
105 va_start(va, fmt);
106 internal_error_vwarn(fmt, va);
107 va_end(va);
110 /* Warn and unwind when something that shouldn't happen does */
111 __dead void
112 internal_errorf(const char *fmt, ...)
114 va_list va;
116 va_start(va, fmt);
117 internal_error_vwarn(fmt, va);
118 va_end(va);
119 unwind(LERROR);
122 /* used by error reporting functions to print "ksh: .kshrc[25]: " */
123 void
124 error_prefix(int fileline)
126 /* Avoid foo: foo[2]: ... */
127 if (!fileline || !source || !source->file ||
128 strcmp(source->file, kshname) != 0)
129 shf_fprintf(shl_out, "%s: ", kshname + (*kshname == '-'));
130 if (fileline && source && source->file != NULL) {
131 shf_fprintf(shl_out, "%s[%d]: ", source->file,
132 source->errline > 0 ? source->errline : source->line);
133 source->errline = 0;
137 /* printf to shl_out (stderr) with flush */
138 void
139 shellf(const char *fmt, ...)
141 va_list va;
143 if (!initio_done) /* shl_out may not be set up yet... */
144 return;
145 va_start(va, fmt);
146 shf_vfprintf(shl_out, fmt, va);
147 va_end(va);
148 shf_flush(shl_out);
151 /* printf to shl_stdout (stdout) */
152 void
153 shprintf(const char *fmt, ...)
155 va_list va;
157 if (!shl_stdout_ok)
158 internal_errorf("shl_stdout not valid");
159 va_start(va, fmt);
160 shf_vfprintf(shl_stdout, fmt, va);
161 va_end(va);
164 #ifdef KSH_DEBUG
165 static struct shf *kshdebug_shf;
167 void
168 kshdebug_init_(void)
170 if (kshdebug_shf)
171 shf_close(kshdebug_shf);
172 kshdebug_shf = shf_open("/tmp/ksh-debug.log",
173 O_WRONLY|O_APPEND|O_CREAT, 0600, SHF_WR|SHF_MAPHI);
174 if (kshdebug_shf) {
175 shf_fprintf(kshdebug_shf, "\nNew shell[pid %d]\n", getpid());
176 shf_flush(kshdebug_shf);
180 /* print to debugging log */
181 void
182 kshdebug_printf_(const char *fmt, ...)
184 va_list va;
186 if (!kshdebug_shf)
187 return;
188 va_start(va, fmt);
189 shf_fprintf(kshdebug_shf, "[%d] ", getpid());
190 shf_vfprintf(kshdebug_shf, fmt, va);
191 va_end(va);
192 shf_flush(kshdebug_shf);
195 void
196 kshdebug_dump_(const char *str, const void *mem, int nbytes)
198 int i, j;
199 int nprow = 16;
201 if (!kshdebug_shf)
202 return;
203 shf_fprintf(kshdebug_shf, "[%d] %s:\n", getpid(), str);
204 for (i = 0; i < nbytes; i += nprow) {
205 char c = '\t';
207 for (j = 0; j < nprow && i + j < nbytes; j++) {
208 shf_fprintf(kshdebug_shf, "%c%02x", c,
209 ((const unsigned char *) mem)[i + j]);
210 c = ' ';
212 shf_fprintf(kshdebug_shf, "\n");
214 shf_flush(kshdebug_shf);
216 #endif /* KSH_DEBUG */
218 /* test if we can seek backwards fd (returns 0 or SHF_UNBUF) */
220 can_seek(int fd)
222 struct stat statb;
224 return fstat(fd, &statb) == 0 && !S_ISREG(statb.st_mode) ?
225 SHF_UNBUF : 0;
228 struct shf shf_iob[3];
230 void
231 initio(void)
233 shf_fdopen(1, SHF_WR, shl_stdout); /* force buffer allocation */
234 shf_fdopen(2, SHF_WR, shl_out);
235 shf_fdopen(2, SHF_WR, shl_spare); /* force buffer allocation */
236 initio_done = 1;
237 kshdebug_init();
240 /* A dup2() with error checking */
242 ksh_dup2(int ofd, int nfd, int errok)
244 int ret = dup2(ofd, nfd);
246 if (ret < 0 && errno != EBADF && !errok)
247 errorf("too many files open in shell");
249 return ret;
253 * move fd from user space (0<=fd<10) to shell space (fd>=10),
254 * set close-on-exec flag.
257 savefd(int fd)
259 int nfd;
261 if (fd < FDBASE) {
262 nfd = fcntl(fd, F_DUPFD_CLOEXEC, FDBASE);
263 if (nfd < 0) {
264 if (errno == EBADF)
265 return -1;
266 else
267 errorf("too many files open in shell");
269 } else {
270 nfd = fd;
271 fcntl(nfd, F_SETFD, FD_CLOEXEC);
273 return nfd;
276 void
277 restfd(int fd, int ofd)
279 if (fd == 2)
280 shf_flush(&shf_iob[fd]);
281 if (ofd < 0) /* original fd closed */
282 close(fd);
283 else if (fd != ofd) {
284 ksh_dup2(ofd, fd, true); /* XXX: what to do if this fails? */
285 close(ofd);
289 void
290 openpipe(int *pv)
292 int lpv[2];
294 if (pipe(lpv) < 0)
295 errorf("can't create pipe - try again");
296 pv[0] = savefd(lpv[0]);
297 if (pv[0] != lpv[0])
298 close(lpv[0]);
299 pv[1] = savefd(lpv[1]);
300 if (pv[1] != lpv[1])
301 close(lpv[1]);
304 void
305 closepipe(int *pv)
307 close(pv[0]);
308 close(pv[1]);
311 /* Called by iosetup() (deals with 2>&4, etc.), c_read, c_print to turn
312 * a string (the X in 2>&X, read -uX, print -uX) into a file descriptor.
315 check_fd(char *name, int mode, const char **emsgp)
317 int fd, fl;
319 if (isdigit((unsigned char)name[0]) && !name[1]) {
320 fd = name[0] - '0';
321 if ((fl = fcntl(fd, F_GETFL)) < 0) {
322 if (emsgp)
323 *emsgp = "bad file descriptor";
324 return -1;
326 fl &= O_ACCMODE;
327 /* X_OK is a kludge to disable this check for dups (x<&1):
328 * historical shells never did this check (XXX don't know what
329 * posix has to say).
331 if (!(mode & X_OK) && fl != O_RDWR &&
332 (((mode & R_OK) && fl != O_RDONLY) ||
333 ((mode & W_OK) && fl != O_WRONLY))) {
334 if (emsgp)
335 *emsgp = (fl == O_WRONLY) ?
336 "fd not open for reading" :
337 "fd not open for writing";
338 return -1;
340 return fd;
341 } else if (name[0] == 'p' && !name[1])
342 return coproc_getfd(mode, emsgp);
343 if (emsgp)
344 *emsgp = "illegal file descriptor name";
345 return -1;
348 /* Called once from main */
349 void
350 coproc_init(void)
352 coproc.read = coproc.readw = coproc.write = -1;
353 coproc.njobs = 0;
354 coproc.id = 0;
357 /* Called by c_read() when eof is read - close fd if it is the co-process fd */
358 void
359 coproc_read_close(int fd)
361 if (coproc.read >= 0 && fd == coproc.read) {
362 coproc_readw_close(fd);
363 close(coproc.read);
364 coproc.read = -1;
368 /* Called by c_read() and by iosetup() to close the other side of the
369 * read pipe, so reads will actually terminate.
371 void
372 coproc_readw_close(int fd)
374 if (coproc.readw >= 0 && coproc.read >= 0 && fd == coproc.read) {
375 close(coproc.readw);
376 coproc.readw = -1;
380 /* Called by c_print when a write to a fd fails with EPIPE and by iosetup
381 * when co-process input is dup'd
383 void
384 coproc_write_close(int fd)
386 if (coproc.write >= 0 && fd == coproc.write) {
387 close(coproc.write);
388 coproc.write = -1;
392 /* Called to check for existence of/value of the co-process file descriptor.
393 * (Used by check_fd() and by c_read/c_print to deal with -p option).
396 coproc_getfd(int mode, const char **emsgp)
398 int fd = (mode & R_OK) ? coproc.read : coproc.write;
400 if (fd >= 0)
401 return fd;
402 if (emsgp)
403 *emsgp = "no coprocess";
404 return -1;
407 /* called to close file descriptors related to the coprocess (if any)
408 * Should be called with SIGCHLD blocked.
410 void
411 coproc_cleanup(int reuse)
413 /* This to allow co-processes to share output pipe */
414 if (!reuse || coproc.readw < 0 || coproc.read < 0) {
415 if (coproc.read >= 0) {
416 close(coproc.read);
417 coproc.read = -1;
419 if (coproc.readw >= 0) {
420 close(coproc.readw);
421 coproc.readw = -1;
424 if (coproc.write >= 0) {
425 close(coproc.write);
426 coproc.write = -1;
432 * temporary files
435 struct temp *
436 maketemp(Area *ap, Temp_type type, struct temp **tlist)
438 struct temp *tp;
439 int len;
440 int fd;
441 char *path;
442 const char *dir;
444 dir = tmpdir ? tmpdir : "/tmp";
445 /* The 20 + 20 is a paranoid worst case for pid/inc */
446 len = strlen(dir) + 3 + 20 + 20 + 1;
447 tp = alloc(sizeof(struct temp) + len, ap);
448 tp->name = path = (char *) &tp[1];
449 tp->shf = NULL;
450 tp->type = type;
451 shf_snprintf(path, len, "%s/shXXXXXXXX", dir);
452 fd = mkstemp(path);
453 if (fd >= 0)
454 tp->shf = shf_fdopen(fd, SHF_WR, NULL);
455 tp->pid = procpid;
457 tp->next = *tlist;
458 *tlist = tp;
459 return tp;