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