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