1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-02-13 19:52:20 +00:00

whence -a/type -a: report both function and built-in by same name

'whence -a' is documented to list all possible interpretations of a
command, but failed to list a built-in command if a shell function
by the same name exists or is marked undefined using 'autoload'.

src/cmd/ksh93/bltins/whence.c: whence():
- Refactor and separate the code for reporting functions and
  built-in commands so that both can be reported for one name.

src/cmd/ksh93/data/builtins.c: sh_optwhence[]:
- Correct 'whence --man' to document that:
  * 'type' is equivalent to 'whence -v'
  * '-a' output is like '-v'

src/cmd/ksh93/tests/builtins.sh:
- Test 'whence -a' with these combinations:
  * a function, built-in and external command
  * an undefined/autoload function, built-in and external command

Fixes https://github.com/ksh93/ksh/issues/83
This commit is contained in:
Martijn Dekker 2020-07-20 19:49:32 +01:00
parent 01c25cb14b
commit bc8b36faba
5 changed files with 45 additions and 20 deletions

5
NEWS
View file

@ -3,6 +3,11 @@ For full details, see the git log at: https://github.com/ksh93/ksh
Any uppercase BUG_* names are modernish shell bug IDs. Any uppercase BUG_* names are modernish shell bug IDs.
2020-07-20:
- If a shell function and a built-in command by the same name exist,
'whence -a' and 'type -a' now report both.
2020-07-19: 2020-07-19:
- Fixed a crash that occured in the '.' command when using kshdb. - Fixed a crash that occured in the '.' command when using kshdb.

View file

@ -138,7 +138,6 @@ static int whence(Shell_t *shp,char **argv, register int flags)
register int aflag,r=0; register int aflag,r=0;
register const char *msg; register const char *msg;
int tofree; int tofree;
Dt_t *root;
Namval_t *nq; Namval_t *nq;
char *notused; char *notused;
Pathcomp_t *pp=0; Pathcomp_t *pp=0;
@ -181,32 +180,36 @@ static int whence(Shell_t *shp,char **argv, register int flags)
} }
/* built-ins and functions next */ /* built-ins and functions next */
bltins: bltins:
root = (flags&F_FLAG)?shp->bltin_tree:shp->fun_tree; if(!(flags&F_FLAG) && (np = nv_bfsearch(name, shp->fun_tree, &nq, &notused)) && !is_abuiltin(np))
if(np= nv_bfsearch(name, root, &nq, &notused))
{ {
if(is_abuiltin(np) && nv_isnull(np))
goto search;
cp = "";
if(flags&V_FLAG) if(flags&V_FLAG)
{
if(nv_isnull(np)) if(nv_isnull(np))
cp = sh_translate(is_ufunction); cp = sh_translate(is_ufunction);
else if(is_abuiltin(np))
{
if(nv_isattr(np,BLT_SPC))
cp = sh_translate(is_spcbuiltin);
else
cp = sh_translate(is_builtin);
}
else else
cp = sh_translate(is_function); cp = sh_translate(is_function);
} else
cp = "";
if(flags&Q_FLAG)
continue;
sfprintf(sfstdout,"%s%s\n",name,cp);
if(!aflag)
continue;
aflag++;
}
if((np = nv_bfsearch(name, shp->bltin_tree, &nq, &notused)) && !nv_isnull(np))
{
if(flags&V_FLAG)
if(nv_isattr(np,BLT_SPC))
cp = sh_translate(is_spcbuiltin);
else
cp = sh_translate(is_builtin);
else
cp = "";
if(flags&Q_FLAG) if(flags&Q_FLAG)
continue; continue;
sfprintf(sfstdout,"%s%s\n",name,cp); sfprintf(sfstdout,"%s%s\n",name,cp);
if(!aflag) if(!aflag)
continue; continue;
cp = 0;
aflag++; aflag++;
} }
search: search:

View file

@ -2017,9 +2017,9 @@ _JOB_
; ;
const char sh_optwhence[] = const char sh_optwhence[] =
"[-1c?\n@(#)$Id: whence (AT&T Research) 2007-04-24 $\n]" "[-1c?\n@(#)$Id: whence (AT&T Research/ksh93) 2020-07-20 $\n]"
USAGE_LICENSE USAGE_LICENSE
"[+NAME?whence - locate a command and describe its type]" "[+NAME?whence, type - locate a command and describe its type]"
"[+DESCRIPTION?Without \b-v\b, \bwhence\b writes on standard output an " "[+DESCRIPTION?Without \b-v\b, \bwhence\b writes on standard output an "
"absolute pathname, if any, corresponding to \aname\a based " "absolute pathname, if any, corresponding to \aname\a based "
"on the complete search order that the shell uses. If \aname\a " "on the complete search order that the shell uses. If \aname\a "
@ -2027,7 +2027,8 @@ USAGE_LICENSE
"[+?If \b-v\b is specified, the output will also contain information " "[+?If \b-v\b is specified, the output will also contain information "
"that indicates how the given \aname\a would be interpreted by " "that indicates how the given \aname\a would be interpreted by "
"the shell in the current execution environment.]" "the shell in the current execution environment.]"
"[a?Displays all uses for each \aname\a rather than the first.]" "[+?The \btype\b command is equivalent to \bwhence -v\b.]"
"[a?Like \b-v\b but displays all uses for each \aname\a rather than the first.]"
"[f?Do not check for functions.]" "[f?Do not check for functions.]"
"[p?Do not check to see if \aname\a is a reserved word, a built-in, " "[p?Do not check to see if \aname\a is a reserved word, a built-in, "
"an alias, or a function. This turns off the \b-v\b option.]" "an alias, or a function. This turns off the \b-v\b option.]"

View file

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

View file

@ -753,6 +753,22 @@ $(whence -a -p printf | sed 's/^/printf is /')"
[[ $actual == $expected ]] || err_exit "'whence -a': incorrect output" \ [[ $actual == $expected ]] || err_exit "'whence -a': incorrect output" \
"(expected $(printf %q "$expected"), got $(printf %q "$actual"))" "(expected $(printf %q "$expected"), got $(printf %q "$actual"))"
# 'whence -a'/'type -a' failed to list builtin if function exists: https://github.com/ksh93/ksh/issues/83
actual=$(printf() { :; }; whence -a printf)
expected="printf is a function
printf is a shell builtin
$(whence -a -p printf | sed 's/^/printf is /')"
[[ $actual == $expected ]] || err_exit "'whence -a': incorrect output for function+builtin" \
"(expected $(printf %q "$expected"), got $(printf %q "$actual"))"
# 'whence -a'/'type -a' failed to list builtin if autoload function exists: https://github.com/ksh93/ksh/issues/83
actual=$(autoload printf; whence -a printf)
expected="printf is an undefined function
printf is a shell builtin
$(whence -a -p printf | sed 's/^/printf is /')"
[[ $actual == $expected ]] || err_exit "'whence -a': incorrect output for autoload+builtin" \
"(expected $(printf %q "$expected"), got $(printf %q "$actual"))"
# ====== # ======
# 'cd ../.foo' should not exclude the '.' in '.foo' # 'cd ../.foo' should not exclude the '.' in '.foo'
( (