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