]> git.sesse.net Git - vlc/blob - src/config/cmdline.c
cfca4f2475fcdc27196e5639b934bc7a41a8ae8c
[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 pi_argc number of command line arguments [IN/OUT]
51  * @param ppsz_args commandl ine arguments [IN/OUT]
52  * @param b_ignore_errors whether to ignore parsing errors
53  * @return 0 on success, -1 on error.
54  *
55  * @warning This function is not re-entrant (because of getopt_long()).
56  * It must be called with the module bank initialization global lock held.
57  * FIXME: this still breaks if getopt() is used outside of LibVLC.
58  */
59 int config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc,
60                         const char *ppsz_argv[], bool b_ignore_errors )
61 {
62     int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
63     module_t *p_parser;
64     struct option *p_longopts;
65     const char **argv_copy = NULL;
66
67     /* Short options */
68     module_config_t *pp_shortopts[256];
69     char *psz_shortopts;
70
71     /* List all modules */
72     module_t **list = module_list_get (NULL);
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; (p_parser = list[i]) != NULL; i++)
80         /* count the number of exported configuration options (to allocate
81          * longopts). We also need to allocate space for two options when
82          * dealing with boolean to allow for --foo and --no-foo */
83         i_opts += p_parser->i_config_items + 2 * p_parser->i_bool_items;
84
85     p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
86     if( p_longopts == NULL )
87     {
88         module_list_free (list);
89         return -1;
90     }
91
92     psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
93     if( psz_shortopts == NULL )
94     {
95         free( p_longopts );
96         module_list_free (list);
97         return -1;
98     }
99
100     /* If we are requested to ignore errors, then we must work on a copy
101      * of the ppsz_argv array, otherwise getopt_long will reorder it for
102      * us, ignoring the arity of the options */
103     if( b_ignore_errors )
104     {
105         argv_copy = (const char**)malloc( *pi_argc * sizeof(char *) );
106         if( argv_copy == NULL )
107         {
108             free( psz_shortopts );
109             free( p_longopts );
110             module_list_free (list);
111             return -1;
112         }
113         memcpy( argv_copy, ppsz_argv, *pi_argc * sizeof(char *) );
114         ppsz_argv = argv_copy;
115     }
116
117     i_shortopts = 0;
118     for( i_index = 0; i_index < 256; i_index++ )
119     {
120         pp_shortopts[i_index] = NULL;
121     }
122
123     /* Fill the p_longopts and psz_shortopts structures */
124     i_index = 0;
125     for (size_t i = 0; (p_parser = list[i]) != NULL; i++)
126     {
127         module_config_t *p_item, *p_end;
128
129         if( !p_parser->i_config_items )
130             continue;
131
132         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
133              p_item < p_end;
134              p_item++ )
135         {
136             /* Ignore hints */
137             if( p_item->i_type & CONFIG_HINT )
138                 continue;
139
140             /* Add item to long options */
141             p_longopts[i_index].name = strdup( p_item->psz_name );
142             if( p_longopts[i_index].name == NULL ) continue;
143             p_longopts[i_index].has_arg =
144                 (p_item->i_type == CONFIG_ITEM_BOOL) ? no_argument : 
145 #ifndef __APPLE__
146                                                        required_argument;
147 #else
148 /* It seems that required_argument is broken on Darwin.
149  * Radar ticket #6113829 */
150                                                        optional_argument;
151 #endif
152             p_longopts[i_index].flag = &flag;
153             p_longopts[i_index].val = 0;
154             i_index++;
155
156             /* When dealing with bools we also need to add the --no-foo
157              * option */
158             if( p_item->i_type == CONFIG_ITEM_BOOL )
159             {
160                 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
161                 if( psz_name == NULL ) continue;
162                 strcpy( psz_name, "no" );
163                 strcat( psz_name, p_item->psz_name );
164
165                 p_longopts[i_index].name = psz_name;
166                 p_longopts[i_index].has_arg = no_argument;
167                 p_longopts[i_index].flag = &flag;
168                 p_longopts[i_index].val = 1;
169                 i_index++;
170
171                 psz_name = malloc( strlen(p_item->psz_name) + 4 );
172                 if( psz_name == NULL ) continue;
173                 strcpy( psz_name, "no-" );
174                 strcat( psz_name, p_item->psz_name );
175
176                 p_longopts[i_index].name = psz_name;
177                 p_longopts[i_index].has_arg = no_argument;
178                 p_longopts[i_index].flag = &flag;
179                 p_longopts[i_index].val = 1;
180                 i_index++;
181             }
182
183             /* If item also has a short option, add it */
184             if( p_item->i_short )
185             {
186                 pp_shortopts[(int)p_item->i_short] = p_item;
187                 psz_shortopts[i_shortopts] = p_item->i_short;
188                 i_shortopts++;
189                 if( p_item->i_type != CONFIG_ITEM_BOOL )
190                 {
191                     psz_shortopts[i_shortopts] = ':';
192                     i_shortopts++;
193
194                     if( p_item->i_short == 'v' )
195                     {
196                         psz_shortopts[i_shortopts] = ':';
197                         i_shortopts++;
198                     }
199                 }
200             }
201         }
202     }
203
204     /* We don't need the module list anymore */
205     module_list_free( list );
206
207     /* Close the longopts and shortopts structures */
208     memset( &p_longopts[i_index], 0, sizeof(struct option) );
209     psz_shortopts[i_shortopts] = '\0';
210
211     /*
212      * Parse the command line options
213      */
214     optind = 0; /* set to 0 to tell GNU getopt to reinitialize */
215     while( ( i_cmd = vlc_getopt_long( *pi_argc, (char **)ppsz_argv, psz_shortopts,
216                                   p_longopts, &i_index ) ) != -1 )
217     {
218         /* A long option has been recognized */
219         if( i_cmd == 0 )
220         {
221             module_config_t *p_conf;
222             const char *psz_name = p_longopts[i_index].name;
223
224             /* Check if we deal with a --nofoo or --no-foo long option */
225             if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
226
227             /* Store the configuration option */
228             p_conf = config_FindConfig( p_this, psz_name );
229             if( p_conf )
230             {
231                 /* Check if the option is deprecated */
232                 if( p_conf->b_removed )
233                 {
234                     fprintf(stderr,
235                             "Warning: option --%s no longer exists.\n",
236                             psz_name);
237                     continue;
238                 }
239
240                 if( p_conf->psz_oldname
241                  && !strcmp( p_conf->psz_oldname, psz_name) )
242                 {
243                     fprintf( stderr,
244                              "%s: option --%s is deprecated. Use --%s instead.\n",
245                              b_ignore_errors ? "Warning" : "Error",
246                              psz_name, p_conf->psz_name );
247                     if( !b_ignore_errors )
248                     {
249                         /*free */
250                         for( i_index = 0; p_longopts[i_index].name; i_index++ )
251                              free( (char *)p_longopts[i_index].name );
252
253                         free( p_longopts );
254                         free( psz_shortopts );
255                         return -1;
256                     }
257
258                     psz_name = p_conf->psz_name;
259                 }
260 #ifdef __APPLE__
261                 if( p_conf->i_type != CONFIG_ITEM_BOOL && !optarg )
262                 {
263                     fprintf( stderr, "Warning: missing argument for option --%s\n", p_conf->psz_name );
264                     fprintf( stderr, "Try specifying options as '--optionname=value' instead of '--optionname value'\n" );
265                     continue;
266                 }
267 #endif
268                 switch( p_conf->i_type )
269                 {
270                     case CONFIG_ITEM_STRING:
271                     case CONFIG_ITEM_PASSWORD:
272                     case CONFIG_ITEM_FILE:
273                     case CONFIG_ITEM_DIRECTORY:
274                     case CONFIG_ITEM_MODULE:
275                     case CONFIG_ITEM_MODULE_LIST:
276                     case CONFIG_ITEM_MODULE_LIST_CAT:
277                     case CONFIG_ITEM_MODULE_CAT:
278                         var_Create( p_this, psz_name, VLC_VAR_STRING );
279                         var_SetString( p_this, psz_name, optarg );
280                         break;
281                     case CONFIG_ITEM_INTEGER:
282                         var_Create( p_this, psz_name, VLC_VAR_INTEGER );
283                         var_SetInteger( p_this, psz_name,
284                                         strtol(optarg, NULL, 0));
285                         break;
286                     case CONFIG_ITEM_FLOAT:
287                         var_Create( p_this, psz_name, VLC_VAR_FLOAT );
288                         var_SetFloat( p_this, psz_name, us_atof(optarg) );
289                         break;
290                     case CONFIG_ITEM_KEY:
291                         var_Create( p_this, psz_name, VLC_VAR_INTEGER );
292                         var_SetInteger( p_this, psz_name,
293                                         ConfigStringToKey( optarg ) );
294                         break;
295                     case CONFIG_ITEM_BOOL:
296                         var_Create( p_this, psz_name, VLC_VAR_BOOL );
297                         var_SetBool( p_this, psz_name, !flag );
298                         break;
299                 }
300                 continue;
301             }
302         }
303
304         /* A short option has been recognized */
305         if( pp_shortopts[i_cmd] != NULL )
306         {
307             const char *name = pp_shortopts[i_cmd]->psz_name;
308             switch( pp_shortopts[i_cmd]->i_type )
309             {
310                 case CONFIG_ITEM_STRING:
311                 case CONFIG_ITEM_PASSWORD:
312                 case CONFIG_ITEM_FILE:
313                 case CONFIG_ITEM_DIRECTORY:
314                 case CONFIG_ITEM_MODULE:
315                 case CONFIG_ITEM_MODULE_CAT:
316                 case CONFIG_ITEM_MODULE_LIST:
317                 case CONFIG_ITEM_MODULE_LIST_CAT:
318                     var_Create( p_this, name, VLC_VAR_STRING );
319                     var_SetString( p_this, name, optarg );
320                     break;
321                 case CONFIG_ITEM_INTEGER:
322                     var_Create( p_this, name, VLC_VAR_INTEGER );
323                     if( i_cmd == 'v' )
324                     {
325                         if( optarg )
326                         {
327                             if( *optarg == 'v' ) /* eg. -vvv */
328                             {
329                                 i_verbose++;
330                                 while( *optarg == 'v' )
331                                 {
332                                     i_verbose++;
333                                     optarg++;
334                                 }
335                             }
336                             else
337                             {
338                                 i_verbose += atoi( optarg ); /* eg. -v2 */
339                             }
340                         }
341                         else
342                         {
343                             i_verbose++; /* -v */
344                         }
345                         var_SetInteger( p_this, name, i_verbose );
346                     }
347                     else
348                     {
349                         var_SetInteger( p_this, name,
350                                         strtol(optarg, NULL, 0) );
351                     }
352                     break;
353                 case CONFIG_ITEM_BOOL:
354                     var_Create( p_this, name, VLC_VAR_BOOL );
355                     var_SetBool( p_this, name, true );
356                     break;
357             }
358
359             continue;
360         }
361
362         /* Internal error: unknown option */
363         if( !b_ignore_errors )
364         {
365             fputs( "vlc: unknown option"
366                      " or missing mandatory argument ", stderr );
367             if( optopt )
368             {
369                 fprintf( stderr, "`-%c'\n", optopt );
370             }
371             else
372             {
373                 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
374             }
375             fputs( "Try `vlc --help' for more information.\n", stderr );
376
377             for( i_index = 0; p_longopts[i_index].name; i_index++ )
378                 free( (char *)p_longopts[i_index].name );
379             free( p_longopts );
380             free( psz_shortopts );
381             return -1;
382         }
383     }
384
385     /* Free allocated resources */
386     for( i_index = 0; p_longopts[i_index].name; i_index++ )
387         free( (char *)p_longopts[i_index].name );
388     free( p_longopts );
389     free( psz_shortopts );
390     free( argv_copy );
391
392     return 0;
393 }
394