]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Commands/CommandObjectApropos.cpp
Import libxo-1.0.2
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Commands / CommandObjectApropos.cpp
1 //===-- CommandObjectApropos.cpp ---------------------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #include "CommandObjectApropos.h"
12 #include "lldb/Interpreter/CommandInterpreter.h"
13 #include "lldb/Interpreter/CommandReturnObject.h"
14 #include "lldb/Interpreter/Options.h"
15 #include "lldb/Interpreter/Property.h"
16 #include "lldb/Utility/Args.h"
17
18 using namespace lldb;
19 using namespace lldb_private;
20
21 //-------------------------------------------------------------------------
22 // CommandObjectApropos
23 //-------------------------------------------------------------------------
24
25 CommandObjectApropos::CommandObjectApropos(CommandInterpreter &interpreter)
26     : CommandObjectParsed(
27           interpreter, "apropos",
28           "List debugger commands related to a word or subject.", nullptr) {
29   CommandArgumentEntry arg;
30   CommandArgumentData search_word_arg;
31
32   // Define the first (and only) variant of this arg.
33   search_word_arg.arg_type = eArgTypeSearchWord;
34   search_word_arg.arg_repetition = eArgRepeatPlain;
35
36   // There is only one variant this argument could be; put it into the argument
37   // entry.
38   arg.push_back(search_word_arg);
39
40   // Push the data for the first argument into the m_arguments vector.
41   m_arguments.push_back(arg);
42 }
43
44 CommandObjectApropos::~CommandObjectApropos() = default;
45
46 bool CommandObjectApropos::DoExecute(Args &args, CommandReturnObject &result) {
47   const size_t argc = args.GetArgumentCount();
48
49   if (argc == 1) {
50     auto search_word = args[0].ref;
51     if (!search_word.empty()) {
52       // The bulk of the work must be done inside the Command Interpreter,
53       // since the command dictionary is private.
54       StringList commands_found;
55       StringList commands_help;
56
57       m_interpreter.FindCommandsForApropos(search_word, commands_found,
58                                            commands_help, true, true, true);
59
60       if (commands_found.GetSize() == 0) {
61         result.AppendMessageWithFormat("No commands found pertaining to '%s'. "
62                                        "Try 'help' to see a complete list of "
63                                        "debugger commands.\n",
64                                        args[0].c_str());
65       } else {
66         if (commands_found.GetSize() > 0) {
67           result.AppendMessageWithFormat(
68               "The following commands may relate to '%s':\n", args[0].c_str());
69           size_t max_len = 0;
70
71           for (size_t i = 0; i < commands_found.GetSize(); ++i) {
72             size_t len = strlen(commands_found.GetStringAtIndex(i));
73             if (len > max_len)
74               max_len = len;
75           }
76
77           for (size_t i = 0; i < commands_found.GetSize(); ++i)
78             m_interpreter.OutputFormattedHelpText(
79                 result.GetOutputStream(), commands_found.GetStringAtIndex(i),
80                 "--", commands_help.GetStringAtIndex(i), max_len);
81         }
82       }
83
84       std::vector<const Property *> properties;
85       const size_t num_properties =
86           m_interpreter.GetDebugger().Apropos(search_word, properties);
87       if (num_properties) {
88         const bool dump_qualified_name = true;
89         result.AppendMessageWithFormatv(
90             "\nThe following settings variables may relate to '{0}': \n\n",
91             args[0].ref);
92         for (size_t i = 0; i < num_properties; ++i)
93           properties[i]->DumpDescription(
94               m_interpreter, result.GetOutputStream(), 0, dump_qualified_name);
95       }
96
97       result.SetStatus(eReturnStatusSuccessFinishNoResult);
98     } else {
99       result.AppendError("'' is not a valid search word.\n");
100       result.SetStatus(eReturnStatusFailed);
101     }
102   } else {
103     result.AppendError("'apropos' must be called with exactly one argument.\n");
104     result.SetStatus(eReturnStatusFailed);
105   }
106
107   return result.Succeeded();
108 }