Add/update copyright notice in all source code files.
[mmh] / sbr / seq_add.c
1
2 /*
3  * seq_add.c -- add message(s) to a sequence
4  *
5  * $Id$
6  *
7  * This code is Copyright (c) 2002, by the authors of nmh.  See the
8  * COPYRIGHT file in the root directory of the nmh distribution for
9  * complete copyright information.
10  */
11
12 #include <h/mh.h>
13
14
15 /*
16  * Add all the SELECTED messages to a (possibly new) sequence.
17  *
18  * If public ==  1, make sequence public.
19  * If public ==  0, make sequence private.
20  * If public == -1, leave the public/private bit alone for existing
21  *                  sequences.  For new sequences, set this bit based
22  *                  on its readonly status.
23  *
24  * If error, return 0, else return 1.
25  */
26
27 int
28 seq_addsel (struct msgs *mp, char *cp, int public, int zero)
29 {
30     int i, msgnum, new_seq = 1;
31
32     if (!seq_nameok (cp))
33         return 0;
34
35     /*
36      * We keep mp->curmsg and "cur" sequence in sync.
37      * See seq_list() and seq_init().
38      */
39     if (!strcmp (current,cp))
40         mp->curmsg = mp->hghsel;
41
42     /*
43      * Get the number for this sequence
44      */
45     for (i = 0; mp->msgattrs[i]; i++) {
46         if (!strcmp (mp->msgattrs[i], cp)) {
47             new_seq = 0;
48             break;
49         }
50     }
51
52     /*
53      * If this is a new sequence, add a slot for it
54      */
55     if (new_seq) {
56         if (i >= NUMATTRS) {
57             advise (NULL, "only %d sequences allowed (no room for %s)!", NUMATTRS, cp);
58             return 0;
59         }
60         if (!(mp->msgattrs[i] = strdup (cp))) {
61             advise (NULL, "strdup failed");
62             return 0;
63         }
64         mp->msgattrs[i + 1] = NULL;
65     }
66
67     /*
68      * If sequence is new, or zero flag is set, then first
69      * clear the bit for this sequence from all messages.
70      */
71     if (new_seq || zero) {
72         for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
73             clear_sequence (mp, i, msgnum);
74     }
75
76     /*
77      * Now flip on the bit for this sequence
78      * for all selected messages.
79      */
80     for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
81         if (is_selected (mp, msgnum))
82             add_sequence (mp, i, msgnum);
83
84     /*
85      * Set the public/private bit for this sequence.
86      */
87     if (public == 1)
88         make_seq_public (mp, i);
89     else if (public == 0)
90         make_seq_private (mp, i);
91     else if (new_seq) {
92         /*
93          * If public == -1, then only set the
94          * public/private bit for new sequences.
95          */
96         if (is_readonly (mp))
97             make_seq_private (mp, i);
98         else
99             make_seq_public (mp, i);
100     }
101
102     mp->msgflags |= SEQMOD;
103     return 1;
104 }
105
106
107 /*
108  * Add a message to a (possibly new) sequence.
109  *
110  * If public ==  1, make sequence public.
111  * If public ==  0, make sequence private.
112  * If public == -1, leave the public/private bit alone for existing
113  *                  sequences.  For new sequences, set this bit based
114  *                  on its readonly status.
115  *
116  * If error, return 0, else return 1.
117  */
118
119 int
120 seq_addmsg (struct msgs *mp, char *cp, int msgnum, int public, int zero)
121 {
122     int i, j, new_seq = 1;
123
124     if (!seq_nameok (cp))
125         return 0;
126
127     /*
128      * keep mp->curmsg and msgattrs["cur"] in sync - see seq_list()
129      */
130     if (!strcmp (current,cp))
131         mp->curmsg = msgnum;    
132
133     /*
134      * Get the number for this sequence
135      */
136     for (i = 0; mp->msgattrs[i]; i++) {
137         if (!strcmp (mp->msgattrs[i], cp)) {
138             new_seq = 0;
139             break;
140         }
141     }
142
143     /*
144      * If this is a new sequence, add a slot for it
145      */
146     if (new_seq) {
147         if (i >= NUMATTRS) {
148             advise (NULL, "only %d sequences allowed (no room for %s)!", NUMATTRS, cp);
149             return 0;
150         }
151         if (!(mp->msgattrs[i] = strdup (cp))) {
152             advise (NULL, "strdup failed");
153             return 0;
154         }
155         mp->msgattrs[i + 1] = NULL;
156     }
157
158     /*
159      * If sequence is new, or zero flag is set, then first
160      * clear the bit for this sequence from all messages.
161      */
162     if (new_seq || zero) {
163         for (j = mp->lowmsg; j <= mp->hghmsg; j++)
164             clear_sequence (mp, i, j);
165     }
166
167     /*
168      * Now flip on the bit for this sequence
169      * for this particular message.
170      */
171     add_sequence (mp, i, msgnum);
172
173     /*
174      * Set the public/private bit for this sequence.
175      */
176     if (public == 1)
177         make_seq_public (mp, i);
178     else if (public == 0)
179         make_seq_private (mp, i);
180     else if (new_seq) {
181         /*
182          * If public == -1, then only set the
183          * public/private bit for new sequences.
184          */
185         if (is_readonly (mp))
186             make_seq_private (mp, i);
187         else
188             make_seq_public (mp, i);
189     }
190
191     mp->msgflags |= SEQMOD;
192     return 1;
193 }