Home | History | Annotate | Download | only in nawk-20071023
      1 /****************************************************************
      2 Copyright (C) Lucent Technologies 1997
      3 All Rights Reserved
      4 
      5 Permission to use, copy, modify, and distribute this software and
      6 its documentation for any purpose and without fee is hereby
      7 granted, provided that the above copyright notice appear in all
      8 copies and that both that the copyright notice and this
      9 permission notice and warranty disclaimer appear in supporting
     10 documentation, and that the name Lucent Technologies or any of
     11 its entities not be used in advertising or publicity pertaining
     12 to distribution of the software without specific, written prior
     13 permission.
     14 
     15 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
     16 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
     17 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
     18 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
     20 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
     21 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
     22 THIS SOFTWARE.
     23 ****************************************************************/
     24 
     25 /*
     26  * this program makes the table to link function names
     27  * and type indices that is used by execute() in run.c.
     28  * it finds the indices in ytab.h, produced by yacc.
     29  */
     30 
     31 #include <stdio.h>
     32 #include <string.h>
     33 #include <stdlib.h>
     34 #include "awk.h"
     35 #include "ytab.h"
     36 
     37 struct xx
     38 {	int token;
     39 	const char *name;
     40 	const char *pname;
     41 } proc[] = {
     42 	{ PROGRAM, "program", NULL },
     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", NULL },
     53 	{ INDIRECT, "indirect", "$(" },
     54 	{ SUBSTR, "substr", "substr" },
     55 	{ SUB, "sub", "sub" },
     56 	{ GSUB, "gsub", "gsub" },
     57 	{ INDEX, "sindex", "sindex" },
     58 	{ SPRINTF, "awksprintf", "sprintf " },
     59 	{ ADD, "arith", " + " },
     60 	{ MINUS, "arith", " - " },
     61 	{ MULT, "arith", " * " },
     62 	{ DIVIDE, "arith", " / " },
     63 	{ MOD, "arith", " % " },
     64 	{ UMINUS, "arith", " -" },
     65 	{ POWER, "arith", " **" },
     66 	{ PREINCR, "incrdecr", "++" },
     67 	{ POSTINCR, "incrdecr", "++" },
     68 	{ PREDECR, "incrdecr", "--" },
     69 	{ POSTDECR, "incrdecr", "--" },
     70 	{ CAT, "cat", " " },
     71 	{ PASTAT, "pastat", NULL },
     72 	{ PASTAT2, "dopa2", NULL },
     73 	{ MATCH, "matchop", " ~ " },
     74 	{ NOTMATCH, "matchop", " !~ " },
     75 	{ MATCHFCN, "matchop", "matchop" },
     76 	{ INTEST, "intest", "intest" },
     77 	{ PRINTF, "awkprintf", "printf" },
     78 	{ PRINT, "printstat", "print" },
     79 	{ CLOSE, "closefile", "closefile" },
     80 	{ DELETE, "awkdelete", "awkdelete" },
     81 	{ SPLIT, "split", "split" },
     82 	{ ASSIGN, "assign", " = " },
     83 	{ ADDEQ, "assign", " += " },
     84 	{ SUBEQ, "assign", " -= " },
     85 	{ MULTEQ, "assign", " *= " },
     86 	{ DIVEQ, "assign", " /= " },
     87 	{ MODEQ, "assign", " %= " },
     88 	{ POWEQ, "assign", " ^= " },
     89 	{ CONDEXPR, "condexpr", " ?: " },
     90 	{ IF, "ifstat", "if(" },
     91 	{ WHILE, "whilestat", "while(" },
     92 	{ FOR, "forstat", "for(" },
     93 	{ DO, "dostat", "do" },
     94 	{ IN, "instat", "instat" },
     95 	{ NEXT, "jump", "next" },
     96 	{ NEXTFILE, "jump", "nextfile" },
     97 	{ EXIT, "jump", "exit" },
     98 	{ BREAK, "jump", "break" },
     99 	{ CONTINUE, "jump", "continue" },
    100 	{ RETURN, "jump", "ret" },
    101 	{ BLTIN, "bltin", "bltin" },
    102 	{ CALL, "call", "call" },
    103 	{ ARG, "arg", "arg" },
    104 	{ VARNF, "getnf", "NF" },
    105 	{ GETLINE, "getline", "getline" },
    106 	{ 0, "", "" },
    107 };
    108 
    109 #define SIZE	(LASTTOKEN - FIRSTTOKEN + 1)
    110 const char *table[SIZE];
    111 char *names[SIZE];
    112 
    113 int main(int argc, char *argv[])
    114 {
    115 	const struct xx *p;
    116 	int i, n, tok;
    117 	char c;
    118 	FILE *fp;
    119         const char* input;
    120 	char buf[200], name[200], def[200];
    121 
    122 	printf("#include <stdio.h>\n");
    123 	printf("#include \"awk.h\"\n");
    124 	printf("#include \"ytab.h\"\n\n");
    125 	for (i = SIZE; --i >= 0; )
    126 		names[i] = "";
    127 
    128         input = "ytab.h";
    129         if (argc > 1) {
    130             input = argv[1];
    131             if (!strcmp(input,"-"))
    132                 input = NULL;
    133         }
    134         if (input == NULL) {
    135             fp = stdin;
    136         } else {
    137             if ((fp = fopen(input, "rt")) == NULL) {
    138                     fprintf(stderr, "maketab can't open %s!\n", input);
    139                     exit(1);
    140             }
    141         }
    142 	printf("static char *printname[%d] = {\n", SIZE);
    143 	i = 0;
    144 	while (fgets(buf, sizeof buf, fp) != NULL) {
    145 		n = sscanf(buf, "%1c %s %s %d", &c, def, name, &tok);
    146 		if (c != '#' || (n != 4 && strcmp(def,"define") != 0))	/* not a valid #define */
    147 			continue;
    148 		if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
    149 			/* fprintf(stderr, "maketab funny token %d %s ignored\n", tok, buf); */
    150 			continue;
    151 		}
    152 		names[tok-FIRSTTOKEN] = (char *) malloc(strlen(name)+1);
    153 		strcpy(names[tok-FIRSTTOKEN], name);
    154 		printf("\t(char *) \"%s\",\t/* %d */\n", name, tok);
    155 		i++;
    156 	}
    157 	printf("};\n\n");
    158 
    159 	for (p=proc; p->token!=0; p++)
    160 		table[p->token-FIRSTTOKEN] = p->name;
    161 	printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE);
    162 	for (i=0; i<SIZE; i++)
    163 		if (table[i]==0)
    164 			printf("\tnullproc,\t/* %s */\n", names[i]);
    165 		else
    166 			printf("\t%s,\t/* %s */\n", table[i], names[i]);
    167 	printf("};\n\n");
    168 
    169 	printf("char *tokname(int n)\n");	/* print a tokname() function */
    170 	printf("{\n");
    171 	printf("	static char buf[100];\n\n");
    172 	printf("	if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
    173 	printf("		sprintf(buf, \"token %%d\", n);\n");
    174 	printf("		return buf;\n");
    175 	printf("	}\n");
    176 	printf("	return printname[n-FIRSTTOKEN];\n");
    177 	printf("}\n");
    178 	return 0;
    179 }
    180