]> diplodocus.org Git - nmh/blobdiff - uip/mhlsbr.c
pending-release-notes: add mhshow's "-prefer", and mh-format's %(kibi/kilo)
[nmh] / uip / mhlsbr.c
index 12add6ccca2a793b7fbe5cbb77317a8639684e2d..3bc080856d02f663c51f0a765ed35afdce34c452 100644 (file)
@@ -88,11 +88,12 @@ DEFINE_SWITCH_ARRAY(MHL, mhlswitches);
 #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)
 
 /*
@@ -221,6 +222,8 @@ static struct triple triples[] = {
     { "nocompress",    0,           COMPRESS },
     { "split",         SPLIT,       0 },
     { "nosplit",       0,           SPLIT },
+    { "rtrim",         RTRIM,       0 },
+    { "nortrim",       0,           RTRIM },
     { "addrfield",     ADDRFMT,     DATEFMT },
     { "bell",          BELL,        0 },
     { "nobell",        0,           BELL },
@@ -775,7 +778,7 @@ evalvar (struct mcomp *c1)
            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;
@@ -872,14 +875,18 @@ parse (void)
 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) {
@@ -910,12 +917,12 @@ process (char *folder, char *fname, int ofilen, int ofilec)
            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;
@@ -1181,7 +1188,7 @@ mcomp_format (struct mcomp *c1, struct mcomp *c2)
 
     (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) {
@@ -1237,7 +1244,7 @@ add_queue (struct mcomp **head, struct mcomp **tail, char *name, char *text, int
 {
     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;
@@ -1293,6 +1300,12 @@ putcomp (struct mcomp *c1, struct mcomp *c2, int flag)
 {
     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;
@@ -1308,7 +1321,8 @@ putcomp (struct mcomp *c1, struct mcomp *c2, int flag)
     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;
     }
@@ -1382,21 +1396,33 @@ putcomp (struct mcomp *c1, struct mcomp *c2, int flag)
     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);
 }