cmdline.c 10.7 KB
Newer Older
1 2 3
/*****************************************************************************
 * cmdline.c: command line parsing
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2001-2007 VLC authors and VideoLAN
5 6 7 8
 * $Id$
 *
 * Authors: Gildas Bazin <gbazin@videolan.org>
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12 13 14 15
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23
 *****************************************************************************/

24 25 26 27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

28
#include <vlc_common.h>
29
#include "../libvlc.h"
30
#include <vlc_actions.h>
31
#include <vlc_charset.h>
32
#include <vlc_modules.h>
33
#include <vlc_plugin.h>
34

35
#include "vlc_getopt.h"
36

37
#include "configuration.h"
38 39
#include "modules/modules.h"

40 41
#include <assert.h>

42
#undef config_LoadCmdLine
43
/**
44
 * Parse command line for configuration options.
45
 *
46 47 48 49
 * Now that the module_bank has been initialized, we can dynamically
 * generate the longopts structure used by getops. We have to do it this way
 * because we don't know (and don't want to know) in advance the configuration
 * options used (ie. exported) by each module.
50 51
 *
 * @param p_this object to write command line options as variables to
52
 * @param i_argc number of command line arguments
53
 * @param ppsz_args commandl ine arguments [IN/OUT]
54 55
 * @param pindex NULL to ignore unknown options,
 *               otherwise index of the first non-option argument [OUT]
56 57
 * @return 0 on success, -1 on error.
 */
58
int config_LoadCmdLine( vlc_object_t *p_this, int i_argc,
59
                        const char *ppsz_argv[], int *pindex )
60 61
{
    int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
62
    struct vlc_option *p_longopts;
63
    const char **argv_copy = NULL;
64
#define b_ignore_errors (pindex == NULL)
65 66

    /* Short options */
67
    const module_config_t *pp_shortopts[256];
68 69 70 71 72 73 74
    char *psz_shortopts;

    /*
     * Generate the longopts and shortopts structures used by getopt_long
     */

    i_opts = 0;
75
    for (const vlc_plugin_t *p = vlc_plugins; p != NULL; p = p->next)
76 77 78
        /* count the number of exported configuration options (to allocate
         * longopts). We also need to allocate space for two options when
         * dealing with boolean to allow for --foo and --no-foo */
79
        i_opts += p->conf.count + 2 * p->conf.booleans;
80

81
    p_longopts = vlc_alloc( i_opts + 1, sizeof(*p_longopts)  );
82 83 84
    if( p_longopts == NULL )
        return -1;

85
    psz_shortopts = malloc( 2 * i_opts + 1 );
86 87 88 89 90 91 92 93 94 95 96
    if( psz_shortopts == NULL )
    {
        free( p_longopts );
        return -1;
    }

    /* If we are requested to ignore errors, then we must work on a copy
     * of the ppsz_argv array, otherwise getopt_long will reorder it for
     * us, ignoring the arity of the options */
    if( b_ignore_errors )
    {
97
        argv_copy = vlc_alloc( i_argc, sizeof(char *) );
98
        if( argv_copy == NULL )
99 100 101 102 103
        {
            free( psz_shortopts );
            free( p_longopts );
            return -1;
        }
104
        memcpy( argv_copy, ppsz_argv, i_argc * sizeof(char *) );
105
        ppsz_argv = argv_copy;
106 107 108 109 110 111 112 113 114 115
    }

    i_shortopts = 0;
    for( i_index = 0; i_index < 256; i_index++ )
    {
        pp_shortopts[i_index] = NULL;
    }

    /* Fill the p_longopts and psz_shortopts structures */
    i_index = 0;
116
    for (const vlc_plugin_t *p = vlc_plugins; p != NULL; p = p->next)
117
    {
118 119
        for (const module_config_t *p_item = p->conf.items,
                                   *p_end = p_item + p->conf.size;
120
             p_item < p_end;
121
             p_item++)
122 123
        {
            /* Ignore hints */
124
            if( !CONFIG_ITEM(p_item->i_type) )
125 126 127 128 129 130 131 132
                continue;

            /* Add item to long options */
            p_longopts[i_index].name = strdup( p_item->psz_name );
            if( p_longopts[i_index].name == NULL ) continue;
            p_longopts[i_index].flag = &flag;
            p_longopts[i_index].val = 0;

133 134 135 136
            if( CONFIG_CLASS(p_item->i_type) != CONFIG_ITEM_BOOL )
                p_longopts[i_index].has_arg = true;
            else
            /* Booleans also need --no-foo and --nofoo options */
137
            {
138
                char *psz_name;
139

140 141 142 143 144
                p_longopts[i_index].has_arg = false;
                i_index++;

                if( asprintf( &psz_name, "no%s", p_item->psz_name ) == -1 )
                    continue;
145
                p_longopts[i_index].name = psz_name;
146
                p_longopts[i_index].has_arg = false;
147 148 149 150
                p_longopts[i_index].flag = &flag;
                p_longopts[i_index].val = 1;
                i_index++;

151 152
                if( asprintf( &psz_name, "no-%s", p_item->psz_name ) == -1 )
                    continue;
153
                p_longopts[i_index].name = psz_name;
154
                p_longopts[i_index].has_arg = false;
155 156 157
                p_longopts[i_index].flag = &flag;
                p_longopts[i_index].val = 1;
            }
158
            i_index++;
159 160 161 162 163 164 165

            /* If item also has a short option, add it */
            if( p_item->i_short )
            {
                pp_shortopts[(int)p_item->i_short] = p_item;
                psz_shortopts[i_shortopts] = p_item->i_short;
                i_shortopts++;
166 167
                if( p_item->i_type != CONFIG_ITEM_BOOL
                 && p_item->i_short != 'v' )
168 169 170 171 172 173 174 175 176
                {
                    psz_shortopts[i_shortopts] = ':';
                    i_shortopts++;
                }
            }
        }
    }

    /* Close the longopts and shortopts structures */
177
    memset( &p_longopts[i_index], 0, sizeof(*p_longopts) );
178 179
    psz_shortopts[i_shortopts] = '\0';

180 181
    int ret = -1;

182 183 184
    /*
     * Parse the command line options
     */
185
    vlc_getopt_t state;
186
    state.ind = 0 ; /* set to 0 to tell GNU getopt to reinitialize */
187
    while( ( i_cmd = vlc_getopt_long( i_argc, (char **)ppsz_argv,
188
                                      psz_shortopts,
189
                                      p_longopts, &i_index, &state ) ) != -1 )
190 191 192 193 194
    {
        /* A long option has been recognized */
        if( i_cmd == 0 )
        {
            module_config_t *p_conf;
195
            const char *psz_name = p_longopts[i_index].name;
196 197 198 199 200

            /* Check if we deal with a --nofoo or --no-foo long option */
            if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;

            /* Store the configuration option */
201
            p_conf = config_FindConfig( psz_name );
202 203 204
            if( p_conf )
            {
                /* Check if the option is deprecated */
205
                if( p_conf->b_removed )
206
                {
207 208 209 210 211
                    fprintf(stderr,
                            "Warning: option --%s no longer exists.\n",
                            psz_name);
                    continue;
                }
212

213
                switch( CONFIG_CLASS(p_conf->i_type) )
214 215
                {
                    case CONFIG_ITEM_STRING:
216
                        var_Create( p_this, psz_name, VLC_VAR_STRING );
217
                        var_SetString( p_this, psz_name, state.arg );
218 219
                        break;
                    case CONFIG_ITEM_INTEGER:
220
                        var_Create( p_this, psz_name, VLC_VAR_INTEGER );
221 222 223
                        var_Change( p_this, psz_name, VLC_VAR_SETMINMAX,
                            &(vlc_value_t){ .i_int = p_conf->min.i },
                            &(vlc_value_t){ .i_int = p_conf->max.i } );
224
                        var_SetInteger( p_this, psz_name,
225
                                        strtoll(state.arg, NULL, 0));
226 227
                        break;
                    case CONFIG_ITEM_FLOAT:
228
                        var_Create( p_this, psz_name, VLC_VAR_FLOAT );
229 230 231
                        var_Change( p_this, psz_name, VLC_VAR_SETMINMAX,
                            &(vlc_value_t){ .f_float = p_conf->min.f },
                            &(vlc_value_t){ .f_float = p_conf->max.f } );
232
                        var_SetFloat( p_this, psz_name, us_atof(state.arg) );
233 234
                        break;
                    case CONFIG_ITEM_BOOL:
235 236
                        var_Create( p_this, psz_name, VLC_VAR_BOOL );
                        var_SetBool( p_this, psz_name, !flag );
237 238 239 240 241 242 243 244 245
                        break;
                }
                continue;
            }
        }

        /* A short option has been recognized */
        if( pp_shortopts[i_cmd] != NULL )
        {
246
            const char *name = pp_shortopts[i_cmd]->psz_name;
247
            switch( CONFIG_CLASS(pp_shortopts[i_cmd]->i_type) )
248 249
            {
                case CONFIG_ITEM_STRING:
250
                    var_Create( p_this, name, VLC_VAR_STRING );
251
                    var_SetString( p_this, name, state.arg );
252 253
                    break;
                case CONFIG_ITEM_INTEGER:
254
                    var_Create( p_this, name, VLC_VAR_INTEGER );
255 256
                    if( i_cmd == 'v' )
                    {
257
                        i_verbose++; /* -v */
258
                        var_SetInteger( p_this, name, i_verbose );
259 260 261
                    }
                    else
                    {
262
                        var_SetInteger( p_this, name,
263
                                        strtoll(state.arg, NULL, 0) );
264 265 266
                    }
                    break;
                case CONFIG_ITEM_BOOL:
267 268
                    var_Create( p_this, name, VLC_VAR_BOOL );
                    var_SetBool( p_this, name, true );
269 270 271 272 273 274 275 276 277
                    break;
            }

            continue;
        }

        /* Internal error: unknown option */
        if( !b_ignore_errors )
        {
278 279
            fputs( "vlc: unknown option"
                     " or missing mandatory argument ", stderr );
280
            if( state.opt )
281
            {
282
                fprintf( stderr, "`-%c'\n", state.opt );
283 284 285
            }
            else
            {
286
                fprintf( stderr, "`%s'\n", ppsz_argv[state.ind-1] );
287
            }
288
            fputs( "Try `vlc --help' for more information.\n", stderr );
289
            goto out;
290 291 292
        }
    }

293
    ret = 0;
294
    if( pindex != NULL )
295
        *pindex = state.ind;
296
out:
297 298 299 300 301
    /* Free allocated resources */
    for( i_index = 0; p_longopts[i_index].name; i_index++ )
        free( (char *)p_longopts[i_index].name );
    free( p_longopts );
    free( psz_shortopts );
302
    free( argv_copy );
303
    return ret;
304 305
}