在 part 1 中,咱们已经建立了一个主要的 shell 循环、切分了的命令输入,以及经过 fork
和 exec
执行命令。在这部分,咱们将会解决剩下的问题。首先,cd test_dir2
命令没法修改咱们的当前目录。其次,咱们仍没法优雅地从 shell 中退出。python
“cd test_dir2 没法修改咱们的当前目录” 这句话是对的,但在某种意义上也是错的。在执行完该命令以后,咱们仍然处在同一目录,从这个意义上讲,它是对的。然而,目录实际上已经被修改,只不过它是在子进程中被修改。linux
还记得咱们 fork 了一个子进程,而后执行命令,执行命令的过程没有发生在父进程上。结果是咱们只是改变了子进程的当前目录,而不是父进程的目录。git
而后子进程退出,而父进程在原封不动的目录下继续运行。github
所以,这类与 shell 本身相关的命令必须是内置命令。它必须在 shell 进程中执行而没有分叉(forking)。shell
让咱们从 cd
命令开始。app
咱们首先建立一个 builtins
目录。每个内置命令都会被放进这个目录中。ide
yosh_project |-- yosh |-- builtins | |-- __init__.py | |-- cd.py |-- __init__.py |-- shell.py
在 cd.py
中,咱们经过使用系统调用 os.chdir
实现本身的 cd
命令。函数
import os from yosh.constants import * def cd(args): os.chdir(args[0]) return SHELL_STATUS_RUN
注意,咱们会从内置函数返回 shell 的运行状态。因此,为了可以在项目中继续使用常量,咱们将它们移至 yosh/constants.py
。oop
yosh_project |-- yosh |-- builtins | |-- __init__.py | |-- cd.py |-- __init__.py |-- constants.py |-- shell.py
在 constants.py
中,咱们将状态常量都放在这里。
SHELL_STATUS_STOP = 0 SHELL_STATUS_RUN = 1
如今,咱们的内置 cd
已经准备好了。让咱们修改 shell.py
来处理这些内置函数。
... # Import constants from yosh.constants import * # Hash map to store built-in function name and reference as key and value built_in_cmds = {} def tokenize(string): return shlex.split(string) def execute(cmd_tokens): # Extract command name and arguments from tokens cmd_name = cmd_tokens[0] cmd_args = cmd_tokens[1:] # If the command is a built-in command, invoke its function with arguments if cmd_name in built_in_cmds: return built_in_cmds[cmd_name](cmd_args) ...
咱们使用一个 python 字典变量 built_in_cmds
做为哈希映射(hash map),以存储咱们的内置函数。咱们在 execute
函数中提取命令的名字和参数。若是该命令在咱们的哈希映射中,则调用对应的内置函数。
(提示:built_in_cmds[cmd_name]
返回能直接使用参数调用的函数引用的。)
咱们差很少准备好使用内置的 cd
函数了。最后一步是将 cd
函数添加到 built_in_cmds
映射中。
... # Import all built-in function references from yosh.builtins import * ... # Register a built-in function to built-in command hash map def register_command(name, func): built_in_cmds[name] = func # Register all built-in commands here def init(): register_command("cd", cd) def main(): # Init shell before starting the main loop init() shell_loop()
咱们定义了 register_command
函数,以添加一个内置函数到咱们内置的命令哈希映射。接着,咱们定义 init
函数而且在这里注册内置的 cd
函数。
注意这行 register_command("cd", cd)
。第一个参数为命令的名字。第二个参数为一个函数引用。为了可以让第二个参数 cd
引用到 yosh/builtins/cd.py
中的 cd
函数引用,咱们必须将如下这行代码放在 yosh/builtins/__init__.py
文件中。
from yosh.builtins.cd import *
所以,在 yosh/shell.py
中,当咱们从 yosh.builtins
导入 *
时,咱们能够获得已经经过 yosh.builtins
导入的 cd
函数引用。
咱们已经准备好了代码。让咱们尝试在 yosh
同级目录下以模块形式运行咱们的 shell,python -m yosh.shell
。
如今,cd
命令能够正确修改咱们的 shell 目录了,同时非内置命令仍然能够工做。很是好!
最后一块终于来了:优雅地退出。
咱们须要一个能够修改 shell 状态为 SHELL_STATUS_STOP
的函数。这样,shell 循环能够天然地结束,shell 将到达终点而退出。
和 cd
同样,若是咱们在子进程中 fork 和执行 exit
函数,其对父进程是不起做用的。所以,exit
函数须要成为一个 shell 内置函数。
让咱们从这开始:在 builtins
目录下建立一个名为 exit.py
的新文件。
yosh_project |-- yosh |-- builtins | |-- __init__.py | |-- cd.py | |-- exit.py |-- __init__.py |-- constants.py |-- shell.py
exit.py
定义了一个 exit
函数,该函数仅仅返回一个能够退出主循环的状态。
from yosh.constants import * def exit(args): return SHELL_STATUS_STOP
而后,咱们导入位于 yosh/builtins/__init__.py
文件的 exit
函数引用。
from yosh.builtins.cd import * from yosh.builtins.exit import *
最后,咱们在 shell.py
中的 init()
函数注册 exit
命令。
... # Register all built-in commands here def init(): register_command("cd", cd) register_command("exit", exit) ...
到此为止!
尝试执行 python -m yosh.shell
。如今你能够输入 exit
优雅地退出程序了。
我但愿你能像我同样享受建立 yosh
(your own shell)的过程。但个人 yosh
版本仍处于早期阶段。我没有处理一些会使 shell 崩溃的极端情况。还有不少我没有覆盖的内置命令。为了提升性能,一些非内置命令也能够实现为内置命令(避免新进程建立时间)。同时,大量的功能尚未实现(请看 公共特性 和 不一样特性)
我已经在 github.com/supasate/yosh 中提供了源代码。请随意 fork 和尝试。
如今该是建立你真正本身拥有的 Shell 的时候了。
Happy Coding!
via: https://hackercollider.com/articles/2016/07/06/create-your-own-shell-in-python-part-2/