Fix out-of-bounds error when incorporating email from stdin
[mmh] / uip / mark.c
1 /*
2 ** mark.c -- add message(s) to sequences in given folder
3 **        -- delete messages (s) from sequences in given folder
4 **        -- list sequences in given folder
5 **
6 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
7 ** COPYRIGHT file in the root directory of the nmh distribution for
8 ** complete copyright information.
9 */
10
11 #include <h/mh.h>
12 #include <h/utils.h>
13 #include <unistd.h>
14 #include <locale.h>
15 #include <sysexits.h>
16
17 static struct swit switches[] = {
18 #define ADDSW  0
19         { "add", 0 },
20 #define DELSW  1
21         { "delete", 0 },
22 #define LSTSW  2
23         { "list", 0 },
24 #define SEQSW  3
25         { "sequence name", 0 },
26 #define PUBLSW  4
27         { "public", 0 },
28 #define NPUBLSW  5
29         { "nopublic", 2 },
30 #define ZEROSW  6
31         { "zero", 0 },
32 #define NZEROSW  7
33         { "nozero", 2 },
34 #define VERSIONSW  8
35         { "Version", 0 },
36 #define HELPSW  9
37         { "help", 0 },
38 #define DEBUGSW  10
39         { "debug", -5 },
40         { NULL, 0 }
41 };
42
43 /*
44 ** static prototypes
45 */
46 static void print_debug(struct msgs *);
47 static void seq_printdebug(struct msgs *);
48
49
50 int
51 main(int argc, char **argv)
52 {
53         int addsw = 0, deletesw = 0, debugsw = 0;
54         int listsw = 0, publicsw = -1, zerosw = 0, msgnum;
55         unsigned int seqp = 0;
56         char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
57         char **argp, **arguments;
58         char *seqs[NUMATTRS + 1];
59         struct msgs_array msgs = { 0, 0, NULL };
60         struct msgs *mp;
61
62         setlocale(LC_ALL, "");
63         invo_name = mhbasename(argv[0]);
64
65         /* read user profile/context */
66         context_read();
67
68         arguments = getarguments(invo_name, argc, argv, 1);
69         argp = arguments;
70
71         /*
72         ** Parse arguments
73         */
74         while ((cp = *argp++)) {
75                 if (*cp == '-') {
76                         switch (smatch(++cp, switches)) {
77                         case AMBIGSW:
78                                 ambigsw(cp, switches);
79                                 exit(EX_USAGE);
80                         case UNKWNSW:
81                                 adios(EX_USAGE, NULL, "-%s unknown\n", cp);
82
83                         case HELPSW:
84                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
85                                 print_help(buf, switches, 1);
86                                 exit(argc == 2 ? EX_OK : EX_USAGE);
87                         case VERSIONSW:
88                                 print_version(invo_name);
89                                 exit(argc == 2 ? EX_OK : EX_USAGE);
90
91                         case ADDSW:
92                                 addsw++;
93                                 deletesw = listsw = 0;
94                                 continue;
95                         case DELSW:
96                                 deletesw++;
97                                 addsw = listsw = 0;
98                                 continue;
99                         case LSTSW:
100                                 listsw++;
101                                 addsw = deletesw = 0;
102                                 continue;
103
104                         case SEQSW:
105                                 if (!(cp = *argp++) || *cp == '-') {
106                                         adios(EX_USAGE, NULL, "missing argument to %s",
107                                                         argp[-2]);
108                                 }
109
110                                 /* check if too many sequences specified */
111                                 if (seqp >= NUMATTRS) {
112                                         adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS);
113                                 }
114                                 seqs[seqp++] = cp;
115                                 continue;
116
117                         case PUBLSW:
118                                 publicsw = 1;
119                                 continue;
120                         case NPUBLSW:
121                                 publicsw = 0;
122                                 continue;
123
124                         case DEBUGSW:
125                                 debugsw++;
126                                 continue;
127
128                         case ZEROSW:
129                                 zerosw++;
130                                 continue;
131                         case NZEROSW:
132                                 zerosw = 0;
133                                 continue;
134                         }
135                 }
136                 if (*cp == '+' || *cp == '@') {
137                         if (folder) {
138                                 adios(EX_USAGE, NULL, "only one folder at a time!");
139                         } else {
140                                 folder = mh_xstrdup(expandfol(cp));
141                         }
142                 } else {
143                         app_msgarg(&msgs, cp);
144                 }
145         }
146
147         /*
148         ** If we haven't specified -add, -delete, or -list,
149         ** then use -add if a sequence was specified, else
150         ** use -list.
151         */
152         if (!addsw && !deletesw && !listsw) {
153                 if (seqp)
154                         addsw++;
155                 else
156                         listsw++;
157         }
158
159         if (!msgs.size)
160                 app_msgarg(&msgs, listsw ? seq_all : seq_cur);
161         if (!folder)
162                 folder = getcurfol();
163         maildir = toabsdir(folder);
164
165         if (chdir(maildir) == NOTOK) {
166                 adios(EX_OSERR, maildir, "unable to change directory to");
167         }
168
169         /* read folder and create message structure */
170         if (!(mp = folder_read(folder))) {
171                 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
172         }
173
174         /* print some general debugging info */
175         if (debugsw)
176                 print_debug(mp);
177
178         /* check for empty folder */
179         if (mp->nummsg == 0) {
180                 adios(EX_DATAERR, NULL, "no messages in %s", folder);
181         }
182
183         /* parse all the message ranges/sequences and set SELECTED */
184         for (msgnum = 0; msgnum < msgs.size; msgnum++) {
185                 if (!m_convert(mp, msgs.msgs[msgnum])) {
186                         exit(EX_USAGE);
187                 }
188         }
189
190         if (publicsw == 1 && is_readonly(mp)) {
191                 adios(EX_NOPERM, NULL, "folder %s is read-only, so -public not allowed",
192                                 folder);
193         }
194
195         /*
196         ** Make sure at least one sequence has been
197         ** specified if we are adding or deleting.
198         */
199         if (seqp == 0 && (addsw || deletesw)) {
200                 adios(EX_USAGE, NULL, "-%s requires at least one -sequence argument",
201                                 addsw ? "add" : "delete");
202         }
203         seqs[seqp] = NULL;
204
205         /* Adding messages to sequences */
206         if (addsw) {
207                 for (seqp = 0; seqs[seqp]; seqp++) {
208                         if (!seq_addsel(mp, seqs[seqp], publicsw, zerosw)) {
209                                 exit(EX_SOFTWARE);
210                         }
211                 }
212         }
213
214         /* Deleting messages from sequences */
215         if (deletesw) {
216                 for (seqp = 0; seqs[seqp]; seqp++) {
217                         if (!seq_delsel(mp, seqs[seqp], publicsw, zerosw)) {
218                                 exit(EX_SOFTWARE);
219                         }
220                 }
221         }
222
223         /* Listing messages in sequences */
224         if (listsw) {
225                 if (seqp) {
226                         /* print the sequences given */
227                         for (seqp = 0; seqs[seqp]; seqp++)
228                                 seq_print(mp, seqs[seqp]);
229                 } else {
230                         /* else print them all */
231                         seq_printall(mp);
232                 }
233
234                 /* print debugging info about SELECTED messages */
235                 if (debugsw)
236                         seq_printdebug(mp);
237         }
238
239         seq_save(mp);  /* synchronize message sequences */
240         context_replace(curfolder, folder);  /* update current folder */
241         context_save();  /* save the context file */
242         folder_free(mp);  /* free folder/message structure */
243         return EX_OK;
244 }
245
246
247 /*
248 ** Print general debugging info
249 */
250 static void
251 print_debug(struct msgs *mp)
252 {
253         char buf[100];
254
255         printf("invo_name = %s\n", invo_name);
256         printf("mypath = %s\n", mypath);
257         printf("defpath = %s\n", defpath);
258         printf("ctxpath = %s\n", ctxpath);
259         printf("context flags = %s\n", snprintb(buf, sizeof(buf),
260                         (unsigned) ctxflags, DBITS));
261         printf("foldpath = %s\n", mp->foldpath);
262         printf("folder flags  = %s\n\n", snprintb(buf, sizeof(buf),
263                         (unsigned) mp->msgflags, FBITS));
264         printf("lowmsg=%d hghmsg=%d nummsg=%d curmsg=%d\n",
265                         mp->lowmsg, mp->hghmsg, mp->nummsg, mp->curmsg);
266         printf("lowsel=%d hghsel=%d numsel=%d\n",
267                         mp->lowsel, mp->hghsel, mp->numsel);
268         printf("lowoff=%d hghoff=%d\n\n", mp->lowoff, mp->hghoff);
269 }
270
271
272 /*
273 ** Print debugging info about all the SELECTED
274 ** messages and the sequences they are in.
275 */
276 static void
277 seq_printdebug(struct msgs *mp)
278 {
279         int msgnum;
280         char buf[100];
281
282         printf("\n");
283         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
284                 if (is_selected(mp, msgnum))
285                         printf("%*d: %s\n", DMAXFOLDER, msgnum, snprintb(buf, sizeof(buf), (unsigned) mp->msgstats[msgnum - mp->lowoff], seq_bits(mp)));
286         }
287 }