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

Most upvoted comments

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.