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 | |
| 28 | CommandObjectHelp::CommandObjectHelp () : |
| 29 | CommandObject ("help", |
Caroline Tice | c1ad82e | 2010-09-07 22:38:08 +0000 | [diff] [blame^] | 30 | "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] | 31 | "help [<cmd-name>]") |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | { |
| 33 | } |
| 34 | |
| 35 | CommandObjectHelp::~CommandObjectHelp() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | |
| 40 | bool |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 41 | CommandObjectHelp::Execute (CommandInterpreter &interpreter, Args& command, CommandReturnObject &result) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | { |
| 43 | CommandObject::CommandMap::iterator pos; |
| 44 | CommandObject *cmd_obj; |
| 45 | const int argc = command.GetArgumentCount (); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | // 'help' doesn't take any options or arguments, other than command names. If argc is 0, we show the user |
| 48 | // 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] | 49 | if (argc == 0) |
| 50 | { |
| 51 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 52 | interpreter.GetHelp (result); // General help, for ALL commands. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | } |
| 54 | else |
| 55 | { |
| 56 | // 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] | 57 | StringList matches; |
| 58 | cmd_obj = interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 60 | if (cmd_obj != NULL) |
| 61 | { |
| 62 | bool all_okay = true; |
| 63 | CommandObject *sub_cmd_obj = cmd_obj; |
| 64 | // Loop down through sub_command dictionaries until we find the command object that corresponds |
| 65 | // to the help command entered. |
| 66 | for (int i = 1; i < argc && all_okay; ++i) |
| 67 | { |
| 68 | std::string sub_command = command.GetArgumentAtIndex(i); |
| 69 | if (! sub_cmd_obj->IsMultiwordObject ()) |
| 70 | { |
| 71 | all_okay = false; |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | pos = ((CommandObjectMultiword *) sub_cmd_obj)->m_subcommand_dict.find (sub_command); |
| 76 | if (pos != ((CommandObjectMultiword *) sub_cmd_obj)->m_subcommand_dict.end()) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 77 | sub_cmd_obj = pos->second.get(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 78 | else |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 79 | all_okay = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 83 | if (!all_okay || (sub_cmd_obj == NULL)) |
| 84 | { |
| 85 | std::string cmd_string; |
| 86 | command.GetCommandString (cmd_string); |
| 87 | result.AppendErrorWithFormat |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 88 | ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n", |
| 89 | cmd_string.c_str()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 90 | result.SetStatus (eReturnStatusFailed); |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | Stream &output_strm = result.GetOutputStream(); |
| 95 | if (sub_cmd_obj->GetOptions() != NULL) |
| 96 | { |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 97 | interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax()); |
| 99 | sub_cmd_obj->GetOptions()->GenerateOptionUsage (output_strm, sub_cmd_obj); |
| 100 | const char *long_help = sub_cmd_obj->GetHelpLong(); |
| 101 | if ((long_help != NULL) |
| 102 | && (strlen (long_help) > 0)) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 103 | output_strm.Printf ("\n%s", long_help); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | } |
| 105 | else if (sub_cmd_obj->IsMultiwordObject()) |
| 106 | { |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 107 | interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 108 | ((CommandObjectMultiword *) sub_cmd_obj)->GenerateHelpText (interpreter, result); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | } |
| 110 | else |
| 111 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 112 | const char *long_help = sub_cmd_obj->GetHelpLong(); |
| 113 | if ((long_help != NULL) |
| 114 | && (strlen (long_help) > 0)) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 115 | interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelpLong(), 1); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 116 | else |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 117 | interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 118 | output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | } |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 122 | else if (matches.GetSize() > 0) |
| 123 | { |
| 124 | Stream &output_strm = result.GetOutputStream(); |
| 125 | output_strm.Printf("Help requested with ambiguous command name, possible completions:\n"); |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 126 | const uint32_t match_count = matches.GetSize(); |
| 127 | for (uint32_t i = 0; i < match_count; i++) |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 128 | { |
| 129 | output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i)); |
| 130 | } |
| 131 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 132 | else |
| 133 | { |
| 134 | result.AppendErrorWithFormat |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 135 | ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n", |
| 136 | command.GetArgumentAtIndex(0)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 137 | result.SetStatus (eReturnStatusFailed); |
| 138 | } |
| 139 | } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 141 | return result.Succeeded(); |
| 142 | } |
| 143 | |
| 144 | int |
| 145 | CommandObjectHelp::HandleCompletion |
| 146 | ( |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 147 | CommandInterpreter &interpreter, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | { |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 160 | return interpreter.HandleCompletionMatches(input, cursor_index, cursor_char_position, match_start_point, |
| 161 | max_return_elements, word_complete, matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 162 | } |
| 163 | else |
| 164 | { |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 165 | CommandObject *cmd_obj = interpreter.GetCommandObject (input.GetArgumentAtIndex(0)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 166 | input.Shift(); |
| 167 | cursor_index--; |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 168 | return cmd_obj->HandleCompletion (interpreter, input, cursor_index, cursor_char_position, match_start_point, |
| 169 | max_return_elements, word_complete, matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 170 | } |
| 171 | } |