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 module_config_t
*pp_shortopts
[256];
70 /* List all modules */
72 module_t
**list
= module_list_get (&count
);
75 * Generate the longopts and shortopts structures used by getopt_long
79 for (size_t i
= 0; i
< count
; i
++)
81 /* count the number of exported configuration options (to allocate
82 * longopts). We also need to allocate space for two options when
83 * dealing with boolean to allow for --foo and --no-foo */
84 module_t
*p_parser
= list
[i
];
86 i_opts
+= p_parser
->i_config_items
+ 2 * p_parser
->i_bool_items
;
89 p_longopts
= malloc( sizeof(*p_longopts
) * (i_opts
+ 1) );
90 if( p_longopts
== NULL
)
92 module_list_free (list
);
96 psz_shortopts
= malloc( 2 * i_opts
+ 1 );
97 if( psz_shortopts
== NULL
)
100 module_list_free (list
);
104 /* If we are requested to ignore errors, then we must work on a copy
105 * of the ppsz_argv array, otherwise getopt_long will reorder it for
106 * us, ignoring the arity of the options */
107 if( b_ignore_errors
)
109 argv_copy
= (const char**)malloc( i_argc
* sizeof(char *) );
110 if( argv_copy
== NULL
)
112 free( psz_shortopts
);
114 module_list_free (list
);
117 memcpy( argv_copy
, ppsz_argv
, i_argc
* sizeof(char *) );
118 ppsz_argv
= argv_copy
;
122 for( i_index
= 0; i_index
< 256; i_index
++ )
124 pp_shortopts
[i_index
] = NULL
;
127 /* Fill the p_longopts and psz_shortopts structures */
129 for (size_t i
= 0; i
< count
; i
++)
131 module_t
*p_parser
= list
[i
];
132 module_config_t
*p_item
, *p_end
;
134 if( !p_parser
->i_config_items
)
137 for( p_item
= p_parser
->p_config
, p_end
= p_item
+ p_parser
->confsize
;
142 if( !CONFIG_ITEM(p_item
->i_type
) )
145 /* Add item to long options */
146 p_longopts
[i_index
].name
= strdup( p_item
->psz_name
);
147 if( p_longopts
[i_index
].name
== NULL
) continue;
148 p_longopts
[i_index
].flag
= &flag
;
149 p_longopts
[i_index
].val
= 0;
151 if( CONFIG_CLASS(p_item
->i_type
) != CONFIG_ITEM_BOOL
)
152 p_longopts
[i_index
].has_arg
= true;
154 /* Booleans also need --no-foo and --nofoo options */
158 p_longopts
[i_index
].has_arg
= false;
161 if( asprintf( &psz_name
, "no%s", p_item
->psz_name
) == -1 )
163 p_longopts
[i_index
].name
= psz_name
;
164 p_longopts
[i_index
].has_arg
= false;
165 p_longopts
[i_index
].flag
= &flag
;
166 p_longopts
[i_index
].val
= 1;
169 if( asprintf( &psz_name
, "no-%s", p_item
->psz_name
) == -1 )
171 p_longopts
[i_index
].name
= psz_name
;
172 p_longopts
[i_index
].has_arg
= false;
173 p_longopts
[i_index
].flag
= &flag
;
174 p_longopts
[i_index
].val
= 1;
178 /* If item also has a short option, add it */
179 if( p_item
->i_short
)
181 pp_shortopts
[(int)p_item
->i_short
] = p_item
;
182 psz_shortopts
[i_shortopts
] = p_item
->i_short
;
184 if( p_item
->i_type
!= CONFIG_ITEM_BOOL
185 && p_item
->i_short
!= 'v' )
187 psz_shortopts
[i_shortopts
] = ':';
194 /* We don't need the module list anymore */
195 module_list_free( list
);
197 /* Close the longopts and shortopts structures */
198 memset( &p_longopts
[i_index
], 0, sizeof(*p_longopts
) );
199 psz_shortopts
[i_shortopts
] = '\0';
204 * Parse the command line options
207 state
.ind
= 0 ; /* set to 0 to tell GNU getopt to reinitialize */
208 while( ( i_cmd
= vlc_getopt_long( i_argc
, (char **)ppsz_argv
,
210 p_longopts
, &i_index
, &state
) ) != -1 )
212 /* A long option has been recognized */
215 module_config_t
*p_conf
;
216 const char *psz_name
= p_longopts
[i_index
].name
;
218 /* Check if we deal with a --nofoo or --no-foo long option */
219 if( flag
) psz_name
+= psz_name
[2] == '-' ? 3 : 2;
221 /* Store the configuration option */
222 p_conf
= config_FindConfig( p_this
, psz_name
);
225 /* Check if the option is deprecated */
226 if( p_conf
->b_removed
)
229 "Warning: option --%s no longer exists.\n",
234 switch( CONFIG_CLASS(p_conf
->i_type
) )
236 case CONFIG_ITEM_STRING
:
237 var_Create( p_this
, psz_name
, VLC_VAR_STRING
);
238 var_SetString( p_this
, psz_name
, state
.arg
);
240 case CONFIG_ITEM_INTEGER
:
241 var_Create( p_this
, psz_name
, VLC_VAR_INTEGER
);
242 var_SetInteger( p_this
, psz_name
,
243 strtoll(state
.arg
, NULL
, 0));
245 case CONFIG_ITEM_FLOAT
:
246 var_Create( p_this
, psz_name
, VLC_VAR_FLOAT
);
247 var_SetFloat( p_this
, psz_name
, us_atof(state
.arg
) );
249 case CONFIG_ITEM_BOOL
:
250 var_Create( p_this
, psz_name
, VLC_VAR_BOOL
);
251 var_SetBool( p_this
, psz_name
, !flag
);
258 /* A short option has been recognized */
259 if( pp_shortopts
[i_cmd
] != NULL
)
261 const char *name
= pp_shortopts
[i_cmd
]->psz_name
;
262 switch( CONFIG_CLASS(pp_shortopts
[i_cmd
]->i_type
) )
264 case CONFIG_ITEM_STRING
:
265 var_Create( p_this
, name
, VLC_VAR_STRING
);
266 var_SetString( p_this
, name
, state
.arg
);
268 case CONFIG_ITEM_INTEGER
:
269 var_Create( p_this
, name
, VLC_VAR_INTEGER
);
272 i_verbose
++; /* -v */
273 var_SetInteger( p_this
, name
, i_verbose
);
277 var_SetInteger( p_this
, name
,
278 strtoll(state
.arg
, NULL
, 0) );
281 case CONFIG_ITEM_BOOL
:
282 var_Create( p_this
, name
, VLC_VAR_BOOL
);
283 var_SetBool( p_this
, name
, true );
290 /* Internal error: unknown option */
291 if( !b_ignore_errors
)
293 fputs( "vlc: unknown option"
294 " or missing mandatory argument ", stderr
);
297 fprintf( stderr
, "`-%c'\n", state
.opt
);
301 fprintf( stderr
, "`%s'\n", ppsz_argv
[state
.ind
-1] );
303 fputs( "Try `vlc --help' for more information.\n", stderr
);
312 /* Free allocated resources */
313 for( i_index
= 0; p_longopts
[i_index
].name
; i_index
++ )
314 free( (char *)p_longopts
[i_index
].name
);
316 free( psz_shortopts
);