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