-
Notifications
You must be signed in to change notification settings - Fork 8
/
ptyiface.cpp
146 lines (119 loc) · 3.49 KB
/
ptyiface.cpp
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
/*
Copyright 2011-2012 Heikki Holstila <[email protected]>
This file is part of FingerTerm.
FingerTerm is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
FingerTerm 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with FingerTerm. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QCoreApplication>
extern "C" {
#include <pty.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
}
#include "terminal.h"
#include "ptyiface.h"
static bool childProcessQuit = false;
static int childProcessPid = 0;
void sighandler(int sig)
{
if (sig==SIGCHLD) {
int pid = wait(NULL);
if (pid > 0 && childProcessPid > 0 && pid==childProcessPid) {
childProcessQuit = true;
childProcessPid = 0;
qApp->quit();
}
}
}
PtyIFace::PtyIFace(int pid, int masterFd, Terminal *term, QString charset, QObject *parent) :
QObject(parent),
iTerm(term),
iPid(pid),
iMasterFd(masterFd),
iFailed(false),
iReadNotifier(0),
iTextCodec(0)
{
childProcessPid = iPid;
if (!iTerm || childProcessQuit) {
iFailed = true;
qFatal("PtyIFace: null Terminal pointer");
}
iTerm->setPtyIFace(this);
resize(iTerm->rows(), iTerm->columns());
connect(iTerm, SIGNAL(termSizeChanged(int,int)), this, SLOT(resize(int,int)));
iReadNotifier = new QSocketNotifier(iMasterFd, QSocketNotifier::Read, this);
connect(iReadNotifier, SIGNAL(activated(int)), this, SLOT(readActivated()));
signal(SIGCHLD,&sighandler);
fcntl(iMasterFd, F_SETFL, O_NONBLOCK); // reads from the descriptor should be non-blocking
if (!charset.isEmpty())
iTextCodec = QTextCodec::codecForName(charset.toLatin1());
if (!iTextCodec)
iTextCodec = QTextCodec::codecForName("UTF-8");
if (!iTextCodec)
qFatal("No valid text codec");
}
PtyIFace::~PtyIFace()
{
if (!childProcessQuit) {
// make the process quit
kill(iPid, SIGHUP);
kill(iPid, SIGTERM);
int status=0;
waitpid(-1,&status,0);
}
}
void PtyIFace::readActivated()
{
QByteArray data;
readTerm(data);
if (iTerm)
iTerm->insertInBuffer(iTextCodec->toUnicode(data));
}
void PtyIFace::resize(int rows, int columns)
{
if (childProcessQuit)
return;
winsize winp;
winp.ws_col = columns;
winp.ws_row = rows;
ioctl(iMasterFd, TIOCSWINSZ, &winp);
}
void PtyIFace::writeTerm(const QString &chars)
{
writeTerm(iTextCodec->fromUnicode(chars));
}
void PtyIFace::writeTerm(const QByteArray &chars)
{
if (childProcessQuit)
return;
int ret = write(iMasterFd, chars, chars.size());
if (ret != chars.size())
qDebug() << "write error!";
}
void PtyIFace::readTerm(QByteArray &chars)
{
if (childProcessQuit)
return;
int ret = 0;
char ch[64];
while(ret != -1) {
ret = read(iMasterFd, &ch, 64);
if (ret > 0)
chars.append((char*)&ch, ret);
}
}