1 //===-- SBCommandInterpreter.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 "lldb/lldb-types.h"
11 #include "lldb/Interpreter/Args.h"
12 #include "lldb/Core/SourceManager.h"
13 #include "lldb/Core/Listener.h"
14 #include "lldb/Interpreter/CommandInterpreter.h"
15 #include "lldb/Interpreter/CommandReturnObject.h"
16 #include "lldb/Target/Target.h"
17 
18 #include "lldb/API/SBBroadcaster.h"
19 #include "lldb/API/SBDebugger.h"
20 #include "lldb/API/SBCommandReturnObject.h"
21 #include "lldb/API/SBSourceManager.h"
22 #include "lldb/API/SBCommandInterpreter.h"
23 #include "lldb/API/SBProcess.h"
24 #include "lldb/API/SBTarget.h"
25 #include "lldb/API/SBListener.h"
26 #include "lldb/API/SBStringList.h"
27 
28 using namespace lldb;
29 using namespace lldb_private;
30 
31 
32 SBCommandInterpreter::SBCommandInterpreter (CommandInterpreter *interpreter) :
33     m_opaque_ptr (interpreter)
34 {
35 }
36 
37 SBCommandInterpreter::~SBCommandInterpreter ()
38 {
39 }
40 
41 bool
42 SBCommandInterpreter::IsValid() const
43 {
44     return m_opaque_ptr != NULL;
45 }
46 
47 
48 bool
49 SBCommandInterpreter::CommandExists (const char *cmd)
50 {
51     if (m_opaque_ptr)
52         return m_opaque_ptr->CommandExists (cmd);
53     return false;
54 }
55 
56 bool
57 SBCommandInterpreter::AliasExists (const char *cmd)
58 {
59     if (m_opaque_ptr)
60         return m_opaque_ptr->AliasExists (cmd);
61     return false;
62 }
63 
64 lldb::ReturnStatus
65 SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history)
66 {
67     result.Clear();
68     if (m_opaque_ptr)
69     {
70         m_opaque_ptr->HandleCommand (command_line, add_to_history, result.ref());
71     }
72     else
73     {
74         result->AppendError ("SBCommandInterpreter is not valid");
75         result->SetStatus (eReturnStatusFailed);
76     }
77     return result.GetStatus();
78 }
79 
80 int
81 SBCommandInterpreter::HandleCompletion (const char *current_line,
82                                         const char *cursor,
83                                         const char *last_char,
84                                         int match_start_point,
85                                         int max_return_elements,
86                                         SBStringList &matches)
87 {
88     int num_completions = 0;
89     if (m_opaque_ptr)
90     {
91         lldb_private::StringList lldb_matches;
92         num_completions =  m_opaque_ptr->HandleCompletion (current_line, cursor, last_char, match_start_point,
93                                                            max_return_elements, lldb_matches);
94 
95         SBStringList temp_list (&lldb_matches);
96         matches.AppendList (temp_list);
97     }
98     return num_completions;
99 }
100 
101 bool
102 SBCommandInterpreter::HasCommands ()
103 {
104     if (m_opaque_ptr)
105         return m_opaque_ptr->HasCommands();
106     return false;
107 }
108 
109 bool
110 SBCommandInterpreter::HasAliases ()
111 {
112     if (m_opaque_ptr)
113         return m_opaque_ptr->HasAliases();
114     return false;
115 }
116 
117 bool
118 SBCommandInterpreter::HasAliasOptions ()
119 {
120     if (m_opaque_ptr)
121         return m_opaque_ptr->HasAliasOptions ();
122     return false;
123 }
124 
125 SBProcess
126 SBCommandInterpreter::GetProcess ()
127 {
128     SBProcess process;
129     if (m_opaque_ptr)
130     {
131         Debugger &debugger = m_opaque_ptr->GetDebugger();
132         Target *target = debugger.GetSelectedTarget().get();
133         if (target)
134             process.SetProcess(target->GetProcessSP());
135     }
136     return process;
137 }
138 
139 ssize_t
140 SBCommandInterpreter::WriteToScriptInterpreter (const char *src)
141 {
142     if (m_opaque_ptr && src && src[0])
143         return WriteToScriptInterpreter (src, strlen(src));
144     return 0;
145 }
146 
147 ssize_t
148 SBCommandInterpreter::WriteToScriptInterpreter (const char *src, size_t src_len)
149 {
150     if (m_opaque_ptr && src && src[0])
151     {
152         ScriptInterpreter *script_interpreter = m_opaque_ptr->GetScriptInterpreter();
153         if (script_interpreter)
154             return ::write (script_interpreter->GetMasterFileDescriptor(), src, src_len);
155     }
156     return 0;
157 }
158 
159 
160 CommandInterpreter *
161 SBCommandInterpreter::get ()
162 {
163     return m_opaque_ptr;
164 }
165 
166 CommandInterpreter &
167 SBCommandInterpreter::ref ()
168 {
169     assert (m_opaque_ptr);
170     return *m_opaque_ptr;
171 }
172 
173 void
174 SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
175 {
176     m_opaque_ptr = interpreter;
177 }
178 
179 void
180 SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
181 {
182     result.Clear();
183     if (m_opaque_ptr)
184     {
185         m_opaque_ptr->SourceInitFile (false, result.ref());
186     }
187     else
188     {
189         result->AppendError ("SBCommandInterpreter is not valid");
190         result->SetStatus (eReturnStatusFailed);
191     }
192 }
193 
194 void
195 SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result)
196 {
197     result.Clear();
198     if (m_opaque_ptr)
199     {
200         m_opaque_ptr->SourceInitFile (true, result.ref());
201     }
202     else
203     {
204         result->AppendError ("SBCommandInterpreter is not valid");
205         result->SetStatus (eReturnStatusFailed);
206     }
207 }
208 
209 SBBroadcaster
210 SBCommandInterpreter::GetBroadcaster ()
211 {
212     SBBroadcaster broadcaster (m_opaque_ptr, false);
213     return broadcaster;
214 }
215 
216