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