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