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