[libvirt] [PATCH] virsh: Clarify escape sequence

Currently, we put no strains on escape sequence possibly leaving users with console that cannot be terminated. However, not all ASCII characters can be used as escape sequence. Only those falling in @ - _ can be; implement and document this constraint. --- tools/console.c | 3 ++- tools/virsh.c | 13 ++++++++++++- tools/virsh.pod | 3 ++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/tools/console.c b/tools/console.c index ca226c3..0f30b95 100644 --- a/tools/console.c +++ b/tools/console.c @@ -34,6 +34,7 @@ # include <errno.h> # include <unistd.h> # include <signal.h> +# include <c-ctype.h> # include "internal.h" # include "console.h" @@ -292,7 +293,7 @@ static char vshGetEscapeChar(const char *s) { if (*s == '^') - return CONTROL(s[1]); + return CONTROL(c_islower(s[1]) ? c_toupper(s[1]) : s[1]); return *s; } diff --git a/tools/virsh.c b/tools/virsh.c index 1ed2dda..cfdd040 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -19879,6 +19879,16 @@ vshShowVersion(vshControl *ctl ATTRIBUTE_UNUSED) vshPrint(ctl, "\n"); } +static bool +vshAllowedEscapeChar(char c) +{ + /* Allowed escape characters: + * a-z A-Z @ [ \ ] ^ _ + */ + return ('a' <= c && c <= 'z') || + ('@' <= c && c <= '_'); +} + /* * argv[]: virsh [options] [command] * @@ -19942,7 +19952,8 @@ vshParseArgv(vshControl *ctl, int argc, char **argv) case 'e': len = strlen(optarg); - if ((len == 2 && *optarg == '^') || + if ((len == 2 && *optarg == '^' && + vshAllowedEscapeChar(optarg[1])) || (len == 1 && *optarg != '^')) { ctl->escapeChar = optarg; } else { diff --git a/tools/virsh.pod b/tools/virsh.pod index d4971a3..a60e667 100644 --- a/tools/virsh.pod +++ b/tools/virsh.pod @@ -95,7 +95,8 @@ Output elapsed time information for each command. =item B<-e>, B<--escape> I<string> Set alternative escape sequence for I<console> command. By default, -telnet's B<^]> is used. +telnet's B<^]> is used. Allowed characters when using hat notation are: +alphabetic character, @, [, ], \, ^, _. =back -- 1.7.8.5

On 04/03/2012 07:10 AM, Michal Privoznik wrote:
Currently, we put no strains on escape sequence possibly leaving users with console that cannot be terminated. However, not all ASCII characters can be used as escape sequence. Only those falling in @ - _ can be; implement and document this constraint.
vshGetEscapeChar(const char *s) { if (*s == '^') - return CONTROL(s[1]); + return CONTROL(c_islower(s[1]) ? c_toupper(s[1]) : s[1]);
Unlike toupper(), c_toupper() is safe even on non-alphabetic characters (that's part of why gnulib wrote "c-ctype.h"); you can shorten this to: return CONTROL(c_toupper(s[1]));
return *s; } diff --git a/tools/virsh.c b/tools/virsh.c index 1ed2dda..cfdd040 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -19879,6 +19879,16 @@ vshShowVersion(vshControl *ctl ATTRIBUTE_UNUSED) vshPrint(ctl, "\n"); }
+static bool +vshAllowedEscapeChar(char c) +{ + /* Allowed escape characters: + * a-z A-Z @ [ \ ] ^ _ + */ + return ('a' <= c && c <= 'z') || + ('@' <= c && c <= '_');
Assumes ASCII encoding, but I guess it's okay (no one has really complained about compiling libvirt on EBCDIC). ACK with nit fixed. -- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

On 03.04.2012 16:39, Eric Blake wrote:
On 04/03/2012 07:10 AM, Michal Privoznik wrote:
Currently, we put no strains on escape sequence possibly leaving users with console that cannot be terminated. However, not all ASCII characters can be used as escape sequence. Only those falling in @ - _ can be; implement and document this constraint.
vshGetEscapeChar(const char *s) { if (*s == '^') - return CONTROL(s[1]); + return CONTROL(c_islower(s[1]) ? c_toupper(s[1]) : s[1]);
Unlike toupper(), c_toupper() is safe even on non-alphabetic characters (that's part of why gnulib wrote "c-ctype.h"); you can shorten this to: ('@' <= c && c <= '_');
ACK with nit fixed.
Fixed and pushed. Thanks. Michal

On Tue, Apr 03, 2012 at 03:10:32PM +0200, Michal Privoznik wrote:
Currently, we put no strains on escape sequence possibly leaving users with console that cannot be terminated. However, not all ASCII characters can be used as escape sequence. Only those falling in @ - _ can be; implement and document this constraint. --- tools/console.c | 3 ++- tools/virsh.c | 13 ++++++++++++- tools/virsh.pod | 3 ++- 3 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/tools/console.c b/tools/console.c index ca226c3..0f30b95 100644 --- a/tools/console.c +++ b/tools/console.c @@ -34,6 +34,7 @@ # include <errno.h> # include <unistd.h> # include <signal.h> +# include <c-ctype.h>
# include "internal.h" # include "console.h" @@ -292,7 +293,7 @@ static char vshGetEscapeChar(const char *s) { if (*s == '^') - return CONTROL(s[1]); + return CONTROL(c_islower(s[1]) ? c_toupper(s[1]) : s[1]);
I'm not really understanding why you need to convert to uppercase here. I tested it though, and confirmed you are doing the right thing though. If you understand, then can you add a comment explaining why we need uppercase. Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
participants (3)
-
Daniel P. Berrange
-
Eric Blake
-
Michal Privoznik