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(&env.system)
103 .await
104 .ok();
105 if env.options.get(Monitor) == On {
106 env.traps
107 .enable_internal_dispositions_for_stoppers(&env.system)
108 .await
109 .ok();
110 }
111 }
112
113 if env.options.get(Monitor) == On {
115 env.ensure_foreground().await.ok();
117 }
118
119 env.builtins.extend(yash_builtin::iter());
121
122 env.init_variables();
124
125 inject_dependencies(env);
127
128 run.work
129}
130
131fn inject_dependencies<S: Runtime + 'static>(env: &mut Env<S>) {
133 env.any.insert(Box::new(IsKeyword::<S>(|_env, word| {
134 yash_syntax::parser::lex::Keyword::from_str(word).is_ok()
135 })));
136
137 env.any.insert(Box::new(IsName::<S>(|_env, name| {
138 yash_syntax::parser::lex::is_name(name)
139 })));
140
141 env.any.insert(Box::new(RunReadEvalLoop::<S>(|env, config| {
142 Box::pin(async move {
143 let mut lexer = Lexer::from(config);
144 yash_semantics::read_eval_loop(env, &mut lexer).await
145 })
146 })));
147
148 env.any.insert(Box::new(RunFunction::<S>(
149 |env, function, fields, env_prep_hook| {
150 Box::pin(async move {
151 yash_semantics::command::simple_command::execute_function_body(
152 env,
153 function,
154 fields,
155 env_prep_hook,
156 )
157 .await
158 })
159 },
160 )));
161
162 env.any
163 .insert(Box::new(RunSignalTrapIfCaught::<S>(|env, signal| {
164 Box::pin(async move { yash_semantics::trap::run_trap_if_caught(env, signal).await })
165 })));
166
167 env.any.insert(Box::new(ExpandText::<S>(|env, text| {
168 Box::pin(async move { expand_text(env, text).await.ok() })
169 })));
170
171 env.any.insert(Box::new(GetPrompt::<S>(|env, context| {
172 Box::pin(async move {
173 let prompt = yash_prompt::fetch_posix(&env.variables, context);
174 yash_prompt::expand_posix(env, &prompt, false).await
175 })
176 })));
177}