1178784Skmacy/* 2178784Skmacy * lifted from fs/ncpfs/getopt.c 3178784Skmacy * 4178784Skmacy */ 5178784Skmacy#include <sys/cdefs.h> 6178784Skmacy__FBSDID("$FreeBSD: stable/11/sys/contrib/rdma/krping_compat/getopt.c 256470 2013-10-14 23:02:05Z np $"); 7178784Skmacy 8256470Snp#include <sys/types.h> 9256470Snp#include <linux/kernel.h> 10256470Snp#include <linux/string.h> 11256470Snp 12178784Skmacy#include "getopt.h" 13178784Skmacy 14178784Skmacy/** 15178784Skmacy * krping_getopt - option parser 16178784Skmacy * @caller: name of the caller, for error messages 17178784Skmacy * @options: the options string 18178784Skmacy * @opts: an array of &struct option entries controlling parser operations 19178784Skmacy * @optopt: output; will contain the current option 20178784Skmacy * @optarg: output; will contain the value (if one exists) 21178784Skmacy * @flag: output; may be NULL; should point to a long for or'ing flags 22178784Skmacy * @value: output; may be NULL; will be overwritten with the integer value 23178784Skmacy * of the current argument. 24178784Skmacy * 25178784Skmacy * Helper to parse options on the format used by mount ("a=b,c=d,e,f"). 26178784Skmacy * Returns opts->val if a matching entry in the 'opts' array is found, 27178784Skmacy * 0 when no more tokens are found, -1 if an error is encountered. 28178784Skmacy */ 29178784Skmacyint krping_getopt(const char *caller, char **options, 30178784Skmacy const struct krping_option *opts, char **optopt, 31178784Skmacy char **optarg, unsigned long *value) 32178784Skmacy{ 33178784Skmacy char *token; 34178784Skmacy char *val; 35178784Skmacy 36178784Skmacy do { 37178784Skmacy if ((token = strsep(options, ",")) == NULL) 38178784Skmacy return 0; 39178784Skmacy } while (*token == '\0'); 40178784Skmacy if (optopt) 41178784Skmacy *optopt = token; 42178784Skmacy 43178784Skmacy if ((val = strchr (token, '=')) != NULL) { 44178784Skmacy *val++ = 0; 45178784Skmacy } 46178784Skmacy *optarg = val; 47178784Skmacy for (; opts->name; opts++) { 48178784Skmacy if (!strcmp(opts->name, token)) { 49178784Skmacy if (!val) { 50178784Skmacy if (opts->has_arg & OPT_NOPARAM) { 51178784Skmacy return opts->val; 52178784Skmacy } 53256470Snp printk(KERN_INFO "%s: the %s option requires " 54178784Skmacy "an argument\n", caller, token); 55178784Skmacy return -EINVAL; 56178784Skmacy } 57178784Skmacy if (opts->has_arg & OPT_INT) { 58178784Skmacy char* v; 59178784Skmacy 60256470Snp *value = simple_strtoul(val, &v, 0); 61178784Skmacy if (!*v) { 62178784Skmacy return opts->val; 63178784Skmacy } 64256470Snp printk(KERN_INFO "%s: invalid numeric value " 65178784Skmacy "in %s=%s\n", caller, token, val); 66178784Skmacy return -EDOM; 67178784Skmacy } 68178784Skmacy if (opts->has_arg & OPT_STRING) { 69178784Skmacy return opts->val; 70178784Skmacy } 71256470Snp printk(KERN_INFO "%s: unexpected argument %s to the " 72178784Skmacy "%s option\n", caller, val, token); 73178784Skmacy return -EINVAL; 74178784Skmacy } 75178784Skmacy } 76256470Snp printk(KERN_INFO "%s: Unrecognized option %s\n", caller, token); 77178784Skmacy return -EOPNOTSUPP; 78178784Skmacy} 79