]> diplodocus.org Git - nmh/blob - sbr/seq_read.c
Bring these changes over from the branch.
[nmh] / sbr / seq_read.c
1
2 /*
3 * seq_read.c -- read the .mh_sequence file and
4 * -- initialize sequence information
5 *
6 * $Id$
7 *
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.
11 */
12
13 #include <h/mh.h>
14
15 /*
16 * static prototypes
17 */
18 static int seq_init (struct msgs *, char *, char *);
19 static void seq_public (struct msgs *);
20 static void seq_private (struct msgs *);
21
22
23 /*
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).
27 */
28
29 void
30 seq_read (struct msgs *mp)
31 {
32 /* sanity check - check that context has been read */
33 if (defpath == NULL)
34 adios (NULL, "oops, context hasn't been read yet");
35
36 /*
37 * Initialize the list of sequence names. Go ahead and
38 * add the "cur" sequence to the list of sequences.
39 */
40 mp->msgattrs[0] = getcpy (current);
41 mp->msgattrs[1] = NULL;
42 make_all_public (mp); /* initially, make all public */
43
44 /* If folder is empty, don't scan for sequence information */
45 if (mp->nummsg == 0)
46 return;
47
48 /* Initialize the public sequences */
49 seq_public (mp);
50
51 /* Initialize the private sequences */
52 seq_private (mp);
53 }
54
55
56 /*
57 * read folder's sequences file for public sequences
58 */
59
60 static void
61 seq_public (struct msgs *mp)
62 {
63 int state;
64 char *cp, seqfile[PATH_MAX];
65 char name[NAMESZ], field[BUFSIZ];
66 FILE *fp;
67
68 /*
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.
73 */
74 if (mh_seq == NULL || *mh_seq == '\0')
75 return;
76
77 /* get filename of sequence file */
78 snprintf (seqfile, sizeof(seqfile), "%s/%s", mp->foldpath, mh_seq);
79
80 if ((fp = fopen (seqfile, "r")) == NULL)
81 return;
82
83 /* Use m_getfld to scan sequence file */
84 for (state = FLD;;) {
85 switch (state = m_getfld (state, name, field, sizeof(field), fp)) {
86 case FLD:
87 case FLDPLUS:
88 case FLDEOF:
89 if (state == FLDPLUS) {
90 cp = getcpy (field);
91 while (state == FLDPLUS) {
92 state = m_getfld (state, name, field, sizeof(field), fp);
93 cp = add (field, cp);
94 }
95 seq_init (mp, getcpy (name), trimcpy (cp));
96 free (cp);
97 } else {
98 seq_init (mp, getcpy (name), trimcpy (field));
99 }
100 if (state == FLDEOF)
101 break;
102 continue;
103
104 case BODY:
105 case BODYEOF:
106 adios (NULL, "no blank lines are permitted in %s", seqfile);
107 /* fall */
108
109 case FILEEOF:
110 break;
111
112 default:
113 adios (NULL, "%s is poorly formatted", seqfile);
114 }
115 break; /* break from for loop */
116 }
117
118 fclose (fp);
119 }
120
121
122 /*
123 * Scan profile/context list for private sequences.
124 *
125 * We search the context list for all keys that look like
126 * "atr-seqname-folderpath", and add them as private sequences.
127 */
128
129 static void
130 seq_private (struct msgs *mp)
131 {
132 int i, j, alen, plen;
133 char *cp;
134 struct node *np;
135
136 alen = strlen ("atr-");
137 plen = strlen (mp->foldpath) + 1;
138
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);
148 }
149 }
150 }
151
152
153 /*
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.
158 *
159 * Return internal index for the sequence if successful.
160 * Return -1 on error.
161 */
162
163 static int
164 seq_init (struct msgs *mp, char *name, char *field)
165 {
166 int i, j, k, is_current;
167 char *cp, **ap;
168
169 /*
170 * Check if this is "cur" sequence,
171 * so we can do some special things.
172 */
173 is_current = !strcmp (current, name);
174
175 /*
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.
180 */
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);
185 break;
186 }
187 }
188
189 /* Return error, if too many sequences */
190 if (i >= NUMATTRS) {
191 free (name);
192 free (field);
193 return -1;
194 }
195
196 /*
197 * If we've already seen this sequence name, just free the
198 * name string. Else add it to the list of sequence names.
199 */
200 if (mp->msgattrs[i]) {
201 free (name);
202 } else {
203 mp->msgattrs[i] = name;
204 mp->msgattrs[i + 1] = NULL;
205 }
206
207 /*
208 * Split up the different message ranges at whitespace
209 */
210 for (ap = brkstring (field, " ", "\n"); *ap; ap++) {
211 if ((cp = strchr(*ap, '-')))
212 *cp++ = '\0';
213 if ((j = m_atoi (*ap)) > 0) {
214 k = cp ? m_atoi (cp) : j;
215
216 /*
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
222 * other sequences.
223 */
224 if (is_current)
225 mp->curmsg = j;
226 /*
227 * We iterate through messages in this range
228 * and flip on bit for this sequence.
229 */
230 for (; j <= k; j++) {
231 if (j >= mp->lowmsg && j <= mp->hghmsg && does_exist(mp, j))
232 add_sequence (mp, i, j);
233 }
234 }
235 }
236
237 free (field); /* free string containing message ranges */
238 return i;
239 }