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