]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * getopt.c | |
3 | */ | |
4 | ||
5 | #include <linux/kernel.h> | |
6 | #include <linux/string.h> | |
7 | ||
8 | #include <asm/errno.h> | |
9 | ||
10 | #include "getopt.h" | |
11 | ||
12 | /** | |
13 | * ncp_getopt - option parser | |
14 | * @caller: name of the caller, for error messages | |
15 | * @options: the options string | |
16 | * @opts: an array of &struct option entries controlling parser operations | |
17 | * @optopt: output; will contain the current option | |
18 | * @optarg: output; will contain the value (if one exists) | |
1da177e4 LT |
19 | * @value: output; may be NULL; will be overwritten with the integer value |
20 | * of the current argument. | |
21 | * | |
22 | * Helper to parse options on the format used by mount ("a=b,c=d,e,f"). | |
23 | * Returns opts->val if a matching entry in the 'opts' array is found, | |
24 | * 0 when no more tokens are found, -1 if an error is encountered. | |
25 | */ | |
26 | int ncp_getopt(const char *caller, char **options, const struct ncp_option *opts, | |
27 | char **optopt, char **optarg, unsigned long *value) | |
28 | { | |
29 | char *token; | |
30 | char *val; | |
31 | ||
32 | do { | |
33 | if ((token = strsep(options, ",")) == NULL) | |
34 | return 0; | |
35 | } while (*token == '\0'); | |
36 | if (optopt) | |
37 | *optopt = token; | |
38 | ||
39 | if ((val = strchr (token, '=')) != NULL) { | |
40 | *val++ = 0; | |
41 | } | |
42 | *optarg = val; | |
43 | for (; opts->name; opts++) { | |
44 | if (!strcmp(opts->name, token)) { | |
45 | if (!val) { | |
46 | if (opts->has_arg & OPT_NOPARAM) { | |
47 | return opts->val; | |
48 | } | |
49 | printk(KERN_INFO "%s: the %s option requires an argument\n", | |
50 | caller, token); | |
51 | return -EINVAL; | |
52 | } | |
53 | if (opts->has_arg & OPT_INT) { | |
54 | char* v; | |
55 | ||
56 | *value = simple_strtoul(val, &v, 0); | |
57 | if (!*v) { | |
58 | return opts->val; | |
59 | } | |
60 | printk(KERN_INFO "%s: invalid numeric value in %s=%s\n", | |
61 | caller, token, val); | |
62 | return -EDOM; | |
63 | } | |
64 | if (opts->has_arg & OPT_STRING) { | |
65 | return opts->val; | |
66 | } | |
67 | printk(KERN_INFO "%s: unexpected argument %s to the %s option\n", | |
68 | caller, val, token); | |
69 | return -EINVAL; | |
70 | } | |
71 | } | |
72 | printk(KERN_INFO "%s: Unrecognized mount option %s\n", caller, token); | |
73 | return -EOPNOTSUPP; | |
74 | } |