ksh: `enum` types cause a syntax error when using `shcomp` or `source`
This is a follow-up on the syntax error that occurs when using shcomp
(discussion: https://github.com/ksh93/ksh/issues/87#issuecomment-813811003). The basic problem is that parser limitations prevent shcomp
or source
from handling enum
types correctly:
$ cat /tmp/colors.sh
enum Color_t=(red green blue orange yellow)
Color_t -A Colors=([foo]=red)
$ shcomp /tmp/colors.sh > /dev/null
/tmp/colors.sh: syntax error at line 2: `(' unexpected
$ source /tmp/colors.sh
/bin/ksh: source: syntax error: `(' unexpected
For shcomp
I suggested using the patch below to work around the parser limitation. The current version of shcomp
will run the alias
command to set aliases, so this does the same thing for enum
types:
--- a/src/cmd/ksh93/sh/shcomp.c
+++ b/src/cmd/ksh93/sh/shcomp.c
@@ -148,8 +148,12 @@ int main(int argc, char *argv[])
stakset((char*)0,0);
if(t = (Shnode_t*)sh_parse(shp,in,0))
{
- if((t->tre.tretyp&(COMMSK|COMSCAN))==0 && t->com.comnamp && strcmp(nv_name((Namval_t*)t->com.comnamp),"alias")==0)
- sh_exec(t,0);
+ if((t->tre.tretyp&(COMMSK|COMSCAN))==0 && t->com.comnamp)
+ {
+ char *cmd = nv_name((Namval_t*)t->com.comnamp);
+ if(strcmp(cmd,"alias")==0 || strcmp(cmd,"enum")==0)
+ sh_exec(t,0);
+ }
if(!dflag && sh_tdump(out,t) < 0)
{
errormsg(SH_DICT,ERROR_exit(1),"dump failed");
The workaround shcomp
currently uses isn’t perfect, as it causes scripts that use alias
to list aliases (such as with alias -p
) to break:
$ cat ./alias-p.sh
alias foo=bar
alias -p
echo foo
$ ksh <(shcomp ./alias-p.sh) # No output
$ ksh ./alias-p.sh
alias foo=bar
foo
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 25
Commits related to this issue
- Fix enum type definition pre-parsing for shcomp and dot/source Parser limitations prevent shcomp or source from handling enum types correctly: $ cat /tmp/colors.sh enum Color_t=(red green bl... — committed to ksh93/ksh by McDutchie 3 years ago
- Fix defining types conditionally and/or in subshells (re: 1dc18346) This commit mitigates the effects of the hack explained in the referenced commit so that dummy built-in command nodes added by the ... — committed to ksh93/ksh by McDutchie 3 years ago
- Fix defining types conditionally and/or in subshells (re: 1dc18346) This commit mitigates the effects of the hack explained in the referenced commit so that dummy built-in command nodes added by the ... — committed to ksh93/ksh by McDutchie 3 years ago
- Fix defining types conditionally and/or in subshells (re: 8ced1daa) This commit mitigates the effects of the hack explained in the referenced commit so that dummy built-in command nodes added by the ... — committed to ksh93/ksh by McDutchie 3 years ago
- Re-fix defining types conditionally or in subshells (re: 5cc9fcc1) New version. I'm pretty sure the problems that forced me to revert it earlier are fixed. This commit mitigates the effects of the h... — committed to ksh93/ksh by McDutchie 3 years ago
- Re-fix defining types conditionally or in subshells (re: f508660d) New version. I'm pretty sure the problems that forced me to revert it earlier are fixed. This commit mitigates the effects of the h... — committed to ksh93/ksh by McDutchie 3 years ago
I have tested the new patch and it’s working well on my end.
Just tested your updated patch and it is working as you described on my machine as well.