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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* pagesel.c -- Page selection mechanism */
/*
 * Copyright (C) 2000, Matias Atria
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <config.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>

#include "mdvi.h"
#include "private.h"

char	*program_name = "page";

struct _DviPageSpec {
	DviRange *ranges;
	int	nranges;
};

DviRange *mdvi_parse_range(const char *format, DviRange *limit, int *nitems, char **endptr)
{
	int	quoted;
	int	size;
	int	curr;
	int	done;
	int	lower;
	int	upper;
	int	type;
	char *	cp;
	char *	copy;
	char *	text;
	DviRange one;
	DviRange *range;

	quoted = (*format == '{');
	if(quoted) format++;

	size  = 0;
	curr  = 0;
	range = NULL;
	copy  = mdvi_strdup(format);
	done  = 0;
	lower = 0;
	upper = 0;
	type  = MDVI_RANGE_UNBOUNDED;

	if(limit) {
		switch(limit->type) {
			case MDVI_RANGE_BOUNDED:
				lower = limit->from;
				upper = limit->to;
				break;
			case MDVI_RANGE_UPPER:
				lower = INT_MIN;
				upper = limit->to;
				break;
			case MDVI_RANGE_LOWER:
				lower = limit->from;
				upper = INT_MAX;
				break;
			case MDVI_RANGE_UNBOUNDED:
				lower = INT_MIN;
				upper = INT_MAX;
				break;
		}
		type = limit->type;
	} else {
		lower = INT_MIN;
		upper = INT_MAX;
		type  = MDVI_RANGE_UNBOUNDED;
	}
	one.type = type;
	one.from = lower;
	one.to   = upper;
	one.step = 1;
	for(cp = text = copy; !done; cp++) {
		char	*p;
		int	f, t, s;
		int	ch;
		int	this_type;
		int	lower_given = 0;
		int	upper_given = 0;

		if(*cp == 0 || *cp == '.' || (*cp == '}' && quoted))
			done = 1;
		else if(*cp != ',')
			continue;

		if(text == cp)
			continue;
		ch = *cp;
		*cp = 0;
		f = lower;
		t = upper;
		s = 1;

		p = strchr(text, ':');
		if(p) *p++ = 0;
		if(*text) {
			lower_given = 1;
			f = strtol(text, NULL, 0);
		}
		if(p == NULL) {
			if(lower_given) {
				upper_given = 1;
				t = f; s = 1;
			}
			goto finish;
		}
		text = p;
		p = strchr(text, ':');
		if(p) *p++ = 0;
		if(*text) {
			upper_given = 1;
			t = strtol(text, NULL, 0);
		}
		if(p == NULL)
			goto finish;
		text = p;
		if(*text)
			s = strtol(text, NULL, 0);
finish:
		if(lower_given && upper_given)
			this_type = MDVI_RANGE_BOUNDED;
		else if(lower_given) {
			if(!RANGE_HAS_UPPER(type))
				this_type = MDVI_RANGE_LOWER;
			else
				this_type = MDVI_RANGE_BOUNDED;
			t = upper;
		} else if(upper_given) {
			if(RANGE_HAS_UPPER(one.type)) {
				one.to++;
				this_type = MDVI_RANGE_BOUNDED;
			} else {
				one.to = lower;
				if(!RANGE_HAS_LOWER(type))
					this_type = MDVI_RANGE_UPPER;
				else
					this_type = MDVI_RANGE_BOUNDED;
			}
			f = one.to;
		} else {
			this_type = type;
			f = lower;
			t = upper;
		}
		one.type = this_type;
		one.to   = t;
		one.from = f;
		one.step = s;

		if(curr == size) {
			size += 8;
			range = mdvi_realloc(range, size * sizeof(DviRange));
		}
		memcpy(&range[curr++], &one, sizeof(DviRange));
		*cp = ch;
		text = cp + 1;
	}
	if(done)
		cp--;
	if(quoted && *cp == '}')
		cp++;
	if(endptr)
		*endptr = (char *)format + (cp - copy);
	if(curr && curr < size)
		range = mdvi_realloc(range, curr * sizeof(DviRange));
	*nitems = curr;
	mdvi_free(copy);
	return range;
}

DviPageSpec *mdvi_parse_page_spec(const char *format)
{
	/*
	 * a page specification looks like this:
	 *   '{'RANGE_SPEC'}'         for a DVI spec
	 *   '{'RANGE_SPEC'}' '.' ... for a TeX spec
	 */
	DviPageSpec *spec;
	DviRange *range;
	int	count;
	int	i;
	char	*ptr;

	spec = xnalloc(struct _DviPageSpec *, 11);
	for(i = 0; i < 11; i++)
		spec[i] = NULL;

	/* check what kind of spec we're parsing */
	if(*format != '*') {
		range = mdvi_parse_range(format, NULL, &count, &ptr);
		if(ptr == format) {
			if(range) mdvi_free(range);
			mdvi_error(_("invalid page specification `%s'\n"), format);
			return NULL;
		}
	} else
		range = NULL;

	if(*format == 'D' || *format == 'd' || *ptr != '.')
		i = 0;
	else
		i = 1;

	if(range) {
		spec[i] = xalloc(struct _DviPageSpec);
		spec[i]->ranges = range;
		spec[i]->nranges = count;
	} else
		spec[i] = NULL;

	if(*ptr != '.') {
		if(*ptr)
			mdvi_warning(_("garbage after DVI page specification ignored\n"));
		return spec;
	}

	for(i++; *ptr == '.' && i <= 10; i++) {
		ptr++;
		if(*ptr == '*') {
			ptr++;
			range = NULL;
		} else {
			char	*end;

			range = mdvi_parse_range(ptr, NULL, &count, &end);
			if(end == ptr) {
				if(range) mdvi_free(range);
				range = NULL;
			} else
				ptr = end;
		}
		if(range != NULL) {
			spec[i] = xalloc(struct _DviPageSpec);
			spec[i]->ranges = range;
			spec[i]->nranges = count;
		} else
			spec[i] = NULL;
	}

	if(i > 10)
		mdvi_warning(_("more than 10 counters in page specification\n"));
	else if(*ptr)
		mdvi_warning(_("garbage after TeX page specification ignored\n"));

	return spec;
}

/* returns non-zero if the given page is included by `spec' */
int	mdvi_page_selected(DviPageSpec *spec, PageNum page, int dvipage)
{
	int	i;
	int	not_found;

	if(spec == NULL)
		return 1;
	if(spec[0]) {
		not_found = mdvi_in_range(spec[0]->ranges,
			spec[0]->nranges, dvipage);
		if(not_found < 0)
			return 0;
	}
	for(i = 1; i <= 10; i++) {
		if(spec[i] == NULL)
			continue;
		not_found = mdvi_in_range(spec[i]->ranges,
			spec[i]->nranges, (int)page[i]);
		if(not_found < 0)
			return 0;
	}
	return 1;
}

void	mdvi_free_page_spec(DviPageSpec *spec)
{
	int	i;

	for(i = 0; i < 11; i++)
		if(spec[i]) {
			mdvi_free(spec[i]->ranges);
			mdvi_free(spec[i]);
		}
	mdvi_free(spec);
}

int	mdvi_in_range(DviRange *range, int nitems, int value)
{
	DviRange *r;

	for(r = range; r < range + nitems; r++) {
		int	cond;

		switch(r->type) {
		case MDVI_RANGE_BOUNDED:
			if(value == r->from)
				return (r - range);
			if(r->step < 0)
				cond = (value <= r->from) && (value >= r->to);
			else
				cond = (value <= r->to) && (value >= r->from);
			if(cond && ((value - r->from) % r->step) == 0)
				return (r - range);
			break;
		case MDVI_RANGE_LOWER:
			if(value == r->from)
				return (r - range);
			if(r->step < 0)
				cond = (value < r->from);
			else
				cond = (value > r->from);
			if(cond && ((value - r->from) % r->step) == 0)
				return (r - range);
			break;
		case MDVI_RANGE_UPPER:
			if(value == r->to)
				return (r - range);
			if(r->step < 0)
				cond = (value > r->to);
			else
				cond = (value < r->to);
			if(cond && ((value - r->to) % r->step) == 0)
				return (r - range);
			break;
		case MDVI_RANGE_UNBOUNDED:
			if((value % r->step) == 0)
				return (r - range);
			break;
		}
	}
	return -1;
}

int	mdvi_range_length(DviRange *range, int nitems)
{
	int	count = 0;
	DviRange *r;

	for(r = range; r < range + nitems; r++) {
		int	n;

		if(r->type != MDVI_RANGE_BOUNDED)
			return -2;
		n = (r->to - r->from) / r->step;
		if(n < 0)
			n = 0;
		count += n + 1;
	}
	return count;
}

#ifdef TEST

void	print_range(DviRange *range)
{
	switch(range->type) {
	case MDVI_RANGE_BOUNDED:
		printf("From %d to %d, step %d\n",
			range->from, range->to, range->step);
		break;
	case MDVI_RANGE_LOWER:
		printf("From %d, step %d\n",
			range->from, range->step);
		break;
	case MDVI_RANGE_UPPER:
		printf("From %d, step -%d\n",
			range->to, range->step);
		break;
	case MDVI_RANGE_UNBOUNDED:
		printf("From 0, step %d and %d\n",
			range->step, -range->step);
		break;
	}
}

int main()
{
#if 0
	char	buf[256];
	DviRange limit;

	limit.from = 0;
	limit.to = 100;
	limit.step = 2;
	limit.type = MDVI_RANGE_UNBOUNDED;
	while(1) {
		DviRange *range;
		char	*end;
		int	count;
		int	i;

		printf("Range> "); fflush(stdout);
		if(fgets(buf, 256, stdin) == NULL)
			break;
		if(buf[strlen(buf)-1] == '\n')
			buf[strlen(buf)-1] = 0;
		if(buf[0] == 0)
			continue;
		end = NULL;
		range = mdvi_parse_range(buf, &limit, &count, &end);
		if(range == NULL) {
			printf("range is empty\n");
			continue;
		}

		for(i = 0; i < count; i++) {
			printf("Range %d (%d elements):\n",
				i, mdvi_range_length(&range[i], 1));
			print_range(&range[i]);
		}
		if(end && *end)
			printf("Tail: [%s]\n", end);
		printf("range has %d elements\n",
			mdvi_range_length(range, count));
#if 1
		while(1) {
			int	v;

			printf("Value: "); fflush(stdout);
			if(fgets(buf, 256, stdin) == NULL)
				break;
			if(buf[strlen(buf)-1] == '\n')
				buf[strlen(buf)-1] = 0;
			if(buf[0] == 0)
				break;
			v = atoi(buf);
			i = mdvi_in_range(range, count, v);
			if(i == -1)
				printf("%d not in range\n", v);
			else {
				printf("%d in range: ", v);
				print_range(&range[i]);
			}
		}
#endif
		if(range) mdvi_free(range);
	}
#else
	DviPageSpec *spec;
	char	buf[256];

	while(1) {
		printf("Spec> "); fflush(stdout);
		if(fgets(buf, 256, stdin) == NULL)
			break;
		if(buf[strlen(buf)-1] == '\n')
			buf[strlen(buf)-1] = 0;
		if(buf[0] == 0)
			continue;
		spec = mdvi_parse_page_spec(buf);
		if(spec == NULL)
			printf("no spec parsed\n");
		else {
			int	i;

			printf("spec = ");
			for(i = 0; i < 11; i++) {
				printf("Counter %d:\n", i);
				if(spec[i]) {
					int	k;

					for(k = 0; k < spec[i]->nranges; k++)
						print_range(&spec[i]->ranges[k]);
				} else
					printf("\t*\n");
			}
			mdvi_free_page_spec(spec);
		}
	}
#endif
	exit(0);

}
#endif /* TEST */