parser.c revision 1.3
1/*	$OpenBSD: parser.c,v 1.3 2006/12/16 17:53:03 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 "hostated.h"
36
37#include "parser.h"
38
39enum token_type {
40	NOTOKEN,
41	ENDTOKEN,
42	HOSTID,
43	TABLEID,
44	SERVICEID,
45	KEYWORD
46};
47
48struct token {
49	enum token_type		 type;
50	const char		*keyword;
51	int			 value;
52	const struct token	*next;
53};
54
55static const struct token t_main[];
56static const struct token t_service[];
57static const struct token t_table[];
58static const struct token t_host[];
59static const struct token t_service_id[];
60static const struct token t_table_id[];
61static const struct token t_host_id[];
62
63static 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
72static 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
79static 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
86static 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
93static const struct token t_service_id[] = {
94	{SERVICEID,	"",		NONE,		NULL},
95	{ENDTOKEN,	"",		NONE,		NULL}
96};
97
98static const struct token t_table_id[] = {
99	{TABLEID,	"",		NONE,		NULL},
100	{ENDTOKEN,	"",		NONE,		NULL}
101};
102
103static const struct token t_host_id[] = {
104	{HOSTID,	"",		NONE,		NULL},
105	{ENDTOKEN,	"",		NONE,		NULL}
106};
107
108static struct parse_result	res;
109
110struct parse_result *
111parse(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
142const struct token *
143match_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 = strtonum(word, 0, UINT_MAX, &errstr);
170			if (errstr)
171				errx(1, "host id %s is %s", word, errstr);
172			t = &table[i];
173			match++;
174			break;
175		case TABLEID:
176			res.id = strtonum(word, 0, UINT_MAX, &errstr);
177			if (errstr)
178				errx(1, "table id %s is %s", word, errstr);
179			t = &table[i];
180			match++;
181			break;
182		case SERVICEID:
183			res.id = strtonum(word, 0, UINT_MAX, &errstr);
184			if (errstr)
185				errx(1, "service id %s is %s", word, errstr);
186			t = &table[i];
187			match++;
188			break;
189		case ENDTOKEN:
190			break;
191		}
192	}
193
194	if (match != 1) {
195		if (match > 1)
196			fprintf(stderr, "ambiguous argument: %s\n", word);
197		if (match < 1)
198			fprintf(stderr, "unknown argument: %s\n", word);
199		return (NULL);
200	}
201
202	return (t);
203}
204
205void
206show_valid_args(const struct token table[])
207{
208	int	i;
209
210	for (i = 0; table[i].type != ENDTOKEN; i++) {
211		switch (table[i].type) {
212		case NOTOKEN:
213			fprintf(stderr, "  <cr>\n");
214			break;
215		case KEYWORD:
216			fprintf(stderr, "  %s\n", table[i].keyword);
217			break;
218		case SERVICEID:
219			fprintf(stderr, "  <serviceid>\n");
220			break;
221		case TABLEID:
222			fprintf(stderr, "  <tableid>\n");
223			break;
224		case HOSTID:
225			fprintf(stderr, "  <hostid>\n");
226			break;
227		case ENDTOKEN:
228			break;
229		}
230	}
231}
232