1 /* $OpenBSD: parser.c,v 1.1 2010/09/24 09:45:17 claudio Exp $ */ 2 3 /* 4 * Copyright (c) 2004 Esben Norby <norby@openbsd.org> 5 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/types.h> 21 #include <sys/queue.h> 22 #include <sys/socket.h> 23 #include <sys/uio.h> 24 #include <netinet/in.h> 25 #include <err.h> 26 #include <errno.h> 27 #include <event.h> 28 #include <limits.h> 29 #include <netdb.h> 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <unistd.h> 34 35 #include "iscsid.h" 36 #include "iscsictl.h" 37 38 enum token_type { 39 NOTOKEN, 40 ENDTOKEN, 41 KEYWORD, 42 ADDRESS, 43 FLAG 44 }; 45 46 struct token { 47 enum token_type type; 48 const char *keyword; 49 int value; 50 const struct token *next; 51 }; 52 53 static const struct token t_main[]; 54 static const struct token t_show[]; 55 static const struct token t_log[]; 56 static const struct token t_discovery[]; 57 58 static const struct token t_main[] = { 59 {KEYWORD, "reload", RELOAD, NULL}, 60 {KEYWORD, "discover", DISCOVERY, t_discovery}, 61 {KEYWORD, "show", SHOW, t_show}, 62 {KEYWORD, "log", NONE, t_log}, 63 {ENDTOKEN, "", NONE, NULL} 64 }; 65 66 static const struct token t_show[] = { 67 {NOTOKEN, "", NONE, NULL}, 68 {KEYWORD, "summary", SHOW_SUM, NULL}, 69 {ENDTOKEN, "", NONE, NULL} 70 }; 71 72 static const struct token t_log[] = { 73 {KEYWORD, "verbose", LOG_VERBOSE, NULL}, 74 {KEYWORD, "brief", LOG_BRIEF, NULL}, 75 {ENDTOKEN, "", NONE, NULL} 76 }; 77 78 static const struct token t_discovery[] = { 79 {ADDRESS, "", NONE, NULL}, 80 {ENDTOKEN, "", NONE, NULL} 81 }; 82 83 static struct parse_result res; 84 85 struct parse_result * 86 parse(int argc, char *argv[]) 87 { 88 const struct token *table = t_main; 89 const struct token *match; 90 91 bzero(&res, sizeof(res)); 92 93 while (argc >= 0) { 94 if ((match = match_token(argv[0], table)) == NULL) { 95 fprintf(stderr, "valid commands/args:\n"); 96 show_valid_args(table); 97 return (NULL); 98 } 99 100 argc--; 101 argv++; 102 103 if (match->type == NOTOKEN || match->next == NULL) 104 break; 105 106 table = match->next; 107 } 108 109 if (argc > 0) { 110 fprintf(stderr, "superfluous argument: %s\n", argv[0]); 111 return (NULL); 112 } 113 114 return (&res); 115 } 116 117 const struct token * 118 match_token(const char *word, const struct token *table) 119 { 120 u_int i, match; 121 const struct token *t = NULL; 122 123 match = 0; 124 125 for (i = 0; table[i].type != ENDTOKEN; i++) { 126 switch (table[i].type) { 127 case NOTOKEN: 128 if (word == NULL || strlen(word) == 0) { 129 match++; 130 t = &table[i]; 131 } 132 break; 133 case KEYWORD: 134 if (word != NULL && strncmp(word, table[i].keyword, 135 strlen(word)) == 0) { 136 match++; 137 t = &table[i]; 138 if (t->value) 139 res.action = t->value; 140 } 141 break; 142 case FLAG: 143 if (word != NULL && strncmp(word, table[i].keyword, 144 strlen(word)) == 0) { 145 match++; 146 t = &table[i]; 147 res.flags |= t->value; 148 } 149 break; 150 case ADDRESS: 151 if (!parse_addr(word, &res.addr)) { 152 match++; 153 t = &table[i]; 154 if (t->value) 155 res.action = t->value; 156 } 157 break; 158 case ENDTOKEN: 159 break; 160 } 161 } 162 163 if (match != 1) { 164 if (word == NULL) 165 fprintf(stderr, "missing argument:\n"); 166 else if (match > 1) 167 fprintf(stderr, "ambiguous argument: %s\n", word); 168 else if (match < 1) 169 fprintf(stderr, "unknown argument: %s\n", word); 170 return (NULL); 171 } 172 173 return (t); 174 } 175 176 void 177 show_valid_args(const struct token *table) 178 { 179 int i; 180 181 for (i = 0; table[i].type != ENDTOKEN; i++) { 182 switch (table[i].type) { 183 case NOTOKEN: 184 fprintf(stderr, " <cr>\n"); 185 break; 186 case KEYWORD: 187 case FLAG: 188 fprintf(stderr, " %s\n", table[i].keyword); 189 break; 190 case ADDRESS: 191 fprintf(stderr, " <address>\n"); 192 break; 193 case ENDTOKEN: 194 break; 195 } 196 } 197 } 198 199 int 200 parse_addr(const char *word, struct sockaddr_storage *sa) 201 { 202 struct addrinfo hints, *addrs; 203 int rv; 204 205 bzero(&hints, sizeof(hints)); 206 hints.ai_family = PF_UNSPEC; 207 hints.ai_socktype = SOCK_STREAM; 208 hints.ai_protocol = IPPROTO_TCP; 209 210 if ((rv = getaddrinfo(word, "iscsi", &hints, &addrs)) == 0) { 211 if (sizeof(*sa) < addrs->ai_addrlen) 212 err(1, "parse_host: bork bork bork"); 213 bcopy(addrs->ai_addr, sa, addrs->ai_addrlen); 214 freeaddrinfo(addrs); 215 return (0); 216 } 217 218 errx(1, "parse_host: %s", gai_strerror(rv)); 219 } 220