185587Sobrien/****************************************************************
285587SobrienCopyright (C) Lucent Technologies 1997
385587SobrienAll Rights Reserved
485587Sobrien
585587SobrienPermission to use, copy, modify, and distribute this software and
685587Sobrienits documentation for any purpose and without fee is hereby
785587Sobriengranted, provided that the above copyright notice appear in all
885587Sobriencopies and that both that the copyright notice and this
985587Sobrienpermission notice and warranty disclaimer appear in supporting
1085587Sobriendocumentation, and that the name Lucent Technologies or any of
1185587Sobrienits entities not be used in advertising or publicity pertaining
1285587Sobriento distribution of the software without specific, written prior
1385587Sobrienpermission.
1485587Sobrien
1585587SobrienLUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
1685587SobrienINCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
1785587SobrienIN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
1885587SobrienSPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1985587SobrienWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
2085587SobrienIN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
2185587SobrienARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
2285587SobrienTHIS SOFTWARE.
2385587Sobrien****************************************************************/
2485587Sobrien
2585587Sobrien/*
2685587Sobrien * this program makes the table to link function names
2785587Sobrien * and type indices that is used by execute() in run.c.
2885587Sobrien * it finds the indices in ytab.h, produced by yacc.
2985587Sobrien */
3085587Sobrien
3185587Sobrien#include <stdio.h>
3285587Sobrien#include <string.h>
3385587Sobrien#include <stdlib.h>
3485587Sobrien#include "awk.h"
3585587Sobrien#include "ytab.h"
3685587Sobrien
3785587Sobrienstruct xx
3885587Sobrien{	int token;
39107806Sobrien	const char *name;
40107806Sobrien	const char *pname;
4185587Sobrien} proc[] = {
4285587Sobrien	{ PROGRAM, "program", NULL },
4385587Sobrien	{ BOR, "boolop", " || " },
4485587Sobrien	{ AND, "boolop", " && " },
4585587Sobrien	{ NOT, "boolop", " !" },
4685587Sobrien	{ NE, "relop", " != " },
4785587Sobrien	{ EQ, "relop", " == " },
4885587Sobrien	{ LE, "relop", " <= " },
4985587Sobrien	{ LT, "relop", " < " },
5085587Sobrien	{ GE, "relop", " >= " },
5185587Sobrien	{ GT, "relop", " > " },
5285587Sobrien	{ ARRAY, "array", NULL },
5385587Sobrien	{ INDIRECT, "indirect", "$(" },
5485587Sobrien	{ SUBSTR, "substr", "substr" },
5585587Sobrien	{ SUB, "sub", "sub" },
5685587Sobrien	{ GSUB, "gsub", "gsub" },
5785587Sobrien	{ INDEX, "sindex", "sindex" },
5885587Sobrien	{ SPRINTF, "awksprintf", "sprintf " },
5985587Sobrien	{ ADD, "arith", " + " },
6085587Sobrien	{ MINUS, "arith", " - " },
6185587Sobrien	{ MULT, "arith", " * " },
6285587Sobrien	{ DIVIDE, "arith", " / " },
6385587Sobrien	{ MOD, "arith", " % " },
6485587Sobrien	{ UMINUS, "arith", " -" },
6585587Sobrien	{ POWER, "arith", " **" },
6685587Sobrien	{ PREINCR, "incrdecr", "++" },
6785587Sobrien	{ POSTINCR, "incrdecr", "++" },
6885587Sobrien	{ PREDECR, "incrdecr", "--" },
6985587Sobrien	{ POSTDECR, "incrdecr", "--" },
7085587Sobrien	{ CAT, "cat", " " },
7185587Sobrien	{ PASTAT, "pastat", NULL },
7285587Sobrien	{ PASTAT2, "dopa2", NULL },
7385587Sobrien	{ MATCH, "matchop", " ~ " },
7485587Sobrien	{ NOTMATCH, "matchop", " !~ " },
7585587Sobrien	{ MATCHFCN, "matchop", "matchop" },
7685587Sobrien	{ INTEST, "intest", "intest" },
7785587Sobrien	{ PRINTF, "awkprintf", "printf" },
7885587Sobrien	{ PRINT, "printstat", "print" },
7985587Sobrien	{ CLOSE, "closefile", "closefile" },
8085587Sobrien	{ DELETE, "awkdelete", "awkdelete" },
8185587Sobrien	{ SPLIT, "split", "split" },
8285587Sobrien	{ ASSIGN, "assign", " = " },
8385587Sobrien	{ ADDEQ, "assign", " += " },
8485587Sobrien	{ SUBEQ, "assign", " -= " },
8585587Sobrien	{ MULTEQ, "assign", " *= " },
8685587Sobrien	{ DIVEQ, "assign", " /= " },
8785587Sobrien	{ MODEQ, "assign", " %= " },
8885587Sobrien	{ POWEQ, "assign", " ^= " },
8985587Sobrien	{ CONDEXPR, "condexpr", " ?: " },
9085587Sobrien	{ IF, "ifstat", "if(" },
9185587Sobrien	{ WHILE, "whilestat", "while(" },
9285587Sobrien	{ FOR, "forstat", "for(" },
9385587Sobrien	{ DO, "dostat", "do" },
9485587Sobrien	{ IN, "instat", "instat" },
9585587Sobrien	{ NEXT, "jump", "next" },
9685587Sobrien	{ NEXTFILE, "jump", "nextfile" },
9785587Sobrien	{ EXIT, "jump", "exit" },
9885587Sobrien	{ BREAK, "jump", "break" },
9985587Sobrien	{ CONTINUE, "jump", "continue" },
10085587Sobrien	{ RETURN, "jump", "ret" },
10185587Sobrien	{ BLTIN, "bltin", "bltin" },
10285587Sobrien	{ CALL, "call", "call" },
10385587Sobrien	{ ARG, "arg", "arg" },
10485587Sobrien	{ VARNF, "getnf", "NF" },
105201951Sru	{ GETLINE, "awkgetline", "getline" },
10685587Sobrien	{ 0, "", "" },
10785587Sobrien};
10885587Sobrien
10985587Sobrien#define SIZE	(LASTTOKEN - FIRSTTOKEN + 1)
110107806Sobrienconst char *table[SIZE];
11185587Sobrienchar *names[SIZE];
11285587Sobrien
11385587Sobrienint main(int argc, char *argv[])
11485587Sobrien{
115107806Sobrien	const struct xx *p;
11685587Sobrien	int i, n, tok;
11785587Sobrien	char c;
11885587Sobrien	FILE *fp;
11985587Sobrien	char buf[200], name[200], def[200];
12085587Sobrien
12185587Sobrien	printf("#include <stdio.h>\n");
12285587Sobrien	printf("#include \"awk.h\"\n");
12385587Sobrien	printf("#include \"ytab.h\"\n\n");
12485587Sobrien	for (i = SIZE; --i >= 0; )
12585587Sobrien		names[i] = "";
12685587Sobrien
12785587Sobrien	if ((fp = fopen("ytab.h", "r")) == NULL) {
12885587Sobrien		fprintf(stderr, "maketab can't open ytab.h!\n");
12985587Sobrien		exit(1);
13085587Sobrien	}
13185587Sobrien	printf("static char *printname[%d] = {\n", SIZE);
13285587Sobrien	i = 0;
13385587Sobrien	while (fgets(buf, sizeof buf, fp) != NULL) {
13485587Sobrien		n = sscanf(buf, "%1c %s %s %d", &c, def, name, &tok);
13585587Sobrien		if (c != '#' || (n != 4 && strcmp(def,"define") != 0))	/* not a valid #define */
13685587Sobrien			continue;
13785587Sobrien		if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
138170331Srafan			/* fprintf(stderr, "maketab funny token %d %s ignored\n", tok, buf); */
13985587Sobrien			continue;
14085587Sobrien		}
14185587Sobrien		names[tok-FIRSTTOKEN] = (char *) malloc(strlen(name)+1);
14285587Sobrien		strcpy(names[tok-FIRSTTOKEN], name);
14385587Sobrien		printf("\t(char *) \"%s\",\t/* %d */\n", name, tok);
14485587Sobrien		i++;
14585587Sobrien	}
14685587Sobrien	printf("};\n\n");
14785587Sobrien
14885587Sobrien	for (p=proc; p->token!=0; p++)
14985587Sobrien		table[p->token-FIRSTTOKEN] = p->name;
15085587Sobrien	printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE);
15185587Sobrien	for (i=0; i<SIZE; i++)
15285587Sobrien		if (table[i]==0)
15385587Sobrien			printf("\tnullproc,\t/* %s */\n", names[i]);
15485587Sobrien		else
15585587Sobrien			printf("\t%s,\t/* %s */\n", table[i], names[i]);
15685587Sobrien	printf("};\n\n");
15785587Sobrien
15885587Sobrien	printf("char *tokname(int n)\n");	/* print a tokname() function */
15985587Sobrien	printf("{\n");
16085587Sobrien	printf("	static char buf[100];\n\n");
16185587Sobrien	printf("	if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
16285587Sobrien	printf("		sprintf(buf, \"token %%d\", n);\n");
16385587Sobrien	printf("		return buf;\n");
16485587Sobrien	printf("	}\n");
16585587Sobrien	printf("	return printname[n-FIRSTTOKEN];\n");
16685587Sobrien	printf("}\n");
16785587Sobrien	return 0;
16885587Sobrien}
169