14b588458SPeter Avalos /****************************************************************
24b588458SPeter Avalos Copyright (C) Lucent Technologies 1997
34b588458SPeter Avalos All Rights Reserved
44b588458SPeter Avalos
54b588458SPeter Avalos Permission to use, copy, modify, and distribute this software and
64b588458SPeter Avalos its documentation for any purpose and without fee is hereby
74b588458SPeter Avalos granted, provided that the above copyright notice appear in all
84b588458SPeter Avalos copies and that both that the copyright notice and this
94b588458SPeter Avalos permission notice and warranty disclaimer appear in supporting
104b588458SPeter Avalos documentation, and that the name Lucent Technologies or any of
114b588458SPeter Avalos its entities not be used in advertising or publicity pertaining
124b588458SPeter Avalos to distribution of the software without specific, written prior
134b588458SPeter Avalos permission.
144b588458SPeter Avalos
154b588458SPeter Avalos LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
164b588458SPeter Avalos INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
174b588458SPeter Avalos IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
184b588458SPeter Avalos SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
194b588458SPeter Avalos WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
204b588458SPeter Avalos IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
214b588458SPeter Avalos ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
224b588458SPeter Avalos THIS SOFTWARE.
234b588458SPeter Avalos ****************************************************************/
244b588458SPeter Avalos
254b588458SPeter Avalos /*
264b588458SPeter Avalos * this program makes the table to link function names
274b588458SPeter Avalos * and type indices that is used by execute() in run.c.
2848f09a05SAntonio Huete Jimenez * it finds the indices in awkgram.tab.h, produced by bison.
294b588458SPeter Avalos */
304b588458SPeter Avalos
314b588458SPeter Avalos #include <stdio.h>
324b588458SPeter Avalos #include <string.h>
334b588458SPeter Avalos #include <stdlib.h>
344b588458SPeter Avalos #include "awk.h"
3548f09a05SAntonio Huete Jimenez #include "awkgram.tab.h"
364b588458SPeter Avalos
374b588458SPeter Avalos struct xx
384b588458SPeter Avalos { int token;
394b588458SPeter Avalos const char *name;
404b588458SPeter Avalos const char *pname;
414b588458SPeter Avalos } proc[] = {
424b588458SPeter Avalos { PROGRAM, "program", NULL },
434b588458SPeter Avalos { BOR, "boolop", " || " },
444b588458SPeter Avalos { AND, "boolop", " && " },
454b588458SPeter Avalos { NOT, "boolop", " !" },
464b588458SPeter Avalos { NE, "relop", " != " },
474b588458SPeter Avalos { EQ, "relop", " == " },
484b588458SPeter Avalos { LE, "relop", " <= " },
494b588458SPeter Avalos { LT, "relop", " < " },
504b588458SPeter Avalos { GE, "relop", " >= " },
514b588458SPeter Avalos { GT, "relop", " > " },
524b588458SPeter Avalos { ARRAY, "array", NULL },
534b588458SPeter Avalos { INDIRECT, "indirect", "$(" },
544b588458SPeter Avalos { SUBSTR, "substr", "substr" },
55*ed569bc2SAaron LI { SUB, "dosub", "sub" },
56*ed569bc2SAaron LI { GSUB, "dosub", "gsub" },
574b588458SPeter Avalos { INDEX, "sindex", "sindex" },
584b588458SPeter Avalos { SPRINTF, "awksprintf", "sprintf " },
594b588458SPeter Avalos { ADD, "arith", " + " },
604b588458SPeter Avalos { MINUS, "arith", " - " },
614b588458SPeter Avalos { MULT, "arith", " * " },
624b588458SPeter Avalos { DIVIDE, "arith", " / " },
634b588458SPeter Avalos { MOD, "arith", " % " },
644b588458SPeter Avalos { UMINUS, "arith", " -" },
651d48fce0SDaniel Fojt { UPLUS, "arith", " +" },
664b588458SPeter Avalos { POWER, "arith", " **" },
674b588458SPeter Avalos { PREINCR, "incrdecr", "++" },
684b588458SPeter Avalos { POSTINCR, "incrdecr", "++" },
694b588458SPeter Avalos { PREDECR, "incrdecr", "--" },
704b588458SPeter Avalos { POSTDECR, "incrdecr", "--" },
714b588458SPeter Avalos { CAT, "cat", " " },
724b588458SPeter Avalos { PASTAT, "pastat", NULL },
734b588458SPeter Avalos { PASTAT2, "dopa2", NULL },
744b588458SPeter Avalos { MATCH, "matchop", " ~ " },
754b588458SPeter Avalos { NOTMATCH, "matchop", " !~ " },
764b588458SPeter Avalos { MATCHFCN, "matchop", "matchop" },
774b588458SPeter Avalos { INTEST, "intest", "intest" },
784b588458SPeter Avalos { PRINTF, "awkprintf", "printf" },
794b588458SPeter Avalos { PRINT, "printstat", "print" },
804b588458SPeter Avalos { CLOSE, "closefile", "closefile" },
814b588458SPeter Avalos { DELETE, "awkdelete", "awkdelete" },
824b588458SPeter Avalos { SPLIT, "split", "split" },
834b588458SPeter Avalos { ASSIGN, "assign", " = " },
844b588458SPeter Avalos { ADDEQ, "assign", " += " },
854b588458SPeter Avalos { SUBEQ, "assign", " -= " },
864b588458SPeter Avalos { MULTEQ, "assign", " *= " },
874b588458SPeter Avalos { DIVEQ, "assign", " /= " },
884b588458SPeter Avalos { MODEQ, "assign", " %= " },
894b588458SPeter Avalos { POWEQ, "assign", " ^= " },
904b588458SPeter Avalos { CONDEXPR, "condexpr", " ?: " },
914b588458SPeter Avalos { IF, "ifstat", "if(" },
924b588458SPeter Avalos { WHILE, "whilestat", "while(" },
934b588458SPeter Avalos { FOR, "forstat", "for(" },
944b588458SPeter Avalos { DO, "dostat", "do" },
954b588458SPeter Avalos { IN, "instat", "instat" },
964b588458SPeter Avalos { NEXT, "jump", "next" },
974b588458SPeter Avalos { NEXTFILE, "jump", "nextfile" },
984b588458SPeter Avalos { EXIT, "jump", "exit" },
994b588458SPeter Avalos { BREAK, "jump", "break" },
1004b588458SPeter Avalos { CONTINUE, "jump", "continue" },
1014b588458SPeter Avalos { RETURN, "jump", "ret" },
1024b588458SPeter Avalos { BLTIN, "bltin", "bltin" },
1034b588458SPeter Avalos { CALL, "call", "call" },
1044b588458SPeter Avalos { ARG, "arg", "arg" },
1054b588458SPeter Avalos { VARNF, "getnf", "NF" },
1064b588458SPeter Avalos { GETLINE, "awkgetline", "getline" },
1074b588458SPeter Avalos { 0, "", "" },
1084b588458SPeter Avalos };
1094b588458SPeter Avalos
1104b588458SPeter Avalos #define SIZE (LASTTOKEN - FIRSTTOKEN + 1)
1114b588458SPeter Avalos const char *table[SIZE];
1124b588458SPeter Avalos char *names[SIZE];
1134b588458SPeter Avalos
main(int argc,char * argv[])1144b588458SPeter Avalos int main(int argc, char *argv[])
1154b588458SPeter Avalos {
1164b588458SPeter Avalos const struct xx *p;
1174b588458SPeter Avalos int i, n, tok;
1184b588458SPeter Avalos char c;
1194b588458SPeter Avalos FILE *fp;
1204b588458SPeter Avalos char buf[200], name[200], def[200];
1211d48fce0SDaniel Fojt enum { TOK_UNKNOWN, TOK_ENUM, TOK_DEFINE } tokentype = TOK_UNKNOWN;
1224b588458SPeter Avalos
1234b588458SPeter Avalos printf("#include <stdio.h>\n");
1244b588458SPeter Avalos printf("#include \"awk.h\"\n");
12548f09a05SAntonio Huete Jimenez printf("#include \"awkgram.tab.h\"\n\n");
1264b588458SPeter Avalos
1271d48fce0SDaniel Fojt if (argc != 2) {
1281d48fce0SDaniel Fojt fprintf(stderr, "usage: maketab YTAB_H\n");
1294b588458SPeter Avalos exit(1);
1304b588458SPeter Avalos }
1311d48fce0SDaniel Fojt if ((fp = fopen(argv[1], "r")) == NULL) {
1321d48fce0SDaniel Fojt fprintf(stderr, "maketab can't open %s!\n", argv[1]);
1331d48fce0SDaniel Fojt exit(1);
1341d48fce0SDaniel Fojt }
1351d48fce0SDaniel Fojt printf("static const char * const printname[%d] = {\n", SIZE);
1364b588458SPeter Avalos i = 0;
1374b588458SPeter Avalos while (fgets(buf, sizeof buf, fp) != NULL) {
1381d48fce0SDaniel Fojt // 199 is sizeof(def) - 1
1391d48fce0SDaniel Fojt if (tokentype != TOK_ENUM) {
1401d48fce0SDaniel Fojt n = sscanf(buf, "%1c %199s %199s %d", &c, def, name,
1411d48fce0SDaniel Fojt &tok);
1421d48fce0SDaniel Fojt if (n == 4 && c == '#' && strcmp(def, "define") == 0) {
1431d48fce0SDaniel Fojt tokentype = TOK_DEFINE;
1441d48fce0SDaniel Fojt } else if (tokentype != TOK_UNKNOWN) {
1454b588458SPeter Avalos continue;
1461d48fce0SDaniel Fojt }
1471d48fce0SDaniel Fojt }
1481d48fce0SDaniel Fojt if (tokentype != TOK_DEFINE) {
1491d48fce0SDaniel Fojt /* not a valid #define, bison uses enums now */
1501d48fce0SDaniel Fojt n = sscanf(buf, "%199s = %d,\n", name, &tok);
1511d48fce0SDaniel Fojt if (n != 2)
1521d48fce0SDaniel Fojt continue;
1531d48fce0SDaniel Fojt tokentype = TOK_ENUM;
1541d48fce0SDaniel Fojt }
1551d48fce0SDaniel Fojt if (strcmp(name, "YYSTYPE_IS_DECLARED") == 0) {
1561d48fce0SDaniel Fojt tokentype = TOK_UNKNOWN;
1571d48fce0SDaniel Fojt continue;
1581d48fce0SDaniel Fojt }
1594b588458SPeter Avalos if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
1601d48fce0SDaniel Fojt tokentype = TOK_UNKNOWN;
1614b588458SPeter Avalos /* fprintf(stderr, "maketab funny token %d %s ignored\n", tok, buf); */
1624b588458SPeter Avalos continue;
1634b588458SPeter Avalos }
1641d48fce0SDaniel Fojt names[tok-FIRSTTOKEN] = strdup(name);
1651d48fce0SDaniel Fojt if (names[tok-FIRSTTOKEN] == NULL) {
1661d48fce0SDaniel Fojt fprintf(stderr, "maketab out of space copying %s", name);
1671d48fce0SDaniel Fojt continue;
1681d48fce0SDaniel Fojt }
1691d48fce0SDaniel Fojt printf("\t\"%s\",\t/* %d */\n", name, tok);
1704b588458SPeter Avalos i++;
1714b588458SPeter Avalos }
1724b588458SPeter Avalos printf("};\n\n");
1734b588458SPeter Avalos
1744b588458SPeter Avalos for (p=proc; p->token!=0; p++)
1754b588458SPeter Avalos table[p->token-FIRSTTOKEN] = p->name;
1764b588458SPeter Avalos printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE);
1774b588458SPeter Avalos for (i=0; i<SIZE; i++)
1781d48fce0SDaniel Fojt printf("\t%s,\t/* %s */\n",
1791d48fce0SDaniel Fojt table[i] ? table[i] : "nullproc", names[i] ? names[i] : "");
1804b588458SPeter Avalos printf("};\n\n");
1814b588458SPeter Avalos
1821d48fce0SDaniel Fojt printf("const char *tokname(int n)\n"); /* print a tokname() function */
1834b588458SPeter Avalos printf("{\n");
1841d48fce0SDaniel Fojt printf("\tstatic char buf[100];\n\n");
1851d48fce0SDaniel Fojt printf("\tif (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
1861d48fce0SDaniel Fojt printf("\t\tsnprintf(buf, sizeof(buf), \"token %%d\", n);\n");
1871d48fce0SDaniel Fojt printf("\t\treturn buf;\n");
1881d48fce0SDaniel Fojt printf("\t}\n");
1891d48fce0SDaniel Fojt printf("\treturn printname[n-FIRSTTOKEN];\n");
1904b588458SPeter Avalos printf("}\n");
1914b588458SPeter Avalos return 0;
1924b588458SPeter Avalos }
193