]>
diplodocus.org Git - nmh/blob - sbr/m_convert.c
3 * m_convert.c -- parse a message range or sequence and set SELECTED
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
13 * error codes for sequence
14 * and message range processing
26 #define getnew(mp) (mp->hghmsg + 1)
28 static int convdir
; /* convert direction */
34 static int m_conv (struct msgs
*, char *, int);
35 static int attr (struct msgs
*, char *);
39 m_convert (struct msgs
*mp
, char *name
)
41 int first
, last
, found
, count
, is_range
, err
;
44 /* check if user defined sequence */
45 err
= attr (mp
, cp
= name
);
54 * else err == 0, so continue
61 * Check for special "new" sequence, which
62 * is valid only if ALLOW_NEW is set.
64 if ((mp
->msgflags
& ALLOW_NEW
) && !strcmp (cp
, "new")) {
65 if ((err
= first
= getnew (mp
)) <= 0)
70 if (!strcmp (cp
, "all"))
73 if ((err
= first
= m_conv (mp
, cp
, FIRST
)) <= 0)
77 if (*cp
!= '\0' && *cp
!= '-' && *cp
!= ':' && *cp
!= '=') {
79 advise (NULL
, "illegal argument delimiter: `%c'(0%o)", *delimp
, *delimp
);
85 if ((err
= last
= m_conv (mp
, cp
, LAST
)) <= 0) {
89 advise (NULL
, "no %s message", cp
);
93 advise (NULL
, "message %s doesn't exist", cp
);
97 advise (NULL
, "message %s out of range 1-%d", cp
, mp
->hghmsg
);
102 advise (NULL
, "bad message list %s", name
);
106 advise (NULL
, "folder full, no %s message", name
);
110 advise (NULL
, "no messages match specification");
119 if (first
> mp
->hghmsg
|| last
< mp
->lowmsg
) {
121 advise (NULL
, "no messages in range %s", name
);
125 /* tighten the range to search */
126 if (last
> mp
->hghmsg
)
128 if (first
< mp
->lowmsg
)
131 } else if (*cp
== ':' || *cp
== '=') {
139 /* foo:-3 or foo=-3 */
142 } else if (*cp
== '+') {
143 /* foo:+3 or foo=+3 is same as foo:3 or foo=3 */
147 if ((count
= atoi (bp
= cp
)) == 0)
149 while (isdigit ((unsigned char) *bp
))
153 if ((convdir
> 0 && first
> mp
->hghmsg
)
154 || (convdir
< 0 && first
< mp
->lowmsg
))
157 /* tighten the range to search */
158 if (first
< mp
->lowmsg
)
160 if (first
> mp
->hghmsg
)
164 last
>= mp
->lowmsg
&& last
<= mp
->hghmsg
;
166 if (does_exist (mp
, last
))
169 if (is_range
) { /* a range includes any messages that exist */
170 if (last
< mp
->lowmsg
)
172 if (last
> mp
->hghmsg
)
179 } else { /* looking for the nth message. if not enough, fail. */
180 if (last
< mp
->lowmsg
|| last
> mp
->hghmsg
) {
181 advise (NULL
, "no such message");
192 * If ALLOW_NEW is set, then allow selecting of an
193 * empty slot. If ALLOW_NEW is not set, then we
194 * check if message is in-range and exists.
196 if (mp
->msgflags
& ALLOW_NEW
) {
198 * We can get into a case where the "cur" sequence is way out
199 * of range, and because it's allowed to not exist (think
200 * of "rmm; next") it doesn't get checked to make sure it's
201 * within the range of messages in seq_init(). So if our
202 * desired sequence is out of range of the allocated folder
203 * limits simply reallocate the folder so it's within range.
205 if (first
< mp
->lowoff
|| first
> mp
->hghoff
)
206 mp
= folder_realloc(mp
, first
< mp
->lowoff
? first
: mp
->lowoff
,
207 first
> mp
->hghoff
? first
: mp
->hghoff
);
209 set_select_empty (mp
, first
);
211 if (first
> mp
->hghmsg
212 || first
< mp
->lowmsg
213 || !(does_exist (mp
, first
))) {
214 if (!strcmp (name
, "cur") || !strcmp (name
, "."))
215 advise (NULL
, "no %s message", name
);
217 advise (NULL
, "message %d doesn't exist", first
);
221 last
= first
; /* range of 1 */
225 * Cycle through the range and select the messages
226 * that exist. If ALLOW_NEW is set, then we also check
227 * if we are selecting an empty slot.
229 for (; first
<= last
; first
++) {
230 if (does_exist (mp
, first
) ||
231 ((mp
->msgflags
& ALLOW_NEW
) && is_select_empty (mp
, first
))) {
232 if (!is_selected (mp
, first
)) {
233 set_selected (mp
, first
);
235 if (mp
->lowsel
== 0 || first
< mp
->lowsel
)
237 if (first
> mp
->hghsel
)
251 * Convert the various message names to
252 * their numeric values.
264 m_conv (struct msgs
*mp
, char *str
, int call
)
272 if (isdigit ((unsigned char) *cp
)) {
273 while (isdigit ((unsigned char) *bp
))
280 if (*delimp
|| call
== LAST
)
281 return mp
->hghmsg
+ 1;
282 if (mp
->msgflags
& ALLOW_NEW
)
287 /* doesn't enforce lower case */
288 for (bp
= buf
; (isalpha((unsigned char) *cp
) || *cp
== '.')
289 && (bp
- buf
< (int) sizeof(buf
) - 1); )
296 if (!strcmp (buf
, "first"))
297 return (mp
->hghmsg
|| !(mp
->msgflags
& ALLOW_NEW
)
298 ? mp
->lowmsg
: BADMSG
);
300 if (!strcmp (buf
, "last")) {
302 return (mp
->hghmsg
|| !(mp
->msgflags
& ALLOW_NEW
) ? mp
->hghmsg
: BADMSG
);
305 if (!strcmp (buf
, "cur") || !strcmp (buf
, "."))
306 return (mp
->curmsg
> 0 ? mp
->curmsg
: BADMSG
);
308 if (!strcmp (buf
, "prev")) {
310 for (i
= (mp
->curmsg
<= mp
->hghmsg
) ? mp
->curmsg
- 1 : mp
->hghmsg
;
311 i
>= mp
->lowmsg
; i
--) {
312 if (does_exist (mp
, i
))
318 if (!strcmp (buf
, "next")) {
319 for (i
= (mp
->curmsg
>= mp
->lowmsg
) ? mp
->curmsg
+ 1 : mp
->lowmsg
;
320 i
<= mp
->hghmsg
; i
++) {
321 if (does_exist (mp
, i
))
331 * Handle user defined sequences.
332 * They can take the following forms:
345 attr (struct msgs
*mp
, char *cp
)
354 count
= 0, /* range given? else use entire sequence */
355 just_one
= 0, /* want entire sequence or range */
359 /* hack for "cur-name", "cur-n", etc. */
360 if (!strcmp (cp
, "cur"))
362 if (strncmp ("cur", cp
, 3) == 0) {
363 if (cp
[3] == ':' || cp
[3] == '=')
367 /* Check for sequence negation */
368 if ((dp
= context_find (nsequence
)) && *dp
!= '\0' && ssequal (dp
, cp
)) {
373 convdir
= 1; /* convert direction */
375 for (dp
= cp
; *dp
&& isalnum((unsigned char) *dp
); dp
++)
388 if (isalpha ((unsigned char) *dp
)) {
389 if (!strcmp (dp
, "prev")) {
391 first
= (mp
->curmsg
> 0) && (mp
->curmsg
<= mp
->hghmsg
)
396 else if (!strcmp (dp
, "next")) {
398 first
= (mp
->curmsg
>= mp
->lowmsg
)
403 else if (!strcmp (dp
, "first")) {
407 else if (!strcmp (dp
, "last")) {
421 /* foo:+3 is same as foo:3 */
425 } else if (*dp
== '-' || *dp
== ':') {
426 /* foo:-3 or foo::3 */
431 count
= strtol(dp
,&ep
,10);
432 if (count
== 0 || *ep
) /* 0 illegal */
437 *bp
= '\0'; /* temporarily terminate sequence name */
438 } else if (*dp
== '=') {
443 /* foo=+3 is same as foo=3 */
447 } else if (*dp
== '-') {
454 count
= strtol(dp
,&ep
,10); /* 0 illegal */
455 if (count
== 0 || *ep
)
461 *bp
= '\0'; /* temporarily terminate sequence name */
464 i
= seq_getnum (mp
, cp
); /* get index of sequence */
467 *bp
= op
; /* restore sequence name */
471 found
= 0; /* count the number we select for this argument */
476 for (j
= start
; j
>= mp
->lowmsg
&& j
<= mp
->hghmsg
; j
+= convdir
) {
478 if (does_exist (mp
, j
)
479 && inverted
? !in_sequence (mp
, i
, j
) : in_sequence (mp
, i
, j
)) {
481 /* we want all that we find, or just the last in the +/_ case */
482 if (!just_one
|| found
>= count
) {
483 if (!is_selected (mp
, j
)) {
484 set_selected (mp
, j
);
486 if (mp
->lowsel
== 0 || j
< mp
->lowsel
)
493 * If we have any sort of limit, then break out
494 * once we've found enough.
496 if (count
&& found
>= count
)
505 if (first
|| just_one
)
507 advise (NULL
, "sequence %s %s", cp
, inverted
? "full" : "empty");