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