blob: 3c14cc0d44ec4ac3f7e6a2657111eed40266a072 [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
14#include <bitset>
Caroline Ticee5f18b02010-09-09 16:44:14 +000015#include <algorithm>
Chris Lattner24943d22010-06-08 16:52:24 +000016
17// Other libraries and framework includes
18// Project includes
19#include "lldb/Interpreter/CommandObject.h"
20#include "lldb/Interpreter/CommandReturnObject.h"
21#include "lldb/Interpreter/CommandCompletions.h"
22#include "lldb/Interpreter/CommandInterpreter.h"
23#include "lldb/Core/StreamString.h"
24#include "lldb/Target/Target.h"
25
26using namespace lldb;
27using namespace lldb_private;
28
29//-------------------------------------------------------------------------
30// Options
31//-------------------------------------------------------------------------
Greg Claytonf15996e2011-04-07 22:46:35 +000032Options::Options (CommandInterpreter &interpreter) :
33 m_interpreter (interpreter),
Chris Lattner24943d22010-06-08 16:52:24 +000034 m_getopt_table ()
35{
Jim Ingham34e9a982010-06-15 18:47:14 +000036 BuildValidOptionSets();
Chris Lattner24943d22010-06-08 16:52:24 +000037}
38
39Options::~Options ()
40{
41}
42
Chris Lattner24943d22010-06-08 16:52:24 +000043void
Greg Clayton24bc5d92011-03-30 18:16:51 +000044Options::Reset ()
Chris Lattner24943d22010-06-08 16:52:24 +000045{
46 m_seen_options.clear();
Greg Clayton24bc5d92011-03-30 18:16:51 +000047 // Let the subclass reset its option values
48 ResetOptionValues ();
Chris Lattner24943d22010-06-08 16:52:24 +000049}
50
51void
52Options::OptionSeen (int option_idx)
53{
54 m_seen_options.insert ((char) option_idx);
55}
56
57// Returns true is set_a is a subset of set_b; Otherwise returns false.
58
59bool
60Options::IsASubset (const OptionSet& set_a, const OptionSet& set_b)
61{
62 bool is_a_subset = true;
63 OptionSet::const_iterator pos_a;
64 OptionSet::const_iterator pos_b;
65
66 // set_a is a subset of set_b if every member of set_a is also a member of set_b
67
68 for (pos_a = set_a.begin(); pos_a != set_a.end() && is_a_subset; ++pos_a)
69 {
70 pos_b = set_b.find(*pos_a);
71 if (pos_b == set_b.end())
72 is_a_subset = false;
73 }
74
75 return is_a_subset;
76}
77
78// Returns the set difference set_a - set_b, i.e. { x | ElementOf (x, set_a) && !ElementOf (x, set_b) }
79
80size_t
81Options::OptionsSetDiff (const OptionSet& set_a, const OptionSet& set_b, OptionSet& diffs)
82{
83 size_t num_diffs = 0;
84 OptionSet::const_iterator pos_a;
85 OptionSet::const_iterator pos_b;
86
87 for (pos_a = set_a.begin(); pos_a != set_a.end(); ++pos_a)
88 {
89 pos_b = set_b.find(*pos_a);
90 if (pos_b == set_b.end())
91 {
92 ++num_diffs;
93 diffs.insert(*pos_a);
94 }
95 }
96
97 return num_diffs;
98}
99
100// Returns the union of set_a and set_b. Does not put duplicate members into the union.
101
102void
103Options::OptionsSetUnion (const OptionSet &set_a, const OptionSet &set_b, OptionSet &union_set)
104{
105 OptionSet::const_iterator pos;
106 OptionSet::iterator pos_union;
107
108 // Put all the elements of set_a into the union.
109
110 for (pos = set_a.begin(); pos != set_a.end(); ++pos)
111 union_set.insert(*pos);
112
113 // Put all the elements of set_b that are not already there into the union.
114 for (pos = set_b.begin(); pos != set_b.end(); ++pos)
115 {
116 pos_union = union_set.find(*pos);
117 if (pos_union == union_set.end())
118 union_set.insert(*pos);
119 }
120}
121
122bool
123Options::VerifyOptions (CommandReturnObject &result)
124{
125 bool options_are_valid = false;
126
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000127 int num_levels = GetRequiredOptions().size();
Chris Lattner24943d22010-06-08 16:52:24 +0000128 if (num_levels)
129 {
130 for (int i = 0; i < num_levels && !options_are_valid; ++i)
131 {
132 // This is the correct set of options if: 1). m_seen_options contains all of m_required_options[i]
133 // (i.e. all the required options at this level are a subset of m_seen_options); AND
134 // 2). { m_seen_options - m_required_options[i] is a subset of m_options_options[i] (i.e. all the rest of
135 // m_seen_options are in the set of optional options at this level.
136
137 // Check to see if all of m_required_options[i] are a subset of m_seen_options
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000138 if (IsASubset (GetRequiredOptions()[i], m_seen_options))
Chris Lattner24943d22010-06-08 16:52:24 +0000139 {
140 // Construct the set difference: remaining_options = {m_seen_options} - {m_required_options[i]}
141 OptionSet remaining_options;
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000142 OptionsSetDiff (m_seen_options, GetRequiredOptions()[i], remaining_options);
Chris Lattner24943d22010-06-08 16:52:24 +0000143 // Check to see if remaining_options is a subset of m_optional_options[i]
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000144 if (IsASubset (remaining_options, GetOptionalOptions()[i]))
Chris Lattner24943d22010-06-08 16:52:24 +0000145 options_are_valid = true;
146 }
147 }
148 }
149 else
150 {
151 options_are_valid = true;
152 }
153
154 if (options_are_valid)
155 {
156 result.SetStatus (eReturnStatusSuccessFinishNoResult);
157 }
158 else
159 {
160 result.AppendError ("invalid combination of options for the given command");
161 result.SetStatus (eReturnStatusFailed);
162 }
163
164 return options_are_valid;
165}
166
Jim Ingham34e9a982010-06-15 18:47:14 +0000167// This is called in the Options constructor, though we could call it lazily if that ends up being
168// a performance problem.
169
Chris Lattner24943d22010-06-08 16:52:24 +0000170void
171Options::BuildValidOptionSets ()
172{
173 // Check to see if we already did this.
174 if (m_required_options.size() != 0)
175 return;
176
177 // Check to see if there are any options.
178 int num_options = NumCommandOptions ();
179 if (num_options == 0)
180 return;
181
Greg Claytonb3448432011-03-24 21:19:54 +0000182 const OptionDefinition *full_options_table = GetDefinitions();
Chris Lattner24943d22010-06-08 16:52:24 +0000183 m_required_options.resize(1);
184 m_optional_options.resize(1);
Jim Ingham34e9a982010-06-15 18:47:14 +0000185
186 // First count the number of option sets we've got. Ignore LLDB_ALL_OPTION_SETS...
187
188 uint32_t num_option_sets = 0;
189
190 for (int i = 0; i < num_options; i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000191 {
Jim Ingham34e9a982010-06-15 18:47:14 +0000192 uint32_t this_usage_mask = full_options_table[i].usage_mask;
193 if (this_usage_mask == LLDB_OPT_SET_ALL)
Chris Lattner24943d22010-06-08 16:52:24 +0000194 {
Jim Ingham34e9a982010-06-15 18:47:14 +0000195 if (num_option_sets == 0)
196 num_option_sets = 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000197 }
198 else
199 {
Jim Ingham34e9a982010-06-15 18:47:14 +0000200 for (int j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++)
201 {
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000202 if (this_usage_mask & (1 << j))
Jim Ingham34e9a982010-06-15 18:47:14 +0000203 {
204 if (num_option_sets <= j)
205 num_option_sets = j + 1;
206 }
207 }
Chris Lattner24943d22010-06-08 16:52:24 +0000208 }
Jim Ingham34e9a982010-06-15 18:47:14 +0000209 }
Chris Lattner24943d22010-06-08 16:52:24 +0000210
Jim Ingham34e9a982010-06-15 18:47:14 +0000211 if (num_option_sets > 0)
212 {
213 m_required_options.resize(num_option_sets);
214 m_optional_options.resize(num_option_sets);
215
216 for (int i = 0; i < num_options; ++i)
217 {
218 for (int j = 0; j < num_option_sets; j++)
219 {
220 if (full_options_table[i].usage_mask & 1 << j)
221 {
222 if (full_options_table[i].required)
223 m_required_options[j].insert(full_options_table[i].short_option);
224 else
225 m_optional_options[j].insert(full_options_table[i].short_option);
226 }
227 }
228 }
Chris Lattner24943d22010-06-08 16:52:24 +0000229 }
230}
231
232uint32_t
233Options::NumCommandOptions ()
234{
Greg Claytonb3448432011-03-24 21:19:54 +0000235 const OptionDefinition *full_options_table = GetDefinitions ();
Jim Ingham34e9a982010-06-15 18:47:14 +0000236 if (full_options_table == NULL)
237 return 0;
238
Chris Lattner24943d22010-06-08 16:52:24 +0000239 int i = 0;
240
241 if (full_options_table != NULL)
242 {
243 while (full_options_table[i].long_option != NULL)
244 ++i;
245 }
246
247 return i;
248}
249
250struct option *
251Options::GetLongOptions ()
252{
253 // Check to see if this has already been done.
254 if (m_getopt_table.empty())
255 {
256 // Check to see if there are any options.
257 const uint32_t num_options = NumCommandOptions();
258 if (num_options == 0)
259 return NULL;
260
261 uint32_t i;
262 uint32_t j;
Greg Claytonb3448432011-03-24 21:19:54 +0000263 const OptionDefinition *full_options_table = GetDefinitions();
Chris Lattner24943d22010-06-08 16:52:24 +0000264
265 std::bitset<256> option_seen;
266
267 m_getopt_table.resize(num_options + 1);
268 for (i = 0, j = 0; i < num_options; ++i)
269 {
270 char short_opt = full_options_table[i].short_option;
271
272 if (option_seen.test(short_opt) == false)
273 {
274 m_getopt_table[j].name = full_options_table[i].long_option;
275 m_getopt_table[j].has_arg = full_options_table[i].option_has_arg;
276 m_getopt_table[j].flag = NULL;
277 m_getopt_table[j].val = full_options_table[i].short_option;
278 option_seen.set(short_opt);
279 ++j;
280 }
281 }
282
283 //getopt_long requires a NULL final entry in the table:
284
285 m_getopt_table[j].name = NULL;
286 m_getopt_table[j].has_arg = 0;
287 m_getopt_table[j].flag = NULL;
288 m_getopt_table[j].val = 0;
289 }
290
Greg Clayton53d68e72010-07-20 22:52:08 +0000291 if (m_getopt_table.empty())
292 return NULL;
293
294 return &m_getopt_table.front();
Chris Lattner24943d22010-06-08 16:52:24 +0000295}
296
297
298// This function takes INDENT, which tells how many spaces to output at the front of each line; SPACES, which is
299// a string containing 80 spaces; and TEXT, which is the text that is to be output. It outputs the text, on
300// multiple lines if necessary, to RESULT, with INDENT spaces at the front of each line. It breaks lines on spaces,
301// tabs or newlines, shortening the line if necessary to not break in the middle of a word. It assumes that each
302// output line should contain a maximum of OUTPUT_MAX_COLUMNS characters.
303
304
305void
306Options::OutputFormattedUsageText
307(
308 Stream &strm,
309 const char *text,
310 uint32_t output_max_columns
311)
312{
313 int len = strlen (text);
314
315 // Will it all fit on one line?
316
317 if ((len + strm.GetIndentLevel()) < output_max_columns)
318 {
319 // Output it as a single line.
320 strm.Indent (text);
321 strm.EOL();
322 }
323 else
324 {
325 // We need to break it up into multiple lines.
326
327 int text_width = output_max_columns - strm.GetIndentLevel() - 1;
328 int start = 0;
329 int end = start;
330 int final_end = strlen (text);
331 int sub_len;
332
333 while (end < final_end)
334 {
335 // Don't start the 'text' on a space, since we're already outputting the indentation.
336 while ((start < final_end) && (text[start] == ' '))
337 start++;
338
339 end = start + text_width;
340 if (end > final_end)
341 end = final_end;
342 else
343 {
344 // If we're not at the end of the text, make sure we break the line on white space.
345 while (end > start
346 && text[end] != ' ' && text[end] != '\t' && text[end] != '\n')
347 end--;
348 }
349
350 sub_len = end - start;
351 if (start != 0)
352 strm.EOL();
353 strm.Indent();
354 assert (start < final_end);
355 assert (start + sub_len <= final_end);
356 strm.Write(text + start, sub_len);
357 start = end + 1;
358 }
359 strm.EOL();
360 }
361}
362
363void
364Options::GenerateOptionUsage
365(
366 Stream &strm,
Greg Clayton238c0a12010-09-18 01:14:36 +0000367 CommandObject *cmd
368)
Chris Lattner24943d22010-06-08 16:52:24 +0000369{
Greg Claytonf15996e2011-04-07 22:46:35 +0000370 const uint32_t screen_width = m_interpreter.GetDebugger().GetTerminalWidth();
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000371
Greg Claytonb3448432011-03-24 21:19:54 +0000372 const OptionDefinition *full_options_table = GetDefinitions();
Chris Lattner24943d22010-06-08 16:52:24 +0000373 const uint32_t save_indent_level = strm.GetIndentLevel();
374 const char *name;
375
Caroline Ticefb355112010-10-01 17:46:38 +0000376 StreamString arguments_str;
377
Chris Lattner24943d22010-06-08 16:52:24 +0000378 if (cmd)
Caroline Ticefb355112010-10-01 17:46:38 +0000379 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000380 name = cmd->GetCommandName();
Caroline Ticefb355112010-10-01 17:46:38 +0000381 cmd->GetFormattedCommandArguments (arguments_str);
382 }
Chris Lattner24943d22010-06-08 16:52:24 +0000383 else
Greg Clayton238c0a12010-09-18 01:14:36 +0000384 name = "";
Chris Lattner24943d22010-06-08 16:52:24 +0000385
386 strm.PutCString ("\nCommand Options Usage:\n");
387
388 strm.IndentMore(2);
389
390 // First, show each usage level set of options, e.g. <cmd> [options-for-level-0]
391 // <cmd> [options-for-level-1]
392 // etc.
393
Chris Lattner24943d22010-06-08 16:52:24 +0000394 const uint32_t num_options = NumCommandOptions();
Jim Ingham34e9a982010-06-15 18:47:14 +0000395 if (num_options == 0)
396 return;
397
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000398 int num_option_sets = GetRequiredOptions().size();
Jim Ingham34e9a982010-06-15 18:47:14 +0000399
Chris Lattner24943d22010-06-08 16:52:24 +0000400 uint32_t i;
Jim Ingham34e9a982010-06-15 18:47:14 +0000401
402 for (uint32_t opt_set = 0; opt_set < num_option_sets; ++opt_set)
Chris Lattner24943d22010-06-08 16:52:24 +0000403 {
Jim Ingham34e9a982010-06-15 18:47:14 +0000404 uint32_t opt_set_mask;
405
406 opt_set_mask = 1 << opt_set;
407 if (opt_set > 0)
408 strm.Printf ("\n");
409 strm.Indent (name);
Caroline Ticee5f18b02010-09-09 16:44:14 +0000410
Greg Claytonfe424a92010-09-18 03:37:20 +0000411 // First go through and print all options that take no arguments as
412 // a single string. If a command has "-a" "-b" and "-c", this will show
413 // up as [-abc]
414
415 std::set<char> options;
416 std::set<char>::const_iterator options_pos, options_end;
417 bool first;
418 for (i = 0, first = true; i < num_options; ++i)
419 {
420 if (full_options_table[i].usage_mask & opt_set_mask)
421 {
422 // Add current option to the end of out_stream.
423
424 if (full_options_table[i].required == true &&
425 full_options_table[i].option_has_arg == no_argument)
426 {
427 options.insert (full_options_table[i].short_option);
428 }
429 }
430 }
431
432 if (options.empty() == false)
433 {
434 // We have some required options with no arguments
435 strm.PutCString(" -");
436 for (i=0; i<2; ++i)
437 for (options_pos = options.begin(), options_end = options.end();
438 options_pos != options_end;
439 ++options_pos)
440 {
441 if (i==0 && ::isupper (*options_pos))
442 continue;
443 if (i==1 && ::islower (*options_pos))
444 continue;
445 strm << *options_pos;
446 }
447 }
448
449 for (i = 0, options.clear(); i < num_options; ++i)
450 {
451 if (full_options_table[i].usage_mask & opt_set_mask)
452 {
453 // Add current option to the end of out_stream.
454
455 if (full_options_table[i].required == false &&
456 full_options_table[i].option_has_arg == no_argument)
457 {
458 options.insert (full_options_table[i].short_option);
459 }
460 }
461 }
462
463 if (options.empty() == false)
464 {
465 // We have some required options with no arguments
466 strm.PutCString(" [-");
467 for (i=0; i<2; ++i)
468 for (options_pos = options.begin(), options_end = options.end();
469 options_pos != options_end;
470 ++options_pos)
471 {
472 if (i==0 && ::isupper (*options_pos))
473 continue;
474 if (i==1 && ::islower (*options_pos))
475 continue;
476 strm << *options_pos;
477 }
478 strm.PutChar(']');
479 }
480
Caroline Ticee5f18b02010-09-09 16:44:14 +0000481 // First go through and print the required options (list them up front).
Jim Ingham34e9a982010-06-15 18:47:14 +0000482
483 for (i = 0; i < num_options; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000484 {
Jim Ingham34e9a982010-06-15 18:47:14 +0000485 if (full_options_table[i].usage_mask & opt_set_mask)
Chris Lattner24943d22010-06-08 16:52:24 +0000486 {
Jim Ingham34e9a982010-06-15 18:47:14 +0000487 // Add current option to the end of out_stream.
Caroline Tice4d6675c2010-10-01 19:59:14 +0000488 CommandArgumentType arg_type = full_options_table[i].argument_type;
489
Jim Ingham34e9a982010-06-15 18:47:14 +0000490 if (full_options_table[i].required)
491 {
492 if (full_options_table[i].option_has_arg == required_argument)
493 {
Caroline Tice4d6675c2010-10-01 19:59:14 +0000494 strm.Printf (" -%c <%s>",
495 full_options_table[i].short_option,
496 CommandObject::GetArgumentName (arg_type));
Jim Ingham34e9a982010-06-15 18:47:14 +0000497 }
498 else if (full_options_table[i].option_has_arg == optional_argument)
499 {
Caroline Tice4d6675c2010-10-01 19:59:14 +0000500 strm.Printf (" -%c [<%s>]",
Jim Ingham34e9a982010-06-15 18:47:14 +0000501 full_options_table[i].short_option,
Caroline Tice4d6675c2010-10-01 19:59:14 +0000502 CommandObject::GetArgumentName (arg_type));
Jim Ingham34e9a982010-06-15 18:47:14 +0000503 }
Jim Ingham34e9a982010-06-15 18:47:14 +0000504 }
Caroline Ticee5f18b02010-09-09 16:44:14 +0000505 }
506 }
507
508 // Now go through again, and this time only print the optional options.
509
510 for (i = 0; i < num_options; ++i)
511 {
512 if (full_options_table[i].usage_mask & opt_set_mask)
513 {
514 // Add current option to the end of out_stream.
515
Caroline Tice4d6675c2010-10-01 19:59:14 +0000516 CommandArgumentType arg_type = full_options_table[i].argument_type;
517
Caroline Ticee5f18b02010-09-09 16:44:14 +0000518 if (! full_options_table[i].required)
Jim Ingham34e9a982010-06-15 18:47:14 +0000519 {
520 if (full_options_table[i].option_has_arg == required_argument)
Caroline Tice4d6675c2010-10-01 19:59:14 +0000521 strm.Printf (" [-%c <%s>]", full_options_table[i].short_option,
522 CommandObject::GetArgumentName (arg_type));
Jim Ingham34e9a982010-06-15 18:47:14 +0000523 else if (full_options_table[i].option_has_arg == optional_argument)
Caroline Tice4d6675c2010-10-01 19:59:14 +0000524 strm.Printf (" [-%c [<%s>]]", full_options_table[i].short_option,
525 CommandObject::GetArgumentName (arg_type));
Jim Ingham34e9a982010-06-15 18:47:14 +0000526 }
Chris Lattner24943d22010-06-08 16:52:24 +0000527 }
Chris Lattner24943d22010-06-08 16:52:24 +0000528 }
Caroline Ticefb355112010-10-01 17:46:38 +0000529 if (arguments_str.GetSize() > 0)
530 strm.Printf (" %s", arguments_str.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +0000531 }
Chris Lattner24943d22010-06-08 16:52:24 +0000532 strm.Printf ("\n\n");
533
534 // Now print out all the detailed information about the various options: long form, short form and help text:
Greg Claytonfe424a92010-09-18 03:37:20 +0000535 // --long_name <argument> ( -short <argument> )
Chris Lattner24943d22010-06-08 16:52:24 +0000536 // help text
537
538 // This variable is used to keep track of which options' info we've printed out, because some options can be in
539 // more than one usage level, but we only want to print the long form of its information once.
540
541 OptionSet options_seen;
542 OptionSet::iterator pos;
543 strm.IndentMore (5);
544
Caroline Ticee5f18b02010-09-09 16:44:14 +0000545 std::vector<char> sorted_options;
546
547
548 // Put the unique command options in a vector & sort it, so we can output them alphabetically (by short_option)
549 // when writing out detailed help for each option.
550
Chris Lattner24943d22010-06-08 16:52:24 +0000551 for (i = 0; i < num_options; ++i)
552 {
Chris Lattner24943d22010-06-08 16:52:24 +0000553 pos = options_seen.find (full_options_table[i].short_option);
554 if (pos == options_seen.end())
555 {
Chris Lattner24943d22010-06-08 16:52:24 +0000556 options_seen.insert (full_options_table[i].short_option);
Caroline Ticee5f18b02010-09-09 16:44:14 +0000557 sorted_options.push_back (full_options_table[i].short_option);
558 }
559 }
Chris Lattner24943d22010-06-08 16:52:24 +0000560
Caroline Ticee5f18b02010-09-09 16:44:14 +0000561 std::sort (sorted_options.begin(), sorted_options.end());
562
563 // Go through the unique'd and alphabetically sorted vector of options, find the table entry for each option
564 // and write out the detailed help information for that option.
565
566 int first_option_printed = 1;
567 size_t end = sorted_options.size();
568 for (size_t j = 0; j < end; ++j)
569 {
570 char option = sorted_options[j];
571 bool found = false;
572 for (i = 0; i < num_options && !found; ++i)
573 {
574 if (full_options_table[i].short_option == option)
575 {
576 found = true;
577 //Print out the help information for this option.
578
579 // Put a newline separation between arguments
580 if (first_option_printed)
581 first_option_printed = 0;
582 else
583 strm.EOL();
584
Caroline Tice4d6675c2010-10-01 19:59:14 +0000585 CommandArgumentType arg_type = full_options_table[i].argument_type;
586
587 StreamString arg_name_str;
588 arg_name_str.Printf ("<%s>", CommandObject::GetArgumentName (arg_type));
589
Caroline Ticee5f18b02010-09-09 16:44:14 +0000590 strm.Indent ();
Caroline Tice9e8e6962010-09-20 21:52:58 +0000591 strm.Printf ("-%c", full_options_table[i].short_option);
Caroline Tice4d6675c2010-10-01 19:59:14 +0000592 if (arg_type != eArgTypeNone)
593 strm.Printf (" <%s>", CommandObject::GetArgumentName (arg_type));
Caroline Tice9e8e6962010-09-20 21:52:58 +0000594 strm.Printf (" ( --%s", full_options_table[i].long_option);
Caroline Tice4d6675c2010-10-01 19:59:14 +0000595 if (arg_type != eArgTypeNone)
596 strm.Printf (" <%s>", CommandObject::GetArgumentName (arg_type));
Greg Claytonfe424a92010-09-18 03:37:20 +0000597 strm.PutCString(" )\n");
Caroline Ticee5f18b02010-09-09 16:44:14 +0000598
599 strm.IndentMore (5);
600
601 if (full_options_table[i].usage_text)
Chris Lattner24943d22010-06-08 16:52:24 +0000602 OutputFormattedUsageText (strm,
603 full_options_table[i].usage_text,
604 screen_width);
Caroline Ticee5f18b02010-09-09 16:44:14 +0000605 if (full_options_table[i].enum_values != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000606 {
Caroline Ticee5f18b02010-09-09 16:44:14 +0000607 strm.Indent ();
608 strm.Printf("Values: ");
609 for (int k = 0; full_options_table[i].enum_values[k].string_value != NULL; k++)
610 {
611 if (k == 0)
612 strm.Printf("%s", full_options_table[i].enum_values[k].string_value);
613 else
614 strm.Printf(" | %s", full_options_table[i].enum_values[k].string_value);
615 }
616 strm.EOL();
Chris Lattner24943d22010-06-08 16:52:24 +0000617 }
Caroline Ticee5f18b02010-09-09 16:44:14 +0000618 strm.IndentLess (5);
Chris Lattner24943d22010-06-08 16:52:24 +0000619 }
Chris Lattner24943d22010-06-08 16:52:24 +0000620 }
621 }
622
623 // Restore the indent level
624 strm.SetIndentLevel (save_indent_level);
625}
626
627// This function is called when we have been given a potentially incomplete set of
628// options, such as when an alias has been defined (more options might be added at
629// at the time the alias is invoked). We need to verify that the options in the set
630// m_seen_options are all part of a set that may be used together, but m_seen_options
631// may be missing some of the "required" options.
632
633bool
634Options::VerifyPartialOptions (CommandReturnObject &result)
635{
636 bool options_are_valid = false;
637
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000638 int num_levels = GetRequiredOptions().size();
Chris Lattner24943d22010-06-08 16:52:24 +0000639 if (num_levels)
640 {
641 for (int i = 0; i < num_levels && !options_are_valid; ++i)
642 {
643 // In this case we are treating all options as optional rather than required.
644 // Therefore a set of options is correct if m_seen_options is a subset of the
645 // union of m_required_options and m_optional_options.
646 OptionSet union_set;
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000647 OptionsSetUnion (GetRequiredOptions()[i], GetOptionalOptions()[i], union_set);
Chris Lattner24943d22010-06-08 16:52:24 +0000648 if (IsASubset (m_seen_options, union_set))
649 options_are_valid = true;
650 }
651 }
652
653 return options_are_valid;
654}
655
656bool
657Options::HandleOptionCompletion
658(
659 Args &input,
660 OptionElementVector &opt_element_vector,
661 int cursor_index,
662 int char_pos,
663 int match_start_point,
664 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000665 bool &word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000666 lldb_private::StringList &matches
667)
668{
Jim Ingham802f8b02010-06-30 05:02:46 +0000669 word_complete = true;
670
Chris Lattner24943d22010-06-08 16:52:24 +0000671 // For now we just scan the completions to see if the cursor position is in
672 // an option or its argument. Otherwise we'll call HandleArgumentCompletion.
673 // In the future we can use completion to validate options as well if we want.
674
675 const OptionDefinition *opt_defs = GetDefinitions();
676
677 std::string cur_opt_std_str (input.GetArgumentAtIndex(cursor_index));
678 cur_opt_std_str.erase(char_pos);
679 const char *cur_opt_str = cur_opt_std_str.c_str();
680
681 for (int i = 0; i < opt_element_vector.size(); i++)
682 {
683 int opt_pos = opt_element_vector[i].opt_pos;
684 int opt_arg_pos = opt_element_vector[i].opt_arg_pos;
685 int opt_defs_index = opt_element_vector[i].opt_defs_index;
686 if (opt_pos == cursor_index)
687 {
688 // We're completing the option itself.
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000689
690 if (opt_defs_index == OptionArgElement::eBareDash)
691 {
692 // We're completing a bare dash. That means all options are open.
693 // FIXME: We should scan the other options provided and only complete options
694 // within the option group they belong to.
695 char opt_str[3] = {'-', 'a', '\0'};
696
Greg Claytonbef15832010-07-14 00:18:15 +0000697 for (int j = 0 ; opt_defs[j].short_option != 0 ; j++)
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000698 {
Greg Claytonbef15832010-07-14 00:18:15 +0000699 opt_str[1] = opt_defs[j].short_option;
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000700 matches.AppendString (opt_str);
701 }
702 return true;
703 }
704 else if (opt_defs_index == OptionArgElement::eBareDoubleDash)
705 {
706 std::string full_name ("--");
Greg Claytonbef15832010-07-14 00:18:15 +0000707 for (int j = 0 ; opt_defs[j].short_option != 0 ; j++)
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000708 {
709 full_name.erase(full_name.begin() + 2, full_name.end());
Greg Claytonbef15832010-07-14 00:18:15 +0000710 full_name.append (opt_defs[j].long_option);
Jim Ingham8b9af1c2010-06-24 20:30:15 +0000711 matches.AppendString (full_name.c_str());
712 }
713 return true;
714 }
715 else if (opt_defs_index != OptionArgElement::eUnrecognizedArg)
Chris Lattner24943d22010-06-08 16:52:24 +0000716 {
717 // We recognized it, if it an incomplete long option, complete it anyway (getopt_long is
718 // happy with shortest unique string, but it's still a nice thing to do.) Otherwise return
719 // The string so the upper level code will know this is a full match and add the " ".
720 if (cur_opt_str && strlen (cur_opt_str) > 2
721 && cur_opt_str[0] == '-' && cur_opt_str[1] == '-'
722 && strcmp (opt_defs[opt_defs_index].long_option, cur_opt_str) != 0)
723 {
724 std::string full_name ("--");
725 full_name.append (opt_defs[opt_defs_index].long_option);
726 matches.AppendString(full_name.c_str());
727 return true;
728 }
729 else
730 {
731 matches.AppendString(input.GetArgumentAtIndex(cursor_index));
732 return true;
733 }
734 }
735 else
736 {
737 // FIXME - not handling wrong options yet:
738 // Check to see if they are writing a long option & complete it.
739 // I think we will only get in here if the long option table has two elements
740 // that are not unique up to this point. getopt_long does shortest unique match
741 // for long options already.
742
743 if (cur_opt_str && strlen (cur_opt_str) > 2
744 && cur_opt_str[0] == '-' && cur_opt_str[1] == '-')
745 {
Greg Claytonbef15832010-07-14 00:18:15 +0000746 for (int j = 0 ; opt_defs[j].short_option != 0 ; j++)
Chris Lattner24943d22010-06-08 16:52:24 +0000747 {
Greg Claytonbef15832010-07-14 00:18:15 +0000748 if (strstr(opt_defs[j].long_option, cur_opt_str + 2) == opt_defs[j].long_option)
Chris Lattner24943d22010-06-08 16:52:24 +0000749 {
750 std::string full_name ("--");
Greg Claytonbef15832010-07-14 00:18:15 +0000751 full_name.append (opt_defs[j].long_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000752 // The options definitions table has duplicates because of the
753 // way the grouping information is stored, so only add once.
754 bool duplicate = false;
Greg Claytonbef15832010-07-14 00:18:15 +0000755 for (int k = 0; k < matches.GetSize(); k++)
Chris Lattner24943d22010-06-08 16:52:24 +0000756 {
Greg Claytonbef15832010-07-14 00:18:15 +0000757 if (matches.GetStringAtIndex(k) == full_name)
Chris Lattner24943d22010-06-08 16:52:24 +0000758 {
759 duplicate = true;
760 break;
761 }
762 }
763 if (!duplicate)
764 matches.AppendString(full_name.c_str());
765 }
766 }
767 }
768 return true;
769 }
770
771
772 }
773 else if (opt_arg_pos == cursor_index)
774 {
775 // Okay the cursor is on the completion of an argument.
776 // See if it has a completion, otherwise return no matches.
777
778 if (opt_defs_index != -1)
779 {
Greg Claytonf15996e2011-04-07 22:46:35 +0000780 HandleOptionArgumentCompletion (input,
Greg Clayton63094e02010-06-23 01:19:29 +0000781 cursor_index,
782 strlen (input.GetArgumentAtIndex(cursor_index)),
783 opt_element_vector,
784 i,
785 match_start_point,
786 max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000787 word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000788 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000789 return true;
790 }
791 else
792 {
793 // No completion callback means no completions...
794 return true;
795 }
796
797 }
798 else
799 {
800 // Not the last element, keep going.
801 continue;
802 }
803 }
804 return false;
805}
806
807bool
808Options::HandleOptionArgumentCompletion
809(
810 Args &input,
811 int cursor_index,
812 int char_pos,
813 OptionElementVector &opt_element_vector,
814 int opt_element_index,
815 int match_start_point,
816 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000817 bool &word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000818 lldb_private::StringList &matches
819)
820{
821 const OptionDefinition *opt_defs = GetDefinitions();
822 std::auto_ptr<SearchFilter> filter_ap;
823
824 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
825 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
826
827 // See if this is an enumeration type option, and if so complete it here:
828
829 OptionEnumValueElement *enum_values = opt_defs[opt_defs_index].enum_values;
830 if (enum_values != NULL)
831 {
832 bool return_value = false;
833 std::string match_string(input.GetArgumentAtIndex (opt_arg_pos), input.GetArgumentAtIndex (opt_arg_pos) + char_pos);
834 for (int i = 0; enum_values[i].string_value != NULL; i++)
835 {
836 if (strstr(enum_values[i].string_value, match_string.c_str()) == enum_values[i].string_value)
837 {
838 matches.AppendString (enum_values[i].string_value);
839 return_value = true;
840 }
841 }
842 return return_value;
843 }
844
845 // If this is a source file or symbol type completion, and there is a
846 // -shlib option somewhere in the supplied arguments, then make a search filter
847 // for that shared library.
848 // FIXME: Do we want to also have an "OptionType" so we don't have to match string names?
849
850 uint32_t completion_mask = opt_defs[opt_defs_index].completionType;
851 if (completion_mask & CommandCompletions::eSourceFileCompletion
852 || completion_mask & CommandCompletions::eSymbolCompletion)
853 {
854 for (int i = 0; i < opt_element_vector.size(); i++)
855 {
856 int cur_defs_index = opt_element_vector[i].opt_defs_index;
857 int cur_arg_pos = opt_element_vector[i].opt_arg_pos;
858 const char *cur_opt_name = opt_defs[cur_defs_index].long_option;
859
860 // If this is the "shlib" option and there was an argument provided,
861 // restrict it to that shared library.
862 if (strcmp(cur_opt_name, "shlib") == 0 && cur_arg_pos != -1)
863 {
864 const char *module_name = input.GetArgumentAtIndex(cur_arg_pos);
865 if (module_name)
866 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000867 FileSpec module_spec(module_name, false);
Greg Claytonf15996e2011-04-07 22:46:35 +0000868 lldb::TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
Chris Lattner24943d22010-06-08 16:52:24 +0000869 // Search filters require a target...
870 if (target_sp != NULL)
871 filter_ap.reset (new SearchFilterByModule (target_sp, module_spec));
872 }
873 break;
874 }
875 }
876 }
877
Greg Claytonf15996e2011-04-07 22:46:35 +0000878 return CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
Greg Clayton63094e02010-06-23 01:19:29 +0000879 completion_mask,
880 input.GetArgumentAtIndex (opt_arg_pos),
881 match_start_point,
882 max_return_elements,
883 filter_ap.get(),
Jim Ingham802f8b02010-06-30 05:02:46 +0000884 word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000885 matches);
886
Chris Lattner24943d22010-06-08 16:52:24 +0000887}