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