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 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>
21 fb1a36c0 2022-01-09 op
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>
27 fb1a36c0 2022-01-09 op
28 fb1a36c0 2022-01-09 op #include "ctl_parser.h"
29 fb1a36c0 2022-01-09 op #include "kamid.h"
30 fb1a36c0 2022-01-09 op
31 fb1a36c0 2022-01-09 op enum token_type {
32 fb1a36c0 2022-01-09 op NOTOKEN,
33 fb1a36c0 2022-01-09 op ENDTOKEN,
34 fb1a36c0 2022-01-09 op KEYWORD,
35 fb1a36c0 2022-01-09 op };
36 fb1a36c0 2022-01-09 op
37 fb1a36c0 2022-01-09 op
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;
41 fb1a36c0 2022-01-09 op int value;
42 fb1a36c0 2022-01-09 op const struct token *next;
43 fb1a36c0 2022-01-09 op };
44 fb1a36c0 2022-01-09 op
45 fb1a36c0 2022-01-09 op static const struct token t_main[];
46 fb1a36c0 2022-01-09 op static const struct token t_log[];
47 fb1a36c0 2022-01-09 op
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},
52 fb1a36c0 2022-01-09 op };
53 fb1a36c0 2022-01-09 op
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},
58 fb1a36c0 2022-01-09 op };
59 fb1a36c0 2022-01-09 op
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 *);
63 fb1a36c0 2022-01-09 op
64 fb1a36c0 2022-01-09 op struct parse_result *
65 fb1a36c0 2022-01-09 op parse(int argc, char **argv)
66 fb1a36c0 2022-01-09 op {
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;
70 fb1a36c0 2022-01-09 op
71 fb1a36c0 2022-01-09 op memset(&res, 0, sizeof(res));
72 fb1a36c0 2022-01-09 op
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);
77 fb1a36c0 2022-01-09 op return NULL;
78 fb1a36c0 2022-01-09 op }
79 fb1a36c0 2022-01-09 op
80 fb1a36c0 2022-01-09 op argc--;
81 fb1a36c0 2022-01-09 op argv++;
82 fb1a36c0 2022-01-09 op
83 fb1a36c0 2022-01-09 op if (match->type == NOTOKEN || match->next == NULL)
84 fb1a36c0 2022-01-09 op break;
85 fb1a36c0 2022-01-09 op
86 fb1a36c0 2022-01-09 op table = match->next;
87 fb1a36c0 2022-01-09 op }
88 fb1a36c0 2022-01-09 op
89 fb1a36c0 2022-01-09 op if (argc > 0) {
90 fb1a36c0 2022-01-09 op fprintf(stderr, "superfluous argument: %s\n", argv[0]);
91 fb1a36c0 2022-01-09 op return NULL;
92 fb1a36c0 2022-01-09 op }
93 fb1a36c0 2022-01-09 op
94 fb1a36c0 2022-01-09 op return &res;
95 fb1a36c0 2022-01-09 op }
96 fb1a36c0 2022-01-09 op
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)
100 fb1a36c0 2022-01-09 op {
101 fb1a36c0 2022-01-09 op size_t i, match;
102 fb1a36c0 2022-01-09 op const struct token *t = NULL;
103 fb1a36c0 2022-01-09 op
104 fb1a36c0 2022-01-09 op match = 0;
105 fb1a36c0 2022-01-09 op
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) {
110 fb1a36c0 2022-01-09 op match++;
111 fb1a36c0 2022-01-09 op t = &table[i];
112 fb1a36c0 2022-01-09 op }
113 fb1a36c0 2022-01-09 op break;
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) {
117 fb1a36c0 2022-01-09 op match++;
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;
121 fb1a36c0 2022-01-09 op }
122 fb1a36c0 2022-01-09 op break;
123 fb1a36c0 2022-01-09 op case ENDTOKEN:
124 fb1a36c0 2022-01-09 op break;
125 fb1a36c0 2022-01-09 op }
126 fb1a36c0 2022-01-09 op }
127 fb1a36c0 2022-01-09 op
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);
135 fb1a36c0 2022-01-09 op return NULL;
136 fb1a36c0 2022-01-09 op }
137 fb1a36c0 2022-01-09 op
138 fb1a36c0 2022-01-09 op return t;
139 fb1a36c0 2022-01-09 op }
140 fb1a36c0 2022-01-09 op
141 fb1a36c0 2022-01-09 op static void
142 fb1a36c0 2022-01-09 op show_valid_args(const struct token *table)
143 fb1a36c0 2022-01-09 op {
144 fb1a36c0 2022-01-09 op int i;
145 fb1a36c0 2022-01-09 op
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");
150 fb1a36c0 2022-01-09 op break;
151 fb1a36c0 2022-01-09 op case KEYWORD:
152 fb1a36c0 2022-01-09 op fprintf(stderr, " %s\n", table[i].keyword);
153 fb1a36c0 2022-01-09 op break;
154 fb1a36c0 2022-01-09 op case ENDTOKEN:
155 fb1a36c0 2022-01-09 op break;
156 fb1a36c0 2022-01-09 op }
157 fb1a36c0 2022-01-09 op }
158 fb1a36c0 2022-01-09 op }