]>
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/folder_read.h"
10 #include "sbr/folder_free.h"
11 #include "sbr/context_save.h"
12 #include "sbr/context_replace.h"
13 #include "sbr/context_find.h"
14 #include "sbr/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/seq_nameok.h"
19 #include "sbr/seq_add.h"
20 #include "sbr/error.h"
22 #include "h/picksbr.h"
25 #include "sbr/m_maildir.h"
27 #define PICK_SWITCHES \
28 X("reverse", 0, REVSW) \
32 X("lbrace", 0, LBRSW) \
33 X("rbrace", 0, RBRSW) \
34 X("cc pattern", 0, CCSW) \
35 X("date pattern", 0, DATESW) \
36 X("from pattern", 0, FROMSW) \
37 X("search pattern", 0, SRCHSW) \
38 X("subject pattern", 0, SUBJSW) \
39 X("to pattern", 0, TOSW) \
40 X("-othercomponent pattern", 0, OTHRSW) \
41 X("after date", 0, AFTRSW) \
42 X("before date", 0, BEFRSW) \
43 X("datefield field", 5, DATFDSW) \
44 X("sequence name", 0, SEQSW) \
45 X("nosequence", 0, NSEQSW) \
46 X("public", 0, PUBLSW) \
47 X("nopublic", 0, NPUBLSW) \
48 X("zero", 0, ZEROSW) \
49 X("nozero", 0, NZEROSW) \
50 X("list", 0, LISTSW) \
51 X("nolist", 0, NLISTSW) \
52 X("debug", 0, DEBUGSW) \
53 X("version", 0, VERSIONSW) \
54 X("help", 0, HELPSW) \
56 #define X(sw, minchars, id) id,
57 DEFINE_SWITCH_ENUM(PICK
);
60 #define X(sw, minchars, id) { sw, minchars, id },
61 DEFINE_SWITCH_ARRAY(PICK
, switches
);
64 static int listsw
= -1;
66 static void putzero_done (int) NORETURN
;
69 main (int argc
, char **argv
)
76 char *maildir
, *folder
= NULL
, buf
[100];
77 char *cp
, **argp
, **arguments
;
78 svector_t seqs
= svector_create (0);
80 struct msgs_array msgs
= { 0, 0, NULL
};
81 struct msgnum_array nums
= { 0, 0, NULL
};
82 struct msgs
*mp
, *mp2
;
88 if (nmh_init(argv
[0], true, true)) { return 1; }
90 set_done(putzero_done
);
92 arguments
= getarguments (invo_name
, argc
, argv
, 1);
95 while ((cp
= *argp
++)) {
101 switch (smatch (cp
, switches
)) {
103 ambigsw (cp
, switches
);
104 listsw
= 0; /* HACK */
107 die("-%s unknown", cp
);
110 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
112 print_help (buf
, switches
, 1);
113 listsw
= 0; /* HACK */
116 print_version(invo_name
);
117 listsw
= 0; /* HACK */
135 if (!(cp
= *argp
++))/* allow -xyz arguments */
136 die("missing argument to %s", argp
[-2]);
140 die("internal error!");
151 if (!(cp
= *argp
++) || *cp
== '-')
152 die("missing argument to %s", argp
[-2]);
154 if (!seq_nameok (cp
))
157 svector_push_back (seqs
, cp
);
188 if (*cp
== '+' || *cp
== '@') {
190 die("only one folder at a time!");
191 folder
= pluspath (cp
);
193 app_msgarg(&msgs
, cp
);
197 if (!context_find ("path"))
198 free (path ("./", TFOLDER
));
201 * If we didn't specify which messages to search,
202 * then search the whole folder.
205 app_msgarg(&msgs
, "all");
208 folder
= getfolder (1);
209 maildir
= m_maildir (folder
);
211 if (chdir (maildir
) == NOTOK
)
212 adios (maildir
, "unable to change directory to");
214 /* read folder and create message structure */
215 if (!(mp
= folder_read (folder
, 0)))
216 die("unable to read folder %s", folder
);
218 /* check for empty folder */
220 die("no messages in %s", folder
);
222 /* parse all the message ranges/sequences and set SELECTED */
223 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
224 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
228 * If we aren't saving the results to a sequence,
229 * we default to list the results.
234 if (publicsw
== 1 && is_readonly(mp
))
235 die("folder %s is read-only, so -public not allowed", folder
);
237 if (!pcompile (vec
, NULL
))
240 /* If printing message numbers to standard out, force line buffering on.
243 setvbuf (stdout
, NULL
, _IOLBF
, 0);
246 * Scan through all the SELECTED messages and check for a
247 * match. If there is NOT a match, then add it to a list to
248 * remove from the final sequence (it will make sense later)
250 if (!reverse
) { /* Overflow or underflow is fine. */
252 end
= mp
->hghsel
+ 1;
256 end
= mp
->lowsel
- 1;
259 for (msgnum
= start
; msgnum
!= end
; msgnum
+= inc
) {
260 if (is_selected (mp
, msgnum
)) {
261 if ((fp
= fopen (cp
= m_name (msgnum
), "r")) == NULL
)
262 admonish (cp
, "unable to read message");
263 if (fp
&& pmatches (fp
, msgnum
, 0L, 0L, debug
)) {
265 puts(m_name (msgnum
));
267 app_msgnum(&nums
, msgnum
);
274 if (nums
.size
>= mp
->numsel
)
275 die("no messages match specification");
278 * So, what's happening here?
280 * - Re-read the folder and sequences, but with locking.
281 * - Recreate the original set of selected messages from the command line
282 * - Set the previous sequence
283 * - Remove any messages that did NOT result in hits from the selection
284 * - Add to any new sequences
287 if (!(mp2
= folder_read (folder
, 1)))
288 die("unable to reread folder %s", folder
);
290 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
291 if (!m_convert (mp2
, msgs
.msgs
[msgnum
]))
293 seq_setprev (mp2
); /* set the previous-sequence */
296 * Nums contains a list of messages that we did NOT match. Remove
297 * that from the selection.
300 for (msgnum
= 0; msgnum
< nums
.size
; msgnum
++) {
301 unset_selected(mp2
, nums
.msgnums
[msgnum
]);
306 * Add the matching messages to sequences
309 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
310 if (!seq_addsel (mp2
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
315 * Print total matched if not printing each matched message number.
318 printf ("%d hit%s\n", mp2
->numsel
, PLURALS(mp2
->numsel
));
322 context_replace (pfolder
, folder
); /* update current folder */
323 seq_save (mp2
); /* synchronize message sequences */
324 context_save (); /* save the context file */
325 folder_free (mp
); /* free folder/message structure */
333 putzero_done (int status
)
335 if (listsw
&& status
&& !isatty (fileno (stdout
)))