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
19class TTYState {
20public:
21  TTYState();
22  ~TTYState();
23
24  bool GetTTYState(int fd, bool saveProcessGroup);
25  bool SetTTYState() const;
26
27  bool IsValid() const {
28    return FileDescriptorValid() && TFlagsValid() && TTYStateValid();
29  }
30  bool FileDescriptorValid() const { return m_fd >= 0; }
31  bool TFlagsValid() const { return m_tflags != -1; }
32  bool TTYStateValid() const { return m_ttystateErr == 0; }
33  bool ProcessGroupValid() const { return m_processGroup != -1; }
34
35protected:
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
43class TTYStateSwitcher {
44public:
45  TTYStateSwitcher();
46  ~TTYStateSwitcher();
47
48  bool GetState(uint32_t idx, int fd, bool saveProcessGroup);
49  bool SetState(uint32_t idx) const;
50  uint32_t NumStates() const { return sizeof(m_ttystates) / sizeof(TTYState); }
51  bool ValidStateIndex(uint32_t idx) const { return idx < NumStates(); }
52
53protected:
54  mutable uint32_t m_currentState;
55  TTYState m_ttystates[2];
56};
57
58#endif
59