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;
73 main (int argc, char **argv)
75 int publicsw = -1, zerosw = 1, seqp = 0, vecp = 0;
77 char *maildir, *folder = NULL, buf[100];
78 char *cp, **argp, **arguments;
79 char *seqs[NUMATTRS + 1], *vec[MAXARGS];
80 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);
176 if (*cp == '+' || *cp == '@') {
178 adios (NULL, "only one folder at a time!");
180 folder = pluspath (cp);
182 app_msgarg(&msgs, cp);
186 if (!context_find ("path"))
187 free (path ("./", TFOLDER));
190 * If we didn't specify which messages to search,
191 * then search the whole folder.
194 app_msgarg(&msgs, "all");
197 folder = getfolder (1);
198 maildir = m_maildir (folder);
200 if (chdir (maildir) == NOTOK)
201 adios (maildir, "unable to change directory to");
203 /* read folder and create message structure */
204 if (!(mp = folder_read (folder)))
205 adios (NULL, "unable to read folder %s", folder);
207 /* check for empty folder */
209 adios (NULL, "no messages in %s", folder);
211 /* parse all the message ranges/sequences and set SELECTED */
212 for (msgnum = 0; msgnum < msgs.size; msgnum++)
213 if (!m_convert (mp, msgs.msgs[msgnum]))
215 seq_setprev (mp); /* set the previous-sequence */
218 * If we aren't saving the results to a sequence,
219 * we default to list the results.
224 if (publicsw == 1 && is_readonly(mp))
225 adios (NULL, "folder %s is read-only, so -public not allowed", folder);
227 if (!pcompile (vec, NULL))
234 * Scan through all the SELECTED messages and check for a
235 * match. If the message does not match, then unselect it.
237 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
238 if (is_selected (mp, msgnum)) {
239 if ((fp = fopen (cp = m_name (msgnum), "r")) == NULL)
240 admonish (cp, "unable to read message");
241 if (fp && pmatches (fp, msgnum, 0L, 0L)) {
247 /* if it doesn't match, then unselect it */
248 unset_selected (mp, msgnum);
260 adios (NULL, "no messages match specification");
265 * Add the matching messages to sequences
267 for (seqp = 0; seqs[seqp]; seqp++)
268 if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
272 * Print the name of all the matches
275 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
276 if (is_selected (mp, msgnum))
277 printf ("%s\n", m_name (msgnum));
279 printf ("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
282 context_replace (pfolder, folder); /* update current folder */
283 seq_save (mp); /* synchronize message sequences */
284 context_save (); /* save the context file */
285 folder_free (mp); /* free folder/message structure */
293 if (listsw && status && !isatty (fileno (stdout)))
296 return 1; /* dead code to satisfy the compiler */