xref: /titanic_44/usr/src/cmd/awk/maketab.c (revision 6a634c9dca3093f3922e4b7ab826d7bdf17bf78e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d0983205SRoger A. Faulkner  * Common Development and Distribution License (the "License").
6d0983205SRoger A. Faulkner  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21d0983205SRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
23*23a1cceaSRoger A. Faulkner  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <libintl.h>
337c478bd9Sstevel@tonic-gate #include "awk.h"
347c478bd9Sstevel@tonic-gate #include "y.tab.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate struct xx {
377c478bd9Sstevel@tonic-gate 	int token;
387c478bd9Sstevel@tonic-gate 	char *name;
397c478bd9Sstevel@tonic-gate 	char *pname;
407c478bd9Sstevel@tonic-gate } proc[] = {
417c478bd9Sstevel@tonic-gate 	{ PROGRAM, "program", NULL },
427c478bd9Sstevel@tonic-gate 	{ BOR, "boolop", " || " },
437c478bd9Sstevel@tonic-gate 	{ AND, "boolop", " && " },
447c478bd9Sstevel@tonic-gate 	{ NOT, "boolop", " !" },
457c478bd9Sstevel@tonic-gate 	{ NE, "relop", " != " },
467c478bd9Sstevel@tonic-gate 	{ EQ, "relop", " == " },
477c478bd9Sstevel@tonic-gate 	{ LE, "relop", " <= " },
487c478bd9Sstevel@tonic-gate 	{ LT, "relop", " < " },
497c478bd9Sstevel@tonic-gate 	{ GE, "relop", " >= " },
507c478bd9Sstevel@tonic-gate 	{ GT, "relop", " > " },
517c478bd9Sstevel@tonic-gate 	{ ARRAY, "array", NULL },
527c478bd9Sstevel@tonic-gate 	{ INDIRECT, "indirect", "$(" },
537c478bd9Sstevel@tonic-gate 	{ SUBSTR, "substr", "substr" },
547c478bd9Sstevel@tonic-gate 	{ SUB, "sub", "sub" },
557c478bd9Sstevel@tonic-gate 	{ GSUB, "gsub", "gsub" },
567c478bd9Sstevel@tonic-gate 	{ INDEX, "sindex", "sindex" },
57d0983205SRoger A. Faulkner 	{ SPRINTF, "a_sprintf", "sprintf " },
587c478bd9Sstevel@tonic-gate 	{ ADD, "arith", " + " },
597c478bd9Sstevel@tonic-gate 	{ MINUS, "arith", " - " },
607c478bd9Sstevel@tonic-gate 	{ MULT, "arith", " * " },
617c478bd9Sstevel@tonic-gate 	{ DIVIDE, "arith", " / " },
627c478bd9Sstevel@tonic-gate 	{ MOD, "arith", " % " },
637c478bd9Sstevel@tonic-gate 	{ UMINUS, "arith", " -" },
647c478bd9Sstevel@tonic-gate 	{ POWER, "arith", " **" },
657c478bd9Sstevel@tonic-gate 	{ PREINCR, "incrdecr", "++" },
667c478bd9Sstevel@tonic-gate 	{ POSTINCR, "incrdecr", "++" },
677c478bd9Sstevel@tonic-gate 	{ PREDECR, "incrdecr", "--" },
687c478bd9Sstevel@tonic-gate 	{ POSTDECR, "incrdecr", "--" },
697c478bd9Sstevel@tonic-gate 	{ CAT, "cat", " " },
707c478bd9Sstevel@tonic-gate 	{ PASTAT, "pastat", NULL },
717c478bd9Sstevel@tonic-gate 	{ PASTAT2, "dopa2", NULL },
727c478bd9Sstevel@tonic-gate 	{ MATCH, "matchop", " ~ " },
737c478bd9Sstevel@tonic-gate 	{ NOTMATCH, "matchop", " !~ " },
747c478bd9Sstevel@tonic-gate 	{ MATCHFCN, "matchop", "matchop" },
757c478bd9Sstevel@tonic-gate 	{ INTEST, "intest", "intest" },
767c478bd9Sstevel@tonic-gate 	{ PRINTF, "aprintf", "printf" },
777c478bd9Sstevel@tonic-gate 	{ PRINT, "print", "print" },
787c478bd9Sstevel@tonic-gate 	{ CLOSE, "closefile", "closefile" },
797c478bd9Sstevel@tonic-gate 	{ DELETE, "delete", "delete" },
807c478bd9Sstevel@tonic-gate 	{ SPLIT, "split", "split" },
817c478bd9Sstevel@tonic-gate 	{ ASSIGN, "assign", " = " },
827c478bd9Sstevel@tonic-gate 	{ ADDEQ, "assign", " += " },
837c478bd9Sstevel@tonic-gate 	{ SUBEQ, "assign", " -= " },
847c478bd9Sstevel@tonic-gate 	{ MULTEQ, "assign", " *= " },
857c478bd9Sstevel@tonic-gate 	{ DIVEQ, "assign", " /= " },
867c478bd9Sstevel@tonic-gate 	{ MODEQ, "assign", " %= " },
877c478bd9Sstevel@tonic-gate 	{ POWEQ, "assign", " ^= " },
887c478bd9Sstevel@tonic-gate 	{ CONDEXPR, "condexpr", " ?: " },
897c478bd9Sstevel@tonic-gate 	{ IF, "ifstat", "if(" },
907c478bd9Sstevel@tonic-gate 	{ WHILE, "whilestat", "while(" },
917c478bd9Sstevel@tonic-gate 	{ FOR, "forstat", "for(" },
927c478bd9Sstevel@tonic-gate 	{ DO, "dostat", "do" },
937c478bd9Sstevel@tonic-gate 	{ IN, "instat", "instat" },
947c478bd9Sstevel@tonic-gate 	{ NEXT, "jump", "next" },
957c478bd9Sstevel@tonic-gate 	{ EXIT, "jump", "exit" },
967c478bd9Sstevel@tonic-gate 	{ BREAK, "jump", "break" },
977c478bd9Sstevel@tonic-gate 	{ CONTINUE, "jump", "continue" },
987c478bd9Sstevel@tonic-gate 	{ RETURN, "jump", "ret" },
997c478bd9Sstevel@tonic-gate 	{ BLTIN, "bltin", "bltin" },
1007c478bd9Sstevel@tonic-gate 	{ CALL, "call", "call" },
1017c478bd9Sstevel@tonic-gate 	{ ARG, "arg", "arg" },
1027c478bd9Sstevel@tonic-gate 	{ VARNF, "getnf", "NF" },
103*23a1cceaSRoger A. Faulkner 	{ GETLINE, "getaline", "getline" },
1047c478bd9Sstevel@tonic-gate 	{ 0, "", "" },
1057c478bd9Sstevel@tonic-gate };
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #define	SIZE	LASTTOKEN - FIRSTTOKEN + 1
1087c478bd9Sstevel@tonic-gate char *table[SIZE];
1097c478bd9Sstevel@tonic-gate char *names[SIZE];
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate int
main()1127c478bd9Sstevel@tonic-gate main()
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	struct xx *p;
1157c478bd9Sstevel@tonic-gate 	int i, n, tok;
1167c478bd9Sstevel@tonic-gate 	char c;
1177c478bd9Sstevel@tonic-gate 	FILE *fp;
1187c478bd9Sstevel@tonic-gate 	char buf[100], name[100], def[100];
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	printf("#include \"awk.h\"\n");
1217c478bd9Sstevel@tonic-gate 	printf("#include \"y.tab.h\"\n\n");
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	if ((fp = fopen("y.tab.h", "r")) == NULL) {
1247c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("maketab can't open y.tab.h!\n"));
1257c478bd9Sstevel@tonic-gate 		exit(1);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 	printf("static uchar *printname[%d] = {\n", SIZE);
1287c478bd9Sstevel@tonic-gate 	i = 0;
1297c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
1307c478bd9Sstevel@tonic-gate 		n = sscanf(buf, "%1c %s %s %d", &c, def, name, &tok);
1317c478bd9Sstevel@tonic-gate 		/* not a valid #define? */
1327c478bd9Sstevel@tonic-gate 		if (c != '#' || n != 4 && strcmp(def, "define") != 0)
1337c478bd9Sstevel@tonic-gate 			continue;
1347c478bd9Sstevel@tonic-gate 		if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
1357c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("maketab funny token %d %s\n"),
1367c478bd9Sstevel@tonic-gate 			    tok, buf);
1377c478bd9Sstevel@tonic-gate 			exit(1);
1387c478bd9Sstevel@tonic-gate 		}
1397c478bd9Sstevel@tonic-gate 		names[tok-FIRSTTOKEN] = malloc(strlen(name)+1);
1407c478bd9Sstevel@tonic-gate 		strcpy(names[tok-FIRSTTOKEN], name);
1417c478bd9Sstevel@tonic-gate 		printf("\t(uchar *) \"%s\",\t/* %d */\n", name, tok);
1427c478bd9Sstevel@tonic-gate 		i++;
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 	printf("};\n\n");
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	for (p = proc; p->token != 0; p++)
1477c478bd9Sstevel@tonic-gate 		table[p->token-FIRSTTOKEN] = p->name;
1487c478bd9Sstevel@tonic-gate 	printf("\nCell *(*proctab[%d])() = {\n", SIZE);
1497c478bd9Sstevel@tonic-gate 	for (i = 0; i < SIZE; i++)
1507c478bd9Sstevel@tonic-gate 		if (table[i] == 0)
1517c478bd9Sstevel@tonic-gate 			printf("\tnullproc,\t/* %s */\n", names[i]);
1527c478bd9Sstevel@tonic-gate 		else
1537c478bd9Sstevel@tonic-gate 			printf("\t%s,\t/* %s */\n", table[i], names[i]);
1547c478bd9Sstevel@tonic-gate 	printf("};\n\n");
1557c478bd9Sstevel@tonic-gate 
1561ee2e5faSnakanon 	printf("uchar *\ntokname(int n)\n");	/* print a tokname() function */
1577c478bd9Sstevel@tonic-gate 	printf("{\n");
1581ee2e5faSnakanon 	printf("	static char buf[100];\n\n");
1597c478bd9Sstevel@tonic-gate 	printf("	if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
1601ee2e5faSnakanon 	printf("		(void) sprintf(buf, \"token %%d\", n);\n");
1611ee2e5faSnakanon 	printf("		return ((uchar *)buf);\n");
1627c478bd9Sstevel@tonic-gate 	printf("	}\n");
1637c478bd9Sstevel@tonic-gate 	printf("	return printname[n-257];\n");
1647c478bd9Sstevel@tonic-gate 	printf("}\n");
1657c478bd9Sstevel@tonic-gate 	exit(0);
1667c478bd9Sstevel@tonic-gate }
167