Skip to content
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

sh_deparse(): braces ignored in and/or lists #530

Open
McDutchie opened this issue Sep 1, 2022 · 0 comments
Open

sh_deparse(): braces ignored in and/or lists #530

McDutchie opened this issue Sep 1, 2022 · 0 comments
Labels
1.1 Issue relevant to dev branch (v1.1.*) bug Something is not working

Comments

@McDutchie
Copy link

McDutchie commented Sep 1, 2022

(note: this bug does not apply to the 1.0.* releases)

Background info: f21ace5

Reproducer:

f() { true || { false && echo WRONG; }; }
typeset -f f
eval "$(typeset -f f)"
f

Actual output:

f()
{	true || false && echo WRONG
}
WRONG

Expected output: something like

f()
{       true || {
                false && echo WRONG
        }
}

The braces should be preserved in the generated typeset -f output as not having them changes the meaning of the code, and WRONG is printed.

I have no idea where or how to solve this issue. The problem is here somewhere:

case TAND:
cp = "&&";
goto andor;
case TORF:
cp = "||";
goto andor;
case TFIL:
cp = "|";
andor:
{
int bracket = 0;
if(t->tre.tretyp&TTEST)
{
tflags |= NO_NEWLINE;
if(!(tflags&NO_BRACKET))
{
p_keyword("[[",BEGIN|NOTAB);
tflags |= NO_BRACKET;
bracket=1;
}
}
p_tree(t->lst.lstlef,NEED_BRACE|NO_NEWLINE|(tflags&NO_BRACKET));
begin_line = 0;
if(tflags&FALTPIPE)
{
Shnode_t *tt = t->lst.lstrit;
if(tt->tre.tretyp!=TFIL || !(tt->lst.lstlef->tre.tretyp&FALTPIPE))
{
sfputc(outfile,'\n');
return;
}
}
sfputr(outfile,cp,here_doc?'\n':' ');
if(here_doc)
{
here_body(here_doc);
here_doc = 0;
}
level++;
p_tree(t->lst.lstrit,tflags|NEED_BRACE);
if(bracket)
p_keyword("]]",END);
level--;
break;
}

A naïve patch could reuse code like this:

if(needbrace)
p_keyword("{",BEGIN);
if(needbrace)
p_keyword("}",END);
– but if that is copied over to the &&/|| code, we end up with far too many braces. The braces should be reproduced only if they were in the original code, but I have no idea how the deparser can determine that. Any ideas appreciated.

@McDutchie McDutchie added bug Something is not working 1.1 Issue relevant to dev branch (v1.1.*) labels Sep 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.1 Issue relevant to dev branch (v1.1.*) bug Something is not working
Projects
None yet
Development

No branches or pull requests

1 participant