4 * Copyright (C) 2002 Felix Domke <tmbinc@tuxbox.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * $Id: console.cpp,v 1.1 2003-10-17 15:35:47 tmbinc Exp $
23 #include <lib/base/console.h>
25 #include <lib/base/estring.h>
26 #include <sys/vfs.h> // for statfs
31 int bidirpipe(int pfd[], char *cmd , char *argv[])
33 int pfdin[2]; /* from child to parent */
34 int pfdout[2]; /* from parent to child */
35 int pfderr[2]; /* stderr from child to parent */
36 int pid; /* child's pid */
38 if ( pipe(pfdin) == -1 || pipe(pfdout) == -1 || pipe(pfderr) == -1)
41 if ( ( pid = fork() ) == -1 )
43 else if (pid == 0) /* child process */
45 if ( close(0) == -1 || close(1) == -1 || close(2) == -1 )
48 if (dup(pfdout[0]) != 0 || dup(pfdin[1]) != 1 || dup(pfderr[1]) != 2 )
51 if (close(pfdout[0]) == -1 || close(pfdout[1]) == -1 ||
52 close(pfdin[0]) == -1 || close(pfdin[1]) == -1 ||
53 close(pfderr[0]) == -1 || close(pfderr[1]) == -1 )
59 if (close(pfdout[0]) == -1 || close(pfdin[1]) == -1 || close(pfderr[1]) == -1)
69 eConsoleAppContainer::eConsoleAppContainer( const eString &cmd )
70 :pid(-1), killstate(0), outbuf(0)
72 // eDebug("cmd = %s", cmd.c_str() );
73 memset(fd, 0, sizeof(fd) );
74 int cnt=2; // path to app + terminated 0
75 eString str(cmd?cmd:"");
77 while( str.length() && str[0] == ' ' ) // kill spaces at beginning
80 while( str.length() && str[str.length()-1] == ' ' ) // kill spaces at the end
81 str = str.left( str.length() - 1 );
87 eString path = str.left( (idx = str.find(' ')) != eString::npos ? idx : str.length() );
88 // eDebug("path = %s", path.c_str() );
90 eString cmds = str.mid( path.length()+1 );
91 // eDebug("cmds = %s", cmds.c_str() );
94 while ( (idx = cmds.find(' ',idx) ) != eString::npos ) // count args
100 // eDebug("idx = %d, %d counted spaces", idx, cnt-2);
105 // eDebug("increase cnt");
108 // eDebug("%d args", cnt-2);
109 char **argv = new char*[cnt]; // min two args... path and terminating 0
110 argv[0] = new char[ path.length() ];
111 strcpy( argv[0], path.c_str() );
112 argv[cnt-1] = 0; // set terminating null
114 if ( cnt > 2 ) // more then default args?
116 cnt=1; // do not overwrite path in argv[0]
118 while ( (idx = cmds.find(' ')) != eString::npos ) // parse all args..
120 argv[cnt] = new char[ idx ];
121 // eDebug("idx=%d, arg = %s", idx, cmds.left(idx).c_str() );
122 strcpy( argv[cnt++], cmds.left( idx ).c_str() );
123 cmds = cmds.mid(idx+1);
124 // eDebug("str = %s", cmds.c_str() );
126 // store the last arg
127 argv[cnt] = new char[ cmds.length() ];
128 strcpy( argv[cnt], cmds.c_str() );
131 // get one read ,one write and the err pipe to the prog..
133 if ( (pid = bidirpipe(fd, argv[0], argv)) == -1 )
141 while ( cnt-- > 0 ) // release heap memory
145 eDebug("pipe in = %d, out = %d, err = %d", fd[0], fd[1], fd[2]);
147 in = new eSocketNotifier(eApp, fd[0], 19 ); // 19 = POLLIN, POLLPRI, POLLHUP
148 out = new eSocketNotifier(eApp, fd[1], eSocketNotifier::Write); // POLLOUT
149 err = new eSocketNotifier(eApp, fd[2], 19 ); // 19 = POLLIN, POLLPRI, POLLHUP
150 CONNECT(in->activated, eConsoleAppContainer::readyRead);
151 CONNECT(out->activated, eConsoleAppContainer::readyWrite);
152 CONNECT(err->activated, eConsoleAppContainer::readyErrRead);
153 signal(SIGCHLD, SIG_IGN); // no zombie when child killed
156 eConsoleAppContainer::~eConsoleAppContainer()
167 void eConsoleAppContainer::kill()
170 system( eString().sprintf("kill %d", pid).c_str() );
171 eDebug("user kill console App");
174 void eConsoleAppContainer::closePipes()
185 eDebug("pipes closed");
188 void eConsoleAppContainer::readyRead(int what)
190 if (what & POLLPRI|POLLIN)
194 int readed = read(fd[0], buf, 2048);
195 eDebug("%d bytes read", readed);
196 if ( readed != -1 && readed )
197 /*emit*/ dataAvail( eString( buf ) );
198 else if (readed == -1)
199 eDebug("readerror %d", errno);
201 if (what & eSocketNotifier::Hungup)
203 eDebug("child has terminated");
205 /*emit*/ appClosed(killstate);
209 void eConsoleAppContainer::readyErrRead(int what)
211 if (what & POLLPRI|POLLIN)
215 int readed = read(fd[2], buf, 2048);
216 eDebug("%d bytes read", readed);
217 if ( readed != -1 && readed )
218 /*emit*/ dataAvail( eString( buf ) );
219 else if (readed == -1)
220 eDebug("readerror %d", errno);
224 void eConsoleAppContainer::write( const eString & str )
226 outbuf = new char[ str.length()];
227 strcpy( outbuf, str.c_str() );
230 void eConsoleAppContainer::readyWrite(int what)
232 if (what == 4 && outbuf)
234 if ( ::write( fd[1], outbuf, strlen(outbuf) ) != (int) strlen(outbuf) )
236 /* emit */ dataSent(-1);
237 eDebug("writeError");
241 /* emit */ dataSent(0);