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]", invo_name);
111 print_help (buf, switches, 1);
112 listsw = 0; /* HACK */
115 print_version(invo_name);
116 listsw = 0; /* HACK */
130 if (!(cp = *argp++)) /* allow -xyz arguments */
131 adios (NULL, "missing argument to %s",
136 adios (NULL, "internal error!");
147 if (!(cp = *argp++) || *cp == '-')
148 adios (NULL, "missing argument to %s",
151 /* check if too many sequences specified */
152 if (seqp >= NUMATTRS)
153 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
155 if (!seq_nameok (cp))
181 if (*cp == '+' || *cp == '@') {
183 adios (NULL, "only one folder at a time!");
185 folder = pluspath (cp);
187 app_msgarg(&msgs, cp);
191 if (!context_find ("path"))
192 free (path ("./", TFOLDER));
195 ** If we didn't specify which messages to search,
196 ** then search the whole folder.
199 app_msgarg(&msgs, "all");
202 folder = getfolder (1);
203 maildir = m_maildir (folder);
205 if (chdir (maildir) == NOTOK)
206 adios (maildir, "unable to change directory to");
208 /* read folder and create message structure */
209 if (!(mp = folder_read (folder)))
210 adios (NULL, "unable to read folder %s", folder);
212 /* check for empty folder */
214 adios (NULL, "no messages in %s", folder);
216 /* parse all the message ranges/sequences and set SELECTED */
217 for (msgnum = 0; msgnum < msgs.size; msgnum++)
218 if (!m_convert (mp, msgs.msgs[msgnum]))
220 seq_setprev (mp); /* set the previous-sequence */
223 ** If we aren't saving the results to a sequence,
224 ** we default to list the results.
229 if (publicsw == 1 && is_readonly(mp))
230 adios (NULL, "folder %s is read-only, so -public not allowed",
233 if (!pcompile (vec, NULL))
240 ** If printing message numbers to standard out,
241 ** force line buffering on.
244 setvbuf (stdout, NULL, _IOLBF, 0);
247 ** Scan through all the SELECTED messages and check for a
248 ** match. If the message does not match, then unselect it.
250 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
251 if (is_selected (mp, msgnum)) {
252 if ((fp = fopen (cp = m_name (msgnum), "r")) == NULL)
253 admonish (cp, "unable to read message");
254 if (fp && pmatches (fp, msgnum, 0L, 0L)) {
261 printf ("%s\n", m_name (msgnum));
263 /* if it doesn't match, then unselect it */
264 unset_selected (mp, msgnum);
276 adios (NULL, "no messages match specification");
281 ** Add the matching messages to sequences
283 for (seqp = 0; seqs[seqp]; seqp++)
284 if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
288 ** Print total matched if not printing each matched message number.
291 printf ("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
294 context_replace (pfolder, folder); /* update current folder */
295 seq_save (mp); /* synchronize message sequences */
296 context_save (); /* save the context file */
297 folder_free (mp); /* free folder/message structure */
304 putzero_done (int status)
306 if (listsw && status && !isatty (fileno (stdout)))