TTYState.h revision 24943d2ee8bfaa7cf5893e4709143924157a5c1e
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 <termios.h>
18#include <stdint.h>
19
20class TTYState
21{
22public:
23    TTYState();
24    ~TTYState();
25
26    bool    GetTTYState (int fd, bool saveProcessGroup);
27    bool    SetTTYState () const;
28
29    bool    IsValid() const { return FileDescriptorValid() && TFlagsValid() && TTYStateValid(); }
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};
43
44
45class TTYStateSwitcher
46{
47public:
48    TTYStateSwitcher();
49    ~TTYStateSwitcher();
50
51    bool GetState(uint32_t idx, int fd, bool saveProcessGroup);
52    bool SetState(uint32_t idx) const;
53    uint32_t NumStates() const { return sizeof(m_ttystates)/sizeof(TTYState); }
54    bool ValidStateIndex(uint32_t idx) const { return idx < NumStates(); }
55
56protected:
57    mutable uint32_t    m_currentState;
58    TTYState            m_ttystates[2];
59};
60
61#endif