Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1 | //===-- CommandObjectApropos.cpp ---------------------------------*- C++ |
| 2 | //-*-===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | // C Includes |
| 12 | // C++ Includes |
| 13 | // Other libraries and framework includes |
| 14 | // Project includes |
Eugene Zelenko | c8ecc2a | 2016-02-19 19:33:46 +0000 | [diff] [blame] | 15 | #include "CommandObjectApropos.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 17 | #include "lldb/Interpreter/CommandReturnObject.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 18 | #include "lldb/Interpreter/Options.h" |
| 19 | #include "lldb/Interpreter/Property.h" |
Pavel Labath | 145d95c | 2018-04-17 18:53:35 +0000 | [diff] [blame] | 20 | #include "lldb/Utility/Args.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace lldb; |
| 23 | using namespace lldb_private; |
| 24 | |
| 25 | //------------------------------------------------------------------------- |
| 26 | // CommandObjectApropos |
| 27 | //------------------------------------------------------------------------- |
| 28 | |
Kate Stone | 7428a18 | 2016-07-14 22:03:10 +0000 | [diff] [blame] | 29 | CommandObjectApropos::CommandObjectApropos(CommandInterpreter &interpreter) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 30 | : CommandObjectParsed( |
| 31 | interpreter, "apropos", |
| 32 | "List debugger commands related to a word or subject.", nullptr) { |
| 33 | CommandArgumentEntry arg; |
| 34 | CommandArgumentData search_word_arg; |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 35 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | // Define the first (and only) variant of this arg. |
| 37 | search_word_arg.arg_type = eArgTypeSearchWord; |
| 38 | search_word_arg.arg_repetition = eArgRepeatPlain; |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 39 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | // There is only one variant this argument could be; put it into the argument |
| 41 | // entry. |
| 42 | arg.push_back(search_word_arg); |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 43 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 44 | // Push the data for the first argument into the m_arguments vector. |
| 45 | m_arguments.push_back(arg); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Eugene Zelenko | c8ecc2a | 2016-02-19 19:33:46 +0000 | [diff] [blame] | 48 | CommandObjectApropos::~CommandObjectApropos() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | bool CommandObjectApropos::DoExecute(Args &args, CommandReturnObject &result) { |
| 51 | const size_t argc = args.GetArgumentCount(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | if (argc == 1) { |
Zachary Turner | 867e7d1 | 2016-12-09 01:20:58 +0000 | [diff] [blame] | 54 | auto search_word = args[0].ref; |
| 55 | if (!search_word.empty()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 56 | // The bulk of the work must be done inside the Command Interpreter, |
| 57 | // since the command dictionary is private. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 58 | StringList commands_found; |
| 59 | StringList commands_help; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 60 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | m_interpreter.FindCommandsForApropos(search_word, commands_found, |
| 62 | commands_help, true, true, true); |
Jim Ingham | af3753e | 2013-05-17 01:30:37 +0000 | [diff] [blame] | 63 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | if (commands_found.GetSize() == 0) { |
| 65 | result.AppendMessageWithFormat("No commands found pertaining to '%s'. " |
| 66 | "Try 'help' to see a complete list of " |
| 67 | "debugger commands.\n", |
Zachary Turner | 867e7d1 | 2016-12-09 01:20:58 +0000 | [diff] [blame] | 68 | args[0].c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | } else { |
| 70 | if (commands_found.GetSize() > 0) { |
| 71 | result.AppendMessageWithFormat( |
Zachary Turner | 867e7d1 | 2016-12-09 01:20:58 +0000 | [diff] [blame] | 72 | "The following commands may relate to '%s':\n", args[0].c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | size_t max_len = 0; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 74 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | for (size_t i = 0; i < commands_found.GetSize(); ++i) { |
| 76 | size_t len = strlen(commands_found.GetStringAtIndex(i)); |
| 77 | if (len > max_len) |
| 78 | max_len = len; |
| 79 | } |
| 80 | |
| 81 | for (size_t i = 0; i < commands_found.GetSize(); ++i) |
| 82 | m_interpreter.OutputFormattedHelpText( |
| 83 | result.GetOutputStream(), commands_found.GetStringAtIndex(i), |
| 84 | "--", commands_help.GetStringAtIndex(i), max_len); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 85 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 87 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 88 | std::vector<const Property *> properties; |
| 89 | const size_t num_properties = |
| 90 | m_interpreter.GetDebugger().Apropos(search_word, properties); |
| 91 | if (num_properties) { |
| 92 | const bool dump_qualified_name = true; |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 93 | result.AppendMessageWithFormatv( |
| 94 | "\nThe following settings variables may relate to '{0}': \n\n", |
| 95 | args[0].ref); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | for (size_t i = 0; i < num_properties; ++i) |
| 97 | properties[i]->DumpDescription( |
| 98 | m_interpreter, result.GetOutputStream(), 0, dump_qualified_name); |
| 99 | } |
| 100 | |
| 101 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 102 | } else { |
| 103 | result.AppendError("'' is not a valid search word.\n"); |
| 104 | result.SetStatus(eReturnStatusFailed); |
| 105 | } |
| 106 | } else { |
| 107 | result.AppendError("'apropos' must be called with exactly one argument.\n"); |
| 108 | result.SetStatus(eReturnStatusFailed); |
| 109 | } |
| 110 | |
| 111 | return result.Succeeded(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 112 | } |