]> git.sesse.net Git - vlc/blob - src/config/cmdline.c
modules: remove plugins count from the plugins cache
[vlc] / src / config / cmdline.c
1 /*****************************************************************************
2  * cmdline.c: command line parsing
3  *****************************************************************************
4  * Copyright (C) 2001-2007 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include "../libvlc.h"
30 #include <vlc_keys.h>
31 #include <vlc_charset.h>
32 #include <vlc_modules.h>
33 #include <vlc_plugin.h>
34
35 #include "vlc_getopt.h"
36
37 #include "configuration.h"
38 #include "modules/modules.h"
39
40 #include <assert.h>
41
42 #undef config_LoadCmdLine
43 /**
44  * Parse command line for configuration options.
45  *
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.
50  *
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.
57  */
58 int config_LoadCmdLine( vlc_object_t *p_this, int i_argc,
59                         const char *ppsz_argv[], int *pindex )
60 {
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)
65
66     /* Short options */
67     module_config_t *pp_shortopts[256];
68     char *psz_shortopts;
69
70     /* List all modules */
71     size_t count;
72     module_t **list = module_list_get (&count);
73
74     /*
75      * Generate the longopts and shortopts structures used by getopt_long
76      */
77
78     i_opts = 0;
79     for (size_t i = 0; i < count; i++)
80     {
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];
85
86         i_opts += p_parser->i_config_items + 2 * p_parser->i_bool_items;
87     }
88
89     p_longopts = malloc( sizeof(*p_longopts) * (i_opts + 1) );
90     if( p_longopts == NULL )
91     {
92         module_list_free (list);
93         return -1;
94     }
95
96     psz_shortopts = malloc( 2 * i_opts + 1 );
97     if( psz_shortopts == NULL )
98     {
99         free( p_longopts );
100         module_list_free (list);
101         return -1;
102     }
103
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 )
108     {
109         argv_copy = (const char**)malloc( i_argc * sizeof(char *) );
110         if( argv_copy == NULL )
111         {
112             free( psz_shortopts );
113             free( p_longopts );
114             module_list_free (list);
115             return -1;
116         }
117         memcpy( argv_copy, ppsz_argv, i_argc * sizeof(char *) );
118         ppsz_argv = argv_copy;
119     }
120
121     i_shortopts = 0;
122     for( i_index = 0; i_index < 256; i_index++ )
123     {
124         pp_shortopts[i_index] = NULL;
125     }
126
127     /* Fill the p_longopts and psz_shortopts structures */
128     i_index = 0;
129     for (size_t i = 0; i < count; i++)
130     {
131         module_t *p_parser = list[i];
132         module_config_t *p_item, *p_end;
133
134         if( !p_parser->i_config_items )
135             continue;
136
137         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
138              p_item < p_end;
139              p_item++ )
140         {
141             /* Ignore hints */
142             if( !CONFIG_ITEM(p_item->i_type) )
143                 continue;
144
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;
150
151             if( CONFIG_CLASS(p_item->i_type) != CONFIG_ITEM_BOOL )
152                 p_longopts[i_index].has_arg = true;
153             else
154             /* Booleans also need --no-foo and --nofoo options */
155             {
156                 char *psz_name;
157
158                 p_longopts[i_index].has_arg = false;
159                 i_index++;
160
161                 if( asprintf( &psz_name, "no%s", p_item->psz_name ) == -1 )
162                     continue;
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;
167                 i_index++;
168
169                 if( asprintf( &psz_name, "no-%s", p_item->psz_name ) == -1 )
170                     continue;
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;
175             }
176             i_index++;
177
178             /* If item also has a short option, add it */
179             if( p_item->i_short )
180             {
181                 pp_shortopts[(int)p_item->i_short] = p_item;
182                 psz_shortopts[i_shortopts] = p_item->i_short;
183                 i_shortopts++;
184                 if( p_item->i_type != CONFIG_ITEM_BOOL
185                  && p_item->i_short != 'v' )
186                 {
187                     psz_shortopts[i_shortopts] = ':';
188                     i_shortopts++;
189                 }
190             }
191         }
192     }
193
194     /* We don't need the module list anymore */
195     module_list_free( list );
196
197     /* Close the longopts and shortopts structures */
198     memset( &p_longopts[i_index], 0, sizeof(*p_longopts) );
199     psz_shortopts[i_shortopts] = '\0';
200
201     int ret = -1;
202
203     /*
204      * Parse the command line options
205      */
206     vlc_getopt_t state;
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,
209                                       psz_shortopts,
210                                       p_longopts, &i_index, &state ) ) != -1 )
211     {
212         /* A long option has been recognized */
213         if( i_cmd == 0 )
214         {
215             module_config_t *p_conf;
216             const char *psz_name = p_longopts[i_index].name;
217
218             /* Check if we deal with a --nofoo or --no-foo long option */
219             if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
220
221             /* Store the configuration option */
222             p_conf = config_FindConfig( p_this, psz_name );
223             if( p_conf )
224             {
225                 /* Check if the option is deprecated */
226                 if( p_conf->b_removed )
227                 {
228                     fprintf(stderr,
229                             "Warning: option --%s no longer exists.\n",
230                             psz_name);
231                     continue;
232                 }
233
234                 switch( CONFIG_CLASS(p_conf->i_type) )
235                 {
236                     case CONFIG_ITEM_STRING:
237                         var_Create( p_this, psz_name, VLC_VAR_STRING );
238                         var_SetString( p_this, psz_name, state.arg );
239                         break;
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));
244                         break;
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) );
248                         break;
249                     case CONFIG_ITEM_BOOL:
250                         var_Create( p_this, psz_name, VLC_VAR_BOOL );
251                         var_SetBool( p_this, psz_name, !flag );
252                         break;
253                 }
254                 continue;
255             }
256         }
257
258         /* A short option has been recognized */
259         if( pp_shortopts[i_cmd] != NULL )
260         {
261             const char *name = pp_shortopts[i_cmd]->psz_name;
262             switch( CONFIG_CLASS(pp_shortopts[i_cmd]->i_type) )
263             {
264                 case CONFIG_ITEM_STRING:
265                     var_Create( p_this, name, VLC_VAR_STRING );
266                     var_SetString( p_this, name, state.arg );
267                     break;
268                 case CONFIG_ITEM_INTEGER:
269                     var_Create( p_this, name, VLC_VAR_INTEGER );
270                     if( i_cmd == 'v' )
271                     {
272                         i_verbose++; /* -v */
273                         var_SetInteger( p_this, name, i_verbose );
274                     }
275                     else
276                     {
277                         var_SetInteger( p_this, name,
278                                         strtoll(state.arg, NULL, 0) );
279                     }
280                     break;
281                 case CONFIG_ITEM_BOOL:
282                     var_Create( p_this, name, VLC_VAR_BOOL );
283                     var_SetBool( p_this, name, true );
284                     break;
285             }
286
287             continue;
288         }
289
290         /* Internal error: unknown option */
291         if( !b_ignore_errors )
292         {
293             fputs( "vlc: unknown option"
294                      " or missing mandatory argument ", stderr );
295             if( state.opt )
296             {
297                 fprintf( stderr, "`-%c'\n", state.opt );
298             }
299             else
300             {
301                 fprintf( stderr, "`%s'\n", ppsz_argv[state.ind-1] );
302             }
303             fputs( "Try `vlc --help' for more information.\n", stderr );
304             goto out;
305         }
306     }
307
308     ret = 0;
309     if( pindex != NULL )
310         *pindex = state.ind;
311 out:
312     /* Free allocated resources */
313     for( i_index = 0; p_longopts[i_index].name; i_index++ )
314         free( (char *)p_longopts[i_index].name );
315     free( p_longopts );
316     free( psz_shortopts );
317     free( argv_copy );
318     return ret;
319 }
320