input: resource: move vouts handling from array to list
[vlc.git] / src / config / cmdline.c
blob72df85347a97898d33671ec1a700d3efe61d89a2
1 /*****************************************************************************
2 * cmdline.c: command line parsing
3 *****************************************************************************
4 * Copyright (C) 2001-2007 VLC authors and VideoLAN
6 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include <vlc_common.h>
28 #include "../libvlc.h"
29 #include <vlc_actions.h>
30 #include <vlc_charset.h>
31 #include <vlc_modules.h>
32 #include <vlc_plugin.h>
34 #include "vlc_getopt.h"
36 #include "configuration.h"
37 #include "modules/modules.h"
39 #include <assert.h>
41 #undef config_LoadCmdLine
42 /**
43 * Parse command line for configuration options.
45 * Now that the module_bank has been initialized, we can dynamically
46 * generate the longopts structure used by getops. We have to do it this way
47 * because we don't know (and don't want to know) in advance the configuration
48 * options used (ie. exported) by each module.
50 * @param p_this object to write command line options as variables to
51 * @param i_argc number of command line arguments
52 * @param ppsz_args commandl ine arguments [IN/OUT]
53 * @param pindex NULL to ignore unknown options,
54 * otherwise index of the first non-option argument [OUT]
55 * @return 0 on success, -1 on error.
57 int config_LoadCmdLine( vlc_object_t *p_this, int i_argc,
58 const char *ppsz_argv[], int *pindex )
60 int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
61 struct vlc_option *p_longopts;
62 const char **argv_copy = NULL;
63 #define b_ignore_errors (pindex == NULL)
65 /* Short options */
66 const module_config_t *pp_shortopts[256];
67 char *psz_shortopts;
70 * Generate the longopts and shortopts structures used by getopt_long
73 i_opts = 0;
74 for (const vlc_plugin_t *p = vlc_plugins; p != NULL; p = p->next)
75 /* count the number of exported configuration options (to allocate
76 * longopts). We also need to allocate space for two options when
77 * dealing with boolean to allow for --foo and --no-foo */
78 i_opts += p->conf.count + 2 * p->conf.booleans;
80 p_longopts = vlc_alloc( i_opts + 1, sizeof(*p_longopts) );
81 if( p_longopts == NULL )
82 return -1;
84 psz_shortopts = malloc( 2 * i_opts + 1 );
85 if( psz_shortopts == NULL )
87 free( p_longopts );
88 return -1;
91 /* If we are requested to ignore errors, then we must work on a copy
92 * of the ppsz_argv array, otherwise getopt_long will reorder it for
93 * us, ignoring the arity of the options */
94 if( b_ignore_errors )
96 argv_copy = vlc_alloc( i_argc, sizeof(char *) );
97 if( argv_copy == NULL )
99 free( psz_shortopts );
100 free( p_longopts );
101 return -1;
103 memcpy( argv_copy, ppsz_argv, i_argc * sizeof(char *) );
104 ppsz_argv = argv_copy;
107 i_shortopts = 0;
108 for( i_index = 0; i_index < 256; i_index++ )
110 pp_shortopts[i_index] = NULL;
113 /* Fill the p_longopts and psz_shortopts structures */
114 i_index = 0;
115 for (const vlc_plugin_t *p = vlc_plugins; p != NULL; p = p->next)
117 for (const module_config_t *p_item = p->conf.items,
118 *p_end = p_item + p->conf.size;
119 p_item < p_end;
120 p_item++)
122 /* Ignore hints */
123 if( !CONFIG_ITEM(p_item->i_type) )
124 continue;
126 /* Add item to long options */
127 p_longopts[i_index].name = strdup( p_item->psz_name );
128 if( p_longopts[i_index].name == NULL ) continue;
129 p_longopts[i_index].flag = &flag;
130 p_longopts[i_index].val = 0;
132 if( CONFIG_CLASS(p_item->i_type) != CONFIG_ITEM_BOOL )
133 p_longopts[i_index].has_arg = true;
134 else
135 /* Booleans also need --no-foo and --nofoo options */
137 char *psz_name;
139 p_longopts[i_index].has_arg = false;
140 i_index++;
142 if( asprintf( &psz_name, "no%s", p_item->psz_name ) == -1 )
143 continue;
144 p_longopts[i_index].name = psz_name;
145 p_longopts[i_index].has_arg = false;
146 p_longopts[i_index].flag = &flag;
147 p_longopts[i_index].val = 1;
148 i_index++;
150 if( asprintf( &psz_name, "no-%s", p_item->psz_name ) == -1 )
151 continue;
152 p_longopts[i_index].name = psz_name;
153 p_longopts[i_index].has_arg = false;
154 p_longopts[i_index].flag = &flag;
155 p_longopts[i_index].val = 1;
157 i_index++;
159 /* If item also has a short option, add it */
160 if( p_item->i_short )
162 pp_shortopts[(int)p_item->i_short] = p_item;
163 psz_shortopts[i_shortopts] = p_item->i_short;
164 i_shortopts++;
165 if( p_item->i_type != CONFIG_ITEM_BOOL
166 && p_item->i_short != 'v' )
168 psz_shortopts[i_shortopts] = ':';
169 i_shortopts++;
175 /* Close the longopts and shortopts structures */
176 memset( &p_longopts[i_index], 0, sizeof(*p_longopts) );
177 psz_shortopts[i_shortopts] = '\0';
179 int ret = -1;
182 * Parse the command line options
184 vlc_getopt_t state;
185 state.ind = 0 ; /* set to 0 to tell GNU getopt to reinitialize */
186 while( ( i_cmd = vlc_getopt_long( i_argc, (char **)ppsz_argv,
187 psz_shortopts,
188 p_longopts, &i_index, &state ) ) != -1 )
190 /* A long option has been recognized */
191 if( i_cmd == 0 )
193 module_config_t *p_conf;
194 const char *psz_name = p_longopts[i_index].name;
196 /* Check if we deal with a --nofoo or --no-foo long option */
197 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
199 /* Store the configuration option */
200 p_conf = config_FindConfig( psz_name );
201 if( p_conf )
203 /* Check if the option is deprecated */
204 if( p_conf->b_removed )
206 fprintf(stderr,
207 "Warning: option --%s no longer exists.\n",
208 psz_name);
209 continue;
212 switch( CONFIG_CLASS(p_conf->i_type) )
214 case CONFIG_ITEM_STRING:
215 var_Create( p_this, psz_name, VLC_VAR_STRING );
216 var_SetString( p_this, psz_name, state.arg );
217 break;
218 case CONFIG_ITEM_INTEGER:
219 var_Create( p_this, psz_name, VLC_VAR_INTEGER );
220 var_Change( p_this, psz_name, VLC_VAR_SETMINMAX,
221 (vlc_value_t){ .i_int = p_conf->min.i },
222 (vlc_value_t){ .i_int = p_conf->max.i } );
223 var_SetInteger( p_this, psz_name,
224 strtoll(state.arg, NULL, 0));
225 break;
226 case CONFIG_ITEM_FLOAT:
227 var_Create( p_this, psz_name, VLC_VAR_FLOAT );
228 var_Change( p_this, psz_name, VLC_VAR_SETMINMAX,
229 (vlc_value_t){ .f_float = p_conf->min.f },
230 (vlc_value_t){ .f_float = p_conf->max.f } );
231 var_SetFloat( p_this, psz_name, us_atof(state.arg) );
232 break;
233 case CONFIG_ITEM_BOOL:
234 var_Create( p_this, psz_name, VLC_VAR_BOOL );
235 var_SetBool( p_this, psz_name, !flag );
236 break;
238 continue;
242 /* A short option has been recognized */
243 if( pp_shortopts[i_cmd] != NULL )
245 const char *name = pp_shortopts[i_cmd]->psz_name;
246 switch( CONFIG_CLASS(pp_shortopts[i_cmd]->i_type) )
248 case CONFIG_ITEM_STRING:
249 var_Create( p_this, name, VLC_VAR_STRING );
250 var_SetString( p_this, name, state.arg );
251 break;
252 case CONFIG_ITEM_INTEGER:
253 var_Create( p_this, name, VLC_VAR_INTEGER );
254 if( i_cmd == 'v' )
256 i_verbose++; /* -v */
257 var_SetInteger( p_this, name, i_verbose );
259 else
261 var_SetInteger( p_this, name,
262 strtoll(state.arg, NULL, 0) );
264 break;
265 case CONFIG_ITEM_BOOL:
266 var_Create( p_this, name, VLC_VAR_BOOL );
267 var_SetBool( p_this, name, true );
268 break;
271 continue;
274 /* Internal error: unknown option */
275 if( !b_ignore_errors )
277 fputs( "vlc: unknown option"
278 " or missing mandatory argument ", stderr );
279 if( state.opt )
281 fprintf( stderr, "`-%c'\n", state.opt );
283 else
285 fprintf( stderr, "`%s'\n", ppsz_argv[state.ind-1] );
287 fputs( "Try `vlc --help' for more information.\n", stderr );
288 goto out;
292 ret = 0;
293 if( pindex != NULL )
294 *pindex = state.ind;
295 out:
296 /* Free allocated resources */
297 for( i_index = 0; p_longopts[i_index].name; i_index++ )
298 free( (char *)p_longopts[i_index].name );
299 free( p_longopts );
300 free( psz_shortopts );
301 free( argv_copy );
302 return ret;