1*ce8041bdSkamil /* $NetBSD: c_sh.c,v 1.24 2018/05/08 16:37:59 kamil Exp $ */
22ab2e20cStls
3e1b2664cSjtc /*
4e1b2664cSjtc * built-in Bourne commands
5e1b2664cSjtc */
66377cac7Sagc #include <sys/cdefs.h>
76377cac7Sagc
86377cac7Sagc #ifndef lint
9*ce8041bdSkamil __RCSID("$NetBSD: c_sh.c,v 1.24 2018/05/08 16:37:59 kamil Exp $");
106377cac7Sagc #endif
116377cac7Sagc
121fe37413Skamil #include <sys/stat.h>
13f6a99c40Skamil #include <sys/time.h>
14f3f9b281Skamil #include <sys/times.h>
15f6a99c40Skamil #include <time.h>
16e1b2664cSjtc
17e1b2664cSjtc #include "sh.h"
18e1b2664cSjtc
19e1b2664cSjtc static char *clocktos ARGS((clock_t t));
20e1b2664cSjtc
2148ee8d12Shubertf
22e1b2664cSjtc /* :, false and true */
23e1b2664cSjtc int
c_label(wp)24e1b2664cSjtc c_label(wp)
25e1b2664cSjtc char **wp;
26e1b2664cSjtc {
27e1b2664cSjtc return wp[0][0] == 'f' ? 1 : 0;
28e1b2664cSjtc }
29e1b2664cSjtc
30e1b2664cSjtc int
c_shift(wp)31e1b2664cSjtc c_shift(wp)
32e1b2664cSjtc char **wp;
33e1b2664cSjtc {
34*ce8041bdSkamil struct block *l = e->loc;
35*ce8041bdSkamil int n;
36e1b2664cSjtc long val;
37e1b2664cSjtc char *arg;
38e1b2664cSjtc
39e1b2664cSjtc if (ksh_getopt(wp, &builtin_opt, null) == '?')
40e1b2664cSjtc return 1;
41e1b2664cSjtc arg = wp[builtin_opt.optind];
42e1b2664cSjtc
43e1b2664cSjtc if (arg) {
4448ee8d12Shubertf evaluate(arg, &val, KSH_UNWIND_ERROR);
45e1b2664cSjtc n = val;
46e1b2664cSjtc } else
47e1b2664cSjtc n = 1;
48e1b2664cSjtc if (n < 0) {
49e1b2664cSjtc bi_errorf("%s: bad number", arg);
50e1b2664cSjtc return (1);
51e1b2664cSjtc }
52e1b2664cSjtc if (l->argc < n) {
53e1b2664cSjtc bi_errorf("nothing to shift");
54e1b2664cSjtc return (1);
55e1b2664cSjtc }
56e1b2664cSjtc l->argv[n] = l->argv[0];
57e1b2664cSjtc l->argv += n;
58e1b2664cSjtc l->argc -= n;
59e1b2664cSjtc return 0;
60e1b2664cSjtc }
61e1b2664cSjtc
62e1b2664cSjtc int
c_umask(wp)63e1b2664cSjtc c_umask(wp)
64e1b2664cSjtc char **wp;
65e1b2664cSjtc {
66*ce8041bdSkamil int i;
67*ce8041bdSkamil char *cp;
68e1b2664cSjtc int symbolic = 0;
69e1b2664cSjtc int old_umask;
70e1b2664cSjtc int optc;
71e1b2664cSjtc
72e1b2664cSjtc while ((optc = ksh_getopt(wp, &builtin_opt, "S")) != EOF)
73e1b2664cSjtc switch (optc) {
74e1b2664cSjtc case 'S':
75e1b2664cSjtc symbolic = 1;
76e1b2664cSjtc break;
77e1b2664cSjtc case '?':
78e1b2664cSjtc return 1;
79e1b2664cSjtc }
80e1b2664cSjtc cp = wp[builtin_opt.optind];
81e1b2664cSjtc if (cp == NULL) {
82e1b2664cSjtc old_umask = umask(0);
83e1b2664cSjtc umask(old_umask);
84e1b2664cSjtc if (symbolic) {
85e1b2664cSjtc char buf[18];
86e1b2664cSjtc int j;
87e1b2664cSjtc
88e1b2664cSjtc old_umask = ~old_umask;
89e1b2664cSjtc cp = buf;
90e1b2664cSjtc for (i = 0; i < 3; i++) {
91e1b2664cSjtc *cp++ = "ugo"[i];
92e1b2664cSjtc *cp++ = '=';
93e1b2664cSjtc for (j = 0; j < 3; j++)
94e1b2664cSjtc if (old_umask & (1 << (8 - (3*i + j))))
95e1b2664cSjtc *cp++ = "rwx"[j];
96e1b2664cSjtc *cp++ = ',';
97e1b2664cSjtc }
98e1b2664cSjtc cp[-1] = '\0';
99e1b2664cSjtc shprintf("%s\n", buf);
100e1b2664cSjtc } else
101e1b2664cSjtc shprintf("%#3.3o\n", old_umask);
102e1b2664cSjtc } else {
103e1b2664cSjtc int new_umask;
104e1b2664cSjtc
105e1b2664cSjtc if (digit(*cp)) {
106e1b2664cSjtc for (new_umask = 0; *cp >= '0' && *cp <= '7'; cp++)
107e1b2664cSjtc new_umask = new_umask * 8 + (*cp - '0');
108e1b2664cSjtc if (*cp) {
109e1b2664cSjtc bi_errorf("bad number");
110e1b2664cSjtc return 1;
111e1b2664cSjtc }
112e1b2664cSjtc } else {
113e1b2664cSjtc /* symbolic format */
114e1b2664cSjtc int positions, new_val;
115e1b2664cSjtc char op;
116e1b2664cSjtc
117e1b2664cSjtc old_umask = umask(0);
118e1b2664cSjtc umask(old_umask); /* in case of error */
119e1b2664cSjtc old_umask = ~old_umask;
120e1b2664cSjtc new_umask = old_umask;
121e1b2664cSjtc positions = 0;
122e1b2664cSjtc while (*cp) {
123e1b2664cSjtc while (*cp && strchr("augo", *cp))
124e1b2664cSjtc switch (*cp++) {
125e1b2664cSjtc case 'a': positions |= 0111; break;
126e1b2664cSjtc case 'u': positions |= 0100; break;
127e1b2664cSjtc case 'g': positions |= 0010; break;
128e1b2664cSjtc case 'o': positions |= 0001; break;
129e1b2664cSjtc }
130e1b2664cSjtc if (!positions)
131e1b2664cSjtc positions = 0111; /* default is a */
132e1b2664cSjtc if (!strchr("=+-", op = *cp))
133e1b2664cSjtc break;
134e1b2664cSjtc cp++;
135e1b2664cSjtc new_val = 0;
136e1b2664cSjtc while (*cp && strchr("rwxugoXs", *cp))
137e1b2664cSjtc switch (*cp++) {
138e1b2664cSjtc case 'r': new_val |= 04; break;
139e1b2664cSjtc case 'w': new_val |= 02; break;
140e1b2664cSjtc case 'x': new_val |= 01; break;
141e1b2664cSjtc case 'u': new_val |= old_umask >> 6;
142e1b2664cSjtc break;
143e1b2664cSjtc case 'g': new_val |= old_umask >> 3;
144e1b2664cSjtc break;
145e1b2664cSjtc case 'o': new_val |= old_umask >> 0;
146e1b2664cSjtc break;
147e1b2664cSjtc case 'X': if (old_umask & 0111)
148e1b2664cSjtc new_val |= 01;
149e1b2664cSjtc break;
150e1b2664cSjtc case 's': /* ignored */
151e1b2664cSjtc break;
152e1b2664cSjtc }
153e1b2664cSjtc new_val = (new_val & 07) * positions;
154e1b2664cSjtc switch (op) {
155e1b2664cSjtc case '-':
156e1b2664cSjtc new_umask &= ~new_val;
157e1b2664cSjtc break;
158e1b2664cSjtc case '=':
159e1b2664cSjtc new_umask = new_val
160e1b2664cSjtc | (new_umask & ~(positions * 07));
161e1b2664cSjtc break;
162e1b2664cSjtc case '+':
163e1b2664cSjtc new_umask |= new_val;
164e1b2664cSjtc }
165e1b2664cSjtc if (*cp == ',') {
166e1b2664cSjtc positions = 0;
167e1b2664cSjtc cp++;
168e1b2664cSjtc } else if (!strchr("=+-", *cp))
169e1b2664cSjtc break;
170e1b2664cSjtc }
171e1b2664cSjtc if (*cp) {
172e1b2664cSjtc bi_errorf("bad mask");
173e1b2664cSjtc return 1;
174e1b2664cSjtc }
175e1b2664cSjtc new_umask = ~new_umask;
176e1b2664cSjtc }
177e1b2664cSjtc umask(new_umask);
178e1b2664cSjtc }
179e1b2664cSjtc return 0;
180e1b2664cSjtc }
181e1b2664cSjtc
182e1b2664cSjtc int
c_dot(wp)183e1b2664cSjtc c_dot(wp)
184e1b2664cSjtc char **wp;
185e1b2664cSjtc {
186e1b2664cSjtc char *file, *cp;
187e1b2664cSjtc char **argv;
188e1b2664cSjtc int argc;
189e1b2664cSjtc int i;
19048ee8d12Shubertf int err;
191e1b2664cSjtc
192e1b2664cSjtc if (ksh_getopt(wp, &builtin_opt, null) == '?')
193e1b2664cSjtc return 1;
194e1b2664cSjtc
195e1b2664cSjtc if ((cp = wp[builtin_opt.optind]) == NULL)
196e1b2664cSjtc return 0;
19748ee8d12Shubertf file = search(cp, path, R_OK, &err);
198e1b2664cSjtc if (file == NULL) {
19948ee8d12Shubertf bi_errorf("%s: %s", cp, err ? strerror(err) : "not found");
200e1b2664cSjtc return 1;
201e1b2664cSjtc }
202e1b2664cSjtc
203e1b2664cSjtc /* Set positional parameters? */
204e1b2664cSjtc if (wp[builtin_opt.optind + 1]) {
205e1b2664cSjtc argv = wp + builtin_opt.optind;
206e1b2664cSjtc argv[0] = e->loc->argv[0]; /* preserve $0 */
207e1b2664cSjtc for (argc = 0; argv[argc + 1]; argc++)
208e1b2664cSjtc ;
209e1b2664cSjtc } else {
210e1b2664cSjtc argc = 0;
211e1b2664cSjtc argv = (char **) 0;
212e1b2664cSjtc }
213e1b2664cSjtc i = include(file, argc, argv, 0);
214e1b2664cSjtc if (i < 0) { /* should not happen */
215e1b2664cSjtc bi_errorf("%s: %s", cp, strerror(errno));
216e1b2664cSjtc return 1;
217e1b2664cSjtc }
218e1b2664cSjtc return i;
219e1b2664cSjtc }
220e1b2664cSjtc
221e1b2664cSjtc int
c_wait(wp)222e1b2664cSjtc c_wait(wp)
223e1b2664cSjtc char **wp;
224e1b2664cSjtc {
225e1b2664cSjtc int UNINITIALIZED(rv);
226e1b2664cSjtc int sig;
227e1b2664cSjtc
228e1b2664cSjtc if (ksh_getopt(wp, &builtin_opt, null) == '?')
229e1b2664cSjtc return 1;
230e1b2664cSjtc wp += builtin_opt.optind;
231e1b2664cSjtc if (*wp == (char *) 0) {
232e1b2664cSjtc while (waitfor((char *) 0, &sig) >= 0)
233e1b2664cSjtc ;
234e1b2664cSjtc rv = sig;
235e1b2664cSjtc } else {
236e1b2664cSjtc for (; *wp; wp++)
237e1b2664cSjtc rv = waitfor(*wp, &sig);
238e1b2664cSjtc if (rv < 0)
239e1b2664cSjtc rv = sig ? sig : 127; /* magic exit code: bad job-id */
240e1b2664cSjtc }
241e1b2664cSjtc return rv;
242e1b2664cSjtc }
243e1b2664cSjtc
244e1b2664cSjtc int
c_read(wp)245e1b2664cSjtc c_read(wp)
246e1b2664cSjtc char **wp;
247e1b2664cSjtc {
248*ce8041bdSkamil int c = 0;
249a397ec1fSchristos int expandv = 1, history = 0;
250e1b2664cSjtc int expanding;
251e1b2664cSjtc int ecode = 0;
252*ce8041bdSkamil char *cp;
253e1b2664cSjtc int fd = 0;
254e1b2664cSjtc struct shf *shf;
255e1b2664cSjtc int optc;
256e1b2664cSjtc const char *emsg;
257e1b2664cSjtc XString cs, xs;
258e1b2664cSjtc struct tbl *vp;
259e1b2664cSjtc char UNINITIALIZED(*xp);
260a397ec1fSchristos static char REPLY[] = "REPLY";
261e1b2664cSjtc
262e1b2664cSjtc while ((optc = ksh_getopt(wp, &builtin_opt, "prsu,")) != EOF)
263e1b2664cSjtc switch (optc) {
264e1b2664cSjtc #ifdef KSH
265e1b2664cSjtc case 'p':
266e1b2664cSjtc if ((fd = coproc_getfd(R_OK, &emsg)) < 0) {
267e1b2664cSjtc bi_errorf("-p: %s", emsg);
268e1b2664cSjtc return 1;
269e1b2664cSjtc }
270e1b2664cSjtc break;
271e1b2664cSjtc #endif /* KSH */
272e1b2664cSjtc case 'r':
273a397ec1fSchristos expandv = 0;
274e1b2664cSjtc break;
275e1b2664cSjtc case 's':
276e1b2664cSjtc history = 1;
277e1b2664cSjtc break;
278e1b2664cSjtc case 'u':
279e1b2664cSjtc if (!*(cp = builtin_opt.optarg))
280e1b2664cSjtc fd = 0;
281e1b2664cSjtc else if ((fd = check_fd(cp, R_OK, &emsg)) < 0) {
282e1b2664cSjtc bi_errorf("-u: %s: %s", cp, emsg);
283e1b2664cSjtc return 1;
284e1b2664cSjtc }
285e1b2664cSjtc break;
286e1b2664cSjtc case '?':
287e1b2664cSjtc return 1;
288e1b2664cSjtc }
289e1b2664cSjtc wp += builtin_opt.optind;
290e1b2664cSjtc
291e1b2664cSjtc if (*wp == NULL)
292a397ec1fSchristos *--wp = REPLY;
293e1b2664cSjtc
294e1b2664cSjtc /* Since we can't necessarily seek backwards on non-regular files,
295e1b2664cSjtc * don't buffer them so we can't read too much.
296e1b2664cSjtc */
297e1b2664cSjtc shf = shf_reopen(fd, SHF_RD | SHF_INTERRUPT | can_seek(fd), shl_spare);
298e1b2664cSjtc
299e1b2664cSjtc if ((cp = strchr(*wp, '?')) != NULL) {
300e1b2664cSjtc *cp = 0;
3010de5aac4Sjtc if (isatty(fd)) {
3020de5aac4Sjtc /* at&t ksh says it prints prompt on fd if it's open
303e1b2664cSjtc * for writing and is a tty, but it doesn't do it
3040de5aac4Sjtc * (it also doesn't check the interactive flag,
3050de5aac4Sjtc * as is indicated in the Kornshell book).
306e1b2664cSjtc */
307e1b2664cSjtc shellf("%s", cp+1);
308e1b2664cSjtc }
309e1b2664cSjtc }
310e1b2664cSjtc
311e1b2664cSjtc #ifdef KSH
312e1b2664cSjtc /* If we are reading from the co-process for the first time,
313e1b2664cSjtc * make sure the other side of the pipe is closed first. This allows
314e1b2664cSjtc * the detection of eof.
315e1b2664cSjtc *
316f662a744Smycroft * This is not compatible with at&t ksh... the fd is kept so another
317617b132aSwiz * coproc can be started with same output, however, this means eof
318e1b2664cSjtc * can't be detected... This is why it is closed here.
319e1b2664cSjtc * If this call is removed, remove the eof check below, too.
320e1b2664cSjtc * coproc_readw_close(fd);
321e1b2664cSjtc */
322e1b2664cSjtc #endif /* KSH */
323e1b2664cSjtc
324e1b2664cSjtc if (history)
325e1b2664cSjtc Xinit(xs, xp, 128, ATEMP);
326e1b2664cSjtc expanding = 0;
327e1b2664cSjtc Xinit(cs, cp, 128, ATEMP);
328e1b2664cSjtc for (; *wp != NULL; wp++) {
329e1b2664cSjtc for (cp = Xstring(cs, cp); ; ) {
330e1b2664cSjtc if (c == '\n' || c == EOF)
331e1b2664cSjtc break;
332e1b2664cSjtc while (1) {
333e1b2664cSjtc c = shf_getc(shf);
334e1b2664cSjtc if (c == '\0'
335e1b2664cSjtc )
336e1b2664cSjtc continue;
337e1b2664cSjtc if (c == EOF && shf_error(shf)
338e1b2664cSjtc && shf_errno(shf) == EINTR)
339e1b2664cSjtc {
340e1b2664cSjtc /* Was the offending signal one that
341e1b2664cSjtc * would normally kill a process?
342e1b2664cSjtc * If so, pretend the read was killed.
343e1b2664cSjtc */
344e1b2664cSjtc ecode = fatal_trap_check();
345e1b2664cSjtc
346e1b2664cSjtc /* non fatal (eg, CHLD), carry on */
347e1b2664cSjtc if (!ecode) {
348e1b2664cSjtc shf_clearerr(shf);
349e1b2664cSjtc continue;
350e1b2664cSjtc }
351e1b2664cSjtc }
352e1b2664cSjtc break;
353e1b2664cSjtc }
354e1b2664cSjtc if (history) {
355e1b2664cSjtc Xcheck(xs, xp);
356e1b2664cSjtc Xput(xs, xp, c);
357e1b2664cSjtc }
358e1b2664cSjtc Xcheck(cs, cp);
359e1b2664cSjtc if (expanding) {
360e1b2664cSjtc expanding = 0;
361e1b2664cSjtc if (c == '\n') {
362e1b2664cSjtc c = 0;
36348ee8d12Shubertf if (Flag(FTALKING_I) && isatty(fd)) {
364e1b2664cSjtc /* set prompt in case this is
365e1b2664cSjtc * called from .profile or $ENV
366e1b2664cSjtc */
367e1b2664cSjtc set_prompt(PS2, (Source *) 0);
368e1b2664cSjtc pprompt(prompt, 0);
369e1b2664cSjtc }
370e1b2664cSjtc } else if (c != EOF)
371e1b2664cSjtc Xput(cs, cp, c);
372e1b2664cSjtc continue;
373e1b2664cSjtc }
374a397ec1fSchristos if (expandv && c == '\\') {
375e1b2664cSjtc expanding = 1;
376e1b2664cSjtc continue;
377e1b2664cSjtc }
378e1b2664cSjtc if (c == '\n' || c == EOF)
379e1b2664cSjtc break;
380e1b2664cSjtc if (ctype(c, C_IFS)) {
381e1b2664cSjtc if (Xlength(cs, cp) == 0 && ctype(c, C_IFSWS))
382e1b2664cSjtc continue;
383e1b2664cSjtc if (wp[1])
384e1b2664cSjtc break;
385e1b2664cSjtc }
386e1b2664cSjtc Xput(cs, cp, c);
387e1b2664cSjtc }
388e1b2664cSjtc /* strip trailing IFS white space from last variable */
389e1b2664cSjtc if (!wp[1])
390e1b2664cSjtc while (Xlength(cs, cp) && ctype(cp[-1], C_IFS)
391e1b2664cSjtc && ctype(cp[-1], C_IFSWS))
392e1b2664cSjtc cp--;
393e1b2664cSjtc Xput(cs, cp, '\0');
394e1b2664cSjtc vp = global(*wp);
39548ee8d12Shubertf /* Must be done before setting export. */
396e1b2664cSjtc if (vp->flag & RDONLY) {
397e1b2664cSjtc shf_flush(shf);
398e1b2664cSjtc bi_errorf("%s is read only", *wp);
399e1b2664cSjtc return 1;
400e1b2664cSjtc }
401e1b2664cSjtc if (Flag(FEXPORT))
402e1b2664cSjtc typeset(*wp, EXPORT, 0, 0, 0);
40348ee8d12Shubertf if (!setstr(vp, Xstring(cs, cp), KSH_RETURN_ERROR)) {
40448ee8d12Shubertf shf_flush(shf);
40548ee8d12Shubertf return 1;
40648ee8d12Shubertf }
407e1b2664cSjtc }
408e1b2664cSjtc
409e1b2664cSjtc shf_flush(shf);
410e1b2664cSjtc if (history) {
411e1b2664cSjtc Xput(xs, xp, '\0');
412e1b2664cSjtc source->line++;
413e1b2664cSjtc histsave(source->line, Xstring(xs, xp), 1);
414e1b2664cSjtc Xfree(xs, xp);
415e1b2664cSjtc }
416e1b2664cSjtc #ifdef KSH
417e1b2664cSjtc /* if this is the co-process fd, close the file descriptor
418e1b2664cSjtc * (can get eof if and only if all processes are have died, ie,
419e1b2664cSjtc * coproc.njobs is 0 and the pipe is closed).
420e1b2664cSjtc */
421e1b2664cSjtc if (c == EOF && !ecode)
422e1b2664cSjtc coproc_read_close(fd);
423e1b2664cSjtc #endif /* KSH */
424e1b2664cSjtc
425e1b2664cSjtc return ecode ? ecode : c == EOF;
426e1b2664cSjtc }
427e1b2664cSjtc
428e1b2664cSjtc int
c_eval(wp)429e1b2664cSjtc c_eval(wp)
430e1b2664cSjtc char **wp;
431e1b2664cSjtc {
432*ce8041bdSkamil struct source *s;
433868accdbSchristos int rv;
434e1b2664cSjtc
435e1b2664cSjtc if (ksh_getopt(wp, &builtin_opt, null) == '?')
436e1b2664cSjtc return 1;
437e1b2664cSjtc s = pushs(SWORDS, ATEMP);
438e1b2664cSjtc s->u.strv = wp + builtin_opt.optind;
43948ee8d12Shubertf if (!Flag(FPOSIX)) {
44048ee8d12Shubertf /*
44148ee8d12Shubertf * Handle case where the command is empty due to failed
44248ee8d12Shubertf * command substitution, eg, eval "$(false)".
44348ee8d12Shubertf * In this case, shell() will not set/change exstat (because
44448ee8d12Shubertf * compiled tree is empty), so will use this value.
44548ee8d12Shubertf * subst_exstat is cleared in execute(), so should be 0 if
44648ee8d12Shubertf * there were no substitutions.
44748ee8d12Shubertf *
44848ee8d12Shubertf * A strict reading of POSIX says we don't do this (though
44948ee8d12Shubertf * it is traditionally done). [from 1003.2-1992]
45048ee8d12Shubertf * 3.9.1: Simple Commands
45148ee8d12Shubertf * ... If there is a command name, execution shall
45248ee8d12Shubertf * continue as described in 3.9.1.1. If there
45348ee8d12Shubertf * is no command name, but the command contained a command
45448ee8d12Shubertf * substitution, the command shall complete with the exit
45548ee8d12Shubertf * status of the last command substitution
45648ee8d12Shubertf * 3.9.1.1: Command Search and Execution
45748ee8d12Shubertf * ...(1)...(a) If the command name matches the name of
45848ee8d12Shubertf * a special built-in utility, that special built-in
45948ee8d12Shubertf * utility shall be invoked.
46048ee8d12Shubertf * 3.14.5: Eval
46148ee8d12Shubertf * ... If there are no arguments, or only null arguments,
46248ee8d12Shubertf * eval shall return an exit status of zero.
46348ee8d12Shubertf */
46448ee8d12Shubertf exstat = subst_exstat;
46548ee8d12Shubertf }
46648ee8d12Shubertf
46713351aa1Skamil rv = shell(s, false);
468868accdbSchristos afree(s, ATEMP);
469868accdbSchristos return rv;
470e1b2664cSjtc }
471e1b2664cSjtc
472e1b2664cSjtc int
c_trap(wp)473e1b2664cSjtc c_trap(wp)
474e1b2664cSjtc char **wp;
475e1b2664cSjtc {
476e1b2664cSjtc int i;
477e1b2664cSjtc char *s;
478*ce8041bdSkamil Trap *p;
479e1b2664cSjtc
480e1b2664cSjtc if (ksh_getopt(wp, &builtin_opt, null) == '?')
481e1b2664cSjtc return 1;
482e1b2664cSjtc wp += builtin_opt.optind;
483e1b2664cSjtc
484e1b2664cSjtc if (*wp == NULL) {
485e1b2664cSjtc int anydfl = 0;
486e1b2664cSjtc
487e1b2664cSjtc for (p = sigtraps, i = SIGNALS+1; --i >= 0; p++) {
488e1b2664cSjtc if (p->trap == NULL)
489e1b2664cSjtc anydfl = 1;
490e1b2664cSjtc else {
491e1b2664cSjtc shprintf("trap -- ");
492e1b2664cSjtc print_value_quoted(p->trap);
493e1b2664cSjtc shprintf(" %s\n", p->name);
494e1b2664cSjtc }
495e1b2664cSjtc }
496e1b2664cSjtc #if 0 /* this is ugly and not clear POSIX needs it */
497e1b2664cSjtc /* POSIX may need this so output of trap can be saved and
498e1b2664cSjtc * used to restore trap conditions
499e1b2664cSjtc */
500e1b2664cSjtc if (anydfl) {
501e1b2664cSjtc shprintf("trap -- -");
502e1b2664cSjtc for (p = sigtraps, i = SIGNALS+1; --i >= 0; p++)
503e1b2664cSjtc if (p->trap == NULL && p->name)
504e1b2664cSjtc shprintf(" %s", p->name);
505e1b2664cSjtc shprintf(newline);
506e1b2664cSjtc }
50716304c51Schristos #else
50816304c51Schristos __USE(anydfl);
509e1b2664cSjtc #endif
510e1b2664cSjtc return 0;
511e1b2664cSjtc }
512e1b2664cSjtc
51348ee8d12Shubertf /*
51448ee8d12Shubertf * Use case sensitive lookup for first arg so the
51548ee8d12Shubertf * command 'exit' isn't confused with the pseudo-signal
51648ee8d12Shubertf * 'EXIT'.
51748ee8d12Shubertf */
51813351aa1Skamil s = (gettrap(*wp, false) == NULL) ? *wp++ : NULL; /* get command */
519e1b2664cSjtc if (s != NULL && s[0] == '-' && s[1] == '\0')
520e1b2664cSjtc s = NULL;
521e1b2664cSjtc
522e1b2664cSjtc /* set/clear traps */
523e1b2664cSjtc while (*wp != NULL) {
52413351aa1Skamil p = gettrap(*wp++, true);
525e1b2664cSjtc if (p == NULL) {
526e1b2664cSjtc bi_errorf("bad signal %s", wp[-1]);
527e1b2664cSjtc return 1;
528e1b2664cSjtc }
529e1b2664cSjtc settrap(p, s);
530e1b2664cSjtc }
531e1b2664cSjtc return 0;
532e1b2664cSjtc }
533e1b2664cSjtc
534e1b2664cSjtc int
c_exitreturn(wp)535e1b2664cSjtc c_exitreturn(wp)
536e1b2664cSjtc char **wp;
537e1b2664cSjtc {
538e1b2664cSjtc int how = LEXIT;
53948ee8d12Shubertf int n;
540e1b2664cSjtc char *arg;
541e1b2664cSjtc
542e1b2664cSjtc if (ksh_getopt(wp, &builtin_opt, null) == '?')
543e1b2664cSjtc return 1;
544e1b2664cSjtc arg = wp[builtin_opt.optind];
545e1b2664cSjtc
54648ee8d12Shubertf if (arg) {
54748ee8d12Shubertf if (!getn(arg, &n)) {
548e1b2664cSjtc exstat = 1;
54913351aa1Skamil warningf(true, "%s: bad number", arg);
55048ee8d12Shubertf } else
55148ee8d12Shubertf exstat = n;
552e1b2664cSjtc }
553e1b2664cSjtc if (wp[0][0] == 'r') { /* return */
554e1b2664cSjtc struct env *ep;
555e1b2664cSjtc
556e1b2664cSjtc /* need to tell if this is exit or return so trap exit will
557e1b2664cSjtc * work right (POSIX)
558e1b2664cSjtc */
559e1b2664cSjtc for (ep = e; ep; ep = ep->oenv)
560e1b2664cSjtc if (STOP_RETURN(ep->type)) {
561e1b2664cSjtc how = LRETURN;
562e1b2664cSjtc break;
563e1b2664cSjtc }
564e1b2664cSjtc }
565e1b2664cSjtc
566e1b2664cSjtc if (how == LEXIT && !really_exit && j_stopped_running()) {
567e1b2664cSjtc really_exit = 1;
568e1b2664cSjtc how = LSHELL;
569e1b2664cSjtc }
570e1b2664cSjtc
571e1b2664cSjtc quitenv(); /* get rid of any i/o redirections */
572e1b2664cSjtc unwind(how);
573e1b2664cSjtc /*NOTREACHED*/
574e1b2664cSjtc return 0;
575e1b2664cSjtc }
576e1b2664cSjtc
577e1b2664cSjtc int
c_brkcont(wp)578e1b2664cSjtc c_brkcont(wp)
579e1b2664cSjtc char **wp;
580e1b2664cSjtc {
581e1b2664cSjtc int n, quit;
582e1b2664cSjtc struct env *ep, *last_ep = (struct env *) 0;
583e1b2664cSjtc char *arg;
584e1b2664cSjtc
585e1b2664cSjtc if (ksh_getopt(wp, &builtin_opt, null) == '?')
586e1b2664cSjtc return 1;
587e1b2664cSjtc arg = wp[builtin_opt.optind];
588e1b2664cSjtc
589e1b2664cSjtc if (!arg)
590e1b2664cSjtc n = 1;
591e1b2664cSjtc else if (!bi_getn(arg, &n))
592e1b2664cSjtc return 1;
593e1b2664cSjtc quit = n;
594e1b2664cSjtc if (quit <= 0) {
595e1b2664cSjtc /* at&t ksh does this for non-interactive shells only - weird */
596e1b2664cSjtc bi_errorf("%s: bad value", arg);
597e1b2664cSjtc return 1;
598e1b2664cSjtc }
599e1b2664cSjtc
600e1b2664cSjtc /* Stop at E_NONE, E_PARSE, E_FUNC, or E_INCL */
601e1b2664cSjtc for (ep = e; ep && !STOP_BRKCONT(ep->type); ep = ep->oenv)
602e1b2664cSjtc if (ep->type == E_LOOP) {
603e1b2664cSjtc if (--quit == 0)
604e1b2664cSjtc break;
605e1b2664cSjtc ep->flags |= EF_BRKCONT_PASS;
606e1b2664cSjtc last_ep = ep;
607e1b2664cSjtc }
608e1b2664cSjtc
609e1b2664cSjtc if (quit) {
610e1b2664cSjtc /* at&t ksh doesn't print a message - just does what it
611e1b2664cSjtc * can. We print a message 'cause it helps in debugging
612e1b2664cSjtc * scripts, but don't generate an error (ie, keep going).
613e1b2664cSjtc */
614e1b2664cSjtc if (n == quit) {
61513351aa1Skamil warningf(true, "%s: cannot %s", wp[0], wp[0]);
616e1b2664cSjtc return 0;
617e1b2664cSjtc }
618e1b2664cSjtc /* POSIX says if n is too big, the last enclosing loop
619e1b2664cSjtc * shall be used. Doesn't say to print an error but we
620e1b2664cSjtc * do anyway 'cause the user messed up.
621e1b2664cSjtc */
622bb7bab2dSchristos if (last_ep)
623e1b2664cSjtc last_ep->flags &= ~EF_BRKCONT_PASS;
62413351aa1Skamil warningf(true, "%s: can only %s %d level(s)",
625e1b2664cSjtc wp[0], wp[0], n - quit);
626e1b2664cSjtc }
627e1b2664cSjtc
628e1b2664cSjtc unwind(*wp[0] == 'b' ? LBREAK : LCONTIN);
629e1b2664cSjtc /*NOTREACHED*/
630e1b2664cSjtc }
631e1b2664cSjtc
632e1b2664cSjtc int
c_set(wp)633e1b2664cSjtc c_set(wp)
634e1b2664cSjtc char **wp;
635e1b2664cSjtc {
636e1b2664cSjtc int argi, setargs;
637e1b2664cSjtc struct block *l = e->loc;
638*ce8041bdSkamil char **owp = wp;
639e1b2664cSjtc
640e1b2664cSjtc if (wp[1] == NULL) {
641e1b2664cSjtc static const char *const args [] = { "set", "-", NULL };
642a397ec1fSchristos return c_typeset((char **)__UNCONST(args));
643e1b2664cSjtc }
644e1b2664cSjtc
645e1b2664cSjtc argi = parse_args(wp, OF_SET, &setargs);
646e1b2664cSjtc if (argi < 0)
647e1b2664cSjtc return 1;
648e1b2664cSjtc /* set $# and $* */
649e1b2664cSjtc if (setargs) {
650e1b2664cSjtc owp = wp += argi - 1;
651e1b2664cSjtc wp[0] = l->argv[0]; /* save $0 */
652e1b2664cSjtc while (*++wp != NULL)
653e1b2664cSjtc *wp = str_save(*wp, &l->area);
654e1b2664cSjtc l->argc = wp - owp - 1;
655e1b2664cSjtc l->argv = (char **) alloc(sizeofN(char *, l->argc+2), &l->area);
656e1b2664cSjtc for (wp = l->argv; (*wp++ = *owp++) != NULL; )
657e1b2664cSjtc ;
658e1b2664cSjtc }
659e1b2664cSjtc /* POSIX says set exit status is 0, but old scripts that use
660e1b2664cSjtc * getopt(1), use the construct: set -- `getopt ab:c "$@"`
661e1b2664cSjtc * which assumes the exit value set will be that of the ``
662e1b2664cSjtc * (subst_exstat is cleared in execute() so that it will be 0
663e1b2664cSjtc * if there are no command substitutions).
664e1b2664cSjtc */
665e1b2664cSjtc return Flag(FPOSIX) ? 0 : subst_exstat;
666e1b2664cSjtc }
667e1b2664cSjtc
668e1b2664cSjtc int
c_unset(wp)669e1b2664cSjtc c_unset(wp)
670e1b2664cSjtc char **wp;
671e1b2664cSjtc {
672*ce8041bdSkamil char *id;
673e1b2664cSjtc int optc, unset_var = 1;
674e1b2664cSjtc int ret = 0;
675e1b2664cSjtc
676e1b2664cSjtc while ((optc = ksh_getopt(wp, &builtin_opt, "fv")) != EOF)
677e1b2664cSjtc switch (optc) {
678e1b2664cSjtc case 'f':
679e1b2664cSjtc unset_var = 0;
680e1b2664cSjtc break;
681e1b2664cSjtc case 'v':
682e1b2664cSjtc unset_var = 1;
683e1b2664cSjtc break;
684e1b2664cSjtc case '?':
685e1b2664cSjtc return 1;
686e1b2664cSjtc }
687e1b2664cSjtc wp += builtin_opt.optind;
688e1b2664cSjtc for (; (id = *wp) != NULL; wp++)
689e1b2664cSjtc if (unset_var) { /* unset variable */
690e1b2664cSjtc struct tbl *vp = global(id);
691e1b2664cSjtc
692e1b2664cSjtc if ((vp->flag&RDONLY)) {
693e1b2664cSjtc bi_errorf("%s is read only", vp->name);
694e1b2664cSjtc return 1;
695e1b2664cSjtc }
696e1b2664cSjtc unset(vp, strchr(id, '[') ? 1 : 0);
697e1b2664cSjtc } else { /* unset function */
6984a1b3429Splunky if (define(id, NULL))
699e1b2664cSjtc ret = 1;
700e1b2664cSjtc }
701e1b2664cSjtc return ret;
702e1b2664cSjtc }
703e1b2664cSjtc
704e1b2664cSjtc int
c_times(wp)705e1b2664cSjtc c_times(wp)
706e1b2664cSjtc char **wp;
707e1b2664cSjtc {
708e1b2664cSjtc struct tms all;
709e1b2664cSjtc
710f3f9b281Skamil times(&all);
71148ee8d12Shubertf shprintf("Shell: %8ss user ", clocktos(all.tms_utime));
71248ee8d12Shubertf shprintf("%8ss system\n", clocktos(all.tms_stime));
71348ee8d12Shubertf shprintf("Kids: %8ss user ", clocktos(all.tms_cutime));
71448ee8d12Shubertf shprintf("%8ss system\n", clocktos(all.tms_cstime));
715e1b2664cSjtc
716e1b2664cSjtc return 0;
717e1b2664cSjtc }
718e1b2664cSjtc
719e1b2664cSjtc /*
720e1b2664cSjtc * time pipeline (really a statement, not a built-in command)
721e1b2664cSjtc */
722e1b2664cSjtc int
timex(t,f)723e1b2664cSjtc timex(t, f)
724e1b2664cSjtc struct op *t;
725e1b2664cSjtc int f;
726e1b2664cSjtc {
72748ee8d12Shubertf #define TF_NOARGS BIT(0)
72848ee8d12Shubertf #define TF_NOREAL BIT(1) /* don't report real time */
72948ee8d12Shubertf #define TF_POSIX BIT(2) /* report in posix format */
73048ee8d12Shubertf int rv = 0;
73148ee8d12Shubertf struct tms t0, t1, tms;
73248ee8d12Shubertf clock_t t0t, t1t = 0;
73348ee8d12Shubertf int tf = 0;
734f662a744Smycroft extern clock_t j_usrtime, j_systime; /* computed by j_wait */
73548ee8d12Shubertf char opts[1];
736e1b2664cSjtc
737f3f9b281Skamil t0t = times(&t0);
73848ee8d12Shubertf if (t->left) {
73948ee8d12Shubertf /*
740f662a744Smycroft * Two ways of getting cpu usage of a command: just use t0
741f662a744Smycroft * and t1 (which will get cpu usage from other jobs that
74248ee8d12Shubertf * finish while we are executing t->left), or get the
743f662a744Smycroft * cpu usage of t->left. at&t ksh does the former, while
74448ee8d12Shubertf * pdksh tries to do the later (the j_usrtime hack doesn't
74548ee8d12Shubertf * really work as it only counts the last job).
74648ee8d12Shubertf */
74748ee8d12Shubertf j_usrtime = j_systime = 0;
74848ee8d12Shubertf if (t->left->type == TCOM)
74948ee8d12Shubertf t->left->str = opts;
75048ee8d12Shubertf opts[0] = 0;
75148ee8d12Shubertf rv = execute(t->left, f | XTIME);
75248ee8d12Shubertf tf |= opts[0];
753f3f9b281Skamil t1t = times(&t1);
75448ee8d12Shubertf } else
75548ee8d12Shubertf tf = TF_NOARGS;
756e1b2664cSjtc
75748ee8d12Shubertf if (tf & TF_NOARGS) { /* ksh93 - report shell times (shell+kids) */
75848ee8d12Shubertf tf |= TF_NOREAL;
75948ee8d12Shubertf tms.tms_utime = t0.tms_utime + t0.tms_cutime;
76048ee8d12Shubertf tms.tms_stime = t0.tms_stime + t0.tms_cstime;
76148ee8d12Shubertf } else {
76248ee8d12Shubertf tms.tms_utime = t1.tms_utime - t0.tms_utime + j_usrtime;
76348ee8d12Shubertf tms.tms_stime = t1.tms_stime - t0.tms_stime + j_systime;
76448ee8d12Shubertf }
76548ee8d12Shubertf
76648ee8d12Shubertf if (!(tf & TF_NOREAL))
76748ee8d12Shubertf shf_fprintf(shl_out,
76848ee8d12Shubertf tf & TF_POSIX ? "real %8s\n" : "%8ss real ",
76948ee8d12Shubertf clocktos(t1t - t0t));
77048ee8d12Shubertf shf_fprintf(shl_out, tf & TF_POSIX ? "user %8s\n" : "%8ss user ",
77148ee8d12Shubertf clocktos(tms.tms_utime));
77248ee8d12Shubertf shf_fprintf(shl_out, tf & TF_POSIX ? "sys %8s\n" : "%8ss system\n",
77348ee8d12Shubertf clocktos(tms.tms_stime));
77448ee8d12Shubertf shf_flush(shl_out);
775e1b2664cSjtc
776e1b2664cSjtc return rv;
777e1b2664cSjtc }
778e1b2664cSjtc
77948ee8d12Shubertf void
timex_hook(t,app)78048ee8d12Shubertf timex_hook(t, app)
78148ee8d12Shubertf struct op *t;
78248ee8d12Shubertf char ** volatile *app;
78348ee8d12Shubertf {
78448ee8d12Shubertf char **wp = *app;
78548ee8d12Shubertf int optc;
78648ee8d12Shubertf int i, j;
78748ee8d12Shubertf Getopt opt;
78848ee8d12Shubertf
78948ee8d12Shubertf ksh_getopt_reset(&opt, 0);
79048ee8d12Shubertf opt.optind = 0; /* start at the start */
79148ee8d12Shubertf while ((optc = ksh_getopt(wp, &opt, ":p")) != EOF)
79248ee8d12Shubertf switch (optc) {
79348ee8d12Shubertf case 'p':
79448ee8d12Shubertf t->str[0] |= TF_POSIX;
79548ee8d12Shubertf break;
79648ee8d12Shubertf case '?':
79748ee8d12Shubertf errorf("time: -%s unknown option", opt.optarg);
79848ee8d12Shubertf case ':':
79948ee8d12Shubertf errorf("time: -%s requires an argument",
80048ee8d12Shubertf opt.optarg);
80148ee8d12Shubertf }
80248ee8d12Shubertf /* Copy command words down over options. */
80348ee8d12Shubertf if (opt.optind != 0) {
80448ee8d12Shubertf for (i = 0; i < opt.optind; i++)
80548ee8d12Shubertf afree(wp[i], ATEMP);
80648ee8d12Shubertf for (i = 0, j = opt.optind; (wp[i] = wp[j]); i++, j++)
80748ee8d12Shubertf ;
80848ee8d12Shubertf }
80948ee8d12Shubertf if (!wp[0])
81048ee8d12Shubertf t->str[0] |= TF_NOARGS;
81148ee8d12Shubertf *app = wp;
81248ee8d12Shubertf }
81348ee8d12Shubertf
814e1b2664cSjtc static char *
clocktos(t)815e1b2664cSjtc clocktos(t)
816e1b2664cSjtc clock_t t;
817e1b2664cSjtc {
81848ee8d12Shubertf static char temp[22]; /* enough for 64 bit clock_t */
819*ce8041bdSkamil int i;
820*ce8041bdSkamil char *cp = temp + sizeof(temp);
821e1b2664cSjtc
82248ee8d12Shubertf /* note: posix says must use max precision, ie, if clk_tck is
82348ee8d12Shubertf * 1000, must print 3 places after decimal (if non-zero, else 1).
82448ee8d12Shubertf */
825e1b2664cSjtc if (CLK_TCK != 100) /* convert to 1/100'ths */
8267ca13b8bSlukem t = (t < (clock_t)(1000000000/CLK_TCK)) ?
827e1b2664cSjtc (t * 100) / CLK_TCK : (t / CLK_TCK) * 100;
828e1b2664cSjtc
829e1b2664cSjtc *--cp = '\0';
830e1b2664cSjtc for (i = -2; i <= 0 || t > 0; i++) {
831e1b2664cSjtc if (i == 0)
832e1b2664cSjtc *--cp = '.';
833e1b2664cSjtc *--cp = '0' + (char)(t%10);
834e1b2664cSjtc t /= 10;
835e1b2664cSjtc }
836e1b2664cSjtc return cp;
837e1b2664cSjtc }
838e1b2664cSjtc
839e1b2664cSjtc /* exec with no args - args case is taken care of in comexec() */
840e1b2664cSjtc int
c_exec(wp)841e1b2664cSjtc c_exec(wp)
842e1b2664cSjtc char ** wp;
843e1b2664cSjtc {
844e1b2664cSjtc int i;
845e1b2664cSjtc
846e1b2664cSjtc /* make sure redirects stay in place */
847e1b2664cSjtc if (e->savefd != NULL) {
848e1b2664cSjtc for (i = 0; i < NUFILE; i++) {
849e1b2664cSjtc if (e->savefd[i] > 0)
850e1b2664cSjtc close(e->savefd[i]);
85148ee8d12Shubertf /*
85248ee8d12Shubertf * For ksh keep anything > 2 private,
85348ee8d12Shubertf * for sh, let them be (POSIX says what
85448ee8d12Shubertf * happens is unspecified and the bourne shell
85548ee8d12Shubertf * keeps them open).
85648ee8d12Shubertf */
85748ee8d12Shubertf #ifdef KSH
858e1b2664cSjtc if (i > 2 && e->savefd[i])
859e1b2664cSjtc fd_clexec(i);
86048ee8d12Shubertf #endif /* KSH */
861e1b2664cSjtc }
862e1b2664cSjtc e->savefd = NULL;
863e1b2664cSjtc }
864e1b2664cSjtc return 0;
865e1b2664cSjtc }
866e1b2664cSjtc
867e1b2664cSjtc /* dummy function, special case in comexec() */
868e1b2664cSjtc int
c_builtin(wp)869e1b2664cSjtc c_builtin(wp)
870e1b2664cSjtc char ** wp;
871e1b2664cSjtc {
872e1b2664cSjtc return 0;
873e1b2664cSjtc }
874e1b2664cSjtc
875f662a744Smycroft extern int c_test ARGS((char **wp)); /* in c_test.c */
876f662a744Smycroft extern int c_ulimit ARGS((char **wp)); /* in c_ulimit.c */
877f662a744Smycroft
878e1b2664cSjtc /* A leading = means assignments before command are kept;
879e1b2664cSjtc * a leading * means a POSIX special builtin;
880e1b2664cSjtc * a leading + means a POSIX regular builtin
881e1b2664cSjtc * (* and + should not be combined).
882e1b2664cSjtc */
883e1b2664cSjtc const struct builtin shbuiltins [] = {
884e1b2664cSjtc {"*=.", c_dot},
885e1b2664cSjtc {"*=:", c_label},
886e1b2664cSjtc {"[", c_test},
887e1b2664cSjtc {"*=break", c_brkcont},
888e1b2664cSjtc {"=builtin", c_builtin},
889e1b2664cSjtc {"*=continue", c_brkcont},
890e1b2664cSjtc {"*=eval", c_eval},
891e1b2664cSjtc {"*=exec", c_exec},
892e1b2664cSjtc {"*=exit", c_exitreturn},
893e1b2664cSjtc {"+false", c_label},
894e1b2664cSjtc {"*=return", c_exitreturn},
895e1b2664cSjtc {"*=set", c_set},
896e1b2664cSjtc {"*=shift", c_shift},
897f662a744Smycroft {"=times", c_times},
898e1b2664cSjtc {"*=trap", c_trap},
899e1b2664cSjtc {"+=wait", c_wait},
900e1b2664cSjtc {"+read", c_read},
901e1b2664cSjtc {"test", c_test},
902e1b2664cSjtc {"+true", c_label},
903e1b2664cSjtc {"ulimit", c_ulimit},
904e1b2664cSjtc {"+umask", c_umask},
905e1b2664cSjtc {"*=unset", c_unset},
906e1b2664cSjtc {NULL, NULL}
907e1b2664cSjtc };
908