coding style: Added braces even for one-line blocks
[mmh] / uip / rmm.c
1 /*
2 ** rmm.c -- remove a message(s)
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11
12 static struct swit switches[] = {
13 #define UNLINKSW  0
14         { "unlink", 0 },
15 #define NUNLINKSW  1
16         { "nounlink", 2 },
17 #define VERSIONSW  2
18         { "Version", 0 },
19 #define HELPSW  3
20         { "help", 0 },
21         { NULL, 0 }
22 };
23
24
25 int
26 main(int argc, char **argv)
27 {
28         int msgnum, unlink_msgs = 0, vecp = 0;
29         char *cp, *maildir, *folder = NULL, **vec;
30         char buf[BUFSIZ], **argp;
31         char **arguments;
32         struct msgs_array msgs = { 0, 0, NULL };
33         struct msgs *mp;
34         pid_t pid;
35
36         setlocale(LC_ALL, "");
37         invo_name = mhbasename(argv[0]);
38
39         context_read();
40
41         arguments = getarguments(invo_name, argc, argv, 1);
42         argp = arguments;
43
44         /* parse arguments */
45         while ((cp = *argp++)) {
46                 if (*cp == '-') {
47                         switch (smatch(++cp, switches)) {
48                         case AMBIGSW:
49                                 ambigsw(cp, switches);
50                                 done(1);
51                         case UNKWNSW:
52                                 adios(NULL, "-%s unknown\n", cp);
53
54                         case HELPSW:
55                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
56                                 print_help(buf, switches, 1);
57                                 done(1);
58                         case VERSIONSW:
59                                 print_version(invo_name);
60                                 done(1);
61
62                         case UNLINKSW:
63                                 unlink_msgs++;
64                                 continue;
65                         case NUNLINKSW:
66                                 unlink_msgs = 0;
67                                 continue;
68                         }
69                 }
70                 if (*cp == '+' || *cp == '@') {
71                         if (folder) {
72                                 adios(NULL, "only one folder at a time!");
73                         } else {
74                                 folder = getcpy(expandfol(cp));
75                         }
76                 } else {
77                         app_msgarg(&msgs, cp);
78                 }
79         }
80
81         if (!msgs.size) {
82                 app_msgarg(&msgs, seq_cur);
83         }
84         if (!folder) {
85                 folder = getcurfol();
86         }
87         maildir = toabsdir(folder);
88
89         if (chdir(maildir) == NOTOK) {
90                 adios(maildir, "unable to change directory to");
91         }
92
93         /* read folder and create message structure */
94         if (!(mp = folder_read(folder))) {
95                 adios(NULL, "unable to read folder %s", folder);
96         }
97
98         /* check for empty folder */
99         if (mp->nummsg == 0) {
100                 adios(NULL, "no messages in %s", folder);
101         }
102
103         /* parse all the message ranges/sequences and set SELECTED */
104         for (msgnum = 0; msgnum < msgs.size; msgnum++) {
105                 if (!m_convert(mp, msgs.msgs[msgnum])) {
106                         done(1);
107                 }
108         }
109         seq_setprev(mp);  /* set the previous-sequence */
110
111
112         if (unlink_msgs) {
113                 /* "remove" the SELECTED messages */
114                 folder_delmsgs(mp, 1);
115
116                 seq_save(mp);
117                 context_replace(curfolder, folder);
118                 context_save();
119                 folder_free(mp);
120                 done(0);
121                 return 1;
122         }
123
124         /*
125         ** This is hackish.  If we don't unlink, but refile,
126         ** then we need to update the current folder in the
127         ** context so the external program will refile files
128         ** from the correct directory.
129         */
130         context_replace(curfolder, folder);
131         context_save();
132         fflush(stdout);
133
134         /* remove by refiling. */
135         /* Unset the EXISTS flag for each message to be removed */
136         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
137                 if (is_selected(mp, msgnum)) {
138                         unset_exists(mp, msgnum);
139                 }
140         }
141
142         /* Mark that the sequence information has changed */
143         mp->msgflags |= SEQMOD;
144
145         if (mp->numsel+4 > MAXARGS) {
146                 adios(NULL, "more than %d messages for refile exec",
147                                 MAXARGS - 4);
148         }
149         vec = (char **)mh_xmalloc((size_t)(mp->numsel + 4) * sizeof(*vec));
150         vec[vecp++] = "refile";
151         vec[vecp++] = "-nolink";
152         vec[vecp++] = concat("+", trashfolder, NULL);
153         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
154                 if (!is_selected(mp, msgnum)) {
155                         continue;
156                 }
157                 if (!(vec[vecp++] = strdup(m_name(msgnum)))) {
158                         adios(NULL, "strdup failed");
159                 }
160         }
161         vec[vecp] = NULL;
162
163         fflush(stdout);
164         switch (pid = fork()) {
165         case -1:
166                 adios("fork", "unable to");
167
168         case 0:
169                 execvp(*vec, vec);
170                 fprintf(stderr, "unable to exec ");
171                 perror(*vec);
172                 _exit(-1);
173
174         default:
175                 pidwait(pid, -1);
176         }
177
178         folder_free(mp);
179         done(0);
180         return 1;
181 }