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