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 #include <set> 18 19 // Other libraries and framework includes 20 // Project includes 21 #include "lldb/Interpreter/CommandObject.h" 22 #include "lldb/Interpreter/CommandReturnObject.h" 23 #include "lldb/Interpreter/CommandCompletions.h" 24 #include "lldb/Interpreter/CommandInterpreter.h" 25 #include "lldb/Core/StreamString.h" 26 #include "lldb/Target/Target.h" 27 28 using namespace lldb; 29 using namespace lldb_private; 30 31 //------------------------------------------------------------------------- 32 // Options 33 //------------------------------------------------------------------------- 34 Options::Options () : 35 m_getopt_table () 36 { 37 BuildValidOptionSets(); 38 } 39 40 Options::~Options () 41 { 42 } 43 44 void 45 Options::NotifyOptionParsingStarting(ExecutionContext *execution_context) 46 { 47 m_seen_options.clear(); 48 // Let the subclass reset its option values 49 OptionParsingStarting(execution_context); 50 } 51 52 Error 53 Options::NotifyOptionParsingFinished(ExecutionContext *execution_context) 54 { 55 return OptionParsingFinished(execution_context); 56 } 57 58 void 59 Options::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 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 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 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 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 239 uint32_t 240 Options::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 257 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 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 331 void 332 Options::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 400 bool 401 Options::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 422 enum OptionDisplayType 423 { 424 eDisplayBestOption, 425 eDisplayShortOption, 426 eDisplayLongOption 427 }; 428 429 static bool 430 PrintOption (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 473 void 474 Options::GenerateOptionUsage 475 ( 476 Stream &strm, 477 CommandObject *cmd, 478 uint32_t screen_width 479 ) 480 { 481 const bool only_print_args = cmd->IsDashDashCommand(); 482 483 const OptionDefinition *opt_defs = GetDefinitions(); 484 const uint32_t save_indent_level = strm.GetIndentLevel(); 485 const char *name; 486 487 StreamString arguments_str; 488 489 if (cmd) 490 { 491 name = cmd->GetCommandName(); 492 cmd->GetFormattedCommandArguments (arguments_str); 493 } 494 else 495 name = ""; 496 497 strm.PutCString ("\nCommand Options Usage:\n"); 498 499 strm.IndentMore(2); 500 501 // First, show each usage level set of options, e.g. <cmd> [options-for-level-0] 502 // <cmd> [options-for-level-1] 503 // etc. 504 505 const uint32_t num_options = NumCommandOptions(); 506 if (num_options == 0) 507 return; 508 509 uint32_t num_option_sets = GetRequiredOptions().size(); 510 511 uint32_t i; 512 513 if (!only_print_args) 514 { 515 for (uint32_t opt_set = 0; opt_set < num_option_sets; ++opt_set) 516 { 517 uint32_t opt_set_mask; 518 519 opt_set_mask = 1 << opt_set; 520 if (opt_set > 0) 521 strm.Printf ("\n"); 522 strm.Indent (name); 523 524 // Different option sets may require different args. 525 StreamString args_str; 526 if (cmd) 527 cmd->GetFormattedCommandArguments(args_str, opt_set_mask); 528 529 // First go through and print all options that take no arguments as 530 // a single string. If a command has "-a" "-b" and "-c", this will show 531 // up as [-abc] 532 533 std::set<int> options; 534 std::set<int>::const_iterator options_pos, options_end; 535 for (i = 0; i < num_options; ++i) 536 { 537 if (opt_defs[i].usage_mask & opt_set_mask && isprint8(opt_defs[i].short_option)) 538 { 539 // Add current option to the end of out_stream. 540 541 if (opt_defs[i].required == true && 542 opt_defs[i].option_has_arg == OptionParser::eNoArgument) 543 { 544 options.insert (opt_defs[i].short_option); 545 } 546 } 547 } 548 549 if (options.empty() == false) 550 { 551 // We have some required options with no arguments 552 strm.PutCString(" -"); 553 for (i=0; i<2; ++i) 554 for (options_pos = options.begin(), options_end = options.end(); 555 options_pos != options_end; 556 ++options_pos) 557 { 558 if (i==0 && ::islower (*options_pos)) 559 continue; 560 if (i==1 && ::isupper (*options_pos)) 561 continue; 562 strm << (char)*options_pos; 563 } 564 } 565 566 for (i = 0, options.clear(); i < num_options; ++i) 567 { 568 if (opt_defs[i].usage_mask & opt_set_mask && isprint8(opt_defs[i].short_option)) 569 { 570 // Add current option to the end of out_stream. 571 572 if (opt_defs[i].required == false && 573 opt_defs[i].option_has_arg == OptionParser::eNoArgument) 574 { 575 options.insert (opt_defs[i].short_option); 576 } 577 } 578 } 579 580 if (options.empty() == false) 581 { 582 // We have some required options with no arguments 583 strm.PutCString(" [-"); 584 for (i=0; i<2; ++i) 585 for (options_pos = options.begin(), options_end = options.end(); 586 options_pos != options_end; 587 ++options_pos) 588 { 589 if (i==0 && ::islower (*options_pos)) 590 continue; 591 if (i==1 && ::isupper (*options_pos)) 592 continue; 593 strm << (char)*options_pos; 594 } 595 strm.PutChar(']'); 596 } 597 598 // First go through and print the required options (list them up front). 599 600 for (i = 0; i < num_options; ++i) 601 { 602 if (opt_defs[i].usage_mask & opt_set_mask && isprint8(opt_defs[i].short_option)) 603 { 604 if (opt_defs[i].required && opt_defs[i].option_has_arg != OptionParser::eNoArgument) 605 PrintOption (opt_defs[i], eDisplayBestOption, " ", nullptr, true, strm); 606 } 607 } 608 609 // Now go through again, and this time only print the optional options. 610 611 for (i = 0; i < num_options; ++i) 612 { 613 if (opt_defs[i].usage_mask & opt_set_mask) 614 { 615 // Add current option to the end of out_stream. 616 617 if (!opt_defs[i].required && opt_defs[i].option_has_arg != OptionParser::eNoArgument) 618 PrintOption (opt_defs[i], eDisplayBestOption, " ", nullptr, true, strm); 619 } 620 } 621 622 if (args_str.GetSize() > 0) 623 { 624 if (cmd->WantsRawCommandString() && !only_print_args) 625 strm.Printf(" --"); 626 627 strm.Printf (" %s", args_str.GetData()); 628 if (only_print_args) 629 break; 630 } 631 } 632 } 633 634 if (cmd && 635 (only_print_args || cmd->WantsRawCommandString()) && 636 arguments_str.GetSize() > 0) 637 { 638 if (!only_print_args) strm.PutChar('\n'); 639 strm.Indent(name); 640 strm.Printf(" %s", arguments_str.GetData()); 641 } 642 643 strm.Printf ("\n\n"); 644 645 if (!only_print_args) 646 { 647 // Now print out all the detailed information about the various options: long form, short form and help text: 648 // -short <argument> ( --long_name <argument> ) 649 // help text 650 651 // This variable is used to keep track of which options' info we've printed out, because some options can be in 652 // more than one usage level, but we only want to print the long form of its information once. 653 654 std::multimap<int, uint32_t> options_seen; 655 strm.IndentMore (5); 656 657 // Put the unique command options in a vector & sort it, so we can output them alphabetically (by short_option) 658 // when writing out detailed help for each option. 659 660 for (i = 0; i < num_options; ++i) 661 options_seen.insert(std::make_pair(opt_defs[i].short_option, i)); 662 663 // Go through the unique'd and alphabetically sorted vector of options, find the table entry for each option 664 // and write out the detailed help information for that option. 665 666 bool first_option_printed = false;; 667 668 for (auto pos : options_seen) 669 { 670 i = pos.second; 671 //Print out the help information for this option. 672 673 // Put a newline separation between arguments 674 if (first_option_printed) 675 strm.EOL(); 676 else 677 first_option_printed = true; 678 679 CommandArgumentType arg_type = opt_defs[i].argument_type; 680 681 StreamString arg_name_str; 682 arg_name_str.Printf ("<%s>", CommandObject::GetArgumentName (arg_type)); 683 684 strm.Indent (); 685 if (opt_defs[i].short_option && isprint8(opt_defs[i].short_option)) 686 { 687 PrintOption (opt_defs[i], eDisplayShortOption, nullptr, nullptr, false, strm); 688 PrintOption (opt_defs[i], eDisplayLongOption, " ( ", " )", false, strm); 689 } 690 else 691 { 692 // Short option is not printable, just print long option 693 PrintOption (opt_defs[i], eDisplayLongOption, nullptr, nullptr, false, strm); 694 } 695 strm.EOL(); 696 697 strm.IndentMore (5); 698 699 if (opt_defs[i].usage_text) 700 OutputFormattedUsageText (strm, 701 opt_defs[i], 702 screen_width); 703 if (opt_defs[i].enum_values != nullptr) 704 { 705 strm.Indent (); 706 strm.Printf("Values: "); 707 for (int k = 0; opt_defs[i].enum_values[k].string_value != nullptr; k++) 708 { 709 if (k == 0) 710 strm.Printf("%s", opt_defs[i].enum_values[k].string_value); 711 else 712 strm.Printf(" | %s", opt_defs[i].enum_values[k].string_value); 713 } 714 strm.EOL(); 715 } 716 strm.IndentLess (5); 717 } 718 } 719 720 // Restore the indent level 721 strm.SetIndentLevel (save_indent_level); 722 } 723 724 // This function is called when we have been given a potentially incomplete set of 725 // options, such as when an alias has been defined (more options might be added at 726 // at the time the alias is invoked). We need to verify that the options in the set 727 // m_seen_options are all part of a set that may be used together, but m_seen_options 728 // may be missing some of the "required" options. 729 730 bool 731 Options::VerifyPartialOptions (CommandReturnObject &result) 732 { 733 bool options_are_valid = false; 734 735 int num_levels = GetRequiredOptions().size(); 736 if (num_levels) 737 { 738 for (int i = 0; i < num_levels && !options_are_valid; ++i) 739 { 740 // In this case we are treating all options as optional rather than required. 741 // Therefore a set of options is correct if m_seen_options is a subset of the 742 // union of m_required_options and m_optional_options. 743 OptionSet union_set; 744 OptionsSetUnion (GetRequiredOptions()[i], GetOptionalOptions()[i], union_set); 745 if (IsASubset (m_seen_options, union_set)) 746 options_are_valid = true; 747 } 748 } 749 750 return options_are_valid; 751 } 752 753 bool 754 Options::HandleOptionCompletion 755 ( 756 Args &input, 757 OptionElementVector &opt_element_vector, 758 int cursor_index, 759 int char_pos, 760 int match_start_point, 761 int max_return_elements, 762 CommandInterpreter &interpreter, 763 bool &word_complete, 764 lldb_private::StringList &matches 765 ) 766 { 767 word_complete = true; 768 769 // For now we just scan the completions to see if the cursor position is in 770 // an option or its argument. Otherwise we'll call HandleArgumentCompletion. 771 // In the future we can use completion to validate options as well if we want. 772 773 const OptionDefinition *opt_defs = GetDefinitions(); 774 775 std::string cur_opt_std_str (input.GetArgumentAtIndex(cursor_index)); 776 cur_opt_std_str.erase(char_pos); 777 const char *cur_opt_str = cur_opt_std_str.c_str(); 778 779 for (size_t i = 0; i < opt_element_vector.size(); i++) 780 { 781 int opt_pos = opt_element_vector[i].opt_pos; 782 int opt_arg_pos = opt_element_vector[i].opt_arg_pos; 783 int opt_defs_index = opt_element_vector[i].opt_defs_index; 784 if (opt_pos == cursor_index) 785 { 786 // We're completing the option itself. 787 788 if (opt_defs_index == OptionArgElement::eBareDash) 789 { 790 // We're completing a bare dash. That means all options are open. 791 // FIXME: We should scan the other options provided and only complete options 792 // within the option group they belong to. 793 char opt_str[3] = {'-', 'a', '\0'}; 794 795 for (int j = 0 ; opt_defs[j].short_option != 0 ; j++) 796 { 797 opt_str[1] = opt_defs[j].short_option; 798 matches.AppendString (opt_str); 799 } 800 return true; 801 } 802 else if (opt_defs_index == OptionArgElement::eBareDoubleDash) 803 { 804 std::string full_name ("--"); 805 for (int j = 0 ; opt_defs[j].short_option != 0 ; j++) 806 { 807 full_name.erase(full_name.begin() + 2, full_name.end()); 808 full_name.append (opt_defs[j].long_option); 809 matches.AppendString (full_name.c_str()); 810 } 811 return true; 812 } 813 else if (opt_defs_index != OptionArgElement::eUnrecognizedArg) 814 { 815 // We recognized it, if it an incomplete long option, complete it anyway (getopt_long_only is 816 // happy with shortest unique string, but it's still a nice thing to do.) Otherwise return 817 // The string so the upper level code will know this is a full match and add the " ". 818 if (cur_opt_str && strlen (cur_opt_str) > 2 819 && cur_opt_str[0] == '-' && cur_opt_str[1] == '-' 820 && strcmp (opt_defs[opt_defs_index].long_option, cur_opt_str) != 0) 821 { 822 std::string full_name ("--"); 823 full_name.append (opt_defs[opt_defs_index].long_option); 824 matches.AppendString(full_name.c_str()); 825 return true; 826 } 827 else 828 { 829 matches.AppendString(input.GetArgumentAtIndex(cursor_index)); 830 return true; 831 } 832 } 833 else 834 { 835 // FIXME - not handling wrong options yet: 836 // Check to see if they are writing a long option & complete it. 837 // I think we will only get in here if the long option table has two elements 838 // that are not unique up to this point. getopt_long_only does shortest unique match 839 // for long options already. 840 841 if (cur_opt_str && strlen (cur_opt_str) > 2 842 && cur_opt_str[0] == '-' && cur_opt_str[1] == '-') 843 { 844 for (int j = 0 ; opt_defs[j].short_option != 0 ; j++) 845 { 846 if (strstr(opt_defs[j].long_option, cur_opt_str + 2) == opt_defs[j].long_option) 847 { 848 std::string full_name ("--"); 849 full_name.append (opt_defs[j].long_option); 850 // The options definitions table has duplicates because of the 851 // way the grouping information is stored, so only add once. 852 bool duplicate = false; 853 for (size_t k = 0; k < matches.GetSize(); k++) 854 { 855 if (matches.GetStringAtIndex(k) == full_name) 856 { 857 duplicate = true; 858 break; 859 } 860 } 861 if (!duplicate) 862 matches.AppendString(full_name.c_str()); 863 } 864 } 865 } 866 return true; 867 } 868 869 870 } 871 else if (opt_arg_pos == cursor_index) 872 { 873 // Okay the cursor is on the completion of an argument. 874 // See if it has a completion, otherwise return no matches. 875 876 if (opt_defs_index != -1) 877 { 878 HandleOptionArgumentCompletion (input, 879 cursor_index, 880 strlen (input.GetArgumentAtIndex(cursor_index)), 881 opt_element_vector, 882 i, 883 match_start_point, 884 max_return_elements, 885 interpreter, 886 word_complete, 887 matches); 888 return true; 889 } 890 else 891 { 892 // No completion callback means no completions... 893 return true; 894 } 895 896 } 897 else 898 { 899 // Not the last element, keep going. 900 continue; 901 } 902 } 903 return false; 904 } 905 906 bool 907 Options::HandleOptionArgumentCompletion 908 ( 909 Args &input, 910 int cursor_index, 911 int char_pos, 912 OptionElementVector &opt_element_vector, 913 int opt_element_index, 914 int match_start_point, 915 int max_return_elements, 916 CommandInterpreter &interpreter, 917 bool &word_complete, 918 lldb_private::StringList &matches 919 ) 920 { 921 const OptionDefinition *opt_defs = GetDefinitions(); 922 std::unique_ptr<SearchFilter> filter_ap; 923 924 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos; 925 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index; 926 927 // See if this is an enumeration type option, and if so complete it here: 928 929 OptionEnumValueElement *enum_values = opt_defs[opt_defs_index].enum_values; 930 if (enum_values != nullptr) 931 { 932 bool return_value = false; 933 std::string match_string(input.GetArgumentAtIndex (opt_arg_pos), input.GetArgumentAtIndex (opt_arg_pos) + char_pos); 934 for (int i = 0; enum_values[i].string_value != nullptr; i++) 935 { 936 if (strstr(enum_values[i].string_value, match_string.c_str()) == enum_values[i].string_value) 937 { 938 matches.AppendString (enum_values[i].string_value); 939 return_value = true; 940 } 941 } 942 return return_value; 943 } 944 945 // If this is a source file or symbol type completion, and there is a 946 // -shlib option somewhere in the supplied arguments, then make a search filter 947 // for that shared library. 948 // FIXME: Do we want to also have an "OptionType" so we don't have to match string names? 949 950 uint32_t completion_mask = opt_defs[opt_defs_index].completion_type; 951 952 if (completion_mask == 0) 953 { 954 lldb::CommandArgumentType option_arg_type = opt_defs[opt_defs_index].argument_type; 955 if (option_arg_type != eArgTypeNone) 956 { 957 const CommandObject::ArgumentTableEntry *arg_entry = CommandObject::FindArgumentDataByType (opt_defs[opt_defs_index].argument_type); 958 if (arg_entry) 959 completion_mask = arg_entry->completion_type; 960 } 961 } 962 963 if (completion_mask & CommandCompletions::eSourceFileCompletion 964 || completion_mask & CommandCompletions::eSymbolCompletion) 965 { 966 for (size_t i = 0; i < opt_element_vector.size(); i++) 967 { 968 int cur_defs_index = opt_element_vector[i].opt_defs_index; 969 970 // trying to use <0 indices will definitely cause problems 971 if (cur_defs_index == OptionArgElement::eUnrecognizedArg || 972 cur_defs_index == OptionArgElement::eBareDash || 973 cur_defs_index == OptionArgElement::eBareDoubleDash) 974 continue; 975 976 int cur_arg_pos = opt_element_vector[i].opt_arg_pos; 977 const char *cur_opt_name = opt_defs[cur_defs_index].long_option; 978 979 // If this is the "shlib" option and there was an argument provided, 980 // restrict it to that shared library. 981 if (cur_opt_name && strcmp(cur_opt_name, "shlib") == 0 && cur_arg_pos != -1) 982 { 983 const char *module_name = input.GetArgumentAtIndex(cur_arg_pos); 984 if (module_name) 985 { 986 FileSpec module_spec(module_name, false); 987 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget(); 988 // Search filters require a target... 989 if (target_sp) 990 filter_ap.reset (new SearchFilterByModule (target_sp, module_spec)); 991 } 992 break; 993 } 994 } 995 } 996 997 return CommandCompletions::InvokeCommonCompletionCallbacks (interpreter, 998 completion_mask, 999 input.GetArgumentAtIndex (opt_arg_pos), 1000 match_start_point, 1001 max_return_elements, 1002 filter_ap.get(), 1003 word_complete, 1004 matches); 1005 1006 } 1007 1008 1009 void 1010 OptionGroupOptions::Append (OptionGroup* group) 1011 { 1012 const OptionDefinition* group_option_defs = group->GetDefinitions (); 1013 const uint32_t group_option_count = group->GetNumDefinitions(); 1014 for (uint32_t i=0; i<group_option_count; ++i) 1015 { 1016 m_option_infos.push_back (OptionInfo (group, i)); 1017 m_option_defs.push_back (group_option_defs[i]); 1018 } 1019 } 1020 1021 const OptionGroup* 1022 OptionGroupOptions::GetGroupWithOption (char short_opt) 1023 { 1024 for (uint32_t i = 0; i < m_option_defs.size(); i++) 1025 { 1026 OptionDefinition opt_def = m_option_defs[i]; 1027 if (opt_def.short_option == short_opt) 1028 return m_option_infos[i].option_group; 1029 } 1030 return nullptr; 1031 } 1032 1033 void 1034 OptionGroupOptions::Append (OptionGroup* group, 1035 uint32_t src_mask, 1036 uint32_t dst_mask) 1037 { 1038 const OptionDefinition* group_option_defs = group->GetDefinitions (); 1039 const uint32_t group_option_count = group->GetNumDefinitions(); 1040 for (uint32_t i=0; i<group_option_count; ++i) 1041 { 1042 if (group_option_defs[i].usage_mask & src_mask) 1043 { 1044 m_option_infos.push_back (OptionInfo (group, i)); 1045 m_option_defs.push_back (group_option_defs[i]); 1046 m_option_defs.back().usage_mask = dst_mask; 1047 } 1048 } 1049 } 1050 1051 void 1052 OptionGroupOptions::Finalize () 1053 { 1054 m_did_finalize = true; 1055 OptionDefinition empty_option_def = { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }; 1056 m_option_defs.push_back (empty_option_def); 1057 } 1058 1059 Error 1060 OptionGroupOptions::SetOptionValue (uint32_t option_idx, 1061 const char *option_value, 1062 ExecutionContext *execution_context) 1063 { 1064 // After calling OptionGroupOptions::Append(...), you must finalize the groups 1065 // by calling OptionGroupOptions::Finlize() 1066 assert (m_did_finalize); 1067 assert (m_option_infos.size() + 1 == m_option_defs.size()); 1068 Error error; 1069 if (option_idx < m_option_infos.size()) 1070 { 1071 error = m_option_infos[option_idx].option_group->SetOptionValue (m_option_infos[option_idx].option_index, 1072 option_value, 1073 execution_context); 1074 1075 } 1076 else 1077 { 1078 error.SetErrorString ("invalid option index"); // Shouldn't happen... 1079 } 1080 return error; 1081 } 1082 1083 void 1084 OptionGroupOptions::OptionParsingStarting (ExecutionContext *execution_context) 1085 { 1086 std::set<OptionGroup*> group_set; 1087 OptionInfos::iterator pos, end = m_option_infos.end(); 1088 for (pos = m_option_infos.begin(); pos != end; ++pos) 1089 { 1090 OptionGroup* group = pos->option_group; 1091 if (group_set.find(group) == group_set.end()) 1092 { 1093 group->OptionParsingStarting(execution_context); 1094 group_set.insert(group); 1095 } 1096 } 1097 } 1098 Error 1099 OptionGroupOptions::OptionParsingFinished (ExecutionContext *execution_context) 1100 { 1101 std::set<OptionGroup*> group_set; 1102 Error error; 1103 OptionInfos::iterator pos, end = m_option_infos.end(); 1104 for (pos = m_option_infos.begin(); pos != end; ++pos) 1105 { 1106 OptionGroup* group = pos->option_group; 1107 if (group_set.find(group) == group_set.end()) 1108 { 1109 error = group->OptionParsingFinished (execution_context); 1110 group_set.insert(group); 1111 if (error.Fail()) 1112 return error; 1113 } 1114 } 1115 return error; 1116 } 1117