2 * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
3 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
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.
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.
24 #include "ctl_parser.h"
38 const struct token *next;
41 static const struct token t_main[];
42 static const struct token t_log[];
44 static const struct token t_main[] = {
45 {KEYWORD, "reload", RELOAD, NULL},
46 {KEYWORD, "log", NONE, t_log},
47 {ENDTOKEN, "", NONE, NULL},
50 static const struct token t_log[] = {
51 {KEYWORD, "verbose", LOG_VERBOSE, NULL},
52 {KEYWORD, "brief", LOG_BRIEF, NULL},
53 {ENDTOKEN, "", NONE, NULL},
56 static const struct token *match_token(const char *, const struct token *,
57 struct parse_result *);
58 static void show_valid_args(const struct token *);
61 parse(int argc, char **argv)
63 static struct parse_result res;
64 const struct token *table = t_main;
65 const struct token *match;
67 memset(&res, 0, sizeof(res));
70 if ((match = match_token(argv[0], table, &res)) == NULL) {
71 fprintf(stderr, "valid commands/args:\n");
72 show_valid_args(table);
79 if (match->type == NOTOKEN || match->next == NULL)
86 fprintf(stderr, "superfluous argument: %s\n", argv[0]);
93 static const struct token *
94 match_token(const char *word, const struct token *table,
95 struct parse_result *res)
98 const struct token *t = NULL;
102 for (i = 0; table[i].type != ENDTOKEN; i++) {
103 switch (table[i].type) {
105 if (word == NULL || strlen(word) == 0) {
111 if (word != NULL && strncmp(word, table[i].keyword,
112 strlen(word)) == 0) {
116 res->action = t->value;
126 fprintf(stderr, "missing argument:\n");
128 fprintf(stderr, "ambiuous argument: %s\n", word);
130 fprintf(stderr, "unknown argument: %s\n", word);
138 show_valid_args(const struct token *table)
142 for (i = 0; table[i].type != ENDTOKEN; i++) {
143 switch (table[i].type) {
145 fprintf(stderr, " <cr>\n");
148 fprintf(stderr, " %s\n", table[i].keyword);