Skip to content

bug-666: can't expand a function name from an alias #870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cmd/ksh93/include/shlex.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ struct _shlex_pvt_lexdata_
char warn;
char message;
char arith;
char funcalias; /* bug-666: Phi: */
char *first;
int level;
int lastc;
Expand Down
36 changes: 35 additions & 1 deletion src/cmd/ksh93/sh/lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,14 @@ int sh_lex(Lex_t* lp)
}
}
c = 0;
/*
* bug-666: Phi:
*
*/
if(lp->lexd.funcalias==1)
{ goto check_alias;
}

if(!lp->lex.skipword)
{
if(n>1 && lp->lex.reservok==1 && mode==ST_NAME &&
Expand Down Expand Up @@ -1496,18 +1504,44 @@ int sh_lex(Lex_t* lp)
{
/* check for aliases */
Namval_t* np;

check_alias: /* bug-666: Phi: */
if(!lp->lex.incase && !assignment && fcpeek(0)!=LPAREN &&
(np=nv_search(state,sh.alias_tree,0))
&& !nv_isattr(np,NV_NOEXPAND)
&& (lp->aliasok!=2 || nv_isattr(np,BLT_DCL))
&& (!sh_isstate(SH_NOALIAS) || nv_isattr(np,NV_NOFREE))
&& (state=nv_getval(np)))
{
int t; /* bug-666: Phi: */
setupalias(lp,state,np);
nv_onattr(np,NV_NOEXPAND);
lp->lex.reservok = 1;
lp->assignok |= lp->lex.reservok;
return sh_lex(lp);
/*
* bug-666: Phi: Check if we just expanded
* "function " (i.e trailig space), if so we
* got to set lp->lexd.funcalias=1;
* so that next funct() will have a chance to
* expand next word. The original return
* below is replaced with the funcalias
* setup
*/
/* return sh_lex(lp); */
if(lp->lexd.funcalias==1)
{
lp->lexd.funcalias++;
}
t=sh_lex(lp);
if(strcmp(state,"function ")==0)
{
lp->lexd.funcalias=1;
}
else
{
lp->lexd.funcalias=0;
}
return t;
}
}
lp->lex.reservok = 0;
Expand Down
6 changes: 6 additions & 0 deletions src/cmd/ksh93/tests/alias.sh
Original file line number Diff line number Diff line change
Expand Up @@ -319,5 +319,11 @@ chmod +x bad_func # bug only triggered if file is executable
(FPATH=$PWD; alias -t bad_func 2>/dev/null; typeset -f bad_func >/dev/null)
(($? > 0)) || err_exit "'hash'/'alias -t' autoloads function"

# bug-666: Phi:
alias 'x=function ' y=f z='{ ' t='echo ' u='AA ' v='}'
x y z t u v
got=$(f)
[ "$got" = 'AA' ] || err_exit "alias x='function ' not working"

# ======
exit $((Errors<125?Errors:125))