home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.lbl.gov / 2014.05.ftp.ee.lbl.gov.tar / ftp.ee.lbl.gov / hf-1.2.tar.gz / hf-1.2.tar / hf-1.2 / ef.l < prev    next >
Text File  |  2009-10-10  |  6KB  |  271 lines

  1. L              [0-9a-f]
  2. H              ({L}{1,2})
  3.  
  4.     #include <sys/types.h>
  5.     #include <sys/socket.h>
  6.  
  7.     #ifdef HAVE_NETINET_ETHER_H
  8.     #include <netinet/ether.h>
  9.     #elif HAVE_NET_ETHERNET_H
  10.     #include <net/ethernet.h>
  11.     #endif
  12.     #include <net/if.h>
  13.  
  14.     #include <netinet/in.h>
  15.     #include <netinet/if_ether.h>
  16.  
  17.     #include <arpa/nameser.h>
  18.     #include <arpa/inet.h>
  19.  
  20.     #include <ctype.h>
  21.     #include <netdb.h>
  22.     #include <resolv.h>
  23.     #include <stdio.h>
  24.     #include <string.h>
  25.     #include <unistd.h>
  26.  
  27.     #include "gnuc.h"
  28.     #ifdef HAVE_OS_PROTO_H
  29.     #include "os-proto.h"
  30.     #endif
  31.  
  32.     #undef yywrap
  33.     #ifdef FLEX_SCANNER
  34.     #define YY_NO_UNPUT
  35.     #endif
  36.     #define convert(str) (fputs(ether2host(str), stdout))
  37.     char *ether2host(char *);
  38.     int yywrap(void);
  39.     int yylex(void);
  40. %%
  41.  
  42. {H}:{H}:{H}:{H}:{H}:{H}    convert(yytext);
  43.  
  44. ({H}:){1,5}        ECHO;        /* anti-backtrack */
  45. {H}((:{H}){1,4})    ECHO;        /* anti-backtrack */
  46.  
  47. {L}+            ECHO;
  48. [^0-9a-z\n]+        ECHO;
  49. [^0-9a-z\n]+\n        ECHO;
  50.  
  51. %%
  52.  
  53. /*
  54.  * Copyright (c) 1990, 1991, 1996, 1999, 2000, 2004, 2005, 2009
  55.  *    The Regents of the University of California.  All rights reserved.
  56.  *
  57.  * Redistribution and use in source and binary forms, with or without
  58.  * modification, are permitted provided that: (1) source code distributions
  59.  * retain the above copyright notice and this paragraph in its entirety, (2)
  60.  * distributions including binary code include the above copyright notice and
  61.  * this paragraph in its entirety in the documentation or other materials
  62.  * provided with the distribution, and (3) all advertising materials mentioning
  63.  * features or use of this software display the following acknowledgement:
  64.  * ``This product includes software developed by the University of California,
  65.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  66.  * the University nor the names of its contributors may be used to endorse
  67.  * or promote products derived from this software without specific prior
  68.  * written permission.
  69.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  70.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  71.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  72.  */
  73.  
  74. #ifndef lint
  75. static const char copyright[] =
  76.     "@(#) Copyright (c) 1990, 1991, 1996, 1999, 2000, 2004, 2005, 2009\n\
  77. The Regents of the University of California.  All rights reserved.\n";
  78. static const char rcsid[] =
  79.     "@(#) $Id: ef.l 171 2009-10-11 00:27:43Z leres $ (LBL)";
  80. #endif
  81.  
  82. #define HSIZE 1024        /* must be a power of two */
  83.  
  84. struct htable {
  85.     u_int high;
  86.     u_short low;
  87.     char *name;
  88.     struct htable *next;
  89. } htable[HSIZE];
  90.  
  91. #ifdef DEBUG
  92. int debug = 0;
  93. #endif
  94.  
  95. int targc;
  96. char **targv;
  97.  
  98. extern char *optarg;
  99. extern int optind, opterr;
  100.  
  101. /* Forwards */
  102. #ifdef DEBUG
  103. void dump(void);
  104. #endif
  105. int main(int, char **);
  106.  
  107. int
  108. main(argc, argv)
  109.     int argc;
  110.     char **argv;
  111. {
  112.     char *cp;
  113.     int op;
  114.     char *prog;
  115.  
  116.     if (argv[0] == NULL)
  117.         prog = "ef";
  118.     else if ((cp = strrchr(argv[0], '/')) != NULL)
  119.         prog = cp + 1;
  120.     else
  121.         prog = argv[0];
  122.  
  123.     opterr = 0;
  124.     while ((op = getopt(argc, argv, "d")) != EOF)
  125.         switch (op) {
  126.  
  127. #ifdef DEBUG
  128.         case 'd':
  129.             ++debug;
  130.             break;
  131. #endif
  132.  
  133.         default:
  134.             (void)fprintf(stderr, "usage: %s [-d] [file ...]\n",
  135.                 prog);
  136.             exit(1);
  137.             /* NOTREACHED */
  138.         }
  139.  
  140.     /* Let yywrap() figure out if there are any arguments to open */
  141.     targc = argc - optind;
  142.     targv = &argv[optind];
  143.     yyin = 0;
  144.     (void)yywrap();
  145.  
  146.     /* Process file opened by yywrap() or stdin if no arguments */
  147.     if (yyin)
  148.         yylex();
  149. #ifdef DEBUG
  150.     if (debug) {
  151.         fflush(stdout);
  152.         dump();
  153.     }
  154. #endif
  155.     exit(0);
  156. }
  157.  
  158. int
  159. yywrap()
  160. {
  161.     char *file;
  162.     static int didany = 0;
  163.  
  164.     /* Close file, if necessary */
  165.     if (yyin) {
  166.         if (yyin != stdin)
  167.             (void)fclose(yyin);
  168.         yyin = NULL;
  169.     }
  170.  
  171.     /* Spin through arguments until we run out or successfully open one */
  172.     while (targc > 0) {
  173.         file = targv[0];
  174.         --targc;
  175.         ++targv;
  176.         ++didany;
  177.         if ((yyin = fopen(file, "r")) != NULL)
  178.             return (0);
  179.         perror(file);
  180.     }
  181.     if (!didany) {
  182.         ++didany;
  183.         yyin = stdin;
  184.         return (0);
  185.     }
  186.     return (1);
  187. }
  188.  
  189. char *
  190. ether2host(str)
  191.     char *str;
  192. {
  193.     u_int high;
  194.     u_short low, ehash;
  195.     char *cp;
  196.     struct ether_addr *ether;
  197.     struct htable *p, *p2;
  198.     char buf[512];
  199.  
  200.     if ((ether = ether_aton(str)) == NULL)
  201.         return (0);
  202.  
  203.     high = *(u_int *)ðer[0];
  204.     low = *(u_short *)ðer[4];
  205.     ehash = high >> 16;
  206.     ehash ^= high ^ low;
  207.  
  208.     /* First check if we already know about it */
  209.     for (p = &htable[ehash & (HSIZE - 1)]; p; p = p->next)
  210.         if (p->high == high && p->low == low && p->name)
  211.             return (p->name);
  212.  
  213.     if (ether_ntohost(buf, ether))
  214.         strcpy(buf, str);
  215.  
  216.     /* Malloc space for new name */
  217.     cp = malloc((u_int) strlen(buf) + 1);
  218.     if (cp == 0)
  219.         return (str);
  220.  
  221.     /* add to hash table */
  222.     p = &htable[ehash & (HSIZE - 1)];
  223.     if (p->name) {
  224.         /* Handle the collision */
  225.         p2 = (struct htable *)malloc(sizeof(struct htable));
  226.         if (p2 == 0) {
  227.             /* Lose, lose */
  228.             free(cp);
  229.             return (str);
  230.         }
  231.         memset((char *)p2, 0, sizeof(struct htable));
  232.         p2->next = p->next;
  233.         p->next = p2;
  234.         p = p2;
  235.     }
  236.  
  237.     /* Install new host */
  238.     p->high = high;
  239.     p->low = low;
  240.     p->name = strcpy(cp, buf);
  241.  
  242.     /* Return answer */
  243.     return (p->name);
  244. }
  245.  
  246. #ifdef DEBUG
  247. void
  248. dump()
  249. {
  250.     int i, j, n, d;
  251.     struct htable *p, *p2;
  252.  
  253.     d = n = 0;
  254.     for (p = htable, i = 0; i < HSIZE; ++p, ++i)
  255.         if (p->name) {
  256.             ++n;
  257.             j = 0;
  258.             for (p2 = p; p2; p2 = p2->next) {
  259.                 (void)fprintf(stderr,
  260.                     "%4d:%d 0x%08x%04x \"%s\"\n", i, j,
  261.                     p2->high, p2->low,
  262.                     p2->name ? p2->name : "<nil>");
  263.                 ++d;
  264.                 ++j;
  265.             }
  266.         }
  267.     d -= n;
  268.     (void)fprintf(stderr, "%d entries (%d dynamically linked)\n", n, d);
  269. }
  270. #endif
  271.