fbef8c95d85070507889d76dc0553f460aacdb7f
[libcmdline.git] / src / extension_example / commands.c
1 /*
2  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
15  *
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.
26  */
27
28 #include <math.h>
29 #include <stdio.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35
36 #include <cmdline_parse.h>
37 #include <cmdline_parse_ipaddr.h>
38 #include <cmdline_parse_num.h>
39 #include <cmdline_parse_string.h>
40 #include <cmdline.h>
41
42 #include "parse_obj_list.h"
43
44 struct object_list global_obj_list;
45
46 /**********************************************************/
47
48 struct cmd_obj_del_show_result {
49         cmdline_fixed_string_t action;
50         struct object *obj;
51 };
52
53 static void cmd_obj_del_show_parsed(void *parsed_result, 
54                                 struct cmdline *cl,
55                                 void *data)
56 {
57         struct cmd_obj_del_show_result *res = parsed_result;
58         char ip_str[INET6_ADDRSTRLEN];
59         
60         inet_ntop(res->obj->ip.family, (void *)&res->obj->ip.addr,
61                   ip_str, sizeof(ip_str));
62
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);
67                 free(res->obj);
68         }
69         else if (strcmp(res->action, "show") == 0) {
70                 cmdline_printf(cl, "Object %s, ip=%s\r\n", 
71                                res->obj->name, ip_str);
72         }
73 }
74
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);
77
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, 
84                 (void *)&cmd_obj_obj, 
85                 NULL,
86         },
87 };
88
89 /**********************************************************/
90
91 struct cmd_obj_add_result {
92         cmdline_fixed_string_t action;
93         cmdline_fixed_string_t name;
94         cmdline_ipaddr_t ip;
95 };
96
97 static void cmd_obj_add_parsed(void *parsed_result, 
98                                 struct cmdline *cl,
99                                 void *data)
100 {
101         struct cmd_obj_add_result *res = parsed_result;
102         struct object *o;
103         char ip_str[INET6_ADDRSTRLEN];
104         
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);
108                         return;
109                 }
110                 break;
111         }
112
113         o = malloc(sizeof(*o));
114         if (!o) {
115                 cmdline_printf(cl, "mem error\r\n");
116                 return;
117         }
118         strcpy(o->name, res->name);
119         o->ip = res->ip;
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", 
124                        o->name, ip_str);
125 }
126
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);
130
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, 
138                 (void *)&cmd_obj_ip,
139                 NULL,
140         },
141 };
142
143
144 /**********************************************************/
145 /**********************************************************/
146 /****** CONTEXT (list of instruction) */
147
148 /* in progmem */
149 cmdline_parse_ctx_t main_ctx[] = {
150         (cmdline_parse_inst_t *)&cmd_obj_del_show, 
151         (cmdline_parse_inst_t *)&cmd_obj_add, 
152         NULL,
153 };
154