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, seqp = 0, vecp = 0;
76 char *maildir, *folder = NULL, buf[100];
77 char *cp, **argp, **arguments;
78 char *seqs[NUMATTRS + 1], *vec[MAXARGS];
79 struct msgs_array msgs = { 0, 0, NULL };
86 setlocale(LC_ALL, "");
88 invo_name = r1bindex (argv[0], '/');
90 /* read user profile/context */
93 arguments = getarguments (invo_name, argc, argv, 1);
96 while ((cp = *argp++)) {
102 switch (smatch (cp, switches)) {
104 ambigsw (cp, switches);
105 listsw = 0; /* HACK */
108 adios (NULL, "-%s unknown", cp);
111 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
113 print_help (buf, switches, 1);
114 listsw = 0; /* HACK */
117 print_version(invo_name);
118 listsw = 0; /* HACK */
132 if (!(cp = *argp++))/* allow -xyz arguments */
133 adios (NULL, "missing argument to %s", argp[-2]);
137 adios (NULL, "internal error!");
148 if (!(cp = *argp++) || *cp == '-')
149 adios (NULL, "missing argument to %s", argp[-2]);
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", folder);
232 if (!pcompile (vec, NULL))
238 /* If printing message numbers to standard out, 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 (pfolder, 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)))