summaryrefslogtreecommitdiffstats
path: root/lskat/lskat/TDEProcessConnect.cpp
blob: b2038b4f2c2041812bd432f8f1c5961ce26c9ac9 (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
/***************************************************************************
                          TDEProcessConnect.cpp  -  description
                             -------------------
    begin                : Tue May 2 2000
    copyright            : (C) 2000 by Martin Heni
    email                : martin@heni-online.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
/***************************************************************************
                          FILENAME|  -  description
                             -------------------
    begin                : Tue Apr 4 2000
    copyright            : (C) |1995-2000 by Martin Heni
    email                : martin@heni-online.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
#include <stdio.h>
#include "TDEProcessConnect.h"

#include "TDEProcessConnect.moc"

TDEProcessConnect::TDEProcessConnect()
  : KChildConnect()
{
  running=false;
  process=0;
}

TDEProcessConnect::~TDEProcessConnect()
{
  Exit();
  delete process;
//  printf("DESTRUCTRING KPROCESSCONNECT\n");
}

bool TDEProcessConnect::Init(int id,KEMessage *msg)
{
  int size;
  char *p;

  SetID(id);
  if (msg)
  {
    if (!msg->GetData(TQCString("ProcessName"),p,size)) return false; // no process name
    processname=p;
    /*
    printf("Found processname '%s' size %d size=%u\n",
       p,size,msg->QueryNumberOfKeys());
    */
    msg->Remove(TQCString("ProcessName"));
  }
  if (processname.length()<1) return false;

  inputbuffer="";

  // Delete first on multiple init
  if (running) Exit();

  // create process
  process=new TDEProcess;
  *process << processname;
  connect(process, TQT_SIGNAL(receivedStdout(TDEProcess *, char *, int )),
                        this, TQT_SLOT(slotReceivedStdout(TDEProcess *, char * , int )));
  connect(process, TQT_SIGNAL(processExited(TDEProcess *)),
                        this, TQT_SLOT(slotProcessExited(TDEProcess *)));
  /*
  connect(process, TQT_SIGNAL(wroteStdin(TDEProcess *)),
                        this, TQT_SLOT(slotWroteStdin(TDEProcess *)));
  */

  // TRUE if ok
  running=process->start(TDEProcess::NotifyOnExit,TDEProcess::All);

  if (running && msg && msg->QueryNumberOfKeys()>0)
  {
    SendMsg(msg);
  }

  return running;
}

void TDEProcessConnect::slotReceivedStdout(TDEProcess *, char *buffer, int buflen)
{
  TQString s;
  char c;
  int pos;

  if (buflen<1) return ;
  if (buffer[buflen-1]!=0) // shit..we got a not null terminated string
  {
    c=buffer[buflen-1];
    buffer[buflen-1]=0;
    s=buffer;
    s+=c;
  }
  else
  {
    s=buffer;
  }
  // Append old unresolved input
  s=inputbuffer+s;
  pos=s.findRev(KEMESSAGE_CR);
  // printf("String '%s' pos=%d len=%d\n",(const char *)s,pos,s.length());
  if (pos<0)
  {
    inputbuffer=s;
  }
  else if (pos+KEMESSAGE_CR.length()==s.length())
  {
    // CR at the end...calling receive
    Receive(s);
  }
  else
  {
    inputbuffer=s.right(s.length()-pos-KEMESSAGE_CR.length());
    s=s.left(pos+KEMESSAGE_CR.length());
    // printf("s='%s' in='%s'\n",(const char *)s,(const char *)inputbuffer);
    Receive(s);
  }
}
void TDEProcessConnect::slotProcessExited(TDEProcess *)
{
  running=false;
  delete process;
  process=0;
  Init(QueryID());
}
void TDEProcessConnect::slotWroteStdin(TDEProcess *)
{
  printf("slotWroteStdin:: IS NEVER CALLED\n");
}

bool TDEProcessConnect::Exit()
{
  // kill process if running
  if (running)
  {
    running=false;
    if (process) process->kill();
    delete process;
    process=0;
  }
  return true;
}

bool TDEProcessConnect::Next()
{
  bool result;
  if (!running) return false;
  // create and send message
  // printf("+- TDEProcessConnect::ProcessNext\n");
  KEMessage *msg=new KEMessage;
  // User fills message
  emit signalPrepareMove(msg,KG_INPUTTYPE_PROCESS);
  result=SendMsg(msg);
  delete msg;
  return result;
}

// Send string to child
bool TDEProcessConnect::Send(TQString str)
{
  bool result;
  // printf("****** PROCESS:SEND\n");
  if (!running || !process) return false;
  if (!str || str.length()<1) return true; // no need to send crap
  // TODO ..why?
  TQString s;
  s=KEMESSAGE_CR+KEMESSAGE_CR;
  str=s+str;
  // printf("+++ Sending to child '%s'!!!\n",(const char *)str);
  result=process->writeStdin(str.latin1(),str.length()+1);
  if (!result) printf("ERROR in PROCESS SEND\n");
  return result;
}