3 * pick.c -- search for messages by content
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/picksbr.h>
17 static struct swit switches[] = {
31 { "date pattern", 0 },
33 { "from pattern", 0 },
35 { "search pattern", 0 },
37 { "subject pattern", 0 },
41 { "-othercomponent pattern", 0 },
47 { "datefield field", 5 },
49 { "sequence name", 0 },
69 static int listsw = -1;
71 static void putzero_done (int);
74 main (int argc, char **argv)
76 int publicsw = -1, zerosw = 1, seqp = 0, vecp = 0;
78 char *maildir, *folder = NULL, buf[100];
79 char *cp, **argp, **arguments;
80 char *seqs[NUMATTRS + 1], *vec[MAXARGS];
81 struct msgs_array msgs = { 0, 0, NULL };
88 setlocale(LC_ALL, "");
90 invo_name = r1bindex (argv[0], '/');
92 /* read user profile/context */
95 arguments = getarguments (invo_name, argc, argv, 1);
98 while ((cp = *argp++)) {
104 switch (smatch (cp, switches)) {
106 ambigsw (cp, switches);
107 listsw = 0; /* HACK */
110 adios (NULL, "-%s unknown", cp);
113 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
115 print_help (buf, switches, 1);
116 listsw = 0; /* HACK */
119 print_version(invo_name);
120 listsw = 0; /* HACK */
134 if (!(cp = *argp++))/* allow -xyz arguments */
135 adios (NULL, "missing argument to %s", argp[-2]);
139 adios (NULL, "internal error!");
150 if (!(cp = *argp++) || *cp == '-')
151 adios (NULL, "missing argument to %s", argp[-2]);
153 /* check if too many sequences specified */
154 if (seqp >= NUMATTRS)
155 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
179 if (*cp == '+' || *cp == '@') {
181 adios (NULL, "only one folder at a time!");
183 folder = pluspath (cp);
185 app_msgarg(&msgs, cp);
189 if (!context_find ("path"))
190 free (path ("./", TFOLDER));
193 * If we didn't specify which messages to search,
194 * then search the whole folder.
197 app_msgarg(&msgs, "all");
200 folder = getfolder (1);
201 maildir = m_maildir (folder);
203 if (chdir (maildir) == NOTOK)
204 adios (maildir, "unable to change directory to");
206 /* read folder and create message structure */
207 if (!(mp = folder_read (folder)))
208 adios (NULL, "unable to read folder %s", folder);
210 /* check for empty folder */
212 adios (NULL, "no messages in %s", folder);
214 /* parse all the message ranges/sequences and set SELECTED */
215 for (msgnum = 0; msgnum < msgs.size; msgnum++)
216 if (!m_convert (mp, msgs.msgs[msgnum]))
218 seq_setprev (mp); /* set the previous-sequence */
221 * If we aren't saving the results to a sequence,
222 * we default to list the results.
227 if (publicsw == 1 && is_readonly(mp))
228 adios (NULL, "folder %s is read-only, so -public not allowed", folder);
230 if (!pcompile (vec, NULL))
237 * Scan through all the SELECTED messages and check for a
238 * match. If the message does not match, then unselect it.
240 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
241 if (is_selected (mp, msgnum)) {
242 if ((fp = fopen (cp = m_name (msgnum), "r")) == NULL)
243 admonish (cp, "unable to read message");
244 if (fp && pmatches (fp, msgnum, 0L, 0L)) {
250 /* if it doesn't match, then unselect it */
251 unset_selected (mp, msgnum);
263 adios (NULL, "no messages match specification");
268 * Add the matching messages to sequences
270 for (seqp = 0; seqs[seqp]; seqp++)
271 if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
275 * Print the name of all the matches
278 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
279 if (is_selected (mp, msgnum))
280 printf ("%s\n", m_name (msgnum));
282 printf ("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
285 context_replace (pfolder, folder); /* update current folder */
286 seq_save (mp); /* synchronize message sequences */
287 context_save (); /* save the context file */
288 folder_free (mp); /* free folder/message structure */
295 putzero_done (int status)
297 if (listsw && status && !isatty (fileno (stdout)))