Terminal.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //===-- Terminal.h ----------------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLDB_HOST_TERMINAL_H
  9. #define LLDB_HOST_TERMINAL_H
  10. #if defined(__cplusplus)
  11. #include "lldb/Host/Config.h"
  12. #include "lldb/lldb-private.h"
  13. struct termios;
  14. namespace lldb_private {
  15. class Terminal {
  16. public:
  17. Terminal(int fd = -1) : m_fd(fd) {}
  18. ~Terminal() {}
  19. bool IsATerminal() const;
  20. int GetFileDescriptor() const { return m_fd; }
  21. void SetFileDescriptor(int fd) { m_fd = fd; }
  22. bool FileDescriptorIsValid() const { return m_fd != -1; }
  23. void Clear() { m_fd = -1; }
  24. bool SetEcho(bool enabled);
  25. bool SetCanonical(bool enabled);
  26. protected:
  27. int m_fd; // This may or may not be a terminal file descriptor
  28. };
  29. /// \class State Terminal.h "lldb/Host/Terminal.h"
  30. /// A terminal state saving/restoring class.
  31. ///
  32. /// This class can be used to remember the terminal state for a file
  33. /// descriptor and later restore that state as it originally was.
  34. class TerminalState {
  35. public:
  36. /// Default constructor
  37. TerminalState();
  38. /// Destructor
  39. ~TerminalState();
  40. /// Save the TTY state for \a fd.
  41. ///
  42. /// Save the current state of the TTY for the file descriptor "fd" and if
  43. /// "save_process_group" is true, attempt to save the process group info for
  44. /// the TTY.
  45. ///
  46. /// \param[in] fd
  47. /// The file descriptor to save the state of.
  48. ///
  49. /// \param[in] save_process_group
  50. /// If \b true, save the process group settings, else do not
  51. /// save the process group settings for a TTY.
  52. ///
  53. /// \return
  54. /// Returns \b true if \a fd describes a TTY and if the state
  55. /// was able to be saved, \b false otherwise.
  56. bool Save(int fd, bool save_process_group);
  57. /// Restore the TTY state to the cached state.
  58. ///
  59. /// Restore the state of the TTY using the cached values from a previous
  60. /// call to TerminalState::Save(int,bool).
  61. ///
  62. /// \return
  63. /// Returns \b true if the TTY state was successfully restored,
  64. /// \b false otherwise.
  65. bool Restore() const;
  66. /// Test for valid cached TTY state information.
  67. ///
  68. /// \return
  69. /// Returns \b true if this object has valid saved TTY state
  70. /// settings that can be used to restore a previous state,
  71. /// \b false otherwise.
  72. bool IsValid() const;
  73. void Clear();
  74. protected:
  75. /// Test if tflags is valid.
  76. ///
  77. /// \return
  78. /// Returns \b true if \a m_tflags is valid and can be restored,
  79. /// \b false otherwise.
  80. bool TFlagsIsValid() const;
  81. /// Test if ttystate is valid.
  82. ///
  83. /// \return
  84. /// Returns \b true if \a m_ttystate is valid and can be
  85. /// restored, \b false otherwise.
  86. bool TTYStateIsValid() const;
  87. /// Test if the process group information is valid.
  88. ///
  89. /// \return
  90. /// Returns \b true if \a m_process_group is valid and can be
  91. /// restored, \b false otherwise.
  92. bool ProcessGroupIsValid() const;
  93. // Member variables
  94. Terminal m_tty; ///< A terminal
  95. int m_tflags; ///< Cached tflags information.
  96. #if LLDB_ENABLE_TERMIOS
  97. std::unique_ptr<struct termios>
  98. m_termios_up; ///< Cached terminal state information.
  99. #endif
  100. lldb::pid_t m_process_group; ///< Cached process group information.
  101. };
  102. /// \class TerminalStateSwitcher Terminal.h "lldb/Host/Terminal.h"
  103. /// A TTY state switching class.
  104. ///
  105. /// This class can be used to remember 2 TTY states for a given file
  106. /// descriptor and switch between the two states.
  107. class TerminalStateSwitcher {
  108. public:
  109. /// Constructor
  110. TerminalStateSwitcher();
  111. /// Destructor
  112. ~TerminalStateSwitcher();
  113. /// Get the number of possible states to save.
  114. ///
  115. /// \return
  116. /// The number of states that this TTY switcher object contains.
  117. uint32_t GetNumberOfStates() const;
  118. /// Restore the TTY state for state at index \a idx.
  119. ///
  120. /// \return
  121. /// Returns \b true if the TTY state was successfully restored,
  122. /// \b false otherwise.
  123. bool Restore(uint32_t idx) const;
  124. /// Save the TTY state information for the state at index \a idx. The TTY
  125. /// state is saved for the file descriptor \a fd and the process group
  126. /// information will also be saved if requested by \a save_process_group.
  127. ///
  128. /// \param[in] idx
  129. /// The index into the state array where the state should be
  130. /// saved.
  131. ///
  132. /// \param[in] fd
  133. /// The file descriptor for which to save the settings.
  134. ///
  135. /// \param[in] save_process_group
  136. /// If \b true, save the process group information for the TTY.
  137. ///
  138. /// \return
  139. /// Returns \b true if the save was successful, \b false
  140. /// otherwise.
  141. bool Save(uint32_t idx, int fd, bool save_process_group);
  142. protected:
  143. // Member variables
  144. mutable uint32_t m_currentState; ///< The currently active TTY state index.
  145. TerminalState
  146. m_ttystates[2]; ///< The array of TTY states that holds saved TTY info.
  147. };
  148. } // namespace lldb_private
  149. #endif // #if defined(__cplusplus)
  150. #endif // LLDB_HOST_TERMINAL_H