3 * pick.c -- search for messages by content
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.
14 #include <h/picksbr.h>
17 * We allocate space for messages (msgs array)
18 * this number of elements at a time.
23 static struct swit switches[] = {
37 { "date pattern", 0 },
39 { "from pattern", 0 },
41 { "search pattern", 0 },
43 { "subject pattern", 0 },
47 { "-othercomponent pattern", 0 },
53 { "datefield field", 5 },
55 { "sequence name", 0 },
75 static int listsw = -1;
79 main (int argc, char **argv)
81 int publicsw = -1, zerosw = 1, seqp = 0, vecp = 0;
82 int nummsgs, maxmsgs, lo, hi, msgnum;
83 char *maildir, *folder = NULL, buf[100];
84 char *cp, **argp, **arguments;
85 char **msgs, *seqs[NUMATTRS + 1], *vec[MAXARGS];
90 setlocale(LC_ALL, "");
92 invo_name = r1bindex (argv[0], '/');
94 /* read user profile/context */
97 arguments = getarguments (invo_name, argc, argv, 1);
101 * Allocate the initial space to record message
102 * names, ranges, and sequences.
106 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
107 adios (NULL, "unable to allocate storage");
109 while ((cp = *argp++)) {
115 switch (smatch (cp, switches)) {
117 ambigsw (cp, switches);
118 listsw = 0; /* HACK */
121 adios (NULL, "-%s unknown", cp);
124 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
126 print_help (buf, switches, 1);
127 listsw = 0; /* HACK */
130 print_version(invo_name);
131 listsw = 0; /* HACK */
145 if (!(cp = *argp++))/* allow -xyz arguments */
146 adios (NULL, "missing argument to %s", argp[-2]);
150 adios (NULL, "internal error!");
161 if (!(cp = *argp++) || *cp == '-')
162 adios (NULL, "missing argument to %s", argp[-2]);
164 /* check if too many sequences specified */
165 if (seqp >= NUMATTRS)
166 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
190 if (*cp == '+' || *cp == '@') {
192 adios (NULL, "only one folder at a time!");
194 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
197 * Check if we need to allocate more space
198 * for message name/ranges/sequences.
200 if (nummsgs >= maxmsgs) {
202 if (!(msgs = (char **) realloc (msgs,
203 (size_t) (maxmsgs * sizeof(*msgs)))))
204 adios (NULL, "unable to reallocate msgs storage");
206 msgs[nummsgs++] = cp;
211 if (!context_find ("path"))
212 free (path ("./", TFOLDER));
215 * If we didn't specify which messages to search,
216 * then search the whole folder.
219 msgs[nummsgs++] = "all";
222 folder = getfolder (1);
223 maildir = m_maildir (folder);
225 if (chdir (maildir) == NOTOK)
226 adios (maildir, "unable to change directory to");
228 /* read folder and create message structure */
229 if (!(mp = folder_read (folder)))
230 adios (NULL, "unable to read folder %s", folder);
232 /* check for empty folder */
234 adios (NULL, "no messages in %s", folder);
236 /* parse all the message ranges/sequences and set SELECTED */
237 for (msgnum = 0; msgnum < nummsgs; msgnum++)
238 if (!m_convert (mp, msgs[msgnum]))
240 seq_setprev (mp); /* set the previous-sequence */
243 * If we aren't saving the results to a sequence,
244 * we default to list the results.
249 if (publicsw == 1 && is_readonly(mp))
250 adios (NULL, "folder %s is read-only, so -public not allowed", folder);
252 if (!pcompile (vec, NULL))
259 * Scan through all the SELECTED messages and check for a
260 * match. If the message does not match, then unselect it.
262 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
263 if (is_selected (mp, msgnum)) {
264 if ((fp = fopen (cp = m_name (msgnum), "r")) == NULL)
265 admonish (cp, "unable to read message");
266 if (fp && pmatches (fp, msgnum, 0L, 0L)) {
272 /* if it doesn't match, then unselect it */
273 unset_selected (mp, msgnum);
285 adios (NULL, "no messages match specification");
290 * Add the matching messages to sequences
292 for (seqp = 0; seqs[seqp]; seqp++)
293 if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
297 * Print the name of all the matches
300 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
301 if (is_selected (mp, msgnum))
302 printf ("%s\n", m_name (msgnum));
304 printf ("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
307 context_replace (pfolder, folder); /* update current folder */
308 seq_save (mp); /* synchronize message sequences */
309 context_save (); /* save the context file */
310 folder_free (mp); /* free folder/message structure */
318 if (listsw && status && !isatty (fileno (stdout)))
321 return 1; /* dead code to satisfy the compiler */