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_INPUT
17 #define YY_NO_UNPUT
18 #endif
19 int yywrap(void);
20 int yylex(void);
21 void convert(char *);
22
23 %%
24
25 "["{N}+"]" convert(yytext);
26 [^0-9[\]\n]+\n? ECHO;
27 .|\n ECHO;
28
29 %%
30
31 /*
32 * Copyright (c) 1990, 1991, 1996, 1999, 2000, 2004, 2005, 2009, 2014, 2017
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions are met:
37 * * Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * * Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * * Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
47 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 */
58
59 #ifndef lint
60 static const char copyright[] __attribute__ ((unused)) =
61 "@(#) Copyright (c) 1990, 1991, 1996, 1999, 2000, 2004, 2005, 2009, 2014, 2017\n\
62 The Regents of the University of California. All rights reserved.\n";
63 static const char rcsid[] __attribute__ ((unused)) =
64 "@(#) $Id: pf.l 210 2017-10-20 23:30:56Z leres $ (LBL)";
65 #endif
66
67 #ifdef DEBUG
68 int debug = 0;
69 #endif
70
71 #define MAX_PORT_NUM 65535
72 char *port_to_name[MAX_PORT_NUM+1];
73
74 int targc;
75 char **targv;
76
77 extern char *optarg;
78 extern int optind, opterr;
79
80 /* Forwards */
81 int main(int, char **);
82 char *dup_string(char *);
83 void portinit(void);
84
85 int
main(argc,argv)86 main(argc, argv)
87 int argc;
88 char **argv;
89 {
90 char *cp;
91 int op;
92 char *prog;
93
94 if (argv[0] == NULL)
95 prog = "hf";
96 else if ((cp = strrchr(argv[0], '/')) != NULL)
97 prog = cp + 1;
98 else
99 prog = argv[0];
100
101 opterr = 0;
102 while ((op = getopt(argc, argv, "d")) != EOF)
103 switch (op) {
104
105 #ifdef DEBUG
106 case 'd':
107 ++debug;
108 break;
109 #endif
110
111 default:
112 (void)fprintf(stderr, "usage: %s [-d] [file ...]\n",
113 prog);
114 exit(1);
115 /* NOTREACHED */
116 }
117
118 /* Let yywrap() figure out if there are any arguments to open */
119 targc = argc - optind;
120 targv = &argv[optind];
121 yyin = 0;
122 (void)yywrap();
123
124 portinit();
125
126 /* Process file opened by yywrap() or stdin if no arguments */
127 if (yyin)
128 yylex();
129
130 #ifdef DEBUG
131 if (debug) {
132 int i;
133 for (i=0; i <= MAX_PORT_NUM; ++i)
134 if (port_to_name[i])
135 fprintf(stderr, "[%d]\t%s\n", i,
136 port_to_name[i]);
137 }
138 #endif /* DEBUG */
139 exit(0);
140 }
141
142 int
yywrap()143 yywrap()
144 {
145 char *file;
146 static int didany = 0;
147
148 /* Close file, if necessary */
149 if (yyin) {
150 if (yyin != stdin)
151 (void)fclose(yyin);
152 yyin = NULL;
153 }
154
155 /* Spin through arguments until we run out or successfully open one */
156 while (targc > 0) {
157 file = targv[0];
158 --targc;
159 ++targv;
160 ++didany;
161 if ((yyin = fopen(file, "r")) != NULL)
162 return (0);
163 perror(file);
164 }
165 if (!didany) {
166 ++didany;
167 yyin = stdin;
168 return (0);
169 }
170 return (1);
171 }
172
173 char *
dup_string(src)174 dup_string(src)
175 char *src;
176 {
177 char *dst;
178
179 dst = malloc(strlen(src)+1);
180 if (dst)
181 strcpy(dst, src);
182 return dst;
183 }
184
185 void
convert(str)186 convert(str)
187 char *str;
188 {
189 int port;
190
191 port = atoi(str+1);
192 if (port >= 0 && port <= MAX_PORT_NUM && port_to_name[port] != 0)
193 str = port_to_name[port];
194 fputs(str, stdout);
195 }
196
197 void
portinit()198 portinit()
199 {
200 struct servent *sp;
201
202 while ((sp = getservent()) != 0) {
203 if (port_to_name[sp->s_port] == 0 ||
204 sp->s_proto[0] == 't')
205 port_to_name[sp->s_port] = dup_string(sp->s_name);
206 }
207 endservent();
208
209 }
210