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.
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
37 #include <cmdline_parse.h>
38 #include <cmdline_parse_ipaddr.h>
39 #include <cmdline_parse_num.h>
40 #include <cmdline_parse_string.h>
43 #include "parse_obj_list.h"
45 struct object_list global_obj_list;
47 /**********************************************************/
49 struct cmd_obj_del_show_result {
50 cmdline_fixed_string_t action;
54 static void cmd_obj_del_show_parsed(void *parsed_result,
58 struct cmd_obj_del_show_result *res = parsed_result;
59 char ip_str[INET6_ADDRSTRLEN];
61 inet_ntop(res->obj->ip.family, (void *)&res->obj->ip.addr,
62 ip_str, sizeof(ip_str));
64 if (strcmp(res->action, "del") == 0) {
65 SLIST_REMOVE(&global_obj_list, res->obj, object, next);
66 cmdline_printf(cl, "Object %s removed, ip=%s\r\n",
67 res->obj->name, ip_str);
70 else if (strcmp(res->action, "show") == 0) {
71 cmdline_printf(cl, "Object %s, ip=%s\r\n",
72 res->obj->name, ip_str);
76 cmdline_parse_token_string_t cmd_obj_action = TOKEN_STRING_INITIALIZER(struct cmd_obj_del_show_result, action, "show#del");
77 parse_token_obj_list_t cmd_obj_obj = TOKEN_OBJ_LIST_INITIALIZER(struct cmd_obj_del_show_result, obj, &global_obj_list);
79 cmdline_parse_inst_t cmd_obj_del_show = {
80 .f = cmd_obj_del_show_parsed, /* function to call */
81 .data = NULL, /* 2nd arg of func */
82 .help_str = "Show/del an object",
83 .tokens = { /* token list, NULL terminated */
84 (void *)&cmd_obj_action,
90 /**********************************************************/
92 struct cmd_obj_add_result {
93 cmdline_fixed_string_t action;
94 cmdline_fixed_string_t name;
98 static void cmd_obj_add_parsed(void *parsed_result,
102 struct cmd_obj_add_result *res = parsed_result;
104 char ip_str[INET6_ADDRSTRLEN];
106 SLIST_FOREACH(o, &global_obj_list, next) {
107 if (!strcmp(res->name, o->name)) {
108 cmdline_printf(cl, "Object %s already exist\n", res->name);
114 o = malloc(sizeof(*o));
116 cmdline_printf(cl, "mem error\r\n");
119 strcpy(o->name, res->name);
121 SLIST_INSERT_HEAD(&global_obj_list, o, next);
122 inet_ntop(o->ip.family, (void *)&o->ip.addr,
123 ip_str, sizeof(ip_str));
124 cmdline_printf(cl, "Object %s added, ip=%s\r\n",
128 cmdline_parse_token_string_t cmd_obj_action_add = TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, action, "add");
129 cmdline_parse_token_string_t cmd_obj_name = TOKEN_STRING_INITIALIZER(struct cmd_obj_add_result, name, NULL);
130 cmdline_parse_token_ipaddr_t cmd_obj_ip = TOKEN_IPADDR_INITIALIZER(struct cmd_obj_add_result, ip);
132 cmdline_parse_inst_t cmd_obj_add = {
133 .f = cmd_obj_add_parsed, /* function to call */
134 .data = NULL, /* 2nd arg of func */
135 .help_str = "Add an object (name, val)",
136 .tokens = { /* token list, NULL terminated */
137 (void *)&cmd_obj_action_add,
138 (void *)&cmd_obj_name,
145 /**********************************************************/
146 /**********************************************************/
147 /****** CONTEXT (list of instruction) */
150 cmdline_parse_ctx_t main_ctx = {