2 * Copyright (c) 2010 The WebM 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.
18 #define snprintf _snprintf
21 #if defined(__GNUC__) && __GNUC__
22 extern void die(const char *fmt
, ...) __attribute__((noreturn
));
24 extern void die(const char *fmt
, ...);
28 struct arg
arg_init(char **argv
) {
39 int arg_match(struct arg
*arg_
, const struct arg_def
*def
, char **argv
) {
42 if (!argv
[0] || argv
[0][0] != '-')
48 && strlen(arg
.argv
[0]) == strlen(def
->short_name
) + 1
49 && !strcmp(arg
.argv
[0] + 1, def
->short_name
)) {
51 arg
.name
= arg
.argv
[0] + 1;
52 arg
.val
= def
->has_val
? arg
.argv
[1] : NULL
;
53 arg
.argv_step
= def
->has_val
? 2 : 1;
54 } else if (def
->long_name
) {
55 const size_t name_len
= strlen(def
->long_name
);
57 if (strlen(arg
.argv
[0]) >= name_len
+ 2
58 && arg
.argv
[0][1] == '-'
59 && !strncmp(arg
.argv
[0] + 2, def
->long_name
, name_len
)
60 && (arg
.argv
[0][name_len
+ 2] == '='
61 || arg
.argv
[0][name_len
+ 2] == '\0')) {
63 arg
.name
= arg
.argv
[0] + 2;
64 arg
.val
= arg
.name
[name_len
] == '=' ? arg
.name
+ name_len
+ 1 : NULL
;
69 if (arg
.name
&& !arg
.val
&& def
->has_val
)
70 die("Error: option %s requires argument.\n", arg
.name
);
72 if (arg
.name
&& arg
.val
&& !def
->has_val
)
73 die("Error: option %s requires no argument.\n", arg
.name
);
76 && (arg
.val
|| !def
->has_val
)) {
86 const char *arg_next(struct arg
*arg
) {
88 arg
->argv
+= arg
->argv_step
;
94 char **argv_dup(int argc
, const char **argv
) {
95 char **new_argv
= malloc((argc
+ 1) * sizeof(*argv
));
97 memcpy(new_argv
, argv
, argc
* sizeof(*argv
));
98 new_argv
[argc
] = NULL
;
103 void arg_show_usage(FILE *fp
, const struct arg_def
*const *defs
) {
104 char option_text
[40] = {0};
106 for (; *defs
; defs
++) {
107 const struct arg_def
*def
= *defs
;
108 char *short_val
= def
->has_val
? " <arg>" : "";
109 char *long_val
= def
->has_val
? "=<arg>" : "";
111 if (def
->short_name
&& def
->long_name
) {
112 char *comma
= def
->has_val
? "," : ", ";
114 snprintf(option_text
, 37, "-%s%s%s --%s%6s",
115 def
->short_name
, short_val
, comma
,
116 def
->long_name
, long_val
);
117 } else if (def
->short_name
)
118 snprintf(option_text
, 37, "-%s%s",
119 def
->short_name
, short_val
);
120 else if (def
->long_name
)
121 snprintf(option_text
, 37, " --%s%s",
122 def
->long_name
, long_val
);
124 fprintf(fp
, " %-37s\t%s\n", option_text
, def
->desc
);
127 const struct arg_enum_list
*listptr
;
129 fprintf(fp
, " %-37s\t ", "");
131 for (listptr
= def
->enums
; listptr
->name
; listptr
++)
132 fprintf(fp
, "%s%s", listptr
->name
,
133 listptr
[1].name
? ", " : "\n");
139 unsigned int arg_parse_uint(const struct arg
*arg
) {
143 rawval
= strtol(arg
->val
, &endptr
, 10);
145 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
) {
162 rawval
= strtol(arg
->val
, &endptr
, 10);
164 if (arg
->val
[0] != '\0' && endptr
[0] == '\0') {
165 if (rawval
>= INT_MIN
&& rawval
<= INT_MAX
)
168 die("Option %s: Value %ld out of range for signed int\n",
172 die("Option %s: Invalid character '%c'\n", arg
->name
, *endptr
);
177 struct vpx_rational
{
178 int num
; /**< fraction numerator */
179 int den
; /**< fraction denominator */
181 struct vpx_rational
arg_parse_rational(const struct arg
*arg
) {
184 struct vpx_rational rat
;
186 /* parse numerator */
187 rawval
= strtol(arg
->val
, &endptr
, 10);
189 if (arg
->val
[0] != '\0' && endptr
[0] == '/') {
190 if (rawval
>= INT_MIN
&& rawval
<= INT_MAX
)
192 else die("Option %s: Value %ld out of range for signed int\n",
194 } else die("Option %s: Expected / at '%c'\n", arg
->name
, *endptr
);
196 /* parse denominator */
197 rawval
= strtol(endptr
+ 1, &endptr
, 10);
199 if (arg
->val
[0] != '\0' && endptr
[0] == '\0') {
200 if (rawval
>= INT_MIN
&& rawval
<= INT_MAX
)
202 else die("Option %s: Value %ld out of range for signed int\n",
204 } else die("Option %s: Invalid character '%c'\n", arg
->name
, *endptr
);
210 int arg_parse_enum(const struct arg
*arg
) {
211 const struct arg_enum_list
*listptr
;
215 /* First see if the value can be parsed as a raw value */
216 rawval
= strtol(arg
->val
, &endptr
, 10);
217 if (arg
->val
[0] != '\0' && endptr
[0] == '\0') {
218 /* Got a raw value, make sure it's valid */
219 for (listptr
= arg
->def
->enums
; listptr
->name
; listptr
++)
220 if (listptr
->val
== rawval
)
224 /* Next see if it can be parsed as a string */
225 for (listptr
= arg
->def
->enums
; listptr
->name
; listptr
++)
226 if (!strcmp(arg
->val
, listptr
->name
))
229 die("Option %s: Invalid value '%s'\n", arg
->name
, arg
->val
);
234 int arg_parse_enum_or_int(const struct arg
*arg
) {
236 return arg_parse_enum(arg
);
237 return arg_parse_int(arg
);