xref: /openbsd/usr.bin/make/generate.c (revision d485f761)
1 /*	$OpenPackages$ */
2 /*	$OpenBSD: generate.c,v 1.4 2001/05/23 12:34:43 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_ELIFDEF),
73 	M(COND_ELIFNDEF),
74 	M(COND_ELIFMAKE),
75 	M(COND_ELIFNMAKE),
76 	M(COND_ENDIF),
77 	M(COND_FOR),
78 	M(COND_INCLUDE),
79 	M(COND_UNDEF),
80 	NULL
81 };
82 
83 
84 char **table[] = {
85 	table_var,
86 	table_cond
87 };
88 
89 int
90 main(int argc, char *argv[])
91 {
92 	u_int32_t i;
93 	u_int32_t v;
94 	u_int32_t h;
95 	u_int32_t slots;
96 	const char *e;
97 	char **occupied;
98 	char **t;
99 	int tn;
100 
101 	Init_Stats();
102 	if (argc != 3)
103 		exit(1);
104 
105 	tn = atoi(argv[1]);
106 	if (!tn)
107 		exit(1);
108 	t = table[tn-1];
109 	slots = atoi(argv[2]);
110 	if (slots) {
111 		occupied = malloc(sizeof(char *) * slots);
112 		if (!occupied)
113 			exit(1);
114 		for (i = 0; i < slots; i++)
115 			occupied[i] = NULL;
116 	} else
117 		occupied = NULL;
118 
119 	printf("/* File created by generate %d %d, do not edit */\n",
120 	    tn, slots);
121 	for (i = 0; t[i] != NULL; i++) {
122 		e = NULL;
123 		v = ohash_interval(t[i], &e);
124 		if (slots) {
125 			h = v % slots;
126 			if (occupied[h]) {
127 				fprintf(stderr,
128 				    "Collision: %s / %s (%d)\n", occupied[h],
129 				    t[i], h);
130 				exit(1);
131 			}
132 			occupied[h] = t[i];
133 		}
134 		i++;
135 		printf("#define K_%s %u\n", t[i], v);
136 	}
137 	if (slots)
138 		printf("#define MAGICSLOTS%d %u\n", tn, slots);
139 	exit(0);
140 }
141