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