Annotation of 43BSD/contrib/hyper/hyroute/hyr_lex.l, revision 1.1.1.1

1.1       root        1: %Start GATE
                      2: ws     [ \t\n]*
                      3: s      [ \t\n]+
                      4: num    [0123456789abcdefABCDEF]+
                      5: name   [a-zA-Z.\-_][a-zA-Z0-9.\-_]*
                      6: other  [^ \t\na-zA-Z.\-_0-9;]
                      7: 
                      8: %{
                      9: #include <ctype.h>
                     10: #include <sys/types.h>
                     11: #include <sys/socket.h>
                     12: #include <netinet/in.h>
                     13: #include <netdb.h>
                     14: #include "hyr_sym.h"
                     15: #undef output
                     16: 
                     17: struct sym *curgate = NULL;
                     18: struct sym *sym_head = NULL;
                     19: static char rcsid[] = "$Header: hyr_lex.l,v 2.4 84/05/04 12:15:34 steveg Exp $$Locker:  $";
                     20: 
                     21: char name[32];
                     22: 
                     23: int lexdebug = 0;
                     24: 
                     25: extern struct sym *lookup();
                     26: %}
                     27: 
                     28: %%
                     29: ^\*.*$ ;
                     30: ^#.*$  ;
                     31: ^{ws}$ ;
                     32: 
                     33: {ws}direct{s}{name}{s}{num}{s}{num}{s}{num}{ws};       {
                     34:        register struct sym *s;
                     35:        unsigned dst, ctl, access;
                     36: 
                     37:        outwrap();
                     38:        sscanf(yytext, " direct %s %x %x %x ", name, &dst, &ctl, &access);
                     39:        if (lexdebug)
                     40:                fprintf(stderr, "DIRECT %s %04x %04x %04x\n", name, dst, ctl, access);
                     41:        if (s = lookup(name)) {
                     42:                s->sym_dst = htons(dst);
                     43:                s->sym_ctl = htons(ctl);
                     44:                s->sym_access = htons(access);
                     45:                s->sym_flags = HS_DIR;
                     46:        }
                     47: }
                     48: 
                     49: {ws}loop{s}{name}{s}{num}{s}{num}{s}{num}{ws}; {
                     50:        register struct sym *s;
                     51:        unsigned dst, ctl, access;
                     52: 
                     53:        outwrap();
                     54:        sscanf(yytext, " loop %s %x %x %x ", name, &dst, &ctl, &access);
                     55:        if (lexdebug)
                     56:                fprintf(stderr, "LOOP %s %04x %04x %04x\n", name, dst, ctl, access);
                     57:        if (s = lookup(name)) {
                     58:                s->sym_dst = htons(dst);
                     59:                s->sym_ctl = htons(ctl);
                     60:                s->sym_access = htons(access);
                     61:                s->sym_flags = HS_DIR + HS_LOOP;
                     62:        }
                     63: }
                     64: 
                     65: {ws}remloop{s}{name}{s}{num}{s}{num}{s}{num}{ws};      {
                     66:        register struct sym *s;
                     67:        unsigned dst, ctl, access;
                     68: 
                     69:        outwrap();
                     70:        sscanf(yytext, " remloop %s %x %x %x ", name, &dst, &ctl, &access);
                     71:        if (lexdebug)
                     72:                fprintf(stderr, "REMLOOP %s %04x %04x %04x\n", name, dst, ctl, access);
                     73:        if (s = lookup(name)) {
                     74:                s->sym_dst = htons(dst);
                     75:                s->sym_ctl = htons(ctl);
                     76:                s->sym_access = htons(access);
                     77:                s->sym_flags = HS_DIR + HS_RLOOP;
                     78:        }
                     79: }
                     80: 
                     81: {ws}gateway{s}{name}   {
                     82:        register struct sym *s;
                     83: 
                     84:        outwrap();
                     85:        sscanf(yytext, " gateway %s ", name);
                     86:        if (lexdebug)
                     87:                fprintf(stderr, "GATEWAY %s ->", name);
                     88:        if (s = lookup(name)) {
                     89:                s->sym_flags = HS_INDIR;
                     90:                curgate = s;
                     91:        }
                     92:        BEGIN GATE;
                     93: }
                     94: 
                     95: <GATE>{s}{name}                {
                     96:        register struct sym *s;
                     97: 
                     98:        outwrap();
                     99:        sscanf(yytext, " %s ", name);
                    100:        if (lexdebug)
                    101:                fprintf(stderr, " %s", name);
                    102:        if (s = lookup(name)) {
                    103:                if (curgate->sym_ngate < 32)
                    104:                        curgate->sym_gate[curgate->sym_ngate++] = s;
                    105:                s->sym_flags |= HS_GATE;
                    106:        }
                    107: }
                    108: 
                    109: <GATE>{ws};    {
                    110:        outwrap();
                    111:        if (lexdebug)
                    112:                fprintf(stderr, "\n");
                    113:        curgate = NULL;
                    114:        BEGIN 0;
                    115: }
                    116: ;      ;       /* ignore extra ';'s */
                    117: 
                    118: %%
                    119: 
                    120: struct sym *
                    121: lookup(cp)
                    122:        char *cp;
                    123: {
                    124:        register struct sym *s;
                    125:        register struct hostent *hp;
                    126:        struct hostent *gethostbyname();
                    127:        extern char *malloc();
                    128: 
                    129:        for (s = sym_head; s != NULL; s = s->sym_next) {
                    130:                if (strncmp(cp, s->sym_name, sizeof(s->sym_name)) == 0 ||
                    131:                    strncmp(cp, s->sym_nickname, sizeof(s->sym_nickname)) == 0) {
                    132:                        return(s);
                    133:                }
                    134:        }
                    135:        hp = gethostbyname(cp);
                    136:        if (hp == NULL) {
                    137:                printf("Host %s not found\n", cp);
                    138:                return(NULL);
                    139:        }
                    140:        if (hp->h_addrtype != AF_INET) {
                    141:                printf("Host %s not Internet\n", cp);
                    142:                return(NULL);
                    143:        }
                    144:        s = (struct sym *) malloc(sizeof *s);
                    145:        if (s != NULL) {
                    146:                s->sym_lastok = 0;
                    147:                s->sym_flags = 0;
                    148:                s->sym_ngate = 0;
                    149:                s->sym_dst = s->sym_ctl = s->sym_access = 0;
                    150:                s->sym_inaddr = *(struct in_addr *)hp->h_addr;
                    151:                s->sym_next = sym_head;
                    152:                sym_head = s;
                    153:                strncpy(s->sym_name, hp->h_name, sizeof(s->sym_name));
                    154:                strncpy(s->sym_nickname, cp, sizeof(s->sym_nickname));
                    155:        }
                    156:        return(s);
                    157: }
                    158: 
                    159: sym_print(s)
                    160:        register struct sym *s;
                    161: {
                    162:        register int i;
                    163:        char *str = " ";
                    164: 
                    165:        printf("Host %s", s->sym_name);
                    166:        if (strcmp(s->sym_name, s->sym_nickname) != 0)
                    167:                printf(" (aka %s)", s->sym_nickname);
                    168:        printf("\n\tInternet address: ");
                    169:        in_print(s->sym_inaddr.s_addr);
                    170:        printf("\n");
                    171:        if (s->sym_flags & HS_DIR) {
                    172:                if (s->sym_flags & HS_LOOP)
                    173:                        printf("\tLoop:    ");
                    174:                else if (s->sym_flags & HS_RLOOP)
                    175:                        printf("\tRemloop: ");
                    176:                else
                    177:                        printf("\tDirect:  ");
                    178:                printf("dst %04x ctl %04x access %04x%s\n",
                    179:                        ntohs(s->sym_dst),
                    180:                        ntohs(s->sym_ctl),
                    181:                        ntohs(s->sym_access),
                    182:                        (s->sym_flags & HS_GATE) ? "\tused as a gateway" : "");
                    183:        } else if (s->sym_flags & HS_INDIR) {
                    184:                printf("\tGatewayed through:");
                    185:                for (i = 0; i < s->sym_ngate; i++) {
                    186:                        printf("%s%s", str, s->sym_gate[i]->sym_name);
                    187:                        str = ", ";
                    188:                }
                    189:                printf("\n");
                    190:        }
                    191: }
                    192: 
                    193: symtab_print()
                    194: {
                    195:        register struct sym *s;
                    196: 
                    197:        printf("\n");
                    198:        for (s = sym_head; s != NULL; s = s->sym_next)
                    199:                sym_print(s);
                    200:        printf("End of Symbol table\n");
                    201: }
                    202: 
                    203: char outbuf[128] = "";
                    204: char *outbufp = outbuf;
                    205: 
                    206: /*
                    207:  * record characters in the output buffer for later error message
                    208:  */
                    209: output(c)
                    210:        int c;
                    211: {
                    212:        if ((isprint(c) || c == ' ') && isascii(c) && outbufp < &outbuf[sizeof(outbuf)])
                    213:                *outbufp++ = c;
                    214:        *outbufp = '\0';
                    215: }
                    216: 
                    217: /*
                    218:  * if there are any characters in the output buffer, print an error message
                    219:  */
                    220: outwrap()
                    221: {
                    222:        if (outbufp != outbuf) {
                    223:                fprintf(stderr, "hyroute: syntax error on \"%s\"\n", outbuf);
                    224:                outbufp = outbuf;
                    225:                lex_error++;
                    226:        }
                    227: }
                    228: 
                    229: /*
                    230:  * flush last error message out then return saying no more input available
                    231:  */
                    232: yywrap() {
                    233:        outwrap();
                    234:        return(1);
                    235: }
                    236: 
                    237: /*
                    238:  * print an internet format address in "dot" notation
                    239:  * input address is in network byte ordering
                    240:  */
                    241: in_print(n)
                    242:        register unsigned long n;
                    243: {
                    244:        n = ntohl(n);
                    245:        printf("%d.%d.%d.%d",   (n >> 24) & 0xff, (n >> 16) & 0xff, (n >> 8) & 0xff, n & 0xff);
                    246: }

unix.superglobalmegacorp.com

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