blob: 338671b95712de3dd3ee23af2e9592213c21be34 [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;
Zachary Turner97d2c402016-10-05 23:40:23 +000060 // TODO: Convert to entry-based iteration. Requires converting
61 // GetSubcommandObject.
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 for (size_t i = 1; i < argc; ++i) {
63 std::string sub_command = command.GetArgumentAtIndex(i);
64 if (!cmd_obj->IsMultiwordObject()) {
65 all_okay = false;
66 break;
67 } else {
68 cmd_obj = cmd_obj->GetSubcommandObject(sub_command.c_str());
69 if (!cmd_obj) {
70 all_okay = false;
71 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074 }
75
Kate Stoneb9c1b512016-09-06 20:57:50 +000076 if (all_okay && (cmd_obj != nullptr)) {
77 Stream &output_strm = result.GetOutputStream();
78 if (cmd_obj->GetOptions() != nullptr) {
79 output_strm.Printf("\nSyntax: %s\n", cmd_obj->GetSyntax());
80 output_strm.Printf(
81 "(Try 'help %s' for more information on command options syntax.)\n",
Zachary Turnera4496982016-10-05 21:14:38 +000082 cmd_obj->GetCommandName().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 result.SetStatus(eReturnStatusSuccessFinishNoResult);
84 } else {
85 output_strm.Printf("\nSyntax: %s\n", cmd_obj->GetSyntax());
86 result.SetStatus(eReturnStatusSuccessFinishNoResult);
87 }
88 } else {
89 std::string cmd_string;
90 command.GetCommandString(cmd_string);
91
92 StreamString error_msg_stream;
93 const bool generate_apropos = true;
94 const bool generate_type_lookup = false;
95 CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage(
96 &error_msg_stream, cmd_string.c_str(), nullptr, nullptr,
97 generate_apropos, generate_type_lookup);
98 result.AppendErrorWithFormat("%s", error_msg_stream.GetData());
99 result.SetStatus(eReturnStatusFailed);
100 }
101 } else {
102 result.AppendError("Must call 'syntax' with a valid command.");
103 result.SetStatus(eReturnStatusFailed);
104 }
105
106 return result.Succeeded();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107}