xref: /openbsd-src/usr.bin/make/generate.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenPackages$ */
2 /*	$OpenBSD: generate.c,v 1.5 2002/06/11 21:12:11 espie Exp $ */
3 
4 /*
5  * Copyright (c) 2001 Marc Espie.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
20  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <stddef.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 
33 #include "stats.h"
34 #include "ohash.h"
35 #include "cond_int.h"
36 #include "var_int.h"
37 
38 #define M(x)	x, #x
39 char *table_var[] = {
40 	M(TARGET),
41 	M(OODATE),
42 	M(ALLSRC),
43 	M(IMPSRC),
44 	M(PREFIX),
45 	M(ARCHIVE),
46 	M(MEMBER),
47 	M(LONGTARGET),
48 	M(LONGOODATE),
49 	M(LONGALLSRC),
50 	M(LONGIMPSRC),
51 	M(LONGPREFIX),
52 	M(LONGARCHIVE),
53 	M(LONGMEMBER),
54 	M(FTARGET),
55 	M(DTARGET),
56 	M(FPREFIX),
57 	M(DPREFIX),
58 	M(FARCHIVE),
59 	M(DARCHIVE),
60 	M(FMEMBER),
61 	M(DMEMBER),
62 	NULL
63 };
64 
65 char *table_cond[] = {
66 	M(COND_IF),
67 	M(COND_IFDEF),
68 	M(COND_IFNDEF),
69 	M(COND_IFMAKE),
70 	M(COND_IFNMAKE),
71 	M(COND_ELSE),
72 	M(COND_ELIF),
73 	M(COND_ELIFDEF),
74 	M(COND_ELIFNDEF),
75 	M(COND_ELIFMAKE),
76 	M(COND_ELIFNMAKE),
77 	M(COND_ENDIF),
78 	M(COND_FOR),
79 	M(COND_ENDFOR),
80 	M(COND_INCLUDE),
81 	M(COND_UNDEF),
82 	NULL
83 };
84 
85 
86 char **table[] = {
87 	table_var,
88 	table_cond
89 };
90 
91 int
92 main(int argc, char *argv[])
93 {
94 	u_int32_t i;
95 	u_int32_t v;
96 	u_int32_t h;
97 	u_int32_t slots;
98 	const char *e;
99 	char **occupied;
100 	char **t;
101 	int tn;
102 
103 	Init_Stats();
104 	if (argc != 3)
105 		exit(1);
106 
107 	tn = atoi(argv[1]);
108 	if (!tn)
109 		exit(1);
110 	t = table[tn-1];
111 	slots = atoi(argv[2]);
112 	if (slots) {
113 		occupied = malloc(sizeof(char *) * slots);
114 		if (!occupied)
115 			exit(1);
116 		for (i = 0; i < slots; i++)
117 			occupied[i] = NULL;
118 	} else
119 		occupied = NULL;
120 
121 	printf("/* File created by generate %d %d, do not edit */\n",
122 	    tn, slots);
123 	for (i = 0; t[i] != NULL; i++) {
124 		e = NULL;
125 		v = ohash_interval(t[i], &e);
126 		if (slots) {
127 			h = v % slots;
128 			if (occupied[h]) {
129 				fprintf(stderr,
130 				    "Collision: %s / %s (%d)\n", occupied[h],
131 				    t[i], h);
132 				exit(1);
133 			}
134 			occupied[h] = t[i];
135 		}
136 		i++;
137 		printf("#define K_%s %u\n", t[i], v);
138 	}
139 	if (slots)
140 		printf("#define MAGICSLOTS%d %u\n", tn, slots);
141 	exit(0);
142 }
143