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 = 0;
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);
120 adios (NULL, "-%s unknown", cp);
123 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
125 print_help (buf, switches, 1);
126 listsw = 0; /* HACK */
129 print_version(invo_name);
143 if (!(cp = *argp++))/* allow -xyz arguments */
144 adios (NULL, "missing argument to %s", argp[-2]);
148 adios (NULL, "internal error!");
159 if (!(cp = *argp++) || *cp == '-')
160 adios (NULL, "missing argument to %s", argp[-2]);
162 /* check if too many sequences specified */
163 if (seqp >= NUMATTRS)
164 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
189 if (*cp == '+' || *cp == '@') {
191 adios (NULL, "only one folder at a time!");
193 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
196 * Check if we need to allocate more space
197 * for message name/ranges/sequences.
199 if (nummsgs >= maxmsgs) {
201 if (!(msgs = (char **) realloc (msgs,
202 (size_t) (maxmsgs * sizeof(*msgs)))))
203 adios (NULL, "unable to reallocate msgs storage");
205 msgs[nummsgs++] = cp;
210 if (!context_find ("path"))
211 free (path ("./", TFOLDER));
214 * If we didn't specify which messages to search,
215 * then search the whole folder.
218 msgs[nummsgs++] = "all";
221 folder = getfolder (1);
222 maildir = m_maildir (folder);
224 if (chdir (maildir) == NOTOK)
225 adios (maildir, "unable to change directory to");
227 /* read folder and create message structure */
228 if (!(mp = folder_read (folder)))
229 adios (NULL, "unable to read folder %s", folder);
231 /* check for empty folder */
233 adios (NULL, "no messages in %s", folder);
235 /* parse all the message ranges/sequences and set SELECTED */
236 for (msgnum = 0; msgnum < nummsgs; msgnum++)
237 if (!m_convert (mp, msgs[msgnum]))
239 seq_setprev (mp); /* set the previous-sequence */
242 * If we aren't saving the results to a sequence,
243 * we need to list the results.
248 if (publicsw == 1 && is_readonly(mp))
249 adios (NULL, "folder %s is read-only, so -public not allowed", folder);
251 if (!pcompile (vec, NULL))
258 * Scan through all the SELECTED messages and check for a
259 * match. If the message does not match, then unselect it.
261 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
262 if (is_selected (mp, msgnum)) {
263 if ((fp = fopen (cp = m_name (msgnum), "r")) == NULL)
264 admonish (cp, "unable to read message");
265 if (fp && pmatches (fp, msgnum, 0L, 0L)) {
271 /* if it doesn't match, then unselect it */
272 unset_selected (mp, msgnum);
284 adios (NULL, "no messages match specification");
289 * Add the matching messages to sequences
291 for (seqp = 0; seqs[seqp]; seqp++)
292 if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
296 * Print the name of all the matches
299 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
300 if (is_selected (mp, msgnum))
301 printf ("%s\n", m_name (msgnum));
303 printf ("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
306 context_replace (pfolder, folder); /* update current folder */
307 seq_save (mp); /* synchronize message sequences */
308 context_save (); /* save the context file */
309 folder_free (mp); /* free folder/message structure */
317 if (listsw && status && !isatty (fileno (stdout)))
320 return 1; /* dead code to satisfy the compiler */