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