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