xref: /dpdk/examples/cmdline/parse_obj_list.c (revision e9d48c0072d36eb6423b45fba4ec49d0def6c36f)
1af75078fSIntel /*-
2af75078fSIntel  *   BSD LICENSE
3af75078fSIntel  *
4*e9d48c00SBruce 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
87af75078fSIntel parse_obj_list(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
88af75078fSIntel {
89af75078fSIntel 	struct token_obj_list *tk2 = (struct token_obj_list *)tk;
90af75078fSIntel 	struct token_obj_list_data *tkd = &tk2->obj_list_data;
91af75078fSIntel 	struct object *o;
92af75078fSIntel 	unsigned int token_len = 0;
93af75078fSIntel 
94af75078fSIntel 	if (*buf == 0)
95af75078fSIntel 		return -1;
96af75078fSIntel 
97af75078fSIntel 	while(!cmdline_isendoftoken(buf[token_len]))
98af75078fSIntel 		token_len++;
99af75078fSIntel 
100af75078fSIntel 	SLIST_FOREACH(o, tkd->list, next) {
101af75078fSIntel 		if (token_len != strnlen(o->name, OBJ_NAME_LEN_MAX))
102af75078fSIntel 			continue;
103af75078fSIntel 		if (strncmp(buf, o->name, token_len))
104af75078fSIntel 			continue;
105af75078fSIntel 		break;
106af75078fSIntel 	}
107af75078fSIntel 	if (!o) /* not found */
108af75078fSIntel 		return -1;
109af75078fSIntel 
110af75078fSIntel 	/* store the address of object in structure */
111af75078fSIntel 	if (res)
112af75078fSIntel 		*(struct object **)res = o;
113af75078fSIntel 
114af75078fSIntel 	return token_len;
115af75078fSIntel }
116af75078fSIntel 
117af75078fSIntel int complete_get_nb_obj_list(cmdline_parse_token_hdr_t *tk)
118af75078fSIntel {
119af75078fSIntel 	struct token_obj_list *tk2 = (struct token_obj_list *)tk;
120af75078fSIntel 	struct token_obj_list_data *tkd = &tk2->obj_list_data;
121af75078fSIntel 	struct object *o;
122af75078fSIntel 	int ret = 0;
123af75078fSIntel 
124af75078fSIntel 	SLIST_FOREACH(o, tkd->list, next) {
125af75078fSIntel 		ret ++;
126af75078fSIntel 	}
127af75078fSIntel 	return ret;
128af75078fSIntel }
129af75078fSIntel 
130af75078fSIntel int complete_get_elt_obj_list(cmdline_parse_token_hdr_t *tk,
131af75078fSIntel 			      int idx, char *dstbuf, unsigned int size)
132af75078fSIntel {
133af75078fSIntel 	struct token_obj_list *tk2 = (struct token_obj_list *)tk;
134af75078fSIntel 	struct token_obj_list_data *tkd = &tk2->obj_list_data;
135af75078fSIntel 	struct object *o;
136af75078fSIntel 	int i = 0;
137af75078fSIntel 	unsigned len;
138af75078fSIntel 
139af75078fSIntel 	SLIST_FOREACH(o, tkd->list, next) {
140af75078fSIntel 		if (i++ == idx)
141af75078fSIntel 			break;
142af75078fSIntel 	}
143af75078fSIntel 	if (!o)
144af75078fSIntel 		return -1;
145af75078fSIntel 
146af75078fSIntel 	len = strnlen(o->name, OBJ_NAME_LEN_MAX);
147af75078fSIntel 	if ((len + 1) > size)
148af75078fSIntel 		return -1;
149af75078fSIntel 
150af75078fSIntel 	if (dstbuf)
151af75078fSIntel 		rte_snprintf(dstbuf, size, "%s", o->name);
152af75078fSIntel 
153af75078fSIntel 	return 0;
154af75078fSIntel }
155af75078fSIntel 
156af75078fSIntel 
157af75078fSIntel int get_help_obj_list(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,
158af75078fSIntel 		      char *dstbuf, unsigned int size)
159af75078fSIntel {
160af75078fSIntel 	rte_snprintf(dstbuf, size, "Obj-List");
161af75078fSIntel 	return 0;
162af75078fSIntel }
163