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