]>
diplodocus.org Git - nmh/blob - sbr/m_convert.c
d5d069053092d2bf3c8af7b4e056be3f2f2d1c00
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.
9 #include "seq_getnum.h"
14 * error codes for sequence
15 * and message range processing
27 #define getnew(mp) (mp->hghmsg + 1)
29 static int convdir
; /* convert direction */
35 static int m_conv (struct msgs
*, char *, int);
36 static int attr (struct msgs
*, char *);
40 m_convert (struct msgs
*mp
, char *name
)
42 int first
, last
, found
, count
, err
;
45 /* check if user defined sequence */
46 err
= attr (mp
, cp
= name
);
55 * 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 inform("illegal argument delimiter: `%c'(0%o)", *delimp
, *delimp
);
85 if ((err
= last
= m_conv (mp
, cp
, LAST
)) <= 0) {
89 inform("no %s message", cp
);
93 inform("message %s doesn't exist", cp
);
97 inform("message %s out of range 1-%d", cp
, mp
->hghmsg
);
102 inform("bad message list %s", name
);
106 inform("folder full, no %s message", name
);
110 inform("no messages match specification");
119 if (first
> mp
->hghmsg
|| last
< mp
->lowmsg
) {
121 inform("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
== '=') {
133 bool is_range
= *cp
== ':';
137 /* foo:-3 or foo=-3 */
140 } else if (*cp
== '+') {
141 /* foo:+3 or foo=+3 is same as foo:3 or foo=3 */
145 if ((count
= atoi (bp
= cp
)) == 0)
147 while (isdigit ((unsigned char) *bp
))
151 if ((convdir
> 0 && first
> mp
->hghmsg
)
152 || (convdir
< 0 && first
< mp
->lowmsg
))
155 /* tighten the range to search */
156 if (first
< mp
->lowmsg
)
158 if (first
> mp
->hghmsg
)
162 last
>= mp
->lowmsg
&& last
<= mp
->hghmsg
;
164 if (does_exist (mp
, last
))
167 if (is_range
) { /* a range includes any messages that exist */
168 if (last
< mp
->lowmsg
)
170 if (last
> mp
->hghmsg
)
177 } else { /* looking for the nth message. if not enough, fail. */
178 if (last
< mp
->lowmsg
|| last
> mp
->hghmsg
) {
179 inform("no such message");
190 * If ALLOW_NEW is set, then allow selecting of an
191 * empty slot. If ALLOW_NEW is not set, then we
192 * check if message is in-range and exists.
194 if (mp
->msgflags
& ALLOW_NEW
) {
196 * We can get into a case where the "cur" sequence is way out
197 * of range, and because it's allowed to not exist (think
198 * of "rmm; next") it doesn't get checked to make sure it's
199 * within the range of messages in seq_init(). So if our
200 * desired sequence is out of range of the allocated folder
201 * limits simply reallocate the folder so it's within range.
203 if (first
< mp
->lowoff
|| first
> mp
->hghoff
)
204 mp
= folder_realloc(mp
, min(first
, mp
->lowoff
),
205 max(first
, mp
->hghoff
));
207 set_select_empty (mp
, first
);
209 if (first
> mp
->hghmsg
210 || first
< mp
->lowmsg
211 || !(does_exist (mp
, first
))) {
212 if (!strcmp (name
, "cur") || !strcmp (name
, "."))
213 inform("no %s message", name
);
215 inform("message %d doesn't exist", first
);
219 last
= first
; /* range of 1 */
223 * Cycle through the range and select the messages
224 * that exist. If ALLOW_NEW is set, then we also check
225 * if we are selecting an empty slot.
227 for (; first
<= last
; first
++) {
228 if (does_exist (mp
, first
) ||
229 ((mp
->msgflags
& ALLOW_NEW
) && is_select_empty (mp
, first
))) {
230 if (!is_selected (mp
, first
)) {
231 set_selected (mp
, first
);
233 if (mp
->lowsel
== 0 || first
< mp
->lowsel
)
235 if (first
> mp
->hghsel
)
249 * Convert the various message names to
250 * their numeric values.
262 m_conv (struct msgs
*mp
, char *str
, int call
)
270 if (isdigit ((unsigned char) *cp
)) {
271 while (isdigit ((unsigned char) *bp
))
278 if (*delimp
|| call
== LAST
)
279 return mp
->hghmsg
+ 1;
280 if (mp
->msgflags
& ALLOW_NEW
)
285 /* doesn't enforce lower case */
286 for (bp
= buf
; (isalpha((unsigned char) *cp
) || *cp
== '.')
287 && (bp
- buf
< (int) sizeof(buf
) - 1); )
294 if (!strcmp (buf
, "first"))
295 return mp
->hghmsg
|| !(mp
->msgflags
& ALLOW_NEW
) ?
298 if (!strcmp (buf
, "last")) {
300 return mp
->hghmsg
|| !(mp
->msgflags
& ALLOW_NEW
) ? mp
->hghmsg
: BADMSG
;
303 if (!strcmp (buf
, "cur") || !strcmp (buf
, "."))
304 return mp
->curmsg
> 0 ? mp
->curmsg
: BADMSG
;
306 if (!strcmp (buf
, "prev")) {
308 for (i
= (mp
->curmsg
<= mp
->hghmsg
) ? mp
->curmsg
- 1 : mp
->hghmsg
;
309 i
>= mp
->lowmsg
; i
--) {
310 if (does_exist (mp
, i
))
316 if (!strcmp (buf
, "next")) {
317 for (i
= (mp
->curmsg
>= mp
->lowmsg
) ? mp
->curmsg
+ 1 : mp
->lowmsg
;
318 i
<= mp
->hghmsg
; i
++) {
319 if (does_exist (mp
, i
))
329 * Handle user defined sequences.
330 * They can take the following forms:
343 attr (struct msgs
*mp
, char *cp
)
351 count
= 0, /* range given? else use entire sequence */
355 /* hack for "cur-name", "cur-n", etc. */
356 if (!strcmp (cp
, "cur"))
358 if (has_prefix(cp
, "cur")) {
359 if (cp
[3] == ':' || cp
[3] == '=')
363 /* Check for sequence negation */
364 bool inverted
= false;
365 if ((dp
= context_find (nsequence
)) && *dp
!= '\0' && ssequal (dp
, cp
)) {
370 convdir
= 1; /* convert direction */
372 for (dp
= cp
; isalnum((unsigned char)*dp
); dp
++)
375 bool just_one
= *dp
== '='; /* want entire sequence or range */
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
)
458 *bp
= '\0'; /* temporarily terminate sequence name */
461 i
= seq_getnum (mp
, cp
); /* get index of sequence */
464 *bp
= op
; /* restore sequence name */
468 found
= 0; /* count the number we select for this argument */
473 for (j
= start
; j
>= mp
->lowmsg
&& j
<= mp
->hghmsg
; j
+= convdir
) {
475 if (does_exist (mp
, j
)
476 && inverted
? !in_sequence (mp
, i
, j
) : in_sequence (mp
, i
, j
)) {
478 /* we want all that we find, or just the last in the +/_ case */
479 if (!just_one
|| found
>= count
) {
480 if (!is_selected (mp
, j
)) {
481 set_selected (mp
, j
);
483 if (mp
->lowsel
== 0 || j
< mp
->lowsel
)
490 * If we have any sort of limit, then break out
491 * once we've found enough.
493 if (count
&& found
>= count
)
502 if (first
|| just_one
)
504 inform("sequence %s %s", cp
, inverted
? "full" : "empty");