]> diplodocus.org Git - nmh/blob - sbr/seq_read.c
Makefile.am: Add test/inc/test-eom-align to XFAIL_TESTS.
[nmh] / sbr / seq_read.c
1 /* seq_read.c -- read the .mh_sequence file and
2 * -- initialize sequence information
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11 #include "lock_file.h"
12
13 /*
14 * static prototypes
15 */
16 static int seq_init (struct msgs *, char *, char *);
17 static int seq_public (struct msgs *, int, int *);
18 static void seq_private (struct msgs *);
19
20
21 /*
22 * Get the sequence information for this folder from
23 * .mh_sequences (or equivalent specified in .mh_profile)
24 * or context file (for private sequences).
25 */
26
27 int
28 seq_read (struct msgs *mp, int lockflag)
29 {
30 int failed_to_lock = 0;
31
32 /*
33 * Initialize the list of sequence names. Go ahead and
34 * add the "cur" sequence to the list of sequences.
35 */
36 svector_push_back (mp->msgattrs, getcpy (current));
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 OK;
42
43 /* Initialize the public sequences */
44 if (seq_public (mp, lockflag, &failed_to_lock) == NOTOK) {
45 if (failed_to_lock) return NOTOK;
46 }
47
48 /* Initialize the private sequences */
49 seq_private (mp);
50
51 return OK;
52 }
53
54
55 /*
56 * read folder's sequences file for public sequences
57 */
58
59 static int
60 seq_public (struct msgs *mp, int lockflag, int *failed_to_lock)
61 {
62 int state;
63 char *cp, seqfile[PATH_MAX];
64 char name[NAMESZ], field[NMH_BUFSIZ];
65 FILE *fp;
66 m_getfld_state_t gstate = 0;
67
68 /*
69 * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
70 * the "mh-sequences" profile entry, but left it empty),
71 * then just return, and do not initialize any public sequences.
72 */
73 if (mh_seq == NULL || *mh_seq == '\0')
74 return OK;
75
76 /* get filename of sequence file */
77 snprintf (seqfile, sizeof(seqfile), "%s/%s", mp->foldpath, mh_seq);
78
79 if ((fp = lkfopendata (seqfile, lockflag ? "r+" : "r", failed_to_lock))
80 == NULL)
81 return NOTOK;
82
83 /* Use m_getfld to scan sequence file */
84 for (;;) {
85 int fieldsz = sizeof field;
86 switch (state = m_getfld (&gstate, name, field, &fieldsz, fp)) {
87 case FLD:
88 case FLDPLUS:
89 if (state == FLDPLUS) {
90 cp = mh_xstrdup(field);
91 while (state == FLDPLUS) {
92 fieldsz = sizeof field;
93 state = m_getfld (&gstate, name, field, &fieldsz, fp);
94 cp = add (field, cp);
95 }
96 seq_init (mp, mh_xstrdup(name), trimcpy (cp));
97 free (cp);
98 } else {
99 seq_init (mp, mh_xstrdup(name), trimcpy (field));
100 }
101 continue;
102
103 case BODY:
104 lkfclosedata (fp, seqfile);
105 adios (NULL, "no blank lines are permitted in %s", seqfile);
106 break;
107
108 case FILEEOF:
109 break;
110
111 default:
112 lkfclosedata (fp, seqfile);
113 adios (NULL, "%s is poorly formatted", seqfile);
114 }
115 break; /* break from for loop */
116 }
117 m_getfld_state_destroy (&gstate);
118
119 if (lockflag) {
120 mp->seqhandle = fp;
121 mp->seqname = mh_xstrdup(seqfile);
122 } else {
123 lkfclosedata (fp, seqfile);
124 }
125
126 return OK;
127 }
128
129
130 /*
131 * Scan profile/context list for private sequences.
132 *
133 * We search the context list for all keys that look like
134 * "atr-seqname-folderpath", and add them as private sequences.
135 */
136
137 static void
138 seq_private (struct msgs *mp)
139 {
140 int i, j, alen, plen;
141 char *cp;
142 struct node *np;
143
144 alen = LEN("atr-");
145 plen = strlen (mp->foldpath) + 1;
146
147 for (np = m_defs; np; np = np->n_next) {
148 if (ssequal ("atr-", np->n_name)
149 && (j = strlen (np->n_name) - plen) > alen
150 && *(np->n_name + j) == '-'
151 && strcmp (mp->foldpath, np->n_name + j + 1) == 0) {
152 cp = mh_xstrdup(np->n_name + alen);
153 *(cp + j - alen) = '\0';
154 if ((i = seq_init (mp, cp, getcpy (np->n_field))) != -1)
155 make_seq_private (mp, i);
156 }
157 }
158 }
159
160
161 /*
162 * Add the name of sequence to the list of folder sequences.
163 * Then parse the list of message ranges for this
164 * sequence, and setup the various bit flags for each
165 * message in the sequence.
166 *
167 * Return internal index for the sequence if successful.
168 * Return -1 on error.
169 */
170
171 static int
172 seq_init (struct msgs *mp, char *name, char *field)
173 {
174 unsigned int i;
175 int j, k, is_current;
176 char *cp, **ap;
177
178 /*
179 * Check if this is "cur" sequence,
180 * so we can do some special things.
181 */
182 is_current = !strcmp (current, name);
183
184 /*
185 * Search for this sequence name to see if we've seen
186 * it already. If we've seen this sequence before,
187 * then clear the bit for this sequence from all the
188 * messages in this folder.
189 */
190 for (i = 0; i < svector_size (mp->msgattrs); i++) {
191 if (!strcmp (svector_at (mp->msgattrs, i), name)) {
192 for (j = mp->lowmsg; j <= mp->hghmsg; j++)
193 clear_sequence (mp, i, j);
194 break;
195 }
196 }
197
198 /*
199 * If we've already seen this sequence name, just free the
200 * name string. Else add it to the list of sequence names.
201 */
202 if (svector_at (mp->msgattrs, i)) {
203 free (name);
204 } else {
205 svector_push_back (mp->msgattrs, name);
206 }
207
208 /*
209 * Split up the different message ranges at whitespace
210 */
211 for (ap = brkstring (field, " ", "\n"); *ap; ap++) {
212 if ((cp = strchr(*ap, '-')))
213 *cp++ = '\0';
214 if ((j = m_atoi (*ap)) > 0) {
215 k = cp ? m_atoi (cp) : j;
216
217 /*
218 * Keep mp->curmsg and "cur" sequence in synch. Unlike
219 * other sequences, this message doesn't need to exist.
220 * Think about the series of command (rmm; next) to
221 * understand why this can be the case. But if it does
222 * exist, we will still set the bit flag for it like
223 * other sequences.
224 */
225 if (is_current)
226 mp->curmsg = j;
227 /*
228 * We iterate through messages in this range
229 * and flip on bit for this sequence.
230 */
231 for (; j <= k; j++) {
232 if (j >= mp->lowmsg && j <= mp->hghmsg && does_exist(mp, j))
233 add_sequence (mp, i, j);
234 }
235 }
236 }
237
238 free (field); /* free string containing message ranges */
239 return i;
240 }