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>
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.
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.
18 fb1a36c0 2022-01-09 op #include <sys/types.h>
19 fb1a36c0 2022-01-09 op #include <sys/queue.h>
20 fb1a36c0 2022-01-09 op #include <sys/uio.h>
22 fb1a36c0 2022-01-09 op #include <event.h>
23 fb1a36c0 2022-01-09 op #include <stdint.h>
24 fb1a36c0 2022-01-09 op #include <stdio.h>
25 fb1a36c0 2022-01-09 op #include <string.h>
26 fb1a36c0 2022-01-09 op #include <imsg.h>
28 fb1a36c0 2022-01-09 op #include "ctl_parser.h"
29 fb1a36c0 2022-01-09 op #include "kamid.h"
31 fb1a36c0 2022-01-09 op enum token_type {
38 fb1a36c0 2022-01-09 op struct token {
39 fb1a36c0 2022-01-09 op enum token_type type;
40 fb1a36c0 2022-01-09 op const char *keyword;
42 fb1a36c0 2022-01-09 op const struct token *next;
45 fb1a36c0 2022-01-09 op static const struct token t_main[];
46 fb1a36c0 2022-01-09 op static const struct token t_log[];
48 fb1a36c0 2022-01-09 op static const struct token t_main[] = {
49 fb1a36c0 2022-01-09 op {KEYWORD, "reload", RELOAD, NULL},
50 fb1a36c0 2022-01-09 op {KEYWORD, "log", NONE, t_log},
51 fb1a36c0 2022-01-09 op {ENDTOKEN, "", NONE, NULL},
54 fb1a36c0 2022-01-09 op static const struct token t_log[] = {
55 fb1a36c0 2022-01-09 op {KEYWORD, "verbose", LOG_VERBOSE, NULL},
56 fb1a36c0 2022-01-09 op {KEYWORD, "brief", LOG_BRIEF, NULL},
57 fb1a36c0 2022-01-09 op {ENDTOKEN, "", NONE, NULL},
60 fb1a36c0 2022-01-09 op static const struct token *match_token(const char *, const struct token *,
61 fb1a36c0 2022-01-09 op struct parse_result *);
62 fb1a36c0 2022-01-09 op static void show_valid_args(const struct token *);
64 fb1a36c0 2022-01-09 op struct parse_result *
65 fb1a36c0 2022-01-09 op parse(int argc, char **argv)
67 fb1a36c0 2022-01-09 op static struct parse_result res;
68 fb1a36c0 2022-01-09 op const struct token *table = t_main;
69 fb1a36c0 2022-01-09 op const struct token *match;
71 fb1a36c0 2022-01-09 op memset(&res, 0, sizeof(res));
73 fb1a36c0 2022-01-09 op while (argc >= 0) {
74 fb1a36c0 2022-01-09 op if ((match = match_token(argv[0], table, &res)) == NULL) {
75 fb1a36c0 2022-01-09 op fprintf(stderr, "valid commands/args:\n");
76 fb1a36c0 2022-01-09 op show_valid_args(table);
83 fb1a36c0 2022-01-09 op if (match->type == NOTOKEN || match->next == NULL)
86 fb1a36c0 2022-01-09 op table = match->next;
89 fb1a36c0 2022-01-09 op if (argc > 0) {
90 fb1a36c0 2022-01-09 op fprintf(stderr, "superfluous argument: %s\n", argv[0]);
97 fb1a36c0 2022-01-09 op static const struct token *
98 fb1a36c0 2022-01-09 op match_token(const char *word, const struct token *table,
99 fb1a36c0 2022-01-09 op struct parse_result *res)
101 fb1a36c0 2022-01-09 op size_t i, match;
102 fb1a36c0 2022-01-09 op const struct token *t = NULL;
106 fb1a36c0 2022-01-09 op for (i = 0; table[i].type != ENDTOKEN; i++) {
107 fb1a36c0 2022-01-09 op switch (table[i].type) {
108 fb1a36c0 2022-01-09 op case NOTOKEN:
109 fb1a36c0 2022-01-09 op if (word == NULL || strlen(word) == 0) {
111 fb1a36c0 2022-01-09 op t = &table[i];
114 fb1a36c0 2022-01-09 op case KEYWORD:
115 fb1a36c0 2022-01-09 op if (word != NULL && strncmp(word, table[i].keyword,
116 fb1a36c0 2022-01-09 op strlen(word)) == 0) {
118 fb1a36c0 2022-01-09 op t = &table[i];
119 fb1a36c0 2022-01-09 op if (t->value)
120 fb1a36c0 2022-01-09 op res->action = t->value;
123 fb1a36c0 2022-01-09 op case ENDTOKEN:
128 fb1a36c0 2022-01-09 op if (match != 1) {
129 fb1a36c0 2022-01-09 op if (word == NULL)
130 fb1a36c0 2022-01-09 op fprintf(stderr, "missing argument:\n");
131 fb1a36c0 2022-01-09 op else if (match > 1)
132 fb1a36c0 2022-01-09 op fprintf(stderr, "ambiuous argument: %s\n", word);
133 fb1a36c0 2022-01-09 op else if (match < 1)
134 fb1a36c0 2022-01-09 op fprintf(stderr, "unknown argument: %s\n", word);
142 fb1a36c0 2022-01-09 op show_valid_args(const struct token *table)
146 fb1a36c0 2022-01-09 op for (i = 0; table[i].type != ENDTOKEN; i++) {
147 fb1a36c0 2022-01-09 op switch (table[i].type) {
148 fb1a36c0 2022-01-09 op case NOTOKEN:
149 fb1a36c0 2022-01-09 op fprintf(stderr, " <cr>\n");
151 fb1a36c0 2022-01-09 op case KEYWORD:
152 fb1a36c0 2022-01-09 op fprintf(stderr, " %s\n", table[i].keyword);
154 fb1a36c0 2022-01-09 op case ENDTOKEN: