xref: /illumos-gate/usr/src/cmd/awk/maketab.c (revision 63f91fbc3c024870d86dc3332a4a0080fb29bc40)
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  * CDDL HEADER START
27  *
28  * The contents of this file are subject to the terms of the
29  * Common Development and Distribution License (the "License").
30  * You may not use this file except in compliance with the License.
31  *
32  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
33  * or http://www.opensolaris.org/os/licensing.
34  * See the License for the specific language governing permissions
35  * and limitations under the License.
36  *
37  * When distributing Covered Code, include this CDDL HEADER in each
38  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
39  * If applicable, add the following below this CDDL HEADER, with the
40  * fields enclosed by brackets "[]" replaced with your own identifying
41  * information: Portions Copyright [yyyy] [name of copyright owner]
42  *
43  * CDDL HEADER END
44  */
45 
46 /*
47  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
48  */
49 
50 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
51 /*	  All Rights Reserved  	*/
52 
53 /*
54  * this program makes the table to link function names
55  * and type indices that is used by execute() in run.c.
56  * it finds the indices in ytab.h, produced by yacc.
57  */
58 
59 #include <stdio.h>
60 #include <string.h>
61 #include <stdlib.h>
62 #include <libintl.h>
63 #include "awk.h"
64 #include "y.tab.h"
65 
66 struct xx {
67 	int token;
68 	const char *name;
69 	const char *pname;
70 } proc[] = {
71 	{ PROGRAM, "program", NULL },
72 	{ BOR, "boolop", " || " },
73 	{ AND, "boolop", " && " },
74 	{ NOT, "boolop", " !" },
75 	{ NE, "relop", " != " },
76 	{ EQ, "relop", " == " },
77 	{ LE, "relop", " <= " },
78 	{ LT, "relop", " < " },
79 	{ GE, "relop", " >= " },
80 	{ GT, "relop", " > " },
81 	{ ARRAY, "array", NULL },
82 	{ INDIRECT, "indirect", "$(" },
83 	{ SUBSTR, "substr", "substr" },
84 	{ SUB, "sub", "sub" },
85 	{ GSUB, "gsub", "gsub" },
86 	{ INDEX, "sindex", "sindex" },
87 	{ SPRINTF, "awksprintf", "sprintf " },
88 	{ ADD, "arith", " + " },
89 	{ MINUS, "arith", " - " },
90 	{ MULT, "arith", " * " },
91 	{ DIVIDE, "arith", " / " },
92 	{ MOD, "arith", " % " },
93 	{ UMINUS, "arith", " -" },
94 	{ UPLUS, "arith", " +" },
95 	{ POWER, "arith", " **" },
96 	{ PREINCR, "incrdecr", "++" },
97 	{ POSTINCR, "incrdecr", "++" },
98 	{ PREDECR, "incrdecr", "--" },
99 	{ POSTDECR, "incrdecr", "--" },
100 	{ CAT, "cat", " " },
101 	{ PASTAT, "pastat", NULL },
102 	{ PASTAT2, "dopa2", NULL },
103 	{ MATCH, "matchop", " ~ " },
104 	{ NOTMATCH, "matchop", " !~ " },
105 	{ MATCHFCN, "matchop", "matchop" },
106 	{ INTEST, "intest", "intest" },
107 	{ PRINTF, "awkprintf", "printf" },
108 	{ PRINT, "printstat", "print" },
109 	{ CLOSE, "closefile", "closefile" },
110 	{ DELETE, "awkdelete", "awkdelete" },
111 	{ SPLIT, "split", "split" },
112 	{ ASSIGN, "assign", " = " },
113 	{ ADDEQ, "assign", " += " },
114 	{ SUBEQ, "assign", " -= " },
115 	{ MULTEQ, "assign", " *= " },
116 	{ DIVEQ, "assign", " /= " },
117 	{ MODEQ, "assign", " %= " },
118 	{ POWEQ, "assign", " ^= " },
119 	{ CONDEXPR, "condexpr", " ?: " },
120 	{ IF, "ifstat", "if(" },
121 	{ WHILE, "whilestat", "while(" },
122 	{ FOR, "forstat", "for(" },
123 	{ DO, "dostat", "do" },
124 	{ IN, "instat", "instat" },
125 	{ NEXT, "jump", "next" },
126 	{ NEXTFILE, "jump", "nextfile" },
127 	{ EXIT, "jump", "exit" },
128 	{ BREAK, "jump", "break" },
129 	{ CONTINUE, "jump", "continue" },
130 	{ RETURN, "jump", "ret" },
131 	{ BLTIN, "bltin", "bltin" },
132 	{ CALL, "call", "call" },
133 	{ ARG, "arg", "arg" },
134 	{ VARNF, "getnf", "NF" },
135 	{ GETLINE, "awkgetline", "getline" },
136 	{ 0, "", "" },
137 };
138 
139 #define	SIZE	(LASTTOKEN - FIRSTTOKEN + 1)
140 const char *table[SIZE];
141 char *names[SIZE];
142 
143 int
144 main(int argc, char *argv[])
145 {
146 	const struct xx *p;
147 	int i, n, tok;
148 	char c;
149 	FILE *fp;
150 	char buf[200], name[200], def[200];
151 
152 	printf("#include <stdio.h>\n");
153 	printf("#include \"awk.h\"\n");
154 	printf("#include \"y.tab.h\"\n\n");
155 
156 	if ((fp = fopen("y.tab.h", "r")) == NULL) {
157 		fprintf(stderr, gettext("maketab can't open y.tab.h!\n"));
158 		exit(1);
159 	}
160 	printf("static char *printname[%d] = {\n", SIZE);
161 	i = 0;
162 	while (fgets(buf, sizeof (buf), fp) != NULL) {
163 		n = sscanf(buf, "%1c %s %s %d", &c, def, name, &tok);
164 		if (c != '#' || (n != 4 && strcmp(def, "define") != 0)) {
165 			/* not a valid #define */
166 			continue;
167 		}
168 		if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
169 			fprintf(stderr, gettext("maketab funny token %d %s\n"),
170 			    tok, buf);
171 			exit(1);
172 		}
173 		names[tok-FIRSTTOKEN] = (char *)malloc(strlen(name)+1);
174 		strcpy(names[tok-FIRSTTOKEN], name);
175 		printf("\t(char *) \"%s\",\t/* %d */\n", name, tok);
176 		i++;
177 	}
178 	printf("};\n\n");
179 
180 	for (p = proc; p->token != 0; p++)
181 		table[p->token-FIRSTTOKEN] = p->name;
182 	printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE);
183 	for (i = 0; i < SIZE; i++)
184 		if (table[i] == 0)
185 			printf("\tnullproc,\t/* %s */\n", names[i]);
186 		else
187 			printf("\t%s,\t/* %s */\n", table[i], names[i]);
188 	printf("};\n\n");
189 
190 	printf("char *\ntokname(int n)\n");	/* print a tokname() function */
191 	printf("{\n");
192 	printf("	static char buf[100];\n\n");
193 	printf("	if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
194 	printf("		(void) sprintf(buf, \"token %%d\", n);\n");
195 	printf("		return (buf);\n");
196 	printf("	}\n");
197 	printf("	return printname[n-FIRSTTOKEN];\n");
198 	printf("}\n");
199 	return (0);
200 }
201