summaryrefslogtreecommitdiff
path: root/qtermwidget/kpty.h
blob: 828683418c40e9f557095fbe28a9b296b2d5eefb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* This file is part of the KDE libraries

    Copyright (C) 2003,2007 Oswald Buddenhagen <ossi@kde.org>

    Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#ifndef kpty_h
#define kpty_h

#include <QtCore>

struct KPtyPrivate;
struct termios;

/**
 * Provides primitives for opening & closing a pseudo TTY pair, assigning the
 * controlling TTY, utmp registration and setting various terminal attributes.
 */
class KPty {
    Q_DECLARE_PRIVATE(KPty)

public:

  /**
   * Constructor
   */
  KPty();

  /**
   * Destructor:
   *
   *  If the pty is still open, it will be closed. Note, however, that
   *  an utmp registration is @em not undone.
  */
  ~KPty();

  /**
   * Create a pty master/slave pair.
   *
   * @return true if a pty pair was successfully opened
   */
  bool open();

  /**
   * Close the pty master/slave pair.
   */
  void close();

  /**
   * Close the pty slave descriptor.
   *
   * When creating the pty, KPty also opens the slave and keeps it open.
   * Consequently the master will never receive an EOF notification.
   * Usually this is the desired behavior, as a closed pty slave can be
   * reopened any time - unlike a pipe or socket. However, in some cases
   * pipe-alike behavior might be desired.
   *
   * After this function was called, slaveFd() and setCTty() cannot be
   * used.
   */
  void closeSlave();

  /**
   * Creates a new session and process group and makes this pty the
   * controlling tty.
   */
  void setCTty();

  /**
   * Creates an utmp entry for the tty.
   * This function must be called after calling setCTty and
   * making this pty the stdin.
   * @param user the user to be logged on
   * @param remotehost the host from which the login is coming. This is
   *  @em not the local host. For remote logins it should be the hostname
   *  of the client. For local logins from inside an X session it should
   *  be the name of the X display. Otherwise it should be empty.
   */
  void login(const char *user = 0, const char *remotehost = 0);

  /**
   * Removes the utmp entry for this tty.
   */
  void logout();

  /**
   * Wrapper around tcgetattr(3).
   *
   * This function can be used only while the PTY is open.
   * You will need an #include &lt;termios.h&gt; to do anything useful
   * with it.
   *
   * @param ttmode a pointer to a termios structure.
   *  Note: when declaring ttmode, @c struct @c ::termios must be used -
   *  without the '::' some version of HP-UX thinks, this declares
   *  the struct in your class, in your method.
   * @return @c true on success, false otherwise
   */
  bool tcGetAttr(struct ::termios *ttmode) const;

  /**
   * Wrapper around tcsetattr(3) with mode TCSANOW.
   *
   * This function can be used only while the PTY is open.
   *
   * @param ttmode a pointer to a termios structure.
   * @return @c true on success, false otherwise. Note that success means
   *  that @em at @em least @em one attribute could be set.
   */
  bool tcSetAttr(struct ::termios *ttmode);

  /**
   * Change the logical (screen) size of the pty.
   * The default is 24 lines by 80 columns.
   *
   * This function can be used only while the PTY is open.
   *
   * @param lines the number of rows
   * @param columns the number of columns
   * @return @c true on success, false otherwise
   */
  bool setWinSize(int lines, int columns);

  /**
   * Set whether the pty should echo input.
   *
   * Echo is on by default.
   * If the output of automatically fed (non-interactive) PTY clients
   * needs to be parsed, disabling echo often makes it much simpler.
   *
   * This function can be used only while the PTY is open.
   *
   * @param echo true if input should be echoed.
   * @return @c true on success, false otherwise
   */
  bool setEcho(bool echo);

  /**
   * @return the name of the slave pty device.
   *
   * This function should be called only while the pty is open.
   */
  const char *ttyName() const;

  /**
   * @return the file descriptor of the master pty
   *
   * This function should be called only while the pty is open.
   */
  int masterFd() const;

  /**
   * @return the file descriptor of the slave pty
   *
   * This function should be called only while the pty slave is open.
   */
  int slaveFd() const;

protected:
  /**
   * @internal
   */
  KPty(KPtyPrivate *d);

  /**
   * @internal
   */
  KPtyPrivate * const d_ptr;
};

#endif