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