-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.c
191 lines (166 loc) · 3.9 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Alina Hlazkova (346316979)
* Sergey Nazaryev (342537685)
*/
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include "helper.h"
#include "builtin.h"
#include "cmd.h"
#include "validate.h"
int cmd_argslen(command_t * const cmd)
{
int len = 0;
for(len = 0; cmd->args[len] != NULL; len++);
return len;
}
int cmd_parse(char *cmdline, command_t *cmd)
{
int prev_is_space = 1;
static char *line = NULL;
int pos, i, argcnt;
size_t len;
char *curargptr;
if (cmdline != NULL)
line = cmdline;
len = strlen(line);
argcnt = 0;
for(pos = 0; pos < len; pos++) {
if (line[pos] != ' ' && prev_is_space) {
curargptr = line + pos;
prev_is_space = 0;
if (line[pos] == '|') {
line = line + pos + 1;
break;
}
} else if (line[pos] == ' ' && !prev_is_space) {
line[pos] = '\0';
cmd->args[argcnt++] = curargptr;
prev_is_space = 1;
} else if (line[pos] == ' ' && prev_is_space) {
continue;
} else if (line[pos] == '\n') {
line[pos] = '\0';
cmd->args[argcnt++] = curargptr;
} else if (line[pos] == '|') {
line[pos] = '\0';
cmd->args[argcnt++] = curargptr;
line = line + pos + 1;
break;
}
}
cmd->stdout_redirect = NULL;
cmd->args[argcnt++] = NULL;
for(i = 0; cmd->args[i] != NULL; i++) {
if (cmd->args[i][0] == '>' && cmd->args[i+1] != NULL) {
cmd->stdout_redirect = cmd->args[i+1];
cmd->args[i] = NULL;
}
}
return 0;
}
void cmd_pipe(command_t * const cmd1, command_t * const cmd2) {
int pipefd[2];
pid_t pid1, pid2;
int status1, status2;
if (pipe(pipefd) == -1) {
perror("Failed to create pipe");
return;
}
pid1 = fork();
if (pid1 == -1) {
perror("Failed to fork proccess");
close(pipefd[0]);
close(pipefd[1]);
return;
}
if (pid1 > 0) {
pid2 = fork();
if (pid2 == -1) {
perror("Failed to fork proccess");
close(pipefd[0]);
close(pipefd[1]);
// TODO: kill child 1
}
if (pid2 > 0) {
// parent: close fd[0], fd[1], wait for childrens
signal(SIGINT, SIG_IGN);
close(pipefd[0]);
close(pipefd[1]);
waitpid(pid1, &status1, 0);
waitpid(pid2, &status2, 0);
signal(SIGINT, SIG_DFL);
} else {
// child 2: close fd[0], stdout -> fd[1]
close(pipefd[0]);
dup2(pipefd[1], 1);
close(pipefd[1]);
cmd_run(cmd1);
_exit(0);
}
} else {
// child 1: close fd[1], stdin <- fd[0]
close(pipefd[1]);
dup2(pipefd[0], 0);
close(pipefd[0]);
cmd_run(cmd2);
_exit(0);
}
}
int cmd_validate(command_t * const cmd)
{
struct validate_info *info = validate_info_get(cmd->args[0]);
if (info == NULL) {
fprintf(stderr, "Command `%s` is not supported\n", cmd->args[0]);
return 0;
}
if (!info->validate(cmd))
return 0;
return 1;
}
void cmd_run(command_t * const cmd)
{
pid_t pid;
int status, fd;
builtin *cmdptr;
if ((cmdptr = is_builtin(cmd)) != NULL) {
if (!cmd_validate(cmd))
return;
dup2(1, 10);
fd = open(cmd->stdout_redirect, O_WRONLY|O_CREAT|O_TRUNC, 0666);
dup2(fd, 1);
close(fd);
cmdptr(cmd_argslen(cmd), cmd->args);
dup2(10, 1);
close(10);
} else {
pid = fork();
if (pid == -1) {
perror("Failed to fork proccess");
}
if (pid > 0) {
signal(SIGINT, SIG_IGN);
waitpid(pid, &status, 0);
signal(SIGINT, SIG_DFL);
} else if (pid == 0) {
if (!cmd_validate(cmd))
_exit(2);
if (cmd->stdout_redirect) {
int fd = open(cmd->stdout_redirect, O_WRONLY|O_CREAT|O_TRUNC, 0666);
dup2(fd, 1);
close(fd);
}
if (execvp(cmd->args[0], cmd->args) == -1) {
perror("Failed to exec");
_exit(2);
}
}
}
}