]>
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
, 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)
68 if (!strcmp (cp
, "all"))
71 if ((err
= first
= m_conv (mp
, cp
, FIRST
)) <= 0)
75 if (*cp
!= '\0' && *cp
!= '-' && *cp
!= ':' && *cp
!= '=') {
77 inform("illegal argument delimiter: `%c'(0%o)", *delimp
, *delimp
);
83 if ((err
= last
= m_conv (mp
, cp
, LAST
)) <= 0) {
87 inform("no %s message", cp
);
91 inform("message %s doesn't exist", cp
);
95 inform("message %s out of range 1-%d", cp
, mp
->hghmsg
);
100 inform("bad message list %s", name
);
104 inform("folder full, no %s message", name
);
108 inform("no messages match specification");
117 if (first
> mp
->hghmsg
|| last
< mp
->lowmsg
) {
119 inform("no messages in range %s", name
);
123 /* tighten the range to search */
124 if (last
> mp
->hghmsg
)
126 if (first
< mp
->lowmsg
)
129 } else if (*cp
== ':' || *cp
== '=') {
131 bool is_range
= *cp
== ':';
135 /* foo:-3 or foo=-3 */
138 } else if (*cp
== '+') {
139 /* foo:+3 or foo=+3 is same as foo:3 or foo=3 */
143 if ((count
= atoi (bp
= cp
)) == 0)
145 while (isdigit ((unsigned char) *bp
))
149 if ((convdir
> 0 && first
> mp
->hghmsg
)
150 || (convdir
< 0 && first
< mp
->lowmsg
))
153 /* tighten the range to search */
154 if (first
< mp
->lowmsg
)
156 if (first
> mp
->hghmsg
)
160 last
>= mp
->lowmsg
&& last
<= mp
->hghmsg
;
162 if (does_exist (mp
, last
))
165 if (is_range
) { /* a range includes any messages that exist */
166 if (last
< mp
->lowmsg
)
168 if (last
> mp
->hghmsg
)
175 } else { /* looking for the nth message. if not enough, fail. */
176 if (last
< mp
->lowmsg
|| last
> mp
->hghmsg
) {
177 inform("no such message");
188 * If ALLOW_NEW is set, then allow selecting of an
189 * empty slot. If ALLOW_NEW is not set, then we
190 * check if message is in-range and exists.
192 if (mp
->msgflags
& ALLOW_NEW
) {
194 * We can get into a case where the "cur" sequence is way out
195 * of range, and because it's allowed to not exist (think
196 * of "rmm; next") it doesn't get checked to make sure it's
197 * within the range of messages in seq_init(). So if our
198 * desired sequence is out of range of the allocated folder
199 * limits simply reallocate the folder so it's within range.
201 if (first
< mp
->lowoff
|| first
> mp
->hghoff
)
202 mp
= folder_realloc(mp
, min(first
, mp
->lowoff
),
203 max(first
, mp
->hghoff
));
205 set_select_empty (mp
, first
);
207 if (first
> mp
->hghmsg
208 || first
< mp
->lowmsg
209 || !(does_exist (mp
, first
))) {
210 if (!strcmp (name
, "cur") || !strcmp (name
, "."))
211 inform("no %s message", name
);
213 inform("message %d doesn't exist", first
);
217 last
= first
; /* range of 1 */
221 * Cycle through the range and select the messages
222 * that exist. If ALLOW_NEW is set, then we also check
223 * if we are selecting an empty slot.
225 for (; first
<= last
; first
++) {
226 if (does_exist (mp
, first
) ||
227 ((mp
->msgflags
& ALLOW_NEW
) && is_select_empty (mp
, first
))) {
228 if (!is_selected (mp
, first
)) {
229 set_selected (mp
, first
);
231 if (mp
->lowsel
== 0 || first
< mp
->lowsel
)
233 if (first
> mp
->hghsel
)
247 * Convert the various message names to
248 * their numeric values.
260 m_conv (struct msgs
*mp
, char *str
, int call
)
268 if (isdigit ((unsigned char) *cp
)) {
269 while (isdigit ((unsigned char) *bp
))
276 if (*delimp
|| call
== LAST
)
277 return mp
->hghmsg
+ 1;
278 if (mp
->msgflags
& ALLOW_NEW
)
283 /* doesn't enforce lower case */
284 for (bp
= buf
; (isalpha((unsigned char) *cp
) || *cp
== '.')
285 && (bp
- buf
< (int) sizeof(buf
) - 1); )
292 if (!strcmp (buf
, "first"))
293 return mp
->hghmsg
|| !(mp
->msgflags
& ALLOW_NEW
) ?
296 if (!strcmp (buf
, "last")) {
298 return mp
->hghmsg
|| !(mp
->msgflags
& ALLOW_NEW
) ? mp
->hghmsg
: BADMSG
;
301 if (!strcmp (buf
, "cur") || !strcmp (buf
, "."))
302 return mp
->curmsg
> 0 ? mp
->curmsg
: BADMSG
;
304 if (!strcmp (buf
, "prev")) {
306 for (i
= (mp
->curmsg
<= mp
->hghmsg
) ? mp
->curmsg
- 1 : mp
->hghmsg
;
307 i
>= mp
->lowmsg
; i
--) {
308 if (does_exist (mp
, i
))
314 if (!strcmp (buf
, "next")) {
315 for (i
= (mp
->curmsg
>= mp
->lowmsg
) ? mp
->curmsg
+ 1 : mp
->lowmsg
;
316 i
<= mp
->hghmsg
; i
++) {
317 if (does_exist (mp
, i
))
327 * Handle user defined sequences.
328 * They can take the following forms:
341 attr (struct msgs
*mp
, char *cp
)
349 count
= 0, /* range given? else use entire sequence */
353 /* hack for "cur-name", "cur-n", etc. */
354 if (!strcmp (cp
, "cur"))
356 if (has_prefix(cp
, "cur")) {
357 if (cp
[3] == ':' || cp
[3] == '=')
361 /* Check for sequence negation */
362 bool inverted
= false;
363 if ((dp
= context_find (nsequence
)) && *dp
!= '\0' && ssequal (dp
, cp
)) {
368 convdir
= 1; /* convert direction */
370 for (dp
= cp
; isalnum((unsigned char)*dp
); dp
++)
373 bool just_one
= *dp
== '='; /* want entire sequence or range */
385 if (isalpha ((unsigned char) *dp
)) {
386 if (!strcmp (dp
, "prev")) {
388 first
= (mp
->curmsg
> 0) && (mp
->curmsg
<= mp
->hghmsg
)
393 else if (!strcmp (dp
, "next")) {
395 first
= (mp
->curmsg
>= mp
->lowmsg
)
400 else if (!strcmp (dp
, "first")) {
404 else if (!strcmp (dp
, "last")) {
418 /* foo:+3 is same as foo:3 */
422 } else if (*dp
== '-' || *dp
== ':') {
423 /* foo:-3 or foo::3 */
428 count
= strtol(dp
,&ep
,10);
429 if (count
== 0 || *ep
) /* 0 illegal */
434 *bp
= '\0'; /* temporarily terminate sequence name */
435 } else if (*dp
== '=') {
440 /* foo=+3 is same as foo=3 */
444 } else if (*dp
== '-') {
451 count
= strtol(dp
,&ep
,10); /* 0 illegal */
452 if (count
== 0 || *ep
)
456 *bp
= '\0'; /* temporarily terminate sequence name */
459 i
= seq_getnum (mp
, cp
); /* get index of sequence */
462 *bp
= op
; /* restore sequence name */
466 found
= 0; /* count the number we select for this argument */
471 for (j
= start
; j
>= mp
->lowmsg
&& j
<= mp
->hghmsg
; j
+= convdir
) {
473 if (does_exist (mp
, j
)
474 && inverted
? !in_sequence (mp
, i
, j
) : in_sequence (mp
, i
, j
)) {
476 /* we want all that we find, or just the last in the +/_ case */
477 if (!just_one
|| found
>= count
) {
478 if (!is_selected (mp
, j
)) {
479 set_selected (mp
, j
);
481 if (mp
->lowsel
== 0 || j
< mp
->lowsel
)
488 * If we have any sort of limit, then break out
489 * once we've found enough.
491 if (count
&& found
>= count
)
500 if (first
|| just_one
)
502 inform("sequence %s %s", cp
, inverted
? "full" : "empty");