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? */
16 ** error codes for sequence
17 ** and message range processing
28 #define getbeyond(mp) ((mp)->hghmsg + 1)
30 static int convdir; /* convert direction */
31 static char *delimp; /* delimiter pointer */
36 static int m_conv(struct msgs *, char *, int);
37 static int attr(struct msgs *, char *);
41 addtosel(struct msgs *mp, int msg)
43 if (is_selected(mp, msg)) {
44 return; /* dont select twice */
46 set_selected(mp, msg);
50 m_convert(struct msgs *mp, char *name)
52 int first, last, found, range, err;
56 /* check if user defined sequence */
57 err = attr(mp, cp = name);
63 /* it had been a user-defined sequence: we're finished */
67 ** else err == 0, so continue
68 ** here we know: it is no user-defined seq
72 ** Handle the special beyond sequence, which is valid only if
73 ** ALLOW_BEYOND is set, and can appear only on its own.
74 ** Also, it is available in any folder.
76 if ((mp->msgflags & ALLOW_BEYOND) && strcmp(cp, seq_beyond)==0) {
77 addtosel(mp, getbeyond(mp));
82 ** Handle the special all sequence: replace `a' with `f-l'
84 if (strcmp(cp, seq_all)==0) {
85 cp = concat(seq_first, "-", seq_last, NULL);
88 if ((err = first = m_conv(mp, cp, FIRST)) <= 0)
96 if ((err = last = m_conv(mp, cp, LAST)) <= 0) {
100 advise(NULL, "no %s message", cp);
104 advise(NULL, "message %s doesn't exist", cp);
108 advise(NULL, "message %s out of range 1-%d",
114 advise(NULL, "bad message list %s", name);
118 advise(NULL, "folder full, no %s message",
123 advise(NULL, "no messages match specification");
132 if (first > mp->hghmsg || last < mp->lowmsg) {
134 advise(NULL, "no messages in range %s", name);
138 /* tighten the range to search */
139 if (last > mp->hghmsg)
141 if (first < mp->lowmsg)
151 } else if (*cp == '+') {
155 if ((range = atoi(bp = cp)) == 0)
161 if ((convdir > 0 && first > mp->hghmsg)
162 || (convdir < 0 && first < mp->lowmsg))
165 /* tighten the range to search */
166 if (first < mp->lowmsg)
168 if (first > mp->hghmsg)
171 for (last = first; last >= mp->lowmsg && last <= mp->hghmsg;
173 if (does_exist(mp, last) && (--range <= 0))
175 if (last < mp->lowmsg)
177 if (last > mp->hghmsg)
189 ** AFAICS, the only cases to reach here are:
191 ** But I'm not sure. --meillo
196 ** Check if message is in-range and exists.
198 if (first > mp->hghmsg || first < mp->lowmsg ||
199 !does_exist(mp, first)) {
200 if (strcmp(name, seq_cur)==0)
201 advise(NULL, "no current message");
203 /* this case seems to never be reached */
204 advise(NULL, "message %d doesn't exist",
208 last = first; /* range of 1 */
213 advise(NULL, "illegal argument delimiter: `%c'(0%o)",
219 /* Cycle through the range and select the messages that exist. */
220 for (found=0; first <= last; first++) {
221 if (does_exist(mp, first)) {
233 ** Convert the various message names to their numeric values.
243 m_conv(struct msgs *mp, char *str, int call)
246 unsigned char *cp, *bp;
247 unsigned char buf[16]; /* for reserved sequence name */
252 /* Handle an integer */
261 else if (*delimp || call == LAST)
262 return mp->hghmsg + 1;
263 else if (mp->msgflags & ALLOW_BEYOND)
269 for (bp = buf; isalpha(*cp) && (bp - buf < (int)sizeof(buf) - 1); ) {
275 /* Which one of the reserved names is it? */
276 if (strcmp(buf, seq_first)==0) {
277 return (mp->hghmsg || !(mp->msgflags & ALLOW_BEYOND) ?
278 mp->lowmsg : BADMSG);
280 } else if (strcmp(buf, seq_last)==0) {
282 return (mp->hghmsg || !(mp->msgflags & ALLOW_BEYOND) ?
283 mp->hghmsg : BADMSG);
285 } else if (strcmp(buf, seq_cur)==0) {
286 return (mp->curmsg > 0 ? mp->curmsg : BADMSG);
288 } else if (strcmp(buf, seq_prev)==0) {
290 for (i = (mp->curmsg <= mp->hghmsg) ?
291 mp->curmsg - 1 : mp->hghmsg;
292 i >= mp->lowmsg; i--) {
293 if (does_exist(mp, i))
298 } else if (strcmp(buf, seq_next)==0) {
299 for (i = (mp->curmsg >= mp->lowmsg) ?
300 mp->curmsg + 1 : mp->lowmsg;
301 i <= mp->hghmsg; i++) {
302 if (does_exist(mp, i))
313 ** Handle user defined sequences.
314 ** They can take the following forms:
325 ** (`42' being an arbitrary integer)
328 attr(struct msgs *mp, char *cp)
335 int range = 0; /* no range */
338 /* hack for "c-..." */
339 if (strcmp(cp, seq_cur)==0)
341 /* "c:..." -- this code need to be rewritten... */
342 if (strncmp(seq_cur, cp, strlen(seq_cur))==0 &&
343 cp[strlen(seq_cur)] == ':') {
347 /* Check for sequence negation */
348 if (!(dp = context_find(nsequence))) {
349 dp = seq_neg; /* use default */
351 if (*dp && strncmp(cp, dp, strlen(dp))==0) {
356 convdir = 1; /* convert direction */
358 for (dp = cp; *dp && isalnum(*dp); dp++)
372 if (strcmp(dp, seq_prev)==0) {
374 first = (mp->curmsg > 0) && (mp->curmsg <= mp->hghmsg)
375 ? mp->curmsg - 1 : mp->hghmsg;
376 } else if (strcmp(dp, seq_next)==0) {
378 first = (mp->curmsg >= mp->lowmsg)
379 ? mp->curmsg + 1 : mp->lowmsg;
380 } else if (strcmp(dp, seq_first)==0) {
382 } else if (strcmp(dp, seq_last)==0) {
394 else if (*dp == '-') {
398 if ((range = atoi(dp)) == 0)
406 *bp = '\0'; /* temporarily terminate sequence name */
409 i = seq_getnum(mp, cp); /* get index of sequence */
412 *bp = ':'; /* restore sequence name */
416 found = 0; /* count the number we select for this argument */
418 for (j = first ? first : (convdir > 0) ? mp->lowmsg : mp->hghmsg;
419 j >= mp->lowmsg && j <= mp->hghmsg; j += convdir) {
420 if (does_exist(mp, j)
421 && inverted ? !in_sequence(mp, i, j) :
422 in_sequence(mp, i, j)) {
427 ** If we have a range, then break out
428 ** once we've found enough.
430 if (range && found >= range)
440 advise(NULL, "sequence %s %s", cp, inverted ? "full" : "empty");