]> diplodocus.org Git - nmh/blob - uip/mark.c
Use va_copy() to get a copy of va_list, instead of using original.
[nmh] / uip / mark.c
1 /* mark.c -- add message(s) to sequences in given folder
2 * -- delete messages (s) from sequences in given folder
3 * -- list sequences in given folder
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/done.h"
12 #include <h/utils.h>
13 #include "sbr/m_maildir.h"
14
15 #define MARK_SWITCHES \
16 X("add", 0, ADDSW) \
17 X("delete", 0, DELSW) \
18 X("list", 0, LSTSW) \
19 X("sequence name", 0, SEQSW) \
20 X("public", 0, PUBLSW) \
21 X("nopublic", 0, NPUBLSW) \
22 X("zero", 0, ZEROSW) \
23 X("nozero", 0, NZEROSW) \
24 X("version", 0, VERSIONSW) \
25 X("help", 0, HELPSW) \
26 X("debug", -5, DEBUGSW) \
27
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(MARK);
30 #undef X
31
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(MARK, switches);
34 #undef X
35
36 /*
37 * static prototypes
38 */
39 static void print_debug (struct msgs *);
40 static void seq_printdebug (struct msgs *);
41
42
43 int
44 main (int argc, char **argv)
45 {
46 bool addsw = false;
47 bool deletesw = false;
48 bool debugsw = false;
49 bool listsw = false;
50 int publicsw = -1;
51 bool zerosw = false;
52 int msgnum;
53 unsigned int seqp = 0;
54 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
55 char **argp, **arguments;
56 svector_t seqs = svector_create (0);
57 struct msgs_array msgs = { 0, 0, NULL };
58 struct msgs *mp;
59
60 if (nmh_init(argv[0], true, true)) { return 1; }
61
62 arguments = getarguments (invo_name, argc, argv, 1);
63 argp = arguments;
64
65 /*
66 * Parse arguments
67 */
68 while ((cp = *argp++)) {
69 if (*cp == '-') {
70 switch (smatch (++cp, switches)) {
71 case AMBIGSW:
72 ambigsw (cp, switches);
73 done (1);
74 case UNKWNSW:
75 die("-%s unknown\n", cp);
76
77 case HELPSW:
78 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
79 invo_name);
80 print_help (buf, switches, 1);
81 done (0);
82 case VERSIONSW:
83 print_version(invo_name);
84 done (0);
85
86 case ADDSW:
87 addsw = true;
88 deletesw = false;
89 listsw = false;
90 continue;
91 case DELSW:
92 deletesw = true;
93 addsw = false;
94 listsw = false;
95 continue;
96 case LSTSW:
97 listsw = true;
98 addsw = false;
99 deletesw = false;
100 continue;
101
102 case SEQSW:
103 if (!(cp = *argp++) || *cp == '-')
104 die("missing argument to %s", argp[-2]);
105
106 svector_push_back (seqs, cp);
107 seqp++;
108 continue;
109
110 case PUBLSW:
111 publicsw = 1;
112 continue;
113 case NPUBLSW:
114 publicsw = 0;
115 continue;
116
117 case DEBUGSW:
118 debugsw = true;
119 continue;
120
121 case ZEROSW:
122 zerosw = true;
123 continue;
124 case NZEROSW:
125 zerosw = false;
126 continue;
127 }
128 }
129 if (*cp == '+' || *cp == '@') {
130 if (folder)
131 die("only one folder at a time!");
132 folder = pluspath (cp);
133 } else
134 app_msgarg(&msgs, cp);
135 }
136
137 /*
138 * If we haven't specified -add, -delete, or -list,
139 * then use -add if a sequence was specified, else
140 * use -list.
141 */
142 if (!addsw && !deletesw && !listsw) {
143 if (seqp)
144 addsw = true;
145 else
146 listsw = true;
147 }
148
149 if (!context_find ("path"))
150 free (path ("./", TFOLDER));
151 if (!msgs.size)
152 app_msgarg(&msgs, listsw ? "all" :"cur");
153 if (!folder)
154 folder = getfolder (1);
155 maildir = m_maildir (folder);
156
157 if (chdir (maildir) == NOTOK)
158 adios (maildir, "unable to change directory to");
159
160 /* read folder and create message structure */
161 if (!(mp = folder_read (folder, 1)))
162 die("unable to read folder %s", folder);
163
164 /* print some general debugging info */
165 if (debugsw)
166 print_debug(mp);
167
168 /* check for empty folder */
169 if (mp->nummsg == 0)
170 die("no messages in %s", folder);
171
172 /* parse all the message ranges/sequences and set SELECTED */
173 for (msgnum = 0; msgnum < msgs.size; msgnum++)
174 if (!m_convert (mp, msgs.msgs[msgnum]))
175 done (1);
176
177 if (publicsw == 1 && is_readonly(mp))
178 die("folder %s is read-only, so -public not allowed", folder);
179
180 /*
181 * Make sure at least one sequence has been
182 * specified if we are adding or deleting.
183 */
184 if (seqp == 0 && (addsw || deletesw))
185 die("-%s requires at least one -sequence argument",
186 addsw ? "add" : "delete");
187
188 /* Adding messages to sequences */
189 if (addsw) {
190 for (seqp = 0; seqp < svector_size (seqs); seqp++)
191 if (!seq_addsel (mp, svector_at (seqs, seqp), publicsw, zerosw))
192 done (1);
193 }
194
195 /* Deleting messages from sequences */
196 if (deletesw) {
197 for (seqp = 0; seqp < svector_size (seqs); seqp++)
198 if (!seq_delsel (mp, svector_at (seqs, seqp), publicsw, zerosw))
199 done (1);
200 }
201
202 /* Listing messages in sequences */
203 if (listsw) {
204 if (seqp) {
205 /* print the sequences given */
206 for (seqp = 0; seqp < svector_size (seqs); seqp++)
207 seq_print (mp, svector_at (seqs, seqp));
208 } else {
209 /* else print them all */
210 seq_printall (mp);
211 }
212
213 /* print debugging info about SELECTED messages */
214 if (debugsw)
215 seq_printdebug (mp);
216 }
217
218 svector_free (seqs);
219 seq_save (mp); /* synchronize message sequences */
220 context_replace (pfolder, folder); /* update current folder */
221 context_save (); /* save the context file */
222 folder_free (mp); /* free folder/message structure */
223 done (0);
224 return 1;
225 }
226
227
228 /*
229 * Print general debugging info
230 */
231 static void
232 print_debug (struct msgs *mp)
233 {
234 char buf[100];
235
236 printf ("invo_name = %s\n", invo_name);
237 printf ("mypath = %s\n", mypath);
238 printf ("defpath = %s\n", defpath);
239 printf ("ctxpath = %s\n", ctxpath);
240 printf ("context flags = %s\n", snprintb (buf, sizeof(buf),
241 (unsigned) ctxflags, DBITS));
242 printf ("foldpath = %s\n", mp->foldpath);
243 printf ("folder flags = %s\n\n", snprintb(buf, sizeof(buf),
244 (unsigned) mp->msgflags, FBITS));
245 printf ("lowmsg=%d hghmsg=%d nummsg=%d curmsg=%d\n",
246 mp->lowmsg, mp->hghmsg, mp->nummsg, mp->curmsg);
247 printf ("lowsel=%d hghsel=%d numsel=%d\n",
248 mp->lowsel, mp->hghsel, mp->numsel);
249 printf ("lowoff=%d hghoff=%d\n\n", mp->lowoff, mp->hghoff);
250 }
251
252
253 /*
254 * Print debugging info about all the SELECTED
255 * messages and the sequences they are in.
256 * Due to limitations of snprintb(), only a limited
257 * number of sequences will be printed. See the
258 * comments in sbr/seq_bits.c.
259 */
260 static void
261 seq_printdebug (struct msgs *mp)
262 {
263 int msgnum;
264 char buf[BUFSIZ];
265
266 putchar('\n');
267 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
268 if (is_selected (mp, msgnum))
269 printf ("%*d: %s\n", DMAXFOLDER, msgnum,
270 snprintb (buf, sizeof buf,
271 (unsigned) bvector_first_bits (msgstat (mp, msgnum)),
272 seq_bits (mp)));
273 }
274 }