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 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 21 | #include "lldb/Core/StreamString.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" |
| 27 | |
| 28 | using namespace lldb; |
| 29 | using namespace lldb_private; |
| 30 | |
| 31 | //------------------------------------------------------------------------- |
| 32 | // Options |
| 33 | //------------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 34 | Options::Options() : m_getopt_table() { BuildValidOptionSets(); } |
| 35 | |
| 36 | Options::~Options() {} |
| 37 | |
| 38 | void Options::NotifyOptionParsingStarting(ExecutionContext *execution_context) { |
| 39 | m_seen_options.clear(); |
| 40 | // Let the subclass reset its option values |
| 41 | OptionParsingStarting(execution_context); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 44 | Error Options::NotifyOptionParsingFinished( |
| 45 | ExecutionContext *execution_context) { |
| 46 | return OptionParsingFinished(execution_context); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | void Options::OptionSeen(int option_idx) { m_seen_options.insert(option_idx); } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 50 | |
| 51 | // Returns true is set_a is a subset of set_b; Otherwise returns false. |
| 52 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | bool Options::IsASubset(const OptionSet &set_a, const OptionSet &set_b) { |
| 54 | bool is_a_subset = true; |
| 55 | OptionSet::const_iterator pos_a; |
| 56 | OptionSet::const_iterator pos_b; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 57 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 58 | // set_a is a subset of set_b if every member of set_a is also a member of |
| 59 | // set_b |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 60 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | for (pos_a = set_a.begin(); pos_a != set_a.end() && is_a_subset; ++pos_a) { |
| 62 | pos_b = set_b.find(*pos_a); |
| 63 | if (pos_b == set_b.end()) |
| 64 | is_a_subset = false; |
| 65 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 66 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | return is_a_subset; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | // Returns the set difference set_a - set_b, i.e. { x | ElementOf (x, set_a) && |
| 71 | // !ElementOf (x, set_b) } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 72 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | size_t Options::OptionsSetDiff(const OptionSet &set_a, const OptionSet &set_b, |
| 74 | OptionSet &diffs) { |
| 75 | size_t num_diffs = 0; |
| 76 | OptionSet::const_iterator pos_a; |
| 77 | OptionSet::const_iterator pos_b; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 78 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 79 | for (pos_a = set_a.begin(); pos_a != set_a.end(); ++pos_a) { |
| 80 | pos_b = set_b.find(*pos_a); |
| 81 | if (pos_b == set_b.end()) { |
| 82 | ++num_diffs; |
| 83 | diffs.insert(*pos_a); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 85 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 86 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | return num_diffs; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | // Returns the union of set_a and set_b. Does not put duplicate members into |
| 91 | // the union. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | void Options::OptionsSetUnion(const OptionSet &set_a, const OptionSet &set_b, |
| 94 | OptionSet &union_set) { |
| 95 | OptionSet::const_iterator pos; |
| 96 | OptionSet::iterator pos_union; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 97 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 98 | // Put all the elements of set_a into the union. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 99 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 100 | for (pos = set_a.begin(); pos != set_a.end(); ++pos) |
| 101 | union_set.insert(*pos); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 102 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 103 | // Put all the elements of set_b that are not already there into the union. |
| 104 | for (pos = set_b.begin(); pos != set_b.end(); ++pos) { |
| 105 | pos_union = union_set.find(*pos); |
| 106 | if (pos_union == union_set.end()) |
| 107 | union_set.insert(*pos); |
| 108 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 111 | bool Options::VerifyOptions(CommandReturnObject &result) { |
| 112 | bool options_are_valid = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | int num_levels = GetRequiredOptions().size(); |
| 115 | if (num_levels) { |
| 116 | for (int i = 0; i < num_levels && !options_are_valid; ++i) { |
| 117 | // This is the correct set of options if: 1). m_seen_options contains all |
| 118 | // of m_required_options[i] |
| 119 | // (i.e. all the required options at this level are a subset of |
| 120 | // m_seen_options); AND |
| 121 | // 2). { m_seen_options - m_required_options[i] is a subset of |
| 122 | // m_options_options[i] (i.e. all the rest of |
| 123 | // m_seen_options are in the set of optional options at this 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 |
| 154 | // that ends up being |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 155 | // a performance problem. |
| 156 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 157 | void Options::BuildValidOptionSets() { |
| 158 | // Check to see if we already did this. |
| 159 | if (m_required_options.size() != 0) |
| 160 | return; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 161 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 162 | // Check to see if there are any options. |
| 163 | int num_options = NumCommandOptions(); |
| 164 | if (num_options == 0) |
| 165 | return; |
| 166 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 167 | auto opt_defs = GetDefinitions(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 168 | m_required_options.resize(1); |
| 169 | m_optional_options.resize(1); |
| 170 | |
| 171 | // First count the number of option sets we've got. Ignore |
| 172 | // LLDB_ALL_OPTION_SETS... |
| 173 | |
| 174 | uint32_t num_option_sets = 0; |
| 175 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 176 | for (const auto &def : opt_defs) { |
| 177 | uint32_t this_usage_mask = def.usage_mask; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 178 | if (this_usage_mask == LLDB_OPT_SET_ALL) { |
| 179 | if (num_option_sets == 0) |
| 180 | num_option_sets = 1; |
| 181 | } else { |
| 182 | for (uint32_t j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++) { |
| 183 | if (this_usage_mask & (1 << j)) { |
| 184 | if (num_option_sets <= j) |
| 185 | num_option_sets = j + 1; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | if (num_option_sets > 0) { |
| 192 | m_required_options.resize(num_option_sets); |
| 193 | m_optional_options.resize(num_option_sets); |
| 194 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 195 | for (const auto &def : opt_defs) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 196 | for (uint32_t j = 0; j < num_option_sets; j++) { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 197 | if (def.usage_mask & 1 << j) { |
| 198 | if (def.required) |
| 199 | m_required_options[j].insert(def.short_option); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 200 | else |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 201 | m_optional_options[j].insert(def.short_option); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 208 | uint32_t Options::NumCommandOptions() { return GetDefinitions().size(); } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | |
| 210 | Option *Options::GetLongOptions() { |
| 211 | // Check to see if this has already been done. |
| 212 | if (m_getopt_table.empty()) { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 213 | auto defs = GetDefinitions(); |
| 214 | if (defs.empty()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 215 | return nullptr; |
| 216 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 217 | std::map<int, uint32_t> option_seen; |
Enrico Granata | 4ebb8a4 | 2016-03-15 01:17:32 +0000 | [diff] [blame] | 218 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 219 | m_getopt_table.resize(defs.size() + 1); |
| 220 | for (size_t i = 0; i < defs.size(); ++i) { |
| 221 | const int short_opt = defs[i].short_option; |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 222 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 223 | m_getopt_table[i].definition = &defs[i]; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 224 | m_getopt_table[i].flag = nullptr; |
| 225 | m_getopt_table[i].val = short_opt; |
Enrico Granata | bef55ac | 2016-03-14 22:17:04 +0000 | [diff] [blame] | 226 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 227 | if (option_seen.find(short_opt) == option_seen.end()) { |
| 228 | option_seen[short_opt] = i; |
| 229 | } else if (short_opt) { |
| 230 | m_getopt_table[i].val = 0; |
| 231 | std::map<int, uint32_t>::const_iterator pos = |
| 232 | option_seen.find(short_opt); |
| 233 | StreamString strm; |
| 234 | if (isprint8(short_opt)) |
| 235 | Host::SystemLog(Host::eSystemLogError, |
| 236 | "option[%u] --%s has a short option -%c that " |
| 237 | "conflicts with option[%u] --%s, short option won't " |
| 238 | "be used for --%s\n", |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 239 | (int)i, defs[i].long_option, short_opt, pos->second, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 240 | m_getopt_table[pos->second].definition->long_option, |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 241 | defs[i].long_option); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 242 | else |
| 243 | Host::SystemLog(Host::eSystemLogError, |
| 244 | "option[%u] --%s has a short option 0x%x that " |
| 245 | "conflicts with option[%u] --%s, short option won't " |
| 246 | "be used for --%s\n", |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 247 | (int)i, defs[i].long_option, short_opt, pos->second, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 248 | m_getopt_table[pos->second].definition->long_option, |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 249 | defs[i].long_option); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 250 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 253 | // getopt_long_only requires a NULL final entry in the table: |
| 254 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 255 | m_getopt_table.back().definition = nullptr; |
| 256 | m_getopt_table.back().flag = nullptr; |
| 257 | m_getopt_table.back().val = 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | if (m_getopt_table.empty()) |
| 261 | return nullptr; |
| 262 | |
| 263 | return &m_getopt_table.front(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 266 | // This function takes INDENT, which tells how many spaces to output at the |
| 267 | // front of each line; SPACES, which is |
| 268 | // a string containing 80 spaces; and TEXT, which is the text that is to be |
| 269 | // output. It outputs the text, on |
| 270 | // multiple lines if necessary, to RESULT, with INDENT spaces at the front of |
| 271 | // each line. It breaks lines on spaces, |
| 272 | // tabs or newlines, shortening the line if necessary to not break in the middle |
| 273 | // of a word. It assumes that each |
| 274 | // output line should contain a maximum of OUTPUT_MAX_COLUMNS characters. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 275 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 276 | void Options::OutputFormattedUsageText(Stream &strm, |
| 277 | const OptionDefinition &option_def, |
| 278 | uint32_t output_max_columns) { |
| 279 | std::string actual_text; |
| 280 | if (option_def.validator) { |
| 281 | const char *condition = option_def.validator->ShortConditionString(); |
| 282 | if (condition) { |
| 283 | actual_text = "["; |
| 284 | actual_text.append(condition); |
| 285 | actual_text.append("] "); |
| 286 | } |
| 287 | } |
| 288 | actual_text.append(option_def.usage_text); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 289 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 290 | // Will it all fit on one line? |
| 291 | |
| 292 | if (static_cast<uint32_t>(actual_text.length() + strm.GetIndentLevel()) < |
| 293 | output_max_columns) { |
| 294 | // Output it as a single line. |
| 295 | strm.Indent(actual_text.c_str()); |
| 296 | strm.EOL(); |
| 297 | } else { |
| 298 | // We need to break it up into multiple lines. |
| 299 | |
| 300 | int text_width = output_max_columns - strm.GetIndentLevel() - 1; |
| 301 | int start = 0; |
| 302 | int end = start; |
| 303 | int final_end = actual_text.length(); |
| 304 | int sub_len; |
| 305 | |
| 306 | while (end < final_end) { |
| 307 | // Don't start the 'text' on a space, since we're already outputting the |
| 308 | // indentation. |
| 309 | while ((start < final_end) && (actual_text[start] == ' ')) |
| 310 | start++; |
| 311 | |
| 312 | end = start + text_width; |
| 313 | if (end > final_end) |
| 314 | end = final_end; |
| 315 | else { |
| 316 | // If we're not at the end of the text, make sure we break the line on |
| 317 | // white space. |
| 318 | while (end > start && actual_text[end] != ' ' && |
| 319 | actual_text[end] != '\t' && actual_text[end] != '\n') |
| 320 | end--; |
| 321 | } |
| 322 | |
| 323 | sub_len = end - start; |
| 324 | if (start != 0) |
| 325 | strm.EOL(); |
| 326 | strm.Indent(); |
| 327 | assert(start < final_end); |
| 328 | assert(start + sub_len <= final_end); |
| 329 | strm.Write(actual_text.c_str() + start, sub_len); |
| 330 | start = end + 1; |
| 331 | } |
| 332 | strm.EOL(); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | bool Options::SupportsLongOption(const char *long_option) { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 337 | if (!long_option || !long_option[0]) |
| 338 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 339 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 340 | auto opt_defs = GetDefinitions(); |
| 341 | if (opt_defs.empty()) |
| 342 | return false; |
| 343 | |
| 344 | const char *long_option_name = long_option; |
| 345 | if (long_option[0] == '-' && long_option[1] == '-') |
| 346 | long_option_name += 2; |
| 347 | |
| 348 | for (auto &def : opt_defs) { |
| 349 | if (!def.long_option) |
| 350 | continue; |
| 351 | |
| 352 | if (strcmp(def.long_option, long_option_name) == 0) |
| 353 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 354 | } |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 355 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 356 | return false; |
| 357 | } |
| 358 | |
| 359 | enum OptionDisplayType { |
| 360 | eDisplayBestOption, |
| 361 | eDisplayShortOption, |
| 362 | eDisplayLongOption |
| 363 | }; |
| 364 | |
| 365 | static bool PrintOption(const OptionDefinition &opt_def, |
| 366 | OptionDisplayType display_type, const char *header, |
| 367 | const char *footer, bool show_optional, Stream &strm) { |
| 368 | const bool has_short_option = isprint8(opt_def.short_option) != 0; |
| 369 | |
| 370 | if (display_type == eDisplayShortOption && !has_short_option) |
| 371 | return false; |
| 372 | |
| 373 | if (header && header[0]) |
| 374 | strm.PutCString(header); |
| 375 | |
| 376 | if (show_optional && !opt_def.required) |
| 377 | strm.PutChar('['); |
| 378 | const bool show_short_option = |
| 379 | has_short_option && display_type != eDisplayLongOption; |
| 380 | if (show_short_option) |
| 381 | strm.Printf("-%c", opt_def.short_option); |
| 382 | else |
| 383 | strm.Printf("--%s", opt_def.long_option); |
| 384 | switch (opt_def.option_has_arg) { |
| 385 | case OptionParser::eNoArgument: |
| 386 | break; |
| 387 | case OptionParser::eRequiredArgument: |
| 388 | strm.Printf(" <%s>", CommandObject::GetArgumentName(opt_def.argument_type)); |
| 389 | break; |
| 390 | |
| 391 | case OptionParser::eOptionalArgument: |
| 392 | strm.Printf("%s[<%s>]", show_short_option ? "" : "=", |
| 393 | CommandObject::GetArgumentName(opt_def.argument_type)); |
| 394 | break; |
| 395 | } |
| 396 | if (show_optional && !opt_def.required) |
| 397 | strm.PutChar(']'); |
| 398 | if (footer && footer[0]) |
| 399 | strm.PutCString(footer); |
| 400 | return true; |
| 401 | } |
| 402 | |
| 403 | void Options::GenerateOptionUsage(Stream &strm, CommandObject *cmd, |
| 404 | uint32_t screen_width) { |
| 405 | const bool only_print_args = cmd->IsDashDashCommand(); |
| 406 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 407 | auto opt_defs = GetDefinitions(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 408 | const uint32_t save_indent_level = strm.GetIndentLevel(); |
Zachary Turner | a449698 | 2016-10-05 21:14:38 +0000 | [diff] [blame] | 409 | llvm::StringRef name; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 410 | |
| 411 | StreamString arguments_str; |
| 412 | |
| 413 | if (cmd) { |
| 414 | name = cmd->GetCommandName(); |
| 415 | cmd->GetFormattedCommandArguments(arguments_str); |
| 416 | } else |
| 417 | name = ""; |
| 418 | |
| 419 | strm.PutCString("\nCommand Options Usage:\n"); |
| 420 | |
| 421 | strm.IndentMore(2); |
| 422 | |
| 423 | // First, show each usage level set of options, e.g. <cmd> |
| 424 | // [options-for-level-0] |
| 425 | // <cmd> |
| 426 | // [options-for-level-1] |
| 427 | // etc. |
| 428 | |
| 429 | const uint32_t num_options = NumCommandOptions(); |
| 430 | if (num_options == 0) |
| 431 | return; |
| 432 | |
| 433 | uint32_t num_option_sets = GetRequiredOptions().size(); |
| 434 | |
| 435 | uint32_t i; |
| 436 | |
| 437 | if (!only_print_args) { |
| 438 | for (uint32_t opt_set = 0; opt_set < num_option_sets; ++opt_set) { |
| 439 | uint32_t opt_set_mask; |
| 440 | |
| 441 | opt_set_mask = 1 << opt_set; |
| 442 | if (opt_set > 0) |
| 443 | strm.Printf("\n"); |
| 444 | strm.Indent(name); |
| 445 | |
| 446 | // Different option sets may require different args. |
| 447 | StreamString args_str; |
| 448 | if (cmd) |
| 449 | cmd->GetFormattedCommandArguments(args_str, opt_set_mask); |
| 450 | |
| 451 | // First go through and print all options that take no arguments as |
| 452 | // a single string. If a command has "-a" "-b" and "-c", this will show |
| 453 | // up as [-abc] |
| 454 | |
| 455 | std::set<int> options; |
| 456 | std::set<int>::const_iterator options_pos, options_end; |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 457 | for (auto &def : opt_defs) { |
| 458 | if (def.usage_mask & opt_set_mask && isprint8(def.short_option)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 459 | // Add current option to the end of out_stream. |
| 460 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 461 | if (def.required && def.option_has_arg == OptionParser::eNoArgument) { |
| 462 | options.insert(def.short_option); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | if (options.empty() == false) { |
| 468 | // We have some required options with no arguments |
| 469 | strm.PutCString(" -"); |
| 470 | for (i = 0; i < 2; ++i) |
| 471 | for (options_pos = options.begin(), options_end = options.end(); |
| 472 | options_pos != options_end; ++options_pos) { |
| 473 | if (i == 0 && ::islower(*options_pos)) |
| 474 | continue; |
| 475 | if (i == 1 && ::isupper(*options_pos)) |
| 476 | continue; |
| 477 | strm << (char)*options_pos; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 481 | options.clear(); |
| 482 | for (auto &def : opt_defs) { |
| 483 | if (def.usage_mask & opt_set_mask && isprint8(def.short_option)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 484 | // Add current option to the end of out_stream. |
| 485 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 486 | if (def.required == false && |
| 487 | def.option_has_arg == OptionParser::eNoArgument) { |
| 488 | options.insert(def.short_option); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | if (options.empty() == false) { |
| 494 | // We have some required options with no arguments |
| 495 | strm.PutCString(" [-"); |
| 496 | for (i = 0; i < 2; ++i) |
| 497 | for (options_pos = options.begin(), options_end = options.end(); |
| 498 | options_pos != options_end; ++options_pos) { |
| 499 | if (i == 0 && ::islower(*options_pos)) |
| 500 | continue; |
| 501 | if (i == 1 && ::isupper(*options_pos)) |
| 502 | continue; |
| 503 | strm << (char)*options_pos; |
| 504 | } |
| 505 | strm.PutChar(']'); |
| 506 | } |
| 507 | |
| 508 | // First go through and print the required options (list them up front). |
| 509 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 510 | for (auto &def : opt_defs) { |
| 511 | if (def.usage_mask & opt_set_mask && isprint8(def.short_option)) { |
| 512 | if (def.required && def.option_has_arg != OptionParser::eNoArgument) |
| 513 | PrintOption(def, eDisplayBestOption, " ", nullptr, true, strm); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
| 517 | // Now go through again, and this time only print the optional options. |
| 518 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 519 | for (auto &def : opt_defs) { |
| 520 | if (def.usage_mask & opt_set_mask) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 521 | // Add current option to the end of out_stream. |
| 522 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 523 | if (!def.required && def.option_has_arg != OptionParser::eNoArgument) |
| 524 | PrintOption(def, eDisplayBestOption, " ", nullptr, true, strm); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | if (args_str.GetSize() > 0) { |
| 529 | if (cmd->WantsRawCommandString() && !only_print_args) |
| 530 | strm.Printf(" --"); |
| 531 | |
| 532 | strm.Printf(" %s", args_str.GetData()); |
| 533 | if (only_print_args) |
| 534 | break; |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | if (cmd && (only_print_args || cmd->WantsRawCommandString()) && |
| 540 | arguments_str.GetSize() > 0) { |
| 541 | if (!only_print_args) |
| 542 | strm.PutChar('\n'); |
| 543 | strm.Indent(name); |
| 544 | strm.Printf(" %s", arguments_str.GetData()); |
| 545 | } |
| 546 | |
| 547 | strm.Printf("\n\n"); |
| 548 | |
| 549 | if (!only_print_args) { |
| 550 | // Now print out all the detailed information about the various options: |
| 551 | // long form, short form and help text: |
| 552 | // -short <argument> ( --long_name <argument> ) |
| 553 | // help text |
| 554 | |
| 555 | // This variable is used to keep track of which options' info we've printed |
| 556 | // out, because some options can be in |
| 557 | // more than one usage level, but we only want to print the long form of its |
| 558 | // information once. |
| 559 | |
| 560 | std::multimap<int, uint32_t> options_seen; |
| 561 | strm.IndentMore(5); |
| 562 | |
| 563 | // Put the unique command options in a vector & sort it, so we can output |
| 564 | // them alphabetically (by short_option) |
| 565 | // when writing out detailed help for each option. |
| 566 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 567 | i = 0; |
| 568 | for (auto &def : opt_defs) |
| 569 | options_seen.insert(std::make_pair(def.short_option, i++)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 570 | |
| 571 | // Go through the unique'd and alphabetically sorted vector of options, find |
| 572 | // the table entry for each option |
| 573 | // and write out the detailed help information for that option. |
| 574 | |
| 575 | bool first_option_printed = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 576 | |
| 577 | for (auto pos : options_seen) { |
| 578 | i = pos.second; |
| 579 | // Print out the help information for this option. |
| 580 | |
| 581 | // Put a newline separation between arguments |
| 582 | if (first_option_printed) |
| 583 | strm.EOL(); |
| 584 | else |
| 585 | first_option_printed = true; |
| 586 | |
| 587 | CommandArgumentType arg_type = opt_defs[i].argument_type; |
| 588 | |
| 589 | StreamString arg_name_str; |
| 590 | arg_name_str.Printf("<%s>", CommandObject::GetArgumentName(arg_type)); |
| 591 | |
| 592 | strm.Indent(); |
| 593 | if (opt_defs[i].short_option && isprint8(opt_defs[i].short_option)) { |
| 594 | PrintOption(opt_defs[i], eDisplayShortOption, nullptr, nullptr, false, |
| 595 | strm); |
| 596 | PrintOption(opt_defs[i], eDisplayLongOption, " ( ", " )", false, strm); |
| 597 | } else { |
| 598 | // Short option is not printable, just print long option |
| 599 | PrintOption(opt_defs[i], eDisplayLongOption, nullptr, nullptr, false, |
| 600 | strm); |
| 601 | } |
| 602 | strm.EOL(); |
| 603 | |
| 604 | strm.IndentMore(5); |
| 605 | |
| 606 | if (opt_defs[i].usage_text) |
| 607 | OutputFormattedUsageText(strm, opt_defs[i], screen_width); |
| 608 | if (opt_defs[i].enum_values != nullptr) { |
| 609 | strm.Indent(); |
| 610 | strm.Printf("Values: "); |
| 611 | for (int k = 0; opt_defs[i].enum_values[k].string_value != nullptr; |
| 612 | k++) { |
| 613 | if (k == 0) |
| 614 | strm.Printf("%s", opt_defs[i].enum_values[k].string_value); |
| 615 | else |
| 616 | strm.Printf(" | %s", opt_defs[i].enum_values[k].string_value); |
| 617 | } |
| 618 | strm.EOL(); |
| 619 | } |
| 620 | strm.IndentLess(5); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | // Restore the indent level |
| 625 | strm.SetIndentLevel(save_indent_level); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 628 | // This function is called when we have been given a potentially incomplete set |
| 629 | // of |
| 630 | // options, such as when an alias has been defined (more options might be added |
| 631 | // at |
| 632 | // at the time the alias is invoked). We need to verify that the options in the |
| 633 | // set |
| 634 | // m_seen_options are all part of a set that may be used together, but |
| 635 | // m_seen_options |
| 636 | // may be missing some of the "required" options. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 637 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 638 | bool Options::VerifyPartialOptions(CommandReturnObject &result) { |
| 639 | bool options_are_valid = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 640 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 641 | int num_levels = GetRequiredOptions().size(); |
| 642 | if (num_levels) { |
| 643 | for (int i = 0; i < num_levels && !options_are_valid; ++i) { |
| 644 | // In this case we are treating all options as optional rather than |
| 645 | // required. |
| 646 | // Therefore a set of options is correct if m_seen_options is a subset of |
| 647 | // the |
| 648 | // union of m_required_options and m_optional_options. |
| 649 | OptionSet union_set; |
| 650 | OptionsSetUnion(GetRequiredOptions()[i], GetOptionalOptions()[i], |
| 651 | union_set); |
| 652 | if (IsASubset(m_seen_options, union_set)) |
| 653 | options_are_valid = true; |
| 654 | } |
| 655 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 656 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 657 | return options_are_valid; |
| 658 | } |
Jim Ingham | d6ccc60 | 2010-06-24 20:30:15 +0000 | [diff] [blame] | 659 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 660 | bool Options::HandleOptionCompletion( |
| 661 | Args &input, OptionElementVector &opt_element_vector, int cursor_index, |
| 662 | int char_pos, int match_start_point, int max_return_elements, |
| 663 | CommandInterpreter &interpreter, bool &word_complete, |
| 664 | lldb_private::StringList &matches) { |
| 665 | word_complete = true; |
| 666 | |
| 667 | // For now we just scan the completions to see if the cursor position is in |
| 668 | // an option or its argument. Otherwise we'll call HandleArgumentCompletion. |
| 669 | // In the future we can use completion to validate options as well if we want. |
| 670 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 671 | auto opt_defs = GetDefinitions(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 672 | |
| 673 | std::string cur_opt_std_str(input.GetArgumentAtIndex(cursor_index)); |
| 674 | cur_opt_std_str.erase(char_pos); |
| 675 | const char *cur_opt_str = cur_opt_std_str.c_str(); |
| 676 | |
| 677 | for (size_t i = 0; i < opt_element_vector.size(); i++) { |
| 678 | int opt_pos = opt_element_vector[i].opt_pos; |
| 679 | int opt_arg_pos = opt_element_vector[i].opt_arg_pos; |
| 680 | int opt_defs_index = opt_element_vector[i].opt_defs_index; |
| 681 | if (opt_pos == cursor_index) { |
| 682 | // We're completing the option itself. |
| 683 | |
| 684 | if (opt_defs_index == OptionArgElement::eBareDash) { |
| 685 | // We're completing a bare dash. That means all options are open. |
| 686 | // FIXME: We should scan the other options provided and only complete |
| 687 | // options |
| 688 | // within the option group they belong to. |
| 689 | char opt_str[3] = {'-', 'a', '\0'}; |
| 690 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 691 | for (auto &def : opt_defs) { |
| 692 | if (!def.short_option) |
| 693 | continue; |
| 694 | opt_str[1] = def.short_option; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 695 | matches.AppendString(opt_str); |
| 696 | } |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 697 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 698 | return true; |
| 699 | } else if (opt_defs_index == OptionArgElement::eBareDoubleDash) { |
| 700 | std::string full_name("--"); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 701 | for (auto &def : opt_defs) { |
| 702 | if (!def.short_option) |
| 703 | continue; |
| 704 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 705 | full_name.erase(full_name.begin() + 2, full_name.end()); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 706 | full_name.append(def.long_option); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 707 | matches.AppendString(full_name.c_str()); |
| 708 | } |
| 709 | return true; |
| 710 | } else if (opt_defs_index != OptionArgElement::eUnrecognizedArg) { |
| 711 | // We recognized it, if it an incomplete long option, complete it anyway |
| 712 | // (getopt_long_only is |
| 713 | // happy with shortest unique string, but it's still a nice thing to |
| 714 | // do.) Otherwise return |
| 715 | // The string so the upper level code will know this is a full match and |
| 716 | // add the " ". |
| 717 | if (cur_opt_str && strlen(cur_opt_str) > 2 && cur_opt_str[0] == '-' && |
| 718 | cur_opt_str[1] == '-' && |
| 719 | strcmp(opt_defs[opt_defs_index].long_option, cur_opt_str) != 0) { |
| 720 | std::string full_name("--"); |
| 721 | full_name.append(opt_defs[opt_defs_index].long_option); |
| 722 | matches.AppendString(full_name.c_str()); |
| 723 | return true; |
| 724 | } else { |
| 725 | matches.AppendString(input.GetArgumentAtIndex(cursor_index)); |
| 726 | return true; |
| 727 | } |
| 728 | } else { |
| 729 | // FIXME - not handling wrong options yet: |
| 730 | // Check to see if they are writing a long option & complete it. |
| 731 | // I think we will only get in here if the long option table has two |
| 732 | // elements |
| 733 | // that are not unique up to this point. getopt_long_only does shortest |
| 734 | // unique match |
| 735 | // for long options already. |
| 736 | |
| 737 | if (cur_opt_str && strlen(cur_opt_str) > 2 && cur_opt_str[0] == '-' && |
| 738 | cur_opt_str[1] == '-') { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 739 | for (auto &def : opt_defs) { |
| 740 | if (!def.long_option) |
| 741 | continue; |
| 742 | |
| 743 | if (strstr(def.long_option, cur_opt_str + 2) == def.long_option) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 744 | std::string full_name("--"); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 745 | full_name.append(def.long_option); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 746 | // The options definitions table has duplicates because of the |
| 747 | // way the grouping information is stored, so only add once. |
| 748 | bool duplicate = false; |
| 749 | for (size_t k = 0; k < matches.GetSize(); k++) { |
| 750 | if (matches.GetStringAtIndex(k) == full_name) { |
| 751 | duplicate = true; |
| 752 | break; |
Jim Ingham | d6ccc60 | 2010-06-24 20:30:15 +0000 | [diff] [blame] | 753 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 754 | } |
| 755 | if (!duplicate) |
| 756 | matches.AppendString(full_name.c_str()); |
Jim Ingham | d6ccc60 | 2010-06-24 20:30:15 +0000 | [diff] [blame] | 757 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 758 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 759 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 760 | return true; |
| 761 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 762 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 763 | } else if (opt_arg_pos == cursor_index) { |
| 764 | // Okay the cursor is on the completion of an argument. |
| 765 | // See if it has a completion, otherwise return no matches. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 766 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 767 | if (opt_defs_index != -1) { |
| 768 | HandleOptionArgumentCompletion( |
| 769 | input, cursor_index, strlen(input.GetArgumentAtIndex(cursor_index)), |
| 770 | opt_element_vector, i, match_start_point, max_return_elements, |
| 771 | interpreter, word_complete, matches); |
| 772 | return true; |
| 773 | } else { |
| 774 | // No completion callback means no completions... |
| 775 | return true; |
| 776 | } |
| 777 | |
| 778 | } else { |
| 779 | // Not the last element, keep going. |
| 780 | continue; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 781 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 782 | } |
| 783 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 786 | bool Options::HandleOptionArgumentCompletion( |
| 787 | Args &input, int cursor_index, int char_pos, |
| 788 | OptionElementVector &opt_element_vector, int opt_element_index, |
| 789 | int match_start_point, int max_return_elements, |
| 790 | CommandInterpreter &interpreter, bool &word_complete, |
| 791 | lldb_private::StringList &matches) { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 792 | auto opt_defs = GetDefinitions(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 793 | std::unique_ptr<SearchFilter> filter_ap; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 794 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 795 | int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos; |
| 796 | int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index; |
| 797 | |
| 798 | // See if this is an enumeration type option, and if so complete it here: |
| 799 | |
| 800 | OptionEnumValueElement *enum_values = opt_defs[opt_defs_index].enum_values; |
| 801 | if (enum_values != nullptr) { |
| 802 | bool return_value = false; |
| 803 | std::string match_string(input.GetArgumentAtIndex(opt_arg_pos), |
| 804 | input.GetArgumentAtIndex(opt_arg_pos) + char_pos); |
| 805 | for (int i = 0; enum_values[i].string_value != nullptr; i++) { |
| 806 | if (strstr(enum_values[i].string_value, match_string.c_str()) == |
| 807 | enum_values[i].string_value) { |
| 808 | matches.AppendString(enum_values[i].string_value); |
| 809 | return_value = true; |
| 810 | } |
| 811 | } |
| 812 | return return_value; |
| 813 | } |
| 814 | |
| 815 | // If this is a source file or symbol type completion, and there is a |
| 816 | // -shlib option somewhere in the supplied arguments, then make a search |
| 817 | // filter |
| 818 | // for that shared library. |
| 819 | // FIXME: Do we want to also have an "OptionType" so we don't have to match |
| 820 | // string names? |
| 821 | |
| 822 | uint32_t completion_mask = opt_defs[opt_defs_index].completion_type; |
| 823 | |
| 824 | if (completion_mask == 0) { |
| 825 | lldb::CommandArgumentType option_arg_type = |
| 826 | opt_defs[opt_defs_index].argument_type; |
| 827 | if (option_arg_type != eArgTypeNone) { |
| 828 | const CommandObject::ArgumentTableEntry *arg_entry = |
| 829 | CommandObject::FindArgumentDataByType( |
| 830 | opt_defs[opt_defs_index].argument_type); |
| 831 | if (arg_entry) |
| 832 | completion_mask = arg_entry->completion_type; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | if (completion_mask & CommandCompletions::eSourceFileCompletion || |
| 837 | completion_mask & CommandCompletions::eSymbolCompletion) { |
| 838 | for (size_t i = 0; i < opt_element_vector.size(); i++) { |
| 839 | int cur_defs_index = opt_element_vector[i].opt_defs_index; |
| 840 | |
| 841 | // trying to use <0 indices will definitely cause problems |
| 842 | if (cur_defs_index == OptionArgElement::eUnrecognizedArg || |
| 843 | cur_defs_index == OptionArgElement::eBareDash || |
| 844 | cur_defs_index == OptionArgElement::eBareDoubleDash) |
| 845 | continue; |
| 846 | |
| 847 | int cur_arg_pos = opt_element_vector[i].opt_arg_pos; |
| 848 | const char *cur_opt_name = opt_defs[cur_defs_index].long_option; |
| 849 | |
| 850 | // If this is the "shlib" option and there was an argument provided, |
| 851 | // restrict it to that shared library. |
| 852 | if (cur_opt_name && strcmp(cur_opt_name, "shlib") == 0 && |
| 853 | cur_arg_pos != -1) { |
| 854 | const char *module_name = input.GetArgumentAtIndex(cur_arg_pos); |
| 855 | if (module_name) { |
| 856 | FileSpec module_spec(module_name, false); |
| 857 | lldb::TargetSP target_sp = |
| 858 | interpreter.GetDebugger().GetSelectedTarget(); |
| 859 | // Search filters require a target... |
| 860 | if (target_sp) |
| 861 | filter_ap.reset(new SearchFilterByModule(target_sp, module_spec)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 862 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 863 | break; |
| 864 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 865 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 866 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 867 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 868 | return CommandCompletions::InvokeCommonCompletionCallbacks( |
| 869 | interpreter, completion_mask, input.GetArgumentAtIndex(opt_arg_pos), |
| 870 | match_start_point, max_return_elements, filter_ap.get(), word_complete, |
| 871 | matches); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 872 | } |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 873 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 874 | void OptionGroupOptions::Append(OptionGroup *group) { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 875 | auto group_option_defs = group->GetDefinitions(); |
| 876 | for (uint32_t i = 0; i < group_option_defs.size(); ++i) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 877 | m_option_infos.push_back(OptionInfo(group, i)); |
| 878 | m_option_defs.push_back(group_option_defs[i]); |
| 879 | } |
Greg Clayton | 84c3966 | 2011-04-27 22:04:39 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 882 | const OptionGroup *OptionGroupOptions::GetGroupWithOption(char short_opt) { |
| 883 | for (uint32_t i = 0; i < m_option_defs.size(); i++) { |
| 884 | OptionDefinition opt_def = m_option_defs[i]; |
| 885 | if (opt_def.short_option == short_opt) |
| 886 | return m_option_infos[i].option_group; |
| 887 | } |
| 888 | return nullptr; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 891 | void OptionGroupOptions::Append(OptionGroup *group, uint32_t src_mask, |
| 892 | uint32_t dst_mask) { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 893 | auto group_option_defs = group->GetDefinitions(); |
| 894 | for (uint32_t i = 0; i < group_option_defs.size(); ++i) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 895 | if (group_option_defs[i].usage_mask & src_mask) { |
| 896 | m_option_infos.push_back(OptionInfo(group, i)); |
| 897 | m_option_defs.push_back(group_option_defs[i]); |
| 898 | m_option_defs.back().usage_mask = dst_mask; |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 899 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 900 | } |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 903 | void OptionGroupOptions::Finalize() { |
| 904 | m_did_finalize = true; |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 907 | Error OptionGroupOptions::SetOptionValue(uint32_t option_idx, |
| 908 | const char *option_value, |
| 909 | ExecutionContext *execution_context) { |
| 910 | // After calling OptionGroupOptions::Append(...), you must finalize the groups |
| 911 | // by calling OptionGroupOptions::Finlize() |
| 912 | assert(m_did_finalize); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 913 | Error error; |
| 914 | if (option_idx < m_option_infos.size()) { |
| 915 | error = m_option_infos[option_idx].option_group->SetOptionValue( |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 916 | m_option_infos[option_idx].option_index, |
| 917 | llvm::StringRef::withNullAsEmpty(option_value), execution_context); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 918 | |
| 919 | } else { |
| 920 | error.SetErrorString("invalid option index"); // Shouldn't happen... |
| 921 | } |
| 922 | return error; |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 925 | void OptionGroupOptions::OptionParsingStarting( |
| 926 | ExecutionContext *execution_context) { |
| 927 | std::set<OptionGroup *> group_set; |
| 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 | group->OptionParsingStarting(execution_context); |
| 933 | group_set.insert(group); |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 934 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 935 | } |
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 | Error OptionGroupOptions::OptionParsingFinished( |
| 938 | ExecutionContext *execution_context) { |
| 939 | std::set<OptionGroup *> group_set; |
| 940 | Error error; |
| 941 | OptionInfos::iterator pos, end = m_option_infos.end(); |
| 942 | for (pos = m_option_infos.begin(); pos != end; ++pos) { |
| 943 | OptionGroup *group = pos->option_group; |
| 944 | if (group_set.find(group) == group_set.end()) { |
| 945 | error = group->OptionParsingFinished(execution_context); |
| 946 | group_set.insert(group); |
| 947 | if (error.Fail()) |
| 948 | return error; |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 949 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 950 | } |
| 951 | return error; |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 952 | } |