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