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>
18 * We allocate space for messages (msgs array)
19 * this number of elements at a time.
24 static struct swit switches[] = {
38 { "date pattern", 0 },
40 { "from pattern", 0 },
42 { "search pattern", 0 },
44 { "subject pattern", 0 },
48 { "-othercomponent pattern", 0 },
54 { "datefield field", 5 },
56 { "sequence name", 0 },
76 static int listsw = -1;
80 main (int argc, char **argv)
82 int publicsw = -1, zerosw = 1, seqp = 0, vecp = 0;
83 int nummsgs, maxmsgs, lo, hi, msgnum;
84 char *maildir, *folder = NULL, buf[100];
85 char *cp, **argp, **arguments;
86 char **msgs, *seqs[NUMATTRS + 1], *vec[MAXARGS];
91 setlocale(LC_ALL, "");
93 invo_name = r1bindex (argv[0], '/');
95 /* read user profile/context */
98 arguments = getarguments (invo_name, argc, argv, 1);
102 * Allocate the initial space to record message
103 * names, ranges, and sequences.
107 msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs)));
109 while ((cp = *argp++)) {
115 switch (smatch (cp, switches)) {
117 ambigsw (cp, switches);
118 listsw = 0; /* HACK */
121 adios (NULL, "-%s unknown", cp);
124 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
126 print_help (buf, switches, 1);
127 listsw = 0; /* HACK */
130 print_version(invo_name);
131 listsw = 0; /* HACK */
145 if (!(cp = *argp++))/* allow -xyz arguments */
146 adios (NULL, "missing argument to %s", argp[-2]);
150 adios (NULL, "internal error!");
161 if (!(cp = *argp++) || *cp == '-')
162 adios (NULL, "missing argument to %s", argp[-2]);
164 /* check if too many sequences specified */
165 if (seqp >= NUMATTRS)
166 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
190 if (*cp == '+' || *cp == '@') {
192 adios (NULL, "only one folder at a time!");
194 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
197 * Check if we need to allocate more space
198 * for message name/ranges/sequences.
200 if (nummsgs >= maxmsgs) {
202 msgs = (char **) mh_xrealloc (msgs,
203 (size_t) (maxmsgs * sizeof(*msgs)));
205 msgs[nummsgs++] = cp;
210 if (!context_find ("path"))
211 free (path ("./", TFOLDER));
214 * If we didn't specify which messages to search,
215 * then search the whole folder.
218 msgs[nummsgs++] = "all";
221 folder = getfolder (1);
222 maildir = m_maildir (folder);
224 if (chdir (maildir) == NOTOK)
225 adios (maildir, "unable to change directory to");
227 /* read folder and create message structure */
228 if (!(mp = folder_read (folder)))
229 adios (NULL, "unable to read folder %s", folder);
231 /* check for empty folder */
233 adios (NULL, "no messages in %s", folder);
235 /* parse all the message ranges/sequences and set SELECTED */
236 for (msgnum = 0; msgnum < nummsgs; msgnum++)
237 if (!m_convert (mp, msgs[msgnum]))
239 seq_setprev (mp); /* set the previous-sequence */
242 * If we aren't saving the results to a sequence,
243 * we default to list the results.
248 if (publicsw == 1 && is_readonly(mp))
249 adios (NULL, "folder %s is read-only, so -public not allowed", folder);
251 if (!pcompile (vec, NULL))
258 * Scan through all the SELECTED messages and check for a
259 * match. If the message does not match, then unselect it.
261 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
262 if (is_selected (mp, msgnum)) {
263 if ((fp = fopen (cp = m_name (msgnum), "r")) == NULL)
264 admonish (cp, "unable to read message");
265 if (fp && pmatches (fp, msgnum, 0L, 0L)) {
271 /* if it doesn't match, then unselect it */
272 unset_selected (mp, msgnum);
284 adios (NULL, "no messages match specification");
289 * Add the matching messages to sequences
291 for (seqp = 0; seqs[seqp]; seqp++)
292 if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
296 * Print the name of all the matches
299 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
300 if (is_selected (mp, msgnum))
301 printf ("%s\n", m_name (msgnum));
303 printf ("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
306 context_replace (pfolder, folder); /* update current folder */
307 seq_save (mp); /* synchronize message sequences */
308 context_save (); /* save the context file */
309 folder_free (mp); /* free folder/message structure */
317 if (listsw && status && !isatty (fileno (stdout)))
320 return 1; /* dead code to satisfy the compiler */