]> diplodocus.org Git - nmh/blob - uip/rmm.c
Use va_copy() to get a copy of va_list, instead of using original.
[nmh] / uip / rmm.c
1 /* rmm.c -- remove a message(s)
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include "h/done.h"
10 #include <h/utils.h>
11 #include "sbr/m_maildir.h"
12
13 #define RMM_SWITCHES \
14 X("unlink", 0, UNLINKSW) \
15 X("nounlink", 0, NUNLINKSW) \
16 X("rmmproc program", 0, RPROCSW) \
17 X("normmproc", 0, NRPRCSW) \
18 X("version", 0, VERSIONSW) \
19 X("help", 0, HELPSW) \
20
21 #define X(sw, minchars, id) id,
22 DEFINE_SWITCH_ENUM(RMM);
23 #undef X
24
25 #define X(sw, minchars, id) { sw, minchars, id },
26 DEFINE_SWITCH_ARRAY(RMM, switches);
27 #undef X
28
29
30 int
31 main (int argc, char **argv)
32 {
33 int msgnum;
34 bool unlink_msgs = false;
35 char *cp, *maildir, *folder = NULL;
36 char buf[BUFSIZ], **argp;
37 char **arguments;
38 struct msgs_array msgs = { 0, 0, NULL };
39 struct msgs *mp;
40
41 if (nmh_init(argv[0], true, true)) { return 1; }
42
43 arguments = getarguments (invo_name, argc, argv, 1);
44 argp = arguments;
45
46 /* parse arguments */
47 while ((cp = *argp++)) {
48 if (*cp == '-') {
49 switch (smatch (++cp, switches)) {
50 case AMBIGSW:
51 ambigsw (cp, switches);
52 done (1);
53 case UNKWNSW:
54 die("-%s unknown\n", cp);
55
56 case HELPSW:
57 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
58 invo_name);
59 print_help (buf, switches, 1);
60 done (0);
61 case VERSIONSW:
62 print_version(invo_name);
63 done (0);
64
65 case UNLINKSW:
66 unlink_msgs = true;
67 continue;
68 case NUNLINKSW:
69 unlink_msgs = false;
70 continue;
71
72 case RPROCSW:
73 if (!(rmmproc = *argp++) || *rmmproc == '-')
74 die("missing argument to %s", argp[-2]);
75 continue;
76 case NRPRCSW:
77 rmmproc = NULL;
78 continue;
79 }
80 }
81 if (*cp == '+' || *cp == '@') {
82 if (folder)
83 die("only one folder at a time!");
84 folder = pluspath (cp);
85 } else
86 app_msgarg(&msgs, cp);
87 }
88
89 if (!context_find ("path"))
90 free (path ("./", TFOLDER));
91 if (!msgs.size)
92 app_msgarg(&msgs, "cur");
93 if (!folder)
94 folder = getfolder (1);
95 maildir = m_maildir (folder);
96
97 if (chdir (maildir) == NOTOK)
98 adios (maildir, "unable to change directory to");
99
100 /* read folder and create message structure */
101 if (!(mp = folder_read (folder, 1)))
102 die("unable to read folder %s", folder);
103
104 /* check for empty folder */
105 if (mp->nummsg == 0)
106 die("no messages in %s", folder);
107
108 /* parse all the message ranges/sequences and set SELECTED */
109 for (msgnum = 0; msgnum < msgs.size; msgnum++)
110 if (!m_convert (mp, msgs.msgs[msgnum]))
111 done (1);
112 seq_setprev (mp); /* set the previous-sequence */
113
114 /*
115 * As part of the new world locking order, folder_delmsgs() now updates
116 * the sequence and context for us. But since folder_delmsgs() doesn't
117 * have access to the folder name, change the context now.
118 */
119
120 context_replace (pfolder, folder);
121
122 /* "remove" the SELECTED messages */
123 folder_delmsgs (mp, unlink_msgs, 0);
124
125 folder_free (mp); /* free folder structure */
126 done (0);
127 return 1;
128 }