OS-挑战性任务

OWPETER Lv4

info 以下为shell任务的试题

SHELL 挑战性任务

任务要求

本任务需要你基于MOS LAB6中的Shell进行增强,完成任务内容一节中的要求,提交实现代码以及实现报告并通过自动化评测,即可拿到该挑战性任务对应的分数。代码与报告提交通道将会在后续分别发布。


任务内容

支持相对路径

MOS 中现有的文件系统操作并不支持相对路径,对于一切路径都从根目录开始查找,因此在 shell 命令中也需要用绝对路径指代文件,这为命令的描述带来了不便。你需要为每个进程维护工作目录这一状态,实现相关内建指令,并为其他与路径相关的指令提供路径支持。

你可能需要了解以下术语:

  • 工作目录:进程当前所在的目录,用于解析相对路径。
  • 绝对路径:以 / 开头的路径,从根目录开始。
  • 相对路径:不以 / 开头的路径,相对于当前工作目录解析,可能包含以下特殊符号。
  • 特殊符号: .表示当前路径, ..表示上一级路径

你需要实现以下功能:

内建指令cd

输入 行为 输出 返回值
cd 切换工作目录到 / 0
cd <abspath> 若绝对路径 <abspath> 存在且为目录,切换到该目录 0
cd <relpath> 根据当前工作路径,对相对路径<relpath>,若存在且为目录,切换到该目录 0
cd <noexist_path> 路径不存在 cd: The directory '原始输入' does not exist\n 1
cd <filepath> 路径存在但不是目录 cd: '原始输入' is not a directory\n 1
cd <arg1>..<argn> 输入多于 1 个参数 Too many args for cd command\n 1
cd . 解析为当前目录 /dir1/dir2,无变化 0
cd .. 解析为 /dir1,若存在且为目录,切换到该目录 0

内建指令pwd

输入 行为 输出 返回值
pwd 输出当前工作目录 /dir1/dir2\n 0
pwd arg1 ... argn 参数数量错误 pwd: expected 0 arguments; got n\n 2

注意事项

父进程能够将工作路径传递给子进程。

环境变量管理

MOS 中的Shell目前并不支持环境变量,你需要在shell中增加对环境变量的支持。

规定环境变量在命令中以$开头,名称与C语言变量命名要求,且长度不超过16,环境变量的值长度同样不超过16。环境变量可分为局部变量与非局部变量,仅非局部变量可传入子Shell中,并且只有非只读变量可被修改。

你需要实现以下内容:

  1. 支持 内建指令declare [-xr] [NAME [=VALUE]] ,其中:

    • -x 表示变量 NAME 为环境变量,否则为局部变量。
      • 环境变量对子 shell 可见,也就是说在 shell 中输入 sh.b 启动一个子 shell 后,可以读取并修改 NAME 的值,即支持环境变量的继承。
      • 局部变量对子 shell 不可见,也就是说在 shell 中输入 sh.b 启动一个子 shell 后,没有该局部变量。
    • -r 表示将变量 NAME 设为只读。只读变量不能被 declare 重新赋值或被 unset 删除。
    • 如果变量 NAME 不存在,需要创建该环境变量;如果变量 NAME 存在,将该变量赋值为 VALUE
    • 其中 VALUE 为可选参数,缺省时将该变量赋值为空字符串即可。
    • 如果没有 [-xr][NAME [=VALUE]] 部分,即只输入 declare,则输出当前 shell 的所有变量,包括局部变量和环境变量。
  2. 支持内建指令 unset NAME 命令,若变量 NAME 不是只读变量,则删除变量 NAME

  3. 支持在命令中展开变量的值,如使用 echo.b $NAME 打印变量 NAME 的值。

注意事项

当执行declare指令时,需要以<var>=<val>(<var>为环境变量名,<val>为对应的值)输出当前Shell中的所有环境变量,评测不会对输出顺序进行测试。子Shell对环境变量的修改不会影响父Shell,且上述指令不能正确执行时返回非零值。

输入指令优化

指令自由输入

现有的 shell 不支持在输入命令时移动光标。你需要实现:键入命令时,可以使用 <Left><Right> 移动光标位置,并可以在当前光标位置进行字符的增加与删除。要求每次在不同位置键入后,可以完整回显修改后的命令,并且键入回车后可以正常运行修改后的命令。

不带 .b 后缀指令

你需要实现不带 .b 后缀的指令,但仍需兼容带有 .b 后缀的指令,如 lsls.b 都应能够正确列出当前目录下的文件。

快捷键

你需要在Shell中实现以下快捷键:

快捷键 行为
left-arrow 光标尝试向左移动,如果可以移动则移动
right-arrow 光标尝试向右移动,如果可以移动则移动
backspace 删除光标左侧 1 个字符并将光标向左移动 1 列;若已在行首则无动作
Ctrl-E 光标跳至最后
Ctrl-A 光标跳至最前
Ctrl-K 删除从当前光标处到最后的文本
Ctrl-U 删除从最开始到光标前的文本
Ctrl-W 向左删除最近一个 word:先越过空白(如果有),再删除连续非空白字符

!!! 上述行为与Bash行为一致,如有模糊之处可直接参考Bash。

历史指令

你需要实现 shell 中保存历史指令的功能,可以通过 <Up><Down> 选择所保存的指令并执行。你需要将历史指令保存到根目录的 .mos_history 文件中(一条指令一行),为了评测的方便,我们设定 $HISTFILESIZE=20(bash 中默认为 500),即在 .mos_history 中至多保存最近的 20 条指令。你还需要支持通过 history 命令输出 .mos_history 文件中的内容。

注:在 bash 中,history 为 shell built-in command,我们规定需要将 history 实现为 built-in command。

你需要将当前执行的指令先存入 .mos_history 中,例如:

1
2
3
echo `ls | cat`echo meow # comment
history
history | cat

当历史指令为空时,依次执行上述四条指令后,后两条指令会分别输出

1
2
3
echo `ls | cat`
echo meow # comment
history

1
2
3
4
echo `ls | cat`
echo meow # comment
history
history | cat

使用 <Up> 能够切换到上一条指令(如果上一条指令存在),使用 <Down> 能够切换到下一条指令(如果下一条指令存在)。能够选择的指令范围为:用户当前输入的指令与 .mos_history 文件中保存的所有指令。例如在执行了上述四条指令后,用户输入了 echo,此时 <Up> 应该将指令切换至 history | cat,再次进行三次 <Up> 后切换至 echo \ls | cat`,此时再次 <Up>应保留在该指令(因为已经不存在上一条指令);再进行四次<Down>后将切换回echo,此时再次 <Down>` 应保留在该指令(因为不存在下一条指令)。

注意事项

历史指令输入时光标的处理参考Bash,当切换到新的指令时,光标会自动恢复到输入最末端。

实现注释功能

你需要使用 # 实现注释功能,例如 ls | cat # this is a comment meowls | cat 会被正确执行,而后面的注释则会被抛弃。

实现反引号

你需要使用反引号实现指令替换。你需要将反引号内指令执行的所有标准输出代替原有指令中的反引号内容。例如:

1
echo `ls | cat | cat | cat`

实现一行多指令

你需要支持使用;将指令隔开,并按顺序执行,比如:

1
ls;ls;ls;ls

指令条件执行

你需要实现 Linux shell 中的 &&||。对于 command1 && command2command2 被执行当且仅当 command1 返回 0;对于 command1 || command2command2 被执行当且仅当 command1 返回非 0 值。

注: 评测中保证不出现括号。并且需要注意的是,在 bash 中 &&|| 的优先级相同,按照从左到右的顺序求值。

例如 cmd1 || cmd2 && cmd3,若 cmd1 返回 0,则 cmd1 执行后 cmd2 不会被执行,cmd3 会被执行;若 cmd1 返回非 0 且 cmd2 返回非 0,则 cmd3 将不会被执行。

提示:你可能需要修改 MOS 中对用户进程 exit 的实现,使其能够返回值。

更多指令

你需要实现 touchmkdirrm 指令以及内建指令exit,只需要考虑如下情形:

  • touch:
    • touch <file>:创建空文件 file,若文件存在则放弃创建,正常退出无输出。若创建文件的父目录不存在则输出 touch: cannot touch '<file>': No such file or directory。例如 touch nonexistent/dir/a.txt 时应输出 touch: cannot touch 'nonexistent/dir/a.txt': No such file or directory
  • mkdir:
    • mkdir <dir>:若目录已存在则输出 mkdir: cannot create directory '<dir>': File exists,若创建目录的父目录不存在则输出 mkdir: cannot create directory '<dir>': No such file or directory,否则正常创建目录。
    • mkdir -p <dir>:当使用 -p 选项时忽略错误,若目录已存在则直接退出,若创建目录的父目录不存在则递归创建目录。
  • rm:
    • rm <file>:若文件存在则删除 <file>,否则输出 rm: cannot remove '<file>': No such file or directory
    • rm <dir>:命令行输出: rm: cannot remove '<dir>': Is a directory
    • rm -r <dir>|<file>:若文件或文件夹存在则删除,否则输出 rm: cannot remove '<dir>|<file>': No such file or directory
    • rm -rf <dir>|<file>:如果对应文件或文件夹存在则删除,否则直接退出。
  • (内建指令)exit:执行后退出当前shell

注:对于rm,mkdir,touch指令,若成功执行则返回0,否则返回非零值即可。

追加重定向

你需要实现 shell 中 >> 追加重定向的功能,例如:

1
2
ls >> file1
ls >> file1

最后文件 file1 中将会有两次 ls 指令的输出。


实现提示

​ Shell是一种命令解释器,对输入指令进行解析并执行。现有MOS实现的Shell实现的较为简陋,如果在其基础上尝试实现上述内容可能复杂度较高,可以参考sh,bash等工业界shell实现原理进行重新实现,以下是一个可行的实现方案:

其中:

  1. 输入指令层
  2. 输入缓冲管理:负责原始命令的读取,支持行编辑、历史记录等功能。
  3. 屏幕输出管理:负责屏幕输出内容刷新。
  4. 词法分析
  5. 将读入的原始指令划分为最小语法单元Token,并识别特殊标识符(重定向,管道,注释,标识符等)
  6. 语法分析
  7. 根据Token序列构建语法树AST并识别命令结构
  8. AST节点执行
  9. 遍历AST,根据节点类型在当前进程处执行并更新状态

语法分析可以使用递归下降法进行解析,EBNF文法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 指令输入
line ::= list
# 列表
list ::= and_or ( ( ";" ) and_or )*
# 逻辑与/或: 支持 && 和 ||
and_or ::= pipeline ( ( "&&" | "||" ) pipeline )*
# 管道: 支持 |
pipeline ::= command ( "|" command )*
# 简单命令:WORD 串 + 重定向
command ::= WORD ( WORD | redirect )*
# 重定向
redirect ::= "<" WORD | ">" WORD | ">>" WORD
# 词法单元
TOKEN ::= WORD (* 普通字或变量展开后的字符串 *) | T_PIPE (* "|" *) | T_SEMI (* ";" *) | T_AMP (* "&" *) | T_AND (* "&&" *) | T_OR (* "||" *) | T_REDIR_IN (* "<" *) | T_REDIR_OUT (* ">" *) | T_REDIR_APP (* ">>" *) | T_EOL (* 换行 或是 # *) | T_EOF (* 输入结束 *)

AST节点指令的执行参考Shell Command Language


测试

评测通过make clean && MOS_PROFILE=release make all && make run进行测试。

提交代码时请确保代码可以执行上述指令。

相对路径+新增指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
pwd
/mkdir.b /dir1
cd /dir1
/mkdir.b .././dir1/dir2
cd ./dir2
pwd
cd ..
pwd
/touch.b ../file1
/ls.b /dir1 > ../file1
/cat.b ../file1
/rm.b ../file1
/rm.b ../file1
/rm.b -rf ../file1
/rm.b ./dir2
/rm.b -r ./dir2
cd ./dir2
/mkdir.b ./dir2/dir3
/mkdir.b -p ./dir2/dir3
cd ./dir2/dir3
pwd
/touch.b file1
/touch.b dir4/file1
/halt.b

输出为:

1
2
3
4
5
6
7
8
9
10
/
/dir1/dir2
/dir1
/dir1/dir2
rm: cannot remove '../file1': No such file or directory
rm: cannot remove './dir2': Is a directory
cd: The directory './dir2' does not exist
mkdir: cannot create directory './dir2/dir3': No such file or directory
/dir1/dir2/dir3
touch: cannot touch 'dir4/file1': No such file or directory

环境变量+注释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/mkdir.b -p dir1/dir2
/ls.b /dir1
cd ./dir1
pwd
cd ../ls.b /dir1
declare -x ls_alias=/ls.b
$ls_alias /dir1
/echo.b $ls_alias
declare echo_alias=/echo.b
declare
/sh.b
echo $echo_alias
echo $ls_alias
declare -x ls_alias=/echo.b
$ls_alias $ls_alias
exit
$echo_alias $ls_alias
declare var_dir1=/dir1
cd /
cd $var_dir1/dir2
pwd
/halt.b

输出应为:

1
2
3
4
5
6
7
8
9
10
11
12
/dir1/dir2
/dir1
/dir1/dir2
/dir1/dir2
/ls.b
ls_alias=/ls.b
echo_alias=/echo.b

/ls.b
/echo.b
/ls.b
/dir1/dir2

指令输入优化+历史指令

1
2
3
4
5
6
7
8
9
# 由于需要展示输入快捷键,这里仅作说明
输入序列为:</> <m> <k> <d> <i> <r> <space> <-> <p> <space> </> <d> <i> <r> <1> </> <d> <i> <r> <2>
输入序列为:<c> <d> <space> </> <d> <i> <r> <1>
输入序列为:<p> <w> <d> <left-arrow> <d> <up-arrow> <down-arrow> <backspace>
输入序列为:</> <l> <s> <up-arrow> <up-arrow> <left-arrow> <s> <backspace>
输入序列为:<h> <i> <s> <t> <o> <r> <y>
输入序列为:<a> <space> <b> <c> <ctrl-w> <backspace> <backspace> <c> <h> <o> <ctrl-a> </> <e> <right-arrow> <ctrl-e> <space> <l> <s> <right-arrow> <right-arrow>
输入序列为:<h> <i> <s> <t> <o> <r> <y>
输入序列为:</> <h> <a> <l> <t>

输出为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/dir1
/mkdir -p /dir1/dir2
cd /dir1
pwd
cd /dir1
history
ls
/mkdir -p /dir1/dir2
cd /dir1
pwd
cd /dir1
history
/echo ls
history

追加重定向+条件执行+反引号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/mkdir -p /dir1/dir2
cd /dir1
pwd
/echo `/ls /dir1 | /cat`
/mkdir `/ls /dir1 | /cat`
/mkdir `pwd`
/echo `/ls /dir1 | /cat` > file1
/echo `/ls /dir1 | /cat` >> file1
/cat file1
/mkdir `pwd` && /ls /dir1
/ls /dir1 && /ls /dir1
/mkdir `pwd` || /mkdir -p /dir3
/ls /dir1 || /ls /dir1
cd /dir3
pwd
/halt

输出应为:

1
2
3
4
5
6
7
8
9
10
11
/dir1
/dir1/dir2
mkdir: cannot create directory '/dir1/dir2': File exists
mkdir: cannot create directory '/dir1': File exists
/dir1/dir2 /dir1/file1
mkdir: cannot create directory '/dir1': File exists
/dir1/dir2 /dir1/file1
/dir1/dir2 /dir1/file1
mkdir: cannot create directory '/dir1': File exists
/dir1/dir2 /dir1/file1
/dir3

info 以下为笔者的实现

输入指令优化

不带.b后缀指令

就是简单的字符串处理,如果末尾没有.b,那么加上.b

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// lab6-ch
int len = strlen(prog);
int needs_suffix = 1;

// Check if the string already ends with ".b"
if (len >= 2) {
if (prog[len - 2] == '.' && prog[len - 1] == 'b') {
needs_suffix = 0;
}
}

char result_str[256];
if (needs_suffix) {
strcpy(result_str, prog);
result_str[len] = '.';
result_str[len + 1] = 'b';
result_str[len + 2] = '\0'; // Add the new null terminator
} else {
strcpy(result_str, prog); // Copy the original string
}

if ((fd = open(result_str, O_RDONLY)) < 0) {
return fd;
}

注释功能

也是字符串处理,将#后面的内容全部丢弃

1
2
3
4
5
6
// lab6-ch
for (int i = 0; i < strlen(buf); ++i) {
if (buf[i] == '#') {
buf[i] = '\0';
}
}

快捷键

为了实现快捷键,我们需要对终端的显示逻辑进行一定修改。首先利用display函数刷新并重绘当前命令。之后使用readline函数,在while循环中不断读取用户的键盘输入行为,根据用户的键盘行为设置指针或缓冲区内容,并将处理后的缓冲区使用display函数打印出来。

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
if (c == '\x1b') { // ESC - potential arrow key
char seq[3]; // Increased size to potentially handle more complex sequences
if (read(0, &seq[0], 1) != 1) continue;
if (seq[0] == '[') {
if (read(0, &seq[1], 1) != 1) continue;
if (seq[1] == 'A') { // Up arrow
if (current_history_index == history_count) {
// User is on the current input line, capture it before moving up
strcpy(current_command_capture, buf);
}
if (current_history_index > 0) {
current_history_index--;
strcpy(buf, history[current_history_index]);
line_len = strlen(buf);
cursor_pos = line_len; // Move cursor to end of historical command
}
} else if (seq[1] == 'B') { // Down arrow
if (current_history_index < history_count - 1) {
current_history_index++;
strcpy(buf, history[current_history_index]);
line_len = strlen(buf);
cursor_pos = line_len;
} else if (current_history_index == history_count - 1) {
// User was on the last historical command, moving to current input line
current_history_index++; // Now current_history_index == history_count
strcpy(buf, current_command_capture); // Restore captured current line
line_len = strlen(buf);
cursor_pos = line_len;
}
} else if (seq[1] == 'D') { // Left Arrow (from existing code)
if (cursor_pos > 0) {
cursor_pos--;
}
} else if (seq[1] == 'C') { // Right Arrow (from existing code)
if (cursor_pos < line_len) {
cursor_pos++;
}
}
// Potentially read seq[2] if needed for other escape sequences
}
} else if (c == 1) { // Ctrl-A (SOH): Move cursor to beginning of line
if (cursor_pos > 0) {
cursor_pos = 0;
}
} else if (c == 5) { // Ctrl-E (ENQ): Move cursor to end of line
if (cursor_pos < line_len) {
cursor_pos = line_len;
}
} else if (c == 11) { // Ctrl-K (VT): Delete from cursor to end of line
if (cursor_pos < line_len) {
line_len = cursor_pos;
buf[line_len] = '\0';
if (current_history_index == history_count) strcpy(current_command_capture, buf);
}
} else if (c == 21) { // Ctrl-U (NAK): Delete from start to cursor
if (cursor_pos > 0) {
memmove(buf, buf + cursor_pos, line_len - cursor_pos + 1); // +1 for null terminator
line_len -= cursor_pos;
cursor_pos = 0;
if (current_history_index == history_count) strcpy(current_command_capture, buf);
}
} else if (c == 23) { // Ctrl-W (ETB): Delete word before cursor
if (cursor_pos > 0) {
int end_of_word_to_delete = cursor_pos;
int start_of_word_to_delete = cursor_pos;

while (start_of_word_to_delete > 0 && buf[start_of_word_to_delete - 1] == ' ') {
start_of_word_to_delete--;
}
while (start_of_word_to_delete > 0 && buf[start_of_word_to_delete - 1] != ' ') {
start_of_word_to_delete--;
}

if (end_of_word_to_delete > start_of_word_to_delete) {
int num_chars_to_delete = end_of_word_to_delete - start_of_word_to_delete;
memmove(buf + start_of_word_to_delete, buf + end_of_word_to_delete, line_len - end_of_word_to_delete + 1); // +1 for null
line_len -= num_chars_to_delete;
cursor_pos = start_of_word_to_delete;
if (current_history_index == history_count) strcpy(current_command_capture, buf);
}
}
} else if (c == '\b' || c == 0x7f) { // Backspace or Delete key
if (cursor_pos > 0) {
memmove(buf + cursor_pos - 1, buf + cursor_pos, line_len - cursor_pos + 1); // +1 for null
line_len--;
cursor_pos--;
if (current_history_index == history_count) strcpy(current_command_capture, buf);
}
} else if (c == '\r' || c == '\n') { // Enter key
buf[line_len] = '\0';
printf("\n"); // Move to next line in console after command is entered
return; // Exit readline
} else { // other char
// ...
}

如果读取到的是上箭头或下箭头,则读取历史内容缓冲区,并将指定历史内容复制到缓冲区中打印出来。

历史指令

在每个shell初始化阶段需要调用load_mosh_history函数来初始化历史命令缓冲区并新建.mos_history文件。通过快捷键获得历史指令的操作在上文已经提到,history内建指令的实现在下文会提到。

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
void load_mosh_history() {
int fd;
char read_buf[1024 * 4]; // Read in chunks
char line_buf[1024];
int n, i, line_pos = 0;
history_count = 0;

fd = open(HISTORY_FILE, O_RDONLY);
if (fd < 0) {
// File doesn't exist or cannot be opened, start with empty history
history_loaded_from_file = 1; // Mark as "loaded" to prevent re-attempts
return;
}

while ((n = read(fd, read_buf, sizeof(read_buf) - 1)) > 0 && history_count < MAX_HISTORY) {
read_buf[n] = '\0'; // Null-terminate the chunk
for (i = 0; i < n && history_count < MAX_HISTORY; i++) {
if (read_buf[i] == '\n') {
if (line_pos > 0) {
line_buf[line_pos] = '\0';
strcpy(history[history_count++], line_buf);
line_pos = 0;
}
} else {
if (line_pos < sizeof(line_buf) - 1) {
line_buf[line_pos++] = read_buf[i];
}
}
}
}
// Add the last line if the file doesn't end with a newline
if (line_pos > 0 && history_count < MAX_HISTORY) {
line_buf[line_pos] = '\0';
strcpy(history[history_count++], line_buf);
}

close(fd);
history_loaded_from_file = 1;
}

反引号

runcmd中,parsecmd前,调用substitute_backticks函数,解析并执行反引号内的指令。

substitute_backticks 的核心处理逻辑如下:

  1. 一旦找到反引号对,它会提取其中的命令,之后fork一个子进程来执行这个命令,并利用pipe捕获该命令的标准输出。

  2. 父进程等待子进程执行完毕,然后从管道读取子进程的输出结果。它会移除输出结果末尾的一个换行符,然后将处理后的结果拼接到一个临时的缓冲区中。在拼接前,函数会先把反引号之前的部分字符串复制到临时缓冲区。

  3. 函数重复上述过程,直到处理完原字符串中所有的反引号。最后,用构建好的临时缓冲区内容覆盖掉原始的 command_string,从而完成替换。

指令条件执行

指令条件执行的实现依赖于读取各指令的返回值。关于这部分内容可参看exit指令的实现部分。

指令条件执行的核心逻辑位于parsecmd函数中:

  1. 当解析器遇到 && 时,我们会fork()一个子进程去执行&&左侧的command1。父进程则会调用 ipc_recv() 等待,直到子进程执行完毕并返回一个退出状态码。如果状态码为 0,那么就会继续解析并执行 && 右侧的 command2。如果状态码非 0,它会跳过 command2

  2. 当解析到||时,同样会 fork() 子进程执行 || 左侧的 command1,父进程等待结果。逻辑与 && 相反。只有当父进程收到的状态码为非 0(代表 command1 执行失败)时,它才会继续解析并执行 || 右侧的 command2。如果状态码为 0,则跳过 command2

内建指令

几乎所有的内建指令的第一步都是要通过系统调用syscall_get_cwd获取当前工作目录

  • cd

获得工作目录后,调用normalize_path函数,将用户输入的包含...的相对路径与工作目录结合,获得绝对路径。之后使用check_if_directory函数,检查该路径是一个目录而非文件,如果检查通过,那么使用syscall_set_cwd更新工作目录

我将每个进程的工作目录存储在env块中,syscall_get_cwdsyscall_set_cwd系统调用本质就是寻找该进程控制块,并更新或获得其env_cwd域。

  • pwd

直接通过syscall_get_cwd获取当前进程的工作目录即可

  • declareunset

为了存储用户定义的环境变量,我设计了一个EnvVar结构体来存储相关信息

1
2
3
4
5
6
7
struct EnvVar {
char name[ENV_NAME_LEN + 1];
char value[ENV_VALUE_LEN + 1];
u_char is_set; // 0 = not set, 1 = set (even if value is empty)
u_char is_exported; // 0 = local, 1 = exported
u_char is_readonly; // 0 = writable, 1 = readonly
};

之后设置一个专门的内存地址,并分配一页空间来存储当前进程的EnvVar结构体

1
2
3
4
#define ENVIRON_VA (0x60000000 - 2 * PDMAP) // Example VA, ensure it's page-aligned and user-accessible
#define MAX_ENV_VARS (PAGE_SIZE / sizeof(struct EnvVar))
#define ENV_NAME_LEN 16
#define ENV_VALUE_LEN 16

对于declare指令处理的核心逻辑如下:

  1. 解析以-为开头的参数,根据参数设置只读或全局的flag
  2. 如果没有解析到参数,那么遍历当前环境变量并打印
  3. 如果解析到参数,那么按照NAME=VALUE的格式解析变量名和值,然后检查变量是否存在,如果存在检查是否只读,若只读则报错,否则重新赋值;如果变量不存在则寻找一个可用空间创建新的EnvVar结构体,并按照之前设置的flag位对相应标志进行置位

unset指令的实现较为简单,只需要将通过find_variable函数找到变量对应的结构体,然后将其所有域置0即可

  • history

实现非常简单,直接读取历史指令缓冲区即可

  • exit

为了能够将指令的实现结果回传给父进程,我修改了exit函数,并新设置一个inner_exit函数,用来返回内建指令的返回值。他们的实现逻辑是相近的,都是利用syscall_ipc_try_send系统调用像父进程发送返回值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void exit(void) {
// After fs is ready (lab5), all our open files should be closed before dying.
#if !defined(LAB) || LAB >= 5
close_all();
#endif
debugf("[%08x] exit and send %d\n", env->env_id, ret);
syscall_ipc_try_send(env->env_parent_id, ret, 0, 0);
syscall_env_destroy(0);
user_panic("unreachable code");
}

void inner_exit(int ret) {
// After fs is ready (lab5), all our open files should be closed before dying.
#if !defined(LAB) || LAB >= 5
close_all();
#endif
syscall_ipc_try_send(env->env_parent_id, ret, 0, 0);
syscall_env_destroy(0);
user_panic("unreachable code");
}

相应的,父进程需要使用exit_status = ipc_recv(NULL, 0, 0);读取子进程的返回值。

对于exit指令来说,子进程向父进程发送特定返回值EXIT,当父进程读取到该返回值时,立即退出。

更多指令

首先需要注意的是,由于25年的shell实现了cd,导致工作目录会发生变化,因此所有非内建指令都需要根据工作目录和相对目录拼接出一个绝对目录,然后根据这个绝目录进行操作

  • mkdir

main 函数负责解析命令行参数,侦测 -p 选项并设定一个全局标志。

核心的 mkdir 函数根据此标志有两种行为:

  1. 标准模式:创建单个目录。若目录已存在或其父目录不存在,就会报错。
  2. -p 模式:递归创建路径中所有不存在的父目录。若目标目录已存在,则不会报错。

另外需要对文件系统的serve_open函数进行一定修改,使其支持创建文件夹:

1
2
3
4
5
6
7
8
9
if ((rq->req_omode & O_MKDIR) && (r = file_create(rq->req_path, &f)) < 0 &&
r != -E_FILE_EXISTS) {
ipc_send(envid, r, 0, 0);
return;
}
//...
if (rq->req_omode & O_MKDIR) {
f->f_type = FTYPE_DIR;
}
  • touch

这个指令的实现非常简单,就是直接调用open函数,如果返回值为错误码,那么报错并直接返回该错误码

  • rm
  1. 遍历命令行参数,查找 -r-rf
  2. 如果找到 -r,设置递归标志 flag_r。如果找到 -rf,同时设置递归标志 flag_r 和强制标志 flag_f
  3. 对每个非选项的参数(即文件或目录路径),调用 rm 函数。

rm函数的逻辑为

  1. 尝试打开路径,如果路径不存在:检查是否设置了强制标志 flag_f。如果设置了,则忽略该错误并返回;否则,报错“文件或目录不存在”。
  2. 如果路径存在,获取其状态信息。如果目标是目录(st.st_isdir 为真),但未设置递归标志 flag_r,则报错“是一个目录”。
  3. 如果通过以上所有检查,则调用 remove(path) 来删除该文件或目录。
  • Title: OS-挑战性任务
  • Author: OWPETER
  • Created at : 2025-06-29 17:15:21
  • Updated at : 2025-08-09 17:29:10
  • Link: https://owpeter.github.io/2025/06/29/OS/OS-挑战性任务/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments