Replace done with exit at uip
[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                                 exit(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                                 exit(0);
58                         case VERSIONSW:
59                                 print_version(invo_name);
60                                 exit(0);
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         if (mp->nummsg == 0) {
98                 adios(NULL, "no messages in %s", folder);
99         }
100         /*
101         ** parse all the message ranges/sequences and set SELECTED
102         ** (We do this for the refiling case as well, to complain
103         ** about invalid msg arguments in rmm, before we call refile.)
104         */
105         for (msgnum = 0; msgnum < msgs.size; msgnum++) {
106                 if (!m_convert(mp, msgs.msgs[msgnum])) {
107                         /* sysexits EX_USAGE */
108                         exit(1);
109                 }
110         }
111
112         context_replace(curfolder, folder);
113         context_save();
114
115         if (unlink_msgs) {
116                 /* "remove" the SELECTED messages */
117                 folder_delmsgs(mp, 1);
118                 seq_setprev(mp);
119                 seq_save(mp);
120                 folder_free(mp);
121                 return 0;
122         }
123
124         /* remove by refiling. */
125
126         folder_free(mp);
127         fflush(stdout);
128
129         if (msgs.size+6 > MAXARGS) {
130                 adios(NULL, "more than %d messages for refile exec",
131                                 MAXARGS - 6);
132         }
133         vec = (char **)mh_xmalloc((size_t)(msgs.size + 6) * sizeof(*vec));
134         vec[vecp++] = "refile";
135         vec[vecp++] = "-src";
136         vec[vecp++] = concat("+", folder, NULL);
137         vec[vecp++] = "-nolink";
138         vec[vecp++] = concat("+", trashfolder, NULL);
139         for (msgnum = 0; msgnum < msgs.size; msgnum++) {
140                 vec[vecp++] = msgs.msgs[msgnum];
141         }
142         vec[vecp] = NULL;
143
144         fflush(stdout);
145         switch (pid = fork()) {
146         case -1:
147                 adios("fork", "unable to");
148
149         case 0:
150                 execvp(*vec, vec);
151                 fprintf(stderr, "unable to exec ");
152                 perror(*vec);
153                 _exit(-1);
154
155         default:
156                 pidwait(pid, -1);
157         }
158
159         return 0;
160 }