3 * seq_read.c -- read the .mh_sequence file and
4 * -- initialize sequence information
8 * This code is Copyright (c) 2002, by the authors of nmh. See the
9 * COPYRIGHT file in the root directory of the nmh distribution for
10 * complete copyright information.
18 static int seq_init (struct msgs *, char *, char *);
19 static void seq_public (struct msgs *);
20 static void seq_private (struct msgs *);
24 * Get the sequence information for this folder from
25 * .mh_sequence (or equivalent specified in .mh_profile)
26 * or context file (for private sequences).
30 seq_read (struct msgs *mp)
32 /* sanity check - check that context has been read */
34 adios (NULL, "oops, context hasn't been read yet");
37 * Initialize the list of sequence names. Go ahead and
38 * add the "cur" sequence to the list of sequences.
40 mp->msgattrs[0] = getcpy (current);
41 mp->msgattrs[1] = NULL;
42 make_all_public (mp); /* initially, make all public */
44 /* If folder is empty, don't scan for sequence information */
48 /* Initialize the public sequences */
51 /* Initialize the private sequences */
57 * read folder's sequences file for public sequences
61 seq_public (struct msgs *mp)
64 char *cp, seqfile[PATH_MAX];
65 char name[NAMESZ], field[BUFSIZ];
69 * If mh_seq == NULL (such as if nmh been compiled with
70 * NOPUBLICSEQ), or if *mh_seq == '\0' (the user has defined
71 * the "mh-sequences" profile entry, but left it empty),
72 * then just return, and do not initialize any public sequences.
74 if (mh_seq == NULL || *mh_seq == '\0')
77 /* get filename of sequence file */
78 snprintf (seqfile, sizeof(seqfile), "%s/%s", mp->foldpath, mh_seq);
80 if ((fp = fopen (seqfile, "r")) == NULL)
83 /* Use m_getfld to scan sequence file */
85 switch (state = m_getfld (state, name, field, sizeof(field), fp)) {
89 if (state == FLDPLUS) {
91 while (state == FLDPLUS) {
92 state = m_getfld (state, name, field, sizeof(field), fp);
95 seq_init (mp, getcpy (name), trimcpy (cp));
98 seq_init (mp, getcpy (name), trimcpy (field));
106 adios (NULL, "no blank lines are permitted in %s", seqfile);
113 adios (NULL, "%s is poorly formatted", seqfile);
115 break; /* break from for loop */
123 * Scan profile/context list for private sequences.
125 * We search the context list for all keys that look like
126 * "atr-seqname-folderpath", and add them as private sequences.
130 seq_private (struct msgs *mp)
132 int i, j, alen, plen;
136 alen = strlen ("atr-");
137 plen = strlen (mp->foldpath) + 1;
139 for (np = m_defs; np; np = np->n_next) {
140 if (ssequal ("atr-", np->n_name)
141 && (j = strlen (np->n_name) - plen) > alen
142 && *(np->n_name + j) == '-'
143 && strcmp (mp->foldpath, np->n_name + j + 1) == 0) {
144 cp = getcpy (np->n_name + alen);
145 *(cp + j - alen) = '\0';
146 if ((i = seq_init (mp, cp, getcpy (np->n_field))) != -1)
147 make_seq_private (mp, i);
154 * Add the name of sequence to the list of folder sequences.
155 * Then parse the list of message ranges for this
156 * sequence, and setup the various bit flags for each
157 * message in the sequence.
159 * Return internal index for the sequence if successful.
160 * Return -1 on error.
164 seq_init (struct msgs *mp, char *name, char *field)
166 int i, j, k, is_current;
170 * Check if this is "cur" sequence,
171 * so we can do some special things.
173 is_current = !strcmp (current, name);
176 * Search for this sequence name to see if we've seen
177 * it already. If we've seen this sequence before,
178 * then clear the bit for this sequence from all the
179 * mesages in this folder.
181 for (i = 0; mp->msgattrs[i]; i++) {
182 if (!strcmp (mp->msgattrs[i], name)) {
183 for (j = mp->lowmsg; j <= mp->hghmsg; j++)
184 clear_sequence (mp, i, j);
189 /* Return error, if too many sequences */
197 * If we've already seen this sequence name, just free the
198 * name string. Else add it to the list of sequence names.
200 if (mp->msgattrs[i]) {
203 mp->msgattrs[i] = name;
204 mp->msgattrs[i + 1] = NULL;
208 * Split up the different message ranges at whitespace
210 for (ap = brkstring (field, " ", "\n"); *ap; ap++) {
211 if ((cp = strchr(*ap, '-')))
213 if ((j = m_atoi (*ap)) > 0) {
214 k = cp ? m_atoi (cp) : j;
217 * Keep mp->curmsg and "cur" sequence in synch. Unlike
218 * other sequences, this message doesn't need to exist.
219 * Think about the series of command (rmm; next) to
220 * understand why this can be the case. But if it does
221 * exist, we will still set the bit flag for it like
227 * We iterate through messages in this range
228 * and flip on bit for this sequence.
230 for (; j <= k; j++) {
231 if (j >= mp->lowmsg && j <= mp->hghmsg && does_exist(mp, j))
232 add_sequence (mp, i, j);
237 free (field); /* free string containing message ranges */