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_
21 // Other libraries and framework includes
23 #include "lldb/lldb-public.h"
24 #include "lldb/lldb-enumerations.h"
25 #include "lldb/Core/ConstString.h"
26 #include "lldb/Core/Error.h"
27 #include "lldb/Core/Flags.h"
28 #include "lldb/Core/Stream.h"
29 #include "lldb/Core/StringList.h"
30 #include "lldb/Core/ValueObjectList.h"
31 #include "lldb/Host/Mutex.h"
32 #include "lldb/Host/Predicate.h"
37 typedef std::unique_ptr<Application> ApplicationAP;
40 namespace lldb_private {
58 IOHandler (Debugger &debugger,
59 IOHandler::Type type);
61 IOHandler (Debugger &debugger,
63 const lldb::StreamFileSP &input_sp,
64 const lldb::StreamFileSP &output_sp,
65 const lldb::StreamFileSP &error_sp,
71 // Each IOHandler gets to run until it is done. It should read data
72 // from the "in" and place output into "out" and "err and return
77 // Called when an input reader should relinquish its control so another
78 // can be pushed onto the IO handler stack, or so the current IO
79 // handler can pop itself off the stack
84 // Called when CTRL+C is pressed which usually causes
85 // Debugger::DispatchInputInterrupt to be called.
96 return m_active && !m_done;
132 // Prompt support isn't mandatory
137 SetPrompt (const char *prompt)
139 // Prompt support isn't mandatory
144 GetControlSequence (char ch)
146 return ConstString();
180 GetInputStreamFile();
183 GetOutputStreamFile();
186 GetErrorStreamFile();
201 SetUserData (void *user_data)
203 m_user_data = user_data;
218 //------------------------------------------------------------------
219 /// Check if the input is being supplied interactively by a user
221 /// This will return true if the input stream is a terminal (tty or
222 /// pty) and can cause IO handlers to do different things (like
223 /// for a confirmation when deleting all breakpoints).
224 //------------------------------------------------------------------
228 //------------------------------------------------------------------
229 /// Check if the input is coming from a real terminal.
231 /// A real terminal has a valid size with a certain number of rows
232 /// and columns. If this function returns true, then terminal escape
233 /// sequences are expected to work (cursor movement escape sequences,
234 /// clearing lines, etc).
235 //------------------------------------------------------------------
237 GetIsRealTerminal ();
246 PrintAsync (Stream *stream, const char *s, size_t len)
248 stream->Write (s, len);
253 Debugger &m_debugger;
254 lldb::StreamFileSP m_input_sp;
255 lldb::StreamFileSP m_output_sp;
256 lldb::StreamFileSP m_error_sp;
257 Predicate<bool> m_popped;
265 DISALLOW_COPY_AND_ASSIGN (IOHandler);
268 //------------------------------------------------------------------
269 /// A delegate class for use with IOHandler subclasses.
271 /// The IOHandler delegate is designed to be mixed into classes so
272 /// they can use an IOHandler subclass to fetch input and notify the
273 /// object that inherits from this delegate class when a token is
275 //------------------------------------------------------------------
276 class IOHandlerDelegate
279 enum class Completion {
285 IOHandlerDelegate (Completion completion = Completion::None) :
286 m_completion(completion),
287 m_io_handler_done (false)
292 ~IOHandlerDelegate() = default;
295 IOHandlerActivated (IOHandler &io_handler)
300 IOHandlerDeactivated (IOHandler &io_handler)
305 IOHandlerComplete (IOHandler &io_handler,
306 const char *current_line,
308 const char *last_char,
309 int skip_first_n_matches,
311 StringList &matches);
314 IOHandlerGetFixIndentationCharacters ()
319 //------------------------------------------------------------------
320 /// Called when a new line is created or one of an identified set of
321 /// indentation characters is typed.
323 /// This function determines how much indentation should be added
324 /// or removed to match the recommended amount for the final line.
326 /// @param[in] io_handler
327 /// The IOHandler that responsible for input.
330 /// The current input up to the line to be corrected. Lines
331 /// following the line containing the cursor are not included.
333 /// @param[in] cursor_position
334 /// The number of characters preceding the cursor on the final
335 /// line at the time.
338 /// Returns an integer describing the number of spaces needed
339 /// to correct the indentation level. Positive values indicate
340 /// that spaces should be added, while negative values represent
341 /// spaces that should be removed.
342 //------------------------------------------------------------------
344 IOHandlerFixIndentation (IOHandler &io_handler,
345 const StringList &lines,
351 //------------------------------------------------------------------
352 /// Called when a line or lines have been retrieved.
354 /// This function can handle the current line and possibly call
355 /// IOHandler::SetIsDone(true) when the IO handler is done like when
356 /// "quit" is entered as a command, of when an empty line is
357 /// received. It is up to the delegate to determine when a line
358 /// should cause a IOHandler to exit.
359 //------------------------------------------------------------------
361 IOHandlerInputComplete (IOHandler &io_handler, std::string &data) = 0;
364 IOHandlerInputInterrupted (IOHandler &io_handler, std::string &data)
368 //------------------------------------------------------------------
369 /// Called to determine whether typing enter after the last line in
370 /// \a lines should end input. This function will not be called on
371 /// IOHandler objects that are getting single lines.
372 /// @param[in] io_handler
373 /// The IOHandler that responsible for updating the lines.
376 /// The current multi-line content. May be altered to provide
377 /// alternative input when complete.
380 /// Return an boolean to indicate whether input is complete,
381 /// true indicates that no additional input is necessary, while
382 /// false indicates that more input is required.
383 //------------------------------------------------------------------
385 IOHandlerIsInputComplete (IOHandler &io_handler,
388 // Impose no requirements for input to be considered
389 // complete. subclasses should do something more intelligent.
394 IOHandlerGetControlSequence (char ch)
396 return ConstString();
400 IOHandlerGetCommandPrefix ()
406 IOHandlerGetHelpPrologue ()
411 //------------------------------------------------------------------
412 // Intercept the IOHandler::Interrupt() calls and do something.
414 // Return true if the interrupt was handled, false if the IOHandler
415 // should continue to try handle the interrupt itself.
416 //------------------------------------------------------------------
418 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 : "")
446 ~IOHandlerDelegateMultiline() override = default;
449 IOHandlerGetControlSequence (char ch) override
452 return ConstString (m_end_line + "\n");
453 return ConstString();
457 IOHandlerIsInputComplete (IOHandler &io_handler,
458 StringList &lines) override
460 // Determine whether the end of input signal has been entered
461 const size_t num_lines = lines.GetSize();
462 if (num_lines > 0 && lines[num_lines - 1] == m_end_line)
464 // Remove the terminal line from "lines" so it doesn't appear in
465 // the resulting input and return true to indicate we are done
474 const std::string m_end_line;
477 class IOHandlerEditline : public IOHandler
480 IOHandlerEditline (Debugger &debugger,
481 IOHandler::Type type,
482 const char *editline_name, // Used for saving history files
484 const char *continuation_prompt,
487 uint32_t line_number_start, // If non-zero show line numbers starting at 'line_number_start'
488 IOHandlerDelegate &delegate);
490 IOHandlerEditline (Debugger &debugger,
491 IOHandler::Type type,
492 const lldb::StreamFileSP &input_sp,
493 const lldb::StreamFileSP &output_sp,
494 const lldb::StreamFileSP &error_sp,
496 const char *editline_name, // Used for saving history files
498 const char *continuation_prompt,
501 uint32_t line_number_start, // If non-zero show line numbers starting at 'line_number_start'
502 IOHandlerDelegate &delegate);
504 ~IOHandlerEditline() override;
513 Interrupt () override;
519 Activate () override;
522 Deactivate () override;
525 GetControlSequence (char ch) override
527 return m_delegate.IOHandlerGetControlSequence (ch);
531 GetCommandPrefix () override
533 return m_delegate.IOHandlerGetCommandPrefix ();
537 GetHelpPrologue () override
539 return m_delegate.IOHandlerGetHelpPrologue ();
543 GetPrompt () override;
546 SetPrompt (const char *prompt) override;
549 GetContinuationPrompt ();
552 SetContinuationPrompt (const char *prompt);
555 GetLine (std::string &line, bool &interrupted);
558 GetLines (StringList &lines, bool &interrupted);
561 SetBaseLineNumber (uint32_t line);
566 return m_interrupt_exits;
570 SetInterruptExits (bool b)
572 m_interrupt_exits = b;
576 GetCurrentLines () const
578 return m_current_lines_ptr;
582 GetCurrentLineIndex () const;
585 PrintAsync (Stream *stream, const char *s, size_t len) override;
588 #ifndef LLDB_DISABLE_LIBEDIT
590 IsInputCompleteCallback (Editline *editline,
595 FixIndentationCallback (Editline *editline,
596 const StringList &lines,
600 static int AutoCompleteCallback (const char *current_line,
602 const char *last_char,
603 int skip_first_n_matches,
610 #ifndef LLDB_DISABLE_LIBEDIT
611 std::unique_ptr<Editline> m_editline_ap;
613 IOHandlerDelegate &m_delegate;
614 std::string m_prompt;
615 std::string m_continuation_prompt;
616 StringList *m_current_lines_ptr;
617 uint32_t m_base_line_number; // If non-zero, then show line numbers in prompt
618 uint32_t m_curr_line_idx;
620 bool m_color_prompts;
621 bool m_interrupt_exits;
622 bool m_editing; // Set to true when fetching a line manually (not using libedit)
625 // The order of base classes is important. Look at the constructor of IOHandlerConfirm
627 class IOHandlerConfirm :
628 public IOHandlerDelegate,
629 public IOHandlerEditline
632 IOHandlerConfirm (Debugger &debugger,
634 bool default_response);
636 ~IOHandlerConfirm() override;
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);
697 ~IOHandlerCursesValueObjectList() override;
706 ValueObjectList m_valobj_list;
714 m_mutex(Mutex::eMutexTypeRecursive),
719 ~IOHandlerStack() = default;
724 Mutex::Locker locker (m_mutex);
725 return m_stack.size();
729 Push (const lldb::IOHandlerSP& sp)
733 Mutex::Locker locker (m_mutex);
734 sp->SetPopped (false);
735 m_stack.push_back (sp);
736 // Set m_top the non-locking IsTop() call
744 Mutex::Locker locker (m_mutex);
745 return m_stack.empty();
751 lldb::IOHandlerSP sp;
753 Mutex::Locker locker (m_mutex);
754 if (!m_stack.empty())
763 Mutex::Locker locker (m_mutex);
764 if (!m_stack.empty())
766 lldb::IOHandlerSP sp (m_stack.back());
768 sp->SetPopped (true);
770 // Set m_top the non-locking IsTop() call
772 m_top = (m_stack.empty() ? nullptr : m_stack.back().get());
782 IsTop (const lldb::IOHandlerSP &io_handler_sp) const
784 return m_top == io_handler_sp.get();
788 CheckTopIOHandlerTypes (IOHandler::Type top_type, IOHandler::Type second_top_type)
790 Mutex::Locker locker (m_mutex);
791 const size_t num_io_handlers = m_stack.size();
792 return (num_io_handlers >= 2 &&
793 m_stack[num_io_handlers-1]->GetType() == top_type &&
794 m_stack[num_io_handlers-2]->GetType() == second_top_type);
798 GetTopIOHandlerControlSequence (char ch)
800 return ((m_top != nullptr) ? m_top->GetControlSequence(ch) : ConstString());
804 GetTopIOHandlerCommandPrefix()
806 return ((m_top != nullptr) ? m_top->GetCommandPrefix() : nullptr);
810 GetTopIOHandlerHelpPrologue()
812 return ((m_top != nullptr) ? m_top->GetHelpPrologue() : nullptr);
816 PrintAsync (Stream *stream, const char *s, size_t len);
819 typedef std::vector<lldb::IOHandlerSP> collection;
821 mutable Mutex m_mutex;
825 DISALLOW_COPY_AND_ASSIGN (IOHandlerStack);
828 } // namespace lldb_private
830 #endif // liblldb_IOHandler_h_