• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
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  */
10 
11 #include <stdlib.h>
12 #include <string.h>
13 #include <limits.h>
14 #include "args.h"
15 
16 #include "vpx/vpx_integer.h"
17 #include "vpx_ports/msvc.h"
18 
19 #if defined(__GNUC__) && __GNUC__
20 extern void die(const char *fmt, ...) __attribute__((noreturn));
21 #else
22 extern void die(const char *fmt, ...);
23 #endif
24 
arg_init(char ** argv)25 struct arg arg_init(char **argv) {
26   struct arg a;
27 
28   a.argv = argv;
29   a.argv_step = 1;
30   a.name = NULL;
31   a.val = NULL;
32   a.def = NULL;
33   return a;
34 }
35 
arg_match(struct arg * arg_,const struct arg_def * def,char ** argv)36 int arg_match(struct arg *arg_, const struct arg_def *def, char **argv) {
37   struct arg arg;
38 
39   if (!argv[0] || argv[0][0] != '-') return 0;
40 
41   arg = arg_init(argv);
42 
43   if (def->short_name && strlen(arg.argv[0]) == strlen(def->short_name) + 1 &&
44       !strcmp(arg.argv[0] + 1, def->short_name)) {
45     arg.name = arg.argv[0] + 1;
46     arg.val = def->has_val ? arg.argv[1] : NULL;
47     arg.argv_step = def->has_val ? 2 : 1;
48   } else if (def->long_name) {
49     const size_t name_len = strlen(def->long_name);
50 
51     if (strlen(arg.argv[0]) >= name_len + 2 && arg.argv[0][1] == '-' &&
52         !strncmp(arg.argv[0] + 2, def->long_name, name_len) &&
53         (arg.argv[0][name_len + 2] == '=' ||
54          arg.argv[0][name_len + 2] == '\0')) {
55       arg.name = arg.argv[0] + 2;
56       arg.val = arg.name[name_len] == '=' ? arg.name + name_len + 1 : NULL;
57       arg.argv_step = 1;
58     }
59   }
60 
61   if (arg.name && !arg.val && def->has_val)
62     die("Error: option %s requires argument.\n", arg.name);
63 
64   if (arg.name && arg.val && !def->has_val)
65     die("Error: option %s requires no argument.\n", arg.name);
66 
67   if (arg.name && (arg.val || !def->has_val)) {
68     arg.def = def;
69     *arg_ = arg;
70     return 1;
71   }
72 
73   return 0;
74 }
75 
arg_next(struct arg * arg)76 const char *arg_next(struct arg *arg) {
77   if (arg->argv[0]) arg->argv += arg->argv_step;
78 
79   return *arg->argv;
80 }
81 
argv_dup(int argc,const char ** argv)82 char **argv_dup(int argc, const char **argv) {
83   char **new_argv = malloc((argc + 1) * sizeof(*argv));
84 
85   memcpy(new_argv, argv, argc * sizeof(*argv));
86   new_argv[argc] = NULL;
87   return new_argv;
88 }
89 
arg_show_usage(FILE * fp,const struct arg_def * const * defs)90 void arg_show_usage(FILE *fp, const struct arg_def *const *defs) {
91   char option_text[40] = { 0 };
92 
93   for (; *defs; defs++) {
94     const struct arg_def *def = *defs;
95     char *short_val = def->has_val ? " <arg>" : "";
96     char *long_val = def->has_val ? "=<arg>" : "";
97 
98     if (def->short_name && def->long_name) {
99       char *comma = def->has_val ? "," : ",      ";
100 
101       snprintf(option_text, 37, "-%s%s%s --%s%6s", def->short_name, short_val,
102                comma, def->long_name, long_val);
103     } else if (def->short_name)
104       snprintf(option_text, 37, "-%s%s", def->short_name, short_val);
105     else if (def->long_name)
106       snprintf(option_text, 37, "          --%s%s", def->long_name, long_val);
107 
108     fprintf(fp, "  %-37s\t%s\n", option_text, def->desc);
109 
110     if (def->enums) {
111       const struct arg_enum_list *listptr;
112 
113       fprintf(fp, "  %-37s\t  ", "");
114 
115       for (listptr = def->enums; listptr->name; listptr++)
116         fprintf(fp, "%s%s", listptr->name, listptr[1].name ? ", " : "\n");
117     }
118   }
119 }
120 
arg_parse_uint(const struct arg * arg)121 unsigned int arg_parse_uint(const struct arg *arg) {
122   uint32_t rawval;
123   char *endptr;
124 
125   rawval = (uint32_t)strtoul(arg->val, &endptr, 10);
126 
127   if (arg->val[0] != '\0' && endptr[0] == '\0') {
128     if (rawval <= UINT_MAX) return rawval;
129 
130     die("Option %s: Value %ld out of range for unsigned int\n", arg->name,
131         rawval);
132   }
133 
134   die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
135   return 0;
136 }
137 
arg_parse_int(const struct arg * arg)138 int arg_parse_int(const struct arg *arg) {
139   int32_t rawval;
140   char *endptr;
141 
142   rawval = (int32_t)strtol(arg->val, &endptr, 10);
143 
144   if (arg->val[0] != '\0' && endptr[0] == '\0') {
145     if (rawval >= INT_MIN && rawval <= INT_MAX) return (int)rawval;
146 
147     die("Option %s: Value %ld out of range for signed int\n", arg->name,
148         rawval);
149   }
150 
151   die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
152   return 0;
153 }
154 
155 struct vpx_rational {
156   int num; /**< fraction numerator */
157   int den; /**< fraction denominator */
158 };
arg_parse_rational(const struct arg * arg)159 struct vpx_rational arg_parse_rational(const struct arg *arg) {
160   long int rawval;
161   char *endptr;
162   struct vpx_rational rat;
163 
164   /* parse numerator */
165   rawval = strtol(arg->val, &endptr, 10);
166 
167   if (arg->val[0] != '\0' && endptr[0] == '/') {
168     if (rawval >= INT_MIN && rawval <= INT_MAX)
169       rat.num = (int)rawval;
170     else
171       die("Option %s: Value %ld out of range for signed int\n", arg->name,
172           rawval);
173   } else
174     die("Option %s: Expected / at '%c'\n", arg->name, *endptr);
175 
176   /* parse denominator */
177   rawval = strtol(endptr + 1, &endptr, 10);
178 
179   if (arg->val[0] != '\0' && endptr[0] == '\0') {
180     if (rawval >= INT_MIN && rawval <= INT_MAX)
181       rat.den = (int)rawval;
182     else
183       die("Option %s: Value %ld out of range for signed int\n", arg->name,
184           rawval);
185   } else
186     die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
187 
188   return rat;
189 }
190 
arg_parse_enum(const struct arg * arg)191 int arg_parse_enum(const struct arg *arg) {
192   const struct arg_enum_list *listptr;
193   long int rawval;
194   char *endptr;
195 
196   /* First see if the value can be parsed as a raw value */
197   rawval = strtol(arg->val, &endptr, 10);
198   if (arg->val[0] != '\0' && endptr[0] == '\0') {
199     /* Got a raw value, make sure it's valid */
200     for (listptr = arg->def->enums; listptr->name; listptr++)
201       if (listptr->val == rawval) return (int)rawval;
202   }
203 
204   /* Next see if it can be parsed as a string */
205   for (listptr = arg->def->enums; listptr->name; listptr++)
206     if (!strcmp(arg->val, listptr->name)) return listptr->val;
207 
208   die("Option %s: Invalid value '%s'\n", arg->name, arg->val);
209   return 0;
210 }
211 
arg_parse_enum_or_int(const struct arg * arg)212 int arg_parse_enum_or_int(const struct arg *arg) {
213   if (arg->def->enums) return arg_parse_enum(arg);
214   return arg_parse_int(arg);
215 }
216