]>
diplodocus.org Git - nmh/blob - sbr/m_convert.c
3 * m_convert.c -- parse a message range or sequence and set SELECTED
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
15 * error codes for sequence
16 * 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
, range
, 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)
71 if (!strcmp (cp
, "all"))
74 if ((err
= first
= m_conv (mp
, cp
, FIRST
)) <= 0)
78 if (*cp
!= '\0' && *cp
!= '-' && *cp
!= ':') {
80 advise (NULL
, "illegal argument delimiter: `%c'(0%o)", *delimp
, *delimp
);
86 if ((err
= last
= m_conv (mp
, cp
, LAST
)) <= 0) {
90 advise (NULL
, "no %s message", cp
);
94 advise (NULL
, "message %s doesn't exist", cp
);
98 advise (NULL
, "message %s out of range 1-%d", cp
, mp
->hghmsg
);
103 advise (NULL
, "bad message list %s", name
);
107 advise (NULL
, "folder full, no %s message", name
);
111 advise (NULL
, "no messages match specification");
120 if (first
> mp
->hghmsg
|| last
< mp
->lowmsg
) {
122 advise (NULL
, "no messages in range %s", name
);
126 /* tighten the range to search */
127 if (last
> mp
->hghmsg
)
129 if (first
< mp
->lowmsg
)
132 } else if (*cp
== ':') {
143 if ((range
= atoi (bp
= cp
)) == 0)
145 while (isdigit (*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 (last
< mp
->lowmsg
)
167 if (last
> mp
->hghmsg
)
180 * If ALLOW_NEW is set, then allow selecting of an
181 * empty slot. If ALLOW_NEW is not set, then we
182 * check if message is in-range and exists.
184 if (mp
->msgflags
& ALLOW_NEW
) {
185 set_select_empty (mp
, first
);
187 if (first
> mp
->hghmsg
188 || first
< mp
->lowmsg
189 || !(does_exist (mp
, first
))) {
190 if (!strcmp (name
, "cur") || !strcmp (name
, "."))
191 advise (NULL
, "no %s message", name
);
193 advise (NULL
, "message %d doesn't exist", first
);
197 last
= first
; /* range of 1 */
201 * Cycle through the range and select the messages
202 * that exist. If ALLOW_NEW is set, then we also check
203 * if we are selecting an empty slot.
205 for (; first
<= last
; first
++) {
206 if (does_exist (mp
, first
) ||
207 ((mp
->msgflags
& ALLOW_NEW
) && is_select_empty (mp
, first
))) {
208 if (!is_selected (mp
, first
)) {
209 set_selected (mp
, first
);
211 if (mp
->lowsel
== 0 || first
< mp
->lowsel
)
213 if (first
> mp
->hghsel
)
227 * Convert the various message names to
228 * their numeric values.
240 m_conv (struct msgs
*mp
, char *str
, int call
)
243 register char *cp
, *bp
;
249 while (isdigit (*bp
))
256 else if (*delimp
|| call
== LAST
)
257 return mp
->hghmsg
+ 1;
258 else if (mp
->msgflags
& ALLOW_NEW
)
265 /* doesn't enforce lower case */
266 for (bp
= buf
; (isalpha(*cp
) || *cp
== '.')
267 && (bp
- buf
< sizeof(buf
) - 1); )
269 for (bp
= buf
; ((*cp
>= 'a' && *cp
<= 'z') || *cp
== '.')
270 && (bp
- buf
< sizeof(buf
) - 1); )
278 if (!strcmp (buf
, "first"))
279 return (mp
->hghmsg
|| !(mp
->msgflags
& ALLOW_NEW
)
280 ? mp
->lowmsg
: BADMSG
);
282 if (!strcmp (buf
, "last")) {
284 return (mp
->hghmsg
|| !(mp
->msgflags
& ALLOW_NEW
) ? mp
->hghmsg
: BADMSG
);
287 if (!strcmp (buf
, "cur") || !strcmp (buf
, "."))
288 return (mp
->curmsg
> 0 ? mp
->curmsg
: BADMSG
);
290 if (!strcmp (buf
, "prev")) {
292 for (i
= (mp
->curmsg
<= mp
->hghmsg
) ? mp
->curmsg
- 1 : mp
->hghmsg
;
293 i
>= mp
->lowmsg
; i
--) {
294 if (does_exist (mp
, i
))
300 if (!strcmp (buf
, "next")) {
301 for (i
= (mp
->curmsg
>= mp
->lowmsg
) ? mp
->curmsg
+ 1 : mp
->lowmsg
;
302 i
<= mp
->hghmsg
; i
++) {
303 if (does_exist (mp
, i
))
313 * Handle user defined sequences.
314 * They can take the following forms:
327 attr (struct msgs
*mp
, char *cp
)
334 range
= 0, /* no range */
337 /* hack for "cur-name", "cur-n", etc. */
338 if (!strcmp (cp
, "cur"))
340 if (ssequal ("cur:", cp
)) /* this code need to be rewritten... */
343 /* Check for sequence negation */
344 if ((dp
= context_find (nsequence
)) && *dp
!= '\0' && ssequal (dp
, cp
)) {
349 convdir
= 1; /* convert direction */
351 for (dp
= cp
; *dp
&& isalnum(*dp
); dp
++)
365 if (!strcmp (dp
, "prev")) {
367 first
= (mp
->curmsg
> 0) && (mp
->curmsg
<= mp
->hghmsg
)
371 else if (!strcmp (dp
, "next")) {
373 first
= (mp
->curmsg
>= mp
->lowmsg
)
377 else if (!strcmp (dp
, "first")) {
380 else if (!strcmp (dp
, "last")) {
393 else if (*dp
== '-') {
397 if ((range
= atoi(dp
)) == 0)
399 while (isdigit (*dp
))
405 *bp
= '\0'; /* temporarily terminate sequence name */
408 i
= seq_getnum (mp
, cp
); /* get index of sequence */
411 *bp
= ':'; /* restore sequence name */
415 found
= 0; /* count the number we select for this argument */
417 for (j
= first
? first
: (convdir
> 0) ? mp
->lowmsg
: mp
->hghmsg
;
418 j
>= mp
->lowmsg
&& j
<= mp
->hghmsg
; j
+= convdir
) {
419 if (does_exist (mp
, j
)
420 && inverted
? !in_sequence (mp
, i
, j
) : in_sequence (mp
, i
, j
)) {
421 if (!is_selected (mp
, j
)) {
422 set_selected (mp
, j
);
424 if (mp
->lowsel
== 0 || j
< mp
->lowsel
)
432 * If we have a range, then break out
433 * once we've found enough.
435 if (range
&& found
>= range
)
445 advise (NULL
, "sequence %s %s", cp
, inverted
? "full" : "empty");