blob: 7131e24deec30368f97e2d288f6414ee5503e86a [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
Tom Finegandd3e2a52018-05-23 14:33:09 -070012#include "common/args.h"
13
John Koleszar0ea50ce2010-05-18 11:58:33 -040014#include <stdlib.h>
15#include <string.h>
16#include <limits.h>
John Koleszar0ea50ce2010-05-18 11:58:33 -040017
Urvang Joshif792a722016-07-11 15:51:21 -070018#include "aom/aom_integer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070019#include "aom_ports/msvc.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040020
21#if defined(__GNUC__) && __GNUC__
22extern void die(const char *fmt, ...) __attribute__((noreturn));
23#else
24extern void die(const char *fmt, ...);
25#endif
26
John Koleszarc6b90392012-07-13 15:21:29 -070027struct arg arg_init(char **argv) {
28 struct arg a;
John Koleszar0ea50ce2010-05-18 11:58:33 -040029
clang-format6c4d83e2016-08-08 19:03:30 -070030 a.argv = argv;
John Koleszarc6b90392012-07-13 15:21:29 -070031 a.argv_step = 1;
clang-format6c4d83e2016-08-08 19:03:30 -070032 a.name = NULL;
33 a.val = NULL;
34 a.def = NULL;
John Koleszarc6b90392012-07-13 15:21:29 -070035 return a;
John Koleszar0ea50ce2010-05-18 11:58:33 -040036}
37
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +010038char *ignore_front_spaces(const char *str) {
39 while (str[0] == ' ' || str[0] == '\t') ++str;
40 return (char *)str;
41}
42
43void ignore_end_spaces(char *str) {
44 char *end = str + strlen(str);
45 while (end > str && (end[0] == ' ' || end[0] == '\t' || end[0] == '\n' ||
46 end[0] == '\r' || end[0] == '\0'))
47 --end;
48 if (end >= str) end[1] = '\0';
49}
50
51int arg_cfg(int *argc, char ***argv, const char *file) {
52 char **argv_local = (char **)*argv;
53 char **argv_org = (char **)*argv;
54 char line[1024 * 10];
55 FILE *f = fopen(file, "r");
56 if (!f) return 1;
57
58 while (fgets(line, sizeof(line) - 1, f)) {
59 char *actual_line = ignore_front_spaces(line);
60 char *left, *right, *comment;
61 size_t length = strlen(actual_line);
62
63 if (length == 0 || actual_line[0] == '#') continue;
64 right = strchr(actual_line, ':');
65 if (right == NULL) continue;
66 right[0] = '\0';
67
68 left = ignore_front_spaces(actual_line);
69 right = ignore_front_spaces(right + 1);
70
71 comment = strchr(right, '#');
72 if (comment != NULL) comment[0] = '\0';
73
74 ignore_end_spaces(left);
75 ignore_end_spaces(right);
76
77 char **new_args = argv_dup(*argc, (const char **)argv_local);
78 char *new_line = (char *)malloc(sizeof(*new_line) * 128);
79
80 if (argv_local != argv_org) free(argv_local);
81
82 if (!strcmp(right, "ON"))
83 snprintf(new_line, sizeof(*new_line) * 128, "--%s", left);
84 else
85 snprintf(new_line, sizeof(*new_line) * 128, "--%s=%s", left, right);
86
87 new_args[(*argc) - 1] = new_args[(*argc) - 2];
88 new_args[(*argc) - 2] = new_line;
89 argv_local = new_args;
90 *argv = new_args;
91 (*argc)++;
92 }
93 fclose(f);
94 return 0;
95}
96
John Koleszarc6b90392012-07-13 15:21:29 -070097int arg_match(struct arg *arg_, const struct arg_def *def, char **argv) {
98 struct arg arg;
John Koleszar0ea50ce2010-05-18 11:58:33 -040099
clang-format6c4d83e2016-08-08 19:03:30 -0700100 if (!argv[0] || argv[0][0] != '-') return 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400101
John Koleszarc6b90392012-07-13 15:21:29 -0700102 arg = arg_init(argv);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400103
clang-format6c4d83e2016-08-08 19:03:30 -0700104 if (def->short_name && strlen(arg.argv[0]) == strlen(def->short_name) + 1 &&
105 !strcmp(arg.argv[0] + 1, def->short_name)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700106 arg.name = arg.argv[0] + 1;
107 arg.val = def->has_val ? arg.argv[1] : NULL;
108 arg.argv_step = def->has_val ? 2 : 1;
109 } else if (def->long_name) {
John Koleszar82b1a342012-11-06 12:08:05 -0800110 const size_t name_len = strlen(def->long_name);
John Koleszarc6b90392012-07-13 15:21:29 -0700111
clang-format6c4d83e2016-08-08 19:03:30 -0700112 if (strlen(arg.argv[0]) >= name_len + 2 && arg.argv[0][1] == '-' &&
113 !strncmp(arg.argv[0] + 2, def->long_name, name_len) &&
114 (arg.argv[0][name_len + 2] == '=' ||
115 arg.argv[0][name_len + 2] == '\0')) {
John Koleszarc6b90392012-07-13 15:21:29 -0700116 arg.name = arg.argv[0] + 2;
117 arg.val = arg.name[name_len] == '=' ? arg.name + name_len + 1 : NULL;
118 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400119 }
John Koleszarc6b90392012-07-13 15:21:29 -0700120 }
121
122 if (arg.name && !arg.val && def->has_val)
123 die("Error: option %s requires argument.\n", arg.name);
124
125 if (arg.name && arg.val && !def->has_val)
126 die("Error: option %s requires no argument.\n", arg.name);
127
clang-format6c4d83e2016-08-08 19:03:30 -0700128 if (arg.name && (arg.val || !def->has_val)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700129 arg.def = def;
130 *arg_ = arg;
131 return 1;
132 }
133
134 return 0;
135}
136
John Koleszarc6b90392012-07-13 15:21:29 -0700137const char *arg_next(struct arg *arg) {
clang-format6c4d83e2016-08-08 19:03:30 -0700138 if (arg->argv[0]) arg->argv += arg->argv_step;
John Koleszarc6b90392012-07-13 15:21:29 -0700139
140 return *arg->argv;
141}
142
John Koleszarc6b90392012-07-13 15:21:29 -0700143char **argv_dup(int argc, const char **argv) {
144 char **new_argv = malloc((argc + 1) * sizeof(*argv));
145
146 memcpy(new_argv, argv, argc * sizeof(*argv));
147 new_argv[argc] = NULL;
148 return new_argv;
149}
150
John Koleszarc6b90392012-07-13 15:21:29 -0700151void arg_show_usage(FILE *fp, const struct arg_def *const *defs) {
clang-format6c4d83e2016-08-08 19:03:30 -0700152 char option_text[40] = { 0 };
John Koleszarc6b90392012-07-13 15:21:29 -0700153
154 for (; *defs; defs++) {
155 const struct arg_def *def = *defs;
156 char *short_val = def->has_val ? " <arg>" : "";
157 char *long_val = def->has_val ? "=<arg>" : "";
158
159 if (def->short_name && def->long_name) {
160 char *comma = def->has_val ? "," : ", ";
161
clang-format6c4d83e2016-08-08 19:03:30 -0700162 snprintf(option_text, 37, "-%s%s%s --%s%6s", def->short_name, short_val,
163 comma, def->long_name, long_val);
John Koleszarc6b90392012-07-13 15:21:29 -0700164 } else if (def->short_name)
clang-format6c4d83e2016-08-08 19:03:30 -0700165 snprintf(option_text, 37, "-%s%s", def->short_name, short_val);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400166 else if (def->long_name)
clang-format6c4d83e2016-08-08 19:03:30 -0700167 snprintf(option_text, 37, " --%s%s", def->long_name, long_val);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400168
John Koleszarc6b90392012-07-13 15:21:29 -0700169 fprintf(fp, " %-37s\t%s\n", option_text, def->desc);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400170
John Koleszarc6b90392012-07-13 15:21:29 -0700171 if (def->enums) {
172 const struct arg_enum_list *listptr;
173
174 fprintf(fp, " %-37s\t ", "");
175
176 for (listptr = def->enums; listptr->name; listptr++)
clang-format6c4d83e2016-08-08 19:03:30 -0700177 fprintf(fp, "%s%s", listptr->name, listptr[1].name ? ", " : "\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400178 }
John Koleszarc6b90392012-07-13 15:21:29 -0700179 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400180}
181
John Koleszarc6b90392012-07-13 15:21:29 -0700182unsigned int arg_parse_uint(const struct arg *arg) {
clang-format6c4d83e2016-08-08 19:03:30 -0700183 char *endptr;
James Zern0d749d62017-04-04 09:26:51 -0700184 const unsigned long rawval = strtoul(arg->val, &endptr, 10); // NOLINT
John Koleszarc6b90392012-07-13 15:21:29 -0700185
186 if (arg->val[0] != '\0' && endptr[0] == '\0') {
James Zern12ec6c62017-03-24 17:36:36 -0700187 if (rawval <= UINT_MAX) return (unsigned int)rawval;
John Koleszarc6b90392012-07-13 15:21:29 -0700188
James Zern0d749d62017-04-04 09:26:51 -0700189 die("Option %s: Value %lu out of range for unsigned int\n", arg->name,
clang-format6c4d83e2016-08-08 19:03:30 -0700190 rawval);
John Koleszarc6b90392012-07-13 15:21:29 -0700191 }
192
193 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
194 return 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400195}
196
John Koleszarc6b90392012-07-13 15:21:29 -0700197int arg_parse_int(const struct arg *arg) {
clang-format6c4d83e2016-08-08 19:03:30 -0700198 char *endptr;
James Zern0d749d62017-04-04 09:26:51 -0700199 const long rawval = strtol(arg->val, &endptr, 10); // NOLINT
John Koleszarc6b90392012-07-13 15:21:29 -0700200
201 if (arg->val[0] != '\0' && endptr[0] == '\0') {
James Zern12ec6c62017-03-24 17:36:36 -0700202 if (rawval >= INT_MIN && rawval <= INT_MAX) return (int)rawval;
John Koleszarc6b90392012-07-13 15:21:29 -0700203
clang-format6c4d83e2016-08-08 19:03:30 -0700204 die("Option %s: Value %ld out of range for signed int\n", arg->name,
205 rawval);
John Koleszarc6b90392012-07-13 15:21:29 -0700206 }
207
208 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
209 return 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400210}
211
Yaowu Xuf883b422016-08-30 14:01:10 -0700212struct aom_rational {
John Koleszarc6b90392012-07-13 15:21:29 -0700213 int num; /**< fraction numerator */
214 int den; /**< fraction denominator */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400215};
Yaowu Xuf883b422016-08-30 14:01:10 -0700216struct aom_rational arg_parse_rational(const struct arg *arg) {
clang-format6c4d83e2016-08-08 19:03:30 -0700217 long int rawval;
218 char *endptr;
Yaowu Xuf883b422016-08-30 14:01:10 -0700219 struct aom_rational rat;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400220
John Koleszarc6b90392012-07-13 15:21:29 -0700221 /* parse numerator */
222 rawval = strtol(arg->val, &endptr, 10);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400223
John Koleszarc6b90392012-07-13 15:21:29 -0700224 if (arg->val[0] != '\0' && endptr[0] == '/') {
225 if (rawval >= INT_MIN && rawval <= INT_MAX)
James Zern12ec6c62017-03-24 17:36:36 -0700226 rat.num = (int)rawval;
clang-format6c4d83e2016-08-08 19:03:30 -0700227 else
228 die("Option %s: Value %ld out of range for signed int\n", arg->name,
229 rawval);
230 } else
231 die("Option %s: Expected / at '%c'\n", arg->name, *endptr);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400232
John Koleszarc6b90392012-07-13 15:21:29 -0700233 /* parse denominator */
234 rawval = strtol(endptr + 1, &endptr, 10);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400235
John Koleszarc6b90392012-07-13 15:21:29 -0700236 if (arg->val[0] != '\0' && endptr[0] == '\0') {
237 if (rawval >= INT_MIN && rawval <= INT_MAX)
James Zern12ec6c62017-03-24 17:36:36 -0700238 rat.den = (int)rawval;
clang-format6c4d83e2016-08-08 19:03:30 -0700239 else
240 die("Option %s: Value %ld out of range for signed int\n", arg->name,
241 rawval);
242 } else
243 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400244
John Koleszarc6b90392012-07-13 15:21:29 -0700245 return rat;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400246}
John Koleszarb0da9b32010-12-17 09:43:39 -0500247
John Koleszarc6b90392012-07-13 15:21:29 -0700248int arg_parse_enum(const struct arg *arg) {
249 const struct arg_enum_list *listptr;
clang-format6c4d83e2016-08-08 19:03:30 -0700250 long int rawval;
251 char *endptr;
John Koleszarb0da9b32010-12-17 09:43:39 -0500252
John Koleszarc6b90392012-07-13 15:21:29 -0700253 /* First see if the value can be parsed as a raw value */
254 rawval = strtol(arg->val, &endptr, 10);
255 if (arg->val[0] != '\0' && endptr[0] == '\0') {
256 /* Got a raw value, make sure it's valid */
257 for (listptr = arg->def->enums; listptr->name; listptr++)
James Zern12ec6c62017-03-24 17:36:36 -0700258 if (listptr->val == rawval) return (int)rawval;
John Koleszarc6b90392012-07-13 15:21:29 -0700259 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500260
John Koleszarc6b90392012-07-13 15:21:29 -0700261 /* Next see if it can be parsed as a string */
262 for (listptr = arg->def->enums; listptr->name; listptr++)
clang-format6c4d83e2016-08-08 19:03:30 -0700263 if (!strcmp(arg->val, listptr->name)) return listptr->val;
John Koleszarb0da9b32010-12-17 09:43:39 -0500264
John Koleszarc6b90392012-07-13 15:21:29 -0700265 die("Option %s: Invalid value '%s'\n", arg->name, arg->val);
266 return 0;
John Koleszarb0da9b32010-12-17 09:43:39 -0500267}
268
John Koleszarc6b90392012-07-13 15:21:29 -0700269int arg_parse_enum_or_int(const struct arg *arg) {
clang-format6c4d83e2016-08-08 19:03:30 -0700270 if (arg->def->enums) return arg_parse_enum(arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700271 return arg_parse_int(arg);
John Koleszarb0da9b32010-12-17 09:43:39 -0500272}
Dominic Symes26ad0b22017-10-01 16:35:13 +0200273
274// parse a comma separated list of at most n integers
275// return the number of elements in the list
276int arg_parse_list(const struct arg *arg, int *list, int n) {
277 const char *ptr = arg->val;
278 char *endptr;
279 int i = 0;
280
281 while (ptr[0] != '\0') {
282 int32_t rawval = (int32_t)strtol(ptr, &endptr, 10);
283 if (rawval < INT_MIN || rawval > INT_MAX) {
284 die("Option %s: Value %ld out of range for signed int\n", arg->name,
285 rawval);
286 } else if (i >= n) {
287 die("Option %s: List has more than %d entries\n", arg->name, n);
288 } else if (*endptr == ',') {
289 endptr++;
290 } else if (*endptr != '\0') {
291 die("Option %s: Bad list separator '%c'\n", arg->name, *endptr);
292 }
293 list[i++] = (int)rawval;
294 ptr = endptr;
295 }
296 return i;
297}