parser.c revision 1.6
1/* $OpenBSD: parser.c,v 1.6 2007/01/29 10:28:11 claudio Exp $ */ 2 3/* 4 * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@spootnik.org> 5 * Copyright (c) 2004 Esben Norby <norby@openbsd.org> 6 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21#include <sys/types.h> 22#include <sys/socket.h> 23#include <sys/queue.h> 24#include <netinet/in.h> 25#include <net/if.h> 26#include <arpa/inet.h> 27#include <err.h> 28#include <errno.h> 29#include <limits.h> 30#include <stdio.h> 31#include <stdlib.h> 32#include <string.h> 33#include <event.h> 34 35#include "hoststated.h" 36 37#include "parser.h" 38 39enum token_type { 40 NOTOKEN, 41 ENDTOKEN, 42 HOSTID, 43 TABLEID, 44 SERVICEID, 45 KEYWORD 46}; 47 48struct token { 49 enum token_type type; 50 const char *keyword; 51 int value; 52 const struct token *next; 53}; 54 55static const struct token t_main[]; 56static const struct token t_service[]; 57static const struct token t_table[]; 58static const struct token t_host[]; 59static const struct token t_service_id[]; 60static const struct token t_table_id[]; 61static const struct token t_host_id[]; 62 63static const struct token t_main[] = { 64 {KEYWORD, "show", SHOW_SUM, NULL}, 65 {KEYWORD, "stop", SHUTDOWN, NULL}, 66 {KEYWORD, "service", NULL, t_service}, 67 {KEYWORD, "table", NULL, t_table}, 68 {KEYWORD, "host", NULL, t_host}, 69 {ENDTOKEN, "", NONE, NULL} 70}; 71 72static const struct token t_service[] = { 73 {NOTOKEN, "", NONE, NULL}, 74 {KEYWORD, "disable", SERV_DISABLE, t_service_id}, 75 {KEYWORD, "enable", SERV_ENABLE, t_service_id}, 76 {ENDTOKEN, "", NONE, NULL} 77}; 78 79static const struct token t_table[] = { 80 {NOTOKEN, "", NONE, NULL}, 81 {KEYWORD, "disable", TABLE_DISABLE, t_table_id}, 82 {KEYWORD, "enable", TABLE_ENABLE, t_table_id}, 83 {ENDTOKEN, "", NONE, NULL} 84}; 85 86static const struct token t_host[] = { 87 {NOTOKEN, "", NONE, NULL}, 88 {KEYWORD, "disable", HOST_DISABLE, t_host_id}, 89 {KEYWORD, "enable", HOST_ENABLE, t_host_id}, 90 {ENDTOKEN, "", NONE, NULL} 91}; 92 93static const struct token t_service_id[] = { 94 {SERVICEID, "", NONE, NULL}, 95 {ENDTOKEN, "", NONE, NULL} 96}; 97 98static const struct token t_table_id[] = { 99 {TABLEID, "", NONE, NULL}, 100 {ENDTOKEN, "", NONE, NULL} 101}; 102 103static const struct token t_host_id[] = { 104 {HOSTID, "", NONE, NULL}, 105 {ENDTOKEN, "", NONE, NULL} 106}; 107 108static struct parse_result res; 109 110struct parse_result * 111parse(int argc, char *argv[]) 112{ 113 const struct token *table = t_main; 114 const struct token *match; 115 116 bzero(&res, sizeof(res)); 117 118 while (argc >= 0) { 119 if ((match = match_token(argv[0], table)) == NULL) { 120 fprintf(stderr, "valid commands/args:\n"); 121 show_valid_args(table); 122 return (NULL); 123 } 124 125 argc--; 126 argv++; 127 128 if (match->type == NOTOKEN || match->next == NULL) 129 break; 130 131 table = match->next; 132 } 133 134 if (argc > 0) { 135 fprintf(stderr, "superfluous argument: %s\n", argv[0]); 136 return (NULL); 137 } 138 139 return (&res); 140} 141 142const struct token * 143match_token(const char *word, const struct token table[]) 144{ 145 u_int i, match; 146 const struct token *t = NULL; 147 const char *errstr; 148 149 match = 0; 150 151 for (i = 0; table[i].type != ENDTOKEN; i++) { 152 switch (table[i].type) { 153 case NOTOKEN: 154 if (word == NULL || strlen(word) == 0) { 155 match++; 156 t = &table[i]; 157 } 158 break; 159 case KEYWORD: 160 if (word != NULL && strncmp(word, table[i].keyword, 161 strlen(word)) == 0) { 162 match++; 163 t = &table[i]; 164 if (t->value) 165 res.action = t->value; 166 } 167 break; 168 case HOSTID: 169 if (word == NULL) 170 break; 171 res.id.id = strtonum(word, 0, UINT_MAX, &errstr); 172 if (errstr) { 173 strlcpy(res.id.name, word, sizeof(res.id.name)); 174 res.id.id = EMPTY_ID; 175 } 176 t = &table[i]; 177 match++; 178 break; 179 case TABLEID: 180 if (word == NULL) 181 break; 182 res.id.id = strtonum(word, 0, UINT_MAX, &errstr); 183 if (errstr) { 184 strlcpy(res.id.name, word, sizeof(res.id.name)); 185 res.id.id = EMPTY_ID; 186 } 187 t = &table[i]; 188 match++; 189 break; 190 case SERVICEID: 191 if (word == NULL) 192 break; 193 res.id.id = strtonum(word, 0, UINT_MAX, &errstr); 194 if (errstr) { 195 strlcpy(res.id.name, word, sizeof(res.id.name)); 196 res.id.id = EMPTY_ID; 197 } 198 t = &table[i]; 199 match++; 200 break; 201 case ENDTOKEN: 202 break; 203 } 204 } 205 206 if (match != 1) { 207 if (word == NULL) 208 fprintf(stderr, "missing argument:\n"); 209 else if (match > 1) 210 fprintf(stderr, "ambiguous argument: %s\n", word); 211 else if (match < 1) 212 fprintf(stderr, "unknown argument: %s\n", word); 213 return (NULL); 214 } 215 216 return (t); 217} 218 219void 220show_valid_args(const struct token table[]) 221{ 222 int i; 223 224 for (i = 0; table[i].type != ENDTOKEN; i++) { 225 switch (table[i].type) { 226 case NOTOKEN: 227 fprintf(stderr, " <cr>\n"); 228 break; 229 case KEYWORD: 230 fprintf(stderr, " %s\n", table[i].keyword); 231 break; 232 case SERVICEID: 233 fprintf(stderr, " <serviceid>\n"); 234 break; 235 case TABLEID: 236 fprintf(stderr, " <tableid>\n"); 237 break; 238 case HOSTID: 239 fprintf(stderr, " <hostid>\n"); 240 break; 241 case ENDTOKEN: 242 break; 243 } 244 } 245} 246