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"
32 /* prototypes for argument parsing */
33 static char const * parse_int( char const * const str
, int * const valp
);
34 static char const * parse_str( char const * const str
, strarg_t
* const valp
);
37 * \brief Try to parse all options in str and fail if it was not possible.
39 * \param str Pointer to the zero terminated string to be parsed.
40 * \param opts Pointer to a options array. The array must be terminated
41 * with an element having set name to NULL in its opt_t structure.
43 * \return The return value is zero if the string could be parsed
44 * else a non-zero value is returned.
47 int subopt_parse( char const * const str
, opt_t
* opts
)
49 int parse_err
= 0, idx
;
50 unsigned int parse_pos
= 0;
52 /* Initialize set member to false. *
53 * It is set to true if it was found in str */
54 for ( idx
=0; opts
[idx
].name
; ++idx
)
61 while ( str
[parse_pos
] && !parse_err
)
65 idx
= 0; // reset index for the below loop
66 while ( opts
[idx
].name
)
71 // get length of the option we test against */
72 opt_len
= strlen( opts
[idx
].name
);
74 // get length of the current substring of str */
76 char * delim
, * arg_delim
;
78 /* search nearest delimiter ( option or argument delimiter ) */
79 delim
= strchr( &str
[parse_pos
], ':' );
80 arg_delim
= strchr( &str
[parse_pos
], '=' );
82 if ( ( delim
&& arg_delim
&& delim
> arg_delim
) ||
85 delim
= strchr( &str
[parse_pos
], '=' );
88 substr_len
= delim
? // is a delim present
89 delim
- &str
[parse_pos
] : // yes
90 strlen( &str
[parse_pos
] ); // no, end of string
93 //printf( "substr_len=%d, opt_len=%d\n", substr_len, opt_len );
95 /* Check if the length of the current option matches the *
96 * length of the option we want to test again. */
97 if ( substr_len
== opt_len
)
99 /* check if option was activated/deactivated */
100 if( strncmp( &str
[parse_pos
], opts
[idx
].name
, opt_len
) == 0 )
102 /* option was found */
103 opts
[idx
].set
= 1; next
= 1;
105 assert( opts
[idx
].valp
&& "Need a pointer to store the arg!" );
107 /* type specific code */
108 if ( opts
[idx
].type
== OPT_ARG_BOOL
)
110 /* Handle OPT_ARG_BOOL seperately so *
111 * the others can share code. */
113 /* set option to true */
114 *((int *)(opts
[idx
].valp
)) = 1;
116 /* increment position */
117 parse_pos
+= opt_len
;
121 /* Type is not OPT_ARG_BOOL, means we have to parse *
122 * for the arg delimiter character and eventually *
123 * call a test function. */
126 /* increment position to check for arg */
127 parse_pos
+= opt_len
;
129 if ( str
[parse_pos
] != '=' )
131 parse_err
= 1; break;
134 /* '=' char was there, so let's move after it */
137 switch ( opts
[idx
].type
)
140 last
= parse_int( &str
[parse_pos
],
141 (int *)opts
[idx
].valp
);
145 last
= parse_str( &str
[parse_pos
],
146 (strarg_t
*)opts
[idx
].valp
);
150 char **valp
= opts
[idx
].valp
;
154 last
= parse_str( &str
[parse_pos
], &tmp
);
158 if (tmp
.str
&& tmp
.len
> 0) {
159 *valp
= malloc(tmp
.len
+ 1);
160 memcpy(*valp
, tmp
.str
, tmp
.len
);
161 (*valp
)[tmp
.len
] = 0;
166 assert( 0 && "Arg type of suboption doesn't exist!" );
167 last
= NULL
; // break parsing!
170 /* was the conversion succesful? */
173 parse_err
= 1; break;
176 /* make test if supplied */
177 if ( opts
[idx
].test
&& !opts
[idx
].test( opts
[idx
].valp
) )
179 parse_err
= 1; break;
182 /* we succeded, set position */
183 parse_pos
= last
- str
;
187 else if ( substr_len
== opt_len
+2 )
189 if ( opts
[idx
].type
== OPT_ARG_BOOL
&& // check for no<opt>
190 strncmp( &str
[parse_pos
], "no", 2 ) == 0 &&
191 strncmp( &str
[parse_pos
+2], opts
[idx
].name
, opt_len
) == 0 )
193 /* option was found but negated */
194 opts
[idx
].set
= 1; next
= 1;
196 /* set arg to false */
197 *((int *)(opts
[idx
].valp
)) = 0;
199 /* increment position */
200 parse_pos
+= opt_len
+2;
204 ++idx
; // test against next option
206 /* break out of the loop, if this subopt is processed */
207 if ( next
) { break; }
210 /* if we had a valid suboption the current pos should *
211 * equal the delimiter char, which should be ':' for *
213 if ( !parse_err
&& str
[parse_pos
] == ':' ) { ++parse_pos
; }
214 else if ( str
[parse_pos
] ) { parse_err
= 1; }
218 /* if an error was encountered */
222 mp_msg( MSGT_VO
, MSGL_FATAL
, "Could not parse arguments at the position indicated below:\n%s\n", str
);
223 for ( i
= 0; i
< parse_pos
; ++i
)
225 mp_msg(MSGT_VO
, MSGL_FATAL
, " ");
227 mp_msg(MSGT_VO
, MSGL_FATAL
, "^\n");
232 /* we could parse everything */
236 static char const * parse_int( char const * const str
, int * const valp
)
240 assert( str
&& "parse_int(): str == NULL" );
242 *valp
= (int)strtol( str
, &endp
, 0 );
244 /* nothing was converted */
245 if ( str
== endp
) { return NULL
; }
250 #define QUOTE_CHAR '%'
251 static char const * parse_str( char const * str
, strarg_t
* const valp
)
253 char const * match
= strchr( str
, ':' );
255 if (str
[0] == QUOTE_CHAR
) {
258 len
= (int)strtol(str
, (char **)&str
, 0);
259 if (!str
|| str
[0] != QUOTE_CHAR
|| (len
> strlen(str
) - 1))
266 match
= &str
[strlen(str
)];
268 // empty string or too long
269 if ((match
== str
) || (match
- str
> INT_MAX
))
272 valp
->len
= match
- str
;
279 /*** common test functions ***/
281 /** \brief Test if i is not negative */
282 int int_non_neg( int * i
)
284 if ( *i
< 0 ) { return 0; }
288 /** \brief Test if i is positive. */
289 int int_pos( int * i
)
291 if ( *i
> 0 ) { return 1; }
296 /*** little helpers */
298 /** \brief compare the stings just as strcmp does */
299 int strargcmp(strarg_t
*arg
, char *str
) {
300 int res
= strncmp(arg
->str
, str
, arg
->len
);
301 if (!res
&& arg
->len
!= strlen(str
))
302 res
= arg
->len
- strlen(str
);
306 /** \brief compare the stings just as strcasecmp does */
307 int strargcasecmp(strarg_t
*arg
, char *str
) {
308 int res
= strncasecmp(arg
->str
, str
, arg
->len
);
309 if (!res
&& arg
->len
!= strlen(str
))
310 res
= arg
->len
- strlen(str
);