platform autodetect: accept amd64 as a synonym for x86_64
[libvpx.git] / args.c
blob5fd046671ecf9e6497458af7162ca153d32c1f49
1 /*
2 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * 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
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
12 #include <stdlib.h>
13 #include <string.h>
14 #include <limits.h>
15 #include "args.h"
17 #ifdef _MSC_VER
18 #define snprintf _snprintf
19 #endif
21 #if defined(__GNUC__) && __GNUC__
22 extern void die(const char *fmt, ...) __attribute__((noreturn));
23 #else
24 extern void die(const char *fmt, ...);
25 #endif
28 struct arg arg_init(char **argv)
30 struct arg a;
32 a.argv = argv;
33 a.argv_step = 1;
34 a.name = NULL;
35 a.val = NULL;
36 a.def = NULL;
37 return a;
40 int arg_match(struct arg *arg_, const struct arg_def *def, char **argv)
42 struct arg arg;
44 if (!argv[0] || argv[0][0] != '-')
45 return 0;
47 arg = arg_init(argv);
49 if (def->short_name
50 && strlen(arg.argv[0]) == strlen(def->short_name) + 1
51 && !strcmp(arg.argv[0] + 1, def->short_name))
54 arg.name = arg.argv[0] + 1;
55 arg.val = def->has_val ? arg.argv[1] : NULL;
56 arg.argv_step = def->has_val ? 2 : 1;
58 else if (def->long_name)
60 int name_len = strlen(def->long_name);
62 if (strlen(arg.argv[0]) >= name_len + 2
63 && arg.argv[0][1] == '-'
64 && !strncmp(arg.argv[0] + 2, def->long_name, name_len)
65 && (arg.argv[0][name_len+2] == '='
66 || arg.argv[0][name_len+2] == '\0'))
69 arg.name = arg.argv[0] + 2;
70 arg.val = arg.name[name_len] == '=' ? arg.name + name_len + 1 : NULL;
71 arg.argv_step = 1;
75 if (arg.name && !arg.val && def->has_val)
76 die("Error: option %s requires argument.\n", arg.name);
78 if (arg.name && arg.val && !def->has_val)
79 die("Error: option %s requires no argument.\n", arg.name);
81 if (arg.name
82 && (arg.val || !def->has_val))
84 arg.def = def;
85 *arg_ = arg;
86 return 1;
89 return 0;
93 const char *arg_next(struct arg *arg)
95 if (arg->argv[0])
96 arg->argv += arg->argv_step;
98 return *arg->argv;
102 char **argv_dup(int argc, const char **argv)
104 char **new_argv = malloc((argc + 1) * sizeof(*argv));
106 memcpy(new_argv, argv, argc * sizeof(*argv));
107 new_argv[argc] = NULL;
108 return new_argv;
112 void arg_show_usage(FILE *fp, const struct arg_def *const *defs)
114 char option_text[40] = {0};
116 for (; *defs; defs++)
118 const struct arg_def *def = *defs;
119 char *short_val = def->has_val ? " <arg>" : "";
120 char *long_val = def->has_val ? "=<arg>" : "";
122 if (def->short_name && def->long_name)
123 snprintf(option_text, 37, "-%s%s, --%s%s",
124 def->short_name, short_val,
125 def->long_name, long_val);
126 else if (def->short_name)
127 snprintf(option_text, 37, "-%s%s",
128 def->short_name, short_val);
129 else if (def->long_name)
130 snprintf(option_text, 37, " --%s%s",
131 def->long_name, long_val);
133 fprintf(fp, " %-37s\t%s\n", option_text, def->desc);
138 unsigned int arg_parse_uint(const struct arg *arg)
140 long int rawval;
141 char *endptr;
143 rawval = strtol(arg->val, &endptr, 10);
145 if (arg->val[0] != '\0' && endptr[0] == '\0')
147 if (rawval >= 0 && rawval <= UINT_MAX)
148 return rawval;
150 die("Option %s: Value %ld out of range for unsigned int\n",
151 arg->name, rawval);
154 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
155 return 0;
159 int arg_parse_int(const struct arg *arg)
161 long int rawval;
162 char *endptr;
164 rawval = strtol(arg->val, &endptr, 10);
166 if (arg->val[0] != '\0' && endptr[0] == '\0')
168 if (rawval >= INT_MIN && rawval <= INT_MAX)
169 return rawval;
171 die("Option %s: Value %ld out of range for signed int\n",
172 arg->name, rawval);
175 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
176 return 0;
180 struct vpx_rational
182 int num; /**< fraction numerator */
183 int den; /**< fraction denominator */
185 struct vpx_rational arg_parse_rational(const struct arg *arg)
187 long int rawval;
188 char *endptr;
189 struct vpx_rational rat;
191 /* parse numerator */
192 rawval = strtol(arg->val, &endptr, 10);
194 if (arg->val[0] != '\0' && endptr[0] == '/')
196 if (rawval >= INT_MIN && rawval <= INT_MAX)
197 rat.num = rawval;
198 else die("Option %s: Value %ld out of range for signed int\n",
199 arg->name, rawval);
201 else die("Option %s: Expected / at '%c'\n", arg->name, *endptr);
203 /* parse denominator */
204 rawval = strtol(endptr + 1, &endptr, 10);
206 if (arg->val[0] != '\0' && endptr[0] == '\0')
208 if (rawval >= INT_MIN && rawval <= INT_MAX)
209 rat.den = rawval;
210 else die("Option %s: Value %ld out of range for signed int\n",
211 arg->name, rawval);
213 else die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
215 return rat;