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