]>
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/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"
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"
29 #include "sbr/m_maildir.h"
31 #define MARK_SWITCHES \
33 X("delete", 0, DELSW) \
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) \
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(MARK
);
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(MARK
, switches
);
55 static void print_debug (struct msgs
*);
56 static void seq_printdebug (struct msgs
*);
60 main (int argc
, char **argv
)
63 bool deletesw
= false;
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
};
76 if (nmh_init(argv
[0], true, true)) { return 1; }
78 arguments
= getarguments (invo_name
, argc
, argv
, 1);
84 while ((cp
= *argp
++)) {
86 switch (smatch (++cp
, switches
)) {
88 ambigsw (cp
, switches
);
91 die("-%s unknown\n", cp
);
94 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
96 print_help (buf
, switches
, 1);
99 print_version(invo_name
);
119 if (!(cp
= *argp
++) || *cp
== '-')
120 die("missing argument to %s", argp
[-2]);
122 svector_push_back (seqs
, cp
);
145 if (*cp
== '+' || *cp
== '@') {
147 die("only one folder at a time!");
148 folder
= pluspath (cp
);
150 app_msgarg(&msgs
, cp
);
154 * If we haven't specified -add, -delete, or -list,
155 * then use -add if a sequence was specified, else
158 if (!addsw
&& !deletesw
&& !listsw
) {
165 if (!context_find ("path"))
166 free (path ("./", TFOLDER
));
168 app_msgarg(&msgs
, listsw
? "all" :"cur");
170 folder
= getfolder (1);
171 maildir
= m_maildir (folder
);
173 if (chdir (maildir
) == NOTOK
)
174 adios (maildir
, "unable to change directory to");
176 /* read folder and create message structure */
177 if (!(mp
= folder_read (folder
, 1)))
178 die("unable to read folder %s", folder
);
180 /* print some general debugging info */
184 /* check for empty folder */
186 die("no messages in %s", folder
);
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
]))
193 if (publicsw
== 1 && is_readonly(mp
))
194 die("folder %s is read-only, so -public not allowed", folder
);
197 * Make sure at least one sequence has been
198 * specified if we are adding or deleting.
200 if (seqp
== 0 && (addsw
|| deletesw
))
201 die("-%s requires at least one -sequence argument",
202 addsw
? "add" : "delete");
204 /* Adding messages to sequences */
206 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
207 if (!seq_addsel (mp
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
211 /* Deleting messages from sequences */
213 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
214 if (!seq_delsel (mp
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
218 /* Listing messages in sequences */
221 /* print the sequences given */
222 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
223 seq_print (mp
, svector_at (seqs
, seqp
));
225 /* else print them all */
229 /* print debugging info about SELECTED messages */
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 */
245 * Print general debugging info
248 print_debug (struct msgs
*mp
)
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
);
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.
277 seq_printdebug (struct msgs
*mp
)
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
)),