]> diplodocus.org Git - nmh/blobdiff - uip/mark.c
Add basic support for the STLS command in POP
[nmh] / uip / mark.c
index e26ca6a5938e0579a354d248eb900a994f1d0f62..72328bacaccfae16cebfb5e3fdd79ec4afe9d416 100644 (file)
@@ -1,44 +1,57 @@
-
-/*
- * 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
  *
- * $Id$
- *
  * 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>
-
-static struct swit switches[] = {
-#define        ADDSW               0
-    { "add", 0 },
-#define        DELSW               1
-    { "delete", 0 },
-#define        LSTSW               2
-    { "list", 0 },
-#define        SEQSW               3
-    { "sequence name", 0 },
-#define        PUBLSW              4
-    { "public", 0 },
-#define        NPUBLSW             5
-    { "nopublic", 0 },
-#define        ZEROSW              6
-    { "zero", 0 },
-#define        NZEROSW             7
-    { "nozero", 0 },
-#define VERSIONSW           8
-    { "version", 0 },
-#define        HELPSW              9
-    { "help", 0 },
-#define        DEBUGSW            10
-    { "debug", -5 },
-    { NULL, 0 }
-};
+#include "h/mh.h"
+#include "sbr/getarguments.h"
+#include "sbr/seq_save.h"
+#include "sbr/smatch.h"
+#include "sbr/snprintb.h"
+#include "sbr/m_convert.h"
+#include "sbr/getfolder.h"
+#include "sbr/folder_read.h"
+#include "sbr/folder_free.h"
+#include "sbr/context_save.h"
+#include "sbr/context_replace.h"
+#include "sbr/context_find.h"
+#include "sbr/ambigsw.h"
+#include "sbr/path.h"
+#include "sbr/print_version.h"
+#include "sbr/print_help.h"
+#include "sbr/seq_bits.h"
+#include "sbr/seq_del.h"
+#include "sbr/seq_print.h"
+#include "sbr/seq_add.h"
+#include "sbr/error.h"
+#include "h/done.h"
+#include "h/utils.h"
+#include "sbr/m_maildir.h"
+
+#define MARK_SWITCHES \
+    X("add", 0, ADDSW) \
+    X("delete", 0, DELSW) \
+    X("list", 0, LSTSW) \
+    X("sequence name", 0, SEQSW) \
+    X("public", 0, PUBLSW) \
+    X("nopublic", 0, NPUBLSW) \
+    X("zero", 0, ZEROSW) \
+    X("nozero", 0, NZEROSW) \
+    X("version", 0, VERSIONSW) \
+    X("help", 0, HELPSW) \
+    X("debug", -5, DEBUGSW) \
+
+#define X(sw, minchars, id) id,
+DEFINE_SWITCH_ENUM(MARK);
+#undef X
+
+#define X(sw, minchars, id) { sw, minchars, id },
+DEFINE_SWITCH_ARRAY(MARK, switches);
+#undef X
 
 /*
  * static prototypes
@@ -50,22 +63,21 @@ static void seq_printdebug (struct msgs *);
 int
 main (int argc, char **argv)
 {
-    int addsw = 0, deletesw = 0, debugsw = 0;
-    int listsw = 0, publicsw = -1, zerosw = 0;
-    int seqp = 0, msgnum;
+    bool addsw = false;
+    bool deletesw = false;
+    bool debugsw = false;
+    bool listsw = false;
+    int publicsw = -1;
+    bool zerosw = false;
+    int msgnum;
+    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], true, true)) { return 1; }
 
     arguments = getarguments (invo_name, argc, argv, 1);
     argp = arguments;
@@ -80,38 +92,39 @@ main (int argc, char **argv)
                ambigsw (cp, switches);
                done (1);
            case UNKWNSW: 
-               adios (NULL, "-%s unknown\n", cp);
+               die("-%s unknown\n", cp);
 
            case HELPSW: 
                snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
                          invo_name);
                print_help (buf, switches, 1);
-               done (1);
+               done (0);
            case VERSIONSW:
                print_version(invo_name);
-               done (1);
+               done (0);
 
            case ADDSW: 
-               addsw++;
-               deletesw = listsw = 0;
+               addsw = true;
+               deletesw = false;
+                listsw = false;
                continue;
            case DELSW: 
-               deletesw++;
-               addsw = listsw = 0;
+               deletesw = true;
+               addsw = false;
+                listsw = false;
                continue;
            case LSTSW: 
-               listsw++;
-               addsw = deletesw = 0;
+               listsw = true;
+               addsw = false;
+                deletesw = false;
                continue;
 
            case SEQSW: 
                if (!(cp = *argp++) || *cp == '-')
-                   adios (NULL, "missing argument to %s", argp[-2]);
+                   die("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: 
@@ -122,24 +135,23 @@ main (int argc, char **argv)
                continue;
 
            case DEBUGSW: 
-               debugsw++;
+               debugsw = true;
                continue;
 
            case ZEROSW: 
-               zerosw++;
+               zerosw = true;
                continue;
            case NZEROSW: 
-               zerosw = 0;
+               zerosw = false;
                continue;
            }
        }
        if (*cp == '+' || *cp == '@') {
            if (folder)
-               adios (NULL, "only one folder at a time!");
-           else
-               folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
+               die("only one folder at a time!");
+            folder = pluspath (cp);
        } else
-               app_msgarg(&msgs, cp);
+            app_msgarg(&msgs, cp);
     }
 
     /*
@@ -149,9 +161,9 @@ main (int argc, char **argv)
      */
     if (!addsw && !deletesw && !listsw) {
        if (seqp)
-           addsw++;
+           addsw = true;
        else
-           listsw++;
+           listsw = true;
     }
 
     if (!context_find ("path"))
@@ -166,8 +178,8 @@ main (int argc, char **argv)
        adios (maildir, "unable to change directory to");
 
     /* read folder and create message structure */
-    if (!(mp = folder_read (folder)))
-       adios (NULL, "unable to read folder %s", folder);
+    if (!(mp = folder_read (folder, 1)))
+       die("unable to read folder %s", folder);
 
     /* print some general debugging info */
     if (debugsw)
@@ -175,7 +187,7 @@ main (int argc, char **argv)
 
     /* check for empty folder */
     if (mp->nummsg == 0)
-       adios (NULL, "no messages in %s", folder);
+       die("no messages in %s", folder);
 
     /* parse all the message ranges/sequences and set SELECTED */
     for (msgnum = 0; msgnum < msgs.size; msgnum++)
@@ -183,28 +195,27 @@ main (int argc, char **argv)
            done (1);
 
     if (publicsw == 1 && is_readonly(mp))
-       adios (NULL, "folder %s is read-only, so -public not allowed", folder);
+       die("folder %s is read-only, so -public not allowed", folder);
 
     /*
      * Make sure at least one sequence has been
      * specified if we are adding or deleting.
      */
     if (seqp == 0 && (addsw || deletesw))
-       adios (NULL, "-%s requires at least one -sequence argument",
+       die("-%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);
     }
 
@@ -212,8 +223,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);
@@ -224,11 +235,13 @@ 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         */
     folder_free (mp);                  /* free folder/message structure */
-    return done (0);
+    done (0);
+    return 1;
 }
 
 
@@ -260,18 +273,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)));
     }
 }