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