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