xref: /openbsd-src/usr.sbin/unwindctl/parser.c (revision c071f09071f839d58d840d707112a4e1152c6f24)
1 /*	$OpenBSD: parser.c,v 1.11 2019/12/18 09:18:28 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 
55 static const struct token t_main[] = {
56 	{KEYWORD,	"reload",	RELOAD,		NULL},
57 	{KEYWORD,	"status",	STATUS,		t_status},
58 	{KEYWORD,	"log",		NONE,		t_log},
59 	{ENDTOKEN,	"",		NONE,		NULL}
60 };
61 
62 static const struct token t_log[] = {
63 	{KEYWORD,	"debug",	LOG_DEBUG,	NULL},
64 	{KEYWORD,	"verbose",	LOG_VERBOSE,	NULL},
65 	{KEYWORD,	"brief",	LOG_BRIEF,	NULL},
66 	{ENDTOKEN,	"",		NONE,		NULL}
67 };
68 
69 static const struct token t_status[] = {
70 	{NOTOKEN,	"",		NONE,		NULL},
71 	{KEYWORD,	"autoconf",	AUTOCONF,	NULL},
72 	{KEYWORD,	"memory",	MEM,		NULL},
73 	{ENDTOKEN,	"",		NONE,		NULL}
74 };
75 
76 static const struct token *match_token(const char *, const struct token *,
77     struct parse_result *);
78 static void show_valid_args(const struct token *);
79 
80 struct parse_result *
parse(int argc,char * argv[])81 parse(int argc, char *argv[])
82 {
83 	static struct parse_result	res;
84 	const struct token	*table = t_main;
85 	const struct token	*match;
86 
87 	memset(&res, 0, sizeof(res));
88 
89 	while (argc >= 0) {
90 		if ((match = match_token(argv[0], table, &res)) == NULL) {
91 			fprintf(stderr, "valid commands/args:\n");
92 			show_valid_args(table);
93 			return (NULL);
94 		}
95 
96 		argc--;
97 		argv++;
98 
99 		if (match->type == NOTOKEN || match->next == NULL)
100 			break;
101 
102 		table = match->next;
103 	}
104 
105 	if (argc > 0) {
106 		fprintf(stderr, "superfluous argument: %s\n", argv[0]);
107 		return (NULL);
108 	}
109 
110 	return (&res);
111 }
112 
113 static const struct token *
match_token(const char * word,const struct token * table,struct parse_result * res)114 match_token(const char *word, const struct token *table,
115     struct parse_result *res)
116 {
117 	u_int			 i, match;
118 	const struct token	*t = NULL;
119 
120 	match = 0;
121 
122 	for (i = 0; table[i].type != ENDTOKEN; i++) {
123 		switch (table[i].type) {
124 		case NOTOKEN:
125 			if (word == NULL || strlen(word) == 0) {
126 				match++;
127 				t = &table[i];
128 			}
129 			break;
130 		case KEYWORD:
131 			if (word != NULL && strncmp(word, table[i].keyword,
132 			    strlen(word)) == 0) {
133 				match++;
134 				t = &table[i];
135 				if (t->value)
136 					res->action = t->value;
137 			}
138 			break;
139 		case ENDTOKEN:
140 			break;
141 		}
142 	}
143 
144 	if (match != 1) {
145 		if (word == NULL)
146 			fprintf(stderr, "missing argument:\n");
147 		else if (match > 1)
148 			fprintf(stderr, "ambiguous argument: %s\n", word);
149 		else if (match < 1)
150 			fprintf(stderr, "unknown argument: %s\n", word);
151 		return (NULL);
152 	}
153 
154 	return (t);
155 }
156 
157 static void
show_valid_args(const struct token * table)158 show_valid_args(const struct token *table)
159 {
160 	int	i;
161 
162 	for (i = 0; table[i].type != ENDTOKEN; i++) {
163 		switch (table[i].type) {
164 		case NOTOKEN:
165 			fprintf(stderr, "  <cr>\n");
166 			break;
167 		case KEYWORD:
168 			fprintf(stderr, "  %s\n", table[i].keyword);
169 			break;
170 		case ENDTOKEN:
171 			break;
172 		}
173 	}
174 }
175