MIT 6.828 - 2. Lab 02: Simple xv6 shell

Tags: MIT 6.828

实验总结

  1. 本次实验用时约两个小时,使用了大量恶臭代码。

其他遇到的问题包括:

  1. 使用 dup 重定向 stdin/stdout 之后程序变得难以调试,最佳实践是使用 stderr 进行调试。

测试结果:

1
2
3
4
5
6
7
8
9
10
11
$ make grade
simple echo: OK
simple grep: OK
two commands: OK
output redirection: OK
input redirection: OK
both redirections: OK
simple pipe: OK
pipe and redirects: OK
lots of commands: OK
Score: 100/100

0. 实验准备

实验指导连接

上来直接:

1
2
$ cd xv6-riscv-fall19
$ git checkout sh

1. 代码实现

部分参考了 user/sh.c 的代码,但 sh.c 构建了 AST,接着在 AST 上执行整个流程,我使用字符串处理糊弄过关。

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
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
#include "kernel/fcntl.h"

inline int print(int fd, char *str)
{
return write(fd, str, strlen(str));
}

// [in-place]
// replace the left side "|" with "\0"
// then return the rest of the string or NULL
char *simple_tok(char *p, char d)
{
while (*p != '\0' && *p != d)
p++;
if (*p == '\0')
return 0;
*p = '\0';
return p + 1;
}


// [in-place]
// trim spaces on both side
char *trim(char *c)
{
char *e = c;
while (*e)
e++;
while (*c == ' ')
*(c++) = '\0';
while (*(--e) == ' ')
;
*(e + 1) = '\0';
return c;
}

void redirect(int k, int pd[])
{
close(k);
dup(pd[k]);
close(pd[0]);
close(pd[1]);
}

char cmd_buf[1024];
char *a, *n;

void handle(char *cmd)
{
char buf[32][32];
char *pass[32];
int argc = 0;

cmd = trim(cmd);
// fprintf(2, "cmd: %s\n", cmd);

for (int i = 0; i < 32; i++)
pass[i] = buf[i];

char *c = buf[argc];
int input_pos = 0, output_pos = 0;
for (char *p = cmd; *p; p++)
{
if (*p == ' ' || *p == '\n')
{
*c = '\0';
argc++;
c = buf[argc];
}
else {
if(*p == '<') {
input_pos = argc + 1;
} if(*p == '>') {
output_pos = argc + 1;
}
*c++ = *p;
}
}
*c = '\0';
argc++;
pass[argc] = 0;

// fprintf(2, "inpos: %d, outpos: %d\n", input_pos, output_pos);

if(input_pos) {
close(0);
open(pass[input_pos], O_RDONLY);
}

if(output_pos) {
close(1);
open(pass[output_pos], O_WRONLY | O_CREATE);
}

char *pass2[32];
int argc2 = 0;
for(int pos = 0; pos < argc; pos++) {
if(pos == input_pos - 1) pos += 2;
if(pos == output_pos - 1) pos += 2;
pass2[argc2++] = pass[pos];
}
pass2[argc2] = 0;

if (fork())
{
wait(0);
}
else
{
exec(pass2[0], pass2);
}
}

void handle_cmd()
{
if (a)
{
int pd[2];
pipe(pd);
// int parent_pid = getpid();
// fprintf(2, "pid: %d, cmd: %s\n", parent_pid, a);

if(!fork()){
// fprintf(2, "%d -> %d source\n", parent_pid, getpid());
if(n) redirect(1, pd);
handle(a);
} else if(!fork()) {
// fprintf(2, "%d -> %d sink\n", parent_pid, getpid());
if(n) {
redirect(0, pd);
a = n;
n = simple_tok(a, '|');
handle_cmd();
}
}

close(pd[0]);
close(pd[1]);
wait(0);
wait(0);
}

exit(0);
}

// a simple shell
int main(int argc, char *argv[])
{
while (1)
{
print(1, "@ ");
memset(cmd_buf, 0, 1024);
gets(cmd_buf, 1024);

if (cmd_buf[0] == 0) // EOF
exit(0);

*strchr(cmd_buf, '\n') = '\0';

if (fork())
{
wait(0);
}
else
{
a = cmd_buf;
n = simple_tok(a, '|');
handle_cmd();
}
}

exit(0);
}


知识共享许可协议 2000 - 2099 MonKey's Blog | 自豪地采用 Hexo + Pandoc + KaTeX + Highlight.js