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