From 1bc25e920ab25e8dabf279b43cd918ed84f9c37d Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Mon, 2 Oct 2017 00:00:00 -0500 Subject: [PATCH] top: correct an insidious occasional truncation buglet With the help of our Swedish translator, hopefully the final buglet has now been vanquished in the multi-byte translation support. This one was a real nasty bugger. Although it didn't occur with every terminal emulator, occasionally random text lines were being chopped off. As it turns out, those terminals were blameless. There were two separate places in top's show_special routine where potential multi-byte sequences were inadequately addressed. Solution: exploit existing utf-8 functions. [ it also became apparent that the translation hints ] [ in the top_nls module were deficient. so a special ] [ caution was added regarding the final line of txt. ] Reference(s): https://gitlab.com/procps-ng/procps/issues/68 Signed-off-by: Jim Warner --- top/top.c | 4 ++-- top/top.h | 3 +++ top/top_nls.c | 11 +++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/top/top.c b/top/top.c index 394b8a94..c7e8e52d 100644 --- a/top/top.c +++ b/top/top.c @@ -771,7 +771,7 @@ static void show_special (int interact, const char *glob) { case 5: case 6: case 7: case 8: *sub_end = '\0'; snprintf(tmp, sizeof(tmp), "%s%.*s%s" - , Curwin->captab[ch], room, sub_beg, Caps_off); + , Curwin->captab[ch], utf8_embody(sub_beg, room), sub_beg, Caps_off); rp = scat(rp, tmp); room -= (sub_end - sub_beg); room += utf8_delta(sub_beg); @@ -794,7 +794,7 @@ static void show_special (int interact, const char *glob) { it probably means caller wants to retain cursor position on this final line. That, in turn, means we're interactive and so we'll just do our 'fit-to-screen' thingy while also leaving room for the cursor... */ - if (*glob) PUTT("%.*s", Screen_cols -1, glob); + if (*glob) PUTT("%.*s", utf8_embody(glob, Screen_cols - 1), glob); } // end: show_special diff --git a/top/top.h b/top/top.h index fb8fb350..63380927 100644 --- a/top/top.h +++ b/top/top.h @@ -69,6 +69,9 @@ /* For the impetus and NUMA/Node prototype design, thanks to: Lance Shelton - April, 2013 */ + /* For prompting & helping with top's utf-8 support, thanks to: + Göran Uddeborg - September, 2017 */ + // pretend as if #define _GNU_SOURCE char *strcasestr(const char *haystack, const char *needle); diff --git a/top/top_nls.c b/top/top_nls.c index 05f8c206..d72d875d 100644 --- a/top/top_nls.c +++ b/top/top_nls.c @@ -520,6 +520,17 @@ static void build_uniq_nlstab (void) { . If you would like additional information regarding these strings, . please see the prologue to the show_special function in the top.c . source file. + . + . Caution: + . The next three items represent pages for interacting with a user. + . In all cases, the last lines of text must be treated with care. + . + . They must not end with a newline character so that at runtime the + . cursor will remain on that final text line. + . + . Also, the special sequences (tilde+number) must not appear on the + . last line (the one without the newline). So please avoid any line + . wraps that could place them there. . */ Uniq_nlstab[KEYS_helpbas_fmt] = _("" -- 2.40.0