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