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
|
/*
* Copyright (C) 1999 Jonathan R. Hudson
* Developed by Jonathan R. Hudson <jonathan@daria.co.uk>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
// Example to show VDKInputChannel, multiple input streams,
// user defined VDKsignals, interaction with Unix signals etc
#define _GNU_SOURCE
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/wait.h>
#include <ctime>
#include <fstream>
#include <sys/socket.h>
#include "iodemo0.h"
DEFINE_SIGNAL_MAP(MyForm,VDKForm)
ON_SIGNAL(inp1,INPUT_SIGNAL,DoIO),
ON_SIGNAL(text, realize_signal,runcmd),
ON_SIGNAL(entry, activate_signal,sendstuff),
ON_SIGNAL(timer,timer_tick_signal,Quit),
ON_SIGNAL(quit,clicked_signal,Quit)
END_SIGNAL_MAP
DEFINE_SIGNAL_LIST(MyForm,VDKForm);
bool MyForm::sendstuff(VDKObject* obj)
{
char txt[1024];
char *t;
strcpy(txt, entry->GetText());
t = txt+strlen(txt);
*t++ = '\n';
*t = 0;
if(xmit != -1)
{
write(xmit, txt, (t - txt));
}
entry->SetText("");
return true;
}
bool MyForm::CanClose(void)
{
return Stop(0);
}
bool MyForm::Stop (VDKObject *obj)
{
if(pid)
{
kill(pid, SIGTERM);
}
return true;
}
bool MyForm::DoIO (VDKObject *obj)
{
VDKInputChannel *ip = static_cast<VDKInputChannel*>(obj);
int res;
char buf[1024];
res = read (ip->getfd(), buf, sizeof(buf));
if(res > 0)
{
text->TextInsert(buf, res);
}
if(res == 0)
{
close(ip->getfd());
ShowInfanticide();
ip->Destroy();
}
return true;
}
bool MyForm::runcmd(VDKObject *obj)
{
MyApp *a = static_cast<MyApp *>(Application());
int sock[2];
if(access (a->args[0], X_OK) == 0)
{
socketpair(AF_FILE, SOCK_STREAM, 0, sock);
pid = fork();
switch(pid)
{
// Child
case 0:
close(sock[0]);
dup2(sock[1], 0);
dup2(sock[1], 1);
dup2(sock[1], 2);
execvp(a->args[0], a->args);
_exit(0);
break;
case -1:
break;
default:
// Parent
close(sock[1]);
xmit = sock[0];
inp1 = new VDKInputChannel(this,sock[0]);
text->Clear();
break;
}
}
else
{
char x[256];
sprintf(x, "Can't execute your program \"%s\"",
(a->args[0]) ? a->args[0] : "????");
text->TextInsert(x);
SignalEmit("KillSignal");
}
return true;
}
bool MyForm:: Quit(VDKObject*o)
{
if(pid)
{
kill(pid, SIGTERM);
}
Application()->Terminate();
return true;
}
void MyForm::Setup(void)
{
VDKBox* vbox1 = new VDKBox(this, v_box);
VDKFrame* frame1 = new VDKFrame(this, "You type here");
VDKFrame* frame2 = new VDKFrame(this, "Your forked friend");
// Just to show user signals ....
SignalConnect("KillSignal",&MyForm::Mourn, false);
VDKBox* hbox1 = new VDKBox(this, h_box);
vbox1->Add(frame1);
vbox1->Add(frame2);
vbox1->Add(hbox1,c_justify, 0, 0);
hbox1->Add((quit = new VDKLabelButton(this,"Quit")),
c_justify, 1, 0);
entry = new VDKEntry(this, 0);
entry->Font = new VDKFont(this,
"-*-lucidatypewriter-medium-r-normal-*-*-120-*-*-*-*-iso8859-1");
frame1->Add(entry);
text = new VDKText(this, 0);
text->Font = new VDKFont(this,
"-*-lucidatypewriter-medium-r-normal-*-*-120-*-*-*-*-iso8859-1");
frame2->Add(text);
Add(vbox1);
SetSize(500,-1);
entry->GrabFocus();
}
void MyForm::ShowInfanticide()
{
SignalEmit("KillSignal");
Pid(0);
}
bool MyForm::Mourn(VDKObject *)
{
if(timer == 0)
{
timer = new VDKTimer(this, 30*1000);
}
text->TextInsert("\nPress Quit to exit(or wait 30 seconds)\n");
return true;
}
// This is a static member so we can access class members Somewhat
// contrived example In this case, we don't get SIGPIPE but we should
// be ready to handle it
void MyApp::plumber(int sig)
{
cerr << "Otherwise unhandled Unix Signal " << sig << endl;
}
void MyApp::reaper(int)
{
int sts;
int pid;
pid = waitpid(-1, &sts, WNOHANG);
if(pid == ((MyForm*)MyApp::me->MainForm)->Pid())
{
cerr << "-- child dies, pid was " << pid << ", status " << sts << endl;
}
}
void MyApp::Setup()
{
if(*args == NULL)
{
cerr << "No command to run" << endl;
}
MainForm = new MyForm(this,"Running Command ...");
MyApp::me = this;
struct sigaction sac;
sigemptyset(&(sac.sa_mask));
sac.sa_flags=0;
sac.sa_handler = MyApp::reaper;
sigaction(SIGCHLD, &sac, NULL);
for(int i = 0; i < NSIG; i++)
{
if(i != SIGQUIT && i != SIGKILL && i != SIGTERM && i != SIGCHLD &&
i != SIGSTOP && i != SIGCONT && i != SIGTSTP)
{
sigemptyset(&(sac.sa_mask));
sac.sa_flags=0;
sac.sa_handler = MyApp::plumber;
sigaction(i, &sac, NULL);
}
}
MainForm->Setup();
MainForm->Show();
}
MyApp *MyApp::me;
int main (int argc, char *argv[])
{
MyApp app(&argc, argv);
app.Run();
return 0;
}
|