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