17 ev_io_stop(main_loop, child_io);
18 ev_child_stop(main_loop, child_sig);
24 void strip_dzen_formats(char *buffer) {
27 while (*src != '\0') {
29 if (!strncmp(src, "^ro", strlen("^ro"))) {
47 void child_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
51 int buffer_len = STDIN_CHUNK_SIZE;
52 char *buffer = malloc(buffer_len);
53 memset(buffer, '\0', buffer_len);
55 n = read(fd, buffer + rec, buffer_len - rec);
57 if (errno == EAGAIN) {
60 printf("ERROR: read() failed!");
64 if (rec == buffer_len) {
66 buffer = malloc(buffer_len + STDIN_CHUNK_SIZE);
67 memset(buffer, '\0', buffer_len);
68 strncpy(buffer, tmp, buffer_len);
69 buffer_len += STDIN_CHUNK_SIZE;
77 if (strlen(buffer) == 0) {
81 strip_dzen_formats(buffer);
88 void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
89 printf("Child (pid: %d) unexpectedly exited with status %d\n", child_pid, watcher->rstatus);
93 void start_child(char *command) {
99 printf("ERROR: Couldn't fork()");
104 dup2(fd[1], STDOUT_FILENO);
106 static const char *shell = NULL;
108 if ((shell = getenv("SHELL")) == NULL)
111 execl(shell, shell, "-c", command, (char*) NULL);
116 dup2(fd[0], STDIN_FILENO);
117 fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
119 child_io = malloc(sizeof(ev_io));
120 ev_io_init(child_io, &child_io_cb, STDIN_FILENO, EV_READ);
121 ev_io_start(main_loop, child_io);
123 /* We must cleanup, if the child unexpectedly terminates */
124 child_sig = malloc(sizeof(ev_io));
125 ev_child_init(child_sig, &child_sig_cb, child_pid, 0);
126 ev_child_start(main_loop, child_sig);
133 kill(child_pid, SIGQUIT);