-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathsh2.c
332 lines (293 loc) · 7.12 KB
/
sh2.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
struct cmd {
int argc;
char **argv;
int capa;
int status;
int pid;
struct cmd *next;
};
#define REDIRECT_P(cmd) ((cmd)->argc == -1)
#define PID_BUILTIN -2
#define BUILTIN_P(cmd) ((cmd)->pid == PID_BUILTIN)
struct builtin {
char *name;
int (*f)(int argc, char *argv[]);
};
static void prompt(void);
static int invoke_commands(struct cmd *cmd);
static void exec_pipeline(struct cmd *cmdhead);
static void redirect_stdout(char *path);
static int wait_pipeline(struct cmd *cmdhead);
static struct cmd* pipeline_tail(struct cmd *cmdhead);
static struct cmd* parse_command_line(char *cmdline);
static void free_cmd(struct cmd *p);
static struct builtin* lookup_builtin(char *name);
static int builtin_cd(int argc, char *argv[]);
static int builtin_pwd(int argc, char *argv[]);
static int builtin_exit(int argc, char *argv[]);
static void* xmalloc(size_t sz);
static void* xrealloc(void *ptr, size_t sz);
static char *program_name;
int
main(int argc, char *argv[])
{
program_name = argv[0];
for (;;) {
prompt();
}
exit(0);
}
#define LINEBUF_MAX 2048
static void
prompt(void)
{
static char buf[LINEBUF_MAX];
struct cmd *cmd;
fprintf(stdout, "$ ");
fflush(stdout);
if (fgets(buf, LINEBUF_MAX, stdin) == NULL)
exit(0);
cmd = parse_command_line(buf);
if (cmd == NULL) {
fprintf(stderr, "%s: syntax error\n", program_name);
return;
}
if (cmd->argc > 0)
invoke_commands(cmd);
free_cmd(cmd);
}
static int
invoke_commands(struct cmd *cmdhead)
{
int st;
int original_stdin = dup(0);
int original_stdout = dup(1);
exec_pipeline(cmdhead);
st = wait_pipeline(cmdhead);
close(0); dup2(original_stdin, 0); close(original_stdin);
close(1); dup2(original_stdout, 1); close(original_stdout);
return st;
}
#define HEAD_P(cmd) ((cmd) == cmdhead)
#define TAIL_P(cmd) (((cmd)->next == NULL) || REDIRECT_P((cmd)->next))
static void
exec_pipeline(struct cmd *cmdhead)
{
struct cmd *cmd;
int fds1[2] = {-1, -1};
int fds2[2] = {-1, -1};
for (cmd = cmdhead; cmd && !REDIRECT_P(cmd); cmd = cmd->next) {
fds1[0] = fds2[0];
fds1[1] = fds2[1];
if (! TAIL_P(cmd)) {
if (pipe(fds2) < 0) {
perror("pipe");
exit(3);
}
}
if (lookup_builtin(cmd->argv[0]) != NULL) {
cmd->pid = PID_BUILTIN;
}
else {
cmd->pid = fork();
if (cmd->pid < 0) {
perror("fork");
exit(3);
}
if (cmd->pid > 0) { /* parent */
if (fds1[0] != -1) close(fds1[0]);
if (fds1[1] != -1) close(fds1[1]);
continue;
}
}
if (! HEAD_P(cmd)) {
close(0); dup2(fds1[0], 0); close(fds1[0]);
close(fds1[1]);
}
if (! TAIL_P(cmd)) {
close(fds2[0]);
close(1); dup2(fds2[1], 1); close(fds2[1]);
}
if ((cmd->next != NULL) && REDIRECT_P(cmd->next)) {
redirect_stdout(cmd->next->argv[0]);
}
if (!BUILTIN_P(cmd)) {
execvp(cmd->argv[0], cmd->argv);
fprintf(stderr, "%s: command not found: %s\n",
program_name, cmd->argv[0]);
exit(1);
}
}
}
static void
redirect_stdout(char *path)
{
int fd;
close(1);
fd = open(path, O_WRONLY|O_TRUNC|O_CREAT, 0666);
if (fd < 0) {
perror(path);
return;
}
if (fd != 1) {
dup2(fd, 1);
close(fd);
}
}
static int
wait_pipeline(struct cmd *cmdhead)
{
struct cmd *cmd;
for (cmd = cmdhead; cmd && !REDIRECT_P(cmd); cmd = cmd->next) {
if (BUILTIN_P(cmd))
cmd->status = lookup_builtin(cmd->argv[0])->f(cmd->argc, cmd->argv);
else
waitpid(cmd->pid, &cmd->status, 0);
}
return pipeline_tail(cmdhead)->status;
}
static struct cmd*
pipeline_tail(struct cmd *cmdhead)
{
struct cmd *cmd;
for (cmd = cmdhead; !TAIL_P(cmd); cmd = cmd->next)
;
return cmd;
}
#define INIT_ARGV 8
#define IDENT_CHAR_P(c) (!isspace((int)c) && ((c) != '|') && ((c) != '>'))
static struct cmd*
parse_command_line(char *p)
{
struct cmd *cmd;
cmd = xmalloc(sizeof(struct cmd));
cmd->argc = 0;
cmd->argv = xmalloc(sizeof(char*) * INIT_ARGV);
cmd->capa = INIT_ARGV;
cmd->next = NULL;
while (*p) {
while (*p && isspace((int)*p))
*p++ = '\0';
if (! IDENT_CHAR_P(*p))
break;
if (*p && IDENT_CHAR_P(*p)) {
if (cmd->capa <= cmd->argc) {
cmd->capa *= 2;
cmd->argv = xrealloc(cmd->argv, cmd->capa);
}
cmd->argv[cmd->argc] = p;
cmd->argc++;
}
while (*p && IDENT_CHAR_P(*p))
p++;
}
if (cmd->capa <= cmd->argc) {
cmd->capa += 1;
cmd->argv = xrealloc(cmd->argv, cmd->capa);
}
cmd->argv[cmd->argc] = NULL;
if (*p == '|' || *p == '>') {
if (cmd == NULL || cmd->argc == 0) goto parse_error;
cmd->next = parse_command_line(p + 1);
if (cmd->next == NULL || cmd->next->argc == 0) goto parse_error;
if (*p == '>') {
if (cmd->next->argc != 1) goto parse_error;
cmd->next->argc = -1;
}
*p = '\0';
}
return cmd;
parse_error:
if (cmd) free_cmd(cmd);
return NULL;
}
static void
free_cmd(struct cmd *cmd)
{
if (cmd->next != NULL)
free_cmd(cmd->next);
free(cmd->argv);
free(cmd);
}
struct builtin builtins_list[] = {
{"cd", builtin_cd},
{"pwd", builtin_pwd},
{"exit", builtin_exit},
{NULL, NULL}
};
static struct builtin*
lookup_builtin(char *cmd)
{
struct builtin *p;
for (p = builtins_list; p->name; p++) {
if (strcmp(cmd, p->name) == 0)
return p;
}
return NULL;
}
static int
builtin_cd(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "%s: wrong argument\n", argv[0]);
return 1;
}
if (chdir(argv[1]) < 0) {
perror(argv[1]);
return 1;
}
return 0;
}
static int
builtin_pwd(int argc, char *argv[])
{
char buf[PATH_MAX];
if (argc != 1) {
fprintf(stderr, "%s: wrong argument\n", argv[0]);
return 1;
}
if (!getcwd(buf, PATH_MAX)) {
fprintf(stderr, "%s: cannot get working directory\n", argv[0]);
return 1;
}
printf("%s\n", buf);
return 0;
}
static int
builtin_exit(int argc, char *argv[])
{
if (argc != 1) {
fprintf(stderr, "%s: too many arguments\n", argv[0]);
return 1;
}
exit(0);
}
static void*
xmalloc(size_t sz)
{
void *p;
p = calloc(1, sz);
if (!p)
exit(3);
return p;
}
static void*
xrealloc(void *ptr, size_t sz)
{
void *p;
if (!ptr) return xmalloc(sz);
p = realloc(ptr, sz);
if (!p)
exit(3);
return p;
}