* uip/mhlistsbr.c, uip/mhlsbr.c, uip/picksbr.c: cast
[mmh] / uip / scan.c
index 1612717..31aa245 100644 (file)
 #include <h/scansbr.h>
 #include <h/tws.h>
 #include <h/mts.h>
+#include <h/utils.h>
 #include <errno.h>
 
-/*
- * We allocate space for message names (msgs array)
- * this number of elements at a time.
- */
-#define MAXMSGS  256
-
-
 static struct swit switches[] = {
 #define        CLRSW   0
     { "clear", 0 },
@@ -51,7 +45,6 @@ static struct swit switches[] = {
     { NULL, 0 }
 };
 
-extern int errno;
 
 /*
  * global for sbr/formatsbr.c - yech!
@@ -71,11 +64,12 @@ main (int argc, char **argv)
 {
     int clearflag = 0, hdrflag = 0, ontty;
     int width = 0, revflag = 0;
-    int i, state, msgnum, nummsgs, maxmsgs;
+    int i, state, msgnum;
     int seqnum[NUMATTRS], unseen, num_unseen_seq = 0;
     char *cp, *maildir, *file = NULL, *folder = NULL;
     char *form = NULL, *format = NULL, buf[BUFSIZ];
-    char **argp, *nfs, **arguments, **msgs;
+    char **argp, *nfs, **arguments;
+    struct msgs_array msgs = { 0, 0, NULL };
     struct msgs *mp;
     FILE *in;
 
@@ -92,15 +86,6 @@ main (int argc, char **argv)
     argp = arguments;
 
     /*
-     * Allocate the initial space to record message
-     * names, ranges, and sequences.
-     */
-    nummsgs = 0;
-    maxmsgs = MAXMSGS;
-    if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
-       adios (NULL, "unable to allocate storage");
-
-    /*
      * Parse arguments
      */
     while ((cp = *argp++)) {
@@ -170,20 +155,9 @@ main (int argc, char **argv)
            if (folder)
                adios (NULL, "only one folder at a time!");
            else
-               folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
-       } else {
-           /*
-            * Check if we need to allocate more space
-            * for message names/ranges/sequences.
-            */
-           if (nummsgs >= maxmsgs) {
-               maxmsgs += MAXMSGS;
-               if (!(msgs = (char **) realloc (msgs,
-                       (size_t) (maxmsgs * sizeof(*msgs)))))
-                   adios (NULL, "unable to reallocate msgs storage");
-           }
-           msgs[nummsgs++] = cp;
-       }
+               folder = pluspath (cp);
+       } else
+               app_msgarg(&msgs, cp);
     }
 
     if (!context_find ("path"))
@@ -198,7 +172,7 @@ main (int argc, char **argv)
      * We are scanning a maildrop file
      */
     if (file) {
-       if (nummsgs)
+       if (msgs.size)
            adios (NULL, "\"msgs\" not allowed with -file");
        if (folder)
            adios (NULL, "\"+folder\" not allowed with -file");
@@ -233,8 +207,8 @@ main (int argc, char **argv)
      * We are scanning a folder
      */
 
-    if (!nummsgs)
-       msgs[nummsgs++] = "all";
+    if (!msgs.size)
+       app_msgarg(&msgs, "all");
     if (!folder)
        folder = getfolder (1);
     maildir = m_maildir (folder);
@@ -251,8 +225,8 @@ main (int argc, char **argv)
        adios (NULL, "no messages in %s", folder);
 
     /* parse all the message ranges/sequences and set SELECTED */
-    for (msgnum = 0; msgnum < nummsgs; msgnum++)
-       if (!m_convert (mp, msgs[msgnum]))
+    for (msgnum = 0; msgnum < msgs.size; msgnum++)
+       if (!m_convert (mp, msgs.msgs[msgnum]))
            done(1);
     seq_setprev (mp);                  /* set the Previous-Sequence */
 
@@ -320,7 +294,7 @@ main (int argc, char **argv)
 
            switch (state = scan (in, msgnum, 0, nfs, width,
                        msgnum == mp->curmsg, unseen,
-                       hdrflag ? folder : NULL, 0L, 1)) {
+                       folder, 0L, 1)) {
                case SCNMSG: 
                case SCNENC: 
                case SCNERR: 
@@ -352,5 +326,6 @@ main (int argc, char **argv)
     if (clearflag)
        clear_screen ();
 
-    return done (0);
+    done (0);
+    return 1;
 }