annotate subopt-helper.c @ 16316:ff5274370d5c

synced with 1.12
author gabrov
date Mon, 29 Aug 2005 19:53:27 +0000
parents 7e4fa8fc255c
children 061d6e09ad62
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
1 /**
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
2 * \file subopt-helper.c
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
3 *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
4 * \brief Compensates the suboption parsing code duplication a bit.
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
5 *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
6 * The routines defined below are there to help you with the
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
7 * suboption parsing. Meaning extracting the options and their
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
8 * values for you and also outputting generic help message if
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
9 * a parse error is encountered.
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
10 *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
11 * Most stuff happens in the subopt_parse function: if you call it
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
12 * it parses for the passed opts in the passed string. It calls some
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
13 * extra functions for explicit argument parsing ( where the option
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
14 * itself isn't the argument but a value given after the argument
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
15 * delimiter ('='). It also calls your test function if you supplied
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
16 * one.
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
17 *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
18 */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
19
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
20 #include "subopt-helper.h"
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
21 #include "mp_msg.h"
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
22
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
23 #include <stdlib.h>
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
24 #include <string.h>
14538
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
25 #include <limits.h>
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
26 #include <assert.h>
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
27
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
28 #ifndef MPDEBUG
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
29 #define NDEBUG
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
30 #endif
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
31
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
32 /* prototypes for argument parsing */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
33 static char const * parse_int( char const * const str, int * const valp );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
34 static char const * parse_str( char const * const str, strarg_t * const valp );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
35
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
36 /**
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
37 * \brief Try to parse all options in str and fail if it was not possible.
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
38 *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
39 * \param str Pointer to the zero terminated string to be parsed.
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
40 * \param opts Pointer to a options array. The array must be terminated
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
41 * with an element having set name to NULL in its opt_t structure.
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
42 *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
43 * \return The return value is zero if the string could be parsed
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
44 * else a non-zero value is returned.
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
45 *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
46 */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
47 int subopt_parse( char const * const str, opt_t * opts )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
48 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
49 int parse_err = 0, idx;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
50 unsigned int parse_pos = 0;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
51
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
52 /* Initialize set member to false. *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
53 * It is set to true if it was found in str */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
54 for ( idx=0; opts[idx].name; ++idx )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
55 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
56 opts[idx].set = 0;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
57 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
58
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
59 if ( str )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
60 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
61 while ( str[parse_pos] && !parse_err )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
62 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
63 int next = 0;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
64
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
65 idx = 0; // reset index for the below loop
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
66 while ( opts[idx].name )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
67 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
68 int opt_len;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
69 int substr_len;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
70
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
71 // get length of the option we test against */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
72 opt_len = strlen( opts[idx].name );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
73
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
74 // get length of the current substring of str */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
75 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
76 char * delim, * arg_delim;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
77
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
78 /* search nearest delimiter ( option or argument delimiter ) */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
79 delim = strchr( &str[parse_pos], ':' );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
80 arg_delim = strchr( &str[parse_pos], '=' );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
81
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
82 if ( ( delim && arg_delim && delim > arg_delim ) ||
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
83 delim == NULL )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
84 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
85 delim = strchr( &str[parse_pos], '=' );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
86 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
87
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
88 substr_len = delim ? // is a delim present
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
89 delim - &str[parse_pos] : // yes
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
90 strlen( &str[parse_pos] ); // no, end of string
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
91 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
92
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
93 //printf( "substr_len=%d, opt_len=%d\n", substr_len, opt_len );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
94
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
95 /* Check if the length of the current option matches the *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
96 * length of the option we want to test again. */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
97 if ( substr_len == opt_len )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
98 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
99 /* check if option was activated/deactivated */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
100 if( strncmp( &str[parse_pos], opts[idx].name, opt_len ) == 0 )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
101 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
102 /* option was found */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
103 opts[idx].set = 1; next = 1;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
104
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
105 assert( opts[idx].valp && "Need a pointer to store the arg!" );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
106
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
107 /* type specific code */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
108 if ( opts[idx].type == OPT_ARG_BOOL )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
109 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
110 /* Handle OPT_ARG_BOOL seperately so *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
111 * the others can share code. */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
112
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
113 /* set option to true */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
114 *((int *)(opts[idx].valp)) = 1;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
115
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
116 /* increment position */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
117 parse_pos += opt_len;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
118 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
119 else
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
120 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
121 /* Type is not OPT_ARG_BOOL, means we have to parse *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
122 * for the arg delimiter character and eventually *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
123 * call a test function. */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
124 char const * last;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
125
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
126 /* increment position to check for arg */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
127 parse_pos += opt_len;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
128
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
129 if ( str[parse_pos] != '=' )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
130 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
131 parse_err = 1; break;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
132 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
133
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
134 /* '=' char was there, so let's move after it */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
135 ++parse_pos;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
136
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
137 switch ( opts[idx].type )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
138 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
139 case OPT_ARG_INT:
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
140 last = parse_int( &str[parse_pos],
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
141 (int *)opts[idx].valp );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
142
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
143 break;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
144 case OPT_ARG_STR:
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
145 last = parse_str( &str[parse_pos],
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
146 (strarg_t *)opts[idx].valp );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
147 break;
14538
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
148 case OPT_ARG_MSTRZ:
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
149 {
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
150 char **valp = opts[idx].valp;
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
151 strarg_t tmp;
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
152 tmp.str = NULL;
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
153 tmp.len = 0;
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
154 last = parse_str( &str[parse_pos], &tmp );
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
155 if (*valp)
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
156 free(*valp);
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
157 *valp = NULL;
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
158 if (tmp.str && tmp.len > 0) {
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
159 *valp = malloc(tmp.len + 1);
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
160 memcpy(*valp, tmp.str, tmp.len);
14572
cfe2bbf96000 100l, missing () around *valp
reimar
parents: 14538
diff changeset
161 (*valp)[tmp.len] = 0;
14538
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
162 }
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
163 break;
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
164 }
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
165 default:
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
166 assert( 0 && "Arg type of suboption doesn't exist!" );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
167 last = NULL; // break parsing!
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
168 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
169
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
170 /* was the conversion succesful? */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
171 if ( !last )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
172 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
173 parse_err = 1; break;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
174 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
175
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
176 /* make test if supplied */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
177 if ( opts[idx].test && !opts[idx].test( opts[idx].valp ) )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
178 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
179 parse_err = 1; break;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
180 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
181
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
182 /* we succeded, set position */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
183 parse_pos = last - str;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
184 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
185 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
186 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
187 else if ( substr_len == opt_len+2 )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
188 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
189 if ( opts[idx].type == OPT_ARG_BOOL && // check for no<opt>
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
190 strncmp( &str[parse_pos], "no", 2 ) == 0 &&
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
191 strncmp( &str[parse_pos+2], opts[idx].name, opt_len ) == 0 )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
192 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
193 /* option was found but negated */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
194 opts[idx].set = 1; next = 1;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
195
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
196 /* set arg to false */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
197 *((int *)(opts[idx].valp)) = 0;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
198
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
199 /* increment position */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
200 parse_pos += opt_len+2;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
201 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
202 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
203
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
204 ++idx; // test against next option
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
205
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
206 /* break out of the loop, if this subopt is processed */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
207 if ( next ) { break; }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
208 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
209
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
210 /* if we had a valid suboption the current pos should *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
211 * equal the delimiter char, which should be ':' for *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
212 * suboptions. */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
213 if ( !parse_err && str[parse_pos] == ':' ) { ++parse_pos; }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
214 else if ( str[parse_pos] ) { parse_err = 1; }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
215 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
216 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
217
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
218 /* if an error was encountered */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
219 if (parse_err)
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
220 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
221 unsigned int i;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
222 mp_msg( MSGT_VO, MSGL_FATAL, "Could not parse arguments at the position indicated below:\n%s\n", str );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
223 for ( i = 0; i < parse_pos; ++i )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
224 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
225 mp_msg(MSGT_VO, MSGL_FATAL, " ");
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
226 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
227 mp_msg(MSGT_VO, MSGL_FATAL, "^\n");
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
228
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
229 return -1;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
230 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
231
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
232 /* we could parse everything */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
233 return 0;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
234 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
235
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
236 static char const * parse_int( char const * const str, int * const valp )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
237 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
238 char * endp;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
239
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
240 assert( str && "parse_int(): str == NULL" );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
241
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
242 *valp = (int)strtol( str, &endp, 0 );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
243
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
244 /* nothing was converted */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
245 if ( str == endp ) { return NULL; }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
246
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
247 return endp;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
248 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
249
15733
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
250 #define QUOTE_CHAR '%'
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
251 static char const * parse_str( char const * str, strarg_t * const valp )
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
252 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
253 char const * match = strchr( str, ':' );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
254
15733
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
255 if (str[0] == QUOTE_CHAR) {
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
256 int len = 0;
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
257 str = &str[1];
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
258 len = (int)strtol(str, (char **)&str, 0);
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
259 if (!str || str[0] != QUOTE_CHAR || (len > strlen(str) - 1))
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
260 return NULL;
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
261 str = &str[1];
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
262 match = &str[len];
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
263 }
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
264 else
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
265 if ( !match )
14294
90bcd37dba7f fix string argument parsing (e.g. one char strings were not accepted)
reimar
parents: 14281
diff changeset
266 match = &str[strlen(str)];
90bcd37dba7f fix string argument parsing (e.g. one char strings were not accepted)
reimar
parents: 14281
diff changeset
267
90bcd37dba7f fix string argument parsing (e.g. one char strings were not accepted)
reimar
parents: 14281
diff changeset
268 // empty string or too long
14538
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
269 if ((match == str) || (match - str > INT_MAX))
14294
90bcd37dba7f fix string argument parsing (e.g. one char strings were not accepted)
reimar
parents: 14281
diff changeset
270 return NULL;
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
271
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
272 valp->len = match - str;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
273 valp->str = str;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
274
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
275 return match;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
276 }
14736
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
277
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
278
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
279 /*** common test functions ***/
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
280
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
281 /** \brief Test if i is not negative */
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
282 int int_non_neg( int * i )
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
283 {
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
284 if ( *i < 0 ) { return 0; }
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
285
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
286 return 1;
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
287 }
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
288 /** \brief Test if i is positive. */
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
289 int int_pos( int * i )
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
290 {
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
291 if ( *i > 0 ) { return 1; }
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
292
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
293 return 0;
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
294 }
15734
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
295
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
296 /*** little helpers */
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
297
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
298 /** \brief compare the stings just as strcmp does */
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
299 int strargcmp(strarg_t *arg, char *str) {
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
300 int res = strncmp(arg->str, str, arg->len);
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
301 if (!res && arg->len != strlen(str))
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
302 res = arg->len - strlen(str);
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
303 return res;
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
304 }
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
305
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
306 /** \brief compare the stings just as strcasecmp does */
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
307 int strargcasecmp(strarg_t *arg, char *str) {
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
308 int res = strncasecmp(arg->str, str, arg->len);
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
309 if (!res && arg->len != strlen(str))
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
310 res = arg->len - strlen(str);
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
311 return res;
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
312 }
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
313