SBCommandInterpreter.cpp revision a66ba46379fe41036d870975c56ccc2319cb6618
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/SBStream.h"
27#include "lldb/API/SBStringList.h"
28
29using namespace lldb;
30using namespace lldb_private;
31
32
33SBCommandInterpreter::SBCommandInterpreter (CommandInterpreter *interpreter) :
34    m_opaque_ptr (interpreter)
35{
36    Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
37
38    if (log)
39        log->Printf ("SBCommandInterpreter::SBCommandInterpreter (interpreter=%p)"
40                     " => SBCommandInterpreter(%p)", interpreter, m_opaque_ptr);
41}
42
43SBCommandInterpreter::~SBCommandInterpreter ()
44{
45}
46
47bool
48SBCommandInterpreter::IsValid() const
49{
50    return m_opaque_ptr != NULL;
51}
52
53
54bool
55SBCommandInterpreter::CommandExists (const char *cmd)
56{
57    if (m_opaque_ptr)
58        return m_opaque_ptr->CommandExists (cmd);
59    return false;
60}
61
62bool
63SBCommandInterpreter::AliasExists (const char *cmd)
64{
65    if (m_opaque_ptr)
66        return m_opaque_ptr->AliasExists (cmd);
67    return false;
68}
69
70lldb::ReturnStatus
71SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history)
72{
73    Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
74
75    if (log)
76        log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command_line='%s', result=%p, add_to_history=%i)",
77                     m_opaque_ptr, command_line, &result, add_to_history);
78
79    result.Clear();
80    if (m_opaque_ptr)
81    {
82        m_opaque_ptr->HandleCommand (command_line, add_to_history, result.ref());
83    }
84    else
85    {
86        result->AppendError ("SBCommandInterpreter is not valid");
87        result->SetStatus (eReturnStatusFailed);
88    }
89
90    // We need to get the value again, in case the command disabled the log!
91    log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
92    if (log)
93    {
94        SBStream sstr;
95        result.GetDescription (sstr);
96        log->Printf ("SBCommandInterpreter(%p)::HandleCommand (\"%s\") => SBCommandReturnObject(%p): '%s'",
97                     m_opaque_ptr, command_line, result.get(), sstr.GetData());
98    }
99
100    return result.GetStatus();
101}
102
103int
104SBCommandInterpreter::HandleCompletion (const char *current_line,
105                                        const char *cursor,
106                                        const char *last_char,
107                                        int match_start_point,
108                                        int max_return_elements,
109                                        SBStringList &matches)
110{
111    int num_completions = 0;
112    if (m_opaque_ptr)
113    {
114        lldb_private::StringList lldb_matches;
115        num_completions =  m_opaque_ptr->HandleCompletion (current_line, cursor, last_char, match_start_point,
116                                                           max_return_elements, lldb_matches);
117
118        SBStringList temp_list (&lldb_matches);
119        matches.AppendList (temp_list);
120    }
121    return num_completions;
122}
123
124bool
125SBCommandInterpreter::HasCommands ()
126{
127    if (m_opaque_ptr)
128        return m_opaque_ptr->HasCommands();
129    return false;
130}
131
132bool
133SBCommandInterpreter::HasAliases ()
134{
135    if (m_opaque_ptr)
136        return m_opaque_ptr->HasAliases();
137    return false;
138}
139
140bool
141SBCommandInterpreter::HasAliasOptions ()
142{
143    if (m_opaque_ptr)
144        return m_opaque_ptr->HasAliasOptions ();
145    return false;
146}
147
148SBProcess
149SBCommandInterpreter::GetProcess ()
150{
151    SBProcess process;
152    if (m_opaque_ptr)
153    {
154        Debugger &debugger = m_opaque_ptr->GetDebugger();
155        Target *target = debugger.GetSelectedTarget().get();
156        if (target)
157            process.SetProcess(target->GetProcessSP());
158    }
159    Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
160
161    if (log)
162        log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)",
163                     m_opaque_ptr, process.get());
164
165
166    return process;
167}
168
169ssize_t
170SBCommandInterpreter::WriteToScriptInterpreter (const char *src)
171{
172    return WriteToScriptInterpreter (src, strlen(src));
173}
174
175ssize_t
176SBCommandInterpreter::WriteToScriptInterpreter (const char *src, size_t src_len)
177{
178    Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
179
180    ssize_t bytes_written = 0;
181    if (m_opaque_ptr && src && src[0])
182    {
183        ScriptInterpreter *script_interpreter = m_opaque_ptr->GetScriptInterpreter();
184        if (script_interpreter)
185            bytes_written = ::write (script_interpreter->GetMasterFileDescriptor(), src, src_len);
186    }
187    if (log)
188        log->Printf ("SBCommandInterpreter(%p)::WriteToScriptInterpreter (src=\"%s\", src_len=%zu) => %zi",
189                     m_opaque_ptr, src, src_len, bytes_written);
190
191    return bytes_written;
192}
193
194
195CommandInterpreter *
196SBCommandInterpreter::get ()
197{
198    return m_opaque_ptr;
199}
200
201CommandInterpreter &
202SBCommandInterpreter::ref ()
203{
204    assert (m_opaque_ptr);
205    return *m_opaque_ptr;
206}
207
208void
209SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
210{
211    m_opaque_ptr = interpreter;
212}
213
214void
215SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
216{
217    result.Clear();
218    if (m_opaque_ptr)
219    {
220        m_opaque_ptr->SourceInitFile (false, result.ref());
221    }
222    else
223    {
224        result->AppendError ("SBCommandInterpreter is not valid");
225        result->SetStatus (eReturnStatusFailed);
226    }
227    Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
228
229    if (log)
230        log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))",
231                     m_opaque_ptr, result.get());
232
233}
234
235void
236SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result)
237{
238    result.Clear();
239    if (m_opaque_ptr)
240    {
241        m_opaque_ptr->SourceInitFile (true, result.ref());
242    }
243    else
244    {
245        result->AppendError ("SBCommandInterpreter is not valid");
246        result->SetStatus (eReturnStatusFailed);
247    }
248    Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
249
250    if (log)
251        log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))",
252                     m_opaque_ptr, result.get());
253}
254
255SBBroadcaster
256SBCommandInterpreter::GetBroadcaster ()
257{
258    Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
259
260    SBBroadcaster broadcaster (m_opaque_ptr, false);
261
262    if (log)
263        log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
264                     m_opaque_ptr, broadcaster.get());
265
266    return broadcaster;
267}
268
269