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