Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Options.cpp ---------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Jim Ingham | 40af72e | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 10 | #include "lldb/Interpreter/Options.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
Caroline Tice | f362c45 | 2010-09-09 16:44:14 +0000 | [diff] [blame] | 14 | #include <algorithm> |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 15 | #include <bitset> |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 16 | #include <map> |
Enrico Granata | bef55ac | 2016-03-14 22:17:04 +0000 | [diff] [blame] | 17 | #include <set> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | |
| 19 | // Other libraries and framework includes |
| 20 | // Project includes |
Zachary Turner | 3eb2b44 | 2017-03-22 23:33:16 +0000 | [diff] [blame] | 21 | #include "lldb/Host/OptionParser.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/Interpreter/CommandCompletions.h" |
| 23 | #include "lldb/Interpreter/CommandInterpreter.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | #include "lldb/Interpreter/CommandObject.h" |
| 25 | #include "lldb/Interpreter/CommandReturnObject.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | #include "lldb/Target/Target.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 27 | #include "lldb/Utility/StreamString.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace lldb; |
| 30 | using namespace lldb_private; |
| 31 | |
| 32 | //------------------------------------------------------------------------- |
| 33 | // Options |
| 34 | //------------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 35 | Options::Options() : m_getopt_table() { BuildValidOptionSets(); } |
| 36 | |
| 37 | Options::~Options() {} |
| 38 | |
| 39 | void Options::NotifyOptionParsingStarting(ExecutionContext *execution_context) { |
| 40 | m_seen_options.clear(); |
| 41 | // Let the subclass reset its option values |
| 42 | OptionParsingStarting(execution_context); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 45 | Status |
| 46 | Options::NotifyOptionParsingFinished(ExecutionContext *execution_context) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | return OptionParsingFinished(execution_context); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | void Options::OptionSeen(int option_idx) { m_seen_options.insert(option_idx); } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | |
| 52 | // Returns true is set_a is a subset of set_b; Otherwise returns false. |
| 53 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | bool Options::IsASubset(const OptionSet &set_a, const OptionSet &set_b) { |
| 55 | bool is_a_subset = true; |
| 56 | OptionSet::const_iterator pos_a; |
| 57 | OptionSet::const_iterator pos_b; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 58 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | // set_a is a subset of set_b if every member of set_a is also a member of |
| 60 | // set_b |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 62 | for (pos_a = set_a.begin(); pos_a != set_a.end() && is_a_subset; ++pos_a) { |
| 63 | pos_b = set_b.find(*pos_a); |
| 64 | if (pos_b == set_b.end()) |
| 65 | is_a_subset = false; |
| 66 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 67 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | return is_a_subset; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 71 | // Returns the set difference set_a - set_b, i.e. { x | ElementOf (x, set_a) && |
| 72 | // !ElementOf (x, set_b) } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | size_t Options::OptionsSetDiff(const OptionSet &set_a, const OptionSet &set_b, |
| 75 | OptionSet &diffs) { |
| 76 | size_t num_diffs = 0; |
| 77 | OptionSet::const_iterator pos_a; |
| 78 | OptionSet::const_iterator pos_b; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 79 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | for (pos_a = set_a.begin(); pos_a != set_a.end(); ++pos_a) { |
| 81 | pos_b = set_b.find(*pos_a); |
| 82 | if (pos_b == set_b.end()) { |
| 83 | ++num_diffs; |
| 84 | diffs.insert(*pos_a); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 85 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 87 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 88 | return num_diffs; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | // Returns the union of set_a and set_b. Does not put duplicate members into |
| 92 | // the union. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 93 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 94 | void Options::OptionsSetUnion(const OptionSet &set_a, const OptionSet &set_b, |
| 95 | OptionSet &union_set) { |
| 96 | OptionSet::const_iterator pos; |
| 97 | OptionSet::iterator pos_union; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | // Put all the elements of set_a into the union. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | for (pos = set_a.begin(); pos != set_a.end(); ++pos) |
| 102 | union_set.insert(*pos); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 103 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | // Put all the elements of set_b that are not already there into the union. |
| 105 | for (pos = set_b.begin(); pos != set_b.end(); ++pos) { |
| 106 | pos_union = union_set.find(*pos); |
| 107 | if (pos_union == union_set.end()) |
| 108 | union_set.insert(*pos); |
| 109 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | bool Options::VerifyOptions(CommandReturnObject &result) { |
| 113 | bool options_are_valid = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 114 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 115 | int num_levels = GetRequiredOptions().size(); |
| 116 | if (num_levels) { |
| 117 | for (int i = 0; i < num_levels && !options_are_valid; ++i) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 118 | // This is the correct set of options if: 1). m_seen_options contains |
| 119 | // all of m_required_options[i] (i.e. all the required options at this |
| 120 | // level are a subset of m_seen_options); AND 2). { m_seen_options - |
| 121 | // m_required_options[i] is a subset of m_options_options[i] (i.e. all |
| 122 | // the rest of m_seen_options are in the set of optional options at this |
| 123 | // level. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 124 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 125 | // Check to see if all of m_required_options[i] are a subset of |
| 126 | // m_seen_options |
| 127 | if (IsASubset(GetRequiredOptions()[i], m_seen_options)) { |
| 128 | // Construct the set difference: remaining_options = {m_seen_options} - |
| 129 | // {m_required_options[i]} |
| 130 | OptionSet remaining_options; |
| 131 | OptionsSetDiff(m_seen_options, GetRequiredOptions()[i], |
| 132 | remaining_options); |
| 133 | // Check to see if remaining_options is a subset of |
| 134 | // m_optional_options[i] |
| 135 | if (IsASubset(remaining_options, GetOptionalOptions()[i])) |
| 136 | options_are_valid = true; |
| 137 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 138 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | } else { |
| 140 | options_are_valid = true; |
| 141 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 143 | if (options_are_valid) { |
| 144 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 145 | } else { |
| 146 | result.AppendError("invalid combination of options for the given command"); |
| 147 | result.SetStatus(eReturnStatusFailed); |
| 148 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 149 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 150 | return options_are_valid; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 153 | // This is called in the Options constructor, though we could call it lazily if |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 154 | // that ends up being a performance problem. |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 155 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 156 | void Options::BuildValidOptionSets() { |
| 157 | // Check to see if we already did this. |
| 158 | if (m_required_options.size() != 0) |
| 159 | return; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 160 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 161 | // Check to see if there are any options. |
| 162 | int num_options = NumCommandOptions(); |
| 163 | if (num_options == 0) |
| 164 | return; |
| 165 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 166 | auto opt_defs = GetDefinitions(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 167 | m_required_options.resize(1); |
| 168 | m_optional_options.resize(1); |
| 169 | |
| 170 | // First count the number of option sets we've got. Ignore |
| 171 | // LLDB_ALL_OPTION_SETS... |
| 172 | |
| 173 | uint32_t num_option_sets = 0; |
| 174 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 175 | for (const auto &def : opt_defs) { |
| 176 | uint32_t this_usage_mask = def.usage_mask; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 177 | if (this_usage_mask == LLDB_OPT_SET_ALL) { |
| 178 | if (num_option_sets == 0) |
| 179 | num_option_sets = 1; |
| 180 | } else { |
| 181 | for (uint32_t j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++) { |
| 182 | if (this_usage_mask & (1 << j)) { |
| 183 | if (num_option_sets <= j) |
| 184 | num_option_sets = j + 1; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (num_option_sets > 0) { |
| 191 | m_required_options.resize(num_option_sets); |
| 192 | m_optional_options.resize(num_option_sets); |
| 193 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 194 | for (const auto &def : opt_defs) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 195 | for (uint32_t j = 0; j < num_option_sets; j++) { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 196 | if (def.usage_mask & 1 << j) { |
| 197 | if (def.required) |
| 198 | m_required_options[j].insert(def.short_option); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 199 | else |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 200 | m_optional_options[j].insert(def.short_option); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 207 | uint32_t Options::NumCommandOptions() { return GetDefinitions().size(); } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 208 | |
| 209 | Option *Options::GetLongOptions() { |
| 210 | // Check to see if this has already been done. |
| 211 | if (m_getopt_table.empty()) { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 212 | auto defs = GetDefinitions(); |
| 213 | if (defs.empty()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 214 | return nullptr; |
| 215 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 216 | std::map<int, uint32_t> option_seen; |
Enrico Granata | 4ebb8a4 | 2016-03-15 01:17:32 +0000 | [diff] [blame] | 217 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 218 | m_getopt_table.resize(defs.size() + 1); |
| 219 | for (size_t i = 0; i < defs.size(); ++i) { |
| 220 | const int short_opt = defs[i].short_option; |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 221 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 222 | m_getopt_table[i].definition = &defs[i]; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 223 | m_getopt_table[i].flag = nullptr; |
| 224 | m_getopt_table[i].val = short_opt; |
Enrico Granata | bef55ac | 2016-03-14 22:17:04 +0000 | [diff] [blame] | 225 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 226 | if (option_seen.find(short_opt) == option_seen.end()) { |
| 227 | option_seen[short_opt] = i; |
| 228 | } else if (short_opt) { |
| 229 | m_getopt_table[i].val = 0; |
| 230 | std::map<int, uint32_t>::const_iterator pos = |
| 231 | option_seen.find(short_opt); |
| 232 | StreamString strm; |
| 233 | if (isprint8(short_opt)) |
| 234 | Host::SystemLog(Host::eSystemLogError, |
| 235 | "option[%u] --%s has a short option -%c that " |
| 236 | "conflicts with option[%u] --%s, short option won't " |
| 237 | "be used for --%s\n", |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 238 | (int)i, defs[i].long_option, short_opt, pos->second, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 239 | m_getopt_table[pos->second].definition->long_option, |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 240 | defs[i].long_option); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 241 | else |
| 242 | Host::SystemLog(Host::eSystemLogError, |
| 243 | "option[%u] --%s has a short option 0x%x that " |
| 244 | "conflicts with option[%u] --%s, short option won't " |
| 245 | "be used for --%s\n", |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 246 | (int)i, defs[i].long_option, short_opt, pos->second, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 247 | m_getopt_table[pos->second].definition->long_option, |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 248 | defs[i].long_option); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 249 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 252 | // getopt_long_only requires a NULL final entry in the table: |
| 253 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 254 | m_getopt_table.back().definition = nullptr; |
| 255 | m_getopt_table.back().flag = nullptr; |
| 256 | m_getopt_table.back().val = 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | if (m_getopt_table.empty()) |
| 260 | return nullptr; |
| 261 | |
| 262 | return &m_getopt_table.front(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 265 | // This function takes INDENT, which tells how many spaces to output at the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 266 | // front of each line; SPACES, which is a string containing 80 spaces; and |
| 267 | // TEXT, which is the text that is to be output. It outputs the text, on |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 268 | // multiple lines if necessary, to RESULT, with INDENT spaces at the front of |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 269 | // each line. It breaks lines on spaces, tabs or newlines, shortening the line |
| 270 | // if necessary to not break in the middle of a word. It assumes that each |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 271 | // output line should contain a maximum of OUTPUT_MAX_COLUMNS characters. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 272 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 273 | void Options::OutputFormattedUsageText(Stream &strm, |
| 274 | const OptionDefinition &option_def, |
| 275 | uint32_t output_max_columns) { |
| 276 | std::string actual_text; |
| 277 | if (option_def.validator) { |
| 278 | const char *condition = option_def.validator->ShortConditionString(); |
| 279 | if (condition) { |
| 280 | actual_text = "["; |
| 281 | actual_text.append(condition); |
| 282 | actual_text.append("] "); |
| 283 | } |
| 284 | } |
| 285 | actual_text.append(option_def.usage_text); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 286 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 287 | // Will it all fit on one line? |
| 288 | |
| 289 | if (static_cast<uint32_t>(actual_text.length() + strm.GetIndentLevel()) < |
| 290 | output_max_columns) { |
| 291 | // Output it as a single line. |
| 292 | strm.Indent(actual_text.c_str()); |
| 293 | strm.EOL(); |
| 294 | } else { |
| 295 | // We need to break it up into multiple lines. |
| 296 | |
| 297 | int text_width = output_max_columns - strm.GetIndentLevel() - 1; |
| 298 | int start = 0; |
| 299 | int end = start; |
| 300 | int final_end = actual_text.length(); |
| 301 | int sub_len; |
| 302 | |
| 303 | while (end < final_end) { |
| 304 | // Don't start the 'text' on a space, since we're already outputting the |
| 305 | // indentation. |
| 306 | while ((start < final_end) && (actual_text[start] == ' ')) |
| 307 | start++; |
| 308 | |
| 309 | end = start + text_width; |
| 310 | if (end > final_end) |
| 311 | end = final_end; |
| 312 | else { |
| 313 | // If we're not at the end of the text, make sure we break the line on |
| 314 | // white space. |
| 315 | while (end > start && actual_text[end] != ' ' && |
| 316 | actual_text[end] != '\t' && actual_text[end] != '\n') |
| 317 | end--; |
| 318 | } |
| 319 | |
| 320 | sub_len = end - start; |
| 321 | if (start != 0) |
| 322 | strm.EOL(); |
| 323 | strm.Indent(); |
| 324 | assert(start < final_end); |
| 325 | assert(start + sub_len <= final_end); |
| 326 | strm.Write(actual_text.c_str() + start, sub_len); |
| 327 | start = end + 1; |
| 328 | } |
| 329 | strm.EOL(); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | bool Options::SupportsLongOption(const char *long_option) { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 334 | if (!long_option || !long_option[0]) |
| 335 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 336 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 337 | auto opt_defs = GetDefinitions(); |
| 338 | if (opt_defs.empty()) |
| 339 | return false; |
| 340 | |
| 341 | const char *long_option_name = long_option; |
| 342 | if (long_option[0] == '-' && long_option[1] == '-') |
| 343 | long_option_name += 2; |
| 344 | |
| 345 | for (auto &def : opt_defs) { |
| 346 | if (!def.long_option) |
| 347 | continue; |
| 348 | |
| 349 | if (strcmp(def.long_option, long_option_name) == 0) |
| 350 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 351 | } |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 352 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 353 | return false; |
| 354 | } |
| 355 | |
| 356 | enum OptionDisplayType { |
| 357 | eDisplayBestOption, |
| 358 | eDisplayShortOption, |
| 359 | eDisplayLongOption |
| 360 | }; |
| 361 | |
| 362 | static bool PrintOption(const OptionDefinition &opt_def, |
| 363 | OptionDisplayType display_type, const char *header, |
| 364 | const char *footer, bool show_optional, Stream &strm) { |
| 365 | const bool has_short_option = isprint8(opt_def.short_option) != 0; |
| 366 | |
| 367 | if (display_type == eDisplayShortOption && !has_short_option) |
| 368 | return false; |
| 369 | |
| 370 | if (header && header[0]) |
| 371 | strm.PutCString(header); |
| 372 | |
| 373 | if (show_optional && !opt_def.required) |
| 374 | strm.PutChar('['); |
| 375 | const bool show_short_option = |
| 376 | has_short_option && display_type != eDisplayLongOption; |
| 377 | if (show_short_option) |
| 378 | strm.Printf("-%c", opt_def.short_option); |
| 379 | else |
| 380 | strm.Printf("--%s", opt_def.long_option); |
| 381 | switch (opt_def.option_has_arg) { |
| 382 | case OptionParser::eNoArgument: |
| 383 | break; |
| 384 | case OptionParser::eRequiredArgument: |
| 385 | strm.Printf(" <%s>", CommandObject::GetArgumentName(opt_def.argument_type)); |
| 386 | break; |
| 387 | |
| 388 | case OptionParser::eOptionalArgument: |
| 389 | strm.Printf("%s[<%s>]", show_short_option ? "" : "=", |
| 390 | CommandObject::GetArgumentName(opt_def.argument_type)); |
| 391 | break; |
| 392 | } |
| 393 | if (show_optional && !opt_def.required) |
| 394 | strm.PutChar(']'); |
| 395 | if (footer && footer[0]) |
| 396 | strm.PutCString(footer); |
| 397 | return true; |
| 398 | } |
| 399 | |
| 400 | void Options::GenerateOptionUsage(Stream &strm, CommandObject *cmd, |
| 401 | uint32_t screen_width) { |
| 402 | const bool only_print_args = cmd->IsDashDashCommand(); |
| 403 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 404 | auto opt_defs = GetDefinitions(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 405 | const uint32_t save_indent_level = strm.GetIndentLevel(); |
Zachary Turner | a449698 | 2016-10-05 21:14:38 +0000 | [diff] [blame] | 406 | llvm::StringRef name; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 407 | |
| 408 | StreamString arguments_str; |
| 409 | |
| 410 | if (cmd) { |
| 411 | name = cmd->GetCommandName(); |
| 412 | cmd->GetFormattedCommandArguments(arguments_str); |
| 413 | } else |
| 414 | name = ""; |
| 415 | |
| 416 | strm.PutCString("\nCommand Options Usage:\n"); |
| 417 | |
| 418 | strm.IndentMore(2); |
| 419 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 420 | // First, show each usage level set of options, e.g. <cmd> [options-for- |
| 421 | // level-0] |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 422 | // <cmd> |
| 423 | // [options-for-level-1] |
| 424 | // etc. |
| 425 | |
| 426 | const uint32_t num_options = NumCommandOptions(); |
| 427 | if (num_options == 0) |
| 428 | return; |
| 429 | |
| 430 | uint32_t num_option_sets = GetRequiredOptions().size(); |
| 431 | |
| 432 | uint32_t i; |
| 433 | |
| 434 | if (!only_print_args) { |
| 435 | for (uint32_t opt_set = 0; opt_set < num_option_sets; ++opt_set) { |
| 436 | uint32_t opt_set_mask; |
| 437 | |
| 438 | opt_set_mask = 1 << opt_set; |
| 439 | if (opt_set > 0) |
| 440 | strm.Printf("\n"); |
| 441 | strm.Indent(name); |
| 442 | |
| 443 | // Different option sets may require different args. |
| 444 | StreamString args_str; |
| 445 | if (cmd) |
| 446 | cmd->GetFormattedCommandArguments(args_str, opt_set_mask); |
| 447 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 448 | // First go through and print all options that take no arguments as a |
| 449 | // single string. If a command has "-a" "-b" and "-c", this will show up |
| 450 | // as [-abc] |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 451 | |
| 452 | std::set<int> options; |
| 453 | std::set<int>::const_iterator options_pos, options_end; |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 454 | for (auto &def : opt_defs) { |
| 455 | if (def.usage_mask & opt_set_mask && isprint8(def.short_option)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 456 | // Add current option to the end of out_stream. |
| 457 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 458 | if (def.required && def.option_has_arg == OptionParser::eNoArgument) { |
| 459 | options.insert(def.short_option); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | if (options.empty() == false) { |
| 465 | // We have some required options with no arguments |
| 466 | strm.PutCString(" -"); |
| 467 | for (i = 0; i < 2; ++i) |
| 468 | for (options_pos = options.begin(), options_end = options.end(); |
| 469 | options_pos != options_end; ++options_pos) { |
| 470 | if (i == 0 && ::islower(*options_pos)) |
| 471 | continue; |
| 472 | if (i == 1 && ::isupper(*options_pos)) |
| 473 | continue; |
| 474 | strm << (char)*options_pos; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 478 | options.clear(); |
| 479 | for (auto &def : opt_defs) { |
| 480 | if (def.usage_mask & opt_set_mask && isprint8(def.short_option)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 481 | // Add current option to the end of out_stream. |
| 482 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 483 | if (def.required == false && |
| 484 | def.option_has_arg == OptionParser::eNoArgument) { |
| 485 | options.insert(def.short_option); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | if (options.empty() == false) { |
| 491 | // We have some required options with no arguments |
| 492 | strm.PutCString(" [-"); |
| 493 | for (i = 0; i < 2; ++i) |
| 494 | for (options_pos = options.begin(), options_end = options.end(); |
| 495 | options_pos != options_end; ++options_pos) { |
| 496 | if (i == 0 && ::islower(*options_pos)) |
| 497 | continue; |
| 498 | if (i == 1 && ::isupper(*options_pos)) |
| 499 | continue; |
| 500 | strm << (char)*options_pos; |
| 501 | } |
| 502 | strm.PutChar(']'); |
| 503 | } |
| 504 | |
| 505 | // First go through and print the required options (list them up front). |
| 506 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 507 | for (auto &def : opt_defs) { |
| 508 | if (def.usage_mask & opt_set_mask && isprint8(def.short_option)) { |
| 509 | if (def.required && def.option_has_arg != OptionParser::eNoArgument) |
| 510 | PrintOption(def, eDisplayBestOption, " ", nullptr, true, strm); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
| 514 | // Now go through again, and this time only print the optional options. |
| 515 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 516 | for (auto &def : opt_defs) { |
| 517 | if (def.usage_mask & opt_set_mask) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 518 | // Add current option to the end of out_stream. |
| 519 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 520 | if (!def.required && def.option_has_arg != OptionParser::eNoArgument) |
| 521 | PrintOption(def, eDisplayBestOption, " ", nullptr, true, strm); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | |
| 525 | if (args_str.GetSize() > 0) { |
| 526 | if (cmd->WantsRawCommandString() && !only_print_args) |
| 527 | strm.Printf(" --"); |
| 528 | |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 529 | strm << " " << args_str.GetString(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 530 | if (only_print_args) |
| 531 | break; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | if (cmd && (only_print_args || cmd->WantsRawCommandString()) && |
| 537 | arguments_str.GetSize() > 0) { |
| 538 | if (!only_print_args) |
| 539 | strm.PutChar('\n'); |
| 540 | strm.Indent(name); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 541 | strm << " " << arguments_str.GetString(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | strm.Printf("\n\n"); |
| 545 | |
| 546 | if (!only_print_args) { |
| 547 | // Now print out all the detailed information about the various options: |
| 548 | // long form, short form and help text: |
| 549 | // -short <argument> ( --long_name <argument> ) |
| 550 | // help text |
| 551 | |
| 552 | // This variable is used to keep track of which options' info we've printed |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 553 | // out, because some options can be in more than one usage level, but we |
| 554 | // only want to print the long form of its information once. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 555 | |
| 556 | std::multimap<int, uint32_t> options_seen; |
| 557 | strm.IndentMore(5); |
| 558 | |
| 559 | // Put the unique command options in a vector & sort it, so we can output |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 560 | // them alphabetically (by short_option) when writing out detailed help for |
| 561 | // each option. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 562 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 563 | i = 0; |
| 564 | for (auto &def : opt_defs) |
| 565 | options_seen.insert(std::make_pair(def.short_option, i++)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 566 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 567 | // Go through the unique'd and alphabetically sorted vector of options, |
| 568 | // find the table entry for each option and write out the detailed help |
| 569 | // information for that option. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 570 | |
| 571 | bool first_option_printed = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 572 | |
| 573 | for (auto pos : options_seen) { |
| 574 | i = pos.second; |
| 575 | // Print out the help information for this option. |
| 576 | |
| 577 | // Put a newline separation between arguments |
| 578 | if (first_option_printed) |
| 579 | strm.EOL(); |
| 580 | else |
| 581 | first_option_printed = true; |
| 582 | |
| 583 | CommandArgumentType arg_type = opt_defs[i].argument_type; |
| 584 | |
| 585 | StreamString arg_name_str; |
| 586 | arg_name_str.Printf("<%s>", CommandObject::GetArgumentName(arg_type)); |
| 587 | |
| 588 | strm.Indent(); |
| 589 | if (opt_defs[i].short_option && isprint8(opt_defs[i].short_option)) { |
| 590 | PrintOption(opt_defs[i], eDisplayShortOption, nullptr, nullptr, false, |
| 591 | strm); |
| 592 | PrintOption(opt_defs[i], eDisplayLongOption, " ( ", " )", false, strm); |
| 593 | } else { |
| 594 | // Short option is not printable, just print long option |
| 595 | PrintOption(opt_defs[i], eDisplayLongOption, nullptr, nullptr, false, |
| 596 | strm); |
| 597 | } |
| 598 | strm.EOL(); |
| 599 | |
| 600 | strm.IndentMore(5); |
| 601 | |
| 602 | if (opt_defs[i].usage_text) |
| 603 | OutputFormattedUsageText(strm, opt_defs[i], screen_width); |
| 604 | if (opt_defs[i].enum_values != nullptr) { |
| 605 | strm.Indent(); |
| 606 | strm.Printf("Values: "); |
| 607 | for (int k = 0; opt_defs[i].enum_values[k].string_value != nullptr; |
| 608 | k++) { |
| 609 | if (k == 0) |
| 610 | strm.Printf("%s", opt_defs[i].enum_values[k].string_value); |
| 611 | else |
| 612 | strm.Printf(" | %s", opt_defs[i].enum_values[k].string_value); |
| 613 | } |
| 614 | strm.EOL(); |
| 615 | } |
| 616 | strm.IndentLess(5); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | // Restore the indent level |
| 621 | strm.SetIndentLevel(save_indent_level); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 624 | // This function is called when we have been given a potentially incomplete set |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 625 | // of options, such as when an alias has been defined (more options might be |
| 626 | // added at at the time the alias is invoked). We need to verify that the |
| 627 | // options in the set m_seen_options are all part of a set that may be used |
| 628 | // together, but m_seen_options may be missing some of the "required" options. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 629 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 630 | bool Options::VerifyPartialOptions(CommandReturnObject &result) { |
| 631 | bool options_are_valid = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 632 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 633 | int num_levels = GetRequiredOptions().size(); |
| 634 | if (num_levels) { |
| 635 | for (int i = 0; i < num_levels && !options_are_valid; ++i) { |
| 636 | // In this case we are treating all options as optional rather than |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 637 | // required. Therefore a set of options is correct if m_seen_options is a |
| 638 | // subset of the union of m_required_options and m_optional_options. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 639 | OptionSet union_set; |
| 640 | OptionsSetUnion(GetRequiredOptions()[i], GetOptionalOptions()[i], |
| 641 | union_set); |
| 642 | if (IsASubset(m_seen_options, union_set)) |
| 643 | options_are_valid = true; |
| 644 | } |
| 645 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 646 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 647 | return options_are_valid; |
| 648 | } |
Jim Ingham | d6ccc60 | 2010-06-24 20:30:15 +0000 | [diff] [blame] | 649 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 650 | bool Options::HandleOptionCompletion( |
| 651 | Args &input, OptionElementVector &opt_element_vector, int cursor_index, |
| 652 | int char_pos, int match_start_point, int max_return_elements, |
| 653 | CommandInterpreter &interpreter, bool &word_complete, |
| 654 | lldb_private::StringList &matches) { |
| 655 | word_complete = true; |
| 656 | |
| 657 | // For now we just scan the completions to see if the cursor position is in |
| 658 | // an option or its argument. Otherwise we'll call HandleArgumentCompletion. |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 659 | // In the future we can use completion to validate options as well if we |
| 660 | // want. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 661 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 662 | auto opt_defs = GetDefinitions(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 663 | |
| 664 | std::string cur_opt_std_str(input.GetArgumentAtIndex(cursor_index)); |
| 665 | cur_opt_std_str.erase(char_pos); |
| 666 | const char *cur_opt_str = cur_opt_std_str.c_str(); |
| 667 | |
| 668 | for (size_t i = 0; i < opt_element_vector.size(); i++) { |
| 669 | int opt_pos = opt_element_vector[i].opt_pos; |
| 670 | int opt_arg_pos = opt_element_vector[i].opt_arg_pos; |
| 671 | int opt_defs_index = opt_element_vector[i].opt_defs_index; |
| 672 | if (opt_pos == cursor_index) { |
| 673 | // We're completing the option itself. |
| 674 | |
| 675 | if (opt_defs_index == OptionArgElement::eBareDash) { |
| 676 | // We're completing a bare dash. That means all options are open. |
| 677 | // FIXME: We should scan the other options provided and only complete |
| 678 | // options |
| 679 | // within the option group they belong to. |
| 680 | char opt_str[3] = {'-', 'a', '\0'}; |
| 681 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 682 | for (auto &def : opt_defs) { |
| 683 | if (!def.short_option) |
| 684 | continue; |
| 685 | opt_str[1] = def.short_option; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 686 | matches.AppendString(opt_str); |
| 687 | } |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 688 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 689 | return true; |
| 690 | } else if (opt_defs_index == OptionArgElement::eBareDoubleDash) { |
| 691 | std::string full_name("--"); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 692 | for (auto &def : opt_defs) { |
| 693 | if (!def.short_option) |
| 694 | continue; |
| 695 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 696 | full_name.erase(full_name.begin() + 2, full_name.end()); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 697 | full_name.append(def.long_option); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 698 | matches.AppendString(full_name.c_str()); |
| 699 | } |
| 700 | return true; |
| 701 | } else if (opt_defs_index != OptionArgElement::eUnrecognizedArg) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 702 | // We recognized it, if it an incomplete long option, complete it |
| 703 | // anyway (getopt_long_only is happy with shortest unique string, but |
| 704 | // it's still a nice thing to do.) Otherwise return The string so the |
| 705 | // upper level code will know this is a full match and add the " ". |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 706 | if (cur_opt_str && strlen(cur_opt_str) > 2 && cur_opt_str[0] == '-' && |
| 707 | cur_opt_str[1] == '-' && |
| 708 | strcmp(opt_defs[opt_defs_index].long_option, cur_opt_str) != 0) { |
| 709 | std::string full_name("--"); |
| 710 | full_name.append(opt_defs[opt_defs_index].long_option); |
| 711 | matches.AppendString(full_name.c_str()); |
| 712 | return true; |
| 713 | } else { |
| 714 | matches.AppendString(input.GetArgumentAtIndex(cursor_index)); |
| 715 | return true; |
| 716 | } |
| 717 | } else { |
| 718 | // FIXME - not handling wrong options yet: |
| 719 | // Check to see if they are writing a long option & complete it. |
| 720 | // I think we will only get in here if the long option table has two |
| 721 | // elements |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 722 | // that are not unique up to this point. getopt_long_only does |
| 723 | // shortest unique match for long options already. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 724 | |
| 725 | if (cur_opt_str && strlen(cur_opt_str) > 2 && cur_opt_str[0] == '-' && |
| 726 | cur_opt_str[1] == '-') { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 727 | for (auto &def : opt_defs) { |
| 728 | if (!def.long_option) |
| 729 | continue; |
| 730 | |
| 731 | if (strstr(def.long_option, cur_opt_str + 2) == def.long_option) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 732 | std::string full_name("--"); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 733 | full_name.append(def.long_option); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 734 | // The options definitions table has duplicates because of the |
| 735 | // way the grouping information is stored, so only add once. |
| 736 | bool duplicate = false; |
| 737 | for (size_t k = 0; k < matches.GetSize(); k++) { |
| 738 | if (matches.GetStringAtIndex(k) == full_name) { |
| 739 | duplicate = true; |
| 740 | break; |
Jim Ingham | d6ccc60 | 2010-06-24 20:30:15 +0000 | [diff] [blame] | 741 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 742 | } |
| 743 | if (!duplicate) |
| 744 | matches.AppendString(full_name.c_str()); |
Jim Ingham | d6ccc60 | 2010-06-24 20:30:15 +0000 | [diff] [blame] | 745 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 746 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 747 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 748 | return true; |
| 749 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 750 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 751 | } else if (opt_arg_pos == cursor_index) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 752 | // Okay the cursor is on the completion of an argument. See if it has a |
| 753 | // completion, otherwise return no matches. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 754 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 755 | if (opt_defs_index != -1) { |
| 756 | HandleOptionArgumentCompletion( |
| 757 | input, cursor_index, strlen(input.GetArgumentAtIndex(cursor_index)), |
| 758 | opt_element_vector, i, match_start_point, max_return_elements, |
| 759 | interpreter, word_complete, matches); |
| 760 | return true; |
| 761 | } else { |
| 762 | // No completion callback means no completions... |
| 763 | return true; |
| 764 | } |
| 765 | |
| 766 | } else { |
| 767 | // Not the last element, keep going. |
| 768 | continue; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 769 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 770 | } |
| 771 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 774 | bool Options::HandleOptionArgumentCompletion( |
| 775 | Args &input, int cursor_index, int char_pos, |
| 776 | OptionElementVector &opt_element_vector, int opt_element_index, |
| 777 | int match_start_point, int max_return_elements, |
| 778 | CommandInterpreter &interpreter, bool &word_complete, |
| 779 | lldb_private::StringList &matches) { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 780 | auto opt_defs = GetDefinitions(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 781 | std::unique_ptr<SearchFilter> filter_ap; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 782 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 783 | int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos; |
| 784 | int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index; |
| 785 | |
| 786 | // See if this is an enumeration type option, and if so complete it here: |
| 787 | |
| 788 | OptionEnumValueElement *enum_values = opt_defs[opt_defs_index].enum_values; |
| 789 | if (enum_values != nullptr) { |
| 790 | bool return_value = false; |
| 791 | std::string match_string(input.GetArgumentAtIndex(opt_arg_pos), |
| 792 | input.GetArgumentAtIndex(opt_arg_pos) + char_pos); |
| 793 | for (int i = 0; enum_values[i].string_value != nullptr; i++) { |
| 794 | if (strstr(enum_values[i].string_value, match_string.c_str()) == |
| 795 | enum_values[i].string_value) { |
| 796 | matches.AppendString(enum_values[i].string_value); |
| 797 | return_value = true; |
| 798 | } |
| 799 | } |
| 800 | return return_value; |
| 801 | } |
| 802 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 803 | // If this is a source file or symbol type completion, and there is a -shlib |
| 804 | // option somewhere in the supplied arguments, then make a search filter for |
| 805 | // that shared library. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 806 | // FIXME: Do we want to also have an "OptionType" so we don't have to match |
| 807 | // string names? |
| 808 | |
| 809 | uint32_t completion_mask = opt_defs[opt_defs_index].completion_type; |
| 810 | |
| 811 | if (completion_mask == 0) { |
| 812 | lldb::CommandArgumentType option_arg_type = |
| 813 | opt_defs[opt_defs_index].argument_type; |
| 814 | if (option_arg_type != eArgTypeNone) { |
| 815 | const CommandObject::ArgumentTableEntry *arg_entry = |
| 816 | CommandObject::FindArgumentDataByType( |
| 817 | opt_defs[opt_defs_index].argument_type); |
| 818 | if (arg_entry) |
| 819 | completion_mask = arg_entry->completion_type; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | if (completion_mask & CommandCompletions::eSourceFileCompletion || |
| 824 | completion_mask & CommandCompletions::eSymbolCompletion) { |
| 825 | for (size_t i = 0; i < opt_element_vector.size(); i++) { |
| 826 | int cur_defs_index = opt_element_vector[i].opt_defs_index; |
| 827 | |
| 828 | // trying to use <0 indices will definitely cause problems |
| 829 | if (cur_defs_index == OptionArgElement::eUnrecognizedArg || |
| 830 | cur_defs_index == OptionArgElement::eBareDash || |
| 831 | cur_defs_index == OptionArgElement::eBareDoubleDash) |
| 832 | continue; |
| 833 | |
| 834 | int cur_arg_pos = opt_element_vector[i].opt_arg_pos; |
| 835 | const char *cur_opt_name = opt_defs[cur_defs_index].long_option; |
| 836 | |
| 837 | // If this is the "shlib" option and there was an argument provided, |
| 838 | // restrict it to that shared library. |
| 839 | if (cur_opt_name && strcmp(cur_opt_name, "shlib") == 0 && |
| 840 | cur_arg_pos != -1) { |
| 841 | const char *module_name = input.GetArgumentAtIndex(cur_arg_pos); |
| 842 | if (module_name) { |
| 843 | FileSpec module_spec(module_name, false); |
| 844 | lldb::TargetSP target_sp = |
| 845 | interpreter.GetDebugger().GetSelectedTarget(); |
| 846 | // Search filters require a target... |
| 847 | if (target_sp) |
| 848 | filter_ap.reset(new SearchFilterByModule(target_sp, module_spec)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 849 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 850 | break; |
| 851 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 852 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 853 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 854 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 855 | return CommandCompletions::InvokeCommonCompletionCallbacks( |
| 856 | interpreter, completion_mask, input.GetArgumentAtIndex(opt_arg_pos), |
| 857 | match_start_point, max_return_elements, filter_ap.get(), word_complete, |
| 858 | matches); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 859 | } |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 860 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 861 | void OptionGroupOptions::Append(OptionGroup *group) { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 862 | auto group_option_defs = group->GetDefinitions(); |
| 863 | for (uint32_t i = 0; i < group_option_defs.size(); ++i) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 864 | m_option_infos.push_back(OptionInfo(group, i)); |
| 865 | m_option_defs.push_back(group_option_defs[i]); |
| 866 | } |
Greg Clayton | 84c3966 | 2011-04-27 22:04:39 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 869 | const OptionGroup *OptionGroupOptions::GetGroupWithOption(char short_opt) { |
| 870 | for (uint32_t i = 0; i < m_option_defs.size(); i++) { |
| 871 | OptionDefinition opt_def = m_option_defs[i]; |
| 872 | if (opt_def.short_option == short_opt) |
| 873 | return m_option_infos[i].option_group; |
| 874 | } |
| 875 | return nullptr; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 878 | void OptionGroupOptions::Append(OptionGroup *group, uint32_t src_mask, |
| 879 | uint32_t dst_mask) { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 880 | auto group_option_defs = group->GetDefinitions(); |
| 881 | for (uint32_t i = 0; i < group_option_defs.size(); ++i) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 882 | if (group_option_defs[i].usage_mask & src_mask) { |
| 883 | m_option_infos.push_back(OptionInfo(group, i)); |
| 884 | m_option_defs.push_back(group_option_defs[i]); |
| 885 | m_option_defs.back().usage_mask = dst_mask; |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 886 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 887 | } |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 890 | void OptionGroupOptions::Finalize() { |
| 891 | m_did_finalize = true; |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 894 | Status OptionGroupOptions::SetOptionValue(uint32_t option_idx, |
| 895 | llvm::StringRef option_value, |
| 896 | ExecutionContext *execution_context) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 897 | // After calling OptionGroupOptions::Append(...), you must finalize the |
| 898 | // groups by calling OptionGroupOptions::Finlize() |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 899 | assert(m_did_finalize); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 900 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 901 | if (option_idx < m_option_infos.size()) { |
| 902 | error = m_option_infos[option_idx].option_group->SetOptionValue( |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 903 | m_option_infos[option_idx].option_index, option_value, |
| 904 | execution_context); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 905 | |
| 906 | } else { |
| 907 | error.SetErrorString("invalid option index"); // Shouldn't happen... |
| 908 | } |
| 909 | return error; |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 912 | void OptionGroupOptions::OptionParsingStarting( |
| 913 | ExecutionContext *execution_context) { |
| 914 | std::set<OptionGroup *> group_set; |
| 915 | OptionInfos::iterator pos, end = m_option_infos.end(); |
| 916 | for (pos = m_option_infos.begin(); pos != end; ++pos) { |
| 917 | OptionGroup *group = pos->option_group; |
| 918 | if (group_set.find(group) == group_set.end()) { |
| 919 | group->OptionParsingStarting(execution_context); |
| 920 | group_set.insert(group); |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 921 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 922 | } |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 923 | } |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 924 | Status |
| 925 | OptionGroupOptions::OptionParsingFinished(ExecutionContext *execution_context) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 926 | std::set<OptionGroup *> group_set; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 927 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 928 | OptionInfos::iterator pos, end = m_option_infos.end(); |
| 929 | for (pos = m_option_infos.begin(); pos != end; ++pos) { |
| 930 | OptionGroup *group = pos->option_group; |
| 931 | if (group_set.find(group) == group_set.end()) { |
| 932 | error = group->OptionParsingFinished(execution_context); |
| 933 | group_set.insert(group); |
| 934 | if (error.Fail()) |
| 935 | return error; |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 936 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 937 | } |
| 938 | return error; |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 939 | } |
Pavel Labath | 5f56fca | 2018-03-09 10:39:40 +0000 | [diff] [blame] | 940 | |
| 941 | // OptionParser permutes the arguments while processing them, so we create a |
| 942 | // temporary array holding to avoid modification of the input arguments. The |
| 943 | // options themselves are never modified, but the API expects a char * anyway, |
| 944 | // hence the const_cast. |
| 945 | static std::vector<char *> GetArgvForParsing(const Args &args) { |
| 946 | std::vector<char *> result; |
| 947 | // OptionParser always skips the first argument as it is based on getopt(). |
| 948 | result.push_back(const_cast<char *>("<FAKE-ARG0>")); |
| 949 | for (const Args::ArgEntry &entry : args) |
| 950 | result.push_back(const_cast<char *>(entry.c_str())); |
| 951 | return result; |
| 952 | } |
| 953 | |
| 954 | // Given a permuted argument, find it's position in the original Args vector. |
| 955 | static Args::const_iterator FindOriginalIter(const char *arg, |
| 956 | const Args &original) { |
| 957 | return llvm::find_if( |
| 958 | original, [arg](const Args::ArgEntry &D) { return D.c_str() == arg; }); |
| 959 | } |
| 960 | |
| 961 | // Given a permuted argument, find it's index in the original Args vector. |
| 962 | static size_t FindOriginalIndex(const char *arg, const Args &original) { |
| 963 | return std::distance(original.begin(), FindOriginalIter(arg, original)); |
| 964 | } |
| 965 | |
| 966 | // Construct a new Args object, consisting of the entries from the original |
| 967 | // arguments, but in the permuted order. |
| 968 | static Args ReconstituteArgsAfterParsing(llvm::ArrayRef<char *> parsed, |
| 969 | const Args &original) { |
| 970 | Args result; |
| 971 | for (const char *arg : parsed) { |
| 972 | auto pos = FindOriginalIter(arg, original); |
| 973 | assert(pos != original.end()); |
| 974 | result.AppendArgument(pos->ref, pos->quote); |
| 975 | } |
| 976 | return result; |
| 977 | } |
| 978 | |
| 979 | static size_t FindArgumentIndexForOption(const Args &args, |
| 980 | const Option &long_option) { |
| 981 | std::string short_opt = llvm::formatv("-{0}", char(long_option.val)).str(); |
| 982 | std::string long_opt = |
| 983 | llvm::formatv("--{0}", long_option.definition->long_option); |
| 984 | for (const auto &entry : llvm::enumerate(args)) { |
| 985 | if (entry.value().ref.startswith(short_opt) || |
| 986 | entry.value().ref.startswith(long_opt)) |
| 987 | return entry.index(); |
| 988 | } |
| 989 | |
| 990 | return size_t(-1); |
| 991 | } |
| 992 | |
| 993 | llvm::Expected<Args> Options::ParseAlias(const Args &args, |
| 994 | OptionArgVector *option_arg_vector, |
| 995 | std::string &input_line) { |
| 996 | StreamString sstr; |
| 997 | int i; |
| 998 | Option *long_options = GetLongOptions(); |
| 999 | |
| 1000 | if (long_options == nullptr) { |
| 1001 | return llvm::make_error<llvm::StringError>("Invalid long options", |
| 1002 | llvm::inconvertibleErrorCode()); |
| 1003 | } |
| 1004 | |
| 1005 | for (i = 0; long_options[i].definition != nullptr; ++i) { |
| 1006 | if (long_options[i].flag == nullptr) { |
| 1007 | sstr << (char)long_options[i].val; |
| 1008 | switch (long_options[i].definition->option_has_arg) { |
| 1009 | default: |
| 1010 | case OptionParser::eNoArgument: |
| 1011 | break; |
| 1012 | case OptionParser::eRequiredArgument: |
| 1013 | sstr << ":"; |
| 1014 | break; |
| 1015 | case OptionParser::eOptionalArgument: |
| 1016 | sstr << "::"; |
| 1017 | break; |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | Args args_copy = args; |
| 1023 | std::vector<char *> argv = GetArgvForParsing(args); |
| 1024 | |
| 1025 | std::unique_lock<std::mutex> lock; |
| 1026 | OptionParser::Prepare(lock); |
| 1027 | int val; |
| 1028 | while (1) { |
| 1029 | int long_options_index = -1; |
| 1030 | val = OptionParser::Parse(argv.size(), &*argv.begin(), sstr.GetString(), |
| 1031 | long_options, &long_options_index); |
| 1032 | |
| 1033 | if (val == -1) |
| 1034 | break; |
| 1035 | |
| 1036 | if (val == '?') { |
| 1037 | return llvm::make_error<llvm::StringError>( |
| 1038 | "Unknown or ambiguous option", llvm::inconvertibleErrorCode()); |
| 1039 | } |
| 1040 | |
| 1041 | if (val == 0) |
| 1042 | continue; |
| 1043 | |
| 1044 | OptionSeen(val); |
| 1045 | |
| 1046 | // Look up the long option index |
| 1047 | if (long_options_index == -1) { |
| 1048 | for (int j = 0; long_options[j].definition || long_options[j].flag || |
| 1049 | long_options[j].val; |
| 1050 | ++j) { |
| 1051 | if (long_options[j].val == val) { |
| 1052 | long_options_index = j; |
| 1053 | break; |
| 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | // See if the option takes an argument, and see if one was supplied. |
| 1059 | if (long_options_index == -1) { |
| 1060 | return llvm::make_error<llvm::StringError>( |
| 1061 | llvm::formatv("Invalid option with value '{0}'.", char(val)).str(), |
| 1062 | llvm::inconvertibleErrorCode()); |
| 1063 | } |
| 1064 | |
| 1065 | StreamString option_str; |
| 1066 | option_str.Printf("-%c", val); |
| 1067 | const OptionDefinition *def = long_options[long_options_index].definition; |
| 1068 | int has_arg = |
| 1069 | (def == nullptr) ? OptionParser::eNoArgument : def->option_has_arg; |
| 1070 | |
| 1071 | const char *option_arg = nullptr; |
| 1072 | switch (has_arg) { |
| 1073 | case OptionParser::eRequiredArgument: |
| 1074 | if (OptionParser::GetOptionArgument() == nullptr) { |
| 1075 | return llvm::make_error<llvm::StringError>( |
| 1076 | llvm::formatv("Option '{0}' is missing argument specifier.", |
| 1077 | option_str.GetString()) |
| 1078 | .str(), |
| 1079 | llvm::inconvertibleErrorCode()); |
| 1080 | } |
| 1081 | LLVM_FALLTHROUGH; |
| 1082 | case OptionParser::eOptionalArgument: |
| 1083 | option_arg = OptionParser::GetOptionArgument(); |
| 1084 | LLVM_FALLTHROUGH; |
| 1085 | case OptionParser::eNoArgument: |
| 1086 | break; |
| 1087 | default: |
| 1088 | return llvm::make_error<llvm::StringError>( |
| 1089 | llvm::formatv("error with options table; invalid value in has_arg " |
| 1090 | "field for option '{0}'.", |
| 1091 | char(val)) |
| 1092 | .str(), |
| 1093 | llvm::inconvertibleErrorCode()); |
| 1094 | } |
| 1095 | if (!option_arg) |
| 1096 | option_arg = "<no-argument>"; |
| 1097 | option_arg_vector->emplace_back(option_str.GetString(), has_arg, |
| 1098 | option_arg); |
| 1099 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1100 | // Find option in the argument list; also see if it was supposed to take an |
| 1101 | // argument and if one was supplied. Remove option (and argument, if |
Pavel Labath | 5f56fca | 2018-03-09 10:39:40 +0000 | [diff] [blame] | 1102 | // given) from the argument list. Also remove them from the |
| 1103 | // raw_input_string, if one was passed in. |
| 1104 | size_t idx = |
| 1105 | FindArgumentIndexForOption(args_copy, long_options[long_options_index]); |
| 1106 | if (idx == size_t(-1)) |
| 1107 | continue; |
| 1108 | |
| 1109 | if (!input_line.empty()) { |
| 1110 | auto tmp_arg = args_copy[idx].ref; |
| 1111 | size_t pos = input_line.find(tmp_arg); |
| 1112 | if (pos != std::string::npos) |
| 1113 | input_line.erase(pos, tmp_arg.size()); |
| 1114 | } |
| 1115 | args_copy.DeleteArgumentAtIndex(idx); |
| 1116 | if ((long_options[long_options_index].definition->option_has_arg != |
| 1117 | OptionParser::eNoArgument) && |
| 1118 | (OptionParser::GetOptionArgument() != nullptr) && |
| 1119 | (idx < args_copy.GetArgumentCount()) && |
| 1120 | (args_copy[idx].ref == OptionParser::GetOptionArgument())) { |
| 1121 | if (input_line.size() > 0) { |
| 1122 | auto tmp_arg = args_copy[idx].ref; |
| 1123 | size_t pos = input_line.find(tmp_arg); |
| 1124 | if (pos != std::string::npos) |
| 1125 | input_line.erase(pos, tmp_arg.size()); |
| 1126 | } |
| 1127 | args_copy.DeleteArgumentAtIndex(idx); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | return std::move(args_copy); |
| 1132 | } |
| 1133 | |
| 1134 | OptionElementVector Options::ParseForCompletion(const Args &args, |
| 1135 | uint32_t cursor_index) { |
| 1136 | OptionElementVector option_element_vector; |
| 1137 | StreamString sstr; |
| 1138 | Option *long_options = GetLongOptions(); |
| 1139 | option_element_vector.clear(); |
| 1140 | |
| 1141 | if (long_options == nullptr) |
| 1142 | return option_element_vector; |
| 1143 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1144 | // Leading : tells getopt to return a : for a missing option argument AND to |
| 1145 | // suppress error messages. |
Pavel Labath | 5f56fca | 2018-03-09 10:39:40 +0000 | [diff] [blame] | 1146 | |
| 1147 | sstr << ":"; |
| 1148 | for (int i = 0; long_options[i].definition != nullptr; ++i) { |
| 1149 | if (long_options[i].flag == nullptr) { |
| 1150 | sstr << (char)long_options[i].val; |
| 1151 | switch (long_options[i].definition->option_has_arg) { |
| 1152 | default: |
| 1153 | case OptionParser::eNoArgument: |
| 1154 | break; |
| 1155 | case OptionParser::eRequiredArgument: |
| 1156 | sstr << ":"; |
| 1157 | break; |
| 1158 | case OptionParser::eOptionalArgument: |
| 1159 | sstr << "::"; |
| 1160 | break; |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | std::unique_lock<std::mutex> lock; |
| 1166 | OptionParser::Prepare(lock); |
| 1167 | OptionParser::EnableError(false); |
| 1168 | |
| 1169 | int val; |
| 1170 | auto opt_defs = GetDefinitions(); |
| 1171 | |
| 1172 | std::vector<char *> dummy_vec = GetArgvForParsing(args); |
| 1173 | |
| 1174 | // I stick an element on the end of the input, because if the last element |
| 1175 | // is option that requires an argument, getopt_long_only will freak out. |
| 1176 | dummy_vec.push_back(const_cast<char *>("<FAKE-VALUE>")); |
| 1177 | |
| 1178 | bool failed_once = false; |
| 1179 | uint32_t dash_dash_pos = -1; |
| 1180 | |
| 1181 | while (1) { |
| 1182 | bool missing_argument = false; |
| 1183 | int long_options_index = -1; |
| 1184 | |
| 1185 | val = OptionParser::Parse(dummy_vec.size(), &dummy_vec[0], sstr.GetString(), |
| 1186 | long_options, &long_options_index); |
| 1187 | |
| 1188 | if (val == -1) { |
| 1189 | // When we're completing a "--" which is the last option on line, |
| 1190 | if (failed_once) |
| 1191 | break; |
| 1192 | |
| 1193 | failed_once = true; |
| 1194 | |
| 1195 | // If this is a bare "--" we mark it as such so we can complete it |
| 1196 | // successfully later. Handling the "--" is a little tricky, since that |
| 1197 | // may mean end of options or arguments, or the user might want to |
| 1198 | // complete options by long name. I make this work by checking whether |
| 1199 | // the cursor is in the "--" argument, and if so I assume we're |
| 1200 | // completing the long option, otherwise I let it pass to |
| 1201 | // OptionParser::Parse which will terminate the option parsing. Note, in |
| 1202 | // either case we continue parsing the line so we can figure out what |
| 1203 | // other options were passed. This will be useful when we come to |
| 1204 | // restricting completions based on what other options we've seen on the |
| 1205 | // line. |
| 1206 | |
| 1207 | if (static_cast<size_t>(OptionParser::GetOptionIndex()) < |
| 1208 | dummy_vec.size() && |
| 1209 | (strcmp(dummy_vec[OptionParser::GetOptionIndex() - 1], "--") == 0)) { |
| 1210 | dash_dash_pos = FindOriginalIndex( |
| 1211 | dummy_vec[OptionParser::GetOptionIndex() - 1], args); |
| 1212 | if (dash_dash_pos == cursor_index) { |
| 1213 | option_element_vector.push_back( |
| 1214 | OptionArgElement(OptionArgElement::eBareDoubleDash, dash_dash_pos, |
| 1215 | OptionArgElement::eBareDoubleDash)); |
| 1216 | continue; |
| 1217 | } else |
| 1218 | break; |
| 1219 | } else |
| 1220 | break; |
| 1221 | } else if (val == '?') { |
| 1222 | option_element_vector.push_back(OptionArgElement( |
| 1223 | OptionArgElement::eUnrecognizedArg, |
| 1224 | FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1], |
| 1225 | args), |
| 1226 | OptionArgElement::eUnrecognizedArg)); |
| 1227 | continue; |
| 1228 | } else if (val == 0) { |
| 1229 | continue; |
| 1230 | } else if (val == ':') { |
| 1231 | // This is a missing argument. |
| 1232 | val = OptionParser::GetOptionErrorCause(); |
| 1233 | missing_argument = true; |
| 1234 | } |
| 1235 | |
| 1236 | OptionSeen(val); |
| 1237 | |
| 1238 | // Look up the long option index |
| 1239 | if (long_options_index == -1) { |
| 1240 | for (int j = 0; long_options[j].definition || long_options[j].flag || |
| 1241 | long_options[j].val; |
| 1242 | ++j) { |
| 1243 | if (long_options[j].val == val) { |
| 1244 | long_options_index = j; |
| 1245 | break; |
| 1246 | } |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | // See if the option takes an argument, and see if one was supplied. |
| 1251 | if (long_options_index >= 0) { |
| 1252 | int opt_defs_index = -1; |
| 1253 | for (size_t i = 0; i < opt_defs.size(); i++) { |
| 1254 | if (opt_defs[i].short_option != val) |
| 1255 | continue; |
| 1256 | opt_defs_index = i; |
| 1257 | break; |
| 1258 | } |
| 1259 | |
| 1260 | const OptionDefinition *def = long_options[long_options_index].definition; |
| 1261 | int has_arg = |
| 1262 | (def == nullptr) ? OptionParser::eNoArgument : def->option_has_arg; |
| 1263 | switch (has_arg) { |
| 1264 | case OptionParser::eNoArgument: |
| 1265 | option_element_vector.push_back(OptionArgElement( |
| 1266 | opt_defs_index, |
| 1267 | FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1], |
| 1268 | args), |
| 1269 | 0)); |
| 1270 | break; |
| 1271 | case OptionParser::eRequiredArgument: |
| 1272 | if (OptionParser::GetOptionArgument() != nullptr) { |
| 1273 | int arg_index; |
| 1274 | if (missing_argument) |
| 1275 | arg_index = -1; |
| 1276 | else |
| 1277 | arg_index = OptionParser::GetOptionIndex() - 2; |
| 1278 | |
| 1279 | option_element_vector.push_back(OptionArgElement( |
| 1280 | opt_defs_index, |
| 1281 | FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 2], |
| 1282 | args), |
| 1283 | arg_index)); |
| 1284 | } else { |
| 1285 | option_element_vector.push_back(OptionArgElement( |
| 1286 | opt_defs_index, |
| 1287 | FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1], |
| 1288 | args), |
| 1289 | -1)); |
| 1290 | } |
| 1291 | break; |
| 1292 | case OptionParser::eOptionalArgument: |
| 1293 | if (OptionParser::GetOptionArgument() != nullptr) { |
| 1294 | option_element_vector.push_back(OptionArgElement( |
| 1295 | opt_defs_index, |
| 1296 | FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 2], |
| 1297 | args), |
| 1298 | FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1], |
| 1299 | args))); |
| 1300 | } else { |
| 1301 | option_element_vector.push_back(OptionArgElement( |
| 1302 | opt_defs_index, |
| 1303 | FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 2], |
| 1304 | args), |
| 1305 | FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1], |
| 1306 | args))); |
| 1307 | } |
| 1308 | break; |
| 1309 | default: |
| 1310 | // The options table is messed up. Here we'll just continue |
| 1311 | option_element_vector.push_back(OptionArgElement( |
| 1312 | OptionArgElement::eUnrecognizedArg, |
| 1313 | FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1], |
| 1314 | args), |
| 1315 | OptionArgElement::eUnrecognizedArg)); |
| 1316 | break; |
| 1317 | } |
| 1318 | } else { |
| 1319 | option_element_vector.push_back(OptionArgElement( |
| 1320 | OptionArgElement::eUnrecognizedArg, |
| 1321 | FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1], |
| 1322 | args), |
| 1323 | OptionArgElement::eUnrecognizedArg)); |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | // Finally we have to handle the case where the cursor index points at a |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1328 | // single "-". We want to mark that in the option_element_vector, but only |
| 1329 | // if it is not after the "--". But it turns out that OptionParser::Parse |
| 1330 | // just ignores an isolated "-". So we have to look it up by hand here. We |
| 1331 | // only care if it is AT the cursor position. Note, a single quoted dash is |
| 1332 | // not the same as a single dash... |
Pavel Labath | 5f56fca | 2018-03-09 10:39:40 +0000 | [diff] [blame] | 1333 | |
| 1334 | const Args::ArgEntry &cursor = args[cursor_index]; |
| 1335 | if ((static_cast<int32_t>(dash_dash_pos) == -1 || |
| 1336 | cursor_index < dash_dash_pos) && |
Raphael Isemann | 3a0e127 | 2018-07-10 20:17:38 +0000 | [diff] [blame] | 1337 | !cursor.IsQuoted() && cursor.ref == "-") { |
Pavel Labath | 5f56fca | 2018-03-09 10:39:40 +0000 | [diff] [blame] | 1338 | option_element_vector.push_back( |
| 1339 | OptionArgElement(OptionArgElement::eBareDash, cursor_index, |
| 1340 | OptionArgElement::eBareDash)); |
| 1341 | } |
| 1342 | return option_element_vector; |
| 1343 | } |
| 1344 | |
| 1345 | llvm::Expected<Args> Options::Parse(const Args &args, |
| 1346 | ExecutionContext *execution_context, |
| 1347 | lldb::PlatformSP platform_sp, |
| 1348 | bool require_validation) { |
| 1349 | StreamString sstr; |
| 1350 | Status error; |
| 1351 | Option *long_options = GetLongOptions(); |
| 1352 | if (long_options == nullptr) { |
| 1353 | return llvm::make_error<llvm::StringError>("Invalid long options.", |
| 1354 | llvm::inconvertibleErrorCode()); |
| 1355 | } |
| 1356 | |
| 1357 | for (int i = 0; long_options[i].definition != nullptr; ++i) { |
| 1358 | if (long_options[i].flag == nullptr) { |
| 1359 | if (isprint8(long_options[i].val)) { |
| 1360 | sstr << (char)long_options[i].val; |
| 1361 | switch (long_options[i].definition->option_has_arg) { |
| 1362 | default: |
| 1363 | case OptionParser::eNoArgument: |
| 1364 | break; |
| 1365 | case OptionParser::eRequiredArgument: |
| 1366 | sstr << ':'; |
| 1367 | break; |
| 1368 | case OptionParser::eOptionalArgument: |
| 1369 | sstr << "::"; |
| 1370 | break; |
| 1371 | } |
| 1372 | } |
| 1373 | } |
| 1374 | } |
| 1375 | std::vector<char *> argv = GetArgvForParsing(args); |
| 1376 | std::unique_lock<std::mutex> lock; |
| 1377 | OptionParser::Prepare(lock); |
| 1378 | int val; |
| 1379 | while (1) { |
| 1380 | int long_options_index = -1; |
| 1381 | val = OptionParser::Parse(argv.size(), &*argv.begin(), sstr.GetString(), |
| 1382 | long_options, &long_options_index); |
| 1383 | if (val == -1) |
| 1384 | break; |
| 1385 | |
| 1386 | // Did we get an error? |
| 1387 | if (val == '?') { |
| 1388 | error.SetErrorStringWithFormat("unknown or ambiguous option"); |
| 1389 | break; |
| 1390 | } |
| 1391 | // The option auto-set itself |
| 1392 | if (val == 0) |
| 1393 | continue; |
| 1394 | |
| 1395 | OptionSeen(val); |
| 1396 | |
| 1397 | // Lookup the long option index |
| 1398 | if (long_options_index == -1) { |
| 1399 | for (int i = 0; long_options[i].definition || long_options[i].flag || |
| 1400 | long_options[i].val; |
| 1401 | ++i) { |
| 1402 | if (long_options[i].val == val) { |
| 1403 | long_options_index = i; |
| 1404 | break; |
| 1405 | } |
| 1406 | } |
| 1407 | } |
| 1408 | // Call the callback with the option |
| 1409 | if (long_options_index >= 0 && |
| 1410 | long_options[long_options_index].definition) { |
| 1411 | const OptionDefinition *def = long_options[long_options_index].definition; |
| 1412 | |
| 1413 | if (!platform_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1414 | // User did not pass in an explicit platform. Try to grab from the |
| 1415 | // execution context. |
Pavel Labath | 5f56fca | 2018-03-09 10:39:40 +0000 | [diff] [blame] | 1416 | TargetSP target_sp = |
| 1417 | execution_context ? execution_context->GetTargetSP() : TargetSP(); |
| 1418 | platform_sp = target_sp ? target_sp->GetPlatform() : PlatformSP(); |
| 1419 | } |
| 1420 | OptionValidator *validator = def->validator; |
| 1421 | |
| 1422 | if (!platform_sp && require_validation) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1423 | // Caller requires validation but we cannot validate as we don't have |
| 1424 | // the mandatory platform against which to validate. |
Pavel Labath | 5f56fca | 2018-03-09 10:39:40 +0000 | [diff] [blame] | 1425 | return llvm::make_error<llvm::StringError>( |
| 1426 | "cannot validate options: no platform available", |
| 1427 | llvm::inconvertibleErrorCode()); |
| 1428 | } |
| 1429 | |
| 1430 | bool validation_failed = false; |
| 1431 | if (platform_sp) { |
| 1432 | // Ensure we have an execution context, empty or not. |
| 1433 | ExecutionContext dummy_context; |
| 1434 | ExecutionContext *exe_ctx_p = |
| 1435 | execution_context ? execution_context : &dummy_context; |
| 1436 | if (validator && !validator->IsValid(*platform_sp, *exe_ctx_p)) { |
| 1437 | validation_failed = true; |
| 1438 | error.SetErrorStringWithFormat("Option \"%s\" invalid. %s", |
| 1439 | def->long_option, |
| 1440 | def->validator->LongConditionString()); |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | // As long as validation didn't fail, we set the option value. |
| 1445 | if (!validation_failed) |
| 1446 | error = |
| 1447 | SetOptionValue(long_options_index, |
| 1448 | (def->option_has_arg == OptionParser::eNoArgument) |
| 1449 | ? nullptr |
| 1450 | : OptionParser::GetOptionArgument(), |
| 1451 | execution_context); |
| 1452 | } else { |
| 1453 | error.SetErrorStringWithFormat("invalid option with value '%i'", val); |
| 1454 | } |
| 1455 | if (error.Fail()) |
| 1456 | return error.ToError(); |
| 1457 | } |
| 1458 | |
| 1459 | argv.erase(argv.begin(), argv.begin() + OptionParser::GetOptionIndex()); |
| 1460 | return ReconstituteArgsAfterParsing(argv, args); |
| 1461 | } |