-
Notifications
You must be signed in to change notification settings - Fork 27
/
cmd.c
144 lines (141 loc) · 3.14 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#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 and stderr */
close(1);
dup(pipefds1[1]);
close(2);
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 shell command.
*
* If ibuf is given, it is passed as standard input to the process.
* Otherwise, the process reads from the terminal.
*
* If oproc is 0, the process writes directly to the terminal. If it
* is 1, process' output is saved and returned. If it is 2, in addition
* to returning the output, it is written to the terminal.
*/
char *cmd_pipe(char *cmd, char *ibuf, 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 = ibuf != NULL ? strlen(ibuf) : 0;
int nw = 0;
int pid = cmd_make(argv, ibuf != NULL ? &ifd : NULL, oproc ? &ofd : NULL);
if (pid <= 0)
return NULL;
if (oproc)
sb = sbuf_make();
if (ibuf == NULL) {
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 = isatty(0) && ibuf != NULL ? 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 && oproc == 2)
write(1, buf, ret);
if (ret > 0)
sbuf_mem(sb, buf, ret);
if (ret <= 0) {
close(fds[0].fd);
fds[0].fd = -1;
}
} else if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) {
close(fds[0].fd);
fds[0].fd = -1;
}
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);
fds[1].fd = -1;
}
} else if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) {
close(fds[1].fd);
fds[1].fd = -1;
}
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);
} else if (fds[2].revents & (POLLERR | POLLHUP | POLLNVAL)) {
fds[2].fd = -1;
}
}
close(fds[0].fd);
close(fds[1].fd);
waitpid(pid, NULL, 0);
if (ibuf == NULL) {
term_init();
signal(SIGINT, SIG_DFL);
}
if (oproc)
return sbuf_done(sb);
return NULL;
}
int cmd_exec(char *cmd)
{
cmd_pipe(cmd, NULL, 0);
return 0;
}