]> diplodocus.org Git - nmh/blobdiff - uip/sortm.c
pending-release-notes: add mhshow's "-prefer", and mh-format's %(kibi/kilo)
[nmh] / uip / sortm.c
index 05b6ed02eb59e0ea2ec92491cd112cecfea7c7cd..0b8e18534188e3a02932cfefc8cec1b3f2471307 100644 (file)
 #include <h/tws.h>
 #include <h/utils.h>
 
-static struct swit switches[] = {
-#define DATESW                 0
-     { "datefield field", 0 },
-#define        TEXTSW                 1
-     { "textfield field", 0 },
-#define        NSUBJSW                2
-     { "notextfield", 0 },
-#define SUBJSW                 3
-     { "subject", -3 },                   /* backward-compatibility */
-#define LIMSW                  4
-     { "limit days", 0 },
-#define        NLIMSW                 5
-     { "nolimit", 0 },
-#define VERBSW                 6
-     { "verbose", 0 },
-#define NVERBSW                7
-     { "noverbose", 0 },
-#define ALLMSGS                8
-     { "all", 0 },
-#define NALLMSGS               9
-     { "noall", 0 },
-#define CHECKSW               10
-     { "check", 0 },
-#define NCHECKSW              11
-     { "nocheck", 0 },
-#define VERSIONSW             12
-     { "version", 0 },
-#define HELPSW                13
-     { "help", 0 },
-     { NULL, 0 }
-};
+#define SORTM_SWITCHES \
+    X("datefield field", 0, DATESW) \
+    X("textfield field", 0, TEXTSW) \
+    X("notextfield", 0, NSUBJSW) \
+    X("subject", -3, SUBJSW) /* backward-compatibility */ \
+    X("limit days", 0, LIMSW) \
+    X("nolimit", 0, NLIMSW) \
+    X("verbose", 0, VERBSW) \
+    X("noverbose", 0, NVERBSW) \
+    X("all", 0, ALLMSGS) \
+    X("noall", 0, NALLMSGS) \
+    X("check", 0, CHECKSW) \
+    X("nocheck", 0, NCHECKSW) \
+    X("version", 0, VERSIONSW) \
+    X("help", 0, HELPSW) \
+
+#define X(sw, minchars, id) id,
+DEFINE_SWITCH_ENUM(SORTM);
+#undef X
+
+#define X(sw, minchars, id) { sw, minchars, id },
+DEFINE_SWITCH_ARRAY(SORTM, switches);
+#undef X
 
 struct smsg {
     int s_msg;
@@ -78,8 +70,7 @@ int
 main (int argc, char **argv)
 {
     int        i, msgnum;
-    unsigned char *cp;
-    char *maildir, *datesw = NULL;
+    char *cp, *maildir, *datesw = NULL;
     char *folder = NULL, buf[BUFSIZ], **argp;
     char **arguments;
     struct msgs_array msgs = { 0, 0, NULL };
@@ -87,13 +78,7 @@ main (int argc, char **argv)
     struct smsg **dlist;
     int checksw = 0;
 
-#ifdef LOCALE
-    setlocale(LC_ALL, "");
-#endif
-    invo_name = r1bindex (argv[0], '/');
-
-    /* read user profile/context */
-    context_read();
+    if (nmh_init(argv[0], 1)) { return 1; }
 
     arguments = getarguments (invo_name, argc, argv, 1);
     argp = arguments;
@@ -149,7 +134,7 @@ main (int argc, char **argv)
                    submajor++;         /* sort subject-major */
                    continue;
                }
-               if (!isdigit(*cp) || !(datelimit = atoi(cp)))
+               if (!isdigit((unsigned char) *cp) || !(datelimit = atoi(cp)))
                    adios (NULL, "impossible limit %s", cp);
                datelimit *= 60*60*24;
                continue;
@@ -208,7 +193,7 @@ main (int argc, char **argv)
        adios (maildir, "unable to change directory to");
 
     /* read folder and create message structure */
-    if (!(mp = folder_read (folder)))
+    if (!(mp = folder_read (folder, 1)))
        adios (NULL, "unable to read folder %s", folder);
 
     /* check for empty folder */
@@ -268,7 +253,7 @@ main (int argc, char **argv)
         * the collection of messages with the same subj
         * given a message number.
         */
-       il = (struct smsg ***) calloc (mp->hghsel+1, sizeof(*il));
+       il = (struct smsg ***) mh_xcalloc (mp->hghsel+1, sizeof(*il));
        if (! il)
            adios (NULL, "couldn't allocate msg list");
        for (i = 0; i < nmsgs; i++)
@@ -302,6 +287,7 @@ main (int argc, char **argv)
            }
        }
        *fp = 0;
+       free (il);
        free (slist);
        free (dlist);
        dlist = flist;
@@ -332,7 +318,7 @@ read_hdrs (struct msgs *mp, char *datesw)
     twscopy (&tb, dlocaltimenow ());
 
     smsgs = (struct smsg *)
-       calloc ((size_t) (mp->hghsel - mp->lowsel + 2),
+       mh_xcalloc ((size_t) (mp->hghsel - mp->lowsel + 2),
            sizeof(*smsgs));
     if (smsgs == NULL)
        adios (NULL, "unable to allocate sort storage");
@@ -377,7 +363,7 @@ get_fields (char *datesw, int msg, struct smsg *smsg)
        case FLD:
        case FLDPLUS:
            compnum++;
-           if (!mh_strcasecmp (nam, datesw)) {
+           if (!strcasecmp (nam, datesw)) {
                datecomp = add (buf, datecomp);
                while (state == FLDPLUS) {
                    bufsz = sizeof buf;
@@ -386,7 +372,7 @@ get_fields (char *datesw, int msg, struct smsg *smsg)
                }
                if (!subjsort || subjcomp)
                    break;
-           } else if (subjsort && !mh_strcasecmp (nam, subjsort)) {
+           } else if (subjsort && !strcasecmp (nam, subjsort)) {
                subjcomp = add (buf, subjcomp);
                while (state == FLDPLUS) {
                    bufsz = sizeof buf;
@@ -436,7 +422,10 @@ get_fields (char *datesw, int msg, struct smsg *smsg)
     if (!datecomp || (tw = dparsetime (datecomp)) == NULL) {
        struct stat st;
 
-       admonish (NULL, "can't parse %s field in message %d", datesw, msg);
+       advise (NULL,
+               "can't parse %s field in message %d, "
+               "will use file modification time",
+               datesw, msg);
        fstat (fileno (in), &st);
        smsg->s_clock = st.st_mtime;
        check_failed = 1;
@@ -451,14 +440,13 @@ get_fields (char *datesw, int msg, struct smsg *smsg)
             * leading "re:", everything but letters & smash
             * letters to lower case.
             */
-           register char  *cp, *cp2;
-           register unsigned char c;
+           register char  *cp, *cp2, c;
 
            cp = subjcomp;
            cp2 = subjcomp;
            if (strcmp (subjsort, "subject") == 0) {
                while ((c = *cp)) {
-                   if (! isspace(c)) {
+                   if (! isspace((unsigned char) c)) {
                        if(uprf(cp, "re:"))
                            cp += 2;
                        else
@@ -469,8 +457,9 @@ get_fields (char *datesw, int msg, struct smsg *smsg)
            }
 
            while ((c = *cp++)) {
-               if (isalnum(c))
-                   *cp2++ = isupper(c) ? tolower(c) : c;
+               if (isascii((unsigned char) c) && isalnum((unsigned char) c))
+                   *cp2++ = isupper((unsigned char) c) ?
+                                       tolower((unsigned char) c) : c;
            }
 
            *cp2 = '\0';
@@ -571,7 +560,7 @@ static void
 rename_msgs (struct msgs *mp, struct smsg **mlist)
 {
     int i, j, old, new;
-    seqset_t tmpset;
+    bvector_t tmpset = bvector_create (0);
     char f1[BUFSIZ], tmpfil[BUFSIZ];
     char newbuf[PATH_MAX + 1];
     struct smsg *sp;
@@ -611,7 +600,7 @@ rename_msgs (struct msgs *mp, struct smsg **mlist)
        if (rename (f1, tmpfil) == NOTOK)
            adios (tmpfil, "unable to rename %s to ", f1);
 
-       get_msg_flags (mp, &tmpset, old);
+       get_msg_flags (mp, tmpset, old);
 
        rename_chain (mp, mlist, j, i);
 
@@ -626,7 +615,9 @@ rename_msgs (struct msgs *mp, struct smsg **mlist)
        if (rename (tmpfil, m_name(new)) == NOTOK)
            adios (m_name(new), "unable to rename %s to", tmpfil);
 
-       set_msg_flags (mp, &tmpset, new);
+       set_msg_flags (mp, tmpset, new);
        mp->msgflags |= SEQMOD;
     }
+
+    bvector_free (tmpset);
 }