我正在努力创建自己的外壳。
我为用户输入创建了一个词法分析器和一个解析器(它们创建了一个二叉树)。因此,对于这样的命令:cat main.c | ls | wc。
我得到了这棵树:
"|"
/ \
/ \
/ \
"cat main.c" "|"
/ \
/ \
"ls" "wc"
所以我的树遍历函数(按顺序)是这样的:
inorder(root)
{
inorder(root->left);
//exec cmd and do redirection
inorder(root->right);
}
我的问题是当我在节点“ ls”或“ wc”上时,我不知道如何检查命令前后的管道。
任何的想法 ?
在您的分析树中,管道是节点,命令是叶子。管道必须同时具有左右分支。当您从管道左移时,您现在要使用的管道就是您要执行的命令的管道。当您向右走时,您所在的管道是destination命令的入管道。
因此,将输入和输出管道作为参数传递。它们指向NULL
该命令或|
节点之一是否没有重定向。
inorder(root, in, out)
{
if (root is cmd) {
execute(root, in, out);
} else {
// root is pipe
inorder(root->left, in, root);
redirect(in, out);
inorder(root->right, root, out);
}
}
从树的根部开始inorder(root, NULL, NULL)
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句