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>
32 #include <vlc_modules.h>
34 #include "vlc_getopt.h"
36 #include "configuration.h"
37 #include "modules/modules.h"
41 #undef config_LoadCmdLine
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;
62 struct vlc_option *p_longopts;
63 const char **argv_copy = NULL;
64 #define b_ignore_errors (pindex == NULL)
67 module_config_t *pp_shortopts[256];
70 /* List all modules */
71 module_t **list = module_list_get (NULL);
74 * Generate the longopts and shortopts structures used by getopt_long
78 for (size_t i = 0; (p_parser = list[i]) != NULL; i++)
79 /* count the number of exported configuration options (to allocate
80 * longopts). We also need to allocate space for two options when
81 * dealing with boolean to allow for --foo and --no-foo */
82 i_opts += p_parser->i_config_items + 2 * p_parser->i_bool_items;
84 p_longopts = malloc( sizeof(*p_longopts) * (i_opts + 1) );
85 if( p_longopts == NULL )
87 module_list_free (list);
91 psz_shortopts = malloc( 2 * i_opts + 1 );
92 if( psz_shortopts == NULL )
95 module_list_free (list);
99 /* If we are requested to ignore errors, then we must work on a copy
100 * of the ppsz_argv array, otherwise getopt_long will reorder it for
101 * us, ignoring the arity of the options */
102 if( b_ignore_errors )
104 argv_copy = (const char**)malloc( i_argc * sizeof(char *) );
105 if( argv_copy == NULL )
107 free( psz_shortopts );
109 module_list_free (list);
112 memcpy( argv_copy, ppsz_argv, i_argc * sizeof(char *) );
113 ppsz_argv = argv_copy;
117 for( i_index = 0; i_index < 256; i_index++ )
119 pp_shortopts[i_index] = NULL;
122 /* Fill the p_longopts and psz_shortopts structures */
124 for (size_t i = 0; (p_parser = list[i]) != NULL; i++)
126 module_config_t *p_item, *p_end;
128 if( !p_parser->i_config_items )
131 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
136 if( p_item->i_type & CONFIG_HINT )
139 /* Add item to long options */
140 p_longopts[i_index].name = strdup( p_item->psz_name );
141 if( p_longopts[i_index].name == NULL ) continue;
142 p_longopts[i_index].has_arg =
143 (p_item->i_type != CONFIG_ITEM_BOOL);
144 p_longopts[i_index].flag = &flag;
145 p_longopts[i_index].val = 0;
148 /* When dealing with bools we also need to add the --no-foo
150 if( p_item->i_type == CONFIG_ITEM_BOOL )
152 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
153 if( psz_name == NULL ) continue;
154 strcpy( psz_name, "no" );
155 strcat( psz_name, p_item->psz_name );
157 p_longopts[i_index].name = psz_name;
158 p_longopts[i_index].has_arg = false;
159 p_longopts[i_index].flag = &flag;
160 p_longopts[i_index].val = 1;
163 psz_name = malloc( strlen(p_item->psz_name) + 4 );
164 if( psz_name == NULL ) continue;
165 strcpy( psz_name, "no-" );
166 strcat( psz_name, p_item->psz_name );
168 p_longopts[i_index].name = psz_name;
169 p_longopts[i_index].has_arg = false;
170 p_longopts[i_index].flag = &flag;
171 p_longopts[i_index].val = 1;
175 /* If item also has a short option, add it */
176 if( p_item->i_short )
178 pp_shortopts[(int)p_item->i_short] = p_item;
179 psz_shortopts[i_shortopts] = p_item->i_short;
181 if( p_item->i_type != CONFIG_ITEM_BOOL
182 && p_item->i_short != 'v' )
184 psz_shortopts[i_shortopts] = ':';
191 /* We don't need the module list anymore */
192 module_list_free( list );
194 /* Close the longopts and shortopts structures */
195 memset( &p_longopts[i_index], 0, sizeof(*p_longopts) );
196 psz_shortopts[i_shortopts] = '\0';
201 * Parse the command line options
204 state.ind = 0 ; /* set to 0 to tell GNU getopt to reinitialize */
205 while( ( i_cmd = vlc_getopt_long( i_argc, (char **)ppsz_argv,
207 p_longopts, &i_index, &state ) ) != -1 )
209 /* A long option has been recognized */
212 module_config_t *p_conf;
213 const char *psz_name = p_longopts[i_index].name;
215 /* Check if we deal with a --nofoo or --no-foo long option */
216 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
218 /* Store the configuration option */
219 p_conf = config_FindConfig( p_this, psz_name );
222 /* Check if the option is deprecated */
223 if( p_conf->b_removed )
226 "Warning: option --%s no longer exists.\n",
231 if( p_conf->psz_oldname
232 && !strcmp( p_conf->psz_oldname, psz_name) )
234 if( !b_ignore_errors )
236 fprintf( stderr, "Error: option --%s is deprecated. "
237 "Use --%s instead.\n",
238 psz_name, p_conf->psz_name );
242 psz_name = p_conf->psz_name;
245 switch( p_conf->i_type )
247 case CONFIG_ITEM_STRING:
248 case CONFIG_ITEM_PASSWORD:
249 case CONFIG_ITEM_FILE:
250 case CONFIG_ITEM_DIRECTORY:
251 case CONFIG_ITEM_MODULE:
252 case CONFIG_ITEM_MODULE_LIST:
253 case CONFIG_ITEM_MODULE_LIST_CAT:
254 case CONFIG_ITEM_MODULE_CAT:
255 var_Create( p_this, psz_name, VLC_VAR_STRING );
256 var_SetString( p_this, psz_name, state.arg );
258 case CONFIG_ITEM_INTEGER:
259 var_Create( p_this, psz_name, VLC_VAR_INTEGER );
260 var_SetInteger( p_this, psz_name,
261 strtoll(state.arg, NULL, 0));
263 case CONFIG_ITEM_FLOAT:
264 var_Create( p_this, psz_name, VLC_VAR_FLOAT );
265 var_SetFloat( p_this, psz_name, us_atof(state.arg) );
267 case CONFIG_ITEM_KEY:
268 var_Create( p_this, psz_name, VLC_VAR_INTEGER );
269 var_SetInteger( p_this, psz_name,
270 ConfigStringToKey( state.arg ) );
272 case CONFIG_ITEM_BOOL:
273 var_Create( p_this, psz_name, VLC_VAR_BOOL );
274 var_SetBool( p_this, psz_name, !flag );
281 /* A short option has been recognized */
282 if( pp_shortopts[i_cmd] != NULL )
284 const char *name = pp_shortopts[i_cmd]->psz_name;
285 switch( pp_shortopts[i_cmd]->i_type )
287 case CONFIG_ITEM_STRING:
288 case CONFIG_ITEM_PASSWORD:
289 case CONFIG_ITEM_FILE:
290 case CONFIG_ITEM_DIRECTORY:
291 case CONFIG_ITEM_MODULE:
292 case CONFIG_ITEM_MODULE_CAT:
293 case CONFIG_ITEM_MODULE_LIST:
294 case CONFIG_ITEM_MODULE_LIST_CAT:
295 var_Create( p_this, name, VLC_VAR_STRING );
296 var_SetString( p_this, name, state.arg );
298 case CONFIG_ITEM_INTEGER:
299 var_Create( p_this, name, VLC_VAR_INTEGER );
302 i_verbose++; /* -v */
303 var_SetInteger( p_this, name, i_verbose );
307 var_SetInteger( p_this, name,
308 strtoll(state.arg, NULL, 0) );
311 case CONFIG_ITEM_BOOL:
312 var_Create( p_this, name, VLC_VAR_BOOL );
313 var_SetBool( p_this, name, true );
320 /* Internal error: unknown option */
321 if( !b_ignore_errors )
323 fputs( "vlc: unknown option"
324 " or missing mandatory argument ", stderr );
327 fprintf( stderr, "`-%c'\n", state.opt );
331 fprintf( stderr, "`%s'\n", ppsz_argv[state.ind-1] );
333 fputs( "Try `vlc --help' for more information.\n", stderr );
342 /* Free allocated resources */
343 for( i_index = 0; p_longopts[i_index].name; i_index++ )
344 free( (char *)p_longopts[i_index].name );
346 free( psz_shortopts );