]>
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.
10 #include "m_convert.h"
11 #include "folder_realloc.h"
12 #include "context_find.h"
13 #include "seq_getnum.h"
18 * error codes for sequence
19 * and message range processing
31 #define getnew(mp) (mp->hghmsg + 1)
33 static int convdir
; /* convert direction */
39 static int m_conv (struct msgs
*, char *, int);
40 static int attr (struct msgs
*, char *);
44 m_convert (struct msgs
*mp
, char *name
)
46 int first
, last
, found
, count
, err
;
49 /* check if user defined sequence */
50 err
= attr (mp
, cp
= name
);
59 * else err == 0, so continue
65 * Check for special "new" sequence, which
66 * is valid only if ALLOW_NEW is set.
68 if ((mp
->msgflags
& ALLOW_NEW
) && !strcmp (cp
, "new")) {
69 if ((err
= first
= getnew (mp
)) <= 0)
74 if (!strcmp (cp
, "all"))
77 if ((err
= first
= m_conv (mp
, cp
, FIRST
)) <= 0)
81 if (*cp
!= '\0' && *cp
!= '-' && *cp
!= ':' && *cp
!= '=') {
83 inform("illegal argument delimiter: `%c'(0%o)", *delimp
, *delimp
);
89 if ((err
= last
= m_conv (mp
, cp
, LAST
)) <= 0) {
93 inform("no %s message", cp
);
97 inform("message %s doesn't exist", cp
);
101 inform("message %s out of range 1-%d", cp
, mp
->hghmsg
);
106 inform("bad message list %s", name
);
110 inform("folder full, no %s message", name
);
114 inform("no messages match specification");
123 if (first
> mp
->hghmsg
|| last
< mp
->lowmsg
) {
125 inform("no messages in range %s", name
);
129 /* tighten the range to search */
130 if (last
> mp
->hghmsg
)
132 if (first
< mp
->lowmsg
)
135 } else if (*cp
== ':' || *cp
== '=') {
137 bool is_range
= *cp
== ':';
141 /* foo:-3 or foo=-3 */
144 } else if (*cp
== '+') {
145 /* foo:+3 or foo=+3 is same as foo:3 or foo=3 */
149 if ((count
= atoi (bp
= cp
)) == 0)
151 while (isdigit ((unsigned char) *bp
))
155 if ((convdir
> 0 && first
> mp
->hghmsg
)
156 || (convdir
< 0 && first
< mp
->lowmsg
))
159 /* tighten the range to search */
160 if (first
< mp
->lowmsg
)
162 if (first
> mp
->hghmsg
)
166 last
>= mp
->lowmsg
&& last
<= mp
->hghmsg
;
168 if (does_exist (mp
, last
))
171 if (is_range
) { /* a range includes any messages that exist */
172 if (last
< mp
->lowmsg
)
174 if (last
> mp
->hghmsg
)
181 } else { /* looking for the nth message. if not enough, fail. */
182 if (last
< mp
->lowmsg
|| last
> mp
->hghmsg
) {
183 inform("no such message");
194 * If ALLOW_NEW is set, then allow selecting of an
195 * empty slot. If ALLOW_NEW is not set, then we
196 * check if message is in-range and exists.
198 if (mp
->msgflags
& ALLOW_NEW
) {
200 * We can get into a case where the "cur" sequence is way out
201 * of range, and because it's allowed to not exist (think
202 * of "rmm; next") it doesn't get checked to make sure it's
203 * within the range of messages in seq_init(). So if our
204 * desired sequence is out of range of the allocated folder
205 * limits simply reallocate the folder so it's within range.
207 if (first
< mp
->lowoff
|| first
> mp
->hghoff
)
208 mp
= folder_realloc(mp
, min(first
, mp
->lowoff
),
209 max(first
, mp
->hghoff
));
211 set_select_empty (mp
, first
);
213 if (first
> mp
->hghmsg
214 || first
< mp
->lowmsg
215 || !(does_exist (mp
, first
))) {
216 if (!strcmp (name
, "cur") || !strcmp (name
, "."))
217 inform("no %s message", name
);
219 inform("message %d doesn't exist", first
);
223 last
= first
; /* range of 1 */
227 * Cycle through the range and select the messages
228 * that exist. If ALLOW_NEW is set, then we also check
229 * if we are selecting an empty slot.
231 for (; first
<= last
; first
++) {
232 if (does_exist (mp
, first
) ||
233 ((mp
->msgflags
& ALLOW_NEW
) && is_select_empty (mp
, first
))) {
234 if (!is_selected (mp
, first
)) {
235 set_selected (mp
, first
);
237 if (mp
->lowsel
== 0 || first
< mp
->lowsel
)
239 if (first
> mp
->hghsel
)
253 * Convert the various message names to
254 * their numeric values.
266 m_conv (struct msgs
*mp
, char *str
, int call
)
274 if (isdigit ((unsigned char) *cp
)) {
275 while (isdigit ((unsigned char) *bp
))
282 if (*delimp
|| call
== LAST
)
283 return mp
->hghmsg
+ 1;
284 if (mp
->msgflags
& ALLOW_NEW
)
289 /* doesn't enforce lower case */
290 for (bp
= buf
; (isalpha((unsigned char) *cp
) || *cp
== '.')
291 && (bp
- buf
< (int) sizeof(buf
) - 1); )
298 if (!strcmp (buf
, "first"))
299 return mp
->hghmsg
|| !(mp
->msgflags
& ALLOW_NEW
) ?
302 if (!strcmp (buf
, "last")) {
304 return mp
->hghmsg
|| !(mp
->msgflags
& ALLOW_NEW
) ? mp
->hghmsg
: BADMSG
;
307 if (!strcmp (buf
, "cur") || !strcmp (buf
, "."))
308 return mp
->curmsg
> 0 ? mp
->curmsg
: BADMSG
;
310 if (!strcmp (buf
, "prev")) {
312 for (i
= (mp
->curmsg
<= mp
->hghmsg
) ? mp
->curmsg
- 1 : mp
->hghmsg
;
313 i
>= mp
->lowmsg
; i
--) {
314 if (does_exist (mp
, i
))
320 if (!strcmp (buf
, "next")) {
321 for (i
= (mp
->curmsg
>= mp
->lowmsg
) ? mp
->curmsg
+ 1 : mp
->lowmsg
;
322 i
<= mp
->hghmsg
; i
++) {
323 if (does_exist (mp
, i
))
333 * Handle user defined sequences.
334 * They can take the following forms:
347 attr (struct msgs
*mp
, char *cp
)
355 count
= 0, /* range given? else use entire sequence */
359 /* hack for "cur-name", "cur-n", etc. */
360 if (!strcmp (cp
, "cur"))
362 if (has_prefix(cp
, "cur")) {
363 if (cp
[3] == ':' || cp
[3] == '=')
367 /* Check for sequence negation */
368 bool inverted
= false;
369 if ((dp
= context_find (nsequence
)) && *dp
!= '\0' && ssequal (dp
, cp
)) {
374 convdir
= 1; /* convert direction */
376 for (dp
= cp
; isalnum((unsigned char)*dp
); dp
++)
379 bool just_one
= *dp
== '='; /* want entire sequence or range */
391 if (isalpha ((unsigned char) *dp
)) {
392 if (!strcmp (dp
, "prev")) {
394 first
= (mp
->curmsg
> 0) && (mp
->curmsg
<= mp
->hghmsg
)
399 else if (!strcmp (dp
, "next")) {
401 first
= (mp
->curmsg
>= mp
->lowmsg
)
406 else if (!strcmp (dp
, "first")) {
410 else if (!strcmp (dp
, "last")) {
424 /* foo:+3 is same as foo:3 */
428 } else if (*dp
== '-' || *dp
== ':') {
429 /* foo:-3 or foo::3 */
434 count
= strtol(dp
,&ep
,10);
435 if (count
== 0 || *ep
) /* 0 illegal */
440 *bp
= '\0'; /* temporarily terminate sequence name */
441 } else if (*dp
== '=') {
446 /* foo=+3 is same as foo=3 */
450 } else if (*dp
== '-') {
457 count
= strtol(dp
,&ep
,10); /* 0 illegal */
458 if (count
== 0 || *ep
)
462 *bp
= '\0'; /* temporarily terminate sequence name */
465 i
= seq_getnum (mp
, cp
); /* get index of sequence */
468 *bp
= op
; /* restore sequence name */
472 found
= 0; /* count the number we select for this argument */
477 for (j
= start
; j
>= mp
->lowmsg
&& j
<= mp
->hghmsg
; j
+= convdir
) {
479 if (does_exist (mp
, j
)
480 && inverted
? !in_sequence (mp
, i
, j
) : in_sequence (mp
, i
, j
)) {
482 /* we want all that we find, or just the last in the +/_ case */
483 if (!just_one
|| found
>= count
) {
484 if (!is_selected (mp
, j
)) {
485 set_selected (mp
, j
);
487 if (mp
->lowsel
== 0 || j
< mp
->lowsel
)
494 * If we have any sort of limit, then break out
495 * once we've found enough.
497 if (count
&& found
>= count
)
506 if (first
|| just_one
)
508 inform("sequence %s %s", cp
, inverted
? "full" : "empty");