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