]>
diplodocus.org Git - nmh/blob - sbr/m_convert.c
1 /* m_convert.c -- parse a message range or sequence and set SELECTED
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
12 * error codes for sequence
13 * and message range processing
25 #define getnew(mp) (mp->hghmsg + 1)
27 static int convdir
; /* convert direction */
33 static int m_conv (struct msgs
*, char *, int);
34 static int attr (struct msgs
*, char *);
38 m_convert (struct msgs
*mp
, char *name
)
40 int first
, last
, found
, count
, is_range
, err
;
43 /* check if user defined sequence */
44 err
= attr (mp
, cp
= name
);
53 * else err == 0, so continue
60 * Check for special "new" sequence, which
61 * is valid only if ALLOW_NEW is set.
63 if ((mp
->msgflags
& ALLOW_NEW
) && !strcmp (cp
, "new")) {
64 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
!= ':' && *cp
!= '=') {
78 inform("illegal argument delimiter: `%c'(0%o)", *delimp
, *delimp
);
84 if ((err
= last
= m_conv (mp
, cp
, LAST
)) <= 0) {
88 inform("no %s message", cp
);
92 inform("message %s doesn't exist", cp
);
96 inform("message %s out of range 1-%d", cp
, mp
->hghmsg
);
101 inform("bad message list %s", name
);
105 inform("folder full, no %s message", name
);
109 inform("no messages match specification");
118 if (first
> mp
->hghmsg
|| last
< mp
->lowmsg
) {
120 inform("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
== ':' || *cp
== '=') {
138 /* foo:-3 or foo=-3 */
141 } else if (*cp
== '+') {
142 /* foo:+3 or foo=+3 is same as foo:3 or foo=3 */
146 if ((count
= atoi (bp
= cp
)) == 0)
148 while (isdigit ((unsigned char) *bp
))
152 if ((convdir
> 0 && first
> mp
->hghmsg
)
153 || (convdir
< 0 && first
< mp
->lowmsg
))
156 /* tighten the range to search */
157 if (first
< mp
->lowmsg
)
159 if (first
> mp
->hghmsg
)
163 last
>= mp
->lowmsg
&& last
<= mp
->hghmsg
;
165 if (does_exist (mp
, last
))
168 if (is_range
) { /* a range includes any messages that exist */
169 if (last
< mp
->lowmsg
)
171 if (last
> mp
->hghmsg
)
178 } else { /* looking for the nth message. if not enough, fail. */
179 if (last
< mp
->lowmsg
|| last
> mp
->hghmsg
) {
180 inform("no such message");
191 * If ALLOW_NEW is set, then allow selecting of an
192 * empty slot. If ALLOW_NEW is not set, then we
193 * check if message is in-range and exists.
195 if (mp
->msgflags
& ALLOW_NEW
) {
197 * We can get into a case where the "cur" sequence is way out
198 * of range, and because it's allowed to not exist (think
199 * of "rmm; next") it doesn't get checked to make sure it's
200 * within the range of messages in seq_init(). So if our
201 * desired sequence is out of range of the allocated folder
202 * limits simply reallocate the folder so it's within range.
204 if (first
< mp
->lowoff
|| first
> mp
->hghoff
)
205 mp
= folder_realloc(mp
, min(first
, mp
->lowoff
),
206 max(first
, mp
->hghoff
));
208 set_select_empty (mp
, first
);
210 if (first
> mp
->hghmsg
211 || first
< mp
->lowmsg
212 || !(does_exist (mp
, first
))) {
213 if (!strcmp (name
, "cur") || !strcmp (name
, "."))
214 inform("no %s message", name
);
216 inform("message %d doesn't exist", first
);
220 last
= first
; /* range of 1 */
224 * Cycle through the range and select the messages
225 * that exist. If ALLOW_NEW is set, then we also check
226 * if we are selecting an empty slot.
228 for (; first
<= last
; first
++) {
229 if (does_exist (mp
, first
) ||
230 ((mp
->msgflags
& ALLOW_NEW
) && is_select_empty (mp
, first
))) {
231 if (!is_selected (mp
, first
)) {
232 set_selected (mp
, first
);
234 if (mp
->lowsel
== 0 || first
< mp
->lowsel
)
236 if (first
> mp
->hghsel
)
250 * Convert the various message names to
251 * their numeric values.
263 m_conv (struct msgs
*mp
, char *str
, int call
)
271 if (isdigit ((unsigned char) *cp
)) {
272 while (isdigit ((unsigned char) *bp
))
279 if (*delimp
|| call
== LAST
)
280 return mp
->hghmsg
+ 1;
281 if (mp
->msgflags
& ALLOW_NEW
)
286 /* doesn't enforce lower case */
287 for (bp
= buf
; (isalpha((unsigned char) *cp
) || *cp
== '.')
288 && (bp
- buf
< (int) sizeof(buf
) - 1); )
295 if (!strcmp (buf
, "first"))
296 return mp
->hghmsg
|| !(mp
->msgflags
& ALLOW_NEW
) ?
299 if (!strcmp (buf
, "last")) {
301 return mp
->hghmsg
|| !(mp
->msgflags
& ALLOW_NEW
) ? mp
->hghmsg
: BADMSG
;
304 if (!strcmp (buf
, "cur") || !strcmp (buf
, "."))
305 return mp
->curmsg
> 0 ? mp
->curmsg
: BADMSG
;
307 if (!strcmp (buf
, "prev")) {
309 for (i
= (mp
->curmsg
<= mp
->hghmsg
) ? mp
->curmsg
- 1 : mp
->hghmsg
;
310 i
>= mp
->lowmsg
; i
--) {
311 if (does_exist (mp
, i
))
317 if (!strcmp (buf
, "next")) {
318 for (i
= (mp
->curmsg
>= mp
->lowmsg
) ? mp
->curmsg
+ 1 : mp
->lowmsg
;
319 i
<= mp
->hghmsg
; i
++) {
320 if (does_exist (mp
, i
))
330 * Handle user defined sequences.
331 * They can take the following forms:
344 attr (struct msgs
*mp
, char *cp
)
353 count
= 0, /* range given? else use entire sequence */
354 just_one
= 0, /* want entire sequence or range */
358 /* hack for "cur-name", "cur-n", etc. */
359 if (!strcmp (cp
, "cur"))
361 if (has_prefix(cp
, "cur")) {
362 if (cp
[3] == ':' || cp
[3] == '=')
366 /* Check for sequence negation */
367 if ((dp
= context_find (nsequence
)) && *dp
!= '\0' && ssequal (dp
, cp
)) {
372 convdir
= 1; /* convert direction */
374 for (dp
= cp
; isalnum((unsigned char)*dp
); dp
++)
387 if (isalpha ((unsigned char) *dp
)) {
388 if (!strcmp (dp
, "prev")) {
390 first
= (mp
->curmsg
> 0) && (mp
->curmsg
<= mp
->hghmsg
)
395 else if (!strcmp (dp
, "next")) {
397 first
= (mp
->curmsg
>= mp
->lowmsg
)
402 else if (!strcmp (dp
, "first")) {
406 else if (!strcmp (dp
, "last")) {
420 /* foo:+3 is same as foo:3 */
424 } else if (*dp
== '-' || *dp
== ':') {
425 /* foo:-3 or foo::3 */
430 count
= strtol(dp
,&ep
,10);
431 if (count
== 0 || *ep
) /* 0 illegal */
436 *bp
= '\0'; /* temporarily terminate sequence name */
437 } else if (*dp
== '=') {
442 /* foo=+3 is same as foo=3 */
446 } else if (*dp
== '-') {
453 count
= strtol(dp
,&ep
,10); /* 0 illegal */
454 if (count
== 0 || *ep
)
460 *bp
= '\0'; /* temporarily terminate sequence name */
463 i
= seq_getnum (mp
, cp
); /* get index of sequence */
466 *bp
= op
; /* restore sequence name */
470 found
= 0; /* count the number we select for this argument */
475 for (j
= start
; j
>= mp
->lowmsg
&& j
<= mp
->hghmsg
; j
+= convdir
) {
477 if (does_exist (mp
, j
)
478 && inverted
? !in_sequence (mp
, i
, j
) : in_sequence (mp
, i
, j
)) {
480 /* we want all that we find, or just the last in the +/_ case */
481 if (!just_one
|| found
>= count
) {
482 if (!is_selected (mp
, j
)) {
483 set_selected (mp
, j
);
485 if (mp
->lowsel
== 0 || j
< mp
->lowsel
)
492 * If we have any sort of limit, then break out
493 * once we've found enough.
495 if (count
&& found
>= count
)
504 if (first
|| just_one
)
506 inform("sequence %s %s", cp
, inverted
? "full" : "empty");