]> diplodocus.org Git - nmh/blob - sbr/seq_read.c
A few minor fixups; the parser now passes the test suite!
[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 *, int);
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, int lockflag)
30 {
31 /*
32 * Initialize the list of sequence names. Go ahead and
33 * add the "cur" sequence to the list of sequences.
34 */
35 svector_push_back (mp->msgattrs, getcpy (current));
36 make_all_public (mp); /* initially, make all public */
37
38 /* If folder is empty, don't scan for sequence information */
39 if (mp->nummsg == 0)
40 return;
41
42 /* Initialize the public sequences */
43 seq_public (mp, lockflag);
44
45 /* Initialize the private sequences */
46 seq_private (mp);
47 }
48
49
50 /*
51 * read folder's sequences file for public sequences
52 */
53
54 static void
55 seq_public (struct msgs *mp, int lockflag)
56 {
57 int state;
58 char *cp, seqfile[PATH_MAX];
59 char name[NAMESZ], field[BUFSIZ];
60 FILE *fp;
61 m_getfld_state_t gstate = 0;
62
63 /*
64 * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
65 * the "mh-sequences" profile entry, but left it empty),
66 * then just return, and do not initialize any public sequences.
67 */
68 if (mh_seq == NULL || *mh_seq == '\0')
69 return;
70
71 /* get filename of sequence file */
72 snprintf (seqfile, sizeof(seqfile), "%s/%s", mp->foldpath, mh_seq);
73
74 if ((fp = lkfopendata (seqfile, lockflag ? "r+" : "r")) == NULL)
75 return;
76
77 /* Use m_getfld to scan sequence file */
78 for (;;) {
79 int fieldsz = sizeof field;
80 switch (state = m_getfld (&gstate, name, field, &fieldsz, fp)) {
81 case FLD:
82 case FLDPLUS:
83 if (state == FLDPLUS) {
84 cp = getcpy (field);
85 while (state == FLDPLUS) {
86 fieldsz = sizeof field;
87 state = m_getfld (&gstate, name, field, &fieldsz, 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 continue;
96
97 case BODY:
98 lkfclosedata (fp, seqfile);
99 adios (NULL, "no blank lines are permitted in %s", seqfile);
100 /* fall */
101
102 case FILEEOF:
103 break;
104
105 default:
106 lkfclosedata (fp, seqfile);
107 adios (NULL, "%s is poorly formatted", seqfile);
108 }
109 break; /* break from for loop */
110 }
111 m_getfld_state_destroy (&gstate);
112
113 if (lockflag) {
114 mp->seqhandle = fp;
115 mp->seqname = getcpy(seqfile);
116 } else {
117 lkfclosedata (fp, seqfile);
118 }
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 unsigned int i;
167 int j, k, is_current;
168 char *cp, **ap;
169
170 /*
171 * Check if this is "cur" sequence,
172 * so we can do some special things.
173 */
174 is_current = !strcmp (current, name);
175
176 /*
177 * Search for this sequence name to see if we've seen
178 * it already. If we've seen this sequence before,
179 * then clear the bit for this sequence from all the
180 * mesages in this folder.
181 */
182 for (i = 0; i < svector_size (mp->msgattrs); i++) {
183 if (!strcmp (svector_at (mp->msgattrs, i), name)) {
184 for (j = mp->lowmsg; j <= mp->hghmsg; j++)
185 clear_sequence (mp, i, j);
186 break;
187 }
188 }
189
190 /*
191 * If we've already seen this sequence name, just free the
192 * name string. Else add it to the list of sequence names.
193 */
194 if (svector_at (mp->msgattrs, i)) {
195 free (name);
196 } else {
197 svector_push_back (mp->msgattrs, name);
198 }
199
200 /*
201 * Split up the different message ranges at whitespace
202 */
203 for (ap = brkstring (field, " ", "\n"); *ap; ap++) {
204 if ((cp = strchr(*ap, '-')))
205 *cp++ = '\0';
206 if ((j = m_atoi (*ap)) > 0) {
207 k = cp ? m_atoi (cp) : j;
208
209 /*
210 * Keep mp->curmsg and "cur" sequence in synch. Unlike
211 * other sequences, this message doesn't need to exist.
212 * Think about the series of command (rmm; next) to
213 * understand why this can be the case. But if it does
214 * exist, we will still set the bit flag for it like
215 * other sequences.
216 */
217 if (is_current)
218 mp->curmsg = j;
219 /*
220 * We iterate through messages in this range
221 * and flip on bit for this sequence.
222 */
223 for (; j <= k; j++) {
224 if (j >= mp->lowmsg && j <= mp->hghmsg && does_exist(mp, j))
225 add_sequence (mp, i, j);
226 }
227 }
228 }
229
230 free (field); /* free string containing message ranges */
231 return i;
232 }