diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-25 21:13:47 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-25 21:13:47 -0600 |
| commit | 6ea4a5ac01d04a3ed857f37634265bca25b89edb (patch) | |
| tree | 69b9e9156b5051fb3a99646835cf4ca1699fd645 | |
| parent | 9dd085c8ed0853f4d96b4de0b9aab71be537240e (diff) | |
| download | ztop-6ea4a5ac01d04a3ed857f37634265bca25b89edb.tar.gz | |
tui: translate input from EBCDIC, restore the cursor
The tty converts output but not input, so keys and cursor reports arrive
as EBCDIC in raw mode: 'q' is 0x98 and the report starts 0x27 0x5B.
Translate each read with __e2a_l before decoding, and make the code that
looks for ESC use the ASCII 0x1b it sees afterwards. Leaving the screen
also forgot to show the cursor again.
| -rw-r--r-- | src/tui.c | 18 |
1 files changed, 16 insertions, 2 deletions
@@ -16,11 +16,23 @@ #ifdef __MVS__ #define ESCS "\x27" /* EBCDIC ESC; the tty converts it to 0x1b */ -#define ESC_CHAR 0x27u #else #define ESCS "\x1b" +#endif + +/* Input is not converted: keys and cursor reports arrive EBCDIC even in + * raw mode, so every read is translated before it is looked at. */ #define ESC_CHAR 0x1bu + +static void to_ascii(unsigned char *b, size_t n) +{ +#ifdef __MVS__ + __e2a_l((char *)b, n); +#else + (void)b; + (void)n; #endif +} #define ALT_ON ESCS "[?1049h" #define ALT_OFF ESCS "[?1049l" @@ -51,7 +63,7 @@ static void restore(struct tui *t) { if (!t->open) return; - wr(t->fd_out, RESET ALT_OFF, strlen(RESET ALT_OFF)); + wr(t->fd_out, RESET CUR_ON ALT_OFF, strlen(RESET CUR_ON ALT_OFF)); (void)tcsetattr(t->fd_in, TCSANOW, &t->orig); } @@ -147,6 +159,7 @@ static bool cpr_size(int in, int outfd, int *w, int *h) ssize_t k = read(in, b + n, sizeof b - (size_t)n); if (k <= 0) continue; + to_ascii(b + n, (size_t)k); for (ssize_t i = 0; i < k; i++) { unsigned char c = b[n + (size_t)i]; if (!esc) { @@ -547,6 +560,7 @@ int tui_key(struct tui *t, int timeout_ms) continue; return TUI_KEY_ESC; } + to_ascii(t->in + t->inlen, (size_t)n); t->inlen += (int)n; } } |
