Various minor cleanups in sbr/m_convert.c.
[mmh] / sbr / m_convert.c
index 3065941..1350c37 100644 (file)
@@ -6,6 +6,9 @@
 ** complete copyright information.
 */
 
+
+/* FIXME: This code needs rework! Rewrite as a parser? */
+
 #include <h/mh.h>
 
 /*
 #define FIRST 1
 #define LAST 2
 
-#define getnew(mp) (mp->hghmsg + 1)
+#define getbeyond(mp) ((mp)->hghmsg + 1)
 
-static int convdir; /* convert direction */
-static char *delimp;
+static int convdir;  /* convert direction */
+static char *delimp;  /* delimiter pointer */
 
 /*
 ** static prototypes
@@ -42,45 +45,44 @@ m_convert(struct msgs *mp, char *name)
 
        /* check if user defined sequence */
        err = attr(mp, cp = name);
-
        if (err == -1)
                return 0;
        else if (err < 0)
                goto badmsg;
-       else if (err > 0)
+       else if (err > 0) {
+               /* it had been a user-defined sequence: we're finished */
                return 1;
+       }
        /*
        ** else err == 0, so continue
+       ** (we know here: it is no user-defined seq)
        */
 
        found = 0;
 
        /*
-       ** Check for special "new" sequence, which
-       ** is valid only if ALLOW_NEW is set.
+       ** Check for special beyond sequence, which
+       ** is valid only if ALLOW_BEYOND is set.
+       ** (It can appear only on its own.)
        */
-       if ((mp->msgflags & ALLOW_NEW) && !strcmp(cp, "new")) {
-               if ((err = first = getnew(mp)) <= 0)
+       if ((mp->msgflags & ALLOW_BEYOND) && !strcmp(cp, seq_beyond)) {
+               if ((err = first = getbeyond(mp)) <= 0)
                        goto badmsg;
                else
                        goto single;
        }
 
-       if (!strcmp(cp, "all"))
-               cp = "first-last";
+       /* replace `a' with `f-l' */
+       if (!strcmp(cp, seq_all))
+               cp = concat(seq_first, "-", seq_last, NULL);
 
        if ((err = first = m_conv(mp, cp, FIRST)) <= 0)
                goto badmsg;
 
        cp = delimp;
-       if (*cp != '\0' && *cp != '-' && *cp != ':') {
-badelim:
-               advise(NULL, "illegal argument delimiter: `%c'(0%o)",
-                               *delimp, *delimp);
-               return 0;
-       }
-
-       if (*cp == '-') {
+       switch (*cp) {
+       case '-':
+               /* global range */
                cp++;
                if ((err = last = m_conv(mp, cp, LAST)) <= 0) {
 badmsg:
@@ -129,8 +131,10 @@ rangerr:
                        last = mp->hghmsg;
                if (first < mp->lowmsg)
                        first = mp->lowmsg;
+               break;
 
-       } else if (*cp == ':') {
+       case ':':
+               /* anchored range */
                cp++;
                if (*cp == '-') {
                        convdir = -1;
@@ -146,7 +150,7 @@ rangerr:
                if (*bp)
                        goto badelim;
                if ((convdir > 0 && first > mp->hghmsg)
-                       || (convdir < 0 && first < mp->lowmsg))
+                               || (convdir < 0 && first < mp->lowmsg))
                        goto rangerr;
 
                /* tighten the range to search */
@@ -157,9 +161,8 @@ rangerr:
 
                for (last = first; last >= mp->lowmsg && last <= mp->hghmsg;
                        last += convdir)
-                       if (does_exist(mp, last))
-                               if (--range <= 0)
-                                       break;
+                       if (does_exist(mp, last) && (--range <= 0))
+                               break;
                if (last < mp->lowmsg)
                        last = mp->lowmsg;
                if (last > mp->hghmsg)
@@ -169,40 +172,54 @@ rangerr:
                        last = first;
                        first = range;
                }
-       } else {
+               break;
 
+       case '\0':
+               /* single name */
 single:
                /*
+               ** AFAICS, the only cases to reach here are:
+               **     f, l, p, n, c, b
+               ** But I'm not sure.  --meillo
+               */
+               /*
                ** Single Message
                **
-               ** If ALLOW_NEW is set, then allow selecting of an
-               ** empty slot.  If ALLOW_NEW is not set, then we
+               ** 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.
                */
-               if (mp->msgflags & ALLOW_NEW) {
+               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, "cur"))
-                                       advise(NULL, "no %s message", name);
-                               else
-                                       advise(NULL, "message %d doesn't exist", first);
-                               return 0;
-                       }
+               } else if (first > mp->hghmsg || first < mp->lowmsg
+                               || !does_exist(mp, first)) {
+                       if (!strcmp(name, seq_cur))
+                               advise(NULL, "no current message");
+                       else
+                               advise(NULL, "message %d doesn't exist",
+                                               first);
+                       return 0;
                }
                last = first;  /* range of 1 */
+               break;
+
+       default:
+badelim:
+               advise(NULL, "illegal argument delimiter: `%c'(0%o)",
+                               *delimp, *delimp);
+               return 0;
+               break;
        }
 
        /*
        ** Cycle through the range and select the messages
-       ** that exist.  If ALLOW_NEW is set, then we also check
+       ** 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_NEW) && is_select_empty(mp, 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++;
@@ -222,15 +239,14 @@ single:
 }
 
 /*
-** Convert the various message names to
-** their numeric values.
+** Convert the various message names to their numeric values.
 **
-** n  (integer)
-** prev
-** next
-** first
-** last
-** cur
+** 42  (any integer)
+** p
+** n
+** f
+** l
+** c
 */
 static int
 m_conv(struct msgs *mp, char *str, int call)
@@ -251,7 +267,7 @@ m_conv(struct msgs *mp, char *str, int call)
                        return i;
                else if (*delimp || call == LAST)
                        return mp->hghmsg + 1;
-               else if (mp->msgflags & ALLOW_NEW)
+               else if (mp->msgflags & ALLOW_BEYOND)
                        return BADRNG;
                else
                        return BADNUM;
@@ -269,19 +285,19 @@ m_conv(struct msgs *mp, char *str, int call)
        *bp++ = '\0';
        delimp = cp;
 
-       if (!strcmp(buf, "first"))
-               return (mp->hghmsg || !(mp->msgflags & ALLOW_NEW)
+       if (!strcmp(buf, seq_first))
+               return (mp->hghmsg || !(mp->msgflags & ALLOW_BEYOND)
                                ? mp->lowmsg : BADMSG);
 
-       if (!strcmp(buf, "last")) {
+       if (!strcmp(buf, seq_last)) {
                convdir = -1;
-               return (mp->hghmsg || !(mp->msgflags & ALLOW_NEW) ? mp->hghmsg : BADMSG);
+               return (mp->hghmsg || !(mp->msgflags & ALLOW_BEYOND) ? mp->hghmsg : BADMSG);
        }
 
-       if (!strcmp(buf, "cur"))
+       if (!strcmp(buf, seq_cur))
                return (mp->curmsg > 0 ? mp->curmsg : BADMSG);
 
-       if (!strcmp(buf, "prev")) {
+       if (!strcmp(buf, seq_prev)) {
                convdir = -1;
                for (i = (mp->curmsg <= mp->hghmsg) ? mp->curmsg - 1 : mp->hghmsg;
                        i >= mp->lowmsg; i--) {
@@ -291,7 +307,7 @@ m_conv(struct msgs *mp, char *str, int call)
                return BADMSG;
        }
 
-       if (!strcmp(buf, "next")) {
+       if (!strcmp(buf, seq_next)) {
                for (i = (mp->curmsg >= mp->lowmsg) ? mp->curmsg + 1 : mp->lowmsg;
                        i <= mp->hghmsg; i++) {
                        if (does_exist(mp, i))
@@ -308,15 +324,16 @@ m_conv(struct msgs *mp, char *str, int call)
 ** They can take the following forms:
 **
 ** seq
-** seq:prev
-** seq:next
-** seq:first
-** seq:last
-** seq:+n
-** seq:-n
+** seq:p
 ** seq:n
+** seq:f
+** seq:l
+** seq:+42
+** seq:-42
+** seq:42
+**
+** (`42' being an arbitrary integer)
 */
-
 static int
 attr(struct msgs *mp, char *cp)
 {
@@ -328,11 +345,14 @@ attr(struct msgs *mp, char *cp)
        int range = 0;  /* no range */
        int first = 0;
 
-       /* hack for "cur-name", "cur-n", etc. */
-       if (!strcmp(cp, "cur"))
+       /* hack for "c-..." */
+       if (!strcmp(cp, seq_cur))
                return 0;
-       if (isprefix("cur:", cp))  /* this code need to be rewritten... */
+       /* "c:..." -- this code need to be rewritten... */
+       if (strncmp(seq_cur, cp, strlen(seq_cur))==0 &&
+                       cp[strlen(seq_cur)] == ':') {
                return 0;
+       }
 
        /* Check for sequence negation */
        if (!(dp = context_find(nsequence))) {
@@ -353,31 +373,31 @@ attr(struct msgs *mp, char *cp)
                range = 1;
 
                /*
-               ** seq:prev  (or)
-               ** seq:next  (or)
-               ** seq:first (or)
-               ** seq:last
+               ** seq:p  (or)
+               ** seq:n  (or)
+               ** seq:f  (or)
+               ** seq:l
                */
                if (isalpha(*dp)) {
-                       if (!strcmp(dp, "prev")) {
+                       if (!strcmp(dp, seq_prev)) {
                                convdir = -1;
                                first = (mp->curmsg > 0) && (mp->curmsg <= mp->hghmsg)
                                        ? mp->curmsg - 1 : mp->hghmsg;
-                       } else if (!strcmp(dp, "next")) {
+                       } else if (!strcmp(dp, seq_next)) {
                                convdir = 1;
                                first = (mp->curmsg >= mp->lowmsg)
                                        ? mp->curmsg + 1 : mp->lowmsg;
-                       } else if (!strcmp(dp, "first")) {
+                       } else if (!strcmp(dp, seq_first)) {
                                convdir = 1;
-                       } else if (!strcmp(dp, "last")) {
+                       } else if (!strcmp(dp, seq_last)) {
                                convdir = -1;
                        } else
                                return BADLST;
                } else {
                        /*
-                       ** seq:n  (or)
-                       ** seq:+n (or)
-                       ** seq:-n
+                       ** seq:42  (or)
+                       ** seq:+42 (or)
+                       ** seq:-42
                        */
                        if (*dp == '+')
                                dp++;