]> diplodocus.org Git - nmh/blob - sbr/seq_read.c
A bug fix and an enhancement to mhfixmsg based on patches
[nmh] / sbr / seq_read.c
1
2 /*
3 * seq_read.c -- read the .mh_sequence file and
4 * -- initialize sequence information
5 *
6 * This code is Copyright (c) 2002, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
9 */
10
11 #include <h/mh.h>
12 #include <h/utils.h>
13
14 /*
15 * static prototypes
16 */
17 static int seq_init (struct msgs *, char *, char *);
18 static void seq_public (struct msgs *, int);
19 static void seq_private (struct msgs *);
20
21
22 /*
23 * Get the sequence information for this folder from
24 * .mh_sequences (or equivalent specified in .mh_profile)
25 * or context file (for private sequences).
26 */
27
28 void
29 seq_read (struct msgs *mp, int lockflag)
30 {
31 /*
32 * Initialize the list of sequence names. Go ahead and
33 * add the "cur" sequence to the list of sequences.
34 */
35 mp->msgattrs[0] = getcpy (current);
36 mp->msgattrs[1] = NULL;
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;
42
43 /* Initialize the public sequences */
44 seq_public (mp, lockflag);
45
46 /* Initialize the private sequences */
47 seq_private (mp);
48 }
49
50
51 /*
52 * read folder's sequences file for public sequences
53 */
54
55 static void
56 seq_public (struct msgs *mp, int lockflag)
57 {
58 int state;
59 char *cp, seqfile[PATH_MAX];
60 char name[NAMESZ], field[BUFSIZ];
61 FILE *fp;
62 m_getfld_state_t gstate = 0;
63
64 /*
65 * If mh_seq == NULL (such as if nmh been compiled with
66 * NOPUBLICSEQ), or if *mh_seq == '\0' (the user has defined
67 * the "mh-sequences" profile entry, but left it empty),
68 * then just return, and do not initialize any public sequences.
69 */
70 if (mh_seq == NULL || *mh_seq == '\0')
71 return;
72
73 /* get filename of sequence file */
74 snprintf (seqfile, sizeof(seqfile), "%s/%s", mp->foldpath, mh_seq);
75
76 if ((fp = lkfopendata (seqfile, lockflag ? "r+" : "r")) == NULL)
77 return;
78
79 /* Use m_getfld to scan sequence file */
80 for (;;) {
81 int fieldsz = sizeof field;
82 switch (state = m_getfld (&gstate, name, field, &fieldsz, fp)) {
83 case FLD:
84 case FLDPLUS:
85 if (state == FLDPLUS) {
86 cp = getcpy (field);
87 while (state == FLDPLUS) {
88 fieldsz = sizeof field;
89 state = m_getfld (&gstate, name, field, &fieldsz, fp);
90 cp = add (field, cp);
91 }
92 seq_init (mp, getcpy (name), trimcpy (cp));
93 free (cp);
94 } else {
95 seq_init (mp, getcpy (name), trimcpy (field));
96 }
97 continue;
98
99 case BODY:
100 lkfclosedata (fp, seqfile);
101 adios (NULL, "no blank lines are permitted in %s", seqfile);
102 /* fall */
103
104 case FILEEOF:
105 break;
106
107 default:
108 lkfclosedata (fp, seqfile);
109 adios (NULL, "%s is poorly formatted", seqfile);
110 }
111 break; /* break from for loop */
112 }
113 m_getfld_state_destroy (&gstate);
114
115 if (lockflag) {
116 mp->seqhandle = fp;
117 mp->seqname = getcpy(seqfile);
118 } else {
119 lkfclosedata (fp, seqfile);
120 }
121 }
122
123
124 /*
125 * Scan profile/context list for private sequences.
126 *
127 * We search the context list for all keys that look like
128 * "atr-seqname-folderpath", and add them as private sequences.
129 */
130
131 static void
132 seq_private (struct msgs *mp)
133 {
134 int i, j, alen, plen;
135 char *cp;
136 struct node *np;
137
138 alen = strlen ("atr-");
139 plen = strlen (mp->foldpath) + 1;
140
141 for (np = m_defs; np; np = np->n_next) {
142 if (ssequal ("atr-", np->n_name)
143 && (j = strlen (np->n_name) - plen) > alen
144 && *(np->n_name + j) == '-'
145 && strcmp (mp->foldpath, np->n_name + j + 1) == 0) {
146 cp = getcpy (np->n_name + alen);
147 *(cp + j - alen) = '\0';
148 if ((i = seq_init (mp, cp, getcpy (np->n_field))) != -1)
149 make_seq_private (mp, i);
150 }
151 }
152 }
153
154
155 /*
156 * Add the name of sequence to the list of folder sequences.
157 * Then parse the list of message ranges for this
158 * sequence, and setup the various bit flags for each
159 * message in the sequence.
160 *
161 * Return internal index for the sequence if successful.
162 * Return -1 on error.
163 */
164
165 static int
166 seq_init (struct msgs *mp, char *name, char *field)
167 {
168 unsigned int i;
169 int j, k, is_current;
170 char *cp, **ap;
171
172 /*
173 * Check if this is "cur" sequence,
174 * so we can do some special things.
175 */
176 is_current = !strcmp (current, name);
177
178 /*
179 * Search for this sequence name to see if we've seen
180 * it already. If we've seen this sequence before,
181 * then clear the bit for this sequence from all the
182 * mesages in this folder.
183 */
184 for (i = 0; mp->msgattrs[i]; i++) {
185 if (!strcmp (mp->msgattrs[i], name)) {
186 for (j = mp->lowmsg; j <= mp->hghmsg; j++)
187 clear_sequence (mp, i, j);
188 break;
189 }
190 }
191
192 /* Return error, if too many sequences */
193 if (i >= NUMATTRS) {
194 admonish (NULL, "Too many sequences, sequence %s ignored", name);
195 free (name);
196 free (field);
197 return -1;
198 }
199
200 /*
201 * If we've already seen this sequence name, just free the
202 * name string. Else add it to the list of sequence names.
203 */
204 if (mp->msgattrs[i]) {
205 free (name);
206 } else {
207 mp->msgattrs[i] = name;
208 mp->msgattrs[i + 1] = NULL;
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 }