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
13 * We allocate space for messages (msgs array)
14 * this number of elements at a time.
19 static struct swit switches[] = {
27 { "sequence name", 0 },
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;
57 int seqp = 0, msgnum, nummsgs, maxmsgs;
58 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
59 char **argp, **arguments;
60 char *seqs[NUMATTRS + 1], **msgs;
64 setlocale(LC_ALL, "");
66 invo_name = r1bindex (argv[0], '/');
68 /* read user profile/context */
71 arguments = getarguments (invo_name, argc, argv, 1);
75 * Allocate the initial space to record message
76 * names, ranges, and sequences.
80 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
81 adios (NULL, "unable to allocate storage");
86 while ((cp = *argp++)) {
88 switch (smatch (++cp, switches)) {
90 ambigsw (cp, switches);
93 adios (NULL, "-%s unknown\n", cp);
96 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
98 print_help (buf, switches, 1);
101 print_version(invo_name);
106 deletesw = listsw = 0;
114 addsw = deletesw = 0;
118 if (!(cp = *argp++) || *cp == '-')
119 adios (NULL, "missing argument to %s", argp[-2]);
121 /* check if too many sequences specified */
122 if (seqp >= NUMATTRS)
123 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
146 if (*cp == '+' || *cp == '@') {
148 adios (NULL, "only one folder at a time!");
150 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
153 * Check if we need to allocate more space
154 * for message names/ranges/sequences.
156 if (nummsgs >= maxmsgs) {
158 if (!(msgs = (char **) realloc (msgs,
159 (size_t) (maxmsgs * sizeof(*msgs)))))
160 adios (NULL, "unable to reallocate msgs storage");
162 msgs[nummsgs++] = cp;
167 * If we haven't specified -add, -delete, or -list,
168 * then use -add if a sequence was specified, else
171 if (!addsw && !deletesw && !listsw) {
178 if (!context_find ("path"))
179 free (path ("./", TFOLDER));
181 msgs[nummsgs++] = listsw ? "all" :"cur";
183 folder = getfolder (1);
184 maildir = m_maildir (folder);
186 if (chdir (maildir) == NOTOK)
187 adios (maildir, "unable to change directory to");
189 /* read folder and create message structure */
190 if (!(mp = folder_read (folder)))
191 adios (NULL, "unable to read folder %s", folder);
193 /* print some general debugging info */
197 /* check for empty folder */
199 adios (NULL, "no messages in %s", folder);
201 /* parse all the message ranges/sequences and set SELECTED */
202 for (msgnum = 0; msgnum < nummsgs; msgnum++)
203 if (!m_convert (mp, msgs[msgnum]))
206 if (publicsw == 1 && is_readonly(mp))
207 adios (NULL, "folder %s is read-only, so -public not allowed", folder);
210 * Make sure at least one sequence has been
211 * specified if we are adding or deleting.
213 if (seqp == 0 && (addsw || deletesw))
214 adios (NULL, "-%s requires at least one -sequence argument",
215 addsw ? "add" : "delete");
218 /* Adding messages to sequences */
220 for (seqp = 0; seqs[seqp]; seqp++)
221 if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
225 /* Deleting messages from sequences */
227 for (seqp = 0; seqs[seqp]; seqp++)
228 if (!seq_delsel (mp, seqs[seqp], publicsw, zerosw))
232 /* Listing messages in sequences */
235 /* print the sequences given */
236 for (seqp = 0; seqs[seqp]; seqp++)
237 seq_print (mp, seqs[seqp]);
239 /* else print them all */
243 /* print debugging info about SELECTED messages */
248 seq_save (mp); /* synchronize message sequences */
249 context_replace (pfolder, folder); /* update current folder */
250 context_save (); /* save the context file */
251 folder_free (mp); /* free folder/message structure */
257 * Print general debugging info
260 print_debug (struct msgs *mp)
264 printf ("invo_name = %s\n", invo_name);
265 printf ("mypath = %s\n", mypath);
266 printf ("defpath = %s\n", defpath);
267 printf ("ctxpath = %s\n", ctxpath);
268 printf ("context flags = %s\n", snprintb (buf, sizeof(buf),
269 (unsigned) ctxflags, DBITS));
270 printf ("foldpath = %s\n", mp->foldpath);
271 printf ("folder flags = %s\n\n", snprintb(buf, sizeof(buf),
272 (unsigned) mp->msgflags, FBITS));
273 printf ("lowmsg=%d hghmsg=%d nummsg=%d curmsg=%d\n",
274 mp->lowmsg, mp->hghmsg, mp->nummsg, mp->curmsg);
275 printf ("lowsel=%d hghsel=%d numsel=%d\n",
276 mp->lowsel, mp->hghsel, mp->numsel);
277 printf ("lowoff=%d hghoff=%d\n\n", mp->lowoff, mp->hghoff);
282 * Print debugging info about all the SELECTED
283 * messages and the sequences they are in.
286 seq_printdebug (struct msgs *mp)
292 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
293 if (is_selected (mp, msgnum))
294 printf ("%*d: %s\n", DMAXFOLDER, msgnum,
295 snprintb (buf, sizeof(buf),
296 (unsigned) mp->msgstats[msgnum - mp->lowoff], seq_bits (mp)));