[BACK]Return to maketab.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / awk

Annotation of src/usr.bin/awk/maketab.c, Revision 1.16

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