1 //===-- TTYState.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 //  Created by Greg Clayton on 3/26/07.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLDB_TOOLS_DEBUGSERVER_SOURCE_TTYSTATE_H
14 #define LLDB_TOOLS_DEBUGSERVER_SOURCE_TTYSTATE_H
15 
16 #include <cstdint>
17 #include <termios.h>
18 
19 class TTYState {
20 public:
21   TTYState();
22   ~TTYState();
23 
24   bool GetTTYState(int fd, bool saveProcessGroup);
25   bool SetTTYState() const;
26 
IsValid()27   bool IsValid() const {
28     return FileDescriptorValid() && TFlagsValid() && TTYStateValid();
29   }
FileDescriptorValid()30   bool FileDescriptorValid() const { return m_fd >= 0; }
TFlagsValid()31   bool TFlagsValid() const { return m_tflags != -1; }
TTYStateValid()32   bool TTYStateValid() const { return m_ttystateErr == 0; }
ProcessGroupValid()33   bool ProcessGroupValid() const { return m_processGroup != -1; }
34 
35 protected:
36   int m_fd; // File descriptor
37   int m_tflags;
38   int m_ttystateErr;
39   struct termios m_ttystate;
40   pid_t m_processGroup;
41 };
42 
43 class TTYStateSwitcher {
44 public:
45   TTYStateSwitcher();
46   ~TTYStateSwitcher();
47 
48   bool GetState(uint32_t idx, int fd, bool saveProcessGroup);
49   bool SetState(uint32_t idx) const;
NumStates()50   uint32_t NumStates() const { return sizeof(m_ttystates) / sizeof(TTYState); }
ValidStateIndex(uint32_t idx)51   bool ValidStateIndex(uint32_t idx) const { return idx < NumStates(); }
52 
53 protected:
54   mutable uint32_t m_currentState;
55   TTYState m_ttystates[2];
56 };
57 
58 #endif
59