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 },
69 static int listsw = -1;
71 static void putzero_done (int) NORETURN;
74 main (int argc, char **argv)
76 int publicsw = -1, zerosw = 1, vecp = 0;
79 char *maildir, *folder = NULL, buf[100];
80 char *cp, **argp, **arguments;
81 char *seqs[NUMATTRS + 1], *vec[MAXARGS];
82 struct msgs_array msgs = { 0, 0, NULL };
89 setlocale(LC_ALL, "");
91 invo_name = r1bindex (argv[0], '/');
93 /* read user profile/context */
96 arguments = getarguments (invo_name, argc, argv, 1);
99 while ((cp = *argp++)) {
105 switch (smatch (cp, switches)) {
107 ambigsw (cp, switches);
108 listsw = 0; /* HACK */
111 adios (NULL, "-%s unknown", cp);
114 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
116 print_help (buf, switches, 1);
117 listsw = 0; /* HACK */
120 print_version(invo_name);
121 listsw = 0; /* HACK */
135 if (!(cp = *argp++))/* allow -xyz arguments */
136 adios (NULL, "missing argument to %s", argp[-2]);
140 adios (NULL, "internal error!");
151 if (!(cp = *argp++) || *cp == '-')
152 adios (NULL, "missing argument to %s", argp[-2]);
154 /* check if too many sequences specified */
155 if (seqp >= NUMATTRS)
156 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
158 if (!seq_nameok (cp))
187 if (*cp == '+' || *cp == '@') {
189 adios (NULL, "only one folder at a time!");
191 folder = pluspath (cp);
193 app_msgarg(&msgs, cp);
197 if (!context_find ("path"))
198 free (path ("./", TFOLDER));
201 * If we didn't specify which messages to search,
202 * then search the whole folder.
205 app_msgarg(&msgs, "all");
208 folder = getfolder (1);
209 maildir = m_maildir (folder);
211 if (chdir (maildir) == NOTOK)
212 adios (maildir, "unable to change directory to");
214 /* read folder and create message structure */
215 if (!(mp = folder_read (folder)))
216 adios (NULL, "unable to read folder %s", folder);
218 /* check for empty folder */
220 adios (NULL, "no messages in %s", folder);
222 /* parse all the message ranges/sequences and set SELECTED */
223 for (msgnum = 0; msgnum < msgs.size; msgnum++)
224 if (!m_convert (mp, msgs.msgs[msgnum]))
226 seq_setprev (mp); /* set the previous-sequence */
229 * If we aren't saving the results to a sequence,
230 * we default to list the results.
235 if (publicsw == 1 && is_readonly(mp))
236 adios (NULL, "folder %s is read-only, so -public not allowed", folder);
238 if (!pcompile (vec, NULL))
244 /* If printing message numbers to standard out, force line buffering on.
247 setvbuf (stdout, NULL, _IOLBF, 0);
250 * Scan through all the SELECTED messages and check for a
251 * match. If the message does not match, then unselect it.
253 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
254 if (is_selected (mp, msgnum)) {
255 if ((fp = fopen (cp = m_name (msgnum), "r")) == NULL)
256 admonish (cp, "unable to read message");
257 if (fp && pmatches (fp, msgnum, 0L, 0L)) {
264 printf ("%s\n", m_name (msgnum));
266 /* if it doesn't match, then unselect it */
267 unset_selected (mp, msgnum);
279 adios (NULL, "no messages match specification");
284 * Add the matching messages to sequences
286 for (seqp = 0; seqs[seqp]; seqp++)
287 if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
291 * Print total matched if not printing each matched message number.
294 printf ("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
297 context_replace (pfolder, folder); /* update current folder */
298 seq_save (mp); /* synchronize message sequences */
299 context_save (); /* save the context file */
300 folder_free (mp); /* free folder/message structure */
307 putzero_done (int status)
309 if (listsw && status && !isatty (fileno (stdout)))