cmdline: big rework and clean of cmdline library
[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 <stdarg.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36
37 #include <cmdline_parse.h>
38 #include <cmdline_parse_ipaddr.h>
39 #include <cmdline_parse_num.h>
40 #include <cmdline_parse_string.h>
41 #include <cmdline.h>
42
43 #include "parse_obj_list.h"
44
45 struct object_list global_obj_list;
46
47 /**********************************************************/
48
49 struct cmd_obj_del_show_result {
50         cmdline_fixed_string_t action;
51         struct object *obj;
52 };
53
54 static void cmd_obj_del_show_parsed(void *parsed_result, 
55                                 struct cmdline *cl,
56                                 void *data)
57 {
58         struct cmd_obj_del_show_result *res = parsed_result;
59         char ip_str[INET6_ADDRSTRLEN];
60         
61         inet_ntop(res->obj->ip.family, (void *)&res->obj->ip.addr,
62                   ip_str, sizeof(ip_str));
63
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);
68                 free(res->obj);
69         }
70         else if (strcmp(res->action, "show") == 0) {
71                 cmdline_printf(cl, "Object %s, ip=%s\r\n", 
72                                res->obj->name, ip_str);
73         }
74 }
75
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);
78
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, 
85                 (void *)&cmd_obj_obj, 
86                 NULL,
87         },
88 };
89
90 /**********************************************************/
91
92 struct cmd_obj_add_result {
93         cmdline_fixed_string_t action;
94         cmdline_fixed_string_t name;
95         cmdline_ipaddr_t ip;
96 };
97
98 static void cmd_obj_add_parsed(void *parsed_result, 
99                                 struct cmdline *cl,
100                                 void *data)
101 {
102         struct cmd_obj_add_result *res = parsed_result;
103         struct object *o;
104         char ip_str[INET6_ADDRSTRLEN];
105         
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);
109                         return;
110                 }
111                 break;
112         }
113
114         o = malloc(sizeof(*o));
115         if (!o) {
116                 cmdline_printf(cl, "mem error\r\n");
117                 return;
118         }
119         strcpy(o->name, res->name);
120         o->ip = res->ip;
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", 
125                        o->name, ip_str);
126 }
127
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);
131
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, 
139                 (void *)&cmd_obj_ip,
140                 NULL,
141         },
142 };
143
144
145 /**********************************************************/
146 /**********************************************************/
147 /****** CONTEXT (list of instruction) */
148
149 /* in progmem */
150 cmdline_parse_ctx_t main_ctx = {
151         .name = "main",
152         .insts = {
153                 &cmd_obj_del_show,
154                 &cmd_obj_add,
155                 NULL,
156         },
157 };
158