#include "UIshowexec.h"
UIshowexec::UIshowexec(int dimx,int dimy,const char* command,const char* label,unsigned char font,unsigned char fontsize)
{
m_pid=-1;
m_wMain = new Fl_Double_Window (dimx,dimy);
if (label) m_wMain->label(label);
m_wMain->callback(cb_wMain_link, this);
m_buffer = new Fl_Text_Buffer();
m_display = new Fl_Text_Display( 5,5,(dimx-10),(dimy-10) );
m_display->buffer(m_buffer);
m_display->textfont(font);
m_display->textsize(fontsize);
m_wMain->end();
pipe(m_pipe);
m_pid=fork();
if (m_pid==0){
close(m_pipe[0]);
dup2(m_pipe[1], 2);
close(m_pipe[1]);
char cmd[1024];
snprintf(cmd,1024,"(%s) 1>&2",command);
execlp("/bin/sh", "sh", "-c", cmd, 0);
}
close(m_pipe[1]);
Fl::add_fd(m_pipe[0], cb_Data_link, this);
m_wMain->resizable(m_wMain);
m_wMain->show();
}
UIshowexec::~UIshowexec()
{
delete m_display;
delete m_buffer;
delete m_wMain;
}
void UIshowexec::cb_wMain()
{
if ( m_pid != -1 ) kill(m_pid, 9);
m_wMain->hide();
}
void UIshowexec::cb_Data(int fd)
{
char s[4096];
int bytes = read(fd, s, 4096-1);
if ( bytes == 0 ) { waitpid(m_pid,NULL,WNOHANG);
close(fd);
Fl::remove_fd(fd);
m_pid = -1;
} else { s[bytes] = 0;
m_buffer->append(s);
}
}