• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include "common/args.h"
13 
14 #include <stdlib.h>
15 #include <string.h>
16 #include <limits.h>
17 
18 #include "aom/aom_integer.h"
19 #include "aom_ports/msvc.h"
20 
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
26 
arg_init(char ** argv)27 struct arg arg_init(char **argv) {
28   struct arg a;
29 
30   a.argv = argv;
31   a.argv_step = 1;
32   a.name = NULL;
33   a.val = NULL;
34   a.def = NULL;
35   return a;
36 }
37 
ignore_front_spaces(const char * str)38 char *ignore_front_spaces(const char *str) {
39   while (str[0] == ' ' || str[0] == '\t') ++str;
40   return (char *)str;
41 }
42 
ignore_end_spaces(char * str)43 void ignore_end_spaces(char *str) {
44   char *end = str + strlen(str);
45   while (end > str && (end[0] == ' ' || end[0] == '\t' || end[0] == '\n' ||
46                        end[0] == '\r' || end[0] == '\0'))
47     --end;
48   if (end >= str) end[1] = '\0';
49 }
50 
arg_cfg(int * argc,char *** argv,const char * file)51 int arg_cfg(int *argc, char ***argv, const char *file) {
52   char **argv_local = (char **)*argv;
53   char **argv_org = (char **)*argv;
54   char line[1024 * 10];
55   FILE *f = fopen(file, "r");
56   if (!f) return 1;
57 
58   while (fgets(line, sizeof(line) - 1, f)) {
59     char *actual_line = ignore_front_spaces(line);
60     char *left, *right, *comment;
61     size_t length = strlen(actual_line);
62 
63     if (length == 0 || actual_line[0] == '#') continue;
64     right = strchr(actual_line, ':');
65     if (right == NULL) continue;
66     right[0] = '\0';
67 
68     left = ignore_front_spaces(actual_line);
69     right = ignore_front_spaces(right + 1);
70 
71     comment = strchr(right, '#');
72     if (comment != NULL) comment[0] = '\0';
73 
74     ignore_end_spaces(left);
75     ignore_end_spaces(right);
76 
77     char **new_args = argv_dup(*argc, (const char **)argv_local);
78     char *new_line = (char *)malloc(sizeof(*new_line) * 128);
79 
80     if (argv_local != argv_org) free(argv_local);
81 
82     if (!strcmp(right, "ON"))
83       snprintf(new_line, sizeof(*new_line) * 128, "--%s", left);
84     else
85       snprintf(new_line, sizeof(*new_line) * 128, "--%s=%s", left, right);
86 
87     new_args[(*argc) - 1] = new_args[(*argc) - 2];
88     new_args[(*argc) - 2] = new_line;
89     argv_local = new_args;
90     *argv = new_args;
91     (*argc)++;
92   }
93   fclose(f);
94   return 0;
95 }
96 
arg_match(struct arg * arg_,const struct arg_def * def,char ** argv)97 int arg_match(struct arg *arg_, const struct arg_def *def, char **argv) {
98   struct arg arg;
99 
100   if (!argv[0] || argv[0][0] != '-') return 0;
101 
102   arg = arg_init(argv);
103 
104   if (def->short_name && strlen(arg.argv[0]) == strlen(def->short_name) + 1 &&
105       !strcmp(arg.argv[0] + 1, def->short_name)) {
106     arg.name = arg.argv[0] + 1;
107     arg.val = def->has_val ? arg.argv[1] : NULL;
108     arg.argv_step = def->has_val ? 2 : 1;
109   } else if (def->long_name) {
110     const size_t name_len = strlen(def->long_name);
111 
112     if (strlen(arg.argv[0]) >= name_len + 2 && arg.argv[0][1] == '-' &&
113         !strncmp(arg.argv[0] + 2, def->long_name, name_len) &&
114         (arg.argv[0][name_len + 2] == '=' ||
115          arg.argv[0][name_len + 2] == '\0')) {
116       arg.name = arg.argv[0] + 2;
117       arg.val = arg.name[name_len] == '=' ? arg.name + name_len + 1 : NULL;
118       arg.argv_step = 1;
119     }
120   }
121 
122   if (arg.name && !arg.val && def->has_val)
123     die("Error: option %s requires argument.\n", arg.name);
124 
125   if (arg.name && arg.val && !def->has_val)
126     die("Error: option %s requires no argument.\n", arg.name);
127 
128   if (arg.name && (arg.val || !def->has_val)) {
129     arg.def = def;
130     *arg_ = arg;
131     return 1;
132   }
133 
134   return 0;
135 }
136 
arg_next(struct arg * arg)137 const char *arg_next(struct arg *arg) {
138   if (arg->argv[0]) arg->argv += arg->argv_step;
139 
140   return *arg->argv;
141 }
142 
argv_dup(int argc,const char ** argv)143 char **argv_dup(int argc, const char **argv) {
144   char **new_argv = malloc((argc + 1) * sizeof(*argv));
145 
146   memcpy(new_argv, argv, argc * sizeof(*argv));
147   new_argv[argc] = NULL;
148   return new_argv;
149 }
150 
arg_show_usage(FILE * fp,const struct arg_def * const * defs)151 void arg_show_usage(FILE *fp, const struct arg_def *const *defs) {
152   char option_text[40] = { 0 };
153 
154   for (; *defs; defs++) {
155     const struct arg_def *def = *defs;
156     char *short_val = def->has_val ? " <arg>" : "";
157     char *long_val = def->has_val ? "=<arg>" : "";
158 
159     if (def->short_name && def->long_name) {
160       char *comma = def->has_val ? "," : ",      ";
161 
162       snprintf(option_text, 37, "-%s%s%s --%s%6s", def->short_name, short_val,
163                comma, def->long_name, long_val);
164     } else if (def->short_name)
165       snprintf(option_text, 37, "-%s%s", def->short_name, short_val);
166     else if (def->long_name)
167       snprintf(option_text, 37, "          --%s%s", def->long_name, long_val);
168 
169     fprintf(fp, "  %-37s\t%s\n", option_text, def->desc);
170 
171     if (def->enums) {
172       const struct arg_enum_list *listptr;
173 
174       fprintf(fp, "  %-37s\t  ", "");
175 
176       for (listptr = def->enums; listptr->name; listptr++)
177         fprintf(fp, "%s%s", listptr->name, listptr[1].name ? ", " : "\n");
178     }
179   }
180 }
181 
arg_parse_uint(const struct arg * arg)182 unsigned int arg_parse_uint(const struct arg *arg) {
183   char *endptr;
184   const unsigned long rawval = strtoul(arg->val, &endptr, 10);  // NOLINT
185 
186   if (arg->val[0] != '\0' && endptr[0] == '\0') {
187     if (rawval <= UINT_MAX) return (unsigned int)rawval;
188 
189     die("Option %s: Value %lu out of range for unsigned int\n", arg->name,
190         rawval);
191   }
192 
193   die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
194   return 0;
195 }
196 
arg_parse_int(const struct arg * arg)197 int arg_parse_int(const struct arg *arg) {
198   char *endptr;
199   const long rawval = strtol(arg->val, &endptr, 10);  // NOLINT
200 
201   if (arg->val[0] != '\0' && endptr[0] == '\0') {
202     if (rawval >= INT_MIN && rawval <= INT_MAX) return (int)rawval;
203 
204     die("Option %s: Value %ld out of range for signed int\n", arg->name,
205         rawval);
206   }
207 
208   die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
209   return 0;
210 }
211 
212 struct aom_rational {
213   int num; /**< fraction numerator */
214   int den; /**< fraction denominator */
215 };
arg_parse_rational(const struct arg * arg)216 struct aom_rational arg_parse_rational(const struct arg *arg) {
217   long int rawval;
218   char *endptr;
219   struct aom_rational rat;
220 
221   /* parse numerator */
222   rawval = strtol(arg->val, &endptr, 10);
223 
224   if (arg->val[0] != '\0' && endptr[0] == '/') {
225     if (rawval >= INT_MIN && rawval <= INT_MAX)
226       rat.num = (int)rawval;
227     else
228       die("Option %s: Value %ld out of range for signed int\n", arg->name,
229           rawval);
230   } else
231     die("Option %s: Expected / at '%c'\n", arg->name, *endptr);
232 
233   /* parse denominator */
234   rawval = strtol(endptr + 1, &endptr, 10);
235 
236   if (arg->val[0] != '\0' && endptr[0] == '\0') {
237     if (rawval >= INT_MIN && rawval <= INT_MAX)
238       rat.den = (int)rawval;
239     else
240       die("Option %s: Value %ld out of range for signed int\n", arg->name,
241           rawval);
242   } else
243     die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
244 
245   return rat;
246 }
247 
arg_parse_enum(const struct arg * arg)248 int arg_parse_enum(const struct arg *arg) {
249   const struct arg_enum_list *listptr;
250   long int rawval;
251   char *endptr;
252 
253   /* First see if the value can be parsed as a raw value */
254   rawval = strtol(arg->val, &endptr, 10);
255   if (arg->val[0] != '\0' && endptr[0] == '\0') {
256     /* Got a raw value, make sure it's valid */
257     for (listptr = arg->def->enums; listptr->name; listptr++)
258       if (listptr->val == rawval) return (int)rawval;
259   }
260 
261   /* Next see if it can be parsed as a string */
262   for (listptr = arg->def->enums; listptr->name; listptr++)
263     if (!strcmp(arg->val, listptr->name)) return listptr->val;
264 
265   die("Option %s: Invalid value '%s'\n", arg->name, arg->val);
266   return 0;
267 }
268 
arg_parse_enum_or_int(const struct arg * arg)269 int arg_parse_enum_or_int(const struct arg *arg) {
270   if (arg->def->enums) return arg_parse_enum(arg);
271   return arg_parse_int(arg);
272 }
273 
274 // parse a comma separated list of at most n integers
275 // return the number of elements in the list
arg_parse_list(const struct arg * arg,int * list,int n)276 int arg_parse_list(const struct arg *arg, int *list, int n) {
277   const char *ptr = arg->val;
278   char *endptr;
279   int i = 0;
280 
281   while (ptr[0] != '\0') {
282     int32_t rawval = (int32_t)strtol(ptr, &endptr, 10);
283     if (rawval < INT_MIN || rawval > INT_MAX) {
284       die("Option %s: Value %ld out of range for signed int\n", arg->name,
285           rawval);
286     } else if (i >= n) {
287       die("Option %s: List has more than %d entries\n", arg->name, n);
288     } else if (*endptr == ',') {
289       endptr++;
290     } else if (*endptr != '\0') {
291       die("Option %s: Bad list separator '%c'\n", arg->name, *endptr);
292     }
293     list[i++] = (int)rawval;
294     ptr = endptr;
295   }
296   return i;
297 }
298