parser.c revision 1.10
1/*	$OpenBSD: parser.c,v 1.10 2007/02/22 03:32:40 reyk 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 <openssl/ssl.h>
36
37#include "hoststated.h"
38
39#include "parser.h"
40
41enum token_type {
42	NOTOKEN,
43	ENDTOKEN,
44	HOSTID,
45	TABLEID,
46	SERVICEID,
47	KEYWORD
48};
49
50struct token {
51	enum token_type		 type;
52	const char		*keyword;
53	int			 value;
54	const struct token	*next;
55};
56
57static const struct token t_main[];
58static const struct token t_show[];
59static const struct token t_service[];
60static const struct token t_table[];
61static const struct token t_host[];
62static const struct token t_service_id[];
63static const struct token t_table_id[];
64static const struct token t_host_id[];
65
66static const struct token t_main[] = {
67	{KEYWORD,	"monitor",	MONITOR,	NULL},
68	{KEYWORD,	"show",		NULL,		t_show},
69	{KEYWORD,	"stop",		SHUTDOWN,	NULL},
70	{KEYWORD,	"service",	NONE,		t_service},
71	{KEYWORD,	"table",	NONE,		t_table},
72	{KEYWORD,	"host",		NONE,		t_host},
73	{ENDTOKEN,	"",		NONE,		NULL}
74};
75
76static const struct token t_show[] = {
77	{KEYWORD,	"summary",	SHOW_SUM,	NULL},
78	{KEYWORD,	"hosts",	SHOW_HOSTS,	NULL},
79	{KEYWORD,	"relays",	SHOW_RELAYS,	NULL},
80	{ENDTOKEN,	"",		NONE,		NULL}
81};
82
83static const struct token t_service[] = {
84	{NOTOKEN,	"",		NONE,		NULL},
85	{KEYWORD,	"disable",	SERV_DISABLE,	t_service_id},
86	{KEYWORD,	"enable",	SERV_ENABLE,	t_service_id},
87	{ENDTOKEN,	"",		NONE,		NULL}
88};
89
90static const struct token t_table[] = {
91	{NOTOKEN,	"",		NONE,		NULL},
92	{KEYWORD,	"disable",	TABLE_DISABLE,	t_table_id},
93	{KEYWORD,	"enable",	TABLE_ENABLE,	t_table_id},
94	{ENDTOKEN,	"",		NONE,		NULL}
95};
96
97static const struct token t_host[] = {
98	{NOTOKEN,	"",		NONE,		NULL},
99	{KEYWORD,	"disable",	HOST_DISABLE,	t_host_id},
100	{KEYWORD,	"enable",	HOST_ENABLE,	t_host_id},
101	{ENDTOKEN,	"",		NONE,		NULL}
102};
103
104static const struct token t_service_id[] = {
105	{SERVICEID,	"",		NONE,		NULL},
106	{ENDTOKEN,	"",		NONE,		NULL}
107};
108
109static const struct token t_table_id[] = {
110	{TABLEID,	"",		NONE,		NULL},
111	{ENDTOKEN,	"",		NONE,		NULL}
112};
113
114static const struct token t_host_id[] = {
115	{HOSTID,	"",		NONE,		NULL},
116	{ENDTOKEN,	"",		NONE,		NULL}
117};
118
119static struct parse_result	res;
120
121struct parse_result *
122parse(int argc, char *argv[])
123{
124	const struct token	*table = t_main;
125	const struct token	*match;
126
127	bzero(&res, sizeof(res));
128
129	while (argc >= 0) {
130		if ((match = match_token(argv[0], table)) == NULL) {
131			fprintf(stderr, "valid commands/args:\n");
132			show_valid_args(table);
133			return (NULL);
134		}
135
136		argc--;
137		argv++;
138
139		if (match->type == NOTOKEN || match->next == NULL)
140			break;
141
142		table = match->next;
143	}
144
145	if (argc > 0) {
146		fprintf(stderr, "superfluous argument: %s\n", argv[0]);
147		return (NULL);
148	}
149
150	return (&res);
151}
152
153const struct token *
154match_token(const char *word, const struct token table[])
155{
156	u_int			 i, match;
157	const struct token	*t = NULL;
158	const char		*errstr;
159
160	match = 0;
161
162	for (i = 0; table[i].type != ENDTOKEN; i++) {
163		switch (table[i].type) {
164		case NOTOKEN:
165			if (word == NULL || strlen(word) == 0) {
166				match++;
167				t = &table[i];
168			}
169			break;
170		case KEYWORD:
171			if (word != NULL && strncmp(word, table[i].keyword,
172			    strlen(word)) == 0) {
173				match++;
174				t = &table[i];
175				if (t->value)
176					res.action = t->value;
177			}
178			break;
179		case HOSTID:
180			if (word == NULL)
181				break;
182			res.id.id = strtonum(word, 0, UINT_MAX, &errstr);
183			if (errstr) {
184				strlcpy(res.id.name, word, sizeof(res.id.name));
185				res.id.id = EMPTY_ID;
186			}
187			t = &table[i];
188			match++;
189			break;
190		case TABLEID:
191			if (word == NULL)
192				break;
193			res.id.id = strtonum(word, 0, UINT_MAX, &errstr);
194			if (errstr) {
195				strlcpy(res.id.name, word, sizeof(res.id.name));
196				res.id.id = EMPTY_ID;
197			}
198			t = &table[i];
199			match++;
200			break;
201		case SERVICEID:
202			if (word == NULL)
203				break;
204			res.id.id = strtonum(word, 0, UINT_MAX, &errstr);
205			if (errstr) {
206				strlcpy(res.id.name, word, sizeof(res.id.name));
207				res.id.id = EMPTY_ID;
208			}
209			t = &table[i];
210			match++;
211			break;
212		case ENDTOKEN:
213			break;
214		}
215	}
216
217	if (match != 1) {
218		if (word == NULL)
219			fprintf(stderr, "missing argument:\n");
220		else if (match > 1)
221			fprintf(stderr, "ambiguous argument: %s\n", word);
222		else if (match < 1)
223			fprintf(stderr, "unknown argument: %s\n", word);
224		return (NULL);
225	}
226
227	return (t);
228}
229
230void
231show_valid_args(const struct token table[])
232{
233	int	i;
234
235	for (i = 0; table[i].type != ENDTOKEN; i++) {
236		switch (table[i].type) {
237		case NOTOKEN:
238			fprintf(stderr, "  <cr>\n");
239			break;
240		case KEYWORD:
241			fprintf(stderr, "  %s\n", table[i].keyword);
242			break;
243		case SERVICEID:
244			fprintf(stderr, "  <serviceid>\n");
245			break;
246		case TABLEID:
247			fprintf(stderr, "  <tableid>\n");
248			break;
249		case HOSTID:
250			fprintf(stderr, "  <hostid>\n");
251			break;
252		case ENDTOKEN:
253			break;
254		}
255	}
256}
257