2 ** m_convert.c -- parse a message range or sequence and set SELECTED
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
10 /* FIXME: This code needs rework! Rewrite as a parser? */
15 ** error codes for sequence
16 ** and message range processing
27 #define getbeyond(mp) ((mp)->hghmsg + 1)
29 static int convdir; /* convert direction */
30 static char *delimp; /* delimiter pointer */
35 static int m_conv(struct msgs *, char *, int);
36 static int attr(struct msgs *, char *);
40 addtosel(struct msgs *mp, int msg)
42 if (is_selected(mp, msg)) {
43 return; /* dont select twice */
45 set_selected(mp, msg);
47 if (mp->lowsel == 0 || msg < mp->lowsel) {
50 if (msg > mp->hghsel) {
56 m_convert(struct msgs *mp, char *name)
58 int first, last, found, range, err;
62 /* check if user defined sequence */
63 err = attr(mp, cp = name);
69 /* it had been a user-defined sequence: we're finished */
73 ** else err == 0, so continue
74 ** here we know: it is no user-defined seq
78 ** Handle the special beyond sequence, which is valid only if
79 ** ALLOW_BEYOND is set, and can appear only on its own.
80 ** Also, it is available in any folder.
82 if ((mp->msgflags & ALLOW_BEYOND) && !strcmp(cp, seq_beyond)) {
83 addtosel(mp, getbeyond(mp));
88 ** Handle the special all sequence: replace `a' with `f-l'
90 if (!strcmp(cp, seq_all)) {
91 cp = concat(seq_first, "-", seq_last, NULL);
94 if ((err = first = m_conv(mp, cp, FIRST)) <= 0)
102 if ((err = last = m_conv(mp, cp, LAST)) <= 0) {
106 advise(NULL, "no %s message", cp);
110 advise(NULL, "message %s doesn't exist", cp);
114 advise(NULL, "message %s out of range 1-%d",
120 advise(NULL, "bad message list %s", name);
124 advise(NULL, "folder full, no %s message",
129 advise(NULL, "no messages match specification");
138 if (first > mp->hghmsg || last < mp->lowmsg) {
140 advise(NULL, "no messages in range %s", name);
144 /* tighten the range to search */
145 if (last > mp->hghmsg)
147 if (first < mp->lowmsg)
157 } else if (*cp == '+') {
161 if ((range = atoi(bp = cp)) == 0)
167 if ((convdir > 0 && first > mp->hghmsg)
168 || (convdir < 0 && first < mp->lowmsg))
171 /* tighten the range to search */
172 if (first < mp->lowmsg)
174 if (first > mp->hghmsg)
177 for (last = first; last >= mp->lowmsg && last <= mp->hghmsg;
179 if (does_exist(mp, last) && (--range <= 0))
181 if (last < mp->lowmsg)
183 if (last > mp->hghmsg)
195 ** AFAICS, the only cases to reach here are:
197 ** But I'm not sure. --meillo
202 ** Check if message is in-range and exists.
204 if (first > mp->hghmsg || first < mp->lowmsg ||
205 !does_exist(mp, first)) {
206 if (!strcmp(name, seq_cur))
207 advise(NULL, "no current message");
209 /* this case seems to never be reached */
210 advise(NULL, "message %d doesn't exist",
214 last = first; /* range of 1 */
219 advise(NULL, "illegal argument delimiter: `%c'(0%o)",
225 /* Cycle through the range and select the messages that exist. */
226 for (found=0; first <= last; first++) {
227 if (does_exist(mp, first)) {
239 ** Convert the various message names to their numeric values.
249 m_conv(struct msgs *mp, char *str, int call)
252 register unsigned char *cp, *bp;
253 unsigned char buf[16];
265 else if (*delimp || call == LAST)
266 return mp->hghmsg + 1;
267 else if (mp->msgflags & ALLOW_BEYOND)
274 /* doesn't enforce lower case */
275 for (bp = buf; isalpha(*cp) && (bp - buf < sizeof(buf) - 1); )
277 for (bp = buf; islower(*cp) && (bp - buf < sizeof(buf) - 1); )
285 if (!strcmp(buf, seq_first))
286 return (mp->hghmsg || !(mp->msgflags & ALLOW_BEYOND) ?
287 mp->lowmsg : BADMSG);
289 if (!strcmp(buf, seq_last)) {
291 return (mp->hghmsg || !(mp->msgflags & ALLOW_BEYOND) ?
292 mp->hghmsg : BADMSG);
295 if (!strcmp(buf, seq_cur))
296 return (mp->curmsg > 0 ? mp->curmsg : BADMSG);
298 if (!strcmp(buf, seq_prev)) {
300 for (i = (mp->curmsg <= mp->hghmsg) ? mp->curmsg - 1 : mp->hghmsg;
301 i >= mp->lowmsg; i--) {
302 if (does_exist(mp, i))
308 if (!strcmp(buf, seq_next)) {
309 for (i = (mp->curmsg >= mp->lowmsg) ? mp->curmsg + 1 : mp->lowmsg;
310 i <= mp->hghmsg; i++) {
311 if (does_exist(mp, i))
321 ** Handle user defined sequences.
322 ** They can take the following forms:
333 ** (`42' being an arbitrary integer)
336 attr(struct msgs *mp, char *cp)
338 register unsigned char *dp;
343 int range = 0; /* no range */
346 /* hack for "c-..." */
347 if (!strcmp(cp, seq_cur))
349 /* "c:..." -- this code need to be rewritten... */
350 if (strncmp(seq_cur, cp, strlen(seq_cur))==0 &&
351 cp[strlen(seq_cur)] == ':') {
355 /* Check for sequence negation */
356 if (!(dp = context_find(nsequence))) {
357 dp = seq_neg; /* use default */
359 if (dp && *dp && isprefix(dp, cp)) {
364 convdir = 1; /* convert direction */
366 for (dp = cp; *dp && isalnum(*dp); dp++)
380 if (!strcmp(dp, seq_prev)) {
382 first = (mp->curmsg > 0) && (mp->curmsg <= mp->hghmsg)
383 ? mp->curmsg - 1 : mp->hghmsg;
384 } else if (!strcmp(dp, seq_next)) {
386 first = (mp->curmsg >= mp->lowmsg)
387 ? mp->curmsg + 1 : mp->lowmsg;
388 } else if (!strcmp(dp, seq_first)) {
390 } else if (!strcmp(dp, seq_last)) {
402 else if (*dp == '-') {
406 if ((range = atoi(dp)) == 0)
414 *bp = '\0'; /* temporarily terminate sequence name */
417 i = seq_getnum(mp, cp); /* get index of sequence */
420 *bp = ':'; /* restore sequence name */
424 found = 0; /* count the number we select for this argument */
426 for (j = first ? first : (convdir > 0) ? mp->lowmsg : mp->hghmsg;
427 j >= mp->lowmsg && j <= mp->hghmsg; j += convdir) {
428 if (does_exist(mp, j)
429 && inverted ? !in_sequence(mp, i, j) :
430 in_sequence(mp, i, j)) {
435 ** If we have a range, then break out
436 ** once we've found enough.
438 if (range && found >= range)
448 advise(NULL, "sequence %s %s", cp, inverted ? "full" : "empty");