3 * anno.c -- annotate messages
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
15 * We allocate space for messages (msgs array)
16 * this number of elements at a time.
21 static struct swit switches[] = {
23 { "component field", 0 },
44 static void make_comp (char **);
48 main (int argc, char **argv)
50 int inplace = 1, datesw = 1;
51 int nummsgs, maxmsgs, msgnum;
52 char *cp, *maildir, *comp = NULL;
53 char *text = NULL, *folder = NULL, buf[BUFSIZ];
54 char **argp, **arguments, **msgs;
58 setlocale(LC_ALL, "");
60 invo_name = r1bindex (argv[0], '/');
62 /* read user profile/context */
65 arguments = getarguments (invo_name, argc, argv, 1);
69 * Allocate the initial space to record message
74 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
75 adios (NULL, "unable to allocate storage");
77 while ((cp = *argp++)) {
79 switch (smatch (++cp, switches)) {
81 ambigsw (cp, switches);
84 adios (NULL, "-%s unknown", cp);
87 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
89 print_help (buf, switches, 1);
92 print_version(invo_name);
97 adios (NULL, "only one component at a time!");
98 if (!(comp = *argp++) || *comp == '-')
99 adios (NULL, "missing argument to %s", argp[-2]);
118 adios (NULL, "only one body at a time!");
119 if (!(text = *argp++) || *text == '-')
120 adios (NULL, "missing argument to %s", argp[-2]);
124 if (*cp == '+' || *cp == '@') {
126 adios (NULL, "only one folder at a time!");
128 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
131 * Check if we need to allocate more space
132 * for message name/ranges.
134 if (nummsgs >= maxmsgs) {
136 if (!(msgs = (char **) realloc (msgs,
137 (size_t) (maxmsgs * sizeof(*msgs)))))
138 adios (NULL, "unable to reallocate msgs storage");
140 msgs[nummsgs++] = cp;
145 if (strcmp(invo_name, "fanno") == 0) /* ugh! */
149 if (!context_find ("path"))
150 free (path ("./", TFOLDER));
152 msgs[nummsgs++] = "cur";
154 folder = getfolder (1);
155 maildir = m_maildir (folder);
157 if (chdir (maildir) == NOTOK)
158 adios (maildir, "unable to change directory to");
160 /* read folder and create message structure */
161 if (!(mp = folder_read (folder)))
162 adios (NULL, "unable to read folder %s", folder);
164 /* check for empty folder */
166 adios (NULL, "no messages in %s", folder);
168 /* parse all the message ranges/sequences and set SELECTED */
169 for (msgnum = 0; msgnum < nummsgs; msgnum++)
170 if (!m_convert (mp, msgs[msgnum]))
175 /* annotate all the SELECTED messages */
176 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
177 if (is_selected(mp, msgnum))
178 annotate (m_name (msgnum), comp, text, inplace, datesw);
180 context_replace (pfolder, folder); /* update current folder */
181 seq_setcur (mp, mp->lowsel); /* update current message */
182 seq_save (mp); /* synchronize message sequences */
183 folder_free (mp); /* free folder/message structure */
184 context_save (); /* save the context file */
190 make_comp (char **ap)
196 printf ("Enter component name: ");
199 if (fgets (buffer, sizeof buffer, stdin) == NULL)
201 *ap = trimcpy (buffer);
204 if ((cp = *ap + strlen (*ap) - 1) > *ap && *cp == ':')
206 if (strlen (*ap) == 0)
207 adios (NULL, "null component name");
209 adios (NULL, "invalid component name %s", *ap);
210 if (strlen (*ap) >= NAMESZ)
211 adios (NULL, "too large component name %s", *ap);
213 for (cp = *ap; *cp; cp++)
214 if (!isalnum (*cp) && *cp != '-')
215 adios (NULL, "invalid component name %s", *ap);