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