]> diplodocus.org Git - nmh/blobdiff - uip/new.c
mh-sequence.man: document new '=+' and '=-' for selecting relative msgs
[nmh] / uip / new.c
index 41d2ebf8cb6b7fb6ae9e21dac9186cbfae187b88..5efdf74c712aa87ba203775a79159678b1bb02d7 100644 (file)
--- a/uip/new.c
+++ b/uip/new.c
 #include <h/crawl_folders.h>
 #include <h/utils.h>
 
-static struct swit switches[] = {
-#define MODESW 0
-    { "mode", 1 },
-#define FOLDERSSW 1
-    { "folders", 1 },
-#define VERSIONSW 2
-    { "version", 1 },
-#define HELPSW 3
-    { "help", 1 },
-    { NULL, 0 }
-};
+#define NEW_SWITCHES \
+    X("mode", 1, MODESW) \
+    X("folders", 1, FOLDERSSW) \
+    X("version", 1, VERSIONSW) \
+    X("help", 1, HELPSW) \
+
+#define X(sw, minchars, id) id,
+DEFINE_SWITCH_ENUM(NEW);
+#undef X
+
+#define X(sw, minchars, id) { sw, minchars, id },
+DEFINE_SWITCH_ARRAY(NEW, switches);
+#undef X
 
 static enum { NEW, FNEXT, FPREV, UNSEEN } run_mode = NEW;
 
@@ -97,6 +99,7 @@ get_msgnums(char *folder, char *sequences[])
     char name[NAMESZ], field[BUFSIZ];
     char *cp;
     char *msgnums = NULL, *this_msgnums, *old_msgnums;
+    m_getfld_state_t gstate = 0;
 
     /* no sequences file -> no messages */
     if (fp == NULL) {
@@ -104,16 +107,16 @@ get_msgnums(char *folder, char *sequences[])
     }
 
     /* copied from seq_read.c:seq_public */
-    for (state = FLD;;) {
-        switch (state = m_getfld (state, name, field, sizeof(field), fp)) {
+    for (;;) {
+       int fieldsz = sizeof field;
+       switch (state = m_getfld (&gstate, name, field, &fieldsz, fp)) {
             case FLD:
             case FLDPLUS:
-            case FLDEOF:
                 if (state == FLDPLUS) {
                     cp = getcpy (field);
                     while (state == FLDPLUS) {
-                        state = m_getfld (state, name, field,
-                                          sizeof(field), fp);
+                       fieldsz = sizeof field;
+                       state = m_getfld (&gstate, name, field, &fieldsz, fp);
                         cp = add (field, cp);
                     }
 
@@ -148,12 +151,9 @@ get_msgnums(char *folder, char *sequences[])
                     }
                 }
 
-                if (state == FLDEOF)
-                    break;
                 continue;
 
             case BODY:
-            case BODYEOF:
                 adios (NULL, "no blank lines are permitted in %s", seqfile);
                 /* fall */
 
@@ -165,6 +165,7 @@ get_msgnums(char *folder, char *sequences[])
         }
         break;  /* break from for loop */
     }
+    m_getfld_state_destroy (&gstate);
 
     fclose(fp);
 
@@ -218,6 +219,8 @@ crawl_callback(char *folder, void *baton)
  * `cur' points to the name of the current folder, `folders' points to the
  * name of a .folder (if NULL, crawl all folders), and `sequences' points to
  * the array of sequences for which to look.
+ *
+ * An empty list is returned as first=last=NULL.
  */
 static void
 check_folders(struct node **first, struct node **last,
@@ -229,7 +232,7 @@ check_folders(struct node **first, struct node **last,
     char *line;
     size_t len;
 
-    *first = *cur_node = NULL;
+    *first = *last = *cur_node = NULL;
     *maxlen = 0;
 
     b.first = first;
@@ -308,7 +311,10 @@ doit(char *cur, char *folders, char *sequences[])
                  folders, sequences);
 
     if (run_mode == FNEXT || run_mode == FPREV) {
-       if (first->n_next == NULL) {
+       if (first == NULL) {
+           /* No folders at all... */
+           return NULL;
+       } else if (first->n_next == NULL) {
            /* We have only one node; any desired messages in it? */
            if (first->n_field == NULL) {
                return NULL;
@@ -398,7 +404,7 @@ main(int argc, char **argv)
     char **ap, *cp, **argp, **arguments;
     char help[BUFSIZ];
     char *folders = NULL;
-    char *sequences[NUMATTRS + 1];
+    svector_t sequences = svector_create (0);
     int i = 0;
     char *unseen;
     struct node *folder;
@@ -430,10 +436,10 @@ main(int argc, char **argv)
                snprintf (help, sizeof(help), "%s [switches] [sequences]",
                          invo_name);
                print_help (help, switches, 1);
-               done (1);
+               done (0);
            case VERSIONSW:
                print_version(invo_name);
-               done (1);
+               done (0);
 
            case FOLDERSSW:
                if (!(folders = *argp++) || *folders == '-')
@@ -447,8 +453,9 @@ main(int argc, char **argv)
            }
        }
        /* have a sequence argument */
-       if (!seq_in_list(cp, sequences)) {
-           sequences[i++] = cp;
+       if (!seq_in_list(cp, svector_strs (sequences))) {
+           svector_push_back (sequences, cp);
+           ++i;
        }
     }
 
@@ -475,12 +482,12 @@ main(int argc, char **argv)
            adios(NULL, "must specify sequences or set %s", usequence);
        }
        for (ap = brkstring(unseen, " ", "\n"); *ap; ap++) {
-           sequences[i++] = *ap;
+           svector_push_back (sequences, *ap);
+           ++i;
        }
     }
-    sequences[i] = NULL;
 
-    folder = doit(context_find(pfolder), folders, sequences);
+    folder = doit(context_find(pfolder), folders, svector_strs (sequences));
     if (folder == NULL) {
         done(0);
         return 1;
@@ -504,6 +511,7 @@ main(int argc, char **argv)
 
     context_save();
 
+    svector_free (sequences);
     done (0);
     return 1;
 }