Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandObjectHelp.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 "CommandObjectHelp.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
| 16 | #include "lldb/Interpreter/CommandObjectMultiword.h" |
| 17 | #include "lldb/Interpreter/CommandInterpreter.h" |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 18 | #include "lldb/Interpreter/Options.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 20 | |
| 21 | using namespace lldb; |
| 22 | using namespace lldb_private; |
| 23 | |
| 24 | //------------------------------------------------------------------------- |
| 25 | // CommandObjectHelp |
| 26 | //------------------------------------------------------------------------- |
| 27 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame^] | 28 | CommandObjectHelp::CommandObjectHelp (CommandInterpreter &interpreter) : |
| 29 | CommandObject (interpreter, |
| 30 | "help", |
Caroline Tice | c1ad82e | 2010-09-07 22:38:08 +0000 | [diff] [blame] | 31 | "Show a list of all debugger commands, or give details about specific commands.", |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 32 | "help [<cmd-name>]") |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | { |
| 34 | } |
| 35 | |
| 36 | CommandObjectHelp::~CommandObjectHelp() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | |
| 41 | bool |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame^] | 42 | CommandObjectHelp::Execute (Args& command, CommandReturnObject &result) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | { |
| 44 | CommandObject::CommandMap::iterator pos; |
| 45 | CommandObject *cmd_obj; |
| 46 | const int argc = command.GetArgumentCount (); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | // 'help' doesn't take any options or arguments, other than command names. If argc is 0, we show the user |
| 49 | // all commands and aliases. Otherwise every argument must be the name of a command or a sub-command. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 50 | if (argc == 0) |
| 51 | { |
| 52 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame^] | 53 | m_interpreter.GetHelp (result); // General help, for ALL commands. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | } |
| 55 | else |
| 56 | { |
| 57 | // Get command object for the first command argument. Only search built-in command dictionary. |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 58 | StringList matches; |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame^] | 59 | cmd_obj = m_interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | if (cmd_obj != NULL) |
| 62 | { |
| 63 | bool all_okay = true; |
| 64 | CommandObject *sub_cmd_obj = cmd_obj; |
| 65 | // Loop down through sub_command dictionaries until we find the command object that corresponds |
| 66 | // to the help command entered. |
| 67 | for (int i = 1; i < argc && all_okay; ++i) |
| 68 | { |
| 69 | std::string sub_command = command.GetArgumentAtIndex(i); |
| 70 | if (! sub_cmd_obj->IsMultiwordObject ()) |
| 71 | { |
| 72 | all_okay = false; |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | pos = ((CommandObjectMultiword *) sub_cmd_obj)->m_subcommand_dict.find (sub_command); |
| 77 | if (pos != ((CommandObjectMultiword *) sub_cmd_obj)->m_subcommand_dict.end()) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 78 | sub_cmd_obj = pos->second.get(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 79 | else |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 80 | all_okay = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | if (!all_okay || (sub_cmd_obj == NULL)) |
| 85 | { |
| 86 | std::string cmd_string; |
| 87 | command.GetCommandString (cmd_string); |
| 88 | result.AppendErrorWithFormat |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 89 | ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n", |
| 90 | cmd_string.c_str()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | result.SetStatus (eReturnStatusFailed); |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | Stream &output_strm = result.GetOutputStream(); |
| 96 | if (sub_cmd_obj->GetOptions() != NULL) |
| 97 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame^] | 98 | m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 99 | output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax()); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame^] | 100 | sub_cmd_obj->GetOptions()->GenerateOptionUsage (m_interpreter, output_strm, sub_cmd_obj); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 101 | const char *long_help = sub_cmd_obj->GetHelpLong(); |
| 102 | if ((long_help != NULL) |
| 103 | && (strlen (long_help) > 0)) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 104 | output_strm.Printf ("\n%s", long_help); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 105 | } |
| 106 | else if (sub_cmd_obj->IsMultiwordObject()) |
| 107 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame^] | 108 | m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); |
| 109 | ((CommandObjectMultiword *) sub_cmd_obj)->GenerateHelpText (result); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 110 | } |
| 111 | else |
| 112 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 113 | const char *long_help = sub_cmd_obj->GetHelpLong(); |
| 114 | if ((long_help != NULL) |
| 115 | && (strlen (long_help) > 0)) |
Caroline Tice | abb507a | 2010-09-08 21:06:11 +0000 | [diff] [blame] | 116 | output_strm.Printf ("\n%s", long_help); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 117 | else |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame^] | 118 | m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 119 | output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 123 | else if (matches.GetSize() > 0) |
| 124 | { |
| 125 | Stream &output_strm = result.GetOutputStream(); |
| 126 | output_strm.Printf("Help requested with ambiguous command name, possible completions:\n"); |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 127 | const uint32_t match_count = matches.GetSize(); |
| 128 | for (uint32_t i = 0; i < match_count; i++) |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 129 | { |
| 130 | output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i)); |
| 131 | } |
| 132 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 133 | else |
| 134 | { |
| 135 | result.AppendErrorWithFormat |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 136 | ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n", |
| 137 | command.GetArgumentAtIndex(0)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 138 | result.SetStatus (eReturnStatusFailed); |
| 139 | } |
| 140 | } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | return result.Succeeded(); |
| 143 | } |
| 144 | |
| 145 | int |
| 146 | CommandObjectHelp::HandleCompletion |
| 147 | ( |
| 148 | Args &input, |
| 149 | int &cursor_index, |
| 150 | int &cursor_char_position, |
| 151 | int match_start_point, |
| 152 | int max_return_elements, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 153 | bool &word_complete, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 154 | StringList &matches |
| 155 | ) |
| 156 | { |
| 157 | // Return the completions of the commands in the help system: |
| 158 | if (cursor_index == 0) |
| 159 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame^] | 160 | return m_interpreter.HandleCompletionMatches (input, |
| 161 | cursor_index, |
| 162 | cursor_char_position, |
| 163 | match_start_point, |
| 164 | max_return_elements, |
| 165 | word_complete, |
| 166 | matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 167 | } |
| 168 | else |
| 169 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame^] | 170 | CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 171 | input.Shift(); |
| 172 | cursor_index--; |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame^] | 173 | return cmd_obj->HandleCompletion (input, |
| 174 | cursor_index, |
| 175 | cursor_char_position, |
| 176 | match_start_point, |
| 177 | max_return_elements, |
| 178 | word_complete, |
| 179 | matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | } |
| 181 | } |