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