Home | History | Annotate | Download | only in Host
      1 //===-- Terminal.h ----------------------------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #ifndef liblldb_Terminal_h_
     11 #define liblldb_Terminal_h_
     12 #if defined(__cplusplus)
     13 
     14 #include "lldb/lldb-private.h"
     15 
     16 struct termios;
     17 
     18 namespace lldb_private {
     19 
     20 class Terminal
     21 {
     22 public:
     23 
     24     Terminal (int fd = -1) :
     25         m_fd (fd)
     26     {
     27     }
     28 
     29     ~Terminal ()
     30     {
     31     }
     32 
     33     bool
     34     IsATerminal () const;
     35 
     36     int
     37     GetFileDescriptor () const
     38     {
     39         return m_fd;
     40     }
     41 
     42     void
     43     SetFileDescriptor (int fd)
     44     {
     45         m_fd = fd;
     46     }
     47 
     48     bool
     49     FileDescriptorIsValid () const
     50     {
     51         return m_fd != -1;
     52     }
     53 
     54     void
     55     Clear ()
     56     {
     57         m_fd = -1;
     58     }
     59 
     60     bool
     61     SetEcho (bool enabled);
     62 
     63     bool
     64     SetCanonical (bool enabled);
     65 
     66 protected:
     67     int m_fd;   // This may or may not be a terminal file descriptor
     68 };
     69 
     70 
     71 //----------------------------------------------------------------------
     72 /// @class State Terminal.h "lldb/Host/Terminal.h"
     73 /// @brief A terminal state saving/restoring class.
     74 ///
     75 /// This class can be used to remember the terminal state for a file
     76 /// descriptor and later restore that state as it originally was.
     77 //----------------------------------------------------------------------
     78 class TerminalState
     79 {
     80 public:
     81     //------------------------------------------------------------------
     82     /// Default constructor
     83     //------------------------------------------------------------------
     84     TerminalState();
     85 
     86     //------------------------------------------------------------------
     87     /// Destructor
     88     //------------------------------------------------------------------
     89     ~TerminalState();
     90 
     91     //------------------------------------------------------------------
     92     /// Save the TTY state for \a fd.
     93     ///
     94     /// Save the current state of the TTY for the file descriptor "fd"
     95     /// and if "save_process_group" is true, attempt to save the process
     96     /// group info for the TTY.
     97     ///
     98     /// @param[in] fd
     99     ///     The file descriptor to save the state of.
    100     ///
    101     /// @param[in] save_process_group
    102     ///     If \b true, save the process group settings, else do not
    103     ///     save the process group setttings for a TTY.
    104     ///
    105     /// @return
    106     ///     Returns \b true if \a fd describes a TTY and if the state
    107     ///     was able to be saved, \b false otherwise.
    108     //------------------------------------------------------------------
    109     bool
    110     Save (int fd, bool save_process_group);
    111 
    112     //------------------------------------------------------------------
    113     /// Restore the TTY state to the cached state.
    114     ///
    115     /// Restore the state of the TTY using the cached values from a
    116     /// previous call to TerminalState::Save(int,bool).
    117     ///
    118     /// @return
    119     ///     Returns \b true if the TTY state was successfully restored,
    120     ///     \b false otherwise.
    121     //------------------------------------------------------------------
    122     bool
    123     Restore () const;
    124 
    125     //------------------------------------------------------------------
    126     /// Test for valid cached TTY state information.
    127     ///
    128     /// @return
    129     ///     Returns \b true if this object has valid saved TTY state
    130     ///     settings that can be used to restore a previous state,
    131     ///     \b false otherwise.
    132     //------------------------------------------------------------------
    133     bool
    134     IsValid() const;
    135 
    136     void
    137     Clear ();
    138 
    139 protected:
    140 
    141     //------------------------------------------------------------------
    142     /// Test if tflags is valid.
    143     ///
    144     /// @return
    145     ///     Returns \b true if \a m_tflags is valid and can be restored,
    146     ///     \b false otherwise.
    147     //------------------------------------------------------------------
    148     bool
    149     TFlagsIsValid() const;
    150 
    151     //------------------------------------------------------------------
    152     /// Test if ttystate is valid.
    153     ///
    154     /// @return
    155     ///     Returns \b true if \a m_ttystate is valid and can be
    156     ///     restored, \b false otherwise.
    157     //------------------------------------------------------------------
    158     bool
    159     TTYStateIsValid() const;
    160 
    161     //------------------------------------------------------------------
    162     /// Test if the process group information is valid.
    163     ///
    164     /// @return
    165     ///     Returns \b true if \a m_process_group is valid and can be
    166     ///     restored, \b false otherwise.
    167     //------------------------------------------------------------------
    168     bool
    169     ProcessGroupIsValid() const;
    170 
    171     //------------------------------------------------------------------
    172     // Member variables
    173     //------------------------------------------------------------------
    174     Terminal        m_tty;          ///< A terminal
    175     int             m_tflags;       ///< Cached tflags information.
    176     std::unique_ptr<struct termios> m_termios_ap; ///< Cached terminal state information.
    177     lldb::pid_t     m_process_group;///< Cached process group information.
    178 
    179 };
    180 
    181 //----------------------------------------------------------------------
    182 /// @class TerminalStateSwitcher Terminal.h "lldb/Host/Terminal.h"
    183 /// @brief A TTY state switching class.
    184 ///
    185 /// This class can be used to remember 2 TTY states for a given file
    186 /// descriptor and switch between the two states.
    187 //----------------------------------------------------------------------
    188 class TerminalStateSwitcher
    189 {
    190 public:
    191     //------------------------------------------------------------------
    192     /// Constructor
    193     //------------------------------------------------------------------
    194     TerminalStateSwitcher();
    195 
    196     //------------------------------------------------------------------
    197     /// Destructor
    198     //------------------------------------------------------------------
    199     ~TerminalStateSwitcher();
    200 
    201     //------------------------------------------------------------------
    202     /// Get the number of possible states to save.
    203     ///
    204     /// @return
    205     ///     The number of states that this TTY switcher object contains.
    206     //------------------------------------------------------------------
    207     uint32_t
    208     GetNumberOfStates() const;
    209 
    210     //------------------------------------------------------------------
    211     /// Restore the TTY state for state at index \a idx.
    212     ///
    213     /// @return
    214     ///     Returns \b true if the TTY state was successfully restored,
    215     ///     \b false otherwise.
    216     //------------------------------------------------------------------
    217     bool
    218     Restore (uint32_t idx) const;
    219 
    220     //------------------------------------------------------------------
    221     /// Save the TTY state information for the state at index \a idx.
    222     /// The TTY state is saved for the file descriptor \a fd and
    223     /// the process group information will also be saved if requested
    224     /// by \a save_process_group.
    225     ///
    226     /// @param[in] idx
    227     ///     The index into the state array where the state should be
    228     ///     saved.
    229     ///
    230     /// @param[in] fd
    231     ///     The file descriptor for which to save the settings.
    232     ///
    233     /// @param[in] save_process_group
    234     ///     If \b true, save the process group information for the TTY.
    235     ///
    236     /// @return
    237     ///     Returns \b true if the save was successful, \b false
    238     ///     otherwise.
    239     //------------------------------------------------------------------
    240     bool
    241     Save (uint32_t idx, int fd, bool save_process_group);
    242 
    243 protected:
    244     //------------------------------------------------------------------
    245     // Member variables
    246     //------------------------------------------------------------------
    247     mutable uint32_t m_currentState; ///< The currently active TTY state index.
    248     TerminalState m_ttystates[2]; ///< The array of TTY states that holds saved TTY info.
    249 };
    250 
    251 } // namespace lldb_private
    252 
    253 #endif  // #if defined(__cplusplus)
    254 #endif  // #ifndef liblldb_Terminal_h_
    255