blob: a71c2b6b93a8f9837b1901c24930f6f6ee4da40e [file] [log] [blame]
Chris Lattner24943d22010-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
10#include "CommandObjectApropos.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Jim Ingham84cdc152010-06-15 19:49:27 +000016#include "lldb/Interpreter/Args.h"
17#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018
19#include "lldb/Interpreter/CommandInterpreter.h"
20#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021
22using namespace lldb;
23using namespace lldb_private;
24
25//-------------------------------------------------------------------------
26// CommandObjectApropos
27//-------------------------------------------------------------------------
28
Greg Clayton238c0a12010-09-18 01:14:36 +000029CommandObjectApropos::CommandObjectApropos (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +000030 CommandObjectParsed (interpreter,
31 "apropos",
32 "Find a list of debugger commands related to a particular word/subject.",
33 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +000034{
Caroline Tice43b014a2010-10-04 22:28:36 +000035 CommandArgumentEntry arg;
36 CommandArgumentData search_word_arg;
37
38 // Define the first (and only) variant of this arg.
39 search_word_arg.arg_type = eArgTypeSearchWord;
40 search_word_arg.arg_repetition = eArgRepeatPlain;
41
42 // There is only one variant this argument could be; put it into the argument entry.
43 arg.push_back (search_word_arg);
44
45 // Push the data for the first argument into the m_arguments vector.
46 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +000047}
48
49CommandObjectApropos::~CommandObjectApropos()
50{
51}
52
53
54bool
Jim Inghamda26bd22012-06-08 21:56:10 +000055CommandObjectApropos::DoExecute (Args& args, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +000056{
Greg Clayton63094e02010-06-23 01:19:29 +000057 const int argc = args.GetArgumentCount ();
Chris Lattner24943d22010-06-08 16:52:24 +000058
59 if (argc == 1)
60 {
Greg Clayton63094e02010-06-23 01:19:29 +000061 const char *search_word = args.GetArgumentAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +000062 if ((search_word != NULL)
63 && (strlen (search_word) > 0))
64 {
65 // The bulk of the work must be done inside the Command Interpreter, since the command dictionary
66 // is private.
67 StringList commands_found;
68 StringList commands_help;
Greg Clayton238c0a12010-09-18 01:14:36 +000069 m_interpreter.FindCommandsForApropos (search_word, commands_found, commands_help);
Chris Lattner24943d22010-06-08 16:52:24 +000070 if (commands_found.GetSize() == 0)
71 {
Greg Clayton02c20f12010-11-02 18:23:13 +000072 result.AppendMessageWithFormat ("No commands found pertaining to '%s'. Try 'help' to see a complete list of debugger commands.\n", search_word);
Chris Lattner24943d22010-06-08 16:52:24 +000073 }
74 else
75 {
76 result.AppendMessageWithFormat ("The following commands may relate to '%s':\n", search_word);
77 size_t max_len = 0;
78
Greg Clayton54e7afa2010-07-09 20:39:50 +000079 for (size_t i = 0; i < commands_found.GetSize(); ++i)
Chris Lattner24943d22010-06-08 16:52:24 +000080 {
Greg Clayton54e7afa2010-07-09 20:39:50 +000081 size_t len = strlen (commands_found.GetStringAtIndex (i));
Chris Lattner24943d22010-06-08 16:52:24 +000082 if (len > max_len)
83 max_len = len;
84 }
85
Greg Clayton54e7afa2010-07-09 20:39:50 +000086 for (size_t i = 0; i < commands_found.GetSize(); ++i)
Greg Clayton238c0a12010-09-18 01:14:36 +000087 m_interpreter.OutputFormattedHelpText (result.GetOutputStream(),
88 commands_found.GetStringAtIndex(i),
89 "--", commands_help.
90 GetStringAtIndex(i),
91 max_len);
92
Chris Lattner24943d22010-06-08 16:52:24 +000093 }
Caroline Ticea04acd82011-02-04 00:16:49 +000094
95
96 StreamString settings_search_results;
97 lldb::UserSettingsControllerSP root = Debugger::GetSettingsController ();
Greg Clayton41c56fa2011-04-19 22:32:36 +000098 const char *settings_prefix = root->GetLevelName().GetCString();
Caroline Ticea04acd82011-02-04 00:16:49 +000099
Greg Clayton41c56fa2011-04-19 22:32:36 +0000100 UserSettingsController::SearchAllSettingsDescriptions (m_interpreter,
101 root,
102 settings_prefix,
103 search_word,
Caroline Ticea04acd82011-02-04 00:16:49 +0000104 settings_search_results);
105
106 if (settings_search_results.GetSize() > 0)
107 {
108 result.AppendMessageWithFormat ("\nThe following settings variables may relate to '%s': \n\n", search_word);
109 result.AppendMessageWithFormat ("%s", settings_search_results.GetData());
110 }
111
Chris Lattner24943d22010-06-08 16:52:24 +0000112 result.SetStatus (eReturnStatusSuccessFinishNoResult);
113 }
114 else
115 {
116 result.AppendError ("'' is not a valid search word.\n");
117 result.SetStatus (eReturnStatusFailed);
118 }
119 }
120 else
121 {
122 result.AppendError ("'apropos' must be called with exactly one argument.\n");
123 result.SetStatus (eReturnStatusFailed);
124 }
125
126 return result.Succeeded();
127}