]>
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/seq_save.h"
12 #include "sbr/smatch.h"
13 #include "sbr/snprintb.h"
14 #include "sbr/m_convert.h"
15 #include "sbr/getfolder.h"
16 #include "sbr/folder_read.h"
17 #include "sbr/folder_free.h"
18 #include "sbr/context_save.h"
19 #include "sbr/context_replace.h"
20 #include "sbr/context_find.h"
21 #include "sbr/ambigsw.h"
23 #include "sbr/print_version.h"
24 #include "sbr/print_help.h"
25 #include "sbr/seq_bits.h"
26 #include "sbr/seq_del.h"
27 #include "sbr/seq_print.h"
28 #include "sbr/seq_add.h"
29 #include "sbr/error.h"
32 #include "sbr/m_maildir.h"
34 #define MARK_SWITCHES \
36 X("delete", 0, DELSW) \
38 X("sequence name", 0, SEQSW) \
39 X("public", 0, PUBLSW) \
40 X("nopublic", 0, NPUBLSW) \
41 X("zero", 0, ZEROSW) \
42 X("nozero", 0, NZEROSW) \
43 X("version", 0, VERSIONSW) \
44 X("help", 0, HELPSW) \
45 X("debug", -5, DEBUGSW) \
47 #define X(sw, minchars, id) id,
48 DEFINE_SWITCH_ENUM(MARK
);
51 #define X(sw, minchars, id) { sw, minchars, id },
52 DEFINE_SWITCH_ARRAY(MARK
, switches
);
58 static void print_debug (struct msgs
*);
59 static void seq_printdebug (struct msgs
*);
63 main (int argc
, char **argv
)
66 bool deletesw
= false;
72 unsigned int seqp
= 0;
73 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
74 char **argp
, **arguments
;
75 svector_t seqs
= svector_create (0);
76 struct msgs_array msgs
= { 0, 0, NULL
};
79 if (nmh_init(argv
[0], true, true)) { return 1; }
81 arguments
= getarguments (invo_name
, argc
, argv
, 1);
87 while ((cp
= *argp
++)) {
89 switch (smatch (++cp
, switches
)) {
91 ambigsw (cp
, switches
);
94 die("-%s unknown\n", cp
);
97 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
99 print_help (buf
, switches
, 1);
102 print_version(invo_name
);
122 if (!(cp
= *argp
++) || *cp
== '-')
123 die("missing argument to %s", argp
[-2]);
125 svector_push_back (seqs
, cp
);
148 if (*cp
== '+' || *cp
== '@') {
150 die("only one folder at a time!");
151 folder
= pluspath (cp
);
153 app_msgarg(&msgs
, cp
);
157 * If we haven't specified -add, -delete, or -list,
158 * then use -add if a sequence was specified, else
161 if (!addsw
&& !deletesw
&& !listsw
) {
168 if (!context_find ("path"))
169 free (path ("./", TFOLDER
));
171 app_msgarg(&msgs
, listsw
? "all" :"cur");
173 folder
= getfolder (1);
174 maildir
= m_maildir (folder
);
176 if (chdir (maildir
) == NOTOK
)
177 adios (maildir
, "unable to change directory to");
179 /* read folder and create message structure */
180 if (!(mp
= folder_read (folder
, 1)))
181 die("unable to read folder %s", folder
);
183 /* print some general debugging info */
187 /* check for empty folder */
189 die("no messages in %s", folder
);
191 /* parse all the message ranges/sequences and set SELECTED */
192 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
193 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
196 if (publicsw
== 1 && is_readonly(mp
))
197 die("folder %s is read-only, so -public not allowed", folder
);
200 * Make sure at least one sequence has been
201 * specified if we are adding or deleting.
203 if (seqp
== 0 && (addsw
|| deletesw
))
204 die("-%s requires at least one -sequence argument",
205 addsw
? "add" : "delete");
207 /* Adding messages to sequences */
209 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
210 if (!seq_addsel (mp
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
214 /* Deleting messages from sequences */
216 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
217 if (!seq_delsel (mp
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
221 /* Listing messages in sequences */
224 /* print the sequences given */
225 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
226 seq_print (mp
, svector_at (seqs
, seqp
));
228 /* else print them all */
232 /* print debugging info about SELECTED messages */
238 seq_save (mp
); /* synchronize message sequences */
239 context_replace (pfolder
, folder
); /* update current folder */
240 context_save (); /* save the context file */
241 folder_free (mp
); /* free folder/message structure */
248 * Print general debugging info
251 print_debug (struct msgs
*mp
)
255 printf ("invo_name = %s\n", invo_name
);
256 printf ("mypath = %s\n", mypath
);
257 printf ("defpath = %s\n", defpath
);
258 printf ("ctxpath = %s\n", ctxpath
);
259 printf ("context flags = %s\n", snprintb (buf
, sizeof(buf
),
260 (unsigned) ctxflags
, DBITS
));
261 printf ("foldpath = %s\n", mp
->foldpath
);
262 printf ("folder flags = %s\n\n", snprintb(buf
, sizeof(buf
),
263 (unsigned) mp
->msgflags
, FBITS
));
264 printf ("lowmsg=%d hghmsg=%d nummsg=%d curmsg=%d\n",
265 mp
->lowmsg
, mp
->hghmsg
, mp
->nummsg
, mp
->curmsg
);
266 printf ("lowsel=%d hghsel=%d numsel=%d\n",
267 mp
->lowsel
, mp
->hghsel
, mp
->numsel
);
268 printf ("lowoff=%d hghoff=%d\n\n", mp
->lowoff
, mp
->hghoff
);
273 * Print debugging info about all the SELECTED
274 * messages and the sequences they are in.
275 * Due to limitations of snprintb(), only a limited
276 * number of sequences will be printed. See the
277 * comments in sbr/seq_bits.c.
280 seq_printdebug (struct msgs
*mp
)
286 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
287 if (is_selected (mp
, msgnum
))
288 printf ("%*d: %s\n", DMAXFOLDER
, msgnum
,
289 snprintb (buf
, sizeof buf
,
290 (unsigned) bvector_first_bits (msgstat (mp
, msgnum
)),