xref: /openbsd-src/usr.sbin/ospfctl/parser.c (revision 2090e587e396fa6aa6d6dd325ec2f660e606a84e)
1 /*	$OpenBSD: parser.c,v 1.2 2005/01/28 17:26:05 norby Exp $ */
2 
3 /*
4  * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
5  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include "parser.h"
32 
33 enum token_type {
34 	NOTOKEN,
35 	ENDTOKEN,
36 	KEYWORD,
37 	ADDRESS,
38 	FLAG,
39 	PREFIX,
40 	IFNAME
41 };
42 
43 struct token {
44 	enum token_type		 type;
45 	const char		*keyword;
46 	int			 value;
47 	const struct token	*next;
48 };
49 
50 static const struct token t_main[];
51 static const struct token t_show[];
52 static const struct token t_show_iface[];
53 static const struct token t_show_db[];
54 static const struct token t_show_area[];
55 static const struct token t_show_nbr[];
56 
57 static const struct token t_main[] = {
58 /*	{KEYWORD,	"reload",	RELOAD,		NULL}, */
59 	{KEYWORD,	"show",		SHOW,		t_show},
60 	{ENDTOKEN,	"",		NONE,		NULL}
61 };
62 
63 static const struct token t_show[] = {
64 	{NOTOKEN,	"",		NONE,		NULL},
65 	{KEYWORD,	"interfaces",	SHOW_IFACE,	t_show_iface},
66 	{KEYWORD,	"database",	SHOW_DB,	t_show_db},
67 	{KEYWORD,	"neighbor",	SHOW_NBR,	t_show_nbr},
68 /*	{KEYWORD,	"summary",	SHOW_SUMMARY,	NULL}, */
69 	{ENDTOKEN,	"",		NONE,		NULL}
70 };
71 
72 static const struct token t_show_iface[] = {
73 	{NOTOKEN,	"",		NONE,		NULL},
74 	{IFNAME,	"",		NONE,		NULL},
75 	{ENDTOKEN,	"",		NONE,		NULL}
76 };
77 
78 static const struct token t_show_db[] = {
79 	{NOTOKEN,	"",		NONE,		NULL},
80 	{KEYWORD,	"area",		SHOW_DBBYAREA,	t_show_area},
81 /*	{KEYWORD,	"router",	NONE,		NULL},
82 	{KEYWORD,	"network",	NONE,		NULL}, */
83 	{ENDTOKEN,	"",		NONE,		NULL}
84 };
85 
86 static const struct token t_show_area[] = {
87 	{ADDRESS,	"",		NONE,		NULL},
88 	{ENDTOKEN,	"",		NONE,		NULL}
89 };
90 
91 static const struct token t_show_nbr[] = {
92 	{NOTOKEN,	"",		NONE,		NULL},
93 	{KEYWORD,	"detail",	SHOW_NBR_DTAIL,	NULL},
94 	{ENDTOKEN,	"",		NONE,		NULL}
95 };
96 
97 static struct parse_result	res;
98 
99 struct parse_result *
100 parse(int argc, char *argv[])
101 {
102 	const struct token	*table = t_main;
103 	const struct token	*match;
104 
105 	bzero(&res, sizeof(res));
106 
107 	while (argc > 0) {
108 		if ((match = match_token(argv[0], table)) == NULL) {
109 			fprintf(stderr, "valid commands/args:\n");
110 			show_valid_args(table);
111 			return (NULL);
112 		}
113 
114 		argc--;
115 		argv++;
116 
117 		if (match->type == NOTOKEN || match->next == NULL)
118 			break;
119 
120 		table = match->next;
121 	}
122 
123 	if (argc > 0) {
124 		fprintf(stderr, "superfluous argument: %s\n", argv[0]);
125 		return (NULL);
126 	}
127 
128 	return (&res);
129 }
130 
131 const struct token *
132 match_token(const char *word, const struct token table[])
133 {
134 	u_int			 i, match;
135 	const struct token	*t = NULL;
136 
137 	match = 0;
138 
139 	for (i = 0; table[i].type != ENDTOKEN; i++) {
140 		switch (table[i].type) {
141 		case NOTOKEN:
142 			if (word == NULL || strlen(word) == 0) {
143 				match++;
144 				t = &table[i];
145 			}
146 			break;
147 		case KEYWORD:
148 			if (word != NULL && strncmp(word, table[i].keyword,
149 			    strlen(word)) == 0) {
150 				match++;
151 				t = &table[i];
152 				if (t->value)
153 					res.action = t->value;
154 			}
155 			break;
156 		case FLAG:
157 			if (word != NULL && strncmp(word, table[i].keyword,
158 			    strlen(word)) == 0) {
159 				match++;
160 				t = &table[i];
161 				res.flags |= t->value;
162 			}
163 			break;
164 		case ADDRESS:
165 			if (parse_addr(word, &res.addr)) {
166 				match++;
167 				t = &table[i];
168 				if (t->value)
169 					res.action = t->value;
170 			}
171 			break;
172 		case PREFIX:
173 			if (parse_prefix(word, &res.addr, &res.prefixlen)) {
174 				match++;
175 				t = &table[i];
176 				if (t->value)
177 					res.action = t->value;
178 			}
179 			break;
180 		case IFNAME:
181 			if (!match && word != NULL && strlen(word) > 0) {
182 				if (strlcpy(res.ifname, word,
183 				    sizeof(res.ifname)) >=
184 				    sizeof(res.ifname))
185 					err(1, "interface name too long");
186 				match++;
187 				t = &table[i];
188 			}
189 			break;
190 
191 		case ENDTOKEN:
192 			break;
193 		}
194 	}
195 
196 	if (match != 1) {
197 		if (match > 1)
198 			fprintf(stderr, "ambiguous argument: %s\n", word);
199 		if (match < 1)
200 			fprintf(stderr, "unknown argument: %s\n", word);
201 		return (NULL);
202 	}
203 
204 	return (t);
205 }
206 
207 void
208 show_valid_args(const struct token table[])
209 {
210 	int	i;
211 
212 	for (i = 0; table[i].type != ENDTOKEN; i++) {
213 		switch (table[i].type) {
214 		case NOTOKEN:
215 			fprintf(stderr, "  <cr>\n");
216 			break;
217 		case KEYWORD:
218 		case FLAG:
219 			fprintf(stderr, "  %s\n", table[i].keyword);
220 			break;
221 		case ADDRESS:
222 			fprintf(stderr, "  <address>\n");
223 			break;
224 		case PREFIX:
225 			fprintf(stderr, "  <address>[/<len>]\n");
226 			break;
227 		case IFNAME:
228 			fprintf(stderr, "  <interface>\n");
229 		case ENDTOKEN:
230 			break;
231 		}
232 	}
233 }
234 
235 int
236 parse_addr(const char *word, struct in_addr *addr)
237 {
238 	struct in_addr	ina;
239 
240 	if (word == NULL)
241 		return (0);
242 
243 	bzero(addr, sizeof(struct in_addr));
244 	bzero(&ina, sizeof(ina));
245 
246 	if (inet_pton(AF_INET, word, &ina)) {
247 		addr->s_addr = ina.s_addr;
248 		return (1);
249 	}
250 
251 	return (0);
252 }
253 
254 int
255 parse_prefix(const char *word, struct in_addr *addr, u_int8_t *prefixlen)
256 {
257 	struct in_addr	 ina;
258 	int		 bits = 32;
259 
260 	if (word == NULL)
261 		return (0);
262 
263 	bzero(addr, sizeof(struct in_addr));
264 	bzero(&ina, sizeof(ina));
265 
266 	if (strrchr(word, '/') != NULL) {
267 		if ((bits = inet_net_pton(AF_INET, word,
268 		    &ina, sizeof(ina))) == -1)
269 			return (0);
270 		addr->s_addr = ina.s_addr & htonl(0xffffffff << (32 - bits));
271 		*prefixlen = bits;
272 		return (1);
273 	} else {
274 		*prefixlen = 32;
275 		return (parse_addr(word, addr));
276 	}
277 
278 	return (0);
279 }
280