]>
diplodocus.org Git - nmh/blob - sbr/seq_read.c
e63f8101db3020df59828c5932992f4b72f622d0
1 /* seq_read.c -- read the .mh_sequence file and
2 * -- initialize sequence information
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.
15 #include "brkstring.h"
18 #include "lock_file.h"
23 static int seq_init (struct msgs
*, char *, char *);
24 static int seq_public (struct msgs
*, int, int *);
25 static void seq_private (struct msgs
*);
29 * Get the sequence information for this folder from
30 * .mh_sequences (or equivalent specified in .mh_profile)
31 * or context file (for private sequences).
35 seq_read (struct msgs
*mp
, int lockflag
)
37 int failed_to_lock
= 0;
40 * Initialize the list of sequence names. Go ahead and
41 * add the "cur" sequence to the list of sequences.
43 svector_push_back (mp
->msgattrs
, getcpy (current
));
44 make_all_public (mp
); /* initially, make all public */
46 /* If folder is empty, don't scan for sequence information */
50 /* Initialize the public sequences */
51 if (seq_public (mp
, lockflag
, &failed_to_lock
) == NOTOK
) {
52 if (failed_to_lock
) return NOTOK
;
55 /* Initialize the private sequences */
63 * read folder's sequences file for public sequences
67 seq_public (struct msgs
*mp
, int lockflag
, int *failed_to_lock
)
70 char *cp
, seqfile
[PATH_MAX
];
71 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
73 m_getfld_state_t gstate
;
76 * If mh_seq == NULL or if *mh_seq == '\0' (the user has defined
77 * the "mh-sequences" profile entry, but left it empty),
78 * then just return, and do not initialize any public sequences.
80 if (mh_seq
== NULL
|| *mh_seq
== '\0')
83 /* get filename of sequence file */
84 snprintf (seqfile
, sizeof(seqfile
), "%s/%s", mp
->foldpath
, mh_seq
);
86 if ((fp
= lkfopendata (seqfile
, lockflag
? "r+" : "r", failed_to_lock
))
90 /* Use m_getfld2 to scan sequence file */
91 gstate
= m_getfld_state_init(fp
);
93 int fieldsz
= sizeof field
;
94 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
97 if (state
== FLDPLUS
) {
98 cp
= mh_xstrdup(field
);
99 while (state
== FLDPLUS
) {
100 fieldsz
= sizeof field
;
101 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
102 cp
= add (field
, cp
);
104 seq_init (mp
, mh_xstrdup(name
), trimcpy (cp
));
107 seq_init (mp
, mh_xstrdup(name
), trimcpy (field
));
112 lkfclosedata (fp
, seqfile
);
113 die("no blank lines are permitted in %s", seqfile
);
120 lkfclosedata (fp
, seqfile
);
121 die("%s is poorly formatted", seqfile
);
123 break; /* break from for loop */
125 m_getfld_state_destroy (&gstate
);
129 mp
->seqname
= mh_xstrdup(seqfile
);
131 lkfclosedata (fp
, seqfile
);
139 * Scan profile/context list for private sequences.
141 * We search the context list for all keys that look like
142 * "atr-seqname-folderpath", and add them as private sequences.
146 seq_private (struct msgs
*mp
)
148 int i
, j
, alen
, plen
;
153 plen
= strlen (mp
->foldpath
) + 1;
155 for (np
= m_defs
; np
; np
= np
->n_next
) {
156 if (ssequal ("atr-", np
->n_name
)
157 && (j
= strlen (np
->n_name
) - plen
) > alen
158 && *(np
->n_name
+ j
) == '-'
159 && strcmp (mp
->foldpath
, np
->n_name
+ j
+ 1) == 0) {
160 cp
= mh_xstrdup(np
->n_name
+ alen
);
161 *(cp
+ j
- alen
) = '\0';
162 if ((i
= seq_init (mp
, cp
, getcpy (np
->n_field
))) != -1)
163 make_seq_private (mp
, i
);
170 * Add the name of sequence to the list of folder sequences.
171 * Then parse the list of message ranges for this
172 * sequence, and setup the various bit flags for each
173 * message in the sequence.
175 * Return internal index for the sequence if successful.
176 * Return -1 on error.
180 seq_init (struct msgs
*mp
, char *name
, char *field
)
183 int j
, k
, is_current
;
187 * Check if this is "cur" sequence,
188 * so we can do some special things.
190 is_current
= !strcmp (current
, name
);
193 * Search for this sequence name to see if we've seen
194 * it already. If we've seen this sequence before,
195 * then clear the bit for this sequence from all the
196 * messages in this folder.
198 for (i
= 0; i
< svector_size (mp
->msgattrs
); i
++) {
199 if (!strcmp (svector_at (mp
->msgattrs
, i
), name
)) {
200 for (j
= mp
->lowmsg
; j
<= mp
->hghmsg
; j
++)
201 clear_sequence (mp
, i
, j
);
207 * If we've already seen this sequence name, just free the
208 * name string. Else add it to the list of sequence names.
210 if (svector_at (mp
->msgattrs
, i
)) {
213 svector_push_back (mp
->msgattrs
, name
);
217 * Split up the different message ranges at whitespace
219 for (ap
= brkstring (field
, " ", "\n"); *ap
; ap
++) {
220 if ((cp
= strchr(*ap
, '-')))
222 if ((j
= m_atoi (*ap
)) > 0) {
223 k
= cp
? m_atoi (cp
) : j
;
226 * Keep mp->curmsg and "cur" sequence in synch. Unlike
227 * other sequences, this message doesn't need to exist.
228 * Think about the series of command (rmm; next) to
229 * understand why this can be the case. But if it does
230 * exist, we will still set the bit flag for it like
236 * We iterate through messages in this range
237 * and flip on bit for this sequence.
239 for (; j
<= k
; j
++) {
240 if (j
>= mp
->lowmsg
&& j
<= mp
->hghmsg
&& does_exist(mp
, j
))
241 add_sequence (mp
, i
, j
);
246 free (field
); /* free string containing message ranges */