]> diplodocus.org Git - nmh/blobdiff - uip/mhlsbr.c
Deference char pointer to test for empty string instead of strlen(3).
[nmh] / uip / mhlsbr.c
index 3a53941d8df13c211e3c0675574537bf083f882f..ee045ea0dd6e0e930d898ef179fbd8554c5551af 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 },
@@ -400,7 +403,8 @@ mhl (int argc, char **argv)
                case SLEEPSW:
                    if (!(cp = *argp++) || *cp == '-')
                        adios (NULL, "missing argument to %s", argp[-2]);
-                   sleepsw = atoi (cp);/* ZERO ok! */
+                   else
+                       sleepsw = atoi (cp);/* ZERO ok! */
                    continue;
 
                case PROGSW:
@@ -422,13 +426,13 @@ mhl (int argc, char **argv)
                case LENSW: 
                    if (!(cp = *argp++) || *cp == '-')
                        adios (NULL, "missing argument to %s", argp[-2]);
-                   if ((length = atoi (cp)) < 1)
+                   else if ((length = atoi (cp)) < 1)
                        adios (NULL, "bad argument %s %s", argp[-2], cp);
                    continue;
                case WIDTHSW: 
                    if (!(cp = *argp++) || *cp == '-')
                        adios (NULL, "missing argument to %s", argp[-2]);
-                   if ((width = atoi (cp)) < 1)
+                   else if ((width = atoi (cp)) < 1)
                        adios (NULL, "bad argument %s %s", argp[-2], cp);
                    continue;
 
@@ -439,13 +443,13 @@ mhl (int argc, char **argv)
                case ISSUESW:
                    if (!(cp = *argp++) || *cp == '-')
                        adios (NULL, "missing argument to %s", argp[-2]);
-                   if ((issue = atoi (cp)) < 1)
+                   else if ((issue = atoi (cp)) < 1)
                        adios (NULL, "bad argument %s %s", argp[-2], cp);
                    continue;
                case VOLUMSW:
                    if (!(cp = *argp++) || *cp == '-')
                        adios (NULL, "missing argument to %s", argp[-2]);
-                   if ((volume = atoi (cp)) < 1)
+                   else if ((volume = atoi (cp)) < 1)
                        adios (NULL, "bad argument %s %s", argp[-2], cp);
                    continue;
 
@@ -591,7 +595,7 @@ mhl_format (char *file, int length, int width)
            *cp = 0;
 
        if (*bp == ':') {
-           c1 = add_queue (&fmthd, &fmttl, NULL, bp + 1, CLEARTEXT);
+           (void) add_queue (&fmthd, &fmttl, NULL, bp + 1, CLEARTEXT);
            continue;
        }
 
@@ -774,7 +778,7 @@ evalvar (struct mcomp *c1)
            return 1;
        }
 
-       args = (struct arglist *) calloc((size_t) 1, sizeof(struct arglist));
+       NEW0(args);
 
        if (arglist_tail)
            arglist_tail->a_next = args;
@@ -871,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) {
@@ -900,6 +908,7 @@ process (char *folder, char *fname, int ofilen, int ofilec)
            if (ontty != PITTY)
                SIGNAL (SIGINT, intrser);
            mhlfile (fp, cp, ofilen, ofilec);  /* FALL THROUGH! */
+            free (cp);
 
            for (ap = arglist_head; ap; ap = ap->a_next) {
                fmt_free(ap->a_fmt, 0);
@@ -909,12 +918,11 @@ 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);
            if (holder.c_text) {
                free (holder.c_text);
                holder.c_text = NULL;
@@ -973,7 +981,9 @@ mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
                    }
                    fflush (stdout);
                    buf[0] = 0;
-                   read (fileno (stdout), buf, sizeof(buf));
+                   if (read (fileno (stdout), buf, sizeof(buf)) < 0) {
+                       advise ("stdout", "read");
+                   }
                }
                if (strchr(buf, '\n')) {
                    if ((global.c_flags & CLEARSCR))
@@ -1178,17 +1188,15 @@ 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)
-           adios (NULL, "unable to allocate pqpair memory");
-
-       if ((mp = getm (cp, NULL, 0, error, sizeof(error))) == NULL) {
-           p->pq_text = getcpy (cp);
-           p->pq_error = getcpy (error);
-       } else {
-           p->pq_text = getcpy (mp->m_text);
-           mnfree (mp);
-       }
-       q = (q->pq_next = p);
+       NEW0(p);
+        if ((mp = getm (cp, NULL, 0, error, sizeof(error))) == NULL) {
+            p->pq_text = getcpy (cp);
+            p->pq_error = getcpy (error);
+        } else {
+            p->pq_text = getcpy (mp->m_text);
+            mnfree (mp);
+        }
+        q = (q->pq_next = p);
     }
 
     for (p = pq.pq_next; p; p = q) {
@@ -1206,7 +1214,7 @@ mcomp_format (struct mcomp *c1, struct mcomp *c2)
 
        fmt_scan (c1->c_fmt, scanl, BUFSIZ - 1, dat, NULL);
        buffer = charstring_buffer_copy (scanl);
-       if (strlen (buffer) > 0) {
+       if (*buffer) {
            if (c2->c_text)
                c2->c_text = add (",\n", c2->c_text);
            if (*(cp = buffer + strlen (buffer) - 1) == '\n')
@@ -1233,21 +1241,19 @@ 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)
-       adios (NULL, "unable to allocate comp memory");
-
+    NEW0(c1);
     c1->c_flags = flags & ~INIT;
     if ((c1->c_name = name ? getcpy (name) : NULL))
-       c1->c_flags |= mcomp_flags (c1->c_name);
+        c1->c_flags |= mcomp_flags (c1->c_name);
     c1->c_text = text ? getcpy (text) : NULL;
     if (flags & INIT) {
-       if (global.c_ovtxt)
-           c1->c_ovtxt = getcpy (global.c_ovtxt);
-       c1->c_offset = global.c_offset;
-       c1->c_ovoff = global. c_ovoff;
-       c1->c_width = c1->c_length = 0;
-       c1->c_cwidth = global.c_cwidth;
-       c1->c_flags |= global.c_flags & GFLAGS;
+        if (global.c_ovtxt)
+            c1->c_ovtxt = getcpy (global.c_ovtxt);
+        c1->c_offset = global.c_offset;
+        c1->c_ovoff = global. c_ovoff;
+        c1->c_width = c1->c_length = 0;
+        c1->c_cwidth = global.c_cwidth;
+        c1->c_flags |= global.c_flags & GFLAGS;
     }
     if (*head == NULL)
        *head = c1;
@@ -1288,6 +1294,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;
@@ -1303,7 +1315,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;
     }
@@ -1377,21 +1390,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);
 }
 
 
@@ -1487,7 +1512,9 @@ putch (char ch, long flags)
                putchar ('\007');
            fflush (stdout);
            buf[0] = 0;
-           read (fileno (stdout), buf, sizeof(buf));
+           if (read (fileno (stdout), buf, sizeof(buf)) < 0) {
+               advise ("stdout", "read");
+           }
            if (strchr(buf, '\n')) {
                if (global.c_flags & CLEARSCR)
                    nmh_clear_screen ();
@@ -1719,7 +1746,9 @@ filterbody (struct mcomp *c1, char *buf, int bufsz, int state, FILE *fp,
 
        while (state == BODY) {
            int bufsz2 = bufsz;
-           write(fdinput[1], buf, strlen(buf));
+           if (write(fdinput[1], buf, strlen(buf)) < 0) {
+               advise ("pipe output", "write");
+           }
            state = m_getfld (&gstate, name, buf, &bufsz2, fp);
        }