forked from Mudlet/Mudlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTForkedProcess.cpp
206 lines (173 loc) · 7.46 KB
/
TForkedProcess.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/***************************************************************************
* Copyright (C) 2009 by Benjamin Lerman - mudlet@ambre.net *
* Copyright (C) 2014 by Ahmed Charles - acharles@outlook.com *
* Copyright (C) 2016 by Christer Oscarsson *
* - christer.oscarsson@gmail.com *
* Copyright (C) 2020, 2022 by Stephen Lyons - slysven@virginmedia.com *
* *
* This program 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. *
* *
* This program 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 this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "TForkedProcess.h"
TForkedProcess::~TForkedProcess()
{
if (callBackFunctionRef != -1) {
luaL_unref(mpInterpreter->pGlobalLua, LUA_REGISTRYINDEX, callBackFunctionRef);
}
}
TForkedProcess::TForkedProcess(TLuaInterpreter* pInterpreter, lua_State* L)
: QProcess()
, mpInterpreter(pInterpreter)
{
int n = lua_gettop(L);
if (n < 2) {
lua_pushstring(L, "Need read function and process name as parameters.");
lua_error(L);
}
if (!lua_isfunction(L, 1)) {
lua_pushstring(L, "Need read function as first parameter.");
lua_error(L);
}
lua_pushvalue(L, 1);
callBackFunctionRef = luaL_ref(L, LUA_REGISTRYINDEX);
QString prog{luaL_checkstring(L, 2)};
QStringList args;
for (int i = 3; i <= n; i++) {
args << luaL_checkstring(L, i);
}
// QProcess::finished is overloaded so we have to say which form we are
// connecting here
connect(this, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), mpInterpreter, &TLuaInterpreter::slot_deleteSender);
connect(this, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this, &TForkedProcess::slot_finished);
connect(this, &QProcess::readyReadStandardOutput, this, &TForkedProcess::slot_receivedData);
setProcessChannelMode(QProcess::MergedChannels);
start(prog, args, QIODevice::ReadWrite);
waitForStarted();
running = true;
}
void TForkedProcess::slot_finished(int exitCode, QProcess::ExitStatus exitStatus)
{
Q_UNUSED(exitCode);
Q_UNUSED(exitStatus);
running = false;
}
void TForkedProcess::slot_receivedData()
{
while (canReadLine()) {
QByteArray line = readLine();
// Call lua function by stored Reference
lua_rawgeti(mpInterpreter->pGlobalLua, LUA_REGISTRYINDEX, callBackFunctionRef);
lua_pushstring(mpInterpreter->pGlobalLua, line.data());
lua_pcall(mpInterpreter->pGlobalLua, 1, 0, 0);
}
}
int TForkedProcess::sendMessage(lua_State* L)
{
QPointer<TForkedProcess>* forkedProcess = *((QPointer<TForkedProcess>**)lua_topointer(L, lua_upvalueindex(1)));
if (!forkedProcess || forkedProcess->isNull() || !(*forkedProcess)->running) {
lua_pushstring(L, "Unable to send data to process. Process has ended.");
return lua_error(L);
}
size_t stringLength = 0;
const char* toWrite = lua_tolstring(L, 1, &stringLength);
if (!toWrite) {
lua_pushstring(L, "Unable to get data to send.");
return lua_error(L);
}
size_t writedBytes = 0;
while (stringLength > writedBytes) {
int res = (*forkedProcess)->write(toWrite + writedBytes, stringLength - writedBytes);
if (res == -1) {
lua_pushstring(L, "Unable to send data to process.");
return lua_error(L);
}
writedBytes += res;
}
return 0;
}
int TForkedProcess::isProcessRunning(lua_State* L)
{
QPointer<TForkedProcess>* forkedProcess = *((QPointer<TForkedProcess>**)lua_topointer(L, lua_upvalueindex(1)));
bool running = (forkedProcess != nullptr && !forkedProcess->isNull() && (*forkedProcess)->running);
lua_pushboolean(L, running);
return 1;
}
int TForkedProcess::closeInputOfProcess(lua_State* L)
{
QPointer<TForkedProcess>* forkedProcess = *((QPointer<TForkedProcess>**)lua_topointer(L, lua_upvalueindex(1)));
if (!forkedProcess || forkedProcess->isNull() || !(*forkedProcess)->running) {
// Process is already finished. Nothing to do.
return 0;
}
(*forkedProcess)->closeWriteChannel();
return 0;
}
static int qPointerGC(lua_State* L)
{
QPointer<TForkedProcess>* forkedProcessPointer = *((QPointer<TForkedProcess>**)lua_topointer(L, 1));
delete forkedProcessPointer;
lua_pushboolean(L, true);
return 1;
}
int TForkedProcess::startProcess(TLuaInterpreter* pInterpreter, lua_State* L)
{
auto process = new TForkedProcess(pInterpreter, L);
// The userdata for the closures.
auto ** luaMemory = (QPointer<TForkedProcess>**)lua_newuserdata(L, sizeof(QPointer<TForkedProcess>*));
int userDataIndex = lua_gettop(L);
if (lua_getmetatable(L, userDataIndex) != 0) {
lua_pushstring(L, "Error: new user data should not have any metatable.");
return lua_error(L);
}
if (luaL_newmetatable(L, "qPointerGCMetatable") == 1) {
// First time one call this method. One must register the garbage collection method.
int tableIndex = lua_gettop(L);
lua_pushstring(L, "__gc");
lua_pushcfunction(L, qPointerGC);
lua_settable(L, tableIndex);
}
lua_setmetatable(L, userDataIndex);
*luaMemory = new QPointer<TForkedProcess>(process);
// One must return a table with the following function:
// send( a ) -> () : to send a to the process
// close() -> () : to close the input of the process
// isRunning() -> bool : to know if the process is still running.
lua_newtable(L);
int tableIndex = lua_gettop(L);
// The name of the send function
lua_pushstring(L, "send");
// The metadatcontaining the process
lua_pushvalue(L, userDataIndex);
// The send function
lua_pushcclosure(L, TForkedProcess::sendMessage, 1);
// Set the table for the send function.
lua_settable(L, tableIndex);
// The name of the close function
lua_pushstring(L, "close");
// The metadatcontaining the process
lua_pushvalue(L, userDataIndex);
// The close function
lua_pushcclosure(L, TForkedProcess::closeInputOfProcess, 1);
// Set the table for the close function.
lua_settable(L, tableIndex);
lua_pushstring(L, "isRunning");
// The metadatcontaining the function
lua_pushvalue(L, userDataIndex);
// The isRunning function
lua_pushcclosure(L, TForkedProcess::isProcessRunning, 1);
// Set the table for the isRunning function.
lua_settable(L, tableIndex);
return 1;
}