Fortunes about QT and "hearing" video codecs
[vlc.git] / src / config / cmdline.c
blobd16462a6245e5182d7a7e9149b7b1c625b220e9a
1 /*****************************************************************************
2 * cmdline.c: command line parsing
3 *****************************************************************************
4 * Copyright (C) 2001-2007 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <vlc_common.h>
29 #include "../libvlc.h"
30 #include <vlc_actions.h>
31 #include <vlc_charset.h>
32 #include <vlc_modules.h>
33 #include <vlc_plugin.h>
35 #include "vlc_getopt.h"
37 #include "configuration.h"
38 #include "modules/modules.h"
40 #include <assert.h>
42 #undef config_LoadCmdLine
43 /**
44 * Parse command line for configuration options.
46 * Now that the module_bank has been initialized, we can dynamically
47 * generate the longopts structure used by getops. We have to do it this way
48 * because we don't know (and don't want to know) in advance the configuration
49 * options used (ie. exported) by each module.
51 * @param p_this object to write command line options as variables to
52 * @param i_argc number of command line arguments
53 * @param ppsz_args commandl ine arguments [IN/OUT]
54 * @param pindex NULL to ignore unknown options,
55 * otherwise index of the first non-option argument [OUT]
56 * @return 0 on success, -1 on error.
58 int config_LoadCmdLine( vlc_object_t *p_this, int i_argc,
59 const char *ppsz_argv[], int *pindex )
61 int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
62 struct vlc_option *p_longopts;
63 const char **argv_copy = NULL;
64 #define b_ignore_errors (pindex == NULL)
66 /* Short options */
67 const module_config_t *pp_shortopts[256];
68 char *psz_shortopts;
71 * Generate the longopts and shortopts structures used by getopt_long
74 i_opts = 0;
75 for (const vlc_plugin_t *p = vlc_plugins; p != NULL; p = p->next)
76 /* count the number of exported configuration options (to allocate
77 * longopts). We also need to allocate space for two options when
78 * dealing with boolean to allow for --foo and --no-foo */
79 i_opts += p->conf.count + 2 * p->conf.booleans;
81 p_longopts = vlc_alloc( i_opts + 1, sizeof(*p_longopts) );
82 if( p_longopts == NULL )
83 return -1;
85 psz_shortopts = malloc( 2 * i_opts + 1 );
86 if( psz_shortopts == NULL )
88 free( p_longopts );
89 return -1;
92 /* If we are requested to ignore errors, then we must work on a copy
93 * of the ppsz_argv array, otherwise getopt_long will reorder it for
94 * us, ignoring the arity of the options */
95 if( b_ignore_errors )
97 argv_copy = vlc_alloc( i_argc, sizeof(char *) );
98 if( argv_copy == NULL )
100 free( psz_shortopts );
101 free( p_longopts );
102 return -1;
104 memcpy( argv_copy, ppsz_argv, i_argc * sizeof(char *) );
105 ppsz_argv = argv_copy;
108 i_shortopts = 0;
109 for( i_index = 0; i_index < 256; i_index++ )
111 pp_shortopts[i_index] = NULL;
114 /* Fill the p_longopts and psz_shortopts structures */
115 i_index = 0;
116 for (const vlc_plugin_t *p = vlc_plugins; p != NULL; p = p->next)
118 for (const module_config_t *p_item = p->conf.items,
119 *p_end = p_item + p->conf.size;
120 p_item < p_end;
121 p_item++)
123 /* Ignore hints */
124 if( !CONFIG_ITEM(p_item->i_type) )
125 continue;
127 /* Add item to long options */
128 p_longopts[i_index].name = strdup( p_item->psz_name );
129 if( p_longopts[i_index].name == NULL ) continue;
130 p_longopts[i_index].flag = &flag;
131 p_longopts[i_index].val = 0;
133 if( CONFIG_CLASS(p_item->i_type) != CONFIG_ITEM_BOOL )
134 p_longopts[i_index].has_arg = true;
135 else
136 /* Booleans also need --no-foo and --nofoo options */
138 char *psz_name;
140 p_longopts[i_index].has_arg = false;
141 i_index++;
143 if( asprintf( &psz_name, "no%s", p_item->psz_name ) == -1 )
144 continue;
145 p_longopts[i_index].name = psz_name;
146 p_longopts[i_index].has_arg = false;
147 p_longopts[i_index].flag = &flag;
148 p_longopts[i_index].val = 1;
149 i_index++;
151 if( asprintf( &psz_name, "no-%s", p_item->psz_name ) == -1 )
152 continue;
153 p_longopts[i_index].name = psz_name;
154 p_longopts[i_index].has_arg = false;
155 p_longopts[i_index].flag = &flag;
156 p_longopts[i_index].val = 1;
158 i_index++;
160 /* If item also has a short option, add it */
161 if( p_item->i_short )
163 pp_shortopts[(int)p_item->i_short] = p_item;
164 psz_shortopts[i_shortopts] = p_item->i_short;
165 i_shortopts++;
166 if( p_item->i_type != CONFIG_ITEM_BOOL
167 && p_item->i_short != 'v' )
169 psz_shortopts[i_shortopts] = ':';
170 i_shortopts++;
176 /* Close the longopts and shortopts structures */
177 memset( &p_longopts[i_index], 0, sizeof(*p_longopts) );
178 psz_shortopts[i_shortopts] = '\0';
180 int ret = -1;
183 * Parse the command line options
185 vlc_getopt_t state;
186 state.ind = 0 ; /* set to 0 to tell GNU getopt to reinitialize */
187 while( ( i_cmd = vlc_getopt_long( i_argc, (char **)ppsz_argv,
188 psz_shortopts,
189 p_longopts, &i_index, &state ) ) != -1 )
191 /* A long option has been recognized */
192 if( i_cmd == 0 )
194 module_config_t *p_conf;
195 const char *psz_name = p_longopts[i_index].name;
197 /* Check if we deal with a --nofoo or --no-foo long option */
198 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
200 /* Store the configuration option */
201 p_conf = config_FindConfig( psz_name );
202 if( p_conf )
204 /* Check if the option is deprecated */
205 if( p_conf->b_removed )
207 fprintf(stderr,
208 "Warning: option --%s no longer exists.\n",
209 psz_name);
210 continue;
213 switch( CONFIG_CLASS(p_conf->i_type) )
215 case CONFIG_ITEM_STRING:
216 var_Create( p_this, psz_name, VLC_VAR_STRING );
217 var_SetString( p_this, psz_name, state.arg );
218 break;
219 case CONFIG_ITEM_INTEGER:
220 var_Create( p_this, psz_name, VLC_VAR_INTEGER );
221 var_Change( p_this, psz_name, VLC_VAR_SETMINMAX,
222 (vlc_value_t){ .i_int = p_conf->min.i },
223 (vlc_value_t){ .i_int = p_conf->max.i } );
224 var_SetInteger( p_this, psz_name,
225 strtoll(state.arg, NULL, 0));
226 break;
227 case CONFIG_ITEM_FLOAT:
228 var_Create( p_this, psz_name, VLC_VAR_FLOAT );
229 var_Change( p_this, psz_name, VLC_VAR_SETMINMAX,
230 (vlc_value_t){ .f_float = p_conf->min.f },
231 (vlc_value_t){ .f_float = p_conf->max.f } );
232 var_SetFloat( p_this, psz_name, us_atof(state.arg) );
233 break;
234 case CONFIG_ITEM_BOOL:
235 var_Create( p_this, psz_name, VLC_VAR_BOOL );
236 var_SetBool( p_this, psz_name, !flag );
237 break;
239 continue;
243 /* A short option has been recognized */
244 if( pp_shortopts[i_cmd] != NULL )
246 const char *name = pp_shortopts[i_cmd]->psz_name;
247 switch( CONFIG_CLASS(pp_shortopts[i_cmd]->i_type) )
249 case CONFIG_ITEM_STRING:
250 var_Create( p_this, name, VLC_VAR_STRING );
251 var_SetString( p_this, name, state.arg );
252 break;
253 case CONFIG_ITEM_INTEGER:
254 var_Create( p_this, name, VLC_VAR_INTEGER );
255 if( i_cmd == 'v' )
257 i_verbose++; /* -v */
258 var_SetInteger( p_this, name, i_verbose );
260 else
262 var_SetInteger( p_this, name,
263 strtoll(state.arg, NULL, 0) );
265 break;
266 case CONFIG_ITEM_BOOL:
267 var_Create( p_this, name, VLC_VAR_BOOL );
268 var_SetBool( p_this, name, true );
269 break;
272 continue;
275 /* Internal error: unknown option */
276 if( !b_ignore_errors )
278 fputs( "vlc: unknown option"
279 " or missing mandatory argument ", stderr );
280 if( state.opt )
282 fprintf( stderr, "`-%c'\n", state.opt );
284 else
286 fprintf( stderr, "`%s'\n", ppsz_argv[state.ind-1] );
288 fputs( "Try `vlc --help' for more information.\n", stderr );
289 goto out;
293 ret = 0;
294 if( pindex != NULL )
295 *pindex = state.ind;
296 out:
297 /* Free allocated resources */
298 for( i_index = 0; p_longopts[i_index].name; i_index++ )
299 free( (char *)p_longopts[i_index].name );
300 free( p_longopts );
301 free( psz_shortopts );
302 free( argv_copy );
303 return ret;