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