Factor trim format function out
[mmh] / sbr / m_convert.c
index 1350c37..d187a34 100644 (file)
@@ -10,6 +10,7 @@
 /* FIXME: This code needs rework! Rewrite as a parser? */
 
 #include <h/mh.h>
+#include <ctype.h>
 
 /*
 ** error codes for sequence
@@ -35,7 +36,6 @@ static char *delimp;  /* delimiter pointer */
 static int m_conv(struct msgs *, char *, int);
 static int attr(struct msgs *, char *);
 
-
 int
 m_convert(struct msgs *mp, char *name)
 {
@@ -55,26 +55,25 @@ m_convert(struct msgs *mp, char *name)
        }
        /*
        ** else err == 0, so continue
-       ** (we know here: it is no user-defined seq)
+       ** here we know: it is no user-defined seq
        */
 
-       found = 0;
-
        /*
-       ** Check for special beyond sequence, which
-       ** is valid only if ALLOW_BEYOND is set.
-       ** (It can appear only on its own.)
+       ** Handle the special beyond sequence, which is valid only if
+       ** ALLOW_BEYOND is set, and can appear only on its own.
+       ** Also, it is available in any folder.
        */
-       if ((mp->msgflags & ALLOW_BEYOND) && !strcmp(cp, seq_beyond)) {
-               if ((err = first = getbeyond(mp)) <= 0)
-                       goto badmsg;
-               else
-                       goto single;
+       if ((mp->msgflags & ALLOW_BEYOND) && strcmp(cp, seq_beyond)==0) {
+               set_selected(mp, getbeyond(mp));
+               return 1;
        }
 
-       /* replace `a' with `f-l' */
-       if (!strcmp(cp, seq_all))
+       /*
+       ** Handle the special all sequence: replace `a' with `f-l'
+       */
+       if (strcmp(cp, seq_all)==0) {
                cp = concat(seq_first, "-", seq_last, NULL);
+       }
 
        if ((err = first = m_conv(mp, cp, FIRST)) <= 0)
                goto badmsg;
@@ -160,7 +159,7 @@ rangerr:
                        first = mp->hghmsg;
 
                for (last = first; last >= mp->lowmsg && last <= mp->hghmsg;
-                       last += convdir)
+                               last += convdir)
                        if (does_exist(mp, last) && (--range <= 0))
                                break;
                if (last < mp->lowmsg)
@@ -176,7 +175,6 @@ rangerr:
 
        case '\0':
                /* single name */
-single:
                /*
                ** AFAICS, the only cases to reach here are:
                **     f, l, p, n, c, b
@@ -185,17 +183,14 @@ single:
                /*
                ** Single Message
                **
-               ** If ALLOW_BEYOND is set, then allow selecting of an
-               ** empty slot.  If ALLOW_BEYOND is not set, then we
-               ** check if message is in-range and exists.
+               ** Check if message is in-range and exists.
                */
-               if (mp->msgflags & ALLOW_BEYOND) {
-                       set_select_empty(mp, first);
-               } else if (first > mp->hghmsg || first < mp->lowmsg
-                               || !does_exist(mp, first)) {
-                       if (!strcmp(name, seq_cur))
+               if (first > mp->hghmsg || first < mp->lowmsg ||
+                               !does_exist(mp, first)) {
+                       if (strcmp(name, seq_cur)==0)
                                advise(NULL, "no current message");
                        else
+                               /* this case seems to never be reached */
                                advise(NULL, "message %d doesn't exist",
                                                first);
                        return 0;
@@ -211,27 +206,13 @@ badelim:
                break;
        }
 
-       /*
-       ** Cycle through the range and select the messages
-       ** that exist.  If ALLOW_BEYOND is set, then we also check
-       ** if we are selecting an empty slot.
-       */
-       for (; first <= last; first++) {
-               if (does_exist(mp, first) || (
-                               (mp->msgflags & ALLOW_BEYOND) &&
-                               is_select_empty(mp, first))) {
-                       if (!is_selected(mp, first)) {
-                               set_selected(mp, first);
-                               mp->numsel++;
-                               if (mp->lowsel == 0 || first < mp->lowsel)
-                                       mp->lowsel = first;
-                               if (first > mp->hghsel)
-                                       mp->hghsel = first;
-                       }
+       /* Cycle through the range and select the messages that exist. */
+       for (found=0; first <= last; first++) {
+               if (does_exist(mp, first)) {
+                       set_selected(mp, first);
                        found++;
                }
        }
-
        if (!found)
                goto rangerr;
 
@@ -251,12 +232,14 @@ badelim:
 static int
 m_conv(struct msgs *mp, char *str, int call)
 {
-       register int i;
-       register unsigned char *cp, *bp;
-       unsigned char buf[16];
+       int i;
+       unsigned char *cp, *bp;
+       unsigned char buf[16];  /* for reserved sequence name */
 
        convdir = 1;
        cp = bp = str;
+
+       /* Handle an integer */
        if (isdigit(*cp)) {
                while (isdigit(*bp))
                        bp++;
@@ -273,50 +256,47 @@ m_conv(struct msgs *mp, char *str, int call)
                        return BADNUM;
        }
 
-#ifdef LOCALE
-       /* doesn't enforce lower case */
-       for (bp = buf; isalpha(*cp) && (bp - buf < sizeof(buf) - 1); )
-#else
-       for (bp = buf; islower(*cp) && (bp - buf < sizeof(buf) - 1); )
-#endif /* LOCALE */
-       {
+       for (bp = buf; isalpha(*cp) && (bp - buf < (int)sizeof(buf) - 1); ) {
                *bp++ = *cp++;
        }
        *bp++ = '\0';
        delimp = cp;
 
-       if (!strcmp(buf, seq_first))
-               return (mp->hghmsg || !(mp->msgflags & ALLOW_BEYOND)
-                               ? mp->lowmsg : BADMSG);
+       /* Which one of the reserved names is it? */
+       if (strcmp(buf, seq_first)==0) {
+               return (mp->hghmsg || !(mp->msgflags & ALLOW_BEYOND) ?
+                               mp->lowmsg : BADMSG);
 
-       if (!strcmp(buf, seq_last)) {
+       } else if (strcmp(buf, seq_last)==0) {
                convdir = -1;
-               return (mp->hghmsg || !(mp->msgflags & ALLOW_BEYOND) ? mp->hghmsg : BADMSG);
-       }
+               return (mp->hghmsg || !(mp->msgflags & ALLOW_BEYOND) ?
+                               mp->hghmsg : BADMSG);
 
-       if (!strcmp(buf, seq_cur))
+       } else if (strcmp(buf, seq_cur)==0) {
                return (mp->curmsg > 0 ? mp->curmsg : BADMSG);
 
-       if (!strcmp(buf, seq_prev)) {
+       } else if (strcmp(buf, seq_prev)==0) {
                convdir = -1;
-               for (i = (mp->curmsg <= mp->hghmsg) ? mp->curmsg - 1 : mp->hghmsg;
-                       i >= mp->lowmsg; i--) {
+               for (i = (mp->curmsg <= mp->hghmsg) ?
+                               mp->curmsg - 1 : mp->hghmsg;
+                               i >= mp->lowmsg; i--) {
                        if (does_exist(mp, i))
                                return i;
                }
                return BADMSG;
-       }
 
-       if (!strcmp(buf, seq_next)) {
-               for (i = (mp->curmsg >= mp->lowmsg) ? mp->curmsg + 1 : mp->lowmsg;
-                       i <= mp->hghmsg; i++) {
+       } else if (strcmp(buf, seq_next)==0) {
+               for (i = (mp->curmsg >= mp->lowmsg) ?
+                               mp->curmsg + 1 : mp->lowmsg;
+                               i <= mp->hghmsg; i++) {
                        if (does_exist(mp, i))
                                return i;
                }
                return BADMSG;
-       }
 
-       return BADLST;
+       } else {
+               return BADLST;
+       }
 }
 
 /*
@@ -337,16 +317,16 @@ m_conv(struct msgs *mp, char *str, int call)
 static int
 attr(struct msgs *mp, char *cp)
 {
-       register unsigned char *dp;
+       unsigned char *dp;
        char *bp = NULL;
-       register int i, j;
+       int i, j;
        int found;
        int inverted = 0;
        int range = 0;  /* no range */
        int first = 0;
 
        /* hack for "c-..." */
-       if (!strcmp(cp, seq_cur))
+       if (strcmp(cp, seq_cur)==0)
                return 0;
        /* "c:..." -- this code need to be rewritten... */
        if (strncmp(seq_cur, cp, strlen(seq_cur))==0 &&
@@ -358,7 +338,7 @@ attr(struct msgs *mp, char *cp)
        if (!(dp = context_find(nsequence))) {
                dp = seq_neg;  /* use default */
        }
-       if (dp && *dp && isprefix(dp, cp)) {
+       if (*dp && strncmp(cp, dp, strlen(dp))==0) {
                inverted = 1;
                cp += strlen(dp);
        }
@@ -379,17 +359,17 @@ attr(struct msgs *mp, char *cp)
                ** seq:l
                */
                if (isalpha(*dp)) {
-                       if (!strcmp(dp, seq_prev)) {
+                       if (strcmp(dp, seq_prev)==0) {
                                convdir = -1;
                                first = (mp->curmsg > 0) && (mp->curmsg <= mp->hghmsg)
                                        ? mp->curmsg - 1 : mp->hghmsg;
-                       } else if (!strcmp(dp, seq_next)) {
+                       } else if (strcmp(dp, seq_next)==0) {
                                convdir = 1;
                                first = (mp->curmsg >= mp->lowmsg)
                                        ? mp->curmsg + 1 : mp->lowmsg;
-                       } else if (!strcmp(dp, seq_first)) {
+                       } else if (strcmp(dp, seq_first)==0) {
                                convdir = 1;
-                       } else if (!strcmp(dp, seq_last)) {
+                       } else if (strcmp(dp, seq_last)==0) {
                                convdir = -1;
                        } else
                                return BADLST;
@@ -426,17 +406,11 @@ attr(struct msgs *mp, char *cp)
        found = 0;  /* count the number we select for this argument */
 
        for (j = first ? first : (convdir > 0) ? mp->lowmsg : mp->hghmsg;
-               j >= mp->lowmsg && j <= mp->hghmsg; j += convdir) {
+                       j >= mp->lowmsg && j <= mp->hghmsg; j += convdir) {
                if (does_exist(mp, j)
-                       && inverted ? !in_sequence(mp, i, j) : in_sequence(mp, i, j)) {
-                       if (!is_selected(mp, j)) {
-                               set_selected(mp, j);
-                               mp->numsel++;
-                               if (mp->lowsel == 0 || j < mp->lowsel)
-                                       mp->lowsel = j;
-                               if (j > mp->hghsel)
-                                       mp->hghsel = j;
-                       }
+                               && inverted ? !in_sequence(mp, i, j) :
+                               in_sequence(mp, i, j)) {
+                       set_selected(mp, j);
                        found++;
 
                        /*
@@ -448,7 +422,7 @@ attr(struct msgs *mp, char *cp)
                }
        }
 
-       if (found > 0)
+       if (found)
                return found;
 
        if (first)