2 * \file subopt-helper.c
4 * \brief Compensates the suboption parsing code duplication a bit.
6 * The routines defined below are there to help you with the
7 * suboption parsing. Meaning extracting the options and their
8 * values for you and also outputting generic help message if
9 * a parse error is encountered.
11 * Most stuff happens in the subopt_parse function: if you call it
12 * it parses for the passed opts in the passed string. It calls some
13 * extra functions for explicit argument parsing ( where the option
14 * itself isn't the argument but a value given after the argument
15 * delimiter ('='). It also calls your test function if you supplied
20 #include "subopt-helper.h"
31 /* prototypes for argument parsing */
32 static char const * parse_int( char const * const str
, int * const valp
);
33 static char const * parse_str( char const * const str
, strarg_t
* const valp
);
36 * \brief Try to parse all options in str and fail if it was not possible.
38 * \param str Pointer to the zero terminated string to be parsed.
39 * \param opts Pointer to a options array. The array must be terminated
40 * with an element having set name to NULL in its opt_t structure.
42 * \return The return value is zero if the string could be parsed
43 * else a non-zero value is returned.
46 int subopt_parse( char const * const str
, opt_t
* opts
)
48 int parse_err
= 0, idx
;
49 unsigned int parse_pos
= 0;
51 /* Initialize set member to false. *
52 * It is set to true if it was found in str */
53 for ( idx
=0; opts
[idx
].name
; ++idx
)
60 while ( str
[parse_pos
] && !parse_err
)
64 idx
= 0; // reset index for the below loop
65 while ( opts
[idx
].name
)
70 // get length of the option we test against */
71 opt_len
= strlen( opts
[idx
].name
);
73 // get length of the current substring of str */
75 char * delim
, * arg_delim
;
77 /* search nearest delimiter ( option or argument delimiter ) */
78 delim
= strchr( &str
[parse_pos
], ':' );
79 arg_delim
= strchr( &str
[parse_pos
], '=' );
81 if ( ( delim
&& arg_delim
&& delim
> arg_delim
) ||
84 delim
= strchr( &str
[parse_pos
], '=' );
87 substr_len
= delim
? // is a delim present
88 delim
- &str
[parse_pos
] : // yes
89 strlen( &str
[parse_pos
] ); // no, end of string
92 //printf( "substr_len=%d, opt_len=%d\n", substr_len, opt_len );
94 /* Check if the length of the current option matches the *
95 * length of the option we want to test again. */
96 if ( substr_len
== opt_len
)
98 /* check if option was activated/deactivated */
99 if( strncmp( &str
[parse_pos
], opts
[idx
].name
, opt_len
) == 0 )
101 /* option was found */
102 opts
[idx
].set
= 1; next
= 1;
104 assert( opts
[idx
].valp
&& "Need a pointer to store the arg!" );
106 /* type specific code */
107 if ( opts
[idx
].type
== OPT_ARG_BOOL
)
109 /* Handle OPT_ARG_BOOL seperately so *
110 * the others can share code. */
112 /* set option to true */
113 *((int *)(opts
[idx
].valp
)) = 1;
115 /* increment position */
116 parse_pos
+= opt_len
;
120 /* Type is not OPT_ARG_BOOL, means we have to parse *
121 * for the arg delimiter character and eventually *
122 * call a test function. */
125 /* increment position to check for arg */
126 parse_pos
+= opt_len
;
128 if ( str
[parse_pos
] != '=' )
130 parse_err
= 1; break;
133 /* '=' char was there, so let's move after it */
136 switch ( opts
[idx
].type
)
139 last
= parse_int( &str
[parse_pos
],
140 (int *)opts
[idx
].valp
);
144 last
= parse_str( &str
[parse_pos
],
145 (strarg_t
*)opts
[idx
].valp
);
148 assert( 0 && "Arg type of suboption doesn't exist!" );
149 last
= NULL
; // break parsing!
152 /* was the conversion succesful? */
155 parse_err
= 1; break;
158 /* make test if supplied */
159 if ( opts
[idx
].test
&& !opts
[idx
].test( opts
[idx
].valp
) )
161 parse_err
= 1; break;
164 /* we succeded, set position */
165 parse_pos
= last
- str
;
169 else if ( substr_len
== opt_len
+2 )
171 if ( opts
[idx
].type
== OPT_ARG_BOOL
&& // check for no<opt>
172 strncmp( &str
[parse_pos
], "no", 2 ) == 0 &&
173 strncmp( &str
[parse_pos
+2], opts
[idx
].name
, opt_len
) == 0 )
175 /* option was found but negated */
176 opts
[idx
].set
= 1; next
= 1;
178 /* set arg to false */
179 *((int *)(opts
[idx
].valp
)) = 0;
181 /* increment position */
182 parse_pos
+= opt_len
+2;
186 ++idx
; // test against next option
188 /* break out of the loop, if this subopt is processed */
189 if ( next
) { break; }
192 /* if we had a valid suboption the current pos should *
193 * equal the delimiter char, which should be ':' for *
195 if ( !parse_err
&& str
[parse_pos
] == ':' ) { ++parse_pos
; }
196 else if ( str
[parse_pos
] ) { parse_err
= 1; }
200 /* if an error was encountered */
204 mp_msg( MSGT_VO
, MSGL_FATAL
, "Could not parse arguments at the position indicated below:\n%s\n", str
);
205 for ( i
= 0; i
< parse_pos
; ++i
)
207 mp_msg(MSGT_VO
, MSGL_FATAL
, " ");
209 mp_msg(MSGT_VO
, MSGL_FATAL
, "^\n");
214 /* we could parse everything */
218 static char const * parse_int( char const * const str
, int * const valp
)
222 assert( str
&& "parse_int(): str == NULL" );
224 *valp
= (int)strtol( str
, &endp
, 0 );
226 /* nothing was converted */
227 if ( str
== endp
) { return NULL
; }
232 static char const * parse_str( char const * const str
, strarg_t
* const valp
)
234 char const * match
= strchr( str
, ':' );
237 match
= &str
[strlen(str
)];
239 // empty string or too long
240 if ((match
== str
) || (match
- str
> 255))
243 valp
->len
= match
- str
;