3 * pick.c -- search for messages by content
9 #include <zotnet/tws/tws.h>
10 #include <h/picksbr.h>
13 * We allocate space for messages (msgs array)
14 * this number of elements at a time.
19 static struct swit switches[] = {
33 { "date pattern", 0 },
35 { "from pattern", 0 },
37 { "search pattern", 0 },
39 { "subject pattern", 0 },
43 { "-othercomponent pattern", 0 },
49 { "datefield field", 5 },
51 { "sequence name", 0 },
71 static int listsw = 0;
75 main (int argc, char **argv)
77 int publicsw = -1, zerosw = 1, seqp = 0, vecp = 0;
78 int nummsgs, maxmsgs, lo, hi, msgnum;
79 char *maildir, *folder = NULL, buf[100];
80 char *cp, **argp, **arguments;
81 char **msgs, *seqs[NUMATTRS + 1], *vec[MAXARGS];
86 setlocale(LC_ALL, "");
88 invo_name = r1bindex (argv[0], '/');
90 /* read user profile/context */
93 arguments = getarguments (invo_name, argc, argv, 1);
97 * Allocate the initial space to record message
98 * names, ranges, and sequences.
102 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
103 adios (NULL, "unable to allocate storage");
105 while ((cp = *argp++)) {
111 switch (smatch (cp, switches)) {
113 ambigsw (cp, switches);
116 adios (NULL, "-%s unknown", cp);
119 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
121 print_help (buf, switches, 1);
122 listsw = 0; /* HACK */
125 print_version(invo_name);
139 if (!(cp = *argp++))/* allow -xyz arguments */
140 adios (NULL, "missing argument to %s", argp[-2]);
144 adios (NULL, "internal error!");
155 if (!(cp = *argp++) || *cp == '-')
156 adios (NULL, "missing argument to %s", argp[-2]);
158 /* check if too many sequences specified */
159 if (seqp >= NUMATTRS)
160 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
185 if (*cp == '+' || *cp == '@') {
187 adios (NULL, "only one folder at a time!");
189 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
192 * Check if we need to allocate more space
193 * for message name/ranges/sequences.
195 if (nummsgs >= maxmsgs) {
197 if (!(msgs = (char **) realloc (msgs,
198 (size_t) (maxmsgs * sizeof(*msgs)))))
199 adios (NULL, "unable to reallocate msgs storage");
201 msgs[nummsgs++] = cp;
206 if (!context_find ("path"))
207 free (path ("./", TFOLDER));
210 * If we didn't specify which messages to search,
211 * then search the whole folder.
214 msgs[nummsgs++] = "all";
217 folder = getfolder (1);
218 maildir = m_maildir (folder);
220 if (chdir (maildir) == NOTOK)
221 adios (maildir, "unable to change directory to");
223 /* read folder and create message structure */
224 if (!(mp = folder_read (folder)))
225 adios (NULL, "unable to read folder %s", folder);
227 /* check for empty folder */
229 adios (NULL, "no messages in %s", folder);
231 /* parse all the message ranges/sequences and set SELECTED */
232 for (msgnum = 0; msgnum < nummsgs; msgnum++)
233 if (!m_convert (mp, msgs[msgnum]))
235 seq_setprev (mp); /* set the previous-sequence */
238 * If we aren't saving the results to a sequence,
239 * we need to list the results.
244 if (publicsw == 1 && is_readonly(mp))
245 adios (NULL, "folder %s is read-only, so -public not allowed", folder);
247 if (!pcompile (vec, NULL))
254 * Scan through all the SELECTED messages and check for a
255 * match. If the message does not match, then unselect it.
257 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
258 if (is_selected (mp, msgnum)) {
259 if ((fp = fopen (cp = m_name (msgnum), "r")) == NULL)
260 admonish (cp, "unable to read message");
261 if (fp && pmatches (fp, msgnum, 0L, 0L)) {
267 /* if it doesn't match, then unselect it */
268 unset_selected (mp, msgnum);
280 adios (NULL, "no messages match specification");
285 * Add the matching messages to sequences
287 for (seqp = 0; seqs[seqp]; seqp++)
288 if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
292 * Print the name of all the matches
295 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
296 if (is_selected (mp, msgnum))
297 printf ("%s\n", m_name (msgnum));
299 printf ("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
302 context_replace (pfolder, folder); /* update current folder */
303 seq_save (mp); /* synchronize message sequences */
304 context_save (); /* save the context file */
305 folder_free (mp); /* free folder/message structure */
313 if (listsw && status && !isatty (fileno (stdout)))