xref: /illumos-gate/usr/src/cmd/awk/maketab.c (revision 5093e10312724e92dd76c008a15bdb59d414708a)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <libintl.h>
34 #include "awk.h"
35 #include "y.tab.h"
36 
37 struct xx {
38 	int token;
39 	char *name;
40 	char *pname;
41 } proc[] = {
42 	{ PROGRAM, "program", NULL },
43 	{ BOR, "boolop", " || " },
44 	{ AND, "boolop", " && " },
45 	{ NOT, "boolop", " !" },
46 	{ NE, "relop", " != " },
47 	{ EQ, "relop", " == " },
48 	{ LE, "relop", " <= " },
49 	{ LT, "relop", " < " },
50 	{ GE, "relop", " >= " },
51 	{ GT, "relop", " > " },
52 	{ ARRAY, "array", NULL },
53 	{ INDIRECT, "indirect", "$(" },
54 	{ SUBSTR, "substr", "substr" },
55 	{ SUB, "sub", "sub" },
56 	{ GSUB, "gsub", "gsub" },
57 	{ INDEX, "sindex", "sindex" },
58 	{ SPRINTF, "a_sprintf", "sprintf " },
59 	{ ADD, "arith", " + " },
60 	{ MINUS, "arith", " - " },
61 	{ MULT, "arith", " * " },
62 	{ DIVIDE, "arith", " / " },
63 	{ MOD, "arith", " % " },
64 	{ UMINUS, "arith", " -" },
65 	{ POWER, "arith", " **" },
66 	{ PREINCR, "incrdecr", "++" },
67 	{ POSTINCR, "incrdecr", "++" },
68 	{ PREDECR, "incrdecr", "--" },
69 	{ POSTDECR, "incrdecr", "--" },
70 	{ CAT, "cat", " " },
71 	{ PASTAT, "pastat", NULL },
72 	{ PASTAT2, "dopa2", NULL },
73 	{ MATCH, "matchop", " ~ " },
74 	{ NOTMATCH, "matchop", " !~ " },
75 	{ MATCHFCN, "matchop", "matchop" },
76 	{ INTEST, "intest", "intest" },
77 	{ PRINTF, "aprintf", "printf" },
78 	{ PRINT, "print", "print" },
79 	{ CLOSE, "closefile", "closefile" },
80 	{ DELETE, "delete", "delete" },
81 	{ SPLIT, "split", "split" },
82 	{ ASSIGN, "assign", " = " },
83 	{ ADDEQ, "assign", " += " },
84 	{ SUBEQ, "assign", " -= " },
85 	{ MULTEQ, "assign", " *= " },
86 	{ DIVEQ, "assign", " /= " },
87 	{ MODEQ, "assign", " %= " },
88 	{ POWEQ, "assign", " ^= " },
89 	{ CONDEXPR, "condexpr", " ?: " },
90 	{ IF, "ifstat", "if(" },
91 	{ WHILE, "whilestat", "while(" },
92 	{ FOR, "forstat", "for(" },
93 	{ DO, "dostat", "do" },
94 	{ IN, "instat", "instat" },
95 	{ NEXT, "jump", "next" },
96 	{ EXIT, "jump", "exit" },
97 	{ BREAK, "jump", "break" },
98 	{ CONTINUE, "jump", "continue" },
99 	{ RETURN, "jump", "ret" },
100 	{ BLTIN, "bltin", "bltin" },
101 	{ CALL, "call", "call" },
102 	{ ARG, "arg", "arg" },
103 	{ VARNF, "getnf", "NF" },
104 	{ GETLINE, "getline", "getline" },
105 	{ 0, "", "" },
106 };
107 
108 #define	SIZE	LASTTOKEN - FIRSTTOKEN + 1
109 char *table[SIZE];
110 char *names[SIZE];
111 
112 int
113 main()
114 {
115 	struct xx *p;
116 	int i, n, tok;
117 	char c;
118 	FILE *fp;
119 	char buf[100], name[100], def[100];
120 
121 	printf("#include \"awk.h\"\n");
122 	printf("#include \"y.tab.h\"\n\n");
123 
124 	if ((fp = fopen("y.tab.h", "r")) == NULL) {
125 		fprintf(stderr, gettext("maketab can't open y.tab.h!\n"));
126 		exit(1);
127 	}
128 	printf("static uchar *printname[%d] = {\n", SIZE);
129 	i = 0;
130 	while (fgets(buf, sizeof (buf), fp) != NULL) {
131 		n = sscanf(buf, "%1c %s %s %d", &c, def, name, &tok);
132 		/* not a valid #define? */
133 		if (c != '#' || n != 4 && strcmp(def, "define") != 0)
134 			continue;
135 		if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
136 			fprintf(stderr, gettext("maketab funny token %d %s\n"),
137 			    tok, buf);
138 			exit(1);
139 		}
140 		names[tok-FIRSTTOKEN] = malloc(strlen(name)+1);
141 		strcpy(names[tok-FIRSTTOKEN], name);
142 		printf("\t(uchar *) \"%s\",\t/* %d */\n", name, tok);
143 		i++;
144 	}
145 	printf("};\n\n");
146 
147 	for (p = proc; p->token != 0; p++)
148 		table[p->token-FIRSTTOKEN] = p->name;
149 	printf("\nCell *(*proctab[%d])() = {\n", SIZE);
150 	for (i = 0; i < SIZE; i++)
151 		if (table[i] == 0)
152 			printf("\tnullproc,\t/* %s */\n", names[i]);
153 		else
154 			printf("\t%s,\t/* %s */\n", table[i], names[i]);
155 	printf("};\n\n");
156 
157 	printf("uchar *\ntokname(int n)\n");	/* print a tokname() function */
158 	printf("{\n");
159 	printf("	static char buf[100];\n\n");
160 	printf("	if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
161 	printf("		(void) sprintf(buf, \"token %%d\", n);\n");
162 	printf("		return ((uchar *)buf);\n");
163 	printf("	}\n");
164 	printf("	return printname[n-257];\n");
165 	printf("}\n");
166 	exit(0);
167 }
168