xref: /titanic_51/usr/src/cmd/oawk/makeprctab.c (revision 23a1ccea6aac035f084a7a4cdc968687d1b02daf)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
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 "awk.h"
30 /* tmaino #define NULL 0 */
31 #define	XNULL	"(null)"
32 
33 
34 struct xx
35 {	int token;
36 	char *name;
37 	char *pname;
38 } proc[] = {
39 	{ PROGRAM, "program", XNULL},
40 	{ BOR, "boolop", " || "},
41 	{ AND, "boolop", " && "},
42 	{ NOT, "boolop", " !"},
43 	{ NE, "relop", " != "},
44 	{ EQ, "relop", " == "},
45 	{ LE, "relop", " <= "},
46 	{ LT, "relop", " < "},
47 	{ GE, "relop", " >= "},
48 	{ GT, "relop", " > "},
49 	{ ARRAY, "array", XNULL},
50 	{ INDIRECT, "indirect", "$("},
51 	{ SUBSTR, "substr", "substr"},
52 	{ INDEX, "sindex", "sindex"},
53 	{ SPRINTF, "a_sprintf", "sprintf "},
54 	{ ADD, "arith", " + "},
55 	{ MINUS, "arith", " - "},
56 	{ MULT, "arith", " * "},
57 	{ DIVIDE, "arith", " / "},
58 	{ MOD, "arith", " % "},
59 	{ UMINUS, "arith", " -"},
60 	{ PREINCR, "incrdecr", "++"},
61 	{ POSTINCR, "incrdecr", "++"},
62 	{ PREDECR, "incrdecr", "--"},
63 	{ POSTDECR, "incrdecr", "--"},
64 	{ CAT, "cat", " "},
65 	{ PASTAT, "pastat", XNULL},
66 	{ PASTAT2, "dopa2", XNULL},
67 	{ MATCH, "matchop", " ~ "},
68 	{ NOTMATCH, "matchop", " !~ "},
69 	{ PRINTF, "aprintf", "printf"},
70 	{ PRINT, "print", "print"},
71 	{ SPLIT, "split", "split"},
72 	{ ASSIGN, "assign", " = "},
73 	{ ADDEQ, "assign", " += "},
74 	{ SUBEQ, "assign", " -= "},
75 	{ MULTEQ, "assign", " *= "},
76 	{ DIVEQ, "assign", " /= "},
77 	{ MODEQ, "assign", " %= "},
78 	{ IF, "ifstat", "if("},
79 	{ WHILE, "whilestat", "while("},
80 	{ FOR, "forstat", "for("},
81 	{ IN, "instat", "instat"},
82 	{ NEXT, "jump", "next"},
83 	{ EXIT, "jump", "exit"},
84 	{ BREAK, "jump", "break"},
85 	{ CONTINUE, "jump", "continue"},
86 	{ FNCN, "fncn", "fncn"},
87 	{ GETLINE, "getaline", "getline"},
88 	{ 0, ""},
89 };
90 #define	SIZE	LASTTOKEN - FIRSTTOKEN
91 char *table[SIZE];
92 char *names[SIZE];
93 
94 int
95 main(void)
96 {
97 	struct xx *p;
98 	int i;
99 
100 
101 	printf("#include \"awk.def\"\n");
102 	printf("CELL *nullproc();\n");
103 	for (i = SIZE; --i >= 0; /* dummy */)
104 		names[i] = "";
105 	for (p = proc; p->token != 0; p++)
106 		if (p == proc || strcmp(p->name, (p-1)->name))
107 			printf("extern CELL *%s();\n", p->name);
108 	for (p = proc; p->token != 0; p++)
109 		table[p->token-FIRSTTOKEN] = p->name;
110 	printf("CELL *(*proctab[%d])() = {\n", SIZE);
111 	for (i = 0; i < SIZE; i++)
112 		if (table[i] == 0)
113 			printf("/*%s*/\tnullproc,\n", tokname(i+FIRSTTOKEN));
114 		else
115 		printf("/*%s*/\t%s,\n", tokname(i+FIRSTTOKEN), table[i]);
116 	printf("};\n");
117 	printf("char *printname[%d] = {\n", SIZE);
118 	for (p = proc; p->token != 0; p++)
119 		names[p->token-FIRSTTOKEN] = p->pname;
120 	for (i = 0; i < SIZE; i++)
121 		printf("/*%s*/\t\"%s\",\n", tokname(i+FIRSTTOKEN), names[i]);
122 	printf("};\n");
123 	return (0);
124 }
125