1 //===-- IOHandler.h ---------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_IOHandler_h_
11 #define liblldb_IOHandler_h_
17 #include "lldb/lldb-public.h"
18 #include "lldb/lldb-enumerations.h"
19 #include "lldb/Core/ConstString.h"
20 #include "lldb/Core/Error.h"
21 #include "lldb/Core/Flags.h"
22 #include "lldb/Core/StringList.h"
23 #include "lldb/Core/ValueObjectList.h"
24 #include "lldb/Host/Mutex.h"
29 typedef std::unique_ptr<Application> ApplicationAP;
32 namespace lldb_private {
37 IOHandler (Debugger &debugger);
39 IOHandler (Debugger &debugger,
40 const lldb::StreamFileSP &input_sp,
41 const lldb::StreamFileSP &output_sp,
42 const lldb::StreamFileSP &error_sp,
48 // Each IOHandler gets to run until it is done. It should read data
49 // from the "in" and place output into "out" and "err and return
54 // Hide any characters that have been displayed so far so async
55 // output can be displayed. Refresh() will be called after the
56 // output has been displayed.
60 // Called when the async output has been received in order to update
61 // the input reader (refresh the prompt and redisplay any current
62 // line(s) that are being edited
66 // Called when an input reader should relinquish its control so another
67 // can be pushed onto the IO handler stack, or so the current IO
68 // handler can pop itself off the stack
73 // Called when CTRL+C is pressed which usually causes
74 // Debugger::DispatchInputInterrupt to be called.
85 return m_active && !m_done;
115 // Prompt support isn't mandatory
120 SetPrompt (const char *prompt)
122 // Prompt support isn't mandatory
127 GetControlSequence (char ch)
129 return ConstString();
151 GetInputStreamFile();
154 GetOutputStreamFile();
157 GetErrorStreamFile();
172 SetUserData (void *user_data)
174 m_user_data = user_data;
189 //------------------------------------------------------------------
190 /// Check if the input is being supplied interactively by a user
192 /// This will return true if the input stream is a terminal (tty or
193 /// pty) and can cause IO handlers to do different things (like
194 /// for a comfirmation when deleting all breakpoints).
195 //------------------------------------------------------------------
199 //------------------------------------------------------------------
200 /// Check if the input is coming from a real terminal.
202 /// A real terminal has a valid size with a certain number of rows
203 /// and colums. If this function returns true, then terminal escape
204 /// sequences are expected to work (cursor movement escape sequences,
205 /// clearning lines, etc).
206 //------------------------------------------------------------------
208 GetIsRealTerminal ();
211 Debugger &m_debugger;
212 lldb::StreamFileSP m_input_sp;
213 lldb::StreamFileSP m_output_sp;
214 lldb::StreamFileSP m_error_sp;
221 DISALLOW_COPY_AND_ASSIGN (IOHandler);
225 //------------------------------------------------------------------
226 /// A delegate class for use with IOHandler subclasses.
228 /// The IOHandler delegate is designed to be mixed into classes so
229 /// they can use an IOHandler subclass to fetch input and notify the
230 /// object that inherits from this delegate class when a token is
232 //------------------------------------------------------------------
233 class IOHandlerDelegate
236 enum class Completion {
242 IOHandlerDelegate (Completion completion = Completion::None) :
243 m_completion(completion),
244 m_io_handler_done (false)
254 IOHandlerActivated (IOHandler &io_handler)
259 IOHandlerComplete (IOHandler &io_handler,
260 const char *current_line,
262 const char *last_char,
263 int skip_first_n_matches,
265 StringList &matches);
267 //------------------------------------------------------------------
268 /// Called when a line or lines have been retrieved.
270 /// This funtion can handle the current line and possibly call
271 /// IOHandler::SetIsDone(true) when the IO handler is done like when
272 /// "quit" is entered as a command, of when an empty line is
273 /// received. It is up to the delegate to determine when a line
274 /// should cause a IOHandler to exit.
275 //------------------------------------------------------------------
277 IOHandlerInputComplete (IOHandler &io_handler, std::string &data) = 0;
279 //------------------------------------------------------------------
280 /// Called when a line in \a lines has been updated when doing
281 /// multi-line input.
284 /// Return an enumeration to indicate the status of the current
286 /// Success - The line is good and should be added to the
288 /// Error - There is an error with the current line and it
289 /// need to be re-edited before it is acceptable
290 /// Done - The lines collection is complete and ready to be
292 //------------------------------------------------------------------
294 IOHandlerLinesUpdated (IOHandler &io_handler,
299 return LineStatus::Done; // Stop getting lines on the first line that is updated
300 // subclasses should do something more intelligent here.
301 // This function will not be called on IOHandler objects
302 // that are getting single lines.
307 GetControlSequence (char ch)
309 return ConstString();
313 Completion m_completion; // Support for common builtin completions
314 bool m_io_handler_done;
317 //----------------------------------------------------------------------
318 // IOHandlerDelegateMultiline
320 // A IOHandlerDelegate that handles terminating multi-line input when
321 // the last line is equal to "end_line" which is specified in the
323 //----------------------------------------------------------------------
324 class IOHandlerDelegateMultiline :
325 public IOHandlerDelegate
328 IOHandlerDelegateMultiline (const char *end_line,
329 Completion completion = Completion::None) :
330 IOHandlerDelegate (completion),
331 m_end_line((end_line && end_line[0]) ? end_line : "")
336 ~IOHandlerDelegateMultiline ()
341 GetControlSequence (char ch)
344 return ConstString (m_end_line + "\n");
345 return ConstString();
349 IOHandlerLinesUpdated (IOHandler &io_handler,
354 if (line_idx == UINT32_MAX)
356 // Remove the last empty line from "lines" so it doesn't appear
357 // in our final expression and return true to indicate we are done
360 return LineStatus::Done;
362 else if (line_idx + 1 == lines.GetSize())
364 // The last line was edited, if this line is empty, then we are done
365 // getting our multiple lines.
366 if (lines[line_idx] == m_end_line)
367 return LineStatus::Done;
369 return LineStatus::Success;
372 const std::string m_end_line;
376 class IOHandlerEditline : public IOHandler
379 IOHandlerEditline (Debugger &debugger,
380 const char *editline_name, // Used for saving history files
383 IOHandlerDelegate &delegate);
385 IOHandlerEditline (Debugger &debugger,
386 const lldb::StreamFileSP &input_sp,
387 const lldb::StreamFileSP &output_sp,
388 const lldb::StreamFileSP &error_sp,
390 const char *editline_name, // Used for saving history files
393 IOHandlerDelegate &delegate);
396 ~IOHandlerEditline ();
419 IOHandler::Activate();
420 m_delegate.IOHandlerActivated(*this);
424 GetControlSequence (char ch)
426 return m_delegate.GetControlSequence (ch);
433 SetPrompt (const char *prompt);
436 GetLine (std::string &line);
439 GetLines (StringList &lines);
443 LineCompletedCallback (Editline *editline,
449 static int AutoCompleteCallback (const char *current_line,
451 const char *last_char,
452 int skip_first_n_matches,
458 std::unique_ptr<Editline> m_editline_ap;
459 IOHandlerDelegate &m_delegate;
460 std::string m_prompt;
464 class IOHandlerConfirm :
465 public IOHandlerEditline,
466 public IOHandlerDelegate
469 IOHandlerConfirm (Debugger &debugger,
471 bool default_response);
474 ~IOHandlerConfirm ();
479 return m_user_response;
483 IOHandlerComplete (IOHandler &io_handler,
484 const char *current_line,
486 const char *last_char,
487 int skip_first_n_matches,
489 StringList &matches);
492 IOHandlerInputComplete (IOHandler &io_handler, std::string &data);
495 const bool m_default_response;
496 bool m_user_response;
499 class IOHandlerCursesGUI :
503 IOHandlerCursesGUI (Debugger &debugger);
506 ~IOHandlerCursesGUI ();
533 curses::ApplicationAP m_app_ap;
536 class IOHandlerCursesValueObjectList :
540 IOHandlerCursesValueObjectList (Debugger &debugger, ValueObjectList &valobj_list);
543 ~IOHandlerCursesValueObjectList ();
560 ValueObjectList m_valobj_list;
569 m_mutex(Mutex::eMutexTypeRecursive),
581 Mutex::Locker locker (m_mutex);
582 return m_stack.size();
586 Push (const lldb::IOHandlerSP& sp)
590 Mutex::Locker locker (m_mutex);
592 // Set m_top the non-locking IsTop() call
600 Mutex::Locker locker (m_mutex);
601 return m_stack.empty();
607 lldb::IOHandlerSP sp;
609 Mutex::Locker locker (m_mutex);
610 if (!m_stack.empty())
619 Mutex::Locker locker (m_mutex);
620 if (!m_stack.empty())
622 // Set m_top the non-locking IsTop() call
626 m_top = m_stack.top().get();
636 IsTop (const lldb::IOHandlerSP &io_handler_sp) const
638 return m_top == io_handler_sp.get();
642 GetTopIOHandlerControlSequence (char ch)
645 return m_top->GetControlSequence(ch);
646 return ConstString();
651 std::stack<lldb::IOHandlerSP> m_stack;
652 mutable Mutex m_mutex;
657 DISALLOW_COPY_AND_ASSIGN (IOHandlerStack);
660 } // namespace lldb_private
662 #endif // #ifndef liblldb_IOHandler_h_