args.c 6.78 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar's avatar
John Koleszar committed
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
John Koleszar's avatar
John Koleszar committed
10 11 12 13 14 15 16
 */

#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "args.h"

17
#include "aom/aom_integer.h"
18
#include "aom_ports/msvc.h"
John Koleszar's avatar
John Koleszar committed
19 20 21 22 23 24 25

#if defined(__GNUC__) && __GNUC__
extern void die(const char *fmt, ...) __attribute__((noreturn));
#else
extern void die(const char *fmt, ...);
#endif

John Koleszar's avatar
John Koleszar committed
26 27 28
struct arg arg_init(char **argv) {
  struct arg a;

clang-format's avatar
clang-format committed
29
  a.argv = argv;
John Koleszar's avatar
John Koleszar committed
30
  a.argv_step = 1;
clang-format's avatar
clang-format committed
31 32 33
  a.name = NULL;
  a.val = NULL;
  a.def = NULL;
John Koleszar's avatar
John Koleszar committed
34
  return a;
John Koleszar's avatar
John Koleszar committed
35 36
}

John Koleszar's avatar
John Koleszar committed
37 38
int arg_match(struct arg *arg_, const struct arg_def *def, char **argv) {
  struct arg arg;
John Koleszar's avatar
John Koleszar committed
39

clang-format's avatar
clang-format committed
40
  if (!argv[0] || argv[0][0] != '-') return 0;
John Koleszar's avatar
John Koleszar committed
41

John Koleszar's avatar
John Koleszar committed
42
  arg = arg_init(argv);
John Koleszar's avatar
John Koleszar committed
43

clang-format's avatar
clang-format committed
44 45
  if (def->short_name && strlen(arg.argv[0]) == strlen(def->short_name) + 1 &&
      !strcmp(arg.argv[0] + 1, def->short_name)) {
John Koleszar's avatar
John Koleszar committed
46 47 48 49
    arg.name = arg.argv[0] + 1;
    arg.val = def->has_val ? arg.argv[1] : NULL;
    arg.argv_step = def->has_val ? 2 : 1;
  } else if (def->long_name) {
John Koleszar's avatar
John Koleszar committed
50
    const size_t name_len = strlen(def->long_name);
John Koleszar's avatar
John Koleszar committed
51

clang-format's avatar
clang-format committed
52 53 54 55
    if (strlen(arg.argv[0]) >= name_len + 2 && arg.argv[0][1] == '-' &&
        !strncmp(arg.argv[0] + 2, def->long_name, name_len) &&
        (arg.argv[0][name_len + 2] == '=' ||
         arg.argv[0][name_len + 2] == '\0')) {
John Koleszar's avatar
John Koleszar committed
56 57 58
      arg.name = arg.argv[0] + 2;
      arg.val = arg.name[name_len] == '=' ? arg.name + name_len + 1 : NULL;
      arg.argv_step = 1;
John Koleszar's avatar
John Koleszar committed
59
    }
John Koleszar's avatar
John Koleszar committed
60
  }
John Koleszar's avatar
John Koleszar committed
61

John Koleszar's avatar
John Koleszar committed
62 63
  if (arg.name && !arg.val && def->has_val)
    die("Error: option %s requires argument.\n", arg.name);
John Koleszar's avatar
John Koleszar committed
64

John Koleszar's avatar
John Koleszar committed
65 66
  if (arg.name && arg.val && !def->has_val)
    die("Error: option %s requires no argument.\n", arg.name);
John Koleszar's avatar
John Koleszar committed
67

clang-format's avatar
clang-format committed
68
  if (arg.name && (arg.val || !def->has_val)) {
John Koleszar's avatar
John Koleszar committed
69 70 71 72
    arg.def = def;
    *arg_ = arg;
    return 1;
  }
John Koleszar's avatar
John Koleszar committed
73

John Koleszar's avatar
John Koleszar committed
74
  return 0;
John Koleszar's avatar
John Koleszar committed
75 76
}

John Koleszar's avatar
John Koleszar committed
77
const char *arg_next(struct arg *arg) {
clang-format's avatar
clang-format committed
78
  if (arg->argv[0]) arg->argv += arg->argv_step;
John Koleszar's avatar
John Koleszar committed
79

John Koleszar's avatar
John Koleszar committed
80
  return *arg->argv;
John Koleszar's avatar
John Koleszar committed
81 82
}

John Koleszar's avatar
John Koleszar committed
83 84
char **argv_dup(int argc, const char **argv) {
  char **new_argv = malloc((argc + 1) * sizeof(*argv));
John Koleszar's avatar
John Koleszar committed
85

John Koleszar's avatar
John Koleszar committed
86 87 88
  memcpy(new_argv, argv, argc * sizeof(*argv));
  new_argv[argc] = NULL;
  return new_argv;
John Koleszar's avatar
John Koleszar committed
89 90
}

John Koleszar's avatar
John Koleszar committed
91
void arg_show_usage(FILE *fp, const struct arg_def *const *defs) {
clang-format's avatar
clang-format committed
92
  char option_text[40] = { 0 };
John Koleszar's avatar
John Koleszar committed
93

John Koleszar's avatar
John Koleszar committed
94 95 96 97
  for (; *defs; defs++) {
    const struct arg_def *def = *defs;
    char *short_val = def->has_val ? " <arg>" : "";
    char *long_val = def->has_val ? "=<arg>" : "";
John Koleszar's avatar
John Koleszar committed
98

John Koleszar's avatar
John Koleszar committed
99 100
    if (def->short_name && def->long_name) {
      char *comma = def->has_val ? "," : ",      ";
101

clang-format's avatar
clang-format committed
102 103
      snprintf(option_text, 37, "-%s%s%s --%s%6s", def->short_name, short_val,
               comma, def->long_name, long_val);
John Koleszar's avatar
John Koleszar committed
104
    } else if (def->short_name)
clang-format's avatar
clang-format committed
105
      snprintf(option_text, 37, "-%s%s", def->short_name, short_val);
John Koleszar's avatar
John Koleszar committed
106
    else if (def->long_name)
clang-format's avatar
clang-format committed
107
      snprintf(option_text, 37, "          --%s%s", def->long_name, long_val);
John Koleszar's avatar
John Koleszar committed
108

John Koleszar's avatar
John Koleszar committed
109
    fprintf(fp, "  %-37s\t%s\n", option_text, def->desc);
John Koleszar's avatar
John Koleszar committed
110

John Koleszar's avatar
John Koleszar committed
111 112
    if (def->enums) {
      const struct arg_enum_list *listptr;
John Koleszar's avatar
John Koleszar committed
113

John Koleszar's avatar
John Koleszar committed
114
      fprintf(fp, "  %-37s\t  ", "");
John Koleszar's avatar
John Koleszar committed
115

John Koleszar's avatar
John Koleszar committed
116
      for (listptr = def->enums; listptr->name; listptr++)
clang-format's avatar
clang-format committed
117
        fprintf(fp, "%s%s", listptr->name, listptr[1].name ? ", " : "\n");
John Koleszar's avatar
John Koleszar committed
118
    }
John Koleszar's avatar
John Koleszar committed
119
  }
John Koleszar's avatar
John Koleszar committed
120 121
}

John Koleszar's avatar
John Koleszar committed
122
unsigned int arg_parse_uint(const struct arg *arg) {
clang-format's avatar
clang-format committed
123
  char *endptr;
124
  const unsigned long rawval = strtoul(arg->val, &endptr, 10);  // NOLINT
John Koleszar's avatar
John Koleszar committed
125

John Koleszar's avatar
John Koleszar committed
126
  if (arg->val[0] != '\0' && endptr[0] == '\0') {
James Zern's avatar
James Zern committed
127
    if (rawval <= UINT_MAX) return (unsigned int)rawval;
John Koleszar's avatar
John Koleszar committed
128

129
    die("Option %s: Value %lu out of range for unsigned int\n", arg->name,
clang-format's avatar
clang-format committed
130
        rawval);
John Koleszar's avatar
John Koleszar committed
131
  }
John Koleszar's avatar
John Koleszar committed
132

John Koleszar's avatar
John Koleszar committed
133 134
  die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
  return 0;
John Koleszar's avatar
John Koleszar committed
135 136
}

John Koleszar's avatar
John Koleszar committed
137
int arg_parse_int(const struct arg *arg) {
clang-format's avatar
clang-format committed
138
  char *endptr;
139
  const long rawval = strtol(arg->val, &endptr, 10);  // NOLINT
John Koleszar's avatar
John Koleszar committed
140

John Koleszar's avatar
John Koleszar committed
141
  if (arg->val[0] != '\0' && endptr[0] == '\0') {
James Zern's avatar
James Zern committed
142
    if (rawval >= INT_MIN && rawval <= INT_MAX) return (int)rawval;
John Koleszar's avatar
John Koleszar committed
143

clang-format's avatar
clang-format committed
144 145
    die("Option %s: Value %ld out of range for signed int\n", arg->name,
        rawval);
John Koleszar's avatar
John Koleszar committed
146
  }
John Koleszar's avatar
John Koleszar committed
147

John Koleszar's avatar
John Koleszar committed
148 149
  die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
  return 0;
John Koleszar's avatar
John Koleszar committed
150 151
}

152
struct aom_rational {
John Koleszar's avatar
John Koleszar committed
153 154
  int num; /**< fraction numerator */
  int den; /**< fraction denominator */
John Koleszar's avatar
John Koleszar committed
155
};
156
struct aom_rational arg_parse_rational(const struct arg *arg) {
clang-format's avatar
clang-format committed
157 158
  long int rawval;
  char *endptr;
159
  struct aom_rational rat;
John Koleszar's avatar
John Koleszar committed
160 161 162 163 164 165

  /* parse numerator */
  rawval = strtol(arg->val, &endptr, 10);

  if (arg->val[0] != '\0' && endptr[0] == '/') {
    if (rawval >= INT_MIN && rawval <= INT_MAX)
James Zern's avatar
James Zern committed
166
      rat.num = (int)rawval;
clang-format's avatar
clang-format committed
167 168 169 170 171
    else
      die("Option %s: Value %ld out of range for signed int\n", arg->name,
          rawval);
  } else
    die("Option %s: Expected / at '%c'\n", arg->name, *endptr);
John Koleszar's avatar
John Koleszar committed
172 173 174 175 176 177

  /* parse denominator */
  rawval = strtol(endptr + 1, &endptr, 10);

  if (arg->val[0] != '\0' && endptr[0] == '\0') {
    if (rawval >= INT_MIN && rawval <= INT_MAX)
James Zern's avatar
James Zern committed
178
      rat.den = (int)rawval;
clang-format's avatar
clang-format committed
179 180 181 182 183
    else
      die("Option %s: Value %ld out of range for signed int\n", arg->name,
          rawval);
  } else
    die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
John Koleszar's avatar
John Koleszar committed
184 185

  return rat;
John Koleszar's avatar
John Koleszar committed
186
}
John Koleszar's avatar
John Koleszar committed
187

John Koleszar's avatar
John Koleszar committed
188 189
int arg_parse_enum(const struct arg *arg) {
  const struct arg_enum_list *listptr;
clang-format's avatar
clang-format committed
190 191
  long int rawval;
  char *endptr;
John Koleszar's avatar
John Koleszar committed
192

John Koleszar's avatar
John Koleszar committed
193 194 195 196 197
  /* First see if the value can be parsed as a raw value */
  rawval = strtol(arg->val, &endptr, 10);
  if (arg->val[0] != '\0' && endptr[0] == '\0') {
    /* Got a raw value, make sure it's valid */
    for (listptr = arg->def->enums; listptr->name; listptr++)
James Zern's avatar
James Zern committed
198
      if (listptr->val == rawval) return (int)rawval;
John Koleszar's avatar
John Koleszar committed
199
  }
John Koleszar's avatar
John Koleszar committed
200

John Koleszar's avatar
John Koleszar committed
201 202
  /* Next see if it can be parsed as a string */
  for (listptr = arg->def->enums; listptr->name; listptr++)
clang-format's avatar
clang-format committed
203
    if (!strcmp(arg->val, listptr->name)) return listptr->val;
John Koleszar's avatar
John Koleszar committed
204 205 206

  die("Option %s: Invalid value '%s'\n", arg->name, arg->val);
  return 0;
John Koleszar's avatar
John Koleszar committed
207 208
}

John Koleszar's avatar
John Koleszar committed
209
int arg_parse_enum_or_int(const struct arg *arg) {
clang-format's avatar
clang-format committed
210
  if (arg->def->enums) return arg_parse_enum(arg);
John Koleszar's avatar
John Koleszar committed
211
  return arg_parse_int(arg);
John Koleszar's avatar
John Koleszar committed
212
}
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

// parse a comma separated list of at most n integers
// return the number of elements in the list
int arg_parse_list(const struct arg *arg, int *list, int n) {
  const char *ptr = arg->val;
  char *endptr;
  int i = 0;

  while (ptr[0] != '\0') {
    int32_t rawval = (int32_t)strtol(ptr, &endptr, 10);
    if (rawval < INT_MIN || rawval > INT_MAX) {
      die("Option %s: Value %ld out of range for signed int\n", arg->name,
          rawval);
    } else if (i >= n) {
      die("Option %s: List has more than %d entries\n", arg->name, n);
    } else if (*endptr == ',') {
      endptr++;
    } else if (*endptr != '\0') {
      die("Option %s: Bad list separator '%c'\n", arg->name, *endptr);
    }
    list[i++] = (int)rawval;
    ptr = endptr;
  }
  return i;
}