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