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