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 <QtCore> |
---|
27 | |
---|
28 | struct KPtyPrivate; |
---|
29 | struct termios; |
---|
30 | |
---|
31 | /** |
---|
32 | * Provides primitives for opening & closing a pseudo TTY pair, assigning the |
---|
33 | * controlling TTY, utmp registration and setting various terminal attributes. |
---|
34 | */ |
---|
35 | class KPty { |
---|
36 | Q_DECLARE_PRIVATE(KPty) |
---|
37 | |
---|
38 | public: |
---|
39 | |
---|
40 | /** |
---|
41 | * Constructor |
---|
42 | */ |
---|
43 | KPty(); |
---|
44 | |
---|
45 | /** |
---|
46 | * Destructor: |
---|
47 | * |
---|
48 | * If the pty is still open, it will be closed. Note, however, that |
---|
49 | * an utmp registration is @em not undone. |
---|
50 | */ |
---|
51 | ~KPty(); |
---|
52 | |
---|
53 | /** |
---|
54 | * Create a pty master/slave pair. |
---|
55 | * |
---|
56 | * @return true if a pty pair was successfully opened |
---|
57 | */ |
---|
58 | bool open(); |
---|
59 | |
---|
60 | /** |
---|
61 | * Close the pty master/slave pair. |
---|
62 | */ |
---|
63 | void close(); |
---|
64 | |
---|
65 | /** |
---|
66 | * Close the pty slave descriptor. |
---|
67 | * |
---|
68 | * When creating the pty, KPty also opens the slave and keeps it open. |
---|
69 | * Consequently the master will never receive an EOF notification. |
---|
70 | * Usually this is the desired behavior, as a closed pty slave can be |
---|
71 | * reopened any time - unlike a pipe or socket. However, in some cases |
---|
72 | * pipe-alike behavior might be desired. |
---|
73 | * |
---|
74 | * After this function was called, slaveFd() and setCTty() cannot be |
---|
75 | * used. |
---|
76 | */ |
---|
77 | void closeSlave(); |
---|
78 | |
---|
79 | /** |
---|
80 | * Creates a new session and process group and makes this pty the |
---|
81 | * controlling tty. |
---|
82 | */ |
---|
83 | void setCTty(); |
---|
84 | |
---|
85 | /** |
---|
86 | * Creates an utmp entry for the tty. |
---|
87 | * This function must be called after calling setCTty and |
---|
88 | * making this pty the stdin. |
---|
89 | * @param user the user to be logged on |
---|
90 | * @param remotehost the host from which the login is coming. This is |
---|
91 | * @em not the local host. For remote logins it should be the hostname |
---|
92 | * of the client. For local logins from inside an X session it should |
---|
93 | * be the name of the X display. Otherwise it should be empty. |
---|
94 | */ |
---|
95 | void login(const char *user = 0, const char *remotehost = 0); |
---|
96 | |
---|
97 | /** |
---|
98 | * Removes the utmp entry for this tty. |
---|
99 | */ |
---|
100 | void logout(); |
---|
101 | |
---|
102 | /** |
---|
103 | * Wrapper around tcgetattr(3). |
---|
104 | * |
---|
105 | * This function can be used only while the PTY is open. |
---|
106 | * You will need an #include <termios.h> to do anything useful |
---|
107 | * with it. |
---|
108 | * |
---|
109 | * @param ttmode a pointer to a termios structure. |
---|
110 | * Note: when declaring ttmode, @c struct @c ::termios must be used - |
---|
111 | * without the '::' some version of HP-UX thinks, this declares |
---|
112 | * the struct in your class, in your method. |
---|
113 | * @return @c true on success, false otherwise |
---|
114 | */ |
---|
115 | bool tcGetAttr(struct ::termios *ttmode) const; |
---|
116 | |
---|
117 | /** |
---|
118 | * Wrapper around tcsetattr(3) with mode TCSANOW. |
---|
119 | * |
---|
120 | * This function can be used only while the PTY is open. |
---|
121 | * |
---|
122 | * @param ttmode a pointer to a termios structure. |
---|
123 | * @return @c true on success, false otherwise. Note that success means |
---|
124 | * that @em at @em least @em one attribute could be set. |
---|
125 | */ |
---|
126 | bool tcSetAttr(struct ::termios *ttmode); |
---|
127 | |
---|
128 | /** |
---|
129 | * Change the logical (screen) size of the pty. |
---|
130 | * The default is 24 lines by 80 columns. |
---|
131 | * |
---|
132 | * This function can be used only while the PTY is open. |
---|
133 | * |
---|
134 | * @param lines the number of rows |
---|
135 | * @param columns the number of columns |
---|
136 | * @return @c true on success, false otherwise |
---|
137 | */ |
---|
138 | bool setWinSize(int lines, int columns); |
---|
139 | |
---|
140 | /** |
---|
141 | * Set whether the pty should echo input. |
---|
142 | * |
---|
143 | * Echo is on by default. |
---|
144 | * If the output of automatically fed (non-interactive) PTY clients |
---|
145 | * needs to be parsed, disabling echo often makes it much simpler. |
---|
146 | * |
---|
147 | * This function can be used only while the PTY is open. |
---|
148 | * |
---|
149 | * @param echo true if input should be echoed. |
---|
150 | * @return @c true on success, false otherwise |
---|
151 | */ |
---|
152 | bool setEcho(bool echo); |
---|
153 | |
---|
154 | /** |
---|
155 | * @return the name of the slave pty device. |
---|
156 | * |
---|
157 | * This function should be called only while the pty is open. |
---|
158 | */ |
---|
159 | const char *ttyName() const; |
---|
160 | |
---|
161 | /** |
---|
162 | * @return the file descriptor of the master pty |
---|
163 | * |
---|
164 | * This function should be called only while the pty is open. |
---|
165 | */ |
---|
166 | int masterFd() const; |
---|
167 | |
---|
168 | /** |
---|
169 | * @return the file descriptor of the slave pty |
---|
170 | * |
---|
171 | * This function should be called only while the pty slave is open. |
---|
172 | */ |
---|
173 | int slaveFd() const; |
---|
174 | |
---|
175 | protected: |
---|
176 | /** |
---|
177 | * @internal |
---|
178 | */ |
---|
179 | KPty(KPtyPrivate *d); |
---|
180 | |
---|
181 | /** |
---|
182 | * @internal |
---|
183 | */ |
---|
184 | KPtyPrivate * const d_ptr; |
---|
185 | }; |
---|
186 | |
---|
187 | #endif |
---|
188 | |
---|