yash_cli/
lib.rs

1// This file is part of yash, an extended POSIX shell.
2// Copyright (C) 2021 WATANABE Yuki
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17//! This is a library crate that implements the command-line frontend for the
18//! yash shell. It is used by the `yash3` binary crate to provide the shell
19//! functionality. Currently, this crate is not intended to be used as a library
20//! by other crates.
21//!
22//! The entry point for the shell is the [`main`] function, which is to be used
23//! as the `main` function in the binary crate. The function sets up the shell
24//! environment and runs the main read-eval loop.
25
26pub mod startup;
27// mod runner;
28
29use self::startup::args::Parse;
30use self::startup::init_file::run_rcfile;
31use self::startup::input::prepare_input;
32use std::cell::RefCell;
33use std::ops::ControlFlow::{Break, Continue};
34use yash_env::Env;
35use yash_env::RealSystem;
36use yash_env::System;
37use yash_env::option::{Interactive, On};
38use yash_env::signal;
39use yash_env::system::{Disposition, Errno};
40use yash_executor::Executor;
41use yash_semantics::trap::run_exit_trap;
42use yash_semantics::{Divert, ExitStatus};
43use yash_semantics::{interactive_read_eval_loop, read_eval_loop};
44
45async fn print_version(env: &mut Env) -> ExitStatus {
46    let version = env!("CARGO_PKG_VERSION");
47    let result = yash_builtin::common::output(env, &format!("yash {}\n", version)).await;
48    result.exit_status()
49}
50
51// The RefCell is local to this function, so it is safe to keep borrows across await points.
52#[allow(clippy::await_holding_refcell_ref)]
53async fn parse_and_print(mut env: Env) -> ExitStatus {
54    // Parse the command-line arguments
55    let run = match self::startup::args::parse(std::env::args()) {
56        Ok(Parse::Help) => todo!("print help"),
57        Ok(Parse::Version) => return print_version(&mut env).await,
58        Ok(Parse::Run(run)) => run,
59        Err(e) => {
60            let arg0 = std::env::args().next().unwrap_or_else(|| "yash".to_owned());
61            env.system.print_error(&format!("{}: {}\n", arg0, e)).await;
62            return ExitStatus::ERROR;
63        }
64    };
65
66    // Import environment variables
67    env.variables.extend_env(std::env::vars());
68
69    let work = self::startup::configure_environment(&mut env, run);
70
71    let is_interactive = env.options.get(Interactive) == On;
72
73    // Run initialization files
74    // TODO run profile if login
75    run_rcfile(&mut env, work.rcfile).await;
76
77    // Prepare the input for the main read-eval loop
78    let ref_env = &RefCell::new(&mut env);
79    let lexer = match prepare_input(ref_env, &work.source) {
80        Ok(lexer) => lexer,
81        Err(e) => {
82            let arg0 = std::env::args().next().unwrap_or_else(|| "yash".to_owned());
83            let message = format!("{}: {}\n", arg0, e);
84            // The borrow checker of Rust 1.79.0 is not smart enough to reason
85            // about the lifetime of `e` here, so we re-borrow from `ref_env`
86            // instead of reusing `env`.
87            // env.system.print_error(&message).await;
88            ref_env.borrow_mut().system.print_error(&message).await;
89            return match e.errno {
90                Errno::ENOENT | Errno::ENOTDIR | Errno::EILSEQ => ExitStatus::NOT_FOUND,
91                _ => ExitStatus::NOEXEC,
92            };
93        }
94    };
95
96    // Run the read-eval loop
97    let result = if is_interactive {
98        interactive_read_eval_loop(ref_env, &mut { lexer }).await
99    } else {
100        read_eval_loop(ref_env, &mut { lexer }).await
101    };
102
103    env.apply_result(result);
104
105    match result {
106        Continue(())
107        | Break(Divert::Continue { .. })
108        | Break(Divert::Break { .. })
109        | Break(Divert::Return(_))
110        | Break(Divert::Interrupt(_))
111        | Break(Divert::Exit(_)) => run_exit_trap(&mut env).await,
112        Break(Divert::Abort(_)) => (),
113    }
114
115    env.exit_status
116}
117
118pub fn main() -> ! {
119    // SAFETY: This is the only instance of RealSystem we create in the whole
120    // process.
121    let system = unsafe { RealSystem::new() };
122    let mut env = Env::with_system(Box::new(system));
123
124    // Rust by default sets SIGPIPE to SIG_IGN, which is not desired.
125    // As an imperfect workaround, we set SIGPIPE to SIG_DFL here.
126    // TODO Use unix_sigpipe: https://github.com/rust-lang/rust/issues/97889
127    let sigpipe = env
128        .system
129        .signal_number_from_name(signal::Name::Pipe)
130        .unwrap();
131    _ = env.system.sigaction(sigpipe, Disposition::Default);
132
133    let system = env.system.clone();
134    let executor = Executor::new();
135    let task = Box::pin(async {
136        let exit_status = parse_and_print(env).await;
137        std::process::exit(exit_status.0);
138    });
139    // SAFETY: We never create new threads in the whole process, so wakers are
140    // never shared between threads.
141    unsafe { executor.spawn_pinned(task) }
142    loop {
143        executor.run_until_stalled();
144        system.select(false).ok();
145    }
146}