]>
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
;
46 /* check if user defined sequence */
47 err
= attr (mp
, cp
= name
);
56 * else err == 0, so continue
62 * Check for special "new" sequence, which
63 * is valid only if ALLOW_NEW is set.
65 if ((mp
->msgflags
& ALLOW_NEW
) && !strcmp (cp
, "new")) {
66 if ((err
= first
= getnew (mp
)) <= 0)
72 if (!strcmp (cp
, "all"))
75 if ((err
= first
= m_conv (mp
, cp
, FIRST
)) <= 0)
79 if (*cp
!= '\0' && *cp
!= '-' && *cp
!= ':') {
81 advise (NULL
, "illegal argument delimiter: `%c'(0%o)", *delimp
, *delimp
);
87 if ((err
= last
= m_conv (mp
, cp
, LAST
)) <= 0) {
91 advise (NULL
, "no %s message", cp
);
95 advise (NULL
, "message %s doesn't exist", cp
);
99 advise (NULL
, "message %s out of range 1-%d", cp
, mp
->hghmsg
);
104 advise (NULL
, "bad message list %s", name
);
108 advise (NULL
, "folder full, no %s message", name
);
112 advise (NULL
, "no messages match specification");
121 if (first
> mp
->hghmsg
|| last
< mp
->lowmsg
) {
123 advise (NULL
, "no messages in range %s", name
);
127 /* tighten the range to search */
128 if (last
> mp
->hghmsg
)
130 if (first
< mp
->lowmsg
)
133 } else if (*cp
== ':') {
144 if ((range
= atoi (bp
= cp
)) == 0)
146 while (isdigit (*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 (last
< mp
->lowmsg
)
168 if (last
> mp
->hghmsg
)
181 * If ALLOW_NEW is set, then allow selecting of an
182 * empty slot. If ALLOW_NEW is not set, then we
183 * check if message is in-range and exists.
185 if (mp
->msgflags
& ALLOW_NEW
) {
186 set_select_empty (mp
, first
);
188 if (first
> mp
->hghmsg
189 || first
< mp
->lowmsg
190 || !(does_exist (mp
, first
))) {
191 if (!strcmp (name
, "cur") || !strcmp (name
, "."))
192 advise (NULL
, "no %s message", name
);
194 advise (NULL
, "message %d doesn't exist", first
);
198 last
= first
; /* range of 1 */
202 * Cycle through the range and select the messages
203 * that exist. If ALLOW_NEW is set, then we also check
204 * if we are selecting an empty slot.
206 for (; first
<= last
; first
++) {
207 if (does_exist (mp
, first
) ||
208 ((mp
->msgflags
& ALLOW_NEW
) && is_select_empty (mp
, first
))) {
209 if (!is_selected (mp
, first
)) {
210 set_selected (mp
, first
);
212 if (mp
->lowsel
== 0 || first
< mp
->lowsel
)
214 if (first
> mp
->hghsel
)
228 * Convert the various message names to
229 * their numeric values.
241 m_conv (struct msgs
*mp
, char *str
, int call
)
244 register unsigned char *cp
, *bp
;
245 unsigned char buf
[16];
250 while (isdigit (*bp
))
257 else if (*delimp
|| call
== LAST
)
258 return mp
->hghmsg
+ 1;
259 else if (mp
->msgflags
& ALLOW_NEW
)
266 /* doesn't enforce lower case */
267 for (bp
= buf
; (isalpha(*cp
) || *cp
== '.')
268 && (bp
- buf
< sizeof(buf
) - 1); )
270 for (bp
= buf
; ((*cp
>= 'a' && *cp
<= 'z') || *cp
== '.')
271 && (bp
- buf
< sizeof(buf
) - 1); )
279 if (!strcmp (buf
, "first"))
280 return (mp
->hghmsg
|| !(mp
->msgflags
& ALLOW_NEW
)
281 ? mp
->lowmsg
: BADMSG
);
283 if (!strcmp (buf
, "last")) {
285 return (mp
->hghmsg
|| !(mp
->msgflags
& ALLOW_NEW
) ? mp
->hghmsg
: BADMSG
);
288 if (!strcmp (buf
, "cur") || !strcmp (buf
, "."))
289 return (mp
->curmsg
> 0 ? mp
->curmsg
: BADMSG
);
291 if (!strcmp (buf
, "prev")) {
293 for (i
= (mp
->curmsg
<= mp
->hghmsg
) ? mp
->curmsg
- 1 : mp
->hghmsg
;
294 i
>= mp
->lowmsg
; i
--) {
295 if (does_exist (mp
, i
))
301 if (!strcmp (buf
, "next")) {
302 for (i
= (mp
->curmsg
>= mp
->lowmsg
) ? mp
->curmsg
+ 1 : mp
->lowmsg
;
303 i
<= mp
->hghmsg
; i
++) {
304 if (does_exist (mp
, i
))
314 * Handle user defined sequences.
315 * They can take the following forms:
328 attr (struct msgs
*mp
, char *cp
)
330 register unsigned char *dp
;
335 range
= 0, /* no range */
338 /* hack for "cur-name", "cur-n", etc. */
339 if (!strcmp (cp
, "cur"))
341 if (ssequal ("cur:", cp
)) /* this code need to be rewritten... */
344 /* Check for sequence negation */
345 if ((dp
= context_find (nsequence
)) && *dp
!= '\0' && ssequal (dp
, cp
)) {
350 convdir
= 1; /* convert direction */
352 for (dp
= cp
; *dp
&& isalnum(*dp
); dp
++)
366 if (!strcmp (dp
, "prev")) {
368 first
= (mp
->curmsg
> 0) && (mp
->curmsg
<= mp
->hghmsg
)
372 else if (!strcmp (dp
, "next")) {
374 first
= (mp
->curmsg
>= mp
->lowmsg
)
378 else if (!strcmp (dp
, "first")) {
381 else if (!strcmp (dp
, "last")) {
394 else if (*dp
== '-') {
398 if ((range
= atoi(dp
)) == 0)
400 while (isdigit (*dp
))
406 *bp
= '\0'; /* temporarily terminate sequence name */
409 i
= seq_getnum (mp
, cp
); /* get index of sequence */
412 *bp
= ':'; /* restore sequence name */
416 found
= 0; /* count the number we select for this argument */
418 for (j
= first
? first
: (convdir
> 0) ? mp
->lowmsg
: mp
->hghmsg
;
419 j
>= mp
->lowmsg
&& j
<= mp
->hghmsg
; j
+= convdir
) {
420 if (does_exist (mp
, j
)
421 && inverted
? !in_sequence (mp
, i
, j
) : in_sequence (mp
, i
, j
)) {
422 if (!is_selected (mp
, j
)) {
423 set_selected (mp
, j
);
425 if (mp
->lowsel
== 0 || j
< mp
->lowsel
)
433 * If we have a range, then break out
434 * once we've found enough.
436 if (range
&& found
>= range
)
446 advise (NULL
, "sequence %s %s", cp
, inverted
? "full" : "empty");