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
)
40 int arg_match(struct arg
*arg_
, const struct arg_def
*def
, char **argv
)
44 if (!argv
[0] || argv
[0][0] != '-')
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
;
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
);
82 && (arg
.val
|| !def
->has_val
))
93 const char *arg_next(struct arg
*arg
)
96 arg
->argv
+= arg
->argv_step
;
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
;
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
)
124 char *comma
= def
->has_val
? "," : ", ";
126 snprintf(option_text
, 37, "-%s%s%s --%s%6s",
127 def
->short_name
, short_val
, comma
,
128 def
->long_name
, long_val
);
130 else if (def
->short_name
)
131 snprintf(option_text
, 37, "-%s%s",
132 def
->short_name
, short_val
);
133 else if (def
->long_name
)
134 snprintf(option_text
, 37, " --%s%s",
135 def
->long_name
, long_val
);
137 fprintf(fp
, " %-37s\t%s\n", option_text
, def
->desc
);
142 unsigned int arg_parse_uint(const struct arg
*arg
)
147 rawval
= strtol(arg
->val
, &endptr
, 10);
149 if (arg
->val
[0] != '\0' && endptr
[0] == '\0')
151 if (rawval
>= 0 && rawval
<= UINT_MAX
)
154 die("Option %s: Value %ld out of range for unsigned int\n",
158 die("Option %s: Invalid character '%c'\n", arg
->name
, *endptr
);
163 int arg_parse_int(const struct arg
*arg
)
168 rawval
= strtol(arg
->val
, &endptr
, 10);
170 if (arg
->val
[0] != '\0' && endptr
[0] == '\0')
172 if (rawval
>= INT_MIN
&& rawval
<= INT_MAX
)
175 die("Option %s: Value %ld out of range for signed int\n",
179 die("Option %s: Invalid character '%c'\n", arg
->name
, *endptr
);
186 int num
; /**< fraction numerator */
187 int den
; /**< fraction denominator */
189 struct vpx_rational
arg_parse_rational(const struct arg
*arg
)
193 struct vpx_rational rat
;
195 /* parse numerator */
196 rawval
= strtol(arg
->val
, &endptr
, 10);
198 if (arg
->val
[0] != '\0' && endptr
[0] == '/')
200 if (rawval
>= INT_MIN
&& rawval
<= INT_MAX
)
202 else die("Option %s: Value %ld out of range for signed int\n",
205 else die("Option %s: Expected / at '%c'\n", arg
->name
, *endptr
);
207 /* parse denominator */
208 rawval
= strtol(endptr
+ 1, &endptr
, 10);
210 if (arg
->val
[0] != '\0' && endptr
[0] == '\0')
212 if (rawval
>= INT_MIN
&& rawval
<= INT_MAX
)
214 else die("Option %s: Value %ld out of range for signed int\n",
217 else die("Option %s: Invalid character '%c'\n", arg
->name
, *endptr
);