]> git.sesse.net Git - vlc/blob - src/config/cmdline.c
d01c1337344ed7d9eeca86b831791de9e8bce0dc
[vlc] / src / config / cmdline.c
1 /*****************************************************************************
2  * cmdline.c: command line parsing
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
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 General Public License for more details.
18  *
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  *****************************************************************************/
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
33 #include "vlc_getopt.h"
34
35 #include "configuration.h"
36 #include "modules/modules.h"
37
38 #include <assert.h>
39
40 #undef config_LoadCmdLine
41 /**
42  * Parse command line for configuration options.
43  *
44  * Now that the module_bank has been initialized, we can dynamically
45  * generate the longopts structure used by getops. We have to do it this way
46  * because we don't know (and don't want to know) in advance the configuration
47  * options used (ie. exported) by each module.
48  *
49  * @param p_this object to write command line options as variables to
50  * @param i_argc number of command line arguments
51  * @param ppsz_args commandl ine arguments [IN/OUT]
52  * @param pindex NULL to ignore unknown options,
53  *               otherwise index of the first non-option argument [OUT]
54  * @return 0 on success, -1 on error.
55  */
56 int config_LoadCmdLine( vlc_object_t *p_this, int i_argc,
57                         const char *ppsz_argv[], int *pindex )
58 {
59     int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
60     module_t *p_parser;
61     struct vlc_option *p_longopts;
62     const char **argv_copy = NULL;
63 #define b_ignore_errors (pindex == NULL)
64
65     /* Short options */
66     module_config_t *pp_shortopts[256];
67     char *psz_shortopts;
68
69     /* List all modules */
70     module_t **list = module_list_get (NULL);
71
72     /*
73      * Generate the longopts and shortopts structures used by getopt_long
74      */
75
76     i_opts = 0;
77     for (size_t i = 0; (p_parser = list[i]) != NULL; i++)
78         /* count the number of exported configuration options (to allocate
79          * longopts). We also need to allocate space for two options when
80          * dealing with boolean to allow for --foo and --no-foo */
81         i_opts += p_parser->i_config_items + 2 * p_parser->i_bool_items;
82
83     p_longopts = malloc( sizeof(*p_longopts) * (i_opts + 1) );
84     if( p_longopts == NULL )
85     {
86         module_list_free (list);
87         return -1;
88     }
89
90     psz_shortopts = malloc( 2 * i_opts + 1 );
91     if( psz_shortopts == NULL )
92     {
93         free( p_longopts );
94         module_list_free (list);
95         return -1;
96     }
97
98     /* If we are requested to ignore errors, then we must work on a copy
99      * of the ppsz_argv array, otherwise getopt_long will reorder it for
100      * us, ignoring the arity of the options */
101     if( b_ignore_errors )
102     {
103         argv_copy = (const char**)malloc( i_argc * sizeof(char *) );
104         if( argv_copy == NULL )
105         {
106             free( psz_shortopts );
107             free( p_longopts );
108             module_list_free (list);
109             return -1;
110         }
111         memcpy( argv_copy, ppsz_argv, i_argc * sizeof(char *) );
112         ppsz_argv = argv_copy;
113     }
114
115     i_shortopts = 0;
116     for( i_index = 0; i_index < 256; i_index++ )
117     {
118         pp_shortopts[i_index] = NULL;
119     }
120
121     /* Fill the p_longopts and psz_shortopts structures */
122     i_index = 0;
123     for (size_t i = 0; (p_parser = list[i]) != NULL; i++)
124     {
125         module_config_t *p_item, *p_end;
126
127         if( !p_parser->i_config_items )
128             continue;
129
130         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
131              p_item < p_end;
132              p_item++ )
133         {
134             /* Ignore hints */
135             if( p_item->i_type & CONFIG_HINT )
136                 continue;
137
138             /* Add item to long options */
139             p_longopts[i_index].name = strdup( p_item->psz_name );
140             if( p_longopts[i_index].name == NULL ) continue;
141             p_longopts[i_index].has_arg =
142                 (p_item->i_type != CONFIG_ITEM_BOOL);
143             p_longopts[i_index].flag = &flag;
144             p_longopts[i_index].val = 0;
145             i_index++;
146
147             /* When dealing with bools we also need to add the --no-foo
148              * option */
149             if( p_item->i_type == CONFIG_ITEM_BOOL )
150             {
151                 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
152                 if( psz_name == NULL ) continue;
153                 strcpy( psz_name, "no" );
154                 strcat( psz_name, p_item->psz_name );
155
156                 p_longopts[i_index].name = psz_name;
157                 p_longopts[i_index].has_arg = false;
158                 p_longopts[i_index].flag = &flag;
159                 p_longopts[i_index].val = 1;
160                 i_index++;
161
162                 psz_name = malloc( strlen(p_item->psz_name) + 4 );
163                 if( psz_name == NULL ) continue;
164                 strcpy( psz_name, "no-" );
165                 strcat( psz_name, p_item->psz_name );
166
167                 p_longopts[i_index].name = psz_name;
168                 p_longopts[i_index].has_arg = false;
169                 p_longopts[i_index].flag = &flag;
170                 p_longopts[i_index].val = 1;
171                 i_index++;
172             }
173
174             /* If item also has a short option, add it */
175             if( p_item->i_short )
176             {
177                 pp_shortopts[(int)p_item->i_short] = p_item;
178                 psz_shortopts[i_shortopts] = p_item->i_short;
179                 i_shortopts++;
180                 if( p_item->i_type != CONFIG_ITEM_BOOL
181                  && p_item->i_short != 'v' )
182                 {
183                     psz_shortopts[i_shortopts] = ':';
184                     i_shortopts++;
185                 }
186             }
187         }
188     }
189
190     /* We don't need the module list anymore */
191     module_list_free( list );
192
193     /* Close the longopts and shortopts structures */
194     memset( &p_longopts[i_index], 0, sizeof(*p_longopts) );
195     psz_shortopts[i_shortopts] = '\0';
196
197     int ret = -1;
198
199     /*
200      * Parse the command line options
201      */
202     vlc_getopt_t state;
203     state.optind = 0 ; /* set to 0 to tell GNU getopt to reinitialize */
204     while( ( i_cmd = vlc_getopt_long( i_argc, (char **)ppsz_argv,
205                                       psz_shortopts,
206                                       p_longopts, &i_index, &state ) ) != -1 )
207     {
208         /* A long option has been recognized */
209         if( i_cmd == 0 )
210         {
211             module_config_t *p_conf;
212             const char *psz_name = p_longopts[i_index].name;
213
214             /* Check if we deal with a --nofoo or --no-foo long option */
215             if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
216
217             /* Store the configuration option */
218             p_conf = config_FindConfig( p_this, psz_name );
219             if( p_conf )
220             {
221                 /* Check if the option is deprecated */
222                 if( p_conf->b_removed )
223                 {
224                     fprintf(stderr,
225                             "Warning: option --%s no longer exists.\n",
226                             psz_name);
227                     continue;
228                 }
229
230                 if( p_conf->psz_oldname
231                  && !strcmp( p_conf->psz_oldname, psz_name) )
232                 {
233                     if( !b_ignore_errors )
234                     {
235                         fprintf( stderr, "Error: option --%s is deprecated. "
236                                  "Use --%s instead.\n",
237                                  psz_name, p_conf->psz_name );
238                         goto out;
239                     }
240
241                     psz_name = p_conf->psz_name;
242                 }
243
244                 switch( p_conf->i_type )
245                 {
246                     case CONFIG_ITEM_STRING:
247                     case CONFIG_ITEM_PASSWORD:
248                     case CONFIG_ITEM_FILE:
249                     case CONFIG_ITEM_DIRECTORY:
250                     case CONFIG_ITEM_MODULE:
251                     case CONFIG_ITEM_MODULE_LIST:
252                     case CONFIG_ITEM_MODULE_LIST_CAT:
253                     case CONFIG_ITEM_MODULE_CAT:
254                         var_Create( p_this, psz_name, VLC_VAR_STRING );
255                         var_SetString( p_this, psz_name, state.optarg );
256                         break;
257                     case CONFIG_ITEM_INTEGER:
258                         var_Create( p_this, psz_name, VLC_VAR_INTEGER );
259                         var_SetInteger( p_this, psz_name,
260                                         strtoll(state.optarg, NULL, 0));
261                         break;
262                     case CONFIG_ITEM_FLOAT:
263                         var_Create( p_this, psz_name, VLC_VAR_FLOAT );
264                         var_SetFloat( p_this, psz_name, us_atof(state.optarg) );
265                         break;
266                     case CONFIG_ITEM_KEY:
267                         var_Create( p_this, psz_name, VLC_VAR_INTEGER );
268                         var_SetInteger( p_this, psz_name,
269                                         ConfigStringToKey( state.optarg ) );
270                         break;
271                     case CONFIG_ITEM_BOOL:
272                         var_Create( p_this, psz_name, VLC_VAR_BOOL );
273                         var_SetBool( p_this, psz_name, !flag );
274                         break;
275                 }
276                 continue;
277             }
278         }
279
280         /* A short option has been recognized */
281         if( pp_shortopts[i_cmd] != NULL )
282         {
283             const char *name = pp_shortopts[i_cmd]->psz_name;
284             switch( pp_shortopts[i_cmd]->i_type )
285             {
286                 case CONFIG_ITEM_STRING:
287                 case CONFIG_ITEM_PASSWORD:
288                 case CONFIG_ITEM_FILE:
289                 case CONFIG_ITEM_DIRECTORY:
290                 case CONFIG_ITEM_MODULE:
291                 case CONFIG_ITEM_MODULE_CAT:
292                 case CONFIG_ITEM_MODULE_LIST:
293                 case CONFIG_ITEM_MODULE_LIST_CAT:
294                     var_Create( p_this, name, VLC_VAR_STRING );
295                     var_SetString( p_this, name, state.optarg );
296                     break;
297                 case CONFIG_ITEM_INTEGER:
298                     var_Create( p_this, name, VLC_VAR_INTEGER );
299                     if( i_cmd == 'v' )
300                     {
301                         i_verbose++; /* -v */
302                         var_SetInteger( p_this, name, i_verbose );
303                     }
304                     else
305                     {
306                         var_SetInteger( p_this, name,
307                                         strtoll(state.optarg, NULL, 0) );
308                     }
309                     break;
310                 case CONFIG_ITEM_BOOL:
311                     var_Create( p_this, name, VLC_VAR_BOOL );
312                     var_SetBool( p_this, name, true );
313                     break;
314             }
315
316             continue;
317         }
318
319         /* Internal error: unknown option */
320         if( !b_ignore_errors )
321         {
322             fputs( "vlc: unknown option"
323                      " or missing mandatory argument ", stderr );
324             if( state.optopt )
325             {
326                 fprintf( stderr, "`-%c'\n", state.optopt );
327             }
328             else
329             {
330                 fprintf( stderr, "`%s'\n", ppsz_argv[state.optind-1] );
331             }
332             fputs( "Try `vlc --help' for more information.\n", stderr );
333             goto out;
334         }
335     }
336
337     ret = 0;
338     if( pindex != NULL )
339         *pindex = state.optind;
340 out:
341     /* Free allocated resources */
342     for( i_index = 0; p_longopts[i_index].name; i_index++ )
343         free( (char *)p_longopts[i_index].name );
344     free( p_longopts );
345     free( psz_shortopts );
346     free( argv_copy );
347     return ret;
348 }
349