2 * pick.c -- search for messages by content
4 * This code is Copyright (c) 2002, 2008, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
11 #include <h/picksbr.h>
14 static struct swit switches[] = {
28 { "date pattern", 0 },
30 { "from pattern", 0 },
32 { "search pattern", 0 },
34 { "subject pattern", 0 },
38 { "-othercomponent pattern", 0 },
44 { "datefield field", 5 },
46 { "sequence name", 0 },
66 static int listsw = -1;
68 static void putzero_done (int) NORETURN;
71 main (int argc, char **argv)
73 int publicsw = -1, zerosw = 1, seqp = 0, vecp = 0;
75 char *maildir, *folder = NULL, buf[100];
76 char *cp, **argp, **arguments;
77 char *seqs[NUMATTRS + 1], *vec[MAXARGS];
78 struct msgs_array msgs = { 0, 0, NULL };
85 setlocale(LC_ALL, "");
87 invo_name = r1bindex (argv[0], '/');
89 /* read user profile/context */
92 arguments = getarguments (invo_name, argc, argv, 1);
95 while ((cp = *argp++)) {
101 switch (smatch (cp, switches)) {
103 ambigsw (cp, switches);
104 listsw = 0; /* HACK */
107 adios (NULL, "-%s unknown", cp);
110 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
112 print_help (buf, switches, 1);
113 listsw = 0; /* HACK */
116 print_version(invo_name);
117 listsw = 0; /* HACK */
131 if (!(cp = *argp++))/* allow -xyz arguments */
132 adios (NULL, "missing argument to %s", argp[-2]);
136 adios (NULL, "internal error!");
147 if (!(cp = *argp++) || *cp == '-')
148 adios (NULL, "missing argument to %s", argp[-2]);
150 /* check if too many sequences specified */
151 if (seqp >= NUMATTRS)
152 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
154 if (!seq_nameok (cp))
180 if (*cp == '+' || *cp == '@') {
182 adios (NULL, "only one folder at a time!");
184 folder = pluspath (cp);
186 app_msgarg(&msgs, cp);
190 if (!context_find ("path"))
191 free (path ("./", TFOLDER));
194 * If we didn't specify which messages to search,
195 * then search the whole folder.
198 app_msgarg(&msgs, "all");
201 folder = getfolder (1);
202 maildir = m_maildir (folder);
204 if (chdir (maildir) == NOTOK)
205 adios (maildir, "unable to change directory to");
207 /* read folder and create message structure */
208 if (!(mp = folder_read (folder)))
209 adios (NULL, "unable to read folder %s", folder);
211 /* check for empty folder */
213 adios (NULL, "no messages in %s", folder);
215 /* parse all the message ranges/sequences and set SELECTED */
216 for (msgnum = 0; msgnum < msgs.size; msgnum++)
217 if (!m_convert (mp, msgs.msgs[msgnum]))
219 seq_setprev (mp); /* set the previous-sequence */
222 * If we aren't saving the results to a sequence,
223 * we default to list the results.
228 if (publicsw == 1 && is_readonly(mp))
229 adios (NULL, "folder %s is read-only, so -public not allowed", folder);
231 if (!pcompile (vec, NULL))
237 /* If printing message numbers to standard out, force line buffering on.
240 setvbuf (stdout, NULL, _IOLBF, 0);
243 * Scan through all the SELECTED messages and check for a
244 * match. If the message does not match, then unselect it.
246 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
247 if (is_selected (mp, msgnum)) {
248 if ((fp = fopen (cp = m_name (msgnum), "r")) == NULL)
249 admonish (cp, "unable to read message");
250 if (fp && pmatches (fp, msgnum, 0L, 0L)) {
257 printf ("%s\n", m_name (msgnum));
259 /* if it doesn't match, then unselect it */
260 unset_selected (mp, msgnum);
272 adios (NULL, "no messages match specification");
277 * Add the matching messages to sequences
279 for (seqp = 0; seqs[seqp]; seqp++)
280 if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
284 * Print total matched if not printing each matched message number.
287 printf ("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
290 context_replace (pfolder, folder); /* update current folder */
291 seq_save (mp); /* synchronize message sequences */
292 context_save (); /* save the context file */
293 folder_free (mp); /* free folder/message structure */
300 putzero_done (int status)
302 if (listsw && status && !isatty (fileno (stdout)))