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 = mhbasename(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);
181 if (*cp == '+' || *cp == '@') {
183 adios(NULL, "only one folder at a time!");
185 folder = getcpy(expandfol(cp));
187 app_msgarg(&msgs, cp);
192 ** If we didn't specify which messages to search,
193 ** then search the whole folder.
196 app_msgarg(&msgs, seq_all);
199 folder = getcurfol();
200 maildir = toabsdir(folder);
202 if (chdir(maildir) == NOTOK)
203 adios(maildir, "unable to change directory to");
205 /* read folder and create message structure */
206 if (!(mp = folder_read(folder)))
207 adios(NULL, "unable to read folder %s", folder);
209 /* check for empty folder */
211 adios(NULL, "no messages in %s", folder);
213 /* parse all the message ranges/sequences and set SELECTED */
214 for (msgnum = 0; msgnum < msgs.size; msgnum++)
215 if (!m_convert(mp, msgs.msgs[msgnum]))
217 seq_setprev(mp); /* set the previous-sequence */
220 ** If we aren't saving the results to a sequence,
221 ** we default to list the results.
226 if (publicsw == 1 && is_readonly(mp))
227 adios(NULL, "folder %s is read-only, so -public not allowed",
230 if (!pcompile(vec, NULL))
237 ** If printing message numbers to standard out,
238 ** force line buffering on.
241 setvbuf(stdout, NULL, _IOLBF, 0);
244 ** Scan through all the SELECTED messages and check for a
245 ** match. If the message does not match, then unselect it.
247 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
248 if (is_selected(mp, msgnum)) {
249 if ((fp = fopen(cp = m_name(msgnum), "r")) == NULL)
250 admonish(cp, "unable to read message");
251 if (fp && pmatches(fp, msgnum, 0L, 0L)) {
258 printf("%s\n", m_name(msgnum));
260 /* if it doesn't match, then unselect it */
261 unset_selected(mp, msgnum);
273 adios(NULL, "no messages match specification");
278 ** Add the matching messages to sequences
280 for (seqp = 0; seqs[seqp]; seqp++)
281 if (!seq_addsel(mp, seqs[seqp], publicsw, zerosw))
285 ** Print total matched if not printing each matched message number.
288 printf("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
291 context_replace(curfolder, folder); /* update current folder */
292 seq_save(mp); /* synchronize message sequences */
293 context_save(); /* save the context file */
294 folder_free(mp); /* free folder/message structure */
301 putzero_done(int status)
303 if (listsw && status && !isatty(fileno(stdout)))