]> diplodocus.org Git - nmh/blob - uip/pick.c
Use existing macros min() and max() more.
[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
13 #define PICK_SWITCHES \
14 X("reverse", 0, REVSW) \
15 X("and", 0, ANDSW) \
16 X("or", 0, ORSW) \
17 X("not", 0, NOTSW) \
18 X("lbrace", 0, LBRSW) \
19 X("rbrace", 0, RBRSW) \
20 X("cc pattern", 0, CCSW) \
21 X("date pattern", 0, DATESW) \
22 X("from pattern", 0, FROMSW) \
23 X("search pattern", 0, SRCHSW) \
24 X("subject pattern", 0, SUBJSW) \
25 X("to pattern", 0, TOSW) \
26 X("-othercomponent pattern", 0, OTHRSW) \
27 X("after date", 0, AFTRSW) \
28 X("before date", 0, BEFRSW) \
29 X("datefield field", 5, DATFDSW) \
30 X("sequence name", 0, SEQSW) \
31 X("nosequence", 0, NSEQSW) \
32 X("public", 0, PUBLSW) \
33 X("nopublic", 0, NPUBLSW) \
34 X("zero", 0, ZEROSW) \
35 X("nozero", 0, NZEROSW) \
36 X("list", 0, LISTSW) \
37 X("nolist", 0, NLISTSW) \
38 X("debug", 0, DEBUGSW) \
39 X("version", 0, VERSIONSW) \
40 X("help", 0, HELPSW) \
41
42 #define X(sw, minchars, id) id,
43 DEFINE_SWITCH_ENUM(PICK);
44 #undef X
45
46 #define X(sw, minchars, id) { sw, minchars, id },
47 DEFINE_SWITCH_ARRAY(PICK, switches);
48 #undef X
49
50 static int listsw = -1;
51
52 static void putzero_done (int) NORETURN;
53
54 int
55 main (int argc, char **argv)
56 {
57 int publicsw = -1, zerosw = 1, vecp = 0;
58 size_t seqp = 0;
59 int msgnum;
60 char *maildir, *folder = NULL, buf[100];
61 char *cp, **argp, **arguments;
62 svector_t seqs = svector_create (0);
63 char *vec[MAXARGS];
64 struct msgs_array msgs = { 0, 0, NULL };
65 struct msgnum_array nums = { 0, 0, NULL };
66 struct msgs *mp, *mp2;
67 FILE *fp;
68 int debug = 0;
69 int reverse = 0;
70 int start, end, inc;
71
72 if (nmh_init(argv[0], 1)) { return 1; }
73
74 done=putzero_done;
75
76 /* Deprecated. Use -debug instead. */
77 if ((cp = getenv ("MHPDEBUG")) && *cp)
78 ++debug;
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 adios (NULL, "-%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 = 1;
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 adios (NULL, "missing argument to %s", argp[-2]);
125 vec[vecp++] = cp;
126 continue;
127 case OTHRSW:
128 adios (NULL, "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 adios (NULL, "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++;
159 continue;
160 case NZEROSW:
161 zerosw = 0;
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 adios (NULL, "only one folder at a time!");
179 else
180 folder = pluspath (cp);
181 } else
182 app_msgarg(&msgs, cp);
183 }
184 vec[vecp] = NULL;
185
186 if (!context_find ("path"))
187 free (path ("./", TFOLDER));
188
189 /*
190 * If we didn't specify which messages to search,
191 * then search the whole folder.
192 */
193 if (!msgs.size)
194 app_msgarg(&msgs, "all");
195
196 if (!folder)
197 folder = getfolder (1);
198 maildir = m_maildir (folder);
199
200 if (chdir (maildir) == NOTOK)
201 adios (maildir, "unable to change directory to");
202
203 /* read folder and create message structure */
204 if (!(mp = folder_read (folder, 0)))
205 adios (NULL, "unable to read folder %s", folder);
206
207 /* check for empty folder */
208 if (mp->nummsg == 0)
209 adios (NULL, "no messages in %s", folder);
210
211 /* parse all the message ranges/sequences and set SELECTED */
212 for (msgnum = 0; msgnum < msgs.size; msgnum++)
213 if (!m_convert (mp, msgs.msgs[msgnum]))
214 done (1);
215
216 /*
217 * If we aren't saving the results to a sequence,
218 * we default to list the results.
219 */
220 if (listsw == -1)
221 listsw = !seqp;
222
223 if (publicsw == 1 && is_readonly(mp))
224 adios (NULL, "folder %s is read-only, so -public not allowed", folder);
225
226 if (!pcompile (vec, NULL))
227 done (1);
228
229 /* If printing message numbers to standard out, force line buffering on.
230 */
231 if (listsw)
232 setvbuf (stdout, NULL, _IOLBF, 0);
233
234 /*
235 * Scan through all the SELECTED messages and check for a
236 * match. If there is NOT a match, then add it to a list to
237 * remove from the final sequence (it will make sense later)
238 */
239 if (!reverse) { /* Overflow or underflow is fine. */
240 start = mp->lowsel;
241 end = mp->hghsel + 1;
242 inc = 1;
243 } else {
244 start = mp->hghsel;
245 end = mp->lowsel - 1;
246 inc = -1;
247 }
248 for (msgnum = start; msgnum != end; msgnum += inc) {
249 if (is_selected (mp, msgnum)) {
250 if ((fp = fopen (cp = m_name (msgnum), "r")) == NULL)
251 admonish (cp, "unable to read message");
252 if (fp && pmatches (fp, msgnum, 0L, 0L, debug)) {
253 if (listsw)
254 puts(m_name (msgnum));
255 } else {
256 app_msgnum(&nums, msgnum);
257 }
258 if (fp)
259 fclose (fp);
260 }
261 }
262
263 if (nums.size >= mp->numsel)
264 adios (NULL, "no messages match specification");
265
266 /*
267 * So, what's happening here?
268 *
269 * - Re-read the folder and sequences, but with locking.
270 * - Recreate the original set of selected messages from the command line
271 * - Set the previous sequence
272 * - Remove any messages that did NOT result in hits from the selection
273 * - Add to any new sequences
274 */
275
276 if (!(mp2 = folder_read (folder, 1)))
277 adios (NULL, "unable to reread folder %s", folder);
278
279 for (msgnum = 0; msgnum < msgs.size; msgnum++)
280 if (!m_convert (mp2, msgs.msgs[msgnum]))
281 done (1);
282 seq_setprev (mp2); /* set the previous-sequence */
283
284 /*
285 * Nums contains a list of messages that we did NOT match. Remove
286 * that from the selection.
287 */
288
289 for (msgnum = 0; msgnum < nums.size; msgnum++) {
290 unset_selected(mp2, nums.msgnums[msgnum]);
291 mp2->numsel--;
292 }
293
294 /*
295 * Add the matching messages to sequences
296 */
297 if (seqp > 0) {
298 for (seqp = 0; seqp < svector_size (seqs); seqp++)
299 if (!seq_addsel (mp2, svector_at (seqs, seqp), publicsw, zerosw))
300 done (1);
301 }
302
303 /*
304 * Print total matched if not printing each matched message number.
305 */
306 if (!listsw) {
307 printf ("%d hit%s\n", mp2->numsel, mp2->numsel == 1 ? "" : "s");
308 }
309
310 svector_free (seqs);
311 context_replace (pfolder, folder); /* update current folder */
312 seq_save (mp2); /* synchronize message sequences */
313 context_save (); /* save the context file */
314 folder_free (mp); /* free folder/message structure */
315 folder_free (mp2);
316 done (0);
317 return 1;
318 }
319
320
321 static void
322 putzero_done (int status)
323 {
324 if (listsw && status && !isatty (fileno (stdout)))
325 puts("0");
326 exit (status);
327 }