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