Skip to main content

yash_cli/
startup.rs

1// This file is part of yash, an extended POSIX shell.
2// Copyright (C) 2023 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//! Shell startup
18
19use self::args::{Run, Source, Work};
20use std::str::FromStr as _;
21use yash_env::Env;
22use yash_env::io::Fd;
23use yash_env::option::Option::{Interactive, Monitor, Stdin};
24use yash_env::option::State::On;
25use yash_env::parser::IsKeyword;
26use yash_env::parser::IsName;
27use yash_env::prompt::GetPrompt;
28use yash_env::semantics::command::RunFunction;
29use yash_env::system::resource::GetRlimit;
30use yash_env::system::{Chdir, GetCwd, GetUid, Isatty, Sysconf, TcGetPgrp, Times, Umask};
31use yash_env::trap::RunSignalTrapIfCaught;
32use yash_prompt::ExpandText;
33use yash_semantics::expansion::expand_text;
34use yash_semantics::{RunReadEvalLoop, Runtime};
35use yash_syntax::parser::lex::Lexer;
36
37pub mod args;
38pub mod init_file;
39pub mod input;
40
41/// Tests whether the shell should be implicitly interactive.
42///
43/// As per POSIX, "if the shell reads commands from the standard input and the
44/// shell's standard input and standard error are attached to a terminal, the
45/// shell is considered to be interactive." This function implements this rule.
46///
47/// This function returns `false` if the interactive option is explicitly
48/// specified in the command line arguments to honor the user's intent.
49pub fn auto_interactive<S: Isatty>(system: &S, run: &Run) -> bool {
50    if run.work.source != Source::Stdin {
51        return false;
52    }
53    if run.options.iter().any(|&(o, _)| o == Interactive) {
54        return false;
55    }
56    system.isatty(Fd::STDIN) && system.isatty(Fd::STDERR)
57}
58
59/// Get the environment ready for performing the work.
60///
61/// This function takes the parsed command-line arguments and applies them to
62/// the environment. It also sets up signal dispositions and prepares built-ins
63/// and variables. The function returns the work to be performed, which is
64/// extracted from the `run` argument.
65///
66/// This function is _pure_ in that all system calls are performed by the
67/// `System` trait object (`env.system`).
68pub async fn configure_environment<S>(env: &mut Env<S>, run: Run) -> Work
69where
70    S: Chdir
71        + GetCwd
72        + GetRlimit
73        + GetUid
74        + Runtime
75        + Sysconf
76        + TcGetPgrp
77        + Times
78        + Umask
79        + 'static,
80{
81    // Apply the parsed options to the environment
82    if auto_interactive(&env.system, &run) {
83        env.options.set(Interactive, On);
84    }
85    if run.work.source == self::args::Source::Stdin {
86        env.options.set(Stdin, On);
87    }
88    for &(option, state) in &run.options {
89        env.options.set(option, state);
90    }
91    if env.options.get(Interactive) == On && !run.options.iter().any(|&(o, _)| o == Monitor) {
92        env.options.set(Monitor, On);
93    }
94
95    // Apply the parsed operands to the environment
96    env.arg0 = run.arg0;
97    env.variables.positional_params_mut().values = run.positional_params;
98
99    // Configure internal dispositions for signals
100    if env.options.get(Interactive) == On {
101        env.traps
102            .enable_internal_dispositions_for_terminators(&mut env.system)
103            .ok();
104        if env.options.get(Monitor) == On {
105            env.traps
106                .enable_internal_dispositions_for_stoppers(&mut env.system)
107                .ok();
108        }
109    }
110
111    // Make sure the shell is in the foreground if job control is enabled
112    if env.options.get(Monitor) == On {
113        // Ignore failures as we can still proceed even if we can't get into the foreground
114        env.ensure_foreground().await.ok();
115    }
116
117    // Prepare built-ins
118    env.builtins.extend(yash_builtin::iter());
119
120    // Prepare variables
121    env.init_variables();
122
123    // Inject dependencies
124    inject_dependencies(env);
125
126    run.work
127}
128
129/// Inject dependencies into the environment.
130fn inject_dependencies<S: Runtime + 'static>(env: &mut Env<S>) {
131    env.any.insert(Box::new(IsKeyword::<S>(|_env, word| {
132        yash_syntax::parser::lex::Keyword::from_str(word).is_ok()
133    })));
134
135    env.any.insert(Box::new(IsName::<S>(|_env, name| {
136        yash_syntax::parser::lex::is_name(name)
137    })));
138
139    env.any.insert(Box::new(RunReadEvalLoop::<S>(|env, config| {
140        Box::pin(async move {
141            let mut lexer = Lexer::from(config);
142            yash_semantics::read_eval_loop(env, &mut lexer).await
143        })
144    })));
145
146    env.any.insert(Box::new(RunFunction::<S>(
147        |env, function, fields, env_prep_hook| {
148            Box::pin(async move {
149                yash_semantics::command::simple_command::execute_function_body(
150                    env,
151                    function,
152                    fields,
153                    env_prep_hook,
154                )
155                .await
156            })
157        },
158    )));
159
160    env.any
161        .insert(Box::new(RunSignalTrapIfCaught::<S>(|env, signal| {
162            Box::pin(async move { yash_semantics::trap::run_trap_if_caught(env, signal).await })
163        })));
164
165    env.any.insert(Box::new(ExpandText::<S>(|env, text| {
166        Box::pin(async move { expand_text(env, text).await.ok() })
167    })));
168
169    env.any.insert(Box::new(GetPrompt::<S>(|env, context| {
170        Box::pin(async move {
171            let prompt = yash_prompt::fetch_posix(&env.variables, context);
172            yash_prompt::expand_posix(env, &prompt, false).await
173        })
174    })));
175}