]> diplodocus.org Git - nmh/blob - uip/rmm.c
Fix stupid accidental dependence on a bash quirk in previous
[nmh] / uip / rmm.c
1
2 /*
3 * rmm.c -- remove a message(s)
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13
14 /*
15 * We allocate space for message names and ranges
16 * (msgs array) this number of elements at a time.
17 */
18 #define MAXMSGS 256
19
20 static struct swit switches[] = {
21 #define UNLINKSW 0
22 { "unlink", 0 },
23 #define NUNLINKSW 1
24 { "nounlink", 0 },
25 #define VERSIONSW 2
26 { "version", 0 },
27 #define HELPSW 3
28 { "help", 0 },
29 { NULL, 0 }
30 };
31
32
33 int
34 main (int argc, char **argv)
35 {
36 int nummsgs, maxmsgs, msgnum, unlink_msgs = 0;
37 char *cp, *maildir, *folder = NULL;
38 char buf[BUFSIZ], **argp;
39 char **arguments, **msgs;
40 struct msgs *mp;
41
42 #ifdef LOCALE
43 setlocale(LC_ALL, "");
44 #endif
45 invo_name = r1bindex (argv[0], '/');
46
47 /* read user profile/context */
48 context_read();
49
50 arguments = getarguments (invo_name, argc, argv, 1);
51 argp = arguments;
52
53 /*
54 * Allocate the initial space to record message
55 * names and ranges.
56 */
57 nummsgs = 0;
58 maxmsgs = MAXMSGS;
59 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
60 adios (NULL, "unable to allocate storage");
61
62 /* parse arguments */
63 while ((cp = *argp++)) {
64 if (*cp == '-') {
65 switch (smatch (++cp, switches)) {
66 case AMBIGSW:
67 ambigsw (cp, switches);
68 done (1);
69 case UNKWNSW:
70 adios (NULL, "-%s unknown\n", cp);
71
72 case HELPSW:
73 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
74 invo_name);
75 print_help (buf, switches, 1);
76 done (1);
77 case VERSIONSW:
78 print_version(invo_name);
79 done (1);
80
81 case UNLINKSW:
82 unlink_msgs++;
83 continue;
84 case NUNLINKSW:
85 unlink_msgs = 0;
86 continue;
87 }
88 }
89 if (*cp == '+' || *cp == '@') {
90 if (folder)
91 adios (NULL, "only one folder at a time!");
92 else
93 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
94 } else {
95 /*
96 * Check if we need to allocate more space
97 * for message names/ranges.
98 */
99 if (nummsgs >= maxmsgs){
100 maxmsgs += MAXMSGS;
101 if (!(msgs = (char **) realloc (msgs,
102 (size_t) (maxmsgs * sizeof(*msgs)))))
103 adios (NULL, "unable to reallocate msgs storage");
104 }
105 msgs[nummsgs++] = cp;
106 }
107 }
108
109 if (!context_find ("path"))
110 free (path ("./", TFOLDER));
111 if (!nummsgs)
112 msgs[nummsgs++] = "cur";
113 if (!folder)
114 folder = getfolder (1);
115 maildir = m_maildir (folder);
116
117 if (chdir (maildir) == NOTOK)
118 adios (maildir, "unable to change directory to");
119
120 /* read folder and create message structure */
121 if (!(mp = folder_read (folder)))
122 adios (NULL, "unable to read folder %s", folder);
123
124 /* check for empty folder */
125 if (mp->nummsg == 0)
126 adios (NULL, "no messages in %s", folder);
127
128 /* parse all the message ranges/sequences and set SELECTED */
129 for (msgnum = 0; msgnum < nummsgs; msgnum++)
130 if (!m_convert (mp, msgs[msgnum]))
131 done (1);
132 seq_setprev (mp); /* set the previous-sequence */
133
134 /*
135 * This is hackish. If we are using a external rmmproc,
136 * then we need to update the current folder in the
137 * context so the external rmmproc will remove files
138 * from the correct directory. This should be moved to
139 * folder_delmsgs().
140 */
141 if (rmmproc) {
142 context_replace (pfolder, folder);
143 context_save ();
144 fflush (stdout);
145 }
146
147 /* "remove" the SELECTED messages */
148 folder_delmsgs (mp, unlink_msgs, 0);
149
150 seq_save (mp); /* synchronize message sequences */
151 context_replace (pfolder, folder); /* update current folder */
152 context_save (); /* save the context file */
153 folder_free (mp); /* free folder structure */
154 return done (0);
155 }