This is the first step to removing the globals plague from avtools.tags/n0.9
@@ -3403,7 +3403,7 @@ static int read_avserver_streams(AVFormatContext *s, const char *filename) | |||||
return 0; | return 0; | ||||
} | } | ||||
static void opt_output_file(const char *filename) | |||||
static void opt_output_file(void *optctx, const char *filename) | |||||
{ | { | ||||
AVFormatContext *oc; | AVFormatContext *oc; | ||||
int i, err; | int i, err; | ||||
@@ -4143,7 +4143,7 @@ int main(int argc, char **argv) | |||||
show_banner(); | show_banner(); | ||||
/* parse options */ | /* parse options */ | ||||
parse_options(argc, argv, options, opt_output_file); | |||||
parse_options(NULL, argc, argv, options, opt_output_file); | |||||
if(nb_output_files <= 0 && nb_input_files == 0) { | if(nb_output_files <= 0 && nb_input_files == 0) { | ||||
show_usage(); | show_usage(); | ||||
@@ -3015,7 +3015,7 @@ static void show_help(void) | |||||
); | ); | ||||
} | } | ||||
static void opt_input_file(const char *filename) | |||||
static void opt_input_file(void *optctx, const char *filename) | |||||
{ | { | ||||
if (input_filename) { | if (input_filename) { | ||||
fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n", | fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n", | ||||
@@ -3048,7 +3048,7 @@ int main(int argc, char **argv) | |||||
show_banner(); | show_banner(); | ||||
parse_options(argc, argv, options, opt_input_file); | |||||
parse_options(NULL, argc, argv, options, opt_input_file); | |||||
if (!input_filename) { | if (!input_filename) { | ||||
show_usage(); | show_usage(); | ||||
@@ -346,7 +346,7 @@ static int opt_format(const char *opt, const char *arg) | |||||
return 0; | return 0; | ||||
} | } | ||||
static void opt_input_file(const char *arg) | |||||
static void opt_input_file(void *optctx, const char *arg) | |||||
{ | { | ||||
if (input_filename) { | if (input_filename) { | ||||
fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n", | fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n", | ||||
@@ -406,7 +406,7 @@ int main(int argc, char **argv) | |||||
#endif | #endif | ||||
show_banner(); | show_banner(); | ||||
parse_options(argc, argv, options, opt_input_file); | |||||
parse_options(NULL, argc, argv, options, opt_input_file); | |||||
if (!input_filename) { | if (!input_filename) { | ||||
show_usage(); | show_usage(); | ||||
@@ -4676,7 +4676,7 @@ int main(int argc, char **argv) | |||||
my_program_dir = getcwd(0, 0); | my_program_dir = getcwd(0, 0); | ||||
avserver_daemon = 1; | avserver_daemon = 1; | ||||
parse_options(argc, argv, options, NULL); | |||||
parse_options(NULL, argc, argv, options, NULL); | |||||
unsetenv("http_proxy"); /* Kill the http_proxy */ | unsetenv("http_proxy"); /* Kill the http_proxy */ | ||||
@@ -203,8 +203,8 @@ static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr) | |||||
} | } | ||||
#endif /* WIN32 && !__MINGW32CE__ */ | #endif /* WIN32 && !__MINGW32CE__ */ | ||||
void parse_options(int argc, char **argv, const OptionDef *options, | |||||
void (* parse_arg_function)(const char*)) | |||||
void parse_options(void *optctx, int argc, char **argv, const OptionDef *options, | |||||
void (* parse_arg_function)(void *, const char*)) | |||||
{ | { | ||||
const char *opt, *arg; | const char *opt, *arg; | ||||
int optindex, handleoptions=1; | int optindex, handleoptions=1; | ||||
@@ -249,7 +249,9 @@ unknown_opt: | |||||
exit_program(1); | exit_program(1); | ||||
} | } | ||||
} | } | ||||
dst = po->u.dst_ptr; | |||||
/* new-style options contain an offset into optctx, old-style address of | |||||
* a global var*/ | |||||
dst = po->flags & OPT_OFFSET ? (uint8_t*)optctx + po->u.off : po->u.dst_ptr; | |||||
if (po->flags & OPT_STRING) { | if (po->flags & OPT_STRING) { | ||||
char *str; | char *str; | ||||
str = av_strdup(arg); | str = av_strdup(arg); | ||||
@@ -263,7 +265,9 @@ unknown_opt: | |||||
} else if (po->flags & OPT_FLOAT) { | } else if (po->flags & OPT_FLOAT) { | ||||
*(float*)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY); | *(float*)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY); | ||||
} else if (po->u.func_arg) { | } else if (po->u.func_arg) { | ||||
if (po->u.func_arg(opt, arg) < 0) { | |||||
int ret = po->flags & OPT_FUNC2 ? po->u.func2_arg(optctx, opt, arg) : | |||||
po->u.func_arg(opt, arg); | |||||
if (ret < 0) { | |||||
fprintf(stderr, "%s: failed to set value '%s' for option '%s'\n", argv[0], arg, opt); | fprintf(stderr, "%s: failed to set value '%s' for option '%s'\n", argv[0], arg, opt); | ||||
exit_program(1); | exit_program(1); | ||||
} | } | ||||
@@ -272,7 +276,7 @@ unknown_opt: | |||||
exit_program(0); | exit_program(0); | ||||
} else { | } else { | ||||
if (parse_arg_function) | if (parse_arg_function) | ||||
parse_arg_function(opt); | |||||
parse_arg_function(optctx, opt); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
@@ -124,9 +124,13 @@ typedef struct { | |||||
#define OPT_INT64 0x0400 | #define OPT_INT64 0x0400 | ||||
#define OPT_EXIT 0x0800 | #define OPT_EXIT 0x0800 | ||||
#define OPT_DATA 0x1000 | #define OPT_DATA 0x1000 | ||||
#define OPT_FUNC2 0x2000 | |||||
#define OPT_OFFSET 0x4000 /* option is specified as an offset in a passed optctx */ | |||||
union { | union { | ||||
void *dst_ptr; | void *dst_ptr; | ||||
int (*func_arg)(const char *, const char *); | int (*func_arg)(const char *, const char *); | ||||
int (*func2_arg)(void *, const char *, const char *); | |||||
size_t off; | |||||
} u; | } u; | ||||
const char *help; | const char *help; | ||||
const char *argname; | const char *argname; | ||||
@@ -136,14 +140,16 @@ void show_help_options(const OptionDef *options, const char *msg, int mask, int | |||||
/** | /** | ||||
* Parse the command line arguments. | * Parse the command line arguments. | ||||
* | |||||
* @param optctx an opaque options context | |||||
* @param options Array with the definitions required to interpret every | * @param options Array with the definitions required to interpret every | ||||
* option of the form: -option_name [argument] | * option of the form: -option_name [argument] | ||||
* @param parse_arg_function Name of the function called to process every | * @param parse_arg_function Name of the function called to process every | ||||
* argument without a leading option name flag. NULL if such arguments do | * argument without a leading option name flag. NULL if such arguments do | ||||
* not have to be processed. | * not have to be processed. | ||||
*/ | */ | ||||
void parse_options(int argc, char **argv, const OptionDef *options, | |||||
void (* parse_arg_function)(const char*)); | |||||
void parse_options(void *optctx, int argc, char **argv, const OptionDef *options, | |||||
void (* parse_arg_function)(void *optctx, const char*)); | |||||
/** | /** | ||||
* Check if the given stream matches a stream specifier. | * Check if the given stream matches a stream specifier. | ||||
@@ -3702,7 +3702,7 @@ static int opt_streamid(const char *opt, const char *arg) | |||||
return 0; | return 0; | ||||
} | } | ||||
static void opt_output_file(const char *filename) | |||||
static void opt_output_file(void *optctx, const char *filename) | |||||
{ | { | ||||
AVFormatContext *oc; | AVFormatContext *oc; | ||||
int err, use_video, use_audio, use_subtitle, use_data; | int err, use_video, use_audio, use_subtitle, use_data; | ||||
@@ -4376,7 +4376,7 @@ int main(int argc, char **argv) | |||||
"(see Changelog for the list of incompatible changes).\n"); | "(see Changelog for the list of incompatible changes).\n"); | ||||
/* parse options */ | /* parse options */ | ||||
parse_options(argc, argv, options, opt_output_file); | |||||
parse_options(NULL, argc, argv, options, opt_output_file); | |||||
if(nb_output_files <= 0 && nb_input_files == 0) { | if(nb_output_files <= 0 && nb_input_files == 0) { | ||||
show_usage(); | show_usage(); | ||||