2 ** seq_read.c -- read the .mh_sequence file and
3 ** -- initialize sequence information
5 ** This code is Copyright (c) 2002, by the authors of nmh. See the
6 ** COPYRIGHT file in the root directory of the nmh distribution for
7 ** complete copyright information.
17 static int seq_init(struct msgs *, char *, char *);
18 static void seq_public(struct msgs *);
19 static void seq_private(struct msgs *);
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).
29 seq_read(struct msgs *mp)
32 ** Initialize the list of sequence names. Go ahead and
33 ** add the cur sequence to the list of sequences.
35 mp->msgattrs[0] = mh_xstrdup(seq_cur);
36 mp->msgattrs[1] = NULL;
37 make_all_public(mp); /* initially, make all public */
39 /* If folder is empty, don't scan for sequence information */
43 /* Initialize the public sequences */
46 /* Initialize the private sequences */
52 ** read folder's sequences file for public sequences
56 seq_public(struct msgs *mp)
59 struct field f = {{0}};
60 char seqfile[PATH_MAX];
64 ** If public sequences are disabled (e.g. the user has defined
65 ** an empty `Mh-Sequences' profile entry), then just return.
67 if (mh_seq == NULL || *mh_seq == '\0')
70 /* get filename of sequence file */
71 snprintf(seqfile, sizeof(seqfile), "%s/%s", mp->foldpath, mh_seq);
73 if ((fp = lkfopen(seqfile, "r")) == NULL)
76 /* Use m_getfld to scan sequence file */
77 for (state = FLD2;;) {
78 switch (state = m_getfld2(state, &f, fp)) {
80 seq_init(mp, mh_xstrdup(f.name), trimcpy(f.value));
84 adios(EX_CONFIG, NULL, "no blank lines are permitted in %s",
92 adios(EX_CONFIG, NULL, "%s is poorly formatted", seqfile);
97 lkfclose(fp, seqfile);
102 ** Scan profile/context list for private sequences.
104 ** We search the context list for all keys that look like
105 ** "atr-seqname-folderpath", and add them as private sequences.
109 seq_private(struct msgs *mp)
111 int i, j, alen, plen;
115 alen = strlen("atr-");
116 plen = strlen(mp->foldpath) + 1;
118 for (np = m_defs; np; np = np->n_next) {
119 if (strncmp(np->n_name, "atr-", alen)==0 &&
120 (j = strlen(np->n_name) - plen) > alen &&
121 *(np->n_name + j) == '-' &&
122 strcmp(mp->foldpath, np->n_name + j + 1)==0) {
123 cp = mh_xstrdup(np->n_name + alen);
124 *(cp + j - alen) = '\0';
125 if ((i = seq_init(mp, cp, mh_xstrdup(np->n_field))) != -1)
126 make_seq_private(mp, i);
133 ** Add the name of sequence to the list of folder sequences.
134 ** Then parse the list of message ranges for this
135 ** sequence, and setup the various bit flags for each
136 ** message in the sequence.
138 ** Return internal index for the sequence if successful.
139 ** Return -1 on error.
143 seq_init(struct msgs *mp, char *name, char *field)
146 int j, k, is_current;
150 ** Check if this is the cur sequence,
151 ** so we can do some special things.
153 is_current = (strcmp(seq_cur, name)==0);
156 ** Search for this sequence name to see if we've seen
157 ** it already. If we've seen this sequence before,
158 ** then clear the bit for this sequence from all the
159 ** mesages in this folder.
161 for (i = 0; mp->msgattrs[i]; i++) {
162 if (strcmp(mp->msgattrs[i], name)==0) {
163 for (j = mp->lowmsg; j <= mp->hghmsg; j++)
164 clear_sequence(mp, i, j);
169 /* Return error, if too many sequences */
177 ** If we've already seen this sequence name, just free the
178 ** name string. Else add it to the list of sequence names.
180 if (mp->msgattrs[i]) {
183 mp->msgattrs[i] = name;
184 mp->msgattrs[i + 1] = NULL;
188 ** Split up the different message ranges at whitespace
190 for (ap = brkstring(field, " ", "\n"); *ap; ap++) {
191 if ((cp = strchr(*ap, '-')))
193 if ((j = m_atoi(*ap)) > 0) {
194 k = cp ? m_atoi(cp) : j;
197 ** Keep mp->curmsg and cur sequence in sync. Unlike
198 ** other sequences, this message doesn't need to exist.
199 ** Think about the series of command (rmm; next) to
200 ** understand why this can be the case. But if it does
201 ** exist, we will still set the bit flag for it like
207 ** We iterate through messages in this range
208 ** and flip on bit for this sequence.
210 for (; j <= k; j++) {
211 if (j >= mp->lowmsg && j <= mp->hghmsg &&
213 add_sequence(mp, i, j);
218 mh_free0(&field); /* free string containing message ranges */