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