]>
diplodocus.org Git - nmh/blob - 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
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.
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"
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"
33 #include "sbr/m_maildir.h"
35 #define MARK_SWITCHES \
37 X("delete", 0, DELSW) \
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) \
48 #define X(sw, minchars, id) id,
49 DEFINE_SWITCH_ENUM(MARK
);
52 #define X(sw, minchars, id) { sw, minchars, id },
53 DEFINE_SWITCH_ARRAY(MARK
, switches
);
59 static void print_debug (struct msgs
*);
60 static void seq_printdebug (struct msgs
*);
64 main (int argc
, char **argv
)
67 bool deletesw
= false;
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
};
80 if (nmh_init(argv
[0], true, true)) { return 1; }
82 arguments
= getarguments (invo_name
, argc
, argv
, 1);
88 while ((cp
= *argp
++)) {
90 switch (smatch (++cp
, switches
)) {
92 ambigsw (cp
, switches
);
95 die("-%s unknown\n", cp
);
98 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
100 print_help (buf
, switches
, 1);
103 print_version(invo_name
);
123 if (!(cp
= *argp
++) || *cp
== '-')
124 die("missing argument to %s", argp
[-2]);
126 svector_push_back (seqs
, cp
);
149 if (*cp
== '+' || *cp
== '@') {
151 die("only one folder at a time!");
152 folder
= pluspath (cp
);
154 app_msgarg(&msgs
, cp
);
158 * If we haven't specified -add, -delete, or -list,
159 * then use -add if a sequence was specified, else
162 if (!addsw
&& !deletesw
&& !listsw
) {
169 if (!context_find ("path"))
170 free (path ("./", TFOLDER
));
172 app_msgarg(&msgs
, listsw
? "all" :"cur");
174 folder
= getfolder (1);
175 maildir
= m_maildir (folder
);
177 if (chdir (maildir
) == NOTOK
)
178 adios (maildir
, "unable to change directory to");
180 /* read folder and create message structure */
181 if (!(mp
= folder_read (folder
, 1)))
182 die("unable to read folder %s", folder
);
184 /* print some general debugging info */
188 /* check for empty folder */
190 die("no messages in %s", folder
);
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
]))
197 if (publicsw
== 1 && is_readonly(mp
))
198 die("folder %s is read-only, so -public not allowed", folder
);
201 * Make sure at least one sequence has been
202 * specified if we are adding or deleting.
204 if (seqp
== 0 && (addsw
|| deletesw
))
205 die("-%s requires at least one -sequence argument",
206 addsw
? "add" : "delete");
208 /* Adding messages to sequences */
210 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
211 if (!seq_addsel (mp
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
215 /* Deleting messages from sequences */
217 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
218 if (!seq_delsel (mp
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
222 /* Listing messages in sequences */
225 /* print the sequences given */
226 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
227 seq_print (mp
, svector_at (seqs
, seqp
));
229 /* else print them all */
233 /* print debugging info about SELECTED messages */
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 */
249 * Print general debugging info
252 print_debug (struct msgs
*mp
)
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
);
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.
281 seq_printdebug (struct msgs
*mp
)
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
)),