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