Annotation of 40BSD/cmd/pascal/pc2.c, revision 1.1.1.1

1.1       root        1: #include <stdio.h>
                      2: #include <ctype.h>
                      3: /*
                      4:  * The hash table must be at least twice as big as the number
                      5:  * of patterns, preferably bigger. It must also be a prime number
                      6:  */
                      7: #define HSHSIZ 101
                      8: 
                      9: struct pats {
                     10:        char    *name;
                     11:        char    *replace;
                     12: } ptab[] = {
                     13: 
                     14:        { "ACTFILE\n",
                     15: "      movl    (sp)+,r1\n\
                     16:        movl    12(r1),r0\n" },
                     17: 
                     18:        { "fgetc\n",
                     19: "      decl    *(sp)\n\
                     20:        jgeq    1f\n\
                     21:        calls   $1,__filbuf\n\
                     22:        jbr     2f\n\
                     23: 1:\n\
                     24:        addl3   $4,(sp)+,r1\n\
                     25:        movzbl  *(r1),r0\n\
                     26:        incl    (r1)\n\
                     27: 2:\n" },
                     28: 
                     29:        { "fputc\n",
                     30: "      decl    *4(sp)\n\
                     31:        jgeq    1f\n\
                     32:        calls   $2,__flsbuf\n\
                     33:        jbr     2f\n\
                     34: 1:\n\
                     35:        movl    (sp)+,r0\n\
                     36:        addl3   $4,(sp)+,r1\n\
                     37:        movb    r0,*(r1)\n\
                     38:        incl    (r1)\n\
                     39: 2:\n" },
                     40: 
                     41:        { "MOVC3\n",
                     42: "      movl    (sp)+,r5\n\
                     43:        movc3   r5,*(sp)+,*(sp)+\n" },
                     44: 
                     45:        { "LOCC\n",
                     46: "      movl    (sp)+,r5\n\
                     47:        movl    (sp)+,r4\n\
                     48:        locc    r5,r4,*(sp)+\n" },
                     49: 
                     50:        { "ROUND\n",
                     51: "      cvtrdl  (sp)+,r0\n" },
                     52: 
                     53:        { "TRUNC\n",
                     54: "      cvtdl   (sp)+,r0\n" },
                     55: 
                     56:        { "FCALL\n",
                     57: "      movl    (sp),r0\n\
                     58:        ashl    $3,4(r0),r1\n\
                     59:        movc3   r1,__disply+8,8(r0)[r1]\n\
                     60:        movl    (sp),r0\n\
                     61:        ashl    $3,4(r0),r1\n\
                     62:        movc3   r1,8(r0),__disply+8\n\
                     63:        movl    *(sp)+,r0\n" },
                     64: 
                     65:        { "FRTN\n",
                     66: "      movl    (sp)+,r0\n\
                     67:        ashl    $3,4(r0),r1\n\
                     68:        movc3   r1,8(r0)[r1],__disply+8\n\
                     69:        movl    (sp)+,r0\n" },
                     70: 
                     71:        { "FSAV\n",
                     72: "      movl    8(sp),r0\n\
                     73:        movl    (sp)+,(r0)\n\
                     74:        movl    (sp)+,4(r0)\n\
                     75:        ashl    $3,4(r0),r1\n\
                     76:        movc3   r1,__disply+8,8(r0)\n\
                     77:        movl    (sp)+,r0\n" },
                     78: 
                     79:        { "RELEQ\n",
                     80: "      movl    (sp)+,r1\n\
                     81:        cmpc3   r1,*(sp)+,*(sp)+\n\
                     82:        beql    1f\n\
                     83:        clrl    r0\n\
                     84:        brb     2f\n\
                     85: 1:\n\
                     86:        movl    $1,r0\n\
                     87: 2:\n" },
                     88: 
                     89:        { "RELNE\n",
                     90: "      movl    (sp)+,r1\n\
                     91:        cmpc3   r1,*(sp)+,*(sp)+\n\
                     92:        bneq    1f\n\
                     93:        clrl    r0\n\
                     94:        brb     2f\n\
                     95: 1:\n\
                     96:        movl    $1,r0\n\
                     97: 2:\n" },
                     98: 
                     99:        { "RELSLT\n",
                    100: "      movl    (sp)+,r1\n\
                    101:        cmpc3   r1,*(sp)+,*(sp)+\n\
                    102:        blss    1f\n\
                    103:        clrl    r0\n\
                    104:        brb     2f\n\
                    105: 1:\n\
                    106:        movl    $1,r0\n\
                    107: 2:\n" },
                    108: 
                    109:        { "RELSLE\n",
                    110: "      movl    (sp)+,r1\n\
                    111:        cmpc3   r1,*(sp)+,*(sp)+\n\
                    112:        bleq    1f\n\
                    113:        clrl    r0\n\
                    114:        brb     2f\n\
                    115: 1:\n\
                    116:        movl    $1,r0\n\
                    117: 2:\n" },
                    118: 
                    119:        { "RELSGT\n",
                    120: "      movl    (sp)+,r1\n\
                    121:        cmpc3   r1,*(sp)+,*(sp)+\n\
                    122:        bgtr    1f\n\
                    123:        clrl    r0\n\
                    124:        brb     2f\n\
                    125: 1:\n\
                    126:        movl    $1,r0\n\
                    127: 2:\n" },
                    128: 
                    129:        { "RELSGE\n",
                    130: "      movl    (sp)+,r1\n\
                    131:        cmpc3   r1,*(sp)+,*(sp)+\n\
                    132:        bgeq    1f\n\
                    133:        clrl    r0\n\
                    134:        brb     2f\n\
                    135: 1:\n\
                    136:        movl    $1,r0\n\
                    137: 2:\n" },
                    138: 
                    139:        { "ADDT\n",
                    140: "      movl    (sp)+,r0\n\
                    141:        movl    (sp)+,r1\n\
                    142:        movl    (sp)+,r2\n\
                    143:        movl    r0,r3\n\
                    144:        movl    (sp)+,r4\n\
                    145: 1:\n\
                    146:        bisl3   (r1)+,(r2)+,(r3)+\n\
                    147:        sobgtr  r4,1b\n" },
                    148: 
                    149:        { "SUBT\n",
                    150: "      movl    (sp)+,r0\n\
                    151:        movl    (sp)+,r1\n\
                    152:        movl    (sp)+,r2\n\
                    153:        movl    r0,r3\n\
                    154:        movl    (sp)+,r4\n\
                    155: 1:\n\
                    156:        bicl3   (r2)+,(r1)+,(r3)+\n\
                    157:        sobgtr  r4,1b\n" },
                    158: 
                    159:        { "MULT\n",
                    160: "      movl    (sp)+,r0\n\
                    161:        movl    (sp)+,r1\n\
                    162:        movl    (sp)+,r2\n\
                    163:        movl    r0,r3\n\
                    164:        movl    (sp)+,r4\n\
                    165: 1:\n\
                    166:        mcoml   (r1)+,r5\n\
                    167:        bicl3   r5,(r2)+,(r3)+\n\
                    168:        sobgtr  r4,1b\n" },
                    169: 
                    170:        { "IN\n",
                    171: "      clrl    r0\n\
                    172:        movl    (sp)+,r1\n\
                    173:        subl2   (sp)+,r1\n\
                    174:        cmpl    r1,(sp)+\n\
                    175:        bgtru   1f\n\
                    176:        bbc     r1,*(sp)+,1f\n\
                    177:        movl    $1,r0\n\
                    178: 1:\n" }
                    179: };
                    180: 
                    181: struct pats            *htbl[HSHSIZ];
                    182: 
                    183: 
                    184: #define        CHK(c)  if (*cp++ != c) goto copy;
                    185: 
                    186: #define HASH(cp, hp) {\
                    187:        hash = 0; rehash = 1; ccp = cp; \
                    188:        do      { \
                    189:                hash *= (int)*ccp++; \
                    190:        } while (*ccp && *ccp != '\n'); \
                    191:        hash >>= 7; hash %= HSHSIZ; hp = &htbl[hash]; size = ccp - cp + 1; \
                    192:        }
                    193: 
                    194: #define REHASH(hp) {\
                    195:        hp += rehash; rehash += 2; \
                    196:        if (hp >= &htbl[HSHSIZ]) \
                    197:                hp -= HSHSIZ; \
                    198:        }
                    199: 
                    200: 
                    201: main(argc, argv)
                    202: 
                    203:        int     argc;
                    204:        char    **argv;
                    205: {
                    206:        register struct pats    *pp;
                    207:        register struct pats    **hp;
                    208:        register char           *cp, *ccp;
                    209:        register int            hash, rehash, size;
                    210:        char                    line[BUFSIZ];
                    211: 
                    212:        if (argc > 1)
                    213:                freopen(argv[1], "r", stdin);
                    214:        if (argc > 2)
                    215:                freopen(argv[2], "w", stdout);
                    216:        /*
                    217:         * set up the hash table
                    218:         */
                    219:        for(pp = ptab; pp < &ptab[sizeof ptab/sizeof ptab[0]]; pp++) {
                    220:                HASH(pp->name, hp);
                    221:                while (*hp)
                    222:                        REHASH(hp);
                    223:                *hp = pp;
                    224:        }
                    225:        /*
                    226:         * check each line and replace as appropriate
                    227:         */
                    228:        while (fgets(line, BUFSIZ, stdin)) {
                    229:                for (cp = line; *cp && *cp == '\t'; )
                    230:                        cp++;
                    231:                CHK('c'); CHK('a'); CHK('l'); CHK('l'); CHK('s'); CHK('\t');
                    232:                CHK('$'); if (!isdigit(*cp++)) goto copy; CHK(','); CHK('_');
                    233:                HASH(cp, hp);
                    234:                while (*hp) {
                    235:                        if (RELEQ(size, (*hp)->name, cp)) {
                    236:                                fputs((*hp)->replace, stdout);
                    237:                                goto nextline;
                    238:                        }
                    239:                        REHASH(hp);
                    240:                }
                    241: copy:
                    242:                fputs(line, stdout);
                    243: nextline:;
                    244:        }
                    245:        exit(0);
                    246: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.