xref: /titanic_54/usr/src/cmd/awk/maketab.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 2.5	*/
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <string.h>
35*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
36*7c478bd9Sstevel@tonic-gate #include <libintl.h>
37*7c478bd9Sstevel@tonic-gate #include "awk.h"
38*7c478bd9Sstevel@tonic-gate #include "y.tab.h"
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate struct xx {
41*7c478bd9Sstevel@tonic-gate 	int token;
42*7c478bd9Sstevel@tonic-gate 	char *name;
43*7c478bd9Sstevel@tonic-gate 	char *pname;
44*7c478bd9Sstevel@tonic-gate } proc[] = {
45*7c478bd9Sstevel@tonic-gate 	{ PROGRAM, "program", NULL },
46*7c478bd9Sstevel@tonic-gate 	{ BOR, "boolop", " || " },
47*7c478bd9Sstevel@tonic-gate 	{ AND, "boolop", " && " },
48*7c478bd9Sstevel@tonic-gate 	{ NOT, "boolop", " !" },
49*7c478bd9Sstevel@tonic-gate 	{ NE, "relop", " != " },
50*7c478bd9Sstevel@tonic-gate 	{ EQ, "relop", " == " },
51*7c478bd9Sstevel@tonic-gate 	{ LE, "relop", " <= " },
52*7c478bd9Sstevel@tonic-gate 	{ LT, "relop", " < " },
53*7c478bd9Sstevel@tonic-gate 	{ GE, "relop", " >= " },
54*7c478bd9Sstevel@tonic-gate 	{ GT, "relop", " > " },
55*7c478bd9Sstevel@tonic-gate 	{ ARRAY, "array", NULL },
56*7c478bd9Sstevel@tonic-gate 	{ INDIRECT, "indirect", "$(" },
57*7c478bd9Sstevel@tonic-gate 	{ SUBSTR, "substr", "substr" },
58*7c478bd9Sstevel@tonic-gate 	{ SUB, "sub", "sub" },
59*7c478bd9Sstevel@tonic-gate 	{ GSUB, "gsub", "gsub" },
60*7c478bd9Sstevel@tonic-gate 	{ INDEX, "sindex", "sindex" },
61*7c478bd9Sstevel@tonic-gate 	{ SPRINTF, "asprintf", "sprintf " },
62*7c478bd9Sstevel@tonic-gate 	{ ADD, "arith", " + " },
63*7c478bd9Sstevel@tonic-gate 	{ MINUS, "arith", " - " },
64*7c478bd9Sstevel@tonic-gate 	{ MULT, "arith", " * " },
65*7c478bd9Sstevel@tonic-gate 	{ DIVIDE, "arith", " / " },
66*7c478bd9Sstevel@tonic-gate 	{ MOD, "arith", " % " },
67*7c478bd9Sstevel@tonic-gate 	{ UMINUS, "arith", " -" },
68*7c478bd9Sstevel@tonic-gate 	{ POWER, "arith", " **" },
69*7c478bd9Sstevel@tonic-gate 	{ PREINCR, "incrdecr", "++" },
70*7c478bd9Sstevel@tonic-gate 	{ POSTINCR, "incrdecr", "++" },
71*7c478bd9Sstevel@tonic-gate 	{ PREDECR, "incrdecr", "--" },
72*7c478bd9Sstevel@tonic-gate 	{ POSTDECR, "incrdecr", "--" },
73*7c478bd9Sstevel@tonic-gate 	{ CAT, "cat", " " },
74*7c478bd9Sstevel@tonic-gate 	{ PASTAT, "pastat", NULL },
75*7c478bd9Sstevel@tonic-gate 	{ PASTAT2, "dopa2", NULL },
76*7c478bd9Sstevel@tonic-gate 	{ MATCH, "matchop", " ~ " },
77*7c478bd9Sstevel@tonic-gate 	{ NOTMATCH, "matchop", " !~ " },
78*7c478bd9Sstevel@tonic-gate 	{ MATCHFCN, "matchop", "matchop" },
79*7c478bd9Sstevel@tonic-gate 	{ INTEST, "intest", "intest" },
80*7c478bd9Sstevel@tonic-gate 	{ PRINTF, "aprintf", "printf" },
81*7c478bd9Sstevel@tonic-gate 	{ PRINT, "print", "print" },
82*7c478bd9Sstevel@tonic-gate 	{ CLOSE, "closefile", "closefile" },
83*7c478bd9Sstevel@tonic-gate 	{ DELETE, "delete", "delete" },
84*7c478bd9Sstevel@tonic-gate 	{ SPLIT, "split", "split" },
85*7c478bd9Sstevel@tonic-gate 	{ ASSIGN, "assign", " = " },
86*7c478bd9Sstevel@tonic-gate 	{ ADDEQ, "assign", " += " },
87*7c478bd9Sstevel@tonic-gate 	{ SUBEQ, "assign", " -= " },
88*7c478bd9Sstevel@tonic-gate 	{ MULTEQ, "assign", " *= " },
89*7c478bd9Sstevel@tonic-gate 	{ DIVEQ, "assign", " /= " },
90*7c478bd9Sstevel@tonic-gate 	{ MODEQ, "assign", " %= " },
91*7c478bd9Sstevel@tonic-gate 	{ POWEQ, "assign", " ^= " },
92*7c478bd9Sstevel@tonic-gate 	{ CONDEXPR, "condexpr", " ?: " },
93*7c478bd9Sstevel@tonic-gate 	{ IF, "ifstat", "if(" },
94*7c478bd9Sstevel@tonic-gate 	{ WHILE, "whilestat", "while(" },
95*7c478bd9Sstevel@tonic-gate 	{ FOR, "forstat", "for(" },
96*7c478bd9Sstevel@tonic-gate 	{ DO, "dostat", "do" },
97*7c478bd9Sstevel@tonic-gate 	{ IN, "instat", "instat" },
98*7c478bd9Sstevel@tonic-gate 	{ NEXT, "jump", "next" },
99*7c478bd9Sstevel@tonic-gate 	{ EXIT, "jump", "exit" },
100*7c478bd9Sstevel@tonic-gate 	{ BREAK, "jump", "break" },
101*7c478bd9Sstevel@tonic-gate 	{ CONTINUE, "jump", "continue" },
102*7c478bd9Sstevel@tonic-gate 	{ RETURN, "jump", "ret" },
103*7c478bd9Sstevel@tonic-gate 	{ BLTIN, "bltin", "bltin" },
104*7c478bd9Sstevel@tonic-gate 	{ CALL, "call", "call" },
105*7c478bd9Sstevel@tonic-gate 	{ ARG, "arg", "arg" },
106*7c478bd9Sstevel@tonic-gate 	{ VARNF, "getnf", "NF" },
107*7c478bd9Sstevel@tonic-gate 	{ GETLINE, "getline", "getline" },
108*7c478bd9Sstevel@tonic-gate 	{ 0, "", "" },
109*7c478bd9Sstevel@tonic-gate };
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate #define	SIZE	LASTTOKEN - FIRSTTOKEN + 1
112*7c478bd9Sstevel@tonic-gate char *table[SIZE];
113*7c478bd9Sstevel@tonic-gate char *names[SIZE];
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate int
116*7c478bd9Sstevel@tonic-gate main()
117*7c478bd9Sstevel@tonic-gate {
118*7c478bd9Sstevel@tonic-gate 	struct xx *p;
119*7c478bd9Sstevel@tonic-gate 	int i, n, tok;
120*7c478bd9Sstevel@tonic-gate 	char c;
121*7c478bd9Sstevel@tonic-gate 	FILE *fp;
122*7c478bd9Sstevel@tonic-gate 	char buf[100], name[100], def[100];
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	printf("#include \"awk.h\"\n");
125*7c478bd9Sstevel@tonic-gate 	printf("#include \"y.tab.h\"\n\n");
126*7c478bd9Sstevel@tonic-gate 	printf("Cell *nullproc();\n");
127*7c478bd9Sstevel@tonic-gate 	for (i = SIZE; --i >= 0; )
128*7c478bd9Sstevel@tonic-gate 		names[i] = "";
129*7c478bd9Sstevel@tonic-gate 	for (p = proc; p->token != 0; p++)
130*7c478bd9Sstevel@tonic-gate 		if (p == proc || strcmp(p->name, (p-1)->name))
131*7c478bd9Sstevel@tonic-gate 			printf("extern Cell *%s();\n", p->name);
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	if ((fp = fopen("y.tab.h", "r")) == NULL) {
134*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("maketab can't open y.tab.h!\n"));
135*7c478bd9Sstevel@tonic-gate 		exit(1);
136*7c478bd9Sstevel@tonic-gate 	}
137*7c478bd9Sstevel@tonic-gate 	printf("static uchar *printname[%d] = {\n", SIZE);
138*7c478bd9Sstevel@tonic-gate 	i = 0;
139*7c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
140*7c478bd9Sstevel@tonic-gate 		n = sscanf(buf, "%1c %s %s %d", &c, def, name, &tok);
141*7c478bd9Sstevel@tonic-gate 		/* not a valid #define? */
142*7c478bd9Sstevel@tonic-gate 		if (c != '#' || n != 4 && strcmp(def, "define") != 0)
143*7c478bd9Sstevel@tonic-gate 			continue;
144*7c478bd9Sstevel@tonic-gate 		if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
145*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("maketab funny token %d %s\n"),
146*7c478bd9Sstevel@tonic-gate 			    tok, buf);
147*7c478bd9Sstevel@tonic-gate 			exit(1);
148*7c478bd9Sstevel@tonic-gate 		}
149*7c478bd9Sstevel@tonic-gate 		names[tok-FIRSTTOKEN] = malloc(strlen(name)+1);
150*7c478bd9Sstevel@tonic-gate 		strcpy(names[tok-FIRSTTOKEN], name);
151*7c478bd9Sstevel@tonic-gate 		printf("\t(uchar *) \"%s\",\t/* %d */\n", name, tok);
152*7c478bd9Sstevel@tonic-gate 		i++;
153*7c478bd9Sstevel@tonic-gate 	}
154*7c478bd9Sstevel@tonic-gate 	printf("};\n\n");
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	for (p = proc; p->token != 0; p++)
157*7c478bd9Sstevel@tonic-gate 		table[p->token-FIRSTTOKEN] = p->name;
158*7c478bd9Sstevel@tonic-gate 	printf("\nCell *(*proctab[%d])() = {\n", SIZE);
159*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < SIZE; i++)
160*7c478bd9Sstevel@tonic-gate 		if (table[i] == 0)
161*7c478bd9Sstevel@tonic-gate 			printf("\tnullproc,\t/* %s */\n", names[i]);
162*7c478bd9Sstevel@tonic-gate 		else
163*7c478bd9Sstevel@tonic-gate 			printf("\t%s,\t/* %s */\n", table[i], names[i]);
164*7c478bd9Sstevel@tonic-gate 	printf("};\n\n");
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	printf("uchar *tokname(n)\n");	/* print a tokname() function */
167*7c478bd9Sstevel@tonic-gate 	printf("{\n");
168*7c478bd9Sstevel@tonic-gate 	printf("	static uchar buf[100];\n\n");
169*7c478bd9Sstevel@tonic-gate 	printf("	if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
170*7c478bd9Sstevel@tonic-gate 	printf("		sprintf(buf, \"token %%d\", n);\n");
171*7c478bd9Sstevel@tonic-gate 	printf("		return buf;\n");
172*7c478bd9Sstevel@tonic-gate 	printf("	}\n");
173*7c478bd9Sstevel@tonic-gate 	printf("	return printname[n-257];\n");
174*7c478bd9Sstevel@tonic-gate 	printf("}\n");
175*7c478bd9Sstevel@tonic-gate 	exit(0);
176*7c478bd9Sstevel@tonic-gate }
177