Add some tests that involve std::vector.
[kdbg.git] / kdbg / ttywnd.cpp
blobb23f2f48bca168b1aa8afa448a2b07a0f9e0f5df
1 /*
2 * Copyright Johannes Sixt
3 * This file is licensed under the GNU General Public License Version 2.
4 * See the file COPYING in the toplevel directory of the source directory.
5 */
7 #include <qsocketnotifier.h>
8 #include <qpopupmenu.h>
9 #include "ttywnd.h"
10 #include <kglobalsettings.h>
11 #include <klocale.h>
13 #include "config.h"
14 #ifdef HAVE_FCNTL_H
15 #include <fcntl.h>
16 #endif
17 #ifdef HAVE_UNISTD_H
18 #include <unistd.h> /* open, close, etc. */
19 #endif
20 #ifdef HAVE_SYS_IOCTL_H
21 #include <sys/ioctl.h>
22 #endif
23 #ifdef HAVE_SYS_STAT_H
24 #include <sys/stat.h>
25 #endif
26 #ifdef HAVE_PTY_H
27 #include <pty.h> /* openpty on Linux */
28 #endif
29 #ifdef HAVE_LIBUTIL_H
30 #include <libutil.h> /* openpty on FreeBSD */
31 #endif
32 #ifdef HAVE_UTIL_H /* openpty on NetBSD, OpenBSD */
33 #include <util.h>
34 #endif
35 #include <errno.h>
37 #include "mydebug.h"
40 STTY::STTY() :
41 QObject(),
42 m_masterfd(-1),
43 m_slavefd(-1),
44 m_outNotifier(0)
46 if (findTTY())
48 ::fcntl(m_masterfd, F_SETFL, O_NDELAY);
49 m_outNotifier = new QSocketNotifier(m_masterfd, QSocketNotifier::Read);
50 connect(m_outNotifier, SIGNAL(activated(int)), SLOT(outReceived(int)));
51 } else {
52 m_slavetty = QString();
56 STTY::~STTY()
58 if (m_outNotifier) {
59 ::close(m_masterfd);
60 if (m_slavefd >= 0)
61 ::close(m_slavefd);
62 delete m_outNotifier;
66 bool STTY::findTTY()
68 m_masterfd = -1;
70 #ifdef HAVE_FUNC_OPENPTY
71 /* use glibc2's openpty */
72 if (m_masterfd < 0)
74 if (::openpty(&m_masterfd, &m_slavefd, 0, 0, 0) == 0) {
75 const char* tname = ::ttyname(m_slavefd);
76 if (tname != 0) {
77 m_slavetty = tname;
78 } else {
79 ::close(m_slavefd);
80 ::close(m_masterfd);
81 m_masterfd = m_slavefd = -1;
85 #endif
87 // resort to BSD-style terminals
88 if (m_masterfd < 0)
90 const char* s3;
91 const char* s4;
93 char ptynam[] = "/dev/ptyxx";
94 char ttynam[] = "/dev/ttyxx";
95 static const char ptyc3[] = "pqrstuvwxyzabcde";
96 static const char ptyc4[] = "0123456789abcdef";
98 // Find a master pty that we can open
99 for (s3 = ptyc3; *s3 != 0 && m_masterfd < 0; s3++)
101 for (s4 = ptyc4; *s4 != 0; s4++)
103 ptynam[8] = ttynam[8] = *s3;
104 ptynam[9] = ttynam[9] = *s4;
105 if ((m_masterfd = ::open(ptynam,O_RDWR)) >= 0)
107 if (::geteuid() == 0 || ::access(ttynam,R_OK|W_OK) == 0)
109 m_slavetty = ttynam;
110 break;
112 ::close(m_masterfd);
113 m_masterfd = -1;
119 return m_masterfd >= 0;
122 void STTY::outReceived(int f)
124 for (;;) {
125 char buf[1024];
126 int n = ::read(f, buf, sizeof(buf));
127 if (n < 0) {
128 if (errno != EAGAIN) { /* this is not an error */
129 // ugh! error! somebody disconnect this signal please!
131 break;
133 emit output(buf, n);
134 if (n == 0)
135 break;
141 TTYWindow::TTYWindow(QWidget* parent, const char* name) :
142 QTextEdit(parent, name),
143 m_tty(0),
144 m_hPos(0)
146 setFont(KGlobalSettings::fixedFont());
147 setReadOnly(true);
148 setAutoFormatting(AutoNone);
149 setTextFormat(PlainText);
150 setWordWrap(NoWrap);
153 TTYWindow::~TTYWindow()
155 if (m_tty)
156 deactivate();
160 QString TTYWindow::activate()
162 // allocate a pseudo terminal
163 m_tty = new STTY;
165 QString ttyName = m_tty->slaveTTY();
166 if (ttyName.isEmpty()) {
167 // failed to allocate terminal
168 delete m_tty;
169 m_tty = 0;
170 return QString();
171 } else {
172 connect(m_tty, SIGNAL(output(char*,int)), SLOT(slotAppend(char*,int)));
173 return ttyName;
177 void TTYWindow::deactivate()
179 delete m_tty;
180 m_tty = 0;
184 * Note that it is necessary to track the horizontal position explicitly
185 * since if the user modifies the selected text in the window, the cursor
186 * position changes, too.
188 void TTYWindow::slotAppend(char* buffer, int count)
190 // parse off lines
191 char* start = buffer;
192 while (count > 0) {
193 int len = 0;
194 while (count > 0 && start[len] != '\n' && start[len] != '\r') {
195 --count;
196 ++len;
198 if (len > 0) {
199 QString str = QString::fromLatin1(start, len);
200 // replace text in the last line
201 int para = paragraphs()-1;
202 // this selection is non-empty only after a '\r' that was not
203 // followed by a '\n'
204 setSelection(para, m_hPos, para, m_hPos+len, 1);
205 removeSelectedText(1);
206 insertAt(str, para, m_hPos);
207 m_hPos += len;
208 start += len;
209 len = 0;
211 if (count > 0 && *start == '\r') {
212 ++start;
213 --count;
214 m_hPos = 0;
216 if (count > 0 && *start == '\n') {
217 ++start;
218 --count;
219 append(QString());
220 m_hPos = 0;
225 QPopupMenu* TTYWindow::createPopupMenu(const QPoint& pos)
227 QPopupMenu* menu = QTextEdit::createPopupMenu(pos);
228 menu->insertSeparator();
229 menu->insertItem(i18n("&Clear"), this, SLOT(slotClear()));
230 return menu;
233 void TTYWindow::slotClear()
235 clear();
236 m_hPos = 0;
239 #include "ttywnd.moc"