summaryrefslogtreecommitdiffstats
path: root/src/frontend.cpp
blob: 52c97c21f5da0f7a80645ac481527d1a966a7f21 (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
/***************************************************************************
 *
 * Copyright (C) 2005 Elad Lahav (elad_lahav@users.sourceforge.net)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ***************************************************************************/

#include <tqfileinfo.h>
#include <tqdir.h>
#include <tdelocale.h>
#include "frontend.h"

/**
 * Class constructor.
 * @param	nRecordSize	The number of fields in each record
 * @param	bAutoDelete	(Optional) true to delete the object when the process
 *						terminates, false (default) otherwise
 */
Frontend::Frontend(uint nRecordSize, bool bAutoDelete) : TDEProcess(),
	m_nRecords(0),
	m_pHeadToken(NULL),
	m_pTailToken(NULL),
	m_pCurToken(NULL),
	m_bAutoDelete(bAutoDelete),
	m_bInToken(false),
	m_nRecordSize(nRecordSize)
{
	// Parse data on the standard output
	connect(this, TQ_SIGNAL(receivedStdout(TDEProcess*, char*, int)), this,
		TQ_SLOT(slotReadStdout(TDEProcess*, char*, int)));

	// Parse data on the standard error
	connect(this, TQ_SIGNAL(receivedStderr(TDEProcess*, char*, int)), this,
		TQ_SLOT(slotReadStderr(TDEProcess*, char*, int)));
		
	// Delete the process object when the process exits
	connect(this, TQ_SIGNAL(processExited(TDEProcess*)), this,
		TQ_SLOT(slotProcessExit(TDEProcess*)));
}

/**
 * Class destructor.
 */
Frontend::~Frontend()
{
	// Delete all pending tokens
	while (m_pHeadToken)
		removeToken();
}

/**
 * Executes the back-end process.
 * @param	sName		The name of the process (for error messages)
 * @param	slArgs		A list containing the command-line arguments
 * @param	sWorkDir	(Optional) working directory
 * @param	bBlock		(Optional) true to block, false otherwise
 * @return	true if the process was executed successfully, false otherwise
 */
bool Frontend::run(const TQString& sName, const TQStringList& slArgs, 
	const TQString& sWorkDir, bool bBlock)
{
	// Cannot start if another controlled process is currently running
	if (isRunning()) {
		m_sError = i18n("Cannot restart while another process is still "
			"running");
		return false;
	}

	// Reset variables
	m_nRecords = 0;
	m_bKilled = false;
	
	// Setup the command-line arguments
	clearArguments();
	*this << slArgs;
	
	// Set the working directory, if requested
	if (!sWorkDir.isEmpty())
		setWorkingDirectory(sWorkDir);

	// Execute the child process
	if (!start(bBlock ? TDEProcess::Block : TDEProcess::NotifyOnExit,
		TDEProcess::All)) {
		m_sError = sName + i18n(": Failed to start process");
		return false;
	}
	
	m_sError = i18n("No error");
	return true;
}

/**
 * Kills the process, and emits the aborted() signal.
 * This function should not be called unless the process needs to be
 * interrupted.
 */
void Frontend::kill()
{
	m_bKilled = true;
	TDEProcess::kill();
	
	emit aborted();
}

/**
 * Appends a token to the end of the token list.
 * @param	pToken	The token to add
 */
void Frontend::addToken(FrontendToken* pToken)
{
	// Check if this is the firt token
	if (m_pHeadToken == NULL) {
		m_pHeadToken = pToken;
		m_pTailToken = pToken;
	}
	else {
		// Not the first token, append and reset the tail token
		m_pTailToken->m_pNext = pToken;
		m_pTailToken = pToken;
	}
}

/**
 * Removes and deletes the token at the head of the token list.
 */
void Frontend::removeToken()
{
	FrontendToken* pToken;
	
	if (m_pHeadToken == NULL)
		return;

	pToken = m_pHeadToken;
	m_pHeadToken = m_pHeadToken->m_pNext;
	delete pToken;

	if (m_pHeadToken == NULL)
		m_pTailToken = NULL;	
}

/**
 * Removes tokens from the head of the list, according to the size of a
 * record.
 */
void Frontend::removeRecord()
{
	uint i;

	for (i = 0; (i < m_nRecordSize) && (m_pHeadToken != NULL); i++)
		removeToken();
}

/**
 * Extracts tokens of text out of a given buffer.
 * @param	ppBuf		Points to the buffer to parse, and is set to the
 *						beginning of the next token, upon return
 * @param	pBufSize	Points to the size of the buffer, and is set to the
 *						remaining size, upon return
 * @param	sResult		Holds the token's text, upon successful return
 * @param	delim		Holds the delimiter by which the token's end was
 * 						determined
 * @return	true if a token was extracted up to the given delimter(s), false
 *			if the buffer ended before a delimiter could be identified
 */
bool Frontend::tokenize(char** ppBuf, int* pBufSize, TQString& sResult,
	ParserDelim& delim)
{
	int nSize;
	char* pBuf;
	bool bDelim, bWhiteSpace, bFoundToken = false;
	
	// Iterate buffer
	for (nSize = *pBufSize, pBuf = *ppBuf; (nSize > 0) && !bFoundToken;
		nSize--, pBuf++) {
		// Test if this is a delimiter character
		switch (*pBuf) {
		case '\n':
			bDelim = ((m_delim & Newline) != 0);
			bWhiteSpace = true;
			delim = Newline;
			break;

		case ' ':
			bDelim = ((m_delim & Space) != 0);
			bWhiteSpace = true;
			delim = Space;
			break;

		case '\t':
			bDelim = ((m_delim & Tab) != 0);
			bWhiteSpace = true;
			delim = Tab;
			break;

		default:
			bDelim = false;
			bWhiteSpace = false;
		}

		if (m_bInToken && bDelim) {
			m_bInToken = false;
			*pBuf = 0;
			bFoundToken = true;
		}
		else if (!m_bInToken && !bWhiteSpace) {
			m_bInToken = true;
			*ppBuf = pBuf;
		}
	}

	// Either a token was found, or the search through the buffer was
	// finished without a delimiter character
	if (bFoundToken) {
		sResult = TQString::fromLocal8Bit(*ppBuf);
		*ppBuf = pBuf;
		*pBufSize = nSize;
	}
	else if (m_bInToken) {
		sResult = TQString::fromLocal8Bit(*ppBuf, *pBufSize);
	}
	else {
		sResult = TQString::null;
	}

	return bFoundToken;
}

/**
 * Handles text sent by the back-end process to the standard error stream.
 * By default, this method emits the error() signal with the given text.
 * @param	sText	The text sent to the standard error stream
 */
void Frontend::parseStderr(const TQString& sText)
{
	emit error(sText);
}

/**
 * Deletes the process object upon the process' exit.
 */
void Frontend::slotProcessExit(TDEProcess*)
{
	// Allow specialised clean-up by inheriting classes
	finalize();
	
	// Signal the process has terminated
	emit finished(m_nRecords);
	
	// Delete the object, if required
	if (m_bAutoDelete)
		delete this;
}

/**
 * Reads data written on the standard output by the controlled process.
 * This is a private slot called attached to the readyReadStdout() signal of
 * the controlled process, which means that it is called whenever data is
 * ready to be read from the process' stream.
 * The method reads whatever data is queued, and sends it to be interpreted
 * by parseStdout().
 */
void Frontend::slotReadStdout(TDEProcess*, char* pBuffer, int nSize)
{
	char* pLocalBuf;
	TQString sToken;
	bool bTokenEnded;
	ParserDelim delim;
	
	// Do nothing if waiting for process to die
	if (m_bKilled)
		return;
	
	pLocalBuf = pBuffer;
	
	// Iterate over the given buffer
	while (nSize > 0) {
		// Create a new token, if the last iteration has completed one
		if (m_pCurToken == NULL)
			m_pCurToken = new FrontendToken();

		// Extract text until the requested delimiter
		bTokenEnded = tokenize(&pLocalBuf, &nSize, sToken, delim);

		// Add the extracted text to the current token
		m_pCurToken->m_sData += sToken;

		// If the buffer has ended before the requested delimiter, we need
		// to wait for more output from the process
		if (!bTokenEnded)
			return;

		// Call the process-specific parser function
		switch (parseStdout(m_pCurToken->m_sData, delim)) {
		case DiscardToken:
			// Token should not be saved
			delete m_pCurToken;
			break;

		case AcceptToken:
			// Store token in linked list
			addToken(m_pCurToken);
			break;

		case RecordReady:
			// Store token, and notify the target object that an entry can
			// be read
			m_nRecords++;
			addToken(m_pCurToken);
			emit dataReady(m_pHeadToken);

			// Delete all tokens in the entry
			removeRecord();
			break;
			
		case Abort:
			kill();
			nSize = 0;
			break;
		}

		m_pCurToken = NULL;
	}
}

/**
 * Reads data written on the standard error by the controlled process.
 * This is a private slot called attached to the readyReadStderr() signal of
 * the controlled process, which means that it is called whenever data is
 * ready to be read from the process' stream.
 * The method reads whatever data is queued, and sends it to be interpreted
 * by parseStderr().
 */
void Frontend::slotReadStderr(TDEProcess*, char* pBuffer, int nSize)
{
	TQString sBuf;
	
	// Do nothing if waiting for process to die
	if (m_bKilled)
		return;

	sBuf = TQString::fromLocal8Bit(pBuffer, nSize);
	parseStderr(sBuf);
}

#include "frontend.moc"