blob: 4deb881260441a0d5676be3638fc9376c75cb2bc [file] [log] [blame]
Adam Langleyaacec172014-06-20 12:00:00 -07001/* 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 Langleyaacec172014-06-20 12:00:00 -070017
Dan McArdle17be3872021-07-20 12:55:35 -040018#include <errno.h>
Adam Langley839b8812015-05-26 11:36:46 -070019#include <limits.h>
Ben Laurieeba23842014-09-30 12:44:15 +010020#include <stdio.h>
David Benjamin1a3c2322015-06-05 15:11:42 -040021#include <stdlib.h>
Adam Langley1f26ed72015-04-13 15:59:36 -070022#include <string.h>
Adam Langleyaacec172014-06-20 12:00:00 -070023
24#include "internal.h"
25
26
27bool 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 Langley1f26ed72015-04-13 15:59:36 -070036 if (strcmp(arg.c_str(), templates[j].name) == 0) {
Adam Langleyaacec172014-06-20 12:00:00 -070037 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 Langleyaacec172014-06-20 12:00:00 -070047 if (out_args->find(arg) != out_args->end()) {
David Benjamin05709232015-03-23 19:01:33 -040048 fprintf(stderr, "Duplicate argument: %s\n", arg.c_str());
Adam Langleyaacec172014-06-20 12:00:00 -070049 return false;
50 }
51
David Benjamin05709232015-03-23 19:01:33 -040052 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 Langleyaacec172014-06-20 12:00:00 -070061 }
62
63 for (size_t j = 0; templates[j].name[0] != 0; j++) {
64 const struct argument *templ = &templates[j];
David Benjamin05709232015-03-23 19:01:33 -040065 if (templ->type == kRequiredArgument &&
66 out_args->find(templ->name) == out_args->end()) {
Adam Langleyaacec172014-06-20 12:00:00 -070067 fprintf(stderr, "Missing value for required argument: %s\n", templ->name);
68 return false;
69 }
70 }
71
72 return true;
73}
74
75void 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 Langley839b8812015-05-26 11:36:46 -070081
82bool 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 McArdle17be3872021-07-20 12:55:35 -040096 errno = 0;
Adam Langley839b8812015-05-26 11:36:46 -070097 char *endptr;
98 unsigned long int num = strtoul(value.c_str(), &endptr, 10);
Dan McArdle17be3872021-07-20 12:55:35 -040099 if (num == ULONG_MAX && errno == ERANGE) {
Adam Langley839b8812015-05-26 11:36:46 -0700100 return false;
101 }
Dan McArdle17be3872021-07-20 12:55:35 -0400102 if (*endptr != 0 || num > UINT_MAX) {
103 return false;
104 }
105 *out = static_cast<unsigned>(num);
Adam Langley839b8812015-05-26 11:36:46 -0700106
Adam Langley839b8812015-05-26 11:36:46 -0700107 return true;
108}