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 <set> 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 //------------------------------------------------------------------------- 33 Options::Options (CommandInterpreter &interpreter) : 34 m_interpreter (interpreter), 35 m_getopt_table () 36 { 37 BuildValidOptionSets(); 38 } 39 40 Options::~Options () 41 { 42 } 43 44 void 45 Options::NotifyOptionParsingStarting () 46 { 47 m_seen_options.clear(); 48 // Let the subclass reset its option values 49 OptionParsingStarting (); 50 } 51 52 Error 53 Options::NotifyOptionParsingFinished () 54 { 55 return OptionParsingFinished (); 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 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 (int 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 (int 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 == NULL) 244 return 0; 245 246 int i = 0; 247 248 if (opt_defs != NULL) 249 { 250 while (opt_defs[i].long_option != NULL) 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; 270 const OptionDefinition *opt_defs = GetDefinitions(); 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 { 277 char short_opt = opt_defs[i].short_option; 278 279 if (option_seen.test(short_opt) == false) 280 { 281 m_getopt_table[j].name = opt_defs[i].long_option; 282 m_getopt_table[j].has_arg = opt_defs[i].option_has_arg; 283 m_getopt_table[j].flag = NULL; 284 m_getopt_table[j].val = opt_defs[i].short_option; 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 298 if (m_getopt_table.empty()) 299 return NULL; 300 301 return &m_getopt_table.front(); 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 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 = long_option; 379 if (long_option[0] == '-' && long_option[1] == '-') 380 long_option_name += 2; 381 382 for (uint32_t i = 0; opt_defs[i].long_option; ++i) 383 { 384 if (strcmp(opt_defs[i].long_option, long_option_name) == 0) 385 return true; 386 } 387 } 388 } 389 return false; 390 } 391 392 void 393 Options::GenerateOptionUsage 394 ( 395 Stream &strm, 396 CommandObject *cmd 397 ) 398 { 399 const uint32_t screen_width = m_interpreter.GetDebugger().GetTerminalWidth(); 400 401 const OptionDefinition *opt_defs = GetDefinitions(); 402 const uint32_t save_indent_level = strm.GetIndentLevel(); 403 const char *name; 404 405 StreamString arguments_str; 406 407 if (cmd) 408 { 409 name = cmd->GetCommandName(); 410 cmd->GetFormattedCommandArguments (arguments_str); 411 } 412 else 413 name = ""; 414 415 strm.PutCString ("\nCommand Options Usage:\n"); 416 417 strm.IndentMore(2); 418 419 // First, show each usage level set of options, e.g. <cmd> [options-for-level-0] 420 // <cmd> [options-for-level-1] 421 // etc. 422 423 const uint32_t num_options = NumCommandOptions(); 424 if (num_options == 0) 425 return; 426 427 int num_option_sets = GetRequiredOptions().size(); 428 429 uint32_t i; 430 431 for (uint32_t opt_set = 0; opt_set < num_option_sets; ++opt_set) 432 { 433 uint32_t opt_set_mask; 434 435 opt_set_mask = 1 << opt_set; 436 if (opt_set > 0) 437 strm.Printf ("\n"); 438 strm.Indent (name); 439 440 // First go through and print all options that take no arguments as 441 // a single string. If a command has "-a" "-b" and "-c", this will show 442 // up as [-abc] 443 444 std::set<char> options; 445 std::set<char>::const_iterator options_pos, options_end; 446 bool first; 447 for (i = 0, first = true; i < num_options; ++i) 448 { 449 if (opt_defs[i].usage_mask & opt_set_mask) 450 { 451 // Add current option to the end of out_stream. 452 453 if (opt_defs[i].required == true && 454 opt_defs[i].option_has_arg == no_argument) 455 { 456 options.insert (opt_defs[i].short_option); 457 } 458 } 459 } 460 461 if (options.empty() == false) 462 { 463 // We have some required options with no arguments 464 strm.PutCString(" -"); 465 for (i=0; i<2; ++i) 466 for (options_pos = options.begin(), options_end = options.end(); 467 options_pos != options_end; 468 ++options_pos) 469 { 470 if (i==0 && ::isupper (*options_pos)) 471 continue; 472 if (i==1 && ::islower (*options_pos)) 473 continue; 474 strm << *options_pos; 475 } 476 } 477 478 for (i = 0, options.clear(); i < num_options; ++i) 479 { 480 if (opt_defs[i].usage_mask & opt_set_mask) 481 { 482 // Add current option to the end of out_stream. 483 484 if (opt_defs[i].required == false && 485 opt_defs[i].option_has_arg == no_argument) 486 { 487 options.insert (opt_defs[i].short_option); 488 } 489 } 490 } 491 492 if (options.empty() == false) 493 { 494 // We have some required options with no arguments 495 strm.PutCString(" [-"); 496 for (i=0; i<2; ++i) 497 for (options_pos = options.begin(), options_end = options.end(); 498 options_pos != options_end; 499 ++options_pos) 500 { 501 if (i==0 && ::isupper (*options_pos)) 502 continue; 503 if (i==1 && ::islower (*options_pos)) 504 continue; 505 strm << *options_pos; 506 } 507 strm.PutChar(']'); 508 } 509 510 // First go through and print the required options (list them up front). 511 512 for (i = 0; i < num_options; ++i) 513 { 514 if (opt_defs[i].usage_mask & opt_set_mask) 515 { 516 // Add current option to the end of out_stream. 517 CommandArgumentType arg_type = opt_defs[i].argument_type; 518 519 if (opt_defs[i].required) 520 { 521 if (opt_defs[i].option_has_arg == required_argument) 522 { 523 strm.Printf (" -%c <%s>", 524 opt_defs[i].short_option, 525 CommandObject::GetArgumentName (arg_type)); 526 } 527 else if (opt_defs[i].option_has_arg == optional_argument) 528 { 529 strm.Printf (" -%c [<%s>]", 530 opt_defs[i].short_option, 531 CommandObject::GetArgumentName (arg_type)); 532 } 533 } 534 } 535 } 536 537 // Now go through again, and this time only print the optional options. 538 539 for (i = 0; i < num_options; ++i) 540 { 541 if (opt_defs[i].usage_mask & opt_set_mask) 542 { 543 // Add current option to the end of out_stream. 544 545 CommandArgumentType arg_type = opt_defs[i].argument_type; 546 547 if (! opt_defs[i].required) 548 { 549 if (opt_defs[i].option_has_arg == required_argument) 550 strm.Printf (" [-%c <%s>]", opt_defs[i].short_option, 551 CommandObject::GetArgumentName (arg_type)); 552 else if (opt_defs[i].option_has_arg == optional_argument) 553 strm.Printf (" [-%c [<%s>]]", opt_defs[i].short_option, 554 CommandObject::GetArgumentName (arg_type)); 555 } 556 } 557 } 558 if (arguments_str.GetSize() > 0) 559 strm.Printf (" %s", arguments_str.GetData()); 560 } 561 strm.Printf ("\n\n"); 562 563 // Now print out all the detailed information about the various options: long form, short form and help text: 564 // --long_name <argument> ( -short <argument> ) 565 // help text 566 567 // This variable is used to keep track of which options' info we've printed out, because some options can be in 568 // more than one usage level, but we only want to print the long form of its information once. 569 570 OptionSet options_seen; 571 OptionSet::iterator pos; 572 strm.IndentMore (5); 573 574 std::vector<char> sorted_options; 575 576 577 // Put the unique command options in a vector & sort it, so we can output them alphabetically (by short_option) 578 // when writing out detailed help for each option. 579 580 for (i = 0; i < num_options; ++i) 581 { 582 pos = options_seen.find (opt_defs[i].short_option); 583 if (pos == options_seen.end()) 584 { 585 options_seen.insert (opt_defs[i].short_option); 586 sorted_options.push_back (opt_defs[i].short_option); 587 } 588 } 589 590 std::sort (sorted_options.begin(), sorted_options.end()); 591 592 // Go through the unique'd and alphabetically sorted vector of options, find the table entry for each option 593 // and write out the detailed help information for that option. 594 595 int first_option_printed = 1; 596 size_t end = sorted_options.size(); 597 for (size_t j = 0; j < end; ++j) 598 { 599 char option = sorted_options[j]; 600 bool found = false; 601 for (i = 0; i < num_options && !found; ++i) 602 { 603 if (opt_defs[i].short_option == option) 604 { 605 found = true; 606 //Print out the help information for this option. 607 608 // Put a newline separation between arguments 609 if (first_option_printed) 610 first_option_printed = 0; 611 else 612 strm.EOL(); 613 614 CommandArgumentType arg_type = opt_defs[i].argument_type; 615 616 StreamString arg_name_str; 617 arg_name_str.Printf ("<%s>", CommandObject::GetArgumentName (arg_type)); 618 619 strm.Indent (); 620 strm.Printf ("-%c", opt_defs[i].short_option); 621 if (arg_type != eArgTypeNone) 622 strm.Printf (" <%s>", CommandObject::GetArgumentName (arg_type)); 623 strm.Printf (" ( --%s", opt_defs[i].long_option); 624 if (arg_type != eArgTypeNone) 625 strm.Printf (" <%s>", CommandObject::GetArgumentName (arg_type)); 626 strm.PutCString(" )\n"); 627 628 strm.IndentMore (5); 629 630 if (opt_defs[i].usage_text) 631 OutputFormattedUsageText (strm, 632 opt_defs[i].usage_text, 633 screen_width); 634 if (opt_defs[i].enum_values != NULL) 635 { 636 strm.Indent (); 637 strm.Printf("Values: "); 638 for (int k = 0; opt_defs[i].enum_values[k].string_value != NULL; k++) 639 { 640 if (k == 0) 641 strm.Printf("%s", opt_defs[i].enum_values[k].string_value); 642 else 643 strm.Printf(" | %s", opt_defs[i].enum_values[k].string_value); 644 } 645 strm.EOL(); 646 } 647 strm.IndentLess (5); 648 } 649 } 650 } 651 652 // Restore the indent level 653 strm.SetIndentLevel (save_indent_level); 654 } 655 656 // This function is called when we have been given a potentially incomplete set of 657 // options, such as when an alias has been defined (more options might be added at 658 // at the time the alias is invoked). We need to verify that the options in the set 659 // m_seen_options are all part of a set that may be used together, but m_seen_options 660 // may be missing some of the "required" options. 661 662 bool 663 Options::VerifyPartialOptions (CommandReturnObject &result) 664 { 665 bool options_are_valid = false; 666 667 int num_levels = GetRequiredOptions().size(); 668 if (num_levels) 669 { 670 for (int i = 0; i < num_levels && !options_are_valid; ++i) 671 { 672 // In this case we are treating all options as optional rather than required. 673 // Therefore a set of options is correct if m_seen_options is a subset of the 674 // union of m_required_options and m_optional_options. 675 OptionSet union_set; 676 OptionsSetUnion (GetRequiredOptions()[i], GetOptionalOptions()[i], union_set); 677 if (IsASubset (m_seen_options, union_set)) 678 options_are_valid = true; 679 } 680 } 681 682 return options_are_valid; 683 } 684 685 bool 686 Options::HandleOptionCompletion 687 ( 688 Args &input, 689 OptionElementVector &opt_element_vector, 690 int cursor_index, 691 int char_pos, 692 int match_start_point, 693 int max_return_elements, 694 bool &word_complete, 695 lldb_private::StringList &matches 696 ) 697 { 698 word_complete = true; 699 700 // For now we just scan the completions to see if the cursor position is in 701 // an option or its argument. Otherwise we'll call HandleArgumentCompletion. 702 // In the future we can use completion to validate options as well if we want. 703 704 const OptionDefinition *opt_defs = GetDefinitions(); 705 706 std::string cur_opt_std_str (input.GetArgumentAtIndex(cursor_index)); 707 cur_opt_std_str.erase(char_pos); 708 const char *cur_opt_str = cur_opt_std_str.c_str(); 709 710 for (int i = 0; i < opt_element_vector.size(); i++) 711 { 712 int opt_pos = opt_element_vector[i].opt_pos; 713 int opt_arg_pos = opt_element_vector[i].opt_arg_pos; 714 int opt_defs_index = opt_element_vector[i].opt_defs_index; 715 if (opt_pos == cursor_index) 716 { 717 // We're completing the option itself. 718 719 if (opt_defs_index == OptionArgElement::eBareDash) 720 { 721 // We're completing a bare dash. That means all options are open. 722 // FIXME: We should scan the other options provided and only complete options 723 // within the option group they belong to. 724 char opt_str[3] = {'-', 'a', '\0'}; 725 726 for (int j = 0 ; opt_defs[j].short_option != 0 ; j++) 727 { 728 opt_str[1] = opt_defs[j].short_option; 729 matches.AppendString (opt_str); 730 } 731 return true; 732 } 733 else if (opt_defs_index == OptionArgElement::eBareDoubleDash) 734 { 735 std::string full_name ("--"); 736 for (int j = 0 ; opt_defs[j].short_option != 0 ; j++) 737 { 738 full_name.erase(full_name.begin() + 2, full_name.end()); 739 full_name.append (opt_defs[j].long_option); 740 matches.AppendString (full_name.c_str()); 741 } 742 return true; 743 } 744 else if (opt_defs_index != OptionArgElement::eUnrecognizedArg) 745 { 746 // We recognized it, if it an incomplete long option, complete it anyway (getopt_long is 747 // happy with shortest unique string, but it's still a nice thing to do.) Otherwise return 748 // The string so the upper level code will know this is a full match and add the " ". 749 if (cur_opt_str && strlen (cur_opt_str) > 2 750 && cur_opt_str[0] == '-' && cur_opt_str[1] == '-' 751 && strcmp (opt_defs[opt_defs_index].long_option, cur_opt_str) != 0) 752 { 753 std::string full_name ("--"); 754 full_name.append (opt_defs[opt_defs_index].long_option); 755 matches.AppendString(full_name.c_str()); 756 return true; 757 } 758 else 759 { 760 matches.AppendString(input.GetArgumentAtIndex(cursor_index)); 761 return true; 762 } 763 } 764 else 765 { 766 // FIXME - not handling wrong options yet: 767 // Check to see if they are writing a long option & complete it. 768 // I think we will only get in here if the long option table has two elements 769 // that are not unique up to this point. getopt_long does shortest unique match 770 // for long options already. 771 772 if (cur_opt_str && strlen (cur_opt_str) > 2 773 && cur_opt_str[0] == '-' && cur_opt_str[1] == '-') 774 { 775 for (int j = 0 ; opt_defs[j].short_option != 0 ; j++) 776 { 777 if (strstr(opt_defs[j].long_option, cur_opt_str + 2) == opt_defs[j].long_option) 778 { 779 std::string full_name ("--"); 780 full_name.append (opt_defs[j].long_option); 781 // The options definitions table has duplicates because of the 782 // way the grouping information is stored, so only add once. 783 bool duplicate = false; 784 for (int k = 0; k < matches.GetSize(); k++) 785 { 786 if (matches.GetStringAtIndex(k) == full_name) 787 { 788 duplicate = true; 789 break; 790 } 791 } 792 if (!duplicate) 793 matches.AppendString(full_name.c_str()); 794 } 795 } 796 } 797 return true; 798 } 799 800 801 } 802 else if (opt_arg_pos == cursor_index) 803 { 804 // Okay the cursor is on the completion of an argument. 805 // See if it has a completion, otherwise return no matches. 806 807 if (opt_defs_index != -1) 808 { 809 HandleOptionArgumentCompletion (input, 810 cursor_index, 811 strlen (input.GetArgumentAtIndex(cursor_index)), 812 opt_element_vector, 813 i, 814 match_start_point, 815 max_return_elements, 816 word_complete, 817 matches); 818 return true; 819 } 820 else 821 { 822 // No completion callback means no completions... 823 return true; 824 } 825 826 } 827 else 828 { 829 // Not the last element, keep going. 830 continue; 831 } 832 } 833 return false; 834 } 835 836 bool 837 Options::HandleOptionArgumentCompletion 838 ( 839 Args &input, 840 int cursor_index, 841 int char_pos, 842 OptionElementVector &opt_element_vector, 843 int opt_element_index, 844 int match_start_point, 845 int max_return_elements, 846 bool &word_complete, 847 lldb_private::StringList &matches 848 ) 849 { 850 const OptionDefinition *opt_defs = GetDefinitions(); 851 std::auto_ptr<SearchFilter> filter_ap; 852 853 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos; 854 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index; 855 856 // See if this is an enumeration type option, and if so complete it here: 857 858 OptionEnumValueElement *enum_values = opt_defs[opt_defs_index].enum_values; 859 if (enum_values != NULL) 860 { 861 bool return_value = false; 862 std::string match_string(input.GetArgumentAtIndex (opt_arg_pos), input.GetArgumentAtIndex (opt_arg_pos) + char_pos); 863 for (int i = 0; enum_values[i].string_value != NULL; i++) 864 { 865 if (strstr(enum_values[i].string_value, match_string.c_str()) == enum_values[i].string_value) 866 { 867 matches.AppendString (enum_values[i].string_value); 868 return_value = true; 869 } 870 } 871 return return_value; 872 } 873 874 // If this is a source file or symbol type completion, and there is a 875 // -shlib option somewhere in the supplied arguments, then make a search filter 876 // for that shared library. 877 // FIXME: Do we want to also have an "OptionType" so we don't have to match string names? 878 879 uint32_t completion_mask = opt_defs[opt_defs_index].completion_type; 880 881 if (completion_mask == 0) 882 { 883 lldb::CommandArgumentType option_arg_type = opt_defs[opt_defs_index].argument_type; 884 if (option_arg_type != eArgTypeNone) 885 { 886 CommandObject::ArgumentTableEntry *arg_entry = CommandObject::FindArgumentDataByType (opt_defs[opt_defs_index].argument_type); 887 if (arg_entry) 888 completion_mask = arg_entry->completion_type; 889 } 890 } 891 892 if (completion_mask & CommandCompletions::eSourceFileCompletion 893 || completion_mask & CommandCompletions::eSymbolCompletion) 894 { 895 for (int i = 0; i < opt_element_vector.size(); i++) 896 { 897 int cur_defs_index = opt_element_vector[i].opt_defs_index; 898 int cur_arg_pos = opt_element_vector[i].opt_arg_pos; 899 const char *cur_opt_name = opt_defs[cur_defs_index].long_option; 900 901 // If this is the "shlib" option and there was an argument provided, 902 // restrict it to that shared library. 903 if (strcmp(cur_opt_name, "shlib") == 0 && cur_arg_pos != -1) 904 { 905 const char *module_name = input.GetArgumentAtIndex(cur_arg_pos); 906 if (module_name) 907 { 908 FileSpec module_spec(module_name, false); 909 lldb::TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget(); 910 // Search filters require a target... 911 if (target_sp) 912 filter_ap.reset (new SearchFilterByModule (target_sp, module_spec)); 913 } 914 break; 915 } 916 } 917 } 918 919 return CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter, 920 completion_mask, 921 input.GetArgumentAtIndex (opt_arg_pos), 922 match_start_point, 923 max_return_elements, 924 filter_ap.get(), 925 word_complete, 926 matches); 927 928 } 929 930 931 void 932 OptionGroupOptions::Append (OptionGroup* group) 933 { 934 const OptionDefinition* group_option_defs = group->GetDefinitions (); 935 const uint32_t group_option_count = group->GetNumDefinitions(); 936 for (uint32_t i=0; i<group_option_count; ++i) 937 { 938 m_option_infos.push_back (OptionInfo (group, i)); 939 m_option_defs.push_back (group_option_defs[i]); 940 } 941 } 942 943 void 944 OptionGroupOptions::Append (OptionGroup* group, 945 uint32_t src_mask, 946 uint32_t dst_mask) 947 { 948 const OptionDefinition* group_option_defs = group->GetDefinitions (); 949 const uint32_t group_option_count = group->GetNumDefinitions(); 950 for (uint32_t i=0; i<group_option_count; ++i) 951 { 952 if (group_option_defs[i].usage_mask & src_mask) 953 { 954 m_option_infos.push_back (OptionInfo (group, i)); 955 m_option_defs.push_back (group_option_defs[i]); 956 m_option_defs.back().usage_mask = dst_mask; 957 } 958 } 959 } 960 961 void 962 OptionGroupOptions::Finalize () 963 { 964 m_did_finalize = true; 965 OptionDefinition empty_option_def = { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }; 966 m_option_defs.push_back (empty_option_def); 967 } 968 969 Error 970 OptionGroupOptions::SetOptionValue (uint32_t option_idx, 971 const char *option_value) 972 { 973 // After calling OptionGroupOptions::Append(...), you must finalize the groups 974 // by calling OptionGroupOptions::Finlize() 975 assert (m_did_finalize); 976 assert (m_option_infos.size() + 1 == m_option_defs.size()); 977 Error error; 978 if (option_idx < m_option_infos.size()) 979 { 980 error = m_option_infos[option_idx].option_group->SetOptionValue (m_interpreter, 981 m_option_infos[option_idx].option_index, 982 option_value); 983 984 } 985 else 986 { 987 error.SetErrorString ("invalid option index"); // Shouldn't happen... 988 } 989 return error; 990 } 991 992 void 993 OptionGroupOptions::OptionParsingStarting () 994 { 995 std::set<OptionGroup*> group_set; 996 OptionInfos::iterator pos, end = m_option_infos.end(); 997 for (pos = m_option_infos.begin(); pos != end; ++pos) 998 { 999 OptionGroup* group = pos->option_group; 1000 if (group_set.find(group) == group_set.end()) 1001 { 1002 group->OptionParsingStarting (m_interpreter); 1003 group_set.insert(group); 1004 } 1005 } 1006 } 1007 Error 1008 OptionGroupOptions::OptionParsingFinished () 1009 { 1010 std::set<OptionGroup*> group_set; 1011 Error error; 1012 OptionInfos::iterator pos, end = m_option_infos.end(); 1013 for (pos = m_option_infos.begin(); pos != end; ++pos) 1014 { 1015 OptionGroup* group = pos->option_group; 1016 if (group_set.find(group) == group_set.end()) 1017 { 1018 error = group->OptionParsingFinished (m_interpreter); 1019 group_set.insert(group); 1020 if (error.Fail()) 1021 return error; 1022 } 1023 } 1024 return error; 1025 } 1026