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 // Called when an input reader should relinquish its control so another
71 // can be pushed onto the IO handler stack, or so the current IO
72 // handler can pop itself off the stack
77 // Called when CTRL+C is pressed which usually causes
78 // Debugger::DispatchInputInterrupt to be called.
89 return m_active && !m_done;
125 // Prompt support isn't mandatory
130 SetPrompt (const char *prompt)
132 // Prompt support isn't mandatory
137 GetControlSequence (char ch)
139 return ConstString();
173 GetInputStreamFile();
176 GetOutputStreamFile();
179 GetErrorStreamFile();
194 SetUserData (void *user_data)
196 m_user_data = user_data;
211 //------------------------------------------------------------------
212 /// Check if the input is being supplied interactively by a user
214 /// This will return true if the input stream is a terminal (tty or
215 /// pty) and can cause IO handlers to do different things (like
216 /// for a confirmation when deleting all breakpoints).
217 //------------------------------------------------------------------
221 //------------------------------------------------------------------
222 /// Check if the input is coming from a real terminal.
224 /// A real terminal has a valid size with a certain number of rows
225 /// and columns. If this function returns true, then terminal escape
226 /// sequences are expected to work (cursor movement escape sequences,
227 /// clearing lines, etc).
228 //------------------------------------------------------------------
230 GetIsRealTerminal ();
239 PrintAsync (Stream *stream, const char *s, size_t len)
241 stream->Write (s, len);
246 Debugger &m_debugger;
247 lldb::StreamFileSP m_input_sp;
248 lldb::StreamFileSP m_output_sp;
249 lldb::StreamFileSP m_error_sp;
250 Predicate<bool> m_popped;
258 DISALLOW_COPY_AND_ASSIGN (IOHandler);
262 //------------------------------------------------------------------
263 /// A delegate class for use with IOHandler subclasses.
265 /// The IOHandler delegate is designed to be mixed into classes so
266 /// they can use an IOHandler subclass to fetch input and notify the
267 /// object that inherits from this delegate class when a token is
269 //------------------------------------------------------------------
270 class IOHandlerDelegate
273 enum class Completion {
279 IOHandlerDelegate (Completion completion = Completion::None) :
280 m_completion(completion),
281 m_io_handler_done (false)
291 IOHandlerActivated (IOHandler &io_handler)
296 IOHandlerDeactivated (IOHandler &io_handler)
301 IOHandlerComplete (IOHandler &io_handler,
302 const char *current_line,
304 const char *last_char,
305 int skip_first_n_matches,
307 StringList &matches);
310 IOHandlerGetFixIndentationCharacters ()
315 //------------------------------------------------------------------
316 /// Called when a new line is created or one of an identified set of
317 /// indentation characters is typed.
319 /// This function determines how much indentation should be added
320 /// or removed to match the recommended amount for the final line.
322 /// @param[in] io_handler
323 /// The IOHandler that responsible for input.
326 /// The current input up to the line to be corrected. Lines
327 /// following the line containing the cursor are not included.
329 /// @param[in] cursor_position
330 /// The number of characters preceding the cursor on the final
331 /// line at the time.
334 /// Returns an integer describing the number of spaces needed
335 /// to correct the indentation level. Positive values indicate
336 /// that spaces should be added, while negative values represent
337 /// spaces that should be removed.
338 //------------------------------------------------------------------
340 IOHandlerFixIndentation (IOHandler &io_handler,
341 const StringList &lines,
347 //------------------------------------------------------------------
348 /// Called when a line or lines have been retrieved.
350 /// This function can handle the current line and possibly call
351 /// IOHandler::SetIsDone(true) when the IO handler is done like when
352 /// "quit" is entered as a command, of when an empty line is
353 /// received. It is up to the delegate to determine when a line
354 /// should cause a IOHandler to exit.
355 //------------------------------------------------------------------
357 IOHandlerInputComplete (IOHandler &io_handler, std::string &data) = 0;
360 IOHandlerInputInterrupted (IOHandler &io_handler, std::string &data)
364 //------------------------------------------------------------------
365 /// Called to determine whether typing enter after the last line in
366 /// \a lines should end input. This function will not be called on
367 /// IOHandler objects that are getting single lines.
368 /// @param[in] io_handler
369 /// The IOHandler that responsible for updating the lines.
372 /// The current multi-line content. May be altered to provide
373 /// alternative input when complete.
376 /// Return an boolean to indicate whether input is complete,
377 /// true indicates that no additional input is necessary, while
378 /// false indicates that more input is required.
379 //------------------------------------------------------------------
381 IOHandlerIsInputComplete (IOHandler &io_handler,
384 // Impose no requirements for input to be considered
385 // complete. subclasses should do something more intelligent.
390 IOHandlerGetControlSequence (char ch)
392 return ConstString();
396 IOHandlerGetCommandPrefix ()
402 IOHandlerGetHelpPrologue ()
407 //------------------------------------------------------------------
408 // Intercept the IOHandler::Interrupt() calls and do something.
410 // Return true if the interrupt was handled, false if the IOHandler
411 // should continue to try handle the interrupt itself.
412 //------------------------------------------------------------------
414 IOHandlerInterrupt (IOHandler &io_handler)
419 Completion m_completion; // Support for common builtin completions
420 bool m_io_handler_done;
423 //----------------------------------------------------------------------
424 // IOHandlerDelegateMultiline
426 // A IOHandlerDelegate that handles terminating multi-line input when
427 // the last line is equal to "end_line" which is specified in the
429 //----------------------------------------------------------------------
430 class IOHandlerDelegateMultiline :
431 public IOHandlerDelegate
434 IOHandlerDelegateMultiline (const char *end_line,
435 Completion completion = Completion::None) :
436 IOHandlerDelegate (completion),
437 m_end_line((end_line && end_line[0]) ? end_line : "")
442 ~IOHandlerDelegateMultiline ()
447 IOHandlerGetControlSequence (char ch) override
450 return ConstString (m_end_line + "\n");
451 return ConstString();
455 IOHandlerIsInputComplete (IOHandler &io_handler,
456 StringList &lines) override
458 // Determine whether the end of input signal has been entered
459 const size_t num_lines = lines.GetSize();
460 if (num_lines > 0 && lines[num_lines - 1] == m_end_line)
462 // Remove the terminal line from "lines" so it doesn't appear in
463 // the resulting input and return true to indicate we are done
471 const std::string m_end_line;
475 class IOHandlerEditline : public IOHandler
478 IOHandlerEditline (Debugger &debugger,
479 IOHandler::Type type,
480 const char *editline_name, // Used for saving history files
482 const char *continuation_prompt,
485 uint32_t line_number_start, // If non-zero show line numbers starting at 'line_number_start'
486 IOHandlerDelegate &delegate);
488 IOHandlerEditline (Debugger &debugger,
489 IOHandler::Type type,
490 const lldb::StreamFileSP &input_sp,
491 const lldb::StreamFileSP &output_sp,
492 const lldb::StreamFileSP &error_sp,
494 const char *editline_name, // Used for saving history files
496 const char *continuation_prompt,
499 uint32_t line_number_start, // If non-zero show line numbers starting at 'line_number_start'
500 IOHandlerDelegate &delegate);
503 ~IOHandlerEditline ();
512 Interrupt () override;
518 Activate () override;
521 Deactivate () override;
524 GetControlSequence (char ch) override
526 return m_delegate.IOHandlerGetControlSequence (ch);
530 GetCommandPrefix () override
532 return m_delegate.IOHandlerGetCommandPrefix ();
536 GetHelpPrologue () override
538 return m_delegate.IOHandlerGetHelpPrologue ();
542 GetPrompt () override;
545 SetPrompt (const char *prompt) override;
548 GetContinuationPrompt ();
551 SetContinuationPrompt (const char *prompt);
554 GetLine (std::string &line, bool &interrupted);
557 GetLines (StringList &lines, bool &interrupted);
560 SetBaseLineNumber (uint32_t line);
565 return m_interrupt_exits;
569 SetInterruptExits (bool b)
571 m_interrupt_exits = b;
575 GetCurrentLines () const
577 return m_current_lines_ptr;
581 GetCurrentLineIndex () const;
584 PrintAsync (Stream *stream, const char *s, size_t len) override;
587 #ifndef LLDB_DISABLE_LIBEDIT
589 IsInputCompleteCallback (Editline *editline,
594 FixIndentationCallback (Editline *editline,
595 const StringList &lines,
599 static int AutoCompleteCallback (const char *current_line,
601 const char *last_char,
602 int skip_first_n_matches,
609 #ifndef LLDB_DISABLE_LIBEDIT
610 std::unique_ptr<Editline> m_editline_ap;
612 IOHandlerDelegate &m_delegate;
613 std::string m_prompt;
614 std::string m_continuation_prompt;
615 StringList *m_current_lines_ptr;
616 uint32_t m_base_line_number; // If non-zero, then show line numbers in prompt
617 uint32_t m_curr_line_idx;
619 bool m_color_prompts;
620 bool m_interrupt_exits;
621 bool m_editing; // Set to true when fetching a line manually (not using libedit)
624 // The order of base classes is important. Look at the constructor of IOHandlerConfirm
626 class IOHandlerConfirm :
627 public IOHandlerDelegate,
628 public IOHandlerEditline
631 IOHandlerConfirm (Debugger &debugger,
633 bool default_response);
636 ~IOHandlerConfirm ();
641 return m_user_response;
645 IOHandlerComplete (IOHandler &io_handler,
646 const char *current_line,
648 const char *last_char,
649 int skip_first_n_matches,
651 StringList &matches) override;
654 IOHandlerInputComplete (IOHandler &io_handler, std::string &data) override;
657 const bool m_default_response;
658 bool m_user_response;
661 class IOHandlerCursesGUI :
665 IOHandlerCursesGUI (Debugger &debugger);
667 ~IOHandlerCursesGUI () override;
676 Interrupt () override;
682 Activate () override;
685 Deactivate () override;
688 curses::ApplicationAP m_app_ap;
691 class IOHandlerCursesValueObjectList :
695 IOHandlerCursesValueObjectList (Debugger &debugger, ValueObjectList &valobj_list);
698 ~IOHandlerCursesValueObjectList ();
706 ValueObjectList m_valobj_list;
715 m_mutex(Mutex::eMutexTypeRecursive),
727 Mutex::Locker locker (m_mutex);
728 return m_stack.size();
732 Push (const lldb::IOHandlerSP& sp)
736 Mutex::Locker locker (m_mutex);
737 sp->SetPopped (false);
738 m_stack.push_back (sp);
739 // Set m_top the non-locking IsTop() call
747 Mutex::Locker locker (m_mutex);
748 return m_stack.empty();
754 lldb::IOHandlerSP sp;
756 Mutex::Locker locker (m_mutex);
757 if (!m_stack.empty())
766 Mutex::Locker locker (m_mutex);
767 if (!m_stack.empty())
769 lldb::IOHandlerSP sp (m_stack.back());
771 sp->SetPopped (true);
773 // Set m_top the non-locking IsTop() call
777 m_top = m_stack.back().get();
787 IsTop (const lldb::IOHandlerSP &io_handler_sp) const
789 return m_top == io_handler_sp.get();
793 CheckTopIOHandlerTypes (IOHandler::Type top_type, IOHandler::Type second_top_type)
795 Mutex::Locker locker (m_mutex);
796 const size_t num_io_handlers = m_stack.size();
797 if (num_io_handlers >= 2 &&
798 m_stack[num_io_handlers-1]->GetType() == top_type &&
799 m_stack[num_io_handlers-2]->GetType() == second_top_type)
806 GetTopIOHandlerControlSequence (char ch)
809 return m_top->GetControlSequence(ch);
810 return ConstString();
814 GetTopIOHandlerCommandPrefix()
817 return m_top->GetCommandPrefix();
822 GetTopIOHandlerHelpPrologue()
825 return m_top->GetHelpPrologue();
830 PrintAsync (Stream *stream, const char *s, size_t len);
834 typedef std::vector<lldb::IOHandlerSP> collection;
836 mutable Mutex m_mutex;
841 DISALLOW_COPY_AND_ASSIGN (IOHandlerStack);
844 } // namespace lldb_private
846 #endif // #ifndef liblldb_IOHandler_h_