Remove RCS keywords, since they no longer work after git migration.
[mmh] / sbr / seq_read.c
1
2 /*
3  * seq_read.c -- read the .mh_sequence file and
4  *            -- initialize sequence information
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 /*
15  * static prototypes
16  */
17 static int seq_init (struct msgs *, char *, char *);
18 static void seq_public (struct msgs *);
19 static void seq_private (struct msgs *);
20
21
22 /*
23  * Get the sequence information for this folder from
24  * .mh_sequences (or equivalent specified in .mh_profile)
25  * or context file (for private sequences).
26  */
27
28 void
29 seq_read (struct msgs *mp)
30 {
31     /*
32      * Initialize the list of sequence names.  Go ahead and
33      * add the "cur" sequence to the list of sequences.
34      */
35     mp->msgattrs[0] = getcpy (current);
36     mp->msgattrs[1] = NULL;
37     make_all_public (mp);       /* initially, make all public */
38
39     /* If folder is empty, don't scan for sequence information */
40     if (mp->nummsg == 0)
41         return;
42
43     /* Initialize the public sequences */
44     seq_public (mp);
45
46     /* Initialize the private sequences */
47     seq_private (mp);
48 }
49
50
51 /*
52  * read folder's sequences file for public sequences
53  */
54
55 static void
56 seq_public (struct msgs *mp)
57 {
58     int state;
59     char *cp, seqfile[PATH_MAX];
60     char name[NAMESZ], field[BUFSIZ];
61     FILE *fp;
62
63     /*
64      * If mh_seq == NULL (such as if nmh been compiled with
65      * NOPUBLICSEQ), or if *mh_seq == '\0' (the user has defined
66      * the "mh-sequences" profile entry, but left it empty),
67      * then just return, and do not initialize any public sequences.
68      */
69     if (mh_seq == NULL || *mh_seq == '\0')
70         return;
71
72     /* get filename of sequence file */
73     snprintf (seqfile, sizeof(seqfile), "%s/%s", mp->foldpath, mh_seq);
74
75     if ((fp = lkfopen (seqfile, "r")) == NULL)
76         return;
77
78     /* Use m_getfld to scan sequence file */
79     for (state = FLD;;) {
80         switch (state = m_getfld (state, name, field, sizeof(field), fp)) {
81             case FLD: 
82             case FLDPLUS:
83             case FLDEOF: 
84                 if (state == FLDPLUS) {
85                     cp = getcpy (field);
86                     while (state == FLDPLUS) {
87                         state = m_getfld (state, name, field, sizeof(field), fp);
88                         cp = add (field, cp);
89                     }
90                     seq_init (mp, getcpy (name), trimcpy (cp));
91                     free (cp);
92                 } else {
93                     seq_init (mp, getcpy (name), trimcpy (field));
94                 }
95                 if (state == FLDEOF)
96                     break;
97                 continue;
98
99             case BODY: 
100             case BODYEOF: 
101                 adios (NULL, "no blank lines are permitted in %s", seqfile);
102                 /* fall */
103
104             case FILEEOF:
105                 break;
106
107             default: 
108                 adios (NULL, "%s is poorly formatted", seqfile);
109         }
110         break;  /* break from for loop */
111     }
112
113     lkfclose (fp, seqfile);
114 }
115
116
117 /*
118  * Scan profile/context list for private sequences.
119  *
120  * We search the context list for all keys that look like
121  * "atr-seqname-folderpath", and add them as private sequences.
122  */
123
124 static void
125 seq_private (struct msgs *mp)
126 {
127     int i, j, alen, plen;
128     char *cp;
129     struct node *np;
130
131     alen = strlen ("atr-");
132     plen = strlen (mp->foldpath) + 1;
133
134     for (np = m_defs; np; np = np->n_next) {
135         if (ssequal ("atr-", np->n_name)
136                 && (j = strlen (np->n_name) - plen) > alen
137                 && *(np->n_name + j) == '-'
138                 && strcmp (mp->foldpath, np->n_name + j + 1) == 0) {
139             cp = getcpy (np->n_name + alen);
140             *(cp + j - alen) = '\0';
141             if ((i = seq_init (mp, cp, getcpy (np->n_field))) != -1)
142                 make_seq_private (mp, i);
143         }
144     }
145 }
146
147
148 /*
149  * Add the name of sequence to the list of folder sequences.
150  * Then parse the list of message ranges for this
151  * sequence, and setup the various bit flags for each
152  * message in the sequence.
153  *
154  * Return internal index for the sequence if successful.
155  * Return -1 on error.
156  */
157
158 static int
159 seq_init (struct msgs *mp, char *name, char *field)
160 {
161     int i, j, k, is_current;
162     char *cp, **ap;
163
164     /*
165      * Check if this is "cur" sequence,
166      * so we can do some special things.
167      */
168     is_current = !strcmp (current, name);
169
170     /*
171      * Search for this sequence name to see if we've seen
172      * it already.  If we've seen this sequence before,
173      * then clear the bit for this sequence from all the
174      * mesages in this folder.
175      */
176     for (i = 0; mp->msgattrs[i]; i++) {
177         if (!strcmp (mp->msgattrs[i], name)) {
178             for (j = mp->lowmsg; j <= mp->hghmsg; j++)
179                 clear_sequence (mp, i, j);
180             break;
181         }
182     }
183
184     /* Return error, if too many sequences */
185     if (i >= NUMATTRS) {
186         free (name);
187         free (field);
188         return -1;
189     }
190
191     /*
192      * If we've already seen this sequence name, just free the
193      * name string.  Else add it to the list of sequence names.
194      */
195     if (mp->msgattrs[i]) {
196         free (name);
197     } else {
198         mp->msgattrs[i] = name;
199         mp->msgattrs[i + 1] = NULL;
200     }
201
202     /*
203      * Split up the different message ranges at whitespace
204      */
205     for (ap = brkstring (field, " ", "\n"); *ap; ap++) {
206         if ((cp = strchr(*ap, '-')))
207             *cp++ = '\0';
208         if ((j = m_atoi (*ap)) > 0) {
209             k = cp ? m_atoi (cp) : j;
210
211             /*
212              * Keep mp->curmsg and "cur" sequence in synch.  Unlike
213              * other sequences, this message doesn't need to exist.
214              * Think about the series of command (rmm; next) to
215              * understand why this can be the case.  But if it does
216              * exist, we will still set the bit flag for it like
217              * other sequences.
218              */
219             if (is_current)
220                 mp->curmsg = j;
221             /*
222              * We iterate through messages in this range
223              * and flip on bit for this sequence.
224              */
225             for (; j <= k; j++) {
226                 if (j >= mp->lowmsg && j <= mp->hghmsg && does_exist(mp, j))
227                     add_sequence (mp, i, j);
228             }
229         }
230     }
231
232     free (field);       /* free string containing message ranges */
233     return i;
234 }