]>
diplodocus.org Git - nmh/blob - sbr/m_convert.c
f62fbb0cd0490d27b5b36c534cb3682026908910
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 "folder_realloc.h"
11 #include "context_find.h"
12 #include "seq_getnum.h"
17 * error codes for sequence
18 * and message range processing
30 #define getnew(mp) (mp->hghmsg + 1)
32 static int convdir
; /* convert direction */
38 static int m_conv (struct msgs
*, char *, int);
39 static int attr (struct msgs
*, char *);
43 m_convert (struct msgs
*mp
, char *name
)
45 int first
, last
, found
, count
, err
;
48 /* check if user defined sequence */
49 err
= attr (mp
, cp
= name
);
58 * else err == 0, so continue
64 * Check for special "new" sequence, which
65 * is valid only if ALLOW_NEW is set.
67 if ((mp
->msgflags
& ALLOW_NEW
) && !strcmp (cp
, "new")) {
68 if ((err
= first
= getnew (mp
)) <= 0)
73 if (!strcmp (cp
, "all"))
76 if ((err
= first
= m_conv (mp
, cp
, FIRST
)) <= 0)
80 if (*cp
!= '\0' && *cp
!= '-' && *cp
!= ':' && *cp
!= '=') {
82 inform("illegal argument delimiter: `%c'(0%o)", *delimp
, *delimp
);
88 if ((err
= last
= m_conv (mp
, cp
, LAST
)) <= 0) {
92 inform("no %s message", cp
);
96 inform("message %s doesn't exist", cp
);
100 inform("message %s out of range 1-%d", cp
, mp
->hghmsg
);
105 inform("bad message list %s", name
);
109 inform("folder full, no %s message", name
);
113 inform("no messages match specification");
122 if (first
> mp
->hghmsg
|| last
< mp
->lowmsg
) {
124 inform("no messages in range %s", name
);
128 /* tighten the range to search */
129 if (last
> mp
->hghmsg
)
131 if (first
< mp
->lowmsg
)
134 } else if (*cp
== ':' || *cp
== '=') {
136 bool is_range
= *cp
== ':';
140 /* foo:-3 or foo=-3 */
143 } else if (*cp
== '+') {
144 /* foo:+3 or foo=+3 is same as foo:3 or foo=3 */
148 if ((count
= atoi (bp
= cp
)) == 0)
150 while (isdigit ((unsigned char) *bp
))
154 if ((convdir
> 0 && first
> mp
->hghmsg
)
155 || (convdir
< 0 && first
< mp
->lowmsg
))
158 /* tighten the range to search */
159 if (first
< mp
->lowmsg
)
161 if (first
> mp
->hghmsg
)
165 last
>= mp
->lowmsg
&& last
<= mp
->hghmsg
;
167 if (does_exist (mp
, last
))
170 if (is_range
) { /* a range includes any messages that exist */
171 if (last
< mp
->lowmsg
)
173 if (last
> mp
->hghmsg
)
180 } else { /* looking for the nth message. if not enough, fail. */
181 if (last
< mp
->lowmsg
|| last
> mp
->hghmsg
) {
182 inform("no such message");
193 * If ALLOW_NEW is set, then allow selecting of an
194 * empty slot. If ALLOW_NEW is not set, then we
195 * check if message is in-range and exists.
197 if (mp
->msgflags
& ALLOW_NEW
) {
199 * We can get into a case where the "cur" sequence is way out
200 * of range, and because it's allowed to not exist (think
201 * of "rmm; next") it doesn't get checked to make sure it's
202 * within the range of messages in seq_init(). So if our
203 * desired sequence is out of range of the allocated folder
204 * limits simply reallocate the folder so it's within range.
206 if (first
< mp
->lowoff
|| first
> mp
->hghoff
)
207 mp
= folder_realloc(mp
, min(first
, mp
->lowoff
),
208 max(first
, mp
->hghoff
));
210 set_select_empty (mp
, first
);
212 if (first
> mp
->hghmsg
213 || first
< mp
->lowmsg
214 || !(does_exist (mp
, first
))) {
215 if (!strcmp (name
, "cur") || !strcmp (name
, "."))
216 inform("no %s message", name
);
218 inform("message %d doesn't exist", first
);
222 last
= first
; /* range of 1 */
226 * Cycle through the range and select the messages
227 * that exist. If ALLOW_NEW is set, then we also check
228 * if we are selecting an empty slot.
230 for (; first
<= last
; first
++) {
231 if (does_exist (mp
, first
) ||
232 ((mp
->msgflags
& ALLOW_NEW
) && is_select_empty (mp
, first
))) {
233 if (!is_selected (mp
, first
)) {
234 set_selected (mp
, first
);
236 if (mp
->lowsel
== 0 || first
< mp
->lowsel
)
238 if (first
> mp
->hghsel
)
252 * Convert the various message names to
253 * their numeric values.
265 m_conv (struct msgs
*mp
, char *str
, int call
)
273 if (isdigit ((unsigned char) *cp
)) {
274 while (isdigit ((unsigned char) *bp
))
281 if (*delimp
|| call
== LAST
)
282 return mp
->hghmsg
+ 1;
283 if (mp
->msgflags
& ALLOW_NEW
)
288 /* doesn't enforce lower case */
289 for (bp
= buf
; (isalpha((unsigned char) *cp
) || *cp
== '.')
290 && (bp
- buf
< (int) sizeof(buf
) - 1); )
297 if (!strcmp (buf
, "first"))
298 return mp
->hghmsg
|| !(mp
->msgflags
& ALLOW_NEW
) ?
301 if (!strcmp (buf
, "last")) {
303 return mp
->hghmsg
|| !(mp
->msgflags
& ALLOW_NEW
) ? mp
->hghmsg
: BADMSG
;
306 if (!strcmp (buf
, "cur") || !strcmp (buf
, "."))
307 return mp
->curmsg
> 0 ? mp
->curmsg
: BADMSG
;
309 if (!strcmp (buf
, "prev")) {
311 for (i
= (mp
->curmsg
<= mp
->hghmsg
) ? mp
->curmsg
- 1 : mp
->hghmsg
;
312 i
>= mp
->lowmsg
; i
--) {
313 if (does_exist (mp
, i
))
319 if (!strcmp (buf
, "next")) {
320 for (i
= (mp
->curmsg
>= mp
->lowmsg
) ? mp
->curmsg
+ 1 : mp
->lowmsg
;
321 i
<= mp
->hghmsg
; i
++) {
322 if (does_exist (mp
, i
))
332 * Handle user defined sequences.
333 * They can take the following forms:
346 attr (struct msgs
*mp
, char *cp
)
354 count
= 0, /* range given? else use entire sequence */
358 /* hack for "cur-name", "cur-n", etc. */
359 if (!strcmp (cp
, "cur"))
361 if (has_prefix(cp
, "cur")) {
362 if (cp
[3] == ':' || cp
[3] == '=')
366 /* Check for sequence negation */
367 bool inverted
= false;
368 if ((dp
= context_find (nsequence
)) && *dp
!= '\0' && ssequal (dp
, cp
)) {
373 convdir
= 1; /* convert direction */
375 for (dp
= cp
; isalnum((unsigned char)*dp
); dp
++)
378 bool just_one
= *dp
== '='; /* want entire sequence or range */
390 if (isalpha ((unsigned char) *dp
)) {
391 if (!strcmp (dp
, "prev")) {
393 first
= (mp
->curmsg
> 0) && (mp
->curmsg
<= mp
->hghmsg
)
398 else if (!strcmp (dp
, "next")) {
400 first
= (mp
->curmsg
>= mp
->lowmsg
)
405 else if (!strcmp (dp
, "first")) {
409 else if (!strcmp (dp
, "last")) {
423 /* foo:+3 is same as foo:3 */
427 } else if (*dp
== '-' || *dp
== ':') {
428 /* foo:-3 or foo::3 */
433 count
= strtol(dp
,&ep
,10);
434 if (count
== 0 || *ep
) /* 0 illegal */
439 *bp
= '\0'; /* temporarily terminate sequence name */
440 } else if (*dp
== '=') {
445 /* foo=+3 is same as foo=3 */
449 } else if (*dp
== '-') {
456 count
= strtol(dp
,&ep
,10); /* 0 illegal */
457 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 inform("sequence %s %s", cp
, inverted
? "full" : "empty");