1 | /* This file is part of the KDE libraries |
---|
2 | |
---|
3 | Copyright (C) 2003,2007 Oswald Buddenhagen <ossi@kde.org> |
---|
4 | |
---|
5 | Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008 |
---|
6 | |
---|
7 | This library is free software; you can redistribute it and/or |
---|
8 | modify it under the terms of the GNU Library General Public |
---|
9 | License as published by the Free Software Foundation; either |
---|
10 | version 2 of the License, or (at your option) any later version. |
---|
11 | |
---|
12 | This library is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
15 | Library General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU Library General Public License |
---|
18 | along with this library; see the file COPYING.LIB. If not, write to |
---|
19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
---|
20 | Boston, MA 02110-1301, USA. |
---|
21 | */ |
---|
22 | |
---|
23 | #ifndef kpty_h |
---|
24 | #define kpty_h |
---|
25 | |
---|
26 | #include <QObject> |
---|
27 | |
---|
28 | #include <memory> |
---|
29 | |
---|
30 | class KPtyPrivate; |
---|
31 | struct termios; |
---|
32 | |
---|
33 | /** |
---|
34 | * Provides primitives for opening & closing a pseudo TTY pair, assigning the |
---|
35 | * controlling TTY, utmp registration and setting various terminal attributes. |
---|
36 | */ |
---|
37 | class KPty { |
---|
38 | Q_DECLARE_PRIVATE(KPty) |
---|
39 | |
---|
40 | public: |
---|
41 | |
---|
42 | /** |
---|
43 | * Constructor |
---|
44 | */ |
---|
45 | KPty(); |
---|
46 | |
---|
47 | /** |
---|
48 | * Destructor: |
---|
49 | * |
---|
50 | * If the pty is still open, it will be closed. Note, however, that |
---|
51 | * an utmp registration is @em not undone. |
---|
52 | */ |
---|
53 | ~KPty(); |
---|
54 | |
---|
55 | KPty(const KPty &) = delete; |
---|
56 | KPty &operator=(const KPty &) = delete; |
---|
57 | |
---|
58 | /** |
---|
59 | * Create a pty master/slave pair. |
---|
60 | * |
---|
61 | * @return true if a pty pair was successfully opened |
---|
62 | */ |
---|
63 | bool open(); |
---|
64 | |
---|
65 | bool open(int fd); |
---|
66 | |
---|
67 | /** |
---|
68 | * Close the pty master/slave pair. |
---|
69 | */ |
---|
70 | void close(); |
---|
71 | |
---|
72 | /** |
---|
73 | * Close the pty slave descriptor. |
---|
74 | * |
---|
75 | * When creating the pty, KPty also opens the slave and keeps it open. |
---|
76 | * Consequently the master will never receive an EOF notification. |
---|
77 | * Usually this is the desired behavior, as a closed pty slave can be |
---|
78 | * reopened any time - unlike a pipe or socket. However, in some cases |
---|
79 | * pipe-alike behavior might be desired. |
---|
80 | * |
---|
81 | * After this function was called, slaveFd() and setCTty() cannot be |
---|
82 | * used. |
---|
83 | */ |
---|
84 | void closeSlave(); |
---|
85 | bool openSlave(); |
---|
86 | |
---|
87 | /** |
---|
88 | * Creates a new session and process group and makes this pty the |
---|
89 | * controlling tty. |
---|
90 | */ |
---|
91 | void setCTty(); |
---|
92 | |
---|
93 | /** |
---|
94 | * Creates an utmp entry for the tty. |
---|
95 | * This function must be called after calling setCTty and |
---|
96 | * making this pty the stdin. |
---|
97 | * @param user the user to be logged on |
---|
98 | * @param remotehost the host from which the login is coming. This is |
---|
99 | * @em not the local host. For remote logins it should be the hostname |
---|
100 | * of the client. For local logins from inside an X session it should |
---|
101 | * be the name of the X display. Otherwise it should be empty. |
---|
102 | */ |
---|
103 | void login(const char * user = nullptr, const char * remotehost = nullptr); |
---|
104 | |
---|
105 | /** |
---|
106 | * Removes the utmp entry for this tty. |
---|
107 | */ |
---|
108 | void logout(); |
---|
109 | |
---|
110 | /** |
---|
111 | * Wrapper around tcgetattr(3). |
---|
112 | * |
---|
113 | * This function can be used only while the PTY is open. |
---|
114 | * You will need an #include <termios.h> to do anything useful |
---|
115 | * with it. |
---|
116 | * |
---|
117 | * @param ttmode a pointer to a termios structure. |
---|
118 | * Note: when declaring ttmode, @c struct @c ::termios must be used - |
---|
119 | * without the '::' some version of HP-UX thinks, this declares |
---|
120 | * the struct in your class, in your method. |
---|
121 | * @return @c true on success, false otherwise |
---|
122 | */ |
---|
123 | bool tcGetAttr(struct ::termios * ttmode) const; |
---|
124 | |
---|
125 | /** |
---|
126 | * Wrapper around tcsetattr(3) with mode TCSANOW. |
---|
127 | * |
---|
128 | * This function can be used only while the PTY is open. |
---|
129 | * |
---|
130 | * @param ttmode a pointer to a termios structure. |
---|
131 | * @return @c true on success, false otherwise. Note that success means |
---|
132 | * that @em at @em least @em one attribute could be set. |
---|
133 | */ |
---|
134 | bool tcSetAttr(struct ::termios * ttmode); |
---|
135 | |
---|
136 | /** |
---|
137 | * Change the logical (screen) size of the pty. |
---|
138 | * The default is 24 lines by 80 columns. |
---|
139 | * |
---|
140 | * This function can be used only while the PTY is open. |
---|
141 | * |
---|
142 | * @param lines the number of rows |
---|
143 | * @param columns the number of columns |
---|
144 | * @return @c true on success, false otherwise |
---|
145 | */ |
---|
146 | bool setWinSize(int lines, int columns); |
---|
147 | |
---|
148 | /** |
---|
149 | * Set whether the pty should echo input. |
---|
150 | * |
---|
151 | * Echo is on by default. |
---|
152 | * If the output of automatically fed (non-interactive) PTY clients |
---|
153 | * needs to be parsed, disabling echo often makes it much simpler. |
---|
154 | * |
---|
155 | * This function can be used only while the PTY is open. |
---|
156 | * |
---|
157 | * @param echo true if input should be echoed. |
---|
158 | * @return @c true on success, false otherwise |
---|
159 | */ |
---|
160 | bool setEcho(bool echo); |
---|
161 | |
---|
162 | /** |
---|
163 | * @return the name of the slave pty device. |
---|
164 | * |
---|
165 | * This function should be called only while the pty is open. |
---|
166 | */ |
---|
167 | const char * ttyName() const; |
---|
168 | |
---|
169 | /** |
---|
170 | * @return the file descriptor of the master pty |
---|
171 | * |
---|
172 | * This function should be called only while the pty is open. |
---|
173 | */ |
---|
174 | int masterFd() const; |
---|
175 | |
---|
176 | /** |
---|
177 | * @return the file descriptor of the slave pty |
---|
178 | * |
---|
179 | * This function should be called only while the pty slave is open. |
---|
180 | */ |
---|
181 | int slaveFd() const; |
---|
182 | |
---|
183 | protected: |
---|
184 | /** |
---|
185 | * @internal |
---|
186 | */ |
---|
187 | KPty(KPtyPrivate * d); |
---|
188 | |
---|
189 | /** |
---|
190 | * @internal |
---|
191 | */ |
---|
192 | std::unique_ptr<KPtyPrivate> const d_ptr; |
---|
193 | }; |
---|
194 | |
---|
195 | #endif |
---|
196 | |
---|