summaryrefslogtreecommitdiffstats
path: root/libk3b/core/k3bprocess.cpp
blob: c46fd0a64d66192c88f79a2feb543759618729d3 (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* 
 *
 * $Id: k3bprocess.cpp 619556 2007-01-03 17:38:12Z trueg $
 * Copyright (C) 2003 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * 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.
 * See the file "COPYING" for the exact licensing terms.
 */



#include "k3bprocess.h"
#include "k3bexternalbinmanager.h"

#include <tqstringlist.h>
#include <tqsocketnotifier.h>
#include <tqptrqueue.h>
#include <tqapplication.h>

#include <kdebug.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>


class K3bProcess::Data
{
public:
  TQString unfinishedStdoutLine;
  TQString unfinishedStderrLine;

  int dupStdoutFd;
  int dupStdinFd;

  bool rawStdin;
  bool rawStdout;

  int in[2];
  int out[2];

  bool suppressEmptyLines;
};


K3bProcess::K3bProcess()
  : KProcess(),
    m_bSplitStdout(false)
{
  d = new Data();
  d->dupStdinFd = d->dupStdoutFd = -1;
  d->rawStdout = d->rawStdin = false;
  d->in[0] = d->in[1] = -1;
  d->out[0] = d->out[1] = -1;
  d->suppressEmptyLines = true;
}

K3bProcess::~K3bProcess()
{
  delete d;
}


K3bProcess& K3bProcess::operator<<( const K3bExternalBin* bin )
{
  return this->operator<<( bin->path );
}

K3bProcess& K3bProcess::operator<<( const TQString& arg )
{
  static_cast<KProcess*>(this)->operator<<( arg );
  return *this;
}

K3bProcess& K3bProcess::operator<<( const char* arg )
{
  static_cast<KProcess*>(this)->operator<<( arg );
  return *this;
}

K3bProcess& K3bProcess::operator<<( const TQCString& arg )
{
  static_cast<KProcess*>(this)->operator<<( arg );
  return *this;
}

K3bProcess& K3bProcess::operator<<( const TQStringList& args )
{
  static_cast<KProcess*>(this)->operator<<( args );
  return *this;
}


bool K3bProcess::start( RunMode run, Communication com )
{
  if( com & Stderr ) {
    connect( this, TQT_SIGNAL(receivedStderr(KProcess*, char*, int)),
	     this, TQT_SLOT(slotSplitStderr(KProcess*, char*, int)) );
  }
  if( com & Stdout ) {
    connect( this, TQT_SIGNAL(receivedStdout(KProcess*, char*, int)),
	     this, TQT_SLOT(slotSplitStdout(KProcess*, char*, int)) );
  }

  return KProcess::start( run, com );
}


void K3bProcess::slotSplitStdout( KProcess*, char* data, int len )
{
  if( m_bSplitStdout ) {
    TQStringList lines = splitOutput( data, len, d->unfinishedStdoutLine, d->suppressEmptyLines );

    for( TQStringList::iterator it = lines.begin(); it != lines.end(); ++it ) {
      TQString& str = *it;
      
      // just to be sure since something in splitOutput does not do this right
      if( d->suppressEmptyLines && str.isEmpty() )
	continue;

      emit stdoutLine( str );
    }
  }
}


void K3bProcess::slotSplitStderr( KProcess*, char* data, int len )
{
  TQStringList lines = splitOutput( data, len, d->unfinishedStderrLine, d->suppressEmptyLines );

  for( TQStringList::iterator it = lines.begin(); it != lines.end(); ++it ) {
    TQString& str = *it;

    // just to be sure since something in splitOutput does not do this right
    if( d->suppressEmptyLines && str.isEmpty() )
      continue;

    emit stderrLine( str );
  }
}


TQStringList K3bProcess::splitOutput( char* data, int len, 
				     TQString& unfinishedLine, bool suppressEmptyLines )
{
  //
  // The stderr splitting is mainly used for parsing of messages
  // That's why we simplify the data before proceeding
  //

  TQString buffer;
  for( int i = 0; i < len; i++ ) {
    if( data[i] == '\b' ) {
      while( data[i] == '\b' )  // we replace multiple backspaces with a single line feed
	i++;
      buffer += '\n';
    }
    if( data[i] == '\r' )
      buffer += '\n';
    else if( data[i] == '\t' )  // replace tabs with a single space
      buffer += " ";
    else
      buffer += data[i];
  }

  TQStringList lines = TQStringList::split( '\n', buffer, !suppressEmptyLines );

  // in case we suppress empty lines we need to handle the following separately
  // to make sure we join unfinished lines correctly
  if( suppressEmptyLines && buffer[0] == '\n' )
    lines.prepend( TQString() );

  if( !unfinishedLine.isEmpty() ) {
    lines.first().prepend( unfinishedLine );
    unfinishedLine.truncate(0);

    kdDebug() << "(K3bProcess)           joined line: '" << (lines.first()) << "'" << endl;
  }

  TQStringList::iterator it;

  // check if line ends with a newline
  // if not save the last line because it is not finished
  TQChar c = buffer.right(1).at(0);
  bool hasUnfinishedLine = ( c != '\n' && c != '\r' && c != TQChar(46) );  // What is unicode 46?? It is printed as a point
  if( hasUnfinishedLine ) {
    kdDebug() << "(K3bProcess) found unfinished line: '" << lines.last() << "'" << endl;
    kdDebug() << "(K3bProcess)             last char: '" << buffer.right(1) << "'" << endl;
    unfinishedLine = lines.last();
    it = lines.end();
    --it;
    lines.remove(it);
  }

  return lines;
}


int K3bProcess::setupCommunication( Communication comm )
{
  if( KProcess::setupCommunication( comm ) ) {

    //
    // Setup our own socketpair
    //

    if( d->rawStdin ) {
      if( socketpair(AF_UNIX, SOCK_STREAM, 0, d->in) == 0 ) {
	fcntl(d->in[0], F_SETFD, FD_CLOEXEC);
	fcntl(d->in[1], F_SETFD, FD_CLOEXEC);
      }
      else
	return 0;
    }

    if( d->rawStdout ) {
      if( socketpair(AF_UNIX, SOCK_STREAM, 0, d->out) == 0 ) {
	fcntl(d->out[0], F_SETFD, FD_CLOEXEC);
	fcntl(d->out[1], F_SETFD, FD_CLOEXEC);
      }
      else {
	if( d->rawStdin || d->dupStdinFd ) {
	  close(d->in[0]);
	  close(d->in[1]);
	}
	return 0;
      }
    }

    return 1;
  }
  else
    return 0;
}


void K3bProcess::commClose()
{
  if( d->rawStdin ) {
    close(d->in[1]);
    d->in[1] = -1;
  }
  if( d->rawStdout ) {
    close(d->out[0]);
    d->out[0] = -1;
  }

  KProcess::commClose();
}


int K3bProcess::commSetupDoneP()
{
  int ok = KProcess::commSetupDoneP();

  if( d->rawStdin )
    close(d->in[0]);
  if( d->rawStdout )
    close(d->out[1]);

  d->in[0] = d->out[1] = -1;

  return ok;
}


int K3bProcess::commSetupDoneC()
{
  int ok = KProcess::commSetupDoneC();

  if( d->dupStdoutFd != -1 ) {
    //
    // make STDOUT_FILENO a duplicate of d->dupStdoutFd such that writes to STDOUT_FILENO are "redirected"
    // to d->dupStdoutFd
    //
    if( ::dup2( d->dupStdoutFd, STDOUT_FILENO ) < 0 ) {
      kdDebug() << "(K3bProcess) Error while dup( " << d->dupStdoutFd << ", " << STDOUT_FILENO << endl;
      ok = 0;
    }
  }
  else if( d->rawStdout ) {
    if( ::dup2( d->out[1], STDOUT_FILENO ) < 0 ) {
      kdDebug() << "(K3bProcess) Error while dup( " << d->out[1] << ", " << STDOUT_FILENO << endl;
      ok = 0;
    }
  }

  if( d->dupStdinFd != -1 ) {
    if( ::dup2( d->dupStdinFd, STDIN_FILENO ) < 0 ) {
      kdDebug() << "(K3bProcess) Error while dup( " << d->dupStdinFd << ", " << STDIN_FILENO << endl;
      ok = 0;
    }
  }
  else if( d->rawStdin ) {
    if( ::dup2( d->in[0], STDIN_FILENO ) < 0 ) {
      kdDebug() << "(K3bProcess) Error while dup( " << d->in[0] << ", " << STDIN_FILENO << endl;
      ok = 0;
    }
  }

  return ok;
}



int K3bProcess::stdinFd() const
{
  if( d->rawStdin )
    return d->in[1];
  else if( d->dupStdinFd != -1 )
    return d->dupStdinFd;
  else
    return -1;
}

int K3bProcess::stdoutFd() const
{
  if( d->rawStdout )
    return d->out[0];
  else if( d->dupStdoutFd != -1 )
    return d->dupStdoutFd;
  else
    return -1;
}


void K3bProcess::dupStdout( int fd )
{
  writeToFd( fd );
}

void K3bProcess::dupStdin( int fd )
{
  readFromFd( fd );
}


void K3bProcess::writeToFd( int fd )
{
  d->dupStdoutFd = fd;
  if( fd != -1 )
    d->rawStdout = false;
}

void K3bProcess::readFromFd( int fd )
{
  d->dupStdinFd = fd;
  if( fd != -1 )
    d->rawStdin = false;
}


void K3bProcess::setRawStdin(bool b)
{
  if( b ) {
    d->rawStdin = true;
    d->dupStdinFd = -1;
  }
  else
    d->rawStdin = false;
}


void K3bProcess::setRawStdout(bool b)
{
  if( b ) {
    d->rawStdout = true;
    d->dupStdoutFd = -1;
  }
  else
    d->rawStdout = false;
}


void K3bProcess::setSuppressEmptyLines( bool b )
{
  d->suppressEmptyLines = b;
}


bool K3bProcess::closeStdin()
{
  if( d->rawStdin ) {
    close(d->in[1]);
    d->in[1] = -1;
    return true;
  }
  else
    return KProcess::closeStdin();
}


bool K3bProcess::closeStdout()
{
  if( d->rawStdout ) {
    close(d->out[0]);
    d->out[0] = -1;
    return true;
  }
  else
    return KProcess::closeStdout();
}


K3bProcessOutputCollector::K3bProcessOutputCollector( KProcess* p )
  : m_process(0)
{
  setProcess( p );
}

void K3bProcessOutputCollector::setProcess( KProcess* p )
{
  if( m_process )
    m_process->disconnect( this );

  m_process = p;
  if( p ) {
    connect( p, TQT_SIGNAL(receivedStdout(KProcess*, char*, int)), 
	     this, TQT_SLOT(slotGatherStdout(KProcess*, char*, int)) );
    connect( p, TQT_SIGNAL(receivedStderr(KProcess*, char*, int)), 
	     this, TQT_SLOT(slotGatherStderr(KProcess*, char*, int)) );
  }

  m_gatheredOutput.truncate( 0 );
  m_stderrOutput.truncate( 0 );
  m_stdoutOutput.truncate( 0 );
}

void K3bProcessOutputCollector::slotGatherStderr( KProcess*, char* data, int len )
{
  m_gatheredOutput.append( TQString::fromLocal8Bit( data, len ) );
  m_stderrOutput.append( TQString::fromLocal8Bit( data, len ) );
}

void K3bProcessOutputCollector::slotGatherStdout( KProcess*, char* data, int len )
{
  m_gatheredOutput.append( TQString::fromLocal8Bit( data, len ) );
  m_stdoutOutput.append( TQString::fromLocal8Bit( data, len ) );
}


#include "k3bprocess.moc"