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