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