]> diplodocus.org Git - nmh/blob - uip/rmm.c
picksbr.c: fprintf(3) function pointer using `%p' and union.
[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, unlink_msgs = 0;
34 char *cp, *maildir, *folder = NULL;
35 char buf[BUFSIZ], **argp;
36 char **arguments;
37 struct msgs_array msgs = { 0, 0, NULL };
38 struct msgs *mp;
39
40 if (nmh_init(argv[0], 1)) { return 1; }
41
42 arguments = getarguments (invo_name, argc, argv, 1);
43 argp = arguments;
44
45 /* parse arguments */
46 while ((cp = *argp++)) {
47 if (*cp == '-') {
48 switch (smatch (++cp, switches)) {
49 case AMBIGSW:
50 ambigsw (cp, switches);
51 done (1);
52 case UNKWNSW:
53 adios (NULL, "-%s unknown\n", cp);
54
55 case HELPSW:
56 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
57 invo_name);
58 print_help (buf, switches, 1);
59 done (0);
60 case VERSIONSW:
61 print_version(invo_name);
62 done (0);
63
64 case UNLINKSW:
65 unlink_msgs++;
66 continue;
67 case NUNLINKSW:
68 unlink_msgs = 0;
69 continue;
70
71 case RPROCSW:
72 if (!(rmmproc = *argp++) || *rmmproc == '-')
73 adios (NULL, "missing argument to %s", argp[-2]);
74 continue;
75 case NRPRCSW:
76 rmmproc = NULL;
77 continue;
78 }
79 }
80 if (*cp == '+' || *cp == '@') {
81 if (folder)
82 adios (NULL, "only one folder at a time!");
83 folder = pluspath (cp);
84 } else
85 app_msgarg(&msgs, cp);
86 }
87
88 if (!context_find ("path"))
89 free (path ("./", TFOLDER));
90 if (!msgs.size)
91 app_msgarg(&msgs, "cur");
92 if (!folder)
93 folder = getfolder (1);
94 maildir = m_maildir (folder);
95
96 if (chdir (maildir) == NOTOK)
97 adios (maildir, "unable to change directory to");
98
99 /* read folder and create message structure */
100 if (!(mp = folder_read (folder, 1)))
101 adios (NULL, "unable to read folder %s", folder);
102
103 /* check for empty folder */
104 if (mp->nummsg == 0)
105 adios (NULL, "no messages in %s", folder);
106
107 /* parse all the message ranges/sequences and set SELECTED */
108 for (msgnum = 0; msgnum < msgs.size; msgnum++)
109 if (!m_convert (mp, msgs.msgs[msgnum]))
110 done (1);
111 seq_setprev (mp); /* set the previous-sequence */
112
113 /*
114 * As part of the new world locking order, folder_delmsgs() now updates
115 * the sequence and context for us. But since folder_delmsgs() doesn't
116 * have access to the folder name, change the context now.
117 */
118
119 context_replace (pfolder, folder);
120
121 /* "remove" the SELECTED messages */
122 folder_delmsgs (mp, unlink_msgs, 0);
123
124 folder_free (mp); /* free folder structure */
125 done (0);
126 return 1;
127 }