blob: 0787d126b4a77020a089a943fcd223621589ce39 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- CommandObjectApropos.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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000014#include "CommandObjectApropos.h"
Jim Ingham40af72e2010-06-15 19:49:27 +000015#include "lldb/Interpreter/Args.h"
16#include "lldb/Interpreter/Options.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000017#include "lldb/Interpreter/Property.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Interpreter/CommandInterpreter.h"
19#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
21using namespace lldb;
22using namespace lldb_private;
23
24//-------------------------------------------------------------------------
25// CommandObjectApropos
26//-------------------------------------------------------------------------
27
Greg Claytona7015092010-09-18 01:14:36 +000028CommandObjectApropos::CommandObjectApropos (CommandInterpreter &interpreter) :
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000029 CommandObjectParsed(interpreter,
30 "apropos",
31 "Find a list of debugger commands related to a particular word/subject.",
32 nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033{
Caroline Tice405fe672010-10-04 22:28:36 +000034 CommandArgumentEntry arg;
35 CommandArgumentData search_word_arg;
36
37 // Define the first (and only) variant of this arg.
38 search_word_arg.arg_type = eArgTypeSearchWord;
39 search_word_arg.arg_repetition = eArgRepeatPlain;
40
41 // There is only one variant this argument could be; put it into the argument entry.
42 arg.push_back (search_word_arg);
43
44 // Push the data for the first argument into the m_arguments vector.
45 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046}
47
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000048CommandObjectApropos::~CommandObjectApropos() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049
50bool
Jim Ingham5a988412012-06-08 21:56:10 +000051CommandObjectApropos::DoExecute (Args& args, CommandReturnObject &result)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052{
Greg Claytonc7bece562013-01-25 18:06:21 +000053 const size_t argc = args.GetArgumentCount ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054
55 if (argc == 1)
56 {
Greg Clayton66111032010-06-23 01:19:29 +000057 const char *search_word = args.GetArgumentAtIndex(0);
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000058 if ((search_word != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059 && (strlen (search_word) > 0))
60 {
61 // The bulk of the work must be done inside the Command Interpreter, since the command dictionary
62 // is private.
63 StringList commands_found;
64 StringList commands_help;
Jim Inghamaf3753e2013-05-17 01:30:37 +000065
Enrico Granatad033e1c2016-03-23 01:21:55 +000066 m_interpreter.FindCommandsForApropos (search_word, commands_found, commands_help, true, true, true);
Jim Inghamaf3753e2013-05-17 01:30:37 +000067
Enrico Granatad033e1c2016-03-23 01:21:55 +000068 if (commands_found.GetSize() == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069 {
Greg Clayton3bcbf842010-11-02 18:23:13 +000070 result.AppendMessageWithFormat ("No commands found pertaining to '%s'. Try 'help' to see a complete list of debugger commands.\n", search_word);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071 }
72 else
73 {
Jim Inghamaf3753e2013-05-17 01:30:37 +000074 if (commands_found.GetSize() > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075 {
Enrico Granatad033e1c2016-03-23 01:21:55 +000076 result.AppendMessageWithFormat ("The following commands may relate to '%s':\n", search_word);
Jim Inghamaf3753e2013-05-17 01:30:37 +000077 size_t max_len = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078
Jim Inghamaf3753e2013-05-17 01:30:37 +000079 for (size_t i = 0; i < commands_found.GetSize(); ++i)
80 {
81 size_t len = strlen (commands_found.GetStringAtIndex (i));
82 if (len > max_len)
83 max_len = len;
84 }
85
86 for (size_t i = 0; i < commands_found.GetSize(); ++i)
87 m_interpreter.OutputFormattedHelpText (result.GetOutputStream(),
88 commands_found.GetStringAtIndex(i),
89 "--",
90 commands_help.GetStringAtIndex(i),
91 max_len);
Jim Inghamaf3753e2013-05-17 01:30:37 +000092 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093 }
Caroline Tice73fd2722011-02-04 00:16:49 +000094
Greg Clayton67cc0632012-08-22 17:17:09 +000095 std::vector<const Property *> properties;
96 const size_t num_properties = m_interpreter.GetDebugger().Apropos(search_word, properties);
97 if (num_properties)
Caroline Tice73fd2722011-02-04 00:16:49 +000098 {
Greg Clayton67cc0632012-08-22 17:17:09 +000099 const bool dump_qualified_name = true;
Caroline Tice73fd2722011-02-04 00:16:49 +0000100 result.AppendMessageWithFormat ("\nThe following settings variables may relate to '%s': \n\n", search_word);
Greg Clayton67cc0632012-08-22 17:17:09 +0000101 for (size_t i=0; i<num_properties; ++i)
102 properties[i]->DumpDescription (m_interpreter, result.GetOutputStream(), 0, dump_qualified_name);
103
Caroline Tice73fd2722011-02-04 00:16:49 +0000104 }
105
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106 result.SetStatus (eReturnStatusSuccessFinishNoResult);
107 }
108 else
109 {
110 result.AppendError ("'' is not a valid search word.\n");
111 result.SetStatus (eReturnStatusFailed);
112 }
113 }
114 else
115 {
116 result.AppendError ("'apropos' must be called with exactly one argument.\n");
117 result.SetStatus (eReturnStatusFailed);
118 }
119
120 return result.Succeeded();
121}