1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-02-15 04:32:24 +00:00

whence -f: ignore functions (#137)

According to 'whence --man', 'whence -f' should ignore functions:
  -f              Do not check for functions.

Right now this is only accomplished partially. As of commit
a329c22d 'whence -f' avoids any output when encountering a
function (in ksh93u+ 'whence -f' has incorrect output). The
return value is still wrong though:

$ foo() { true; }
$ whence -f foo; echo $?
0

This commit fixes the return value and makes 'type -f' error out
when given a function (like in Bash).

src/cmd/ksh93/bltins/whence.c:
- If -f was passed, set 'cp' to NULL since functions should be
  ignored (as documented).
- Simplify return value by avoiding bitwise logic.

src/cmd/ksh93/tests/builtins.sh:
- Add regression tests for 'whence -f' and 'type -f'.

Co-authored-by: Martijn Dekker <martijn@inlv.org>
This commit is contained in:
Johnothan King 2020-09-26 11:26:18 -07:00 committed by GitHub
parent 7e6bbf85b6
commit 8a34fc40e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 7 deletions

4
NEWS
View file

@ -3,6 +3,10 @@ For full details, see the git log at: https://github.com/ksh93/ksh
Any uppercase BUG_* names are modernish shell bug IDs.
2020-09-26:
- 'whence -f' now completely ignores the existence of functions, as documented.
2020-09-25:
- whence -v/-a now reports the path to the file that an "undefined" (i.e.

View file

@ -135,7 +135,7 @@ static int whence(Shell_t *shp,char **argv, register int flags)
register const char *name;
register Namval_t *np;
register const char *cp;
register int aflag,r=0;
register int aflag, ret = 0;
register const char *msg;
Namval_t *nq;
char *notused;
@ -230,7 +230,7 @@ static int whence(Shell_t *shp,char **argv, register int flags)
cp = name;
if(*cp!='/')
{
if(flags&P_FLAG)
if(flags&(P_FLAG|F_FLAG)) /* Ignore functions when passed -f or -p */
cp = 0;
else
maybe_undef_fn = 1;
@ -244,8 +244,9 @@ static int whence(Shell_t *shp,char **argv, register int flags)
}
if(flags&Q_FLAG)
{
pp = 0;
r |= !cp;
/* Since -q ignores -a, return on the first non-match */
if(!cp)
return(1);
}
else if(maybe_undef_fn)
{
@ -286,7 +287,7 @@ static int whence(Shell_t *shp,char **argv, register int flags)
}
else if(aflag<=1)
{
r |= 1;
ret = 1;
if(flags&V_FLAG)
errormsg(SH_DICT,ERROR_exit(0),e_found,sh_fmtq(name));
}
@ -302,6 +303,6 @@ static int whence(Shell_t *shp,char **argv, register int flags)
pp = 0;
} while(pp);
}
return(r);
return(ret);
}

View file

@ -17,4 +17,4 @@
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#define SH_RELEASE "93u+m 2020-09-25"
#define SH_RELEASE "93u+m 2020-09-26"

View file

@ -841,6 +841,27 @@ actual=$(hash -r; alias ls='echo ALL UR F1LEZ R G0N3'; hash ls; whence -a ls)
[[ $actual == "$expect" ]] || err_exit "'whence -a' does not report tracked alias if alias exists" \
"(expected $(printf %q "$expect"), got $(printf %q "$actual"))"
# 'whence -f' should ignore functions
foo_bar() { true; }
actual="$(whence -f foo_bar)"
whence -f foo_bar >/dev/null && err_exit "'whence -f' doesn't ignore functions (got '$(printf %q "$actual")')"
# whence -vq/type -q must be tested as well
actual="$(type -f foo_bar 2>&1)"
type -f foo_bar >/dev/null 2>&1 && err_exit "'type -f' doesn't ignore functions (got '$(printf %q "$actual")')"
type -qf foo_bar && err_exit "'type -qf' doesn't ignore functions"
# Test the exit status of 'whence -q'
(
mkdir "$tmp/fakepath"
ln -s "${ whence -p cat ;}" "$tmp/fakepath"
ln -s "${ whence -p ls ;}" "$tmp/fakepath"
PATH="$tmp/fakepath"
whence -q cat nonexist ls && err_exit "'whence -q' has the wrong exit status"
whence -q cat nonexist && err_exit "'whence -q' has the wrong exit status"
whence -q nonexist && err_exit "'whence -q' has the wrong exit status"
)
# ======
# 'cd ../.foo' should not exclude the '.' in '.foo'
# https://bugzilla.redhat.com/889748