在 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/