]>
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 \
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("version", 0, VERSIONSW) \
40 X("help", 0, HELPSW) \
42 #define X(sw, minchars, id) id,
43 DEFINE_SWITCH_ENUM(PICK
);
46 #define X(sw, minchars, id) { sw, minchars, id },
47 DEFINE_SWITCH_ARRAY(PICK
, switches
);
50 static int listsw
= -1;
52 static void putzero_done (int) NORETURN
;
55 main (int argc
, char **argv
)
57 int publicsw
= -1, zerosw
= 1, vecp
= 0;
60 char *maildir
, *folder
= NULL
, buf
[100];
61 char *cp
, **argp
, **arguments
;
62 char *seqs
[NUMATTRS
+ 1], *vec
[MAXARGS
];
63 struct msgs_array msgs
= { 0, 0, NULL
};
70 setlocale(LC_ALL
, "");
72 invo_name
= r1bindex (argv
[0], '/');
74 /* read user profile/context */
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 */
116 if (!(cp
= *argp
++))/* allow -xyz arguments */
117 adios (NULL
, "missing argument to %s", argp
[-2]);
121 adios (NULL
, "internal error!");
132 if (!(cp
= *argp
++) || *cp
== '-')
133 adios (NULL
, "missing argument to %s", argp
[-2]);
135 /* check if too many sequences specified */
136 if (seqp
>= NUMATTRS
)
137 adios (NULL
, "too many sequences (more than %d) specified", NUMATTRS
);
139 if (!seq_nameok (cp
))
168 if (*cp
== '+' || *cp
== '@') {
170 adios (NULL
, "only one folder at a time!");
172 folder
= pluspath (cp
);
174 app_msgarg(&msgs
, cp
);
178 if (!context_find ("path"))
179 free (path ("./", TFOLDER
));
182 * If we didn't specify which messages to search,
183 * then search the whole folder.
186 app_msgarg(&msgs
, "all");
189 folder
= getfolder (1);
190 maildir
= m_maildir (folder
);
192 if (chdir (maildir
) == NOTOK
)
193 adios (maildir
, "unable to change directory to");
195 /* read folder and create message structure */
196 if (!(mp
= folder_read (folder
)))
197 adios (NULL
, "unable to read folder %s", folder
);
199 /* check for empty folder */
201 adios (NULL
, "no messages in %s", folder
);
203 /* parse all the message ranges/sequences and set SELECTED */
204 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
205 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
207 seq_setprev (mp
); /* set the previous-sequence */
210 * If we aren't saving the results to a sequence,
211 * we default to list the results.
216 if (publicsw
== 1 && is_readonly(mp
))
217 adios (NULL
, "folder %s is read-only, so -public not allowed", folder
);
219 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 the message does not match, then unselect it.
234 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
235 if (is_selected (mp
, msgnum
)) {
236 if ((fp
= fopen (cp
= m_name (msgnum
), "r")) == NULL
)
237 admonish (cp
, "unable to read message");
238 if (fp
&& pmatches (fp
, msgnum
, 0L, 0L)) {
245 printf ("%s\n", m_name (msgnum
));
247 /* if it doesn't match, then unselect it */
248 unset_selected (mp
, msgnum
);
260 adios (NULL
, "no messages match specification");
265 * Add the matching messages to sequences
267 for (seqp
= 0; seqs
[seqp
]; seqp
++)
268 if (!seq_addsel (mp
, seqs
[seqp
], publicsw
, zerosw
))
272 * Print total matched if not printing each matched message number.
275 printf ("%d hit%s\n", mp
->numsel
, mp
->numsel
== 1 ? "" : "s");
278 context_replace (pfolder
, folder
); /* update current folder */
279 seq_save (mp
); /* synchronize message sequences */
280 context_save (); /* save the context file */
281 folder_free (mp
); /* free folder/message structure */
288 putzero_done (int status
)
290 if (listsw
&& status
&& !isatty (fileno (stdout
)))