Fixed "mark -sequence cur -delete all" to work for cur as
[mmh] / sbr / seq_add.c
1
2 /*
3  * seq_add.c -- add message(s) to a sequence
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
12
13 /*
14  * Add all the SELECTED messages to a (possibly new) sequence.
15  *
16  * If public ==  1, make sequence public.
17  * If public ==  0, make sequence private.
18  * If public == -1, leave the public/private bit alone for existing
19  *                  sequences.  For new sequences, set this bit based
20  *                  on its readonly status.
21  *
22  * If error, return 0, else return 1.
23  */
24
25 int
26 seq_addsel (struct msgs *mp, char *cp, int public, int zero)
27 {
28     unsigned int i;
29     int msgnum, new_seq = 1;
30
31     if (!seq_nameok (cp))
32         return 0;
33
34     /*
35      * We keep mp->curmsg and "cur" sequence in sync.
36      * See seq_list() and seq_init().
37      */
38     if (!strcmp (current,cp))
39         mp->curmsg = mp->hghsel;
40
41     /*
42      * Get the number for this sequence
43      */
44     for (i = 0; mp->msgattrs[i]; i++) {
45         if (!strcmp (mp->msgattrs[i], cp)) {
46             new_seq = 0;
47             break;
48         }
49     }
50
51     /*
52      * If this is a new sequence, add a slot for it
53      */
54     if (new_seq) {
55         if (i >= NUMATTRS) {
56             advise (NULL, "only %d sequences allowed (no room for %s)!", NUMATTRS, cp);
57             return 0;
58         }
59         if (!(mp->msgattrs[i] = strdup (cp))) {
60             advise (NULL, "strdup failed");
61             return 0;
62         }
63         mp->msgattrs[i + 1] = NULL;
64     }
65
66     /*
67      * If sequence is new, or zero flag is set, then first
68      * clear the bit for this sequence from all messages.
69      */
70     if ((new_seq || zero) && mp->nummsg > 0) {
71         for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
72             clear_sequence (mp, i, msgnum);
73     }
74
75     /*
76      * Now flip on the bit for this sequence
77      * for all selected messages.
78      */
79     for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
80         if (is_selected (mp, msgnum))
81             add_sequence (mp, i, msgnum);
82
83     /*
84      * Set the public/private bit for this sequence.
85      */
86     if (public == 1)
87         make_seq_public (mp, i);
88     else if (public == 0)
89         make_seq_private (mp, i);
90     else if (new_seq) {
91         /*
92          * If public == -1, then only set the
93          * public/private bit for new sequences.
94          */
95         if (is_readonly (mp))
96             make_seq_private (mp, i);
97         else
98             make_seq_public (mp, i);
99     }
100
101     mp->msgflags |= SEQMOD;
102     return 1;
103 }
104
105
106 /*
107  * Add a message to a (possibly new) sequence.
108  *
109  * If public ==  1, make sequence public.
110  * If public ==  0, make sequence private.
111  * If public == -1, leave the public/private bit alone for existing
112  *                  sequences.  For new sequences, set this bit based
113  *                  on its readonly status.
114  *
115  * If error, return 0, else return 1.
116  */
117
118 int
119 seq_addmsg (struct msgs *mp, char *cp, int msgnum, int public, int zero)
120 {
121     unsigned int i;
122     int 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) && mp->nummsg > 0) {
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 }