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