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