]> diplodocus.org Git - nmh/blob - sbr/seq_read.c
mhlsbr.c: Don't strchr(3) non-string NUL-less buffer.
[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;
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_getfld2 to scan sequence file */
84 gstate = m_getfld_state_init(fp);
85 for (;;) {
86 int fieldsz = sizeof field;
87 switch (state = m_getfld2(&gstate, name, field, &fieldsz)) {
88 case FLD:
89 case FLDPLUS:
90 if (state == FLDPLUS) {
91 cp = mh_xstrdup(field);
92 while (state == FLDPLUS) {
93 fieldsz = sizeof field;
94 state = m_getfld2(&gstate, name, field, &fieldsz);
95 cp = add (field, cp);
96 }
97 seq_init (mp, mh_xstrdup(name), trimcpy (cp));
98 free (cp);
99 } else {
100 seq_init (mp, mh_xstrdup(name), trimcpy (field));
101 }
102 continue;
103
104 case BODY:
105 lkfclosedata (fp, seqfile);
106 adios (NULL, "no blank lines are permitted in %s", seqfile);
107 break;
108
109 case FILEEOF:
110 break;
111
112 default:
113 lkfclosedata (fp, seqfile);
114 adios (NULL, "%s is poorly formatted", seqfile);
115 }
116 break; /* break from for loop */
117 }
118 m_getfld_state_destroy (&gstate);
119
120 if (lockflag) {
121 mp->seqhandle = fp;
122 mp->seqname = mh_xstrdup(seqfile);
123 } else {
124 lkfclosedata (fp, seqfile);
125 }
126
127 return OK;
128 }
129
130
131 /*
132 * Scan profile/context list for private sequences.
133 *
134 * We search the context list for all keys that look like
135 * "atr-seqname-folderpath", and add them as private sequences.
136 */
137
138 static void
139 seq_private (struct msgs *mp)
140 {
141 int i, j, alen, plen;
142 char *cp;
143 struct node *np;
144
145 alen = LEN("atr-");
146 plen = strlen (mp->foldpath) + 1;
147
148 for (np = m_defs; np; np = np->n_next) {
149 if (ssequal ("atr-", np->n_name)
150 && (j = strlen (np->n_name) - plen) > alen
151 && *(np->n_name + j) == '-'
152 && strcmp (mp->foldpath, np->n_name + j + 1) == 0) {
153 cp = mh_xstrdup(np->n_name + alen);
154 *(cp + j - alen) = '\0';
155 if ((i = seq_init (mp, cp, getcpy (np->n_field))) != -1)
156 make_seq_private (mp, i);
157 }
158 }
159 }
160
161
162 /*
163 * Add the name of sequence to the list of folder sequences.
164 * Then parse the list of message ranges for this
165 * sequence, and setup the various bit flags for each
166 * message in the sequence.
167 *
168 * Return internal index for the sequence if successful.
169 * Return -1 on error.
170 */
171
172 static int
173 seq_init (struct msgs *mp, char *name, char *field)
174 {
175 unsigned int i;
176 int j, k, is_current;
177 char *cp, **ap;
178
179 /*
180 * Check if this is "cur" sequence,
181 * so we can do some special things.
182 */
183 is_current = !strcmp (current, name);
184
185 /*
186 * Search for this sequence name to see if we've seen
187 * it already. If we've seen this sequence before,
188 * then clear the bit for this sequence from all the
189 * messages in this folder.
190 */
191 for (i = 0; i < svector_size (mp->msgattrs); i++) {
192 if (!strcmp (svector_at (mp->msgattrs, i), name)) {
193 for (j = mp->lowmsg; j <= mp->hghmsg; j++)
194 clear_sequence (mp, i, j);
195 break;
196 }
197 }
198
199 /*
200 * If we've already seen this sequence name, just free the
201 * name string. Else add it to the list of sequence names.
202 */
203 if (svector_at (mp->msgattrs, i)) {
204 free (name);
205 } else {
206 svector_push_back (mp->msgattrs, name);
207 }
208
209 /*
210 * Split up the different message ranges at whitespace
211 */
212 for (ap = brkstring (field, " ", "\n"); *ap; ap++) {
213 if ((cp = strchr(*ap, '-')))
214 *cp++ = '\0';
215 if ((j = m_atoi (*ap)) > 0) {
216 k = cp ? m_atoi (cp) : j;
217
218 /*
219 * Keep mp->curmsg and "cur" sequence in synch. Unlike
220 * other sequences, this message doesn't need to exist.
221 * Think about the series of command (rmm; next) to
222 * understand why this can be the case. But if it does
223 * exist, we will still set the bit flag for it like
224 * other sequences.
225 */
226 if (is_current)
227 mp->curmsg = j;
228 /*
229 * We iterate through messages in this range
230 * and flip on bit for this sequence.
231 */
232 for (; j <= k; j++) {
233 if (j >= mp->lowmsg && j <= mp->hghmsg && does_exist(mp, j))
234 add_sequence (mp, i, j);
235 }
236 }
237 }
238
239 free (field); /* free string containing message ranges */
240 return i;
241 }