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.
12 * error codes for sequence
13 * and message range processing
24 #define getnew(mp) (mp->hghmsg + 1)
26 static int convdir; /* convert direction */
32 static int m_conv (struct msgs *, char *, int);
33 static int attr (struct msgs *, char *);
37 m_convert (struct msgs *mp, char *name)
39 int first, last, found, range, err;
43 /* check if user defined sequence */
44 err = attr (mp, cp = name);
53 * else err == 0, so continue
59 * Check for special "new" sequence, which
60 * is valid only if ALLOW_NEW is set.
62 if ((mp->msgflags & ALLOW_NEW) && !strcmp (cp, "new")) {
63 if ((err = first = getnew (mp)) <= 0)
69 if (!strcmp (cp, "all"))
72 if ((err = first = m_conv (mp, cp, FIRST)) <= 0)
76 if (*cp != '\0' && *cp != '-' && *cp != ':') {
78 advise (NULL, "illegal argument delimiter: `%c'(0%o)", *delimp, *delimp);
84 if ((err = last = m_conv (mp, cp, LAST)) <= 0) {
88 advise (NULL, "no %s message", cp);
92 advise (NULL, "message %s doesn't exist", cp);
96 advise (NULL, "message %s out of range 1-%d", cp, mp->hghmsg);
101 advise (NULL, "bad message list %s", name);
105 advise (NULL, "folder full, no %s message", name);
109 advise (NULL, "no messages match specification");
118 if (first > mp->hghmsg || last < mp->lowmsg) {
120 advise (NULL, "no messages in range %s", name);
124 /* tighten the range to search */
125 if (last > mp->hghmsg)
127 if (first < mp->lowmsg)
130 } else if (*cp == ':') {
135 } else if (*cp == '+') {
139 if ((range = atoi (bp = cp)) == 0)
141 while (isdigit (*bp))
145 if ((convdir > 0 && first > mp->hghmsg)
146 || (convdir < 0 && first < mp->lowmsg))
149 /* tighten the range to search */
150 if (first < mp->lowmsg)
152 if (first > mp->hghmsg)
155 for (last = first; last >= mp->lowmsg && last <= mp->hghmsg;
157 if (does_exist (mp, last))
160 if (last < mp->lowmsg)
162 if (last > mp->hghmsg)
175 * If ALLOW_NEW is set, then allow selecting of an
176 * empty slot. If ALLOW_NEW is not set, then we
177 * check if message is in-range and exists.
179 if (mp->msgflags & ALLOW_NEW) {
180 set_select_empty (mp, first);
182 if (first > mp->hghmsg
183 || first < mp->lowmsg
184 || !(does_exist (mp, first))) {
185 if (!strcmp (name, "cur") || !strcmp (name, "."))
186 advise (NULL, "no %s message", name);
188 advise (NULL, "message %d doesn't exist", first);
192 last = first; /* range of 1 */
196 * Cycle through the range and select the messages
197 * that exist. If ALLOW_NEW is set, then we also check
198 * if we are selecting an empty slot.
200 for (; first <= last; first++) {
201 if (does_exist (mp, first) ||
202 ((mp->msgflags & ALLOW_NEW) && is_select_empty (mp, first))) {
203 if (!is_selected (mp, first)) {
204 set_selected (mp, first);
206 if (mp->lowsel == 0 || first < mp->lowsel)
208 if (first > mp->hghsel)
222 * Convert the various message names to
223 * their numeric values.
235 m_conv (struct msgs *mp, char *str, int call)
238 register unsigned char *cp, *bp;
239 unsigned char buf[16];
244 while (isdigit (*bp))
251 else if (*delimp || call == LAST)
252 return mp->hghmsg + 1;
253 else if (mp->msgflags & ALLOW_NEW)
260 /* doesn't enforce lower case */
261 for (bp = buf; (isalpha(*cp) || *cp == '.')
262 && (bp - buf < sizeof(buf) - 1); )
264 for (bp = buf; ((*cp >= 'a' && *cp <= 'z') || *cp == '.')
265 && (bp - buf < sizeof(buf) - 1); )
273 if (!strcmp (buf, "first"))
274 return (mp->hghmsg || !(mp->msgflags & ALLOW_NEW)
275 ? mp->lowmsg : BADMSG);
277 if (!strcmp (buf, "last")) {
279 return (mp->hghmsg || !(mp->msgflags & ALLOW_NEW) ? mp->hghmsg : BADMSG);
282 if (!strcmp (buf, "cur") || !strcmp (buf, "."))
283 return (mp->curmsg > 0 ? mp->curmsg : BADMSG);
285 if (!strcmp (buf, "prev")) {
287 for (i = (mp->curmsg <= mp->hghmsg) ? mp->curmsg - 1 : mp->hghmsg;
288 i >= mp->lowmsg; i--) {
289 if (does_exist (mp, i))
295 if (!strcmp (buf, "next")) {
296 for (i = (mp->curmsg >= mp->lowmsg) ? mp->curmsg + 1 : mp->lowmsg;
297 i <= mp->hghmsg; i++) {
298 if (does_exist (mp, i))
308 * Handle user defined sequences.
309 * They can take the following forms:
322 attr (struct msgs *mp, char *cp)
324 register unsigned char *dp;
329 int range = 0; /* no range */
332 /* hack for "cur-name", "cur-n", etc. */
333 if (!strcmp (cp, "cur"))
335 if (ssequal ("cur:", cp)) /* this code need to be rewritten... */
338 /* Check for sequence negation */
339 if ((dp = context_find (nsequence)) && *dp != '\0' && ssequal (dp, cp)) {
344 convdir = 1; /* convert direction */
346 for (dp = cp; *dp && isalnum(*dp); dp++)
360 if (!strcmp (dp, "prev")) {
362 first = (mp->curmsg > 0) && (mp->curmsg <= mp->hghmsg)
363 ? mp->curmsg - 1 : mp->hghmsg;
364 } else if (!strcmp (dp, "next")) {
366 first = (mp->curmsg >= mp->lowmsg)
367 ? mp->curmsg + 1 : mp->lowmsg;
368 } else if (!strcmp (dp, "first")) {
370 } else if (!strcmp (dp, "last")) {
382 else if (*dp == '-') {
386 if ((range = atoi(dp)) == 0)
388 while (isdigit (*dp))
394 *bp = '\0'; /* temporarily terminate sequence name */
397 i = seq_getnum (mp, cp); /* get index of sequence */
400 *bp = ':'; /* restore sequence name */
404 found = 0; /* count the number we select for this argument */
406 for (j = first ? first : (convdir > 0) ? mp->lowmsg : mp->hghmsg;
407 j >= mp->lowmsg && j <= mp->hghmsg; j += convdir) {
408 if (does_exist (mp, j)
409 && inverted ? !in_sequence (mp, i, j) : in_sequence (mp, i, j)) {
410 if (!is_selected (mp, j)) {
411 set_selected (mp, j);
413 if (mp->lowsel == 0 || j < mp->lowsel)
421 * If we have a range, then break out
422 * once we've found enough.
424 if (range && found >= range)
434 advise (NULL, "sequence %s %s", cp, inverted ? "full" : "empty");