]> diplodocus.org Git - nmh/blobdiff - uip/mark.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / mark.c
index f99832a2c76847fa64ad7b38aea5909b5d191dd2..0b17b862a0d0f11770d19b35201c4242d28590db 100644 (file)
@@ -1,6 +1,4 @@
-
-/*
- * mark.c -- add message(s) to sequences in given folder
+/* mark.c -- add message(s) to sequences in given folder
  *        -- delete messages (s) from sequences in given folder
  *        -- list sequences in given folder
  *
@@ -11,6 +9,7 @@
 
 #include <h/mh.h>
 #include <h/utils.h>
+#include "sbr/m_maildir.h"
 
 #define MARK_SWITCHES \
     X("add", 0, ADDSW) \
@@ -48,17 +47,11 @@ main (int argc, char **argv)
     unsigned int seqp = 0;
     char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
     char **argp, **arguments;
-    char *seqs[NUMATTRS + 1];
+    svector_t seqs = svector_create (0);
     struct msgs_array msgs = { 0, 0, NULL };
     struct msgs *mp;
 
-#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;
@@ -101,10 +94,8 @@ main (int argc, char **argv)
                if (!(cp = *argp++) || *cp == '-')
                    adios (NULL, "missing argument to %s", argp[-2]);
 
-               /* check if too many sequences specified */
-               if (seqp >= NUMATTRS)
-                   adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
-               seqs[seqp++] = cp;
+               svector_push_back (seqs, cp);
+               seqp++;
                continue;
 
            case PUBLSW: 
@@ -129,10 +120,9 @@ main (int argc, char **argv)
        if (*cp == '+' || *cp == '@') {
            if (folder)
                adios (NULL, "only one folder at a time!");
-           else
-               folder = pluspath (cp);
+            folder = pluspath (cp);
        } else
-               app_msgarg(&msgs, cp);
+            app_msgarg(&msgs, cp);
     }
 
     /*
@@ -159,7 +149,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);
 
     /* print some general debugging info */
@@ -185,19 +175,18 @@ main (int argc, char **argv)
     if (seqp == 0 && (addsw || deletesw))
        adios (NULL, "-%s requires at least one -sequence argument",
               addsw ? "add" : "delete");
-    seqs[seqp] = NULL;
 
     /* Adding messages to sequences */
     if (addsw) {
-       for (seqp = 0; seqs[seqp]; seqp++)
-           if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
+       for (seqp = 0; seqp < svector_size (seqs); seqp++)
+           if (!seq_addsel (mp, svector_at (seqs, seqp), publicsw, zerosw))
                done (1);
     }
 
     /* Deleting messages from sequences */
     if (deletesw) {
-       for (seqp = 0; seqs[seqp]; seqp++)
-           if (!seq_delsel (mp, seqs[seqp], publicsw, zerosw))
+       for (seqp = 0; seqp < svector_size (seqs); seqp++)
+           if (!seq_delsel (mp, svector_at (seqs, seqp), publicsw, zerosw))
                done (1);
     }
 
@@ -205,8 +194,8 @@ main (int argc, char **argv)
     if (listsw) {
        if (seqp) {
            /* print the sequences given */
-           for (seqp = 0; seqs[seqp]; seqp++)
-               seq_print (mp, seqs[seqp]);
+           for (seqp = 0; seqp < svector_size (seqs); seqp++)
+               seq_print (mp, svector_at (seqs, seqp));
        } else {
            /* else print them all */
            seq_printall (mp);
@@ -217,6 +206,7 @@ main (int argc, char **argv)
            seq_printdebug (mp);
     }
 
+    svector_free (seqs);
     seq_save (mp);                     /* synchronize message sequences */
     context_replace (pfolder, folder); /* update current folder         */
     context_save ();                   /* save the context file         */
@@ -254,18 +244,22 @@ print_debug (struct msgs *mp)
 /*
  * Print debugging info about all the SELECTED
  * messages and the sequences they are in.
+ * Due to limitations of snprintb(), only a limited
+ * number of sequences will be printed.  See the
+ * comments in sbr/seq_bits.c.
  */
 static void
 seq_printdebug (struct msgs *mp)
 {
     int msgnum;
-    char buf[100];
+    char buf[BUFSIZ];
 
-    printf ("\n");
+    putchar('\n');
     for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
        if (is_selected (mp, msgnum))
            printf ("%*d: %s\n", DMAXFOLDER, msgnum,
-               snprintb (buf, sizeof(buf),
-               (unsigned) mp->msgstats[msgnum - mp->lowoff], seq_bits (mp)));
+                   snprintb (buf, sizeof buf,
+                             (unsigned) bvector_first_bits (msgstat (mp, msgnum)),
+                             seq_bits (mp)));
     }
 }