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