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 #ifdef HAVE_GETOPT_LONG
35 # include <getopt.h> /* getopt() */
38 # include "../extras/getopt.h"
41 #include "configuration.h"
42 #include "modules/modules.h"
46 #undef config_LoadCmdLine
47 /*****************************************************************************
48 * config_LoadCmdLine: parse command line
49 *****************************************************************************
50 * Parse command line for configuration options.
51 * Now that the module_bank has been initialized, we can dynamically
52 * generate the longopts structure used by getops. We have to do it this way
53 * because we don't know (and don't want to know) in advance the configuration
54 * options used (ie. exported) by each module.
55 *****************************************************************************/
56 int config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc,
57 const char *ppsz_argv[], bool b_ignore_errors )
59 int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
61 struct option *p_longopts;
62 const char **argv_copy = NULL;
65 module_config_t *pp_shortopts[256];
68 /* List all modules */
69 module_t **list = module_list_get (NULL);
72 * Generate the longopts and shortopts structures used by getopt_long
76 for (size_t i = 0; (p_parser = list[i]) != NULL; i++)
77 /* count the number of exported configuration options (to allocate
78 * longopts). We also need to allocate space for two options when
79 * dealing with boolean to allow for --foo and --no-foo */
80 i_opts += p_parser->i_config_items + 2 * p_parser->i_bool_items;
82 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
83 if( p_longopts == NULL )
85 module_list_free (list);
89 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
90 if( psz_shortopts == NULL )
93 module_list_free (list);
97 /* If we are requested to ignore errors, then we must work on a copy
98 * of the ppsz_argv array, otherwise getopt_long will reorder it for
99 * us, ignoring the arity of the options */
100 if( b_ignore_errors )
102 argv_copy = (const char**)malloc( *pi_argc * sizeof(char *) );
103 if( argv_copy == NULL )
105 free( psz_shortopts );
107 module_list_free (list);
110 memcpy( argv_copy, ppsz_argv, *pi_argc * sizeof(char *) );
111 ppsz_argv = argv_copy;
115 for( i_index = 0; i_index < 256; i_index++ )
117 pp_shortopts[i_index] = NULL;
120 /* Fill the p_longopts and psz_shortopts structures */
122 for (size_t i = 0; (p_parser = list[i]) != NULL; i++)
124 module_config_t *p_item, *p_end;
126 if( !p_parser->i_config_items )
129 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
134 if( p_item->i_type & CONFIG_HINT )
137 /* Add item to long options */
138 p_longopts[i_index].name = strdup( p_item->psz_name );
139 if( p_longopts[i_index].name == NULL ) continue;
140 p_longopts[i_index].has_arg =
141 (p_item->i_type == CONFIG_ITEM_BOOL) ? no_argument :
145 /* It seems that required_argument is broken on Darwin.
146 * Radar ticket #6113829 */
149 p_longopts[i_index].flag = &flag;
150 p_longopts[i_index].val = 0;
153 /* When dealing with bools we also need to add the --no-foo
155 if( p_item->i_type == CONFIG_ITEM_BOOL )
157 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
158 if( psz_name == NULL ) continue;
159 strcpy( psz_name, "no" );
160 strcat( psz_name, p_item->psz_name );
162 p_longopts[i_index].name = psz_name;
163 p_longopts[i_index].has_arg = no_argument;
164 p_longopts[i_index].flag = &flag;
165 p_longopts[i_index].val = 1;
168 psz_name = malloc( strlen(p_item->psz_name) + 4 );
169 if( psz_name == NULL ) continue;
170 strcpy( psz_name, "no-" );
171 strcat( psz_name, p_item->psz_name );
173 p_longopts[i_index].name = psz_name;
174 p_longopts[i_index].has_arg = no_argument;
175 p_longopts[i_index].flag = &flag;
176 p_longopts[i_index].val = 1;
180 /* If item also has a short option, add it */
181 if( p_item->i_short )
183 pp_shortopts[(int)p_item->i_short] = p_item;
184 psz_shortopts[i_shortopts] = p_item->i_short;
186 if( p_item->i_type != CONFIG_ITEM_BOOL )
188 psz_shortopts[i_shortopts] = ':';
191 if( p_item->i_short == 'v' )
193 psz_shortopts[i_shortopts] = ':';
201 /* We don't need the module list anymore */
202 module_list_free( list );
204 /* Close the longopts and shortopts structures */
205 memset( &p_longopts[i_index], 0, sizeof(struct option) );
206 psz_shortopts[i_shortopts] = '\0';
209 * Parse the command line options
212 optind = 0; /* set to 0 to tell GNU getopt to reinitialize */
213 while( ( i_cmd = getopt_long( *pi_argc, (char **)ppsz_argv, psz_shortopts,
214 p_longopts, &i_index ) ) != -1 )
216 /* A long option has been recognized */
219 module_config_t *p_conf;
220 const char *psz_name = p_longopts[i_index].name;
222 /* Check if we deal with a --nofoo or --no-foo long option */
223 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
225 /* Store the configuration option */
226 p_conf = config_FindConfig( p_this, psz_name );
229 /* Check if the option is deprecated */
230 if( p_conf->b_removed )
233 "Warning: option --%s no longer exists.\n",
238 if( p_conf->psz_oldname
239 && !strcmp( p_conf->psz_oldname, psz_name) )
242 "%s: option --%s is deprecated. Use --%s instead.\n",
243 b_ignore_errors ? "Warning" : "Error",
244 psz_name, p_conf->psz_name );
245 if( !b_ignore_errors )
248 for( i_index = 0; p_longopts[i_index].name; i_index++ )
249 free( (char *)p_longopts[i_index].name );
252 free( psz_shortopts );
256 psz_name = p_conf->psz_name;
259 if( p_conf->i_type != CONFIG_ITEM_BOOL && !optarg )
261 fprintf( stderr, "Warning: missing argument for option --%s\n", p_conf->psz_name );
262 fprintf( stderr, "Try specifying options as '--optionname=value' instead of '--optionname value'\n" );
266 switch( p_conf->i_type )
268 case CONFIG_ITEM_STRING:
269 case CONFIG_ITEM_PASSWORD:
270 case CONFIG_ITEM_FILE:
271 case CONFIG_ITEM_DIRECTORY:
272 case CONFIG_ITEM_MODULE:
273 case CONFIG_ITEM_MODULE_LIST:
274 case CONFIG_ITEM_MODULE_LIST_CAT:
275 case CONFIG_ITEM_MODULE_CAT:
276 var_Create( p_this, psz_name, VLC_VAR_STRING );
277 var_SetString( p_this, psz_name, optarg );
279 case CONFIG_ITEM_INTEGER:
280 var_Create( p_this, psz_name, VLC_VAR_INTEGER );
281 var_SetInteger( p_this, psz_name,
282 strtol(optarg, NULL, 0));
284 case CONFIG_ITEM_FLOAT:
285 var_Create( p_this, psz_name, VLC_VAR_FLOAT );
286 var_SetFloat( p_this, psz_name, us_atof(optarg) );
288 case CONFIG_ITEM_KEY:
289 var_Create( p_this, psz_name, VLC_VAR_INTEGER );
290 var_SetInteger( p_this, psz_name,
291 ConfigStringToKey( optarg ) );
293 case CONFIG_ITEM_BOOL:
294 var_Create( p_this, psz_name, VLC_VAR_BOOL );
295 var_SetBool( p_this, psz_name, !flag );
302 /* A short option has been recognized */
303 if( pp_shortopts[i_cmd] != NULL )
305 const char *name = pp_shortopts[i_cmd]->psz_name;
306 switch( pp_shortopts[i_cmd]->i_type )
308 case CONFIG_ITEM_STRING:
309 case CONFIG_ITEM_PASSWORD:
310 case CONFIG_ITEM_FILE:
311 case CONFIG_ITEM_DIRECTORY:
312 case CONFIG_ITEM_MODULE:
313 case CONFIG_ITEM_MODULE_CAT:
314 case CONFIG_ITEM_MODULE_LIST:
315 case CONFIG_ITEM_MODULE_LIST_CAT:
316 var_Create( p_this, name, VLC_VAR_STRING );
317 var_SetString( p_this, name, optarg );
319 case CONFIG_ITEM_INTEGER:
320 var_Create( p_this, name, VLC_VAR_INTEGER );
325 if( *optarg == 'v' ) /* eg. -vvv */
328 while( *optarg == 'v' )
336 i_verbose += atoi( optarg ); /* eg. -v2 */
341 i_verbose++; /* -v */
343 var_SetInteger( p_this, name, i_verbose );
347 var_SetInteger( p_this, name,
348 strtol(optarg, NULL, 0) );
351 case CONFIG_ITEM_BOOL:
352 var_Create( p_this, name, VLC_VAR_BOOL );
353 var_SetBool( p_this, name, true );
360 /* Internal error: unknown option */
361 if( !b_ignore_errors )
363 fputs( "vlc: unknown option"
364 " or missing mandatory argument ", stderr );
367 fprintf( stderr, "`-%c'\n", optopt );
371 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
373 fputs( "Try `vlc --help' for more information.\n", stderr );
375 for( i_index = 0; p_longopts[i_index].name; i_index++ )
376 free( (char *)p_longopts[i_index].name );
378 free( psz_shortopts );
383 /* Free allocated resources */
384 for( i_index = 0; p_longopts[i_index].name; i_index++ )
385 free( (char *)p_longopts[i_index].name );
387 free( psz_shortopts );