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