]>
diplodocus.org Git - nmh/blob - uip/mark.c
4485cedc9f03804dabbe787539ec998f61794e8a
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/smatch.h"
12 #include "sbr/snprintb.h"
13 #include "sbr/m_convert.h"
14 #include "sbr/getfolder.h"
15 #include "sbr/folder_read.h"
16 #include "sbr/folder_free.h"
17 #include "sbr/context_save.h"
18 #include "sbr/context_replace.h"
19 #include "sbr/context_find.h"
20 #include "sbr/ambigsw.h"
22 #include "sbr/print_version.h"
23 #include "sbr/print_help.h"
24 #include "sbr/seq_bits.h"
25 #include "sbr/seq_del.h"
26 #include "sbr/seq_print.h"
27 #include "sbr/seq_add.h"
28 #include "sbr/error.h"
31 #include "sbr/m_maildir.h"
33 #define MARK_SWITCHES \
35 X("delete", 0, DELSW) \
37 X("sequence name", 0, SEQSW) \
38 X("public", 0, PUBLSW) \
39 X("nopublic", 0, NPUBLSW) \
40 X("zero", 0, ZEROSW) \
41 X("nozero", 0, NZEROSW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
44 X("debug", -5, DEBUGSW) \
46 #define X(sw, minchars, id) id,
47 DEFINE_SWITCH_ENUM(MARK
);
50 #define X(sw, minchars, id) { sw, minchars, id },
51 DEFINE_SWITCH_ARRAY(MARK
, switches
);
57 static void print_debug (struct msgs
*);
58 static void seq_printdebug (struct msgs
*);
62 main (int argc
, char **argv
)
65 bool deletesw
= false;
71 unsigned int seqp
= 0;
72 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
73 char **argp
, **arguments
;
74 svector_t seqs
= svector_create (0);
75 struct msgs_array msgs
= { 0, 0, NULL
};
78 if (nmh_init(argv
[0], true, true)) { return 1; }
80 arguments
= getarguments (invo_name
, argc
, argv
, 1);
86 while ((cp
= *argp
++)) {
88 switch (smatch (++cp
, switches
)) {
90 ambigsw (cp
, switches
);
93 die("-%s unknown\n", cp
);
96 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
98 print_help (buf
, switches
, 1);
101 print_version(invo_name
);
121 if (!(cp
= *argp
++) || *cp
== '-')
122 die("missing argument to %s", argp
[-2]);
124 svector_push_back (seqs
, cp
);
147 if (*cp
== '+' || *cp
== '@') {
149 die("only one folder at a time!");
150 folder
= pluspath (cp
);
152 app_msgarg(&msgs
, cp
);
156 * If we haven't specified -add, -delete, or -list,
157 * then use -add if a sequence was specified, else
160 if (!addsw
&& !deletesw
&& !listsw
) {
167 if (!context_find ("path"))
168 free (path ("./", TFOLDER
));
170 app_msgarg(&msgs
, listsw
? "all" :"cur");
172 folder
= getfolder (1);
173 maildir
= m_maildir (folder
);
175 if (chdir (maildir
) == NOTOK
)
176 adios (maildir
, "unable to change directory to");
178 /* read folder and create message structure */
179 if (!(mp
= folder_read (folder
, 1)))
180 die("unable to read folder %s", folder
);
182 /* print some general debugging info */
186 /* check for empty folder */
188 die("no messages in %s", folder
);
190 /* parse all the message ranges/sequences and set SELECTED */
191 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
192 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
195 if (publicsw
== 1 && is_readonly(mp
))
196 die("folder %s is read-only, so -public not allowed", folder
);
199 * Make sure at least one sequence has been
200 * specified if we are adding or deleting.
202 if (seqp
== 0 && (addsw
|| deletesw
))
203 die("-%s requires at least one -sequence argument",
204 addsw
? "add" : "delete");
206 /* Adding messages to sequences */
208 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
209 if (!seq_addsel (mp
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
213 /* Deleting messages from sequences */
215 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
216 if (!seq_delsel (mp
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
220 /* Listing messages in sequences */
223 /* print the sequences given */
224 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
225 seq_print (mp
, svector_at (seqs
, seqp
));
227 /* else print them all */
231 /* print debugging info about SELECTED messages */
237 seq_save (mp
); /* synchronize message sequences */
238 context_replace (pfolder
, folder
); /* update current folder */
239 context_save (); /* save the context file */
240 folder_free (mp
); /* free folder/message structure */
247 * Print general debugging info
250 print_debug (struct msgs
*mp
)
254 printf ("invo_name = %s\n", invo_name
);
255 printf ("mypath = %s\n", mypath
);
256 printf ("defpath = %s\n", defpath
);
257 printf ("ctxpath = %s\n", ctxpath
);
258 printf ("context flags = %s\n", snprintb (buf
, sizeof(buf
),
259 (unsigned) ctxflags
, DBITS
));
260 printf ("foldpath = %s\n", mp
->foldpath
);
261 printf ("folder flags = %s\n\n", snprintb(buf
, sizeof(buf
),
262 (unsigned) mp
->msgflags
, FBITS
));
263 printf ("lowmsg=%d hghmsg=%d nummsg=%d curmsg=%d\n",
264 mp
->lowmsg
, mp
->hghmsg
, mp
->nummsg
, mp
->curmsg
);
265 printf ("lowsel=%d hghsel=%d numsel=%d\n",
266 mp
->lowsel
, mp
->hghsel
, mp
->numsel
);
267 printf ("lowoff=%d hghoff=%d\n\n", mp
->lowoff
, mp
->hghoff
);
272 * Print debugging info about all the SELECTED
273 * messages and the sequences they are in.
274 * Due to limitations of snprintb(), only a limited
275 * number of sequences will be printed. See the
276 * comments in sbr/seq_bits.c.
279 seq_printdebug (struct msgs
*mp
)
285 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
286 if (is_selected (mp
, msgnum
))
287 printf ("%*d: %s\n", DMAXFOLDER
, msgnum
,
288 snprintb (buf
, sizeof buf
,
289 (unsigned) bvector_first_bits (msgstat (mp
, msgnum
)),