#define DATEFMT 0x000800 /* contains dates */
#define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
#define INIT 0x002000 /* initialize component */
+#define RTRIM 0x004000 /* trim trailing whitespace */
#define SPLIT 0x010000 /* split headers (don't concatenate) */
#define NONEWLINE 0x020000 /* don't write trailing newline */
#define NOWRAP 0x040000 /* Don't wrap lines ever */
#define FMTFILTER 0x080000 /* Filter through format filter */
-#define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER"
+#define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017RTRIM\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER"
#define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
/*
{ "nocompress", 0, COMPRESS },
{ "split", SPLIT, 0 },
{ "nosplit", 0, SPLIT },
+ { "rtrim", RTRIM, 0 },
+ { "nortrim", 0, RTRIM },
{ "addrfield", ADDRFMT, DATEFMT },
{ "bell", BELL, 0 },
{ "nobell", 0, BELL },
return 1;
}
- args = (struct arglist *) calloc((size_t) 1, sizeof(struct arglist));
+ args = (struct arglist *) mh_xcalloc ((size_t) 1, sizeof(struct arglist));
if (arglist_tail)
arglist_tail->a_next = args;
static void
process (char *folder, char *fname, int ofilen, int ofilec)
{
- char *cp = NULL;
- FILE *fp = NULL;
+ /* static to prevent "might be clobbered" warning from gcc 4.9.2: */
+ static char *cp;
+ static FILE *fp;
struct mcomp *c1;
struct stat st;
struct arglist *ap;
/* volatile to prevent "might be clobbered" warning from gcc: */
char *volatile fname2 = fname ? fname : "(stdin)";
+ cp = NULL;
+ fp = NULL;
+
switch (setjmp (env)) {
case OK:
if (fname) {
if (arglist_head)
fmt_free(NULL, 1);
- default:
+ default:
if (ontty != PITTY)
SIGNAL (SIGINT, SIG_IGN);
- if (mhl_action == NULL && fp != stdin)
+ if (mhl_action == NULL && fp != stdin && fp != NULL)
fclose (fp);
- free (cp);
+ free (cp);
if (holder.c_text) {
free (holder.c_text);
holder.c_text = NULL;
(q = &pq)->pq_next = NULL;
while ((cp = getname (ap))) {
- if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
+ if ((p = (struct pqpair *) mh_xcalloc ((size_t) 1, sizeof(*p))) == NULL)
adios (NULL, "unable to allocate pqpair memory");
else {
if ((mp = getm (cp, NULL, 0, error, sizeof(error))) == NULL) {
{
struct mcomp *c1;
- if ((c1 = (struct mcomp *) calloc ((size_t) 1, sizeof(*c1))) == NULL)
+ if ((c1 = (struct mcomp *) mh_xcalloc ((size_t) 1, sizeof(*c1))) == NULL)
adios (NULL, "unable to allocate comp memory");
else {
c1->c_flags = flags & ~INIT;
{
int count, cchdr;
char *cp;
+ /*
+ * Create a copy of c1->c_text with trailing whitespace
+ * trimmed, for use with blank lines.
+ */
+ char *trimmed_prefix =
+ rtrim (add (c1->c_text ? c1->c_text : c1->c_name, NULL));
cchdr = 0;
lm = 0;
onelp = NULL;
if (c1->c_flags & CLEARTEXT) {
- putstr (c1->c_text, c1->c_flags);
+ putstr (c1->c_flags & RTRIM ? rtrim (c1->c_text) : c1->c_text,
+ c1->c_flags);
putstr ("\n", c1->c_flags);
return;
}
count += c1->c_offset;
if ((cp = oneline (c2->c_text, c1->c_flags)))
- putstr(cp, c1->c_flags);
+ /* Output line, trimming trailing whitespace if requested. */
+ putstr (c1->c_flags & RTRIM ? rtrim (cp) : cp, c1->c_flags);
if (term == '\n')
putstr ("\n", c1->c_flags);
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, c1->c_flags);
- if (*cp)
- putstr (cp, c1->c_flags);
+ && !(c1->c_flags & NOCOMPONENT)) {
+ /* Output component, trimming trailing whitespace if there
+ is no text on the line. */
+ if (*cp) {
+ putstr (c1->c_text ? c1->c_text : c1->c_name, c1->c_flags);
+ } else {
+ putstr (trimmed_prefix, c1->c_flags);
+ }
+ }
+ if (*cp) {
+ /* Output line, trimming trailing whitespace if requested. */
+ putstr (c1->c_flags & RTRIM ? rtrim (cp) : cp, c1->c_flags);
+ }
if (term == '\n')
putstr ("\n", c1->c_flags);
}
if (flag == BODYCOMP && term == '\n')
c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
+
+ free (trimmed_prefix);
}