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)==0) {
83 addtosel(mp, getbeyond(mp));
88 ** Handle the special all sequence: replace `a' with `f-l'
90 if (strcmp(cp, seq_all)==0) {
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)==0)
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]; /* for reserved sequence name */
258 /* Handle an integer */
267 else if (*delimp || call == LAST)
268 return mp->hghmsg + 1;
269 else if (mp->msgflags & ALLOW_BEYOND)
275 for (bp = buf; isalpha(*cp) && (bp - buf < sizeof(buf) - 1); ) {
281 /* Which one of the reserved names is it? */
282 if (strcmp(buf, seq_first)==0) {
283 return (mp->hghmsg || !(mp->msgflags & ALLOW_BEYOND) ?
284 mp->lowmsg : BADMSG);
286 } else if (strcmp(buf, seq_last)==0) {
288 return (mp->hghmsg || !(mp->msgflags & ALLOW_BEYOND) ?
289 mp->hghmsg : BADMSG);
291 } else if (strcmp(buf, seq_cur)==0) {
292 return (mp->curmsg > 0 ? mp->curmsg : BADMSG);
294 } else if (strcmp(buf, seq_prev)==0) {
296 for (i = (mp->curmsg <= mp->hghmsg) ?
297 mp->curmsg - 1 : mp->hghmsg;
298 i >= mp->lowmsg; i--) {
299 if (does_exist(mp, i))
304 } else if (strcmp(buf, seq_next)==0) {
305 for (i = (mp->curmsg >= mp->lowmsg) ?
306 mp->curmsg + 1 : mp->lowmsg;
307 i <= mp->hghmsg; i++) {
308 if (does_exist(mp, i))
319 ** Handle user defined sequences.
320 ** They can take the following forms:
331 ** (`42' being an arbitrary integer)
334 attr(struct msgs *mp, char *cp)
336 register unsigned char *dp;
341 int range = 0; /* no range */
344 /* hack for "c-..." */
345 if (strcmp(cp, seq_cur)==0)
347 /* "c:..." -- this code need to be rewritten... */
348 if (strncmp(seq_cur, cp, strlen(seq_cur))==0 &&
349 cp[strlen(seq_cur)] == ':') {
353 /* Check for sequence negation */
354 if (!(dp = context_find(nsequence))) {
355 dp = seq_neg; /* use default */
357 if (*dp && strncmp(cp, dp, strlen(dp))==0) {
362 convdir = 1; /* convert direction */
364 for (dp = cp; *dp && isalnum(*dp); dp++)
378 if (strcmp(dp, seq_prev)==0) {
380 first = (mp->curmsg > 0) && (mp->curmsg <= mp->hghmsg)
381 ? mp->curmsg - 1 : mp->hghmsg;
382 } else if (strcmp(dp, seq_next)==0) {
384 first = (mp->curmsg >= mp->lowmsg)
385 ? mp->curmsg + 1 : mp->lowmsg;
386 } else if (strcmp(dp, seq_first)==0) {
388 } else if (strcmp(dp, seq_last)==0) {
400 else if (*dp == '-') {
404 if ((range = atoi(dp)) == 0)
412 *bp = '\0'; /* temporarily terminate sequence name */
415 i = seq_getnum(mp, cp); /* get index of sequence */
418 *bp = ':'; /* restore sequence name */
422 found = 0; /* count the number we select for this argument */
424 for (j = first ? first : (convdir > 0) ? mp->lowmsg : mp->hghmsg;
425 j >= mp->lowmsg && j <= mp->hghmsg; j += convdir) {
426 if (does_exist(mp, j)
427 && inverted ? !in_sequence(mp, i, j) :
428 in_sequence(mp, i, j)) {
433 ** If we have a range, then break out
434 ** once we've found enough.
436 if (range && found >= range)
446 advise(NULL, "sequence %s %s", cp, inverted ? "full" : "empty");