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