1 //===-- 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
16 #include "lldb/Interpreter/Args.h"
17 #include "lldb/Interpreter/Options.h"
18 
19 #include "lldb/Interpreter/CommandInterpreter.h"
20 #include "lldb/Interpreter/CommandReturnObject.h"
21 
22 using namespace lldb;
23 using namespace lldb_private;
24 
25 //-------------------------------------------------------------------------
26 // CommandObjectApropos
27 //-------------------------------------------------------------------------
28 
29 CommandObjectApropos::CommandObjectApropos (CommandInterpreter &interpreter) :
30     CommandObject (interpreter,
31                    "apropos",
32                    "Find a list of debugger commands related to a particular word/subject.",
33                    NULL)
34 {
35     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);
47 }
48 
49 CommandObjectApropos::~CommandObjectApropos()
50 {
51 }
52 
53 
54 bool
55 CommandObjectApropos::Execute
56 (
57     Args& args,
58     CommandReturnObject &result
59 )
60 {
61     const int argc = args.GetArgumentCount ();
62 
63     if (argc == 1)
64     {
65         const char *search_word = args.GetArgumentAtIndex(0);
66         if ((search_word != NULL)
67             && (strlen (search_word) > 0))
68         {
69             // The bulk of the work must be done inside the Command Interpreter, since the command dictionary
70             // is private.
71             StringList commands_found;
72             StringList commands_help;
73             m_interpreter.FindCommandsForApropos (search_word, commands_found, commands_help);
74             if (commands_found.GetSize() == 0)
75             {
76                 result.AppendMessageWithFormat ("No commands found pertaining to '%s'. Try 'help' to see a complete list of debugger commands.\n", search_word);
77             }
78             else
79             {
80                 result.AppendMessageWithFormat ("The following commands may relate to '%s':\n", search_word);
81                 size_t max_len = 0;
82 
83                 for (size_t i = 0; i < commands_found.GetSize(); ++i)
84                 {
85                     size_t len = strlen (commands_found.GetStringAtIndex (i));
86                     if (len > max_len)
87                         max_len = len;
88                 }
89 
90                 for (size_t i = 0; i < commands_found.GetSize(); ++i)
91                     m_interpreter.OutputFormattedHelpText (result.GetOutputStream(),
92                                                            commands_found.GetStringAtIndex(i),
93                                                            "--", commands_help.
94                                                            GetStringAtIndex(i),
95                                                            max_len);
96 
97             }
98 
99 
100             StreamString settings_search_results;
101             lldb::UserSettingsControllerSP root = Debugger::GetSettingsController ();
102             const char *settings_prefix = root->GetLevelName().GetCString();
103 
104             UserSettingsController::SearchAllSettingsDescriptions (m_interpreter,
105                                                                    root,
106                                                                    settings_prefix,
107                                                                    search_word,
108                                                                    settings_search_results);
109 
110             if (settings_search_results.GetSize() > 0)
111             {
112                 result.AppendMessageWithFormat ("\nThe following settings variables may relate to '%s': \n\n", search_word);
113                 result.AppendMessageWithFormat ("%s", settings_search_results.GetData());
114             }
115 
116             result.SetStatus (eReturnStatusSuccessFinishNoResult);
117         }
118         else
119         {
120             result.AppendError ("'' is not a valid search word.\n");
121             result.SetStatus (eReturnStatusFailed);
122         }
123     }
124     else
125     {
126         result.AppendError ("'apropos' must be called with exactly one argument.\n");
127         result.SetStatus (eReturnStatusFailed);
128     }
129 
130     return result.Succeeded();
131 }
132