xref: /onnv-gate/usr/src/cmd/awk/maketab.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
58546SRoger.Faulkner@Sun.COM  * Common Development and Distribution License (the "License").
68546SRoger.Faulkner@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
218546SRoger.Faulkner@Sun.COM 
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <libintl.h>
330Sstevel@tonic-gate #include "awk.h"
340Sstevel@tonic-gate #include "y.tab.h"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate struct xx {
370Sstevel@tonic-gate 	int token;
380Sstevel@tonic-gate 	char *name;
390Sstevel@tonic-gate 	char *pname;
400Sstevel@tonic-gate } proc[] = {
410Sstevel@tonic-gate 	{ PROGRAM, "program", NULL },
420Sstevel@tonic-gate 	{ BOR, "boolop", " || " },
430Sstevel@tonic-gate 	{ AND, "boolop", " && " },
440Sstevel@tonic-gate 	{ NOT, "boolop", " !" },
450Sstevel@tonic-gate 	{ NE, "relop", " != " },
460Sstevel@tonic-gate 	{ EQ, "relop", " == " },
470Sstevel@tonic-gate 	{ LE, "relop", " <= " },
480Sstevel@tonic-gate 	{ LT, "relop", " < " },
490Sstevel@tonic-gate 	{ GE, "relop", " >= " },
500Sstevel@tonic-gate 	{ GT, "relop", " > " },
510Sstevel@tonic-gate 	{ ARRAY, "array", NULL },
520Sstevel@tonic-gate 	{ INDIRECT, "indirect", "$(" },
530Sstevel@tonic-gate 	{ SUBSTR, "substr", "substr" },
540Sstevel@tonic-gate 	{ SUB, "sub", "sub" },
550Sstevel@tonic-gate 	{ GSUB, "gsub", "gsub" },
560Sstevel@tonic-gate 	{ INDEX, "sindex", "sindex" },
578546SRoger.Faulkner@Sun.COM 	{ SPRINTF, "a_sprintf", "sprintf " },
580Sstevel@tonic-gate 	{ ADD, "arith", " + " },
590Sstevel@tonic-gate 	{ MINUS, "arith", " - " },
600Sstevel@tonic-gate 	{ MULT, "arith", " * " },
610Sstevel@tonic-gate 	{ DIVIDE, "arith", " / " },
620Sstevel@tonic-gate 	{ MOD, "arith", " % " },
630Sstevel@tonic-gate 	{ UMINUS, "arith", " -" },
640Sstevel@tonic-gate 	{ POWER, "arith", " **" },
650Sstevel@tonic-gate 	{ PREINCR, "incrdecr", "++" },
660Sstevel@tonic-gate 	{ POSTINCR, "incrdecr", "++" },
670Sstevel@tonic-gate 	{ PREDECR, "incrdecr", "--" },
680Sstevel@tonic-gate 	{ POSTDECR, "incrdecr", "--" },
690Sstevel@tonic-gate 	{ CAT, "cat", " " },
700Sstevel@tonic-gate 	{ PASTAT, "pastat", NULL },
710Sstevel@tonic-gate 	{ PASTAT2, "dopa2", NULL },
720Sstevel@tonic-gate 	{ MATCH, "matchop", " ~ " },
730Sstevel@tonic-gate 	{ NOTMATCH, "matchop", " !~ " },
740Sstevel@tonic-gate 	{ MATCHFCN, "matchop", "matchop" },
750Sstevel@tonic-gate 	{ INTEST, "intest", "intest" },
760Sstevel@tonic-gate 	{ PRINTF, "aprintf", "printf" },
770Sstevel@tonic-gate 	{ PRINT, "print", "print" },
780Sstevel@tonic-gate 	{ CLOSE, "closefile", "closefile" },
790Sstevel@tonic-gate 	{ DELETE, "delete", "delete" },
800Sstevel@tonic-gate 	{ SPLIT, "split", "split" },
810Sstevel@tonic-gate 	{ ASSIGN, "assign", " = " },
820Sstevel@tonic-gate 	{ ADDEQ, "assign", " += " },
830Sstevel@tonic-gate 	{ SUBEQ, "assign", " -= " },
840Sstevel@tonic-gate 	{ MULTEQ, "assign", " *= " },
850Sstevel@tonic-gate 	{ DIVEQ, "assign", " /= " },
860Sstevel@tonic-gate 	{ MODEQ, "assign", " %= " },
870Sstevel@tonic-gate 	{ POWEQ, "assign", " ^= " },
880Sstevel@tonic-gate 	{ CONDEXPR, "condexpr", " ?: " },
890Sstevel@tonic-gate 	{ IF, "ifstat", "if(" },
900Sstevel@tonic-gate 	{ WHILE, "whilestat", "while(" },
910Sstevel@tonic-gate 	{ FOR, "forstat", "for(" },
920Sstevel@tonic-gate 	{ DO, "dostat", "do" },
930Sstevel@tonic-gate 	{ IN, "instat", "instat" },
940Sstevel@tonic-gate 	{ NEXT, "jump", "next" },
950Sstevel@tonic-gate 	{ EXIT, "jump", "exit" },
960Sstevel@tonic-gate 	{ BREAK, "jump", "break" },
970Sstevel@tonic-gate 	{ CONTINUE, "jump", "continue" },
980Sstevel@tonic-gate 	{ RETURN, "jump", "ret" },
990Sstevel@tonic-gate 	{ BLTIN, "bltin", "bltin" },
1000Sstevel@tonic-gate 	{ CALL, "call", "call" },
1010Sstevel@tonic-gate 	{ ARG, "arg", "arg" },
1020Sstevel@tonic-gate 	{ VARNF, "getnf", "NF" },
103*13093SRoger.Faulkner@Oracle.COM 	{ GETLINE, "getaline", "getline" },
1040Sstevel@tonic-gate 	{ 0, "", "" },
1050Sstevel@tonic-gate };
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate #define	SIZE	LASTTOKEN - FIRSTTOKEN + 1
1080Sstevel@tonic-gate char *table[SIZE];
1090Sstevel@tonic-gate char *names[SIZE];
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate int
main()1120Sstevel@tonic-gate main()
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	struct xx *p;
1150Sstevel@tonic-gate 	int i, n, tok;
1160Sstevel@tonic-gate 	char c;
1170Sstevel@tonic-gate 	FILE *fp;
1180Sstevel@tonic-gate 	char buf[100], name[100], def[100];
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	printf("#include \"awk.h\"\n");
1210Sstevel@tonic-gate 	printf("#include \"y.tab.h\"\n\n");
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	if ((fp = fopen("y.tab.h", "r")) == NULL) {
1240Sstevel@tonic-gate 		fprintf(stderr, gettext("maketab can't open y.tab.h!\n"));
1250Sstevel@tonic-gate 		exit(1);
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 	printf("static uchar *printname[%d] = {\n", SIZE);
1280Sstevel@tonic-gate 	i = 0;
1290Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
1300Sstevel@tonic-gate 		n = sscanf(buf, "%1c %s %s %d", &c, def, name, &tok);
1310Sstevel@tonic-gate 		/* not a valid #define? */
1320Sstevel@tonic-gate 		if (c != '#' || n != 4 && strcmp(def, "define") != 0)
1330Sstevel@tonic-gate 			continue;
1340Sstevel@tonic-gate 		if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
1350Sstevel@tonic-gate 			fprintf(stderr, gettext("maketab funny token %d %s\n"),
1360Sstevel@tonic-gate 			    tok, buf);
1370Sstevel@tonic-gate 			exit(1);
1380Sstevel@tonic-gate 		}
1390Sstevel@tonic-gate 		names[tok-FIRSTTOKEN] = malloc(strlen(name)+1);
1400Sstevel@tonic-gate 		strcpy(names[tok-FIRSTTOKEN], name);
1410Sstevel@tonic-gate 		printf("\t(uchar *) \"%s\",\t/* %d */\n", name, tok);
1420Sstevel@tonic-gate 		i++;
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 	printf("};\n\n");
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	for (p = proc; p->token != 0; p++)
1470Sstevel@tonic-gate 		table[p->token-FIRSTTOKEN] = p->name;
1480Sstevel@tonic-gate 	printf("\nCell *(*proctab[%d])() = {\n", SIZE);
1490Sstevel@tonic-gate 	for (i = 0; i < SIZE; i++)
1500Sstevel@tonic-gate 		if (table[i] == 0)
1510Sstevel@tonic-gate 			printf("\tnullproc,\t/* %s */\n", names[i]);
1520Sstevel@tonic-gate 		else
1530Sstevel@tonic-gate 			printf("\t%s,\t/* %s */\n", table[i], names[i]);
1540Sstevel@tonic-gate 	printf("};\n\n");
1550Sstevel@tonic-gate 
156289Snakanon 	printf("uchar *\ntokname(int n)\n");	/* print a tokname() function */
1570Sstevel@tonic-gate 	printf("{\n");
158289Snakanon 	printf("	static char buf[100];\n\n");
1590Sstevel@tonic-gate 	printf("	if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
160289Snakanon 	printf("		(void) sprintf(buf, \"token %%d\", n);\n");
161289Snakanon 	printf("		return ((uchar *)buf);\n");
1620Sstevel@tonic-gate 	printf("	}\n");
1630Sstevel@tonic-gate 	printf("	return printname[n-257];\n");
1640Sstevel@tonic-gate 	printf("}\n");
1650Sstevel@tonic-gate 	exit(0);
1660Sstevel@tonic-gate }
167