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