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