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/Stream.h"
23 #include "lldb/Core/StringList.h"
24 #include "lldb/Core/ValueObjectList.h"
25 #include "lldb/Host/Mutex.h"
26 #include "lldb/Host/Predicate.h"
31 typedef std::unique_ptr<Application> ApplicationAP;
34 namespace lldb_private {
51 IOHandler (Debugger &debugger,
52 IOHandler::Type type);
54 IOHandler (Debugger &debugger,
56 const lldb::StreamFileSP &input_sp,
57 const lldb::StreamFileSP &output_sp,
58 const lldb::StreamFileSP &error_sp,
64 // Each IOHandler gets to run until it is done. It should read data
65 // from the "in" and place output into "out" and "err and return
70 // Hide any characters that have been displayed so far so async
71 // output can be displayed. Refresh() will be called after the
72 // output has been displayed.
76 // Called when the async output has been received in order to update
77 // the input reader (refresh the prompt and redisplay any current
78 // line(s) that are being edited
82 // Called when an input reader should relinquish its control so another
83 // can be pushed onto the IO handler stack, or so the current IO
84 // handler can pop itself off the stack
89 // Called when CTRL+C is pressed which usually causes
90 // Debugger::DispatchInputInterrupt to be called.
101 return m_active && !m_done;
137 // Prompt support isn't mandatory
142 SetPrompt (const char *prompt)
144 // Prompt support isn't mandatory
149 GetControlSequence (char ch)
151 return ConstString();
185 GetInputStreamFile();
188 GetOutputStreamFile();
191 GetErrorStreamFile();
206 SetUserData (void *user_data)
208 m_user_data = user_data;
223 //------------------------------------------------------------------
224 /// Check if the input is being supplied interactively by a user
226 /// This will return true if the input stream is a terminal (tty or
227 /// pty) and can cause IO handlers to do different things (like
228 /// for a confirmation when deleting all breakpoints).
229 //------------------------------------------------------------------
233 //------------------------------------------------------------------
234 /// Check if the input is coming from a real terminal.
236 /// A real terminal has a valid size with a certain number of rows
237 /// and columns. If this function returns true, then terminal escape
238 /// sequences are expected to work (cursor movement escape sequences,
239 /// clearing lines, etc).
240 //------------------------------------------------------------------
242 GetIsRealTerminal ();
251 Debugger &m_debugger;
252 lldb::StreamFileSP m_input_sp;
253 lldb::StreamFileSP m_output_sp;
254 lldb::StreamFileSP m_error_sp;
255 Predicate<bool> m_popped;
263 DISALLOW_COPY_AND_ASSIGN (IOHandler);
267 //------------------------------------------------------------------
268 /// A delegate class for use with IOHandler subclasses.
270 /// The IOHandler delegate is designed to be mixed into classes so
271 /// they can use an IOHandler subclass to fetch input and notify the
272 /// object that inherits from this delegate class when a token is
274 //------------------------------------------------------------------
275 class IOHandlerDelegate
278 enum class Completion {
284 IOHandlerDelegate (Completion completion = Completion::None) :
285 m_completion(completion),
286 m_io_handler_done (false)
296 IOHandlerActivated (IOHandler &io_handler)
301 IOHandlerDeactivated (IOHandler &io_handler)
306 IOHandlerComplete (IOHandler &io_handler,
307 const char *current_line,
309 const char *last_char,
310 int skip_first_n_matches,
312 StringList &matches);
315 IOHandlerGetFixIndentationCharacters ()
320 //------------------------------------------------------------------
321 /// Called when a new line is created or one of an identifed set of
322 /// indentation characters is typed.
324 /// This function determines how much indentation should be added
325 /// or removed to match the recommended amount for the final line.
327 /// @param[in] io_handler
328 /// The IOHandler that responsible for input.
331 /// The current input up to the line to be corrected. Lines
332 /// following the line containing the cursor are not included.
334 /// @param[in] cursor_position
335 /// The number of characters preceeding the cursor on the final
336 /// line at the time.
339 /// Returns an integer describing the number of spaces needed
340 /// to correct the indentation level. Positive values indicate
341 /// that spaces should be added, while negative values represent
342 /// spaces that should be removed.
343 //------------------------------------------------------------------
345 IOHandlerFixIndentation (IOHandler &io_handler,
346 const StringList &lines,
352 //------------------------------------------------------------------
353 /// Called when a line or lines have been retrieved.
355 /// This function can handle the current line and possibly call
356 /// IOHandler::SetIsDone(true) when the IO handler is done like when
357 /// "quit" is entered as a command, of when an empty line is
358 /// received. It is up to the delegate to determine when a line
359 /// should cause a IOHandler to exit.
360 //------------------------------------------------------------------
362 IOHandlerInputComplete (IOHandler &io_handler, std::string &data) = 0;
365 IOHandlerInputInterrupted (IOHandler &io_handler, std::string &data)
369 //------------------------------------------------------------------
370 /// Called to determine whether typing enter after the last line in
371 /// \a lines should end input. This function will not be called on
372 /// IOHandler objects that are getting single lines.
373 /// @param[in] io_handler
374 /// The IOHandler that responsible for updating the lines.
377 /// The current multi-line content. May be altered to provide
378 /// alternative input when complete.
381 /// Return an boolean to indicate whether input is complete,
382 /// true indicates that no additional input is necessary, while
383 /// false indicates that more input is required.
384 //------------------------------------------------------------------
386 IOHandlerIsInputComplete (IOHandler &io_handler,
389 // Impose no requirements for input to be considered
390 // complete. subclasses should do something more intelligent.
395 IOHandlerGetControlSequence (char ch)
397 return ConstString();
401 IOHandlerGetCommandPrefix ()
407 IOHandlerGetHelpPrologue ()
412 //------------------------------------------------------------------
413 // Intercept the IOHandler::Interrupt() calls and do something.
415 // Return true if the interrupt was handled, false if the IOHandler
416 // should continue to try handle the interrupt itself.
417 //------------------------------------------------------------------
419 IOHandlerInterrupt (IOHandler &io_handler)
424 Completion m_completion; // Support for common builtin completions
425 bool m_io_handler_done;
428 //----------------------------------------------------------------------
429 // IOHandlerDelegateMultiline
431 // A IOHandlerDelegate that handles terminating multi-line input when
432 // the last line is equal to "end_line" which is specified in the
434 //----------------------------------------------------------------------
435 class IOHandlerDelegateMultiline :
436 public IOHandlerDelegate
439 IOHandlerDelegateMultiline (const char *end_line,
440 Completion completion = Completion::None) :
441 IOHandlerDelegate (completion),
442 m_end_line((end_line && end_line[0]) ? end_line : "")
447 ~IOHandlerDelegateMultiline ()
452 IOHandlerGetControlSequence (char ch)
455 return ConstString (m_end_line + "\n");
456 return ConstString();
460 IOHandlerIsInputComplete (IOHandler &io_handler,
463 // Determine whether the end of input signal has been entered
464 const size_t num_lines = lines.GetSize();
465 if (num_lines > 0 && lines[num_lines - 1] == m_end_line)
467 // Remove the terminal line from "lines" so it doesn't appear in
468 // the resulting input and return true to indicate we are done
476 const std::string m_end_line;
480 class IOHandlerEditline : public IOHandler
483 IOHandlerEditline (Debugger &debugger,
484 IOHandler::Type type,
485 const char *editline_name, // Used for saving history files
487 const char *continuation_prompt,
490 uint32_t line_number_start, // If non-zero show line numbers starting at 'line_number_start'
491 IOHandlerDelegate &delegate);
493 IOHandlerEditline (Debugger &debugger,
494 IOHandler::Type type,
495 const lldb::StreamFileSP &input_sp,
496 const lldb::StreamFileSP &output_sp,
497 const lldb::StreamFileSP &error_sp,
499 const char *editline_name, // Used for saving history files
501 const char *continuation_prompt,
504 uint32_t line_number_start, // If non-zero show line numbers starting at 'line_number_start'
505 IOHandlerDelegate &delegate);
508 ~IOHandlerEditline ();
535 GetControlSequence (char ch)
537 return m_delegate.IOHandlerGetControlSequence (ch);
543 return m_delegate.IOHandlerGetCommandPrefix ();
549 return m_delegate.IOHandlerGetHelpPrologue ();
556 SetPrompt (const char *prompt);
559 GetContinuationPrompt ();
562 SetContinuationPrompt (const char *prompt);
565 GetLine (std::string &line, bool &interrupted);
568 GetLines (StringList &lines, bool &interrupted);
571 SetBaseLineNumber (uint32_t line);
576 return m_interrupt_exits;
580 SetInterruptExits (bool b)
582 m_interrupt_exits = b;
586 GetCurrentLines () const
588 return m_current_lines_ptr;
592 GetCurrentLineIndex () const;
595 #ifndef LLDB_DISABLE_LIBEDIT
597 IsInputCompleteCallback (Editline *editline,
602 FixIndentationCallback (Editline *editline,
603 const StringList &lines,
607 static int AutoCompleteCallback (const char *current_line,
609 const char *last_char,
610 int skip_first_n_matches,
617 #ifndef LLDB_DISABLE_LIBEDIT
618 std::unique_ptr<Editline> m_editline_ap;
620 IOHandlerDelegate &m_delegate;
621 std::string m_prompt;
622 std::string m_continuation_prompt;
623 StringList *m_current_lines_ptr;
624 uint32_t m_base_line_number; // If non-zero, then show line numbers in prompt
625 uint32_t m_curr_line_idx;
627 bool m_color_prompts;
628 bool m_interrupt_exits;
631 // The order of base classes is important. Look at the constructor of IOHandlerConfirm
633 class IOHandlerConfirm :
634 public IOHandlerDelegate,
635 public IOHandlerEditline
638 IOHandlerConfirm (Debugger &debugger,
640 bool default_response);
643 ~IOHandlerConfirm ();
648 return m_user_response;
652 IOHandlerComplete (IOHandler &io_handler,
653 const char *current_line,
655 const char *last_char,
656 int skip_first_n_matches,
658 StringList &matches);
661 IOHandlerInputComplete (IOHandler &io_handler, std::string &data);
664 const bool m_default_response;
665 bool m_user_response;
668 class IOHandlerCursesGUI :
672 IOHandlerCursesGUI (Debugger &debugger);
675 ~IOHandlerCursesGUI ();
702 curses::ApplicationAP m_app_ap;
705 class IOHandlerCursesValueObjectList :
709 IOHandlerCursesValueObjectList (Debugger &debugger, ValueObjectList &valobj_list);
712 ~IOHandlerCursesValueObjectList ();
729 ValueObjectList m_valobj_list;
738 m_mutex(Mutex::eMutexTypeRecursive),
750 Mutex::Locker locker (m_mutex);
751 return m_stack.size();
755 Push (const lldb::IOHandlerSP& sp)
759 Mutex::Locker locker (m_mutex);
760 sp->SetPopped (false);
761 m_stack.push_back (sp);
762 // Set m_top the non-locking IsTop() call
770 Mutex::Locker locker (m_mutex);
771 return m_stack.empty();
777 lldb::IOHandlerSP sp;
779 Mutex::Locker locker (m_mutex);
780 if (!m_stack.empty())
789 Mutex::Locker locker (m_mutex);
790 if (!m_stack.empty())
792 lldb::IOHandlerSP sp (m_stack.back());
794 sp->SetPopped (true);
796 // Set m_top the non-locking IsTop() call
800 m_top = m_stack.back().get();
810 IsTop (const lldb::IOHandlerSP &io_handler_sp) const
812 return m_top == io_handler_sp.get();
816 CheckTopIOHandlerTypes (IOHandler::Type top_type, IOHandler::Type second_top_type)
818 Mutex::Locker locker (m_mutex);
819 const size_t num_io_handlers = m_stack.size();
820 if (num_io_handlers >= 2 &&
821 m_stack[num_io_handlers-1]->GetType() == top_type &&
822 m_stack[num_io_handlers-2]->GetType() == second_top_type)
829 GetTopIOHandlerControlSequence (char ch)
832 return m_top->GetControlSequence(ch);
833 return ConstString();
837 GetTopIOHandlerCommandPrefix()
840 return m_top->GetCommandPrefix();
845 GetTopIOHandlerHelpPrologue()
848 return m_top->GetHelpPrologue();
854 typedef std::vector<lldb::IOHandlerSP> collection;
856 mutable Mutex m_mutex;
861 DISALLOW_COPY_AND_ASSIGN (IOHandlerStack);
864 } // namespace lldb_private
866 #endif // #ifndef liblldb_IOHandler_h_