c_sh.c revision 1.1.1.3 1 1.1 jtc /*
2 1.1 jtc * built-in Bourne commands
3 1.1 jtc */
4 1.1 jtc
5 1.1 jtc #include "sh.h"
6 1.1 jtc #include "ksh_stat.h" /* umask() */
7 1.1 jtc #include "ksh_time.h"
8 1.1 jtc #include "ksh_times.h"
9 1.1 jtc
10 1.1 jtc static char *clocktos ARGS((clock_t t));
11 1.1 jtc
12 1.1.1.3 hubertf
13 1.1 jtc /* :, false and true */
14 1.1 jtc int
15 1.1 jtc c_label(wp)
16 1.1 jtc char **wp;
17 1.1 jtc {
18 1.1 jtc return wp[0][0] == 'f' ? 1 : 0;
19 1.1 jtc }
20 1.1 jtc
21 1.1 jtc int
22 1.1 jtc c_shift(wp)
23 1.1 jtc char **wp;
24 1.1 jtc {
25 1.1 jtc register struct block *l = e->loc;
26 1.1 jtc register int n;
27 1.1 jtc long val;
28 1.1 jtc char *arg;
29 1.1 jtc
30 1.1 jtc if (ksh_getopt(wp, &builtin_opt, null) == '?')
31 1.1 jtc return 1;
32 1.1 jtc arg = wp[builtin_opt.optind];
33 1.1 jtc
34 1.1 jtc if (arg) {
35 1.1.1.3 hubertf evaluate(arg, &val, KSH_UNWIND_ERROR);
36 1.1 jtc n = val;
37 1.1 jtc } else
38 1.1 jtc n = 1;
39 1.1 jtc if (n < 0) {
40 1.1 jtc bi_errorf("%s: bad number", arg);
41 1.1 jtc return (1);
42 1.1 jtc }
43 1.1 jtc if (l->argc < n) {
44 1.1 jtc bi_errorf("nothing to shift");
45 1.1 jtc return (1);
46 1.1 jtc }
47 1.1 jtc l->argv[n] = l->argv[0];
48 1.1 jtc l->argv += n;
49 1.1 jtc l->argc -= n;
50 1.1 jtc return 0;
51 1.1 jtc }
52 1.1 jtc
53 1.1 jtc int
54 1.1 jtc c_umask(wp)
55 1.1 jtc char **wp;
56 1.1 jtc {
57 1.1 jtc register int i;
58 1.1 jtc register char *cp;
59 1.1 jtc int symbolic = 0;
60 1.1 jtc int old_umask;
61 1.1 jtc int optc;
62 1.1 jtc
63 1.1 jtc while ((optc = ksh_getopt(wp, &builtin_opt, "S")) != EOF)
64 1.1 jtc switch (optc) {
65 1.1 jtc case 'S':
66 1.1 jtc symbolic = 1;
67 1.1 jtc break;
68 1.1 jtc case '?':
69 1.1 jtc return 1;
70 1.1 jtc }
71 1.1 jtc cp = wp[builtin_opt.optind];
72 1.1 jtc if (cp == NULL) {
73 1.1 jtc old_umask = umask(0);
74 1.1 jtc umask(old_umask);
75 1.1 jtc if (symbolic) {
76 1.1 jtc char buf[18];
77 1.1 jtc int j;
78 1.1 jtc
79 1.1 jtc old_umask = ~old_umask;
80 1.1 jtc cp = buf;
81 1.1 jtc for (i = 0; i < 3; i++) {
82 1.1 jtc *cp++ = "ugo"[i];
83 1.1 jtc *cp++ = '=';
84 1.1 jtc for (j = 0; j < 3; j++)
85 1.1 jtc if (old_umask & (1 << (8 - (3*i + j))))
86 1.1 jtc *cp++ = "rwx"[j];
87 1.1 jtc *cp++ = ',';
88 1.1 jtc }
89 1.1 jtc cp[-1] = '\0';
90 1.1 jtc shprintf("%s\n", buf);
91 1.1 jtc } else
92 1.1 jtc shprintf("%#3.3o\n", old_umask);
93 1.1 jtc } else {
94 1.1 jtc int new_umask;
95 1.1 jtc
96 1.1 jtc if (digit(*cp)) {
97 1.1 jtc for (new_umask = 0; *cp >= '0' && *cp <= '7'; cp++)
98 1.1 jtc new_umask = new_umask * 8 + (*cp - '0');
99 1.1 jtc if (*cp) {
100 1.1 jtc bi_errorf("bad number");
101 1.1 jtc return 1;
102 1.1 jtc }
103 1.1 jtc } else {
104 1.1 jtc /* symbolic format */
105 1.1 jtc int positions, new_val;
106 1.1 jtc char op;
107 1.1 jtc
108 1.1 jtc old_umask = umask(0);
109 1.1 jtc umask(old_umask); /* in case of error */
110 1.1 jtc old_umask = ~old_umask;
111 1.1 jtc new_umask = old_umask;
112 1.1 jtc positions = 0;
113 1.1 jtc while (*cp) {
114 1.1 jtc while (*cp && strchr("augo", *cp))
115 1.1 jtc switch (*cp++) {
116 1.1 jtc case 'a': positions |= 0111; break;
117 1.1 jtc case 'u': positions |= 0100; break;
118 1.1 jtc case 'g': positions |= 0010; break;
119 1.1 jtc case 'o': positions |= 0001; break;
120 1.1 jtc }
121 1.1 jtc if (!positions)
122 1.1 jtc positions = 0111; /* default is a */
123 1.1 jtc if (!strchr("=+-", op = *cp))
124 1.1 jtc break;
125 1.1 jtc cp++;
126 1.1 jtc new_val = 0;
127 1.1 jtc while (*cp && strchr("rwxugoXs", *cp))
128 1.1 jtc switch (*cp++) {
129 1.1 jtc case 'r': new_val |= 04; break;
130 1.1 jtc case 'w': new_val |= 02; break;
131 1.1 jtc case 'x': new_val |= 01; break;
132 1.1 jtc case 'u': new_val |= old_umask >> 6;
133 1.1 jtc break;
134 1.1 jtc case 'g': new_val |= old_umask >> 3;
135 1.1 jtc break;
136 1.1 jtc case 'o': new_val |= old_umask >> 0;
137 1.1 jtc break;
138 1.1 jtc case 'X': if (old_umask & 0111)
139 1.1 jtc new_val |= 01;
140 1.1 jtc break;
141 1.1 jtc case 's': /* ignored */
142 1.1 jtc break;
143 1.1 jtc }
144 1.1 jtc new_val = (new_val & 07) * positions;
145 1.1 jtc switch (op) {
146 1.1 jtc case '-':
147 1.1 jtc new_umask &= ~new_val;
148 1.1 jtc break;
149 1.1 jtc case '=':
150 1.1 jtc new_umask = new_val
151 1.1 jtc | (new_umask & ~(positions * 07));
152 1.1 jtc break;
153 1.1 jtc case '+':
154 1.1 jtc new_umask |= new_val;
155 1.1 jtc }
156 1.1 jtc if (*cp == ',') {
157 1.1 jtc positions = 0;
158 1.1 jtc cp++;
159 1.1 jtc } else if (!strchr("=+-", *cp))
160 1.1 jtc break;
161 1.1 jtc }
162 1.1 jtc if (*cp) {
163 1.1 jtc bi_errorf("bad mask");
164 1.1 jtc return 1;
165 1.1 jtc }
166 1.1 jtc new_umask = ~new_umask;
167 1.1 jtc }
168 1.1 jtc umask(new_umask);
169 1.1 jtc }
170 1.1 jtc return 0;
171 1.1 jtc }
172 1.1 jtc
173 1.1 jtc int
174 1.1 jtc c_dot(wp)
175 1.1 jtc char **wp;
176 1.1 jtc {
177 1.1 jtc char *file, *cp;
178 1.1 jtc char **argv;
179 1.1 jtc int argc;
180 1.1 jtc int i;
181 1.1.1.3 hubertf int err;
182 1.1 jtc
183 1.1 jtc if (ksh_getopt(wp, &builtin_opt, null) == '?')
184 1.1 jtc return 1;
185 1.1 jtc
186 1.1 jtc if ((cp = wp[builtin_opt.optind]) == NULL)
187 1.1 jtc return 0;
188 1.1.1.3 hubertf file = search(cp, path, R_OK, &err);
189 1.1 jtc if (file == NULL) {
190 1.1.1.3 hubertf bi_errorf("%s: %s", cp, err ? strerror(err) : "not found");
191 1.1 jtc return 1;
192 1.1 jtc }
193 1.1 jtc
194 1.1 jtc /* Set positional parameters? */
195 1.1 jtc if (wp[builtin_opt.optind + 1]) {
196 1.1 jtc argv = wp + builtin_opt.optind;
197 1.1 jtc argv[0] = e->loc->argv[0]; /* preserve $0 */
198 1.1 jtc for (argc = 0; argv[argc + 1]; argc++)
199 1.1 jtc ;
200 1.1 jtc } else {
201 1.1 jtc argc = 0;
202 1.1 jtc argv = (char **) 0;
203 1.1 jtc }
204 1.1 jtc i = include(file, argc, argv, 0);
205 1.1 jtc if (i < 0) { /* should not happen */
206 1.1 jtc bi_errorf("%s: %s", cp, strerror(errno));
207 1.1 jtc return 1;
208 1.1 jtc }
209 1.1 jtc return i;
210 1.1 jtc }
211 1.1 jtc
212 1.1 jtc int
213 1.1 jtc c_wait(wp)
214 1.1 jtc char **wp;
215 1.1 jtc {
216 1.1 jtc int UNINITIALIZED(rv);
217 1.1 jtc int sig;
218 1.1 jtc
219 1.1 jtc if (ksh_getopt(wp, &builtin_opt, null) == '?')
220 1.1 jtc return 1;
221 1.1 jtc wp += builtin_opt.optind;
222 1.1 jtc if (*wp == (char *) 0) {
223 1.1 jtc while (waitfor((char *) 0, &sig) >= 0)
224 1.1 jtc ;
225 1.1 jtc rv = sig;
226 1.1 jtc } else {
227 1.1 jtc for (; *wp; wp++)
228 1.1 jtc rv = waitfor(*wp, &sig);
229 1.1 jtc if (rv < 0)
230 1.1 jtc rv = sig ? sig : 127; /* magic exit code: bad job-id */
231 1.1 jtc }
232 1.1 jtc return rv;
233 1.1 jtc }
234 1.1 jtc
235 1.1 jtc int
236 1.1 jtc c_read(wp)
237 1.1 jtc char **wp;
238 1.1 jtc {
239 1.1 jtc register int c = 0;
240 1.1 jtc int expand = 1, history = 0;
241 1.1 jtc int expanding;
242 1.1 jtc int ecode = 0;
243 1.1 jtc register char *cp;
244 1.1 jtc int fd = 0;
245 1.1 jtc struct shf *shf;
246 1.1 jtc int optc;
247 1.1 jtc const char *emsg;
248 1.1 jtc XString cs, xs;
249 1.1 jtc struct tbl *vp;
250 1.1 jtc char UNINITIALIZED(*xp);
251 1.1 jtc
252 1.1 jtc while ((optc = ksh_getopt(wp, &builtin_opt, "prsu,")) != EOF)
253 1.1 jtc switch (optc) {
254 1.1 jtc #ifdef KSH
255 1.1 jtc case 'p':
256 1.1 jtc if ((fd = coproc_getfd(R_OK, &emsg)) < 0) {
257 1.1 jtc bi_errorf("-p: %s", emsg);
258 1.1 jtc return 1;
259 1.1 jtc }
260 1.1 jtc break;
261 1.1 jtc #endif /* KSH */
262 1.1 jtc case 'r':
263 1.1 jtc expand = 0;
264 1.1 jtc break;
265 1.1 jtc case 's':
266 1.1 jtc history = 1;
267 1.1 jtc break;
268 1.1 jtc case 'u':
269 1.1 jtc if (!*(cp = builtin_opt.optarg))
270 1.1 jtc fd = 0;
271 1.1 jtc else if ((fd = check_fd(cp, R_OK, &emsg)) < 0) {
272 1.1 jtc bi_errorf("-u: %s: %s", cp, emsg);
273 1.1 jtc return 1;
274 1.1 jtc }
275 1.1 jtc break;
276 1.1 jtc case '?':
277 1.1 jtc return 1;
278 1.1 jtc }
279 1.1 jtc wp += builtin_opt.optind;
280 1.1 jtc
281 1.1 jtc if (*wp == NULL)
282 1.1 jtc *--wp = "REPLY";
283 1.1 jtc
284 1.1 jtc /* Since we can't necessarily seek backwards on non-regular files,
285 1.1 jtc * don't buffer them so we can't read too much.
286 1.1 jtc */
287 1.1 jtc shf = shf_reopen(fd, SHF_RD | SHF_INTERRUPT | can_seek(fd), shl_spare);
288 1.1 jtc
289 1.1 jtc if ((cp = strchr(*wp, '?')) != NULL) {
290 1.1 jtc *cp = 0;
291 1.1.1.2 jtc if (isatty(fd)) {
292 1.1.1.2 jtc /* at&t ksh says it prints prompt on fd if it's open
293 1.1 jtc * for writing and is a tty, but it doesn't do it
294 1.1.1.2 jtc * (it also doesn't check the interactive flag,
295 1.1.1.2 jtc * as is indicated in the Kornshell book).
296 1.1 jtc */
297 1.1 jtc shellf("%s", cp+1);
298 1.1 jtc }
299 1.1 jtc }
300 1.1 jtc
301 1.1 jtc #ifdef KSH
302 1.1 jtc /* If we are reading from the co-process for the first time,
303 1.1 jtc * make sure the other side of the pipe is closed first. This allows
304 1.1 jtc * the detection of eof.
305 1.1 jtc *
306 1.1 jtc * This is not compatiable with at&t ksh... the fd is kept so another
307 1.1 jtc * coproc can be started with same ouput, however, this means eof
308 1.1 jtc * can't be detected... This is why it is closed here.
309 1.1 jtc * If this call is removed, remove the eof check below, too.
310 1.1 jtc * coproc_readw_close(fd);
311 1.1 jtc */
312 1.1 jtc #endif /* KSH */
313 1.1 jtc
314 1.1 jtc if (history)
315 1.1 jtc Xinit(xs, xp, 128, ATEMP);
316 1.1 jtc expanding = 0;
317 1.1 jtc Xinit(cs, cp, 128, ATEMP);
318 1.1 jtc for (; *wp != NULL; wp++) {
319 1.1 jtc for (cp = Xstring(cs, cp); ; ) {
320 1.1 jtc if (c == '\n' || c == EOF)
321 1.1 jtc break;
322 1.1 jtc while (1) {
323 1.1 jtc c = shf_getc(shf);
324 1.1 jtc if (c == '\0'
325 1.1 jtc #ifdef OS2
326 1.1 jtc || c == '\r'
327 1.1 jtc #endif /* OS2 */
328 1.1 jtc )
329 1.1 jtc continue;
330 1.1 jtc if (c == EOF && shf_error(shf)
331 1.1 jtc && shf_errno(shf) == EINTR)
332 1.1 jtc {
333 1.1 jtc /* Was the offending signal one that
334 1.1 jtc * would normally kill a process?
335 1.1 jtc * If so, pretend the read was killed.
336 1.1 jtc */
337 1.1 jtc ecode = fatal_trap_check();
338 1.1 jtc
339 1.1 jtc /* non fatal (eg, CHLD), carry on */
340 1.1 jtc if (!ecode) {
341 1.1 jtc shf_clearerr(shf);
342 1.1 jtc continue;
343 1.1 jtc }
344 1.1 jtc }
345 1.1 jtc break;
346 1.1 jtc }
347 1.1 jtc if (history) {
348 1.1 jtc Xcheck(xs, xp);
349 1.1 jtc Xput(xs, xp, c);
350 1.1 jtc }
351 1.1 jtc Xcheck(cs, cp);
352 1.1 jtc if (expanding) {
353 1.1 jtc expanding = 0;
354 1.1 jtc if (c == '\n') {
355 1.1 jtc c = 0;
356 1.1.1.3 hubertf if (Flag(FTALKING_I) && isatty(fd)) {
357 1.1 jtc /* set prompt in case this is
358 1.1 jtc * called from .profile or $ENV
359 1.1 jtc */
360 1.1 jtc set_prompt(PS2, (Source *) 0);
361 1.1 jtc pprompt(prompt, 0);
362 1.1 jtc }
363 1.1 jtc } else if (c != EOF)
364 1.1 jtc Xput(cs, cp, c);
365 1.1 jtc continue;
366 1.1 jtc }
367 1.1 jtc if (expand && c == '\\') {
368 1.1 jtc expanding = 1;
369 1.1 jtc continue;
370 1.1 jtc }
371 1.1 jtc if (c == '\n' || c == EOF)
372 1.1 jtc break;
373 1.1 jtc if (ctype(c, C_IFS)) {
374 1.1 jtc if (Xlength(cs, cp) == 0 && ctype(c, C_IFSWS))
375 1.1 jtc continue;
376 1.1 jtc if (wp[1])
377 1.1 jtc break;
378 1.1 jtc }
379 1.1 jtc Xput(cs, cp, c);
380 1.1 jtc }
381 1.1 jtc /* strip trailing IFS white space from last variable */
382 1.1 jtc if (!wp[1])
383 1.1 jtc while (Xlength(cs, cp) && ctype(cp[-1], C_IFS)
384 1.1 jtc && ctype(cp[-1], C_IFSWS))
385 1.1 jtc cp--;
386 1.1 jtc Xput(cs, cp, '\0');
387 1.1 jtc vp = global(*wp);
388 1.1.1.3 hubertf /* Must be done before setting export. */
389 1.1 jtc if (vp->flag & RDONLY) {
390 1.1 jtc shf_flush(shf);
391 1.1 jtc bi_errorf("%s is read only", *wp);
392 1.1 jtc return 1;
393 1.1 jtc }
394 1.1 jtc if (Flag(FEXPORT))
395 1.1 jtc typeset(*wp, EXPORT, 0, 0, 0);
396 1.1.1.3 hubertf if (!setstr(vp, Xstring(cs, cp), KSH_RETURN_ERROR)) {
397 1.1.1.3 hubertf shf_flush(shf);
398 1.1.1.3 hubertf return 1;
399 1.1.1.3 hubertf }
400 1.1 jtc }
401 1.1 jtc
402 1.1 jtc shf_flush(shf);
403 1.1 jtc if (history) {
404 1.1 jtc Xput(xs, xp, '\0');
405 1.1 jtc source->line++;
406 1.1 jtc histsave(source->line, Xstring(xs, xp), 1);
407 1.1 jtc Xfree(xs, xp);
408 1.1 jtc }
409 1.1 jtc #ifdef KSH
410 1.1 jtc /* if this is the co-process fd, close the file descriptor
411 1.1 jtc * (can get eof if and only if all processes are have died, ie,
412 1.1 jtc * coproc.njobs is 0 and the pipe is closed).
413 1.1 jtc */
414 1.1 jtc if (c == EOF && !ecode)
415 1.1 jtc coproc_read_close(fd);
416 1.1 jtc #endif /* KSH */
417 1.1 jtc
418 1.1 jtc return ecode ? ecode : c == EOF;
419 1.1 jtc }
420 1.1 jtc
421 1.1 jtc int
422 1.1 jtc c_eval(wp)
423 1.1 jtc char **wp;
424 1.1 jtc {
425 1.1 jtc register struct source *s;
426 1.1 jtc
427 1.1 jtc if (ksh_getopt(wp, &builtin_opt, null) == '?')
428 1.1 jtc return 1;
429 1.1 jtc s = pushs(SWORDS, ATEMP);
430 1.1 jtc s->u.strv = wp + builtin_opt.optind;
431 1.1.1.3 hubertf if (!Flag(FPOSIX)) {
432 1.1.1.3 hubertf /*
433 1.1.1.3 hubertf * Handle case where the command is empty due to failed
434 1.1.1.3 hubertf * command substitution, eg, eval "$(false)".
435 1.1.1.3 hubertf * In this case, shell() will not set/change exstat (because
436 1.1.1.3 hubertf * compiled tree is empty), so will use this value.
437 1.1.1.3 hubertf * subst_exstat is cleared in execute(), so should be 0 if
438 1.1.1.3 hubertf * there were no substitutions.
439 1.1.1.3 hubertf *
440 1.1.1.3 hubertf * A strict reading of POSIX says we don't do this (though
441 1.1.1.3 hubertf * it is traditionally done). [from 1003.2-1992]
442 1.1.1.3 hubertf * 3.9.1: Simple Commands
443 1.1.1.3 hubertf * ... If there is a command name, execution shall
444 1.1.1.3 hubertf * continue as described in 3.9.1.1. If there
445 1.1.1.3 hubertf * is no command name, but the command contained a command
446 1.1.1.3 hubertf * substitution, the command shall complete with the exit
447 1.1.1.3 hubertf * status of the last command substitution
448 1.1.1.3 hubertf * 3.9.1.1: Command Search and Execution
449 1.1.1.3 hubertf * ...(1)...(a) If the command name matches the name of
450 1.1.1.3 hubertf * a special built-in utility, that special built-in
451 1.1.1.3 hubertf * utility shall be invoked.
452 1.1.1.3 hubertf * 3.14.5: Eval
453 1.1.1.3 hubertf * ... If there are no arguments, or only null arguments,
454 1.1.1.3 hubertf * eval shall return an exit status of zero.
455 1.1.1.3 hubertf */
456 1.1.1.3 hubertf exstat = subst_exstat;
457 1.1.1.3 hubertf }
458 1.1.1.3 hubertf
459 1.1 jtc return shell(s, FALSE);
460 1.1 jtc }
461 1.1 jtc
462 1.1 jtc int
463 1.1 jtc c_trap(wp)
464 1.1 jtc char **wp;
465 1.1 jtc {
466 1.1 jtc int i;
467 1.1 jtc char *s;
468 1.1 jtc register Trap *p;
469 1.1 jtc
470 1.1 jtc if (ksh_getopt(wp, &builtin_opt, null) == '?')
471 1.1 jtc return 1;
472 1.1 jtc wp += builtin_opt.optind;
473 1.1 jtc
474 1.1 jtc if (*wp == NULL) {
475 1.1 jtc int anydfl = 0;
476 1.1 jtc
477 1.1 jtc for (p = sigtraps, i = SIGNALS+1; --i >= 0; p++) {
478 1.1 jtc if (p->trap == NULL)
479 1.1 jtc anydfl = 1;
480 1.1 jtc else {
481 1.1 jtc shprintf("trap -- ");
482 1.1 jtc print_value_quoted(p->trap);
483 1.1 jtc shprintf(" %s\n", p->name);
484 1.1 jtc }
485 1.1 jtc }
486 1.1 jtc #if 0 /* this is ugly and not clear POSIX needs it */
487 1.1 jtc /* POSIX may need this so output of trap can be saved and
488 1.1 jtc * used to restore trap conditions
489 1.1 jtc */
490 1.1 jtc if (anydfl) {
491 1.1 jtc shprintf("trap -- -");
492 1.1 jtc for (p = sigtraps, i = SIGNALS+1; --i >= 0; p++)
493 1.1 jtc if (p->trap == NULL && p->name)
494 1.1 jtc shprintf(" %s", p->name);
495 1.1 jtc shprintf(newline);
496 1.1 jtc }
497 1.1 jtc #endif
498 1.1 jtc return 0;
499 1.1 jtc }
500 1.1 jtc
501 1.1.1.3 hubertf /*
502 1.1.1.3 hubertf * Use case sensitive lookup for first arg so the
503 1.1.1.3 hubertf * command 'exit' isn't confused with the pseudo-signal
504 1.1.1.3 hubertf * 'EXIT'.
505 1.1.1.3 hubertf */
506 1.1.1.3 hubertf s = (gettrap(*wp, FALSE) == NULL) ? *wp++ : NULL; /* get command */
507 1.1 jtc if (s != NULL && s[0] == '-' && s[1] == '\0')
508 1.1 jtc s = NULL;
509 1.1 jtc
510 1.1 jtc /* set/clear traps */
511 1.1 jtc while (*wp != NULL) {
512 1.1.1.3 hubertf p = gettrap(*wp++, TRUE);
513 1.1 jtc if (p == NULL) {
514 1.1 jtc bi_errorf("bad signal %s", wp[-1]);
515 1.1 jtc return 1;
516 1.1 jtc }
517 1.1 jtc settrap(p, s);
518 1.1 jtc }
519 1.1 jtc return 0;
520 1.1 jtc }
521 1.1 jtc
522 1.1 jtc int
523 1.1 jtc c_exitreturn(wp)
524 1.1 jtc char **wp;
525 1.1 jtc {
526 1.1 jtc int how = LEXIT;
527 1.1.1.3 hubertf int n;
528 1.1 jtc char *arg;
529 1.1 jtc
530 1.1 jtc if (ksh_getopt(wp, &builtin_opt, null) == '?')
531 1.1 jtc return 1;
532 1.1 jtc arg = wp[builtin_opt.optind];
533 1.1 jtc
534 1.1.1.3 hubertf if (arg) {
535 1.1.1.3 hubertf if (!getn(arg, &n)) {
536 1.1.1.3 hubertf exstat = 1;
537 1.1.1.3 hubertf warningf(TRUE, "%s: bad number", arg);
538 1.1.1.3 hubertf } else
539 1.1.1.3 hubertf exstat = n;
540 1.1 jtc }
541 1.1 jtc if (wp[0][0] == 'r') { /* return */
542 1.1 jtc struct env *ep;
543 1.1 jtc
544 1.1 jtc /* need to tell if this is exit or return so trap exit will
545 1.1 jtc * work right (POSIX)
546 1.1 jtc */
547 1.1 jtc for (ep = e; ep; ep = ep->oenv)
548 1.1 jtc if (STOP_RETURN(ep->type)) {
549 1.1 jtc how = LRETURN;
550 1.1 jtc break;
551 1.1 jtc }
552 1.1 jtc }
553 1.1 jtc
554 1.1 jtc if (how == LEXIT && !really_exit && j_stopped_running()) {
555 1.1 jtc really_exit = 1;
556 1.1 jtc how = LSHELL;
557 1.1 jtc }
558 1.1 jtc
559 1.1 jtc quitenv(); /* get rid of any i/o redirections */
560 1.1 jtc unwind(how);
561 1.1 jtc /*NOTREACHED*/
562 1.1 jtc return 0;
563 1.1 jtc }
564 1.1 jtc
565 1.1 jtc int
566 1.1 jtc c_brkcont(wp)
567 1.1 jtc char **wp;
568 1.1 jtc {
569 1.1 jtc int n, quit;
570 1.1 jtc struct env *ep, *last_ep = (struct env *) 0;
571 1.1 jtc char *arg;
572 1.1 jtc
573 1.1 jtc if (ksh_getopt(wp, &builtin_opt, null) == '?')
574 1.1 jtc return 1;
575 1.1 jtc arg = wp[builtin_opt.optind];
576 1.1 jtc
577 1.1 jtc if (!arg)
578 1.1 jtc n = 1;
579 1.1 jtc else if (!bi_getn(arg, &n))
580 1.1 jtc return 1;
581 1.1 jtc quit = n;
582 1.1 jtc if (quit <= 0) {
583 1.1 jtc /* at&t ksh does this for non-interactive shells only - weird */
584 1.1 jtc bi_errorf("%s: bad value", arg);
585 1.1 jtc return 1;
586 1.1 jtc }
587 1.1 jtc
588 1.1 jtc /* Stop at E_NONE, E_PARSE, E_FUNC, or E_INCL */
589 1.1 jtc for (ep = e; ep && !STOP_BRKCONT(ep->type); ep = ep->oenv)
590 1.1 jtc if (ep->type == E_LOOP) {
591 1.1 jtc if (--quit == 0)
592 1.1 jtc break;
593 1.1 jtc ep->flags |= EF_BRKCONT_PASS;
594 1.1 jtc last_ep = ep;
595 1.1 jtc }
596 1.1 jtc
597 1.1 jtc if (quit) {
598 1.1 jtc /* at&t ksh doesn't print a message - just does what it
599 1.1 jtc * can. We print a message 'cause it helps in debugging
600 1.1 jtc * scripts, but don't generate an error (ie, keep going).
601 1.1 jtc */
602 1.1 jtc if (n == quit) {
603 1.1 jtc warningf(TRUE, "%s: cannot %s", wp[0], wp[0]);
604 1.1 jtc return 0;
605 1.1 jtc }
606 1.1 jtc /* POSIX says if n is too big, the last enclosing loop
607 1.1 jtc * shall be used. Doesn't say to print an error but we
608 1.1 jtc * do anyway 'cause the user messed up.
609 1.1 jtc */
610 1.1 jtc last_ep->flags &= ~EF_BRKCONT_PASS;
611 1.1 jtc warningf(TRUE, "%s: can only %s %d level(s)",
612 1.1 jtc wp[0], wp[0], n - quit);
613 1.1 jtc }
614 1.1 jtc
615 1.1 jtc unwind(*wp[0] == 'b' ? LBREAK : LCONTIN);
616 1.1 jtc /*NOTREACHED*/
617 1.1 jtc }
618 1.1 jtc
619 1.1 jtc int
620 1.1 jtc c_set(wp)
621 1.1 jtc char **wp;
622 1.1 jtc {
623 1.1 jtc int argi, setargs;
624 1.1 jtc struct block *l = e->loc;
625 1.1 jtc register char **owp = wp;
626 1.1 jtc
627 1.1 jtc if (wp[1] == NULL) {
628 1.1 jtc static const char *const args [] = { "set", "-", NULL };
629 1.1 jtc return c_typeset((char **) args);
630 1.1 jtc }
631 1.1 jtc
632 1.1 jtc argi = parse_args(wp, OF_SET, &setargs);
633 1.1 jtc if (argi < 0)
634 1.1 jtc return 1;
635 1.1 jtc /* set $# and $* */
636 1.1 jtc if (setargs) {
637 1.1 jtc owp = wp += argi - 1;
638 1.1 jtc wp[0] = l->argv[0]; /* save $0 */
639 1.1 jtc while (*++wp != NULL)
640 1.1 jtc *wp = str_save(*wp, &l->area);
641 1.1 jtc l->argc = wp - owp - 1;
642 1.1 jtc l->argv = (char **) alloc(sizeofN(char *, l->argc+2), &l->area);
643 1.1 jtc for (wp = l->argv; (*wp++ = *owp++) != NULL; )
644 1.1 jtc ;
645 1.1 jtc }
646 1.1 jtc /* POSIX says set exit status is 0, but old scripts that use
647 1.1 jtc * getopt(1), use the construct: set -- `getopt ab:c "$@"`
648 1.1 jtc * which assumes the exit value set will be that of the ``
649 1.1 jtc * (subst_exstat is cleared in execute() so that it will be 0
650 1.1 jtc * if there are no command substitutions).
651 1.1 jtc */
652 1.1 jtc return Flag(FPOSIX) ? 0 : subst_exstat;
653 1.1 jtc }
654 1.1 jtc
655 1.1 jtc int
656 1.1 jtc c_unset(wp)
657 1.1 jtc char **wp;
658 1.1 jtc {
659 1.1 jtc register char *id;
660 1.1 jtc int optc, unset_var = 1;
661 1.1 jtc int ret = 0;
662 1.1 jtc
663 1.1 jtc while ((optc = ksh_getopt(wp, &builtin_opt, "fv")) != EOF)
664 1.1 jtc switch (optc) {
665 1.1 jtc case 'f':
666 1.1 jtc unset_var = 0;
667 1.1 jtc break;
668 1.1 jtc case 'v':
669 1.1 jtc unset_var = 1;
670 1.1 jtc break;
671 1.1 jtc case '?':
672 1.1 jtc return 1;
673 1.1 jtc }
674 1.1 jtc wp += builtin_opt.optind;
675 1.1 jtc for (; (id = *wp) != NULL; wp++)
676 1.1 jtc if (unset_var) { /* unset variable */
677 1.1 jtc struct tbl *vp = global(id);
678 1.1 jtc
679 1.1 jtc if (!(vp->flag & ISSET))
680 1.1 jtc ret = 1;
681 1.1 jtc if ((vp->flag&RDONLY)) {
682 1.1 jtc bi_errorf("%s is read only", vp->name);
683 1.1 jtc return 1;
684 1.1 jtc }
685 1.1 jtc unset(vp, strchr(id, '[') ? 1 : 0);
686 1.1 jtc } else { /* unset function */
687 1.1 jtc if (define(id, (struct op *) NULL))
688 1.1 jtc ret = 1;
689 1.1 jtc }
690 1.1 jtc return ret;
691 1.1 jtc }
692 1.1 jtc
693 1.1 jtc int
694 1.1 jtc c_times(wp)
695 1.1 jtc char **wp;
696 1.1 jtc {
697 1.1 jtc struct tms all;
698 1.1 jtc
699 1.1 jtc (void) ksh_times(&all);
700 1.1.1.3 hubertf shprintf("Shell: %8ss user ", clocktos(all.tms_utime));
701 1.1.1.3 hubertf shprintf("%8ss system\n", clocktos(all.tms_stime));
702 1.1.1.3 hubertf shprintf("Kids: %8ss user ", clocktos(all.tms_cutime));
703 1.1.1.3 hubertf shprintf("%8ss system\n", clocktos(all.tms_cstime));
704 1.1 jtc
705 1.1 jtc return 0;
706 1.1 jtc }
707 1.1 jtc
708 1.1 jtc /*
709 1.1 jtc * time pipeline (really a statement, not a built-in command)
710 1.1 jtc */
711 1.1 jtc int
712 1.1 jtc timex(t, f)
713 1.1 jtc struct op *t;
714 1.1 jtc int f;
715 1.1 jtc {
716 1.1.1.3 hubertf #define TF_NOARGS BIT(0)
717 1.1.1.3 hubertf #define TF_NOREAL BIT(1) /* don't report real time */
718 1.1.1.3 hubertf #define TF_POSIX BIT(2) /* report in posix format */
719 1.1.1.3 hubertf int rv = 0;
720 1.1.1.3 hubertf struct tms t0, t1, tms;
721 1.1.1.3 hubertf clock_t t0t, t1t = 0;
722 1.1.1.3 hubertf int tf = 0;
723 1.1 jtc extern clock_t j_usrtime, j_systime; /* computed by j_wait */
724 1.1.1.3 hubertf char opts[1];
725 1.1 jtc
726 1.1 jtc t0t = ksh_times(&t0);
727 1.1.1.3 hubertf if (t->left) {
728 1.1.1.3 hubertf /*
729 1.1.1.3 hubertf * Two ways of getting cpu usage of a command: just use t0
730 1.1.1.3 hubertf * and t1 (which will get cpu usage from other jobs that
731 1.1.1.3 hubertf * finish while we are executing t->left), or get the
732 1.1.1.3 hubertf * cpu usage of t->left. at&t ksh does the former, while
733 1.1.1.3 hubertf * pdksh tries to do the later (the j_usrtime hack doesn't
734 1.1.1.3 hubertf * really work as it only counts the last job).
735 1.1.1.3 hubertf */
736 1.1.1.3 hubertf j_usrtime = j_systime = 0;
737 1.1.1.3 hubertf if (t->left->type == TCOM)
738 1.1.1.3 hubertf t->left->str = opts;
739 1.1.1.3 hubertf opts[0] = 0;
740 1.1.1.3 hubertf rv = execute(t->left, f | XTIME);
741 1.1.1.3 hubertf tf |= opts[0];
742 1.1.1.3 hubertf t1t = ksh_times(&t1);
743 1.1.1.3 hubertf } else
744 1.1.1.3 hubertf tf = TF_NOARGS;
745 1.1 jtc
746 1.1.1.3 hubertf if (tf & TF_NOARGS) { /* ksh93 - report shell times (shell+kids) */
747 1.1.1.3 hubertf tf |= TF_NOREAL;
748 1.1.1.3 hubertf tms.tms_utime = t0.tms_utime + t0.tms_cutime;
749 1.1.1.3 hubertf tms.tms_stime = t0.tms_stime + t0.tms_cstime;
750 1.1.1.3 hubertf } else {
751 1.1.1.3 hubertf tms.tms_utime = t1.tms_utime - t0.tms_utime + j_usrtime;
752 1.1.1.3 hubertf tms.tms_stime = t1.tms_stime - t0.tms_stime + j_systime;
753 1.1.1.3 hubertf }
754 1.1.1.3 hubertf
755 1.1.1.3 hubertf if (!(tf & TF_NOREAL))
756 1.1.1.3 hubertf shf_fprintf(shl_out,
757 1.1.1.3 hubertf tf & TF_POSIX ? "real %8s\n" : "%8ss real ",
758 1.1.1.3 hubertf clocktos(t1t - t0t));
759 1.1.1.3 hubertf shf_fprintf(shl_out, tf & TF_POSIX ? "user %8s\n" : "%8ss user ",
760 1.1.1.3 hubertf clocktos(tms.tms_utime));
761 1.1.1.3 hubertf shf_fprintf(shl_out, tf & TF_POSIX ? "sys %8s\n" : "%8ss system\n",
762 1.1.1.3 hubertf clocktos(tms.tms_stime));
763 1.1.1.3 hubertf shf_flush(shl_out);
764 1.1 jtc
765 1.1 jtc return rv;
766 1.1 jtc }
767 1.1 jtc
768 1.1.1.3 hubertf void
769 1.1.1.3 hubertf timex_hook(t, app)
770 1.1.1.3 hubertf struct op *t;
771 1.1.1.3 hubertf char ** volatile *app;
772 1.1.1.3 hubertf {
773 1.1.1.3 hubertf char **wp = *app;
774 1.1.1.3 hubertf int optc;
775 1.1.1.3 hubertf int i, j;
776 1.1.1.3 hubertf Getopt opt;
777 1.1.1.3 hubertf
778 1.1.1.3 hubertf ksh_getopt_reset(&opt, 0);
779 1.1.1.3 hubertf opt.optind = 0; /* start at the start */
780 1.1.1.3 hubertf while ((optc = ksh_getopt(wp, &opt, ":p")) != EOF)
781 1.1.1.3 hubertf switch (optc) {
782 1.1.1.3 hubertf case 'p':
783 1.1.1.3 hubertf t->str[0] |= TF_POSIX;
784 1.1.1.3 hubertf break;
785 1.1.1.3 hubertf case '?':
786 1.1.1.3 hubertf errorf("time: -%s unknown option", opt.optarg);
787 1.1.1.3 hubertf case ':':
788 1.1.1.3 hubertf errorf("time: -%s requires an argument",
789 1.1.1.3 hubertf opt.optarg);
790 1.1.1.3 hubertf }
791 1.1.1.3 hubertf /* Copy command words down over options. */
792 1.1.1.3 hubertf if (opt.optind != 0) {
793 1.1.1.3 hubertf for (i = 0; i < opt.optind; i++)
794 1.1.1.3 hubertf afree(wp[i], ATEMP);
795 1.1.1.3 hubertf for (i = 0, j = opt.optind; (wp[i] = wp[j]); i++, j++)
796 1.1.1.3 hubertf ;
797 1.1.1.3 hubertf }
798 1.1.1.3 hubertf if (!wp[0])
799 1.1.1.3 hubertf t->str[0] |= TF_NOARGS;
800 1.1.1.3 hubertf *app = wp;
801 1.1.1.3 hubertf }
802 1.1.1.3 hubertf
803 1.1 jtc static char *
804 1.1 jtc clocktos(t)
805 1.1 jtc clock_t t;
806 1.1 jtc {
807 1.1.1.3 hubertf static char temp[22]; /* enough for 64 bit clock_t */
808 1.1 jtc register int i;
809 1.1 jtc register char *cp = temp + sizeof(temp);
810 1.1 jtc
811 1.1.1.3 hubertf /* note: posix says must use max precision, ie, if clk_tck is
812 1.1.1.3 hubertf * 1000, must print 3 places after decimal (if non-zero, else 1).
813 1.1.1.3 hubertf */
814 1.1 jtc if (CLK_TCK != 100) /* convert to 1/100'ths */
815 1.1 jtc t = (t < 1000000000/CLK_TCK) ?
816 1.1 jtc (t * 100) / CLK_TCK : (t / CLK_TCK) * 100;
817 1.1 jtc
818 1.1 jtc *--cp = '\0';
819 1.1 jtc for (i = -2; i <= 0 || t > 0; i++) {
820 1.1 jtc if (i == 0)
821 1.1 jtc *--cp = '.';
822 1.1 jtc *--cp = '0' + (char)(t%10);
823 1.1 jtc t /= 10;
824 1.1 jtc }
825 1.1 jtc return cp;
826 1.1 jtc }
827 1.1 jtc
828 1.1 jtc /* exec with no args - args case is taken care of in comexec() */
829 1.1 jtc int
830 1.1 jtc c_exec(wp)
831 1.1 jtc char ** wp;
832 1.1 jtc {
833 1.1 jtc int i;
834 1.1 jtc
835 1.1 jtc /* make sure redirects stay in place */
836 1.1 jtc if (e->savefd != NULL) {
837 1.1 jtc for (i = 0; i < NUFILE; i++) {
838 1.1 jtc if (e->savefd[i] > 0)
839 1.1 jtc close(e->savefd[i]);
840 1.1.1.3 hubertf /*
841 1.1.1.3 hubertf * For ksh keep anything > 2 private,
842 1.1.1.3 hubertf * for sh, let them be (POSIX says what
843 1.1.1.3 hubertf * happens is unspecified and the bourne shell
844 1.1.1.3 hubertf * keeps them open).
845 1.1.1.3 hubertf */
846 1.1.1.3 hubertf #ifdef KSH
847 1.1 jtc if (i > 2 && e->savefd[i])
848 1.1 jtc fd_clexec(i);
849 1.1.1.3 hubertf #endif /* KSH */
850 1.1 jtc }
851 1.1 jtc e->savefd = NULL;
852 1.1 jtc }
853 1.1 jtc return 0;
854 1.1 jtc }
855 1.1 jtc
856 1.1 jtc /* dummy function, special case in comexec() */
857 1.1 jtc int
858 1.1 jtc c_builtin(wp)
859 1.1 jtc char ** wp;
860 1.1 jtc {
861 1.1 jtc return 0;
862 1.1 jtc }
863 1.1 jtc
864 1.1 jtc extern int c_test ARGS((char **wp)); /* in c_test.c */
865 1.1 jtc extern int c_ulimit ARGS((char **wp)); /* in c_ulimit.c */
866 1.1 jtc
867 1.1 jtc /* A leading = means assignments before command are kept;
868 1.1 jtc * a leading * means a POSIX special builtin;
869 1.1 jtc * a leading + means a POSIX regular builtin
870 1.1 jtc * (* and + should not be combined).
871 1.1 jtc */
872 1.1 jtc const struct builtin shbuiltins [] = {
873 1.1 jtc {"*=.", c_dot},
874 1.1 jtc {"*=:", c_label},
875 1.1 jtc {"[", c_test},
876 1.1 jtc {"*=break", c_brkcont},
877 1.1 jtc {"=builtin", c_builtin},
878 1.1 jtc {"*=continue", c_brkcont},
879 1.1 jtc {"*=eval", c_eval},
880 1.1 jtc {"*=exec", c_exec},
881 1.1 jtc {"*=exit", c_exitreturn},
882 1.1 jtc {"+false", c_label},
883 1.1 jtc {"*=return", c_exitreturn},
884 1.1 jtc {"*=set", c_set},
885 1.1 jtc {"*=shift", c_shift},
886 1.1 jtc {"=times", c_times},
887 1.1 jtc {"*=trap", c_trap},
888 1.1 jtc {"+=wait", c_wait},
889 1.1 jtc {"+read", c_read},
890 1.1 jtc {"test", c_test},
891 1.1 jtc {"+true", c_label},
892 1.1 jtc {"ulimit", c_ulimit},
893 1.1 jtc {"+umask", c_umask},
894 1.1 jtc {"*=unset", c_unset},
895 1.1 jtc #ifdef OS2
896 1.1 jtc /* In OS2, the first line of a file can be "extproc name", which
897 1.1 jtc * tells the command interpreter (cmd.exe) to use name to execute
898 1.1 jtc * the file. For this to be useful, ksh must ignore commands
899 1.1 jtc * starting with extproc and this does the trick...
900 1.1 jtc */
901 1.1 jtc {"extproc", c_label},
902 1.1 jtc #endif /* OS2 */
903 1.1 jtc {NULL, NULL}
904 1.1 jtc };
905