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