2 ** mark.c -- add message(s) to sequences in given folder
3 ** -- delete messages (s) from sequences in given folder
4 ** -- list sequences in given folder
6 ** This code is Copyright (c) 2002, by the authors of nmh. See the
7 ** COPYRIGHT file in the root directory of the nmh distribution for
8 ** complete copyright information.
17 static struct swit switches[] = {
25 { "sequence name", 0 },
43 char *version=VERSION;
48 static void print_debug(struct msgs *);
49 static void seq_printdebug(struct msgs *);
53 main(int argc, char **argv)
55 int addsw = 0, deletesw = 0, debugsw = 0;
56 int listsw = 0, publicsw = -1, zerosw = 0, msgnum;
57 unsigned int seqp = 0;
58 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
59 char **argp, **arguments;
60 char *seqs[NUMATTRS + 1];
61 struct msgs_array msgs = { 0, 0, NULL };
64 setlocale(LC_ALL, "");
65 invo_name = mhbasename(argv[0]);
67 /* read user profile/context */
70 arguments = getarguments(invo_name, argc, argv, 1);
76 while ((cp = *argp++)) {
78 switch (smatch(++cp, switches)) {
80 ambigsw(cp, switches);
83 adios(EX_USAGE, NULL, "-%s unknown\n", cp);
86 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
87 print_help(buf, switches, 1);
88 exit(argc == 2 ? EX_OK : EX_USAGE);
90 print_version(invo_name);
91 exit(argc == 2 ? EX_OK : EX_USAGE);
95 deletesw = listsw = 0;
103 addsw = deletesw = 0;
107 if (!(cp = *argp++) || *cp == '-') {
108 adios(EX_USAGE, NULL, "missing argument to %s",
112 /* check if too many sequences specified */
113 if (seqp >= NUMATTRS) {
114 adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS);
138 if (*cp == '+' || *cp == '@') {
140 adios(EX_USAGE, NULL, "only one folder at a time!");
142 folder = mh_xstrdup(expandfol(cp));
145 app_msgarg(&msgs, cp);
150 ** If we haven't specified -add, -delete, or -list,
151 ** then use -add if a sequence was specified, else
154 if (!addsw && !deletesw && !listsw) {
162 app_msgarg(&msgs, listsw ? seq_all : seq_cur);
164 folder = getcurfol();
165 maildir = toabsdir(folder);
167 if (chdir(maildir) == NOTOK) {
168 adios(EX_OSERR, maildir, "unable to change directory to");
171 /* read folder and create message structure */
172 if (!(mp = folder_read(folder))) {
173 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
176 /* print some general debugging info */
180 /* check for empty folder */
181 if (mp->nummsg == 0) {
182 adios(EX_DATAERR, NULL, "no messages in %s", folder);
185 /* parse all the message ranges/sequences and set SELECTED */
186 for (msgnum = 0; msgnum < msgs.size; msgnum++) {
187 if (!m_convert(mp, msgs.msgs[msgnum])) {
192 if (publicsw == 1 && is_readonly(mp)) {
193 adios(EX_NOPERM, NULL, "folder %s is read-only, so -public not allowed",
198 ** Make sure at least one sequence has been
199 ** specified if we are adding or deleting.
201 if (seqp == 0 && (addsw || deletesw)) {
202 adios(EX_USAGE, NULL, "-%s requires at least one -sequence argument",
203 addsw ? "add" : "delete");
207 /* Adding messages to sequences */
209 for (seqp = 0; seqs[seqp]; seqp++) {
210 if (!seq_addsel(mp, seqs[seqp], publicsw, zerosw)) {
216 /* Deleting messages from sequences */
218 for (seqp = 0; seqs[seqp]; seqp++) {
219 if (!seq_delsel(mp, seqs[seqp], publicsw, zerosw)) {
225 /* Listing messages in sequences */
228 /* print the sequences given */
229 for (seqp = 0; seqs[seqp]; seqp++)
230 seq_print(mp, seqs[seqp]);
232 /* else print them all */
236 /* print debugging info about SELECTED messages */
241 seq_save(mp); /* synchronize message sequences */
242 context_replace(curfolder, folder); /* update current folder */
243 context_save(); /* save the context file */
244 folder_free(mp); /* free folder/message structure */
250 ** Print general debugging info
253 print_debug(struct msgs *mp)
257 printf("invo_name = %s\n", invo_name);
258 printf("mypath = %s\n", mypath);
259 printf("defpath = %s\n", defpath);
260 printf("ctxpath = %s\n", ctxpath);
261 printf("context flags = %s\n", snprintb(buf, sizeof(buf),
262 (unsigned) ctxflags, DBITS));
263 printf("foldpath = %s\n", mp->foldpath);
264 printf("folder flags = %s\n\n", snprintb(buf, sizeof(buf),
265 (unsigned) mp->msgflags, FBITS));
266 printf("lowmsg=%d hghmsg=%d nummsg=%d curmsg=%d\n",
267 mp->lowmsg, mp->hghmsg, mp->nummsg, mp->curmsg);
268 printf("lowsel=%d hghsel=%d numsel=%d\n",
269 mp->lowsel, mp->hghsel, mp->numsel);
270 printf("lowoff=%d hghoff=%d\n\n", mp->lowoff, mp->hghoff);
275 ** Print debugging info about all the SELECTED
276 ** messages and the sequences they are in.
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, snprintb(buf, sizeof(buf), (unsigned) mp->msgstats[msgnum - mp->lowoff], seq_bits(mp)));