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