diff options
Diffstat (limited to 'ui/ncurses')
-rw-r--r-- | ui/ncurses/Makefile.am | 2 | ||||
-rw-r--r-- | ui/ncurses/console-codes.c | 178 | ||||
-rw-r--r-- | ui/ncurses/console-codes.h | 20 | ||||
-rw-r--r-- | ui/ncurses/nc-cui.c | 28 |
4 files changed, 228 insertions, 0 deletions
diff --git a/ui/ncurses/Makefile.am b/ui/ncurses/Makefile.am index 40e11b8..3bc5254 100644 --- a/ui/ncurses/Makefile.am +++ b/ui/ncurses/Makefile.am @@ -24,6 +24,8 @@ ui_ncurses_libpbnc_la_SOURCES = \ ui/ncurses/nc-config.c \ ui/ncurses/nc-config.h \ ui/ncurses/nc-config-help.c \ + ui/ncurses/console-codes.c \ + ui/ncurses/console-codes.h \ ui/ncurses/nc-cui.c \ ui/ncurses/nc-cui.h \ ui/ncurses/nc-cui-help.c \ diff --git a/ui/ncurses/console-codes.c b/ui/ncurses/console-codes.c new file mode 100644 index 0000000..5c28615 --- /dev/null +++ b/ui/ncurses/console-codes.c @@ -0,0 +1,178 @@ +/* + * Copyright (C) 2017 IBM Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#if defined(HAVE_CONFIG_H) +#include "config.h" +#endif + +#include <ctype.h> +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> + +#include "talloc/talloc.h" + +#ifndef PETITBOOT_TEST +#include "log/log.h" +#include "nc-scr.h" +#endif + +#include "console-codes.h" + +#define ESC_CHAR 033 +#define CSI_CHAR '[' +#define INTER_CHAR_START 040 +#define INTER_CHAR_END 057 +#define ESC_SEQUENCE_FINAL_START 060 +#define ESC_SEQUENCE_FINAL_END 0176 +#define CTRL_SEQUENCE_FINAL_START 0100 +#define CTRL_SEQUENCE_FINAL_END 0176 +#define DEC_PARAMETER 077 + +enum console_sequence_state { + CONSOLE_STATE_START, + CONSOLE_STATE_ESC_SEQ, + CONSOLE_STATE_CTRL_SEQ_START, + CONSOLE_STATE_CTRL_SEQ, + CONSOLE_STATE_DONE, + CONSOLE_STATE_CONFUSED, +}; + +static inline bool is_intermediate(signed char c) +{ + return c > INTER_CHAR_START && c < INTER_CHAR_END; +} + +static inline bool is_parameter(signed char c) +{ + return (c >= 060 && c <= 071) || c == 073; +} + +static inline bool is_escape_final(signed char c) +{ + return c >= ESC_SEQUENCE_FINAL_START && c < ESC_SEQUENCE_FINAL_END; +} + +static inline bool is_control_final(signed char c) +{ + return c >= CTRL_SEQUENCE_FINAL_START && c <= CTRL_SEQUENCE_FINAL_END; +} + +static char console_sequence_getch(char **sequence) +{ + signed char c = getch(); + + if (c != ERR) + *sequence = talloc_asprintf_append(*sequence, "%c", c); + return c; +} + +/* + * Catch terminal control sequences that have accidentally been sent to + * Petitboot. These are of the form + * ESC I .. I F + * where I is an Intermediate Character and F is a Final Character, eg: + * ESC ^ [ ? 1 ; 0 c + * or ESC # 6 + * + * This is based off the definitions provided by + * https://vt100.net/docs/vt100-ug/contents.html + */ +char *handle_control_sequence(void *ctx, signed char start) +{ + enum console_sequence_state state = CONSOLE_STATE_START; + bool in_sequence = true; + signed char c; + char *seq; + + if (start != ESC_CHAR) { + pb_log("%s: Called with non-escape character: 0%o\n", + __func__, start); + return NULL; + } + + seq = talloc_asprintf(ctx, "%c", start); + + while (in_sequence) { + switch (state) { + case CONSOLE_STATE_START: + c = console_sequence_getch(&seq); + if (c == CSI_CHAR) + state = CONSOLE_STATE_CTRL_SEQ_START; + else if (is_intermediate(c)) + state = CONSOLE_STATE_ESC_SEQ; + else if (is_escape_final(c)) + state = CONSOLE_STATE_DONE; + else if (c != ERR) { + /* wait on c == ERR */ + pb_debug("Unexpected start: \\x%x\n", c); + state = CONSOLE_STATE_CONFUSED; + } + break; + case CONSOLE_STATE_ESC_SEQ: + c = console_sequence_getch(&seq); + if (is_intermediate(c)) + state = CONSOLE_STATE_ESC_SEQ; + else if (is_escape_final(c)) + state = CONSOLE_STATE_DONE; + else if (c != ERR) { + /* wait on c == ERR */ + pb_debug("Unexpected character after intermediate: \\x%x\n", + c); + state = CONSOLE_STATE_CONFUSED; + } + break; + case CONSOLE_STATE_CTRL_SEQ_START: + c = console_sequence_getch(&seq); + if (is_intermediate(c) || is_parameter(c) || + c == DEC_PARAMETER) + state = CONSOLE_STATE_CTRL_SEQ; + else if (is_control_final(c)) + state = CONSOLE_STATE_DONE; + else if (c != ERR) { + /* wait on c == ERR */ + pb_debug("Unexpected character in param string: \\x%x\n", + c); + state = CONSOLE_STATE_CONFUSED; + } + break; + case CONSOLE_STATE_CTRL_SEQ: + c = console_sequence_getch(&seq); + if (is_intermediate(c) || is_parameter(c)) + state = CONSOLE_STATE_CTRL_SEQ; + else if (is_control_final(c)) + state = CONSOLE_STATE_DONE; + else if (c != ERR) { + /* wait on c == ERR */ + pb_debug("Unexpected character in param string: \\x%x\n", + c); + state = CONSOLE_STATE_CONFUSED; + } + break; + case CONSOLE_STATE_DONE: + in_sequence = false; + break; + case CONSOLE_STATE_CONFUSED: + /* fall-through */ + default: + pb_debug("We got lost interpreting a control sequence!\n"); + seq = talloc_asprintf_append(seq, "..."); + in_sequence = false; + break; + }; + } + + return seq; +} diff --git a/ui/ncurses/console-codes.h b/ui/ncurses/console-codes.h new file mode 100644 index 0000000..97c0e52 --- /dev/null +++ b/ui/ncurses/console-codes.h @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2017 IBM Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#if !defined(_PB_UI_CONSOLE_CODES) +#define _PB_UI_CONSOLE_CODES + +char *handle_control_sequence(void *ctx, signed char start); + +#endif diff --git a/ui/ncurses/nc-cui.c b/ui/ncurses/nc-cui.c index 6ced24c..8060510 100644 --- a/ui/ncurses/nc-cui.c +++ b/ui/ncurses/nc-cui.c @@ -21,6 +21,7 @@ #endif #include <assert.h> +#include <ctype.h> #include <errno.h> #include <stdlib.h> #include <string.h> @@ -45,6 +46,7 @@ #include "nc-statuslog.h" #include "nc-subset.h" #include "nc-plugin.h" +#include "console-codes.h" extern const struct help_text main_menu_help_text; extern const struct help_text plugin_menu_help_text; @@ -524,6 +526,9 @@ static bool process_global_keys(struct cui *cui, int key) static int cui_process_key(void *arg) { struct cui *cui = cui_from_arg(arg); + unsigned int i; + char *sequence; + int grab; assert(cui->current); @@ -535,6 +540,29 @@ static int cui_process_key(void *arg) if (c == ERR) break; + if (c == 27) { + /* + * If this is a console code sequence try to parse it + * and don't treat this as a key press. + */ + grab = getch(); + if (grab != ERR && grab != 27) { + ungetch(grab); + pb_debug("%s: Caught unhandled command sequence\n", + __func__); + sequence = handle_control_sequence(cui, c); + pb_debug("Caught sequence "); + if (sequence) { + pb_debug("(%zu): ", strlen(sequence)); + for (i = 0; i < strlen(sequence); i++) + pb_debug("0%o ", sequence[i]); + pb_debug("\n"); + } else + pb_debug("(0): (none)\n"); + continue; + } + } + if (!cui->has_input) { cui->has_input = true; if (cui->client) { |