]> diplodocus.org Git - nmh/blob - uip/pick.c
[bug #4296] PUTD() macro can't hack 10 digit values
[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 = -1;
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 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 if (!(msgs = (char **) realloc (msgs,
203 (size_t) (maxmsgs * sizeof(*msgs)))))
204 adios (NULL, "unable to reallocate msgs storage");
205 }
206 msgs[nummsgs++] = cp;
207 }
208 }
209 vec[vecp] = NULL;
210
211 if (!context_find ("path"))
212 free (path ("./", TFOLDER));
213
214 /*
215 * If we didn't specify which messages to search,
216 * then search the whole folder.
217 */
218 if (!nummsgs)
219 msgs[nummsgs++] = "all";
220
221 if (!folder)
222 folder = getfolder (1);
223 maildir = m_maildir (folder);
224
225 if (chdir (maildir) == NOTOK)
226 adios (maildir, "unable to change directory to");
227
228 /* read folder and create message structure */
229 if (!(mp = folder_read (folder)))
230 adios (NULL, "unable to read folder %s", folder);
231
232 /* check for empty folder */
233 if (mp->nummsg == 0)
234 adios (NULL, "no messages in %s", folder);
235
236 /* parse all the message ranges/sequences and set SELECTED */
237 for (msgnum = 0; msgnum < nummsgs; msgnum++)
238 if (!m_convert (mp, msgs[msgnum]))
239 done (1);
240 seq_setprev (mp); /* set the previous-sequence */
241
242 /*
243 * If we aren't saving the results to a sequence,
244 * we default to list the results.
245 */
246 if (listsw == -1)
247 listsw = !seqp;
248
249 if (publicsw == 1 && is_readonly(mp))
250 adios (NULL, "folder %s is read-only, so -public not allowed", folder);
251
252 if (!pcompile (vec, NULL))
253 done (1);
254
255 lo = mp->lowsel;
256 hi = mp->hghsel;
257
258 /*
259 * Scan through all the SELECTED messages and check for a
260 * match. If the message does not match, then unselect it.
261 */
262 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
263 if (is_selected (mp, msgnum)) {
264 if ((fp = fopen (cp = m_name (msgnum), "r")) == NULL)
265 admonish (cp, "unable to read message");
266 if (fp && pmatches (fp, msgnum, 0L, 0L)) {
267 if (msgnum < lo)
268 lo = msgnum;
269 if (msgnum > hi)
270 hi = msgnum;
271 } else {
272 /* if it doesn't match, then unselect it */
273 unset_selected (mp, msgnum);
274 mp->numsel--;
275 }
276 if (fp)
277 fclose (fp);
278 }
279 }
280
281 mp->lowsel = lo;
282 mp->hghsel = hi;
283
284 if (mp->numsel <= 0)
285 adios (NULL, "no messages match specification");
286
287 seqs[seqp] = NULL;
288
289 /*
290 * Add the matching messages to sequences
291 */
292 for (seqp = 0; seqs[seqp]; seqp++)
293 if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
294 done (1);
295
296 /*
297 * Print the name of all the matches
298 */
299 if (listsw) {
300 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
301 if (is_selected (mp, msgnum))
302 printf ("%s\n", m_name (msgnum));
303 } else {
304 printf ("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
305 }
306
307 context_replace (pfolder, folder); /* update current folder */
308 seq_save (mp); /* synchronize message sequences */
309 context_save (); /* save the context file */
310 folder_free (mp); /* free folder/message structure */
311 return done (0);
312 }
313
314
315 int
316 done (int status)
317 {
318 if (listsw && status && !isatty (fileno (stdout)))
319 printf ("0\n");
320 exit (status);
321 return 1; /* dead code to satisfy the compiler */
322 }