]>
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.
9 #include "sbr/seq_setprev.h"
10 #include "sbr/seq_save.h"
11 #include "sbr/smatch.h"
12 #include "sbr/m_convert.h"
13 #include "sbr/getfolder.h"
14 #include "sbr/folder_read.h"
15 #include "sbr/folder_free.h"
16 #include "sbr/context_save.h"
17 #include "sbr/context_replace.h"
18 #include "sbr/context_find.h"
19 #include "sbr/ambigsw.h"
21 #include "sbr/print_version.h"
22 #include "sbr/print_help.h"
23 #include "sbr/seq_nameok.h"
24 #include "sbr/seq_add.h"
25 #include "sbr/error.h"
27 #include "h/picksbr.h"
30 #include "sbr/m_maildir.h"
32 #define PICK_SWITCHES \
33 X("reverse", 0, REVSW) \
37 X("lbrace", 0, LBRSW) \
38 X("rbrace", 0, RBRSW) \
39 X("cc pattern", 0, CCSW) \
40 X("date pattern", 0, DATESW) \
41 X("from pattern", 0, FROMSW) \
42 X("search pattern", 0, SRCHSW) \
43 X("subject pattern", 0, SUBJSW) \
44 X("to pattern", 0, TOSW) \
45 X("-othercomponent pattern", 0, OTHRSW) \
46 X("after date", 0, AFTRSW) \
47 X("before date", 0, BEFRSW) \
48 X("datefield field", 5, DATFDSW) \
49 X("sequence name", 0, SEQSW) \
50 X("nosequence", 0, NSEQSW) \
51 X("public", 0, PUBLSW) \
52 X("nopublic", 0, NPUBLSW) \
53 X("zero", 0, ZEROSW) \
54 X("nozero", 0, NZEROSW) \
55 X("list", 0, LISTSW) \
56 X("nolist", 0, NLISTSW) \
57 X("debug", 0, DEBUGSW) \
58 X("version", 0, VERSIONSW) \
59 X("help", 0, HELPSW) \
61 #define X(sw, minchars, id) id,
62 DEFINE_SWITCH_ENUM(PICK
);
65 #define X(sw, minchars, id) { sw, minchars, id },
66 DEFINE_SWITCH_ARRAY(PICK
, switches
);
69 static int listsw
= -1;
71 static void putzero_done (int) NORETURN
;
74 main (int argc
, char **argv
)
81 char *maildir
, *folder
= NULL
, buf
[100];
82 char *cp
, **argp
, **arguments
;
83 svector_t seqs
= svector_create (0);
85 struct msgs_array msgs
= { 0, 0, NULL
};
86 struct msgnum_array nums
= { 0, 0, NULL
};
87 struct msgs
*mp
, *mp2
;
93 if (nmh_init(argv
[0], true, true)) { return 1; }
95 set_done(putzero_done
);
97 arguments
= getarguments (invo_name
, argc
, argv
, 1);
100 while ((cp
= *argp
++)) {
106 switch (smatch (cp
, switches
)) {
108 ambigsw (cp
, switches
);
109 listsw
= 0; /* HACK */
112 die("-%s unknown", cp
);
115 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
117 print_help (buf
, switches
, 1);
118 listsw
= 0; /* HACK */
121 print_version(invo_name
);
122 listsw
= 0; /* HACK */
140 if (!(cp
= *argp
++))/* allow -xyz arguments */
141 die("missing argument to %s", argp
[-2]);
145 die("internal error!");
156 if (!(cp
= *argp
++) || *cp
== '-')
157 die("missing argument to %s", argp
[-2]);
159 if (!seq_nameok (cp
))
162 svector_push_back (seqs
, cp
);
193 if (*cp
== '+' || *cp
== '@') {
195 die("only one folder at a time!");
196 folder
= pluspath (cp
);
198 app_msgarg(&msgs
, cp
);
202 if (!context_find ("path"))
203 free (path ("./", TFOLDER
));
206 * If we didn't specify which messages to search,
207 * then search the whole folder.
210 app_msgarg(&msgs
, "all");
213 folder
= getfolder (1);
214 maildir
= m_maildir (folder
);
216 if (chdir (maildir
) == NOTOK
)
217 adios (maildir
, "unable to change directory to");
219 /* read folder and create message structure */
220 if (!(mp
= folder_read (folder
, 0)))
221 die("unable to read folder %s", folder
);
223 /* check for empty folder */
225 die("no messages in %s", folder
);
227 /* parse all the message ranges/sequences and set SELECTED */
228 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
229 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
233 * If we aren't saving the results to a sequence,
234 * we default to list the results.
239 if (publicsw
== 1 && is_readonly(mp
))
240 die("folder %s is read-only, so -public not allowed", folder
);
242 if (!pcompile (vec
, NULL
))
245 /* If printing message numbers to standard out, force line buffering on.
248 setvbuf (stdout
, NULL
, _IOLBF
, 0);
251 * Scan through all the SELECTED messages and check for a
252 * match. If there is NOT a match, then add it to a list to
253 * remove from the final sequence (it will make sense later)
255 if (!reverse
) { /* Overflow or underflow is fine. */
257 end
= mp
->hghsel
+ 1;
261 end
= mp
->lowsel
- 1;
264 for (msgnum
= start
; msgnum
!= end
; msgnum
+= inc
) {
265 if (is_selected (mp
, msgnum
)) {
266 if ((fp
= fopen (cp
= m_name (msgnum
), "r")) == NULL
)
267 admonish (cp
, "unable to read message");
268 if (fp
&& pmatches (fp
, msgnum
, 0L, 0L, debug
)) {
270 puts(m_name (msgnum
));
272 app_msgnum(&nums
, msgnum
);
279 if (nums
.size
>= mp
->numsel
)
280 die("no messages match specification");
283 * So, what's happening here?
285 * - Re-read the folder and sequences, but with locking.
286 * - Recreate the original set of selected messages from the command line
287 * - Set the previous sequence
288 * - Remove any messages that did NOT result in hits from the selection
289 * - Add to any new sequences
292 if (!(mp2
= folder_read (folder
, 1)))
293 die("unable to reread folder %s", folder
);
295 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
296 if (!m_convert (mp2
, msgs
.msgs
[msgnum
]))
298 seq_setprev (mp2
); /* set the previous-sequence */
301 * Nums contains a list of messages that we did NOT match. Remove
302 * that from the selection.
305 for (msgnum
= 0; msgnum
< nums
.size
; msgnum
++) {
306 unset_selected(mp2
, nums
.msgnums
[msgnum
]);
311 * Add the matching messages to sequences
314 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
315 if (!seq_addsel (mp2
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
320 * Print total matched if not printing each matched message number.
323 printf ("%d hit%s\n", mp2
->numsel
, PLURALS(mp2
->numsel
));
327 context_replace (pfolder
, folder
); /* update current folder */
328 seq_save (mp2
); /* synchronize message sequences */
329 context_save (); /* save the context file */
330 folder_free (mp
); /* free folder/message structure */
338 putzero_done (int status
)
340 if (listsw
&& status
&& !isatty (fileno (stdout
)))