Bug 275360 - bashism echo -e in krandrsetup - "Save as default" fails to restore some saved screen resolution settings with dash
Summary: bashism echo -e in krandrsetup - "Save as default" fails to restore some save...
Status: CLOSED FIXED
Alias: None
Product: krandr
Classification: Miscellaneous
Component: general (show other bugs)
Version: unspecified
Platform: Debian testing Linux
: NOR normal
Target Milestone: ---
Assignee: Oswald Buddenhagen
URL:
Keywords:
: 248563 278133 (view as bug list)
Depends on:
Blocks:
 
Reported: 2011-06-10 22:40 UTC by Philippe Cloutier
Modified: 2012-06-20 08:13 UTC (History)
6 users (show)

See Also:
Latest Commit:
Version Fixed In: 4.6.5


Attachments
propsed fix (1.07 KB, patch)
2011-06-12 13:06 UTC, Oswald Buddenhagen
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Philippe Cloutier 2011-06-10 22:40:21 UTC
Version:           unspecified (using KDE 4.6.3) 
OS:                Linux

As reported downstream in http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=630015 krandrsetup has a nasty bashism, echo -e. dash echo does not support -e and rather interprets that as part of the string to print.

krandrsetup ( http://quickgit.kde.org/?p=kde-workspace.git&a=blob&f=kcontrol/randr/krandrstartup ) uses the following blurb to restore the settings, using xrandr:

        echo -e "$krandrrc_display_startupcommands" | \
        while read command; do
            eval "$command"
        done

$krandrrc_display_startupcommands is set in $HOME/.kde/share/config/krandrrc. In my case it is set to:
xrandr --output "LVDS" --pos 0x0 --mode 1366x768 --refresh 60.042\nxrandr --output "CRT1" --pos 0x0 --mode 1920x1080 --refresh 60

Heiner Steven had it set to:
xrandr --output \"VGA-1\" --pos 0x0 --mode 1600x1200 --refresh 75\nxrandr --output \"VGA-1\" --primary

In both cases the command is on 2 lines. In Heiner's case, this made Save as default seem broken for him, as the only significant setting is on the first line. This is what happens on dash:

$ echo -e "echo a\necho b" | while read command; do eval "$command"; done
eval: 1: -e: not found
b
$

In my case I have no issue because the important command for me is on the second line (CRT1 is my main screen). However this finding explains lots of issues with that feature, which had been very unreliable on KDE 4.4, sometimes apparently not working.

krandrsetup was created by Luboš Luňák: http://websvn.kde.org/?revision=1170315&view=revision

Reproducible: Always
Comment 1 Philippe Cloutier 2011-06-10 22:46:29 UTC
Oh, forgot to write that dash is the default /bin/sh on Debian, for performance reasons (although the "default shell" is still bash).

This is probably the explanation for 261285 and 265710.
Comment 2 Philippe Cloutier 2011-06-10 23:44:22 UTC
Hum OK in fact krandrsetup only appeared in 4.6 so #265710 and my problems on KDE 4.4 must rather be instances of #183143.

As krandrsetup is a shell script, there is actually a very simple workaround. Assuming you have bash, edit /usr/bin/krandrsetup changing its first line from
#!/bin/sh
to
#!/bin/bash

Heiner Steven, who discovered the problem, proposed an actual fix using printf rather than echo in the Debian bug report.
Comment 3 Philippe Cloutier 2011-06-12 00:11:12 UTC
I'm forwarding a message from the original reporter about how he found the problem. I think errors from krandrstartup should not be discarded in case it has more bugs.


I tried to find the root cause of the problem by reading the
"startkde" script and the involved helper scripts and functions,
but could not find a problem. Afterwards I changed "startkde"
to include debugging output ("set -vx"), which appeared in the
file

    $HOME/.xsession-errors

Still the problem was not visible. Only after I removed the
stderr-redirection of "krandrstartup" (change

    . krandrstartup 2>/dev/null

to

    . krandrstartup #2>/dev/null

) I saw the error message.
Comment 4 Oswald Buddenhagen 2011-06-12 13:06:55 UTC
Created attachment 60928 [details]
propsed fix

please test
Comment 5 Philippe Cloutier 2011-06-12 20:18:50 UTC
That kind of inverts the bug. It will work if $krandrrc_display_startupcommands contains a single command, but only then.

$ eval "echo a\necho b"
anecho b
chealer@vinci:/etc/cron.weekly$
Comment 6 Oswald Buddenhagen 2011-06-12 22:41:05 UTC
you didn't actually test it or read the commit message, huh?
Comment 7 Philippe Cloutier 2011-06-16 03:41:19 UTC
I didn't actually test, but did read the commit message, just found it to be wrong. In fact there's an important confusion. StartupCommands can actually contain escapes (for newlines). Mine for example has one:
StartupCommands=xrandr --output "LVDS" --pos 0x0 --mode 1366x768 --refresh 60.042\nxrandr --output "CRT1" --pos 0x0 --mode 1920x1080 --refresh 60

The literal format of Heiner's StartupCommands are on the downstream report:
StartupCommands="xrandr --output \"VGA-1\" --pos 0x0 --mode 1600x1200 --refresh 75
	xrandr --output \"VGA-1\" --primary"

As you can see, the format changed. Heiner's string contains a literal newline and is double quoted. Mine is unquoted and contains an encoded newline. The StartupCommands format must have changed, I don't know why nor when. I asked Heiner to test resetting and generating a new StartupCommands, and he confirmed he now has a different format (same as mine).

Therefore the "complicated" code from Luboš must be intended. The echo is required for the new format. FWIW, using ";" as separator would probably be simpler than the new format, allowing your patch to work.

This is quite confusing, it should be documented somewhere, probably just around the problematic lines.

I believe the following structure would work:
eval "`echo -e "$krandrrc_display_startupcommands"`"

The backticks for command substitution are POSIX. For echo -e, I suggest following Heiner's suggestion.
Comment 8 Oswald Buddenhagen 2011-06-19 13:24:50 UTC
heiner's config file is plain invalid and i have no idea how he got it that way - kconfig would not write that, and i found no indication in krandr's history that it would bypass kconfig at any time. probably some custom scripting or manual manipulation.

your config file will work just fine with my patch. just try it. ;)
Comment 9 Philippe Cloutier 2011-06-19 19:43:07 UTC
I tested Oswald's patch and it actually works. The reason is that $krandrrc_display_startupcommands contains actual newlines, no escape sequences. I don't know where $krandrrc_display_startupcommands is defined, but as long as it interprets StartupCommands the same way, Oswald's patch is sufficient.
Comment 10 Oswald Buddenhagen 2011-06-22 08:42:31 UTC
*** Bug 248563 has been marked as a duplicate of this bug. ***
Comment 11 Oswald Buddenhagen 2011-06-22 08:44:57 UTC
Git commit 14d16ce823a1bf4325e0ab632a4893f88b88e231 by Oswald Buddenhagen.
Committed on 12/06/2011 at 12:47.
Pushed by ossi into branch 'KDE/4.6'.

remove pointless bash-ism

there are no escapes to process here.
and as the newlines appear literally in the first place, there is also
no reason for using a loop.

BUG: 275360
FIXED-IN: 4.6.5
(cherry picked from commit 175afa1e2569eeee204d660628c06928882e034f)

M  +1    -4    kcontrol/randr/krandrstartup     

http://commits.kde.org/kde-workspace/14d16ce823a1bf4325e0ab632a4893f88b88e231
Comment 12 Oswald Buddenhagen 2011-06-22 08:45:13 UTC
Git commit 175afa1e2569eeee204d660628c06928882e034f by Oswald Buddenhagen.
Committed on 12/06/2011 at 12:47.
Pushed by ossi into branch 'master'.

remove pointless bash-ism

there are no escapes to process here.
and as the newlines appear literally in the first place, there is also
no reason for using a loop.

BUG: 275360
FIXED-IN: 4.6.5

M  +1    -4    kcontrol/randr/krandrstartup     

http://commits.kde.org/kde-workspace/175afa1e2569eeee204d660628c06928882e034f
Comment 13 apodtele 2011-06-25 03:23:24 UTC
What actual newlines? This is frigging idiotic.
Use ";" like shells do. However:

The problem is not newlines - the problem is multiple commands.

Where did you read that it is Ok to split --output's?
The man page certainly suggests using using all --output's at once.
Comment 14 Myriam Schweingruber 2012-06-20 08:13:56 UTC
*** Bug 278133 has been marked as a duplicate of this bug. ***