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/Core/Args.h"
17 #include "lldb/Core/Options.h"
18 
19 #include "lldb/Interpreter/CommandInterpreter.h"
20 #include "lldb/Interpreter/CommandReturnObject.h"
21 #include "lldb/Interpreter/CommandObjectMultiword.h"
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 //-------------------------------------------------------------------------
27 // CommandObjectApropos
28 //-------------------------------------------------------------------------
29 
30 CommandObjectApropos::CommandObjectApropos () :
31     CommandObject ("apropos",
32                      "Finds a list of debugger commands related to a particular word/subject.",
33                      "apropos <search-word>")
34 {
35 }
36 
37 CommandObjectApropos::~CommandObjectApropos()
38 {
39 }
40 
41 
42 bool
43 CommandObjectApropos::Execute (Args &command, CommandContext *context, CommandInterpreter *interpreter,
44                               CommandReturnObject &result)
45 {
46     const int argc = command.GetArgumentCount ();
47 
48     if (argc == 1)
49     {
50         const char *search_word = command.GetArgumentAtIndex(0);
51         if ((search_word != NULL)
52             && (strlen (search_word) > 0))
53         {
54             // The bulk of the work must be done inside the Command Interpreter, since the command dictionary
55             // is private.
56             StringList commands_found;
57             StringList commands_help;
58             interpreter->FindCommandsForApropos (search_word, commands_found, commands_help);
59             if (commands_found.GetSize() == 0)
60             {
61                 result.AppendMessageWithFormat ("No commands found pertaining to '%s'.", search_word);
62                 result.AppendMessage ("Try 'help' to see a complete list of debugger commands.");
63             }
64             else
65             {
66                 result.AppendMessageWithFormat ("The following commands may relate to '%s':\n", search_word);
67                 size_t max_len = 0;
68 
69                 for (int i = 0; i < commands_found.GetSize(); ++i)
70                 {
71                     int len = strlen (commands_found.GetStringAtIndex (i));
72                     if (len > max_len)
73                         max_len = len;
74                 }
75 
76                 for (int i = 0; i < commands_found.GetSize(); ++i)
77                     interpreter->OutputFormattedHelpText (result.GetOutputStream(), commands_found.GetStringAtIndex(i),
78                                                          "--", commands_help.GetStringAtIndex(i), max_len);
79 
80             }
81             result.SetStatus (eReturnStatusSuccessFinishNoResult);
82         }
83         else
84         {
85             result.AppendError ("'' is not a valid search word.\n");
86             result.SetStatus (eReturnStatusFailed);
87         }
88     }
89     else
90     {
91         result.AppendError ("'apropos' must be called with exactly one argument.\n");
92         result.SetStatus (eReturnStatusFailed);
93     }
94 
95     return result.Succeeded();
96 }
97