File: m_convert.c

package info (click to toggle)
nmh 1.0.2-9
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,172 kB
  • ctags: 4,449
  • sloc: ansic: 50,696; sh: 1,799; makefile: 1,192; awk: 74; sed: 17
file content (443 lines) | stat: -rw-r--r-- 8,774 bytes parent folder | download | duplicates (2)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443

/*
 * m_convert.c -- parse a message range or sequence and set SELECTED
 *
 * $Id: m_convert.c,v 1.1.1.1 1999/04/30 18:08:34 doug Exp $
 */

#include <h/mh.h>

/*
 * error codes for sequence
 * and message range processing
 */
#define	BADMSG	(-2)
#define	BADRNG	(-3)
#define	BADNEW	(-4)
#define	BADNUM	(-5)
#define	BADLST	(-6)

#define	FIRST	1
#define	LAST	2

#define	getnew(mp) (mp->hghmsg + 1)

static int convdir;	/* convert direction */
static char *delimp;

/*
 * static prototypes
 */
static int m_conv (struct msgs *, char *, int);
static int attr (struct msgs *, char *);


int
m_convert (struct msgs *mp, char *name)
{
    int first, last, found, range, err;
    char *bp, *cp;

    /* check if user defined sequence */
    err = attr (mp, cp = name);

    if (err == -1)
	return 0;
    else if (err < 0)
	goto badmsg;
    else if (err > 0)
	return 1;
    /*
     * else err == 0, so continue
     */

    found = 0;

    /*
     * Check for special "new" sequence, which
     * is valid only if ALLOW_NEW is set.
     */
    if ((mp->msgflags & ALLOW_NEW) && !strcmp (cp, "new")) {
	if ((err = first = getnew (mp)) <= 0)
	    goto badmsg;
	else
	    goto single;
    }

    if (!strcmp (cp, "all"))
	cp = "first-last";

    if ((err = first = m_conv (mp, cp, FIRST)) <= 0)
	goto badmsg;

    cp = delimp;
    if (*cp != '\0' && *cp != '-' && *cp != ':') {
badelim:
	advise (NULL, "illegal argument delimiter: `%c'(0%o)", *delimp, *delimp);
	return 0;
    }

    if (*cp == '-') {
	cp++;
	if ((err = last = m_conv (mp, cp, LAST)) <= 0) {
badmsg:
	    switch (err) {
	    case BADMSG: 
		advise (NULL, "no %s message", cp);
		break;

	    case BADNUM: 
		advise (NULL, "message %s doesn't exist", cp);
		break;

	    case BADRNG: 
		advise (NULL, "message %s out of range 1-%d", cp, mp->hghmsg);
		break;

	    case BADLST: 
badlist:
		advise (NULL, "bad message list %s", name);
		break;

	    case BADNEW:
		advise (NULL, "folder full, no %s message", name);
		break;

	    default: 
		advise (NULL, "no messages match specification");
	    }
	    return 0;
	}

	if (last < first)
	    goto badlist;
	if (*delimp)
	    goto badelim;
	if (first > mp->hghmsg || last < mp->lowmsg) {
rangerr:
	    advise (NULL, "no messages in range %s", name);
	    return 0;
	}

	/* tighten the range to search */
	if (last > mp->hghmsg)
	    last = mp->hghmsg;
	if (first < mp->lowmsg)
	    first = mp->lowmsg;

    } else if (*cp == ':') {
	cp++;
	if (*cp == '-') {
	    convdir = -1;
	    cp++;
	} else {
	    if (*cp == '+') {
		convdir = 1;
		cp++;
	    }
	}
	if ((range = atoi (bp = cp)) == 0)
	    goto badlist;
	while (isdigit (*bp))
	    bp++;
	if (*bp)
	    goto badelim;
	if ((convdir > 0 && first > mp->hghmsg)
	    || (convdir < 0 && first < mp->lowmsg))
	    goto rangerr;

	/* tighten the range to search */
	if (first < mp->lowmsg)
	    first = mp->lowmsg;
	if (first > mp->hghmsg)
	    first = mp->hghmsg;

	for (last = first;
	     last >= mp->lowmsg && last <= mp->hghmsg;
	     last += convdir)
	    if (does_exist (mp, last))
		if (--range <= 0)
		    break;
	if (last < mp->lowmsg)
	    last = mp->lowmsg;
	if (last > mp->hghmsg)
	    last = mp->hghmsg;
	if (last < first) {
	    range = last;
	    last = first;
	    first = range;
	}
    } else {

single:
	/*
	 * Single Message
	 *
	 * If ALLOW_NEW is set, then allow selecting of an
	 * empty slot.  If ALLOW_NEW is not set, then we
	 * check if message is in-range and exists.
	 */
	if (mp->msgflags & ALLOW_NEW) {
	    set_select_empty (mp, first);
	} else {
	    if (first > mp->hghmsg
		|| first < mp->lowmsg
		|| !(does_exist (mp, first))) {
		if (!strcmp (name, "cur") || !strcmp (name, "."))
		    advise (NULL, "no %s message", name);
		else
		    advise (NULL, "message %d doesn't exist", first);
		return 0;
	    }
	}
	last = first;	/* range of 1 */
    }

    /*
     * Cycle through the range and select the messages
     * that exist.  If ALLOW_NEW is set, then we also check
     * if we are selecting an empty slot.
     */
    for (; first <= last; first++) {
	if (does_exist (mp, first) ||
	    ((mp->msgflags & ALLOW_NEW) && is_select_empty (mp, first))) {
	    if (!is_selected (mp, first)) {
		set_selected (mp, first);
		mp->numsel++;
		if (mp->lowsel == 0 || first < mp->lowsel)
		    mp->lowsel = first;
		if (first > mp->hghsel)
		    mp->hghsel = first;
	    }
	    found++;
	}
    }

    if (!found)
	goto rangerr;

    return 1;
}

/*
 * Convert the various message names to
 * there numeric value.
 *
 * n     (integer)
 * prev
 * next
 * first
 * last
 * cur
 * .     (same as cur)
 */

static int
m_conv (struct msgs *mp, char *str, int call)
{
    register int i;
    register char *cp, *bp;
    char buf[16];

    convdir = 1;
    cp = bp = str;
    if (isdigit (*cp)) {
	while (isdigit (*bp))
	    bp++;
	delimp = bp;
	i = atoi (cp);

	if (i <= mp->hghmsg)
	    return i;
	else if (*delimp || call == LAST)
	    return mp->hghmsg + 1;
	else if (mp->msgflags & ALLOW_NEW)
	    return BADRNG;
	else
	    return BADNUM;
    }

#ifdef LOCALE
    /* doesn't enforce lower case */
    for (bp = buf; (isalpha(*cp) || *cp == '.')
		&& (bp - buf < sizeof(buf) - 1); )
#else
    for (bp = buf; ((*cp >= 'a' && *cp <= 'z') || *cp == '.')
		&& (bp - buf < sizeof(buf) - 1); )
#endif /* LOCALE */
    {
	*bp++ = *cp++;
    }
    *bp++ = '\0';
    delimp = cp;

    if (!strcmp (buf, "first"))
	return (mp->hghmsg || !(mp->msgflags & ALLOW_NEW)
		? mp->lowmsg : BADMSG);

    if (!strcmp (buf, "last")) {
	convdir = -1;
	return (mp->hghmsg || !(mp->msgflags & ALLOW_NEW) ? mp->hghmsg : BADMSG);
    }

    if (!strcmp (buf, "cur") || !strcmp (buf, "."))
	return (mp->curmsg > 0 ? mp->curmsg : BADMSG);

    if (!strcmp (buf, "prev")) {
	convdir = -1;
	for (i = (mp->curmsg <= mp->hghmsg) ? mp->curmsg - 1 : mp->hghmsg;
		i >= mp->lowmsg; i--) {
	    if (does_exist (mp, i))
		return i;
	}
	return BADMSG;
    }

    if (!strcmp (buf, "next")) {
	for (i = (mp->curmsg >= mp->lowmsg) ? mp->curmsg + 1 : mp->lowmsg;
		i <= mp->hghmsg; i++) {
	    if (does_exist (mp, i))
		return i;
	}
	return BADMSG;
    }

    return BADLST;
}

/*
 * Handle user defined sequences.
 * They can take the following forms:
 *
 * seq
 * seq:prev
 * seq:next
 * seq:first
 * seq:last
 * seq:+n
 * seq:-n
 * seq:n
 */

static int
attr (struct msgs *mp, char *cp)
{
    register char *dp;
    char *bp = NULL;
    register int i, j;
    int found,
	inverted = 0,
	range = 0,		/* no range */
	first = 0;

    /* hack for "cur-name", "cur-n", etc. */
    if (!strcmp (cp, "cur"))
	return 0;
    if (ssequal ("cur:", cp))	/* this code need to be rewritten... */
	return 0;

    /* Check for sequence negation */
    if ((dp = context_find (nsequence)) && *dp != '\0' && ssequal (dp, cp)) {
	inverted = 1;
	cp += strlen (dp);
    }

    convdir = 1;	/* convert direction */

    for (dp = cp; *dp && isalnum(*dp); dp++)
	continue;

    if (*dp == ':') {
	bp = dp++;
	range = 1;

	/*
	 * seq:prev  (or)
	 * seq:next  (or)
	 * seq:first (or)
	 * seq:last
	 */
	if (isalpha (*dp)) {
	    if (!strcmp (dp, "prev")) {
		convdir = -1;
		first = (mp->curmsg > 0) && (mp->curmsg <= mp->hghmsg)
			? mp->curmsg - 1
			: mp->hghmsg;
	    }
	    else if (!strcmp (dp, "next")) {
		convdir = 1;
		first = (mp->curmsg >= mp->lowmsg)
			    ? mp->curmsg + 1
			    : mp->lowmsg;
	    }
	    else if (!strcmp (dp, "first")) {
		convdir = 1;
	    }
	    else if (!strcmp (dp, "last")) {
		convdir = -1;
	    }
	    else
		return BADLST;
	} else {
	    /*
	     * seq:n  (or)
	     * seq:+n (or)
	     * seq:-n
             */
	    if (*dp == '+')
		dp++;
	    else if (*dp == '-') {
		dp++;
		convdir = -1;
	    }
	    if ((range = atoi(dp)) == 0)
		return BADLST;
	    while (isdigit (*dp))
		dp++;
	    if (*dp)
		return BADLST;
	}

	*bp = '\0';	/* temporarily terminate sequence name */
    }

    i = seq_getnum (mp, cp);	/* get index of sequence */

    if (bp)
	*bp = ':';		/* restore sequence name */
    if (i == -1)
	return 0;

    found = 0;	/* count the number we select for this argument */

    for (j = first ? first : (convdir > 0) ? mp->lowmsg : mp->hghmsg;
		j >= mp->lowmsg && j <= mp->hghmsg; j += convdir) {
	if (does_exist (mp, j)
		&& inverted ? !in_sequence (mp, i, j) : in_sequence (mp, i, j)) {
	    if (!is_selected (mp, j)) {
		set_selected (mp, j);
		mp->numsel++;
		if (mp->lowsel == 0 || j < mp->lowsel)
		    mp->lowsel = j;
		if (j > mp->hghsel)
		    mp->hghsel = j;
	    }
	    found++;

	    /*
	     * If we have a range, then break out
	     * once we've found enough.
             */
	    if (range && found >= range)
		break;
	}
    }

    if (found > 0)
	return found;

    if (first)
	return BADMSG;
    advise (NULL, "sequence %s %s", cp, inverted ? "full" : "empty");
    return -1;
}