]>
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/folder_read.h"
12 #include "sbr/folder_free.h"
13 #include "sbr/context_save.h"
14 #include "sbr/context_replace.h"
15 #include "sbr/context_find.h"
16 #include "sbr/ambigsw.h"
18 #include "sbr/print_version.h"
19 #include "sbr/print_help.h"
20 #include "sbr/seq_bits.h"
21 #include "sbr/seq_del.h"
22 #include "sbr/seq_print.h"
23 #include "sbr/seq_add.h"
24 #include "sbr/error.h"
27 #include "sbr/m_maildir.h"
29 #define MARK_SWITCHES \
31 X("delete", 0, DELSW) \
33 X("sequence name", 0, SEQSW) \
34 X("public", 0, PUBLSW) \
35 X("nopublic", 0, NPUBLSW) \
36 X("zero", 0, ZEROSW) \
37 X("nozero", 0, NZEROSW) \
38 X("version", 0, VERSIONSW) \
39 X("help", 0, HELPSW) \
40 X("debug", -5, DEBUGSW) \
42 #define X(sw, minchars, id) id,
43 DEFINE_SWITCH_ENUM(MARK
);
46 #define X(sw, minchars, id) { sw, minchars, id },
47 DEFINE_SWITCH_ARRAY(MARK
, switches
);
53 static void print_debug (struct msgs
*);
54 static void seq_printdebug (struct msgs
*);
58 main (int argc
, char **argv
)
61 bool deletesw
= false;
67 unsigned int seqp
= 0;
68 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
69 char **argp
, **arguments
;
70 svector_t seqs
= svector_create (0);
71 struct msgs_array msgs
= { 0, 0, NULL
};
74 if (nmh_init(argv
[0], true, true)) { return 1; }
76 arguments
= getarguments (invo_name
, argc
, argv
, 1);
82 while ((cp
= *argp
++)) {
84 switch (smatch (++cp
, switches
)) {
86 ambigsw (cp
, switches
);
89 die("-%s unknown\n", cp
);
92 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
94 print_help (buf
, switches
, 1);
97 print_version(invo_name
);
117 if (!(cp
= *argp
++) || *cp
== '-')
118 die("missing argument to %s", argp
[-2]);
120 svector_push_back (seqs
, cp
);
143 if (*cp
== '+' || *cp
== '@') {
145 die("only one folder at a time!");
146 folder
= pluspath (cp
);
148 app_msgarg(&msgs
, cp
);
152 * If we haven't specified -add, -delete, or -list,
153 * then use -add if a sequence was specified, else
156 if (!addsw
&& !deletesw
&& !listsw
) {
163 if (!context_find ("path"))
164 free (path ("./", TFOLDER
));
166 app_msgarg(&msgs
, listsw
? "all" :"cur");
168 folder
= getfolder (1);
169 maildir
= m_maildir (folder
);
171 if (chdir (maildir
) == NOTOK
)
172 adios (maildir
, "unable to change directory to");
174 /* read folder and create message structure */
175 if (!(mp
= folder_read (folder
, 1)))
176 die("unable to read folder %s", folder
);
178 /* print some general debugging info */
182 /* check for empty folder */
184 die("no messages in %s", folder
);
186 /* parse all the message ranges/sequences and set SELECTED */
187 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
188 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
191 if (publicsw
== 1 && is_readonly(mp
))
192 die("folder %s is read-only, so -public not allowed", folder
);
195 * Make sure at least one sequence has been
196 * specified if we are adding or deleting.
198 if (seqp
== 0 && (addsw
|| deletesw
))
199 die("-%s requires at least one -sequence argument",
200 addsw
? "add" : "delete");
202 /* Adding messages to sequences */
204 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
205 if (!seq_addsel (mp
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
209 /* Deleting messages from sequences */
211 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
212 if (!seq_delsel (mp
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
216 /* Listing messages in sequences */
219 /* print the sequences given */
220 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
221 seq_print (mp
, svector_at (seqs
, seqp
));
223 /* else print them all */
227 /* print debugging info about SELECTED messages */
233 seq_save (mp
); /* synchronize message sequences */
234 context_replace (pfolder
, folder
); /* update current folder */
235 context_save (); /* save the context file */
236 folder_free (mp
); /* free folder/message structure */
243 * Print general debugging info
246 print_debug (struct msgs
*mp
)
250 printf ("invo_name = %s\n", invo_name
);
251 printf ("mypath = %s\n", mypath
);
252 printf ("defpath = %s\n", defpath
);
253 printf ("ctxpath = %s\n", ctxpath
);
254 printf ("context flags = %s\n", snprintb (buf
, sizeof(buf
),
255 (unsigned) ctxflags
, DBITS
));
256 printf ("foldpath = %s\n", mp
->foldpath
);
257 printf ("folder flags = %s\n\n", snprintb(buf
, sizeof(buf
),
258 (unsigned) mp
->msgflags
, FBITS
));
259 printf ("lowmsg=%d hghmsg=%d nummsg=%d curmsg=%d\n",
260 mp
->lowmsg
, mp
->hghmsg
, mp
->nummsg
, mp
->curmsg
);
261 printf ("lowsel=%d hghsel=%d numsel=%d\n",
262 mp
->lowsel
, mp
->hghsel
, mp
->numsel
);
263 printf ("lowoff=%d hghoff=%d\n\n", mp
->lowoff
, mp
->hghoff
);
268 * Print debugging info about all the SELECTED
269 * messages and the sequences they are in.
270 * Due to limitations of snprintb(), only a limited
271 * number of sequences will be printed. See the
272 * comments in sbr/seq_bits.c.
275 seq_printdebug (struct msgs
*mp
)
281 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
282 if (is_selected (mp
, msgnum
))
283 printf ("%*d: %s\n", DMAXFOLDER
, msgnum
,
284 snprintb (buf
, sizeof buf
,
285 (unsigned) bvector_first_bits (msgstat (mp
, msgnum
)),