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