blob: 09b17a8eb25324c0a28bc5d4e707d69bee32a8ff [file] [log] [blame]
Chris Lattner24943d22010-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 Ingham84cdc152010-06-15 19:49:27 +000010#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000011
12// C Includes
13// C++ Includes
Caroline Ticee5f18b02010-09-09 16:44:14 +000014#include <algorithm>
Greg Clayton5e342f52011-04-13 22:47:15 +000015#include <bitset>
16#include <set>
Chris Lattner24943d22010-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 Claytonf15996e2011-04-07 22:46:35 +000033Options::Options (CommandInterpreter &interpreter) :
34 m_interpreter (interpreter),
Chris Lattner24943d22010-06-08 16:52:24 +000035 m_getopt_table ()
36{
Jim Ingham34e9a982010-06-15 18:47:14 +000037 BuildValidOptionSets();
Chris Lattner24943d22010-06-08 16:52:24 +000038}
39
40Options::~Options ()
41{
42}
43
Chris Lattner24943d22010-06-08 16:52:24 +000044void
Greg Clayton143fcc32011-04-13 00:18:08 +000045Options::NotifyOptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +000046{
47 m_seen_options.clear();
Greg Clayton24bc5d92011-03-30 18:16:51 +000048 // Let the subclass reset its option values
Greg Clayton143fcc32011-04-13 00:18:08 +000049 OptionParsingStarting ();
50}
51
52Error
53Options::NotifyOptionParsingFinished ()
54{
55 return OptionParsingFinished ();
Chris Lattner24943d22010-06-08 16:52:24 +000056}
57
58void
59Options::OptionSeen (int option_idx)
60{
61 m_seen_options.insert ((char) option_idx);
62}
63
64// Returns true is set_a is a subset of set_b; Otherwise returns false.
65
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 Ingham8b9af1c2010-06-24 20:30:15 +0000134 int num_levels = GetRequiredOptions().size();
Chris Lattner24943d22010-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 Ingham8b9af1c2010-06-24 20:30:15 +0000145 if (IsASubset (GetRequiredOptions()[i], m_seen_options))
Chris Lattner24943d22010-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 Ingham8b9af1c2010-06-24 20:30:15 +0000149 OptionsSetDiff (m_seen_options, GetRequiredOptions()[i], remaining_options);
Chris Lattner24943d22010-06-08 16:52:24 +0000150 // Check to see if remaining_options is a subset of m_optional_options[i]
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000151 if (IsASubset (remaining_options, GetOptionalOptions()[i]))
Chris Lattner24943d22010-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 Ingham34e9a982010-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 Lattner24943d22010-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 Claytond8a218d2011-10-29 00:57:28 +0000189 const OptionDefinition *opt_defs = GetDefinitions();
Chris Lattner24943d22010-06-08 16:52:24 +0000190 m_required_options.resize(1);
191 m_optional_options.resize(1);
Jim Ingham34e9a982010-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 Lattner24943d22010-06-08 16:52:24 +0000198 {
Greg Claytond8a218d2011-10-29 00:57:28 +0000199 uint32_t this_usage_mask = opt_defs[i].usage_mask;
Jim Ingham34e9a982010-06-15 18:47:14 +0000200 if (this_usage_mask == LLDB_OPT_SET_ALL)
Chris Lattner24943d22010-06-08 16:52:24 +0000201 {
Jim Ingham34e9a982010-06-15 18:47:14 +0000202 if (num_option_sets == 0)
203 num_option_sets = 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000204 }
205 else
206 {
Jim Ingham34e9a982010-06-15 18:47:14 +0000207 for (int j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++)
208 {
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000209 if (this_usage_mask & (1 << j))
Jim Ingham34e9a982010-06-15 18:47:14 +0000210 {
211 if (num_option_sets <= j)
212 num_option_sets = j + 1;
213 }
214 }
Chris Lattner24943d22010-06-08 16:52:24 +0000215 }
Jim Ingham34e9a982010-06-15 18:47:14 +0000216 }
Chris Lattner24943d22010-06-08 16:52:24 +0000217
Jim Ingham34e9a982010-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 {
225 for (int j = 0; j < num_option_sets; j++)
226 {
Greg Claytond8a218d2011-10-29 00:57:28 +0000227 if (opt_defs[i].usage_mask & 1 << j)
Jim Ingham34e9a982010-06-15 18:47:14 +0000228 {
Greg Claytond8a218d2011-10-29 00:57:28 +0000229 if (opt_defs[i].required)
230 m_required_options[j].insert(opt_defs[i].short_option);
Jim Ingham34e9a982010-06-15 18:47:14 +0000231 else
Greg Claytond8a218d2011-10-29 00:57:28 +0000232 m_optional_options[j].insert(opt_defs[i].short_option);
Jim Ingham34e9a982010-06-15 18:47:14 +0000233 }
234 }
235 }
Chris Lattner24943d22010-06-08 16:52:24 +0000236 }
237}
238
239uint32_t
240Options::NumCommandOptions ()
241{
Greg Claytond8a218d2011-10-29 00:57:28 +0000242 const OptionDefinition *opt_defs = GetDefinitions ();
243 if (opt_defs == NULL)
Jim Ingham34e9a982010-06-15 18:47:14 +0000244 return 0;
245
Chris Lattner24943d22010-06-08 16:52:24 +0000246 int i = 0;
247
Greg Claytond8a218d2011-10-29 00:57:28 +0000248 if (opt_defs != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000249 {
Greg Claytond8a218d2011-10-29 00:57:28 +0000250 while (opt_defs[i].long_option != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000251 ++i;
252 }
253
254 return i;
255}
256
257struct option *
258Options::GetLongOptions ()
259{
260 // Check to see if this has already been done.
261 if (m_getopt_table.empty())
262 {
263 // Check to see if there are any options.
264 const uint32_t num_options = NumCommandOptions();
265 if (num_options == 0)
266 return NULL;
267
268 uint32_t i;
269 uint32_t j;
Greg Claytond8a218d2011-10-29 00:57:28 +0000270 const OptionDefinition *opt_defs = GetDefinitions();
Chris Lattner24943d22010-06-08 16:52:24 +0000271
272 std::bitset<256> option_seen;
273
274 m_getopt_table.resize(num_options + 1);
275 for (i = 0, j = 0; i < num_options; ++i)
276 {
Greg Claytond8a218d2011-10-29 00:57:28 +0000277 char short_opt = opt_defs[i].short_option;
Chris Lattner24943d22010-06-08 16:52:24 +0000278
279 if (option_seen.test(short_opt) == false)
280 {
Greg Claytond8a218d2011-10-29 00:57:28 +0000281 m_getopt_table[j].name = opt_defs[i].long_option;
282 m_getopt_table[j].has_arg = opt_defs[i].option_has_arg;
Chris Lattner24943d22010-06-08 16:52:24 +0000283 m_getopt_table[j].flag = NULL;
Greg Claytond8a218d2011-10-29 00:57:28 +0000284 m_getopt_table[j].val = opt_defs[i].short_option;
Chris Lattner24943d22010-06-08 16:52:24 +0000285 option_seen.set(short_opt);
286 ++j;
287 }
288 }
289
290 //getopt_long requires a NULL final entry in the table:
291
292 m_getopt_table[j].name = NULL;
293 m_getopt_table[j].has_arg = 0;
294 m_getopt_table[j].flag = NULL;
295 m_getopt_table[j].val = 0;
296 }
297
Greg Clayton53d68e72010-07-20 22:52:08 +0000298 if (m_getopt_table.empty())
299 return NULL;
300
301 return &m_getopt_table.front();
Chris Lattner24943d22010-06-08 16:52:24 +0000302}
303
304
305// This function takes INDENT, which tells how many spaces to output at the front of each line; SPACES, which is
306// a string containing 80 spaces; and TEXT, which is the text that is to be output. It outputs the text, on
307// multiple lines if necessary, to RESULT, with INDENT spaces at the front of each line. It breaks lines on spaces,
308// tabs or newlines, shortening the line if necessary to not break in the middle of a word. It assumes that each
309// output line should contain a maximum of OUTPUT_MAX_COLUMNS characters.
310
311
312void
313Options::OutputFormattedUsageText
314(
315 Stream &strm,
316 const char *text,
317 uint32_t output_max_columns
318)
319{
320 int len = strlen (text);
321
322 // Will it all fit on one line?
323
324 if ((len + strm.GetIndentLevel()) < output_max_columns)
325 {
326 // Output it as a single line.
327 strm.Indent (text);
328 strm.EOL();
329 }
330 else
331 {
332 // We need to break it up into multiple lines.
333
334 int text_width = output_max_columns - strm.GetIndentLevel() - 1;
335 int start = 0;
336 int end = start;
337 int final_end = strlen (text);
338 int sub_len;
339
340 while (end < final_end)
341 {
342 // Don't start the 'text' on a space, since we're already outputting the indentation.
343 while ((start < final_end) && (text[start] == ' '))
344 start++;
345
346 end = start + text_width;
347 if (end > final_end)
348 end = final_end;
349 else
350 {
351 // If we're not at the end of the text, make sure we break the line on white space.
352 while (end > start
353 && text[end] != ' ' && text[end] != '\t' && text[end] != '\n')
354 end--;
355 }
356
357 sub_len = end - start;
358 if (start != 0)
359 strm.EOL();
360 strm.Indent();
361 assert (start < final_end);
362 assert (start + sub_len <= final_end);
363 strm.Write(text + start, sub_len);
364 start = end + 1;
365 }
366 strm.EOL();
367 }
368}
369
Greg Claytond8a218d2011-10-29 00:57:28 +0000370bool
371Options::SupportsLongOption (const char *long_option)
372{
373 if (long_option && long_option[0])
374 {
375 const OptionDefinition *opt_defs = GetDefinitions ();
376 if (opt_defs)
377 {
Greg Claytonb5169392011-10-31 23:51:19 +0000378 const char *long_option_name = long_option;
Greg Claytond8a218d2011-10-29 00:57:28 +0000379 if (long_option[0] == '-' && long_option[1] == '-')
380 long_option_name += 2;
Greg Claytond8a218d2011-10-29 00:57:28 +0000381
382 for (uint32_t i = 0; opt_defs[i].long_option; ++i)
383 {
384 if (strcmp(opt_defs[i].long_option, long_option_name) == 0)
385 return true;
386 }
387 }
388 }
389 return false;
390}
391
Chris Lattner24943d22010-06-08 16:52:24 +0000392void
393Options::GenerateOptionUsage
394(
395 Stream &strm,
Greg Clayton238c0a12010-09-18 01:14:36 +0000396 CommandObject *cmd
397)
Chris Lattner24943d22010-06-08 16:52:24 +0000398{
Greg Claytonf15996e2011-04-07 22:46:35 +0000399 const uint32_t screen_width = m_interpreter.GetDebugger().GetTerminalWidth();
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000400
Greg Claytond8a218d2011-10-29 00:57:28 +0000401 const OptionDefinition *opt_defs = GetDefinitions();
Chris Lattner24943d22010-06-08 16:52:24 +0000402 const uint32_t save_indent_level = strm.GetIndentLevel();
403 const char *name;
404
Caroline Ticefb355112010-10-01 17:46:38 +0000405 StreamString arguments_str;
406
Chris Lattner24943d22010-06-08 16:52:24 +0000407 if (cmd)
Caroline Ticefb355112010-10-01 17:46:38 +0000408 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000409 name = cmd->GetCommandName();
Caroline Ticefb355112010-10-01 17:46:38 +0000410 cmd->GetFormattedCommandArguments (arguments_str);
411 }
Chris Lattner24943d22010-06-08 16:52:24 +0000412 else
Greg Clayton238c0a12010-09-18 01:14:36 +0000413 name = "";
Chris Lattner24943d22010-06-08 16:52:24 +0000414
415 strm.PutCString ("\nCommand Options Usage:\n");
416
417 strm.IndentMore(2);
418
419 // First, show each usage level set of options, e.g. <cmd> [options-for-level-0]
420 // <cmd> [options-for-level-1]
421 // etc.
422
Chris Lattner24943d22010-06-08 16:52:24 +0000423 const uint32_t num_options = NumCommandOptions();
Jim Ingham34e9a982010-06-15 18:47:14 +0000424 if (num_options == 0)
425 return;
426
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000427 int num_option_sets = GetRequiredOptions().size();
Jim Ingham34e9a982010-06-15 18:47:14 +0000428
Chris Lattner24943d22010-06-08 16:52:24 +0000429 uint32_t i;
Jim Ingham34e9a982010-06-15 18:47:14 +0000430
431 for (uint32_t opt_set = 0; opt_set < num_option_sets; ++opt_set)
Chris Lattner24943d22010-06-08 16:52:24 +0000432 {
Jim Ingham34e9a982010-06-15 18:47:14 +0000433 uint32_t opt_set_mask;
434
435 opt_set_mask = 1 << opt_set;
436 if (opt_set > 0)
437 strm.Printf ("\n");
438 strm.Indent (name);
Caroline Ticee5f18b02010-09-09 16:44:14 +0000439
Greg Claytonfe424a92010-09-18 03:37:20 +0000440 // First go through and print all options that take no arguments as
441 // a single string. If a command has "-a" "-b" and "-c", this will show
442 // up as [-abc]
443
444 std::set<char> options;
445 std::set<char>::const_iterator options_pos, options_end;
446 bool first;
447 for (i = 0, first = true; i < num_options; ++i)
448 {
Greg Claytond8a218d2011-10-29 00:57:28 +0000449 if (opt_defs[i].usage_mask & opt_set_mask)
Greg Claytonfe424a92010-09-18 03:37:20 +0000450 {
451 // Add current option to the end of out_stream.
452
Greg Claytond8a218d2011-10-29 00:57:28 +0000453 if (opt_defs[i].required == true &&
454 opt_defs[i].option_has_arg == no_argument)
Greg Claytonfe424a92010-09-18 03:37:20 +0000455 {
Greg Claytond8a218d2011-10-29 00:57:28 +0000456 options.insert (opt_defs[i].short_option);
Greg Claytonfe424a92010-09-18 03:37:20 +0000457 }
458 }
459 }
460
461 if (options.empty() == false)
462 {
463 // We have some required options with no arguments
464 strm.PutCString(" -");
465 for (i=0; i<2; ++i)
466 for (options_pos = options.begin(), options_end = options.end();
467 options_pos != options_end;
468 ++options_pos)
469 {
470 if (i==0 && ::isupper (*options_pos))
471 continue;
472 if (i==1 && ::islower (*options_pos))
473 continue;
474 strm << *options_pos;
475 }
476 }
477
478 for (i = 0, options.clear(); i < num_options; ++i)
479 {
Greg Claytond8a218d2011-10-29 00:57:28 +0000480 if (opt_defs[i].usage_mask & opt_set_mask)
Greg Claytonfe424a92010-09-18 03:37:20 +0000481 {
482 // Add current option to the end of out_stream.
483
Greg Claytond8a218d2011-10-29 00:57:28 +0000484 if (opt_defs[i].required == false &&
485 opt_defs[i].option_has_arg == no_argument)
Greg Claytonfe424a92010-09-18 03:37:20 +0000486 {
Greg Claytond8a218d2011-10-29 00:57:28 +0000487 options.insert (opt_defs[i].short_option);
Greg Claytonfe424a92010-09-18 03:37:20 +0000488 }
489 }
490 }
491
492 if (options.empty() == false)
493 {
494 // We have some required options with no arguments
495 strm.PutCString(" [-");
496 for (i=0; i<2; ++i)
497 for (options_pos = options.begin(), options_end = options.end();
498 options_pos != options_end;
499 ++options_pos)
500 {
501 if (i==0 && ::isupper (*options_pos))
502 continue;
503 if (i==1 && ::islower (*options_pos))
504 continue;
505 strm << *options_pos;
506 }
507 strm.PutChar(']');
508 }
509
Caroline Ticee5f18b02010-09-09 16:44:14 +0000510 // First go through and print the required options (list them up front).
Jim Ingham34e9a982010-06-15 18:47:14 +0000511
512 for (i = 0; i < num_options; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000513 {
Greg Claytond8a218d2011-10-29 00:57:28 +0000514 if (opt_defs[i].usage_mask & opt_set_mask)
Chris Lattner24943d22010-06-08 16:52:24 +0000515 {
Jim Ingham34e9a982010-06-15 18:47:14 +0000516 // Add current option to the end of out_stream.
Greg Claytond8a218d2011-10-29 00:57:28 +0000517 CommandArgumentType arg_type = opt_defs[i].argument_type;
Caroline Tice4d6675c2010-10-01 19:59:14 +0000518
Greg Claytond8a218d2011-10-29 00:57:28 +0000519 if (opt_defs[i].required)
Jim Ingham34e9a982010-06-15 18:47:14 +0000520 {
Greg Claytond8a218d2011-10-29 00:57:28 +0000521 if (opt_defs[i].option_has_arg == required_argument)
Jim Ingham34e9a982010-06-15 18:47:14 +0000522 {
Caroline Tice4d6675c2010-10-01 19:59:14 +0000523 strm.Printf (" -%c <%s>",
Greg Claytond8a218d2011-10-29 00:57:28 +0000524 opt_defs[i].short_option,
Caroline Tice4d6675c2010-10-01 19:59:14 +0000525 CommandObject::GetArgumentName (arg_type));
Jim Ingham34e9a982010-06-15 18:47:14 +0000526 }
Greg Claytond8a218d2011-10-29 00:57:28 +0000527 else if (opt_defs[i].option_has_arg == optional_argument)
Jim Ingham34e9a982010-06-15 18:47:14 +0000528 {
Caroline Tice4d6675c2010-10-01 19:59:14 +0000529 strm.Printf (" -%c [<%s>]",
Greg Claytond8a218d2011-10-29 00:57:28 +0000530 opt_defs[i].short_option,
Caroline Tice4d6675c2010-10-01 19:59:14 +0000531 CommandObject::GetArgumentName (arg_type));
Jim Ingham34e9a982010-06-15 18:47:14 +0000532 }
Jim Ingham34e9a982010-06-15 18:47:14 +0000533 }
Caroline Ticee5f18b02010-09-09 16:44:14 +0000534 }
535 }
536
537 // Now go through again, and this time only print the optional options.
538
539 for (i = 0; i < num_options; ++i)
540 {
Greg Claytond8a218d2011-10-29 00:57:28 +0000541 if (opt_defs[i].usage_mask & opt_set_mask)
Caroline Ticee5f18b02010-09-09 16:44:14 +0000542 {
543 // Add current option to the end of out_stream.
544
Greg Claytond8a218d2011-10-29 00:57:28 +0000545 CommandArgumentType arg_type = opt_defs[i].argument_type;
Caroline Tice4d6675c2010-10-01 19:59:14 +0000546
Greg Claytond8a218d2011-10-29 00:57:28 +0000547 if (! opt_defs[i].required)
Jim Ingham34e9a982010-06-15 18:47:14 +0000548 {
Greg Claytond8a218d2011-10-29 00:57:28 +0000549 if (opt_defs[i].option_has_arg == required_argument)
550 strm.Printf (" [-%c <%s>]", opt_defs[i].short_option,
Caroline Tice4d6675c2010-10-01 19:59:14 +0000551 CommandObject::GetArgumentName (arg_type));
Greg Claytond8a218d2011-10-29 00:57:28 +0000552 else if (opt_defs[i].option_has_arg == optional_argument)
553 strm.Printf (" [-%c [<%s>]]", opt_defs[i].short_option,
Caroline Tice4d6675c2010-10-01 19:59:14 +0000554 CommandObject::GetArgumentName (arg_type));
Jim Ingham34e9a982010-06-15 18:47:14 +0000555 }
Chris Lattner24943d22010-06-08 16:52:24 +0000556 }
Chris Lattner24943d22010-06-08 16:52:24 +0000557 }
Sean Callanan9798d7b2012-01-04 19:11:25 +0000558
Caroline Ticefb355112010-10-01 17:46:38 +0000559 if (arguments_str.GetSize() > 0)
Sean Callanan9798d7b2012-01-04 19:11:25 +0000560 {
561 if (cmd->WantsRawCommandString())
562 strm.Printf(" --");
563
Caroline Ticefb355112010-10-01 17:46:38 +0000564 strm.Printf (" %s", arguments_str.GetData());
Sean Callanan9798d7b2012-01-04 19:11:25 +0000565 }
Chris Lattner24943d22010-06-08 16:52:24 +0000566 }
Sean Callanan9798d7b2012-01-04 19:11:25 +0000567
568 if (cmd->WantsRawCommandString() &&
569 arguments_str.GetSize() > 0)
570 {
571 strm.PutChar('\n');
572 strm.Indent(name);
573 strm.Printf(" %s", arguments_str.GetData());
574 }
575
Chris Lattner24943d22010-06-08 16:52:24 +0000576 strm.Printf ("\n\n");
577
578 // Now print out all the detailed information about the various options: long form, short form and help text:
Greg Claytonfe424a92010-09-18 03:37:20 +0000579 // --long_name <argument> ( -short <argument> )
Chris Lattner24943d22010-06-08 16:52:24 +0000580 // help text
581
582 // This variable is used to keep track of which options' info we've printed out, because some options can be in
583 // more than one usage level, but we only want to print the long form of its information once.
584
585 OptionSet options_seen;
586 OptionSet::iterator pos;
587 strm.IndentMore (5);
588
Caroline Ticee5f18b02010-09-09 16:44:14 +0000589 std::vector<char> sorted_options;
590
591
592 // Put the unique command options in a vector & sort it, so we can output them alphabetically (by short_option)
593 // when writing out detailed help for each option.
594
Chris Lattner24943d22010-06-08 16:52:24 +0000595 for (i = 0; i < num_options; ++i)
596 {
Greg Claytond8a218d2011-10-29 00:57:28 +0000597 pos = options_seen.find (opt_defs[i].short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000598 if (pos == options_seen.end())
599 {
Greg Claytond8a218d2011-10-29 00:57:28 +0000600 options_seen.insert (opt_defs[i].short_option);
601 sorted_options.push_back (opt_defs[i].short_option);
Caroline Ticee5f18b02010-09-09 16:44:14 +0000602 }
603 }
Chris Lattner24943d22010-06-08 16:52:24 +0000604
Caroline Ticee5f18b02010-09-09 16:44:14 +0000605 std::sort (sorted_options.begin(), sorted_options.end());
606
607 // Go through the unique'd and alphabetically sorted vector of options, find the table entry for each option
608 // and write out the detailed help information for that option.
609
610 int first_option_printed = 1;
611 size_t end = sorted_options.size();
612 for (size_t j = 0; j < end; ++j)
613 {
614 char option = sorted_options[j];
615 bool found = false;
616 for (i = 0; i < num_options && !found; ++i)
617 {
Greg Claytond8a218d2011-10-29 00:57:28 +0000618 if (opt_defs[i].short_option == option)
Caroline Ticee5f18b02010-09-09 16:44:14 +0000619 {
620 found = true;
621 //Print out the help information for this option.
622
623 // Put a newline separation between arguments
624 if (first_option_printed)
625 first_option_printed = 0;
626 else
627 strm.EOL();
628
Greg Claytond8a218d2011-10-29 00:57:28 +0000629 CommandArgumentType arg_type = opt_defs[i].argument_type;
Caroline Tice4d6675c2010-10-01 19:59:14 +0000630
631 StreamString arg_name_str;
632 arg_name_str.Printf ("<%s>", CommandObject::GetArgumentName (arg_type));
633
Caroline Ticee5f18b02010-09-09 16:44:14 +0000634 strm.Indent ();
Greg Claytond8a218d2011-10-29 00:57:28 +0000635 strm.Printf ("-%c", opt_defs[i].short_option);
Caroline Tice4d6675c2010-10-01 19:59:14 +0000636 if (arg_type != eArgTypeNone)
637 strm.Printf (" <%s>", CommandObject::GetArgumentName (arg_type));
Greg Claytond8a218d2011-10-29 00:57:28 +0000638 strm.Printf (" ( --%s", opt_defs[i].long_option);
Caroline Tice4d6675c2010-10-01 19:59:14 +0000639 if (arg_type != eArgTypeNone)
640 strm.Printf (" <%s>", CommandObject::GetArgumentName (arg_type));
Greg Claytonfe424a92010-09-18 03:37:20 +0000641 strm.PutCString(" )\n");
Caroline Ticee5f18b02010-09-09 16:44:14 +0000642
643 strm.IndentMore (5);
644
Greg Claytond8a218d2011-10-29 00:57:28 +0000645 if (opt_defs[i].usage_text)
Chris Lattner24943d22010-06-08 16:52:24 +0000646 OutputFormattedUsageText (strm,
Greg Claytond8a218d2011-10-29 00:57:28 +0000647 opt_defs[i].usage_text,
Chris Lattner24943d22010-06-08 16:52:24 +0000648 screen_width);
Greg Claytond8a218d2011-10-29 00:57:28 +0000649 if (opt_defs[i].enum_values != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000650 {
Caroline Ticee5f18b02010-09-09 16:44:14 +0000651 strm.Indent ();
652 strm.Printf("Values: ");
Greg Claytond8a218d2011-10-29 00:57:28 +0000653 for (int k = 0; opt_defs[i].enum_values[k].string_value != NULL; k++)
Caroline Ticee5f18b02010-09-09 16:44:14 +0000654 {
655 if (k == 0)
Greg Claytond8a218d2011-10-29 00:57:28 +0000656 strm.Printf("%s", opt_defs[i].enum_values[k].string_value);
Caroline Ticee5f18b02010-09-09 16:44:14 +0000657 else
Greg Claytond8a218d2011-10-29 00:57:28 +0000658 strm.Printf(" | %s", opt_defs[i].enum_values[k].string_value);
Caroline Ticee5f18b02010-09-09 16:44:14 +0000659 }
660 strm.EOL();
Chris Lattner24943d22010-06-08 16:52:24 +0000661 }
Caroline Ticee5f18b02010-09-09 16:44:14 +0000662 strm.IndentLess (5);
Chris Lattner24943d22010-06-08 16:52:24 +0000663 }
Chris Lattner24943d22010-06-08 16:52:24 +0000664 }
665 }
666
667 // Restore the indent level
668 strm.SetIndentLevel (save_indent_level);
669}
670
671// This function is called when we have been given a potentially incomplete set of
672// options, such as when an alias has been defined (more options might be added at
673// at the time the alias is invoked). We need to verify that the options in the set
674// m_seen_options are all part of a set that may be used together, but m_seen_options
675// may be missing some of the "required" options.
676
677bool
678Options::VerifyPartialOptions (CommandReturnObject &result)
679{
680 bool options_are_valid = false;
681
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000682 int num_levels = GetRequiredOptions().size();
Chris Lattner24943d22010-06-08 16:52:24 +0000683 if (num_levels)
684 {
685 for (int i = 0; i < num_levels && !options_are_valid; ++i)
686 {
687 // In this case we are treating all options as optional rather than required.
688 // Therefore a set of options is correct if m_seen_options is a subset of the
689 // union of m_required_options and m_optional_options.
690 OptionSet union_set;
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000691 OptionsSetUnion (GetRequiredOptions()[i], GetOptionalOptions()[i], union_set);
Chris Lattner24943d22010-06-08 16:52:24 +0000692 if (IsASubset (m_seen_options, union_set))
693 options_are_valid = true;
694 }
695 }
696
697 return options_are_valid;
698}
699
700bool
701Options::HandleOptionCompletion
702(
703 Args &input,
704 OptionElementVector &opt_element_vector,
705 int cursor_index,
706 int char_pos,
707 int match_start_point,
708 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000709 bool &word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000710 lldb_private::StringList &matches
711)
712{
Jim Ingham802f8b02010-06-30 05:02:46 +0000713 word_complete = true;
714
Chris Lattner24943d22010-06-08 16:52:24 +0000715 // For now we just scan the completions to see if the cursor position is in
716 // an option or its argument. Otherwise we'll call HandleArgumentCompletion.
717 // In the future we can use completion to validate options as well if we want.
718
719 const OptionDefinition *opt_defs = GetDefinitions();
720
721 std::string cur_opt_std_str (input.GetArgumentAtIndex(cursor_index));
722 cur_opt_std_str.erase(char_pos);
723 const char *cur_opt_str = cur_opt_std_str.c_str();
724
725 for (int i = 0; i < opt_element_vector.size(); i++)
726 {
727 int opt_pos = opt_element_vector[i].opt_pos;
728 int opt_arg_pos = opt_element_vector[i].opt_arg_pos;
729 int opt_defs_index = opt_element_vector[i].opt_defs_index;
730 if (opt_pos == cursor_index)
731 {
732 // We're completing the option itself.
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000733
734 if (opt_defs_index == OptionArgElement::eBareDash)
735 {
736 // We're completing a bare dash. That means all options are open.
737 // FIXME: We should scan the other options provided and only complete options
738 // within the option group they belong to.
739 char opt_str[3] = {'-', 'a', '\0'};
740
Greg Claytonbef15832010-07-14 00:18:15 +0000741 for (int j = 0 ; opt_defs[j].short_option != 0 ; j++)
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000742 {
Greg Claytonbef15832010-07-14 00:18:15 +0000743 opt_str[1] = opt_defs[j].short_option;
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000744 matches.AppendString (opt_str);
745 }
746 return true;
747 }
748 else if (opt_defs_index == OptionArgElement::eBareDoubleDash)
749 {
750 std::string full_name ("--");
Greg Claytonbef15832010-07-14 00:18:15 +0000751 for (int j = 0 ; opt_defs[j].short_option != 0 ; j++)
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000752 {
753 full_name.erase(full_name.begin() + 2, full_name.end());
Greg Claytonbef15832010-07-14 00:18:15 +0000754 full_name.append (opt_defs[j].long_option);
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000755 matches.AppendString (full_name.c_str());
756 }
757 return true;
758 }
759 else if (opt_defs_index != OptionArgElement::eUnrecognizedArg)
Chris Lattner24943d22010-06-08 16:52:24 +0000760 {
761 // We recognized it, if it an incomplete long option, complete it anyway (getopt_long is
762 // happy with shortest unique string, but it's still a nice thing to do.) Otherwise return
763 // The string so the upper level code will know this is a full match and add the " ".
764 if (cur_opt_str && strlen (cur_opt_str) > 2
765 && cur_opt_str[0] == '-' && cur_opt_str[1] == '-'
766 && strcmp (opt_defs[opt_defs_index].long_option, cur_opt_str) != 0)
767 {
768 std::string full_name ("--");
769 full_name.append (opt_defs[opt_defs_index].long_option);
770 matches.AppendString(full_name.c_str());
771 return true;
772 }
773 else
774 {
775 matches.AppendString(input.GetArgumentAtIndex(cursor_index));
776 return true;
777 }
778 }
779 else
780 {
781 // FIXME - not handling wrong options yet:
782 // Check to see if they are writing a long option & complete it.
783 // I think we will only get in here if the long option table has two elements
784 // that are not unique up to this point. getopt_long does shortest unique match
785 // for long options already.
786
787 if (cur_opt_str && strlen (cur_opt_str) > 2
788 && cur_opt_str[0] == '-' && cur_opt_str[1] == '-')
789 {
Greg Claytonbef15832010-07-14 00:18:15 +0000790 for (int j = 0 ; opt_defs[j].short_option != 0 ; j++)
Chris Lattner24943d22010-06-08 16:52:24 +0000791 {
Greg Claytonbef15832010-07-14 00:18:15 +0000792 if (strstr(opt_defs[j].long_option, cur_opt_str + 2) == opt_defs[j].long_option)
Chris Lattner24943d22010-06-08 16:52:24 +0000793 {
794 std::string full_name ("--");
Greg Claytonbef15832010-07-14 00:18:15 +0000795 full_name.append (opt_defs[j].long_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000796 // The options definitions table has duplicates because of the
797 // way the grouping information is stored, so only add once.
798 bool duplicate = false;
Greg Claytonbef15832010-07-14 00:18:15 +0000799 for (int k = 0; k < matches.GetSize(); k++)
Chris Lattner24943d22010-06-08 16:52:24 +0000800 {
Greg Claytonbef15832010-07-14 00:18:15 +0000801 if (matches.GetStringAtIndex(k) == full_name)
Chris Lattner24943d22010-06-08 16:52:24 +0000802 {
803 duplicate = true;
804 break;
805 }
806 }
807 if (!duplicate)
808 matches.AppendString(full_name.c_str());
809 }
810 }
811 }
812 return true;
813 }
814
815
816 }
817 else if (opt_arg_pos == cursor_index)
818 {
819 // Okay the cursor is on the completion of an argument.
820 // See if it has a completion, otherwise return no matches.
821
822 if (opt_defs_index != -1)
823 {
Greg Claytonf15996e2011-04-07 22:46:35 +0000824 HandleOptionArgumentCompletion (input,
Greg Clayton63094e02010-06-23 01:19:29 +0000825 cursor_index,
826 strlen (input.GetArgumentAtIndex(cursor_index)),
827 opt_element_vector,
828 i,
829 match_start_point,
830 max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000831 word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000832 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000833 return true;
834 }
835 else
836 {
837 // No completion callback means no completions...
838 return true;
839 }
840
841 }
842 else
843 {
844 // Not the last element, keep going.
845 continue;
846 }
847 }
848 return false;
849}
850
851bool
852Options::HandleOptionArgumentCompletion
853(
854 Args &input,
855 int cursor_index,
856 int char_pos,
857 OptionElementVector &opt_element_vector,
858 int opt_element_index,
859 int match_start_point,
860 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000861 bool &word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000862 lldb_private::StringList &matches
863)
864{
865 const OptionDefinition *opt_defs = GetDefinitions();
866 std::auto_ptr<SearchFilter> filter_ap;
867
868 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
869 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
870
871 // See if this is an enumeration type option, and if so complete it here:
872
873 OptionEnumValueElement *enum_values = opt_defs[opt_defs_index].enum_values;
874 if (enum_values != NULL)
875 {
876 bool return_value = false;
877 std::string match_string(input.GetArgumentAtIndex (opt_arg_pos), input.GetArgumentAtIndex (opt_arg_pos) + char_pos);
878 for (int i = 0; enum_values[i].string_value != NULL; i++)
879 {
880 if (strstr(enum_values[i].string_value, match_string.c_str()) == enum_values[i].string_value)
881 {
882 matches.AppendString (enum_values[i].string_value);
883 return_value = true;
884 }
885 }
886 return return_value;
887 }
888
889 // If this is a source file or symbol type completion, and there is a
890 // -shlib option somewhere in the supplied arguments, then make a search filter
891 // for that shared library.
892 // FIXME: Do we want to also have an "OptionType" so we don't have to match string names?
893
Greg Clayton5e342f52011-04-13 22:47:15 +0000894 uint32_t completion_mask = opt_defs[opt_defs_index].completion_type;
895
896 if (completion_mask == 0)
897 {
898 lldb::CommandArgumentType option_arg_type = opt_defs[opt_defs_index].argument_type;
899 if (option_arg_type != eArgTypeNone)
900 {
901 CommandObject::ArgumentTableEntry *arg_entry = CommandObject::FindArgumentDataByType (opt_defs[opt_defs_index].argument_type);
902 if (arg_entry)
903 completion_mask = arg_entry->completion_type;
904 }
905 }
906
Chris Lattner24943d22010-06-08 16:52:24 +0000907 if (completion_mask & CommandCompletions::eSourceFileCompletion
908 || completion_mask & CommandCompletions::eSymbolCompletion)
909 {
910 for (int i = 0; i < opt_element_vector.size(); i++)
911 {
912 int cur_defs_index = opt_element_vector[i].opt_defs_index;
913 int cur_arg_pos = opt_element_vector[i].opt_arg_pos;
914 const char *cur_opt_name = opt_defs[cur_defs_index].long_option;
915
916 // If this is the "shlib" option and there was an argument provided,
917 // restrict it to that shared library.
918 if (strcmp(cur_opt_name, "shlib") == 0 && cur_arg_pos != -1)
919 {
920 const char *module_name = input.GetArgumentAtIndex(cur_arg_pos);
921 if (module_name)
922 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000923 FileSpec module_spec(module_name, false);
Greg Claytonf15996e2011-04-07 22:46:35 +0000924 lldb::TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
Chris Lattner24943d22010-06-08 16:52:24 +0000925 // Search filters require a target...
Greg Clayton987c7eb2011-09-17 08:33:22 +0000926 if (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000927 filter_ap.reset (new SearchFilterByModule (target_sp, module_spec));
928 }
929 break;
930 }
931 }
932 }
933
Greg Claytonf15996e2011-04-07 22:46:35 +0000934 return CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
Greg Clayton63094e02010-06-23 01:19:29 +0000935 completion_mask,
936 input.GetArgumentAtIndex (opt_arg_pos),
937 match_start_point,
938 max_return_elements,
939 filter_ap.get(),
Jim Ingham802f8b02010-06-30 05:02:46 +0000940 word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000941 matches);
942
Chris Lattner24943d22010-06-08 16:52:24 +0000943}
Greg Clayton143fcc32011-04-13 00:18:08 +0000944
945
Greg Clayton143fcc32011-04-13 00:18:08 +0000946void
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000947OptionGroupOptions::Append (OptionGroup* group)
948{
949 const OptionDefinition* group_option_defs = group->GetDefinitions ();
950 const uint32_t group_option_count = group->GetNumDefinitions();
951 for (uint32_t i=0; i<group_option_count; ++i)
952 {
953 m_option_infos.push_back (OptionInfo (group, i));
954 m_option_defs.push_back (group_option_defs[i]);
955 }
956}
957
958void
Greg Clayton5e342f52011-04-13 22:47:15 +0000959OptionGroupOptions::Append (OptionGroup* group,
960 uint32_t src_mask,
961 uint32_t dst_mask)
Greg Clayton143fcc32011-04-13 00:18:08 +0000962{
Greg Clayton143fcc32011-04-13 00:18:08 +0000963 const OptionDefinition* group_option_defs = group->GetDefinitions ();
964 const uint32_t group_option_count = group->GetNumDefinitions();
965 for (uint32_t i=0; i<group_option_count; ++i)
966 {
Greg Clayton5e342f52011-04-13 22:47:15 +0000967 if (group_option_defs[i].usage_mask & src_mask)
968 {
969 m_option_infos.push_back (OptionInfo (group, i));
970 m_option_defs.push_back (group_option_defs[i]);
971 m_option_defs.back().usage_mask = dst_mask;
972 }
Greg Clayton143fcc32011-04-13 00:18:08 +0000973 }
974}
975
976void
977OptionGroupOptions::Finalize ()
978{
979 m_did_finalize = true;
980 OptionDefinition empty_option_def = { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL };
981 m_option_defs.push_back (empty_option_def);
982}
983
984Error
985OptionGroupOptions::SetOptionValue (uint32_t option_idx,
986 const char *option_value)
987{
988 // After calling OptionGroupOptions::Append(...), you must finalize the groups
989 // by calling OptionGroupOptions::Finlize()
990 assert (m_did_finalize);
Greg Clayton5e342f52011-04-13 22:47:15 +0000991 assert (m_option_infos.size() + 1 == m_option_defs.size());
Greg Clayton143fcc32011-04-13 00:18:08 +0000992 Error error;
Greg Clayton5e342f52011-04-13 22:47:15 +0000993 if (option_idx < m_option_infos.size())
994 {
995 error = m_option_infos[option_idx].option_group->SetOptionValue (m_interpreter,
996 m_option_infos[option_idx].option_index,
997 option_value);
998
999 }
1000 else
1001 {
1002 error.SetErrorString ("invalid option index"); // Shouldn't happen...
1003 }
Greg Clayton143fcc32011-04-13 00:18:08 +00001004 return error;
1005}
1006
1007void
1008OptionGroupOptions::OptionParsingStarting ()
1009{
Greg Clayton5e342f52011-04-13 22:47:15 +00001010 std::set<OptionGroup*> group_set;
1011 OptionInfos::iterator pos, end = m_option_infos.end();
1012 for (pos = m_option_infos.begin(); pos != end; ++pos)
1013 {
1014 OptionGroup* group = pos->option_group;
1015 if (group_set.find(group) == group_set.end())
1016 {
1017 group->OptionParsingStarting (m_interpreter);
1018 group_set.insert(group);
1019 }
1020 }
Greg Clayton143fcc32011-04-13 00:18:08 +00001021}
1022Error
1023OptionGroupOptions::OptionParsingFinished ()
1024{
Greg Clayton5e342f52011-04-13 22:47:15 +00001025 std::set<OptionGroup*> group_set;
Greg Clayton143fcc32011-04-13 00:18:08 +00001026 Error error;
Greg Clayton5e342f52011-04-13 22:47:15 +00001027 OptionInfos::iterator pos, end = m_option_infos.end();
1028 for (pos = m_option_infos.begin(); pos != end; ++pos)
Greg Clayton143fcc32011-04-13 00:18:08 +00001029 {
Greg Clayton5e342f52011-04-13 22:47:15 +00001030 OptionGroup* group = pos->option_group;
1031 if (group_set.find(group) == group_set.end())
1032 {
1033 error = group->OptionParsingFinished (m_interpreter);
1034 group_set.insert(group);
1035 if (error.Fail())
1036 return error;
1037 }
Greg Clayton143fcc32011-04-13 00:18:08 +00001038 }
1039 return error;
1040}