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