Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandObjectSyntax.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 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 10 | // C Includes |
| 11 | // C++ Includes |
| 12 | // Other libraries and framework includes |
| 13 | // Project includes |
Eugene Zelenko | 26cac3a | 2016-02-20 00:58:29 +0000 | [diff] [blame] | 14 | #include "CommandObjectSyntax.h" |
Enrico Granata | 46d4aa2 | 2016-02-29 23:22:53 +0000 | [diff] [blame] | 15 | #include "CommandObjectHelp.h" |
Jim Ingham | 40af72e | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 16 | #include "lldb/Interpreter/Args.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Interpreter/CommandInterpreter.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "lldb/Interpreter/CommandObjectMultiword.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 19 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 20 | #include "lldb/Interpreter/Options.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 | // CommandObjectSyntax |
| 27 | //------------------------------------------------------------------------- |
| 28 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | CommandObjectSyntax::CommandObjectSyntax(CommandInterpreter &interpreter) |
| 30 | : CommandObjectParsed( |
| 31 | interpreter, "syntax", |
| 32 | "Shows the correct syntax for a given debugger command.", |
| 33 | "syntax <command>") { |
| 34 | CommandArgumentEntry arg; |
| 35 | CommandArgumentData command_arg; |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 36 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | // Define the first (and only) variant of this arg. |
| 38 | command_arg.arg_type = eArgTypeCommandName; |
| 39 | command_arg.arg_repetition = eArgRepeatPlain; |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 40 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | // There is only one variant this argument could be; put it into the argument |
| 42 | // entry. |
| 43 | arg.push_back(command_arg); |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 44 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 45 | // Push the data for the first argument into the m_arguments vector. |
| 46 | m_arguments.push_back(arg); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Eugene Zelenko | 26cac3a | 2016-02-20 00:58:29 +0000 | [diff] [blame] | 49 | CommandObjectSyntax::~CommandObjectSyntax() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 50 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | bool CommandObjectSyntax::DoExecute(Args &command, |
| 52 | CommandReturnObject &result) { |
| 53 | CommandObject::CommandMap::iterator pos; |
| 54 | CommandObject *cmd_obj; |
| 55 | const size_t argc = command.GetArgumentCount(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 57 | if (argc > 0) { |
| 58 | cmd_obj = m_interpreter.GetCommandObject(command.GetArgumentAtIndex(0)); |
| 59 | bool all_okay = true; |
| 60 | for (size_t i = 1; i < argc; ++i) { |
| 61 | std::string sub_command = command.GetArgumentAtIndex(i); |
| 62 | if (!cmd_obj->IsMultiwordObject()) { |
| 63 | all_okay = false; |
| 64 | break; |
| 65 | } else { |
| 66 | cmd_obj = cmd_obj->GetSubcommandObject(sub_command.c_str()); |
| 67 | if (!cmd_obj) { |
| 68 | all_okay = false; |
| 69 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 71 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | if (all_okay && (cmd_obj != nullptr)) { |
| 75 | Stream &output_strm = result.GetOutputStream(); |
| 76 | if (cmd_obj->GetOptions() != nullptr) { |
| 77 | output_strm.Printf("\nSyntax: %s\n", cmd_obj->GetSyntax()); |
| 78 | output_strm.Printf( |
| 79 | "(Try 'help %s' for more information on command options syntax.)\n", |
Zachary Turner | a449698 | 2016-10-05 21:14:38 +0000 | [diff] [blame] | 80 | cmd_obj->GetCommandName().str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 81 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 82 | } else { |
| 83 | output_strm.Printf("\nSyntax: %s\n", cmd_obj->GetSyntax()); |
| 84 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 85 | } |
| 86 | } else { |
| 87 | std::string cmd_string; |
| 88 | command.GetCommandString(cmd_string); |
| 89 | |
| 90 | StreamString error_msg_stream; |
| 91 | const bool generate_apropos = true; |
| 92 | const bool generate_type_lookup = false; |
| 93 | CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage( |
| 94 | &error_msg_stream, cmd_string.c_str(), nullptr, nullptr, |
| 95 | generate_apropos, generate_type_lookup); |
| 96 | result.AppendErrorWithFormat("%s", error_msg_stream.GetData()); |
| 97 | result.SetStatus(eReturnStatusFailed); |
| 98 | } |
| 99 | } else { |
| 100 | result.AppendError("Must call 'syntax' with a valid command."); |
| 101 | result.SetStatus(eReturnStatusFailed); |
| 102 | } |
| 103 | |
| 104 | return result.Succeeded(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 105 | } |