PseudoTerminal.h revision fa1502444821fb699b14883bd24188c85a8793e0
1//===-- PseudoTerminal.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 1/8/08.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef __PseudoTerminal_h__
15#define __PseudoTerminal_h__
16
17#include <fcntl.h>
18#include <termios.h>
19#include <string>
20
21class PseudoTerminal
22{
23public:
24    enum {
25        invalid_fd = -1,
26        invalid_pid = -1
27    };
28
29    enum Error
30    {
31        success                                     = 0,
32        err_posix_openpt_failed                     = -2,
33        err_grantpt_failed                          = -3,
34        err_unlockpt_failed                         = -4,
35        err_ptsname_failed                          = -5,
36        err_open_slave_failed                       = -6,
37        err_fork_failed                             = -7,
38        err_setsid_failed                           = -8,
39        err_failed_to_acquire_controlling_terminal  = -9,
40        err_dup2_failed_on_stdin                    = -10,
41        err_dup2_failed_on_stdout                   = -11,
42        err_dup2_failed_on_stderr                   = -12
43    };
44    //------------------------------------------------------------------
45    // Constructors and Destructors
46    //------------------------------------------------------------------
47                PseudoTerminal ();
48                ~PseudoTerminal ();
49
50    void        CloseMaster ();
51    void        CloseSlave ();
52    Error       OpenFirstAvailableMaster (int oflag);
53    Error       OpenSlave (int oflag);
54    int         MasterFD () const { return m_master_fd; }
55    int         SlaveFD () const { return m_slave_fd; }
56    int         ReleaseMasterFD ()
57                {
58                    // Release ownership of the master pseudo terminal file
59                    // descriptor without closing it. (the destructor for this
60                    // class will close it otherwise!)
61                    int fd = m_master_fd;
62                    m_master_fd = invalid_fd;
63                    return fd;
64                }
65    int         ReleaseSlaveFD ()
66                {
67                    // Release ownership of the slave pseudo terminal file
68                    // descriptor without closing it (the destructor for this
69                    // class will close it otherwise!)
70                    int fd = m_slave_fd;
71                    m_slave_fd = invalid_fd;
72                    return fd;
73                }
74
75    const char* SlaveName () const;
76
77    pid_t       Fork(Error& error);
78protected:
79    //------------------------------------------------------------------
80    // Classes that inherit from PseudoTerminal can see and modify these
81    //------------------------------------------------------------------
82    int m_master_fd;
83    int m_slave_fd;
84
85private:
86    //------------------------------------------------------------------
87    // Outlaw copy and assignment constructors
88    //------------------------------------------------------------------
89    PseudoTerminal(const PseudoTerminal& rhs);
90    PseudoTerminal& operator=(const PseudoTerminal& rhs);
91
92};
93
94#endif // #ifndef __PseudoTerminal_h__
95