2 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license and patent
5 * grant that can be found in the LICENSE file in the root of the source
6 * tree. All contributing project authors may be found in the AUTHORS
7 * file in the root of the source tree.
17 #define snprintf _snprintf
20 #if defined(__GNUC__) && __GNUC__
21 extern void die(const char *fmt, ...) __attribute__((noreturn));
23 extern void die(const char *fmt, ...);
27 struct arg arg_init(char **argv)
39 int arg_match(struct arg *arg_, const struct arg_def *def, char **argv)
43 if (!argv[0] || argv[0][0] != '-')
49 && strlen(arg.argv[0]) == strlen(def->short_name) + 1
50 && !strcmp(arg.argv[0] + 1, def->short_name))
53 arg.name = arg.argv[0] + 1;
54 arg.val = def->has_val ? arg.argv[1] : NULL;
55 arg.argv_step = def->has_val ? 2 : 1;
57 else if (def->long_name)
59 int name_len = strlen(def->long_name);
61 if (strlen(arg.argv[0]) >= name_len + 2
62 && arg.argv[0][1] == '-'
63 && !strncmp(arg.argv[0] + 2, def->long_name, name_len)
64 && (arg.argv[0][name_len+2] == '='
65 || arg.argv[0][name_len+2] == '\0'))
68 arg.name = arg.argv[0] + 2;
69 arg.val = arg.name[name_len] == '=' ? arg.name + name_len + 1 : NULL;
74 if (arg.name && !arg.val && def->has_val)
75 die("Error: option %s requires argument.\n", arg.name);
77 if (arg.name && arg.val && !def->has_val)
78 die("Error: option %s requires no argument.\n", arg.name);
81 && (arg.val || !def->has_val))
92 const char *arg_next(struct arg *arg)
95 arg->argv += arg->argv_step;
101 char **argv_dup(int argc, const char **argv)
103 char **new_argv = malloc((argc + 1) * sizeof(*argv));
105 memcpy(new_argv, argv, argc * sizeof(*argv));
106 new_argv[argc] = NULL;
111 void arg_show_usage(FILE *fp, const struct arg_def *const *defs)
113 char option_text[40] = {0};
115 for (; *defs; defs++)
117 const struct arg_def *def = *defs;
118 char *short_val = def->has_val ? " <arg>" : "";
119 char *long_val = def->has_val ? "=<arg>" : "";
121 if (def->short_name && def->long_name)
122 snprintf(option_text, 37, "-%s%s, --%s%s",
123 def->short_name, short_val,
124 def->long_name, long_val);
125 else if (def->short_name)
126 snprintf(option_text, 37, "-%s%s",
127 def->short_name, short_val);
128 else if (def->long_name)
129 snprintf(option_text, 37, " --%s%s",
130 def->long_name, long_val);
132 fprintf(fp, " %-37s\t%s\n", option_text, def->desc);
137 unsigned int arg_parse_uint(const struct arg *arg)
142 rawval = strtol(arg->val, &endptr, 10);
144 if (arg->val[0] != '\0' && endptr[0] == '\0')
146 if (rawval >= 0 && rawval <= UINT_MAX)
149 die("Option %s: Value %ld out of range for unsigned int\n",
153 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
158 int arg_parse_int(const struct arg *arg)
163 rawval = strtol(arg->val, &endptr, 10);
165 if (arg->val[0] != '\0' && endptr[0] == '\0')
167 if (rawval >= INT_MIN && rawval <= INT_MAX)
170 die("Option %s: Value %ld out of range for signed int\n",
174 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
181 int num; /**< fraction numerator */
182 int den; /**< fraction denominator */
184 struct vpx_rational arg_parse_rational(const struct arg *arg)
188 struct vpx_rational rat;
190 /* parse numerator */
191 rawval = strtol(arg->val, &endptr, 10);
193 if (arg->val[0] != '\0' && endptr[0] == '/')
195 if (rawval >= INT_MIN && rawval <= INT_MAX)
197 else die("Option %s: Value %ld out of range for signed int\n",
200 else die("Option %s: Expected / at '%c'\n", arg->name, *endptr);
202 /* parse denominator */
203 rawval = strtol(endptr + 1, &endptr, 10);
205 if (arg->val[0] != '\0' && endptr[0] == '\0')
207 if (rawval >= INT_MIN && rawval <= INT_MAX)
209 else die("Option %s: Value %ld out of range for signed int\n",
212 else die("Option %s: Invalid character '%c'\n", arg->name, *endptr);