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 2002 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 31 #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 2.5 */ 32 33 #include <stdio.h> 34 #include <string.h> 35 #include <stdlib.h> 36 #include <libintl.h> 37 #include "awk.h" 38 #include "y.tab.h" 39 40 struct xx { 41 int token; 42 char *name; 43 char *pname; 44 } proc[] = { 45 { PROGRAM, "program", NULL }, 46 { BOR, "boolop", " || " }, 47 { AND, "boolop", " && " }, 48 { NOT, "boolop", " !" }, 49 { NE, "relop", " != " }, 50 { EQ, "relop", " == " }, 51 { LE, "relop", " <= " }, 52 { LT, "relop", " < " }, 53 { GE, "relop", " >= " }, 54 { GT, "relop", " > " }, 55 { ARRAY, "array", NULL }, 56 { INDIRECT, "indirect", "$(" }, 57 { SUBSTR, "substr", "substr" }, 58 { SUB, "sub", "sub" }, 59 { GSUB, "gsub", "gsub" }, 60 { INDEX, "sindex", "sindex" }, 61 { SPRINTF, "asprintf", "sprintf " }, 62 { ADD, "arith", " + " }, 63 { MINUS, "arith", " - " }, 64 { MULT, "arith", " * " }, 65 { DIVIDE, "arith", " / " }, 66 { MOD, "arith", " % " }, 67 { UMINUS, "arith", " -" }, 68 { POWER, "arith", " **" }, 69 { PREINCR, "incrdecr", "++" }, 70 { POSTINCR, "incrdecr", "++" }, 71 { PREDECR, "incrdecr", "--" }, 72 { POSTDECR, "incrdecr", "--" }, 73 { CAT, "cat", " " }, 74 { PASTAT, "pastat", NULL }, 75 { PASTAT2, "dopa2", NULL }, 76 { MATCH, "matchop", " ~ " }, 77 { NOTMATCH, "matchop", " !~ " }, 78 { MATCHFCN, "matchop", "matchop" }, 79 { INTEST, "intest", "intest" }, 80 { PRINTF, "aprintf", "printf" }, 81 { PRINT, "print", "print" }, 82 { CLOSE, "closefile", "closefile" }, 83 { DELETE, "delete", "delete" }, 84 { SPLIT, "split", "split" }, 85 { ASSIGN, "assign", " = " }, 86 { ADDEQ, "assign", " += " }, 87 { SUBEQ, "assign", " -= " }, 88 { MULTEQ, "assign", " *= " }, 89 { DIVEQ, "assign", " /= " }, 90 { MODEQ, "assign", " %= " }, 91 { POWEQ, "assign", " ^= " }, 92 { CONDEXPR, "condexpr", " ?: " }, 93 { IF, "ifstat", "if(" }, 94 { WHILE, "whilestat", "while(" }, 95 { FOR, "forstat", "for(" }, 96 { DO, "dostat", "do" }, 97 { IN, "instat", "instat" }, 98 { NEXT, "jump", "next" }, 99 { EXIT, "jump", "exit" }, 100 { BREAK, "jump", "break" }, 101 { CONTINUE, "jump", "continue" }, 102 { RETURN, "jump", "ret" }, 103 { BLTIN, "bltin", "bltin" }, 104 { CALL, "call", "call" }, 105 { ARG, "arg", "arg" }, 106 { VARNF, "getnf", "NF" }, 107 { GETLINE, "getline", "getline" }, 108 { 0, "", "" }, 109 }; 110 111 #define SIZE LASTTOKEN - FIRSTTOKEN + 1 112 char *table[SIZE]; 113 char *names[SIZE]; 114 115 int 116 main() 117 { 118 struct xx *p; 119 int i, n, tok; 120 char c; 121 FILE *fp; 122 char buf[100], name[100], def[100]; 123 124 printf("#include \"awk.h\"\n"); 125 printf("#include \"y.tab.h\"\n\n"); 126 printf("Cell *nullproc();\n"); 127 for (i = SIZE; --i >= 0; ) 128 names[i] = ""; 129 for (p = proc; p->token != 0; p++) 130 if (p == proc || strcmp(p->name, (p-1)->name)) 131 printf("extern Cell *%s();\n", p->name); 132 133 if ((fp = fopen("y.tab.h", "r")) == NULL) { 134 fprintf(stderr, gettext("maketab can't open y.tab.h!\n")); 135 exit(1); 136 } 137 printf("static uchar *printname[%d] = {\n", SIZE); 138 i = 0; 139 while (fgets(buf, sizeof (buf), fp) != NULL) { 140 n = sscanf(buf, "%1c %s %s %d", &c, def, name, &tok); 141 /* not a valid #define? */ 142 if (c != '#' || n != 4 && strcmp(def, "define") != 0) 143 continue; 144 if (tok < FIRSTTOKEN || tok > LASTTOKEN) { 145 fprintf(stderr, gettext("maketab funny token %d %s\n"), 146 tok, buf); 147 exit(1); 148 } 149 names[tok-FIRSTTOKEN] = malloc(strlen(name)+1); 150 strcpy(names[tok-FIRSTTOKEN], name); 151 printf("\t(uchar *) \"%s\",\t/* %d */\n", name, tok); 152 i++; 153 } 154 printf("};\n\n"); 155 156 for (p = proc; p->token != 0; p++) 157 table[p->token-FIRSTTOKEN] = p->name; 158 printf("\nCell *(*proctab[%d])() = {\n", SIZE); 159 for (i = 0; i < SIZE; i++) 160 if (table[i] == 0) 161 printf("\tnullproc,\t/* %s */\n", names[i]); 162 else 163 printf("\t%s,\t/* %s */\n", table[i], names[i]); 164 printf("};\n\n"); 165 166 printf("uchar *tokname(n)\n"); /* print a tokname() function */ 167 printf("{\n"); 168 printf(" static uchar buf[100];\n\n"); 169 printf(" if (n < FIRSTTOKEN || n > LASTTOKEN) {\n"); 170 printf(" sprintf(buf, \"token %%d\", n);\n"); 171 printf(" return buf;\n"); 172 printf(" }\n"); 173 printf(" return printname[n-257];\n"); 174 printf("}\n"); 175 exit(0); 176 } 177