mirror of
git://git.code.sf.net/p/cdesktopenv/code
synced 2025-03-09 15:50:02 +00:00
redirect: check args before executing redirections (re: 7b82c338
)
The 'redirect' builtin command did not error out before executing any valid redirections. For example, 'redirect ls >foo.txt' issued an "incorrect syntax" error, but still created 'foo.txt' and left standard output permanently redirected to it. src/cmd/ksh93/sh/xec.c: sh_exec(): - If we have redirections (io != NULL), and the command is SYSREDIR, then check for arguments and error out if there are any, before calling sh_redirect() to execute redirections. (Note, the other check for arguments in b_exec() in bltins/misc.c must be kept, as that applies if there are no redirections.) src/cmd/ksh93/sh/io.c: sh_redirect(): - Edit comments to better explain what the flag values do. src/cmd/ksh93/bltins/misc.c: - Add a dummy b_redirect() function declaration "for the dictionary generator" as has historically been done for other builtins that share one C function. I'm not sure what that dictionary generator is supposed to be, but this also improves greppability. src/cmd/ksh93/data/builtins.c, src/cmd/ksh93/sh.1: - Fix misleading "I/O redirection arguments" term. I/O redirections are not arguments at all; no argument parser ever sees them. src/cmd/ksh93/tests/io.sh: - Test both conditions that should make 'redirect' produce an "incorrect syntax" error. - Test that any redirections are not executed if erroneous non-redirection arguments exist. src/cmd/ksh93/tests/builtins.sh: - "... should show usage info on unrecognized options" test: Because 'redirect' now refuses to process redirections on error, the error message was not captured. The fix is to run the builtin in a braces block and add the redirection to the block.
This commit is contained in:
parent
e805c7d9b1
commit
be5ea8bbb2
9 changed files with 37 additions and 14 deletions
|
@ -1074,9 +1074,10 @@ static char *io_usename(char *name, int *perm, int fno, int mode)
|
|||
|
||||
/*
|
||||
* I/O redirection
|
||||
* flag = 0 if files are to be restored
|
||||
* flag = 2 if files are to be closed on exec
|
||||
* flag = 3 when called from $( < ...), just open file and return
|
||||
* flag = 0: normal redirection; file descriptors are restored when command terminates
|
||||
* flag = 1: redirections persist
|
||||
* flag = 2: redirections persist, but file descriptors > 2 are closed when executing an external command
|
||||
* flag = 3: just open file and return; for use when called from $( < ...)
|
||||
* flag = SH_SHOWME for trace only
|
||||
*/
|
||||
int sh_redirect(Shell_t *shp,struct ionod *iop, int flag)
|
||||
|
|
|
@ -1273,8 +1273,13 @@ int sh_exec(register const Shnode_t *t, int flags)
|
|||
if(io)
|
||||
{
|
||||
struct openlist *item;
|
||||
if(np==SYSEXEC || np==SYSREDIR) /* 'exec' or 'redirect' */
|
||||
type=1+!com[1]; /* redirections persist if no args */
|
||||
if(np == SYSEXEC) /* 'exec' */
|
||||
type = 1 + !com[1];
|
||||
else if(np == SYSREDIR) /* 'redirect' */
|
||||
if(!com[1])
|
||||
type = 2;
|
||||
else
|
||||
errormsg(SH_DICT,ERROR_exit(2),"redirect: %s",e_badsyntax);
|
||||
else
|
||||
type = (execflg && !shp->subshell && !shp->st.trapcom[0]);
|
||||
shp->redir0 = 1;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue