3 * anno.c -- annotate messages
11 * We allocate space for messages (msgs array)
12 * this number of elements at a time.
17 static struct swit switches[] = {
19 { "component field", 0 },
40 static void make_comp (char **);
44 main (int argc, char **argv)
46 int inplace = 1, datesw = 1;
47 int nummsgs, maxmsgs, msgnum;
48 char *cp, *maildir, *comp = NULL;
49 char *text = NULL, *folder = NULL, buf[BUFSIZ];
50 char **argp, **arguments, **msgs;
54 setlocale(LC_ALL, "");
56 invo_name = r1bindex (argv[0], '/');
58 /* read user profile/context */
61 arguments = getarguments (invo_name, argc, argv, 1);
65 * Allocate the initial space to record message
70 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
71 adios (NULL, "unable to allocate storage");
73 while ((cp = *argp++)) {
75 switch (smatch (++cp, switches)) {
77 ambigsw (cp, switches);
80 adios (NULL, "-%s unknown", cp);
83 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
85 print_help (buf, switches, 1);
88 print_version(invo_name);
93 adios (NULL, "only one component at a time!");
94 if (!(comp = *argp++) || *comp == '-')
95 adios (NULL, "missing argument to %s", argp[-2]);
114 adios (NULL, "only one body at a time!");
115 if (!(text = *argp++) || *text == '-')
116 adios (NULL, "missing argument to %s", argp[-2]);
120 if (*cp == '+' || *cp == '@') {
122 adios (NULL, "only one folder at a time!");
124 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
127 * Check if we need to allocate more space
128 * for message name/ranges.
130 if (nummsgs >= maxmsgs) {
132 if (!(msgs = (char **) realloc (msgs,
133 (size_t) (maxmsgs * sizeof(*msgs)))))
134 adios (NULL, "unable to reallocate msgs storage");
136 msgs[nummsgs++] = cp;
141 if (strcmp(invo_name, "fanno") == 0) /* ugh! */
145 if (!context_find ("path"))
146 free (path ("./", TFOLDER));
148 msgs[nummsgs++] = "cur";
150 folder = getfolder (1);
151 maildir = m_maildir (folder);
153 if (chdir (maildir) == NOTOK)
154 adios (maildir, "unable to change directory to");
156 /* read folder and create message structure */
157 if (!(mp = folder_read (folder)))
158 adios (NULL, "unable to read folder %s", folder);
160 /* check for empty folder */
162 adios (NULL, "no messages in %s", folder);
164 /* parse all the message ranges/sequences and set SELECTED */
165 for (msgnum = 0; msgnum < nummsgs; msgnum++)
166 if (!m_convert (mp, msgs[msgnum]))
171 /* annotate all the SELECTED messages */
172 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
173 if (is_selected(mp, msgnum))
174 annotate (m_name (msgnum), comp, text, inplace, datesw);
176 context_replace (pfolder, folder); /* update current folder */
177 seq_setcur (mp, mp->lowsel); /* update current message */
178 seq_save (mp); /* synchronize message sequences */
179 folder_free (mp); /* free folder/message structure */
180 context_save (); /* save the context file */
186 make_comp (char **ap)
192 printf ("Enter component name: ");
195 if (fgets (buffer, sizeof buffer, stdin) == NULL)
197 *ap = trimcpy (buffer);
200 if ((cp = *ap + strlen (*ap) - 1) > *ap && *cp == ':')
202 if (strlen (*ap) == 0)
203 adios (NULL, "null component name");
205 adios (NULL, "invalid component name %s", *ap);
206 if (strlen (*ap) >= NAMESZ)
207 adios (NULL, "too large component name %s", *ap);
209 for (cp = *ap; *cp; cp++)
210 if (!isalnum (*cp) && *cp != '-')
211 adios (NULL, "invalid component name %s", *ap);