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