1 /*****************************************************************************
2 * cmdline.c: command line parsing
3 *****************************************************************************
4 * Copyright (C) 2001-2007 VLC authors and VideoLAN
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 *****************************************************************************/
28 #include <vlc_common.h>
29 #include "../libvlc.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"
42 #undef config_LoadCmdLine
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)
67 const module_config_t
*pp_shortopts
[256];
71 * Generate the longopts and shortopts structures used by getopt_long
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
= malloc( sizeof(*p_longopts
) * (i_opts
+ 1) );
82 if( p_longopts
== NULL
)
85 psz_shortopts
= malloc( 2 * i_opts
+ 1 );
86 if( psz_shortopts
== NULL
)
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 */
97 argv_copy
= (const char**)malloc( i_argc
* sizeof(char *) );
98 if( argv_copy
== NULL
)
100 free( psz_shortopts
);
104 memcpy( argv_copy
, ppsz_argv
, i_argc
* sizeof(char *) );
105 ppsz_argv
= argv_copy
;
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 */
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
;
124 if( !CONFIG_ITEM(p_item
->i_type
) )
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;
136 /* Booleans also need --no-foo and --nofoo options */
140 p_longopts
[i_index
].has_arg
= false;
143 if( asprintf( &psz_name
, "no%s", p_item
->psz_name
) == -1 )
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;
151 if( asprintf( &psz_name
, "no-%s", p_item
->psz_name
) == -1 )
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;
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
;
166 if( p_item
->i_type
!= CONFIG_ITEM_BOOL
167 && p_item
->i_short
!= 'v' )
169 psz_shortopts
[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';
183 * Parse the command line options
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
,
189 p_longopts
, &i_index
, &state
) ) != -1 )
191 /* A long option has been recognized */
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( p_this
, psz_name
);
204 /* Check if the option is deprecated */
205 if( p_conf
->b_removed
)
208 "Warning: option --%s no longer exists.\n",
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
);
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));
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
) );
234 case CONFIG_ITEM_BOOL
:
235 var_Create( p_this
, psz_name
, VLC_VAR_BOOL
);
236 var_SetBool( p_this
, psz_name
, !flag
);
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
);
253 case CONFIG_ITEM_INTEGER
:
254 var_Create( p_this
, name
, VLC_VAR_INTEGER
);
257 i_verbose
++; /* -v */
258 var_SetInteger( p_this
, name
, i_verbose
);
262 var_SetInteger( p_this
, name
,
263 strtoll(state
.arg
, NULL
, 0) );
266 case CONFIG_ITEM_BOOL
:
267 var_Create( p_this
, name
, VLC_VAR_BOOL
);
268 var_SetBool( p_this
, name
, true );
275 /* Internal error: unknown option */
276 if( !b_ignore_errors
)
278 fputs( "vlc: unknown option"
279 " or missing mandatory argument ", stderr
);
282 fprintf( stderr
, "`-%c'\n", state
.opt
);
286 fprintf( stderr
, "`%s'\n", ppsz_argv
[state
.ind
-1] );
288 fputs( "Try `vlc --help' for more information.\n", stderr
);
297 /* Free allocated resources */
298 for( i_index
= 0; p_longopts
[i_index
].name
; i_index
++ )
299 free( (char *)p_longopts
[i_index
].name
);
301 free( psz_shortopts
);