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