xref: /openbsd-src/usr.sbin/relayctl/parser.c (revision daf88648c0e349d5c02e1504293082072c981640)
1 /*	$OpenBSD: parser.c,v 1.5 2007/01/09 00:45:32 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@spootnik.org>
5  * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
6  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <sys/queue.h>
24 #include <netinet/in.h>
25 #include <net/if.h>
26 #include <arpa/inet.h>
27 #include <err.h>
28 #include <errno.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <event.h>
34 
35 #include "hoststated.h"
36 
37 #include "parser.h"
38 
39 enum token_type {
40 	NOTOKEN,
41 	ENDTOKEN,
42 	HOSTID,
43 	TABLEID,
44 	SERVICEID,
45 	KEYWORD
46 };
47 
48 struct token {
49 	enum token_type		 type;
50 	const char		*keyword;
51 	int			 value;
52 	const struct token	*next;
53 };
54 
55 static const struct token t_main[];
56 static const struct token t_service[];
57 static const struct token t_table[];
58 static const struct token t_host[];
59 static const struct token t_service_id[];
60 static const struct token t_table_id[];
61 static const struct token t_host_id[];
62 
63 static const struct token t_main[] = {
64 	{KEYWORD,	"show",		SHOW_SUM,	NULL},
65 	{KEYWORD,	"stop",		SHUTDOWN,	NULL},
66 	{KEYWORD,	"service",	NULL,		t_service},
67 	{KEYWORD,	"table",	NULL,		t_table},
68 	{KEYWORD,	"host",		NULL,		t_host},
69 	{ENDTOKEN,	"",		NONE,		NULL}
70 };
71 
72 static const struct token t_service[] = {
73 	{NOTOKEN,	"",		NONE,		NULL},
74 	{KEYWORD,	"disable",	SERV_DISABLE,	t_service_id},
75 	{KEYWORD,	"enable",	SERV_ENABLE,	t_service_id},
76 	{ENDTOKEN,	"",		NONE,		NULL}
77 };
78 
79 static const struct token t_table[] = {
80 	{NOTOKEN,	"",		NONE,		NULL},
81 	{KEYWORD,	"disable",	TABLE_DISABLE,	t_table_id},
82 	{KEYWORD,	"enable",	TABLE_ENABLE,	t_table_id},
83 	{ENDTOKEN,	"",		NONE,		NULL}
84 };
85 
86 static const struct token t_host[] = {
87 	{NOTOKEN,	"",		NONE,		NULL},
88 	{KEYWORD,	"disable",	HOST_DISABLE,	t_host_id},
89 	{KEYWORD,	"enable",	HOST_ENABLE,	t_host_id},
90 	{ENDTOKEN,	"",		NONE,		NULL}
91 };
92 
93 static const struct token t_service_id[] = {
94 	{SERVICEID,	"",		NONE,		NULL},
95 	{ENDTOKEN,	"",		NONE,		NULL}
96 };
97 
98 static const struct token t_table_id[] = {
99 	{TABLEID,	"",		NONE,		NULL},
100 	{ENDTOKEN,	"",		NONE,		NULL}
101 };
102 
103 static const struct token t_host_id[] = {
104 	{HOSTID,	"",		NONE,		NULL},
105 	{ENDTOKEN,	"",		NONE,		NULL}
106 };
107 
108 static struct parse_result	res;
109 
110 struct parse_result *
111 parse(int argc, char *argv[])
112 {
113 	const struct token	*table = t_main;
114 	const struct token	*match;
115 
116 	bzero(&res, sizeof(res));
117 
118 	while (argc > 0) {
119 		if ((match = match_token(argv[0], table)) == NULL) {
120 			fprintf(stderr, "valid commands/args:\n");
121 			show_valid_args(table);
122 			return (NULL);
123 		}
124 
125 		argc--;
126 		argv++;
127 
128 		if (match->type == NOTOKEN || match->next == NULL)
129 			break;
130 
131 		table = match->next;
132 	}
133 
134 	if (argc > 0) {
135 		fprintf(stderr, "superfluous argument: %s\n", argv[0]);
136 		return (NULL);
137 	}
138 
139 	return (&res);
140 }
141 
142 const struct token *
143 match_token(const char *word, const struct token table[])
144 {
145 	u_int			 i, match;
146 	const struct token	*t = NULL;
147 	const char		*errstr;
148 
149 	match = 0;
150 
151 	for (i = 0; table[i].type != ENDTOKEN; i++) {
152 		switch (table[i].type) {
153 		case NOTOKEN:
154 			if (word == NULL || strlen(word) == 0) {
155 				match++;
156 				t = &table[i];
157 			}
158 			break;
159 		case KEYWORD:
160 			if (word != NULL && strncmp(word, table[i].keyword,
161 			    strlen(word)) == 0) {
162 				match++;
163 				t = &table[i];
164 				if (t->value)
165 					res.action = t->value;
166 			}
167 			break;
168 		case HOSTID:
169 			res.id.id = strtonum(word, 0, UINT_MAX, &errstr);
170 			if (errstr) {
171 				strlcpy(res.id.name, word, sizeof(res.id.name));
172 				res.id.id = EMPTY_ID;
173 			}
174 			t = &table[i];
175 			match++;
176 			break;
177 		case TABLEID:
178 			res.id.id = strtonum(word, 0, UINT_MAX, &errstr);
179 			if (errstr) {
180 				strlcpy(res.id.name, word, sizeof(res.id.name));
181 				res.id.id = EMPTY_ID;
182 			}
183 			t = &table[i];
184 			match++;
185 			break;
186 		case SERVICEID:
187 			res.id.id = strtonum(word, 0, UINT_MAX, &errstr);
188 			if (errstr) {
189 				strlcpy(res.id.name, word, sizeof(res.id.name));
190 				res.id.id = EMPTY_ID;
191 			}
192 			t = &table[i];
193 			match++;
194 			break;
195 		case ENDTOKEN:
196 			break;
197 		}
198 	}
199 
200 	if (match != 1) {
201 		if (match > 1)
202 			fprintf(stderr, "ambiguous argument: %s\n", word);
203 		if (match < 1)
204 			fprintf(stderr, "unknown argument: %s\n", word);
205 		return (NULL);
206 	}
207 
208 	return (t);
209 }
210 
211 void
212 show_valid_args(const struct token table[])
213 {
214 	int	i;
215 
216 	for (i = 0; table[i].type != ENDTOKEN; i++) {
217 		switch (table[i].type) {
218 		case NOTOKEN:
219 			fprintf(stderr, "  <cr>\n");
220 			break;
221 		case KEYWORD:
222 			fprintf(stderr, "  %s\n", table[i].keyword);
223 			break;
224 		case SERVICEID:
225 			fprintf(stderr, "  <serviceid>\n");
226 			break;
227 		case TABLEID:
228 			fprintf(stderr, "  <tableid>\n");
229 			break;
230 		case HOSTID:
231 			fprintf(stderr, "  <hostid>\n");
232 			break;
233 		case ENDTOKEN:
234 			break;
235 		}
236 	}
237 }
238