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