1254721Semaste//===-- CommandObjectApropos.cpp ---------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#include "lldb/lldb-python.h"
11254721Semaste
12254721Semaste#include "CommandObjectApropos.h"
13254721Semaste
14254721Semaste// C Includes
15254721Semaste// C++ Includes
16254721Semaste// Other libraries and framework includes
17254721Semaste// Project includes
18254721Semaste#include "lldb/Interpreter/Args.h"
19254721Semaste#include "lldb/Interpreter/Options.h"
20254721Semaste
21254721Semaste#include "lldb/Interpreter/CommandInterpreter.h"
22254721Semaste#include "lldb/Interpreter/CommandReturnObject.h"
23254721Semaste
24254721Semasteusing namespace lldb;
25254721Semasteusing namespace lldb_private;
26254721Semaste
27254721Semaste//-------------------------------------------------------------------------
28254721Semaste// CommandObjectApropos
29254721Semaste//-------------------------------------------------------------------------
30254721Semaste
31254721SemasteCommandObjectApropos::CommandObjectApropos (CommandInterpreter &interpreter) :
32254721Semaste    CommandObjectParsed (interpreter,
33254721Semaste                         "apropos",
34254721Semaste                         "Find a list of debugger commands related to a particular word/subject.",
35254721Semaste                         NULL)
36254721Semaste{
37254721Semaste    CommandArgumentEntry arg;
38254721Semaste    CommandArgumentData search_word_arg;
39254721Semaste
40254721Semaste    // Define the first (and only) variant of this arg.
41254721Semaste    search_word_arg.arg_type = eArgTypeSearchWord;
42254721Semaste    search_word_arg.arg_repetition = eArgRepeatPlain;
43254721Semaste
44254721Semaste    // There is only one variant this argument could be; put it into the argument entry.
45254721Semaste    arg.push_back (search_word_arg);
46254721Semaste
47254721Semaste    // Push the data for the first argument into the m_arguments vector.
48254721Semaste    m_arguments.push_back (arg);
49254721Semaste}
50254721Semaste
51254721SemasteCommandObjectApropos::~CommandObjectApropos()
52254721Semaste{
53254721Semaste}
54254721Semaste
55254721Semaste
56254721Semastebool
57254721SemasteCommandObjectApropos::DoExecute (Args& args, CommandReturnObject &result)
58254721Semaste{
59254721Semaste    const size_t argc = args.GetArgumentCount ();
60254721Semaste
61254721Semaste    if (argc == 1)
62254721Semaste    {
63254721Semaste        const char *search_word = args.GetArgumentAtIndex(0);
64254721Semaste        if ((search_word != NULL)
65254721Semaste            && (strlen (search_word) > 0))
66254721Semaste        {
67254721Semaste            // The bulk of the work must be done inside the Command Interpreter, since the command dictionary
68254721Semaste            // is private.
69254721Semaste            StringList commands_found;
70254721Semaste            StringList commands_help;
71254721Semaste            StringList user_commands_found;
72254721Semaste            StringList user_commands_help;
73254721Semaste
74254721Semaste            m_interpreter.FindCommandsForApropos (search_word, commands_found, commands_help, true, false);
75254721Semaste            m_interpreter.FindCommandsForApropos (search_word, user_commands_found, user_commands_help, false, true);
76254721Semaste
77254721Semaste            if (commands_found.GetSize() == 0 && user_commands_found.GetSize() == 0)
78254721Semaste            {
79254721Semaste                result.AppendMessageWithFormat ("No commands found pertaining to '%s'. Try 'help' to see a complete list of debugger commands.\n", search_word);
80254721Semaste            }
81254721Semaste            else
82254721Semaste            {
83254721Semaste                if (commands_found.GetSize() > 0)
84254721Semaste                {
85254721Semaste                    result.AppendMessageWithFormat ("The following built-in commands may relate to '%s':\n", search_word);
86254721Semaste                    size_t max_len = 0;
87254721Semaste
88254721Semaste                    for (size_t i = 0; i < commands_found.GetSize(); ++i)
89254721Semaste                    {
90254721Semaste                        size_t len = strlen (commands_found.GetStringAtIndex (i));
91254721Semaste                        if (len > max_len)
92254721Semaste                            max_len = len;
93254721Semaste                    }
94254721Semaste
95254721Semaste                    for (size_t i = 0; i < commands_found.GetSize(); ++i)
96254721Semaste                        m_interpreter.OutputFormattedHelpText (result.GetOutputStream(),
97254721Semaste                                                               commands_found.GetStringAtIndex(i),
98254721Semaste                                                               "--",
99254721Semaste                                                               commands_help.GetStringAtIndex(i),
100254721Semaste                                                               max_len);
101254721Semaste                    if (user_commands_found.GetSize() > 0)
102254721Semaste                        result.AppendMessage("");
103254721Semaste                }
104254721Semaste
105254721Semaste                if (user_commands_found.GetSize() > 0)
106254721Semaste                {
107254721Semaste                    result.AppendMessageWithFormat ("The following user commands may relate to '%s':\n", search_word);
108254721Semaste                    size_t max_len = 0;
109254721Semaste
110254721Semaste                    for (size_t i = 0; i < user_commands_found.GetSize(); ++i)
111254721Semaste                    {
112254721Semaste                        size_t len = strlen (user_commands_found.GetStringAtIndex (i));
113254721Semaste                        if (len > max_len)
114254721Semaste                            max_len = len;
115254721Semaste                    }
116254721Semaste
117254721Semaste                    for (size_t i = 0; i < user_commands_found.GetSize(); ++i)
118254721Semaste                        m_interpreter.OutputFormattedHelpText (result.GetOutputStream(),
119254721Semaste                                                               user_commands_found.GetStringAtIndex(i),
120254721Semaste                                                               "--",
121254721Semaste                                                               user_commands_help.GetStringAtIndex(i),
122254721Semaste                                                               max_len);
123254721Semaste                }
124254721Semaste
125254721Semaste            }
126254721Semaste
127254721Semaste
128254721Semaste            std::vector<const Property *> properties;
129254721Semaste            const size_t num_properties = m_interpreter.GetDebugger().Apropos(search_word, properties);
130254721Semaste            if (num_properties)
131254721Semaste            {
132254721Semaste                const bool dump_qualified_name = true;
133254721Semaste                result.AppendMessageWithFormat ("\nThe following settings variables may relate to '%s': \n\n", search_word);
134254721Semaste                for (size_t i=0; i<num_properties; ++i)
135254721Semaste                    properties[i]->DumpDescription (m_interpreter, result.GetOutputStream(), 0, dump_qualified_name);
136254721Semaste
137254721Semaste            }
138254721Semaste
139254721Semaste            result.SetStatus (eReturnStatusSuccessFinishNoResult);
140254721Semaste        }
141254721Semaste        else
142254721Semaste        {
143254721Semaste            result.AppendError ("'' is not a valid search word.\n");
144254721Semaste            result.SetStatus (eReturnStatusFailed);
145254721Semaste        }
146254721Semaste    }
147254721Semaste    else
148254721Semaste    {
149254721Semaste        result.AppendError ("'apropos' must be called with exactly one argument.\n");
150254721Semaste        result.SetStatus (eReturnStatusFailed);
151254721Semaste    }
152254721Semaste
153254721Semaste    return result.Succeeded();
154254721Semaste}
155