1use 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
41pub 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
59pub 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 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 env.arg0 = run.arg0;
97 env.variables.positional_params_mut().values = run.positional_params;
98
99 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 if env.options.get(Monitor) == On {
113 env.ensure_foreground().await.ok();
115 }
116
117 env.builtins.extend(yash_builtin::iter());
119
120 env.init_variables();
122
123 inject_dependencies(env);
125
126 run.work
127}
128
129fn 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}