]> diplodocus.org Git - nmh/blob - uip/pick.c
90cd78cebc0faa38b55c8a86ea3bc546607fdb6a
[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 "sbr/seq_save.h"
10 #include "sbr/smatch.h"
11 #include "sbr/m_convert.h"
12 #include "sbr/getfolder.h"
13 #include "sbr/folder_read.h"
14 #include "sbr/folder_free.h"
15 #include "sbr/context_save.h"
16 #include "sbr/context_replace.h"
17 #include "sbr/context_find.h"
18 #include "sbr/ambigsw.h"
19 #include "sbr/path.h"
20 #include "sbr/print_version.h"
21 #include "sbr/print_help.h"
22 #include "sbr/seq_nameok.h"
23 #include "sbr/seq_add.h"
24 #include "sbr/error.h"
25 #include "h/tws.h"
26 #include "h/picksbr.h"
27 #include "h/done.h"
28 #include "h/utils.h"
29 #include "sbr/m_maildir.h"
30
31 #define PICK_SWITCHES \
32 X("reverse", 0, REVSW) \
33 X("and", 0, ANDSW) \
34 X("or", 0, ORSW) \
35 X("not", 0, NOTSW) \
36 X("lbrace", 0, LBRSW) \
37 X("rbrace", 0, RBRSW) \
38 X("cc pattern", 0, CCSW) \
39 X("date pattern", 0, DATESW) \
40 X("from pattern", 0, FROMSW) \
41 X("search pattern", 0, SRCHSW) \
42 X("subject pattern", 0, SUBJSW) \
43 X("to pattern", 0, TOSW) \
44 X("-othercomponent pattern", 0, OTHRSW) \
45 X("after date", 0, AFTRSW) \
46 X("before date", 0, BEFRSW) \
47 X("datefield field", 5, DATFDSW) \
48 X("sequence name", 0, SEQSW) \
49 X("nosequence", 0, NSEQSW) \
50 X("public", 0, PUBLSW) \
51 X("nopublic", 0, NPUBLSW) \
52 X("zero", 0, ZEROSW) \
53 X("nozero", 0, NZEROSW) \
54 X("list", 0, LISTSW) \
55 X("nolist", 0, NLISTSW) \
56 X("debug", 0, DEBUGSW) \
57 X("version", 0, VERSIONSW) \
58 X("help", 0, HELPSW) \
59
60 #define X(sw, minchars, id) id,
61 DEFINE_SWITCH_ENUM(PICK);
62 #undef X
63
64 #define X(sw, minchars, id) { sw, minchars, id },
65 DEFINE_SWITCH_ARRAY(PICK, switches);
66 #undef X
67
68 static int listsw = -1;
69
70 static void putzero_done (int) NORETURN;
71
72 int
73 main (int argc, char **argv)
74 {
75 int publicsw = -1;
76 bool zerosw = true;
77 int vecp = 0;
78 size_t seqp = 0;
79 int msgnum;
80 char *maildir, *folder = NULL, buf[100];
81 char *cp, **argp, **arguments;
82 svector_t seqs = svector_create (0);
83 char *vec[MAXARGS];
84 struct msgs_array msgs = { 0, 0, NULL };
85 struct msgnum_array nums = { 0, 0, NULL };
86 struct msgs *mp, *mp2;
87 FILE *fp;
88 int debug = 0;
89 bool reverse = false;
90 int start, end, inc;
91
92 if (nmh_init(argv[0], true, true)) { return 1; }
93
94 set_done(putzero_done);
95
96 arguments = getarguments (invo_name, argc, argv, 1);
97 argp = arguments;
98
99 while ((cp = *argp++)) {
100 if (*cp == '-') {
101 if (*++cp == '-') {
102 vec[vecp++] = --cp;
103 goto pattern;
104 }
105 switch (smatch (cp, switches)) {
106 case AMBIGSW:
107 ambigsw (cp, switches);
108 listsw = 0; /* HACK */
109 done (1);
110 case UNKWNSW:
111 die("-%s unknown", cp);
112
113 case HELPSW:
114 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
115 invo_name);
116 print_help (buf, switches, 1);
117 listsw = 0; /* HACK */
118 done (0);
119 case VERSIONSW:
120 print_version(invo_name);
121 listsw = 0; /* HACK */
122 done (0);
123
124 case REVSW:
125 reverse = true;
126 continue;
127
128 case CCSW:
129 case DATESW:
130 case FROMSW:
131 case SUBJSW:
132 case TOSW:
133 case DATFDSW:
134 case AFTRSW:
135 case BEFRSW:
136 case SRCHSW:
137 vec[vecp++] = --cp;
138 pattern:
139 if (!(cp = *argp++))/* allow -xyz arguments */
140 die("missing argument to %s", argp[-2]);
141 vec[vecp++] = cp;
142 continue;
143 case OTHRSW:
144 die("internal error!");
145
146 case ANDSW:
147 case ORSW:
148 case NOTSW:
149 case LBRSW:
150 case RBRSW:
151 vec[vecp++] = --cp;
152 continue;
153
154 case SEQSW:
155 if (!(cp = *argp++) || *cp == '-')
156 die("missing argument to %s", argp[-2]);
157
158 if (!seq_nameok (cp))
159 done (1);
160
161 svector_push_back (seqs, cp);
162 seqp++;
163 continue;
164 case NSEQSW:
165 seqp = 0;
166 continue;
167 case PUBLSW:
168 publicsw = 1;
169 continue;
170 case NPUBLSW:
171 publicsw = 0;
172 continue;
173 case ZEROSW:
174 zerosw = true;
175 continue;
176 case NZEROSW:
177 zerosw = false;
178 continue;
179
180 case LISTSW:
181 listsw = 1;
182 continue;
183 case NLISTSW:
184 listsw = 0;
185 continue;
186
187 case DEBUGSW:
188 ++debug;
189 continue;
190 }
191 }
192 if (*cp == '+' || *cp == '@') {
193 if (folder)
194 die("only one folder at a time!");
195 folder = pluspath (cp);
196 } else
197 app_msgarg(&msgs, cp);
198 }
199 vec[vecp] = NULL;
200
201 if (!context_find ("path"))
202 free (path ("./", TFOLDER));
203
204 /*
205 * If we didn't specify which messages to search,
206 * then search the whole folder.
207 */
208 if (!msgs.size)
209 app_msgarg(&msgs, "all");
210
211 if (!folder)
212 folder = getfolder (1);
213 maildir = m_maildir (folder);
214
215 if (chdir (maildir) == NOTOK)
216 adios (maildir, "unable to change directory to");
217
218 /* read folder and create message structure */
219 if (!(mp = folder_read (folder, 0)))
220 die("unable to read folder %s", folder);
221
222 /* check for empty folder */
223 if (mp->nummsg == 0)
224 die("no messages in %s", folder);
225
226 /* parse all the message ranges/sequences and set SELECTED */
227 for (msgnum = 0; msgnum < msgs.size; msgnum++)
228 if (!m_convert (mp, msgs.msgs[msgnum]))
229 done (1);
230
231 /*
232 * If we aren't saving the results to a sequence,
233 * we default to list the results.
234 */
235 if (listsw == -1)
236 listsw = !seqp;
237
238 if (publicsw == 1 && is_readonly(mp))
239 die("folder %s is read-only, so -public not allowed", folder);
240
241 if (!pcompile (vec, NULL))
242 done (1);
243
244 /* If printing message numbers to standard out, force line buffering on.
245 */
246 if (listsw)
247 setvbuf (stdout, NULL, _IOLBF, 0);
248
249 /*
250 * Scan through all the SELECTED messages and check for a
251 * match. If there is NOT a match, then add it to a list to
252 * remove from the final sequence (it will make sense later)
253 */
254 if (!reverse) { /* Overflow or underflow is fine. */
255 start = mp->lowsel;
256 end = mp->hghsel + 1;
257 inc = 1;
258 } else {
259 start = mp->hghsel;
260 end = mp->lowsel - 1;
261 inc = -1;
262 }
263 for (msgnum = start; msgnum != end; msgnum += inc) {
264 if (is_selected (mp, msgnum)) {
265 if ((fp = fopen (cp = m_name (msgnum), "r")) == NULL)
266 admonish (cp, "unable to read message");
267 if (fp && pmatches (fp, msgnum, 0L, 0L, debug)) {
268 if (listsw)
269 puts(m_name (msgnum));
270 } else {
271 app_msgnum(&nums, msgnum);
272 }
273 if (fp)
274 fclose (fp);
275 }
276 }
277
278 if (nums.size >= mp->numsel)
279 die("no messages match specification");
280
281 /*
282 * So, what's happening here?
283 *
284 * - Re-read the folder and sequences, but with locking.
285 * - Recreate the original set of selected messages from the command line
286 * - Set the previous sequence
287 * - Remove any messages that did NOT result in hits from the selection
288 * - Add to any new sequences
289 */
290
291 if (!(mp2 = folder_read (folder, 1)))
292 die("unable to reread folder %s", folder);
293
294 for (msgnum = 0; msgnum < msgs.size; msgnum++)
295 if (!m_convert (mp2, msgs.msgs[msgnum]))
296 done (1);
297 seq_setprev (mp2); /* set the previous-sequence */
298
299 /*
300 * Nums contains a list of messages that we did NOT match. Remove
301 * that from the selection.
302 */
303
304 for (msgnum = 0; msgnum < nums.size; msgnum++) {
305 unset_selected(mp2, nums.msgnums[msgnum]);
306 mp2->numsel--;
307 }
308
309 /*
310 * Add the matching messages to sequences
311 */
312 if (seqp > 0) {
313 for (seqp = 0; seqp < svector_size (seqs); seqp++)
314 if (!seq_addsel (mp2, svector_at (seqs, seqp), publicsw, zerosw))
315 done (1);
316 }
317
318 /*
319 * Print total matched if not printing each matched message number.
320 */
321 if (!listsw) {
322 printf ("%d hit%s\n", mp2->numsel, PLURALS(mp2->numsel));
323 }
324
325 svector_free (seqs);
326 context_replace (pfolder, folder); /* update current folder */
327 seq_save (mp2); /* synchronize message sequences */
328 context_save (); /* save the context file */
329 folder_free (mp); /* free folder/message structure */
330 folder_free (mp2);
331 done (0);
332 return 1;
333 }
334
335
336 static void NORETURN
337 putzero_done (int status)
338 {
339 if (listsw && status && !isatty (fileno (stdout)))
340 puts("0");
341 exit (status);
342 }