Adam Langley | aacec17 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2014, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include <string> |
| 16 | #include <vector> |
Adam Langley | aacec17 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 17 | |
Dan McArdle | 17be387 | 2021-07-20 12:55:35 -0400 | [diff] [blame] | 18 | #include <errno.h> |
Adam Langley | 839b881 | 2015-05-26 11:36:46 -0700 | [diff] [blame] | 19 | #include <limits.h> |
Ben Laurie | eba2384 | 2014-09-30 12:44:15 +0100 | [diff] [blame] | 20 | #include <stdio.h> |
David Benjamin | 1a3c232 | 2015-06-05 15:11:42 -0400 | [diff] [blame] | 21 | #include <stdlib.h> |
Adam Langley | 1f26ed7 | 2015-04-13 15:59:36 -0700 | [diff] [blame] | 22 | #include <string.h> |
Adam Langley | aacec17 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 23 | |
| 24 | #include "internal.h" |
| 25 | |
| 26 | |
| 27 | bool ParseKeyValueArguments(std::map<std::string, std::string> *out_args, |
| 28 | const std::vector<std::string> &args, |
| 29 | const struct argument *templates) { |
| 30 | out_args->clear(); |
| 31 | |
| 32 | for (size_t i = 0; i < args.size(); i++) { |
| 33 | const std::string &arg = args[i]; |
| 34 | const struct argument *templ = nullptr; |
| 35 | for (size_t j = 0; templates[j].name[0] != 0; j++) { |
Adam Langley | 1f26ed7 | 2015-04-13 15:59:36 -0700 | [diff] [blame] | 36 | if (strcmp(arg.c_str(), templates[j].name) == 0) { |
Adam Langley | aacec17 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 37 | templ = &templates[j]; |
| 38 | break; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | if (templ == nullptr) { |
| 43 | fprintf(stderr, "Unknown argument: %s\n", arg.c_str()); |
| 44 | return false; |
| 45 | } |
| 46 | |
Adam Langley | aacec17 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 47 | if (out_args->find(arg) != out_args->end()) { |
David Benjamin | 0570923 | 2015-03-23 19:01:33 -0400 | [diff] [blame] | 48 | fprintf(stderr, "Duplicate argument: %s\n", arg.c_str()); |
Adam Langley | aacec17 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 49 | return false; |
| 50 | } |
| 51 | |
David Benjamin | 0570923 | 2015-03-23 19:01:33 -0400 | [diff] [blame] | 52 | if (templ->type == kBooleanArgument) { |
| 53 | (*out_args)[arg] = ""; |
| 54 | } else { |
| 55 | if (i + 1 >= args.size()) { |
| 56 | fprintf(stderr, "Missing argument for option: %s\n", arg.c_str()); |
| 57 | return false; |
| 58 | } |
| 59 | (*out_args)[arg] = args[++i]; |
| 60 | } |
Adam Langley | aacec17 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | for (size_t j = 0; templates[j].name[0] != 0; j++) { |
| 64 | const struct argument *templ = &templates[j]; |
David Benjamin | 0570923 | 2015-03-23 19:01:33 -0400 | [diff] [blame] | 65 | if (templ->type == kRequiredArgument && |
| 66 | out_args->find(templ->name) == out_args->end()) { |
Adam Langley | aacec17 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 67 | fprintf(stderr, "Missing value for required argument: %s\n", templ->name); |
| 68 | return false; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | void PrintUsage(const struct argument *templates) { |
| 76 | for (size_t i = 0; templates[i].name[0] != 0; i++) { |
| 77 | const struct argument *templ = &templates[i]; |
| 78 | fprintf(stderr, "%s\t%s\n", templ->name, templ->description); |
| 79 | } |
| 80 | } |
Adam Langley | 839b881 | 2015-05-26 11:36:46 -0700 | [diff] [blame] | 81 | |
| 82 | bool GetUnsigned(unsigned *out, const std::string &arg_name, |
| 83 | unsigned default_value, |
| 84 | const std::map<std::string, std::string> &args) { |
| 85 | const auto &it = args.find(arg_name); |
| 86 | if (it == args.end()) { |
| 87 | *out = default_value; |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | const std::string &value = it->second; |
| 92 | if (value.empty()) { |
| 93 | return false; |
| 94 | } |
| 95 | |
Dan McArdle | 17be387 | 2021-07-20 12:55:35 -0400 | [diff] [blame] | 96 | errno = 0; |
Adam Langley | 839b881 | 2015-05-26 11:36:46 -0700 | [diff] [blame] | 97 | char *endptr; |
| 98 | unsigned long int num = strtoul(value.c_str(), &endptr, 10); |
Dan McArdle | 17be387 | 2021-07-20 12:55:35 -0400 | [diff] [blame] | 99 | if (num == ULONG_MAX && errno == ERANGE) { |
Adam Langley | 839b881 | 2015-05-26 11:36:46 -0700 | [diff] [blame] | 100 | return false; |
| 101 | } |
Dan McArdle | 17be387 | 2021-07-20 12:55:35 -0400 | [diff] [blame] | 102 | if (*endptr != 0 || num > UINT_MAX) { |
| 103 | return false; |
| 104 | } |
| 105 | *out = static_cast<unsigned>(num); |
Adam Langley | 839b881 | 2015-05-26 11:36:46 -0700 | [diff] [blame] | 106 | |
Adam Langley | 839b881 | 2015-05-26 11:36:46 -0700 | [diff] [blame] | 107 | return true; |
| 108 | } |