1 /*****************************************************************************
2 * cmdline.c: command line parsing
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc_common.h>
29 #include "../libvlc.h"
31 #include <vlc_charset.h>
33 #include "vlc_getopt.h"
35 #include "configuration.h"
36 #include "modules/modules.h"
40 #undef config_LoadCmdLine
42 * Parse command line for configuration options.
44 * Now that the module_bank has been initialized, we can dynamically
45 * generate the longopts structure used by getops. We have to do it this way
46 * because we don't know (and don't want to know) in advance the configuration
47 * options used (ie. exported) by each module.
49 * @param p_this object to write command line options as variables to
50 * @param pi_argc number of command line arguments [IN/OUT]
51 * @param ppsz_args commandl ine arguments [IN/OUT]
52 * @param b_ignore_errors whether to ignore parsing errors
53 * @return 0 on success, -1 on error.
55 * @warning This function is not re-entrant (because of getopt_long()).
56 * It must be called with the module bank initialization global lock held.
57 * FIXME: this still breaks if getopt() is used outside of LibVLC.
59 int config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc,
60 const char *ppsz_argv[], bool b_ignore_errors )
62 int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
64 struct option *p_longopts;
65 const char **argv_copy = NULL;
68 module_config_t *pp_shortopts[256];
71 /* List all modules */
72 module_t **list = module_list_get (NULL);
75 * Generate the longopts and shortopts structures used by getopt_long
79 for (size_t i = 0; (p_parser = list[i]) != NULL; i++)
80 /* count the number of exported configuration options (to allocate
81 * longopts). We also need to allocate space for two options when
82 * dealing with boolean to allow for --foo and --no-foo */
83 i_opts += p_parser->i_config_items + 2 * p_parser->i_bool_items;
85 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
86 if( p_longopts == NULL )
88 module_list_free (list);
92 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
93 if( psz_shortopts == NULL )
96 module_list_free (list);
100 /* If we are requested to ignore errors, then we must work on a copy
101 * of the ppsz_argv array, otherwise getopt_long will reorder it for
102 * us, ignoring the arity of the options */
103 if( b_ignore_errors )
105 argv_copy = (const char**)malloc( *pi_argc * sizeof(char *) );
106 if( argv_copy == NULL )
108 free( psz_shortopts );
110 module_list_free (list);
113 memcpy( argv_copy, ppsz_argv, *pi_argc * sizeof(char *) );
114 ppsz_argv = argv_copy;
118 for( i_index = 0; i_index < 256; i_index++ )
120 pp_shortopts[i_index] = NULL;
123 /* Fill the p_longopts and psz_shortopts structures */
125 for (size_t i = 0; (p_parser = list[i]) != NULL; i++)
127 module_config_t *p_item, *p_end;
129 if( !p_parser->i_config_items )
132 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
137 if( p_item->i_type & CONFIG_HINT )
140 /* Add item to long options */
141 p_longopts[i_index].name = strdup( p_item->psz_name );
142 if( p_longopts[i_index].name == NULL ) continue;
143 p_longopts[i_index].has_arg =
144 (p_item->i_type == CONFIG_ITEM_BOOL) ? no_argument :
148 /* It seems that required_argument is broken on Darwin.
149 * Radar ticket #6113829 */
152 p_longopts[i_index].flag = &flag;
153 p_longopts[i_index].val = 0;
156 /* When dealing with bools we also need to add the --no-foo
158 if( p_item->i_type == CONFIG_ITEM_BOOL )
160 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
161 if( psz_name == NULL ) continue;
162 strcpy( psz_name, "no" );
163 strcat( psz_name, p_item->psz_name );
165 p_longopts[i_index].name = psz_name;
166 p_longopts[i_index].has_arg = no_argument;
167 p_longopts[i_index].flag = &flag;
168 p_longopts[i_index].val = 1;
171 psz_name = malloc( strlen(p_item->psz_name) + 4 );
172 if( psz_name == NULL ) continue;
173 strcpy( psz_name, "no-" );
174 strcat( psz_name, p_item->psz_name );
176 p_longopts[i_index].name = psz_name;
177 p_longopts[i_index].has_arg = no_argument;
178 p_longopts[i_index].flag = &flag;
179 p_longopts[i_index].val = 1;
183 /* If item also has a short option, add it */
184 if( p_item->i_short )
186 pp_shortopts[(int)p_item->i_short] = p_item;
187 psz_shortopts[i_shortopts] = p_item->i_short;
189 if( p_item->i_type != CONFIG_ITEM_BOOL )
191 psz_shortopts[i_shortopts] = ':';
194 if( p_item->i_short == 'v' )
196 psz_shortopts[i_shortopts] = ':';
204 /* We don't need the module list anymore */
205 module_list_free( list );
207 /* Close the longopts and shortopts structures */
208 memset( &p_longopts[i_index], 0, sizeof(struct option) );
209 psz_shortopts[i_shortopts] = '\0';
212 * Parse the command line options
215 optind = 0; /* set to 0 to tell GNU getopt to reinitialize */
216 while( ( i_cmd = vlc_getopt_long( *pi_argc, (char **)ppsz_argv, psz_shortopts,
217 p_longopts, &i_index ) ) != -1 )
219 /* A long option has been recognized */
222 module_config_t *p_conf;
223 const char *psz_name = p_longopts[i_index].name;
225 /* Check if we deal with a --nofoo or --no-foo long option */
226 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
228 /* Store the configuration option */
229 p_conf = config_FindConfig( p_this, psz_name );
232 /* Check if the option is deprecated */
233 if( p_conf->b_removed )
236 "Warning: option --%s no longer exists.\n",
241 if( p_conf->psz_oldname
242 && !strcmp( p_conf->psz_oldname, psz_name) )
245 "%s: option --%s is deprecated. Use --%s instead.\n",
246 b_ignore_errors ? "Warning" : "Error",
247 psz_name, p_conf->psz_name );
248 if( !b_ignore_errors )
251 for( i_index = 0; p_longopts[i_index].name; i_index++ )
252 free( (char *)p_longopts[i_index].name );
255 free( psz_shortopts );
259 psz_name = p_conf->psz_name;
262 if( p_conf->i_type != CONFIG_ITEM_BOOL && !optarg )
264 fprintf( stderr, "Warning: missing argument for option --%s\n", p_conf->psz_name );
265 fprintf( stderr, "Try specifying options as '--optionname=value' instead of '--optionname value'\n" );
269 switch( p_conf->i_type )
271 case CONFIG_ITEM_STRING:
272 case CONFIG_ITEM_PASSWORD:
273 case CONFIG_ITEM_FILE:
274 case CONFIG_ITEM_DIRECTORY:
275 case CONFIG_ITEM_MODULE:
276 case CONFIG_ITEM_MODULE_LIST:
277 case CONFIG_ITEM_MODULE_LIST_CAT:
278 case CONFIG_ITEM_MODULE_CAT:
279 var_Create( p_this, psz_name, VLC_VAR_STRING );
280 var_SetString( p_this, psz_name, optarg );
282 case CONFIG_ITEM_INTEGER:
283 var_Create( p_this, psz_name, VLC_VAR_INTEGER );
284 var_SetInteger( p_this, psz_name,
285 strtol(optarg, NULL, 0));
287 case CONFIG_ITEM_FLOAT:
288 var_Create( p_this, psz_name, VLC_VAR_FLOAT );
289 var_SetFloat( p_this, psz_name, us_atof(optarg) );
291 case CONFIG_ITEM_KEY:
292 var_Create( p_this, psz_name, VLC_VAR_INTEGER );
293 var_SetInteger( p_this, psz_name,
294 ConfigStringToKey( optarg ) );
296 case CONFIG_ITEM_BOOL:
297 var_Create( p_this, psz_name, VLC_VAR_BOOL );
298 var_SetBool( p_this, psz_name, !flag );
305 /* A short option has been recognized */
306 if( pp_shortopts[i_cmd] != NULL )
308 const char *name = pp_shortopts[i_cmd]->psz_name;
309 switch( pp_shortopts[i_cmd]->i_type )
311 case CONFIG_ITEM_STRING:
312 case CONFIG_ITEM_PASSWORD:
313 case CONFIG_ITEM_FILE:
314 case CONFIG_ITEM_DIRECTORY:
315 case CONFIG_ITEM_MODULE:
316 case CONFIG_ITEM_MODULE_CAT:
317 case CONFIG_ITEM_MODULE_LIST:
318 case CONFIG_ITEM_MODULE_LIST_CAT:
319 var_Create( p_this, name, VLC_VAR_STRING );
320 var_SetString( p_this, name, optarg );
322 case CONFIG_ITEM_INTEGER:
323 var_Create( p_this, name, VLC_VAR_INTEGER );
328 if( *optarg == 'v' ) /* eg. -vvv */
331 while( *optarg == 'v' )
339 i_verbose += atoi( optarg ); /* eg. -v2 */
344 i_verbose++; /* -v */
346 var_SetInteger( p_this, name, i_verbose );
350 var_SetInteger( p_this, name,
351 strtol(optarg, NULL, 0) );
354 case CONFIG_ITEM_BOOL:
355 var_Create( p_this, name, VLC_VAR_BOOL );
356 var_SetBool( p_this, name, true );
363 /* Internal error: unknown option */
364 if( !b_ignore_errors )
366 fputs( "vlc: unknown option"
367 " or missing mandatory argument ", stderr );
370 fprintf( stderr, "`-%c'\n", optopt );
374 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
376 fputs( "Try `vlc --help' for more information.\n", stderr );
378 for( i_index = 0; p_longopts[i_index].name; i_index++ )
379 free( (char *)p_longopts[i_index].name );
381 free( psz_shortopts );
386 /* Free allocated resources */
387 for( i_index = 0; p_longopts[i_index].name; i_index++ )
388 free( (char *)p_longopts[i_index].name );
390 free( psz_shortopts );