1 /*****************************************************************************
2 * cmdline.c: command line parsing
3 *****************************************************************************
4 * Copyright (C) 2001-2007 VLC authors and VideoLAN
6 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
27 #include <vlc_common.h>
28 #include "../libvlc.h"
29 #include <vlc_actions.h>
30 #include <vlc_charset.h>
31 #include <vlc_modules.h>
32 #include <vlc_plugin.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;
61 struct vlc_option
*p_longopts
;
62 const char **argv_copy
= NULL
;
63 #define b_ignore_errors (pindex == NULL)
66 const module_config_t
*pp_shortopts
[256];
70 * Generate the longopts and shortopts structures used by getopt_long
74 for (const vlc_plugin_t
*p
= vlc_plugins
; p
!= NULL
; p
= p
->next
)
75 /* count the number of exported configuration options (to allocate
76 * longopts). We also need to allocate space for two options when
77 * dealing with boolean to allow for --foo and --no-foo */
78 i_opts
+= p
->conf
.count
+ 2 * p
->conf
.booleans
;
80 p_longopts
= vlc_alloc( i_opts
+ 1, sizeof(*p_longopts
) );
81 if( p_longopts
== NULL
)
84 psz_shortopts
= malloc( 2 * i_opts
+ 1 );
85 if( psz_shortopts
== NULL
)
91 /* If we are requested to ignore errors, then we must work on a copy
92 * of the ppsz_argv array, otherwise getopt_long will reorder it for
93 * us, ignoring the arity of the options */
96 argv_copy
= vlc_alloc( i_argc
, sizeof(char *) );
97 if( argv_copy
== NULL
)
99 free( psz_shortopts
);
103 memcpy( argv_copy
, ppsz_argv
, i_argc
* sizeof(char *) );
104 ppsz_argv
= argv_copy
;
108 for( i_index
= 0; i_index
< 256; i_index
++ )
110 pp_shortopts
[i_index
] = NULL
;
113 /* Fill the p_longopts and psz_shortopts structures */
115 for (const vlc_plugin_t
*p
= vlc_plugins
; p
!= NULL
; p
= p
->next
)
117 for (const module_config_t
*p_item
= p
->conf
.items
,
118 *p_end
= p_item
+ p
->conf
.size
;
123 if( !CONFIG_ITEM(p_item
->i_type
) )
126 /* Add item to long options */
127 p_longopts
[i_index
].name
= strdup( p_item
->psz_name
);
128 if( p_longopts
[i_index
].name
== NULL
) continue;
129 p_longopts
[i_index
].flag
= &flag
;
130 p_longopts
[i_index
].val
= 0;
132 if( CONFIG_CLASS(p_item
->i_type
) != CONFIG_ITEM_BOOL
)
133 p_longopts
[i_index
].has_arg
= true;
135 /* Booleans also need --no-foo and --nofoo options */
139 p_longopts
[i_index
].has_arg
= false;
142 if( asprintf( &psz_name
, "no%s", p_item
->psz_name
) == -1 )
144 p_longopts
[i_index
].name
= psz_name
;
145 p_longopts
[i_index
].has_arg
= false;
146 p_longopts
[i_index
].flag
= &flag
;
147 p_longopts
[i_index
].val
= 1;
150 if( asprintf( &psz_name
, "no-%s", p_item
->psz_name
) == -1 )
152 p_longopts
[i_index
].name
= psz_name
;
153 p_longopts
[i_index
].has_arg
= false;
154 p_longopts
[i_index
].flag
= &flag
;
155 p_longopts
[i_index
].val
= 1;
159 /* If item also has a short option, add it */
160 if( p_item
->i_short
)
162 pp_shortopts
[(int)p_item
->i_short
] = p_item
;
163 psz_shortopts
[i_shortopts
] = p_item
->i_short
;
165 if( p_item
->i_type
!= CONFIG_ITEM_BOOL
166 && p_item
->i_short
!= 'v' )
168 psz_shortopts
[i_shortopts
] = ':';
175 /* Close the longopts and shortopts structures */
176 memset( &p_longopts
[i_index
], 0, sizeof(*p_longopts
) );
177 psz_shortopts
[i_shortopts
] = '\0';
182 * Parse the command line options
185 state
.ind
= 0 ; /* set to 0 to tell GNU getopt to reinitialize */
186 while( ( i_cmd
= vlc_getopt_long( i_argc
, (char **)ppsz_argv
,
188 p_longopts
, &i_index
, &state
) ) != -1 )
190 /* A long option has been recognized */
193 module_config_t
*p_conf
;
194 const char *psz_name
= p_longopts
[i_index
].name
;
196 /* Check if we deal with a --nofoo or --no-foo long option */
197 if( flag
) psz_name
+= psz_name
[2] == '-' ? 3 : 2;
199 /* Store the configuration option */
200 p_conf
= config_FindConfig( psz_name
);
203 /* Check if the option is deprecated */
204 if( p_conf
->b_removed
)
207 "Warning: option --%s no longer exists.\n",
212 switch( CONFIG_CLASS(p_conf
->i_type
) )
214 case CONFIG_ITEM_STRING
:
215 var_Create( p_this
, psz_name
, VLC_VAR_STRING
);
216 var_SetString( p_this
, psz_name
, state
.arg
);
218 case CONFIG_ITEM_INTEGER
:
219 var_Create( p_this
, psz_name
, VLC_VAR_INTEGER
);
220 var_Change( p_this
, psz_name
, VLC_VAR_SETMINMAX
,
221 (vlc_value_t
){ .i_int
= p_conf
->min
.i
},
222 (vlc_value_t
){ .i_int
= p_conf
->max
.i
} );
223 var_SetInteger( p_this
, psz_name
,
224 strtoll(state
.arg
, NULL
, 0));
226 case CONFIG_ITEM_FLOAT
:
227 var_Create( p_this
, psz_name
, VLC_VAR_FLOAT
);
228 var_Change( p_this
, psz_name
, VLC_VAR_SETMINMAX
,
229 (vlc_value_t
){ .f_float
= p_conf
->min
.f
},
230 (vlc_value_t
){ .f_float
= p_conf
->max
.f
} );
231 var_SetFloat( p_this
, psz_name
, us_atof(state
.arg
) );
233 case CONFIG_ITEM_BOOL
:
234 var_Create( p_this
, psz_name
, VLC_VAR_BOOL
);
235 var_SetBool( p_this
, psz_name
, !flag
);
242 /* A short option has been recognized */
243 if( pp_shortopts
[i_cmd
] != NULL
)
245 const char *name
= pp_shortopts
[i_cmd
]->psz_name
;
246 switch( CONFIG_CLASS(pp_shortopts
[i_cmd
]->i_type
) )
248 case CONFIG_ITEM_STRING
:
249 var_Create( p_this
, name
, VLC_VAR_STRING
);
250 var_SetString( p_this
, name
, state
.arg
);
252 case CONFIG_ITEM_INTEGER
:
253 var_Create( p_this
, name
, VLC_VAR_INTEGER
);
256 i_verbose
++; /* -v */
257 var_SetInteger( p_this
, name
, i_verbose
);
261 var_SetInteger( p_this
, name
,
262 strtoll(state
.arg
, NULL
, 0) );
265 case CONFIG_ITEM_BOOL
:
266 var_Create( p_this
, name
, VLC_VAR_BOOL
);
267 var_SetBool( p_this
, name
, true );
274 /* Internal error: unknown option */
275 if( !b_ignore_errors
)
277 fputs( "vlc: unknown option"
278 " or missing mandatory argument ", stderr
);
281 fprintf( stderr
, "`-%c'\n", state
.opt
);
285 fprintf( stderr
, "`%s'\n", ppsz_argv
[state
.ind
-1] );
287 fputs( "Try `vlc --help' for more information.\n", stderr
);
296 /* Free allocated resources */
297 for( i_index
= 0; p_longopts
[i_index
].name
; i_index
++ )
298 free( (char *)p_longopts
[i_index
].name
);
300 free( psz_shortopts
);