xref: /aosp_15_r20/external/boringssl/src/tool/args.cc (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
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>
17 
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "internal.h"
25 
26 
ParseKeyValueArguments(std::map<std::string,std::string> * out_args,const std::vector<std::string> & args,const struct argument * templates)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++) {
36       if (strcmp(arg.c_str(), templates[j].name) == 0) {
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 
47     if (out_args->find(arg) != out_args->end()) {
48       fprintf(stderr, "Duplicate argument: %s\n", arg.c_str());
49       return false;
50     }
51 
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     }
61   }
62 
63   for (size_t j = 0; templates[j].name[0] != 0; j++) {
64     const struct argument *templ = &templates[j];
65     if (templ->type == kRequiredArgument &&
66         out_args->find(templ->name) == out_args->end()) {
67       fprintf(stderr, "Missing value for required argument: %s\n", templ->name);
68       return false;
69     }
70   }
71 
72   return true;
73 }
74 
PrintUsage(const struct argument * templates)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 }
81 
GetUnsigned(unsigned * out,const std::string & arg_name,unsigned default_value,const std::map<std::string,std::string> & args)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 
96   errno = 0;
97   char *endptr;
98   unsigned long int num = strtoul(value.c_str(), &endptr, 10);
99   if (num == ULONG_MAX && errno == ERANGE) {
100     return false;
101   }
102   if (*endptr != 0 || num > UINT_MAX) {
103     return false;
104   }
105   *out = static_cast<unsigned>(num);
106 
107   return true;
108 }
109