xref: /dpdk/examples/cmdline/parse_obj_list.c (revision aaa662e75c23c61a1d79bd4d1f9f35b4967c39db)
1af75078fSIntel /*-
2af75078fSIntel  *   BSD LICENSE
3af75078fSIntel  *
4e9d48c00SBruce Richardson  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5af75078fSIntel  *   All rights reserved.
6af75078fSIntel  *
7af75078fSIntel  *   Redistribution and use in source and binary forms, with or without
8af75078fSIntel  *   modification, are permitted provided that the following conditions
9af75078fSIntel  *   are met:
10af75078fSIntel  *
11af75078fSIntel  *     * Redistributions of source code must retain the above copyright
12af75078fSIntel  *       notice, this list of conditions and the following disclaimer.
13af75078fSIntel  *     * Redistributions in binary form must reproduce the above copyright
14af75078fSIntel  *       notice, this list of conditions and the following disclaimer in
15af75078fSIntel  *       the documentation and/or other materials provided with the
16af75078fSIntel  *       distribution.
17af75078fSIntel  *     * Neither the name of Intel Corporation nor the names of its
18af75078fSIntel  *       contributors may be used to endorse or promote products derived
19af75078fSIntel  *       from this software without specific prior written permission.
20af75078fSIntel  *
21af75078fSIntel  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22af75078fSIntel  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23af75078fSIntel  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24af75078fSIntel  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25af75078fSIntel  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26af75078fSIntel  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27af75078fSIntel  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28af75078fSIntel  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29af75078fSIntel  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30af75078fSIntel  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31af75078fSIntel  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32af75078fSIntel  */
33af75078fSIntel 
34af75078fSIntel /*
35af75078fSIntel  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
36af75078fSIntel  * All rights reserved.
37af75078fSIntel  * Redistribution and use in source and binary forms, with or without
38af75078fSIntel  * modification, are permitted provided that the following conditions are met:
39af75078fSIntel  *
40af75078fSIntel  *     * Redistributions of source code must retain the above copyright
41af75078fSIntel  *       notice, this list of conditions and the following disclaimer.
42af75078fSIntel  *     * Redistributions in binary form must reproduce the above copyright
43af75078fSIntel  *       notice, this list of conditions and the following disclaimer in the
44af75078fSIntel  *       documentation and/or other materials provided with the distribution.
45af75078fSIntel  *     * Neither the name of the University of California, Berkeley nor the
46af75078fSIntel  *       names of its contributors may be used to endorse or promote products
47af75078fSIntel  *       derived from this software without specific prior written permission.
48af75078fSIntel  *
49af75078fSIntel  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
50af75078fSIntel  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51af75078fSIntel  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52af75078fSIntel  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
53af75078fSIntel  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54af75078fSIntel  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55af75078fSIntel  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
56af75078fSIntel  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57af75078fSIntel  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58af75078fSIntel  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59af75078fSIntel  */
60af75078fSIntel 
61af75078fSIntel #include <stdio.h>
62af75078fSIntel #include <inttypes.h>
63af75078fSIntel #include <stdarg.h>
64af75078fSIntel #include <errno.h>
65af75078fSIntel #include <ctype.h>
66af75078fSIntel #include <string.h>
67af75078fSIntel #include <netinet/in.h>
68af75078fSIntel 
69af75078fSIntel #include <cmdline_parse.h>
70af75078fSIntel #include <cmdline_parse_ipaddr.h>
71af75078fSIntel 
72af75078fSIntel #include <rte_string_fns.h>
73af75078fSIntel 
74af75078fSIntel #include "parse_obj_list.h"
75af75078fSIntel 
76af75078fSIntel /* This file is an example of extension of libcmdline. It provides an
77af75078fSIntel  * example of objects stored in a list. */
78af75078fSIntel 
79af75078fSIntel struct cmdline_token_ops token_obj_list_ops = {
80af75078fSIntel 	.parse = parse_obj_list,
81af75078fSIntel 	.complete_get_nb = complete_get_nb_obj_list,
82af75078fSIntel 	.complete_get_elt = complete_get_elt_obj_list,
83af75078fSIntel 	.get_help = get_help_obj_list,
84af75078fSIntel };
85af75078fSIntel 
86af75078fSIntel int
87*aaa662e7SAlan Carew parse_obj_list(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
88*aaa662e7SAlan Carew 	unsigned ressize)
89af75078fSIntel {
90af75078fSIntel 	struct token_obj_list *tk2 = (struct token_obj_list *)tk;
91af75078fSIntel 	struct token_obj_list_data *tkd = &tk2->obj_list_data;
92af75078fSIntel 	struct object *o;
93af75078fSIntel 	unsigned int token_len = 0;
94af75078fSIntel 
95af75078fSIntel 	if (*buf == 0)
96af75078fSIntel 		return -1;
97af75078fSIntel 
98*aaa662e7SAlan Carew 	if (res && ressize < sizeof(struct object *))
99*aaa662e7SAlan Carew 		return -1;
100*aaa662e7SAlan Carew 
101af75078fSIntel 	while(!cmdline_isendoftoken(buf[token_len]))
102af75078fSIntel 		token_len++;
103af75078fSIntel 
104af75078fSIntel 	SLIST_FOREACH(o, tkd->list, next) {
105af75078fSIntel 		if (token_len != strnlen(o->name, OBJ_NAME_LEN_MAX))
106af75078fSIntel 			continue;
107af75078fSIntel 		if (strncmp(buf, o->name, token_len))
108af75078fSIntel 			continue;
109af75078fSIntel 		break;
110af75078fSIntel 	}
111af75078fSIntel 	if (!o) /* not found */
112af75078fSIntel 		return -1;
113af75078fSIntel 
114af75078fSIntel 	/* store the address of object in structure */
115af75078fSIntel 	if (res)
116af75078fSIntel 		*(struct object **)res = o;
117af75078fSIntel 
118af75078fSIntel 	return token_len;
119af75078fSIntel }
120af75078fSIntel 
121af75078fSIntel int complete_get_nb_obj_list(cmdline_parse_token_hdr_t *tk)
122af75078fSIntel {
123af75078fSIntel 	struct token_obj_list *tk2 = (struct token_obj_list *)tk;
124af75078fSIntel 	struct token_obj_list_data *tkd = &tk2->obj_list_data;
125af75078fSIntel 	struct object *o;
126af75078fSIntel 	int ret = 0;
127af75078fSIntel 
128af75078fSIntel 	SLIST_FOREACH(o, tkd->list, next) {
129af75078fSIntel 		ret ++;
130af75078fSIntel 	}
131af75078fSIntel 	return ret;
132af75078fSIntel }
133af75078fSIntel 
134af75078fSIntel int complete_get_elt_obj_list(cmdline_parse_token_hdr_t *tk,
135af75078fSIntel 			      int idx, char *dstbuf, unsigned int size)
136af75078fSIntel {
137af75078fSIntel 	struct token_obj_list *tk2 = (struct token_obj_list *)tk;
138af75078fSIntel 	struct token_obj_list_data *tkd = &tk2->obj_list_data;
139af75078fSIntel 	struct object *o;
140af75078fSIntel 	int i = 0;
141af75078fSIntel 	unsigned len;
142af75078fSIntel 
143af75078fSIntel 	SLIST_FOREACH(o, tkd->list, next) {
144af75078fSIntel 		if (i++ == idx)
145af75078fSIntel 			break;
146af75078fSIntel 	}
147af75078fSIntel 	if (!o)
148af75078fSIntel 		return -1;
149af75078fSIntel 
150af75078fSIntel 	len = strnlen(o->name, OBJ_NAME_LEN_MAX);
151af75078fSIntel 	if ((len + 1) > size)
152af75078fSIntel 		return -1;
153af75078fSIntel 
154af75078fSIntel 	if (dstbuf)
1556f41fe75SStephen Hemminger 		snprintf(dstbuf, size, "%s", o->name);
156af75078fSIntel 
157af75078fSIntel 	return 0;
158af75078fSIntel }
159af75078fSIntel 
160af75078fSIntel 
161af75078fSIntel int get_help_obj_list(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,
162af75078fSIntel 		      char *dstbuf, unsigned int size)
163af75078fSIntel {
1646f41fe75SStephen Hemminger 	snprintf(dstbuf, size, "Obj-List");
165af75078fSIntel 	return 0;
166af75078fSIntel }
167