forked from aligrudi/neatvi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.c
127 lines (124 loc) · 2.63 KB
/
cmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include "vi.h"
static int cmd_make(char **argv, int *ifd, int *ofd)
{
int pid;
int pipefds0[2];
int pipefds1[2];
if (ifd)
pipe(pipefds0);
if (ofd)
pipe(pipefds1);
if (!(pid = fork())) {
if (ifd) { /* setting up stdin */
close(0);
dup(pipefds0[0]);
close(pipefds0[1]);
close(pipefds0[0]);
}
if (ofd) { /* setting up stdout */
close(1);
dup(pipefds1[1]);
close(pipefds1[0]);
close(pipefds1[1]);
}
execvp(argv[0], argv);
exit(1);
}
if (ifd)
close(pipefds0[0]);
if (ofd)
close(pipefds1[1]);
if (pid < 0) {
if (ifd)
close(pipefds0[1]);
if (ofd)
close(pipefds1[0]);
return -1;
}
if (ifd)
*ifd = pipefds0[1];
if (ofd)
*ofd = pipefds1[0];
return pid;
}
/* execute a command; process input if iproc and process output if oproc */
char *cmd_pipe(char *cmd, char *ibuf, int iproc, int oproc)
{
char *argv[] = {"/bin/sh", "-c", cmd, NULL};
struct pollfd fds[3];
struct sbuf *sb = NULL;
char buf[512];
int ifd = -1, ofd = -1;
int slen = iproc ? strlen(ibuf) : 0;
int nw = 0;
int pid = cmd_make(argv, iproc ? &ifd : NULL, oproc ? &ofd : NULL);
if (pid <= 0)
return NULL;
if (oproc)
sb = sbuf_make();
if (!iproc) {
signal(SIGINT, SIG_IGN);
term_done();
}
fcntl(ifd, F_SETFL, fcntl(ifd, F_GETFL, 0) | O_NONBLOCK);
fds[0].fd = ofd;
fds[0].events = POLLIN;
fds[1].fd = ifd;
fds[1].events = POLLOUT;
fds[2].fd = iproc ? 0 : -1;
fds[2].events = POLLIN;
while ((fds[0].fd >= 0 || fds[1].fd >= 0) && poll(fds, 3, 200) >= 0) {
if (fds[0].revents & POLLIN) {
int ret = read(fds[0].fd, buf, sizeof(buf));
if (ret > 0)
sbuf_mem(sb, buf, ret);
if (ret <= 0)
close(fds[0].fd);
continue;
}
if (fds[1].revents & POLLOUT) {
int ret = write(fds[1].fd, ibuf + nw, slen - nw);
if (ret > 0)
nw += ret;
if (ret <= 0 || nw == slen)
close(fds[1].fd);
continue;
}
if (fds[2].revents & POLLIN) {
int ret = read(fds[2].fd, buf, sizeof(buf));
int i;
for (i = 0; i < ret; i++)
if ((unsigned char) buf[i] == TK_CTL('c'))
kill(pid, SIGINT);
}
if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL))
fds[0].fd = -1;
if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL))
fds[1].fd = -1;
if (fds[2].revents & (POLLERR | POLLHUP | POLLNVAL))
fds[2].fd = -1;
}
close(ifd);
close(ofd);
waitpid(pid, NULL, 0);
if (!iproc) {
term_init();
signal(SIGINT, SIG_DFL);
}
if (oproc)
return sbuf_done(sb);
return NULL;
}
int cmd_exec(char *cmd)
{
cmd_pipe(cmd, NULL, 0, 0);
return 0;
}