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