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