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