summaryrefslogtreecommitdiff
path: root/gs/src/genconf.c
blob: 8fa2b71b6f45e3925a70aa604b671c8caa5e7ebb (plain)
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/* Copyright (C) 1993, 1996 Aladdin Enterprises.  All rights reserved.

   This file is part of Aladdin Ghostscript.

   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
   or distributor accepts any responsibility for the consequences of using it,
   or for whether it serves any particular purpose or works at all, unless he
   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
   License (the "License") for full details.

   Every copy of Aladdin Ghostscript must include a copy of the License,
   normally in a plain ASCII text file named PUBLIC.  The License grants you
   the right to copy, modify and redistribute Aladdin Ghostscript, but only
   under certain conditions described in the License.  Among other things, the
   License requires that the copyright notice and this notice be preserved on
   all copies.
 */

/* genconf.c */
/* Generate configuration files */
#include "stdpre.h"
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>		/* for calloc */
#include <string.h>

/* We would like to use the real realloc, but it doesn't work on all systems */
/* (e.g., some Linux versions). */
void *
mrealloc(void *old_ptr, size_t old_size, size_t new_size)
{
    void *new_ptr = malloc(new_size);

    if (new_ptr == NULL)
	return NULL;
    /* We have to pass in the old size, since we have no way to */
    /* determine it otherwise. */
    memcpy(new_ptr, old_ptr, min(old_size, new_size));
    return new_ptr;
}

/*
 * This program generates a set of configuration files.
 * Almost everything it does could be done by a shell program, except that
 * (1) Unix shells are not compatible from one system to another,
 * (2) the DOS shell is not equal to the task,
 * (3) the VMS shell is radically different from all others.
 *
 * Usage:
 *      genconf [-Z] [-n [name_prefix | -]] [@]xxx.dev*
 *        [-f gconfigf.h] [-h gconfig.h]
 *        [-p[l|L][u][e] pattern] [-l|o|lo|ol out.tr]
 * & in the pattern acts as an escape character as follows:
 *      &p produces a %;
 *      &s produces a space;
 *      && produces a \;
 *      &- produces a -;
 *      &x, for any other character x, is an error.
 */

/* DEFAULT_PREFIX should be DEFAULT_NAME_PREFIX. */
#define DEFAULT_PREFIX "gs_"

/* Structures for accumulating information. */
typedef struct string_item_s {
    const char *str;
    int index;
} string_item;

/* The values of uniq_mode are bit masks. */
typedef enum {
    uniq_all = 1,		/* keep all occurrences (default) */
    uniq_first = 2,		/* keep only first occurrence */
    uniq_last = 4		/* keep only last occurrence */
} uniq_mode;
typedef struct string_list_s {
    /* The following are set at creation time. */
    int max_count;
    uniq_mode mode;
    /* The following are updated dynamically. */
    int count;
    string_item *items;
} string_list;

#define max_pattern 60
typedef struct string_pattern_s {
    bool upper_case;
    bool drop_extn;
    char pattern[max_pattern + 1];
} string_pattern;
typedef struct config_s {
    int debug;
    const char *name_prefix;
    const char *file_prefix;
    /* file_names and file_contents are special.... */
    string_list file_names;
    string_list file_contents;
    string_list resources;
    string_list devs;
    string_list fonts;
    string_list headers;
    string_list libs;
    string_list libpaths;
    string_list objs;
    string_pattern lib_p;
    string_pattern libpath_p;
    string_pattern obj_p;
} config;

/* These lists grow automatically if needed, so we could start out with */
/* small allocations. */
static const config init_config =
{
    0,				/* debug */
    DEFAULT_PREFIX,		/* name_prefix */
    "",				/* file_prefix */
    {200},			/* file_names */
    {200},			/* file_contents */
    {100, uniq_first},		/* resources */
    {100, uniq_first},		/* devs */
    {50, uniq_first},		/* fonts */
    {20, uniq_first},		/* headers */
    {20, uniq_last},		/* libs */
    {10, uniq_first},		/* libpaths */
    {400, uniq_first}		/* objs */
};

/* Forward definitions */
int alloc_list(P1(string_list *));
void parse_affix(P2(char *, const char *));
int read_dev(P2(config *, const char *));
int read_token(P3(char *, int, const char **));
int add_entry(P3(config *, char *, const char *));
string_item *add_item(P2(string_list *, const char *));
void sort_uniq(P1(string_list *));
void write_list(P3(FILE *, const string_list *, const char *));
void write_list_pattern(P3(FILE *, const string_list *, const string_pattern *));

main(int argc, char *argv[])
{
    config conf;
    int i;

    /* Allocate string lists. */
    conf = init_config;
    alloc_list(&conf.file_names);
    alloc_list(&conf.file_contents);
    alloc_list(&conf.resources);
    alloc_list(&conf.devs);
    alloc_list(&conf.fonts);
    alloc_list(&conf.headers);
    alloc_list(&conf.libs);
    alloc_list(&conf.libpaths);
    alloc_list(&conf.objs);

    /* Initialize patterns. */
    conf.lib_p.upper_case = false;
    conf.lib_p.drop_extn = false;
    strcpy(conf.lib_p.pattern, "%s\n");
    conf.libpath_p = conf.lib_p;
    conf.obj_p = conf.lib_p;

    /* Process command line arguments. */
    for (i = 1; i < argc; i++) {
	const char *arg = argv[i];
	FILE *out;
	int lib = 0, obj = 0;

	if (*arg != '-') {
	    read_dev(&conf, arg);
	    continue;
	}
	if (i == argc - 1) {
	    fprintf(stderr, "Missing argument after %s.\n",
		    arg);
	    exit(1);
	}
	switch (arg[1]) {
	    case 'C':		/* change directory, by analogy with make */
		conf.file_prefix =
		    (argv[i + 1][0] == '-' ? "" : argv[i + 1]);
		++i;
		continue;
	    case 'n':
		conf.name_prefix =
		    (argv[i + 1][0] == '-' ? "" : argv[i + 1]);
		++i;
		continue;
	    case 'p':
		{
		    string_pattern *pat;

		    switch (*(arg += 2)) {
			case 'l':
			    pat = &conf.lib_p;
			    break;
			case 'L':
			    pat = &conf.libpath_p;
			    break;
			default:
			    pat = &conf.obj_p;
			    arg--;
		    }
		    pat->upper_case = false;
		    pat->drop_extn = false;
		    if (argv[i + 1][0] == '-')
			strcpy(pat->pattern, "%s\n");
		    else {
			char *p, *q;

			for (p = pat->pattern, q = argv[++i];
			     (*p++ = *q++) != 0;
			    )
			    if (p[-1] == '&')
				switch (*q) {
				    case 'p':
					p[-1] = '%';
					q++;
					break;
				    case 's':
					p[-1] = ' ';
					q++;
					break;
				    case '&':
					p[-1] = '\\';
					q++;
					break;
				    case '-':
					p[-1] = '-';
					q++;
					break;
				    default:
					fprintf(stderr,
					    "& not followed by ps&-: &%c\n",
						*q);
					exit(1);
				}
			p[-1] = '\n';
			*p = 0;
		    }
		    for (;;)
			switch (*++arg) {
			    case 'u':
				pat->upper_case = true;
				break;
			    case 'e':
				pat->drop_extn = true;
				break;
			    case 0:
				goto pbreak;
			    default:
				fprintf(stderr, "Unknown switch %s.\n", arg);
				exit(1);
			}
		  pbreak:if (pat == &conf.obj_p) {
			conf.lib_p = *pat;
			conf.libpath_p = *pat;
		    }
		    continue;
	    case 'Z':
		    conf.debug = 1;
		    continue;
		}
	}
	/* Must be an output file. */
	out = fopen(argv[++i], "w");
	if (out == 0) {
	    fprintf(stderr, "Can't open %s for output.\n",
		    argv[i]);
	    exit(1);
	}
	switch (arg[1]) {
	    case 'f':
		fputs("/* This file was generated automatically by genconf.c. */\n", out);
		fputs("/* For documentation, see gsconfig.c. */\n", out);
		{
		    char template[80];

		    sprintf(template,
			    "font_(\"0.font_%%s\",%sf_%%s,zf_%%s)\n",
			    conf.name_prefix);
		    write_list(out, &conf.fonts, template);
		}
		break;
	    case 'h':
		fputs("/* This file was generated automatically by genconf.c. */\n", out);
		{
		    char template[80];

		    sprintf(template, "device_(%s%%s_device)\n",
			    conf.name_prefix);
		    write_list(out, &conf.devs, template);
		}
		sort_uniq(&conf.resources);
		write_list(out, &conf.resources, "%s\n");
		write_list(out, &conf.headers, "#include \"%s\"\n");
		break;
	    case 'l':
		lib = 1;
		obj = arg[2] == 'o';
		goto lo;
	    case 'o':
		obj = 1;
		lib = arg[2] == 'l';
	      lo:if (obj) {
		    sort_uniq(&conf.objs);
		    write_list_pattern(out, &conf.objs, &conf.obj_p);
		}
		if (lib) {
		    sort_uniq(&conf.libs);
		    write_list_pattern(out, &conf.libpaths, &conf.libpath_p);
		    write_list_pattern(out, &conf.libs, &conf.lib_p);
		}
		break;
	    default:
		fclose(out);
		fprintf(stderr, "Unknown switch %s.\n", argv[i]);
		exit(1);
	}
	fclose(out);
    }

    exit(0);
}

/* Allocate and initialize a string list. */
int
alloc_list(string_list * list)
{
    list->count = 0;
    list->items =
	(string_item *) calloc(list->max_count, sizeof(string_item));
    assert(list->items != NULL);
    return 0;
}

/* Read an entire file into memory. */
/* We use the 'index' of the file_contents string_item to record the union */
/* of the uniq_modes of all (direct and indirect) items in the file. */
string_item *
read_file(config * pconf, const char *fname)
{
    char *cname = malloc(strlen(fname) + strlen(pconf->file_prefix) + 1);
    int i;
    FILE *in;
    int end, nread;
    char *cont;
    string_item *item;

    if (cname == 0) {
	fprintf(stderr, "Can't allocate space for file name %s%s.\n",
		pconf->file_prefix, fname);
	exit(1);
    }
    strcpy(cname, pconf->file_prefix);
    strcat(cname, fname);
    for (i = 0; i < pconf->file_names.count; ++i)
	if (!strcmp(pconf->file_names.items[i].str, cname)) {
	    free(cname);
	    return &pconf->file_contents.items[i];
	}
    /* Try to open the file in binary mode, to avoid the overhead */
    /* of unnecessary EOL conversion in the C library. */
    in = fopen(cname, "rb");
    if (in == 0) {
	in = fopen(cname, "r");
	if (in == 0) {
	    fprintf(stderr, "Can't read %s.\n", cname);
	    exit(1);
	}
    }
    fseek(in, 0L, 2 /*SEEK_END */ );
    end = ftell(in);
    cont = malloc(end + 1);
    if (cont == 0) {
	fprintf(stderr, "Can't allocate %d bytes to read %s.\n",
		end + 1, cname);
	exit(1);
    }
    rewind(in);
    nread = fread(cont, 1, end, in);
    fclose(in);
    cont[nread] = 0;
    if (pconf->debug)
	printf("File %s = %d bytes.\n", cname, nread);
    add_item(&pconf->file_names, cname);
    item = add_item(&pconf->file_contents, cont);
    item->index = 0;		/* union of uniq_modes */
    return item;
}

/* Read and parse a .dev file. */
/* Return the union of all its uniq_modes. */
int
read_dev(config * pconf, const char *arg)
{
    string_item *item;
    const char *in;

#define max_token 256
    char *token = malloc(max_token + 1);
    char *category = malloc(max_token + 1);
    int len;

    if (pconf->debug)
	printf("Reading %s;\n", arg);
    item = read_file(pconf, arg);
    if (item->index == uniq_first) {	/* Don't need to read the file again. */
	if (pconf->debug)
	    printf("Skipping duplicate file.\n");
	return uniq_first;
    }
    in = item->str;
    strcpy(category, "obj");
    while ((len = read_token(token, max_token, &in)) > 0)
	item->index |= add_entry(pconf, category, token);
    free(category);
#undef max_token
    if (len < 0) {
	fprintf(stderr, "Token too long: %s.\n", token);
	exit(1);
    }
    if (pconf->debug)
	printf("Finished %s.\n", arg);
    free(token);
    return item->index;
}

/* Read a token from a string that contains the contents of a file. */
int
read_token(char *token, int max_len, const char **pin)
{
    const char *in = *pin;
    int len = 0;

    while (len < max_len) {
	char ch = *in;

	if (ch == 0)
	    break;
	++in;
	if (isspace(ch)) {
	    if (len > 0)
		break;
	    continue;
	}
	token[len++] = ch;
    }
    token[len] = 0;
    *pin = in;
    return (len >= max_len ? -1 /* token too long */ : len);
}

/* Add an entry to a configuration. */
/* Return its uniq_mode. */
int
add_entry(config * pconf, char *category, const char *item)
{
    if (item[0] == '-') {	/* set category */
	strcpy(category, item + 1);
	return 0;
    } else {			/* add to current category */
#define max_str 120
	char str[max_str];
	char template[80];
	const char *pat = 0;
	string_list *list = &pconf->resources;

	if (pconf->debug)
	    printf("Adding %s %s;\n", category, item);
	/* Handle a few resources specially; just queue the rest. */
	switch (category[0]) {
#define is_cat(str) !strcmp(category, str)
	    case 'd':
		if (is_cat("dev")) {
		    list = &pconf->devs;
		    break;
		}
		goto err;
	    case 'e':
		if (is_cat("emulator")) {
		    pat = "emulator_(\"%s\")";
		    break;
		}
		goto err;
	    case 'f':
		if (is_cat("font")) {
		    list = &pconf->fonts;
		    break;
		}
		goto err;
	    case 'h':
		if (is_cat("header")) {
		    list = &pconf->headers;
		    break;
		}
		goto err;
	    case 'i':
		if (is_cat("include")) {
		    int len = strlen(item);

		    strcpy(str, item);
		    if (len < 5 || strcmp(str + len - 4, ".dev"))
			strcat(str, ".dev");
		    return read_dev(pconf, str);
		}
		if (is_cat("includef")) {
		    strcpy(str, item);
		    strcat(str, ".dvc");
		    return read_dev(pconf, str);
		}
		if (is_cat("init")) {
		    pat = "init_(%s%%s_init)";
		} else if (is_cat("iodev")) {
		    pat = "io_device_(%siodev_%%s)";
		} else
		    goto err;
		sprintf(template, pat, pconf->name_prefix);
		pat = template;
		break;
	    case 'l':
		if (is_cat("lib")) {
		    list = &pconf->libs;
		    break;
		}
		if (is_cat("libpath")) {
		    list = &pconf->libpaths;
		    break;
		}
		goto err;
	    case 'o':
		if (is_cat("obj")) {
		    list = &pconf->objs;
		    strcpy(template, pconf->file_prefix);
		    strcat(template, "%s");
		    pat = template;
		    break;
		}
		if (is_cat("oper")) {
		    pat = "oper_(%s_op_defs)";
		    break;
		}
		goto err;
	    case 'p':
		if (is_cat("ps")) {
		    pat = "psfile_(\"%s.ps\")";
		    break;
		}
		goto err;
#undef is_cat
	    default:
	      err:fprintf(stderr, "Unknown category %s.\n", category);
		exit(1);
	}
	if (pat) {
	    sprintf(str, pat, item);
	    assert(strlen(str) < max_str);
	    add_item(list, str);
	} else
	    add_item(list, item);
	return list->mode;
    }
}

/* Add an item to a list. */
string_item *
add_item(string_list * list, const char *str)
{
    char *rstr = malloc(strlen(str) + 1);
    int count = list->count;
    string_item *item;

    strcpy(rstr, str);
    if (count >= list->max_count) {
	list->max_count <<= 1;
	list->items =
	    (string_item *) mrealloc(list->items,
				     (list->max_count >> 1) *
				     sizeof(string_item),
				     list->max_count *
				     sizeof(string_item));
	assert(list->items != NULL);
    }
    item = &list->items[count];
    item->index = count;
    item->str = rstr;
    list->count++;
    return item;
}

/* Remove duplicates from a list of string_items. */
/* In case of duplicates, remove all but the earliest (if last = false) */
/* or the latest (if last = true). */
#define psi1 ((const string_item *)p1)
#define psi2 ((const string_item *)p2)
int
cmp_index(const void *p1, const void *p2)
{
    int cmp = psi1->index - psi2->index;

    return (cmp < 0 ? -1 : cmp > 0 ? 1 : 0);
}
int
cmp_str(const void *p1, const void *p2)
{
    return strcmp(psi1->str, psi2->str);
}
#undef psi1
#undef psi2
void
sort_uniq(string_list * list)
{
    string_item *strlist = list->items;
    int count = list->count;
    const string_item *from;
    string_item *to;
    int i;
    bool last = list->mode == uniq_last;

    if (count == 0)
	return;
    qsort((char *)strlist, count, sizeof(string_item), cmp_str);
    for (from = to = strlist + 1, i = 1; i < count; from++, i++)
	if (strcmp(from->str, to[-1].str))
	    *to++ = *from;
	else if ((last ? from->index > to[-1].index :
		  from->index < to[-1].index)
	    )
	    to[-1] = *from;
    count = to - strlist;
    list->count = count;
    qsort((char *)strlist, count, sizeof(string_item), cmp_index);
}

/* Write a list of strings using a template. */
void
write_list(FILE * out, const string_list * list, const char *pstr)
{
    string_pattern pat;

    pat.upper_case = false;
    pat.drop_extn = false;
    strcpy(pat.pattern, pstr);
    write_list_pattern(out, list, &pat);
}
void
write_list_pattern(FILE * out, const string_list * list, const string_pattern * pat)
{
    int i;
    char macname[40];
    int plen = strlen(pat->pattern);

    *macname = 0;
    for (i = 0; i < list->count; i++) {
	const char *lstr = list->items[i].str;
	int len = strlen(lstr);
	char *str = malloc(len + 1);
	int xlen = plen + len * 3;
	char *xstr = malloc(xlen + 1);
	char *alist;

	strcpy(str, lstr);
	if (pat->drop_extn) {
	    char *dot = str + len;

	    while (dot > str && *dot != '.')
		dot--;
	    if (dot > str)
		*dot = 0, len = dot - str;
	}
	if (pat->upper_case) {
	    char *ptr = str;

	    for (; *ptr; ptr++)
		if (islower(*ptr))
		    *ptr = toupper(*ptr);
	}
	/* We repeat str for the benefit of patterns that */
	/* need the argument substituted in more than one place. */
	sprintf(xstr, pat->pattern, str, str, str);
	/* Check to make sure the item is within the scope of */
	/* an appropriate #ifdef, if necessary. */
	alist = strchr(xstr, '(');
	if (alist != 0 && alist != xstr && alist[-1] == '_') {
	    *alist = 0;
	    if (strcmp(xstr, macname)) {
		if (*macname)
		    fputs("#endif\n", out);
		fprintf(out, "#ifdef %s\n", xstr);
		strcpy(macname, xstr);
	    }
	    *alist = '(';
	} else {
	    if (*macname) {
		fputs("#endif\n", out);
		*macname = 0;
	    }
	}
	fputs(xstr, out);
	free(xstr);
	free(str);
    }
    if (*macname)
	fputs("#endif\n", out);
}