1 /* $OpenBSD: parser.c,v 1.2 2023/04/19 12:58:16 jsg 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
26 #include <net/if.h>
27 #include <netinet/if_ether.h>
28
29 #include <err.h>
30 #include <errno.h>
31 #include <event.h>
32 #include <imsg.h>
33 #include <limits.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "slaacd.h"
39 #include "parser.h"
40
41 enum token_type {
42 NOTOKEN,
43 ENDTOKEN,
44 INTERFACENAME,
45 KEYWORD
46 };
47
48 struct token {
49 enum token_type type;
50 const char *keyword;
51 int value;
52 const struct token *next;
53 };
54
55 static const struct token t_main[];
56 static const struct token t_log[];
57 static const struct token t_show[];
58 static const struct token t_show_interface[];
59 static const struct token t_send[];
60 static const struct token t_send_sol[];
61
62 static const struct token t_main[] = {
63 {KEYWORD, "show", SHOW, t_show},
64 {KEYWORD, "log", NONE, t_log},
65 {KEYWORD, "send", NONE, t_send},
66 {ENDTOKEN, "", NONE, NULL}
67 };
68
69 static const struct token t_log[] = {
70 {KEYWORD, "verbose", LOG_VERBOSE, NULL},
71 {KEYWORD, "brief", LOG_BRIEF, NULL},
72 {ENDTOKEN, "", NONE, NULL}
73 };
74
75 static const struct token t_show[] = {
76 {KEYWORD, "interface", SHOW_INTERFACE, t_show_interface},
77 {ENDTOKEN, "", NONE, NULL}
78 };
79
80 static const struct token t_send[] = {
81 {KEYWORD, "solicitation", SEND_SOLICITATION, t_send_sol},
82 {ENDTOKEN, "", NONE, NULL}
83 };
84 static const struct token t_send_sol[] = {
85 {INTERFACENAME, "", SEND_SOLICITATION, NULL},
86 {ENDTOKEN, "", NONE, NULL}
87 };
88
89 static const struct token t_show_interface[] = {
90 {NOTOKEN, "", NONE, NULL},
91 {INTERFACENAME, "", SHOW_INTERFACE, NULL},
92 {ENDTOKEN, "", NONE, NULL}
93 };
94
95 static const struct token *match_token(const char *, const struct token *,
96 struct parse_result *);
97 static void show_valid_args(const struct token *);
98
99 struct parse_result *
parse(int argc,char * argv[])100 parse(int argc, char *argv[])
101 {
102 static struct parse_result res;
103 const struct token *table = t_main;
104 const struct token *match;
105
106 memset(&res, 0, sizeof(res));
107
108 while (argc >= 0) {
109 if ((match = match_token(argv[0], table, &res)) == NULL) {
110 fprintf(stderr, "valid commands/args:\n");
111 show_valid_args(table);
112 return (NULL);
113 }
114
115 argc--;
116 argv++;
117
118 if (match->type == NOTOKEN || match->next == NULL)
119 break;
120
121 table = match->next;
122 }
123
124 if (argc > 0) {
125 fprintf(stderr, "superfluous argument: %s\n", argv[0]);
126 return (NULL);
127 }
128
129 return (&res);
130 }
131
132 static const struct token *
match_token(const char * word,const struct token * table,struct parse_result * res)133 match_token(const char *word, const struct token *table,
134 struct parse_result *res)
135 {
136 u_int i, match;
137 const struct token *t = NULL;
138
139 match = 0;
140
141 for (i = 0; table[i].type != ENDTOKEN; i++) {
142 switch (table[i].type) {
143 case NOTOKEN:
144 if (word == NULL || strlen(word) == 0) {
145 match++;
146 t = &table[i];
147 }
148 break;
149 case INTERFACENAME:
150 if (!match && word != NULL && strlen(word) > 0) {
151 if ((res->if_index = if_nametoindex(word)) == 0)
152 errx(1, "unknown interface");
153 match++;
154 t = &table[i];
155 if (t->value)
156 res->action = t->value;
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 ENDTOKEN:
169 break;
170 }
171 }
172
173 if (match != 1) {
174 if (word == NULL)
175 fprintf(stderr, "missing argument:\n");
176 else if (match > 1)
177 fprintf(stderr, "ambiguous argument: %s\n", word);
178 else if (match < 1)
179 fprintf(stderr, "unknown argument: %s\n", word);
180 return (NULL);
181 }
182
183 return (t);
184 }
185
186 static void
show_valid_args(const struct token * table)187 show_valid_args(const struct token *table)
188 {
189 int i;
190
191 for (i = 0; table[i].type != ENDTOKEN; i++) {
192 switch (table[i].type) {
193 case NOTOKEN:
194 fprintf(stderr, " <cr>\n");
195 break;
196 case INTERFACENAME:
197 fprintf(stderr, " <interface>\n");
198 break;
199 case KEYWORD:
200 fprintf(stderr, " %s\n", table[i].keyword);
201 break;
202 case ENDTOKEN:
203 break;
204 }
205 }
206 }
207