Blame


1 fb1a36c0 2022-01-09 op /*
2 fb1a36c0 2022-01-09 op * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
3 fb1a36c0 2022-01-09 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
4 fb1a36c0 2022-01-09 op *
5 fb1a36c0 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
6 fb1a36c0 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
7 fb1a36c0 2022-01-09 op * copyright notice and this permission notice appear in all copies.
8 fb1a36c0 2022-01-09 op *
9 fb1a36c0 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 fb1a36c0 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 fb1a36c0 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 fb1a36c0 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 fb1a36c0 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 fb1a36c0 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 fb1a36c0 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 fb1a36c0 2022-01-09 op */
17 fb1a36c0 2022-01-09 op
18 bbcba3ed 2022-01-10 op #include "compat.h"
19 fb1a36c0 2022-01-09 op
20 fb1a36c0 2022-01-09 op #include <stdint.h>
21 fb1a36c0 2022-01-09 op #include <stdio.h>
22 fb1a36c0 2022-01-09 op #include <string.h>
23 fb1a36c0 2022-01-09 op
24 fb1a36c0 2022-01-09 op #include "ctl_parser.h"
25 fb1a36c0 2022-01-09 op #include "kamid.h"
26 fb1a36c0 2022-01-09 op
27 fb1a36c0 2022-01-09 op enum token_type {
28 fb1a36c0 2022-01-09 op NOTOKEN,
29 fb1a36c0 2022-01-09 op ENDTOKEN,
30 fb1a36c0 2022-01-09 op KEYWORD,
31 fb1a36c0 2022-01-09 op };
32 fb1a36c0 2022-01-09 op
33 fb1a36c0 2022-01-09 op struct token {
34 fb1a36c0 2022-01-09 op enum token_type type;
35 fb1a36c0 2022-01-09 op const char *keyword;
36 fb1a36c0 2022-01-09 op int value;
37 fb1a36c0 2022-01-09 op const struct token *next;
38 fb1a36c0 2022-01-09 op };
39 fb1a36c0 2022-01-09 op
40 fb1a36c0 2022-01-09 op static const struct token t_main[];
41 fb1a36c0 2022-01-09 op static const struct token t_log[];
42 fb1a36c0 2022-01-09 op
43 fb1a36c0 2022-01-09 op static const struct token t_main[] = {
44 fb1a36c0 2022-01-09 op {KEYWORD, "reload", RELOAD, NULL},
45 fb1a36c0 2022-01-09 op {KEYWORD, "log", NONE, t_log},
46 4fa9a307 2022-01-29 op {KEYWORD, "debug", DEBUG, NULL},
47 fb1a36c0 2022-01-09 op {ENDTOKEN, "", NONE, NULL},
48 fb1a36c0 2022-01-09 op };
49 fb1a36c0 2022-01-09 op
50 fb1a36c0 2022-01-09 op static const struct token t_log[] = {
51 fb1a36c0 2022-01-09 op {KEYWORD, "verbose", LOG_VERBOSE, NULL},
52 fb1a36c0 2022-01-09 op {KEYWORD, "brief", LOG_BRIEF, NULL},
53 fb1a36c0 2022-01-09 op {ENDTOKEN, "", NONE, NULL},
54 fb1a36c0 2022-01-09 op };
55 fb1a36c0 2022-01-09 op
56 fb1a36c0 2022-01-09 op static const struct token *match_token(const char *, const struct token *,
57 fb1a36c0 2022-01-09 op struct parse_result *);
58 fb1a36c0 2022-01-09 op static void show_valid_args(const struct token *);
59 fb1a36c0 2022-01-09 op
60 fb1a36c0 2022-01-09 op struct parse_result *
61 fb1a36c0 2022-01-09 op parse(int argc, char **argv)
62 fb1a36c0 2022-01-09 op {
63 fb1a36c0 2022-01-09 op static struct parse_result res;
64 fb1a36c0 2022-01-09 op const struct token *table = t_main;
65 fb1a36c0 2022-01-09 op const struct token *match;
66 fb1a36c0 2022-01-09 op
67 fb1a36c0 2022-01-09 op memset(&res, 0, sizeof(res));
68 fb1a36c0 2022-01-09 op
69 fb1a36c0 2022-01-09 op while (argc >= 0) {
70 fb1a36c0 2022-01-09 op if ((match = match_token(argv[0], table, &res)) == NULL) {
71 fb1a36c0 2022-01-09 op fprintf(stderr, "valid commands/args:\n");
72 fb1a36c0 2022-01-09 op show_valid_args(table);
73 fb1a36c0 2022-01-09 op return NULL;
74 fb1a36c0 2022-01-09 op }
75 fb1a36c0 2022-01-09 op
76 fb1a36c0 2022-01-09 op argc--;
77 fb1a36c0 2022-01-09 op argv++;
78 fb1a36c0 2022-01-09 op
79 fb1a36c0 2022-01-09 op if (match->type == NOTOKEN || match->next == NULL)
80 fb1a36c0 2022-01-09 op break;
81 fb1a36c0 2022-01-09 op
82 fb1a36c0 2022-01-09 op table = match->next;
83 fb1a36c0 2022-01-09 op }
84 fb1a36c0 2022-01-09 op
85 fb1a36c0 2022-01-09 op if (argc > 0) {
86 fb1a36c0 2022-01-09 op fprintf(stderr, "superfluous argument: %s\n", argv[0]);
87 fb1a36c0 2022-01-09 op return NULL;
88 fb1a36c0 2022-01-09 op }
89 fb1a36c0 2022-01-09 op
90 fb1a36c0 2022-01-09 op return &res;
91 fb1a36c0 2022-01-09 op }
92 fb1a36c0 2022-01-09 op
93 fb1a36c0 2022-01-09 op static const struct token *
94 fb1a36c0 2022-01-09 op match_token(const char *word, const struct token *table,
95 fb1a36c0 2022-01-09 op struct parse_result *res)
96 fb1a36c0 2022-01-09 op {
97 fb1a36c0 2022-01-09 op size_t i, match;
98 fb1a36c0 2022-01-09 op const struct token *t = NULL;
99 fb1a36c0 2022-01-09 op
100 fb1a36c0 2022-01-09 op match = 0;
101 fb1a36c0 2022-01-09 op
102 fb1a36c0 2022-01-09 op for (i = 0; table[i].type != ENDTOKEN; i++) {
103 fb1a36c0 2022-01-09 op switch (table[i].type) {
104 fb1a36c0 2022-01-09 op case NOTOKEN:
105 fb1a36c0 2022-01-09 op if (word == NULL || strlen(word) == 0) {
106 fb1a36c0 2022-01-09 op match++;
107 fb1a36c0 2022-01-09 op t = &table[i];
108 fb1a36c0 2022-01-09 op }
109 fb1a36c0 2022-01-09 op break;
110 fb1a36c0 2022-01-09 op case KEYWORD:
111 fb1a36c0 2022-01-09 op if (word != NULL && strncmp(word, table[i].keyword,
112 fb1a36c0 2022-01-09 op strlen(word)) == 0) {
113 fb1a36c0 2022-01-09 op match++;
114 fb1a36c0 2022-01-09 op t = &table[i];
115 fb1a36c0 2022-01-09 op if (t->value)
116 fb1a36c0 2022-01-09 op res->action = t->value;
117 fb1a36c0 2022-01-09 op }
118 fb1a36c0 2022-01-09 op break;
119 fb1a36c0 2022-01-09 op case ENDTOKEN:
120 fb1a36c0 2022-01-09 op break;
121 fb1a36c0 2022-01-09 op }
122 fb1a36c0 2022-01-09 op }
123 fb1a36c0 2022-01-09 op
124 fb1a36c0 2022-01-09 op if (match != 1) {
125 fb1a36c0 2022-01-09 op if (word == NULL)
126 fb1a36c0 2022-01-09 op fprintf(stderr, "missing argument:\n");
127 fb1a36c0 2022-01-09 op else if (match > 1)
128 fb1a36c0 2022-01-09 op fprintf(stderr, "ambiuous argument: %s\n", word);
129 fb1a36c0 2022-01-09 op else if (match < 1)
130 fb1a36c0 2022-01-09 op fprintf(stderr, "unknown argument: %s\n", word);
131 fb1a36c0 2022-01-09 op return NULL;
132 fb1a36c0 2022-01-09 op }
133 fb1a36c0 2022-01-09 op
134 fb1a36c0 2022-01-09 op return t;
135 fb1a36c0 2022-01-09 op }
136 fb1a36c0 2022-01-09 op
137 fb1a36c0 2022-01-09 op static void
138 fb1a36c0 2022-01-09 op show_valid_args(const struct token *table)
139 fb1a36c0 2022-01-09 op {
140 fb1a36c0 2022-01-09 op int i;
141 fb1a36c0 2022-01-09 op
142 fb1a36c0 2022-01-09 op for (i = 0; table[i].type != ENDTOKEN; i++) {
143 fb1a36c0 2022-01-09 op switch (table[i].type) {
144 fb1a36c0 2022-01-09 op case NOTOKEN:
145 fb1a36c0 2022-01-09 op fprintf(stderr, " <cr>\n");
146 fb1a36c0 2022-01-09 op break;
147 fb1a36c0 2022-01-09 op case KEYWORD:
148 fb1a36c0 2022-01-09 op fprintf(stderr, " %s\n", table[i].keyword);
149 fb1a36c0 2022-01-09 op break;
150 fb1a36c0 2022-01-09 op case ENDTOKEN:
151 fb1a36c0 2022-01-09 op break;
152 fb1a36c0 2022-01-09 op }
153 fb1a36c0 2022-01-09 op }
154 fb1a36c0 2022-01-09 op }