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