net: provide IP-related API on any OS
[dpdk.git] / examples / cmdline / parse_obj_list.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation.
3  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
4  * All rights reserved.
5  */
6
7 #include <stdio.h>
8 #include <inttypes.h>
9 #include <errno.h>
10 #include <ctype.h>
11 #include <string.h>
12
13 #include <cmdline_parse.h>
14 #include <cmdline_parse_ipaddr.h>
15
16 #include <rte_string_fns.h>
17
18 #include "parse_obj_list.h"
19
20 /* This file is an example of extension of libcmdline. It provides an
21  * example of objects stored in a list. */
22
23 struct cmdline_token_ops token_obj_list_ops = {
24         .parse = parse_obj_list,
25         .complete_get_nb = complete_get_nb_obj_list,
26         .complete_get_elt = complete_get_elt_obj_list,
27         .get_help = get_help_obj_list,
28 };
29
30 int
31 parse_obj_list(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
32         unsigned ressize)
33 {
34         struct token_obj_list *tk2 = (struct token_obj_list *)tk;
35         struct token_obj_list_data *tkd = &tk2->obj_list_data;
36         struct object *o;
37         unsigned int token_len = 0;
38
39         if (*buf == 0)
40                 return -1;
41
42         if (res && ressize < sizeof(struct object *))
43                 return -1;
44
45         while(!cmdline_isendoftoken(buf[token_len]))
46                 token_len++;
47
48         SLIST_FOREACH(o, tkd->list, next) {
49                 if (token_len != strnlen(o->name, OBJ_NAME_LEN_MAX))
50                         continue;
51                 if (strncmp(buf, o->name, token_len))
52                         continue;
53                 break;
54         }
55         if (!o) /* not found */
56                 return -1;
57
58         /* store the address of object in structure */
59         if (res)
60                 *(struct object **)res = o;
61
62         return token_len;
63 }
64
65 int complete_get_nb_obj_list(cmdline_parse_token_hdr_t *tk)
66 {
67         struct token_obj_list *tk2 = (struct token_obj_list *)tk;
68         struct token_obj_list_data *tkd = &tk2->obj_list_data;
69         struct object *o;
70         int ret = 0;
71
72         SLIST_FOREACH(o, tkd->list, next) {
73                 ret ++;
74         }
75         return ret;
76 }
77
78 int complete_get_elt_obj_list(cmdline_parse_token_hdr_t *tk,
79                               int idx, char *dstbuf, unsigned int size)
80 {
81         struct token_obj_list *tk2 = (struct token_obj_list *)tk;
82         struct token_obj_list_data *tkd = &tk2->obj_list_data;
83         struct object *o;
84         int i = 0;
85         unsigned len;
86
87         SLIST_FOREACH(o, tkd->list, next) {
88                 if (i++ == idx)
89                         break;
90         }
91         if (!o)
92                 return -1;
93
94         len = strnlen(o->name, OBJ_NAME_LEN_MAX);
95         if ((len + 1) > size)
96                 return -1;
97
98         if (dstbuf)
99                 strlcpy(dstbuf, o->name, size);
100
101         return 0;
102 }
103
104
105 int get_help_obj_list(__rte_unused cmdline_parse_token_hdr_t *tk,
106                       char *dstbuf, unsigned int size)
107 {
108         snprintf(dstbuf, size, "Obj-List");
109         return 0;
110 }