home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.lbl.gov / 2014.05.ftp.ee.lbl.gov.tar / ftp.ee.lbl.gov / ipsearch-1.0.tar.gz / ipsearch-1.0.tar / ipsearch-1.0 / ipsearch.l < prev    next >
Text File  |  2011-07-06  |  4KB  |  208 lines

  1. O              ([0-9]|[0-9][0-9]|[0-1][0-9][0-9]|2[0-4][0-9]|25[0-5])
  2. IP             {O}((\.{O}){3})
  3. S              [ .]
  4.  
  5.     #include <sys/types.h>
  6.     #include <sys/socket.h>
  7.  
  8.     #include <netinet/in.h>
  9.  
  10.     #include <arpa/inet.h>
  11.     #include <arpa/nameser.h>
  12.  
  13.     #include <ctype.h>
  14.     #include <netdb.h>
  15.     #include <stdio.h>
  16.     #include <string.h>
  17.     #include <time.h>
  18.     #include <unistd.h>
  19.  
  20.     #include "version.h"
  21.  
  22.     #ifdef notdef
  23.     #ifdef HAVE_OS_PROTO_H
  24.     #include "os-proto.h"
  25.     #endif
  26.     #endif
  27.  
  28.     #undef yywrap
  29.     #define YY_NO_UNPUT
  30.     #ifdef FLEX_SCANNER
  31.     #endif
  32.  
  33.     int yywrap(void);
  34.     int yylex(void);
  35.     void addr(const char *, size_t);
  36.     void chain2(const char *, size_t, int);
  37.     void summary(void);
  38.  
  39.     const char *p;
  40.     int i;
  41.     int debug = 0;
  42.     int timestamp = 0;
  43.     int stats = 0;
  44.     u_int lineno = 1;
  45.     const char *file = "?";
  46.     char buf[8192];
  47.     int matched = 0;
  48.     #define MATCH { \
  49.         ++matched; \
  50.         strcat(buf, yytext); \
  51.         BEGIN(INITIAL); \
  52.     }
  53.  
  54. %x ipaddr
  55.  
  56. %%
  57.  
  58. <*>{S}            {
  59.                 strcat(buf, yytext);
  60.             BEGIN(ipaddr);
  61.             }
  62.  
  63. <*>.            {
  64.             strcat(buf, yytext);
  65.                         BEGIN(INITIAL);
  66.             }
  67.  
  68. <*>\n            {
  69.             if (matched) {
  70.                 printf("%s\n", buf);
  71.                 matched = 0;
  72.             }
  73.             buf[0] = '\0';
  74.                         BEGIN(INITIAL);
  75.             }
  76.  
  77. %%
  78.  
  79. /*
  80.  * Copyright (c) 2011
  81.  *    The Regents of the University of California.  All rights reserved.
  82.  *
  83.  * Redistribution and use in source and binary forms, with or without
  84.  * modification, are permitted provided that: (1) source code distributions
  85.  * retain the above copyright notice and this paragraph in its entirety, (2)
  86.  * distributions including binary code include the above copyright notice and
  87.  * this paragraph in its entirety in the documentation or other materials
  88.  * provided with the distribution, and (3) all advertising materials mentioning
  89.  * features or use of this software display the following acknowledgement:
  90.  * ``This product includes software developed by the University of California,
  91.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  92.  * the University nor the names of its contributors may be used to endorse
  93.  * or promote products derived from this software without specific prior
  94.  * written permission.
  95.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  96.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  97.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  98.  */
  99.  
  100. #ifndef lint
  101. static const char copyright[] =
  102.     "@(#) Copyright (c) 2011\n\
  103. The Regents of the University of California.  All rights reserved.\n";
  104. static const char rcsid[] =
  105.     "@(#) $Id: ipsearch.l 4 2011-07-06 22:22:18Z leres $ (LBL)";
  106. #endif
  107.  
  108. char *prog;
  109.  
  110. int targc;
  111. char **targv;
  112.  
  113. extern char *optarg;
  114. extern int optind, opterr;
  115.  
  116. /* Forwards */
  117. int main(int, char **);
  118. void usage(void) __attribute__((noreturn));
  119.  
  120. int
  121. main(argc, argv)
  122.     int argc;
  123.     char **argv;
  124. {
  125.     char *cp;
  126.     int op;
  127.  
  128.     if (argv[0] == NULL)
  129.         prog = "hf";
  130.     else if ((cp = strrchr(argv[0], '/')) != NULL)
  131.         prog = cp + 1;
  132.     else
  133.         prog = argv[0];
  134.  
  135.     opterr = 0;
  136.     while ((op = getopt(argc, argv, "dts")) != EOF)
  137.         switch (op) {
  138.  
  139.         case 'd':
  140.             ++debug;
  141.             break;
  142.  
  143.         case 't':
  144.             ++timestamp;
  145.             break;
  146.  
  147.         case 's':
  148.             ++stats;
  149.             break;
  150.  
  151.         default:
  152.             usage();
  153.         }
  154.  
  155.     /* Let yywrap() figure out if there are any arguments to open */
  156.     targc = argc - optind;
  157.     targv = &argv[optind];
  158.     yyin = NULL;
  159.     (void)yywrap();
  160.  
  161.     /* Process file opened by yywrap() or stdin if no arguments */
  162.     if (yyin)
  163.         yylex();
  164.  
  165.     exit(0);
  166.     /* XXX */
  167.     yyunput(0, NULL);
  168. }
  169.  
  170. int
  171. yywrap()
  172. {
  173.     static int didany = 0;
  174.  
  175.     /* Close file, if necessary */
  176.     if (yyin) {
  177.         if (yyin != stdin)
  178.             (void)fclose(yyin);
  179.         yyin = NULL;
  180.     }
  181.  
  182.     /* Spin through arguments until we run out or successfully open one */
  183.     while (targc > 0) {
  184.         file = targv[0];
  185.         --targc;
  186.         ++targv;
  187.         ++didany;
  188.         if ((yyin = fopen(file, "r")) != NULL)
  189.             return (0);
  190.         perror(file);
  191.     }
  192.     if (!didany) {
  193.         ++didany;
  194.         yyin = stdin;
  195.         file = "<stdin>";
  196.         return (0);
  197.     }
  198.     return (1);
  199. }
  200.  
  201. void
  202. usage()
  203. {
  204.     (void)fprintf(stderr, "Version %s\n", version);
  205.     (void)fprintf(stderr, "usage: %s [-dts] [file ...]\n", prog);
  206.     exit(1);
  207. }
  208.