]>
diplodocus.org Git - nmh/blob - uip/pick.c
3 * pick.c -- search for messages by content
7 * This code is Copyright (c) 2002, 2008, 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) NORETURN
;
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
);
157 if (!seq_nameok (cp
))
183 if (*cp
== '+' || *cp
== '@') {
185 adios (NULL
, "only one folder at a time!");
187 folder
= pluspath (cp
);
189 app_msgarg(&msgs
, cp
);
193 if (!context_find ("path"))
194 free (path ("./", TFOLDER
));
197 * If we didn't specify which messages to search,
198 * then search the whole folder.
201 app_msgarg(&msgs
, "all");
204 folder
= getfolder (1);
205 maildir
= m_maildir (folder
);
207 if (chdir (maildir
) == NOTOK
)
208 adios (maildir
, "unable to change directory to");
210 /* read folder and create message structure */
211 if (!(mp
= folder_read (folder
)))
212 adios (NULL
, "unable to read folder %s", folder
);
214 /* check for empty folder */
216 adios (NULL
, "no messages in %s", folder
);
218 /* parse all the message ranges/sequences and set SELECTED */
219 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
220 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
222 seq_setprev (mp
); /* set the previous-sequence */
225 * If we aren't saving the results to a sequence,
226 * we default to list the results.
231 if (publicsw
== 1 && is_readonly(mp
))
232 adios (NULL
, "folder %s is read-only, so -public not allowed", folder
);
234 if (!pcompile (vec
, NULL
))
240 /* If printing message numbers to standard out, force line buffering on.
243 setvbuf (stdout
, NULL
, _IOLBF
, 0);
246 * Scan through all the SELECTED messages and check for a
247 * match. If the message does not match, then unselect it.
249 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
250 if (is_selected (mp
, msgnum
)) {
251 if ((fp
= fopen (cp
= m_name (msgnum
), "r")) == NULL
)
252 admonish (cp
, "unable to read message");
253 if (fp
&& pmatches (fp
, msgnum
, 0L, 0L)) {
260 printf ("%s\n", m_name (msgnum
));
262 /* if it doesn't match, then unselect it */
263 unset_selected (mp
, msgnum
);
275 adios (NULL
, "no messages match specification");
280 * Add the matching messages to sequences
282 for (seqp
= 0; seqs
[seqp
]; seqp
++)
283 if (!seq_addsel (mp
, seqs
[seqp
], publicsw
, zerosw
))
287 * Print total matched if not printing each matched message number.
290 printf ("%d hit%s\n", mp
->numsel
, mp
->numsel
== 1 ? "" : "s");
293 context_replace (pfolder
, folder
); /* update current folder */
294 seq_save (mp
); /* synchronize message sequences */
295 context_save (); /* save the context file */
296 folder_free (mp
); /* free folder/message structure */
303 putzero_done (int status
)
305 if (listsw
&& status
&& !isatty (fileno (stdout
)))