1 /* $NetBSD: c_sh.c,v 1.12 2006/04/01 23:39:58 christos Exp $ */
4 * built-in Bourne commands
9 __RCSID("$NetBSD: c_sh.c,v 1.12 2006/04/01 23:39:58 christos Exp $");
14 #include "ksh_stat.h" /* umask() */
16 #include "ksh_times.h"
18 static char *clocktos
ARGS((clock_t t
));
21 /* :, false and true */
26 return wp
[0][0] == 'f' ? 1 : 0;
33 register struct block
*l
= e
->loc
;
38 if (ksh_getopt(wp
, &builtin_opt
, null
) == '?')
40 arg
= wp
[builtin_opt
.optind
];
43 evaluate(arg
, &val
, KSH_UNWIND_ERROR
);
48 bi_errorf("%s: bad number", arg
);
52 bi_errorf("nothing to shift");
55 l
->argv
[n
] = l
->argv
[0];
71 while ((optc
= ksh_getopt(wp
, &builtin_opt
, "S")) != EOF
)
79 cp
= wp
[builtin_opt
.optind
];
87 old_umask
= ~old_umask
;
89 for (i
= 0; i
< 3; i
++) {
92 for (j
= 0; j
< 3; j
++)
93 if (old_umask
& (1 << (8 - (3*i
+ j
))))
98 shprintf("%s\n", buf
);
100 shprintf("%#3.3o\n", old_umask
);
105 for (new_umask
= 0; *cp
>= '0' && *cp
<= '7'; cp
++)
106 new_umask
= new_umask
* 8 + (*cp
- '0');
108 bi_errorf("bad number");
112 /* symbolic format */
113 int positions
, new_val
;
116 old_umask
= umask(0);
117 umask(old_umask
); /* in case of error */
118 old_umask
= ~old_umask
;
119 new_umask
= old_umask
;
122 while (*cp
&& strchr("augo", *cp
))
124 case 'a': positions
|= 0111; break;
125 case 'u': positions
|= 0100; break;
126 case 'g': positions
|= 0010; break;
127 case 'o': positions
|= 0001; break;
130 positions
= 0111; /* default is a */
131 if (!strchr("=+-", op
= *cp
))
135 while (*cp
&& strchr("rwxugoXs", *cp
))
137 case 'r': new_val
|= 04; break;
138 case 'w': new_val
|= 02; break;
139 case 'x': new_val
|= 01; break;
140 case 'u': new_val
|= old_umask
>> 6;
142 case 'g': new_val
|= old_umask
>> 3;
144 case 'o': new_val
|= old_umask
>> 0;
146 case 'X': if (old_umask
& 0111)
149 case 's': /* ignored */
152 new_val
= (new_val
& 07) * positions
;
155 new_umask
&= ~new_val
;
159 | (new_umask
& ~(positions
* 07));
162 new_umask
|= new_val
;
167 } else if (!strchr("=+-", *cp
))
171 bi_errorf("bad mask");
174 new_umask
= ~new_umask
;
191 if (ksh_getopt(wp
, &builtin_opt
, null
) == '?')
194 if ((cp
= wp
[builtin_opt
.optind
]) == NULL
)
196 file
= search(cp
, path
, R_OK
, &err
);
198 bi_errorf("%s: %s", cp
, err
? strerror(err
) : "not found");
202 /* Set positional parameters? */
203 if (wp
[builtin_opt
.optind
+ 1]) {
204 argv
= wp
+ builtin_opt
.optind
;
205 argv
[0] = e
->loc
->argv
[0]; /* preserve $0 */
206 for (argc
= 0; argv
[argc
+ 1]; argc
++)
212 i
= include(file
, argc
, argv
, 0);
213 if (i
< 0) { /* should not happen */
214 bi_errorf("%s: %s", cp
, strerror(errno
));
224 int UNINITIALIZED(rv
);
227 if (ksh_getopt(wp
, &builtin_opt
, null
) == '?')
229 wp
+= builtin_opt
.optind
;
230 if (*wp
== (char *) 0) {
231 while (waitfor((char *) 0, &sig
) >= 0)
236 rv
= waitfor(*wp
, &sig
);
238 rv
= sig
? sig
: 127; /* magic exit code: bad job-id */
248 int expandv
= 1, history
= 0;
258 char UNINITIALIZED(*xp
);
259 static char REPLY
[] = "REPLY";
261 while ((optc
= ksh_getopt(wp
, &builtin_opt
, "prsu,")) != EOF
)
265 if ((fd
= coproc_getfd(R_OK
, &emsg
)) < 0) {
266 bi_errorf("-p: %s", emsg
);
278 if (!*(cp
= builtin_opt
.optarg
))
280 else if ((fd
= check_fd(cp
, R_OK
, &emsg
)) < 0) {
281 bi_errorf("-u: %s: %s", cp
, emsg
);
288 wp
+= builtin_opt
.optind
;
293 /* Since we can't necessarily seek backwards on non-regular files,
294 * don't buffer them so we can't read too much.
296 shf
= shf_reopen(fd
, SHF_RD
| SHF_INTERRUPT
| can_seek(fd
), shl_spare
);
298 if ((cp
= strchr(*wp
, '?')) != NULL
) {
301 /* at&t ksh says it prints prompt on fd if it's open
302 * for writing and is a tty, but it doesn't do it
303 * (it also doesn't check the interactive flag,
304 * as is indicated in the Kornshell book).
311 /* If we are reading from the co-process for the first time,
312 * make sure the other side of the pipe is closed first. This allows
313 * the detection of eof.
315 * This is not compatible with at&t ksh... the fd is kept so another
316 * coproc can be started with same output, however, this means eof
317 * can't be detected... This is why it is closed here.
318 * If this call is removed, remove the eof check below, too.
319 * coproc_readw_close(fd);
324 Xinit(xs
, xp
, 128, ATEMP
);
326 Xinit(cs
, cp
, 128, ATEMP
);
327 for (; *wp
!= NULL
; wp
++) {
328 for (cp
= Xstring(cs
, cp
); ; ) {
329 if (c
== '\n' || c
== EOF
)
339 if (c
== EOF
&& shf_error(shf
)
340 && shf_errno(shf
) == EINTR
)
342 /* Was the offending signal one that
343 * would normally kill a process?
344 * If so, pretend the read was killed.
346 ecode
= fatal_trap_check();
348 /* non fatal (eg, CHLD), carry on */
365 if (Flag(FTALKING_I
) && isatty(fd
)) {
366 /* set prompt in case this is
367 * called from .profile or $ENV
369 set_prompt(PS2
, (Source
*) 0);
376 if (expandv
&& c
== '\\') {
380 if (c
== '\n' || c
== EOF
)
382 if (ctype(c
, C_IFS
)) {
383 if (Xlength(cs
, cp
) == 0 && ctype(c
, C_IFSWS
))
390 /* strip trailing IFS white space from last variable */
392 while (Xlength(cs
, cp
) && ctype(cp
[-1], C_IFS
)
393 && ctype(cp
[-1], C_IFSWS
))
397 /* Must be done before setting export. */
398 if (vp
->flag
& RDONLY
) {
400 bi_errorf("%s is read only", *wp
);
404 typeset(*wp
, EXPORT
, 0, 0, 0);
405 if (!setstr(vp
, Xstring(cs
, cp
), KSH_RETURN_ERROR
)) {
415 histsave(source
->line
, Xstring(xs
, xp
), 1);
419 /* if this is the co-process fd, close the file descriptor
420 * (can get eof if and only if all processes are have died, ie,
421 * coproc.njobs is 0 and the pipe is closed).
423 if (c
== EOF
&& !ecode
)
424 coproc_read_close(fd
);
427 return ecode
? ecode
: c
== EOF
;
434 register struct source
*s
;
437 if (ksh_getopt(wp
, &builtin_opt
, null
) == '?')
439 s
= pushs(SWORDS
, ATEMP
);
440 s
->u
.strv
= wp
+ builtin_opt
.optind
;
443 * Handle case where the command is empty due to failed
444 * command substitution, eg, eval "$(false)".
445 * In this case, shell() will not set/change exstat (because
446 * compiled tree is empty), so will use this value.
447 * subst_exstat is cleared in execute(), so should be 0 if
448 * there were no substitutions.
450 * A strict reading of POSIX says we don't do this (though
451 * it is traditionally done). [from 1003.2-1992]
452 * 3.9.1: Simple Commands
453 * ... If there is a command name, execution shall
454 * continue as described in 3.9.1.1. If there
455 * is no command name, but the command contained a command
456 * substitution, the command shall complete with the exit
457 * status of the last command substitution
458 * 3.9.1.1: Command Search and Execution
459 * ...(1)...(a) If the command name matches the name of
460 * a special built-in utility, that special built-in
461 * utility shall be invoked.
463 * ... If there are no arguments, or only null arguments,
464 * eval shall return an exit status of zero.
466 exstat
= subst_exstat
;
469 rv
= shell(s
, FALSE
);
482 if (ksh_getopt(wp
, &builtin_opt
, null
) == '?')
484 wp
+= builtin_opt
.optind
;
489 for (p
= sigtraps
, i
= SIGNALS
+1; --i
>= 0; p
++) {
493 shprintf("trap -- ");
494 print_value_quoted(p
->trap
);
495 shprintf(" %s\n", p
->name
);
498 #if 0 /* this is ugly and not clear POSIX needs it */
499 /* POSIX may need this so output of trap can be saved and
500 * used to restore trap conditions
503 shprintf("trap -- -");
504 for (p
= sigtraps
, i
= SIGNALS
+1; --i
>= 0; p
++)
505 if (p
->trap
== NULL
&& p
->name
)
506 shprintf(" %s", p
->name
);
514 * Use case sensitive lookup for first arg so the
515 * command 'exit' isn't confused with the pseudo-signal
518 s
= (gettrap(*wp
, FALSE
) == NULL
) ? *wp
++ : NULL
; /* get command */
519 if (s
!= NULL
&& s
[0] == '-' && s
[1] == '\0')
522 /* set/clear traps */
523 while (*wp
!= NULL
) {
524 p
= gettrap(*wp
++, TRUE
);
526 bi_errorf("bad signal %s", wp
[-1]);
542 if (ksh_getopt(wp
, &builtin_opt
, null
) == '?')
544 arg
= wp
[builtin_opt
.optind
];
547 if (!getn(arg
, &n
)) {
549 warningf(TRUE
, "%s: bad number", arg
);
553 if (wp
[0][0] == 'r') { /* return */
556 /* need to tell if this is exit or return so trap exit will
559 for (ep
= e
; ep
; ep
= ep
->oenv
)
560 if (STOP_RETURN(ep
->type
)) {
566 if (how
== LEXIT
&& !really_exit
&& j_stopped_running()) {
571 quitenv(); /* get rid of any i/o redirections */
582 struct env
*ep
, *last_ep
= (struct env
*) 0;
585 if (ksh_getopt(wp
, &builtin_opt
, null
) == '?')
587 arg
= wp
[builtin_opt
.optind
];
591 else if (!bi_getn(arg
, &n
))
595 /* at&t ksh does this for non-interactive shells only - weird */
596 bi_errorf("%s: bad value", arg
);
600 /* Stop at E_NONE, E_PARSE, E_FUNC, or E_INCL */
601 for (ep
= e
; ep
&& !STOP_BRKCONT(ep
->type
); ep
= ep
->oenv
)
602 if (ep
->type
== E_LOOP
) {
605 ep
->flags
|= EF_BRKCONT_PASS
;
610 /* at&t ksh doesn't print a message - just does what it
611 * can. We print a message 'cause it helps in debugging
612 * scripts, but don't generate an error (ie, keep going).
615 warningf(TRUE
, "%s: cannot %s", wp
[0], wp
[0]);
618 /* POSIX says if n is too big, the last enclosing loop
619 * shall be used. Doesn't say to print an error but we
620 * do anyway 'cause the user messed up.
623 last_ep
->flags
&= ~EF_BRKCONT_PASS
;
624 warningf(TRUE
, "%s: can only %s %d level(s)",
625 wp
[0], wp
[0], n
- quit
);
628 unwind(*wp
[0] == 'b' ? LBREAK
: LCONTIN
);
637 struct block
*l
= e
->loc
;
638 register char **owp
= wp
;
641 static const char *const args
[] = { "set", "-", NULL
};
642 return c_typeset((char **)__UNCONST(args
));
645 argi
= parse_args(wp
, OF_SET
, &setargs
);
650 owp
= wp
+= argi
- 1;
651 wp
[0] = l
->argv
[0]; /* save $0 */
652 while (*++wp
!= NULL
)
653 *wp
= str_save(*wp
, &l
->area
);
654 l
->argc
= wp
- owp
- 1;
655 l
->argv
= (char **) alloc(sizeofN(char *, l
->argc
+2), &l
->area
);
656 for (wp
= l
->argv
; (*wp
++ = *owp
++) != NULL
; )
659 /* POSIX says set exit status is 0, but old scripts that use
660 * getopt(1), use the construct: set -- `getopt ab:c "$@"`
661 * which assumes the exit value set will be that of the ``
662 * (subst_exstat is cleared in execute() so that it will be 0
663 * if there are no command substitutions).
665 return Flag(FPOSIX
) ? 0 : subst_exstat
;
673 int optc
, unset_var
= 1;
676 while ((optc
= ksh_getopt(wp
, &builtin_opt
, "fv")) != EOF
)
687 wp
+= builtin_opt
.optind
;
688 for (; (id
= *wp
) != NULL
; wp
++)
689 if (unset_var
) { /* unset variable */
690 struct tbl
*vp
= global(id
);
692 if (!(vp
->flag
& ISSET
))
694 if ((vp
->flag
&RDONLY
)) {
695 bi_errorf("%s is read only", vp
->name
);
698 unset(vp
, strchr(id
, '[') ? 1 : 0);
699 } else { /* unset function */
700 if (define(id
, (struct op
*) NULL
))
712 (void) ksh_times(&all
);
713 shprintf("Shell: %8ss user ", clocktos(all
.tms_utime
));
714 shprintf("%8ss system\n", clocktos(all
.tms_stime
));
715 shprintf("Kids: %8ss user ", clocktos(all
.tms_cutime
));
716 shprintf("%8ss system\n", clocktos(all
.tms_cstime
));
722 * time pipeline (really a statement, not a built-in command)
729 #define TF_NOARGS BIT(0)
730 #define TF_NOREAL BIT(1) /* don't report real time */
731 #define TF_POSIX BIT(2) /* report in posix format */
733 struct tms t0
, t1
, tms
;
734 clock_t t0t
, t1t
= 0;
736 extern clock_t j_usrtime
, j_systime
; /* computed by j_wait */
739 t0t
= ksh_times(&t0
);
742 * Two ways of getting cpu usage of a command: just use t0
743 * and t1 (which will get cpu usage from other jobs that
744 * finish while we are executing t->left), or get the
745 * cpu usage of t->left. at&t ksh does the former, while
746 * pdksh tries to do the later (the j_usrtime hack doesn't
747 * really work as it only counts the last job).
749 j_usrtime
= j_systime
= 0;
750 if (t
->left
->type
== TCOM
)
753 rv
= execute(t
->left
, f
| XTIME
);
755 t1t
= ksh_times(&t1
);
759 if (tf
& TF_NOARGS
) { /* ksh93 - report shell times (shell+kids) */
761 tms
.tms_utime
= t0
.tms_utime
+ t0
.tms_cutime
;
762 tms
.tms_stime
= t0
.tms_stime
+ t0
.tms_cstime
;
764 tms
.tms_utime
= t1
.tms_utime
- t0
.tms_utime
+ j_usrtime
;
765 tms
.tms_stime
= t1
.tms_stime
- t0
.tms_stime
+ j_systime
;
768 if (!(tf
& TF_NOREAL
))
770 tf
& TF_POSIX
? "real %8s\n" : "%8ss real ",
771 clocktos(t1t
- t0t
));
772 shf_fprintf(shl_out
, tf
& TF_POSIX
? "user %8s\n" : "%8ss user ",
773 clocktos(tms
.tms_utime
));
774 shf_fprintf(shl_out
, tf
& TF_POSIX
? "sys %8s\n" : "%8ss system\n",
775 clocktos(tms
.tms_stime
));
784 char ** volatile *app
;
791 ksh_getopt_reset(&opt
, 0);
792 opt
.optind
= 0; /* start at the start */
793 while ((optc
= ksh_getopt(wp
, &opt
, ":p")) != EOF
)
796 t
->str
[0] |= TF_POSIX
;
799 errorf("time: -%s unknown option", opt
.optarg
);
801 errorf("time: -%s requires an argument",
804 /* Copy command words down over options. */
805 if (opt
.optind
!= 0) {
806 for (i
= 0; i
< opt
.optind
; i
++)
808 for (i
= 0, j
= opt
.optind
; (wp
[i
] = wp
[j
]); i
++, j
++)
812 t
->str
[0] |= TF_NOARGS
;
820 static char temp
[22]; /* enough for 64 bit clock_t */
822 register char *cp
= temp
+ sizeof(temp
);
824 /* note: posix says must use max precision, ie, if clk_tck is
825 * 1000, must print 3 places after decimal (if non-zero, else 1).
827 if (CLK_TCK
!= 100) /* convert to 1/100'ths */
828 t
= (t
< (clock_t)(1000000000/CLK_TCK
)) ?
829 (t
* 100) / CLK_TCK
: (t
/ CLK_TCK
) * 100;
832 for (i
= -2; i
<= 0 || t
> 0; i
++) {
835 *--cp
= '0' + (char)(t
%10);
841 /* exec with no args - args case is taken care of in comexec() */
848 /* make sure redirects stay in place */
849 if (e
->savefd
!= NULL
) {
850 for (i
= 0; i
< NUFILE
; i
++) {
851 if (e
->savefd
[i
] > 0)
854 * For ksh keep anything > 2 private,
855 * for sh, let them be (POSIX says what
856 * happens is unspecified and the bourne shell
860 if (i
> 2 && e
->savefd
[i
])
869 /* dummy function, special case in comexec() */
877 extern int c_test
ARGS((char **wp
)); /* in c_test.c */
878 extern int c_ulimit
ARGS((char **wp
)); /* in c_ulimit.c */
880 /* A leading = means assignments before command are kept;
881 * a leading * means a POSIX special builtin;
882 * a leading + means a POSIX regular builtin
883 * (* and + should not be combined).
885 const struct builtin shbuiltins
[] = {
889 {"*=break", c_brkcont
},
890 {"=builtin", c_builtin
},
891 {"*=continue", c_brkcont
},
894 {"*=exit", c_exitreturn
},
896 {"*=return", c_exitreturn
},
898 {"*=shift", c_shift
},
905 {"ulimit", c_ulimit
},
907 {"*=unset", c_unset
},
909 /* In OS2, the first line of a file can be "extproc name", which
910 * tells the command interpreter (cmd.exe) to use name to execute
911 * the file. For this to be useful, ksh must ignore commands
912 * starting with extproc and this does the trick...
914 {"extproc", c_label
},