]> diplodocus.org Git - nmh/blobdiff - sbr/m_convert.c
%(divide): Avoid SIGFPE on integer divide of INT_MIN by -1.
[nmh] / sbr / m_convert.c
index 88ea9da5dea491a6edddc2f2748530162c0246da..be12e5070389e64c3fd2bef9f9d5e116158fb6e3 100644 (file)
@@ -1,14 +1,18 @@
-
-/*
- * m_convert.c -- parse a message range or sequence and set SELECTED
+/* m_convert.c -- parse a message range or sequence and set SELECTED
  *
  * This code is Copyright (c) 2002, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
  * complete copyright information.
  */
 
  *
  * This code is Copyright (c) 2002, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
  * complete copyright information.
  */
 
-#include <h/mh.h>
-#include <h/utils.h>
+#include "h/mh.h"
+#include "ssequal.h"
+#include "m_convert.h"
+#include "folder_realloc.h"
+#include "context_find.h"
+#include "seq_getnum.h"
+#include "error.h"
+#include "h/utils.h"
 
 /*
  * error codes for sequence
 
 /*
  * error codes for sequence
@@ -39,7 +43,7 @@ static int attr (struct msgs *, char *);
 int
 m_convert (struct msgs *mp, char *name)
 {
 int
 m_convert (struct msgs *mp, char *name)
 {
-    int first, last, found, count, is_range, err;
+    int first, last, found, count, err;
     char *bp, *cp;
 
     /* check if user defined sequence */
     char *bp, *cp;
 
     /* check if user defined sequence */
@@ -56,7 +60,6 @@ m_convert (struct msgs *mp, char *name)
      */
 
     found = 0;
      */
 
     found = 0;
-    is_range = 1;
 
     /*
      * Check for special "new" sequence, which
 
     /*
      * Check for special "new" sequence, which
@@ -77,7 +80,7 @@ m_convert (struct msgs *mp, char *name)
     cp = delimp;
     if (*cp != '\0' && *cp != '-' && *cp != ':' && *cp != '=') {
 badelim:
     cp = delimp;
     if (*cp != '\0' && *cp != '-' && *cp != ':' && *cp != '=') {
 badelim:
-       advise (NULL, "illegal argument delimiter: `%c'(0%o)", *delimp, *delimp);
+       inform("illegal argument delimiter: `%c'(0%o)", *delimp, *delimp);
        return 0;
     }
 
        return 0;
     }
 
@@ -87,28 +90,28 @@ badelim:
 badmsg:
            switch (err) {
            case BADMSG: 
 badmsg:
            switch (err) {
            case BADMSG: 
-               advise (NULL, "no %s message", cp);
+               inform("no %s message", cp);
                break;
 
            case BADNUM: 
                break;
 
            case BADNUM: 
-               advise (NULL, "message %s doesn't exist", cp);
+               inform("message %s doesn't exist", cp);
                break;
 
            case BADRNG: 
                break;
 
            case BADRNG: 
-               advise (NULL, "message %s out of range 1-%d", cp, mp->hghmsg);
+               inform("message %s out of range 1-%d", cp, mp->hghmsg);
                break;
 
            case BADLST: 
 badlist:
                break;
 
            case BADLST: 
 badlist:
-               advise (NULL, "bad message list %s", name);
+               inform("bad message list %s", name);
                break;
 
            case BADNEW:
                break;
 
            case BADNEW:
-               advise (NULL, "folder full, no %s message", name);
+               inform("folder full, no %s message", name);
                break;
 
            default: 
                break;
 
            default: 
-               advise (NULL, "no messages match specification");
+               inform("no messages match specification");
            }
            return 0;
        }
            }
            return 0;
        }
@@ -119,7 +122,7 @@ badlist:
            goto badelim;
        if (first > mp->hghmsg || last < mp->lowmsg) {
 rangerr:
            goto badelim;
        if (first > mp->hghmsg || last < mp->lowmsg) {
 rangerr:
-           advise (NULL, "no messages in range %s", name);
+           inform("no messages in range %s", name);
            return 0;
        }
 
            return 0;
        }
 
@@ -131,9 +134,7 @@ rangerr:
 
     } else if (*cp == ':' || *cp == '=') {
 
 
     } else if (*cp == ':' || *cp == '=') {
 
-       if (*cp == '=')
-           is_range = 0;
-
+        bool is_range = *cp == ':';
        cp++;
 
        if (*cp == '-') {
        cp++;
 
        if (*cp == '-') {
@@ -179,7 +180,7 @@ rangerr:
            }
        } else { /* looking for the nth message.  if not enough, fail. */
            if (last < mp->lowmsg || last > mp->hghmsg) {
            }
        } else { /* looking for the nth message.  if not enough, fail. */
            if (last < mp->lowmsg || last > mp->hghmsg) {
-               advise (NULL, "no such message");
+               inform("no such message");
                return 0;
            }
            first = last;
                return 0;
            }
            first = last;
@@ -204,8 +205,8 @@ single:
             * limits simply reallocate the folder so it's within range.
             */
            if (first < mp->lowoff || first > mp->hghoff)
             * limits simply reallocate the folder so it's within range.
             */
            if (first < mp->lowoff || first > mp->hghoff)
-               mp = folder_realloc(mp, first < mp->lowoff ? first : mp->lowoff,
-                                   first > mp->hghoff ? first : mp->hghoff);
+                mp = folder_realloc(mp, min(first, mp->lowoff),
+                                    max(first, mp->hghoff));
 
            set_select_empty (mp, first);
        } else {
 
            set_select_empty (mp, first);
        } else {
@@ -213,9 +214,9 @@ single:
                || first < mp->lowmsg
                || !(does_exist (mp, first))) {
                if (!strcmp (name, "cur") || !strcmp (name, "."))
                || first < mp->lowmsg
                || !(does_exist (mp, first))) {
                if (!strcmp (name, "cur") || !strcmp (name, "."))
-                   advise (NULL, "no %s message", name);
+                   inform("no %s message", name);
                else
                else
-                   advise (NULL, "message %d doesn't exist", first);
+                   inform("message %d doesn't exist", first);
                return 0;
            }
        }
                return 0;
            }
        }
@@ -295,16 +296,16 @@ m_conv (struct msgs *mp, char *str, int call)
     delimp = cp;
 
     if (!strcmp (buf, "first"))
     delimp = cp;
 
     if (!strcmp (buf, "first"))
-       return (mp->hghmsg || !(mp->msgflags & ALLOW_NEW)
-               ? mp->lowmsg : BADMSG);
+       return mp->hghmsg || !(mp->msgflags & ALLOW_NEW) ?
+            mp->lowmsg : BADMSG;
 
     if (!strcmp (buf, "last")) {
        convdir = -1;
 
     if (!strcmp (buf, "last")) {
        convdir = -1;
-       return (mp->hghmsg || !(mp->msgflags & ALLOW_NEW) ? mp->hghmsg : BADMSG);
+       return mp->hghmsg || !(mp->msgflags & ALLOW_NEW) ? mp->hghmsg : BADMSG;
     }
 
     if (!strcmp (buf, "cur") || !strcmp (buf, "."))
     }
 
     if (!strcmp (buf, "cur") || !strcmp (buf, "."))
-       return (mp->curmsg > 0 ? mp->curmsg : BADMSG);
+       return mp->curmsg > 0 ? mp->curmsg : BADMSG;
 
     if (!strcmp (buf, "prev")) {
        convdir = -1;
 
     if (!strcmp (buf, "prev")) {
        convdir = -1;
@@ -351,31 +352,32 @@ attr (struct msgs *mp, char *cp)
     char op;
     int i, j;
     int found,
     char op;
     int i, j;
     int found,
-       inverted = 0,
        count = 0,              /* range given?  else use entire sequence */
        count = 0,              /* range given?  else use entire sequence */
-       just_one = 0,           /* want entire sequence or range */
        first = 0,
        start = 0;
 
     /* hack for "cur-name", "cur-n", etc. */
     if (!strcmp (cp, "cur"))
        return 0;
        first = 0,
        start = 0;
 
     /* hack for "cur-name", "cur-n", etc. */
     if (!strcmp (cp, "cur"))
        return 0;
-    if (HasPrefix(cp, "cur")) {
+    if (has_prefix(cp, "cur")) {
        if (cp[3] == ':' || cp[3] == '=')
            return 0;
     }
 
     /* Check for sequence negation */
        if (cp[3] == ':' || cp[3] == '=')
            return 0;
     }
 
     /* Check for sequence negation */
+    bool inverted = false;
     if ((dp = context_find (nsequence)) && *dp != '\0' && ssequal (dp, cp)) {
     if ((dp = context_find (nsequence)) && *dp != '\0' && ssequal (dp, cp)) {
-       inverted = 1;
+       inverted = true;
        cp += strlen (dp);
     }
 
     convdir = 1;       /* convert direction */
 
        cp += strlen (dp);
     }
 
     convdir = 1;       /* convert direction */
 
-    for (dp = cp; *dp && isalnum((unsigned char) *dp); dp++)
+    for (dp = cp; isalnum((unsigned char)*dp); dp++)
        continue;
 
        continue;
 
+    bool just_one = *dp == '='; /* want entire sequence or range */
+
     if (*dp == ':') {
        bp = dp++;
        count = 1;
     if (*dp == ':') {
        bp = dp++;
        count = 1;
@@ -456,8 +458,6 @@ attr (struct msgs *mp, char *cp)
        if (count == 0 || *ep)
            return BADLST;
 
        if (count == 0 || *ep)
            return BADLST;
 
-       just_one = 1;
-
        op = *bp;
        *bp = '\0';     /* temporarily terminate sequence name */
     }
        op = *bp;
        *bp = '\0';     /* temporarily terminate sequence name */
     }
@@ -505,6 +505,6 @@ attr (struct msgs *mp, char *cp)
 
     if (first || just_one)
        return BADMSG;
 
     if (first || just_one)
        return BADMSG;
-    advise (NULL, "sequence %s %s", cp, inverted ? "full" : "empty");
+    inform("sequence %s %s", cp, inverted ? "full" : "empty");
     return -1;
 }
     return -1;
 }