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