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