1 // Copyright 2014 The BoringSSL Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
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