2 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
36 #include <cmdline_parse.h>
37 #include <cmdline_parse_ipaddr.h>
38 #include <cmdline_parse_num.h>
39 #include <cmdline_parse_string.h>
42 #include "parse_obj_list.h"
44 struct object_list global_obj_list;
46 /**********************************************************/
48 struct cmd_obj_del_show_result {
49 cmdline_fixed_string_t action;
53 static void cmd_obj_del_show_parsed(void *parsed_result,
57 struct cmd_obj_del_show_result *res = parsed_result;
58 char ip_str[INET6_ADDRSTRLEN];
60 inet_ntop(res->obj->ip.family, (void *)&res->obj->ip.addr,
61 ip_str, sizeof(ip_str));
63 if (strcmp(res->action, "del") == 0) {
64 SLIST_REMOVE(&global_obj_list, res->obj, object, next);
65 cmdline_printf(cl, "Object %s removed, ip=%s\r\n",
66 res->obj->name, ip_str);
69 else if (strcmp(res->action, "show") == 0) {
70 cmdline_printf(cl, "Object %s, ip=%s\r\n",
71 res->obj->name, ip_str);
75 cmdline_parse_token_string_t cmd_obj_action = TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result, action, "show#del");
76 parse_token_obj_list_t cmd_obj_obj = TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result, obj, &global_obj_list);
78 cmdline_parse_inst_t cmd_obj_del_show = {
79 .f = cmd_obj_del_show_parsed, /* function to call */
80 .data = NULL, /* 2nd arg of func */
81 .help_str = "Show/del an object",
82 .tokens = { /* token list, NULL terminated */
83 (void *)&cmd_obj_action,
89 /**********************************************************/
91 struct cmd_obj_add_result {
92 cmdline_fixed_string_t action;
93 cmdline_fixed_string_t name;
97 static void cmd_obj_add_parsed(void *parsed_result,
101 struct cmd_obj_add_result *res = parsed_result;
103 char ip_str[INET6_ADDRSTRLEN];
105 SLIST_FOREACH(o, &global_obj_list, next) {
106 if (!strcmp(res->name, o->name)) {
107 cmdline_printf(cl, "Object %s already exist\n", res->name);
113 o = malloc(sizeof(*o));
115 cmdline_printf(cl, "mem error\r\n");
118 strcpy(o->name, res->name);
120 SLIST_INSERT_HEAD(&global_obj_list, o, next);
121 inet_ntop(o->ip.family, (void *)&o->ip.addr,
122 ip_str, sizeof(ip_str));
123 cmdline_printf(cl, "Object %s added, ip=%s\r\n",
127 cmdline_parse_token_string_t cmd_obj_action_add = TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, action, "add");
128 cmdline_parse_token_string_t cmd_obj_name = TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, name, NULL);
129 cmdline_parse_token_ipaddr_t cmd_obj_ip = TOKEN_IPADDR_INITIALIZER(struct cmd_obj_add_result, ip);
131 cmdline_parse_inst_t cmd_obj_add = {
132 .f = cmd_obj_add_parsed, /* function to call */
133 .data = NULL, /* 2nd arg of func */
134 .help_str = "Add an object (name, val)",
135 .tokens = { /* token list, NULL terminated */
136 (void *)&cmd_obj_action_add,
137 (void *)&cmd_obj_name,
144 /**********************************************************/
145 /**********************************************************/
146 /****** CONTEXT (list of instruction) */
149 cmdline_parse_ctx_t main_ctx[] = {
150 (cmdline_parse_inst_t *)&cmd_obj_del_show,
151 (cmdline_parse_inst_t *)&cmd_obj_add,