Make minish-eval a separate program
[minish.git] / src / ipc.h
blob20870edba6e1cae39a41fb1c67a662a3e02e6c1d
1 #ifndef PIPE_H
2 #define PIPE_H
4 #define _POSIX_C_SOURCE 200809L
5 #include <signal.h>
6 #include <sys/wait.h>
7 #include <unistd.h>
8 #include "try.h"
10 typedef struct {
11 int read;
12 int write;
13 } Pipe;
15 static void interactive_signals(void (* const handler)(int)) {
16 try_signal(SIGINT, handler);
17 try_signal(SIGQUIT, handler);
18 try_signal(SIGTSTP, handler);
21 static Pipe make_pipe() {
22 Pipe p;
23 int fds[2];
24 if (pipe(fds) == -1) {
25 pexit("pipe()");
27 p.read = fds[0];
28 p.write = fds[1];
29 return p;
32 static void move_fd(int const old, int const new) {
33 if (dup2(old, new) == -1) {
34 pexit("dup2()");
36 close(old);
39 static int wait_script(pid_t const pid) {
40 int status = try_waitpid(pid, WUNTRACED);
41 while (WIFSTOPPED(status)) {
42 if (WSTOPSIG(status) == SIGTSTP) {
43 try_signal(SIGTSTP, SIG_DFL);
44 raise(SIGTSTP);
45 try_signal(SIGTSTP, SIG_IGN);
47 status = try_waitpid(pid, WUNTRACED);
49 if (WIFSIGNALED(status)) {
50 switch (WTERMSIG(status)) {
51 case SIGINT:
52 try_signal(SIGINT, SIG_DFL);
53 raise(SIGINT);
54 case SIGQUIT: exit(EXIT_FAILURE);
57 return status;
60 #endif