| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 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 |
|
|---|
| 32 |
#ifdef HAVE_GETOPT_LONG |
|---|
| 33 |
# ifdef HAVE_GETOPT_H |
|---|
| 34 |
# include <getopt.h> |
|---|
| 35 |
# endif |
|---|
| 36 |
#else |
|---|
| 37 |
# include "../extras/getopt.h" |
|---|
| 38 |
#endif |
|---|
| 39 |
|
|---|
| 40 |
#include "configuration.h" |
|---|
| 41 |
#include "modules/modules.h" |
|---|
| 42 |
|
|---|
| 43 |
#include <assert.h> |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, |
|---|
| 55 |
const char *ppsz_argv[], |
|---|
| 56 |
bool b_ignore_errors ) |
|---|
| 57 |
{ |
|---|
| 58 |
int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0; |
|---|
| 59 |
module_t *p_parser; |
|---|
| 60 |
vlc_list_t *p_list; |
|---|
| 61 |
struct option *p_longopts; |
|---|
| 62 |
int i_modules_index; |
|---|
| 63 |
const char **argv_copy = NULL; |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
module_config_t *pp_shortopts[256]; |
|---|
| 67 |
char *psz_shortopts; |
|---|
| 68 |
|
|---|
| 69 |
#ifdef __APPLE__ |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) ) |
|---|
| 74 |
|
|---|
| 75 |
{ |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
assert( *pi_argc == 2 ); |
|---|
| 82 |
*pi_argc = 1; |
|---|
| 83 |
return 0; |
|---|
| 84 |
} |
|---|
| 85 |
#endif |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE ); |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 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 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 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 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 161 |
if( p_item->i_type & CONFIG_HINT ) |
|---|
| 162 |
continue; |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 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) ? no_argument : |
|---|
| 169 |
#ifndef __APPLE__ |
|---|
| 170 |
required_argument; |
|---|
| 171 |
#else |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
optional_argument; |
|---|
| 175 |
#endif |
|---|
| 176 |
p_longopts[i_index].flag = &flag; |
|---|
| 177 |
p_longopts[i_index].val = 0; |
|---|
| 178 |
i_index++; |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
if( p_item->i_type == CONFIG_ITEM_BOOL ) |
|---|
| 183 |
{ |
|---|
| 184 |
char *psz_name = malloc( strlen(p_item->psz_name) + 3 ); |
|---|
| 185 |
if( psz_name == NULL ) continue; |
|---|
| 186 |
strcpy( psz_name, "no" ); |
|---|
| 187 |
strcat( psz_name, p_item->psz_name ); |
|---|
| 188 |
|
|---|
| 189 |
p_longopts[i_index].name = psz_name; |
|---|
| 190 |
p_longopts[i_index].has_arg = no_argument; |
|---|
| 191 |
p_longopts[i_index].flag = &flag; |
|---|
| 192 |
p_longopts[i_index].val = 1; |
|---|
| 193 |
i_index++; |
|---|
| 194 |
|
|---|
| 195 |
psz_name = malloc( strlen(p_item->psz_name) + 4 ); |
|---|
| 196 |
if( psz_name == NULL ) continue; |
|---|
| 197 |
strcpy( psz_name, "no-" ); |
|---|
| 198 |
strcat( psz_name, p_item->psz_name ); |
|---|
| 199 |
|
|---|
| 200 |
p_longopts[i_index].name = psz_name; |
|---|
| 201 |
p_longopts[i_index].has_arg = no_argument; |
|---|
| 202 |
p_longopts[i_index].flag = &flag; |
|---|
| 203 |
p_longopts[i_index].val = 1; |
|---|
| 204 |
i_index++; |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
if( p_item->i_short ) |
|---|
| 209 |
{ |
|---|
| 210 |
pp_shortopts[(int)p_item->i_short] = p_item; |
|---|
| 211 |
psz_shortopts[i_shortopts] = p_item->i_short; |
|---|
| 212 |
i_shortopts++; |
|---|
| 213 |
if( p_item->i_type != CONFIG_ITEM_BOOL ) |
|---|
| 214 |
{ |
|---|
| 215 |
psz_shortopts[i_shortopts] = ':'; |
|---|
| 216 |
i_shortopts++; |
|---|
| 217 |
|
|---|
| 218 |
if( p_item->i_short == 'v' ) |
|---|
| 219 |
{ |
|---|
| 220 |
psz_shortopts[i_shortopts] = ':'; |
|---|
| 221 |
i_shortopts++; |
|---|
| 222 |
} |
|---|
| 223 |
} |
|---|
| 224 |
} |
|---|
| 225 |
} |
|---|
| 226 |
} |
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
vlc_list_release( p_list ); |
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
memset( &p_longopts[i_index], 0, sizeof(struct option) ); |
|---|
| 233 |
psz_shortopts[i_shortopts] = '\0'; |
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
opterr = 0; |
|---|
| 239 |
optind = 0; |
|---|
| 240 |
while( ( i_cmd = getopt_long( *pi_argc, (char **)ppsz_argv, psz_shortopts, |
|---|
| 241 |
p_longopts, &i_index ) ) != -1 ) |
|---|
| 242 |
{ |
|---|
| 243 |
|
|---|
| 244 |
if( i_cmd == 0 ) |
|---|
| 245 |
{ |
|---|
| 246 |
module_config_t *p_conf; |
|---|
| 247 |
char *psz_name = (char *)p_longopts[i_index].name; |
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2; |
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
p_conf = config_FindConfig( p_this, psz_name ); |
|---|
| 254 |
if( p_conf ) |
|---|
| 255 |
{ |
|---|
| 256 |
|
|---|
| 257 |
if( p_conf->b_removed ) |
|---|
| 258 |
{ |
|---|
| 259 |
fprintf(stderr, |
|---|
| 260 |
"Warning: option --%s no longer exists.\n", |
|---|
| 261 |
psz_name); |
|---|
| 262 |
continue; |
|---|
| 263 |
} |
|---|
| 264 |
|
|---|
| 265 |
if( p_conf->psz_oldname |
|---|
| 266 |
&& !strcmp( p_conf->psz_oldname, psz_name) ) |
|---|
| 267 |
{ |
|---|
| 268 |
fprintf( stderr, |
|---|
| 269 |
"%s: option --%s is deprecated. Use --%s instead.\n", |
|---|
| 270 |
b_ignore_errors ? "Warning" : "Error", |
|---|
| 271 |
psz_name, p_conf->psz_name ); |
|---|
| 272 |
if( !b_ignore_errors ) |
|---|
| 273 |
{ |
|---|
| 274 |
|
|---|
| 275 |
for( i_index = 0; p_longopts[i_index].name; i_index++ ) |
|---|
| 276 |
free( (char *)p_longopts[i_index].name ); |
|---|
| 277 |
|
|---|
| 278 |
free( p_longopts ); |
|---|
| 279 |
free( psz_shortopts ); |
|---|
| 280 |
return -1; |
|---|
| 281 |
} |
|---|
| 282 |
|
|---|
| 283 |
psz_name = p_conf->psz_name; |
|---|
| 284 |
} |
|---|
| 285 |
|
|---|
| 286 |
switch( p_conf->i_type ) |
|---|
| 287 |
{ |
|---|
| 288 |
case CONFIG_ITEM_STRING: |
|---|
| 289 |
case CONFIG_ITEM_PASSWORD: |
|---|
| 290 |
case CONFIG_ITEM_FILE: |
|---|
| 291 |
case CONFIG_ITEM_DIRECTORY: |
|---|
| 292 |
case CONFIG_ITEM_MODULE: |
|---|
| 293 |
case CONFIG_ITEM_MODULE_LIST: |
|---|
| 294 |
case CONFIG_ITEM_MODULE_LIST_CAT: |
|---|
| 295 |
case CONFIG_ITEM_MODULE_CAT: |
|---|
| 296 |
config_PutPsz( p_this, psz_name, optarg ); |
|---|
| 297 |
break; |
|---|
| 298 |
case CONFIG_ITEM_INTEGER: |
|---|
| 299 |
config_PutInt( p_this, psz_name, strtol(optarg, 0, 0)); |
|---|
| 300 |
break; |
|---|
| 301 |
case CONFIG_ITEM_FLOAT: |
|---|
| 302 |
config_PutFloat( p_this, psz_name, (float)atof(optarg) ); |
|---|
| 303 |
break; |
|---|
| 304 |
case CONFIG_ITEM_KEY: |
|---|
| 305 |
config_PutInt( p_this, psz_name, ConfigStringToKey( optarg ) ); |
|---|
| 306 |
break; |
|---|
| 307 |
case CONFIG_ITEM_BOOL: |
|---|
| 308 |
config_PutInt( p_this, psz_name, !flag ); |
|---|
| 309 |
break; |
|---|
| 310 |
} |
|---|
| 311 |
continue; |
|---|
| 312 |
} |
|---|
| 313 |
} |
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
if( pp_shortopts[i_cmd] != NULL ) |
|---|
| 317 |
{ |
|---|
| 318 |
switch( pp_shortopts[i_cmd]->i_type ) |
|---|
| 319 |
{ |
|---|
| 320 |
case CONFIG_ITEM_STRING: |
|---|
| 321 |
case CONFIG_ITEM_PASSWORD: |
|---|
| 322 |
case CONFIG_ITEM_FILE: |
|---|
| 323 |
case CONFIG_ITEM_DIRECTORY: |
|---|
| 324 |
case CONFIG_ITEM_MODULE: |
|---|
| 325 |
case CONFIG_ITEM_MODULE_CAT: |
|---|
| 326 |
case CONFIG_ITEM_MODULE_LIST: |
|---|
| 327 |
case CONFIG_ITEM_MODULE_LIST_CAT: |
|---|
| 328 |
config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg ); |
|---|
| 329 |
break; |
|---|
| 330 |
case CONFIG_ITEM_INTEGER: |
|---|
| 331 |
if( i_cmd == 'v' ) |
|---|
| 332 |
{ |
|---|
| 333 |
if( optarg ) |
|---|
| 334 |
{ |
|---|
| 335 |
if( *optarg == 'v' ) |
|---|
| 336 |
{ |
|---|
| 337 |
i_verbose++; |
|---|
| 338 |
while( *optarg == 'v' ) |
|---|
| 339 |
{ |
|---|
| 340 |
i_verbose++; |
|---|
| 341 |
optarg++; |
|---|
| 342 |
} |
|---|
| 343 |
} |
|---|
| 344 |
else |
|---|
| 345 |
{ |
|---|
| 346 |
i_verbose += atoi( optarg ); |
|---|
| 347 |
} |
|---|
| 348 |
} |
|---|
| 349 |
else |
|---|
| 350 |
{ |
|---|
| 351 |
i_verbose++; |
|---|
| 352 |
} |
|---|
| 353 |
config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, |
|---|
| 354 |
i_verbose ); |
|---|
| 355 |
} |
|---|
| 356 |
else |
|---|
| 357 |
{ |
|---|
| 358 |
config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, |
|---|
| 359 |
strtol(optarg, 0, 0) ); |
|---|
| 360 |
} |
|---|
| 361 |
break; |
|---|
| 362 |
case CONFIG_ITEM_BOOL: |
|---|
| 363 |
config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 ); |
|---|
| 364 |
break; |
|---|
| 365 |
} |
|---|
| 366 |
|
|---|
| 367 |
continue; |
|---|
| 368 |
} |
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
if( !b_ignore_errors ) |
|---|
| 372 |
{ |
|---|
| 373 |
fputs( "vlc: unknown option" |
|---|
| 374 |
" or missing mandatory argument ", stderr ); |
|---|
| 375 |
if( optopt ) |
|---|
| 376 |
{ |
|---|
| 377 |
fprintf( stderr, "`-%c'\n", optopt ); |
|---|
| 378 |
} |
|---|
| 379 |
else |
|---|
| 380 |
{ |
|---|
| 381 |
fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] ); |
|---|
| 382 |
} |
|---|
| 383 |
fputs( "Try `vlc --help' for more information.\n", stderr ); |
|---|
| 384 |
|
|---|
| 385 |
for( i_index = 0; p_longopts[i_index].name; i_index++ ) |
|---|
| 386 |
free( (char *)p_longopts[i_index].name ); |
|---|
| 387 |
free( p_longopts ); |
|---|
| 388 |
free( psz_shortopts ); |
|---|
| 389 |
return -1; |
|---|
| 390 |
} |
|---|
| 391 |
} |
|---|
| 392 |
|
|---|
| 393 |
|
|---|
| 394 |
for( i_index = 0; p_longopts[i_index].name; i_index++ ) |
|---|
| 395 |
free( (char *)p_longopts[i_index].name ); |
|---|
| 396 |
free( p_longopts ); |
|---|
| 397 |
free( psz_shortopts ); |
|---|
| 398 |
free( argv_copy ); |
|---|
| 399 |
|
|---|
| 400 |
return 0; |
|---|
| 401 |
} |
|---|
| 402 |
|
|---|