From: Philipp Takacs Date: Mon, 19 Nov 2018 02:04:12 +0000 (+0100) Subject: Trailing withspace handling in mhl X-Git-Tag: mmh-0.4~16 X-Git-Url: http://git.marmaro.de/?a=commitdiff_plain;h=de6974ed610ffbfaff335021ea7e359e85c27cfa;p=mmh Trailing withspace handling in mhl If a component has trailing whitespace, e.g., body:component="> ", mhl now trims that whitespace off when filtering blank text lines. Also add a rtrim flag to mhl. This flag removes trailing whitespace in filtert text. The implementation was done by David Levine for nmh commit ea0a6d8112a918809bd03d8126411040d22f2bb8 commit 1aa8f3e11e6d83ee4806abaa132ab9466f02ca5f --- diff --git a/h/prototypes.h b/h/prototypes.h index 69556f9..6526582 100644 --- a/h/prototypes.h +++ b/h/prototypes.h @@ -107,6 +107,7 @@ char *snprintb(char *, size_t, unsigned, char *); int stringdex(char *, char *); char *toabsdir(char *); char *trim(unsigned char *); +char *rtrim(char *); char *trimcpy(unsigned char *); int unputenv(char *); void unquote_string(const char *input, char *output); diff --git a/man/mhl.man1 b/man/mhl.man1 index a80d6bf..9785fe2 100644 --- a/man/mhl.man1 +++ b/man/mhl.man1 @@ -136,6 +136,8 @@ leftadjust flag strip off leading whitespace on each noleftadjust flag don't leftadjust compress flag change newlines in text to spaces nocompress flag don't compress +rtrim flag trim whitespace at end of text lines +nortrim flag retain whitespace at end of text lines (default) split flag don't combine multiple fields into a single field nosplit flag combine multiple fields into diff --git a/sbr/trim.c b/sbr/trim.c index 249faf9..cbf9098 100644 --- a/sbr/trim.c +++ b/sbr/trim.c @@ -31,3 +31,15 @@ trim(unsigned char *cp) return cp; } + +char * +rtrim(char *cp) +{ + char *sp = cp + strlen(cp) - 1; + + while (sp >= cp && isspace(*sp)) { + sp--; + } + *++sp = '\0'; + return cp; +} diff --git a/test/tests/mhl/test-mhl-flags b/test/tests/mhl/test-mhl-flags index 7220e6c..578fd37 100755 --- a/test/tests/mhl/test-mhl-flags +++ b/test/tests/mhl/test-mhl-flags @@ -4,7 +4,6 @@ . "$MH_TEST_COMMON" -test_skip "not implemented yet" cat >`mhpath b` <c_text ? c1->c_text : c1->c_name, sizeof(trimmed_prefix) - 1); + rtrim(trimmed_prefix); cchdr = 0; lm = 0; wid = c1->c_width ? c1->c_width : global.c_width; @@ -897,7 +903,7 @@ putcomp(struct mcomp *c1, struct mcomp *c2, int flag) onelp = NULL; if (c1->c_flags & CLEARTEXT) { - putstr(c1->c_text); + putstr((c1->c_flags & RTRIM) ? rtrim(c1->c_text) : c1->c_text); putstr("\n"); return; } @@ -922,7 +928,11 @@ putcomp(struct mcomp *c1, struct mcomp *c2, int flag) for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++) if (islower(*cp)) *cp = toupper(*cp); - putstr(c1->c_text ? c1->c_text : c1->c_name); + if (*c2->c_text && *c2->c_text != '\n' && *c2->c_text != '\r') { + putstr(c1->c_text ? c1->c_text : c1->c_name); + } else { + putstr(trimmed_prefix); + } if (flag != BODYCOMP) { putstr(": "); if (!(c1->c_flags & SPLIT)) @@ -970,16 +980,23 @@ putcomp(struct mcomp *c1, struct mcomp *c2, int flag) } count += c1->c_offset; - if ((cp = oneline(c2->c_text, c1->c_flags))) - putstr(cp); + if ((cp = oneline(c2->c_text, c1->c_flags))) { + putstr((c1->c_flags & RTRIM) ? rtrim(cp) : cp); + } if (term == '\n') putstr("\n"); while ((cp = oneline(c2->c_text, c1->c_flags))) { lm = count; - if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT)) - putstr(c1->c_text ? c1->c_text : c1->c_name); + if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT)) { + if (*cp) { + putstr(c1->c_text ? c1->c_text : c1->c_name); + } else { + putstr(trimmed_prefix); + } + } if (*cp) - putstr(cp); + putstr((c1->c_flags & RTRIM) ? rtrim(cp) : cp); + if (term == '\n') putstr("\n"); }