rust-shell - 課題
概要
Rustでシンプルなシェルを実装し、プロセス管理とI/Oリダイレクションを学ぶ課題です。
マンダトリーパート
必須要件
- コマンド実行
- 組み込みコマンド
cd: ディレクトリ変更
- pwd: カレントディレクトリ表示
- echo: 文字列出力
- exit: シェル終了
- env: 環境変数表示- リダイレクション
>: 出力リダイレクト
- <: 入力リダイレクト
- >>: 追記リダイレクト- パイプ
|: コマンド間のパイプインターフェース
pub struct Shell {
cwd: PathBuf,
env: HashMap<String, String>,
}
impl Shell {
pub fn new() -> Self;
pub fn run(&mut self) -> Result<(), ShellError>;
fn parse_line(&self, line: &str) -> Vec<Command>;
fn execute(&mut self, commands: Vec<Command>) -> Result<i32, ShellError>;
}
pub struct Command {
pub program: String,
pub args: Vec<String>,
pub stdin: Option<Redirect>,
pub stdout: Option<Redirect>,
}
pub enum Redirect {
File(PathBuf),
Append(PathBuf),
Pipe,
}
使用例
$ ./minishell
minishell> ls -la
minishell> echo "Hello, World!"
minishell> cat file.txt | grep pattern
minishell> ls > output.txt
minishell> cd /tmp
minishell> exit
ボーナスパート
ボーナス1: ジョブ制御
- バックグラウンド実行(
&) jobs,fg,bgコマンド
ボーナス2: シグナル処理
- Ctrl+C, Ctrl+Z の適切な処理
ボーナス3: ヒストリー
- コマンド履歴の保存と参照
src/main.rs- エントリーポイントsrc/shell.rs- シェルコアsrc/parser.rs- コマンドパーサーsrc/executor.rs- コマンド実行Cargo.tomlnixクレートは使用可能- bash/zsh 互換性は不要