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