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