From 3457446a0430fdd501537f5ecf39e214c45b8160 Mon Sep 17 00:00:00 2001 From: Martin Lund Date: Tue, 19 Sep 2017 12:19:17 +0200 Subject: [PATCH] Add support for hexidecimal mode A new key command 'ctrl-t h' is introduced which toggles between hexidecial mode and normal mode. When in hexidecimal mode data received will be printed in hexidecimal. --- man/tio.1 | 2 ++ src/include/tio/tty.h | 4 ++++ src/tty.c | 42 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/man/tio.1 b/man/tio.1 index ce0fc70..941a059 100644 --- a/man/tio.1 +++ b/man/tio.1 @@ -74,6 +74,8 @@ List available key commands Send serial break (triggers SysRq on Linux, etc.) .IP "\fBctrl-t c" Show configuration (baudrate, databits, etc.) +.IP "\fBctrl-t h" +Toggle hexidecimal mode .IP "\fBctrl-t l" Clear screen .IP "\fBctrl-t q" diff --git a/src/include/tio/tty.h b/src/include/tio/tty.h index 9941604..3004009 100644 --- a/src/include/tio/tty.h +++ b/src/include/tio/tty.h @@ -25,12 +25,16 @@ #define KEY_QUESTION 0x3f #define KEY_B 0x62 #define KEY_C 0x63 +#define KEY_H 0x68 #define KEY_L 0x6C #define KEY_Q 0x71 #define KEY_S 0x73 #define KEY_T 0x74 #define KEY_CTRL_T 0x14 +#define NORMAL 0 +#define HEX 1 + void stdout_configure(void); void stdout_restore(void); void tty_configure(void); diff --git a/src/tty.c b/src/tty.c index 80da196..df7b511 100644 --- a/src/tty.c +++ b/src/tty.c @@ -47,6 +47,8 @@ static unsigned long rx_total = 0, tx_total = 0; static bool connected = false; static bool tainted = false; static int fd; +static bool print_mode = NORMAL; +static void (*print)(char c); #define tio_printf(format, args...) \ { \ @@ -55,6 +57,16 @@ static int fd; tainted = false; \ } +static void print_hex(char c) +{ + printf("%02x ", (unsigned char) c); +} + +static void print_normal(char c) +{ + putchar(c); +} + void handle_command_sequence(char input_char, char previous_char, char *output_char, bool *forward) { char unused_char; @@ -78,16 +90,19 @@ void handle_command_sequence(char input_char, char previous_char, char *output_c tio_printf(" ctrl-t ? List available key commands"); tio_printf(" ctrl-t b Send break"); tio_printf(" ctrl-t c Show configuration"); + tio_printf(" ctrl-t h Toggle hexidecimal mode"); tio_printf(" ctrl-t l Clear screen"); tio_printf(" ctrl-t q Quit"); tio_printf(" ctrl-t s Show statistics"); tio_printf(" ctrl-t t Send ctrl-t key code"); *forward = false; break; + case KEY_B: tcsendbreak(fd, 0); *forward = false; break; + case KEY_C: tio_printf("Configuration:"); tio_printf(" TTY device: %s", option.tty_device); @@ -101,23 +116,45 @@ void handle_command_sequence(char input_char, char previous_char, char *output_c tio_printf(" Log file: %s", option.log_filename); *forward = false; break; + + case KEY_H: + /* Toggle hexidecimal printing mode */ + if (print_mode == NORMAL) + { + print = print_hex; + print_mode = HEX; + tio_printf("Switched to hexidecimal mode"); + } + else + { + print = print_normal; + print_mode = NORMAL; + tio_printf("Switched to normal mode"); + } + *forward = false; + break; + case KEY_L: status = system("clear"); *forward = false; break; + case KEY_Q: /* Exit upon ctrl-t q sequence */ exit(EXIT_SUCCESS); + case KEY_S: /* Show tx/rx statistics upon ctrl-t s sequence */ tio_printf("Statistics:"); tio_printf(" Sent %lu bytes, received %lu bytes", tx_total, rx_total); *forward = false; break; + case KEY_T: /* Send ctrl-t key code upon ctrl-t t sequence */ *output_char = KEY_CTRL_T; break; + default: /* Ignore unknown ctrl-t escaped keys */ *forward = false; @@ -162,6 +199,9 @@ void stdout_configure(void) tio_printf("tio v%s", VERSION); tio_printf("Press ctrl-t q to quit"); + /* At start use normal print function */ + print = print_normal; + /* Make sure we restore old stdout settings on exit */ atexit(&stdout_restore); } @@ -459,7 +499,7 @@ int tty_connect(void) rx_total++; /* Print received tty character to stdout */ - putchar(input_char); + print(input_char); fflush(stdout); /* Write to log */