]> diplodocus.org Git - nmh/blob - uip/pick.c
picksbr.c: fprintf(3) function pointer using `%p' and union.
[nmh] / uip / pick.c
1 /* pick.c -- search for messages by content
2 *
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.
6 */
7
8 #include <h/mh.h>
9 #include <h/tws.h>
10 #include <h/picksbr.h>
11 #include "h/done.h"
12 #include <h/utils.h>
13 #include "sbr/m_maildir.h"
14
15 #define PICK_SWITCHES \
16 X("reverse", 0, REVSW) \
17 X("and", 0, ANDSW) \
18 X("or", 0, ORSW) \
19 X("not", 0, NOTSW) \
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) \
43
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(PICK);
46 #undef X
47
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(PICK, switches);
50 #undef X
51
52 static int listsw = -1;
53
54 static void putzero_done (int) NORETURN;
55
56 int
57 main (int argc, char **argv)
58 {
59 int publicsw = -1, zerosw = 1, vecp = 0;
60 size_t seqp = 0;
61 int msgnum;
62 char *maildir, *folder = NULL, buf[100];
63 char *cp, **argp, **arguments;
64 svector_t seqs = svector_create (0);
65 char *vec[MAXARGS];
66 struct msgs_array msgs = { 0, 0, NULL };
67 struct msgnum_array nums = { 0, 0, NULL };
68 struct msgs *mp, *mp2;
69 FILE *fp;
70 int debug = 0;
71 int reverse = 0;
72 int start, end, inc;
73
74 if (nmh_init(argv[0], 1)) { return 1; }
75
76 set_done(putzero_done);
77
78 arguments = getarguments (invo_name, argc, argv, 1);
79 argp = arguments;
80
81 while ((cp = *argp++)) {
82 if (*cp == '-') {
83 if (*++cp == '-') {
84 vec[vecp++] = --cp;
85 goto pattern;
86 }
87 switch (smatch (cp, switches)) {
88 case AMBIGSW:
89 ambigsw (cp, switches);
90 listsw = 0; /* HACK */
91 done (1);
92 case UNKWNSW:
93 adios (NULL, "-%s unknown", cp);
94
95 case HELPSW:
96 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
97 invo_name);
98 print_help (buf, switches, 1);
99 listsw = 0; /* HACK */
100 done (0);
101 case VERSIONSW:
102 print_version(invo_name);
103 listsw = 0; /* HACK */
104 done (0);
105
106 case REVSW:
107 reverse = 1;
108 continue;
109
110 case CCSW:
111 case DATESW:
112 case FROMSW:
113 case SUBJSW:
114 case TOSW:
115 case DATFDSW:
116 case AFTRSW:
117 case BEFRSW:
118 case SRCHSW:
119 vec[vecp++] = --cp;
120 pattern:
121 if (!(cp = *argp++))/* allow -xyz arguments */
122 adios (NULL, "missing argument to %s", argp[-2]);
123 vec[vecp++] = cp;
124 continue;
125 case OTHRSW:
126 adios (NULL, "internal error!");
127
128 case ANDSW:
129 case ORSW:
130 case NOTSW:
131 case LBRSW:
132 case RBRSW:
133 vec[vecp++] = --cp;
134 continue;
135
136 case SEQSW:
137 if (!(cp = *argp++) || *cp == '-')
138 adios (NULL, "missing argument to %s", argp[-2]);
139
140 if (!seq_nameok (cp))
141 done (1);
142
143 svector_push_back (seqs, cp);
144 seqp++;
145 continue;
146 case NSEQSW:
147 seqp = 0;
148 continue;
149 case PUBLSW:
150 publicsw = 1;
151 continue;
152 case NPUBLSW:
153 publicsw = 0;
154 continue;
155 case ZEROSW:
156 zerosw++;
157 continue;
158 case NZEROSW:
159 zerosw = 0;
160 continue;
161
162 case LISTSW:
163 listsw = 1;
164 continue;
165 case NLISTSW:
166 listsw = 0;
167 continue;
168
169 case DEBUGSW:
170 ++debug;
171 continue;
172 }
173 }
174 if (*cp == '+' || *cp == '@') {
175 if (folder)
176 adios (NULL, "only one folder at a time!");
177 folder = pluspath (cp);
178 } else
179 app_msgarg(&msgs, cp);
180 }
181 vec[vecp] = NULL;
182
183 if (!context_find ("path"))
184 free (path ("./", TFOLDER));
185
186 /*
187 * If we didn't specify which messages to search,
188 * then search the whole folder.
189 */
190 if (!msgs.size)
191 app_msgarg(&msgs, "all");
192
193 if (!folder)
194 folder = getfolder (1);
195 maildir = m_maildir (folder);
196
197 if (chdir (maildir) == NOTOK)
198 adios (maildir, "unable to change directory to");
199
200 /* read folder and create message structure */
201 if (!(mp = folder_read (folder, 0)))
202 adios (NULL, "unable to read folder %s", folder);
203
204 /* check for empty folder */
205 if (mp->nummsg == 0)
206 adios (NULL, "no messages in %s", folder);
207
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]))
211 done (1);
212
213 /*
214 * If we aren't saving the results to a sequence,
215 * we default to list the results.
216 */
217 if (listsw == -1)
218 listsw = !seqp;
219
220 if (publicsw == 1 && is_readonly(mp))
221 adios (NULL, "folder %s is read-only, so -public not allowed", folder);
222
223 if (!pcompile (vec, NULL))
224 done (1);
225
226 /* If printing message numbers to standard out, force line buffering on.
227 */
228 if (listsw)
229 setvbuf (stdout, NULL, _IOLBF, 0);
230
231 /*
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)
235 */
236 if (!reverse) { /* Overflow or underflow is fine. */
237 start = mp->lowsel;
238 end = mp->hghsel + 1;
239 inc = 1;
240 } else {
241 start = mp->hghsel;
242 end = mp->lowsel - 1;
243 inc = -1;
244 }
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)) {
250 if (listsw)
251 puts(m_name (msgnum));
252 } else {
253 app_msgnum(&nums, msgnum);
254 }
255 if (fp)
256 fclose (fp);
257 }
258 }
259
260 if (nums.size >= mp->numsel)
261 adios (NULL, "no messages match specification");
262
263 /*
264 * So, what's happening here?
265 *
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
271 */
272
273 if (!(mp2 = folder_read (folder, 1)))
274 adios (NULL, "unable to reread folder %s", folder);
275
276 for (msgnum = 0; msgnum < msgs.size; msgnum++)
277 if (!m_convert (mp2, msgs.msgs[msgnum]))
278 done (1);
279 seq_setprev (mp2); /* set the previous-sequence */
280
281 /*
282 * Nums contains a list of messages that we did NOT match. Remove
283 * that from the selection.
284 */
285
286 for (msgnum = 0; msgnum < nums.size; msgnum++) {
287 unset_selected(mp2, nums.msgnums[msgnum]);
288 mp2->numsel--;
289 }
290
291 /*
292 * Add the matching messages to sequences
293 */
294 if (seqp > 0) {
295 for (seqp = 0; seqp < svector_size (seqs); seqp++)
296 if (!seq_addsel (mp2, svector_at (seqs, seqp), publicsw, zerosw))
297 done (1);
298 }
299
300 /*
301 * Print total matched if not printing each matched message number.
302 */
303 if (!listsw) {
304 printf ("%d hit%s\n", mp2->numsel, PLURALS(mp2->numsel));
305 }
306
307 svector_free (seqs);
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 */
312 folder_free (mp2);
313 done (0);
314 return 1;
315 }
316
317
318 static void NORETURN
319 putzero_done (int status)
320 {
321 if (listsw && status && !isatty (fileno (stdout)))
322 puts("0");
323 exit (status);
324 }