cd18406465f029fb8f480f5663eec73c5205ca89
[mmh] / sbr / m_convert.c
1 /*
2 ** m_convert.c -- parse a message range or sequence and set SELECTED
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10
11 /*
12 ** error codes for sequence
13 ** and message range processing
14 */
15 #define BADMSG (-2)
16 #define BADRNG (-3)
17 #define BADNEW (-4)
18 #define BADNUM (-5)
19 #define BADLST (-6)
20
21 #define FIRST 1
22 #define LAST 2
23
24 #define getnew(mp) (mp->hghmsg + 1)
25
26 static int convdir; /* convert direction */
27 static char *delimp;
28
29 /*
30 ** static prototypes
31 */
32 static int m_conv (struct msgs *, char *, int);
33 static int attr (struct msgs *, char *);
34
35
36 int
37 m_convert (struct msgs *mp, char *name)
38 {
39         int first, last, found, range, err;
40         unsigned char *bp;
41         char *cp;
42
43         /* check if user defined sequence */
44         err = attr (mp, cp = name);
45
46         if (err == -1)
47                 return 0;
48         else if (err < 0)
49                 goto badmsg;
50         else if (err > 0)
51                 return 1;
52         /*
53         ** else err == 0, so continue
54         */
55
56         found = 0;
57
58         /*
59         ** Check for special "new" sequence, which
60         ** is valid only if ALLOW_NEW is set.
61         */
62         if ((mp->msgflags & ALLOW_NEW) && !strcmp (cp, "new")) {
63                 if ((err = first = getnew (mp)) <= 0)
64                         goto badmsg;
65                 else
66                         goto single;
67         }
68
69         if (!strcmp (cp, "all"))
70                 cp = "first-last";
71
72         if ((err = first = m_conv (mp, cp, FIRST)) <= 0)
73                 goto badmsg;
74
75         cp = delimp;
76         if (*cp != '\0' && *cp != '-' && *cp != ':') {
77 badelim:
78                 advise (NULL, "illegal argument delimiter: `%c'(0%o)",
79                                 *delimp, *delimp);
80                 return 0;
81         }
82
83         if (*cp == '-') {
84                 cp++;
85                 if ((err = last = m_conv (mp, cp, LAST)) <= 0) {
86 badmsg:
87                         switch (err) {
88                         case BADMSG:
89                                 advise (NULL, "no %s message", cp);
90                                 break;
91
92                         case BADNUM:
93                                 advise (NULL, "message %s doesn't exist", cp);
94                                 break;
95
96                         case BADRNG:
97                                 advise (NULL, "message %s out of range 1-%d",
98                                                 cp, mp->hghmsg);
99                                 break;
100
101                         case BADLST:
102 badlist:
103                                 advise (NULL, "bad message list %s", name);
104                                 break;
105
106                         case BADNEW:
107                                 advise (NULL, "folder full, no %s message",
108                                                 name);
109                                 break;
110
111                         default:
112                                 advise (NULL, "no messages match specification");
113                         }
114                         return 0;
115                 }
116
117                 if (last < first)
118                         goto badlist;
119                 if (*delimp)
120                         goto badelim;
121                 if (first > mp->hghmsg || last < mp->lowmsg) {
122 rangerr:
123                         advise (NULL, "no messages in range %s", name);
124                         return 0;
125                 }
126
127                 /* tighten the range to search */
128                 if (last > mp->hghmsg)
129                         last = mp->hghmsg;
130                 if (first < mp->lowmsg)
131                         first = mp->lowmsg;
132
133         } else if (*cp == ':') {
134                 cp++;
135                 if (*cp == '-') {
136                         convdir = -1;
137                         cp++;
138                 } else if (*cp == '+') {
139                         convdir = 1;
140                         cp++;
141                 }
142                 if ((range = atoi (bp = cp)) == 0)
143                         goto badlist;
144                 while (isdigit (*bp))
145                         bp++;
146                 if (*bp)
147                         goto badelim;
148                 if ((convdir > 0 && first > mp->hghmsg)
149                         || (convdir < 0 && first < mp->lowmsg))
150                         goto rangerr;
151
152                 /* tighten the range to search */
153                 if (first < mp->lowmsg)
154                         first = mp->lowmsg;
155                 if (first > mp->hghmsg)
156                         first = mp->hghmsg;
157
158                 for (last = first; last >= mp->lowmsg && last <= mp->hghmsg;
159                         last += convdir)
160                         if (does_exist (mp, last))
161                                 if (--range <= 0)
162                                         break;
163                 if (last < mp->lowmsg)
164                         last = mp->lowmsg;
165                 if (last > mp->hghmsg)
166                         last = mp->hghmsg;
167                 if (last < first) {
168                         range = last;
169                         last = first;
170                         first = range;
171                 }
172         } else {
173
174 single:
175                 /*
176                 ** Single Message
177                 **
178                 ** If ALLOW_NEW is set, then allow selecting of an
179                 ** empty slot.  If ALLOW_NEW is not set, then we
180                 ** check if message is in-range and exists.
181                 */
182                 if (mp->msgflags & ALLOW_NEW) {
183                         set_select_empty (mp, first);
184                 } else {
185                         if (first > mp->hghmsg
186                                 || first < mp->lowmsg
187                                 || !(does_exist (mp, first))) {
188                                 if (!strcmp (name, "cur") || !strcmp (name, "."))
189                                         advise (NULL, "no %s message", name);
190                                 else
191                                         advise (NULL, "message %d doesn't exist", first);
192                                 return 0;
193                         }
194                 }
195                 last = first;  /* range of 1 */
196         }
197
198         /*
199         ** Cycle through the range and select the messages
200         ** that exist.  If ALLOW_NEW is set, then we also check
201         ** if we are selecting an empty slot.
202         */
203         for (; first <= last; first++) {
204                 if (does_exist (mp, first) ||
205                         ((mp->msgflags & ALLOW_NEW) && is_select_empty (mp, first))) {
206                         if (!is_selected (mp, first)) {
207                                 set_selected (mp, first);
208                                 mp->numsel++;
209                                 if (mp->lowsel == 0 || first < mp->lowsel)
210                                         mp->lowsel = first;
211                                 if (first > mp->hghsel)
212                                         mp->hghsel = first;
213                         }
214                         found++;
215                 }
216         }
217
218         if (!found)
219                 goto rangerr;
220
221         return 1;
222 }
223
224 /*
225 ** Convert the various message names to
226 ** their numeric values.
227 **
228 ** n  (integer)
229 ** prev
230 ** next
231 ** first
232 ** last
233 ** cur
234 ** .  (same as cur)
235 */
236
237 static int
238 m_conv (struct msgs *mp, char *str, int call)
239 {
240         register int i;
241         register unsigned char *cp, *bp;
242         unsigned char buf[16];
243
244         convdir = 1;
245         cp = bp = str;
246         if (isdigit (*cp)) {
247                 while (isdigit (*bp))
248                         bp++;
249                 delimp = bp;
250                 i = atoi (cp);
251
252                 if (i <= mp->hghmsg)
253                         return i;
254                 else if (*delimp || call == LAST)
255                         return mp->hghmsg + 1;
256                 else if (mp->msgflags & ALLOW_NEW)
257                         return BADRNG;
258                 else
259                         return BADNUM;
260         }
261
262 #ifdef LOCALE
263         /* doesn't enforce lower case */
264         for (bp = buf; (isalpha(*cp) || *cp == '.')
265                                 && (bp - buf < sizeof(buf) - 1); )
266 #else
267         for (bp = buf; ((*cp >= 'a' && *cp <= 'z') || *cp == '.')
268                                 && (bp - buf < sizeof(buf) - 1); )
269 #endif /* LOCALE */
270         {
271                 *bp++ = *cp++;
272         }
273         *bp++ = '\0';
274         delimp = cp;
275
276         if (!strcmp (buf, "first"))
277                 return (mp->hghmsg || !(mp->msgflags & ALLOW_NEW)
278                                 ? mp->lowmsg : BADMSG);
279
280         if (!strcmp (buf, "last")) {
281                 convdir = -1;
282                 return (mp->hghmsg || !(mp->msgflags & ALLOW_NEW) ? mp->hghmsg : BADMSG);
283         }
284
285         if (!strcmp (buf, "cur") || !strcmp (buf, "."))
286                 return (mp->curmsg > 0 ? mp->curmsg : BADMSG);
287
288         if (!strcmp (buf, "prev")) {
289                 convdir = -1;
290                 for (i = (mp->curmsg <= mp->hghmsg) ? mp->curmsg - 1 : mp->hghmsg;
291                         i >= mp->lowmsg; i--) {
292                         if (does_exist (mp, i))
293                                 return i;
294                 }
295                 return BADMSG;
296         }
297
298         if (!strcmp (buf, "next")) {
299                 for (i = (mp->curmsg >= mp->lowmsg) ? mp->curmsg + 1 : mp->lowmsg;
300                         i <= mp->hghmsg; i++) {
301                         if (does_exist (mp, i))
302                                 return i;
303                 }
304                 return BADMSG;
305         }
306
307         return BADLST;
308 }
309
310 /*
311 ** Handle user defined sequences.
312 ** They can take the following forms:
313 **
314 ** seq
315 ** seq:prev
316 ** seq:next
317 ** seq:first
318 ** seq:last
319 ** seq:+n
320 ** seq:-n
321 ** seq:n
322 */
323
324 static int
325 attr (struct msgs *mp, char *cp)
326 {
327         register unsigned char *dp;
328         char *bp = NULL;
329         register int i, j;
330         int found;
331         int inverted = 0;
332         int range = 0;  /* no range */
333         int first = 0;
334
335         /* hack for "cur-name", "cur-n", etc. */
336         if (!strcmp (cp, "cur"))
337                 return 0;
338         if (ssequal ("cur:", cp))  /* this code need to be rewritten... */
339                 return 0;
340
341         /* Check for sequence negation */
342         if ((dp = context_find (nsequence)) && *dp != '\0' && ssequal (dp, cp)) {
343                 inverted = 1;
344                 cp += strlen (dp);
345         }
346
347         convdir = 1;  /* convert direction */
348
349         for (dp = cp; *dp && isalnum(*dp); dp++)
350                 continue;
351
352         if (*dp == ':') {
353                 bp = dp++;
354                 range = 1;
355
356                 /*
357                 ** seq:prev  (or)
358                 ** seq:next  (or)
359                 ** seq:first (or)
360                 ** seq:last
361                 */
362                 if (isalpha (*dp)) {
363                         if (!strcmp (dp, "prev")) {
364                                 convdir = -1;
365                                 first = (mp->curmsg > 0) && (mp->curmsg <= mp->hghmsg)
366                                         ? mp->curmsg - 1 : mp->hghmsg;
367                         } else if (!strcmp (dp, "next")) {
368                                 convdir = 1;
369                                 first = (mp->curmsg >= mp->lowmsg)
370                                         ? mp->curmsg + 1 : mp->lowmsg;
371                         } else if (!strcmp (dp, "first")) {
372                                 convdir = 1;
373                         } else if (!strcmp (dp, "last")) {
374                                 convdir = -1;
375                         } else
376                                 return BADLST;
377                 } else {
378                         /*
379                         ** seq:n  (or)
380                         ** seq:+n (or)
381                         ** seq:-n
382                         */
383                         if (*dp == '+')
384                                 dp++;
385                         else if (*dp == '-') {
386                                 dp++;
387                                 convdir = -1;
388                         }
389                         if ((range = atoi(dp)) == 0)
390                                 return BADLST;
391                         while (isdigit (*dp))
392                                 dp++;
393                         if (*dp)
394                                 return BADLST;
395                 }
396
397                 *bp = '\0';  /* temporarily terminate sequence name */
398         }
399
400         i = seq_getnum (mp, cp);  /* get index of sequence */
401
402         if (bp)
403                 *bp = ':';  /* restore sequence name */
404         if (i == -1)
405                 return 0;
406
407         found = 0;  /* count the number we select for this argument */
408
409         for (j = first ? first : (convdir > 0) ? mp->lowmsg : mp->hghmsg;
410                 j >= mp->lowmsg && j <= mp->hghmsg; j += convdir) {
411                 if (does_exist (mp, j)
412                         && inverted ? !in_sequence (mp, i, j) : in_sequence (mp, i, j)) {
413                         if (!is_selected (mp, j)) {
414                                 set_selected (mp, j);
415                                 mp->numsel++;
416                                 if (mp->lowsel == 0 || j < mp->lowsel)
417                                         mp->lowsel = j;
418                                 if (j > mp->hghsel)
419                                         mp->hghsel = j;
420                         }
421                         found++;
422
423                         /*
424                         ** If we have a range, then break out
425                         ** once we've found enough.
426                         */
427                         if (range && found >= range)
428                                 break;
429                 }
430         }
431
432         if (found > 0)
433                 return found;
434
435         if (first)
436                 return BADMSG;
437         advise (NULL, "sequence %s %s", cp, inverted ? "full" : "empty");
438         return -1;
439 }