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