blob: 51c0fb9c4ebf4818a07044f3b23b968dd6f7c6f4 [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
John Koleszarc2140b82010-09-09 08:16:39 -04002 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
John Koleszar94c52e42010-06-18 12:39:21 -04004 * Use of this source code is governed by a BSD-style license
John Koleszar09202d82010-06-04 16:19:40 -04005 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
John Koleszar94c52e42010-06-18 12:39:21 -04007 * in the file PATENTS. All contributing project authors may
John Koleszar09202d82010-06-04 16:19:40 -04008 * be found in the AUTHORS file in the root of the source tree.
John Koleszar0ea50ce2010-05-18 11:58:33 -04009 */
10
John Koleszar0ea50ce2010-05-18 11:58:33 -040011#include <stdlib.h>
12#include <string.h>
13#include <limits.h>
14#include "args.h"
15
Johanncad0eca2015-05-07 16:41:33 -070016#include "vpx_ports/msvc.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040017
18#if defined(__GNUC__) && __GNUC__
19extern void die(const char *fmt, ...) __attribute__((noreturn));
20#else
21extern void die(const char *fmt, ...);
22#endif
23
John Koleszarc6b90392012-07-13 15:21:29 -070024struct arg arg_init(char **argv) {
25 struct arg a;
John Koleszar0ea50ce2010-05-18 11:58:33 -040026
clang-format6c4d83e2016-08-08 19:03:30 -070027 a.argv = argv;
John Koleszarc6b90392012-07-13 15:21:29 -070028 a.argv_step = 1;
clang-format6c4d83e2016-08-08 19:03:30 -070029 a.name = NULL;
30 a.val = NULL;
31 a.def = NULL;
John Koleszarc6b90392012-07-13 15:21:29 -070032 return a;
John Koleszar0ea50ce2010-05-18 11:58:33 -040033}
34
John Koleszarc6b90392012-07-13 15:21:29 -070035int arg_match(struct arg *arg_, const struct arg_def *def, char **argv) {
36 struct arg arg;
John Koleszar0ea50ce2010-05-18 11:58:33 -040037
clang-format6c4d83e2016-08-08 19:03:30 -070038 if (!argv[0] || argv[0][0] != '-') return 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -040039
John Koleszarc6b90392012-07-13 15:21:29 -070040 arg = arg_init(argv);
John Koleszar0ea50ce2010-05-18 11:58:33 -040041
clang-format6c4d83e2016-08-08 19:03:30 -070042 if (def->short_name && strlen(arg.argv[0]) == strlen(def->short_name) + 1 &&
43 !strcmp(arg.argv[0] + 1, def->short_name)) {
John Koleszarc6b90392012-07-13 15:21:29 -070044 arg.name = arg.argv[0] + 1;
45 arg.val = def->has_val ? arg.argv[1] : NULL;
46 arg.argv_step = def->has_val ? 2 : 1;
47 } else if (def->long_name) {
John Koleszar82b1a342012-11-06 12:08:05 -080048 const size_t name_len = strlen(def->long_name);
John Koleszarc6b90392012-07-13 15:21:29 -070049
clang-format6c4d83e2016-08-08 19:03:30 -070050 if (strlen(arg.argv[0]) >= name_len + 2 && arg.argv[0][1] == '-' &&
51 !strncmp(arg.argv[0] + 2, def->long_name, name_len) &&
52 (arg.argv[0][name_len + 2] == '=' ||
53 arg.argv[0][name_len + 2] == '\0')) {
John Koleszarc6b90392012-07-13 15:21:29 -070054 arg.name = arg.argv[0] + 2;
55 arg.val = arg.name[name_len] == '=' ? arg.name + name_len + 1 : NULL;
56 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -040057 }
John Koleszarc6b90392012-07-13 15:21:29 -070058 }
59
60 if (arg.name && !arg.val && def->has_val)
61 die("Error: option %s requires argument.\n", arg.name);
62
63 if (arg.name && arg.val && !def->has_val)
64 die("Error: option %s requires no argument.\n", arg.name);
65
clang-format6c4d83e2016-08-08 19:03:30 -070066 if (arg.name && (arg.val || !def->has_val)) {
John Koleszarc6b90392012-07-13 15:21:29 -070067 arg.def = def;
68 *arg_ = arg;
69 return 1;
70 }
71
72 return 0;
73}
74
John Koleszarc6b90392012-07-13 15:21:29 -070075const char *arg_next(struct arg *arg) {
clang-format6c4d83e2016-08-08 19:03:30 -070076 if (arg->argv[0]) arg->argv += arg->argv_step;
John Koleszarc6b90392012-07-13 15:21:29 -070077
78 return *arg->argv;
79}
80
John Koleszarc6b90392012-07-13 15:21:29 -070081char **argv_dup(int argc, const char **argv) {
82 char **new_argv = malloc((argc + 1) * sizeof(*argv));
83
84 memcpy(new_argv, argv, argc * sizeof(*argv));
85 new_argv[argc] = NULL;
86 return new_argv;
87}
88
John Koleszarc6b90392012-07-13 15:21:29 -070089void arg_show_usage(FILE *fp, const struct arg_def *const *defs) {
clang-format6c4d83e2016-08-08 19:03:30 -070090 char option_text[40] = { 0 };
John Koleszarc6b90392012-07-13 15:21:29 -070091
92 for (; *defs; defs++) {
93 const struct arg_def *def = *defs;
94 char *short_val = def->has_val ? " <arg>" : "";
95 char *long_val = def->has_val ? "=<arg>" : "";
96
97 if (def->short_name && def->long_name) {
98 char *comma = def->has_val ? "," : ", ";
99
clang-format6c4d83e2016-08-08 19:03:30 -0700100 snprintf(option_text, 37, "-%s%s%s --%s%6s", def->short_name, short_val,
101 comma, def->long_name, long_val);
John Koleszarc6b90392012-07-13 15:21:29 -0700102 } else if (def->short_name)
clang-format6c4d83e2016-08-08 19:03:30 -0700103 snprintf(option_text, 37, "-%s%s", def->short_name, short_val);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400104 else if (def->long_name)
clang-format6c4d83e2016-08-08 19:03:30 -0700105 snprintf(option_text, 37, " --%s%s", def->long_name, long_val);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400106
John Koleszarc6b90392012-07-13 15:21:29 -0700107 fprintf(fp, " %-37s\t%s\n", option_text, def->desc);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400108
John Koleszarc6b90392012-07-13 15:21:29 -0700109 if (def->enums) {
110 const struct arg_enum_list *listptr;
111
112 fprintf(fp, " %-37s\t ", "");
113
114 for (listptr = def->enums; listptr->name; listptr++)
clang-format6c4d83e2016-08-08 19:03:30 -0700115 fprintf(fp, "%s%s", listptr->name, listptr[1].name ? ", " : "\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400116 }
John Koleszarc6b90392012-07-13 15:21:29 -0700117 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400118}
119
John Koleszarc6b90392012-07-13 15:21:29 -0700120unsigned int arg_parse_uint(const struct arg *arg) {
clang-format6c4d83e2016-08-08 19:03:30 -0700121 long int rawval;
122 char *endptr;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400123
John Koleszarc6b90392012-07-13 15:21:29 -0700124 rawval = strtol(arg->val, &endptr, 10);
125
126 if (arg->val[0] != '\0' && endptr[0] == '\0') {
clang-format6c4d83e2016-08-08 19:03:30 -0700127 if (rawval >= 0 && rawval <= UINT_MAX) return rawval;
John Koleszarc6b90392012-07-13 15:21:29 -0700128
clang-format6c4d83e2016-08-08 19:03:30 -0700129 die("Option %s: Value %ld out of range for unsigned int\n", arg->name,
130 rawval);
John Koleszarc6b90392012-07-13 15:21:29 -0700131 }
132
133 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
134 return 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400135}
136
John Koleszarc6b90392012-07-13 15:21:29 -0700137int arg_parse_int(const struct arg *arg) {
clang-format6c4d83e2016-08-08 19:03:30 -0700138 long int rawval;
139 char *endptr;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400140
John Koleszarc6b90392012-07-13 15:21:29 -0700141 rawval = strtol(arg->val, &endptr, 10);
142
143 if (arg->val[0] != '\0' && endptr[0] == '\0') {
clang-format6c4d83e2016-08-08 19:03:30 -0700144 if (rawval >= INT_MIN && rawval <= INT_MAX) return rawval;
John Koleszarc6b90392012-07-13 15:21:29 -0700145
clang-format6c4d83e2016-08-08 19:03:30 -0700146 die("Option %s: Value %ld out of range for signed int\n", arg->name,
147 rawval);
John Koleszarc6b90392012-07-13 15:21:29 -0700148 }
149
150 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
151 return 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400152}
153
John Koleszarc6b90392012-07-13 15:21:29 -0700154struct vpx_rational {
155 int num; /**< fraction numerator */
156 int den; /**< fraction denominator */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400157};
John Koleszarc6b90392012-07-13 15:21:29 -0700158struct vpx_rational arg_parse_rational(const struct arg *arg) {
clang-format6c4d83e2016-08-08 19:03:30 -0700159 long int rawval;
160 char *endptr;
161 struct vpx_rational rat;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400162
John Koleszarc6b90392012-07-13 15:21:29 -0700163 /* parse numerator */
164 rawval = strtol(arg->val, &endptr, 10);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400165
John Koleszarc6b90392012-07-13 15:21:29 -0700166 if (arg->val[0] != '\0' && endptr[0] == '/') {
167 if (rawval >= INT_MIN && rawval <= INT_MAX)
168 rat.num = rawval;
clang-format6c4d83e2016-08-08 19:03:30 -0700169 else
170 die("Option %s: Value %ld out of range for signed int\n", arg->name,
171 rawval);
172 } else
173 die("Option %s: Expected / at '%c'\n", arg->name, *endptr);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400174
John Koleszarc6b90392012-07-13 15:21:29 -0700175 /* parse denominator */
176 rawval = strtol(endptr + 1, &endptr, 10);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400177
John Koleszarc6b90392012-07-13 15:21:29 -0700178 if (arg->val[0] != '\0' && endptr[0] == '\0') {
179 if (rawval >= INT_MIN && rawval <= INT_MAX)
180 rat.den = rawval;
clang-format6c4d83e2016-08-08 19:03:30 -0700181 else
182 die("Option %s: Value %ld out of range for signed int\n", arg->name,
183 rawval);
184 } else
185 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400186
John Koleszarc6b90392012-07-13 15:21:29 -0700187 return rat;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400188}
John Koleszarb0da9b32010-12-17 09:43:39 -0500189
John Koleszarc6b90392012-07-13 15:21:29 -0700190int arg_parse_enum(const struct arg *arg) {
191 const struct arg_enum_list *listptr;
clang-format6c4d83e2016-08-08 19:03:30 -0700192 long int rawval;
193 char *endptr;
John Koleszarb0da9b32010-12-17 09:43:39 -0500194
John Koleszarc6b90392012-07-13 15:21:29 -0700195 /* First see if the value can be parsed as a raw value */
196 rawval = strtol(arg->val, &endptr, 10);
197 if (arg->val[0] != '\0' && endptr[0] == '\0') {
198 /* Got a raw value, make sure it's valid */
199 for (listptr = arg->def->enums; listptr->name; listptr++)
clang-format6c4d83e2016-08-08 19:03:30 -0700200 if (listptr->val == rawval) return rawval;
John Koleszarc6b90392012-07-13 15:21:29 -0700201 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500202
John Koleszarc6b90392012-07-13 15:21:29 -0700203 /* Next see if it can be parsed as a string */
204 for (listptr = arg->def->enums; listptr->name; listptr++)
clang-format6c4d83e2016-08-08 19:03:30 -0700205 if (!strcmp(arg->val, listptr->name)) return listptr->val;
John Koleszarb0da9b32010-12-17 09:43:39 -0500206
John Koleszarc6b90392012-07-13 15:21:29 -0700207 die("Option %s: Invalid value '%s'\n", arg->name, arg->val);
208 return 0;
John Koleszarb0da9b32010-12-17 09:43:39 -0500209}
210
John Koleszarc6b90392012-07-13 15:21:29 -0700211int arg_parse_enum_or_int(const struct arg *arg) {
clang-format6c4d83e2016-08-08 19:03:30 -0700212 if (arg->def->enums) return arg_parse_enum(arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700213 return arg_parse_int(arg);
John Koleszarb0da9b32010-12-17 09:43:39 -0500214}