blob: f8eadcc43d53011f68a5ed8bda3423a57a9f4877 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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 Ingham40af72e2010-06-15 19:49:27 +000010#include "lldb/Interpreter/Options.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011
12// C Includes
13// C++ Includes
Caroline Ticef362c452010-09-09 16:44:14 +000014#include <algorithm>
Greg Claytonab65b342011-04-13 22:47:15 +000015#include <bitset>
Greg Clayton3bcdfc02012-12-04 00:32:51 +000016#include <map>
Enrico Granatabef55ac2016-03-14 22:17:04 +000017#include <set>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018
19// Other libraries and framework includes
20// Project includes
Kate Stoneb9c1b512016-09-06 20:57:50 +000021#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Interpreter/CommandCompletions.h"
23#include "lldb/Interpreter/CommandInterpreter.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000024#include "lldb/Interpreter/CommandObject.h"
25#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Target/Target.h"
27
28using namespace lldb;
29using namespace lldb_private;
30
31//-------------------------------------------------------------------------
32// Options
33//-------------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000034Options::Options() : m_getopt_table() { BuildValidOptionSets(); }
35
36Options::~Options() {}
37
38void Options::NotifyOptionParsingStarting(ExecutionContext *execution_context) {
39 m_seen_options.clear();
40 // Let the subclass reset its option values
41 OptionParsingStarting(execution_context);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042}
43
Kate Stoneb9c1b512016-09-06 20:57:50 +000044Error Options::NotifyOptionParsingFinished(
45 ExecutionContext *execution_context) {
46 return OptionParsingFinished(execution_context);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047}
48
Kate Stoneb9c1b512016-09-06 20:57:50 +000049void Options::OptionSeen(int option_idx) { m_seen_options.insert(option_idx); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050
51// Returns true is set_a is a subset of set_b; Otherwise returns false.
52
Kate Stoneb9c1b512016-09-06 20:57:50 +000053bool Options::IsASubset(const OptionSet &set_a, const OptionSet &set_b) {
54 bool is_a_subset = true;
55 OptionSet::const_iterator pos_a;
56 OptionSet::const_iterator pos_b;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 // set_a is a subset of set_b if every member of set_a is also a member of
59 // set_b
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 for (pos_a = set_a.begin(); pos_a != set_a.end() && is_a_subset; ++pos_a) {
62 pos_b = set_b.find(*pos_a);
63 if (pos_b == set_b.end())
64 is_a_subset = false;
65 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 return is_a_subset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068}
69
Kate Stoneb9c1b512016-09-06 20:57:50 +000070// Returns the set difference set_a - set_b, i.e. { x | ElementOf (x, set_a) &&
71// !ElementOf (x, set_b) }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072
Kate Stoneb9c1b512016-09-06 20:57:50 +000073size_t Options::OptionsSetDiff(const OptionSet &set_a, const OptionSet &set_b,
74 OptionSet &diffs) {
75 size_t num_diffs = 0;
76 OptionSet::const_iterator pos_a;
77 OptionSet::const_iterator pos_b;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 for (pos_a = set_a.begin(); pos_a != set_a.end(); ++pos_a) {
80 pos_b = set_b.find(*pos_a);
81 if (pos_b == set_b.end()) {
82 ++num_diffs;
83 diffs.insert(*pos_a);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 return num_diffs;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088}
89
Kate Stoneb9c1b512016-09-06 20:57:50 +000090// Returns the union of set_a and set_b. Does not put duplicate members into
91// the union.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092
Kate Stoneb9c1b512016-09-06 20:57:50 +000093void Options::OptionsSetUnion(const OptionSet &set_a, const OptionSet &set_b,
94 OptionSet &union_set) {
95 OptionSet::const_iterator pos;
96 OptionSet::iterator pos_union;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 // Put all the elements of set_a into the union.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 for (pos = set_a.begin(); pos != set_a.end(); ++pos)
101 union_set.insert(*pos);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 // Put all the elements of set_b that are not already there into the union.
104 for (pos = set_b.begin(); pos != set_b.end(); ++pos) {
105 pos_union = union_set.find(*pos);
106 if (pos_union == union_set.end())
107 union_set.insert(*pos);
108 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109}
110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111bool Options::VerifyOptions(CommandReturnObject &result) {
112 bool options_are_valid = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 int num_levels = GetRequiredOptions().size();
115 if (num_levels) {
116 for (int i = 0; i < num_levels && !options_are_valid; ++i) {
117 // This is the correct set of options if: 1). m_seen_options contains all
118 // of m_required_options[i]
119 // (i.e. all the required options at this level are a subset of
120 // m_seen_options); AND
121 // 2). { m_seen_options - m_required_options[i] is a subset of
122 // m_options_options[i] (i.e. all the rest of
123 // m_seen_options are in the set of optional options at this level.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 // Check to see if all of m_required_options[i] are a subset of
126 // m_seen_options
127 if (IsASubset(GetRequiredOptions()[i], m_seen_options)) {
128 // Construct the set difference: remaining_options = {m_seen_options} -
129 // {m_required_options[i]}
130 OptionSet remaining_options;
131 OptionsSetDiff(m_seen_options, GetRequiredOptions()[i],
132 remaining_options);
133 // Check to see if remaining_options is a subset of
134 // m_optional_options[i]
135 if (IsASubset(remaining_options, GetOptionalOptions()[i]))
136 options_are_valid = true;
137 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 } else {
140 options_are_valid = true;
141 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 if (options_are_valid) {
144 result.SetStatus(eReturnStatusSuccessFinishNoResult);
145 } else {
146 result.AppendError("invalid combination of options for the given command");
147 result.SetStatus(eReturnStatusFailed);
148 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 return options_are_valid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151}
152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153// This is called in the Options constructor, though we could call it lazily if
154// that ends up being
Jim Ingham86511212010-06-15 18:47:14 +0000155// a performance problem.
156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157void Options::BuildValidOptionSets() {
158 // Check to see if we already did this.
159 if (m_required_options.size() != 0)
160 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 // Check to see if there are any options.
163 int num_options = NumCommandOptions();
164 if (num_options == 0)
165 return;
166
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000167 auto opt_defs = GetDefinitions();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 m_required_options.resize(1);
169 m_optional_options.resize(1);
170
171 // First count the number of option sets we've got. Ignore
172 // LLDB_ALL_OPTION_SETS...
173
174 uint32_t num_option_sets = 0;
175
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000176 for (const auto &def : opt_defs) {
177 uint32_t this_usage_mask = def.usage_mask;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 if (this_usage_mask == LLDB_OPT_SET_ALL) {
179 if (num_option_sets == 0)
180 num_option_sets = 1;
181 } else {
182 for (uint32_t j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++) {
183 if (this_usage_mask & (1 << j)) {
184 if (num_option_sets <= j)
185 num_option_sets = j + 1;
186 }
187 }
188 }
189 }
190
191 if (num_option_sets > 0) {
192 m_required_options.resize(num_option_sets);
193 m_optional_options.resize(num_option_sets);
194
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000195 for (const auto &def : opt_defs) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 for (uint32_t j = 0; j < num_option_sets; j++) {
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000197 if (def.usage_mask & 1 << j) {
198 if (def.required)
199 m_required_options[j].insert(def.short_option);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200 else
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000201 m_optional_options[j].insert(def.short_option);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 }
203 }
204 }
205 }
206}
207
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000208uint32_t Options::NumCommandOptions() { return GetDefinitions().size(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209
210Option *Options::GetLongOptions() {
211 // Check to see if this has already been done.
212 if (m_getopt_table.empty()) {
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000213 auto defs = GetDefinitions();
214 if (defs.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215 return nullptr;
216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 std::map<int, uint32_t> option_seen;
Enrico Granata4ebb8a42016-03-15 01:17:32 +0000218
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000219 m_getopt_table.resize(defs.size() + 1);
220 for (size_t i = 0; i < defs.size(); ++i) {
221 const int short_opt = defs[i].short_option;
Greg Claytoned8a7052010-09-18 03:37:20 +0000222
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000223 m_getopt_table[i].definition = &defs[i];
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224 m_getopt_table[i].flag = nullptr;
225 m_getopt_table[i].val = short_opt;
Enrico Granatabef55ac2016-03-14 22:17:04 +0000226
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 if (option_seen.find(short_opt) == option_seen.end()) {
228 option_seen[short_opt] = i;
229 } else if (short_opt) {
230 m_getopt_table[i].val = 0;
231 std::map<int, uint32_t>::const_iterator pos =
232 option_seen.find(short_opt);
233 StreamString strm;
234 if (isprint8(short_opt))
235 Host::SystemLog(Host::eSystemLogError,
236 "option[%u] --%s has a short option -%c that "
237 "conflicts with option[%u] --%s, short option won't "
238 "be used for --%s\n",
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000239 (int)i, defs[i].long_option, short_opt, pos->second,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240 m_getopt_table[pos->second].definition->long_option,
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000241 defs[i].long_option);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 else
243 Host::SystemLog(Host::eSystemLogError,
244 "option[%u] --%s has a short option 0x%x that "
245 "conflicts with option[%u] --%s, short option won't "
246 "be used for --%s\n",
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000247 (int)i, defs[i].long_option, short_opt, pos->second,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 m_getopt_table[pos->second].definition->long_option,
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000249 defs[i].long_option);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251 }
252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 // getopt_long_only requires a NULL final entry in the table:
254
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000255 m_getopt_table.back().definition = nullptr;
256 m_getopt_table.back().flag = nullptr;
257 m_getopt_table.back().val = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258 }
259
260 if (m_getopt_table.empty())
261 return nullptr;
262
263 return &m_getopt_table.front();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264}
265
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266// This function takes INDENT, which tells how many spaces to output at the
267// front of each line; SPACES, which is
268// a string containing 80 spaces; and TEXT, which is the text that is to be
269// output. It outputs the text, on
270// multiple lines if necessary, to RESULT, with INDENT spaces at the front of
271// each line. It breaks lines on spaces,
272// tabs or newlines, shortening the line if necessary to not break in the middle
273// of a word. It assumes that each
274// output line should contain a maximum of OUTPUT_MAX_COLUMNS characters.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276void Options::OutputFormattedUsageText(Stream &strm,
277 const OptionDefinition &option_def,
278 uint32_t output_max_columns) {
279 std::string actual_text;
280 if (option_def.validator) {
281 const char *condition = option_def.validator->ShortConditionString();
282 if (condition) {
283 actual_text = "[";
284 actual_text.append(condition);
285 actual_text.append("] ");
286 }
287 }
288 actual_text.append(option_def.usage_text);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290 // Will it all fit on one line?
291
292 if (static_cast<uint32_t>(actual_text.length() + strm.GetIndentLevel()) <
293 output_max_columns) {
294 // Output it as a single line.
295 strm.Indent(actual_text.c_str());
296 strm.EOL();
297 } else {
298 // We need to break it up into multiple lines.
299
300 int text_width = output_max_columns - strm.GetIndentLevel() - 1;
301 int start = 0;
302 int end = start;
303 int final_end = actual_text.length();
304 int sub_len;
305
306 while (end < final_end) {
307 // Don't start the 'text' on a space, since we're already outputting the
308 // indentation.
309 while ((start < final_end) && (actual_text[start] == ' '))
310 start++;
311
312 end = start + text_width;
313 if (end > final_end)
314 end = final_end;
315 else {
316 // If we're not at the end of the text, make sure we break the line on
317 // white space.
318 while (end > start && actual_text[end] != ' ' &&
319 actual_text[end] != '\t' && actual_text[end] != '\n')
320 end--;
321 }
322
323 sub_len = end - start;
324 if (start != 0)
325 strm.EOL();
326 strm.Indent();
327 assert(start < final_end);
328 assert(start + sub_len <= final_end);
329 strm.Write(actual_text.c_str() + start, sub_len);
330 start = end + 1;
331 }
332 strm.EOL();
333 }
334}
335
336bool Options::SupportsLongOption(const char *long_option) {
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000337 if (!long_option || !long_option[0])
338 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000340 auto opt_defs = GetDefinitions();
341 if (opt_defs.empty())
342 return false;
343
344 const char *long_option_name = long_option;
345 if (long_option[0] == '-' && long_option[1] == '-')
346 long_option_name += 2;
347
348 for (auto &def : opt_defs) {
349 if (!def.long_option)
350 continue;
351
352 if (strcmp(def.long_option, long_option_name) == 0)
353 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 }
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000355
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 return false;
357}
358
359enum OptionDisplayType {
360 eDisplayBestOption,
361 eDisplayShortOption,
362 eDisplayLongOption
363};
364
365static bool PrintOption(const OptionDefinition &opt_def,
366 OptionDisplayType display_type, const char *header,
367 const char *footer, bool show_optional, Stream &strm) {
368 const bool has_short_option = isprint8(opt_def.short_option) != 0;
369
370 if (display_type == eDisplayShortOption && !has_short_option)
371 return false;
372
373 if (header && header[0])
374 strm.PutCString(header);
375
376 if (show_optional && !opt_def.required)
377 strm.PutChar('[');
378 const bool show_short_option =
379 has_short_option && display_type != eDisplayLongOption;
380 if (show_short_option)
381 strm.Printf("-%c", opt_def.short_option);
382 else
383 strm.Printf("--%s", opt_def.long_option);
384 switch (opt_def.option_has_arg) {
385 case OptionParser::eNoArgument:
386 break;
387 case OptionParser::eRequiredArgument:
388 strm.Printf(" <%s>", CommandObject::GetArgumentName(opt_def.argument_type));
389 break;
390
391 case OptionParser::eOptionalArgument:
392 strm.Printf("%s[<%s>]", show_short_option ? "" : "=",
393 CommandObject::GetArgumentName(opt_def.argument_type));
394 break;
395 }
396 if (show_optional && !opt_def.required)
397 strm.PutChar(']');
398 if (footer && footer[0])
399 strm.PutCString(footer);
400 return true;
401}
402
403void Options::GenerateOptionUsage(Stream &strm, CommandObject *cmd,
404 uint32_t screen_width) {
405 const bool only_print_args = cmd->IsDashDashCommand();
406
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000407 auto opt_defs = GetDefinitions();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408 const uint32_t save_indent_level = strm.GetIndentLevel();
409 const char *name;
410
411 StreamString arguments_str;
412
413 if (cmd) {
414 name = cmd->GetCommandName();
415 cmd->GetFormattedCommandArguments(arguments_str);
416 } else
417 name = "";
418
419 strm.PutCString("\nCommand Options Usage:\n");
420
421 strm.IndentMore(2);
422
423 // First, show each usage level set of options, e.g. <cmd>
424 // [options-for-level-0]
425 // <cmd>
426 // [options-for-level-1]
427 // etc.
428
429 const uint32_t num_options = NumCommandOptions();
430 if (num_options == 0)
431 return;
432
433 uint32_t num_option_sets = GetRequiredOptions().size();
434
435 uint32_t i;
436
437 if (!only_print_args) {
438 for (uint32_t opt_set = 0; opt_set < num_option_sets; ++opt_set) {
439 uint32_t opt_set_mask;
440
441 opt_set_mask = 1 << opt_set;
442 if (opt_set > 0)
443 strm.Printf("\n");
444 strm.Indent(name);
445
446 // Different option sets may require different args.
447 StreamString args_str;
448 if (cmd)
449 cmd->GetFormattedCommandArguments(args_str, opt_set_mask);
450
451 // First go through and print all options that take no arguments as
452 // a single string. If a command has "-a" "-b" and "-c", this will show
453 // up as [-abc]
454
455 std::set<int> options;
456 std::set<int>::const_iterator options_pos, options_end;
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000457 for (auto &def : opt_defs) {
458 if (def.usage_mask & opt_set_mask && isprint8(def.short_option)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 // Add current option to the end of out_stream.
460
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000461 if (def.required && def.option_has_arg == OptionParser::eNoArgument) {
462 options.insert(def.short_option);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463 }
464 }
465 }
466
467 if (options.empty() == false) {
468 // We have some required options with no arguments
469 strm.PutCString(" -");
470 for (i = 0; i < 2; ++i)
471 for (options_pos = options.begin(), options_end = options.end();
472 options_pos != options_end; ++options_pos) {
473 if (i == 0 && ::islower(*options_pos))
474 continue;
475 if (i == 1 && ::isupper(*options_pos))
476 continue;
477 strm << (char)*options_pos;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478 }
479 }
480
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000481 options.clear();
482 for (auto &def : opt_defs) {
483 if (def.usage_mask & opt_set_mask && isprint8(def.short_option)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000484 // Add current option to the end of out_stream.
485
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000486 if (def.required == false &&
487 def.option_has_arg == OptionParser::eNoArgument) {
488 options.insert(def.short_option);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489 }
490 }
491 }
492
493 if (options.empty() == false) {
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; ++options_pos) {
499 if (i == 0 && ::islower(*options_pos))
500 continue;
501 if (i == 1 && ::isupper(*options_pos))
502 continue;
503 strm << (char)*options_pos;
504 }
505 strm.PutChar(']');
506 }
507
508 // First go through and print the required options (list them up front).
509
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000510 for (auto &def : opt_defs) {
511 if (def.usage_mask & opt_set_mask && isprint8(def.short_option)) {
512 if (def.required && def.option_has_arg != OptionParser::eNoArgument)
513 PrintOption(def, eDisplayBestOption, " ", nullptr, true, strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 }
515 }
516
517 // Now go through again, and this time only print the optional options.
518
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000519 for (auto &def : opt_defs) {
520 if (def.usage_mask & opt_set_mask) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000521 // Add current option to the end of out_stream.
522
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000523 if (!def.required && def.option_has_arg != OptionParser::eNoArgument)
524 PrintOption(def, eDisplayBestOption, " ", nullptr, true, strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000525 }
526 }
527
528 if (args_str.GetSize() > 0) {
529 if (cmd->WantsRawCommandString() && !only_print_args)
530 strm.Printf(" --");
531
532 strm.Printf(" %s", args_str.GetData());
533 if (only_print_args)
534 break;
535 }
536 }
537 }
538
539 if (cmd && (only_print_args || cmd->WantsRawCommandString()) &&
540 arguments_str.GetSize() > 0) {
541 if (!only_print_args)
542 strm.PutChar('\n');
543 strm.Indent(name);
544 strm.Printf(" %s", arguments_str.GetData());
545 }
546
547 strm.Printf("\n\n");
548
549 if (!only_print_args) {
550 // Now print out all the detailed information about the various options:
551 // long form, short form and help text:
552 // -short <argument> ( --long_name <argument> )
553 // help text
554
555 // This variable is used to keep track of which options' info we've printed
556 // out, because some options can be in
557 // more than one usage level, but we only want to print the long form of its
558 // information once.
559
560 std::multimap<int, uint32_t> options_seen;
561 strm.IndentMore(5);
562
563 // Put the unique command options in a vector & sort it, so we can output
564 // them alphabetically (by short_option)
565 // when writing out detailed help for each option.
566
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000567 i = 0;
568 for (auto &def : opt_defs)
569 options_seen.insert(std::make_pair(def.short_option, i++));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570
571 // Go through the unique'd and alphabetically sorted vector of options, find
572 // the table entry for each option
573 // and write out the detailed help information for that option.
574
575 bool first_option_printed = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000576
577 for (auto pos : options_seen) {
578 i = pos.second;
579 // Print out the help information for this option.
580
581 // Put a newline separation between arguments
582 if (first_option_printed)
583 strm.EOL();
584 else
585 first_option_printed = true;
586
587 CommandArgumentType arg_type = opt_defs[i].argument_type;
588
589 StreamString arg_name_str;
590 arg_name_str.Printf("<%s>", CommandObject::GetArgumentName(arg_type));
591
592 strm.Indent();
593 if (opt_defs[i].short_option && isprint8(opt_defs[i].short_option)) {
594 PrintOption(opt_defs[i], eDisplayShortOption, nullptr, nullptr, false,
595 strm);
596 PrintOption(opt_defs[i], eDisplayLongOption, " ( ", " )", false, strm);
597 } else {
598 // Short option is not printable, just print long option
599 PrintOption(opt_defs[i], eDisplayLongOption, nullptr, nullptr, false,
600 strm);
601 }
602 strm.EOL();
603
604 strm.IndentMore(5);
605
606 if (opt_defs[i].usage_text)
607 OutputFormattedUsageText(strm, opt_defs[i], screen_width);
608 if (opt_defs[i].enum_values != nullptr) {
609 strm.Indent();
610 strm.Printf("Values: ");
611 for (int k = 0; opt_defs[i].enum_values[k].string_value != nullptr;
612 k++) {
613 if (k == 0)
614 strm.Printf("%s", opt_defs[i].enum_values[k].string_value);
615 else
616 strm.Printf(" | %s", opt_defs[i].enum_values[k].string_value);
617 }
618 strm.EOL();
619 }
620 strm.IndentLess(5);
621 }
622 }
623
624 // Restore the indent level
625 strm.SetIndentLevel(save_indent_level);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626}
627
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628// This function is called when we have been given a potentially incomplete set
629// of
630// options, such as when an alias has been defined (more options might be added
631// at
632// at the time the alias is invoked). We need to verify that the options in the
633// set
634// m_seen_options are all part of a set that may be used together, but
635// m_seen_options
636// may be missing some of the "required" options.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638bool Options::VerifyPartialOptions(CommandReturnObject &result) {
639 bool options_are_valid = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640
Kate Stoneb9c1b512016-09-06 20:57:50 +0000641 int num_levels = GetRequiredOptions().size();
642 if (num_levels) {
643 for (int i = 0; i < num_levels && !options_are_valid; ++i) {
644 // In this case we are treating all options as optional rather than
645 // required.
646 // Therefore a set of options is correct if m_seen_options is a subset of
647 // the
648 // union of m_required_options and m_optional_options.
649 OptionSet union_set;
650 OptionsSetUnion(GetRequiredOptions()[i], GetOptionalOptions()[i],
651 union_set);
652 if (IsASubset(m_seen_options, union_set))
653 options_are_valid = true;
654 }
655 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000656
Kate Stoneb9c1b512016-09-06 20:57:50 +0000657 return options_are_valid;
658}
Jim Inghamd6ccc602010-06-24 20:30:15 +0000659
Kate Stoneb9c1b512016-09-06 20:57:50 +0000660bool Options::HandleOptionCompletion(
661 Args &input, OptionElementVector &opt_element_vector, int cursor_index,
662 int char_pos, int match_start_point, int max_return_elements,
663 CommandInterpreter &interpreter, bool &word_complete,
664 lldb_private::StringList &matches) {
665 word_complete = true;
666
667 // For now we just scan the completions to see if the cursor position is in
668 // an option or its argument. Otherwise we'll call HandleArgumentCompletion.
669 // In the future we can use completion to validate options as well if we want.
670
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000671 auto opt_defs = GetDefinitions();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672
673 std::string cur_opt_std_str(input.GetArgumentAtIndex(cursor_index));
674 cur_opt_std_str.erase(char_pos);
675 const char *cur_opt_str = cur_opt_std_str.c_str();
676
677 for (size_t i = 0; i < opt_element_vector.size(); i++) {
678 int opt_pos = opt_element_vector[i].opt_pos;
679 int opt_arg_pos = opt_element_vector[i].opt_arg_pos;
680 int opt_defs_index = opt_element_vector[i].opt_defs_index;
681 if (opt_pos == cursor_index) {
682 // We're completing the option itself.
683
684 if (opt_defs_index == OptionArgElement::eBareDash) {
685 // We're completing a bare dash. That means all options are open.
686 // FIXME: We should scan the other options provided and only complete
687 // options
688 // within the option group they belong to.
689 char opt_str[3] = {'-', 'a', '\0'};
690
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000691 for (auto &def : opt_defs) {
692 if (!def.short_option)
693 continue;
694 opt_str[1] = def.short_option;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000695 matches.AppendString(opt_str);
696 }
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000697
Kate Stoneb9c1b512016-09-06 20:57:50 +0000698 return true;
699 } else if (opt_defs_index == OptionArgElement::eBareDoubleDash) {
700 std::string full_name("--");
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000701 for (auto &def : opt_defs) {
702 if (!def.short_option)
703 continue;
704
Kate Stoneb9c1b512016-09-06 20:57:50 +0000705 full_name.erase(full_name.begin() + 2, full_name.end());
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000706 full_name.append(def.long_option);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000707 matches.AppendString(full_name.c_str());
708 }
709 return true;
710 } else if (opt_defs_index != OptionArgElement::eUnrecognizedArg) {
711 // We recognized it, if it an incomplete long option, complete it anyway
712 // (getopt_long_only is
713 // happy with shortest unique string, but it's still a nice thing to
714 // do.) Otherwise return
715 // The string so the upper level code will know this is a full match and
716 // add the " ".
717 if (cur_opt_str && strlen(cur_opt_str) > 2 && cur_opt_str[0] == '-' &&
718 cur_opt_str[1] == '-' &&
719 strcmp(opt_defs[opt_defs_index].long_option, cur_opt_str) != 0) {
720 std::string full_name("--");
721 full_name.append(opt_defs[opt_defs_index].long_option);
722 matches.AppendString(full_name.c_str());
723 return true;
724 } else {
725 matches.AppendString(input.GetArgumentAtIndex(cursor_index));
726 return true;
727 }
728 } else {
729 // FIXME - not handling wrong options yet:
730 // Check to see if they are writing a long option & complete it.
731 // I think we will only get in here if the long option table has two
732 // elements
733 // that are not unique up to this point. getopt_long_only does shortest
734 // unique match
735 // for long options already.
736
737 if (cur_opt_str && strlen(cur_opt_str) > 2 && cur_opt_str[0] == '-' &&
738 cur_opt_str[1] == '-') {
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000739 for (auto &def : opt_defs) {
740 if (!def.long_option)
741 continue;
742
743 if (strstr(def.long_option, cur_opt_str + 2) == def.long_option) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000744 std::string full_name("--");
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000745 full_name.append(def.long_option);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000746 // The options definitions table has duplicates because of the
747 // way the grouping information is stored, so only add once.
748 bool duplicate = false;
749 for (size_t k = 0; k < matches.GetSize(); k++) {
750 if (matches.GetStringAtIndex(k) == full_name) {
751 duplicate = true;
752 break;
Jim Inghamd6ccc602010-06-24 20:30:15 +0000753 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754 }
755 if (!duplicate)
756 matches.AppendString(full_name.c_str());
Jim Inghamd6ccc602010-06-24 20:30:15 +0000757 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000758 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000760 return true;
761 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762
Kate Stoneb9c1b512016-09-06 20:57:50 +0000763 } else if (opt_arg_pos == cursor_index) {
764 // Okay the cursor is on the completion of an argument.
765 // See if it has a completion, otherwise return no matches.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000766
Kate Stoneb9c1b512016-09-06 20:57:50 +0000767 if (opt_defs_index != -1) {
768 HandleOptionArgumentCompletion(
769 input, cursor_index, strlen(input.GetArgumentAtIndex(cursor_index)),
770 opt_element_vector, i, match_start_point, max_return_elements,
771 interpreter, word_complete, matches);
772 return true;
773 } else {
774 // No completion callback means no completions...
775 return true;
776 }
777
778 } else {
779 // Not the last element, keep going.
780 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000781 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000782 }
783 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784}
785
Kate Stoneb9c1b512016-09-06 20:57:50 +0000786bool Options::HandleOptionArgumentCompletion(
787 Args &input, int cursor_index, int char_pos,
788 OptionElementVector &opt_element_vector, int opt_element_index,
789 int match_start_point, int max_return_elements,
790 CommandInterpreter &interpreter, bool &word_complete,
791 lldb_private::StringList &matches) {
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000792 auto opt_defs = GetDefinitions();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793 std::unique_ptr<SearchFilter> filter_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000794
Kate Stoneb9c1b512016-09-06 20:57:50 +0000795 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
796 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
797
798 // See if this is an enumeration type option, and if so complete it here:
799
800 OptionEnumValueElement *enum_values = opt_defs[opt_defs_index].enum_values;
801 if (enum_values != nullptr) {
802 bool return_value = false;
803 std::string match_string(input.GetArgumentAtIndex(opt_arg_pos),
804 input.GetArgumentAtIndex(opt_arg_pos) + char_pos);
805 for (int i = 0; enum_values[i].string_value != nullptr; i++) {
806 if (strstr(enum_values[i].string_value, match_string.c_str()) ==
807 enum_values[i].string_value) {
808 matches.AppendString(enum_values[i].string_value);
809 return_value = true;
810 }
811 }
812 return return_value;
813 }
814
815 // If this is a source file or symbol type completion, and there is a
816 // -shlib option somewhere in the supplied arguments, then make a search
817 // filter
818 // for that shared library.
819 // FIXME: Do we want to also have an "OptionType" so we don't have to match
820 // string names?
821
822 uint32_t completion_mask = opt_defs[opt_defs_index].completion_type;
823
824 if (completion_mask == 0) {
825 lldb::CommandArgumentType option_arg_type =
826 opt_defs[opt_defs_index].argument_type;
827 if (option_arg_type != eArgTypeNone) {
828 const CommandObject::ArgumentTableEntry *arg_entry =
829 CommandObject::FindArgumentDataByType(
830 opt_defs[opt_defs_index].argument_type);
831 if (arg_entry)
832 completion_mask = arg_entry->completion_type;
833 }
834 }
835
836 if (completion_mask & CommandCompletions::eSourceFileCompletion ||
837 completion_mask & CommandCompletions::eSymbolCompletion) {
838 for (size_t i = 0; i < opt_element_vector.size(); i++) {
839 int cur_defs_index = opt_element_vector[i].opt_defs_index;
840
841 // trying to use <0 indices will definitely cause problems
842 if (cur_defs_index == OptionArgElement::eUnrecognizedArg ||
843 cur_defs_index == OptionArgElement::eBareDash ||
844 cur_defs_index == OptionArgElement::eBareDoubleDash)
845 continue;
846
847 int cur_arg_pos = opt_element_vector[i].opt_arg_pos;
848 const char *cur_opt_name = opt_defs[cur_defs_index].long_option;
849
850 // If this is the "shlib" option and there was an argument provided,
851 // restrict it to that shared library.
852 if (cur_opt_name && strcmp(cur_opt_name, "shlib") == 0 &&
853 cur_arg_pos != -1) {
854 const char *module_name = input.GetArgumentAtIndex(cur_arg_pos);
855 if (module_name) {
856 FileSpec module_spec(module_name, false);
857 lldb::TargetSP target_sp =
858 interpreter.GetDebugger().GetSelectedTarget();
859 // Search filters require a target...
860 if (target_sp)
861 filter_ap.reset(new SearchFilterByModule(target_sp, module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000862 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000863 break;
864 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000865 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000866 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000867
Kate Stoneb9c1b512016-09-06 20:57:50 +0000868 return CommandCompletions::InvokeCommonCompletionCallbacks(
869 interpreter, completion_mask, input.GetArgumentAtIndex(opt_arg_pos),
870 match_start_point, max_return_elements, filter_ap.get(), word_complete,
871 matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000872}
Greg Claytonf6b8b582011-04-13 00:18:08 +0000873
Kate Stoneb9c1b512016-09-06 20:57:50 +0000874void OptionGroupOptions::Append(OptionGroup *group) {
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000875 auto group_option_defs = group->GetDefinitions();
876 for (uint32_t i = 0; i < group_option_defs.size(); ++i) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000877 m_option_infos.push_back(OptionInfo(group, i));
878 m_option_defs.push_back(group_option_defs[i]);
879 }
Greg Clayton84c39662011-04-27 22:04:39 +0000880}
881
Kate Stoneb9c1b512016-09-06 20:57:50 +0000882const OptionGroup *OptionGroupOptions::GetGroupWithOption(char short_opt) {
883 for (uint32_t i = 0; i < m_option_defs.size(); i++) {
884 OptionDefinition opt_def = m_option_defs[i];
885 if (opt_def.short_option == short_opt)
886 return m_option_infos[i].option_group;
887 }
888 return nullptr;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000889}
890
Kate Stoneb9c1b512016-09-06 20:57:50 +0000891void OptionGroupOptions::Append(OptionGroup *group, uint32_t src_mask,
892 uint32_t dst_mask) {
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000893 auto group_option_defs = group->GetDefinitions();
894 for (uint32_t i = 0; i < group_option_defs.size(); ++i) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000895 if (group_option_defs[i].usage_mask & src_mask) {
896 m_option_infos.push_back(OptionInfo(group, i));
897 m_option_defs.push_back(group_option_defs[i]);
898 m_option_defs.back().usage_mask = dst_mask;
Greg Claytonf6b8b582011-04-13 00:18:08 +0000899 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000900 }
Greg Claytonf6b8b582011-04-13 00:18:08 +0000901}
902
Kate Stoneb9c1b512016-09-06 20:57:50 +0000903void OptionGroupOptions::Finalize() {
904 m_did_finalize = true;
Greg Claytonf6b8b582011-04-13 00:18:08 +0000905}
906
Kate Stoneb9c1b512016-09-06 20:57:50 +0000907Error OptionGroupOptions::SetOptionValue(uint32_t option_idx,
908 const char *option_value,
909 ExecutionContext *execution_context) {
910 // After calling OptionGroupOptions::Append(...), you must finalize the groups
911 // by calling OptionGroupOptions::Finlize()
912 assert(m_did_finalize);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000913 Error error;
914 if (option_idx < m_option_infos.size()) {
915 error = m_option_infos[option_idx].option_group->SetOptionValue(
Zachary Turner8cef4b02016-09-23 17:48:13 +0000916 m_option_infos[option_idx].option_index,
917 llvm::StringRef::withNullAsEmpty(option_value), execution_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000918
919 } else {
920 error.SetErrorString("invalid option index"); // Shouldn't happen...
921 }
922 return error;
Greg Claytonf6b8b582011-04-13 00:18:08 +0000923}
924
Kate Stoneb9c1b512016-09-06 20:57:50 +0000925void OptionGroupOptions::OptionParsingStarting(
926 ExecutionContext *execution_context) {
927 std::set<OptionGroup *> group_set;
928 OptionInfos::iterator pos, end = m_option_infos.end();
929 for (pos = m_option_infos.begin(); pos != end; ++pos) {
930 OptionGroup *group = pos->option_group;
931 if (group_set.find(group) == group_set.end()) {
932 group->OptionParsingStarting(execution_context);
933 group_set.insert(group);
Greg Claytonab65b342011-04-13 22:47:15 +0000934 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000935 }
Greg Claytonf6b8b582011-04-13 00:18:08 +0000936}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000937Error OptionGroupOptions::OptionParsingFinished(
938 ExecutionContext *execution_context) {
939 std::set<OptionGroup *> group_set;
940 Error error;
941 OptionInfos::iterator pos, end = m_option_infos.end();
942 for (pos = m_option_infos.begin(); pos != end; ++pos) {
943 OptionGroup *group = pos->option_group;
944 if (group_set.find(group) == group_set.end()) {
945 error = group->OptionParsingFinished(execution_context);
946 group_set.insert(group);
947 if (error.Fail())
948 return error;
Greg Claytonf6b8b582011-04-13 00:18:08 +0000949 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000950 }
951 return error;
Greg Claytonf6b8b582011-04-13 00:18:08 +0000952}