1*f6402ccaSnatano /* $OpenBSD: generate.c,v 1.18 2016/10/14 09:27:21 natano Exp $ */
203bc119aSespie
3f7923656Sespie /*
4f7923656Sespie * Copyright (c) 2001 Marc Espie.
5f7923656Sespie *
6f7923656Sespie * Redistribution and use in source and binary forms, with or without
7f7923656Sespie * modification, are permitted provided that the following conditions
8f7923656Sespie * are met:
9f7923656Sespie * 1. Redistributions of source code must retain the above copyright
10f7923656Sespie * notice, this list of conditions and the following disclaimer.
11f7923656Sespie * 2. Redistributions in binary form must reproduce the above copyright
12f7923656Sespie * notice, this list of conditions and the following disclaimer in the
13f7923656Sespie * documentation and/or other materials provided with the distribution.
14f7923656Sespie *
15f7923656Sespie * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
16f7923656Sespie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17f7923656Sespie * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18f7923656Sespie * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
19f7923656Sespie * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20f7923656Sespie * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21f7923656Sespie * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22f7923656Sespie * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23f7923656Sespie * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24f7923656Sespie * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25f7923656Sespie * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26f7923656Sespie */
27f7923656Sespie
28f7923656Sespie #include <stddef.h>
290b7a013eSespie #include <stdint.h>
301f89b472Sespie #include <stdio.h>
3103bc119aSespie #include <stdlib.h>
32a47b6461Sderaadt #include <limits.h>
331f89b472Sespie #include <ohash.h>
3403bc119aSespie
3504ed836eSespie #include "stats.h"
3604ed836eSespie #include "cond_int.h"
37f7923656Sespie #include "var_int.h"
38b3c55d53Sespie #include "node_int.h"
3903bc119aSespie
4003bc119aSespie #define M(x) x, #x
4104ed836eSespie char *table_var[] = {
4203bc119aSespie M(TARGET),
4303bc119aSespie M(OODATE),
4403bc119aSespie M(ALLSRC),
4503bc119aSespie M(IMPSRC),
4603bc119aSespie M(PREFIX),
4703bc119aSespie M(ARCHIVE),
4803bc119aSespie M(MEMBER),
4903bc119aSespie M(LONGTARGET),
5003bc119aSespie M(LONGOODATE),
5103bc119aSespie M(LONGALLSRC),
5203bc119aSespie M(LONGIMPSRC),
5303bc119aSespie M(LONGPREFIX),
5403bc119aSespie M(LONGARCHIVE),
5504ed836eSespie M(LONGMEMBER),
5604ed836eSespie M(FTARGET),
5704ed836eSespie M(DTARGET),
5804ed836eSespie M(FPREFIX),
5904ed836eSespie M(DPREFIX),
6004ed836eSespie M(FARCHIVE),
6104ed836eSespie M(DARCHIVE),
6204ed836eSespie M(FMEMBER),
6304ed836eSespie M(DMEMBER),
64*f6402ccaSnatano M(FIMPSRC),
65*f6402ccaSnatano M(DIMPSRC),
6604ed836eSespie NULL
6703bc119aSespie };
6803bc119aSespie
6904ed836eSespie char *table_cond[] = {
7004ed836eSespie M(COND_IF),
7104ed836eSespie M(COND_IFDEF),
7204ed836eSespie M(COND_IFNDEF),
7304ed836eSespie M(COND_IFMAKE),
7404ed836eSespie M(COND_IFNMAKE),
7504ed836eSespie M(COND_ELSE),
76c887eb2eSespie M(COND_ELIF),
7704ed836eSespie M(COND_ELIFDEF),
7804ed836eSespie M(COND_ELIFNDEF),
7904ed836eSespie M(COND_ELIFMAKE),
8004ed836eSespie M(COND_ELIFNMAKE),
8104ed836eSespie M(COND_ENDIF),
8204ed836eSespie M(COND_FOR),
83c887eb2eSespie M(COND_ENDFOR),
8404ed836eSespie M(COND_INCLUDE),
8504ed836eSespie M(COND_UNDEF),
86d88ab755Sespie M(COND_POISON),
8704ed836eSespie NULL
8804ed836eSespie };
8904ed836eSespie
90b3c55d53Sespie char *table_nodes[] = {
91b3c55d53Sespie M(NODE_DEFAULT),
92b3c55d53Sespie M(NODE_EXEC),
93b3c55d53Sespie M(NODE_IGNORE),
94b3c55d53Sespie M(NODE_INCLUDES),
95b3c55d53Sespie M(NODE_INVISIBLE),
96b3c55d53Sespie M(NODE_JOIN),
97b3c55d53Sespie M(NODE_LIBS),
98b3c55d53Sespie M(NODE_MADE),
99b3c55d53Sespie M(NODE_MAIN),
100b3c55d53Sespie M(NODE_MAKE),
101b3c55d53Sespie M(NODE_MAKEFLAGS),
102b3c55d53Sespie M(NODE_MFLAGS),
103b3c55d53Sespie M(NODE_NOTMAIN),
104b3c55d53Sespie M(NODE_NOTPARALLEL),
105b3c55d53Sespie M(NODE_NO_PARALLEL),
106b3c55d53Sespie M(NODE_NULL),
107b3c55d53Sespie M(NODE_OPTIONAL),
108b3c55d53Sespie M(NODE_ORDER),
109b3c55d53Sespie M(NODE_PARALLEL),
110b3c55d53Sespie M(NODE_PATH),
111b3c55d53Sespie M(NODE_PHONY),
112b3c55d53Sespie M(NODE_PRECIOUS),
113b3c55d53Sespie M(NODE_RECURSIVE),
114b3c55d53Sespie M(NODE_SILENT),
115b3c55d53Sespie M(NODE_SINGLESHELL),
116b3c55d53Sespie M(NODE_SUFFIXES),
117b3c55d53Sespie M(NODE_USE),
118b3c55d53Sespie M(NODE_WAIT),
119b3c55d53Sespie M(NODE_BEGIN),
120b3c55d53Sespie M(NODE_END),
121b3c55d53Sespie M(NODE_INTERRUPT),
122dd41fd19Sespie M(NODE_CHEAP),
123dd41fd19Sespie M(NODE_EXPENSIVE),
12429936c8aSespie M(NODE_POSIX),
12529936c8aSespie M(NODE_SCCS_GET),
126b3c55d53Sespie NULL
127b3c55d53Sespie };
128b3c55d53Sespie
12904ed836eSespie
13004ed836eSespie char **table[] = {
13104ed836eSespie table_var,
132b3c55d53Sespie table_cond,
133b3c55d53Sespie table_nodes
13404ed836eSespie };
13503bc119aSespie
13603bc119aSespie int
main(int argc,char * argv[])13703bc119aSespie main(int argc, char *argv[])
13803bc119aSespie {
1390b7a013eSespie uint32_t i;
1400b7a013eSespie uint32_t v;
1410b7a013eSespie uint32_t h;
1420b7a013eSespie uint32_t slots;
143a47b6461Sderaadt const char *errstr;
14403bc119aSespie const char *e;
14503bc119aSespie char **occupied;
14604ed836eSespie char **t;
14704ed836eSespie int tn;
14803bc119aSespie
14903bc119aSespie Init_Stats();
15004ed836eSespie if (argc != 3)
15103bc119aSespie exit(1);
15203bc119aSespie
153a47b6461Sderaadt tn = strtonum(argv[1], 1, INT_MAX, &errstr);
154a47b6461Sderaadt if (errstr)
15503bc119aSespie exit(1);
15604ed836eSespie t = table[tn-1];
157a47b6461Sderaadt slots = strtonum(argv[2], 0, INT_MAX, &errstr);
158a47b6461Sderaadt if (errstr)
159a47b6461Sderaadt exit(1);
16004ed836eSespie if (slots) {
16177c73da2Sespie occupied = calloc(slots, sizeof(char *));
16204ed836eSespie if (!occupied)
16304ed836eSespie exit(1);
16404ed836eSespie } else
16504ed836eSespie occupied = NULL;
16603bc119aSespie
16704ed836eSespie printf("/* File created by generate %d %d, do not edit */\n",
16804ed836eSespie tn, slots);
16904ed836eSespie for (i = 0; t[i] != NULL; i++) {
17003bc119aSespie e = NULL;
17104ed836eSespie v = ohash_interval(t[i], &e);
17204ed836eSespie if (slots) {
17303bc119aSespie h = v % slots;
17403bc119aSespie if (occupied[h]) {
17504ed836eSespie fprintf(stderr,
17604ed836eSespie "Collision: %s / %s (%d)\n", occupied[h],
17704ed836eSespie t[i], h);
17803bc119aSespie exit(1);
17903bc119aSespie }
18004ed836eSespie occupied[h] = t[i];
18103bc119aSespie }
18204ed836eSespie i++;
18304ed836eSespie printf("#define K_%s %u\n", t[i], v);
18404ed836eSespie }
18504ed836eSespie if (slots)
18604ed836eSespie printf("#define MAGICSLOTS%d %u\n", tn, slots);
18703bc119aSespie exit(0);
18803bc119aSespie }
189