xref: /aosp_15_r20/external/one-true-awk/maketab.c (revision 9a7741de182b2776d7b30d6355f2585c0780a51b)
1*9a7741deSElliott Hughes /****************************************************************
2*9a7741deSElliott Hughes Copyright (C) Lucent Technologies 1997
3*9a7741deSElliott Hughes All Rights Reserved
4*9a7741deSElliott Hughes 
5*9a7741deSElliott Hughes Permission to use, copy, modify, and distribute this software and
6*9a7741deSElliott Hughes its documentation for any purpose and without fee is hereby
7*9a7741deSElliott Hughes granted, provided that the above copyright notice appear in all
8*9a7741deSElliott Hughes copies and that both that the copyright notice and this
9*9a7741deSElliott Hughes permission notice and warranty disclaimer appear in supporting
10*9a7741deSElliott Hughes documentation, and that the name Lucent Technologies or any of
11*9a7741deSElliott Hughes its entities not be used in advertising or publicity pertaining
12*9a7741deSElliott Hughes to distribution of the software without specific, written prior
13*9a7741deSElliott Hughes permission.
14*9a7741deSElliott Hughes 
15*9a7741deSElliott Hughes LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16*9a7741deSElliott Hughes INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17*9a7741deSElliott Hughes IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
18*9a7741deSElliott Hughes SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19*9a7741deSElliott Hughes WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
20*9a7741deSElliott Hughes IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21*9a7741deSElliott Hughes ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
22*9a7741deSElliott Hughes THIS SOFTWARE.
23*9a7741deSElliott Hughes ****************************************************************/
24*9a7741deSElliott Hughes 
25*9a7741deSElliott Hughes /*
26*9a7741deSElliott Hughes  * this program makes the table to link function names
27*9a7741deSElliott Hughes  * and type indices that is used by execute() in run.c.
28*9a7741deSElliott Hughes  * it finds the indices in awkgram.tab.h, produced by bison.
29*9a7741deSElliott Hughes  */
30*9a7741deSElliott Hughes 
31*9a7741deSElliott Hughes #include <stdio.h>
32*9a7741deSElliott Hughes #include <string.h>
33*9a7741deSElliott Hughes #include <stdlib.h>
34*9a7741deSElliott Hughes #include "awk.h"
35*9a7741deSElliott Hughes #include "awkgram.tab.h"
36*9a7741deSElliott Hughes 
37*9a7741deSElliott Hughes struct xx
38*9a7741deSElliott Hughes {	int token;
39*9a7741deSElliott Hughes 	const char *name;
40*9a7741deSElliott Hughes 	const char *pname;
41*9a7741deSElliott Hughes } proc[] = {
42*9a7741deSElliott Hughes 	{ PROGRAM, "program", NULL },
43*9a7741deSElliott Hughes 	{ BOR, "boolop", " || " },
44*9a7741deSElliott Hughes 	{ AND, "boolop", " && " },
45*9a7741deSElliott Hughes 	{ NOT, "boolop", " !" },
46*9a7741deSElliott Hughes 	{ NE, "relop", " != " },
47*9a7741deSElliott Hughes 	{ EQ, "relop", " == " },
48*9a7741deSElliott Hughes 	{ LE, "relop", " <= " },
49*9a7741deSElliott Hughes 	{ LT, "relop", " < " },
50*9a7741deSElliott Hughes 	{ GE, "relop", " >= " },
51*9a7741deSElliott Hughes 	{ GT, "relop", " > " },
52*9a7741deSElliott Hughes 	{ ARRAY, "array", NULL },
53*9a7741deSElliott Hughes 	{ INDIRECT, "indirect", "$(" },
54*9a7741deSElliott Hughes 	{ SUBSTR, "substr", "substr" },
55*9a7741deSElliott Hughes 	{ SUB, "dosub", "sub" },
56*9a7741deSElliott Hughes 	{ GSUB, "dosub", "gsub" },
57*9a7741deSElliott Hughes 	{ INDEX, "sindex", "sindex" },
58*9a7741deSElliott Hughes 	{ SPRINTF, "awksprintf", "sprintf " },
59*9a7741deSElliott Hughes 	{ ADD, "arith", " + " },
60*9a7741deSElliott Hughes 	{ MINUS, "arith", " - " },
61*9a7741deSElliott Hughes 	{ MULT, "arith", " * " },
62*9a7741deSElliott Hughes 	{ DIVIDE, "arith", " / " },
63*9a7741deSElliott Hughes 	{ MOD, "arith", " % " },
64*9a7741deSElliott Hughes 	{ UMINUS, "arith", " -" },
65*9a7741deSElliott Hughes 	{ UPLUS, "arith", " +" },
66*9a7741deSElliott Hughes 	{ POWER, "arith", " **" },
67*9a7741deSElliott Hughes 	{ PREINCR, "incrdecr", "++" },
68*9a7741deSElliott Hughes 	{ POSTINCR, "incrdecr", "++" },
69*9a7741deSElliott Hughes 	{ PREDECR, "incrdecr", "--" },
70*9a7741deSElliott Hughes 	{ POSTDECR, "incrdecr", "--" },
71*9a7741deSElliott Hughes 	{ CAT, "cat", " " },
72*9a7741deSElliott Hughes 	{ PASTAT, "pastat", NULL },
73*9a7741deSElliott Hughes 	{ PASTAT2, "dopa2", NULL },
74*9a7741deSElliott Hughes 	{ MATCH, "matchop", " ~ " },
75*9a7741deSElliott Hughes 	{ NOTMATCH, "matchop", " !~ " },
76*9a7741deSElliott Hughes 	{ MATCHFCN, "matchop", "matchop" },
77*9a7741deSElliott Hughes 	{ INTEST, "intest", "intest" },
78*9a7741deSElliott Hughes 	{ PRINTF, "awkprintf", "printf" },
79*9a7741deSElliott Hughes 	{ PRINT, "printstat", "print" },
80*9a7741deSElliott Hughes 	{ CLOSE, "closefile", "closefile" },
81*9a7741deSElliott Hughes 	{ DELETE, "awkdelete", "awkdelete" },
82*9a7741deSElliott Hughes 	{ SPLIT, "split", "split" },
83*9a7741deSElliott Hughes 	{ ASSIGN, "assign", " = " },
84*9a7741deSElliott Hughes 	{ ADDEQ, "assign", " += " },
85*9a7741deSElliott Hughes 	{ SUBEQ, "assign", " -= " },
86*9a7741deSElliott Hughes 	{ MULTEQ, "assign", " *= " },
87*9a7741deSElliott Hughes 	{ DIVEQ, "assign", " /= " },
88*9a7741deSElliott Hughes 	{ MODEQ, "assign", " %= " },
89*9a7741deSElliott Hughes 	{ POWEQ, "assign", " ^= " },
90*9a7741deSElliott Hughes 	{ CONDEXPR, "condexpr", " ?: " },
91*9a7741deSElliott Hughes 	{ IF, "ifstat", "if(" },
92*9a7741deSElliott Hughes 	{ WHILE, "whilestat", "while(" },
93*9a7741deSElliott Hughes 	{ FOR, "forstat", "for(" },
94*9a7741deSElliott Hughes 	{ DO, "dostat", "do" },
95*9a7741deSElliott Hughes 	{ IN, "instat", "instat" },
96*9a7741deSElliott Hughes 	{ NEXT, "jump", "next" },
97*9a7741deSElliott Hughes 	{ NEXTFILE, "jump", "nextfile" },
98*9a7741deSElliott Hughes 	{ EXIT, "jump", "exit" },
99*9a7741deSElliott Hughes 	{ BREAK, "jump", "break" },
100*9a7741deSElliott Hughes 	{ CONTINUE, "jump", "continue" },
101*9a7741deSElliott Hughes 	{ RETURN, "jump", "ret" },
102*9a7741deSElliott Hughes 	{ BLTIN, "bltin", "bltin" },
103*9a7741deSElliott Hughes 	{ CALL, "call", "call" },
104*9a7741deSElliott Hughes 	{ ARG, "arg", "arg" },
105*9a7741deSElliott Hughes 	{ VARNF, "getnf", "NF" },
106*9a7741deSElliott Hughes 	{ GETLINE, "awkgetline", "getline" },
107*9a7741deSElliott Hughes 	{ 0, "", "" },
108*9a7741deSElliott Hughes };
109*9a7741deSElliott Hughes 
110*9a7741deSElliott Hughes #define SIZE	(LASTTOKEN - FIRSTTOKEN + 1)
111*9a7741deSElliott Hughes const char *table[SIZE];
112*9a7741deSElliott Hughes char *names[SIZE];
113*9a7741deSElliott Hughes 
main(int argc,char * argv[])114*9a7741deSElliott Hughes int main(int argc, char *argv[])
115*9a7741deSElliott Hughes {
116*9a7741deSElliott Hughes 	const struct xx *p;
117*9a7741deSElliott Hughes 	int i, n, tok;
118*9a7741deSElliott Hughes 	char c;
119*9a7741deSElliott Hughes 	FILE *fp;
120*9a7741deSElliott Hughes 	char buf[200], name[200], def[200];
121*9a7741deSElliott Hughes 	enum { TOK_UNKNOWN, TOK_ENUM, TOK_DEFINE } tokentype = TOK_UNKNOWN;
122*9a7741deSElliott Hughes 
123*9a7741deSElliott Hughes 	printf("#include <stdio.h>\n");
124*9a7741deSElliott Hughes 	printf("#include \"awk.h\"\n");
125*9a7741deSElliott Hughes 	printf("#include \"awkgram.tab.h\"\n\n");
126*9a7741deSElliott Hughes 
127*9a7741deSElliott Hughes 	if (argc != 2) {
128*9a7741deSElliott Hughes 		fprintf(stderr, "usage: maketab YTAB_H\n");
129*9a7741deSElliott Hughes 		exit(1);
130*9a7741deSElliott Hughes 	}
131*9a7741deSElliott Hughes 	if ((fp = fopen(argv[1], "r")) == NULL) {
132*9a7741deSElliott Hughes 		fprintf(stderr, "maketab can't open %s!\n", argv[1]);
133*9a7741deSElliott Hughes 		exit(1);
134*9a7741deSElliott Hughes 	}
135*9a7741deSElliott Hughes 	printf("static const char * const printname[%d] = {\n", SIZE);
136*9a7741deSElliott Hughes 	i = 0;
137*9a7741deSElliott Hughes 	while (fgets(buf, sizeof buf, fp) != NULL) {
138*9a7741deSElliott Hughes 		// 199 is sizeof(def) - 1
139*9a7741deSElliott Hughes 		if (tokentype != TOK_ENUM) {
140*9a7741deSElliott Hughes 			n = sscanf(buf, "%1c %199s %199s %d", &c, def, name,
141*9a7741deSElliott Hughes 			    &tok);
142*9a7741deSElliott Hughes 			if (n == 4 && c == '#' && strcmp(def, "define") == 0) {
143*9a7741deSElliott Hughes 				tokentype = TOK_DEFINE;
144*9a7741deSElliott Hughes 			} else if (tokentype != TOK_UNKNOWN) {
145*9a7741deSElliott Hughes 				continue;
146*9a7741deSElliott Hughes 			}
147*9a7741deSElliott Hughes 		}
148*9a7741deSElliott Hughes 		if (tokentype != TOK_DEFINE) {
149*9a7741deSElliott Hughes 			/* not a valid #define, bison uses enums now */
150*9a7741deSElliott Hughes 			n = sscanf(buf, "%199s = %d,\n", name, &tok);
151*9a7741deSElliott Hughes 			if (n != 2)
152*9a7741deSElliott Hughes 				continue;
153*9a7741deSElliott Hughes 			tokentype = TOK_ENUM;
154*9a7741deSElliott Hughes 		}
155*9a7741deSElliott Hughes 		if (strcmp(name, "YYSTYPE_IS_DECLARED") == 0) {
156*9a7741deSElliott Hughes 			tokentype = TOK_UNKNOWN;
157*9a7741deSElliott Hughes 			continue;
158*9a7741deSElliott Hughes 		}
159*9a7741deSElliott Hughes 		if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
160*9a7741deSElliott Hughes 			tokentype = TOK_UNKNOWN;
161*9a7741deSElliott Hughes 			/* fprintf(stderr, "maketab funny token %d %s ignored\n", tok, buf); */
162*9a7741deSElliott Hughes 			continue;
163*9a7741deSElliott Hughes 		}
164*9a7741deSElliott Hughes 		names[tok-FIRSTTOKEN] = strdup(name);
165*9a7741deSElliott Hughes 		if (names[tok-FIRSTTOKEN] == NULL) {
166*9a7741deSElliott Hughes 			fprintf(stderr, "maketab out of space copying %s", name);
167*9a7741deSElliott Hughes 			continue;
168*9a7741deSElliott Hughes 		}
169*9a7741deSElliott Hughes 		printf("\t\"%s\",\t/* %d */\n", name, tok);
170*9a7741deSElliott Hughes 		i++;
171*9a7741deSElliott Hughes 	}
172*9a7741deSElliott Hughes 	printf("};\n\n");
173*9a7741deSElliott Hughes 
174*9a7741deSElliott Hughes 	for (p=proc; p->token!=0; p++)
175*9a7741deSElliott Hughes 		table[p->token-FIRSTTOKEN] = p->name;
176*9a7741deSElliott Hughes 	printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE);
177*9a7741deSElliott Hughes 	for (i=0; i<SIZE; i++)
178*9a7741deSElliott Hughes 		printf("\t%s,\t/* %s */\n",
179*9a7741deSElliott Hughes 		    table[i] ? table[i] : "nullproc", names[i] ? names[i] : "");
180*9a7741deSElliott Hughes 	printf("};\n\n");
181*9a7741deSElliott Hughes 
182*9a7741deSElliott Hughes 	printf("const char *tokname(int n)\n");	/* print a tokname() function */
183*9a7741deSElliott Hughes 	printf("{\n");
184*9a7741deSElliott Hughes 	printf("\tstatic char buf[100];\n\n");
185*9a7741deSElliott Hughes 	printf("\tif (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
186*9a7741deSElliott Hughes 	printf("\t\tsnprintf(buf, sizeof(buf), \"token %%d\", n);\n");
187*9a7741deSElliott Hughes 	printf("\t\treturn buf;\n");
188*9a7741deSElliott Hughes 	printf("\t}\n");
189*9a7741deSElliott Hughes 	printf("\treturn printname[n-FIRSTTOKEN];\n");
190*9a7741deSElliott Hughes 	printf("}\n");
191*9a7741deSElliott Hughes 	return 0;
192*9a7741deSElliott Hughes }
193