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