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 #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
44 (
45     CommandInterpreter &interpreter,
46     Args& args,
47     CommandReturnObject &result
48 )
49 {
50     const int argc = args.GetArgumentCount ();
51 
52     if (argc == 1)
53     {
54         const char *search_word = args.GetArgumentAtIndex(0);
55         if ((search_word != NULL)
56             && (strlen (search_word) > 0))
57         {
58             // The bulk of the work must be done inside the Command Interpreter, since the command dictionary
59             // is private.
60             StringList commands_found;
61             StringList commands_help;
62             interpreter.FindCommandsForApropos (search_word, commands_found, commands_help);
63             if (commands_found.GetSize() == 0)
64             {
65                 result.AppendMessageWithFormat ("No commands found pertaining to '%s'.", search_word);
66                 result.AppendMessage ("Try 'help' to see a complete list of debugger commands.");
67             }
68             else
69             {
70                 result.AppendMessageWithFormat ("The following commands may relate to '%s':\n", search_word);
71                 size_t max_len = 0;
72 
73                 for (int i = 0; i < commands_found.GetSize(); ++i)
74                 {
75                     int len = strlen (commands_found.GetStringAtIndex (i));
76                     if (len > max_len)
77                         max_len = len;
78                 }
79 
80                 for (int i = 0; i < commands_found.GetSize(); ++i)
81                     interpreter.OutputFormattedHelpText (result.GetOutputStream(),
82                                                          commands_found.GetStringAtIndex(i),
83                                                          "--", commands_help.
84                                                          GetStringAtIndex(i),
85                                                          max_len);
86 
87             }
88             result.SetStatus (eReturnStatusSuccessFinishNoResult);
89         }
90         else
91         {
92             result.AppendError ("'' is not a valid search word.\n");
93             result.SetStatus (eReturnStatusFailed);
94         }
95     }
96     else
97     {
98         result.AppendError ("'apropos' must be called with exactly one argument.\n");
99         result.SetStatus (eReturnStatusFailed);
100     }
101 
102     return result.Succeeded();
103 }
104