1 //===-- source/Host/common/OptionParser.cpp -------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "lldb/Host/OptionParser.h"
10 #include "lldb/Host/HostGetOpt.h"
11 #include "lldb/Utility/OptionDefinition.h"
12 #include "lldb/lldb-private-types.h"
13
14 #include <vector>
15
16 using namespace lldb_private;
17
Prepare(std::unique_lock<std::mutex> & lock)18 void OptionParser::Prepare(std::unique_lock<std::mutex> &lock) {
19 static std::mutex g_mutex;
20 lock = std::unique_lock<std::mutex>(g_mutex);
21 #ifdef __GLIBC__
22 optind = 0;
23 #else
24 optreset = 1;
25 optind = 1;
26 #endif
27 }
28
EnableError(bool error)29 void OptionParser::EnableError(bool error) { opterr = error ? 1 : 0; }
30
Parse(llvm::MutableArrayRef<char * > argv,llvm::StringRef optstring,const Option * longopts,int * longindex)31 int OptionParser::Parse(llvm::MutableArrayRef<char *> argv,
32 llvm::StringRef optstring, const Option *longopts,
33 int *longindex) {
34 std::vector<option> opts;
35 while (longopts->definition != nullptr) {
36 option opt;
37 opt.flag = longopts->flag;
38 opt.val = longopts->val;
39 opt.name = longopts->definition->long_option;
40 opt.has_arg = longopts->definition->option_has_arg;
41 opts.push_back(opt);
42 ++longopts;
43 }
44 opts.push_back(option());
45 std::string opt_cstr = std::string(optstring);
46 return getopt_long_only(argv.size() - 1, argv.data(), opt_cstr.c_str(),
47 &opts[0], longindex);
48 }
49
GetOptionArgument()50 char *OptionParser::GetOptionArgument() { return optarg; }
51
GetOptionIndex()52 int OptionParser::GetOptionIndex() { return optind; }
53
GetOptionErrorCause()54 int OptionParser::GetOptionErrorCause() { return optopt; }
55
GetShortOptionString(struct option * long_options)56 std::string OptionParser::GetShortOptionString(struct option *long_options) {
57 std::string s;
58 int i = 0;
59 bool done = false;
60 while (!done) {
61 if (long_options[i].name == nullptr && long_options[i].has_arg == 0 &&
62 long_options[i].flag == nullptr && long_options[i].val == 0) {
63 done = true;
64 } else {
65 if (long_options[i].flag == nullptr && isalpha(long_options[i].val)) {
66 s.append(1, (char)long_options[i].val);
67 switch (long_options[i].has_arg) {
68 default:
69 case no_argument:
70 break;
71
72 case optional_argument:
73 s.append(2, ':');
74 break;
75 case required_argument:
76 s.append(1, ':');
77 break;
78 }
79 }
80 ++i;
81 }
82 }
83 return s;
84 }
85