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