]>
diplodocus.org Git - nmh/blob - uip/pick.c
e4ad0dcc0d5877bb9c256b98e830d7d60fd69f4c
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/ambigsw.h"
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/seq_nameok.h"
14 #include "sbr/seq_add.h"
15 #include "sbr/error.h"
17 #include "h/picksbr.h"
20 #include "sbr/m_maildir.h"
22 #define PICK_SWITCHES \
23 X("reverse", 0, REVSW) \
27 X("lbrace", 0, LBRSW) \
28 X("rbrace", 0, RBRSW) \
29 X("cc pattern", 0, CCSW) \
30 X("date pattern", 0, DATESW) \
31 X("from pattern", 0, FROMSW) \
32 X("search pattern", 0, SRCHSW) \
33 X("subject pattern", 0, SUBJSW) \
34 X("to pattern", 0, TOSW) \
35 X("-othercomponent pattern", 0, OTHRSW) \
36 X("after date", 0, AFTRSW) \
37 X("before date", 0, BEFRSW) \
38 X("datefield field", 5, DATFDSW) \
39 X("sequence name", 0, SEQSW) \
40 X("nosequence", 0, NSEQSW) \
41 X("public", 0, PUBLSW) \
42 X("nopublic", 0, NPUBLSW) \
43 X("zero", 0, ZEROSW) \
44 X("nozero", 0, NZEROSW) \
45 X("list", 0, LISTSW) \
46 X("nolist", 0, NLISTSW) \
47 X("debug", 0, DEBUGSW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
51 #define X(sw, minchars, id) id,
52 DEFINE_SWITCH_ENUM(PICK
);
55 #define X(sw, minchars, id) { sw, minchars, id },
56 DEFINE_SWITCH_ARRAY(PICK
, switches
);
59 static int listsw
= -1;
61 static void putzero_done (int) NORETURN
;
64 main (int argc
, char **argv
)
71 char *maildir
, *folder
= NULL
, buf
[100];
72 char *cp
, **argp
, **arguments
;
73 svector_t seqs
= svector_create (0);
75 struct msgs_array msgs
= { 0, 0, NULL
};
76 struct msgnum_array nums
= { 0, 0, NULL
};
77 struct msgs
*mp
, *mp2
;
83 if (nmh_init(argv
[0], true, true)) { return 1; }
85 set_done(putzero_done
);
87 arguments
= getarguments (invo_name
, argc
, argv
, 1);
90 while ((cp
= *argp
++)) {
96 switch (smatch (cp
, switches
)) {
98 ambigsw (cp
, switches
);
99 listsw
= 0; /* HACK */
102 die("-%s unknown", cp
);
105 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
107 print_help (buf
, switches
, 1);
108 listsw
= 0; /* HACK */
111 print_version(invo_name
);
112 listsw
= 0; /* HACK */
130 if (!(cp
= *argp
++))/* allow -xyz arguments */
131 die("missing argument to %s", argp
[-2]);
135 die("internal error!");
146 if (!(cp
= *argp
++) || *cp
== '-')
147 die("missing argument to %s", argp
[-2]);
149 if (!seq_nameok (cp
))
152 svector_push_back (seqs
, cp
);
183 if (*cp
== '+' || *cp
== '@') {
185 die("only one folder at a time!");
186 folder
= pluspath (cp
);
188 app_msgarg(&msgs
, cp
);
192 if (!context_find ("path"))
193 free (path ("./", TFOLDER
));
196 * If we didn't specify which messages to search,
197 * then search the whole folder.
200 app_msgarg(&msgs
, "all");
203 folder
= getfolder (1);
204 maildir
= m_maildir (folder
);
206 if (chdir (maildir
) == NOTOK
)
207 adios (maildir
, "unable to change directory to");
209 /* read folder and create message structure */
210 if (!(mp
= folder_read (folder
, 0)))
211 die("unable to read folder %s", folder
);
213 /* check for empty folder */
215 die("no messages in %s", folder
);
217 /* parse all the message ranges/sequences and set SELECTED */
218 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
219 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
223 * If we aren't saving the results to a sequence,
224 * we default to list the results.
229 if (publicsw
== 1 && is_readonly(mp
))
230 die("folder %s is read-only, so -public not allowed", folder
);
232 if (!pcompile (vec
, NULL
))
235 /* If printing message numbers to standard out, force line buffering on.
238 setvbuf (stdout
, NULL
, _IOLBF
, 0);
241 * Scan through all the SELECTED messages and check for a
242 * match. If there is NOT a match, then add it to a list to
243 * remove from the final sequence (it will make sense later)
245 if (!reverse
) { /* Overflow or underflow is fine. */
247 end
= mp
->hghsel
+ 1;
251 end
= mp
->lowsel
- 1;
254 for (msgnum
= start
; msgnum
!= end
; msgnum
+= inc
) {
255 if (is_selected (mp
, msgnum
)) {
256 if ((fp
= fopen (cp
= m_name (msgnum
), "r")) == NULL
)
257 admonish (cp
, "unable to read message");
258 if (fp
&& pmatches (fp
, msgnum
, 0L, 0L, debug
)) {
260 puts(m_name (msgnum
));
262 app_msgnum(&nums
, msgnum
);
269 if (nums
.size
>= mp
->numsel
)
270 die("no messages match specification");
273 * So, what's happening here?
275 * - Re-read the folder and sequences, but with locking.
276 * - Recreate the original set of selected messages from the command line
277 * - Set the previous sequence
278 * - Remove any messages that did NOT result in hits from the selection
279 * - Add to any new sequences
282 if (!(mp2
= folder_read (folder
, 1)))
283 die("unable to reread folder %s", folder
);
285 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
286 if (!m_convert (mp2
, msgs
.msgs
[msgnum
]))
288 seq_setprev (mp2
); /* set the previous-sequence */
291 * Nums contains a list of messages that we did NOT match. Remove
292 * that from the selection.
295 for (msgnum
= 0; msgnum
< nums
.size
; msgnum
++) {
296 unset_selected(mp2
, nums
.msgnums
[msgnum
]);
301 * Add the matching messages to sequences
304 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
305 if (!seq_addsel (mp2
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
310 * Print total matched if not printing each matched message number.
313 printf ("%d hit%s\n", mp2
->numsel
, PLURALS(mp2
->numsel
));
317 context_replace (pfolder
, folder
); /* update current folder */
318 seq_save (mp2
); /* synchronize message sequences */
319 context_save (); /* save the context file */
320 folder_free (mp
); /* free folder/message structure */
328 putzero_done (int status
)
330 if (listsw
&& status
&& !isatty (fileno (stdout
)))