reenabled swaptest. quake should now load data and start on big endian architectures...
[AROS-Contrib.git] / gnu / abc-shell / io.c
blobfb12f227eb53b3445adf317bd11e19d04e17c3bb
1 /*
2 * shell buffered IO and formatted output
3 */
5 #include <ctype.h>
6 #include <sys/stat.h>
7 #include "sh.h"
9 static int initio_done;
12 * formatted output functions
16 /* A shell error occurred (eg, syntax error, etc.) */
17 void
18 errorf(const char *fmt, ...)
20 va_list va;
22 shl_stdout_ok = 0; /* debugging: note that stdout not valid */
23 exstat = 1;
24 if (*fmt) {
25 error_prefix(true);
26 va_start(va, fmt);
27 shf_vfprintf(shl_out, fmt, va);
28 va_end(va);
29 shf_putchar('\n', shl_out);
31 shf_flush(shl_out);
32 unwind(LERROR);
35 /* like errorf(), but no unwind is done */
36 void
37 warningf(int fileline, const char *fmt, ...)
39 va_list va;
41 error_prefix(fileline);
42 va_start(va, fmt);
43 shf_vfprintf(shl_out, fmt, va);
44 va_end(va);
45 shf_putchar('\n', shl_out);
46 shf_flush(shl_out);
49 /* Used by built-in utilities to prefix shell and utility name to message
50 * (also unwinds environments for special builtins).
52 void
53 bi_errorf(const char *fmt, ...)
55 va_list va;
57 shl_stdout_ok = 0; /* debugging: note that stdout not valid */
58 exstat = 1;
59 if (*fmt) {
60 error_prefix(true);
61 /* not set when main() calls parse_args() */
62 if (builtin_argv0)
63 shf_fprintf(shl_out, "%s: ", builtin_argv0);
64 va_start(va, fmt);
65 shf_vfprintf(shl_out, fmt, va);
66 va_end(va);
67 shf_putchar('\n', shl_out);
69 shf_flush(shl_out);
70 /* POSIX special builtins and ksh special builtins cause
71 * non-interactive shells to exit.
72 * XXX odd use of KEEPASN; also may not want LERROR here
74 if ((builtin_flag & SPEC_BI) ||
75 (Flag(FPOSIX) && (builtin_flag & KEEPASN)))
77 builtin_argv0 = (char *) 0;
78 unwind(LERROR);
82 /* Called when something that shouldn't happen does */
83 void
84 internal_errorf(int jump, const char *fmt, ...)
86 va_list va;
88 error_prefix(true);
89 shf_fprintf(shl_out, "internal error: ");
90 va_start(va, fmt);
91 shf_vfprintf(shl_out, fmt, va);
92 va_end(va);
93 shf_putchar('\n', shl_out);
94 shf_flush(shl_out);
95 if (jump)
96 unwind(LERROR);
99 /* used by error reporting functions to print "ksh: .kshrc[25]: " */
100 void
101 error_prefix(int fileline)
103 /* Avoid foo: foo[2]: ... */
104 if (!fileline || !source || !source->file ||
105 strcmp(source->file, kshname) != 0)
106 shf_fprintf(shl_out, "%s: ", kshname + (*kshname == '-'));
107 if (fileline && source && source->file != NULL) {
108 shf_fprintf(shl_out, "%s[%d]: ", source->file,
109 source->errline > 0 ? source->errline : source->line);
110 source->errline = 0;
114 /* printf to shl_out (stderr) with flush */
115 void
116 shellf(const char *fmt, ...)
118 va_list va;
120 if (!initio_done) /* shl_out may not be set up yet... */
121 return;
122 va_start(va, fmt);
123 shf_vfprintf(shl_out, fmt, va);
124 va_end(va);
125 shf_flush(shl_out);
128 /* printf to shl_stdout (stdout) */
129 void
130 shprintf(const char *fmt, ...)
132 va_list va;
134 if (!shl_stdout_ok)
135 internal_errorf(1, "shl_stdout not valid");
136 va_start(va, fmt);
137 shf_vfprintf(shl_stdout, fmt, va);
138 va_end(va);
141 #ifdef KSH_DEBUG
142 static struct shf *kshdebug_shf;
144 void
145 kshdebug_init_(void)
147 if (kshdebug_shf)
148 shf_close(kshdebug_shf);
149 kshdebug_shf = shf_open("/tmp/ksh-debug.log",
150 O_WRONLY|O_APPEND|O_CREAT, 0600, SHF_WR|SHF_MAPHI);
151 if (kshdebug_shf) {
152 shf_fprintf(kshdebug_shf, "\nNew shell[pid %d]\n", getpid());
153 shf_flush(kshdebug_shf);
157 /* print to debugging log */
158 void
159 kshdebug_printf_(const char *fmt, ...)
161 va_list va;
163 if (!kshdebug_shf)
164 return;
165 va_start(va, fmt);
166 shf_fprintf(kshdebug_shf, "[%d] ", getpid());
167 shf_vfprintf(kshdebug_shf, fmt, va);
168 va_end(va);
169 shf_flush(kshdebug_shf);
172 void
173 kshdebug_dump_(const char *str, const void *mem, int nbytes)
175 int i, j;
176 int nprow = 16;
178 if (!kshdebug_shf)
179 return;
180 shf_fprintf(kshdebug_shf, "[%d] %s:\n", getpid(), str);
181 for (i = 0; i < nbytes; i += nprow) {
182 char c = '\t';
184 for (j = 0; j < nprow && i + j < nbytes; j++) {
185 shf_fprintf(kshdebug_shf, "%c%02x",
186 c, ((const unsigned char *) mem)[i + j]);
187 c = ' ';
189 shf_fprintf(kshdebug_shf, "\n");
191 shf_flush(kshdebug_shf);
193 #endif /* KSH_DEBUG */
195 /* test if we can seek backwards fd (returns 0 or SHF_UNBUF) */
197 can_seek(int fd)
199 struct stat statb;
201 return fstat(fd, &statb) == 0 && !S_ISREG(statb.st_mode) ?
202 SHF_UNBUF : 0;
205 struct shf shf_iob[3];
207 void
208 initio(void)
210 shf_fdopen(1, SHF_WR, shl_stdout); /* force buffer allocation */
211 shf_fdopen(2, SHF_WR, shl_out);
212 shf_fdopen(2, SHF_WR, shl_spare); /* force buffer allocation */
213 initio_done = 1;
214 kshdebug_init();
217 /* A dup2() with error checking */
219 ksh_dup2(int ofd, int nfd, int errok)
221 int ret = dup2(ofd, nfd);
223 if (ret < 0 && errno != EBADF && !errok)
224 errorf("too many files open in shell");
226 if (ret >= 0)
227 (void) fcntl(nfd, F_SETFD, 0);
229 return ret;
233 * move fd from user space (0<=fd<10) to shell space (fd>=10),
234 * set close-on-exec flag.
237 savefd(int fd)
239 int nfd;
241 if (fd < FDBASE) {
242 nfd = ksh_dupbase(fd, FDBASE);
243 if (nfd < 0) {
244 if (errno == EBADF)
245 return -1;
246 else
247 errorf("too many files open in shell");
249 } else
250 nfd = fd;
251 #if !defined __amigaos4__ && !defined(__AROS__)
252 fcntl(nfd, F_SETFD, FD_CLOEXEC);
253 #endif
254 return nfd;
257 void
258 restfd(int fd, int ofd)
260 if (fd == 2)
261 shf_flush(&shf_iob[fd]);
262 if (ofd < 0) /* original fd closed */
263 close(fd);
264 else if (fd != ofd) {
265 ksh_dup2(ofd, fd, true); /* XXX: what to do if this fails? */
266 close(ofd);
270 void
271 openpipe(int *pv)
273 int lpv[2];
275 if (pipe(lpv) < 0)
276 errorf("can't create pipe - try again");
277 pv[0] = savefd(lpv[0]);
278 if (pv[0] != lpv[0])
279 close(lpv[0]);
280 pv[1] = savefd(lpv[1]);
281 if (pv[1] != lpv[1])
282 close(lpv[1]);
285 void
286 closepipe(int *pv)
288 close(pv[0]);
289 close(pv[1]);
292 /* Called by iosetup() (deals with 2>&4, etc.), c_read, c_print to turn
293 * a string (the X in 2>&X, read -uX, print -uX) into a file descriptor.
296 check_fd(char *name, int mode, const char **emsgp)
298 int fd, fl;
300 if (isdigit(name[0]) && !name[1]) {
301 fd = name[0] - '0';
302 if ((fl = fcntl(fd = name[0] - '0', F_GETFL, 0)) < 0) {
303 if (emsgp)
304 *emsgp = "bad file descriptor";
305 return -1;
307 fl &= O_ACCMODE;
308 /* X_OK is a kludge to disable this check for dups (x<&1):
309 * historical shells never did this check (XXX don't know what
310 * posix has to say).
312 if (!(mode & X_OK) && fl != O_RDWR &&
313 (((mode & R_OK) && fl != O_RDONLY) ||
314 ((mode & W_OK) && fl != O_WRONLY)))
316 if (emsgp)
317 *emsgp = (fl == O_WRONLY) ?
318 "fd not open for reading" :
319 "fd not open for writing";
320 return -1;
322 return fd;
323 } else if (name[0] == 'p' && !name[1])
324 return coproc_getfd(mode, emsgp);
325 if (emsgp)
326 *emsgp = "illegal file descriptor name";
327 return -1;
330 /* Called once from main */
331 void
332 coproc_init(void)
334 coproc.read = coproc.readw = coproc.write = -1;
335 coproc.njobs = 0;
336 coproc.id = 0;
339 /* Called by c_read() when eof is read - close fd if it is the co-process fd */
340 void
341 coproc_read_close(int fd)
343 if (coproc.read >= 0 && fd == coproc.read) {
344 coproc_readw_close(fd);
345 close(coproc.read);
346 coproc.read = -1;
350 /* Called by c_read() and by iosetup() to close the other side of the
351 * read pipe, so reads will actually terminate.
353 void
354 coproc_readw_close(int fd)
356 if (coproc.readw >= 0 && coproc.read >= 0 && fd == coproc.read) {
357 close(coproc.readw);
358 coproc.readw = -1;
362 /* Called by c_print when a write to a fd fails with EPIPE and by iosetup
363 * when co-process input is dup'd
365 void
366 coproc_write_close(int fd)
368 if (coproc.write >= 0 && fd == coproc.write) {
369 close(coproc.write);
370 coproc.write = -1;
374 /* Called to check for existence of/value of the co-process file descriptor.
375 * (Used by check_fd() and by c_read/c_print to deal with -p option).
378 coproc_getfd(int mode, const char **emsgp)
380 int fd = (mode & R_OK) ? coproc.read : coproc.write;
382 if (fd >= 0)
383 return fd;
384 if (emsgp)
385 *emsgp = "no coprocess";
386 return -1;
389 /* called to close file descriptors related to the coprocess (if any)
390 * Should be called with SIGCHLD blocked.
392 void
393 coproc_cleanup(int reuse)
395 /* This to allow co-processes to share output pipe */
396 if (!reuse || coproc.readw < 0 || coproc.read < 0) {
397 if (coproc.read >= 0) {
398 close(coproc.read);
399 coproc.read = -1;
401 if (coproc.readw >= 0) {
402 close(coproc.readw);
403 coproc.readw = -1;
406 if (coproc.write >= 0) {
407 close(coproc.write);
408 coproc.write = -1;
413 * temporary files
416 struct temp *
417 maketemp(Area *ap, Temp_type type, struct temp **tlist)
419 struct temp *tp;
420 int len;
421 int fd;
422 char *path;
423 const char *dir;
425 dir = tmpdir ? tmpdir : "/tmp";
426 /* The 20 + 20 is a paranoid worst case for pid/inc */
427 len = strlen(dir) + 3 + 20 + 20 + 1;
428 tp = (struct temp *) alloc(sizeof(struct temp) + len, ap);
429 tp->name = path = (char *) &tp[1];
430 tp->shf = (struct shf *) 0;
431 tp->type = type;
432 tp->type = type;
433 shf_snprintf(path, len, "%s/shXXXXXXXX", dir);
434 fd = mkstemp(path);
435 if (fd >= 0)
436 tp->shf = shf_fdopen(fd, SHF_WR, (struct shf *) 0);
437 tp->pid = procpid;
439 tp->next = *tlist;
440 *tlist = tp;
441 return tp;