Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / local / lbl / sbr / m_seq.c
1 /* m_seq.c - print out a message sequence */
2
3 #include "../h/mh.h"
4 #include <stdio.h>
5
6 static char *buffer;
7 static int bufsize = BUFSIZ;
8
9 char *
10 m_seq(mp, cp)
11         struct msgs *mp;
12         char *cp;
13 {
14         int mask;
15         register int i, j, linlen;
16         register char *bp, *ep;
17
18         if (buffer == 0) {
19                 if ((buffer = malloc(bufsize)) == NULL)
20                         adios (NULLCP, "unable to allocate seq str storage");
21         }
22         if (strcmp(current, cp) == 0) {
23                 if (mp->curmsg) {
24                         (void) sprintf(buffer, "%s", m_name(mp->curmsg));
25                         return (buffer);
26                 } else
27                         return (NULL);
28         }
29         for (i = 0; mp->msgattrs[i]; i++)
30                 if (strcmp(mp->msgattrs[i], cp) == 0)
31                         break;
32         
33         if (! mp->msgattrs[i])
34                 return (NULL);
35
36         mask = EXISTS | (1 << (FFATTRSLOT + i));
37         bp = buffer;
38         ep = bp + bufsize - 16;
39         linlen = strlen(cp) + 2;
40         for (i = mp->lowmsg; i <= mp->hghmsg; ++i) {
41                 register char *oldbp;
42
43                 if ((mp->msgstats[i] & mask) != mask)
44                         continue;
45
46                 oldbp = bp;
47                 if (bp > buffer) {
48                         if (bp >= ep) {
49                                 int oldoff = bp - buffer;
50                                 bufsize <<= 1;
51                                 if ((buffer = realloc(buffer, bufsize)) == NULL)
52                                         adios (NULLCP, "unable to allocate seq str storage");
53                                 oldbp = bp = buffer + oldoff;
54                                 ep = bp + bufsize - 16;
55                         }
56                         if (linlen >= 72) {
57                                 linlen = 0;
58                                 *bp++ = '\n';
59                                 *bp++ = ' ';
60                         }
61                         *bp++ = ' ';
62                 }
63                 (void) sprintf(bp, "%s", m_name(i));
64                 bp += strlen(bp);
65                 j = i;
66                 for (++i; i <= mp->hghmsg && (mp->msgstats[i] & mask) == mask;
67                      ++i)
68                         ;
69                 if (i - j > 1) {
70                         (void) sprintf(bp, "-%s", m_name(i - 1));
71                         bp += strlen(bp);
72                 }
73                 linlen += bp - oldbp;
74         }
75         return (bp > buffer? buffer : NULL);
76 }