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