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