2 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "cmdline_parse.h"
34 #include "cmdline_parse_string.h"
36 struct cmdline_token_ops cmdline_token_string_ops = {
37 .parse = cmdline_parse_string,
38 .complete_get_nb = cmdline_complete_get_nb_string,
39 .complete_get_elt = cmdline_complete_get_elt_string,
40 .get_help = cmdline_get_help_string,
43 #define MULTISTRING_HELP "Mul-choice STRING"
44 #define ANYSTRING_HELP "Any STRING"
45 #define FIXEDSTRING_HELP "Fixed STRING"
48 get_token_len(const char *s)
54 while (c!='#' && c!='\0') {
62 get_next_token(const char *s)
72 cmdline_parse_string(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
74 struct cmdline_token_string *tk2 = (struct cmdline_token_string *)tk;
75 struct cmdline_token_string_data *sd = &tk2->string_data;;
76 unsigned int token_len;
86 token_len = get_token_len(str);
88 /* if token is too big... */
89 if (token_len >= STR_TOKEN_SIZE - 1) {
93 if ( strncmp(buf, str, token_len) ) {
97 if ( !cmdline_isendoftoken(*(buf+token_len)) ) {
102 } while ( (str = get_next_token(str)) != NULL );
107 /* unspecified string */
110 while(!cmdline_isendoftoken(buf[token_len]) &&
111 token_len < (STR_TOKEN_SIZE-1))
114 /* return if token too long */
115 if (token_len >= STR_TOKEN_SIZE - 1) {
121 /* we are sure that token_len is < STR_TOKEN_SIZE-1 */
122 strncpy(res, buf, token_len);
123 *((char *)res + token_len) = 0;
129 int cmdline_complete_get_nb_string(cmdline_parse_token_hdr_t *tk)
131 struct cmdline_token_string *tk2 = (struct cmdline_token_string *)tk;
132 struct cmdline_token_string_data *sd = &tk2->string_data;;
140 while( (str = get_next_token(str)) != NULL ) {
146 int cmdline_complete_get_elt_string(cmdline_parse_token_hdr_t *tk, int idx,
147 char *dstbuf, unsigned int size)
149 struct cmdline_token_string *tk2 = (struct cmdline_token_string *)tk;
150 struct cmdline_token_string_data *sd = &tk2->string_data;;
157 s = get_next_token(s);
162 len = get_token_len(s);
166 memcpy(dstbuf, s, len);
172 int cmdline_get_help_string(cmdline_parse_token_hdr_t *tk, char *dstbuf,
175 struct cmdline_token_string *tk2 = (struct cmdline_token_string *)tk;
176 struct cmdline_token_string_data *sd = &tk2->string_data;;
182 if (get_next_token(s)) {
183 strncpy(dstbuf, MULTISTRING_HELP, size);
186 strncpy(dstbuf, FIXEDSTRING_HELP, size);
190 strncpy(dstbuf, ANYSTRING_HELP, size);
193 dstbuf[size-1] = '\0';