]>
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>
13 #include "sbr/m_maildir.h"
15 #define PICK_SWITCHES \
16 X("reverse", 0, REVSW) \
20 X("lbrace", 0, LBRSW) \
21 X("rbrace", 0, RBRSW) \
22 X("cc pattern", 0, CCSW) \
23 X("date pattern", 0, DATESW) \
24 X("from pattern", 0, FROMSW) \
25 X("search pattern", 0, SRCHSW) \
26 X("subject pattern", 0, SUBJSW) \
27 X("to pattern", 0, TOSW) \
28 X("-othercomponent pattern", 0, OTHRSW) \
29 X("after date", 0, AFTRSW) \
30 X("before date", 0, BEFRSW) \
31 X("datefield field", 5, DATFDSW) \
32 X("sequence name", 0, SEQSW) \
33 X("nosequence", 0, NSEQSW) \
34 X("public", 0, PUBLSW) \
35 X("nopublic", 0, NPUBLSW) \
36 X("zero", 0, ZEROSW) \
37 X("nozero", 0, NZEROSW) \
38 X("list", 0, LISTSW) \
39 X("nolist", 0, NLISTSW) \
40 X("debug", 0, DEBUGSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(PICK
);
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(PICK
, switches
);
52 static int listsw
= -1;
54 static void putzero_done (int) NORETURN
;
57 main (int argc
, char **argv
)
59 int publicsw
= -1, zerosw
= 1, vecp
= 0;
62 char *maildir
, *folder
= NULL
, buf
[100];
63 char *cp
, **argp
, **arguments
;
64 svector_t seqs
= svector_create (0);
66 struct msgs_array msgs
= { 0, 0, NULL
};
67 struct msgnum_array nums
= { 0, 0, NULL
};
68 struct msgs
*mp
, *mp2
;
74 if (nmh_init(argv
[0], 1)) { return 1; }
76 set_done(putzero_done
);
78 arguments
= getarguments (invo_name
, argc
, argv
, 1);
81 while ((cp
= *argp
++)) {
87 switch (smatch (cp
, switches
)) {
89 ambigsw (cp
, switches
);
90 listsw
= 0; /* HACK */
93 adios (NULL
, "-%s unknown", cp
);
96 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
98 print_help (buf
, switches
, 1);
99 listsw
= 0; /* HACK */
102 print_version(invo_name
);
103 listsw
= 0; /* HACK */
121 if (!(cp
= *argp
++))/* allow -xyz arguments */
122 adios (NULL
, "missing argument to %s", argp
[-2]);
126 adios (NULL
, "internal error!");
137 if (!(cp
= *argp
++) || *cp
== '-')
138 adios (NULL
, "missing argument to %s", argp
[-2]);
140 if (!seq_nameok (cp
))
143 svector_push_back (seqs
, cp
);
174 if (*cp
== '+' || *cp
== '@') {
176 adios (NULL
, "only one folder at a time!");
177 folder
= pluspath (cp
);
179 app_msgarg(&msgs
, cp
);
183 if (!context_find ("path"))
184 free (path ("./", TFOLDER
));
187 * If we didn't specify which messages to search,
188 * then search the whole folder.
191 app_msgarg(&msgs
, "all");
194 folder
= getfolder (1);
195 maildir
= m_maildir (folder
);
197 if (chdir (maildir
) == NOTOK
)
198 adios (maildir
, "unable to change directory to");
200 /* read folder and create message structure */
201 if (!(mp
= folder_read (folder
, 0)))
202 adios (NULL
, "unable to read folder %s", folder
);
204 /* check for empty folder */
206 adios (NULL
, "no messages in %s", folder
);
208 /* parse all the message ranges/sequences and set SELECTED */
209 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
210 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
214 * If we aren't saving the results to a sequence,
215 * we default to list the results.
220 if (publicsw
== 1 && is_readonly(mp
))
221 adios (NULL
, "folder %s is read-only, so -public not allowed", folder
);
223 if (!pcompile (vec
, NULL
))
226 /* If printing message numbers to standard out, force line buffering on.
229 setvbuf (stdout
, NULL
, _IOLBF
, 0);
232 * Scan through all the SELECTED messages and check for a
233 * match. If there is NOT a match, then add it to a list to
234 * remove from the final sequence (it will make sense later)
236 if (!reverse
) { /* Overflow or underflow is fine. */
238 end
= mp
->hghsel
+ 1;
242 end
= mp
->lowsel
- 1;
245 for (msgnum
= start
; msgnum
!= end
; msgnum
+= inc
) {
246 if (is_selected (mp
, msgnum
)) {
247 if ((fp
= fopen (cp
= m_name (msgnum
), "r")) == NULL
)
248 admonish (cp
, "unable to read message");
249 if (fp
&& pmatches (fp
, msgnum
, 0L, 0L, debug
)) {
251 puts(m_name (msgnum
));
253 app_msgnum(&nums
, msgnum
);
260 if (nums
.size
>= mp
->numsel
)
261 adios (NULL
, "no messages match specification");
264 * So, what's happening here?
266 * - Re-read the folder and sequences, but with locking.
267 * - Recreate the original set of selected messages from the command line
268 * - Set the previous sequence
269 * - Remove any messages that did NOT result in hits from the selection
270 * - Add to any new sequences
273 if (!(mp2
= folder_read (folder
, 1)))
274 adios (NULL
, "unable to reread folder %s", folder
);
276 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
277 if (!m_convert (mp2
, msgs
.msgs
[msgnum
]))
279 seq_setprev (mp2
); /* set the previous-sequence */
282 * Nums contains a list of messages that we did NOT match. Remove
283 * that from the selection.
286 for (msgnum
= 0; msgnum
< nums
.size
; msgnum
++) {
287 unset_selected(mp2
, nums
.msgnums
[msgnum
]);
292 * Add the matching messages to sequences
295 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
296 if (!seq_addsel (mp2
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
301 * Print total matched if not printing each matched message number.
304 printf ("%d hit%s\n", mp2
->numsel
, PLURALS(mp2
->numsel
));
308 context_replace (pfolder
, folder
); /* update current folder */
309 seq_save (mp2
); /* synchronize message sequences */
310 context_save (); /* save the context file */
311 folder_free (mp
); /* free folder/message structure */
319 putzero_done (int status
)
321 if (listsw
&& status
&& !isatty (fileno (stdout
)))