rust-shell - 課題

概要

Rustでシンプルなシェルを実装し、プロセス管理とI/Oリダイレクションを学ぶ課題です。

マンダトリーパート

必須要件

  • コマンド実行
- 外部コマンドの実行 - PATH 環境変数の検索

  • 組み込みコマンド
- 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.toml
  • 制限事項

  • nix クレートは使用可能
  • bash/zsh 互換性は不要