2010-05-18 19:58:33 +04:00
|
|
|
/*
|
2016-09-02 00:32:49 +03:00
|
|
|
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
|
2010-05-18 19:58:33 +04:00
|
|
|
*
|
2016-09-02 00:32:49 +03:00
|
|
|
* This source code is subject to the terms of the BSD 2 Clause License and
|
|
|
|
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
|
|
|
|
* was not distributed with this source code in the LICENSE file, you can
|
|
|
|
* obtain it at www.aomedia.org/license/software. If the Alliance for Open
|
|
|
|
* Media Patent License 1.0 was not distributed with this source code in the
|
|
|
|
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
|
2010-05-18 19:58:33 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include "args.h"
|
|
|
|
|
2016-07-12 01:51:21 +03:00
|
|
|
#include "aom/aom_integer.h"
|
2016-08-23 02:08:15 +03:00
|
|
|
#include "aom_ports/msvc.h"
|
2010-05-18 19:58:33 +04:00
|
|
|
|
|
|
|
#if defined(__GNUC__) && __GNUC__
|
|
|
|
extern void die(const char *fmt, ...) __attribute__((noreturn));
|
|
|
|
#else
|
|
|
|
extern void die(const char *fmt, ...);
|
|
|
|
#endif
|
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
struct arg arg_init(char **argv) {
|
|
|
|
struct arg a;
|
|
|
|
|
2016-08-09 05:03:30 +03:00
|
|
|
a.argv = argv;
|
2012-07-14 02:21:29 +04:00
|
|
|
a.argv_step = 1;
|
2016-08-09 05:03:30 +03:00
|
|
|
a.name = NULL;
|
|
|
|
a.val = NULL;
|
|
|
|
a.def = NULL;
|
2012-07-14 02:21:29 +04:00
|
|
|
return a;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
int arg_match(struct arg *arg_, const struct arg_def *def, char **argv) {
|
|
|
|
struct arg arg;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2016-08-09 05:03:30 +03:00
|
|
|
if (!argv[0] || argv[0][0] != '-') return 0;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
arg = arg_init(argv);
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2016-08-09 05:03:30 +03:00
|
|
|
if (def->short_name && strlen(arg.argv[0]) == strlen(def->short_name) + 1 &&
|
|
|
|
!strcmp(arg.argv[0] + 1, def->short_name)) {
|
2012-07-14 02:21:29 +04:00
|
|
|
arg.name = arg.argv[0] + 1;
|
|
|
|
arg.val = def->has_val ? arg.argv[1] : NULL;
|
|
|
|
arg.argv_step = def->has_val ? 2 : 1;
|
|
|
|
} else if (def->long_name) {
|
2012-11-07 00:08:05 +04:00
|
|
|
const size_t name_len = strlen(def->long_name);
|
2012-07-14 02:21:29 +04:00
|
|
|
|
2016-08-09 05:03:30 +03:00
|
|
|
if (strlen(arg.argv[0]) >= name_len + 2 && arg.argv[0][1] == '-' &&
|
|
|
|
!strncmp(arg.argv[0] + 2, def->long_name, name_len) &&
|
|
|
|
(arg.argv[0][name_len + 2] == '=' ||
|
|
|
|
arg.argv[0][name_len + 2] == '\0')) {
|
2012-07-14 02:21:29 +04:00
|
|
|
arg.name = arg.argv[0] + 2;
|
|
|
|
arg.val = arg.name[name_len] == '=' ? arg.name + name_len + 1 : NULL;
|
|
|
|
arg.argv_step = 1;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
2012-07-14 02:21:29 +04:00
|
|
|
}
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
if (arg.name && !arg.val && def->has_val)
|
|
|
|
die("Error: option %s requires argument.\n", arg.name);
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
if (arg.name && arg.val && !def->has_val)
|
|
|
|
die("Error: option %s requires no argument.\n", arg.name);
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2016-08-09 05:03:30 +03:00
|
|
|
if (arg.name && (arg.val || !def->has_val)) {
|
2012-07-14 02:21:29 +04:00
|
|
|
arg.def = def;
|
|
|
|
*arg_ = arg;
|
|
|
|
return 1;
|
|
|
|
}
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
return 0;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
const char *arg_next(struct arg *arg) {
|
2016-08-09 05:03:30 +03:00
|
|
|
if (arg->argv[0]) arg->argv += arg->argv_step;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
return *arg->argv;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
char **argv_dup(int argc, const char **argv) {
|
|
|
|
char **new_argv = malloc((argc + 1) * sizeof(*argv));
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
memcpy(new_argv, argv, argc * sizeof(*argv));
|
|
|
|
new_argv[argc] = NULL;
|
|
|
|
return new_argv;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
void arg_show_usage(FILE *fp, const struct arg_def *const *defs) {
|
2016-08-09 05:03:30 +03:00
|
|
|
char option_text[40] = { 0 };
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
for (; *defs; defs++) {
|
|
|
|
const struct arg_def *def = *defs;
|
|
|
|
char *short_val = def->has_val ? " <arg>" : "";
|
|
|
|
char *long_val = def->has_val ? "=<arg>" : "";
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
if (def->short_name && def->long_name) {
|
|
|
|
char *comma = def->has_val ? "," : ", ";
|
2010-10-22 00:53:52 +04:00
|
|
|
|
2016-08-09 05:03:30 +03:00
|
|
|
snprintf(option_text, 37, "-%s%s%s --%s%6s", def->short_name, short_val,
|
|
|
|
comma, def->long_name, long_val);
|
2012-07-14 02:21:29 +04:00
|
|
|
} else if (def->short_name)
|
2016-08-09 05:03:30 +03:00
|
|
|
snprintf(option_text, 37, "-%s%s", def->short_name, short_val);
|
2012-07-14 02:21:29 +04:00
|
|
|
else if (def->long_name)
|
2016-08-09 05:03:30 +03:00
|
|
|
snprintf(option_text, 37, " --%s%s", def->long_name, long_val);
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
fprintf(fp, " %-37s\t%s\n", option_text, def->desc);
|
2010-12-17 17:43:39 +03:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
if (def->enums) {
|
|
|
|
const struct arg_enum_list *listptr;
|
2010-12-17 17:43:39 +03:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
fprintf(fp, " %-37s\t ", "");
|
2010-12-17 17:43:39 +03:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
for (listptr = def->enums; listptr->name; listptr++)
|
2016-08-09 05:03:30 +03:00
|
|
|
fprintf(fp, "%s%s", listptr->name, listptr[1].name ? ", " : "\n");
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
2012-07-14 02:21:29 +04:00
|
|
|
}
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
unsigned int arg_parse_uint(const struct arg *arg) {
|
2016-07-09 02:09:36 +03:00
|
|
|
uint32_t rawval;
|
2016-08-09 05:03:30 +03:00
|
|
|
char *endptr;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2016-07-09 02:09:36 +03:00
|
|
|
rawval = strtoul(arg->val, &endptr, 10);
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
if (arg->val[0] != '\0' && endptr[0] == '\0') {
|
2016-07-09 02:09:36 +03:00
|
|
|
if (rawval <= UINT_MAX) return rawval;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2016-08-09 05:03:30 +03:00
|
|
|
die("Option %s: Value %ld out of range for unsigned int\n", arg->name,
|
|
|
|
rawval);
|
2012-07-14 02:21:29 +04:00
|
|
|
}
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
|
|
|
|
return 0;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
int arg_parse_int(const struct arg *arg) {
|
2016-07-09 02:09:36 +03:00
|
|
|
int32_t rawval;
|
2016-08-09 05:03:30 +03:00
|
|
|
char *endptr;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
rawval = strtol(arg->val, &endptr, 10);
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
if (arg->val[0] != '\0' && endptr[0] == '\0') {
|
2016-08-09 05:03:30 +03:00
|
|
|
if (rawval >= INT_MIN && rawval <= INT_MAX) return rawval;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2016-08-09 05:03:30 +03:00
|
|
|
die("Option %s: Value %ld out of range for signed int\n", arg->name,
|
|
|
|
rawval);
|
2012-07-14 02:21:29 +04:00
|
|
|
}
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
|
|
|
|
return 0;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
struct aom_rational {
|
2012-07-14 02:21:29 +04:00
|
|
|
int num; /**< fraction numerator */
|
|
|
|
int den; /**< fraction denominator */
|
2010-05-18 19:58:33 +04:00
|
|
|
};
|
2016-08-31 00:01:10 +03:00
|
|
|
struct aom_rational arg_parse_rational(const struct arg *arg) {
|
2016-08-09 05:03:30 +03:00
|
|
|
long int rawval;
|
|
|
|
char *endptr;
|
2016-08-31 00:01:10 +03:00
|
|
|
struct aom_rational rat;
|
2012-07-14 02:21:29 +04:00
|
|
|
|
|
|
|
/* parse numerator */
|
|
|
|
rawval = strtol(arg->val, &endptr, 10);
|
|
|
|
|
|
|
|
if (arg->val[0] != '\0' && endptr[0] == '/') {
|
|
|
|
if (rawval >= INT_MIN && rawval <= INT_MAX)
|
|
|
|
rat.num = rawval;
|
2016-08-09 05:03:30 +03:00
|
|
|
else
|
|
|
|
die("Option %s: Value %ld out of range for signed int\n", arg->name,
|
|
|
|
rawval);
|
|
|
|
} else
|
|
|
|
die("Option %s: Expected / at '%c'\n", arg->name, *endptr);
|
2012-07-14 02:21:29 +04:00
|
|
|
|
|
|
|
/* parse denominator */
|
|
|
|
rawval = strtol(endptr + 1, &endptr, 10);
|
|
|
|
|
|
|
|
if (arg->val[0] != '\0' && endptr[0] == '\0') {
|
|
|
|
if (rawval >= INT_MIN && rawval <= INT_MAX)
|
|
|
|
rat.den = rawval;
|
2016-08-09 05:03:30 +03:00
|
|
|
else
|
|
|
|
die("Option %s: Value %ld out of range for signed int\n", arg->name,
|
|
|
|
rawval);
|
|
|
|
} else
|
|
|
|
die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
|
2012-07-14 02:21:29 +04:00
|
|
|
|
|
|
|
return rat;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
2010-12-17 17:43:39 +03:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
int arg_parse_enum(const struct arg *arg) {
|
|
|
|
const struct arg_enum_list *listptr;
|
2016-08-09 05:03:30 +03:00
|
|
|
long int rawval;
|
|
|
|
char *endptr;
|
2010-12-17 17:43:39 +03:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
/* First see if the value can be parsed as a raw value */
|
|
|
|
rawval = strtol(arg->val, &endptr, 10);
|
|
|
|
if (arg->val[0] != '\0' && endptr[0] == '\0') {
|
|
|
|
/* Got a raw value, make sure it's valid */
|
|
|
|
for (listptr = arg->def->enums; listptr->name; listptr++)
|
2016-08-09 05:03:30 +03:00
|
|
|
if (listptr->val == rawval) return rawval;
|
2012-07-14 02:21:29 +04:00
|
|
|
}
|
2010-12-17 17:43:39 +03:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
/* Next see if it can be parsed as a string */
|
|
|
|
for (listptr = arg->def->enums; listptr->name; listptr++)
|
2016-08-09 05:03:30 +03:00
|
|
|
if (!strcmp(arg->val, listptr->name)) return listptr->val;
|
2012-07-14 02:21:29 +04:00
|
|
|
|
|
|
|
die("Option %s: Invalid value '%s'\n", arg->name, arg->val);
|
|
|
|
return 0;
|
2010-12-17 17:43:39 +03:00
|
|
|
}
|
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
int arg_parse_enum_or_int(const struct arg *arg) {
|
2016-08-09 05:03:30 +03:00
|
|
|
if (arg->def->enums) return arg_parse_enum(arg);
|
2012-07-14 02:21:29 +04:00
|
|
|
return arg_parse_int(arg);
|
2010-12-17 17:43:39 +03:00
|
|
|
}
|