xref: /illumos-gate/usr/src/cmd/awk/maketab.c (revision 3ee4fc2a)
1*3ee4fc2aSCody Peter Mello /*
2*3ee4fc2aSCody Peter Mello  * Copyright (C) Lucent Technologies 1997
3*3ee4fc2aSCody Peter Mello  * All Rights Reserved
4*3ee4fc2aSCody Peter Mello  *
5*3ee4fc2aSCody Peter Mello  * Permission to use, copy, modify, and distribute this software and
6*3ee4fc2aSCody Peter Mello  * its documentation for any purpose and without fee is hereby
7*3ee4fc2aSCody Peter Mello  * granted, provided that the above copyright notice appear in all
8*3ee4fc2aSCody Peter Mello  * copies and that both that the copyright notice and this
9*3ee4fc2aSCody Peter Mello  * permission notice and warranty disclaimer appear in supporting
10*3ee4fc2aSCody Peter Mello  * documentation, and that the name Lucent Technologies or any of
11*3ee4fc2aSCody Peter Mello  * its entities not be used in advertising or publicity pertaining
12*3ee4fc2aSCody Peter Mello  * to distribution of the software without specific, written prior
13*3ee4fc2aSCody Peter Mello  * permission.
14*3ee4fc2aSCody Peter Mello  *
15*3ee4fc2aSCody Peter Mello  * LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16*3ee4fc2aSCody Peter Mello  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17*3ee4fc2aSCody Peter Mello  * IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
18*3ee4fc2aSCody Peter Mello  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19*3ee4fc2aSCody Peter Mello  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
20*3ee4fc2aSCody Peter Mello  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21*3ee4fc2aSCody Peter Mello  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
22*3ee4fc2aSCody Peter Mello  * THIS SOFTWARE.
23*3ee4fc2aSCody Peter Mello  */
24*3ee4fc2aSCody Peter Mello 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  * CDDL HEADER START
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
29d0983205SRoger A. Faulkner  * Common Development and Distribution License (the "License").
30d0983205SRoger A. Faulkner  * You may not use this file except in compliance with the License.
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
337c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
347c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
357c478bd9Sstevel@tonic-gate  * and limitations under the License.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
387c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
397c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
407c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
417c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * CDDL HEADER END
447c478bd9Sstevel@tonic-gate  */
45d0983205SRoger A. Faulkner 
467c478bd9Sstevel@tonic-gate /*
4723a1cceaSRoger A. Faulkner  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
517c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
527c478bd9Sstevel@tonic-gate 
53*3ee4fc2aSCody Peter Mello /*
54*3ee4fc2aSCody Peter Mello  * this program makes the table to link function names
55*3ee4fc2aSCody Peter Mello  * and type indices that is used by execute() in run.c.
56*3ee4fc2aSCody Peter Mello  * it finds the indices in ytab.h, produced by yacc.
57*3ee4fc2aSCody Peter Mello  */
58*3ee4fc2aSCody Peter Mello 
597c478bd9Sstevel@tonic-gate #include <stdio.h>
607c478bd9Sstevel@tonic-gate #include <string.h>
617c478bd9Sstevel@tonic-gate #include <stdlib.h>
627c478bd9Sstevel@tonic-gate #include <libintl.h>
637c478bd9Sstevel@tonic-gate #include "awk.h"
647c478bd9Sstevel@tonic-gate #include "y.tab.h"
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate struct xx {
677c478bd9Sstevel@tonic-gate 	int token;
68*3ee4fc2aSCody Peter Mello 	const char *name;
69*3ee4fc2aSCody Peter Mello 	const char *pname;
707c478bd9Sstevel@tonic-gate } proc[] = {
717c478bd9Sstevel@tonic-gate 	{ PROGRAM, "program", NULL },
727c478bd9Sstevel@tonic-gate 	{ BOR, "boolop", " || " },
737c478bd9Sstevel@tonic-gate 	{ AND, "boolop", " && " },
747c478bd9Sstevel@tonic-gate 	{ NOT, "boolop", " !" },
757c478bd9Sstevel@tonic-gate 	{ NE, "relop", " != " },
767c478bd9Sstevel@tonic-gate 	{ EQ, "relop", " == " },
777c478bd9Sstevel@tonic-gate 	{ LE, "relop", " <= " },
787c478bd9Sstevel@tonic-gate 	{ LT, "relop", " < " },
797c478bd9Sstevel@tonic-gate 	{ GE, "relop", " >= " },
807c478bd9Sstevel@tonic-gate 	{ GT, "relop", " > " },
817c478bd9Sstevel@tonic-gate 	{ ARRAY, "array", NULL },
827c478bd9Sstevel@tonic-gate 	{ INDIRECT, "indirect", "$(" },
837c478bd9Sstevel@tonic-gate 	{ SUBSTR, "substr", "substr" },
847c478bd9Sstevel@tonic-gate 	{ SUB, "sub", "sub" },
857c478bd9Sstevel@tonic-gate 	{ GSUB, "gsub", "gsub" },
867c478bd9Sstevel@tonic-gate 	{ INDEX, "sindex", "sindex" },
87*3ee4fc2aSCody Peter Mello 	{ SPRINTF, "awksprintf", "sprintf " },
887c478bd9Sstevel@tonic-gate 	{ ADD, "arith", " + " },
897c478bd9Sstevel@tonic-gate 	{ MINUS, "arith", " - " },
907c478bd9Sstevel@tonic-gate 	{ MULT, "arith", " * " },
917c478bd9Sstevel@tonic-gate 	{ DIVIDE, "arith", " / " },
927c478bd9Sstevel@tonic-gate 	{ MOD, "arith", " % " },
937c478bd9Sstevel@tonic-gate 	{ UMINUS, "arith", " -" },
94*3ee4fc2aSCody Peter Mello 	{ UPLUS, "arith", " +" },
957c478bd9Sstevel@tonic-gate 	{ POWER, "arith", " **" },
967c478bd9Sstevel@tonic-gate 	{ PREINCR, "incrdecr", "++" },
977c478bd9Sstevel@tonic-gate 	{ POSTINCR, "incrdecr", "++" },
987c478bd9Sstevel@tonic-gate 	{ PREDECR, "incrdecr", "--" },
997c478bd9Sstevel@tonic-gate 	{ POSTDECR, "incrdecr", "--" },
1007c478bd9Sstevel@tonic-gate 	{ CAT, "cat", " " },
1017c478bd9Sstevel@tonic-gate 	{ PASTAT, "pastat", NULL },
1027c478bd9Sstevel@tonic-gate 	{ PASTAT2, "dopa2", NULL },
1037c478bd9Sstevel@tonic-gate 	{ MATCH, "matchop", " ~ " },
1047c478bd9Sstevel@tonic-gate 	{ NOTMATCH, "matchop", " !~ " },
1057c478bd9Sstevel@tonic-gate 	{ MATCHFCN, "matchop", "matchop" },
1067c478bd9Sstevel@tonic-gate 	{ INTEST, "intest", "intest" },
107*3ee4fc2aSCody Peter Mello 	{ PRINTF, "awkprintf", "printf" },
108*3ee4fc2aSCody Peter Mello 	{ PRINT, "printstat", "print" },
1097c478bd9Sstevel@tonic-gate 	{ CLOSE, "closefile", "closefile" },
110*3ee4fc2aSCody Peter Mello 	{ DELETE, "awkdelete", "awkdelete" },
1117c478bd9Sstevel@tonic-gate 	{ SPLIT, "split", "split" },
1127c478bd9Sstevel@tonic-gate 	{ ASSIGN, "assign", " = " },
1137c478bd9Sstevel@tonic-gate 	{ ADDEQ, "assign", " += " },
1147c478bd9Sstevel@tonic-gate 	{ SUBEQ, "assign", " -= " },
1157c478bd9Sstevel@tonic-gate 	{ MULTEQ, "assign", " *= " },
1167c478bd9Sstevel@tonic-gate 	{ DIVEQ, "assign", " /= " },
1177c478bd9Sstevel@tonic-gate 	{ MODEQ, "assign", " %= " },
1187c478bd9Sstevel@tonic-gate 	{ POWEQ, "assign", " ^= " },
1197c478bd9Sstevel@tonic-gate 	{ CONDEXPR, "condexpr", " ?: " },
1207c478bd9Sstevel@tonic-gate 	{ IF, "ifstat", "if(" },
1217c478bd9Sstevel@tonic-gate 	{ WHILE, "whilestat", "while(" },
1227c478bd9Sstevel@tonic-gate 	{ FOR, "forstat", "for(" },
1237c478bd9Sstevel@tonic-gate 	{ DO, "dostat", "do" },
1247c478bd9Sstevel@tonic-gate 	{ IN, "instat", "instat" },
1257c478bd9Sstevel@tonic-gate 	{ NEXT, "jump", "next" },
126*3ee4fc2aSCody Peter Mello 	{ NEXTFILE, "jump", "nextfile" },
1277c478bd9Sstevel@tonic-gate 	{ EXIT, "jump", "exit" },
1287c478bd9Sstevel@tonic-gate 	{ BREAK, "jump", "break" },
1297c478bd9Sstevel@tonic-gate 	{ CONTINUE, "jump", "continue" },
1307c478bd9Sstevel@tonic-gate 	{ RETURN, "jump", "ret" },
1317c478bd9Sstevel@tonic-gate 	{ BLTIN, "bltin", "bltin" },
1327c478bd9Sstevel@tonic-gate 	{ CALL, "call", "call" },
1337c478bd9Sstevel@tonic-gate 	{ ARG, "arg", "arg" },
1347c478bd9Sstevel@tonic-gate 	{ VARNF, "getnf", "NF" },
135*3ee4fc2aSCody Peter Mello 	{ GETLINE, "awkgetline", "getline" },
1367c478bd9Sstevel@tonic-gate 	{ 0, "", "" },
1377c478bd9Sstevel@tonic-gate };
1387c478bd9Sstevel@tonic-gate 
139*3ee4fc2aSCody Peter Mello #define	SIZE	(LASTTOKEN - FIRSTTOKEN + 1)
140*3ee4fc2aSCody Peter Mello const char *table[SIZE];
1417c478bd9Sstevel@tonic-gate char *names[SIZE];
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])144*3ee4fc2aSCody Peter Mello main(int argc, char *argv[])
1457c478bd9Sstevel@tonic-gate {
146*3ee4fc2aSCody Peter Mello 	const struct xx *p;
1477c478bd9Sstevel@tonic-gate 	int i, n, tok;
1487c478bd9Sstevel@tonic-gate 	char c;
1497c478bd9Sstevel@tonic-gate 	FILE *fp;
150*3ee4fc2aSCody Peter Mello 	char buf[200], name[200], def[200];
1517c478bd9Sstevel@tonic-gate 
152*3ee4fc2aSCody Peter Mello 	printf("#include <stdio.h>\n");
1537c478bd9Sstevel@tonic-gate 	printf("#include \"awk.h\"\n");
1547c478bd9Sstevel@tonic-gate 	printf("#include \"y.tab.h\"\n\n");
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if ((fp = fopen("y.tab.h", "r")) == NULL) {
1577c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("maketab can't open y.tab.h!\n"));
1587c478bd9Sstevel@tonic-gate 		exit(1);
1597c478bd9Sstevel@tonic-gate 	}
160*3ee4fc2aSCody Peter Mello 	printf("static char *printname[%d] = {\n", SIZE);
1617c478bd9Sstevel@tonic-gate 	i = 0;
1627c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
1637c478bd9Sstevel@tonic-gate 		n = sscanf(buf, "%1c %s %s %d", &c, def, name, &tok);
164*3ee4fc2aSCody Peter Mello 		if (c != '#' || (n != 4 && strcmp(def, "define") != 0)) {
165*3ee4fc2aSCody Peter Mello 			/* not a valid #define */
1667c478bd9Sstevel@tonic-gate 			continue;
167*3ee4fc2aSCody Peter Mello 		}
1687c478bd9Sstevel@tonic-gate 		if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
1697c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("maketab funny token %d %s\n"),
1707c478bd9Sstevel@tonic-gate 			    tok, buf);
1717c478bd9Sstevel@tonic-gate 			exit(1);
1727c478bd9Sstevel@tonic-gate 		}
173*3ee4fc2aSCody Peter Mello 		names[tok-FIRSTTOKEN] = (char *)malloc(strlen(name)+1);
1747c478bd9Sstevel@tonic-gate 		strcpy(names[tok-FIRSTTOKEN], name);
175*3ee4fc2aSCody Peter Mello 		printf("\t(char *) \"%s\",\t/* %d */\n", name, tok);
1767c478bd9Sstevel@tonic-gate 		i++;
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 	printf("};\n\n");
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	for (p = proc; p->token != 0; p++)
1817c478bd9Sstevel@tonic-gate 		table[p->token-FIRSTTOKEN] = p->name;
182*3ee4fc2aSCody Peter Mello 	printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE);
1837c478bd9Sstevel@tonic-gate 	for (i = 0; i < SIZE; i++)
1847c478bd9Sstevel@tonic-gate 		if (table[i] == 0)
1857c478bd9Sstevel@tonic-gate 			printf("\tnullproc,\t/* %s */\n", names[i]);
1867c478bd9Sstevel@tonic-gate 		else
1877c478bd9Sstevel@tonic-gate 			printf("\t%s,\t/* %s */\n", table[i], names[i]);
1887c478bd9Sstevel@tonic-gate 	printf("};\n\n");
1897c478bd9Sstevel@tonic-gate 
190*3ee4fc2aSCody Peter Mello 	printf("char *\ntokname(int n)\n");	/* print a tokname() function */
1917c478bd9Sstevel@tonic-gate 	printf("{\n");
1921ee2e5faSnakanon 	printf("	static char buf[100];\n\n");
1937c478bd9Sstevel@tonic-gate 	printf("	if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
1941ee2e5faSnakanon 	printf("		(void) sprintf(buf, \"token %%d\", n);\n");
195*3ee4fc2aSCody Peter Mello 	printf("		return (buf);\n");
1967c478bd9Sstevel@tonic-gate 	printf("	}\n");
197*3ee4fc2aSCody Peter Mello 	printf("	return printname[n-FIRSTTOKEN];\n");
1987c478bd9Sstevel@tonic-gate 	printf("}\n");
199*3ee4fc2aSCody Peter Mello 	return (0);
2007c478bd9Sstevel@tonic-gate }
201