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 },
46 static void print_debug(struct msgs *);
47 static void seq_printdebug(struct msgs *);
51 main(int argc, char **argv)
53 int addsw = 0, deletesw = 0, debugsw = 0;
54 int listsw = 0, publicsw = -1, zerosw = 0, msgnum;
55 unsigned int seqp = 0;
56 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
57 char **argp, **arguments;
58 char *seqs[NUMATTRS + 1];
59 struct msgs_array msgs = { 0, 0, NULL };
62 setlocale(LC_ALL, "");
63 invo_name = mhbasename(argv[0]);
65 /* read user profile/context */
68 arguments = getarguments(invo_name, argc, argv, 1);
74 while ((cp = *argp++)) {
76 switch (smatch(++cp, switches)) {
78 ambigsw(cp, switches);
81 adios(EX_USAGE, NULL, "-%s unknown\n", cp);
84 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
85 print_help(buf, switches, 1);
86 exit(argc == 2 ? EX_OK : EX_USAGE);
88 print_version(invo_name);
89 exit(argc == 2 ? EX_OK : EX_USAGE);
93 deletesw = listsw = 0;
101 addsw = deletesw = 0;
105 if (!(cp = *argp++) || *cp == '-') {
106 adios(EX_USAGE, NULL, "missing argument to %s",
110 /* check if too many sequences specified */
111 if (seqp >= NUMATTRS) {
112 adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS);
136 if (*cp == '+' || *cp == '@') {
138 adios(EX_USAGE, NULL, "only one folder at a time!");
140 folder = getcpy(expandfol(cp));
143 app_msgarg(&msgs, cp);
148 ** If we haven't specified -add, -delete, or -list,
149 ** then use -add if a sequence was specified, else
152 if (!addsw && !deletesw && !listsw) {
160 app_msgarg(&msgs, listsw ? seq_all : seq_cur);
162 folder = getcurfol();
163 maildir = toabsdir(folder);
165 if (chdir(maildir) == NOTOK) {
166 adios(EX_OSERR, maildir, "unable to change directory to");
169 /* read folder and create message structure */
170 if (!(mp = folder_read(folder))) {
171 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
174 /* print some general debugging info */
178 /* check for empty folder */
179 if (mp->nummsg == 0) {
180 adios(EX_DATAERR, NULL, "no messages in %s", folder);
183 /* parse all the message ranges/sequences and set SELECTED */
184 for (msgnum = 0; msgnum < msgs.size; msgnum++) {
185 if (!m_convert(mp, msgs.msgs[msgnum])) {
190 if (publicsw == 1 && is_readonly(mp)) {
191 adios(EX_NOPERM, NULL, "folder %s is read-only, so -public not allowed",
196 ** Make sure at least one sequence has been
197 ** specified if we are adding or deleting.
199 if (seqp == 0 && (addsw || deletesw)) {
200 adios(EX_USAGE, NULL, "-%s requires at least one -sequence argument",
201 addsw ? "add" : "delete");
205 /* Adding messages to sequences */
207 for (seqp = 0; seqs[seqp]; seqp++) {
208 if (!seq_addsel(mp, seqs[seqp], publicsw, zerosw)) {
214 /* Deleting messages from sequences */
216 for (seqp = 0; seqs[seqp]; seqp++) {
217 if (!seq_delsel(mp, seqs[seqp], publicsw, zerosw)) {
223 /* Listing messages in sequences */
226 /* print the sequences given */
227 for (seqp = 0; seqs[seqp]; seqp++)
228 seq_print(mp, seqs[seqp]);
230 /* else print them all */
234 /* print debugging info about SELECTED messages */
239 seq_save(mp); /* synchronize message sequences */
240 context_replace(curfolder, folder); /* update current folder */
241 context_save(); /* save the context file */
242 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.
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, snprintb(buf, sizeof(buf), (unsigned) mp->msgstats[msgnum - mp->lowoff], seq_bits(mp)));