cmdline (merge-intel): fix whitespaces
[libcmdline.git] / src / lib / cmdline_parse_string.c
1 /*
2  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
15  *
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.
26  */
27
28 #include <stdio.h>
29 #include <inttypes.h>
30 #include <ctype.h>
31 #include <string.h>
32
33 #include "cmdline_parse.h"
34 #include "cmdline_parse_string.h"
35
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,
41 };
42
43 #define MULTISTRING_HELP "Mul-choice STRING"
44 #define ANYSTRING_HELP   "Any STRING"
45 #define FIXEDSTRING_HELP "Fixed STRING"
46
47 static unsigned int
48 get_token_len(const char *s)
49 {
50         char c;
51         unsigned int i=0;
52
53         c = s[i];
54         while (c!='#' && c!='\0') {
55                 i++;
56                 c = s[i];
57         }
58         return i;
59 }
60
61 static const char *
62 get_next_token(const char *s)
63 {
64         unsigned int i;
65         i = get_token_len(s);
66         if (s[i] == '#')
67                 return s+i+1;
68         return NULL;
69 }
70
71 int
72 cmdline_parse_string(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
73 {
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;
77         const char *str;
78
79         if (! *buf)
80                 return -1;
81
82         /* fixed string */
83         if (sd->str) {
84                 str = sd->str;
85                 do {
86                         token_len = get_token_len(str);
87
88                         /* if token is too big... */
89                         if (token_len >= STR_TOKEN_SIZE - 1) {
90                                 continue;
91                         }
92
93                         if ( strncmp(buf, str, token_len) ) {
94                                 continue;
95                         }
96
97                         if ( !cmdline_isendoftoken(*(buf+token_len)) ) {
98                                 continue;
99                         }
100
101                         break;
102                 } while ( (str = get_next_token(str)) != NULL );
103
104                 if (!str)
105                         return -1;
106         }
107         /* unspecified string */
108         else {
109                 token_len=0;
110                 while(!cmdline_isendoftoken(buf[token_len]) &&
111                       token_len < (STR_TOKEN_SIZE-1))
112                         token_len++;
113
114                 /* return if token too long */
115                 if (token_len >= STR_TOKEN_SIZE - 1) {
116                         return -1;
117                 }
118         }
119
120         if (res) {
121                 /* we are sure that token_len is < STR_TOKEN_SIZE-1 */
122                 strncpy(res, buf, token_len);
123                 *((char *)res + token_len) = 0;
124         }
125
126         return token_len;
127 }
128
129 int cmdline_complete_get_nb_string(cmdline_parse_token_hdr_t *tk)
130 {
131         struct cmdline_token_string *tk2 = (struct cmdline_token_string *)tk;
132         struct cmdline_token_string_data *sd = &tk2->string_data;;
133         int ret=1;
134         const char *str;
135
136         if (!sd->str)
137                 return 0;
138
139         str = sd->str;
140         while( (str = get_next_token(str)) != NULL ) {
141                 ret++;
142         }
143         return ret;
144 }
145
146 int cmdline_complete_get_elt_string(cmdline_parse_token_hdr_t *tk, int idx,
147                                     char *dstbuf, unsigned int size)
148 {
149         struct cmdline_token_string *tk2 = (struct cmdline_token_string *)tk;
150         struct cmdline_token_string_data *sd = &tk2->string_data;;
151         const char *s;
152         unsigned int len;
153
154         s = sd->str;
155
156         while (idx-- && s)
157                 s = get_next_token(s);
158
159         if (!s)
160                 return -1;
161
162         len = get_token_len(s);
163         if (len > size - 1)
164                 return -1;
165
166         memcpy(dstbuf, s, len);
167         dstbuf[len] = '\0';
168         return 0;
169 }
170
171
172 int cmdline_get_help_string(cmdline_parse_token_hdr_t *tk, char *dstbuf,
173                             unsigned int size)
174 {
175         struct cmdline_token_string *tk2 = (struct cmdline_token_string *)tk;
176         struct cmdline_token_string_data *sd = &tk2->string_data;;
177         const char *s;
178
179         s = sd->str;
180
181         if (s) {
182                 if (get_next_token(s)) {
183                         strncpy(dstbuf, MULTISTRING_HELP, size);
184                 }
185                 else {
186                         strncpy(dstbuf, FIXEDSTRING_HELP, size);
187                 }
188         }
189         else {
190                 strncpy(dstbuf, ANYSTRING_HELP, size);
191         }
192
193         dstbuf[size-1] = '\0';
194
195         return 0;
196 }