]>
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
;
73 if (nmh_init(argv
[0], 1)) { return 1; }
77 /* Deprecated. Use -debug instead. */
78 if ((cp
= getenv ("MHPDEBUG")) && *cp
)
81 arguments
= getarguments (invo_name
, argc
, argv
, 1);
84 while ((cp
= *argp
++)) {
90 switch (smatch (cp
, switches
)) {
92 ambigsw (cp
, switches
);
93 listsw
= 0; /* HACK */
96 adios (NULL
, "-%s unknown", cp
);
99 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
101 print_help (buf
, switches
, 1);
102 listsw
= 0; /* HACK */
105 print_version(invo_name
);
106 listsw
= 0; /* HACK */
124 if (!(cp
= *argp
++))/* allow -xyz arguments */
125 adios (NULL
, "missing argument to %s", argp
[-2]);
129 adios (NULL
, "internal error!");
140 if (!(cp
= *argp
++) || *cp
== '-')
141 adios (NULL
, "missing argument to %s", argp
[-2]);
143 if (!seq_nameok (cp
))
146 svector_push_back (seqs
, cp
);
177 if (*cp
== '+' || *cp
== '@') {
179 adios (NULL
, "only one folder at a time!");
181 folder
= pluspath (cp
);
183 app_msgarg(&msgs
, cp
);
187 if (!context_find ("path"))
188 free (path ("./", TFOLDER
));
191 * If we didn't specify which messages to search,
192 * then search the whole folder.
195 app_msgarg(&msgs
, "all");
198 folder
= getfolder (1);
199 maildir
= m_maildir (folder
);
201 if (chdir (maildir
) == NOTOK
)
202 adios (maildir
, "unable to change directory to");
204 /* read folder and create message structure */
205 if (!(mp
= folder_read (folder
, 0)))
206 adios (NULL
, "unable to read folder %s", folder
);
208 /* check for empty folder */
210 adios (NULL
, "no messages in %s", folder
);
212 /* parse all the message ranges/sequences and set SELECTED */
213 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
214 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
218 * If we aren't saving the results to a sequence,
219 * we default to list the results.
224 if (publicsw
== 1 && is_readonly(mp
))
225 adios (NULL
, "folder %s is read-only, so -public not allowed", folder
);
227 if (!pcompile (vec
, NULL
))
230 /* If printing message numbers to standard out, force line buffering on.
233 setvbuf (stdout
, NULL
, _IOLBF
, 0);
236 * Scan through all the SELECTED messages and check for a
237 * match. If there is NOT a match, then add it to a list to
238 * remove from the final sequence (it will make sense later)
240 for (msgnum
= reverse
? mp
->hghsel
: mp
->lowsel
;
241 reverse
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
;
242 msgnum
+= reverse
? -1 : 1) {
243 if (is_selected (mp
, msgnum
)) {
244 if ((fp
= fopen (cp
= m_name (msgnum
), "r")) == NULL
)
245 admonish (cp
, "unable to read message");
246 if (fp
&& pmatches (fp
, msgnum
, 0L, 0L, debug
)) {
248 printf ("%s\n", m_name (msgnum
));
250 app_msgnum(&nums
, msgnum
);
257 if (nums
.size
>= mp
->numsel
)
258 adios (NULL
, "no messages match specification");
261 * So, what's happening here?
263 * - Re-read the folder and sequences, but with locking.
264 * - Recreate the original set of selected messages from the command line
265 * - Set the previous sequence
266 * - Remove any messages that did NOT result in hits from the selection
267 * - Add to any new sequences
270 if (!(mp2
= folder_read (folder
, 1)))
271 adios (NULL
, "unable to reread folder %s", folder
);
273 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
274 if (!m_convert (mp2
, msgs
.msgs
[msgnum
]))
276 seq_setprev (mp2
); /* set the previous-sequence */
279 * Nums contains a list of messages that we did NOT match. Remove
280 * that from the selection.
283 for (msgnum
= 0; msgnum
< nums
.size
; msgnum
++) {
284 unset_selected(mp2
, nums
.msgnums
[msgnum
]);
289 * Add the matching messages to sequences
292 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++)
293 if (!seq_addsel (mp2
, svector_at (seqs
, seqp
), publicsw
, zerosw
))
298 * Print total matched if not printing each matched message number.
301 printf ("%d hit%s\n", mp2
->numsel
, mp2
->numsel
== 1 ? "" : "s");
305 context_replace (pfolder
, folder
); /* update current folder */
306 seq_save (mp2
); /* synchronize message sequences */
307 context_save (); /* save the context file */
308 folder_free (mp
); /* free folder/message structure */
316 putzero_done (int status
)
318 if (listsw
&& status
&& !isatty (fileno (stdout
)))