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 <stdio.h>
19 #include <string.h>
21 #include "ctl_parser.h"
22 #include "kamid.h"
24 enum token_type {
25 NOTOKEN,
26 ENDTOKEN,
27 KEYWORD,
28 };
31 struct token {
32 enum token_type type;
33 const char *keyword;
34 int value;
35 const struct token *next;
36 };
38 static const struct token t_main[];
39 static const struct token t_log[];
41 static const struct token t_main[] = {
42 {KEYWORD, "reload", RELOAD, NULL},
43 {KEYWORD, "log", NONE, t_log},
44 {ENDTOKEN, "", NONE, NULL},
45 };
47 static const struct token t_log[] = {
48 {KEYWORD, "verbose", LOG_VERBOSE, NULL},
49 {KEYWORD, "brief", LOG_BRIEF, NULL},
50 {ENDTOKEN, "", NONE, NULL},
51 };
53 static const struct token *match_token(const char *, const struct token *,
54 struct parse_result *);
55 static void show_valid_args(const struct token *);
57 struct parse_result *
58 parse(int argc, char **argv)
59 {
60 static struct parse_result res;
61 const struct token *table = t_main;
62 const struct token *match;
64 memset(&res, 0, sizeof(res));
66 while (argc >= 0) {
67 if ((match = match_token(argv[0], table, &res)) == NULL) {
68 fprintf(stderr, "valid commands/args:\n");
69 show_valid_args(table);
70 return NULL;
71 }
73 argc--;
74 argv++;
76 if (match->type == NOTOKEN || match->next == NULL)
77 break;
79 table = match->next;
80 }
82 if (argc > 0) {
83 fprintf(stderr, "superfluous argument: %s\n", argv[0]);
84 return NULL;
85 }
87 return &res;
88 }
90 static const struct token *
91 match_token(const char *word, const struct token *table,
92 struct parse_result *res)
93 {
94 size_t i, match;
95 const struct token *t = NULL;
97 match = 0;
99 for (i = 0; table[i].type != ENDTOKEN; i++) {
100 switch (table[i].type) {
101 case NOTOKEN:
102 if (word == NULL || strlen(word) == 0) {
103 match++;
104 t = &table[i];
106 break;
107 case KEYWORD:
108 if (word != NULL && strncmp(word, table[i].keyword,
109 strlen(word)) == 0) {
110 match++;
111 t = &table[i];
112 if (t->value)
113 res->action = t->value;
115 break;
116 case ENDTOKEN:
117 break;
121 if (match != 1) {
122 if (word == NULL)
123 fprintf(stderr, "missing argument:\n");
124 else if (match > 1)
125 fprintf(stderr, "ambiuous argument: %s\n", word);
126 else if (match < 1)
127 fprintf(stderr, "unknown argument: %s\n", word);
128 return NULL;
131 return t;
134 static void
135 show_valid_args(const struct token *table)
137 int i;
139 for (i = 0; table[i].type != ENDTOKEN; i++) {
140 switch (table[i].type) {
141 case NOTOKEN:
142 fprintf(stderr, " <cr>\n");
143 break;
144 case KEYWORD:
145 fprintf(stderr, " %s\n", table[i].keyword);
146 break;
147 case ENDTOKEN:
148 break;