File: pick.c

package info (click to toggle)
nmh 1.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,056 kB
  • ctags: 4,531
  • sloc: ansic: 50,788; sh: 3,141; makefile: 965; awk: 74
file content (300 lines) | stat: -rw-r--r-- 7,122 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300

/*
 * pick.c -- search for messages by content
 *
 * $Id: pick.c,v 1.12 2007/11/04 11:54:35 jjr Exp $
 *
 * This code is Copyright (c) 2002, by the authors of nmh.  See the
 * COPYRIGHT file in the root directory of the nmh distribution for
 * complete copyright information.
 */

#include <h/mh.h>
#include <h/tws.h>
#include <h/picksbr.h>
#include <h/utils.h>

static struct swit switches[] = {
#define	ANDSW                   0
    { "and", 0 },
#define	ORSW                    1
    { "or", 0 },
#define	NOTSW                   2
    { "not", 0 },
#define	LBRSW                   3
    { "lbrace", 0 },
#define	RBRSW                   4
    { "rbrace", 0 },
#define	CCSW                    5
    { "cc  pattern", 0 },
#define	DATESW                  6
    { "date  pattern", 0 },
#define	FROMSW                  7
    { "from  pattern", 0 },
#define	SRCHSW                  8
    { "search  pattern", 0 },
#define	SUBJSW                  9
    { "subject  pattern", 0 },
#define	TOSW                   10
    { "to  pattern", 0 },
#define	OTHRSW                 11
    { "-othercomponent  pattern", 0 },
#define	AFTRSW                 12
    { "after date", 0 },
#define	BEFRSW                 13
    { "before date", 0 },
#define	DATFDSW                14
    { "datefield field", 5 },
#define	SEQSW                  15
    { "sequence name", 0 },
#define	PUBLSW                 16
    { "public", 0 },
#define	NPUBLSW                17
    { "nopublic", 0 },
#define	ZEROSW                 18
    { "zero", 0 },
#define	NZEROSW                19
    { "nozero", 0 },
#define	LISTSW                 20
    { "list", 0 },
#define	NLISTSW                21
    { "nolist", 0 },
#define VERSIONSW              22
    { "version", 0 },
#define	HELPSW                 23
    { "help", 0 },
    { NULL, 0 }
};

static int listsw = -1;

static void putzero_done (int);

int
main (int argc, char **argv)
{
    int publicsw = -1, zerosw = 1, seqp = 0, vecp = 0;
    int lo, hi, msgnum;
    char *maildir, *folder = NULL, buf[100];
    char *cp, **argp, **arguments;
    char *seqs[NUMATTRS + 1], *vec[MAXARGS];
    struct msgs_array msgs = { 0, 0, NULL };
    struct msgs *mp;
    register FILE *fp;

    done=putzero_done;

#ifdef LOCALE
    setlocale(LC_ALL, "");
#endif
    invo_name = r1bindex (argv[0], '/');

    /* read user profile/context */
    context_read();

    arguments = getarguments (invo_name, argc, argv, 1);
    argp = arguments;

    while ((cp = *argp++)) {
	if (*cp == '-') {
	    if (*++cp == '-') {
		vec[vecp++] = --cp;
		goto pattern;
	    }
	    switch (smatch (cp, switches)) {
	    case AMBIGSW: 
		ambigsw (cp, switches);
		listsw = 0;	/* HACK */
		done (1);
	    case UNKWNSW: 
		adios (NULL, "-%s unknown", cp);

	    case HELPSW: 
		snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
			  invo_name);
		print_help (buf, switches, 1);
		listsw = 0;	/* HACK */
		done (1);
	    case VERSIONSW:
		print_version(invo_name);
		listsw = 0;	/* HACK */
		done (1);

	    case CCSW: 
	    case DATESW: 
	    case FROMSW: 
	    case SUBJSW: 
	    case TOSW: 
	    case DATFDSW: 
	    case AFTRSW: 
	    case BEFRSW: 
	    case SRCHSW: 
		vec[vecp++] = --cp;
	    pattern:
		if (!(cp = *argp++))/* allow -xyz arguments */
		    adios (NULL, "missing argument to %s", argp[-2]);
		vec[vecp++] = cp;
		continue;
	    case OTHRSW: 
		adios (NULL, "internal error!");

	    case ANDSW:
	    case ORSW:
	    case NOTSW:
	    case LBRSW:
	    case RBRSW:
		vec[vecp++] = --cp;
		continue;

	    case SEQSW: 
		if (!(cp = *argp++) || *cp == '-')
		    adios (NULL, "missing argument to %s", argp[-2]);

		/* check if too many sequences specified */
		if (seqp >= NUMATTRS)
		    adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
		seqs[seqp++] = cp;
		continue;
	    case PUBLSW: 
		publicsw = 1;
		continue;
	    case NPUBLSW: 
		publicsw = 0;
		continue;
	    case ZEROSW: 
		zerosw++;
		continue;
	    case NZEROSW: 
		zerosw = 0;
		continue;

	    case LISTSW: 
		listsw = 1;
		continue;
	    case NLISTSW: 
		listsw = 0;
		continue;
	    }
	}
	if (*cp == '+' || *cp == '@') {
	    if (folder)
		adios (NULL, "only one folder at a time!");
	    else
		folder = pluspath (cp);
	} else
		app_msgarg(&msgs, cp);
    }
    vec[vecp] = NULL;

    if (!context_find ("path"))
	free (path ("./", TFOLDER));

    /*
     * If we didn't specify which messages to search,
     * then search the whole folder.
     */
    if (!msgs.size)
	app_msgarg(&msgs, "all");

    if (!folder)
	folder = getfolder (1);
    maildir = m_maildir (folder);

    if (chdir (maildir) == NOTOK)
	adios (maildir, "unable to change directory to");

    /* read folder and create message structure */
    if (!(mp = folder_read (folder)))
	adios (NULL, "unable to read folder %s", folder);

    /* check for empty folder */
    if (mp->nummsg == 0)
	adios (NULL, "no messages in %s", folder);

    /* parse all the message ranges/sequences and set SELECTED */
    for (msgnum = 0; msgnum < msgs.size; msgnum++)
	if (!m_convert (mp, msgs.msgs[msgnum]))
	    done (1);
    seq_setprev (mp);	/* set the previous-sequence */

    /*
     * If we aren't saving the results to a sequence,
     * we default to list the results.
     */
    if (listsw == -1)
	listsw = !seqp;

    if (publicsw == 1 && is_readonly(mp))
	adios (NULL, "folder %s is read-only, so -public not allowed", folder);

    if (!pcompile (vec, NULL))
	done (1);

    lo = mp->lowsel;
    hi = mp->hghsel;

    /*
     * Scan through all the SELECTED messages and check for a
     * match.  If the message does not match, then unselect it.
     */
    for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
	if (is_selected (mp, msgnum)) {
	    if ((fp = fopen (cp = m_name (msgnum), "r")) == NULL)
		admonish (cp, "unable to read message");
	    if (fp && pmatches (fp, msgnum, 0L, 0L)) {
		if (msgnum < lo)
		    lo = msgnum;
		if (msgnum > hi)
		    hi = msgnum;
	    } else {
		/* if it doesn't match, then unselect it */
		unset_selected (mp, msgnum);
		mp->numsel--;
	    }
	    if (fp)
		fclose (fp);
	}
    }

    mp->lowsel = lo;
    mp->hghsel = hi;

    if (mp->numsel <= 0)
	adios (NULL, "no messages match specification");

    seqs[seqp] = NULL;

    /*
     * Add the matching messages to sequences
     */
    for (seqp = 0; seqs[seqp]; seqp++)
	if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
	    done (1);

    /*
     * Print the name of all the matches
     */
    if (listsw) {
	for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
	    if (is_selected (mp, msgnum))
		printf ("%s\n", m_name (msgnum));
    } else {
	printf ("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
    }

    context_replace (pfolder, folder);	/* update current folder         */
    seq_save (mp);			/* synchronize message sequences */
    context_save ();			/* save the context file         */
    folder_free (mp);			/* free folder/message structure */
    done (0);
    return 1;
}


static void
putzero_done (int status)
{
    if (listsw && status && !isatty (fileno (stdout)))
	printf ("0\n");
    exit (status);
}