17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*d0983205SRoger A. Faulkner * Common Development and Distribution License (the "License"). 6*d0983205SRoger A. Faulkner * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21*d0983205SRoger A. Faulkner 227c478bd9Sstevel@tonic-gate /* 23*d0983205SRoger A. Faulkner * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #include <stdio.h> 317c478bd9Sstevel@tonic-gate #include <string.h> 327c478bd9Sstevel@tonic-gate #include <stdlib.h> 337c478bd9Sstevel@tonic-gate #include <libintl.h> 347c478bd9Sstevel@tonic-gate #include "awk.h" 357c478bd9Sstevel@tonic-gate #include "y.tab.h" 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate struct xx { 387c478bd9Sstevel@tonic-gate int token; 397c478bd9Sstevel@tonic-gate char *name; 407c478bd9Sstevel@tonic-gate char *pname; 417c478bd9Sstevel@tonic-gate } proc[] = { 427c478bd9Sstevel@tonic-gate { PROGRAM, "program", NULL }, 437c478bd9Sstevel@tonic-gate { BOR, "boolop", " || " }, 447c478bd9Sstevel@tonic-gate { AND, "boolop", " && " }, 457c478bd9Sstevel@tonic-gate { NOT, "boolop", " !" }, 467c478bd9Sstevel@tonic-gate { NE, "relop", " != " }, 477c478bd9Sstevel@tonic-gate { EQ, "relop", " == " }, 487c478bd9Sstevel@tonic-gate { LE, "relop", " <= " }, 497c478bd9Sstevel@tonic-gate { LT, "relop", " < " }, 507c478bd9Sstevel@tonic-gate { GE, "relop", " >= " }, 517c478bd9Sstevel@tonic-gate { GT, "relop", " > " }, 527c478bd9Sstevel@tonic-gate { ARRAY, "array", NULL }, 537c478bd9Sstevel@tonic-gate { INDIRECT, "indirect", "$(" }, 547c478bd9Sstevel@tonic-gate { SUBSTR, "substr", "substr" }, 557c478bd9Sstevel@tonic-gate { SUB, "sub", "sub" }, 567c478bd9Sstevel@tonic-gate { GSUB, "gsub", "gsub" }, 577c478bd9Sstevel@tonic-gate { INDEX, "sindex", "sindex" }, 58*d0983205SRoger A. Faulkner { SPRINTF, "a_sprintf", "sprintf " }, 597c478bd9Sstevel@tonic-gate { ADD, "arith", " + " }, 607c478bd9Sstevel@tonic-gate { MINUS, "arith", " - " }, 617c478bd9Sstevel@tonic-gate { MULT, "arith", " * " }, 627c478bd9Sstevel@tonic-gate { DIVIDE, "arith", " / " }, 637c478bd9Sstevel@tonic-gate { MOD, "arith", " % " }, 647c478bd9Sstevel@tonic-gate { UMINUS, "arith", " -" }, 657c478bd9Sstevel@tonic-gate { POWER, "arith", " **" }, 667c478bd9Sstevel@tonic-gate { PREINCR, "incrdecr", "++" }, 677c478bd9Sstevel@tonic-gate { POSTINCR, "incrdecr", "++" }, 687c478bd9Sstevel@tonic-gate { PREDECR, "incrdecr", "--" }, 697c478bd9Sstevel@tonic-gate { POSTDECR, "incrdecr", "--" }, 707c478bd9Sstevel@tonic-gate { CAT, "cat", " " }, 717c478bd9Sstevel@tonic-gate { PASTAT, "pastat", NULL }, 727c478bd9Sstevel@tonic-gate { PASTAT2, "dopa2", NULL }, 737c478bd9Sstevel@tonic-gate { MATCH, "matchop", " ~ " }, 747c478bd9Sstevel@tonic-gate { NOTMATCH, "matchop", " !~ " }, 757c478bd9Sstevel@tonic-gate { MATCHFCN, "matchop", "matchop" }, 767c478bd9Sstevel@tonic-gate { INTEST, "intest", "intest" }, 777c478bd9Sstevel@tonic-gate { PRINTF, "aprintf", "printf" }, 787c478bd9Sstevel@tonic-gate { PRINT, "print", "print" }, 797c478bd9Sstevel@tonic-gate { CLOSE, "closefile", "closefile" }, 807c478bd9Sstevel@tonic-gate { DELETE, "delete", "delete" }, 817c478bd9Sstevel@tonic-gate { SPLIT, "split", "split" }, 827c478bd9Sstevel@tonic-gate { ASSIGN, "assign", " = " }, 837c478bd9Sstevel@tonic-gate { ADDEQ, "assign", " += " }, 847c478bd9Sstevel@tonic-gate { SUBEQ, "assign", " -= " }, 857c478bd9Sstevel@tonic-gate { MULTEQ, "assign", " *= " }, 867c478bd9Sstevel@tonic-gate { DIVEQ, "assign", " /= " }, 877c478bd9Sstevel@tonic-gate { MODEQ, "assign", " %= " }, 887c478bd9Sstevel@tonic-gate { POWEQ, "assign", " ^= " }, 897c478bd9Sstevel@tonic-gate { CONDEXPR, "condexpr", " ?: " }, 907c478bd9Sstevel@tonic-gate { IF, "ifstat", "if(" }, 917c478bd9Sstevel@tonic-gate { WHILE, "whilestat", "while(" }, 927c478bd9Sstevel@tonic-gate { FOR, "forstat", "for(" }, 937c478bd9Sstevel@tonic-gate { DO, "dostat", "do" }, 947c478bd9Sstevel@tonic-gate { IN, "instat", "instat" }, 957c478bd9Sstevel@tonic-gate { NEXT, "jump", "next" }, 967c478bd9Sstevel@tonic-gate { EXIT, "jump", "exit" }, 977c478bd9Sstevel@tonic-gate { BREAK, "jump", "break" }, 987c478bd9Sstevel@tonic-gate { CONTINUE, "jump", "continue" }, 997c478bd9Sstevel@tonic-gate { RETURN, "jump", "ret" }, 1007c478bd9Sstevel@tonic-gate { BLTIN, "bltin", "bltin" }, 1017c478bd9Sstevel@tonic-gate { CALL, "call", "call" }, 1027c478bd9Sstevel@tonic-gate { ARG, "arg", "arg" }, 1037c478bd9Sstevel@tonic-gate { VARNF, "getnf", "NF" }, 1047c478bd9Sstevel@tonic-gate { GETLINE, "getline", "getline" }, 1057c478bd9Sstevel@tonic-gate { 0, "", "" }, 1067c478bd9Sstevel@tonic-gate }; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate #define SIZE LASTTOKEN - FIRSTTOKEN + 1 1097c478bd9Sstevel@tonic-gate char *table[SIZE]; 1107c478bd9Sstevel@tonic-gate char *names[SIZE]; 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate int 1137c478bd9Sstevel@tonic-gate main() 1147c478bd9Sstevel@tonic-gate { 1157c478bd9Sstevel@tonic-gate struct xx *p; 1167c478bd9Sstevel@tonic-gate int i, n, tok; 1177c478bd9Sstevel@tonic-gate char c; 1187c478bd9Sstevel@tonic-gate FILE *fp; 1197c478bd9Sstevel@tonic-gate char buf[100], name[100], def[100]; 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate printf("#include \"awk.h\"\n"); 1227c478bd9Sstevel@tonic-gate printf("#include \"y.tab.h\"\n\n"); 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate if ((fp = fopen("y.tab.h", "r")) == NULL) { 1257c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("maketab can't open y.tab.h!\n")); 1267c478bd9Sstevel@tonic-gate exit(1); 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate printf("static uchar *printname[%d] = {\n", SIZE); 1297c478bd9Sstevel@tonic-gate i = 0; 1307c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof (buf), fp) != NULL) { 1317c478bd9Sstevel@tonic-gate n = sscanf(buf, "%1c %s %s %d", &c, def, name, &tok); 1327c478bd9Sstevel@tonic-gate /* not a valid #define? */ 1337c478bd9Sstevel@tonic-gate if (c != '#' || n != 4 && strcmp(def, "define") != 0) 1347c478bd9Sstevel@tonic-gate continue; 1357c478bd9Sstevel@tonic-gate if (tok < FIRSTTOKEN || tok > LASTTOKEN) { 1367c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("maketab funny token %d %s\n"), 1377c478bd9Sstevel@tonic-gate tok, buf); 1387c478bd9Sstevel@tonic-gate exit(1); 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate names[tok-FIRSTTOKEN] = malloc(strlen(name)+1); 1417c478bd9Sstevel@tonic-gate strcpy(names[tok-FIRSTTOKEN], name); 1427c478bd9Sstevel@tonic-gate printf("\t(uchar *) \"%s\",\t/* %d */\n", name, tok); 1437c478bd9Sstevel@tonic-gate i++; 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate printf("};\n\n"); 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate for (p = proc; p->token != 0; p++) 1487c478bd9Sstevel@tonic-gate table[p->token-FIRSTTOKEN] = p->name; 1497c478bd9Sstevel@tonic-gate printf("\nCell *(*proctab[%d])() = {\n", SIZE); 1507c478bd9Sstevel@tonic-gate for (i = 0; i < SIZE; i++) 1517c478bd9Sstevel@tonic-gate if (table[i] == 0) 1527c478bd9Sstevel@tonic-gate printf("\tnullproc,\t/* %s */\n", names[i]); 1537c478bd9Sstevel@tonic-gate else 1547c478bd9Sstevel@tonic-gate printf("\t%s,\t/* %s */\n", table[i], names[i]); 1557c478bd9Sstevel@tonic-gate printf("};\n\n"); 1567c478bd9Sstevel@tonic-gate 1571ee2e5faSnakanon printf("uchar *\ntokname(int n)\n"); /* print a tokname() function */ 1587c478bd9Sstevel@tonic-gate printf("{\n"); 1591ee2e5faSnakanon printf(" static char buf[100];\n\n"); 1607c478bd9Sstevel@tonic-gate printf(" if (n < FIRSTTOKEN || n > LASTTOKEN) {\n"); 1611ee2e5faSnakanon printf(" (void) sprintf(buf, \"token %%d\", n);\n"); 1621ee2e5faSnakanon printf(" return ((uchar *)buf);\n"); 1637c478bd9Sstevel@tonic-gate printf(" }\n"); 1647c478bd9Sstevel@tonic-gate printf(" return printname[n-257];\n"); 1657c478bd9Sstevel@tonic-gate printf("}\n"); 1667c478bd9Sstevel@tonic-gate exit(0); 1677c478bd9Sstevel@tonic-gate } 168