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