]> diplodocus.org Git - nmh/blob - sbr/m_convert.c
pending-release-notes: add mhshow's "-prefer", and mh-format's %(kibi/kilo)
[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
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, is_range, 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 else if (err < 0)
50 goto badmsg;
51 else if (err > 0)
52 return 1;
53 /*
54 * else err == 0, so continue
55 */
56
57 found = 0;
58 is_range = 1;
59
60 /*
61 * Check for special "new" sequence, which
62 * is valid only if ALLOW_NEW is set.
63 */
64 if ((mp->msgflags & ALLOW_NEW) && !strcmp (cp, "new")) {
65 if ((err = first = getnew (mp)) <= 0)
66 goto badmsg;
67 else
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 else if (*delimp || call == LAST)
282 return mp->hghmsg + 1;
283 else if (mp->msgflags & ALLOW_NEW)
284 return BADRNG;
285 else
286 return BADNUM;
287 }
288
289 /* doesn't enforce lower case */
290 for (bp = buf; (isalpha((unsigned char) *cp) || *cp == '.')
291 && (bp - buf < (int) sizeof(buf) - 1); )
292 {
293 *bp++ = *cp++;
294 }
295 *bp++ = '\0';
296 delimp = cp;
297
298 if (!strcmp (buf, "first"))
299 return (mp->hghmsg || !(mp->msgflags & ALLOW_NEW)
300 ? mp->lowmsg : BADMSG);
301
302 if (!strcmp (buf, "last")) {
303 convdir = -1;
304 return (mp->hghmsg || !(mp->msgflags & ALLOW_NEW) ? mp->hghmsg : BADMSG);
305 }
306
307 if (!strcmp (buf, "cur") || !strcmp (buf, "."))
308 return (mp->curmsg > 0 ? mp->curmsg : BADMSG);
309
310 if (!strcmp (buf, "prev")) {
311 convdir = -1;
312 for (i = (mp->curmsg <= mp->hghmsg) ? mp->curmsg - 1 : mp->hghmsg;
313 i >= mp->lowmsg; i--) {
314 if (does_exist (mp, i))
315 return i;
316 }
317 return BADMSG;
318 }
319
320 if (!strcmp (buf, "next")) {
321 for (i = (mp->curmsg >= mp->lowmsg) ? mp->curmsg + 1 : mp->lowmsg;
322 i <= mp->hghmsg; i++) {
323 if (does_exist (mp, i))
324 return i;
325 }
326 return BADMSG;
327 }
328
329 return BADLST;
330 }
331
332 /*
333 * Handle user defined sequences.
334 * They can take the following forms:
335 *
336 * seq
337 * seq:prev
338 * seq:next
339 * seq:first
340 * seq:last
341 * seq:+n
342 * seq:-n
343 * seq:n
344 */
345
346 static int
347 attr (struct msgs *mp, char *cp)
348 {
349 char *dp;
350 char *bp = NULL;
351 char *ep;
352 char op;
353 int i, j;
354 int found,
355 inverted = 0,
356 count = 0, /* range given? else use entire sequence */
357 just_one = 0, /* want entire sequence or range */
358 first = 0,
359 start = 0;
360
361 /* hack for "cur-name", "cur-n", etc. */
362 if (!strcmp (cp, "cur"))
363 return 0;
364 if (strncmp ("cur", cp, 3) == 0) {
365 if (cp[3] == ':' || cp[3] == '=')
366 return 0;
367 }
368
369 /* Check for sequence negation */
370 if ((dp = context_find (nsequence)) && *dp != '\0' && ssequal (dp, cp)) {
371 inverted = 1;
372 cp += strlen (dp);
373 }
374
375 convdir = 1; /* convert direction */
376
377 for (dp = cp; *dp && isalnum((unsigned char) *dp); dp++)
378 continue;
379
380 if (*dp == ':') {
381 bp = dp++;
382 count = 1;
383
384 /*
385 * seq:prev (or)
386 * seq:next (or)
387 * seq:first (or)
388 * seq:last
389 */
390 if (isalpha ((unsigned char) *dp)) {
391 if (!strcmp (dp, "prev")) {
392 convdir = -1;
393 first = (mp->curmsg > 0) && (mp->curmsg <= mp->hghmsg)
394 ? mp->curmsg - 1
395 : mp->hghmsg;
396 start = first;
397 }
398 else if (!strcmp (dp, "next")) {
399 convdir = 1;
400 first = (mp->curmsg >= mp->lowmsg)
401 ? mp->curmsg + 1
402 : mp->lowmsg;
403 start = first;
404 }
405 else if (!strcmp (dp, "first")) {
406 convdir = 1;
407 start = mp->lowmsg;
408 }
409 else if (!strcmp (dp, "last")) {
410 convdir = -1;
411 start = mp->hghmsg;
412 }
413 else {
414 return BADLST;
415 }
416 } else {
417 /*
418 * seq:n (or)
419 * seq:+n (or)
420 * seq:-n
421 */
422 if (*dp == '+') {
423 /* foo:+3 is same as foo:3 */
424 dp++;
425 convdir = 1;
426 start = mp->lowmsg;
427 } else if (*dp == '-' || *dp == ':') {
428 /* foo:-3 or foo::3 */
429 dp++;
430 convdir = -1;
431 start = mp->hghmsg;
432 }
433 count = strtol(dp,&ep,10);
434 if (count == 0 || *ep) /* 0 illegal */
435 return BADLST;
436 }
437
438 op = *bp;
439 *bp = '\0'; /* temporarily terminate sequence name */
440 } else if (*dp == '=') {
441
442 bp = dp++;
443
444 if (*dp == '+') {
445 /* foo=+3 is same as foo=3 */
446 dp++;
447 convdir = 1;
448 start = mp->lowmsg;
449 } else if (*dp == '-') {
450 /* foo=-3 */
451 dp++;
452 convdir = -1;
453 start = mp->hghmsg;
454 }
455
456 count = strtol(dp,&ep,10); /* 0 illegal */
457 if (count == 0 || *ep)
458 return BADLST;
459
460 just_one = 1;
461
462 op = *bp;
463 *bp = '\0'; /* temporarily terminate sequence name */
464 }
465
466 i = seq_getnum (mp, cp); /* get index of sequence */
467
468 if (bp)
469 *bp = op; /* restore sequence name */
470 if (i == -1)
471 return 0;
472
473 found = 0; /* count the number we select for this argument */
474
475 if (!start)
476 start = mp->lowmsg;
477
478 for (j = start; j >= mp->lowmsg && j <= mp->hghmsg; j += convdir) {
479
480 if (does_exist (mp, j)
481 && inverted ? !in_sequence (mp, i, j) : in_sequence (mp, i, j)) {
482 found++;
483 /* we want all that we find, or just the last in the +/_ case */
484 if (!just_one || found >= count) {
485 if (!is_selected (mp, j)) {
486 set_selected (mp, j);
487 mp->numsel++;
488 if (mp->lowsel == 0 || j < mp->lowsel)
489 mp->lowsel = j;
490 if (j > mp->hghsel)
491 mp->hghsel = j;
492 }
493 }
494 /*
495 * If we have any sort of limit, then break out
496 * once we've found enough.
497 */
498 if (count && found >= count)
499 break;
500
501 }
502 }
503
504 if (mp->numsel > 0)
505 return mp->numsel;
506
507 if (first || just_one)
508 return BADMSG;
509 advise (NULL, "sequence %s %s", cp, inverted ? "full" : "empty");
510 return -1;
511 }