Removed the mhlproc profile entry. Call mhl directly.
[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(seq_cur, cp)==0)
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)==0) {
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[] of seq_cur in sync - see seq_list()
126         */
127         if (strcmp(seq_cur, cp)==0)
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)==0) {
135                         new_seq = 0;
136                         break;
137                 }
138         }
139
140         if (new_seq) {
141                 /* If this is a new sequence, add a slot for it */
142                 if (i >= NUMATTRS) {
143                         advise(NULL, "only %d sequences allowed (no room for %s)!", NUMATTRS, cp);
144                         return 0;
145                 }
146                 if (!(mp->msgattrs[i] = strdup(cp))) {
147                         advise(NULL, "strdup failed");
148                         return 0;
149                 }
150                 mp->msgattrs[i + 1] = NULL;
151         }
152
153         if (mp->nummsg>0 && (new_seq || zero)) {
154                 /* Clear the bit for this sequence from all messages. */
155                 for (j = mp->lowmsg; j <= mp->hghmsg; j++)
156                         clear_sequence(mp, i, j);
157         }
158
159         /* Set the bit for this sequence for this particular message. */
160         add_sequence(mp, i, msgnum);
161
162         /* Set the public/private bit for this sequence. */
163         if (public == 1)
164                 make_seq_public(mp, i);
165         else if (public == 0)
166                 make_seq_private(mp, i);
167         else if (new_seq) {
168                 /*
169                 ** If public == -1, then only set the
170                 ** public/private bit for new sequences.
171                 */
172                 if (is_readonly(mp))
173                         make_seq_private(mp, i);
174                 else
175                         make_seq_public(mp, i);
176         }
177
178         mp->msgflags |= SEQMOD;
179         return 1;
180 }