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 / pf.l < prev    next >
Text File  |  2009-10-10  |  4KB  |  203 lines

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