1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

Backport -P and -t flags for 'type'/'whence' from ksh93v- (#392)

This commit backports the whence '-t' option from ksh93v-. The '-t'
option is useful when one needs to identify the type of a command.
The '-t' flag was added by ksh93v- for compatibility with Bash.

It should be noted the ksh93v- patch had one bug, which this commit
fixes. Path-bound builtins from /opt/ast/bin were classified as
files if loaded from /opt/ast/bin in the PATH. Reproducer:
   $ PATH=/opt/ast/bin whence -t cat
   file

src/cmd/ksh93/bltins/whence.c:
- Simplify the bitmask values for the command and whence builtin
  flags.
- Add the -t flag to the whence and type builtins. To prevent bugs,
  -t will always override -v if both of those flags were passed.

src/cmd/ksh93/data/builtins.c,
src/cmd/ksh93/sh.1:
- Add documentation for the new -t option.
This commit is contained in:
Johnothan King 2021-12-26 22:37:01 -08:00 committed by Martijn Dekker
parent 2027648f1a
commit 24174f0fb7
7 changed files with 181 additions and 32 deletions

View file

@ -20,7 +20,8 @@
***********************************************************************/
/*
* command [-pvVx] name [arg...]
* whence [-afpqv] name...
* type [-afpPqt] name...
* whence [-afpPqtv] name...
*
* David Korn
* AT&T Labs
@ -35,12 +36,13 @@
#include "shlex.h"
#include "builtins.h"
#define P_FLAG 1
#define V_FLAG 2
#define A_FLAG 4
#define F_FLAG 010
#define X_FLAG 020
#define Q_FLAG 040
#define P_FLAG (1 << 0)
#define V_FLAG (1 << 1)
#define A_FLAG (1 << 2)
#define F_FLAG (1 << 3)
#define X_FLAG (1 << 4)
#define Q_FLAG (1 << 5)
#define T_FLAG (1 << 6)
static int whence(Shell_t *,char**, int);
@ -121,12 +123,15 @@ int b_whence(int argc,char *argv[],Shbltin_t *context)
case 'v':
flags |= V_FLAG;
break;
case 't':
flags |= T_FLAG;
break;
case 'f':
flags |= F_FLAG;
break;
case 'P':
case 'p':
flags |= P_FLAG;
flags &= ~V_FLAG;
break;
case 'q':
flags |= Q_FLAG;
@ -138,6 +143,8 @@ int b_whence(int argc,char *argv[],Shbltin_t *context)
errormsg(SH_DICT,ERROR_usage(2), "%s", opt_info.arg);
UNREACHABLE();
}
if(flags&(P_FLAG|T_FLAG))
flags &= ~V_FLAG;
argv += opt_info.index;
if(error_info.errors || !*argv)
{
@ -171,7 +178,10 @@ static int whence(Shell_t *shp,char **argv, register int flags)
/* reserved words first */
if(sh_lookup(name,shtab_reserved))
{
sfprintf(sfstdout,"%s%s\n",name,(flags&V_FLAG)?sh_translate(is_reserved):"");
if(flags&T_FLAG)
sfprintf(sfstdout,"keyword\n");
else
sfprintf(sfstdout,"%s%s\n",name,(flags&V_FLAG)?sh_translate(is_reserved):"");
if(!aflag)
continue;
aflag++;
@ -186,7 +196,10 @@ static int whence(Shell_t *shp,char **argv, register int flags)
msg = sh_translate(is_alias);
sfprintf(sfstdout,msg,name);
}
sfputr(sfstdout,sh_fmtq(cp),'\n');
if(flags&T_FLAG)
sfputr(sfstdout,"alias",'\n');
else
sfputr(sfstdout,sh_fmtq(cp),'\n');
if(!aflag)
continue;
cp = 0;
@ -198,7 +211,8 @@ static int whence(Shell_t *shp,char **argv, register int flags)
{
if(flags&Q_FLAG)
continue;
sfputr(sfstdout,name,-1);
if(!(flags&T_FLAG))
sfputr(sfstdout,name,-1);
if(flags&V_FLAG)
{
if(nv_isnull(np))
@ -213,6 +227,8 @@ static int whence(Shell_t *shp,char **argv, register int flags)
else
sfprintf(sfstdout,sh_translate(is_function));
}
else if(flags&T_FLAG)
sfprintf(sfstdout,"function");
sfputc(sfstdout,'\n');
if(!aflag)
continue;
@ -230,7 +246,10 @@ static int whence(Shell_t *shp,char **argv, register int flags)
cp = "";
if(flags&Q_FLAG)
continue;
sfprintf(sfstdout,"%s%s\n",name,cp);
if(flags&T_FLAG)
sfprintf(sfstdout,"builtin\n");
else
sfprintf(sfstdout,"%s%s\n",name,cp);
if(!aflag)
continue;
aflag++;
@ -273,7 +292,9 @@ static int whence(Shell_t *shp,char **argv, register int flags)
{
/* Undefined/autoloadable function on FPATH */
sfputr(sfstdout,sh_fmtq(cp),-1);
if(flags&V_FLAG)
if(flags&T_FLAG)
sfprintf(sfstdout,"function");
else if(flags&V_FLAG)
{
sfprintf(sfstdout,sh_translate(is_ufunction));
sfprintf(sfstdout,sh_translate(e_autoloadfrom),sh_fmtq(stakptr(PATH_OFFSET)));
@ -283,13 +304,20 @@ static int whence(Shell_t *shp,char **argv, register int flags)
}
else if(cp)
{
int is_pathbound_builtin = 0;
cp = path_fullname(shp,cp); /* resolve '.' & '..' */
if(flags&V_FLAG)
if(flags&(V_FLAG|T_FLAG))
{
sfputr(sfstdout,sh_fmtq(name),' ');
if(!(flags&T_FLAG))
sfputr(sfstdout,sh_fmtq(name),' ');
/* built-in version of program */
if(nv_search(cp,shp->bltin_tree,0))
msg = sh_translate(is_builtver);
{
if(flags&T_FLAG)
is_pathbound_builtin = 1;
else
msg = sh_translate(is_builtver);
}
/* tracked aliases next */
else if(!sh_isstate(SH_DEFPATH)
&& (np = nv_search(name,shp->track_tree,0))
@ -298,9 +326,13 @@ static int whence(Shell_t *shp,char **argv, register int flags)
msg = sh_translate(is_talias);
else
msg = sh_translate("is");
sfputr(sfstdout,msg,' ');
if(!(flags&T_FLAG))
sfputr(sfstdout,msg,' ');
}
sfputr(sfstdout,sh_fmtq(cp),'\n');
if(flags&T_FLAG)
sfputr(sfstdout,is_pathbound_builtin ? "builtin" : "file",'\n');
else
sfputr(sfstdout,sh_fmtq(cp),'\n');
free((char*)cp);
}
else if(aflag<=1)