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