LLDB  mainline
Terminal.h
Go to the documentation of this file.
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 
9 #ifndef LLDB_HOST_TERMINAL_H
10 #define LLDB_HOST_TERMINAL_H
11 #if defined(__cplusplus)
12 
13 #include "lldb/Host/Config.h"
14 #include "lldb/lldb-private.h"
15 #include "llvm/Support/Error.h"
16 
17 namespace lldb_private {
18 
19 class TerminalState;
20 
21 class Terminal {
22 public:
23  enum class Parity {
24  No,
25  Even,
26  Odd,
27  Space,
28  Mark,
29  };
30 
31  Terminal(int fd = -1) : m_fd(fd) {}
32 
33  ~Terminal() = default;
34 
35  bool IsATerminal() const;
36 
37  int GetFileDescriptor() const { return m_fd; }
38 
39  void SetFileDescriptor(int fd) { m_fd = fd; }
40 
41  bool FileDescriptorIsValid() const { return m_fd != -1; }
42 
43  void Clear() { m_fd = -1; }
44 
45  llvm::Error SetEcho(bool enabled);
46 
47  llvm::Error SetCanonical(bool enabled);
48 
49  llvm::Error SetRaw();
50 
51  llvm::Error SetBaudRate(unsigned int baud_rate);
52 
53  llvm::Error SetStopBits(unsigned int stop_bits);
54 
55  llvm::Error SetParity(Parity parity);
56 
57  llvm::Error SetHardwareFlowControl(bool enabled);
58 
59 protected:
60  struct Data;
61 
62  int m_fd; // This may or may not be a terminal file descriptor
63 
64  llvm::Expected<Data> GetData();
65  llvm::Error SetData(const Data &data);
66 
67  friend class TerminalState;
68 };
69 
70 /// \class TerminalState Terminal.h "lldb/Host/Terminal.h"
71 /// A RAII-friendly terminal state saving/restoring class.
72 ///
73 /// This class can be used to remember the terminal state for a file
74 /// descriptor and later restore that state as it originally was.
75 class TerminalState {
76 public:
77  /// Construct a new instance and optionally save terminal state.
78  ///
79  /// \param[in] term
80  /// The Terminal instance holding the file descriptor to save the state
81  /// of. If the instance is not associated with a fd, no state will
82  /// be saved.
83  ///
84  /// \param[in] save_process_group
85  /// If \b true, save the process group settings, else do not
86  /// save the process group settings for a TTY.
87  TerminalState(Terminal term = -1, bool save_process_group = false);
88 
89  /// Destroy the instance, restoring terminal state if saved. If restoring
90  /// state is undesirable, the instance needs to be reset before destruction.
91  ~TerminalState();
92 
93  /// Save the TTY state for \a fd.
94  ///
95  /// Save the current state of the TTY for the file descriptor "fd" and if
96  /// "save_process_group" is true, attempt to save the process group info for
97  /// the TTY.
98  ///
99  /// \param[in] term
100  /// The Terminal instance holding fd to save.
101  ///
102  /// \param[in] save_process_group
103  /// If \b true, save the process group settings, else do not
104  /// save the process group settings for a TTY.
105  ///
106  /// \return
107  /// Returns \b true if \a fd describes a TTY and if the state
108  /// was able to be saved, \b false otherwise.
109  bool Save(Terminal term, bool save_process_group);
110 
111  /// Restore the TTY state to the cached state.
112  ///
113  /// Restore the state of the TTY using the cached values from a previous
114  /// call to TerminalState::Save(int,bool).
115  ///
116  /// \return
117  /// Returns \b true if the TTY state was successfully restored,
118  /// \b false otherwise.
119  bool Restore() const;
120 
121  /// Test for valid cached TTY state information.
122  ///
123  /// \return
124  /// Returns \b true if this object has valid saved TTY state
125  /// settings that can be used to restore a previous state,
126  /// \b false otherwise.
127  bool IsValid() const;
128 
129  void Clear();
130 
131 protected:
132  /// Test if tflags is valid.
133  ///
134  /// \return
135  /// Returns \b true if \a m_tflags is valid and can be restored,
136  /// \b false otherwise.
137  bool TFlagsIsValid() const;
138 
139  /// Test if ttystate is valid.
140  ///
141  /// \return
142  /// Returns \b true if \a m_ttystate is valid and can be
143  /// restored, \b false otherwise.
144  bool TTYStateIsValid() const;
145 
146  /// Test if the process group information is valid.
147  ///
148  /// \return
149  /// Returns \b true if \a m_process_group is valid and can be
150  /// restored, \b false otherwise.
151  bool ProcessGroupIsValid() const;
152 
153  // Member variables
154  Terminal m_tty; ///< A terminal
155  int m_tflags = -1; ///< Cached tflags information.
156  std::unique_ptr<Terminal::Data> m_data; ///< Platform-specific implementation.
157  lldb::pid_t m_process_group = -1; ///< Cached process group information.
158 };
159 
160 } // namespace lldb_private
161 
162 #endif // #if defined(__cplusplus)
163 #endif // LLDB_HOST_TERMINAL_H
Terminal
lldb-private.h
lldb::pid_t
uint64_t pid_t
Definition: lldb-types.h:85
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
Error
llvm::Error Error
Definition: UdtRecordCompleter.cpp:29