blob: 0ecbf4b45d3155c92def24085b79db54f9e7c994 [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
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +010037char *ignore_front_spaces(const char *str) {
38 while (str[0] == ' ' || str[0] == '\t') ++str;
39 return (char *)str;
40}
41
42void ignore_end_spaces(char *str) {
43 char *end = str + strlen(str);
44 while (end > str && (end[0] == ' ' || end[0] == '\t' || end[0] == '\n' ||
45 end[0] == '\r' || end[0] == '\0'))
46 --end;
47 if (end >= str) end[1] = '\0';
48}
49
50int arg_cfg(int *argc, char ***argv, const char *file) {
51 char **argv_local = (char **)*argv;
52 char **argv_org = (char **)*argv;
53 char line[1024 * 10];
54 FILE *f = fopen(file, "r");
55 if (!f) return 1;
56
57 while (fgets(line, sizeof(line) - 1, f)) {
58 char *actual_line = ignore_front_spaces(line);
59 char *left, *right, *comment;
60 size_t length = strlen(actual_line);
61
62 if (length == 0 || actual_line[0] == '#') continue;
63 right = strchr(actual_line, ':');
64 if (right == NULL) continue;
65 right[0] = '\0';
66
67 left = ignore_front_spaces(actual_line);
68 right = ignore_front_spaces(right + 1);
69
70 comment = strchr(right, '#');
71 if (comment != NULL) comment[0] = '\0';
72
73 ignore_end_spaces(left);
74 ignore_end_spaces(right);
75
76 char **new_args = argv_dup(*argc, (const char **)argv_local);
77 char *new_line = (char *)malloc(sizeof(*new_line) * 128);
78
79 if (argv_local != argv_org) free(argv_local);
80
81 if (!strcmp(right, "ON"))
82 snprintf(new_line, sizeof(*new_line) * 128, "--%s", left);
83 else
84 snprintf(new_line, sizeof(*new_line) * 128, "--%s=%s", left, right);
85
86 new_args[(*argc) - 1] = new_args[(*argc) - 2];
87 new_args[(*argc) - 2] = new_line;
88 argv_local = new_args;
89 *argv = new_args;
90 (*argc)++;
91 }
92 fclose(f);
93 return 0;
94}
95
John Koleszarc6b90392012-07-13 15:21:29 -070096int arg_match(struct arg *arg_, const struct arg_def *def, char **argv) {
97 struct arg arg;
John Koleszar0ea50ce2010-05-18 11:58:33 -040098
clang-format6c4d83e2016-08-08 19:03:30 -070099 if (!argv[0] || argv[0][0] != '-') return 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400100
John Koleszarc6b90392012-07-13 15:21:29 -0700101 arg = arg_init(argv);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400102
clang-format6c4d83e2016-08-08 19:03:30 -0700103 if (def->short_name && strlen(arg.argv[0]) == strlen(def->short_name) + 1 &&
104 !strcmp(arg.argv[0] + 1, def->short_name)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700105 arg.name = arg.argv[0] + 1;
106 arg.val = def->has_val ? arg.argv[1] : NULL;
107 arg.argv_step = def->has_val ? 2 : 1;
108 } else if (def->long_name) {
John Koleszar82b1a342012-11-06 12:08:05 -0800109 const size_t name_len = strlen(def->long_name);
John Koleszarc6b90392012-07-13 15:21:29 -0700110
clang-format6c4d83e2016-08-08 19:03:30 -0700111 if (strlen(arg.argv[0]) >= name_len + 2 && arg.argv[0][1] == '-' &&
112 !strncmp(arg.argv[0] + 2, def->long_name, name_len) &&
113 (arg.argv[0][name_len + 2] == '=' ||
114 arg.argv[0][name_len + 2] == '\0')) {
John Koleszarc6b90392012-07-13 15:21:29 -0700115 arg.name = arg.argv[0] + 2;
116 arg.val = arg.name[name_len] == '=' ? arg.name + name_len + 1 : NULL;
117 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400118 }
John Koleszarc6b90392012-07-13 15:21:29 -0700119 }
120
121 if (arg.name && !arg.val && def->has_val)
122 die("Error: option %s requires argument.\n", arg.name);
123
124 if (arg.name && arg.val && !def->has_val)
125 die("Error: option %s requires no argument.\n", arg.name);
126
clang-format6c4d83e2016-08-08 19:03:30 -0700127 if (arg.name && (arg.val || !def->has_val)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700128 arg.def = def;
129 *arg_ = arg;
130 return 1;
131 }
132
133 return 0;
134}
135
John Koleszarc6b90392012-07-13 15:21:29 -0700136const char *arg_next(struct arg *arg) {
clang-format6c4d83e2016-08-08 19:03:30 -0700137 if (arg->argv[0]) arg->argv += arg->argv_step;
John Koleszarc6b90392012-07-13 15:21:29 -0700138
139 return *arg->argv;
140}
141
John Koleszarc6b90392012-07-13 15:21:29 -0700142char **argv_dup(int argc, const char **argv) {
143 char **new_argv = malloc((argc + 1) * sizeof(*argv));
144
145 memcpy(new_argv, argv, argc * sizeof(*argv));
146 new_argv[argc] = NULL;
147 return new_argv;
148}
149
John Koleszarc6b90392012-07-13 15:21:29 -0700150void arg_show_usage(FILE *fp, const struct arg_def *const *defs) {
clang-format6c4d83e2016-08-08 19:03:30 -0700151 char option_text[40] = { 0 };
John Koleszarc6b90392012-07-13 15:21:29 -0700152
153 for (; *defs; defs++) {
154 const struct arg_def *def = *defs;
155 char *short_val = def->has_val ? " <arg>" : "";
156 char *long_val = def->has_val ? "=<arg>" : "";
157
158 if (def->short_name && def->long_name) {
159 char *comma = def->has_val ? "," : ", ";
160
clang-format6c4d83e2016-08-08 19:03:30 -0700161 snprintf(option_text, 37, "-%s%s%s --%s%6s", def->short_name, short_val,
162 comma, def->long_name, long_val);
John Koleszarc6b90392012-07-13 15:21:29 -0700163 } else if (def->short_name)
clang-format6c4d83e2016-08-08 19:03:30 -0700164 snprintf(option_text, 37, "-%s%s", def->short_name, short_val);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400165 else if (def->long_name)
clang-format6c4d83e2016-08-08 19:03:30 -0700166 snprintf(option_text, 37, " --%s%s", def->long_name, long_val);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400167
John Koleszarc6b90392012-07-13 15:21:29 -0700168 fprintf(fp, " %-37s\t%s\n", option_text, def->desc);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400169
John Koleszarc6b90392012-07-13 15:21:29 -0700170 if (def->enums) {
171 const struct arg_enum_list *listptr;
172
173 fprintf(fp, " %-37s\t ", "");
174
175 for (listptr = def->enums; listptr->name; listptr++)
clang-format6c4d83e2016-08-08 19:03:30 -0700176 fprintf(fp, "%s%s", listptr->name, listptr[1].name ? ", " : "\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400177 }
John Koleszarc6b90392012-07-13 15:21:29 -0700178 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400179}
180
John Koleszarc6b90392012-07-13 15:21:29 -0700181unsigned int arg_parse_uint(const struct arg *arg) {
clang-format6c4d83e2016-08-08 19:03:30 -0700182 char *endptr;
James Zern0d749d62017-04-04 09:26:51 -0700183 const unsigned long rawval = strtoul(arg->val, &endptr, 10); // NOLINT
John Koleszarc6b90392012-07-13 15:21:29 -0700184
185 if (arg->val[0] != '\0' && endptr[0] == '\0') {
James Zern12ec6c62017-03-24 17:36:36 -0700186 if (rawval <= UINT_MAX) return (unsigned int)rawval;
John Koleszarc6b90392012-07-13 15:21:29 -0700187
James Zern0d749d62017-04-04 09:26:51 -0700188 die("Option %s: Value %lu out of range for unsigned int\n", arg->name,
clang-format6c4d83e2016-08-08 19:03:30 -0700189 rawval);
John Koleszarc6b90392012-07-13 15:21:29 -0700190 }
191
192 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
193 return 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400194}
195
John Koleszarc6b90392012-07-13 15:21:29 -0700196int arg_parse_int(const struct arg *arg) {
clang-format6c4d83e2016-08-08 19:03:30 -0700197 char *endptr;
James Zern0d749d62017-04-04 09:26:51 -0700198 const long rawval = strtol(arg->val, &endptr, 10); // NOLINT
John Koleszarc6b90392012-07-13 15:21:29 -0700199
200 if (arg->val[0] != '\0' && endptr[0] == '\0') {
James Zern12ec6c62017-03-24 17:36:36 -0700201 if (rawval >= INT_MIN && rawval <= INT_MAX) return (int)rawval;
John Koleszarc6b90392012-07-13 15:21:29 -0700202
clang-format6c4d83e2016-08-08 19:03:30 -0700203 die("Option %s: Value %ld out of range for signed int\n", arg->name,
204 rawval);
John Koleszarc6b90392012-07-13 15:21:29 -0700205 }
206
207 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
208 return 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400209}
210
Yaowu Xuf883b422016-08-30 14:01:10 -0700211struct aom_rational {
John Koleszarc6b90392012-07-13 15:21:29 -0700212 int num; /**< fraction numerator */
213 int den; /**< fraction denominator */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400214};
Yaowu Xuf883b422016-08-30 14:01:10 -0700215struct aom_rational arg_parse_rational(const struct arg *arg) {
clang-format6c4d83e2016-08-08 19:03:30 -0700216 long int rawval;
217 char *endptr;
Yaowu Xuf883b422016-08-30 14:01:10 -0700218 struct aom_rational rat;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400219
John Koleszarc6b90392012-07-13 15:21:29 -0700220 /* parse numerator */
221 rawval = strtol(arg->val, &endptr, 10);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400222
John Koleszarc6b90392012-07-13 15:21:29 -0700223 if (arg->val[0] != '\0' && endptr[0] == '/') {
224 if (rawval >= INT_MIN && rawval <= INT_MAX)
James Zern12ec6c62017-03-24 17:36:36 -0700225 rat.num = (int)rawval;
clang-format6c4d83e2016-08-08 19:03:30 -0700226 else
227 die("Option %s: Value %ld out of range for signed int\n", arg->name,
228 rawval);
229 } else
230 die("Option %s: Expected / at '%c'\n", arg->name, *endptr);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400231
John Koleszarc6b90392012-07-13 15:21:29 -0700232 /* parse denominator */
233 rawval = strtol(endptr + 1, &endptr, 10);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400234
John Koleszarc6b90392012-07-13 15:21:29 -0700235 if (arg->val[0] != '\0' && endptr[0] == '\0') {
236 if (rawval >= INT_MIN && rawval <= INT_MAX)
James Zern12ec6c62017-03-24 17:36:36 -0700237 rat.den = (int)rawval;
clang-format6c4d83e2016-08-08 19:03:30 -0700238 else
239 die("Option %s: Value %ld out of range for signed int\n", arg->name,
240 rawval);
241 } else
242 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400243
John Koleszarc6b90392012-07-13 15:21:29 -0700244 return rat;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400245}
John Koleszarb0da9b32010-12-17 09:43:39 -0500246
John Koleszarc6b90392012-07-13 15:21:29 -0700247int arg_parse_enum(const struct arg *arg) {
248 const struct arg_enum_list *listptr;
clang-format6c4d83e2016-08-08 19:03:30 -0700249 long int rawval;
250 char *endptr;
John Koleszarb0da9b32010-12-17 09:43:39 -0500251
John Koleszarc6b90392012-07-13 15:21:29 -0700252 /* First see if the value can be parsed as a raw value */
253 rawval = strtol(arg->val, &endptr, 10);
254 if (arg->val[0] != '\0' && endptr[0] == '\0') {
255 /* Got a raw value, make sure it's valid */
256 for (listptr = arg->def->enums; listptr->name; listptr++)
James Zern12ec6c62017-03-24 17:36:36 -0700257 if (listptr->val == rawval) return (int)rawval;
John Koleszarc6b90392012-07-13 15:21:29 -0700258 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500259
John Koleszarc6b90392012-07-13 15:21:29 -0700260 /* Next see if it can be parsed as a string */
261 for (listptr = arg->def->enums; listptr->name; listptr++)
clang-format6c4d83e2016-08-08 19:03:30 -0700262 if (!strcmp(arg->val, listptr->name)) return listptr->val;
John Koleszarb0da9b32010-12-17 09:43:39 -0500263
John Koleszarc6b90392012-07-13 15:21:29 -0700264 die("Option %s: Invalid value '%s'\n", arg->name, arg->val);
265 return 0;
John Koleszarb0da9b32010-12-17 09:43:39 -0500266}
267
John Koleszarc6b90392012-07-13 15:21:29 -0700268int arg_parse_enum_or_int(const struct arg *arg) {
clang-format6c4d83e2016-08-08 19:03:30 -0700269 if (arg->def->enums) return arg_parse_enum(arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700270 return arg_parse_int(arg);
John Koleszarb0da9b32010-12-17 09:43:39 -0500271}
Dominic Symes26ad0b22017-10-01 16:35:13 +0200272
273// parse a comma separated list of at most n integers
274// return the number of elements in the list
275int arg_parse_list(const struct arg *arg, int *list, int n) {
276 const char *ptr = arg->val;
277 char *endptr;
278 int i = 0;
279
280 while (ptr[0] != '\0') {
281 int32_t rawval = (int32_t)strtol(ptr, &endptr, 10);
282 if (rawval < INT_MIN || rawval > INT_MAX) {
283 die("Option %s: Value %ld out of range for signed int\n", arg->name,
284 rawval);
285 } else if (i >= n) {
286 die("Option %s: List has more than %d entries\n", arg->name, n);
287 } else if (*endptr == ',') {
288 endptr++;
289 } else if (*endptr != '\0') {
290 die("Option %s: Bad list separator '%c'\n", arg->name, *endptr);
291 }
292 list[i++] = (int)rawval;
293 ptr = endptr;
294 }
295 return i;
296}