Skip to content

Commit

Permalink
turn off stdin buffing and echo
Browse files Browse the repository at this point in the history
  • Loading branch information
Sasasu committed May 11, 2019
1 parent 2aa0705 commit 01d941b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
3 changes: 2 additions & 1 deletion epoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ if read EOF from $IN_FD, close $OUT_FD and remove from poll

extern bool FAKE_FD[3]; // in main.c

int fd_sum = 0;

static int epollfd = 0;
static int fd_sum = 0;
static struct epoll_event ev, events[MAX_EVENT];

void set_nonblocking(int fd) {
Expand Down
2 changes: 2 additions & 0 deletions epoll.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
extern int epoll_init();
extern void start_loop();

extern int fd_sum;

#endif // EPOLL_H
30 changes: 28 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ void showhelp(char *name) {
printf("\t ls / </dev/null 2>/dev/null\n");
}

void setup_sig(void);

int main(int argc, char *argv[]) {
int i;
bool use_hook = false;
Expand All @@ -36,9 +38,10 @@ int main(int argc, char *argv[]) {
}

for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-stdin"))
if (!strcmp(argv[i], "-stdin")) {
set_ter(STDIN_FILENO, ~ICANON & ~ECHO);
FAKE_FD[STDIN_FILENO] = true;
else if (!strcmp(argv[i], "-stdout"))
} else if (!strcmp(argv[i], "-stdout"))
FAKE_FD[STDOUT_FILENO] = true;
else if (!strcmp(argv[i], "-stderr"))
FAKE_FD[STDERR_FILENO] = true;
Expand All @@ -55,8 +58,31 @@ int main(int argc, char *argv[]) {

pid = pty_fork_exec(argv[i], argv + i);

setup_sig();
epoll_init();
start_loop();

return waitpid(pid, NULL, 0);
}

void sig_handler(int signo) {
switch (signo) {
case SIGCHLD:
exit(waitpid(pid, NULL, 0));
default:
kill(pid, signo);
}
}

static struct sigaction sa;
void setup_sig(void) {
sa.sa_flags = SA_RESTART;
sa.sa_handler = &sig_handler;
sigfillset(&sa.sa_mask);
if (sigaction(SIGCHLD, &sa, NULL) == -1 || // sub process exit
sigaction(SIGINT, &sa, NULL) == -1 // C-c
) {
perror("handle signal");
exit(1);
}
}
12 changes: 12 additions & 0 deletions pty.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ pid_t pty_fork(int ptys[]) {
if (ptys[i] == 0)
continue;
ptys[i] = ptym_open(pty_names[i]);
if (i == STDIN_FILENO) {
set_ter(ptys[STDIN_FILENO], ~ICANON & ~ECHO);
}
}

if ((pid = fork()) < 0) {
Expand Down Expand Up @@ -106,3 +109,12 @@ int pty_fork_exec(char *path, char *arg[]) {
}
return pid;
}

void set_ter(int fd, tcflag_t flag) {
if (!isatty(fd))
return;
struct termios t;
tcgetattr(fd, &t);
t.c_lflag &= flag;
tcsetattr(fd, TCSANOW, &t);
}
2 changes: 2 additions & 0 deletions pty.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#ifndef PTY_H
#define PTY_H
#include <fcntl.h>
#include <termios.h>

extern int PTYS[3];

extern pid_t pty_fork(int ptys[]);
extern pid_t pty_fork_exec(char *path, char *arg[]);
extern void set_ter(int fd, tcflag_t flag);

#endif // PTY_H

0 comments on commit 01d941b

Please sign in to comment.