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