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