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