summaryrefslogtreecommitdiffstats
path: root/src/kvirc/kvs/kvi_kvs_processmanager.cpp
blob: c6925e6be9b99e0b0e5bc83e25c57a6d850825bf (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//=============================================================================
//
//   File : kvi_kvs_processmanager.cpp
//   Created on Wed 07 Apr 2004 03:03:52 by Szymon Stefanek
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 2004-2007 Szymon Stefanek <pragma at kvirc dot net>
//
//   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 opinion) 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. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

#define __KVIRC__

#include "kvi_kvs_processmanager.h"
#include "kvi_window.h"
#include "kvi_out.h"
#include "kvi_locale.h"
#include "kvi_parameterlist.h"
#include "kvi_app.h"
#include "kvi_console.h"
#include "kvi_kvs_script.h"
//#include <TQSysInfo>

KviKvsProcessAsyncOperation::KviKvsProcessAsyncOperation(KviKvsProcessDescriptorData * d)
: KviKvsAsyncOperation(d->pWnd)
{
	m_pData = d;
	m_pProcess = 0;
	m_pExtendedRunTimeData = new KviKvsExtendedRunTimeData(new KviKvsHash(),TRUE);
	m_pPingTimer = 0;
	m_pRunTimeTimer = 0;
	m_bDeletePending = false;
}

KviKvsProcessAsyncOperation::~KviKvsProcessAsyncOperation()
{
	if(m_pPingTimer)delete m_pPingTimer;
	if(m_pRunTimeTimer)delete m_pRunTimeTimer;
	if(m_pProcess)
	{
		TQObject::disconnect(m_pProcess,0,this,0);
		m_pProcess->kill();
		delete m_pProcess;
	}
	delete m_pExtendedRunTimeData;
	if(m_pData->pCallback)delete m_pData->pCallback;
	if(m_pData->pMagic)delete m_pData->pMagic;
	delete m_pData;
}

bool KviKvsProcessAsyncOperation::start()
{
	TQStringList args;
	
	if(m_pData->iFlags & KVI_KVS_PROCESSDESCRIPTOR_NOSHELL)
	{
		args = TQStringList::split(" ",m_pData->szCommandline);
	} else {
		TQString szShell = m_pData->szShell;
		if(szShell.isEmpty())
		{
#ifdef COMPILE_ON_WINDOWS
			// [01:26:00] <PragmaOff> btw, what is qt_winunicode ?
			// [01:26:12] <kode54> TQt export specific to win32
			// [01:26:27] <kode54> bool which indicates whether system is Unicode (NT) or not
			// [01:26:58] <kode54> not sure if that's documented, but it is a public export
			//
			// [02:50:21] <kode54> if ( TQApplication::winVersion() & TQt::WV_NT_based )
			// [02:50:41] <kode54> I see another implementation using that, maybe it is the official way of detecting that :[
#ifdef COMPILE_USE_QT4
			szShell = !(TQSysInfo::WindowsVersion & TQSysInfo::WV_DOS_based) ? "cmd.exe /c" : "command.com /c";
#else
			szShell = "command.com /c";
#endif

			// Thnx kode54 :)
#else
			szShell = "sh -c";
#endif
		}
		args = TQStringList::split(" ",szShell);
		args.append(m_pData->szCommandline);
	}
	
	m_pProcess = new KviProcess(args);
	int c = KviProcess::Stdin;
	
	if(m_pData->iFlags & KVI_KVS_PROCESSDESCRIPTOR_TRIGGERSTDOUT)
	{
		connect(m_pProcess,TQT_SIGNAL(readyReadStdout()),this,TQT_SLOT(readStdout()));
		c |= KviProcess::Stdout;
	}
	
	if(m_pData->iFlags & KVI_KVS_PROCESSDESCRIPTOR_TRIGGERSTDERR)
	{
		connect(m_pProcess,TQT_SIGNAL(readyReadStderr()),this,TQT_SLOT(readStderr()));
		c |= KviProcess::Stderr;
	}

	m_pProcess->setCommunication(c);
	
	connect(m_pProcess,TQT_SIGNAL(processExited()),this,TQT_SLOT(processExited()));
	
	if(!m_pProcess->start())
	{
		return false;
	}
	
	if(m_pData->iFlags & KVI_KVS_PROCESSDESCRIPTOR_TRIGGERSTARTED)
	{
		TQString szPid; 
		szPid.setNum((int)(m_pProcess->processIdentifier()));
		if(trigger(EventStarted,szPid))
		{
			triggerSelfDelete();
			return true;
		}
	}

	if(m_pData->iMaxRunTime > 0)
	{
		m_pRunTimeTimer = new TQTimer(this);
		connect(m_pRunTimeTimer,TQT_SIGNAL(timeout()),this,TQT_SLOT(maxRunTimeExpired()));
		m_pRunTimeTimer->start(m_pData->iMaxRunTime);
	}
	
	if(m_pData->iPingTimeout > 0)
	{
		m_pPingTimer = new TQTimer(this);
		connect(m_pPingTimer,TQT_SIGNAL(timeout()),this,TQT_SLOT(ping()));
		m_pPingTimer->start(m_pData->iPingTimeout);
	}
	
	return true;
}

void KviKvsProcessAsyncOperation::ping()
{
	if(trigger(EventPing,TQString()))
	{
		triggerSelfDelete();
	}
}

void KviKvsProcessAsyncOperation::triggerSelfDelete()
{
	if(m_bDeletePending)return;
	m_bDeletePending = true;
	TQTimer::singleShot(m_pData->iMaxRunTime,this,TQT_SLOT(selfDelete()));
}

void KviKvsProcessAsyncOperation::selfDelete()
{
	delete this;
}

void KviKvsProcessAsyncOperation::maxRunTimeExpired()
{
	trigger(EventTerminated,"0");
	triggerSelfDelete();
}

bool KviKvsProcessAsyncOperation::trigger(CallbackEvent e,const TQString &szData)
{
	if(m_bDeletePending)return false;
	
	if(!g_pApp->windowExists(m_pData->pWnd))
	{
		if(m_pData->iFlags & KVI_KVS_PROCESSDESCRIPTOR_KILLIFNOWINDOW)
		{
			return true;
		}
		m_pData->pWnd = g_pApp->activeConsole();
	}

	if(m_pData->pCallback)
	{
		KviKvsVariantList params;
		params.setAutoDelete(true);
		
		switch(e)
		{
			case EventStdout:
				params.append(new KviKvsVariant(TQString("stdout")));
			break;
			case EventStderr:
				params.append(new KviKvsVariant(TQString("stderr")));
			break;
			case EventTerminated:
				params.append(new KviKvsVariant(TQString("terminated")));
			break;
			case EventStarted:
				params.append(new KviKvsVariant(TQString("started")));
			break;
			case EventPing:
				params.append(new KviKvsVariant(TQString("ping")));
			break;
			default:
				tqDebug("Ops... unknown trigger() CallbackEvent parameter in KviProcessDescriptor::trigger()");
				return false;
			break;
		}

		params.append(new KviKvsVariant(szData));
		if (m_pData->pMagic) params.append(m_pData->pMagic);

		KviKvsVariant retVal;
		int iRet = m_pData->pCallback->run(m_pData->pWnd,&params,&retVal,KviKvsScript::PreserveParams,m_pExtendedRunTimeData);
		if(!iRet)
		{
			m_pData->pWnd->output(KVI_OUT_PARSERERROR,
				__tr2qs("Error triggered from process callback handler: killing process"));
			return true;
		}

		if(!retVal.isNothing())
		{
			TQString sz;
			retVal.asString(sz);
			m_pProcess->writeToStdin(sz);
		}
	
		if(iRet & KviKvsScript::HaltEncountered)
		{
			// halt encountered: kill the process
			return true;
		}
	}

	return false;
}

void KviKvsProcessAsyncOperation::readStdout()
{
	if(m_bDeletePending)return;
	if(m_pData->iFlags & KVI_KVS_PROCESSDESCRIPTOR_OUTPUTBYBLOCKS)
	{
		TQByteArray a = m_pProcess->readStdout();
		if(a.size() > 0)
			m_szStdoutBuffer += TQString(a);
	} else {
		TQString l = m_pProcess->readLineStdout();
		bool bBreak = false;
		while((!l.isNull()) && (!bBreak))
		{
			if(m_pData->iFlags & KVI_KVS_PROCESSDESCRIPTOR_TRIGGERSTDOUT)
			{
				if(trigger(EventStdout,l))
				{
					bBreak = true;
					triggerSelfDelete();
				}
			}

			l = m_pProcess->readLineStdout();
		}
	}
}

void KviKvsProcessAsyncOperation::readStderr()
{
	if(m_bDeletePending)return;
	if(m_pData->iFlags & KVI_KVS_PROCESSDESCRIPTOR_OUTPUTBYBLOCKS)
	{
		TQByteArray a = m_pProcess->readStderr();
		if(a.size() > 0)
			m_szStderrBuffer += TQString(a);
	} else {
		TQString l = m_pProcess->readLineStderr();
		bool bBreak = false;
		while((!l.isNull()) && (!bBreak))
		{
			if(m_pData->iFlags & KVI_KVS_PROCESSDESCRIPTOR_TRIGGERSTDERR)
			{
				if(trigger(EventStderr,l))
				{
					bBreak = true;
					triggerSelfDelete();
				}
			}

			l = m_pProcess->readLineStderr();
		}
	}
}


void KviKvsProcessAsyncOperation::processExited()
{
	if(m_bDeletePending)return;

	readStdout(); // just to make sure
	readStderr(); // just to make sure
	
	if(m_pData->iFlags & KVI_KVS_PROCESSDESCRIPTOR_OUTPUTBYBLOCKS)
	{
		// trigger Stdout and Stderr once
		if(m_pData->iFlags & KVI_KVS_PROCESSDESCRIPTOR_TRIGGERSTDOUT)
		{
			if(trigger(EventStdout,m_szStdoutBuffer))
			{
				triggerSelfDelete();
				return;
			}
		}
		
		if(m_pData->iFlags & KVI_KVS_PROCESSDESCRIPTOR_TRIGGERSTDERR)
		{
			if(trigger(EventStdout,m_szStderrBuffer))
			{
				triggerSelfDelete();
				return;
			}
		}
	}

	if(m_pData->iFlags & KVI_KVS_PROCESSDESCRIPTOR_TRIGGERTERMINATED)
	{
		TQString szRetVal;
		szRetVal.setNum(m_pProcess->exitStatus());
		trigger(EventTerminated,szRetVal);
	}
	
	triggerSelfDelete();
}


/*

KviKvsProcessManager::KviKvsProcessManager()
: TQObject()
{
}

KviKvsProcessManager::~KviKvsProcessManager()
{
}

void KviKvsProcessManager::init()
{
	if(m_pInstance)return;
	m_pInstance = new KviProcessManager();
}

void KviKvsProcessManager::done()
{
	if(!m_pInstance)return;
	delete m_pInstance;
	m_pInstance = 0;
}

bool KviKvsProcessManager::execute(KviKvsProcessAsyncOperationData * d)
{
	KviKvsProcessAsyncOperation * pd = new KviKvsProcessAsyncOperation(d,this);
	if(!pd->start())
	{
		//delete d; <-- delete by KviKvsProcessAsyncOperation
		delete pd;
		return false;
	}
	return true;
}
*/