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