blob: 58298577fb99e6dcc6acc52a1d5a7371a222fcf9 [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
John Koleszar0ea50ce2010-05-18 11:58:33 -040010 */
11
John Koleszar0ea50ce2010-05-18 11:58:33 -040012#include <stdlib.h>
13#include <string.h>
14#include <limits.h>
15#include "args.h"
16
Urvang Joshif792a722016-07-11 15:51:21 -070017#include "aom/aom_integer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070018#include "aom_ports/msvc.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040019
20#if defined(__GNUC__) && __GNUC__
21extern void die(const char *fmt, ...) __attribute__((noreturn));
22#else
23extern void die(const char *fmt, ...);
24#endif
25
John Koleszarc6b90392012-07-13 15:21:29 -070026struct arg arg_init(char **argv) {
27 struct arg a;
John Koleszar0ea50ce2010-05-18 11:58:33 -040028
clang-format6c4d83e2016-08-08 19:03:30 -070029 a.argv = argv;
John Koleszarc6b90392012-07-13 15:21:29 -070030 a.argv_step = 1;
clang-format6c4d83e2016-08-08 19:03:30 -070031 a.name = NULL;
32 a.val = NULL;
33 a.def = NULL;
John Koleszarc6b90392012-07-13 15:21:29 -070034 return a;
John Koleszar0ea50ce2010-05-18 11:58:33 -040035}
36
John Koleszarc6b90392012-07-13 15:21:29 -070037int arg_match(struct arg *arg_, const struct arg_def *def, char **argv) {
38 struct arg arg;
John Koleszar0ea50ce2010-05-18 11:58:33 -040039
clang-format6c4d83e2016-08-08 19:03:30 -070040 if (!argv[0] || argv[0][0] != '-') return 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -040041
John Koleszarc6b90392012-07-13 15:21:29 -070042 arg = arg_init(argv);
John Koleszar0ea50ce2010-05-18 11:58:33 -040043
clang-format6c4d83e2016-08-08 19:03:30 -070044 if (def->short_name && strlen(arg.argv[0]) == strlen(def->short_name) + 1 &&
45 !strcmp(arg.argv[0] + 1, def->short_name)) {
John Koleszarc6b90392012-07-13 15:21:29 -070046 arg.name = arg.argv[0] + 1;
47 arg.val = def->has_val ? arg.argv[1] : NULL;
48 arg.argv_step = def->has_val ? 2 : 1;
49 } else if (def->long_name) {
John Koleszar82b1a342012-11-06 12:08:05 -080050 const size_t name_len = strlen(def->long_name);
John Koleszarc6b90392012-07-13 15:21:29 -070051
clang-format6c4d83e2016-08-08 19:03:30 -070052 if (strlen(arg.argv[0]) >= name_len + 2 && arg.argv[0][1] == '-' &&
53 !strncmp(arg.argv[0] + 2, def->long_name, name_len) &&
54 (arg.argv[0][name_len + 2] == '=' ||
55 arg.argv[0][name_len + 2] == '\0')) {
John Koleszarc6b90392012-07-13 15:21:29 -070056 arg.name = arg.argv[0] + 2;
57 arg.val = arg.name[name_len] == '=' ? arg.name + name_len + 1 : NULL;
58 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -040059 }
John Koleszarc6b90392012-07-13 15:21:29 -070060 }
61
62 if (arg.name && !arg.val && def->has_val)
63 die("Error: option %s requires argument.\n", arg.name);
64
65 if (arg.name && arg.val && !def->has_val)
66 die("Error: option %s requires no argument.\n", arg.name);
67
clang-format6c4d83e2016-08-08 19:03:30 -070068 if (arg.name && (arg.val || !def->has_val)) {
John Koleszarc6b90392012-07-13 15:21:29 -070069 arg.def = def;
70 *arg_ = arg;
71 return 1;
72 }
73
74 return 0;
75}
76
John Koleszarc6b90392012-07-13 15:21:29 -070077const char *arg_next(struct arg *arg) {
clang-format6c4d83e2016-08-08 19:03:30 -070078 if (arg->argv[0]) arg->argv += arg->argv_step;
John Koleszarc6b90392012-07-13 15:21:29 -070079
80 return *arg->argv;
81}
82
John Koleszarc6b90392012-07-13 15:21:29 -070083char **argv_dup(int argc, const char **argv) {
84 char **new_argv = malloc((argc + 1) * sizeof(*argv));
85
86 memcpy(new_argv, argv, argc * sizeof(*argv));
87 new_argv[argc] = NULL;
88 return new_argv;
89}
90
John Koleszarc6b90392012-07-13 15:21:29 -070091void arg_show_usage(FILE *fp, const struct arg_def *const *defs) {
clang-format6c4d83e2016-08-08 19:03:30 -070092 char option_text[40] = { 0 };
John Koleszarc6b90392012-07-13 15:21:29 -070093
94 for (; *defs; defs++) {
95 const struct arg_def *def = *defs;
96 char *short_val = def->has_val ? " <arg>" : "";
97 char *long_val = def->has_val ? "=<arg>" : "";
98
99 if (def->short_name && def->long_name) {
100 char *comma = def->has_val ? "," : ", ";
101
clang-format6c4d83e2016-08-08 19:03:30 -0700102 snprintf(option_text, 37, "-%s%s%s --%s%6s", def->short_name, short_val,
103 comma, def->long_name, long_val);
John Koleszarc6b90392012-07-13 15:21:29 -0700104 } else if (def->short_name)
clang-format6c4d83e2016-08-08 19:03:30 -0700105 snprintf(option_text, 37, "-%s%s", def->short_name, short_val);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400106 else if (def->long_name)
clang-format6c4d83e2016-08-08 19:03:30 -0700107 snprintf(option_text, 37, " --%s%s", def->long_name, long_val);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400108
John Koleszarc6b90392012-07-13 15:21:29 -0700109 fprintf(fp, " %-37s\t%s\n", option_text, def->desc);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400110
John Koleszarc6b90392012-07-13 15:21:29 -0700111 if (def->enums) {
112 const struct arg_enum_list *listptr;
113
114 fprintf(fp, " %-37s\t ", "");
115
116 for (listptr = def->enums; listptr->name; listptr++)
clang-format6c4d83e2016-08-08 19:03:30 -0700117 fprintf(fp, "%s%s", listptr->name, listptr[1].name ? ", " : "\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400118 }
John Koleszarc6b90392012-07-13 15:21:29 -0700119 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400120}
121
John Koleszarc6b90392012-07-13 15:21:29 -0700122unsigned int arg_parse_uint(const struct arg *arg) {
Urvang Joshid3a75762016-07-08 16:09:36 -0700123 uint32_t rawval;
clang-format6c4d83e2016-08-08 19:03:30 -0700124 char *endptr;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400125
Urvang Joshid3a75762016-07-08 16:09:36 -0700126 rawval = strtoul(arg->val, &endptr, 10);
John Koleszarc6b90392012-07-13 15:21:29 -0700127
128 if (arg->val[0] != '\0' && endptr[0] == '\0') {
Urvang Joshid3a75762016-07-08 16:09:36 -0700129 if (rawval <= UINT_MAX) return rawval;
John Koleszarc6b90392012-07-13 15:21:29 -0700130
clang-format6c4d83e2016-08-08 19:03:30 -0700131 die("Option %s: Value %ld out of range for unsigned int\n", arg->name,
132 rawval);
John Koleszarc6b90392012-07-13 15:21:29 -0700133 }
134
135 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
136 return 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400137}
138
John Koleszarc6b90392012-07-13 15:21:29 -0700139int arg_parse_int(const struct arg *arg) {
Urvang Joshid3a75762016-07-08 16:09:36 -0700140 int32_t rawval;
clang-format6c4d83e2016-08-08 19:03:30 -0700141 char *endptr;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400142
John Koleszarc6b90392012-07-13 15:21:29 -0700143 rawval = strtol(arg->val, &endptr, 10);
144
145 if (arg->val[0] != '\0' && endptr[0] == '\0') {
clang-format6c4d83e2016-08-08 19:03:30 -0700146 if (rawval >= INT_MIN && rawval <= INT_MAX) return rawval;
John Koleszarc6b90392012-07-13 15:21:29 -0700147
clang-format6c4d83e2016-08-08 19:03:30 -0700148 die("Option %s: Value %ld out of range for signed int\n", arg->name,
149 rawval);
John Koleszarc6b90392012-07-13 15:21:29 -0700150 }
151
152 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
153 return 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400154}
155
Yaowu Xuf883b422016-08-30 14:01:10 -0700156struct aom_rational {
John Koleszarc6b90392012-07-13 15:21:29 -0700157 int num; /**< fraction numerator */
158 int den; /**< fraction denominator */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400159};
Yaowu Xuf883b422016-08-30 14:01:10 -0700160struct aom_rational arg_parse_rational(const struct arg *arg) {
clang-format6c4d83e2016-08-08 19:03:30 -0700161 long int rawval;
162 char *endptr;
Yaowu Xuf883b422016-08-30 14:01:10 -0700163 struct aom_rational rat;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400164
John Koleszarc6b90392012-07-13 15:21:29 -0700165 /* parse numerator */
166 rawval = strtol(arg->val, &endptr, 10);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400167
John Koleszarc6b90392012-07-13 15:21:29 -0700168 if (arg->val[0] != '\0' && endptr[0] == '/') {
169 if (rawval >= INT_MIN && rawval <= INT_MAX)
170 rat.num = rawval;
clang-format6c4d83e2016-08-08 19:03:30 -0700171 else
172 die("Option %s: Value %ld out of range for signed int\n", arg->name,
173 rawval);
174 } else
175 die("Option %s: Expected / at '%c'\n", arg->name, *endptr);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400176
John Koleszarc6b90392012-07-13 15:21:29 -0700177 /* parse denominator */
178 rawval = strtol(endptr + 1, &endptr, 10);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400179
John Koleszarc6b90392012-07-13 15:21:29 -0700180 if (arg->val[0] != '\0' && endptr[0] == '\0') {
181 if (rawval >= INT_MIN && rawval <= INT_MAX)
182 rat.den = rawval;
clang-format6c4d83e2016-08-08 19:03:30 -0700183 else
184 die("Option %s: Value %ld out of range for signed int\n", arg->name,
185 rawval);
186 } else
187 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400188
John Koleszarc6b90392012-07-13 15:21:29 -0700189 return rat;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400190}
John Koleszarb0da9b32010-12-17 09:43:39 -0500191
John Koleszarc6b90392012-07-13 15:21:29 -0700192int arg_parse_enum(const struct arg *arg) {
193 const struct arg_enum_list *listptr;
clang-format6c4d83e2016-08-08 19:03:30 -0700194 long int rawval;
195 char *endptr;
John Koleszarb0da9b32010-12-17 09:43:39 -0500196
John Koleszarc6b90392012-07-13 15:21:29 -0700197 /* First see if the value can be parsed as a raw value */
198 rawval = strtol(arg->val, &endptr, 10);
199 if (arg->val[0] != '\0' && endptr[0] == '\0') {
200 /* Got a raw value, make sure it's valid */
201 for (listptr = arg->def->enums; listptr->name; listptr++)
clang-format6c4d83e2016-08-08 19:03:30 -0700202 if (listptr->val == rawval) return rawval;
John Koleszarc6b90392012-07-13 15:21:29 -0700203 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500204
John Koleszarc6b90392012-07-13 15:21:29 -0700205 /* Next see if it can be parsed as a string */
206 for (listptr = arg->def->enums; listptr->name; listptr++)
clang-format6c4d83e2016-08-08 19:03:30 -0700207 if (!strcmp(arg->val, listptr->name)) return listptr->val;
John Koleszarb0da9b32010-12-17 09:43:39 -0500208
John Koleszarc6b90392012-07-13 15:21:29 -0700209 die("Option %s: Invalid value '%s'\n", arg->name, arg->val);
210 return 0;
John Koleszarb0da9b32010-12-17 09:43:39 -0500211}
212
John Koleszarc6b90392012-07-13 15:21:29 -0700213int arg_parse_enum_or_int(const struct arg *arg) {
clang-format6c4d83e2016-08-08 19:03:30 -0700214 if (arg->def->enums) return arg_parse_enum(arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700215 return arg_parse_int(arg);
John Koleszarb0da9b32010-12-17 09:43:39 -0500216}