1 //===-- SBCommandReturnObject.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/API/SBCommandReturnObject.h"
11 #include "lldb/API/SBStream.h"
12 
13 #include "lldb/Core/Log.h"
14 #include "lldb/Interpreter/CommandReturnObject.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 SBCommandReturnObject::SBCommandReturnObject () :
20     m_opaque_ap (new CommandReturnObject ())
21 {
22 }
23 
24 SBCommandReturnObject::SBCommandReturnObject (const SBCommandReturnObject &rhs):
25     m_opaque_ap ()
26 {
27     if (rhs.m_opaque_ap.get())
28         m_opaque_ap.reset (new CommandReturnObject (*rhs.m_opaque_ap));
29 }
30 
31 SBCommandReturnObject::SBCommandReturnObject (CommandReturnObject *ptr) :
32     m_opaque_ap (ptr)
33 {
34 }
35 
36 CommandReturnObject *
37 SBCommandReturnObject::Release ()
38 {
39     return m_opaque_ap.release();
40 }
41 
42 const SBCommandReturnObject &
43 SBCommandReturnObject::operator = (const SBCommandReturnObject &rhs)
44 {
45     if (this != &rhs)
46     {
47         if (rhs.m_opaque_ap.get())
48             m_opaque_ap.reset (new CommandReturnObject (*rhs.m_opaque_ap));
49         else
50             m_opaque_ap.reset();
51     }
52     return *this;
53 }
54 
55 
56 SBCommandReturnObject::~SBCommandReturnObject ()
57 {
58     // m_opaque_ap will automatically delete any pointer it owns
59 }
60 
61 bool
62 SBCommandReturnObject::IsValid() const
63 {
64     return m_opaque_ap.get() != NULL;
65 }
66 
67 
68 const char *
69 SBCommandReturnObject::GetOutput ()
70 {
71     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
72 
73     if (m_opaque_ap.get())
74     {
75         if (log)
76             log->Printf ("SBCommandReturnObject(%p)::GetOutput () => \"%s\"", m_opaque_ap.get(),
77                          m_opaque_ap->GetOutputData());
78 
79         return m_opaque_ap->GetOutputData();
80     }
81 
82     if (log)
83         log->Printf ("SBCommandReturnObject(%p)::GetOutput () => NULL", m_opaque_ap.get());
84 
85     return NULL;
86 }
87 
88 const char *
89 SBCommandReturnObject::GetError ()
90 {
91     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
92 
93     if (m_opaque_ap.get())
94     {
95         if (log)
96             log->Printf ("SBCommandReturnObject(%p)::GetError () => \"%s\"", m_opaque_ap.get(),
97                          m_opaque_ap->GetErrorData());
98 
99         return m_opaque_ap->GetErrorData();
100     }
101 
102     if (log)
103         log->Printf ("SBCommandReturnObject(%p)::GetError () => NULL", m_opaque_ap.get());
104 
105     return NULL;
106 }
107 
108 size_t
109 SBCommandReturnObject::GetOutputSize ()
110 {
111     if (m_opaque_ap.get())
112         return strlen (m_opaque_ap->GetOutputData());
113     return 0;
114 }
115 
116 size_t
117 SBCommandReturnObject::GetErrorSize ()
118 {
119     if (m_opaque_ap.get())
120         return strlen(m_opaque_ap->GetErrorData());
121     return 0;
122 }
123 
124 size_t
125 SBCommandReturnObject::PutOutput (FILE *fh)
126 {
127     if (fh)
128     {
129         size_t num_bytes = GetOutputSize ();
130         if (num_bytes)
131             return ::fprintf (fh, "%s", GetOutput());
132     }
133     return 0;
134 }
135 
136 size_t
137 SBCommandReturnObject::PutError (FILE *fh)
138 {
139     if (fh)
140     {
141         size_t num_bytes = GetErrorSize ();
142         if (num_bytes)
143             return ::fprintf (fh, "%s", GetError());
144     }
145     return 0;
146 }
147 
148 void
149 SBCommandReturnObject::Clear()
150 {
151     if (m_opaque_ap.get())
152         m_opaque_ap->Clear();
153 }
154 
155 lldb::ReturnStatus
156 SBCommandReturnObject::GetStatus()
157 {
158     if (m_opaque_ap.get())
159         return m_opaque_ap->GetStatus();
160     return lldb::eReturnStatusInvalid;
161 }
162 
163 void
164 SBCommandReturnObject::SetStatus(lldb::ReturnStatus status)
165 {
166     if (m_opaque_ap.get())
167          m_opaque_ap->SetStatus(status);
168 }
169 
170 bool
171 SBCommandReturnObject::Succeeded ()
172 {
173     if (m_opaque_ap.get())
174         return m_opaque_ap->Succeeded();
175     return false;
176 }
177 
178 bool
179 SBCommandReturnObject::HasResult ()
180 {
181     if (m_opaque_ap.get())
182         return m_opaque_ap->HasResult();
183     return false;
184 }
185 
186 void
187 SBCommandReturnObject::AppendMessage (const char *message)
188 {
189     if (m_opaque_ap.get())
190         m_opaque_ap->AppendMessage (message);
191 }
192 
193 void
194 SBCommandReturnObject::AppendWarning (const char *message)
195 {
196     if (m_opaque_ap.get())
197         m_opaque_ap->AppendWarning (message);
198 }
199 
200 CommandReturnObject *
201 SBCommandReturnObject::operator ->() const
202 {
203     return m_opaque_ap.get();
204 }
205 
206 CommandReturnObject *
207 SBCommandReturnObject::get() const
208 {
209     return m_opaque_ap.get();
210 }
211 
212 CommandReturnObject &
213 SBCommandReturnObject::operator *() const
214 {
215     assert(m_opaque_ap.get());
216     return *(m_opaque_ap.get());
217 }
218 
219 
220 CommandReturnObject &
221 SBCommandReturnObject::ref() const
222 {
223     assert(m_opaque_ap.get());
224     return *(m_opaque_ap.get());
225 }
226 
227 
228 void
229 SBCommandReturnObject::SetLLDBObjectPtr (CommandReturnObject *ptr)
230 {
231     if (m_opaque_ap.get())
232         m_opaque_ap.reset (ptr);
233 }
234 
235 bool
236 SBCommandReturnObject::GetDescription (SBStream &description)
237 {
238     Stream &strm = description.ref();
239 
240     if (m_opaque_ap.get())
241     {
242         description.Printf ("Status:  ");
243         lldb::ReturnStatus status = m_opaque_ap->GetStatus();
244         if (status == lldb::eReturnStatusStarted)
245             strm.PutCString ("Started");
246         else if (status == lldb::eReturnStatusInvalid)
247             strm.PutCString ("Invalid");
248         else if (m_opaque_ap->Succeeded())
249             strm.PutCString ("Success");
250         else
251             strm.PutCString ("Fail");
252 
253         if (GetOutputSize() > 0)
254             strm.Printf ("\nOutput Message:\n%s", GetOutput());
255 
256         if (GetErrorSize() > 0)
257             strm.Printf ("\nError Message:\n%s", GetError());
258     }
259     else
260         strm.PutCString ("No value");
261 
262     return true;
263 }
264 
265 void
266 SBCommandReturnObject::SetImmediateOutputFile (FILE *fh)
267 {
268     if (m_opaque_ap.get())
269         m_opaque_ap->SetImmediateOutputFile (fh);
270 }
271 
272 void
273 SBCommandReturnObject::SetImmediateErrorFile (FILE *fh)
274 {
275     if (m_opaque_ap.get())
276         m_opaque_ap->SetImmediateErrorFile (fh);
277 }
278 
279 void
280 SBCommandReturnObject::PutCString(const char* string, int len)
281 {
282     if (m_opaque_ap.get())
283     {
284         m_opaque_ap->AppendMessage(string, len);
285     }
286 }
287 
288 const char *
289 SBCommandReturnObject::GetOutput (bool only_if_no_immediate)
290 {
291     if (!m_opaque_ap.get())
292         return NULL;
293     if (only_if_no_immediate == false || m_opaque_ap->GetImmediateOutputStream().get() == NULL)
294         return GetOutput();
295     return NULL;
296 }
297 
298 const char *
299 SBCommandReturnObject::GetError (bool only_if_no_immediate)
300 {
301     if (!m_opaque_ap.get())
302         return NULL;
303     if (only_if_no_immediate == false || m_opaque_ap->GetImmediateErrorStream().get() == NULL)
304         return GetError();
305     return NULL;
306 }
307 
308 size_t
309 SBCommandReturnObject::Printf(const char* format, ...)
310 {
311     if (m_opaque_ap.get())
312     {
313         va_list args;
314         va_start (args, format);
315         size_t result = m_opaque_ap->GetOutputStream().PrintfVarArg(format, args);
316         va_end (args);
317         return result;
318     }
319     return 0;
320 }
321 
322