2 * built-in Bourne commands
10 static char * clocktos(int);
11 #if 0 /* not used at the moment */
12 static void p_time(struct shf
*, int, struct timeval
*, int, char *, char *);
13 #endif /* not used at the moment */
15 /* :, false and true */
19 return wp
[0][0] == 'f' ? 1 : 0;
25 struct block
*l
= e
->loc
;
30 if (ksh_getopt(wp
, &builtin_opt
, null
) == '?')
32 arg
= wp
[builtin_opt
.optind
];
35 evaluate(arg
, &val
, KSH_UNWIND_ERROR
, false);
40 bi_errorf("%s: bad number", arg
);
44 bi_errorf("nothing to shift");
47 l
->argv
[n
] = l
->argv
[0];
62 while ((optc
= ksh_getopt(wp
, &builtin_opt
, "S")) != -1)
70 cp
= wp
[builtin_opt
.optind
];
78 old_umask
= ~old_umask
;
80 for (i
= 0; i
< 3; i
++) {
83 for (j
= 0; j
< 3; j
++)
84 if (old_umask
& (1 << (8 - (3*i
+ j
))))
89 shprintf("%s\n", buf
);
91 shprintf("%#3.3o\n", old_umask
);
96 for (new_umask
= 0; *cp
>= '0' && *cp
<= '7'; cp
++)
97 new_umask
= new_umask
* 8 + (*cp
- '0');
99 bi_errorf("bad number");
103 /* symbolic format */
104 int positions
, new_val
;
107 old_umask
= umask(0);
108 umask(old_umask
); /* in case of error */
109 old_umask
= ~old_umask
;
110 new_umask
= old_umask
;
113 while (*cp
&& strchr("augo", *cp
))
129 positions
= 0111; /* default is a */
130 if (!strchr("=+-", op
= *cp
))
134 while (*cp
&& strchr("rwxugoXs", *cp
))
146 new_val
|= old_umask
>> 6;
149 new_val
|= old_umask
>> 3;
152 new_val
|= old_umask
>> 0;
155 if (old_umask
& 0111)
158 case 's': /* ignored */
161 new_val
= (new_val
& 07) * positions
;
164 new_umask
&= ~new_val
;
167 new_umask
= new_val
|
168 (new_umask
& ~(positions
* 07));
171 new_umask
|= new_val
;
176 } else if (!strchr("=+-", *cp
))
180 bi_errorf("bad mask");
183 new_umask
= ~new_umask
;
199 if (ksh_getopt(wp
, &builtin_opt
, null
) == '?')
202 if ((cp
= wp
[builtin_opt
.optind
]) == NULL
)
204 file
= search(cp
, path
, R_OK
, &err
);
206 bi_errorf("%s: %s", cp
, err
? strerror(err
) : "not found");
210 /* Set positional parameters? */
211 if (wp
[builtin_opt
.optind
+ 1]) {
212 argv
= wp
+ builtin_opt
.optind
;
213 argv
[0] = e
->loc
->argv
[0]; /* preserve $0 */
214 for (argc
= 0; argv
[argc
+ 1]; argc
++)
220 i
= include(file
, argc
, argv
, 0);
221 if (i
< 0) { /* should not happen */
222 bi_errorf("%s: %s", cp
, strerror(errno
));
234 if (ksh_getopt(wp
, &builtin_opt
, null
) == '?')
236 wp
+= builtin_opt
.optind
;
237 if (*wp
== (char *) 0) {
238 while (waitfor((char *) 0, &sig
) >= 0)
243 rv
= waitfor(*wp
, &sig
);
245 rv
= sig
? sig
: 127; /* magic exit code: bad job-id */
254 int expand
= 1, history
= 0;
268 while ((optc
= ksh_getopt(wp
, &builtin_opt
, "prsu,")) != -1)
271 if ((fd
= coproc_getfd(R_OK
, &emsg
)) < 0) {
272 bi_errorf("-p: %s", emsg
);
283 if (!*(cp
= builtin_opt
.optarg
))
285 else if ((fd
= check_fd(cp
, R_OK
, &emsg
)) < 0) {
286 bi_errorf("-u: %s: %s", cp
, emsg
);
293 wp
+= builtin_opt
.optind
;
296 *--wp
= strdup("REPLY");
298 /* Since we can't necessarily seek backwards on non-regular files,
299 * don't buffer them so we can't read too much.
301 shf
= shf_reopen(fd
, SHF_RD
| SHF_INTERRUPT
| can_seek(fd
), shl_spare
);
303 if ((cp
= strchr(*wp
, '?')) != NULL
) {
306 /* at&t ksh says it prints prompt on fd if it's open
307 * for writing and is a tty, but it doesn't do it
308 * (it also doesn't check the interactive flag,
309 * as is indicated in the Kornshell book).
315 /* If we are reading from the co-process for the first time,
316 * make sure the other side of the pipe is closed first. This allows
317 * the detection of eof.
319 * This is not compatiable with at&t ksh... the fd is kept so another
320 * coproc can be started with same ouput, however, this means eof
321 * can't be detected... This is why it is closed here.
322 * If this call is removed, remove the eof check below, too.
323 * coproc_readw_close(fd);
327 Xinit(xs
, xp
, 128, ATEMP
);
329 Xinit(cs
, cp
, 128, ATEMP
);
330 for (; *wp
!= NULL
; wp
++) {
331 for (cp
= Xstring(cs
, cp
); ; ) {
332 if (c
== '\n' || c
== EOF
)
338 if (c
== EOF
&& shf_error(shf
) &&
339 shf_errno(shf
) == EINTR
)
341 /* Was the offending signal one that
342 * would normally kill a process?
343 * If so, pretend the read was killed.
345 ecode
= fatal_trap_check();
347 /* non fatal (eg, CHLD), carry on */
364 if (Flag(FTALKING_I
) && isatty(fd
)) {
365 set_prompt(PS2
, (Source
*) 0);
372 if (expand
&& c
== '\\') {
376 if (c
== '\n' || c
== EOF
)
378 if (ctype(c
, C_IFS
)) {
379 if (Xlength(cs
, cp
) == 0 && ctype(c
, C_IFSWS
))
386 /* strip trailing IFS white space from last variable */
388 while (Xlength(cs
, cp
) && ctype(cp
[-1], C_IFS
) &&
389 ctype(cp
[-1], C_IFSWS
))
393 /* Must be done before setting export. */
394 if (vp
->flag
& RDONLY
) {
396 bi_errorf("%s is read only", *wp
);
400 typeset(*wp
, EXPORTV
, 0, 0, 0);
401 if (!setstr(vp
, Xstring(cs
, cp
), KSH_RETURN_ERROR
)) {
411 histsave(source
->line
, Xstring(xs
, xp
), 1);
414 /* if this is the co-process fd, close the file descriptor
415 * (can get eof if and only if all processes are have died, ie,
416 * coproc.njobs is 0 and the pipe is closed).
418 if (c
== EOF
&& !ecode
)
419 coproc_read_close(fd
);
421 return ecode
? ecode
: c
== EOF
;
430 if (ksh_getopt(wp
, &builtin_opt
, null
) == '?')
432 s
= pushs(SWORDS
, ATEMP
);
433 s
->u
.strv
= wp
+ builtin_opt
.optind
;
436 * Handle case where the command is empty due to failed
437 * command substitution, eg, eval "$(false)".
438 * In this case, shell() will not set/change exstat (because
439 * compiled tree is empty), so will use this value.
440 * subst_exstat is cleared in execute(), so should be 0 if
441 * there were no substitutions.
443 * A strict reading of POSIX says we don't do this (though
444 * it is traditionally done). [from 1003.2-1992]
445 * 3.9.1: Simple Commands
446 * ... If there is a command name, execution shall
447 * continue as described in 3.9.1.1. If there
448 * is no command name, but the command contained a command
449 * substitution, the command shall complete with the exit
450 * status of the last command substitution
451 * 3.9.1.1: Command Search and Execution
452 * ...(1)...(a) If the command name matches the name of
453 * a special built-in utility, that special built-in
454 * utility shall be invoked.
456 * ... If there are no arguments, or only null arguments,
457 * eval shall return an exit status of zero.
459 exstat
= subst_exstat
;
462 rv
= shell(s
, false);
474 if (ksh_getopt(wp
, &builtin_opt
, null
) == '?')
476 wp
+= builtin_opt
.optind
;
481 for (p
= sigtraps
, i
= NSIG
+1; --i
>= 0; p
++) {
485 shprintf("trap -- ");
486 print_value_quoted(p
->trap
);
487 shprintf(" %s\n", p
->name
);
490 #if 0 /* this is ugly and not clear POSIX needs it */
491 /* POSIX may need this so output of trap can be saved and
492 * used to restore trap conditions
495 shprintf("trap -- -");
496 for (p
= sigtraps
, i
= NSIG
+1; --i
>= 0; p
++)
497 if (p
->trap
== NULL
&& p
->name
)
498 shprintf(" %s", p
->name
);
502 (void)anydfl
; // Unused
508 * Use case sensitive lookup for first arg so the
509 * command 'exit' isn't confused with the pseudo-signal
512 s
= (gettrap(*wp
, false) == NULL
) ? *wp
++ : NULL
; /* get command */
513 if (s
!= NULL
&& s
[0] == '-' && s
[1] == '\0')
516 /* set/clear traps */
517 while (*wp
!= NULL
) {
518 p
= gettrap(*wp
++, true);
520 bi_errorf("bad signal %s", wp
[-1]);
529 c_exitreturn(char **wp
)
535 if (ksh_getopt(wp
, &builtin_opt
, null
) == '?')
537 arg
= wp
[builtin_opt
.optind
];
540 if (!getn(arg
, &n
)) {
542 warningf(true, "%s: bad number", arg
);
546 if (wp
[0][0] == 'r') { /* return */
549 /* need to tell if this is exit or return so trap exit will
552 for (ep
= e
; ep
; ep
= ep
->oenv
)
553 if (STOP_RETURN(ep
->type
)) {
559 if (how
== LEXIT
&& !really_exit
&& j_stopped_running()) {
564 quitenv(NULL
); /* get rid of any i/o redirections */
574 struct env
*ep
, *last_ep
= (struct env
*) 0;
577 if (ksh_getopt(wp
, &builtin_opt
, null
) == '?')
579 arg
= wp
[builtin_opt
.optind
];
583 else if (!bi_getn(arg
, &n
))
587 /* at&t ksh does this for non-interactive shells only - weird */
588 bi_errorf("%s: bad value", arg
);
592 /* Stop at E_NONE, E_PARSE, E_FUNC, or E_INCL */
593 for (ep
= e
; ep
&& !STOP_BRKCONT(ep
->type
); ep
= ep
->oenv
)
594 if (ep
->type
== E_LOOP
) {
597 ep
->flags
|= EF_BRKCONT_PASS
;
602 /* at&t ksh doesn't print a message - just does what it
603 * can. We print a message 'cause it helps in debugging
604 * scripts, but don't generate an error (ie, keep going).
607 warningf(true, "%s: cannot %s", wp
[0], wp
[0]);
610 /* POSIX says if n is too big, the last enclosing loop
611 * shall be used. Doesn't say to print an error but we
612 * do anyway 'cause the user messed up.
615 last_ep
->flags
&= ~EF_BRKCONT_PASS
;
616 warningf(true, "%s: can only %s %d level(s)",
617 wp
[0], wp
[0], n
- quit
);
620 unwind(*wp
[0] == 'b' ? LBREAK
: LCONTIN
);
628 struct block
*l
= e
->loc
;
632 static const char *const args
[] = { "set", "-", NULL
};
633 return c_typeset((char **) args
);
636 argi
= parse_args(wp
, OF_SET
, &setargs
);
641 owp
= wp
+= argi
- 1;
642 wp
[0] = l
->argv
[0]; /* save $0 */
643 while (*++wp
!= NULL
)
644 *wp
= str_save(*wp
, &l
->area
);
645 l
->argc
= wp
- owp
- 1;
646 l
->argv
= (char **) alloc(sizeofN(char *, l
->argc
+2), &l
->area
);
647 for (wp
= l
->argv
; (*wp
++ = *owp
++) != NULL
; )
650 /* POSIX says set exit status is 0, but old scripts that use
651 * getopt(1), use the construct: set -- `getopt ab:c "$@"`
652 * which assumes the exit value set will be that of the ``
653 * (subst_exstat is cleared in execute() so that it will be 0
654 * if there are no command substitutions).
656 return Flag(FPOSIX
) ? 0 : subst_exstat
;
663 int optc
, unset_var
= 1;
665 while ((optc
= ksh_getopt(wp
, &builtin_opt
, "fv")) != -1)
676 wp
+= builtin_opt
.optind
;
677 for (; (id
= *wp
) != NULL
; wp
++)
678 if (unset_var
) { /* unset variable */
679 struct tbl
*vp
= global(id
);
681 if ((vp
->flag
&RDONLY
)) {
682 bi_errorf("%s is read only", vp
->name
);
685 unset(vp
, strchr(id
, '[') ? 1 : 0);
686 } else { /* unset function */
687 define(id
, (struct op
*) NULL
);
692 #if 0 /* not used at the moment */
694 p_time(struct shf
*shf
, int posix
, struct timeval
*tv
, int width
, char *prefix
,
698 shf_fprintf(shf
, "%s%*ld.%02ld%s", prefix
? prefix
: "",
699 width
, tv
->tv_sec
, tv
->tv_usec
/ 10000, suffix
);
701 shf_fprintf(shf
, "%s%*ldm%ld.%02lds%s", prefix
? prefix
: "",
702 width
, tv
->tv_sec
/ 60, tv
->tv_sec
% 60,
703 tv
->tv_usec
/ 10000, suffix
);
705 #endif /* not used at the moment */
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)
725 timex(struct op
*t
, int f
)
727 #define TF_NOARGS BIT(0)
728 #define TF_NOREAL BIT(1) /* don't report real time */
729 #define TF_POSIX BIT(2) /* report in posix format */
731 struct tms t0
, t1
, tms
;
734 extern INT32 j_usrtime
, j_systime
; /* computed by j_wait */
737 t0t
= ksh_times(&t0
);
740 * Two ways of getting cpu usage of a command: just use t0
741 * and t1 (which will get cpu usage from other jobs that
742 * finish while we are executing t->left), or get the
743 * cpu usage of t->left. at&t ksh does the former, while
744 * pdksh tries to do the later (the j_usrtime hack doesn't
745 * really work as it only counts the last job).
747 j_usrtime
= j_systime
= 0;
748 if (t
->left
->type
== TCOM
)
751 rv
= execute(t
->left
, f
| XTIME
);
753 t1t
= ksh_times(&t1
);
757 if (tf
& TF_NOARGS
) { /* ksh93 - report shell times (shell+kids) */
759 tms
.tms_utime
= t0
.tms_utime
+ t0
.tms_cutime
;
760 tms
.tms_stime
= t0
.tms_stime
+ t0
.tms_cstime
;
762 tms
.tms_utime
= t1
.tms_utime
- t0
.tms_utime
+ j_usrtime
;
763 tms
.tms_stime
= t1
.tms_stime
- t0
.tms_stime
+ j_systime
;
766 if (!(tf
& TF_NOREAL
))
768 tf
& TF_POSIX
? "real %8s\n" : "%8ss real ",
769 clocktos(t1t
- t0t
));
770 shf_fprintf(shl_out
, tf
& TF_POSIX
? "user %8s\n" : "%8ss user ",
771 clocktos(tms
.tms_utime
));
772 shf_fprintf(shl_out
, tf
& TF_POSIX
? "sys %8s\n" : "%8ss system\n",
773 clocktos(tms
.tms_stime
));
780 timex_hook(struct op
*t
, char **volatile *app
)
787 ksh_getopt_reset(&opt
, 0);
788 opt
.optind
= 0; /* start at the start */
789 while ((optc
= ksh_getopt(wp
, &opt
, ":p")) != -1)
792 t
->str
[0] |= TF_POSIX
;
795 errorf("time: -%s unknown option", opt
.optarg
);
797 errorf("time: -%s requires an argument",
800 /* Copy command words down over options. */
801 if (opt
.optind
!= 0) {
802 for (i
= 0; i
< opt
.optind
; i
++)
804 for (i
= 0, j
= opt
.optind
; (wp
[i
] = wp
[j
]); i
++, j
++)
808 t
->str
[0] |= TF_NOARGS
;
815 static char temp
[22]; /* enough for 64 bit INT32 */
817 char *cp
= temp
+ sizeof(temp
);
819 /* note: posix says must use max precision, ie, if clk_tck is
820 * 1000, must print 3 places after decimal (if non-zero, else 1).
822 if (CLK_TCK
!= 100) /* convert to 1/100'ths */
823 t
= (t
< 1000000000/CLK_TCK
) ?
824 (t
* 100) / CLK_TCK
: (t
/ CLK_TCK
) * 100;
827 for (i
= -2; i
<= 0 || t
> 0; i
++) {
830 *--cp
= '0' + (char)(t
%10);
836 /* exec with no args - args case is taken care of in comexec() */
842 /* make sure redirects stay in place */
843 if (e
->savefd
!= NULL
) {
844 for (i
= 0; i
< NUFILE
; i
++) {
845 if (e
->savefd
[i
] > 0)
847 #if !defined __amigaos4__ && !defined __AROS__
849 * For ksh keep anything > 2 private,
850 * for sh, let them be (POSIX says what
851 * happens is unspecified and the bourne shell
854 if (!Flag(FSH
) && i
> 2 && e
->savefd
[i
])
855 fcntl(i
, F_SETFD
, FD_CLOEXEC
);
863 /* dummy function, special case in comexec() */
865 c_builtin(char ** wp
)
870 extern int c_test(char **wp
); /* in c_test.c */
871 extern int c_ulimit(char **wp
); /* in c_ulimit.c */
873 /* A leading = means assignments before command are kept;
874 * a leading * means a POSIX special builtin;
875 * a leading + means a POSIX regular builtin
876 * (* and + should not be combined).
878 const struct builtin shbuiltins
[] = {
882 {"*=break", c_brkcont
},
883 {"=builtin", c_builtin
},
884 {"*=continue", c_brkcont
},
887 {"*=exit", c_exitreturn
},
889 {"*=return", c_exitreturn
},
891 {"*=shift", c_shift
},
899 {"*=unset", c_unset
},