parser.c revision 1.1
1/*	$OpenBSD: parser.c,v 1.1 2006/12/16 11:45:07 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 "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_show[];
57static const struct token t_service[];
58static const struct token t_table[];
59static const struct token t_host[];
60static const struct token t_service_id[];
61static const struct token t_table_id[];
62static const struct token t_host_id[];
63
64static const struct token t_main[] = {
65	{KEYWORD,	"show",		SHOW_SUM,	NULL},
66	{KEYWORD,	"stop",		SHUTDOWN,	NULL},
67	{KEYWORD,	"service",	NULL,		t_service},
68	{KEYWORD,	"table",	NULL,		t_table},
69	{KEYWORD,	"host",		NULL,		t_host},
70	{ENDTOKEN,	"",		NONE,		NULL}
71};
72
73static const struct token t_service[] = {
74	{NOTOKEN,	"",		NONE,		NULL},
75	{KEYWORD,	"disable",	SERV_DISABLE, 	t_service_id},
76	{KEYWORD,	"enable",	SERV_ENABLE, 	t_service_id},
77	{ENDTOKEN,	"",		NONE,		NULL}
78};
79
80static const struct token t_table[] = {
81	{NOTOKEN,	"",		NONE,		NULL},
82	{KEYWORD,	"disable",	TABLE_DISABLE, 	t_table_id},
83	{KEYWORD,	"enable",	TABLE_ENABLE,	t_table_id},
84	{ENDTOKEN,	"",		NONE,		NULL}
85};
86
87static const struct token t_host[] = {
88	{NOTOKEN,	"",		NONE,		NULL},
89	{KEYWORD,	"disable",	HOST_DISABLE, 	t_host_id},
90	{KEYWORD,	"enable",	HOST_ENABLE,	t_host_id},
91	{ENDTOKEN,	"",		NONE,		NULL}
92};
93
94static const struct token t_service_id[] = {
95	{SERVICEID,	"",		NONE,		NULL},
96	{ENDTOKEN,	"",		NONE,		NULL}
97};
98
99static const struct token t_table_id[] = {
100	{TABLEID,	"",		NONE,		NULL},
101	{ENDTOKEN,	"",		NONE,		NULL}
102};
103
104static const struct token t_host_id[] = {
105	{HOSTID,	"",		NONE,		NULL},
106	{ENDTOKEN,	"",		NONE,		NULL}
107};
108
109static struct parse_result	res;
110
111struct parse_result *
112parse(int argc, char *argv[])
113{
114	const struct token	*table = t_main;
115	const struct token	*match;
116
117	bzero(&res, sizeof(res));
118
119	while (argc > 0) {
120		if ((match = match_token(argv[0], table)) == NULL) {
121			fprintf(stderr, "valid commands/args:\n");
122			show_valid_args(table);
123			return (NULL);
124		}
125
126		argc--;
127		argv++;
128
129		if (match->type == NOTOKEN || match->next == NULL)
130			break;
131
132		table = match->next;
133	}
134
135	if (argc > 0) {
136		fprintf(stderr, "superfluous argument: %s\n", argv[0]);
137		return (NULL);
138	}
139
140	return (&res);
141}
142
143const struct token *
144match_token(const char *word, const struct token table[])
145{
146	u_int			 i, match;
147	const struct token	*t = NULL;
148	const char		*errstr;
149
150	match = 0;
151
152	for (i = 0; table[i].type != ENDTOKEN; i++) {
153		switch (table[i].type) {
154		case NOTOKEN:
155			if (word == NULL || strlen(word) == 0) {
156				match++;
157				t = &table[i];
158			}
159			break;
160		case KEYWORD:
161			if (word != NULL && strncmp(word, table[i].keyword,
162			    strlen(word)) == 0) {
163				match++;
164				t = &table[i];
165				if (t->value)
166					res.action = t->value;
167			}
168			break;
169		case HOSTID:
170			res.id = strtonum(word, 0, UINT_MAX, &errstr);
171			if (errstr)
172				errx(1, "host id %s is %s", word, errstr);
173			t = &table[i];
174			match++;
175			break;
176		case TABLEID:
177			res.id = strtonum(word, 0, UINT_MAX, &errstr);
178			if (errstr)
179				errx(1, "table id %s is %s", word, errstr);
180			t = &table[i];
181			match++;
182			break;
183		case SERVICEID:
184			res.id = strtonum(word, 0, UINT_MAX, &errstr);
185			if (errstr)
186				errx(1, "service id %s is %s", word, errstr);
187			t = &table[i];
188			match++;
189			break;
190		case ENDTOKEN:
191			break;
192		}
193	}
194
195	if (match != 1) {
196		if (match > 1)
197			fprintf(stderr, "ambiguous argument: %s\n", word);
198		if (match < 1)
199			fprintf(stderr, "unknown argument: %s\n", word);
200		return (NULL);
201	}
202
203	return (t);
204}
205
206void
207show_valid_args(const struct token table[])
208{
209	int	i;
210
211	for (i = 0; table[i].type != ENDTOKEN; i++) {
212		switch (table[i].type) {
213		case NOTOKEN:
214			fprintf(stderr, "  <cr>\n");
215			break;
216		case KEYWORD:
217			fprintf(stderr, "  %s\n", table[i].keyword);
218			break;
219		case SERVICEID:
220			fprintf(stderr, "  <serviceid>\n");
221			break;
222		case TABLEID:
223			fprintf(stderr, "  <tableid>\n");
224			break;
225		case HOSTID:
226			fprintf(stderr, "  <hostid>\n");
227			break;
228		case ENDTOKEN:
229			break;
230		}
231	}
232}
233