1 /* Helpers for initial module or kernel cmdline parsing
2 Copyright (C) 2001 Rusty Russell.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include <linux/moduleparam.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/module.h>
27 #define DEBUGP(fmt, a...)
30 static inline int dash2underscore(char c
)
37 static inline int parameq(const char *input
, const char *paramname
)
40 for (i
= 0; dash2underscore(input
[i
]) == paramname
[i
]; i
++)
46 static int parse_one(char *param
,
48 struct kernel_param
*params
,
50 int (*handle_unknown
)(char *param
, char *val
))
55 for (i
= 0; i
< num_params
; i
++) {
56 if (parameq(param
, params
[i
].name
)) {
57 DEBUGP("They are equal! Calling %p\n",
59 return params
[i
].set(val
, ¶ms
[i
]);
64 DEBUGP("Unknown argument: calling %p\n", handle_unknown
);
65 return handle_unknown(param
, val
);
68 DEBUGP("Unknown argument `%s'\n", param
);
72 /* You can use " around spaces, but can't escape ". */
73 /* Hyphens and underscores equivalent in parameter names. */
74 static char *next_arg(char *args
, char **param
, char **val
)
76 unsigned int i
, equals
= 0;
79 /* Chew any extra spaces */
80 while (*args
== ' ') args
++;
82 for (i
= 0; args
[i
]; i
++) {
83 if (args
[i
] == ' ' && !in_quote
)
98 *val
= args
+ equals
+ 1;
100 /* Don't include quotes in value. */
103 if (args
[i
-1] == '"')
115 /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
116 int parse_args(const char *name
,
118 struct kernel_param
*params
,
120 int (*unknown
)(char *param
, char *val
))
124 DEBUGP("Parsing ARGS: %s\n", args
);
129 args
= next_arg(args
, ¶m
, &val
);
130 ret
= parse_one(param
, val
, params
, num
, unknown
);
133 printk(KERN_ERR
"%s: Unknown parameter `%s'\n",
138 "%s: `%s' too large for parameter `%s'\n",
139 name
, val
?: "", param
);
145 "%s: `%s' invalid for parameter `%s'\n",
146 name
, val
?: "", param
);
155 /* Lazy bastard, eh? */
156 #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \
157 int param_set_##name(const char *val, struct kernel_param *kp) \
162 if (!val) return -EINVAL; \
163 l = strtolfn(val, &endp, 0); \
164 if (endp == val || ((type)l != l)) \
166 *((type *)kp->arg) = l; \
169 int param_get_##name(char *buffer, struct kernel_param *kp) \
171 return sprintf(buffer, format, *((type *)kp->arg)); \
174 STANDARD_PARAM_DEF(byte
, unsigned char, "%c", unsigned long, simple_strtoul
);
175 STANDARD_PARAM_DEF(short, short, "%hi", long, simple_strtol
);
176 STANDARD_PARAM_DEF(ushort
, unsigned short, "%hu", unsigned long, simple_strtoul
);
177 STANDARD_PARAM_DEF(int, int, "%i", long, simple_strtol
);
178 STANDARD_PARAM_DEF(uint
, unsigned int, "%u", unsigned long, simple_strtoul
);
179 STANDARD_PARAM_DEF(long, long, "%li", long, simple_strtol
);
180 STANDARD_PARAM_DEF(ulong
, unsigned long, "%lu", unsigned long, simple_strtoul
);
182 int param_set_charp(const char *val
, struct kernel_param
*kp
)
185 printk(KERN_ERR
"%s: string parameter expected\n",
190 if (strlen(val
) > 1024) {
191 printk(KERN_ERR
"%s: string parameter too long\n",
196 *(char **)kp
->arg
= (char *)val
;
200 int param_get_charp(char *buffer
, struct kernel_param
*kp
)
202 return sprintf(buffer
, "%s", *((char **)kp
->arg
));
205 int param_set_bool(const char *val
, struct kernel_param
*kp
)
207 /* No equals means "set"... */
210 /* One of =[yYnN01] */
212 case 'y': case 'Y': case '1':
215 case 'n': case 'N': case '0':
222 int param_get_bool(char *buffer
, struct kernel_param
*kp
)
224 /* Y and N chosen as being relatively non-coder friendly */
225 return sprintf(buffer
, "%c", (*(int *)kp
->arg
) ? 'Y' : 'N');
228 int param_set_invbool(const char *val
, struct kernel_param
*kp
)
231 struct kernel_param dummy
= { .arg
= &boolval
};
233 ret
= param_set_bool(val
, &dummy
);
235 *(int *)kp
->arg
= !boolval
;
239 int param_get_invbool(char *buffer
, struct kernel_param
*kp
)
242 struct kernel_param dummy
= { .arg
= &val
};
244 val
= !*(int *)kp
->arg
;
245 return param_get_bool(buffer
, &dummy
);
248 /* We cheat here and temporarily mangle the string. */
249 int param_array(const char *name
,
251 unsigned int min
, unsigned int max
,
252 void *elem
, int elemsize
,
253 int (*set
)(const char *, struct kernel_param
*kp
),
257 struct kernel_param kp
;
260 /* Get the name right for errors. */
264 /* No equals sign? */
266 printk(KERN_ERR
"%s: expects arguments\n", name
);
271 /* We expect a comma-separated list of values. */
276 printk(KERN_ERR
"%s: can only take %i arguments\n",
280 len
= strcspn(val
, ",");
282 /* nul-terminate and parse */
284 ((char *)val
)[len
] = '\0';
292 } while (save
== ',');
295 printk(KERN_ERR
"%s: needs at least %i arguments\n",
302 int param_array_set(const char *val
, struct kernel_param
*kp
)
304 struct kparam_array
*arr
= kp
->arg
;
306 return param_array(kp
->name
, val
, 1, arr
->max
, arr
->elem
,
307 arr
->elemsize
, arr
->set
, arr
->num
);
310 int param_array_get(char *buffer
, struct kernel_param
*kp
)
313 struct kparam_array
*arr
= kp
->arg
;
314 struct kernel_param p
;
317 for (i
= off
= 0; i
< *arr
->num
; i
++) {
320 p
.arg
= arr
->elem
+ arr
->elemsize
* i
;
321 ret
= arr
->get(buffer
+ off
, &p
);
330 int param_set_copystring(const char *val
, struct kernel_param
*kp
)
332 struct kparam_string
*kps
= kp
->arg
;
334 if (strlen(val
)+1 > kps
->maxlen
) {
335 printk(KERN_ERR
"%s: string doesn't fit in %u chars.\n",
336 kp
->name
, kps
->maxlen
-1);
339 strcpy(kps
->string
, val
);
343 int param_get_string(char *buffer
, struct kernel_param
*kp
)
345 struct kparam_string
*kps
= kp
->arg
;
346 return strlcpy(buffer
, kps
->string
, kps
->maxlen
);
349 EXPORT_SYMBOL(param_set_byte
);
350 EXPORT_SYMBOL(param_get_byte
);
351 EXPORT_SYMBOL(param_set_short
);
352 EXPORT_SYMBOL(param_get_short
);
353 EXPORT_SYMBOL(param_set_ushort
);
354 EXPORT_SYMBOL(param_get_ushort
);
355 EXPORT_SYMBOL(param_set_int
);
356 EXPORT_SYMBOL(param_get_int
);
357 EXPORT_SYMBOL(param_set_uint
);
358 EXPORT_SYMBOL(param_get_uint
);
359 EXPORT_SYMBOL(param_set_long
);
360 EXPORT_SYMBOL(param_get_long
);
361 EXPORT_SYMBOL(param_set_ulong
);
362 EXPORT_SYMBOL(param_get_ulong
);
363 EXPORT_SYMBOL(param_set_charp
);
364 EXPORT_SYMBOL(param_get_charp
);
365 EXPORT_SYMBOL(param_set_bool
);
366 EXPORT_SYMBOL(param_get_bool
);
367 EXPORT_SYMBOL(param_set_invbool
);
368 EXPORT_SYMBOL(param_get_invbool
);
369 EXPORT_SYMBOL(param_array_set
);
370 EXPORT_SYMBOL(param_array_get
);
371 EXPORT_SYMBOL(param_set_copystring
);
372 EXPORT_SYMBOL(param_get_string
);