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.
18 #include <sys/types.h>
19 #include <sys/queue.h>
28 #include "ctl_parser.h"
42 const struct token *next;
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},
54 static const struct token t_log[] = {
55 {KEYWORD, "verbose", LOG_VERBOSE, NULL},
56 {KEYWORD, "brief", LOG_BRIEF, NULL},
57 {ENDTOKEN, "", NONE, NULL},
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 *);
65 parse(int argc, char **argv)
67 static struct parse_result res;
68 const struct token *table = t_main;
69 const struct token *match;
71 memset(&res, 0, sizeof(res));
74 if ((match = match_token(argv[0], table, &res)) == NULL) {
75 fprintf(stderr, "valid commands/args:\n");
76 show_valid_args(table);
83 if (match->type == NOTOKEN || match->next == NULL)
90 fprintf(stderr, "superfluous argument: %s\n", argv[0]);
97 static const struct token *
98 match_token(const char *word, const struct token *table,
99 struct parse_result *res)
102 const struct token *t = NULL;
106 for (i = 0; table[i].type != ENDTOKEN; i++) {
107 switch (table[i].type) {
109 if (word == NULL || strlen(word) == 0) {
115 if (word != NULL && strncmp(word, table[i].keyword,
116 strlen(word)) == 0) {
120 res->action = t->value;
130 fprintf(stderr, "missing argument:\n");
132 fprintf(stderr, "ambiuous argument: %s\n", word);
134 fprintf(stderr, "unknown argument: %s\n", word);
142 show_valid_args(const struct token *table)
146 for (i = 0; table[i].type != ENDTOKEN; i++) {
147 switch (table[i].type) {
149 fprintf(stderr, " <cr>\n");
152 fprintf(stderr, " %s\n", table[i].keyword);