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 / nf.l < prev    next >
Text File  |  2009-10-10  |  6KB  |  319 lines

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