]> git.sesse.net Git - vlc/blob - src/config/cmdline.c
9eacd3d46b4254ac17c57d149c1aefc0a1113289
[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_common.h>
29 #include "../libvlc.h"
30 #include <vlc_keys.h>
31 #include <vlc_charset.h>
32 #include <vlc_modules.h>
33
34 #include "vlc_getopt.h"
35
36 #include "configuration.h"
37 #include "modules/modules.h"
38
39 #include <assert.h>
40
41 #undef config_LoadCmdLine
42 /**
43  * Parse command line for configuration options.
44  *
45  * Now that the module_bank has been initialized, we can dynamically
46  * generate the longopts structure used by getops. We have to do it this way
47  * because we don't know (and don't want to know) in advance the configuration
48  * options used (ie. exported) by each module.
49  *
50  * @param p_this object to write command line options as variables to
51  * @param i_argc number of command line arguments
52  * @param ppsz_args commandl ine arguments [IN/OUT]
53  * @param pindex NULL to ignore unknown options,
54  *               otherwise index of the first non-option argument [OUT]
55  * @return 0 on success, -1 on error.
56  */
57 int config_LoadCmdLine( vlc_object_t *p_this, int i_argc,
58                         const char *ppsz_argv[], int *pindex )
59 {
60     int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
61     module_t *p_parser;
62     struct vlc_option *p_longopts;
63     const char **argv_copy = NULL;
64 #define b_ignore_errors (pindex == NULL)
65
66     /* Short options */
67     module_config_t *pp_shortopts[256];
68     char *psz_shortopts;
69
70     /* List all modules */
71     module_t **list = module_list_get (NULL);
72
73     /*
74      * Generate the longopts and shortopts structures used by getopt_long
75      */
76
77     i_opts = 0;
78     for (size_t i = 0; (p_parser = list[i]) != NULL; i++)
79         /* count the number of exported configuration options (to allocate
80          * longopts). We also need to allocate space for two options when
81          * dealing with boolean to allow for --foo and --no-foo */
82         i_opts += p_parser->i_config_items + 2 * p_parser->i_bool_items;
83
84     p_longopts = malloc( sizeof(*p_longopts) * (i_opts + 1) );
85     if( p_longopts == NULL )
86     {
87         module_list_free (list);
88         return -1;
89     }
90
91     psz_shortopts = malloc( 2 * i_opts + 1 );
92     if( psz_shortopts == NULL )
93     {
94         free( p_longopts );
95         module_list_free (list);
96         return -1;
97     }
98
99     /* If we are requested to ignore errors, then we must work on a copy
100      * of the ppsz_argv array, otherwise getopt_long will reorder it for
101      * us, ignoring the arity of the options */
102     if( b_ignore_errors )
103     {
104         argv_copy = (const char**)malloc( i_argc * sizeof(char *) );
105         if( argv_copy == NULL )
106         {
107             free( psz_shortopts );
108             free( p_longopts );
109             module_list_free (list);
110             return -1;
111         }
112         memcpy( argv_copy, ppsz_argv, i_argc * sizeof(char *) );
113         ppsz_argv = argv_copy;
114     }
115
116     i_shortopts = 0;
117     for( i_index = 0; i_index < 256; i_index++ )
118     {
119         pp_shortopts[i_index] = NULL;
120     }
121
122     /* Fill the p_longopts and psz_shortopts structures */
123     i_index = 0;
124     for (size_t i = 0; (p_parser = list[i]) != NULL; i++)
125     {
126         module_config_t *p_item, *p_end;
127
128         if( !p_parser->i_config_items )
129             continue;
130
131         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
132              p_item < p_end;
133              p_item++ )
134         {
135             /* Ignore hints */
136             if( !CONFIG_ITEM(p_item->i_type) )
137                 continue;
138
139             /* Add item to long options */
140             p_longopts[i_index].name = strdup( p_item->psz_name );
141             if( p_longopts[i_index].name == NULL ) continue;
142             p_longopts[i_index].flag = &flag;
143             p_longopts[i_index].val = 0;
144
145             if( CONFIG_CLASS(p_item->i_type) != CONFIG_ITEM_BOOL )
146                 p_longopts[i_index].has_arg = true;
147             else
148             /* Booleans also need --no-foo and --nofoo options */
149             {
150                 char *psz_name;
151
152                 p_longopts[i_index].has_arg = false;
153                 i_index++;
154
155                 if( asprintf( &psz_name, "no%s", p_item->psz_name ) == -1 )
156                     continue;
157                 p_longopts[i_index].name = psz_name;
158                 p_longopts[i_index].has_arg = false;
159                 p_longopts[i_index].flag = &flag;
160                 p_longopts[i_index].val = 1;
161                 i_index++;
162
163                 if( asprintf( &psz_name, "no-%s", p_item->psz_name ) == -1 )
164                     continue;
165                 p_longopts[i_index].name = psz_name;
166                 p_longopts[i_index].has_arg = false;
167                 p_longopts[i_index].flag = &flag;
168                 p_longopts[i_index].val = 1;
169             }
170             i_index++;
171
172             /* If item also has a short option, add it */
173             if( p_item->i_short )
174             {
175                 pp_shortopts[(int)p_item->i_short] = p_item;
176                 psz_shortopts[i_shortopts] = p_item->i_short;
177                 i_shortopts++;
178                 if( p_item->i_type != CONFIG_ITEM_BOOL
179                  && p_item->i_short != 'v' )
180                 {
181                     psz_shortopts[i_shortopts] = ':';
182                     i_shortopts++;
183                 }
184             }
185         }
186     }
187
188     /* We don't need the module list anymore */
189     module_list_free( list );
190
191     /* Close the longopts and shortopts structures */
192     memset( &p_longopts[i_index], 0, sizeof(*p_longopts) );
193     psz_shortopts[i_shortopts] = '\0';
194
195     int ret = -1;
196
197     /*
198      * Parse the command line options
199      */
200     vlc_getopt_t state;
201     state.ind = 0 ; /* set to 0 to tell GNU getopt to reinitialize */
202     while( ( i_cmd = vlc_getopt_long( i_argc, (char **)ppsz_argv,
203                                       psz_shortopts,
204                                       p_longopts, &i_index, &state ) ) != -1 )
205     {
206         /* A long option has been recognized */
207         if( i_cmd == 0 )
208         {
209             module_config_t *p_conf;
210             const char *psz_name = p_longopts[i_index].name;
211
212             /* Check if we deal with a --nofoo or --no-foo long option */
213             if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
214
215             /* Store the configuration option */
216             p_conf = config_FindConfig( p_this, psz_name );
217             if( p_conf )
218             {
219                 /* Check if the option is deprecated */
220                 if( p_conf->b_removed )
221                 {
222                     fprintf(stderr,
223                             "Warning: option --%s no longer exists.\n",
224                             psz_name);
225                     continue;
226                 }
227
228                 switch( CONFIG_CLASS(p_conf->i_type) )
229                 {
230                     case CONFIG_ITEM_STRING:
231                         var_Create( p_this, psz_name, VLC_VAR_STRING );
232                         var_SetString( p_this, psz_name, state.arg );
233                         break;
234                     case CONFIG_ITEM_INTEGER:
235                         var_Create( p_this, psz_name, VLC_VAR_INTEGER );
236                         var_SetInteger( p_this, psz_name,
237                                         strtoll(state.arg, NULL, 0));
238                         break;
239                     case CONFIG_ITEM_FLOAT:
240                         var_Create( p_this, psz_name, VLC_VAR_FLOAT );
241                         var_SetFloat( p_this, psz_name, us_atof(state.arg) );
242                         break;
243                     case CONFIG_ITEM_BOOL:
244                         var_Create( p_this, psz_name, VLC_VAR_BOOL );
245                         var_SetBool( p_this, psz_name, !flag );
246                         break;
247                 }
248                 continue;
249             }
250         }
251
252         /* A short option has been recognized */
253         if( pp_shortopts[i_cmd] != NULL )
254         {
255             const char *name = pp_shortopts[i_cmd]->psz_name;
256             switch( CONFIG_CLASS(pp_shortopts[i_cmd]->i_type) )
257             {
258                 case CONFIG_ITEM_STRING:
259                     var_Create( p_this, name, VLC_VAR_STRING );
260                     var_SetString( p_this, name, state.arg );
261                     break;
262                 case CONFIG_ITEM_INTEGER:
263                     var_Create( p_this, name, VLC_VAR_INTEGER );
264                     if( i_cmd == 'v' )
265                     {
266                         i_verbose++; /* -v */
267                         var_SetInteger( p_this, name, i_verbose );
268                     }
269                     else
270                     {
271                         var_SetInteger( p_this, name,
272                                         strtoll(state.arg, NULL, 0) );
273                     }
274                     break;
275                 case CONFIG_ITEM_BOOL:
276                     var_Create( p_this, name, VLC_VAR_BOOL );
277                     var_SetBool( p_this, name, true );
278                     break;
279             }
280
281             continue;
282         }
283
284         /* Internal error: unknown option */
285         if( !b_ignore_errors )
286         {
287             fputs( "vlc: unknown option"
288                      " or missing mandatory argument ", stderr );
289             if( state.opt )
290             {
291                 fprintf( stderr, "`-%c'\n", state.opt );
292             }
293             else
294             {
295                 fprintf( stderr, "`%s'\n", ppsz_argv[state.ind-1] );
296             }
297             fputs( "Try `vlc --help' for more information.\n", stderr );
298             goto out;
299         }
300     }
301
302     ret = 0;
303     if( pindex != NULL )
304         *pindex = state.ind;
305 out:
306     /* Free allocated resources */
307     for( i_index = 0; p_longopts[i_index].name; i_index++ )
308         free( (char *)p_longopts[i_index].name );
309     free( p_longopts );
310     free( psz_shortopts );
311     free( argv_copy );
312     return ret;
313 }
314