3 * pick.c -- search for messages by content
5 * This code is Copyright (c) 2002, 2008, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/picksbr.h>
15 static struct swit switches[] = {
29 { "date pattern", 0 },
31 { "from pattern", 0 },
33 { "search pattern", 0 },
35 { "subject pattern", 0 },
39 { "-othercomponent pattern", 0 },
45 { "datefield field", 5 },
47 { "sequence name", 0 },
67 static int listsw = -1;
69 static void putzero_done (int) NORETURN;
72 main (int argc, char **argv)
74 int publicsw = -1, zerosw = 1, vecp = 0;
77 char *maildir, *folder = NULL, buf[100];
78 char *cp, **argp, **arguments;
79 char *seqs[NUMATTRS + 1], *vec[MAXARGS];
80 struct msgs_array msgs = { 0, 0, NULL };
87 setlocale(LC_ALL, "");
89 invo_name = r1bindex (argv[0], '/');
91 /* read user profile/context */
94 arguments = getarguments (invo_name, argc, argv, 1);
97 while ((cp = *argp++)) {
103 switch (smatch (cp, switches)) {
105 ambigsw (cp, switches);
106 listsw = 0; /* HACK */
109 adios (NULL, "-%s unknown", cp);
112 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
114 print_help (buf, switches, 1);
115 listsw = 0; /* HACK */
118 print_version(invo_name);
119 listsw = 0; /* HACK */
133 if (!(cp = *argp++))/* allow -xyz arguments */
134 adios (NULL, "missing argument to %s", argp[-2]);
138 adios (NULL, "internal error!");
149 if (!(cp = *argp++) || *cp == '-')
150 adios (NULL, "missing argument to %s", argp[-2]);
152 /* check if too many sequences specified */
153 if (seqp >= NUMATTRS)
154 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
156 if (!seq_nameok (cp))
182 if (*cp == '+' || *cp == '@') {
184 adios (NULL, "only one folder at a time!");
186 folder = pluspath (cp);
188 app_msgarg(&msgs, cp);
192 if (!context_find ("path"))
193 free (path ("./", TFOLDER));
196 * If we didn't specify which messages to search,
197 * then search the whole folder.
200 app_msgarg(&msgs, "all");
203 folder = getfolder (1);
204 maildir = m_maildir (folder);
206 if (chdir (maildir) == NOTOK)
207 adios (maildir, "unable to change directory to");
209 /* read folder and create message structure */
210 if (!(mp = folder_read (folder)))
211 adios (NULL, "unable to read folder %s", folder);
213 /* check for empty folder */
215 adios (NULL, "no messages in %s", folder);
217 /* parse all the message ranges/sequences and set SELECTED */
218 for (msgnum = 0; msgnum < msgs.size; msgnum++)
219 if (!m_convert (mp, msgs.msgs[msgnum]))
221 seq_setprev (mp); /* set the previous-sequence */
224 * If we aren't saving the results to a sequence,
225 * we default to list the results.
230 if (publicsw == 1 && is_readonly(mp))
231 adios (NULL, "folder %s is read-only, so -public not allowed", folder);
233 if (!pcompile (vec, NULL))
239 /* If printing message numbers to standard out, force line buffering on.
242 setvbuf (stdout, NULL, _IOLBF, 0);
245 * Scan through all the SELECTED messages and check for a
246 * match. If the message does not match, then unselect it.
248 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
249 if (is_selected (mp, msgnum)) {
250 if ((fp = fopen (cp = m_name (msgnum), "r")) == NULL)
251 admonish (cp, "unable to read message");
252 if (fp && pmatches (fp, msgnum, 0L, 0L)) {
259 printf ("%s\n", m_name (msgnum));
261 /* if it doesn't match, then unselect it */
262 unset_selected (mp, msgnum);
274 adios (NULL, "no messages match specification");
279 * Add the matching messages to sequences
281 for (seqp = 0; seqs[seqp]; seqp++)
282 if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
286 * Print total matched if not printing each matched message number.
289 printf ("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
292 context_replace (pfolder, folder); /* update current folder */
293 seq_save (mp); /* synchronize message sequences */
294 context_save (); /* save the context file */
295 folder_free (mp); /* free folder/message structure */
302 putzero_done (int status)
304 if (listsw && status && !isatty (fileno (stdout)))