blob: 7f0e0abc03ea3ac03a842fdeb197c88c6bbf9724 [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>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017
18// Other libraries and framework includes
19// Project includes
20#include "lldb/Interpreter/CommandObject.h"
21#include "lldb/Interpreter/CommandReturnObject.h"
22#include "lldb/Interpreter/CommandCompletions.h"
23#include "lldb/Interpreter/CommandInterpreter.h"
24#include "lldb/Core/StreamString.h"
25#include "lldb/Target/Target.h"
26
27using namespace lldb;
28using namespace lldb_private;
29
30//-------------------------------------------------------------------------
31// Options
32//-------------------------------------------------------------------------
Greg Claytoneb0103f2011-04-07 22:46:35 +000033Options::Options (CommandInterpreter &interpreter) :
34 m_interpreter (interpreter),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035 m_getopt_table ()
36{
Jim Ingham86511212010-06-15 18:47:14 +000037 BuildValidOptionSets();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038}
39
40Options::~Options ()
41{
42}
43
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044void
Greg Claytonf6b8b582011-04-13 00:18:08 +000045Options::NotifyOptionParsingStarting ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046{
47 m_seen_options.clear();
Greg Clayton32e0a752011-03-30 18:16:51 +000048 // Let the subclass reset its option values
Greg Claytonf6b8b582011-04-13 00:18:08 +000049 OptionParsingStarting ();
50}
51
52Error
53Options::NotifyOptionParsingFinished ()
54{
55 return OptionParsingFinished ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056}
57
58void
59Options::OptionSeen (int option_idx)
60{
Greg Clayton3bcdfc02012-12-04 00:32:51 +000061 m_seen_options.insert (option_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062}
63
64// Returns true is set_a is a subset of set_b; Otherwise returns false.
65
66bool
67Options::IsASubset (const OptionSet& set_a, const OptionSet& set_b)
68{
69 bool is_a_subset = true;
70 OptionSet::const_iterator pos_a;
71 OptionSet::const_iterator pos_b;
72
73 // set_a is a subset of set_b if every member of set_a is also a member of set_b
74
75 for (pos_a = set_a.begin(); pos_a != set_a.end() && is_a_subset; ++pos_a)
76 {
77 pos_b = set_b.find(*pos_a);
78 if (pos_b == set_b.end())
79 is_a_subset = false;
80 }
81
82 return is_a_subset;
83}
84
85// Returns the set difference set_a - set_b, i.e. { x | ElementOf (x, set_a) && !ElementOf (x, set_b) }
86
87size_t
88Options::OptionsSetDiff (const OptionSet& set_a, const OptionSet& set_b, OptionSet& diffs)
89{
90 size_t num_diffs = 0;
91 OptionSet::const_iterator pos_a;
92 OptionSet::const_iterator pos_b;
93
94 for (pos_a = set_a.begin(); pos_a != set_a.end(); ++pos_a)
95 {
96 pos_b = set_b.find(*pos_a);
97 if (pos_b == set_b.end())
98 {
99 ++num_diffs;
100 diffs.insert(*pos_a);
101 }
102 }
103
104 return num_diffs;
105}
106
107// Returns the union of set_a and set_b. Does not put duplicate members into the union.
108
109void
110Options::OptionsSetUnion (const OptionSet &set_a, const OptionSet &set_b, OptionSet &union_set)
111{
112 OptionSet::const_iterator pos;
113 OptionSet::iterator pos_union;
114
115 // Put all the elements of set_a into the union.
116
117 for (pos = set_a.begin(); pos != set_a.end(); ++pos)
118 union_set.insert(*pos);
119
120 // Put all the elements of set_b that are not already there into the union.
121 for (pos = set_b.begin(); pos != set_b.end(); ++pos)
122 {
123 pos_union = union_set.find(*pos);
124 if (pos_union == union_set.end())
125 union_set.insert(*pos);
126 }
127}
128
129bool
130Options::VerifyOptions (CommandReturnObject &result)
131{
132 bool options_are_valid = false;
133
Jim Inghamd6ccc602010-06-24 20:30:15 +0000134 int num_levels = GetRequiredOptions().size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 if (num_levels)
136 {
137 for (int i = 0; i < num_levels && !options_are_valid; ++i)
138 {
139 // This is the correct set of options if: 1). m_seen_options contains all of m_required_options[i]
140 // (i.e. all the required options at this level are a subset of m_seen_options); AND
141 // 2). { m_seen_options - m_required_options[i] is a subset of m_options_options[i] (i.e. all the rest of
142 // m_seen_options are in the set of optional options at this level.
143
144 // Check to see if all of m_required_options[i] are a subset of m_seen_options
Jim Inghamd6ccc602010-06-24 20:30:15 +0000145 if (IsASubset (GetRequiredOptions()[i], m_seen_options))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146 {
147 // Construct the set difference: remaining_options = {m_seen_options} - {m_required_options[i]}
148 OptionSet remaining_options;
Jim Inghamd6ccc602010-06-24 20:30:15 +0000149 OptionsSetDiff (m_seen_options, GetRequiredOptions()[i], remaining_options);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150 // Check to see if remaining_options is a subset of m_optional_options[i]
Jim Inghamd6ccc602010-06-24 20:30:15 +0000151 if (IsASubset (remaining_options, GetOptionalOptions()[i]))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152 options_are_valid = true;
153 }
154 }
155 }
156 else
157 {
158 options_are_valid = true;
159 }
160
161 if (options_are_valid)
162 {
163 result.SetStatus (eReturnStatusSuccessFinishNoResult);
164 }
165 else
166 {
167 result.AppendError ("invalid combination of options for the given command");
168 result.SetStatus (eReturnStatusFailed);
169 }
170
171 return options_are_valid;
172}
173
Jim Ingham86511212010-06-15 18:47:14 +0000174// This is called in the Options constructor, though we could call it lazily if that ends up being
175// a performance problem.
176
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177void
178Options::BuildValidOptionSets ()
179{
180 // Check to see if we already did this.
181 if (m_required_options.size() != 0)
182 return;
183
184 // Check to see if there are any options.
185 int num_options = NumCommandOptions ();
186 if (num_options == 0)
187 return;
188
Greg Clayton52ec56c2011-10-29 00:57:28 +0000189 const OptionDefinition *opt_defs = GetDefinitions();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190 m_required_options.resize(1);
191 m_optional_options.resize(1);
Jim Ingham86511212010-06-15 18:47:14 +0000192
193 // First count the number of option sets we've got. Ignore LLDB_ALL_OPTION_SETS...
194
195 uint32_t num_option_sets = 0;
196
197 for (int i = 0; i < num_options; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198 {
Greg Clayton52ec56c2011-10-29 00:57:28 +0000199 uint32_t this_usage_mask = opt_defs[i].usage_mask;
Jim Ingham86511212010-06-15 18:47:14 +0000200 if (this_usage_mask == LLDB_OPT_SET_ALL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201 {
Jim Ingham86511212010-06-15 18:47:14 +0000202 if (num_option_sets == 0)
203 num_option_sets = 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204 }
205 else
206 {
Andy Gibbsa297a972013-06-19 19:04:53 +0000207 for (uint32_t j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++)
Jim Ingham86511212010-06-15 18:47:14 +0000208 {
Jim Inghamd6ccc602010-06-24 20:30:15 +0000209 if (this_usage_mask & (1 << j))
Jim Ingham86511212010-06-15 18:47:14 +0000210 {
211 if (num_option_sets <= j)
212 num_option_sets = j + 1;
213 }
214 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215 }
Jim Ingham86511212010-06-15 18:47:14 +0000216 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217
Jim Ingham86511212010-06-15 18:47:14 +0000218 if (num_option_sets > 0)
219 {
220 m_required_options.resize(num_option_sets);
221 m_optional_options.resize(num_option_sets);
222
223 for (int i = 0; i < num_options; ++i)
224 {
Andy Gibbsa297a972013-06-19 19:04:53 +0000225 for (uint32_t j = 0; j < num_option_sets; j++)
Jim Ingham86511212010-06-15 18:47:14 +0000226 {
Greg Clayton52ec56c2011-10-29 00:57:28 +0000227 if (opt_defs[i].usage_mask & 1 << j)
Jim Ingham86511212010-06-15 18:47:14 +0000228 {
Greg Clayton52ec56c2011-10-29 00:57:28 +0000229 if (opt_defs[i].required)
230 m_required_options[j].insert(opt_defs[i].short_option);
Jim Ingham86511212010-06-15 18:47:14 +0000231 else
Greg Clayton52ec56c2011-10-29 00:57:28 +0000232 m_optional_options[j].insert(opt_defs[i].short_option);
Jim Ingham86511212010-06-15 18:47:14 +0000233 }
234 }
235 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236 }
237}
238
239uint32_t
240Options::NumCommandOptions ()
241{
Greg Clayton52ec56c2011-10-29 00:57:28 +0000242 const OptionDefinition *opt_defs = GetDefinitions ();
Ed Masted78c9572014-04-20 00:31:37 +0000243 if (opt_defs == nullptr)
Jim Ingham86511212010-06-15 18:47:14 +0000244 return 0;
245
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246 int i = 0;
247
Ed Masted78c9572014-04-20 00:31:37 +0000248 if (opt_defs != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249 {
Ed Masted78c9572014-04-20 00:31:37 +0000250 while (opt_defs[i].long_option != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251 ++i;
252 }
253
254 return i;
255}
256
Virgile Belloe2607b52013-09-05 16:42:23 +0000257Option *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258Options::GetLongOptions ()
259{
260 // Check to see if this has already been done.
261 if (m_getopt_table.empty())
262 {
263 // Check to see if there are any options.
264 const uint32_t num_options = NumCommandOptions();
265 if (num_options == 0)
Ed Masted78c9572014-04-20 00:31:37 +0000266 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267
268 uint32_t i;
Greg Clayton52ec56c2011-10-29 00:57:28 +0000269 const OptionDefinition *opt_defs = GetDefinitions();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000271 std::map<int, uint32_t> option_seen;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272
273 m_getopt_table.resize(num_options + 1);
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000274 for (i = 0; i < num_options; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275 {
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000276 const int short_opt = opt_defs[i].short_option;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277
Zachary Turnerd37221d2014-07-09 16:31:49 +0000278 m_getopt_table[i].definition = &opt_defs[i];
Ed Masted78c9572014-04-20 00:31:37 +0000279 m_getopt_table[i].flag = nullptr;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000280 m_getopt_table[i].val = short_opt;
281
282 if (option_seen.find(short_opt) == option_seen.end())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283 {
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000284 option_seen[short_opt] = i;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285 }
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000286 else if (short_opt)
Greg Clayton1c5f1862012-11-30 19:05:35 +0000287 {
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000288 m_getopt_table[i].val = 0;
289 std::map<int, uint32_t>::const_iterator pos = option_seen.find(short_opt);
290 StreamString strm;
Daniel Malea90b0c842012-12-05 20:24:57 +0000291 if (isprint8(short_opt))
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000292 Host::SystemLog (Host::eSystemLogError, "option[%u] --%s has a short option -%c that conflicts with option[%u] --%s, short option won't be used for --%s\n",
293 i,
294 opt_defs[i].long_option,
295 short_opt,
296 pos->second,
Zachary Turnerd37221d2014-07-09 16:31:49 +0000297 m_getopt_table[pos->second].definition->long_option,
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000298 opt_defs[i].long_option);
299 else
300 Host::SystemLog (Host::eSystemLogError, "option[%u] --%s has a short option 0x%x that conflicts with option[%u] --%s, short option won't be used for --%s\n",
301 i,
302 opt_defs[i].long_option,
303 short_opt,
304 pos->second,
Zachary Turnerd37221d2014-07-09 16:31:49 +0000305 m_getopt_table[pos->second].definition->long_option,
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000306 opt_defs[i].long_option);
Greg Clayton1c5f1862012-11-30 19:05:35 +0000307 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308 }
309
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000310 //getopt_long_only requires a NULL final entry in the table:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311
Zachary Turnerd37221d2014-07-09 16:31:49 +0000312 m_getopt_table[i].definition = nullptr;
313 m_getopt_table[i].flag = nullptr;
314 m_getopt_table[i].val = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315 }
316
Greg Clayton471b31c2010-07-20 22:52:08 +0000317 if (m_getopt_table.empty())
Ed Masted78c9572014-04-20 00:31:37 +0000318 return nullptr;
Greg Clayton471b31c2010-07-20 22:52:08 +0000319
320 return &m_getopt_table.front();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321}
322
323
324// This function takes INDENT, which tells how many spaces to output at the front of each line; SPACES, which is
325// a string containing 80 spaces; and TEXT, which is the text that is to be output. It outputs the text, on
326// multiple lines if necessary, to RESULT, with INDENT spaces at the front of each line. It breaks lines on spaces,
327// tabs or newlines, shortening the line if necessary to not break in the middle of a word. It assumes that each
328// output line should contain a maximum of OUTPUT_MAX_COLUMNS characters.
329
330
331void
332Options::OutputFormattedUsageText
333(
334 Stream &strm,
Zachary Turnerd37221d2014-07-09 16:31:49 +0000335 const OptionDefinition &option_def,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336 uint32_t output_max_columns
337)
338{
Zachary Turnerd37221d2014-07-09 16:31:49 +0000339 std::string actual_text;
340 if (option_def.validator)
341 {
342 const char *condition = option_def.validator->ShortConditionString();
343 if (condition)
344 {
345 actual_text = "[";
346 actual_text.append(condition);
347 actual_text.append("] ");
348 }
349 }
350 actual_text.append(option_def.usage_text);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351
352 // Will it all fit on one line?
353
Zachary Turnerd37221d2014-07-09 16:31:49 +0000354 if (static_cast<uint32_t>(actual_text.length() + strm.GetIndentLevel()) < output_max_columns)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355 {
356 // Output it as a single line.
Zachary Turnerd37221d2014-07-09 16:31:49 +0000357 strm.Indent (actual_text.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358 strm.EOL();
359 }
360 else
361 {
362 // We need to break it up into multiple lines.
363
364 int text_width = output_max_columns - strm.GetIndentLevel() - 1;
365 int start = 0;
366 int end = start;
Zachary Turnerd37221d2014-07-09 16:31:49 +0000367 int final_end = actual_text.length();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368 int sub_len;
369
370 while (end < final_end)
371 {
372 // Don't start the 'text' on a space, since we're already outputting the indentation.
Zachary Turnerd37221d2014-07-09 16:31:49 +0000373 while ((start < final_end) && (actual_text[start] == ' '))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374 start++;
375
376 end = start + text_width;
377 if (end > final_end)
378 end = final_end;
379 else
380 {
381 // If we're not at the end of the text, make sure we break the line on white space.
382 while (end > start
Zachary Turnerd37221d2014-07-09 16:31:49 +0000383 && actual_text[end] != ' ' && actual_text[end] != '\t' && actual_text[end] != '\n')
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 end--;
385 }
386
387 sub_len = end - start;
388 if (start != 0)
389 strm.EOL();
390 strm.Indent();
391 assert (start < final_end);
392 assert (start + sub_len <= final_end);
Zachary Turnerd37221d2014-07-09 16:31:49 +0000393 strm.Write(actual_text.c_str() + start, sub_len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394 start = end + 1;
395 }
396 strm.EOL();
397 }
398}
399
Greg Clayton52ec56c2011-10-29 00:57:28 +0000400bool
401Options::SupportsLongOption (const char *long_option)
402{
403 if (long_option && long_option[0])
404 {
405 const OptionDefinition *opt_defs = GetDefinitions ();
406 if (opt_defs)
407 {
Greg Clayton9d3d6882011-10-31 23:51:19 +0000408 const char *long_option_name = long_option;
Greg Clayton52ec56c2011-10-29 00:57:28 +0000409 if (long_option[0] == '-' && long_option[1] == '-')
410 long_option_name += 2;
Greg Clayton52ec56c2011-10-29 00:57:28 +0000411
412 for (uint32_t i = 0; opt_defs[i].long_option; ++i)
413 {
414 if (strcmp(opt_defs[i].long_option, long_option_name) == 0)
415 return true;
416 }
417 }
418 }
419 return false;
420}
421
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000422enum OptionDisplayType
423{
424 eDisplayBestOption,
425 eDisplayShortOption,
426 eDisplayLongOption
427};
428
429static bool
430PrintOption (const OptionDefinition &opt_def,
431 OptionDisplayType display_type,
432 const char *header,
433 const char *footer,
434 bool show_optional,
435 Stream &strm)
436{
Daniel Malea90b0c842012-12-05 20:24:57 +0000437 const bool has_short_option = isprint8(opt_def.short_option) != 0;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000438
439 if (display_type == eDisplayShortOption && !has_short_option)
440 return false;
441
442 if (header && header[0])
443 strm.PutCString(header);
444
445 if (show_optional && !opt_def.required)
446 strm.PutChar('[');
447 const bool show_short_option = has_short_option && display_type != eDisplayLongOption;
448 if (show_short_option)
449 strm.Printf ("-%c", opt_def.short_option);
450 else
451 strm.Printf ("--%s", opt_def.long_option);
452 switch (opt_def.option_has_arg)
453 {
Virgile Belloe2607b52013-09-05 16:42:23 +0000454 case OptionParser::eNoArgument:
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000455 break;
Virgile Belloe2607b52013-09-05 16:42:23 +0000456 case OptionParser::eRequiredArgument:
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000457 strm.Printf (" <%s>", CommandObject::GetArgumentName (opt_def.argument_type));
458 break;
459
Virgile Belloe2607b52013-09-05 16:42:23 +0000460 case OptionParser::eOptionalArgument:
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000461 strm.Printf ("%s[<%s>]",
462 show_short_option ? "" : "=",
463 CommandObject::GetArgumentName (opt_def.argument_type));
464 break;
465 }
466 if (show_optional && !opt_def.required)
467 strm.PutChar(']');
468 if (footer && footer[0])
469 strm.PutCString(footer);
470 return true;
471}
472
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473void
474Options::GenerateOptionUsage
475(
476 Stream &strm,
Greg Claytona7015092010-09-18 01:14:36 +0000477 CommandObject *cmd
478)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479{
Greg Claytoneb0103f2011-04-07 22:46:35 +0000480 const uint32_t screen_width = m_interpreter.GetDebugger().GetTerminalWidth();
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000481
Greg Clayton52ec56c2011-10-29 00:57:28 +0000482 const OptionDefinition *opt_defs = GetDefinitions();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483 const uint32_t save_indent_level = strm.GetIndentLevel();
484 const char *name;
485
Caroline Ticee139cf22010-10-01 17:46:38 +0000486 StreamString arguments_str;
487
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488 if (cmd)
Caroline Ticee139cf22010-10-01 17:46:38 +0000489 {
Greg Claytona7015092010-09-18 01:14:36 +0000490 name = cmd->GetCommandName();
Caroline Ticee139cf22010-10-01 17:46:38 +0000491 cmd->GetFormattedCommandArguments (arguments_str);
492 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493 else
Greg Claytona7015092010-09-18 01:14:36 +0000494 name = "";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495
496 strm.PutCString ("\nCommand Options Usage:\n");
497
498 strm.IndentMore(2);
499
500 // First, show each usage level set of options, e.g. <cmd> [options-for-level-0]
501 // <cmd> [options-for-level-1]
502 // etc.
503
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000504 const uint32_t num_options = NumCommandOptions();
Jim Ingham86511212010-06-15 18:47:14 +0000505 if (num_options == 0)
506 return;
507
Andy Gibbs70f94f92013-06-24 14:04:57 +0000508 uint32_t num_option_sets = GetRequiredOptions().size();
Jim Ingham86511212010-06-15 18:47:14 +0000509
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000510 uint32_t i;
Jim Ingham86511212010-06-15 18:47:14 +0000511
512 for (uint32_t opt_set = 0; opt_set < num_option_sets; ++opt_set)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513 {
Jim Ingham86511212010-06-15 18:47:14 +0000514 uint32_t opt_set_mask;
515
516 opt_set_mask = 1 << opt_set;
517 if (opt_set > 0)
518 strm.Printf ("\n");
519 strm.Indent (name);
Caroline Ticef362c452010-09-09 16:44:14 +0000520
Johnny Chen34ddc8d2012-02-08 01:13:31 +0000521 // Different option sets may require different args.
522 StreamString args_str;
Jim Ingham28eb5712012-10-12 17:34:26 +0000523 if (cmd)
524 cmd->GetFormattedCommandArguments(args_str, opt_set_mask);
Johnny Chen34ddc8d2012-02-08 01:13:31 +0000525
Greg Claytoned8a7052010-09-18 03:37:20 +0000526 // First go through and print all options that take no arguments as
527 // a single string. If a command has "-a" "-b" and "-c", this will show
528 // up as [-abc]
529
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000530 std::set<int> options;
531 std::set<int>::const_iterator options_pos, options_end;
Greg Clayton03da4cc2013-04-19 21:31:16 +0000532 for (i = 0; i < num_options; ++i)
Greg Claytoned8a7052010-09-18 03:37:20 +0000533 {
Daniel Malea90b0c842012-12-05 20:24:57 +0000534 if (opt_defs[i].usage_mask & opt_set_mask && isprint8(opt_defs[i].short_option))
Greg Claytoned8a7052010-09-18 03:37:20 +0000535 {
536 // Add current option to the end of out_stream.
537
Greg Clayton52ec56c2011-10-29 00:57:28 +0000538 if (opt_defs[i].required == true &&
Virgile Belloe2607b52013-09-05 16:42:23 +0000539 opt_defs[i].option_has_arg == OptionParser::eNoArgument)
Greg Claytoned8a7052010-09-18 03:37:20 +0000540 {
Greg Clayton52ec56c2011-10-29 00:57:28 +0000541 options.insert (opt_defs[i].short_option);
Greg Claytoned8a7052010-09-18 03:37:20 +0000542 }
543 }
544 }
545
546 if (options.empty() == false)
547 {
548 // We have some required options with no arguments
549 strm.PutCString(" -");
550 for (i=0; i<2; ++i)
551 for (options_pos = options.begin(), options_end = options.end();
552 options_pos != options_end;
553 ++options_pos)
554 {
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000555 if (i==0 && ::islower (*options_pos))
Greg Claytoned8a7052010-09-18 03:37:20 +0000556 continue;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000557 if (i==1 && ::isupper (*options_pos))
Greg Claytoned8a7052010-09-18 03:37:20 +0000558 continue;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000559 strm << (char)*options_pos;
Greg Claytoned8a7052010-09-18 03:37:20 +0000560 }
561 }
562
563 for (i = 0, options.clear(); i < num_options; ++i)
564 {
Daniel Malea90b0c842012-12-05 20:24:57 +0000565 if (opt_defs[i].usage_mask & opt_set_mask && isprint8(opt_defs[i].short_option))
Greg Claytoned8a7052010-09-18 03:37:20 +0000566 {
567 // Add current option to the end of out_stream.
568
Greg Clayton52ec56c2011-10-29 00:57:28 +0000569 if (opt_defs[i].required == false &&
Virgile Belloe2607b52013-09-05 16:42:23 +0000570 opt_defs[i].option_has_arg == OptionParser::eNoArgument)
Greg Claytoned8a7052010-09-18 03:37:20 +0000571 {
Greg Clayton52ec56c2011-10-29 00:57:28 +0000572 options.insert (opt_defs[i].short_option);
Greg Claytoned8a7052010-09-18 03:37:20 +0000573 }
574 }
575 }
576
577 if (options.empty() == false)
578 {
579 // We have some required options with no arguments
580 strm.PutCString(" [-");
581 for (i=0; i<2; ++i)
582 for (options_pos = options.begin(), options_end = options.end();
583 options_pos != options_end;
584 ++options_pos)
585 {
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000586 if (i==0 && ::islower (*options_pos))
Greg Claytoned8a7052010-09-18 03:37:20 +0000587 continue;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000588 if (i==1 && ::isupper (*options_pos))
Greg Claytoned8a7052010-09-18 03:37:20 +0000589 continue;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000590 strm << (char)*options_pos;
Greg Claytoned8a7052010-09-18 03:37:20 +0000591 }
592 strm.PutChar(']');
593 }
594
Caroline Ticef362c452010-09-09 16:44:14 +0000595 // First go through and print the required options (list them up front).
Jim Ingham86511212010-06-15 18:47:14 +0000596
597 for (i = 0; i < num_options; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598 {
Daniel Malea90b0c842012-12-05 20:24:57 +0000599 if (opt_defs[i].usage_mask & opt_set_mask && isprint8(opt_defs[i].short_option))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600 {
Virgile Belloe2607b52013-09-05 16:42:23 +0000601 if (opt_defs[i].required && opt_defs[i].option_has_arg != OptionParser::eNoArgument)
Ed Masted78c9572014-04-20 00:31:37 +0000602 PrintOption (opt_defs[i], eDisplayBestOption, " ", nullptr, true, strm);
Caroline Ticef362c452010-09-09 16:44:14 +0000603 }
604 }
605
606 // Now go through again, and this time only print the optional options.
607
608 for (i = 0; i < num_options; ++i)
609 {
Greg Clayton52ec56c2011-10-29 00:57:28 +0000610 if (opt_defs[i].usage_mask & opt_set_mask)
Caroline Ticef362c452010-09-09 16:44:14 +0000611 {
612 // Add current option to the end of out_stream.
613
Virgile Belloe2607b52013-09-05 16:42:23 +0000614 if (!opt_defs[i].required && opt_defs[i].option_has_arg != OptionParser::eNoArgument)
Ed Masted78c9572014-04-20 00:31:37 +0000615 PrintOption (opt_defs[i], eDisplayBestOption, " ", nullptr, true, strm);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617 }
Sean Callanana4c6ad12012-01-04 19:11:25 +0000618
Johnny Chen34ddc8d2012-02-08 01:13:31 +0000619 if (args_str.GetSize() > 0)
Sean Callanana4c6ad12012-01-04 19:11:25 +0000620 {
621 if (cmd->WantsRawCommandString())
622 strm.Printf(" --");
623
Johnny Chen34ddc8d2012-02-08 01:13:31 +0000624 strm.Printf (" %s", args_str.GetData());
Sean Callanana4c6ad12012-01-04 19:11:25 +0000625 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626 }
Sean Callanana4c6ad12012-01-04 19:11:25 +0000627
Jim Ingham28eb5712012-10-12 17:34:26 +0000628 if (cmd &&
629 cmd->WantsRawCommandString() &&
Sean Callanana4c6ad12012-01-04 19:11:25 +0000630 arguments_str.GetSize() > 0)
631 {
632 strm.PutChar('\n');
633 strm.Indent(name);
634 strm.Printf(" %s", arguments_str.GetData());
635 }
636
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637 strm.Printf ("\n\n");
638
639 // Now print out all the detailed information about the various options: long form, short form and help text:
Zachary Turnerd37221d2014-07-09 16:31:49 +0000640 // -short <argument> ( --long_name <argument> )
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641 // help text
642
643 // This variable is used to keep track of which options' info we've printed out, because some options can be in
644 // more than one usage level, but we only want to print the long form of its information once.
645
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000646 std::multimap<int, uint32_t> options_seen;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647 strm.IndentMore (5);
648
Caroline Ticef362c452010-09-09 16:44:14 +0000649 // Put the unique command options in a vector & sort it, so we can output them alphabetically (by short_option)
650 // when writing out detailed help for each option.
651
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652 for (i = 0; i < num_options; ++i)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000653 options_seen.insert(std::make_pair(opt_defs[i].short_option, i));
Caroline Ticef362c452010-09-09 16:44:14 +0000654
655 // Go through the unique'd and alphabetically sorted vector of options, find the table entry for each option
656 // and write out the detailed help information for that option.
657
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000658 bool first_option_printed = false;;
659
660 for (auto pos : options_seen)
Caroline Ticef362c452010-09-09 16:44:14 +0000661 {
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000662 i = pos.second;
663 //Print out the help information for this option.
664
665 // Put a newline separation between arguments
666 if (first_option_printed)
667 strm.EOL();
668 else
669 first_option_printed = true;
670
671 CommandArgumentType arg_type = opt_defs[i].argument_type;
672
673 StreamString arg_name_str;
674 arg_name_str.Printf ("<%s>", CommandObject::GetArgumentName (arg_type));
675
676 strm.Indent ();
Daniel Malea90b0c842012-12-05 20:24:57 +0000677 if (opt_defs[i].short_option && isprint8(opt_defs[i].short_option))
Caroline Ticef362c452010-09-09 16:44:14 +0000678 {
Ed Masted78c9572014-04-20 00:31:37 +0000679 PrintOption (opt_defs[i], eDisplayShortOption, nullptr, nullptr, false, strm);
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000680 PrintOption (opt_defs[i], eDisplayLongOption, " ( ", " )", false, strm);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000681 }
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000682 else
683 {
684 // Short option is not printable, just print long option
Ed Masted78c9572014-04-20 00:31:37 +0000685 PrintOption (opt_defs[i], eDisplayLongOption, nullptr, nullptr, false, strm);
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000686 }
687 strm.EOL();
688
689 strm.IndentMore (5);
690
691 if (opt_defs[i].usage_text)
692 OutputFormattedUsageText (strm,
Zachary Turnerd37221d2014-07-09 16:31:49 +0000693 opt_defs[i],
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000694 screen_width);
Ed Masted78c9572014-04-20 00:31:37 +0000695 if (opt_defs[i].enum_values != nullptr)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000696 {
697 strm.Indent ();
698 strm.Printf("Values: ");
Ed Masted78c9572014-04-20 00:31:37 +0000699 for (int k = 0; opt_defs[i].enum_values[k].string_value != nullptr; k++)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000700 {
701 if (k == 0)
702 strm.Printf("%s", opt_defs[i].enum_values[k].string_value);
703 else
704 strm.Printf(" | %s", opt_defs[i].enum_values[k].string_value);
705 }
706 strm.EOL();
707 }
708 strm.IndentLess (5);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709 }
710
711 // Restore the indent level
712 strm.SetIndentLevel (save_indent_level);
713}
714
715// This function is called when we have been given a potentially incomplete set of
716// options, such as when an alias has been defined (more options might be added at
717// at the time the alias is invoked). We need to verify that the options in the set
718// m_seen_options are all part of a set that may be used together, but m_seen_options
719// may be missing some of the "required" options.
720
721bool
722Options::VerifyPartialOptions (CommandReturnObject &result)
723{
724 bool options_are_valid = false;
725
Jim Inghamd6ccc602010-06-24 20:30:15 +0000726 int num_levels = GetRequiredOptions().size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727 if (num_levels)
728 {
729 for (int i = 0; i < num_levels && !options_are_valid; ++i)
730 {
731 // In this case we are treating all options as optional rather than required.
732 // Therefore a set of options is correct if m_seen_options is a subset of the
733 // union of m_required_options and m_optional_options.
734 OptionSet union_set;
Jim Inghamd6ccc602010-06-24 20:30:15 +0000735 OptionsSetUnion (GetRequiredOptions()[i], GetOptionalOptions()[i], union_set);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736 if (IsASubset (m_seen_options, union_set))
737 options_are_valid = true;
738 }
739 }
740
741 return options_are_valid;
742}
743
744bool
745Options::HandleOptionCompletion
746(
747 Args &input,
748 OptionElementVector &opt_element_vector,
749 int cursor_index,
750 int char_pos,
751 int match_start_point,
752 int max_return_elements,
Jim Ingham558ce122010-06-30 05:02:46 +0000753 bool &word_complete,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000754 lldb_private::StringList &matches
755)
756{
Jim Ingham558ce122010-06-30 05:02:46 +0000757 word_complete = true;
758
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759 // For now we just scan the completions to see if the cursor position is in
760 // an option or its argument. Otherwise we'll call HandleArgumentCompletion.
761 // In the future we can use completion to validate options as well if we want.
762
763 const OptionDefinition *opt_defs = GetDefinitions();
764
765 std::string cur_opt_std_str (input.GetArgumentAtIndex(cursor_index));
766 cur_opt_std_str.erase(char_pos);
767 const char *cur_opt_str = cur_opt_std_str.c_str();
768
Andy Gibbsa297a972013-06-19 19:04:53 +0000769 for (size_t i = 0; i < opt_element_vector.size(); i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000770 {
771 int opt_pos = opt_element_vector[i].opt_pos;
772 int opt_arg_pos = opt_element_vector[i].opt_arg_pos;
773 int opt_defs_index = opt_element_vector[i].opt_defs_index;
774 if (opt_pos == cursor_index)
775 {
776 // We're completing the option itself.
Jim Inghamd6ccc602010-06-24 20:30:15 +0000777
778 if (opt_defs_index == OptionArgElement::eBareDash)
779 {
780 // We're completing a bare dash. That means all options are open.
781 // FIXME: We should scan the other options provided and only complete options
782 // within the option group they belong to.
783 char opt_str[3] = {'-', 'a', '\0'};
784
Greg Claytonb1320972010-07-14 00:18:15 +0000785 for (int j = 0 ; opt_defs[j].short_option != 0 ; j++)
Jim Inghamd6ccc602010-06-24 20:30:15 +0000786 {
Greg Claytonb1320972010-07-14 00:18:15 +0000787 opt_str[1] = opt_defs[j].short_option;
Jim Inghamd6ccc602010-06-24 20:30:15 +0000788 matches.AppendString (opt_str);
789 }
790 return true;
791 }
792 else if (opt_defs_index == OptionArgElement::eBareDoubleDash)
793 {
794 std::string full_name ("--");
Greg Claytonb1320972010-07-14 00:18:15 +0000795 for (int j = 0 ; opt_defs[j].short_option != 0 ; j++)
Jim Inghamd6ccc602010-06-24 20:30:15 +0000796 {
797 full_name.erase(full_name.begin() + 2, full_name.end());
Greg Claytonb1320972010-07-14 00:18:15 +0000798 full_name.append (opt_defs[j].long_option);
Jim Inghamd6ccc602010-06-24 20:30:15 +0000799 matches.AppendString (full_name.c_str());
800 }
801 return true;
802 }
803 else if (opt_defs_index != OptionArgElement::eUnrecognizedArg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000804 {
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000805 // We recognized it, if it an incomplete long option, complete it anyway (getopt_long_only is
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000806 // happy with shortest unique string, but it's still a nice thing to do.) Otherwise return
807 // The string so the upper level code will know this is a full match and add the " ".
808 if (cur_opt_str && strlen (cur_opt_str) > 2
809 && cur_opt_str[0] == '-' && cur_opt_str[1] == '-'
810 && strcmp (opt_defs[opt_defs_index].long_option, cur_opt_str) != 0)
811 {
812 std::string full_name ("--");
813 full_name.append (opt_defs[opt_defs_index].long_option);
814 matches.AppendString(full_name.c_str());
815 return true;
816 }
817 else
818 {
819 matches.AppendString(input.GetArgumentAtIndex(cursor_index));
820 return true;
821 }
822 }
823 else
824 {
825 // FIXME - not handling wrong options yet:
826 // Check to see if they are writing a long option & complete it.
827 // I think we will only get in here if the long option table has two elements
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000828 // that are not unique up to this point. getopt_long_only does shortest unique match
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829 // for long options already.
830
831 if (cur_opt_str && strlen (cur_opt_str) > 2
832 && cur_opt_str[0] == '-' && cur_opt_str[1] == '-')
833 {
Greg Claytonb1320972010-07-14 00:18:15 +0000834 for (int j = 0 ; opt_defs[j].short_option != 0 ; j++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000835 {
Greg Claytonb1320972010-07-14 00:18:15 +0000836 if (strstr(opt_defs[j].long_option, cur_opt_str + 2) == opt_defs[j].long_option)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000837 {
838 std::string full_name ("--");
Greg Claytonb1320972010-07-14 00:18:15 +0000839 full_name.append (opt_defs[j].long_option);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000840 // The options definitions table has duplicates because of the
841 // way the grouping information is stored, so only add once.
842 bool duplicate = false;
Andy Gibbsa297a972013-06-19 19:04:53 +0000843 for (size_t k = 0; k < matches.GetSize(); k++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000844 {
Greg Claytonb1320972010-07-14 00:18:15 +0000845 if (matches.GetStringAtIndex(k) == full_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000846 {
847 duplicate = true;
848 break;
849 }
850 }
851 if (!duplicate)
852 matches.AppendString(full_name.c_str());
853 }
854 }
855 }
856 return true;
857 }
858
859
860 }
861 else if (opt_arg_pos == cursor_index)
862 {
863 // Okay the cursor is on the completion of an argument.
864 // See if it has a completion, otherwise return no matches.
865
866 if (opt_defs_index != -1)
867 {
Greg Claytoneb0103f2011-04-07 22:46:35 +0000868 HandleOptionArgumentCompletion (input,
Greg Clayton66111032010-06-23 01:19:29 +0000869 cursor_index,
870 strlen (input.GetArgumentAtIndex(cursor_index)),
871 opt_element_vector,
872 i,
873 match_start_point,
874 max_return_elements,
Jim Ingham558ce122010-06-30 05:02:46 +0000875 word_complete,
Greg Clayton66111032010-06-23 01:19:29 +0000876 matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000877 return true;
878 }
879 else
880 {
881 // No completion callback means no completions...
882 return true;
883 }
884
885 }
886 else
887 {
888 // Not the last element, keep going.
889 continue;
890 }
891 }
892 return false;
893}
894
895bool
896Options::HandleOptionArgumentCompletion
897(
898 Args &input,
899 int cursor_index,
900 int char_pos,
901 OptionElementVector &opt_element_vector,
902 int opt_element_index,
903 int match_start_point,
904 int max_return_elements,
Jim Ingham558ce122010-06-30 05:02:46 +0000905 bool &word_complete,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000906 lldb_private::StringList &matches
907)
908{
909 const OptionDefinition *opt_defs = GetDefinitions();
Greg Clayton7b0992d2013-04-18 22:45:39 +0000910 std::unique_ptr<SearchFilter> filter_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000911
912 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
913 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
914
915 // See if this is an enumeration type option, and if so complete it here:
916
917 OptionEnumValueElement *enum_values = opt_defs[opt_defs_index].enum_values;
Ed Masted78c9572014-04-20 00:31:37 +0000918 if (enum_values != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000919 {
920 bool return_value = false;
921 std::string match_string(input.GetArgumentAtIndex (opt_arg_pos), input.GetArgumentAtIndex (opt_arg_pos) + char_pos);
Ed Masted78c9572014-04-20 00:31:37 +0000922 for (int i = 0; enum_values[i].string_value != nullptr; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000923 {
924 if (strstr(enum_values[i].string_value, match_string.c_str()) == enum_values[i].string_value)
925 {
926 matches.AppendString (enum_values[i].string_value);
927 return_value = true;
928 }
929 }
930 return return_value;
931 }
932
933 // If this is a source file or symbol type completion, and there is a
934 // -shlib option somewhere in the supplied arguments, then make a search filter
935 // for that shared library.
936 // FIXME: Do we want to also have an "OptionType" so we don't have to match string names?
937
Greg Claytonab65b342011-04-13 22:47:15 +0000938 uint32_t completion_mask = opt_defs[opt_defs_index].completion_type;
939
940 if (completion_mask == 0)
941 {
942 lldb::CommandArgumentType option_arg_type = opt_defs[opt_defs_index].argument_type;
943 if (option_arg_type != eArgTypeNone)
944 {
Vince Harrond7e6a4f2015-05-13 00:25:54 +0000945 const CommandObject::ArgumentTableEntry *arg_entry = CommandObject::FindArgumentDataByType (opt_defs[opt_defs_index].argument_type);
Greg Claytonab65b342011-04-13 22:47:15 +0000946 if (arg_entry)
947 completion_mask = arg_entry->completion_type;
948 }
949 }
950
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000951 if (completion_mask & CommandCompletions::eSourceFileCompletion
952 || completion_mask & CommandCompletions::eSymbolCompletion)
953 {
Andy Gibbsa297a972013-06-19 19:04:53 +0000954 for (size_t i = 0; i < opt_element_vector.size(); i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000955 {
956 int cur_defs_index = opt_element_vector[i].opt_defs_index;
957 int cur_arg_pos = opt_element_vector[i].opt_arg_pos;
958 const char *cur_opt_name = opt_defs[cur_defs_index].long_option;
959
960 // If this is the "shlib" option and there was an argument provided,
961 // restrict it to that shared library.
Greg Clayton99dcbe12014-01-29 18:25:07 +0000962 if (cur_opt_name && strcmp(cur_opt_name, "shlib") == 0 && cur_arg_pos != -1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963 {
964 const char *module_name = input.GetArgumentAtIndex(cur_arg_pos);
965 if (module_name)
966 {
Greg Clayton274060b2010-10-20 20:54:39 +0000967 FileSpec module_spec(module_name, false);
Greg Claytoneb0103f2011-04-07 22:46:35 +0000968 lldb::TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000969 // Search filters require a target...
Greg Clayton4d122c42011-09-17 08:33:22 +0000970 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000971 filter_ap.reset (new SearchFilterByModule (target_sp, module_spec));
972 }
973 break;
974 }
975 }
976 }
977
Greg Claytoneb0103f2011-04-07 22:46:35 +0000978 return CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
Greg Clayton66111032010-06-23 01:19:29 +0000979 completion_mask,
980 input.GetArgumentAtIndex (opt_arg_pos),
981 match_start_point,
982 max_return_elements,
983 filter_ap.get(),
Jim Ingham558ce122010-06-30 05:02:46 +0000984 word_complete,
Greg Clayton66111032010-06-23 01:19:29 +0000985 matches);
986
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000987}
Greg Claytonf6b8b582011-04-13 00:18:08 +0000988
989
Greg Claytonf6b8b582011-04-13 00:18:08 +0000990void
Greg Clayton84c39662011-04-27 22:04:39 +0000991OptionGroupOptions::Append (OptionGroup* group)
992{
993 const OptionDefinition* group_option_defs = group->GetDefinitions ();
994 const uint32_t group_option_count = group->GetNumDefinitions();
995 for (uint32_t i=0; i<group_option_count; ++i)
996 {
997 m_option_infos.push_back (OptionInfo (group, i));
998 m_option_defs.push_back (group_option_defs[i]);
999 }
1000}
1001
Daniel Maleae0f8f572013-08-26 23:57:52 +00001002const OptionGroup*
1003OptionGroupOptions::GetGroupWithOption (char short_opt)
1004{
1005 for (uint32_t i = 0; i < m_option_defs.size(); i++)
1006 {
1007 OptionDefinition opt_def = m_option_defs[i];
1008 if (opt_def.short_option == short_opt)
1009 return m_option_infos[i].option_group;
1010 }
Ed Masted78c9572014-04-20 00:31:37 +00001011 return nullptr;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001012}
1013
Greg Clayton84c39662011-04-27 22:04:39 +00001014void
Greg Claytonab65b342011-04-13 22:47:15 +00001015OptionGroupOptions::Append (OptionGroup* group,
1016 uint32_t src_mask,
1017 uint32_t dst_mask)
Greg Claytonf6b8b582011-04-13 00:18:08 +00001018{
Greg Claytonf6b8b582011-04-13 00:18:08 +00001019 const OptionDefinition* group_option_defs = group->GetDefinitions ();
1020 const uint32_t group_option_count = group->GetNumDefinitions();
1021 for (uint32_t i=0; i<group_option_count; ++i)
1022 {
Greg Claytonab65b342011-04-13 22:47:15 +00001023 if (group_option_defs[i].usage_mask & src_mask)
1024 {
1025 m_option_infos.push_back (OptionInfo (group, i));
1026 m_option_defs.push_back (group_option_defs[i]);
1027 m_option_defs.back().usage_mask = dst_mask;
1028 }
Greg Claytonf6b8b582011-04-13 00:18:08 +00001029 }
1030}
1031
1032void
1033OptionGroupOptions::Finalize ()
1034{
1035 m_did_finalize = true;
Zachary Turnerd37221d2014-07-09 16:31:49 +00001036 OptionDefinition empty_option_def = { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr };
Greg Claytonf6b8b582011-04-13 00:18:08 +00001037 m_option_defs.push_back (empty_option_def);
1038}
1039
1040Error
1041OptionGroupOptions::SetOptionValue (uint32_t option_idx,
1042 const char *option_value)
1043{
1044 // After calling OptionGroupOptions::Append(...), you must finalize the groups
1045 // by calling OptionGroupOptions::Finlize()
1046 assert (m_did_finalize);
Greg Claytonab65b342011-04-13 22:47:15 +00001047 assert (m_option_infos.size() + 1 == m_option_defs.size());
Greg Claytonf6b8b582011-04-13 00:18:08 +00001048 Error error;
Greg Claytonab65b342011-04-13 22:47:15 +00001049 if (option_idx < m_option_infos.size())
1050 {
1051 error = m_option_infos[option_idx].option_group->SetOptionValue (m_interpreter,
1052 m_option_infos[option_idx].option_index,
1053 option_value);
1054
1055 }
1056 else
1057 {
1058 error.SetErrorString ("invalid option index"); // Shouldn't happen...
1059 }
Greg Claytonf6b8b582011-04-13 00:18:08 +00001060 return error;
1061}
1062
1063void
1064OptionGroupOptions::OptionParsingStarting ()
1065{
Greg Claytonab65b342011-04-13 22:47:15 +00001066 std::set<OptionGroup*> group_set;
1067 OptionInfos::iterator pos, end = m_option_infos.end();
1068 for (pos = m_option_infos.begin(); pos != end; ++pos)
1069 {
1070 OptionGroup* group = pos->option_group;
1071 if (group_set.find(group) == group_set.end())
1072 {
1073 group->OptionParsingStarting (m_interpreter);
1074 group_set.insert(group);
1075 }
1076 }
Greg Claytonf6b8b582011-04-13 00:18:08 +00001077}
1078Error
1079OptionGroupOptions::OptionParsingFinished ()
1080{
Greg Claytonab65b342011-04-13 22:47:15 +00001081 std::set<OptionGroup*> group_set;
Greg Claytonf6b8b582011-04-13 00:18:08 +00001082 Error error;
Greg Claytonab65b342011-04-13 22:47:15 +00001083 OptionInfos::iterator pos, end = m_option_infos.end();
1084 for (pos = m_option_infos.begin(); pos != end; ++pos)
Greg Claytonf6b8b582011-04-13 00:18:08 +00001085 {
Greg Claytonab65b342011-04-13 22:47:15 +00001086 OptionGroup* group = pos->option_group;
1087 if (group_set.find(group) == group_set.end())
1088 {
1089 error = group->OptionParsingFinished (m_interpreter);
1090 group_set.insert(group);
1091 if (error.Fail())
1092 return error;
1093 }
Greg Claytonf6b8b582011-04-13 00:18:08 +00001094 }
1095 return error;
1096}