Blob


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