]>
diplodocus.org Git - nmh/blob - uip/pick.c
3 * pick.c -- search for messages by content
5 * This code is Copyright (c) 2002, 2008, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/picksbr.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; }
78 /* Deprecated. Use -debug instead. */
79 if ((cp
= getenv ("MHPDEBUG")) && *cp
)
82 arguments
= getarguments (invo_name
, argc
, argv
, 1);
85 while ((cp
= *argp
++)) {
91 switch (smatch (cp
, switches
)) {
93 ambigsw (cp
, switches
);
94 listsw
= 0; /* HACK */
97 adios (NULL
, "-%s unknown", cp
);
100 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
102 print_help (buf
, switches
, 1);
103 listsw
= 0; /* HACK */
106 print_version(invo_name
);
107 listsw
= 0; /* HACK */
125 if (!(cp
= *argp
++))/* allow -xyz arguments */
126 adios (NULL
, "missing argument to %s", argp
[-2]);
130 adios (NULL
, "internal error!");
141 if (!(cp
= *argp
++) || *cp
== '-')
142 adios (NULL
, "missing argument to %s", argp
[-2]);
144 if (!seq_nameok (cp
))
147 svector_push_back (seqs
, cp
);
178 if (*cp
== '+' || *cp
== '@') {
180 adios (NULL
, "only one folder at a time!");
182 folder
= pluspath (cp
);
184 app_msgarg(&msgs
, cp
);
188 if (!context_find ("path"))
189 free (path ("./", TFOLDER
));
192 * If we didn't specify which messages to search,
193 * then search the whole folder.
196 app_msgarg(&msgs
, "all");
199 folder
= getfolder (1);
200 maildir
= m_maildir (folder
);
202 if (chdir (maildir
) == NOTOK
)
203 adios (maildir
, "unable to change directory to");
205 /* read folder and create message structure */
206 if (!(mp
= folder_read (folder
, 0)))
207 adios (NULL
, "unable to read folder %s", folder
);
209 /* check for empty folder */
211 adios (NULL
, "no messages in %s", folder
);
213 /* parse all the message ranges/sequences and set SELECTED */
214 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
215 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
219 * If we aren't saving the results to a sequence,
220 * we default to list the results.
225 if (publicsw
== 1 && is_readonly(mp
))
226 adios (NULL
, "folder %s is read-only, so -public not allowed", folder
);
228 if (!pcompile (vec
, NULL
))
231 /* If printing message numbers to standard out, force line buffering on.
234 setvbuf (stdout
, NULL
, _IOLBF
, 0);
237 * Scan through all the SELECTED messages and check for a
238 * match. If there is NOT a match, then add it to a list to
239 * remove from the final sequence (it will make sense later)
241 if (!reverse
) { /* Overflow or underflow is fine. */
243 end
= mp
->hghsel
+ 1;
247 end
= mp
->lowsel
- 1;
250 for (msgnum
= start
; msgnum
!= end
; msgnum
+= inc
) {
251 if (is_selected (mp
, msgnum
)) {
252 if ((fp
= fopen (cp
= m_name (msgnum
), "r")) == NULL
)
253 admonish (cp
, "unable to read message");
254 if (fp
&& pmatches (fp
, msgnum
, 0L, 0L, debug
)) {
256 printf ("%s\n", m_name (msgnum
));
258 app_msgnum(&nums
, msgnum
);
265 if (nums
.size
>= mp
->numsel
)
266 adios (NULL
, "no messages match specification");
269 * So, what's happening here?
271 * - Re-read the folder and sequences, but with locking.
272 * - Recreate the original set of selected messages from the command line
273 * - Set the previous sequence
274 * - Remove any messages that did NOT result in hits from the selection
275 * - Add to any new sequences
278 if (!(mp2
= folder_read (folder
, 1)))
279 adios (NULL
, "unable to reread folder %s", folder
);
281 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
282 if (!m_convert (mp2
, msgs
.msgs
[msgnum
]))
284 seq_setprev (mp2
); /* set the previous-sequence */
287 * Nums contains a list of messages that we did NOT match. Remove
288 * that from the selection.
291 for (msgnum
= 0; msgnum
< nums
.size
; msgnum
++) {
292 unset_selected(mp2
, nums
.msgnums
[msgnum
]);
297 * Add the matching messages to sequences
300 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
301 if (!seq_addsel (mp2
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
306 * Print total matched if not printing each matched message number.
309 printf ("%d hit%s\n", mp2
->numsel
, mp2
->numsel
== 1 ? "" : "s");
313 context_replace (pfolder
, folder
); /* update current folder */
314 seq_save (mp2
); /* synchronize message sequences */
315 context_save (); /* save the context file */
316 folder_free (mp
); /* free folder/message structure */
324 putzero_done (int status
)
326 if (listsw
&& status
&& !isatty (fileno (stdout
)))