1 //===-- SBInputReader.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 
11 #include "lldb/lldb-enumerations.h"
12 
13 #include "lldb/API/SBDebugger.h"
14 #include "lldb/API/SBError.h"
15 #include "lldb/API/SBInputReader.h"
16 #include "lldb/API/SBStream.h"
17 #include "lldb/API/SBStringList.h"
18 #include "lldb/Core/InputReader.h"
19 #include "lldb/Core/Log.h"
20 
21 
22 using namespace lldb;
23 using namespace lldb_private;
24 
SBInputReader()25 SBInputReader::SBInputReader ()  :
26     m_opaque_sp (),
27     m_callback_function (NULL),
28     m_callback_baton (NULL)
29 
30 {
31 }
32 
SBInputReader(const lldb::InputReaderSP & reader_sp)33 SBInputReader::SBInputReader (const lldb::InputReaderSP &reader_sp) :
34     m_opaque_sp (reader_sp)
35 {
36     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
37 
38     if (log)
39         log->Printf ("SBInputReader::SBInputReader (reader_sp=%p) => SBInputReader(%p)", reader_sp.get(),
40                      m_opaque_sp.get());
41 }
42 
SBInputReader(const SBInputReader & rhs)43 SBInputReader::SBInputReader (const SBInputReader &rhs) :
44     m_opaque_sp (rhs.m_opaque_sp)
45 {
46     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
47 
48     if (log)
49         log->Printf("SBInputReader::SBInputReader (rhs.sp=%p) => SBInputReader(%p)",
50                     rhs.m_opaque_sp.get(), m_opaque_sp.get());
51 }
52 
~SBInputReader()53 SBInputReader::~SBInputReader ()
54 {
55 }
56 
57 size_t
PrivateCallback(void * baton,InputReader & reader,lldb::InputReaderAction notification,const char * bytes,size_t bytes_len)58 SBInputReader::PrivateCallback
59 (
60     void *baton,
61     InputReader &reader,
62     lldb::InputReaderAction notification,
63     const char *bytes,
64     size_t bytes_len
65 )
66 {
67     SBInputReader *sb_reader = (SBInputReader *)baton;
68     return sb_reader->m_callback_function (sb_reader->m_callback_baton,
69                                            sb_reader,
70                                            notification,
71                                            bytes,
72                                            bytes_len);
73 }
74 
75 SBError
Initialize(SBDebugger & debugger,Callback callback_function,void * callback_baton,lldb::InputReaderGranularity granularity,const char * end_token,const char * prompt,bool echo)76 SBInputReader::Initialize
77 (
78     SBDebugger &debugger,
79     Callback callback_function,
80     void *callback_baton,
81     lldb::InputReaderGranularity granularity,
82     const char *end_token,
83     const char *prompt,
84     bool echo
85 )
86 {
87     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
88 
89     if (log)
90         log->Printf("SBInputReader(%p)::Initialize (SBDebugger(%p), callback_function=%p, callback_baton=%p, "
91                     "granularity=%s, end_token=\"%s\", prompt=\"%s\", echo=%i)",
92                     m_opaque_sp.get(),
93                     debugger.get(),
94                     callback_function,
95                     callback_baton,
96                     InputReader::GranularityAsCString (granularity), end_token, prompt,
97                     echo);
98 
99     SBError sb_error;
100     m_opaque_sp.reset (new InputReader (debugger.ref()));
101 
102     m_callback_function = callback_function;
103     m_callback_baton = callback_baton;
104 
105     if (m_opaque_sp)
106     {
107         sb_error.SetError (m_opaque_sp->Initialize (SBInputReader::PrivateCallback,
108                                                     this,
109                                                     granularity,
110                                                     end_token,
111                                                     prompt,
112                                                     echo));
113     }
114 
115     if (sb_error.Fail())
116     {
117         m_opaque_sp.reset ();
118         m_callback_function = NULL;
119         m_callback_baton = NULL;
120     }
121 
122     if (log)
123     {
124         SBStream sstr;
125         sb_error.GetDescription (sstr);
126         log->Printf ("SBInputReader(%p)::Initialize (...) => SBError(%p): %s", m_opaque_sp.get(),
127                      sb_error.get(), sstr.GetData());
128     }
129 
130     return sb_error;
131 }
132 
133 bool
IsValid() const134 SBInputReader::IsValid () const
135 {
136     return (m_opaque_sp.get() != NULL);
137 }
138 
139 const SBInputReader &
operator =(const SBInputReader & rhs)140 SBInputReader::operator = (const SBInputReader &rhs)
141 {
142     if (this != &rhs)
143         m_opaque_sp = rhs.m_opaque_sp;
144     return *this;
145 }
146 
147 InputReader *
operator ->() const148 SBInputReader::operator->() const
149 {
150     return m_opaque_sp.get();
151 }
152 
153 lldb::InputReaderSP &
operator *()154 SBInputReader::operator *()
155 {
156     return m_opaque_sp;
157 }
158 
159 const lldb::InputReaderSP &
operator *() const160 SBInputReader::operator *() const
161 {
162     return m_opaque_sp;
163 }
164 
165 InputReader *
get() const166 SBInputReader::get() const
167 {
168     return m_opaque_sp.get();
169 }
170 
171 InputReader &
ref() const172 SBInputReader::ref() const
173 {
174     assert (m_opaque_sp.get());
175     return *m_opaque_sp;
176 }
177 
178 bool
IsDone() const179 SBInputReader::IsDone () const
180 {
181     if (m_opaque_sp)
182         return m_opaque_sp->IsDone();
183     else
184         return true;
185 }
186 
187 void
SetIsDone(bool value)188 SBInputReader::SetIsDone (bool value)
189 {
190     if (m_opaque_sp)
191         m_opaque_sp->SetIsDone (value);
192 }
193 
194 bool
IsActive() const195 SBInputReader::IsActive () const
196 {
197     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
198 
199     bool ret_value = false;
200     if (m_opaque_sp)
201         ret_value = m_opaque_sp->IsActive();
202 
203     if (log)
204         log->Printf ("SBInputReader(%p)::IsActive () => %i", m_opaque_sp.get(), ret_value);
205 
206     return ret_value;
207 }
208 
209 InputReaderGranularity
GetGranularity()210 SBInputReader::GetGranularity ()
211 {
212     if (m_opaque_sp)
213         return m_opaque_sp->GetGranularity();
214     else
215         return eInputReaderGranularityInvalid;
216 }
217