Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xv6-public
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
问题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
银宸时代
OS Lab Group
奖励实验
xv6-public
提交
a759b8a4
提交
a759b8a4
8月 28, 2007
创建
作者:
rsc
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
formatting tweaks
上级
2868ca0f
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
66 行增加
和
55 行删除
+66
-55
sh.c
sh.c
+66
-55
没有找到文件。
sh.c
浏览文件 @
a759b8a4
...
@@ -49,19 +49,9 @@ struct backcmd {
...
@@ -49,19 +49,9 @@ struct backcmd {
struct
cmd
*
cmd
;
struct
cmd
*
cmd
;
};
};
struct
cmd
*
parsecmd
(
char
*
);
int
fork1
(
void
);
// Fork but panics on failure.
void
panic
(
char
*
);
void
panic
(
char
*
);
struct
cmd
*
parsecmd
(
char
*
);
int
fork1
(
void
)
{
int
pid
;
pid
=
fork
();
if
(
pid
==
-
1
)
panic
(
"fork"
);
return
pid
;
}
// Execute cmd. Never returns.
// Execute cmd. Never returns.
void
void
...
@@ -99,6 +89,14 @@ runcmd(struct cmd *cmd)
...
@@ -99,6 +89,14 @@ runcmd(struct cmd *cmd)
runcmd
(
rcmd
->
cmd
);
runcmd
(
rcmd
->
cmd
);
break
;
break
;
case
LIST
:
lcmd
=
(
struct
listcmd
*
)
cmd
;
if
(
fork1
()
==
0
)
runcmd
(
lcmd
->
left
);
wait
();
runcmd
(
lcmd
->
right
);
break
;
case
PIPE
:
case
PIPE
:
pcmd
=
(
struct
pipecmd
*
)
cmd
;
pcmd
=
(
struct
pipecmd
*
)
cmd
;
if
(
pipe
(
p
)
<
0
)
if
(
pipe
(
p
)
<
0
)
...
@@ -123,14 +121,6 @@ runcmd(struct cmd *cmd)
...
@@ -123,14 +121,6 @@ runcmd(struct cmd *cmd)
wait
();
wait
();
break
;
break
;
case
LIST
:
lcmd
=
(
struct
listcmd
*
)
cmd
;
if
(
fork1
()
==
0
)
runcmd
(
lcmd
->
left
);
wait
();
runcmd
(
lcmd
->
right
);
break
;
case
BACK
:
case
BACK
:
bcmd
=
(
struct
backcmd
*
)
cmd
;
bcmd
=
(
struct
backcmd
*
)
cmd
;
if
(
fork1
()
==
0
)
if
(
fork1
()
==
0
)
...
@@ -155,7 +145,17 @@ int
...
@@ -155,7 +145,17 @@ int
main
(
void
)
main
(
void
)
{
{
static
char
buf
[
100
];
static
char
buf
[
100
];
int
fd
;
// Assumes three file descriptors open.
while
((
fd
=
open
(
"console"
,
O_RDWR
))
>=
0
){
if
(
fd
>=
3
){
close
(
fd
);
break
;
}
}
// Read and run input commands.
while
(
getcmd
(
buf
,
sizeof
(
buf
))
>=
0
)
{
while
(
getcmd
(
buf
,
sizeof
(
buf
))
>=
0
)
{
if
(
fork1
()
==
0
)
if
(
fork1
()
==
0
)
runcmd
(
parsecmd
(
buf
));
runcmd
(
parsecmd
(
buf
));
...
@@ -171,6 +171,18 @@ panic(char *s)
...
@@ -171,6 +171,18 @@ panic(char *s)
exit
();
exit
();
}
}
int
fork1
(
void
)
{
int
pid
;
pid
=
fork
();
if
(
pid
==
-
1
)
panic
(
"fork"
);
return
pid
;
}
//PAGEBREAK!
// Constructors
// Constructors
struct
cmd
*
struct
cmd
*
...
@@ -237,25 +249,13 @@ backcmd(struct cmd *subcmd)
...
@@ -237,25 +249,13 @@ backcmd(struct cmd *subcmd)
cmd
->
cmd
=
subcmd
;
cmd
->
cmd
=
subcmd
;
return
(
struct
cmd
*
)
cmd
;
return
(
struct
cmd
*
)
cmd
;
}
}
//PAGEBREAK!
// Parsing
// Parsing
char
whitespace
[]
=
"
\t\r\n\v
"
;
char
whitespace
[]
=
"
\t\r\n\v
"
;
char
symbols
[]
=
"<|>&;()"
;
char
symbols
[]
=
"<|>&;()"
;
int
int
peek
(
char
**
ps
,
char
*
es
,
char
*
toks
)
{
char
*
s
;
s
=
*
ps
;
while
(
s
<
es
&&
strchr
(
whitespace
,
*
s
))
s
++
;
*
ps
=
s
;
return
*
s
&&
strchr
(
toks
,
*
s
);
}
int
gettoken
(
char
**
ps
,
char
*
es
,
char
**
q
,
char
**
eq
)
gettoken
(
char
**
ps
,
char
*
es
,
char
**
q
,
char
**
eq
)
{
{
char
*
s
;
char
*
s
;
...
@@ -300,12 +300,22 @@ gettoken(char **ps, char *es, char **q, char **eq)
...
@@ -300,12 +300,22 @@ gettoken(char **ps, char *es, char **q, char **eq)
return
ret
;
return
ret
;
}
}
void
nulterminate
(
struct
cmd
*
);
int
peek
(
char
**
ps
,
char
*
es
,
char
*
toks
)
{
char
*
s
;
s
=
*
ps
;
while
(
s
<
es
&&
strchr
(
whitespace
,
*
s
))
s
++
;
*
ps
=
s
;
return
*
s
&&
strchr
(
toks
,
*
s
);
}
struct
cmd
*
parseline
(
char
**
,
char
*
);
struct
cmd
*
parseline
(
char
**
,
char
*
);
struct
cmd
*
parsepipe
(
char
**
,
char
*
);
struct
cmd
*
parsepipe
(
char
**
,
char
*
);
struct
cmd
*
parseredirs
(
struct
cmd
*
,
char
**
,
char
*
);
struct
cmd
*
parseblock
(
char
**
,
char
*
);
struct
cmd
*
parseexec
(
char
**
,
char
*
);
struct
cmd
*
parseexec
(
char
**
,
char
*
);
struct
cmd
*
nulterminate
(
struct
cmd
*
);
struct
cmd
*
struct
cmd
*
parsecmd
(
char
*
s
)
parsecmd
(
char
*
s
)
...
@@ -355,22 +365,6 @@ parsepipe(char **ps, char *es)
...
@@ -355,22 +365,6 @@ parsepipe(char **ps, char *es)
}
}
struct
cmd
*
struct
cmd
*
parseblock
(
char
**
ps
,
char
*
es
)
{
struct
cmd
*
cmd
;
if
(
!
peek
(
ps
,
es
,
"("
))
panic
(
"parseblock"
);
gettoken
(
ps
,
es
,
0
,
0
);
cmd
=
parseline
(
ps
,
es
);
if
(
!
peek
(
ps
,
es
,
")"
))
panic
(
"syntax - missing )"
);
gettoken
(
ps
,
es
,
0
,
0
);
cmd
=
parseredirs
(
cmd
,
ps
,
es
);
return
cmd
;
}
struct
cmd
*
parseredirs
(
struct
cmd
*
cmd
,
char
**
ps
,
char
*
es
)
parseredirs
(
struct
cmd
*
cmd
,
char
**
ps
,
char
*
es
)
{
{
int
tok
;
int
tok
;
...
@@ -396,6 +390,22 @@ parseredirs(struct cmd *cmd, char **ps, char *es)
...
@@ -396,6 +390,22 @@ parseredirs(struct cmd *cmd, char **ps, char *es)
}
}
struct
cmd
*
struct
cmd
*
parseblock
(
char
**
ps
,
char
*
es
)
{
struct
cmd
*
cmd
;
if
(
!
peek
(
ps
,
es
,
"("
))
panic
(
"parseblock"
);
gettoken
(
ps
,
es
,
0
,
0
);
cmd
=
parseline
(
ps
,
es
);
if
(
!
peek
(
ps
,
es
,
")"
))
panic
(
"syntax - missing )"
);
gettoken
(
ps
,
es
,
0
,
0
);
cmd
=
parseredirs
(
cmd
,
ps
,
es
);
return
cmd
;
}
struct
cmd
*
parseexec
(
char
**
ps
,
char
*
es
)
parseexec
(
char
**
ps
,
char
*
es
)
{
{
char
*
q
,
*
eq
;
char
*
q
,
*
eq
;
...
@@ -429,7 +439,7 @@ parseexec(char **ps, char *es)
...
@@ -429,7 +439,7 @@ parseexec(char **ps, char *es)
}
}
// NUL-terminate all the counted strings.
// NUL-terminate all the counted strings.
void
struct
cmd
:
nulterminate
(
struct
cmd
*
cmd
)
nulterminate
(
struct
cmd
*
cmd
)
{
{
int
i
;
int
i
;
...
@@ -440,7 +450,7 @@ nulterminate(struct cmd *cmd)
...
@@ -440,7 +450,7 @@ nulterminate(struct cmd *cmd)
struct
redircmd
*
rcmd
;
struct
redircmd
*
rcmd
;
if
(
cmd
==
0
)
if
(
cmd
==
0
)
return
;
return
0
;
switch
(
cmd
->
type
){
switch
(
cmd
->
type
){
case
EXEC
:
case
EXEC
:
...
@@ -472,4 +482,5 @@ nulterminate(struct cmd *cmd)
...
@@ -472,4 +482,5 @@ nulterminate(struct cmd *cmd)
nulterminate
(
bcmd
->
cmd
);
nulterminate
(
bcmd
->
cmd
);
break
;
break
;
}
}
return
cmd
;
}
}
编写
预览
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论