blob: 67dee8e74130140645b554e862c1212cafa95f72 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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 Lattner30fdc8d2010-06-08 16:52:24 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Eugene Zelenko26cac3a2016-02-20 00:58:29 +000014#include "CommandObjectSyntax.h"
Enrico Granata46d4aa22016-02-29 23:22:53 +000015#include "CommandObjectHelp.h"
Jim Ingham40af72e2010-06-15 19:49:27 +000016#include "lldb/Interpreter/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Interpreter/CommandObjectMultiword.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000019#include "lldb/Interpreter/CommandReturnObject.h"
20#include "lldb/Interpreter/Options.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021
22using namespace lldb;
23using namespace lldb_private;
24
25//-------------------------------------------------------------------------
26// CommandObjectSyntax
27//-------------------------------------------------------------------------
28
Kate Stoneb9c1b512016-09-06 20:57:50 +000029CommandObjectSyntax::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 Tice405fe672010-10-04 22:28:36 +000036
Kate Stoneb9c1b512016-09-06 20:57:50 +000037 // Define the first (and only) variant of this arg.
38 command_arg.arg_type = eArgTypeCommandName;
39 command_arg.arg_repetition = eArgRepeatPlain;
Caroline Tice405fe672010-10-04 22:28:36 +000040
Kate Stoneb9c1b512016-09-06 20:57:50 +000041 // There is only one variant this argument could be; put it into the argument
42 // entry.
43 arg.push_back(command_arg);
Caroline Tice405fe672010-10-04 22:28:36 +000044
Kate Stoneb9c1b512016-09-06 20:57:50 +000045 // Push the data for the first argument into the m_arguments vector.
46 m_arguments.push_back(arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047}
48
Eugene Zelenko26cac3a2016-02-20 00:58:29 +000049CommandObjectSyntax::~CommandObjectSyntax() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050
Kate Stoneb9c1b512016-09-06 20:57:50 +000051bool CommandObjectSyntax::DoExecute(Args &command,
52 CommandReturnObject &result) {
53 CommandObject::CommandMap::iterator pos;
54 CommandObject *cmd_obj;
55 const size_t argc = command.GetArgumentCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056
Kate Stoneb9c1b512016-09-06 20:57:50 +000057 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 Lattner30fdc8d2010-06-08 16:52:24 +000070 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072 }
73
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 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 Turnera4496982016-10-05 21:14:38 +000080 cmd_obj->GetCommandName().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 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 Lattner30fdc8d2010-06-08 16:52:24 +0000105}