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