]>
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/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/seq_nameok.h"
13 #include "sbr/seq_add.h"
14 #include "sbr/error.h"
16 #include "h/picksbr.h"
19 #include "sbr/m_maildir.h"
21 #define PICK_SWITCHES \
22 X("reverse", 0, REVSW) \
26 X("lbrace", 0, LBRSW) \
27 X("rbrace", 0, RBRSW) \
28 X("cc pattern", 0, CCSW) \
29 X("date pattern", 0, DATESW) \
30 X("from pattern", 0, FROMSW) \
31 X("search pattern", 0, SRCHSW) \
32 X("subject pattern", 0, SUBJSW) \
33 X("to pattern", 0, TOSW) \
34 X("-othercomponent pattern", 0, OTHRSW) \
35 X("after date", 0, AFTRSW) \
36 X("before date", 0, BEFRSW) \
37 X("datefield field", 5, DATFDSW) \
38 X("sequence name", 0, SEQSW) \
39 X("nosequence", 0, NSEQSW) \
40 X("public", 0, PUBLSW) \
41 X("nopublic", 0, NPUBLSW) \
42 X("zero", 0, ZEROSW) \
43 X("nozero", 0, NZEROSW) \
44 X("list", 0, LISTSW) \
45 X("nolist", 0, NLISTSW) \
46 X("debug", 0, DEBUGSW) \
47 X("version", 0, VERSIONSW) \
48 X("help", 0, HELPSW) \
50 #define X(sw, minchars, id) id,
51 DEFINE_SWITCH_ENUM(PICK
);
54 #define X(sw, minchars, id) { sw, minchars, id },
55 DEFINE_SWITCH_ARRAY(PICK
, switches
);
58 static int listsw
= -1;
60 static void putzero_done (int) NORETURN
;
63 main (int argc
, char **argv
)
70 char *maildir
, *folder
= NULL
, buf
[100];
71 char *cp
, **argp
, **arguments
;
72 svector_t seqs
= svector_create (0);
74 struct msgs_array msgs
= { 0, 0, NULL
};
75 struct msgnum_array nums
= { 0, 0, NULL
};
76 struct msgs
*mp
, *mp2
;
82 if (nmh_init(argv
[0], true, true)) { return 1; }
84 set_done(putzero_done
);
86 arguments
= getarguments (invo_name
, argc
, argv
, 1);
89 while ((cp
= *argp
++)) {
95 switch (smatch (cp
, switches
)) {
97 ambigsw (cp
, switches
);
98 listsw
= 0; /* HACK */
101 die("-%s unknown", cp
);
104 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
106 print_help (buf
, switches
, 1);
107 listsw
= 0; /* HACK */
110 print_version(invo_name
);
111 listsw
= 0; /* HACK */
129 if (!(cp
= *argp
++))/* allow -xyz arguments */
130 die("missing argument to %s", argp
[-2]);
134 die("internal error!");
145 if (!(cp
= *argp
++) || *cp
== '-')
146 die("missing argument to %s", argp
[-2]);
148 if (!seq_nameok (cp
))
151 svector_push_back (seqs
, cp
);
182 if (*cp
== '+' || *cp
== '@') {
184 die("only one folder at a time!");
185 folder
= pluspath (cp
);
187 app_msgarg(&msgs
, cp
);
191 if (!context_find ("path"))
192 free (path ("./", TFOLDER
));
195 * If we didn't specify which messages to search,
196 * then search the whole folder.
199 app_msgarg(&msgs
, "all");
202 folder
= getfolder (1);
203 maildir
= m_maildir (folder
);
205 if (chdir (maildir
) == NOTOK
)
206 adios (maildir
, "unable to change directory to");
208 /* read folder and create message structure */
209 if (!(mp
= folder_read (folder
, 0)))
210 die("unable to read folder %s", folder
);
212 /* check for empty folder */
214 die("no messages in %s", folder
);
216 /* parse all the message ranges/sequences and set SELECTED */
217 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
218 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
222 * If we aren't saving the results to a sequence,
223 * we default to list the results.
228 if (publicsw
== 1 && is_readonly(mp
))
229 die("folder %s is read-only, so -public not allowed", folder
);
231 if (!pcompile (vec
, NULL
))
234 /* If printing message numbers to standard out, force line buffering on.
237 setvbuf (stdout
, NULL
, _IOLBF
, 0);
240 * Scan through all the SELECTED messages and check for a
241 * match. If there is NOT a match, then add it to a list to
242 * remove from the final sequence (it will make sense later)
244 if (!reverse
) { /* Overflow or underflow is fine. */
246 end
= mp
->hghsel
+ 1;
250 end
= mp
->lowsel
- 1;
253 for (msgnum
= start
; msgnum
!= end
; msgnum
+= inc
) {
254 if (is_selected (mp
, msgnum
)) {
255 if ((fp
= fopen (cp
= m_name (msgnum
), "r")) == NULL
)
256 admonish (cp
, "unable to read message");
257 if (fp
&& pmatches (fp
, msgnum
, 0L, 0L, debug
)) {
259 puts(m_name (msgnum
));
261 app_msgnum(&nums
, msgnum
);
268 if (nums
.size
>= mp
->numsel
)
269 die("no messages match specification");
272 * So, what's happening here?
274 * - Re-read the folder and sequences, but with locking.
275 * - Recreate the original set of selected messages from the command line
276 * - Set the previous sequence
277 * - Remove any messages that did NOT result in hits from the selection
278 * - Add to any new sequences
281 if (!(mp2
= folder_read (folder
, 1)))
282 die("unable to reread folder %s", folder
);
284 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
285 if (!m_convert (mp2
, msgs
.msgs
[msgnum
]))
287 seq_setprev (mp2
); /* set the previous-sequence */
290 * Nums contains a list of messages that we did NOT match. Remove
291 * that from the selection.
294 for (msgnum
= 0; msgnum
< nums
.size
; msgnum
++) {
295 unset_selected(mp2
, nums
.msgnums
[msgnum
]);
300 * Add the matching messages to sequences
303 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
304 if (!seq_addsel (mp2
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
309 * Print total matched if not printing each matched message number.
312 printf ("%d hit%s\n", mp2
->numsel
, PLURALS(mp2
->numsel
));
316 context_replace (pfolder
, folder
); /* update current folder */
317 seq_save (mp2
); /* synchronize message sequences */
318 context_save (); /* save the context file */
319 folder_free (mp
); /* free folder/message structure */
327 putzero_done (int status
)
329 if (listsw
&& status
&& !isatty (fileno (stdout
)))