]>
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 <h/picksbr.h>
12 #include "sbr/m_maildir.h"
14 #define PICK_SWITCHES \
15 X("reverse", 0, REVSW) \
19 X("lbrace", 0, LBRSW) \
20 X("rbrace", 0, RBRSW) \
21 X("cc pattern", 0, CCSW) \
22 X("date pattern", 0, DATESW) \
23 X("from pattern", 0, FROMSW) \
24 X("search pattern", 0, SRCHSW) \
25 X("subject pattern", 0, SUBJSW) \
26 X("to pattern", 0, TOSW) \
27 X("-othercomponent pattern", 0, OTHRSW) \
28 X("after date", 0, AFTRSW) \
29 X("before date", 0, BEFRSW) \
30 X("datefield field", 5, DATFDSW) \
31 X("sequence name", 0, SEQSW) \
32 X("nosequence", 0, NSEQSW) \
33 X("public", 0, PUBLSW) \
34 X("nopublic", 0, NPUBLSW) \
35 X("zero", 0, ZEROSW) \
36 X("nozero", 0, NZEROSW) \
37 X("list", 0, LISTSW) \
38 X("nolist", 0, NLISTSW) \
39 X("debug", 0, DEBUGSW) \
40 X("version", 0, VERSIONSW) \
41 X("help", 0, HELPSW) \
43 #define X(sw, minchars, id) id,
44 DEFINE_SWITCH_ENUM(PICK
);
47 #define X(sw, minchars, id) { sw, minchars, id },
48 DEFINE_SWITCH_ARRAY(PICK
, switches
);
51 static int listsw
= -1;
53 static void putzero_done (int) NORETURN
;
56 main (int argc
, char **argv
)
58 int publicsw
= -1, zerosw
= 1, vecp
= 0;
61 char *maildir
, *folder
= NULL
, buf
[100];
62 char *cp
, **argp
, **arguments
;
63 svector_t seqs
= svector_create (0);
65 struct msgs_array msgs
= { 0, 0, NULL
};
66 struct msgnum_array nums
= { 0, 0, NULL
};
67 struct msgs
*mp
, *mp2
;
73 if (nmh_init(argv
[0], 1)) { return 1; }
77 arguments
= getarguments (invo_name
, argc
, argv
, 1);
80 while ((cp
= *argp
++)) {
86 switch (smatch (cp
, switches
)) {
88 ambigsw (cp
, switches
);
89 listsw
= 0; /* HACK */
92 adios (NULL
, "-%s unknown", cp
);
95 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
97 print_help (buf
, switches
, 1);
98 listsw
= 0; /* HACK */
101 print_version(invo_name
);
102 listsw
= 0; /* HACK */
120 if (!(cp
= *argp
++))/* allow -xyz arguments */
121 adios (NULL
, "missing argument to %s", argp
[-2]);
125 adios (NULL
, "internal error!");
136 if (!(cp
= *argp
++) || *cp
== '-')
137 adios (NULL
, "missing argument to %s", argp
[-2]);
139 if (!seq_nameok (cp
))
142 svector_push_back (seqs
, cp
);
173 if (*cp
== '+' || *cp
== '@') {
175 adios (NULL
, "only one folder at a time!");
176 folder
= pluspath (cp
);
178 app_msgarg(&msgs
, cp
);
182 if (!context_find ("path"))
183 free (path ("./", TFOLDER
));
186 * If we didn't specify which messages to search,
187 * then search the whole folder.
190 app_msgarg(&msgs
, "all");
193 folder
= getfolder (1);
194 maildir
= m_maildir (folder
);
196 if (chdir (maildir
) == NOTOK
)
197 adios (maildir
, "unable to change directory to");
199 /* read folder and create message structure */
200 if (!(mp
= folder_read (folder
, 0)))
201 adios (NULL
, "unable to read folder %s", folder
);
203 /* check for empty folder */
205 adios (NULL
, "no messages in %s", folder
);
207 /* parse all the message ranges/sequences and set SELECTED */
208 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
209 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
213 * If we aren't saving the results to a sequence,
214 * we default to list the results.
219 if (publicsw
== 1 && is_readonly(mp
))
220 adios (NULL
, "folder %s is read-only, so -public not allowed", folder
);
222 if (!pcompile (vec
, NULL
))
225 /* If printing message numbers to standard out, force line buffering on.
228 setvbuf (stdout
, NULL
, _IOLBF
, 0);
231 * Scan through all the SELECTED messages and check for a
232 * match. If there is NOT a match, then add it to a list to
233 * remove from the final sequence (it will make sense later)
235 if (!reverse
) { /* Overflow or underflow is fine. */
237 end
= mp
->hghsel
+ 1;
241 end
= mp
->lowsel
- 1;
244 for (msgnum
= start
; msgnum
!= end
; msgnum
+= inc
) {
245 if (is_selected (mp
, msgnum
)) {
246 if ((fp
= fopen (cp
= m_name (msgnum
), "r")) == NULL
)
247 admonish (cp
, "unable to read message");
248 if (fp
&& pmatches (fp
, msgnum
, 0L, 0L, debug
)) {
250 puts(m_name (msgnum
));
252 app_msgnum(&nums
, msgnum
);
259 if (nums
.size
>= mp
->numsel
)
260 adios (NULL
, "no messages match specification");
263 * So, what's happening here?
265 * - Re-read the folder and sequences, but with locking.
266 * - Recreate the original set of selected messages from the command line
267 * - Set the previous sequence
268 * - Remove any messages that did NOT result in hits from the selection
269 * - Add to any new sequences
272 if (!(mp2
= folder_read (folder
, 1)))
273 adios (NULL
, "unable to reread folder %s", folder
);
275 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
276 if (!m_convert (mp2
, msgs
.msgs
[msgnum
]))
278 seq_setprev (mp2
); /* set the previous-sequence */
281 * Nums contains a list of messages that we did NOT match. Remove
282 * that from the selection.
285 for (msgnum
= 0; msgnum
< nums
.size
; msgnum
++) {
286 unset_selected(mp2
, nums
.msgnums
[msgnum
]);
291 * Add the matching messages to sequences
294 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
295 if (!seq_addsel (mp2
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
300 * Print total matched if not printing each matched message number.
303 printf ("%d hit%s\n", mp2
->numsel
, PLURALS(mp2
->numsel
));
307 context_replace (pfolder
, folder
); /* update current folder */
308 seq_save (mp2
); /* synchronize message sequences */
309 context_save (); /* save the context file */
310 folder_free (mp
); /* free folder/message structure */
318 putzero_done (int status
)
320 if (listsw
&& status
&& !isatty (fileno (stdout
)))