Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:49

0001 //===-- Terminal.h ----------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_HOST_TERMINAL_H
0010 #define LLDB_HOST_TERMINAL_H
0011 
0012 #include "lldb/lldb-private.h"
0013 #include "llvm/Support/Error.h"
0014 
0015 namespace lldb_private {
0016 
0017 class TerminalState;
0018 
0019 class Terminal {
0020 public:
0021   enum class Parity {
0022     No,
0023     Even,
0024     Odd,
0025     Space,
0026     Mark,
0027   };
0028 
0029   enum class ParityCheck {
0030     // No parity checking
0031     No,
0032     // Replace erraneous bytes with NUL
0033     ReplaceWithNUL,
0034     // Ignore erraneous bytes
0035     Ignore,
0036     // Mark erraneous bytes by prepending them with \xFF\x00; real \xFF
0037     // is escaped to \xFF\xFF
0038     Mark,
0039   };
0040 
0041   Terminal(int fd = -1) : m_fd(fd) {}
0042 
0043   ~Terminal() = default;
0044 
0045   bool IsATerminal() const;
0046 
0047   int GetFileDescriptor() const { return m_fd; }
0048 
0049   void SetFileDescriptor(int fd) { m_fd = fd; }
0050 
0051   bool FileDescriptorIsValid() const { return m_fd != -1; }
0052 
0053   void Clear() { m_fd = -1; }
0054 
0055   llvm::Error SetEcho(bool enabled);
0056 
0057   llvm::Error SetCanonical(bool enabled);
0058 
0059   llvm::Error SetRaw();
0060 
0061   llvm::Error SetBaudRate(unsigned int baud_rate);
0062 
0063   llvm::Error SetStopBits(unsigned int stop_bits);
0064 
0065   llvm::Error SetParity(Parity parity);
0066 
0067   llvm::Error SetParityCheck(ParityCheck parity_check);
0068 
0069   llvm::Error SetHardwareFlowControl(bool enabled);
0070 
0071 protected:
0072   struct Data;
0073 
0074   int m_fd; // This may or may not be a terminal file descriptor
0075 
0076   llvm::Expected<Data> GetData();
0077   llvm::Error SetData(const Data &data);
0078 
0079   friend class TerminalState;
0080 };
0081 
0082 /// \class TerminalState Terminal.h "lldb/Host/Terminal.h"
0083 /// A RAII-friendly terminal state saving/restoring class.
0084 ///
0085 /// This class can be used to remember the terminal state for a file
0086 /// descriptor and later restore that state as it originally was.
0087 class TerminalState {
0088 public:
0089   /// Construct a new instance and optionally save terminal state.
0090   ///
0091   /// \param[in] term
0092   ///     The Terminal instance holding the file descriptor to save the state
0093   ///     of.  If the instance is not associated with a fd, no state will
0094   ///     be saved.
0095   ///
0096   /// \param[in] save_process_group
0097   ///     If \b true, save the process group settings, else do not
0098   ///     save the process group settings for a TTY.
0099   TerminalState(Terminal term = -1, bool save_process_group = false);
0100 
0101   /// Destroy the instance, restoring terminal state if saved.  If restoring
0102   /// state is undesirable, the instance needs to be reset before destruction.
0103   ~TerminalState();
0104 
0105   /// Save the TTY state for \a fd.
0106   ///
0107   /// Save the current state of the TTY for the file descriptor "fd" and if
0108   /// "save_process_group" is true, attempt to save the process group info for
0109   /// the TTY.
0110   ///
0111   /// \param[in] term
0112   ///     The Terminal instance holding fd to save.
0113   ///
0114   /// \param[in] save_process_group
0115   ///     If \b true, save the process group settings, else do not
0116   ///     save the process group settings for a TTY.
0117   ///
0118   /// \return
0119   ///     Returns \b true if \a fd describes a TTY and if the state
0120   ///     was able to be saved, \b false otherwise.
0121   bool Save(Terminal term, bool save_process_group);
0122 
0123   /// Restore the TTY state to the cached state.
0124   ///
0125   /// Restore the state of the TTY using the cached values from a previous
0126   /// call to TerminalState::Save(int,bool).
0127   ///
0128   /// \return
0129   ///     Returns \b true if the TTY state was successfully restored,
0130   ///     \b false otherwise.
0131   bool Restore() const;
0132 
0133   /// Test for valid cached TTY state information.
0134   ///
0135   /// \return
0136   ///     Returns \b true if this object has valid saved TTY state
0137   ///     settings that can be used to restore a previous state,
0138   ///     \b false otherwise.
0139   bool IsValid() const;
0140 
0141   void Clear();
0142 
0143 protected:
0144   /// Test if tflags is valid.
0145   ///
0146   /// \return
0147   ///     Returns \b true if \a m_tflags is valid and can be restored,
0148   ///     \b false otherwise.
0149   bool TFlagsIsValid() const;
0150 
0151   /// Test if ttystate is valid.
0152   ///
0153   /// \return
0154   ///     Returns \b true if \a m_ttystate is valid and can be
0155   ///     restored, \b false otherwise.
0156   bool TTYStateIsValid() const;
0157 
0158   /// Test if the process group information is valid.
0159   ///
0160   /// \return
0161   ///     Returns \b true if \a m_process_group is valid and can be
0162   ///     restored, \b false otherwise.
0163   bool ProcessGroupIsValid() const;
0164 
0165   // Member variables
0166   Terminal m_tty;                         ///< A terminal
0167   int m_tflags = -1;                      ///< Cached tflags information.
0168   std::unique_ptr<Terminal::Data> m_data; ///< Platform-specific implementation.
0169   lldb::pid_t m_process_group = -1;       ///< Cached process group information.
0170 };
0171 
0172 } // namespace lldb_private
0173 
0174 #endif // LLDB_HOST_TERMINAL_H