simplify whatnow.c/main() function
[mmh] / sbr / seq_list.c
1 /*
2 ** seq_list.c -- Get all messages in a sequence and return them
3 **            -- as a space separated list of message ranges.
4 **
5 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
6 ** COPYRIGHT file in the root directory of the nmh distribution for
7 ** complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/utils.h>
12
13 /* allocate this much buffer space at a time */
14 #define MAXBUFFER 1024
15
16 /* static buffer to collect the sequence line */
17 static char *buffer = NULL;
18 static int len = 0;
19
20
21 char *
22 seq_list(struct msgs *mp, char *seqname)
23 {
24         int i, j, seqnum;
25         char *bp;
26
27         /* On first invocation, allocate initial buffer space */
28         if (!buffer) {
29                 len = MAXBUFFER;
30                 buffer = mh_xcalloc(len, sizeof(char));
31         }
32
33         /*
34         ** Special processing for the cur sequence.  We assume that the
35         ** cur sequence and mp->curmsg are in sync (see seq_add.c).
36         ** This is returned, even if message doesn't exist or the
37         ** folder is empty.
38         */
39         if (strcmp(seq_cur, seqname)==0) {
40                 if (mp->curmsg) {
41                         sprintf(buffer, "%s", m_name(mp->curmsg));
42                         return (buffer);
43                 } else
44                         return (NULL);
45         }
46
47         /* If the folder is empty, just return NULL */
48         if (mp->nummsg == 0)
49                 return NULL;
50
51         /* Get the index of the sequence */
52         if ((seqnum = seq_getnum(mp, seqname)) == -1)
53                 return NULL;
54
55         bp = buffer;
56
57         for (i = mp->lowmsg; i <= mp->hghmsg; ++i) {
58                 /*
59                 ** If message doesn't exist, or isn't in
60                 ** the sequence, then continue.
61                 */
62                 if (!does_exist(mp, i) || !in_sequence(mp, seqnum, i))
63                         continue;
64
65                 /*
66                 ** See if we need to enlarge buffer.  Since we don't know
67                 ** exactly how many character this particular message range
68                 ** will need, we enlarge the buffer if we are within
69                 ** 50 characters of the end.
70                 */
71                 if (bp - buffer > len - 50) {
72                         char *newbuf;
73
74                         len += MAXBUFFER;
75                         newbuf = mh_xrealloc(buffer, len);
76                         bp = newbuf + (bp - buffer);
77                         buffer = newbuf;
78                 }
79
80                 /*
81                 ** If this is not the first message range in
82                 ** the list, first add a space.
83                 */
84                 if (bp > buffer)
85                         *bp++ = ' ';
86
87                 sprintf(bp, "%s", m_name(i));
88                 bp += strlen(bp);
89                 j = i;  /* Remember beginning of message range */
90
91                 /*
92                 ** Scan to the end of this message range
93                 */
94                 for (++i; i <= mp->hghmsg && does_exist(mp, i) &&
95                                 in_sequence(mp, seqnum, i); ++i)
96                         ;
97
98                 if (i - j > 1) {
99                         sprintf(bp, "-%s", m_name(i - 1));
100                         bp += strlen(bp);
101                 }
102         }
103         return (bp > buffer? buffer : NULL);
104 }