xref: /titanic_41/usr/src/cmd/oawk/makeprctab.c (revision 4a6822d07d6d3f9ffe6907ef5f10d11dcadd75c6)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1991 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include "awk.h"
33 /* tmaino #define NULL 0 */
34 #define	XNULL	"(null)"
35 
36 
37 struct xx
38 {	int token;
39 	char *name;
40 	char *pname;
41 } proc[] = {
42 	{ PROGRAM, "program", XNULL},
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", XNULL},
53 	{ INDIRECT, "indirect", "$("},
54 	{ SUBSTR, "substr", "substr"},
55 	{ INDEX, "sindex", "sindex"},
56 	{ SPRINTF, "asprintf", "sprintf "},
57 	{ ADD, "arith", " + "},
58 	{ MINUS, "arith", " - "},
59 	{ MULT, "arith", " * "},
60 	{ DIVIDE, "arith", " / "},
61 	{ MOD, "arith", " % "},
62 	{ UMINUS, "arith", " -"},
63 	{ PREINCR, "incrdecr", "++"},
64 	{ POSTINCR, "incrdecr", "++"},
65 	{ PREDECR, "incrdecr", "--"},
66 	{ POSTDECR, "incrdecr", "--"},
67 	{ CAT, "cat", " "},
68 	{ PASTAT, "pastat", XNULL},
69 	{ PASTAT2, "dopa2", XNULL},
70 	{ MATCH, "matchop", " ~ "},
71 	{ NOTMATCH, "matchop", " !~ "},
72 	{ PRINTF, "aprintf", "printf"},
73 	{ PRINT, "print", "print"},
74 	{ SPLIT, "split", "split"},
75 	{ ASSIGN, "assign", " = "},
76 	{ ADDEQ, "assign", " += "},
77 	{ SUBEQ, "assign", " -= "},
78 	{ MULTEQ, "assign", " *= "},
79 	{ DIVEQ, "assign", " /= "},
80 	{ MODEQ, "assign", " %= "},
81 	{ IF, "ifstat", "if("},
82 	{ WHILE, "whilestat", "while("},
83 	{ FOR, "forstat", "for("},
84 	{ IN, "instat", "instat"},
85 	{ NEXT, "jump", "next"},
86 	{ EXIT, "jump", "exit"},
87 	{ BREAK, "jump", "break"},
88 	{ CONTINUE, "jump", "continue"},
89 	{ FNCN, "fncn", "fncn"},
90 	{ GETLINE, "getline", "getline"},
91 	{ 0, ""},
92 };
93 #define	SIZE	LASTTOKEN - FIRSTTOKEN
94 char *table[SIZE];
95 char *names[SIZE];
96 
97 int
98 main(void)
99 {
100 	struct xx *p;
101 	int i;
102 
103 
104 	printf("#include \"awk.def\"\n");
105 	printf("CELL *nullproc();\n");
106 	for (i = SIZE; --i >= 0; /* dummy */)
107 		names[i] = "";
108 	for (p = proc; p->token != 0; p++)
109 		if (p == proc || strcmp(p->name, (p-1)->name))
110 			printf("extern CELL *%s();\n", p->name);
111 	for (p = proc; p->token != 0; p++)
112 		table[p->token-FIRSTTOKEN] = p->name;
113 	printf("CELL *(*proctab[%d])() = {\n", SIZE);
114 	for (i = 0; i < SIZE; i++)
115 		if (table[i] == 0)
116 			printf("/*%s*/\tnullproc,\n", tokname(i+FIRSTTOKEN));
117 		else
118 		printf("/*%s*/\t%s,\n", tokname(i+FIRSTTOKEN), table[i]);
119 	printf("};\n");
120 	printf("char *printname[%d] = {\n", SIZE);
121 	for (p = proc; p->token != 0; p++)
122 		names[p->token-FIRSTTOKEN] = p->pname;
123 	for (i = 0; i < SIZE; i++)
124 		printf("/*%s*/\t\"%s\",\n", tokname(i+FIRSTTOKEN), names[i]);
125 	printf("};\n");
126 	return (0);
127 }
128