1 /****************************************************************
2 Copyright (C) Lucent Technologies 1997
3 All Rights Reserved
4 
5 Permission to use, copy, modify, and distribute this software and
6 its documentation for any purpose and without fee is hereby
7 granted, provided that the above copyright notice appear in all
8 copies and that both that the copyright notice and this
9 permission notice and warranty disclaimer appear in supporting
10 documentation, and that the name Lucent Technologies or any of
11 its entities not be used in advertising or publicity pertaining
12 to distribution of the software without specific, written prior
13 permission.
14 
15 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
18 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
20 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
22 THIS SOFTWARE.
23 ****************************************************************/
24 
25 /*
26  * this program makes the table to link function names
27  * and type indices that is used by execute() in run.c.
28  * it finds the indices in ytab.h, produced by yacc.
29  */
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include "awk.h"
35 #include "ytab.h"
36 
37 struct xx
38 {	int token;
39 	const char *name;
40 	const char *pname;
41 } proc[] = {
42 	{ PROGRAM, "program", NULL },
43 	{ BOR, "boolop", " || " },
44 	{ AND, "boolop", " && " },
45 	{ NOT, "boolop", " !" },
46 	{ NE, "relop", " != " },
47 	{ EQ, "relop", " == " },
48 	{ LE, "relop", " <= " },
49 	{ LT, "relop", " < " },
50 	{ GE, "relop", " >= " },
51 	{ GT, "relop", " > " },
52 	{ ARRAY, "array", NULL },
53 	{ INDIRECT, "indirect", "$(" },
54 	{ SUBSTR, "substr", "substr" },
55 	{ SSVSTR2STR, "owk_ssvstr2str", "ssvstr2str" },	/* ONGS Inc. */
56 	{ V2S, "owk_ssvstr2str", "v2s" },		/* ONGS Inc. */
57 	{ STR2SSVSTR, "owk_str2ssvstr", "str2ssvstr" },	/* ONGS Inc. */
58 	{ S2V, "owk_str2ssvstr", "s2v" },		/* ONGS Inc. */
59 	{ SUB, "sub", "sub" },
60 	{ GSUB, "gsub", "gsub" },
61 	{ INDEX, "sindex", "sindex" },
62 	{ SPRINTF, "awksprintf", "sprintf " },
63 	{ ADD, "arith", " + " },
64 	{ MINUS, "arith", " - " },
65 	{ MULT, "arith", " * " },
66 	{ DIVIDE, "arith", " / " },
67 	{ MOD, "arith", " % " },
68 	{ UMINUS, "arith", " -" },
69 	{ POWER, "arith", " **" },
70 	{ PREINCR, "incrdecr", "++" },
71 	{ POSTINCR, "incrdecr", "++" },
72 	{ PREDECR, "incrdecr", "--" },
73 	{ POSTDECR, "incrdecr", "--" },
74 	{ CAT, "cat", " " },
75 	{ PASTAT, "pastat", NULL },
76 	{ PASTAT2, "dopa2", NULL },
77 	{ MATCH, "matchop", " ~ " },
78 	{ NOTMATCH, "matchop", " !~ " },
79 	{ MATCHFCN, "matchop", "matchop" },
80 	{ INTEST, "intest", "intest" },
81 	{ PRINTF, "awkprintf", "printf" },
82 	{ PRINT, "printstat", "print" },
83 	{ CLOSE, "closefile", "closefile" },
84 	{ DELETE, "awkdelete", "awkdelete" },
85 	{ SPLIT, "split", "split" },
86 	{ ASSIGN, "assign", " = " },
87 	{ ADDEQ, "assign", " += " },
88 	{ SUBEQ, "assign", " -= " },
89 	{ MULTEQ, "assign", " *= " },
90 	{ DIVEQ, "assign", " /= " },
91 	{ MODEQ, "assign", " %= " },
92 	{ POWEQ, "assign", " ^= " },
93 	{ CONDEXPR, "condexpr", " ?: " },
94 	{ IF, "ifstat", "if(" },
95 	{ WHILE, "whilestat", "while(" },
96 	{ FOR, "forstat", "for(" },
97 	{ DO, "dostat", "do" },
98 	{ IN, "instat", "instat" },
99 	{ NEXT, "jump", "next" },
100 	{ NEXTFILE, "jump", "nextfile" },
101 	{ EXIT, "jump", "exit" },
102 	{ BREAK, "jump", "break" },
103 	{ CONTINUE, "jump", "continue" },
104 	{ RETURN, "jump", "ret" },
105 	{ BLTIN, "bltin", "bltin" },
106 	{ CALL, "call", "call" },
107 	{ ARG, "arg", "arg" },
108 	{ VARNF, "getnf", "NF" },
109 	{ GETLINE, "awkgetline", "getline" },
110 	{ 0, "", "" },
111 };
112 
113 #define SIZE	(LASTTOKEN - FIRSTTOKEN + 1)
114 const char *table[SIZE];
115 char *names[SIZE];
116 
main(int argc,char * argv[])117 int main(int argc, char *argv[])
118 {
119 	const struct xx *p;
120 	int i, n, tok;
121 	char c;
122 	FILE *fp;
123 	char buf[200], name[200], def[200];
124 
125 	printf("#include <stdio.h>\n");
126 	printf("#include \"awk.h\"\n");
127 	printf("#include \"ytab.h\"\n\n");
128 	for (i = SIZE; --i >= 0; )
129 		names[i] = "";
130 
131 	if ((fp = fopen("ytab.h", "r")) == NULL) {
132 		fprintf(stderr, "maketab can't open ytab.h!\n");
133 		exit(1);
134 	}
135 	printf("static char *printname[%d] = {\n", SIZE);
136 	i = 0;
137 	while (fgets(buf, sizeof buf, fp) != NULL) {
138 		n = sscanf(buf, "%1c %s %s %d", &c, def, name, &tok);
139 		if (c != '#' || (n != 4 && strcmp(def,"define") != 0))	/* not a valid #define */
140 			continue;
141 		if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
142 			/* fprintf(stderr, "maketab funny token %d %s ignored\n", tok, buf); */
143 			continue;
144 		}
145 		names[tok-FIRSTTOKEN] = (char *) malloc(strlen(name)+1);
146 		strcpy(names[tok-FIRSTTOKEN], name);
147 		printf("\t(char *) \"%s\",\t/* %d */\n", name, tok);
148 		i++;
149 	}
150 	printf("};\n\n");
151 
152 	for (p=proc; p->token!=0; p++)
153 		table[p->token-FIRSTTOKEN] = p->name;
154 	printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE);
155 	for (i=0; i<SIZE; i++)
156 		if (table[i]==NULL)
157 			printf("\tnullproc,\t/* %s */\n", names[i]);
158 		else
159 			printf("\t%s,\t/* %s */\n", table[i], names[i]);
160 	printf("};\n\n");
161 
162 	printf("char *tokname(int n)\n");	/* print a tokname() function */
163 	printf("{\n");
164 	printf("	static char buf[100];\n\n");
165 	printf("	if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
166 	printf("		sprintf(buf, \"token %%d\", n);\n");
167 	printf("		return buf;\n");
168 	printf("	}\n");
169 	printf("	return printname[n-FIRSTTOKEN];\n");
170 	printf("}\n");
171 	return 0;
172 }
173