3 * mark.c -- add message(s) to sequences in given folder
4 * -- delete messages (s) from sequences in given folder
5 * -- list sequences in given folder
9 * This code is Copyright (c) 2002, by the authors of nmh. See the
10 * COPYRIGHT file in the root directory of the nmh distribution for
11 * complete copyright information.
17 * We allocate space for messages (msgs array)
18 * this number of elements at a time.
23 static struct swit switches[] = {
31 { "sequence name", 0 },
52 static void print_debug (struct msgs *);
53 static void seq_printdebug (struct msgs *);
57 main (int argc, char **argv)
59 int addsw = 0, deletesw = 0, debugsw = 0;
60 int listsw = 0, publicsw = -1, zerosw = 0;
61 int seqp = 0, msgnum, nummsgs, maxmsgs;
62 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
63 char **argp, **arguments;
64 char *seqs[NUMATTRS + 1], **msgs;
68 setlocale(LC_ALL, "");
70 invo_name = r1bindex (argv[0], '/');
72 /* read user profile/context */
75 arguments = getarguments (invo_name, argc, argv, 1);
79 * Allocate the initial space to record message
80 * names, ranges, and sequences.
84 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
85 adios (NULL, "unable to allocate storage");
90 while ((cp = *argp++)) {
92 switch (smatch (++cp, switches)) {
94 ambigsw (cp, switches);
97 adios (NULL, "-%s unknown\n", cp);
100 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
102 print_help (buf, switches, 1);
105 print_version(invo_name);
110 deletesw = listsw = 0;
118 addsw = deletesw = 0;
122 if (!(cp = *argp++) || *cp == '-')
123 adios (NULL, "missing argument to %s", argp[-2]);
125 /* check if too many sequences specified */
126 if (seqp >= NUMATTRS)
127 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
150 if (*cp == '+' || *cp == '@') {
152 adios (NULL, "only one folder at a time!");
154 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
157 * Check if we need to allocate more space
158 * for message names/ranges/sequences.
160 if (nummsgs >= maxmsgs) {
162 if (!(msgs = (char **) realloc (msgs,
163 (size_t) (maxmsgs * sizeof(*msgs)))))
164 adios (NULL, "unable to reallocate msgs storage");
166 msgs[nummsgs++] = cp;
171 * If we haven't specified -add, -delete, or -list,
172 * then use -add if a sequence was specified, else
175 if (!addsw && !deletesw && !listsw) {
182 if (!context_find ("path"))
183 free (path ("./", TFOLDER));
185 msgs[nummsgs++] = listsw ? "all" :"cur";
187 folder = getfolder (1);
188 maildir = m_maildir (folder);
190 if (chdir (maildir) == NOTOK)
191 adios (maildir, "unable to change directory to");
193 /* read folder and create message structure */
194 if (!(mp = folder_read (folder)))
195 adios (NULL, "unable to read folder %s", folder);
197 /* print some general debugging info */
201 /* check for empty folder */
203 adios (NULL, "no messages in %s", folder);
205 /* parse all the message ranges/sequences and set SELECTED */
206 for (msgnum = 0; msgnum < nummsgs; msgnum++)
207 if (!m_convert (mp, msgs[msgnum]))
210 if (publicsw == 1 && is_readonly(mp))
211 adios (NULL, "folder %s is read-only, so -public not allowed", folder);
214 * Make sure at least one sequence has been
215 * specified if we are adding or deleting.
217 if (seqp == 0 && (addsw || deletesw))
218 adios (NULL, "-%s requires at least one -sequence argument",
219 addsw ? "add" : "delete");
222 /* Adding messages to sequences */
224 for (seqp = 0; seqs[seqp]; seqp++)
225 if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
229 /* Deleting messages from sequences */
231 for (seqp = 0; seqs[seqp]; seqp++)
232 if (!seq_delsel (mp, seqs[seqp], publicsw, zerosw))
236 /* Listing messages in sequences */
239 /* print the sequences given */
240 for (seqp = 0; seqs[seqp]; seqp++)
241 seq_print (mp, seqs[seqp]);
243 /* else print them all */
247 /* print debugging info about SELECTED messages */
252 seq_save (mp); /* synchronize message sequences */
253 context_replace (pfolder, folder); /* update current folder */
254 context_save (); /* save the context file */
255 folder_free (mp); /* free folder/message structure */
261 * Print general debugging info
264 print_debug (struct msgs *mp)
268 printf ("invo_name = %s\n", invo_name);
269 printf ("mypath = %s\n", mypath);
270 printf ("defpath = %s\n", defpath);
271 printf ("ctxpath = %s\n", ctxpath);
272 printf ("context flags = %s\n", snprintb (buf, sizeof(buf),
273 (unsigned) ctxflags, DBITS));
274 printf ("foldpath = %s\n", mp->foldpath);
275 printf ("folder flags = %s\n\n", snprintb(buf, sizeof(buf),
276 (unsigned) mp->msgflags, FBITS));
277 printf ("lowmsg=%d hghmsg=%d nummsg=%d curmsg=%d\n",
278 mp->lowmsg, mp->hghmsg, mp->nummsg, mp->curmsg);
279 printf ("lowsel=%d hghsel=%d numsel=%d\n",
280 mp->lowsel, mp->hghsel, mp->numsel);
281 printf ("lowoff=%d hghoff=%d\n\n", mp->lowoff, mp->hghoff);
286 * Print debugging info about all the SELECTED
287 * messages and the sequences they are in.
290 seq_printdebug (struct msgs *mp)
296 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
297 if (is_selected (mp, msgnum))
298 printf ("%*d: %s\n", DMAXFOLDER, msgnum,
299 snprintb (buf, sizeof(buf),
300 (unsigned) mp->msgstats[msgnum - mp->lowoff], seq_bits (mp)));