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