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