]> diplodocus.org Git - nmh/blob - sbr/seq_read.c
3cf8ad89ceed44715dadaac7e5f97f7d325d3024
[nmh] / 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 int fieldsz = sizeof field;
81 switch (state = m_getfld (state, name, field, &fieldsz, fp)) {
82 case FLD:
83 case FLDPLUS:
84 case FLDEOF:
85 if (state == FLDPLUS) {
86 cp = getcpy (field);
87 while (state == FLDPLUS) {
88 fieldsz = sizeof field;
89 state = m_getfld (state, name, field, &fieldsz, fp);
90 cp = add (field, cp);
91 }
92 seq_init (mp, getcpy (name), trimcpy (cp));
93 free (cp);
94 } else {
95 seq_init (mp, getcpy (name), trimcpy (field));
96 }
97 if (state == FLDEOF)
98 break;
99 continue;
100
101 case BODY:
102 case BODYEOF:
103 adios (NULL, "no blank lines are permitted in %s", seqfile);
104 /* fall */
105
106 case FILEEOF:
107 break;
108
109 default:
110 adios (NULL, "%s is poorly formatted", seqfile);
111 }
112 break; /* break from for loop */
113 }
114
115 lkfclose (fp, seqfile);
116 }
117
118
119 /*
120 * Scan profile/context list for private sequences.
121 *
122 * We search the context list for all keys that look like
123 * "atr-seqname-folderpath", and add them as private sequences.
124 */
125
126 static void
127 seq_private (struct msgs *mp)
128 {
129 int i, j, alen, plen;
130 char *cp;
131 struct node *np;
132
133 alen = strlen ("atr-");
134 plen = strlen (mp->foldpath) + 1;
135
136 for (np = m_defs; np; np = np->n_next) {
137 if (ssequal ("atr-", np->n_name)
138 && (j = strlen (np->n_name) - plen) > alen
139 && *(np->n_name + j) == '-'
140 && strcmp (mp->foldpath, np->n_name + j + 1) == 0) {
141 cp = getcpy (np->n_name + alen);
142 *(cp + j - alen) = '\0';
143 if ((i = seq_init (mp, cp, getcpy (np->n_field))) != -1)
144 make_seq_private (mp, i);
145 }
146 }
147 }
148
149
150 /*
151 * Add the name of sequence to the list of folder sequences.
152 * Then parse the list of message ranges for this
153 * sequence, and setup the various bit flags for each
154 * message in the sequence.
155 *
156 * Return internal index for the sequence if successful.
157 * Return -1 on error.
158 */
159
160 static int
161 seq_init (struct msgs *mp, char *name, char *field)
162 {
163 unsigned int i;
164 int j, k, is_current;
165 char *cp, **ap;
166
167 /*
168 * Check if this is "cur" sequence,
169 * so we can do some special things.
170 */
171 is_current = !strcmp (current, name);
172
173 /*
174 * Search for this sequence name to see if we've seen
175 * it already. If we've seen this sequence before,
176 * then clear the bit for this sequence from all the
177 * mesages in this folder.
178 */
179 for (i = 0; mp->msgattrs[i]; i++) {
180 if (!strcmp (mp->msgattrs[i], name)) {
181 for (j = mp->lowmsg; j <= mp->hghmsg; j++)
182 clear_sequence (mp, i, j);
183 break;
184 }
185 }
186
187 /* Return error, if too many sequences */
188 if (i >= NUMATTRS) {
189 free (name);
190 free (field);
191 return -1;
192 }
193
194 /*
195 * If we've already seen this sequence name, just free the
196 * name string. Else add it to the list of sequence names.
197 */
198 if (mp->msgattrs[i]) {
199 free (name);
200 } else {
201 mp->msgattrs[i] = name;
202 mp->msgattrs[i + 1] = NULL;
203 }
204
205 /*
206 * Split up the different message ranges at whitespace
207 */
208 for (ap = brkstring (field, " ", "\n"); *ap; ap++) {
209 if ((cp = strchr(*ap, '-')))
210 *cp++ = '\0';
211 if ((j = m_atoi (*ap)) > 0) {
212 k = cp ? m_atoi (cp) : j;
213
214 /*
215 * Keep mp->curmsg and "cur" sequence in synch. Unlike
216 * other sequences, this message doesn't need to exist.
217 * Think about the series of command (rmm; next) to
218 * understand why this can be the case. But if it does
219 * exist, we will still set the bit flag for it like
220 * other sequences.
221 */
222 if (is_current)
223 mp->curmsg = j;
224 /*
225 * We iterate through messages in this range
226 * and flip on bit for this sequence.
227 */
228 for (; j <= k; j++) {
229 if (j >= mp->lowmsg && j <= mp->hghmsg && does_exist(mp, j))
230 add_sequence (mp, i, j);
231 }
232 }
233 }
234
235 free (field); /* free string containing message ranges */
236 return i;
237 }