]> diplodocus.org Git - nmh/blob - uip/pick.c
* uip/sendsbr.c: with attachformat values of 1 or 2, only
[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 #include <h/utils.h>
16
17 /*
18 * We allocate space for messages (msgs array)
19 * this number of elements at a time.
20 */
21 #define MAXMSGS 256
22
23
24 static struct swit switches[] = {
25 #define ANDSW 0
26 { "and", 0 },
27 #define ORSW 1
28 { "or", 0 },
29 #define NOTSW 2
30 { "not", 0 },
31 #define LBRSW 3
32 { "lbrace", 0 },
33 #define RBRSW 4
34 { "rbrace", 0 },
35 #define CCSW 5
36 { "cc pattern", 0 },
37 #define DATESW 6
38 { "date pattern", 0 },
39 #define FROMSW 7
40 { "from pattern", 0 },
41 #define SRCHSW 8
42 { "search pattern", 0 },
43 #define SUBJSW 9
44 { "subject pattern", 0 },
45 #define TOSW 10
46 { "to pattern", 0 },
47 #define OTHRSW 11
48 { "-othercomponent pattern", 0 },
49 #define AFTRSW 12
50 { "after date", 0 },
51 #define BEFRSW 13
52 { "before date", 0 },
53 #define DATFDSW 14
54 { "datefield field", 5 },
55 #define SEQSW 15
56 { "sequence name", 0 },
57 #define PUBLSW 16
58 { "public", 0 },
59 #define NPUBLSW 17
60 { "nopublic", 0 },
61 #define ZEROSW 18
62 { "zero", 0 },
63 #define NZEROSW 19
64 { "nozero", 0 },
65 #define LISTSW 20
66 { "list", 0 },
67 #define NLISTSW 21
68 { "nolist", 0 },
69 #define VERSIONSW 22
70 { "version", 0 },
71 #define HELPSW 23
72 { "help", 0 },
73 { NULL, 0 }
74 };
75
76 static int listsw = -1;
77
78
79 int
80 main (int argc, char **argv)
81 {
82 int publicsw = -1, zerosw = 1, seqp = 0, vecp = 0;
83 int nummsgs, maxmsgs, lo, hi, msgnum;
84 char *maildir, *folder = NULL, buf[100];
85 char *cp, **argp, **arguments;
86 char **msgs, *seqs[NUMATTRS + 1], *vec[MAXARGS];
87 struct msgs *mp;
88 register FILE *fp;
89
90 #ifdef LOCALE
91 setlocale(LC_ALL, "");
92 #endif
93 invo_name = r1bindex (argv[0], '/');
94
95 /* read user profile/context */
96 context_read();
97
98 arguments = getarguments (invo_name, argc, argv, 1);
99 argp = arguments;
100
101 /*
102 * Allocate the initial space to record message
103 * names, ranges, and sequences.
104 */
105 nummsgs = 0;
106 maxmsgs = MAXMSGS;
107 msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs)));
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 listsw = 0; /* HACK */
119 done (1);
120 case UNKWNSW:
121 adios (NULL, "-%s unknown", cp);
122
123 case HELPSW:
124 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
125 invo_name);
126 print_help (buf, switches, 1);
127 listsw = 0; /* HACK */
128 done (1);
129 case VERSIONSW:
130 print_version(invo_name);
131 listsw = 0; /* HACK */
132 done (1);
133
134 case CCSW:
135 case DATESW:
136 case FROMSW:
137 case SUBJSW:
138 case TOSW:
139 case DATFDSW:
140 case AFTRSW:
141 case BEFRSW:
142 case SRCHSW:
143 vec[vecp++] = --cp;
144 pattern:
145 if (!(cp = *argp++))/* allow -xyz arguments */
146 adios (NULL, "missing argument to %s", argp[-2]);
147 vec[vecp++] = cp;
148 continue;
149 case OTHRSW:
150 adios (NULL, "internal error!");
151
152 case ANDSW:
153 case ORSW:
154 case NOTSW:
155 case LBRSW:
156 case RBRSW:
157 vec[vecp++] = --cp;
158 continue;
159
160 case SEQSW:
161 if (!(cp = *argp++) || *cp == '-')
162 adios (NULL, "missing argument to %s", argp[-2]);
163
164 /* check if too many sequences specified */
165 if (seqp >= NUMATTRS)
166 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
167 seqs[seqp++] = cp;
168 continue;
169 case PUBLSW:
170 publicsw = 1;
171 continue;
172 case NPUBLSW:
173 publicsw = 0;
174 continue;
175 case ZEROSW:
176 zerosw++;
177 continue;
178 case NZEROSW:
179 zerosw = 0;
180 continue;
181
182 case LISTSW:
183 listsw = 1;
184 continue;
185 case NLISTSW:
186 listsw = 0;
187 continue;
188 }
189 }
190 if (*cp == '+' || *cp == '@') {
191 if (folder)
192 adios (NULL, "only one folder at a time!");
193 else
194 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
195 } else {
196 /*
197 * Check if we need to allocate more space
198 * for message name/ranges/sequences.
199 */
200 if (nummsgs >= maxmsgs) {
201 maxmsgs += MAXMSGS;
202 msgs = (char **) mh_xrealloc (msgs,
203 (size_t) (maxmsgs * sizeof(*msgs)));
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 default to list the results.
244 */
245 if (listsw == -1)
246 listsw = !seqp;
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 }