1 /* This file provides functionality to parse strings of comma-separated
2 * options, each being either a single key name or a key=value pair, where the
3 * value may be enclosed in quotes. A table of optset entries is provided to
4 * determine which options are recognized, how to parse their values, and where
5 * to store those. Unrecognized options are silently ignored; improperly
6 * formatted options are silently set to reasonably acceptable values.
8 * The entry points into this file are:
9 * optset_parse parse the given options string using the given table
12 * May 2009 (D.C. van Moolenbroek)
18 #include <minix/config.h>
19 #include <minix/const.h>
20 #include <minix/optset.h>
22 static void optset_parse_entry(struct optset
*entry
, char *ptr
, int
25 /*===========================================================================*
26 * optset_parse_entry *
27 *===========================================================================*/
28 static void optset_parse_entry(entry
, ptr
, len
)
33 /* Parse and store the value of a single option.
38 switch (entry
->os_type
) {
40 *((int *) entry
->os_ptr
) = entry
->os_val
;
45 if (len
>= entry
->os_val
)
46 len
= entry
->os_val
- 1;
48 dst
= (char *) entry
->os_ptr
;
51 memcpy(dst
, ptr
, len
);
58 val
= (int) strtoul(ptr
, NULL
, entry
->os_val
);
62 *((int *) entry
->os_ptr
) = val
;
68 /*===========================================================================*
70 *===========================================================================*/
71 void optset_parse(table
, string
)
75 /* Parse a string of options, using the provided table of optset entries.
77 char *p
, *kptr
, *vptr
;
80 for (p
= string
; *p
; ) {
81 /* Get the key name for the field. */
82 for (kptr
= p
, klen
= 0; *p
&& *p
!= '=' && *p
!= ','; p
++, klen
++);
85 /* The field has an associated value. */
88 /* If the first character after the '=' is a quote character,
89 * find a matching quote character followed by either a comma
90 * or the terminating null character, and use the string in
91 * between. Otherwise, use the string up to the next comma or
92 * the terminating null character.
94 if (*p
== '\'' || *p
== '"') {
97 for (vlen
= 0; *p
&& (*p
!= *vptr
||
98 (p
[1] && p
[1] != ',')); p
++, vlen
++);
104 for (vlen
= 0; *p
&& *p
!= ','; p
++, vlen
++);
113 /* Find a matching entry for this key in the given table. If found,
114 * call optset_parse_entry() on it. Silently ignore the option
117 for (i
= 0; table
[i
].os_name
!= NULL
; i
++) {
118 if ((int) strlen(table
[i
].os_name
) == klen
&&
119 !strncasecmp(table
[i
].os_name
, kptr
, klen
)) {
121 optset_parse_entry(&table
[i
], vptr
, vlen
);