New upstream version 18.02
[deb_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 <stdarg.h>
10 #include <errno.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include <netinet/in.h>
14
15 #include <cmdline_parse.h>
16 #include <cmdline_parse_ipaddr.h>
17
18 #include <rte_string_fns.h>
19
20 #include "parse_obj_list.h"
21
22 /* This file is an example of extension of libcmdline. It provides an
23  * example of objects stored in a list. */
24
25 struct cmdline_token_ops token_obj_list_ops = {
26         .parse = parse_obj_list,
27         .complete_get_nb = complete_get_nb_obj_list,
28         .complete_get_elt = complete_get_elt_obj_list,
29         .get_help = get_help_obj_list,
30 };
31
32 int
33 parse_obj_list(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
34         unsigned ressize)
35 {
36         struct token_obj_list *tk2 = (struct token_obj_list *)tk;
37         struct token_obj_list_data *tkd = &tk2->obj_list_data;
38         struct object *o;
39         unsigned int token_len = 0;
40
41         if (*buf == 0)
42                 return -1;
43
44         if (res && ressize < sizeof(struct object *))
45                 return -1;
46
47         while(!cmdline_isendoftoken(buf[token_len]))
48                 token_len++;
49
50         SLIST_FOREACH(o, tkd->list, next) {
51                 if (token_len != strnlen(o->name, OBJ_NAME_LEN_MAX))
52                         continue;
53                 if (strncmp(buf, o->name, token_len))
54                         continue;
55                 break;
56         }
57         if (!o) /* not found */
58                 return -1;
59
60         /* store the address of object in structure */
61         if (res)
62                 *(struct object **)res = o;
63
64         return token_len;
65 }
66
67 int complete_get_nb_obj_list(cmdline_parse_token_hdr_t *tk)
68 {
69         struct token_obj_list *tk2 = (struct token_obj_list *)tk;
70         struct token_obj_list_data *tkd = &tk2->obj_list_data;
71         struct object *o;
72         int ret = 0;
73
74         SLIST_FOREACH(o, tkd->list, next) {
75                 ret ++;
76         }
77         return ret;
78 }
79
80 int complete_get_elt_obj_list(cmdline_parse_token_hdr_t *tk,
81                               int idx, char *dstbuf, unsigned int size)
82 {
83         struct token_obj_list *tk2 = (struct token_obj_list *)tk;
84         struct token_obj_list_data *tkd = &tk2->obj_list_data;
85         struct object *o;
86         int i = 0;
87         unsigned len;
88
89         SLIST_FOREACH(o, tkd->list, next) {
90                 if (i++ == idx)
91                         break;
92         }
93         if (!o)
94                 return -1;
95
96         len = strnlen(o->name, OBJ_NAME_LEN_MAX);
97         if ((len + 1) > size)
98                 return -1;
99
100         if (dstbuf)
101                 snprintf(dstbuf, size, "%s", o->name);
102
103         return 0;
104 }
105
106
107 int get_help_obj_list(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,
108                       char *dstbuf, unsigned int size)
109 {
110         snprintf(dstbuf, size, "Obj-List");
111         return 0;
112 }