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 "compat.h"
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <string.h>
24 #include "ctl_parser.h"
25 #include "kamid.h"
27 enum token_type {
28 NOTOKEN,
29 ENDTOKEN,
30 KEYWORD,
31 };
33 struct token {
34 enum token_type type;
35 const char *keyword;
36 int value;
37 const struct token *next;
38 };
40 static const struct token t_main[];
41 static const struct token t_log[];
43 static const struct token t_main[] = {
44 {KEYWORD, "reload", RELOAD, NULL},
45 {KEYWORD, "log", NONE, t_log},
46 {KEYWORD, "debug", DEBUG, NULL},
47 {ENDTOKEN, "", NONE, NULL},
48 };
50 static const struct token t_log[] = {
51 {KEYWORD, "verbose", LOG_VERBOSE, NULL},
52 {KEYWORD, "brief", LOG_BRIEF, NULL},
53 {ENDTOKEN, "", NONE, NULL},
54 };
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 *);
60 struct parse_result *
61 parse(int argc, char **argv)
62 {
63 static struct parse_result res;
64 const struct token *table = t_main;
65 const struct token *match;
67 memset(&res, 0, sizeof(res));
69 while (argc >= 0) {
70 if ((match = match_token(argv[0], table, &res)) == NULL) {
71 fprintf(stderr, "valid commands/args:\n");
72 show_valid_args(table);
73 return NULL;
74 }
76 argc--;
77 argv++;
79 if (match->type == NOTOKEN || match->next == NULL)
80 break;
82 table = match->next;
83 }
85 if (argc > 0) {
86 fprintf(stderr, "superfluous argument: %s\n", argv[0]);
87 return NULL;
88 }
90 return &res;
91 }
93 static const struct token *
94 match_token(const char *word, const struct token *table,
95 struct parse_result *res)
96 {
97 size_t i, match;
98 const struct token *t = NULL;
100 match = 0;
102 for (i = 0; table[i].type != ENDTOKEN; i++) {
103 switch (table[i].type) {
104 case NOTOKEN:
105 if (word == NULL || strlen(word) == 0) {
106 match++;
107 t = &table[i];
109 break;
110 case KEYWORD:
111 if (word != NULL && strncmp(word, table[i].keyword,
112 strlen(word)) == 0) {
113 match++;
114 t = &table[i];
115 if (t->value)
116 res->action = t->value;
118 break;
119 case ENDTOKEN:
120 break;
124 if (match != 1) {
125 if (word == NULL)
126 fprintf(stderr, "missing argument:\n");
127 else if (match > 1)
128 fprintf(stderr, "ambiuous argument: %s\n", word);
129 else if (match < 1)
130 fprintf(stderr, "unknown argument: %s\n", word);
131 return NULL;
134 return t;
137 static void
138 show_valid_args(const struct token *table)
140 int i;
142 for (i = 0; table[i].type != ENDTOKEN; i++) {
143 switch (table[i].type) {
144 case NOTOKEN:
145 fprintf(stderr, " <cr>\n");
146 break;
147 case KEYWORD:
148 fprintf(stderr, " %s\n", table[i].keyword);
149 break;
150 case ENDTOKEN:
151 break;