]> diplodocus.org Git - nmh/blob - uip/pick.c
Bring these changes over from the branch.
[nmh] / uip / pick.c
1
2 /*
3 * pick.c -- search for messages by content
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <h/tws.h>
14 #include <h/picksbr.h>
15
16 /*
17 * We allocate space for messages (msgs array)
18 * this number of elements at a time.
19 */
20 #define MAXMSGS 256
21
22
23 static struct swit switches[] = {
24 #define ANDSW 0
25 { "and", 0 },
26 #define ORSW 1
27 { "or", 0 },
28 #define NOTSW 2
29 { "not", 0 },
30 #define LBRSW 3
31 { "lbrace", 0 },
32 #define RBRSW 4
33 { "rbrace", 0 },
34 #define CCSW 5
35 { "cc pattern", 0 },
36 #define DATESW 6
37 { "date pattern", 0 },
38 #define FROMSW 7
39 { "from pattern", 0 },
40 #define SRCHSW 8
41 { "search pattern", 0 },
42 #define SUBJSW 9
43 { "subject pattern", 0 },
44 #define TOSW 10
45 { "to pattern", 0 },
46 #define OTHRSW 11
47 { "-othercomponent pattern", 0 },
48 #define AFTRSW 12
49 { "after date", 0 },
50 #define BEFRSW 13
51 { "before date", 0 },
52 #define DATFDSW 14
53 { "datefield field", 5 },
54 #define SEQSW 15
55 { "sequence name", 0 },
56 #define PUBLSW 16
57 { "public", 0 },
58 #define NPUBLSW 17
59 { "nopublic", 0 },
60 #define ZEROSW 18
61 { "zero", 0 },
62 #define NZEROSW 19
63 { "nozero", 0 },
64 #define LISTSW 20
65 { "list", 0 },
66 #define NLISTSW 21
67 { "nolist", 0 },
68 #define VERSIONSW 22
69 { "version", 0 },
70 #define HELPSW 23
71 { "help", 0 },
72 { NULL, 0 }
73 };
74
75 static int listsw = 0;
76
77
78 int
79 main (int argc, char **argv)
80 {
81 int publicsw = -1, zerosw = 1, seqp = 0, vecp = 0;
82 int nummsgs, maxmsgs, lo, hi, msgnum;
83 char *maildir, *folder = NULL, buf[100];
84 char *cp, **argp, **arguments;
85 char **msgs, *seqs[NUMATTRS + 1], *vec[MAXARGS];
86 struct msgs *mp;
87 register FILE *fp;
88
89 #ifdef LOCALE
90 setlocale(LC_ALL, "");
91 #endif
92 invo_name = r1bindex (argv[0], '/');
93
94 /* read user profile/context */
95 context_read();
96
97 arguments = getarguments (invo_name, argc, argv, 1);
98 argp = arguments;
99
100 /*
101 * Allocate the initial space to record message
102 * names, ranges, and sequences.
103 */
104 nummsgs = 0;
105 maxmsgs = MAXMSGS;
106 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
107 adios (NULL, "unable to allocate storage");
108
109 while ((cp = *argp++)) {
110 if (*cp == '-') {
111 if (*++cp == '-') {
112 vec[vecp++] = --cp;
113 goto pattern;
114 }
115 switch (smatch (cp, switches)) {
116 case AMBIGSW:
117 ambigsw (cp, switches);
118 done (1);
119 case UNKWNSW:
120 adios (NULL, "-%s unknown", cp);
121
122 case HELPSW:
123 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
124 invo_name);
125 print_help (buf, switches, 1);
126 listsw = 0; /* HACK */
127 done (1);
128 case VERSIONSW:
129 print_version(invo_name);
130 done (1);
131
132 case CCSW:
133 case DATESW:
134 case FROMSW:
135 case SUBJSW:
136 case TOSW:
137 case DATFDSW:
138 case AFTRSW:
139 case BEFRSW:
140 case SRCHSW:
141 vec[vecp++] = --cp;
142 pattern:
143 if (!(cp = *argp++))/* allow -xyz arguments */
144 adios (NULL, "missing argument to %s", argp[-2]);
145 vec[vecp++] = cp;
146 continue;
147 case OTHRSW:
148 adios (NULL, "internal error!");
149
150 case ANDSW:
151 case ORSW:
152 case NOTSW:
153 case LBRSW:
154 case RBRSW:
155 vec[vecp++] = --cp;
156 continue;
157
158 case SEQSW:
159 if (!(cp = *argp++) || *cp == '-')
160 adios (NULL, "missing argument to %s", argp[-2]);
161
162 /* check if too many sequences specified */
163 if (seqp >= NUMATTRS)
164 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
165 seqs[seqp++] = cp;
166 listsw = 0;
167 continue;
168 case PUBLSW:
169 publicsw = 1;
170 continue;
171 case NPUBLSW:
172 publicsw = 0;
173 continue;
174 case ZEROSW:
175 zerosw++;
176 continue;
177 case NZEROSW:
178 zerosw = 0;
179 continue;
180
181 case LISTSW:
182 listsw++;
183 continue;
184 case NLISTSW:
185 listsw = 0;
186 continue;
187 }
188 }
189 if (*cp == '+' || *cp == '@') {
190 if (folder)
191 adios (NULL, "only one folder at a time!");
192 else
193 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
194 } else {
195 /*
196 * Check if we need to allocate more space
197 * for message name/ranges/sequences.
198 */
199 if (nummsgs >= maxmsgs) {
200 maxmsgs += MAXMSGS;
201 if (!(msgs = (char **) realloc (msgs,
202 (size_t) (maxmsgs * sizeof(*msgs)))))
203 adios (NULL, "unable to reallocate msgs storage");
204 }
205 msgs[nummsgs++] = cp;
206 }
207 }
208 vec[vecp] = NULL;
209
210 if (!context_find ("path"))
211 free (path ("./", TFOLDER));
212
213 /*
214 * If we didn't specify which messages to search,
215 * then search the whole folder.
216 */
217 if (!nummsgs)
218 msgs[nummsgs++] = "all";
219
220 if (!folder)
221 folder = getfolder (1);
222 maildir = m_maildir (folder);
223
224 if (chdir (maildir) == NOTOK)
225 adios (maildir, "unable to change directory to");
226
227 /* read folder and create message structure */
228 if (!(mp = folder_read (folder)))
229 adios (NULL, "unable to read folder %s", folder);
230
231 /* check for empty folder */
232 if (mp->nummsg == 0)
233 adios (NULL, "no messages in %s", folder);
234
235 /* parse all the message ranges/sequences and set SELECTED */
236 for (msgnum = 0; msgnum < nummsgs; msgnum++)
237 if (!m_convert (mp, msgs[msgnum]))
238 done (1);
239 seq_setprev (mp); /* set the previous-sequence */
240
241 /*
242 * If we aren't saving the results to a sequence,
243 * we need to list the results.
244 */
245 if (seqp == 0)
246 listsw++;
247
248 if (publicsw == 1 && is_readonly(mp))
249 adios (NULL, "folder %s is read-only, so -public not allowed", folder);
250
251 if (!pcompile (vec, NULL))
252 done (1);
253
254 lo = mp->lowsel;
255 hi = mp->hghsel;
256
257 /*
258 * Scan through all the SELECTED messages and check for a
259 * match. If the message does not match, then unselect it.
260 */
261 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
262 if (is_selected (mp, msgnum)) {
263 if ((fp = fopen (cp = m_name (msgnum), "r")) == NULL)
264 admonish (cp, "unable to read message");
265 if (fp && pmatches (fp, msgnum, 0L, 0L)) {
266 if (msgnum < lo)
267 lo = msgnum;
268 if (msgnum > hi)
269 hi = msgnum;
270 } else {
271 /* if it doesn't match, then unselect it */
272 unset_selected (mp, msgnum);
273 mp->numsel--;
274 }
275 if (fp)
276 fclose (fp);
277 }
278 }
279
280 mp->lowsel = lo;
281 mp->hghsel = hi;
282
283 if (mp->numsel <= 0)
284 adios (NULL, "no messages match specification");
285
286 seqs[seqp] = NULL;
287
288 /*
289 * Add the matching messages to sequences
290 */
291 for (seqp = 0; seqs[seqp]; seqp++)
292 if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
293 done (1);
294
295 /*
296 * Print the name of all the matches
297 */
298 if (listsw) {
299 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
300 if (is_selected (mp, msgnum))
301 printf ("%s\n", m_name (msgnum));
302 } else {
303 printf ("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
304 }
305
306 context_replace (pfolder, folder); /* update current folder */
307 seq_save (mp); /* synchronize message sequences */
308 context_save (); /* save the context file */
309 folder_free (mp); /* free folder/message structure */
310 return done (0);
311 }
312
313
314 int
315 done (int status)
316 {
317 if (listsw && status && !isatty (fileno (stdout)))
318 printf ("0\n");
319 exit (status);
320 return 1; /* dead code to satisfy the compiler */
321 }