fixed io in runnerw(PY-1305)

added runner for mac
This commit is contained in:
Dmitry Trofimov
2010-07-18 10:19:13 +04:00
parent 4b2b42e31f
commit 779aeb5c01
2 changed files with 31 additions and 43 deletions
+21 -29
View File
@@ -1,35 +1,32 @@
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string>
#include <signal.h>
void PrintUsage() {
printf("Usage: runnerw.exe <app> <args>\n");
void print_usage() {
printf("Usage: runneru <app> <args>\n");
printf("where <app> is console application and <args> it's arguments.\n");
printf("\n");
printf(
"Runner invokes console application as a process with inherited input and output streams.\n");
"Runner executes an application as a process with inherited input and output streams.\n");
printf(
"Input stream is scanned for presence of 2 char 255(IAC) and 243(BRK) sequence and generates Ctrl-Break event in that case.\n");
printf(
"Also in case of all type of event(Ctrl-C, Close, Shutdown etc) Ctrl-Break event is generated.");
"Input stream is scanned for presence of 2 char 255(IAC) and 243(BRK) sequence and generates Ctrl-C(SIGING) event in that case.\n");
exit(0);
}
void SigintHandler(int sig)
void sigint_handler(int sig)
{
kill(0, SIGINT);
signal(SIGINT, SIG_DFL);
kill(getpid(), SIGINT);
}
void Break(int pid) {
void generate_break(int pid) {
kill(0, SIGINT);
// kill(pid, SIGTERM);
exit(0);
}
@@ -38,12 +35,9 @@ char BRK = 243;
int main(int argc, char **argv) {
if (argc < 2) {
PrintUsage();
print_usage();
}
std::string app(argv[1]);
std::string args("");
char* argv2[argc];
for (int i = 1; i < argc; i++) {
@@ -51,16 +45,16 @@ int main(int argc, char **argv) {
}
argv2[argc-1]=0;
if (app.length() == 0) {
PrintUsage();
if (strlen(argv[0]) == 0) {
print_usage();
}
int par2child[2]; /* Pipe toward child */
int write_pipe[2]; /* Pipe toward child */
signal(SIGINT, &SigintHandler);
signal(SIGINT, &sigint_handler);
/* Open pipes */
if (pipe(par2child) == -1) {
if (pipe(write_pipe) == -1) {
perror("pipe");
exit(1);
}
@@ -75,27 +69,25 @@ int main(int argc, char **argv) {
case 0:
/* Child section */
if (dup2(par2child[0], STDIN_FILENO) == -1
) {
if (dup2(write_pipe[0], STDIN_FILENO) == -1) {
perror("dup2");
exit(1);
}
/* Close unused fildes. */
close(par2child[0]);
close(par2child[1]);
close(write_pipe[0]);
close(write_pipe[1]);
/* Exec! */
execv(app.c_str(), argv2);
execv(argv[0], argv2);
perror("execv");
exit(1);
default:
close(par2child[0]);
close(write_pipe[0]);
int is_iac = 0;
char buf[1];
@@ -104,7 +96,7 @@ int main(int argc, char **argv) {
if (is_iac) {
if (c == BRK) {
Break(pid);
generate_break(pid);
} else {
is_iac = 0;
}
@@ -113,7 +105,7 @@ int main(int argc, char **argv) {
is_iac = 1;
}
buf[0] = c;
write(par2child[1], buf, 1);
write(write_pipe[1], buf, 1);
}
}
+10 -14
View File
@@ -10,7 +10,7 @@ void PrintUsage() {
printf("\n");
printf("Runner invokes console application as a process with inherited input and output streams.\n");
printf("Input stream is scanned for presence of 2 char 255(IAC) and 243(BRK) sequence and generates Ctrl-Break event in that case.\n");
printf("Also in case of all type of event(Ctrl-C, Close, Shutdown etc) Ctrl-Break event is generated.");
printf("Also in case of all type of event(Ctrl-C, Close, Shutdown etc) Ctrl-Break event is generated.\n");
exit(0);
}
@@ -35,8 +35,8 @@ void CtrlBreak() {
BOOL is_iac = FALSE;
char IAC = 255;
char BRK = 243;
char IAC = 'a';
char BRK = 'b';
BOOL Scan(char buf[], int count) {
for (int i = 0; i<count; i++) {
@@ -89,8 +89,6 @@ int main(int argc, char * argv[]) {
PrintUsage();
}
char buf[1024];
STARTUPINFO si;
SECURITY_ATTRIBUTES sa;
PROCESS_INFORMATION pi;
@@ -137,6 +135,7 @@ int main(int argc, char * argv[]) {
unsigned long b_write;
unsigned long avail;
char buf[1];
memset(buf, 0, sizeof(buf));
for (;;) {
@@ -145,15 +144,12 @@ int main(int argc, char * argv[]) {
if (exit != STILL_ACTIVE)
break;
PeekNamedPipe(GetStdHandle(STD_INPUT_HANDLE), buf, 1023, &b_read, &avail, NULL);
if (b_read != 0) {
memset(buf, 0, sizeof(buf));
ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf, 1023, &b_read, NULL);
Scan(buf, b_read);
WriteFile(write_stdin, buf, b_read, &b_write, NULL);
}
char c;
std::cin >> c;
// char c = fgetc();
buf[0] = c;
Scan(buf, 1);
WriteFile(write_stdin, buf, 1, &b_write, NULL);
}
CloseHandle(pi.hThread);