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