args.c 5.9 KB

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