xref: /openbsd-src/usr.sbin/unwindctl/parser.c (revision 9f11ffb7133c203312a01e4b986886bc88c7d74b)
1 /*	$OpenBSD: parser.c,v 1.3 2019/02/03 12:02:30 florian 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/queue.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <net/if.h>
26 #include <err.h>
27 #include <errno.h>
28 #include <event.h>
29 #include <imsg.h>
30 #include <limits.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 #include "unwind.h"
36 #include "parser.h"
37 
38 enum token_type {
39 	NOTOKEN,
40 	ENDTOKEN,
41 	KEYWORD
42 };
43 
44 struct token {
45 	enum token_type		 type;
46 	const char		*keyword;
47 	int			 value;
48 	const struct token	*next;
49 };
50 
51 static const struct token t_main[];
52 static const struct token t_log[];
53 static const struct token t_status[];
54 static const struct token t_recheck[];
55 
56 static const struct token t_main[] = {
57 	{KEYWORD,	"reload",	RELOAD,		NULL},
58 	{KEYWORD,	"status",	STATUS,		t_status},
59 	{KEYWORD,	"log",		NONE,		t_log},
60 	{KEYWORD,	"recheck",	NONE,		t_recheck},
61 	{ENDTOKEN,	"",		NONE,		NULL}
62 };
63 
64 static const struct token t_log[] = {
65 	{KEYWORD,	"debug",	LOG_DEBUG,	NULL},
66 	{KEYWORD,	"verbose",	LOG_VERBOSE,	NULL},
67 	{KEYWORD,	"brief",	LOG_BRIEF,	NULL},
68 	{ENDTOKEN,	"",		NONE,		NULL}
69 };
70 
71 static const struct token t_status[] = {
72 	{NOTOKEN,	"",		NONE,			NULL},
73 	{KEYWORD,	"recursor",	STATUS_RECURSOR,	NULL},
74 	{KEYWORD,	"dhcp",		STATUS_DHCP,		NULL},
75 	{KEYWORD,	"static",	STATUS_STATIC,		NULL},
76 	{KEYWORD,	"DoT",		STATUS_DOT,		NULL},
77 	{ENDTOKEN,	"",		STATUS,			NULL}
78 };
79 
80 static const struct token t_recheck[] = {
81 	{KEYWORD,	"portal",	PORTAL,		NULL},
82 	{ENDTOKEN,	"",		NONE,		NULL}
83 };
84 
85 static const struct token *match_token(const char *, const struct token *,
86     struct parse_result *);
87 static void show_valid_args(const struct token *);
88 
89 struct parse_result *
90 parse(int argc, char *argv[])
91 {
92 	static struct parse_result	res;
93 	const struct token	*table = t_main;
94 	const struct token	*match;
95 
96 	memset(&res, 0, sizeof(res));
97 
98 	while (argc >= 0) {
99 		if ((match = match_token(argv[0], table, &res)) == NULL) {
100 			fprintf(stderr, "valid commands/args:\n");
101 			show_valid_args(table);
102 			return (NULL);
103 		}
104 
105 		argc--;
106 		argv++;
107 
108 		if (match->type == NOTOKEN || match->next == NULL)
109 			break;
110 
111 		table = match->next;
112 	}
113 
114 	if (argc > 0) {
115 		fprintf(stderr, "superfluous argument: %s\n", argv[0]);
116 		return (NULL);
117 	}
118 
119 	return (&res);
120 }
121 
122 static const struct token *
123 match_token(const char *word, const struct token *table,
124     struct parse_result *res)
125 {
126 	u_int			 i, match;
127 	const struct token	*t = NULL;
128 
129 	match = 0;
130 
131 	for (i = 0; table[i].type != ENDTOKEN; i++) {
132 		switch (table[i].type) {
133 		case NOTOKEN:
134 			if (word == NULL || strlen(word) == 0) {
135 				match++;
136 				t = &table[i];
137 			}
138 			break;
139 		case KEYWORD:
140 			if (word != NULL && strncmp(word, table[i].keyword,
141 			    strlen(word)) == 0) {
142 				match++;
143 				t = &table[i];
144 				if (t->value)
145 					res->action = t->value;
146 			}
147 			break;
148 		case ENDTOKEN:
149 			break;
150 		}
151 	}
152 
153 	if (match != 1) {
154 		if (word == NULL)
155 			fprintf(stderr, "missing argument:\n");
156 		else if (match > 1)
157 			fprintf(stderr, "ambiguous argument: %s\n", word);
158 		else if (match < 1)
159 			fprintf(stderr, "unknown argument: %s\n", word);
160 		return (NULL);
161 	}
162 
163 	return (t);
164 }
165 
166 static void
167 show_valid_args(const struct token *table)
168 {
169 	int	i;
170 
171 	for (i = 0; table[i].type != ENDTOKEN; i++) {
172 		switch (table[i].type) {
173 		case NOTOKEN:
174 			fprintf(stderr, "  <cr>\n");
175 			break;
176 		case KEYWORD:
177 			fprintf(stderr, "  %s\n", table[i].keyword);
178 			break;
179 		case ENDTOKEN:
180 			break;
181 		}
182 	}
183 }
184