]>
diplodocus.org Git - nmh/blob - uip/pick.c
1 /* pick.c -- search for messages by content
3 * This code is Copyright (c) 2002, 2008, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
10 #include "sbr/m_name.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/seq_setprev.h"
13 #include "sbr/seq_save.h"
14 #include "sbr/smatch.h"
15 #include "sbr/m_convert.h"
16 #include "sbr/getfolder.h"
17 #include "sbr/folder_read.h"
18 #include "sbr/folder_free.h"
19 #include "sbr/context_save.h"
20 #include "sbr/context_replace.h"
21 #include "sbr/context_find.h"
22 #include "sbr/ambigsw.h"
24 #include "sbr/print_version.h"
25 #include "sbr/print_help.h"
26 #include "sbr/seq_nameok.h"
27 #include "sbr/seq_add.h"
28 #include "sbr/error.h"
32 #include "sbr/m_maildir.h"
34 #define PICK_SWITCHES \
35 X("reverse", 0, REVSW) \
39 X("lbrace", 0, LBRSW) \
40 X("rbrace", 0, RBRSW) \
41 X("cc pattern", 0, CCSW) \
42 X("date pattern", 0, DATESW) \
43 X("from pattern", 0, FROMSW) \
44 X("search pattern", 0, SRCHSW) \
45 X("subject pattern", 0, SUBJSW) \
46 X("to pattern", 0, TOSW) \
47 X("-othercomponent pattern", 0, OTHRSW) \
48 X("after date", 0, AFTRSW) \
49 X("before date", 0, BEFRSW) \
50 X("datefield field", 5, DATFDSW) \
51 X("sequence name", 0, SEQSW) \
52 X("nosequence", 0, NSEQSW) \
53 X("public", 0, PUBLSW) \
54 X("nopublic", 0, NPUBLSW) \
55 X("zero", 0, ZEROSW) \
56 X("nozero", 0, NZEROSW) \
57 X("list", 0, LISTSW) \
58 X("nolist", 0, NLISTSW) \
59 X("debug", 0, DEBUGSW) \
60 X("version", 0, VERSIONSW) \
61 X("help", 0, HELPSW) \
63 #define X(sw, minchars, id) id,
64 DEFINE_SWITCH_ENUM(PICK
);
67 #define X(sw, minchars, id) { sw, minchars, id },
68 DEFINE_SWITCH_ARRAY(PICK
, switches
);
71 static int listsw
= -1;
73 static void putzero_done (int) NORETURN
;
76 main (int argc
, char **argv
)
83 char *maildir
, *folder
= NULL
, buf
[100];
84 char *cp
, **argp
, **arguments
;
85 svector_t seqs
= svector_create (0);
87 struct msgs_array msgs
= { 0, 0, NULL
};
88 struct msgnum_array nums
= { 0, 0, NULL
};
89 struct msgs
*mp
, *mp2
;
95 if (nmh_init(argv
[0], true, true)) { return 1; }
97 set_done(putzero_done
);
99 arguments
= getarguments (invo_name
, argc
, argv
, 1);
102 while ((cp
= *argp
++)) {
108 switch (smatch (cp
, switches
)) {
110 ambigsw (cp
, switches
);
111 listsw
= 0; /* HACK */
114 die("-%s unknown", cp
);
117 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
119 print_help (buf
, switches
, 1);
120 listsw
= 0; /* HACK */
123 print_version(invo_name
);
124 listsw
= 0; /* HACK */
142 if (!(cp
= *argp
++))/* allow -xyz arguments */
143 die("missing argument to %s", argp
[-2]);
147 die("internal error!");
158 if (!(cp
= *argp
++) || *cp
== '-')
159 die("missing argument to %s", argp
[-2]);
161 if (!seq_nameok (cp
))
164 svector_push_back (seqs
, cp
);
195 if (*cp
== '+' || *cp
== '@') {
197 die("only one folder at a time!");
198 folder
= pluspath (cp
);
200 app_msgarg(&msgs
, cp
);
204 if (!context_find ("path"))
205 free (path ("./", TFOLDER
));
208 * If we didn't specify which messages to search,
209 * then search the whole folder.
212 app_msgarg(&msgs
, "all");
215 folder
= getfolder (1);
216 maildir
= m_maildir (folder
);
218 if (chdir (maildir
) == NOTOK
)
219 adios (maildir
, "unable to change directory to");
221 /* read folder and create message structure */
222 if (!(mp
= folder_read (folder
, 0)))
223 die("unable to read folder %s", folder
);
225 /* check for empty folder */
227 die("no messages in %s", folder
);
229 /* parse all the message ranges/sequences and set SELECTED */
230 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
231 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
235 * If we aren't saving the results to a sequence,
236 * we default to list the results.
241 if (publicsw
== 1 && is_readonly(mp
))
242 die("folder %s is read-only, so -public not allowed", folder
);
244 if (!pcompile (vec
, NULL
))
247 /* If printing message numbers to standard out, force line buffering on.
250 setvbuf (stdout
, NULL
, _IOLBF
, 0);
253 * Scan through all the SELECTED messages and check for a
254 * match. If there is NOT a match, then add it to a list to
255 * remove from the final sequence (it will make sense later)
257 if (!reverse
) { /* Overflow or underflow is fine. */
259 end
= mp
->hghsel
+ 1;
263 end
= mp
->lowsel
- 1;
266 for (msgnum
= start
; msgnum
!= end
; msgnum
+= inc
) {
267 if (is_selected (mp
, msgnum
)) {
268 if ((fp
= fopen (cp
= m_name (msgnum
), "r")) == NULL
)
269 admonish (cp
, "unable to read message");
270 if (fp
&& pmatches (fp
, msgnum
, 0L, 0L, debug
)) {
272 puts(m_name (msgnum
));
274 app_msgnum(&nums
, msgnum
);
281 if (nums
.size
>= mp
->numsel
)
282 die("no messages match specification");
285 * So, what's happening here?
287 * - Re-read the folder and sequences, but with locking.
288 * - Recreate the original set of selected messages from the command line
289 * - Set the previous sequence
290 * - Remove any messages that did NOT result in hits from the selection
291 * - Add to any new sequences
294 if (!(mp2
= folder_read (folder
, 1)))
295 die("unable to reread folder %s", folder
);
297 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
298 if (!m_convert (mp2
, msgs
.msgs
[msgnum
]))
300 seq_setprev (mp2
); /* set the previous-sequence */
303 * Nums contains a list of messages that we did NOT match. Remove
304 * that from the selection.
307 for (msgnum
= 0; msgnum
< nums
.size
; msgnum
++) {
308 unset_selected(mp2
, nums
.msgnums
[msgnum
]);
313 * Add the matching messages to sequences
316 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
317 if (!seq_addsel (mp2
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
322 * Print total matched if not printing each matched message number.
325 printf ("%d hit%s\n", mp2
->numsel
, PLURALS(mp2
->numsel
));
329 context_replace (pfolder
, folder
); /* update current folder */
330 seq_save (mp2
); /* synchronize message sequences */
331 context_save (); /* save the context file */
332 folder_free (mp
); /* free folder/message structure */
340 putzero_done (int status
)
342 if (listsw
&& status
&& !isatty (fileno (stdout
)))