#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);
}
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++) {
}
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);
}
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];
if (is_iac) {
if (c == BRK) {
- Break(pid);
+ generate_break(pid);
} else {
is_iac = 0;
}
is_iac = 1;
}
buf[0] = c;
- write(par2child[1], buf, 1);
+ write(write_pipe[1], buf, 1);
}
}
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);
}
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++) {
PrintUsage();
}
- char buf[1024];
-
STARTUPINFO si;
SECURITY_ATTRIBUTES sa;
PROCESS_INFORMATION pi;
unsigned long b_write;
unsigned long avail;
+ char buf[1];
memset(buf, 0, sizeof(buf));
for (;;) {
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);