]>
diplodocus.org Git - nmh/blob - sbr/m_convert.c
3f67b2cc8737bac9845eb95dd78ebfcea9470d7a
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.
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
, err
;
44 /* check if user defined sequence */
45 err
= attr (mp
, cp
= name
);
54 * 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
== '=') {
132 bool is_range
= *cp
== ':';
136 /* foo:-3 or foo=-3 */
139 } else if (*cp
== '+') {
140 /* foo:+3 or foo=+3 is same as foo:3 or foo=3 */
144 if ((count
= atoi (bp
= cp
)) == 0)
146 while (isdigit ((unsigned char) *bp
))
150 if ((convdir
> 0 && first
> mp
->hghmsg
)
151 || (convdir
< 0 && first
< mp
->lowmsg
))
154 /* tighten the range to search */
155 if (first
< mp
->lowmsg
)
157 if (first
> mp
->hghmsg
)
161 last
>= mp
->lowmsg
&& last
<= mp
->hghmsg
;
163 if (does_exist (mp
, last
))
166 if (is_range
) { /* a range includes any messages that exist */
167 if (last
< mp
->lowmsg
)
169 if (last
> mp
->hghmsg
)
176 } else { /* looking for the nth message. if not enough, fail. */
177 if (last
< mp
->lowmsg
|| last
> mp
->hghmsg
) {
178 inform("no such message");
189 * If ALLOW_NEW is set, then allow selecting of an
190 * empty slot. If ALLOW_NEW is not set, then we
191 * check if message is in-range and exists.
193 if (mp
->msgflags
& ALLOW_NEW
) {
195 * We can get into a case where the "cur" sequence is way out
196 * of range, and because it's allowed to not exist (think
197 * of "rmm; next") it doesn't get checked to make sure it's
198 * within the range of messages in seq_init(). So if our
199 * desired sequence is out of range of the allocated folder
200 * limits simply reallocate the folder so it's within range.
202 if (first
< mp
->lowoff
|| first
> mp
->hghoff
)
203 mp
= folder_realloc(mp
, min(first
, mp
->lowoff
),
204 max(first
, mp
->hghoff
));
206 set_select_empty (mp
, first
);
208 if (first
> mp
->hghmsg
209 || first
< mp
->lowmsg
210 || !(does_exist (mp
, first
))) {
211 if (!strcmp (name
, "cur") || !strcmp (name
, "."))
212 inform("no %s message", name
);
214 inform("message %d doesn't exist", first
);
218 last
= first
; /* range of 1 */
222 * Cycle through the range and select the messages
223 * that exist. If ALLOW_NEW is set, then we also check
224 * if we are selecting an empty slot.
226 for (; first
<= last
; first
++) {
227 if (does_exist (mp
, first
) ||
228 ((mp
->msgflags
& ALLOW_NEW
) && is_select_empty (mp
, first
))) {
229 if (!is_selected (mp
, first
)) {
230 set_selected (mp
, first
);
232 if (mp
->lowsel
== 0 || first
< mp
->lowsel
)
234 if (first
> mp
->hghsel
)
248 * Convert the various message names to
249 * their numeric values.
261 m_conv (struct msgs
*mp
, char *str
, int call
)
269 if (isdigit ((unsigned char) *cp
)) {
270 while (isdigit ((unsigned char) *bp
))
277 if (*delimp
|| call
== LAST
)
278 return mp
->hghmsg
+ 1;
279 if (mp
->msgflags
& ALLOW_NEW
)
284 /* doesn't enforce lower case */
285 for (bp
= buf
; (isalpha((unsigned char) *cp
) || *cp
== '.')
286 && (bp
- buf
< (int) sizeof(buf
) - 1); )
293 if (!strcmp (buf
, "first"))
294 return mp
->hghmsg
|| !(mp
->msgflags
& ALLOW_NEW
) ?
297 if (!strcmp (buf
, "last")) {
299 return mp
->hghmsg
|| !(mp
->msgflags
& ALLOW_NEW
) ? mp
->hghmsg
: BADMSG
;
302 if (!strcmp (buf
, "cur") || !strcmp (buf
, "."))
303 return mp
->curmsg
> 0 ? mp
->curmsg
: BADMSG
;
305 if (!strcmp (buf
, "prev")) {
307 for (i
= (mp
->curmsg
<= mp
->hghmsg
) ? mp
->curmsg
- 1 : mp
->hghmsg
;
308 i
>= mp
->lowmsg
; i
--) {
309 if (does_exist (mp
, i
))
315 if (!strcmp (buf
, "next")) {
316 for (i
= (mp
->curmsg
>= mp
->lowmsg
) ? mp
->curmsg
+ 1 : mp
->lowmsg
;
317 i
<= mp
->hghmsg
; i
++) {
318 if (does_exist (mp
, i
))
328 * Handle user defined sequences.
329 * They can take the following forms:
342 attr (struct msgs
*mp
, char *cp
)
350 count
= 0, /* range given? else use entire sequence */
354 /* hack for "cur-name", "cur-n", etc. */
355 if (!strcmp (cp
, "cur"))
357 if (has_prefix(cp
, "cur")) {
358 if (cp
[3] == ':' || cp
[3] == '=')
362 /* Check for sequence negation */
363 bool inverted
= false;
364 if ((dp
= context_find (nsequence
)) && *dp
!= '\0' && ssequal (dp
, cp
)) {
369 convdir
= 1; /* convert direction */
371 for (dp
= cp
; isalnum((unsigned char)*dp
); dp
++)
374 bool just_one
= *dp
== '='; /* want entire sequence or range */
386 if (isalpha ((unsigned char) *dp
)) {
387 if (!strcmp (dp
, "prev")) {
389 first
= (mp
->curmsg
> 0) && (mp
->curmsg
<= mp
->hghmsg
)
394 else if (!strcmp (dp
, "next")) {
396 first
= (mp
->curmsg
>= mp
->lowmsg
)
401 else if (!strcmp (dp
, "first")) {
405 else if (!strcmp (dp
, "last")) {
419 /* foo:+3 is same as foo:3 */
423 } else if (*dp
== '-' || *dp
== ':') {
424 /* foo:-3 or foo::3 */
429 count
= strtol(dp
,&ep
,10);
430 if (count
== 0 || *ep
) /* 0 illegal */
435 *bp
= '\0'; /* temporarily terminate sequence name */
436 } else if (*dp
== '=') {
441 /* foo=+3 is same as foo=3 */
445 } else if (*dp
== '-') {
452 count
= strtol(dp
,&ep
,10); /* 0 illegal */
453 if (count
== 0 || *ep
)
457 *bp
= '\0'; /* temporarily terminate sequence name */
460 i
= seq_getnum (mp
, cp
); /* get index of sequence */
463 *bp
= op
; /* restore sequence name */
467 found
= 0; /* count the number we select for this argument */
472 for (j
= start
; j
>= mp
->lowmsg
&& j
<= mp
->hghmsg
; j
+= convdir
) {
474 if (does_exist (mp
, j
)
475 && inverted
? !in_sequence (mp
, i
, j
) : in_sequence (mp
, i
, j
)) {
477 /* we want all that we find, or just the last in the +/_ case */
478 if (!just_one
|| found
>= count
) {
479 if (!is_selected (mp
, j
)) {
480 set_selected (mp
, j
);
482 if (mp
->lowsel
== 0 || j
< mp
->lowsel
)
489 * If we have any sort of limit, then break out
490 * once we've found enough.
492 if (count
&& found
>= count
)
501 if (first
|| just_one
)
503 inform("sequence %s %s", cp
, inverted
? "full" : "empty");