]> diplodocus.org Git - nmh/blob - sbr/m_convert.c
Fix invalid pointer arithmetic.
[nmh] / sbr / m_convert.c
1 /* m_convert.c -- parse a message range or sequence and set SELECTED
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/utils.h>
10
11 /*
12 * error codes for sequence
13 * and message range processing
14 */
15 #define BADMSG (-2)
16 #define BADRNG (-3)
17 #define BADNEW (-4)
18 #define BADNUM (-5)
19 #define BADLST (-6)
20
21 #define FIRST 1
22 #define LAST 2
23
24
25 #define getnew(mp) (mp->hghmsg + 1)
26
27 static int convdir; /* convert direction */
28 static char *delimp;
29
30 /*
31 * static prototypes
32 */
33 static int m_conv (struct msgs *, char *, int);
34 static int attr (struct msgs *, char *);
35
36
37 int
38 m_convert (struct msgs *mp, char *name)
39 {
40 int first, last, found, count, is_range, err;
41 char *bp, *cp;
42
43 /* check if user defined sequence */
44 err = attr (mp, cp = name);
45
46 if (err == -1)
47 return 0;
48 if (err < 0)
49 goto badmsg;
50 if (err > 0)
51 return 1;
52 /*
53 * else err == 0, so continue
54 */
55
56 found = 0;
57 is_range = 1;
58
59 /*
60 * Check for special "new" sequence, which
61 * is valid only if ALLOW_NEW is set.
62 */
63 if ((mp->msgflags & ALLOW_NEW) && !strcmp (cp, "new")) {
64 if ((err = first = getnew (mp)) <= 0)
65 goto badmsg;
66 goto single;
67 }
68
69 if (!strcmp (cp, "all"))
70 cp = "first-last";
71
72 if ((err = first = m_conv (mp, cp, FIRST)) <= 0)
73 goto badmsg;
74
75 cp = delimp;
76 if (*cp != '\0' && *cp != '-' && *cp != ':' && *cp != '=') {
77 badelim:
78 inform("illegal argument delimiter: `%c'(0%o)", *delimp, *delimp);
79 return 0;
80 }
81
82 if (*cp == '-') {
83 cp++;
84 if ((err = last = m_conv (mp, cp, LAST)) <= 0) {
85 badmsg:
86 switch (err) {
87 case BADMSG:
88 inform("no %s message", cp);
89 break;
90
91 case BADNUM:
92 inform("message %s doesn't exist", cp);
93 break;
94
95 case BADRNG:
96 inform("message %s out of range 1-%d", cp, mp->hghmsg);
97 break;
98
99 case BADLST:
100 badlist:
101 inform("bad message list %s", name);
102 break;
103
104 case BADNEW:
105 inform("folder full, no %s message", name);
106 break;
107
108 default:
109 inform("no messages match specification");
110 }
111 return 0;
112 }
113
114 if (last < first)
115 goto badlist;
116 if (*delimp)
117 goto badelim;
118 if (first > mp->hghmsg || last < mp->lowmsg) {
119 rangerr:
120 inform("no messages in range %s", name);
121 return 0;
122 }
123
124 /* tighten the range to search */
125 if (last > mp->hghmsg)
126 last = mp->hghmsg;
127 if (first < mp->lowmsg)
128 first = mp->lowmsg;
129
130 } else if (*cp == ':' || *cp == '=') {
131
132 if (*cp == '=')
133 is_range = 0;
134
135 cp++;
136
137 if (*cp == '-') {
138 /* foo:-3 or foo=-3 */
139 convdir = -1;
140 cp++;
141 } else if (*cp == '+') {
142 /* foo:+3 or foo=+3 is same as foo:3 or foo=3 */
143 convdir = 1;
144 cp++;
145 }
146 if ((count = atoi (bp = cp)) == 0)
147 goto badlist;
148 while (isdigit ((unsigned char) *bp))
149 bp++;
150 if (*bp)
151 goto badelim;
152 if ((convdir > 0 && first > mp->hghmsg)
153 || (convdir < 0 && first < mp->lowmsg))
154 goto rangerr;
155
156 /* tighten the range to search */
157 if (first < mp->lowmsg)
158 first = mp->lowmsg;
159 if (first > mp->hghmsg)
160 first = mp->hghmsg;
161
162 for (last = first;
163 last >= mp->lowmsg && last <= mp->hghmsg;
164 last += convdir)
165 if (does_exist (mp, last))
166 if (--count <= 0)
167 break;
168 if (is_range) { /* a range includes any messages that exist */
169 if (last < mp->lowmsg)
170 last = mp->lowmsg;
171 if (last > mp->hghmsg)
172 last = mp->hghmsg;
173 if (last < first) {
174 count = last;
175 last = first;
176 first = count;
177 }
178 } else { /* looking for the nth message. if not enough, fail. */
179 if (last < mp->lowmsg || last > mp->hghmsg) {
180 inform("no such message");
181 return 0;
182 }
183 first = last;
184 }
185 } else {
186
187 single:
188 /*
189 * Single Message
190 *
191 * If ALLOW_NEW is set, then allow selecting of an
192 * empty slot. If ALLOW_NEW is not set, then we
193 * check if message is in-range and exists.
194 */
195 if (mp->msgflags & ALLOW_NEW) {
196 /*
197 * We can get into a case where the "cur" sequence is way out
198 * of range, and because it's allowed to not exist (think
199 * of "rmm; next") it doesn't get checked to make sure it's
200 * within the range of messages in seq_init(). So if our
201 * desired sequence is out of range of the allocated folder
202 * limits simply reallocate the folder so it's within range.
203 */
204 if (first < mp->lowoff || first > mp->hghoff)
205 mp = folder_realloc(mp, min(first, mp->lowoff),
206 max(first, mp->hghoff));
207
208 set_select_empty (mp, first);
209 } else {
210 if (first > mp->hghmsg
211 || first < mp->lowmsg
212 || !(does_exist (mp, first))) {
213 if (!strcmp (name, "cur") || !strcmp (name, "."))
214 inform("no %s message", name);
215 else
216 inform("message %d doesn't exist", first);
217 return 0;
218 }
219 }
220 last = first; /* range of 1 */
221 }
222
223 /*
224 * Cycle through the range and select the messages
225 * that exist. If ALLOW_NEW is set, then we also check
226 * if we are selecting an empty slot.
227 */
228 for (; first <= last; first++) {
229 if (does_exist (mp, first) ||
230 ((mp->msgflags & ALLOW_NEW) && is_select_empty (mp, first))) {
231 if (!is_selected (mp, first)) {
232 set_selected (mp, first);
233 mp->numsel++;
234 if (mp->lowsel == 0 || first < mp->lowsel)
235 mp->lowsel = first;
236 if (first > mp->hghsel)
237 mp->hghsel = first;
238 }
239 found++;
240 }
241 }
242
243 if (!found)
244 goto rangerr;
245
246 return 1;
247 }
248
249 /*
250 * Convert the various message names to
251 * their numeric values.
252 *
253 * n (integer)
254 * prev
255 * next
256 * first
257 * last
258 * cur
259 * . (same as cur)
260 */
261
262 static int
263 m_conv (struct msgs *mp, char *str, int call)
264 {
265 int i;
266 char *cp, *bp;
267 char buf[16];
268
269 convdir = 1;
270 cp = bp = str;
271 if (isdigit ((unsigned char) *cp)) {
272 while (isdigit ((unsigned char) *bp))
273 bp++;
274 delimp = bp;
275 i = atoi (cp);
276
277 if (i <= mp->hghmsg)
278 return i;
279 if (*delimp || call == LAST)
280 return mp->hghmsg + 1;
281 if (mp->msgflags & ALLOW_NEW)
282 return BADRNG;
283 return BADNUM;
284 }
285
286 /* doesn't enforce lower case */
287 for (bp = buf; (isalpha((unsigned char) *cp) || *cp == '.')
288 && (bp - buf < (int) sizeof(buf) - 1); )
289 {
290 *bp++ = *cp++;
291 }
292 *bp++ = '\0';
293 delimp = cp;
294
295 if (!strcmp (buf, "first"))
296 return mp->hghmsg || !(mp->msgflags & ALLOW_NEW) ?
297 mp->lowmsg : BADMSG;
298
299 if (!strcmp (buf, "last")) {
300 convdir = -1;
301 return mp->hghmsg || !(mp->msgflags & ALLOW_NEW) ? mp->hghmsg : BADMSG;
302 }
303
304 if (!strcmp (buf, "cur") || !strcmp (buf, "."))
305 return mp->curmsg > 0 ? mp->curmsg : BADMSG;
306
307 if (!strcmp (buf, "prev")) {
308 convdir = -1;
309 for (i = (mp->curmsg <= mp->hghmsg) ? mp->curmsg - 1 : mp->hghmsg;
310 i >= mp->lowmsg; i--) {
311 if (does_exist (mp, i))
312 return i;
313 }
314 return BADMSG;
315 }
316
317 if (!strcmp (buf, "next")) {
318 for (i = (mp->curmsg >= mp->lowmsg) ? mp->curmsg + 1 : mp->lowmsg;
319 i <= mp->hghmsg; i++) {
320 if (does_exist (mp, i))
321 return i;
322 }
323 return BADMSG;
324 }
325
326 return BADLST;
327 }
328
329 /*
330 * Handle user defined sequences.
331 * They can take the following forms:
332 *
333 * seq
334 * seq:prev
335 * seq:next
336 * seq:first
337 * seq:last
338 * seq:+n
339 * seq:-n
340 * seq:n
341 */
342
343 static int
344 attr (struct msgs *mp, char *cp)
345 {
346 char *dp;
347 char *bp = NULL;
348 char *ep;
349 char op;
350 int i, j;
351 int found,
352 inverted = 0,
353 count = 0, /* range given? else use entire sequence */
354 just_one = 0, /* want entire sequence or range */
355 first = 0,
356 start = 0;
357
358 /* hack for "cur-name", "cur-n", etc. */
359 if (!strcmp (cp, "cur"))
360 return 0;
361 if (has_prefix(cp, "cur")) {
362 if (cp[3] == ':' || cp[3] == '=')
363 return 0;
364 }
365
366 /* Check for sequence negation */
367 if ((dp = context_find (nsequence)) && *dp != '\0' && ssequal (dp, cp)) {
368 inverted = 1;
369 cp += strlen (dp);
370 }
371
372 convdir = 1; /* convert direction */
373
374 for (dp = cp; isalnum((unsigned char)*dp); dp++)
375 continue;
376
377 if (*dp == ':') {
378 bp = dp++;
379 count = 1;
380
381 /*
382 * seq:prev (or)
383 * seq:next (or)
384 * seq:first (or)
385 * seq:last
386 */
387 if (isalpha ((unsigned char) *dp)) {
388 if (!strcmp (dp, "prev")) {
389 convdir = -1;
390 first = (mp->curmsg > 0) && (mp->curmsg <= mp->hghmsg)
391 ? mp->curmsg - 1
392 : mp->hghmsg;
393 start = first;
394 }
395 else if (!strcmp (dp, "next")) {
396 convdir = 1;
397 first = (mp->curmsg >= mp->lowmsg)
398 ? mp->curmsg + 1
399 : mp->lowmsg;
400 start = first;
401 }
402 else if (!strcmp (dp, "first")) {
403 convdir = 1;
404 start = mp->lowmsg;
405 }
406 else if (!strcmp (dp, "last")) {
407 convdir = -1;
408 start = mp->hghmsg;
409 }
410 else {
411 return BADLST;
412 }
413 } else {
414 /*
415 * seq:n (or)
416 * seq:+n (or)
417 * seq:-n
418 */
419 if (*dp == '+') {
420 /* foo:+3 is same as foo:3 */
421 dp++;
422 convdir = 1;
423 start = mp->lowmsg;
424 } else if (*dp == '-' || *dp == ':') {
425 /* foo:-3 or foo::3 */
426 dp++;
427 convdir = -1;
428 start = mp->hghmsg;
429 }
430 count = strtol(dp,&ep,10);
431 if (count == 0 || *ep) /* 0 illegal */
432 return BADLST;
433 }
434
435 op = *bp;
436 *bp = '\0'; /* temporarily terminate sequence name */
437 } else if (*dp == '=') {
438
439 bp = dp++;
440
441 if (*dp == '+') {
442 /* foo=+3 is same as foo=3 */
443 dp++;
444 convdir = 1;
445 start = mp->lowmsg;
446 } else if (*dp == '-') {
447 /* foo=-3 */
448 dp++;
449 convdir = -1;
450 start = mp->hghmsg;
451 }
452
453 count = strtol(dp,&ep,10); /* 0 illegal */
454 if (count == 0 || *ep)
455 return BADLST;
456
457 just_one = 1;
458
459 op = *bp;
460 *bp = '\0'; /* temporarily terminate sequence name */
461 }
462
463 i = seq_getnum (mp, cp); /* get index of sequence */
464
465 if (bp)
466 *bp = op; /* restore sequence name */
467 if (i == -1)
468 return 0;
469
470 found = 0; /* count the number we select for this argument */
471
472 if (!start)
473 start = mp->lowmsg;
474
475 for (j = start; j >= mp->lowmsg && j <= mp->hghmsg; j += convdir) {
476
477 if (does_exist (mp, j)
478 && inverted ? !in_sequence (mp, i, j) : in_sequence (mp, i, j)) {
479 found++;
480 /* we want all that we find, or just the last in the +/_ case */
481 if (!just_one || found >= count) {
482 if (!is_selected (mp, j)) {
483 set_selected (mp, j);
484 mp->numsel++;
485 if (mp->lowsel == 0 || j < mp->lowsel)
486 mp->lowsel = j;
487 if (j > mp->hghsel)
488 mp->hghsel = j;
489 }
490 }
491 /*
492 * If we have any sort of limit, then break out
493 * once we've found enough.
494 */
495 if (count && found >= count)
496 break;
497
498 }
499 }
500
501 if (mp->numsel > 0)
502 return mp->numsel;
503
504 if (first || just_one)
505 return BADMSG;
506 inform("sequence %s %s", cp, inverted ? "full" : "empty");
507 return -1;
508 }