yash_cli/startup/input.rs
1// This file is part of yash, an extended POSIX shell.
2// Copyright (C) 2024 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//! Preparing input for the parser
18//!
19//! This module implements the [`prepare_input`] function that prepares the
20//! input for the shell syntax parser. The input is constructed from the given
21//! source and decorated with the [`Echo`] and [`Prompter`] decorators as
22//! necessary.
23//!
24//! [`PrepareInputError`] defines the error that may occur when preparing the
25//! input.
26
27use super::args::Source;
28use std::cell::RefCell;
29use std::ffi::CString;
30use thiserror::Error;
31use yash_env::Env;
32use yash_env::input::Echo;
33use yash_env::input::FdReader;
34use yash_env::input::IgnoreEof;
35use yash_env::input::Reporter;
36use yash_env::io::Fd;
37use yash_env::io::move_fd_internal;
38use yash_env::option::Option::Interactive;
39use yash_env::option::State::{Off, On};
40use yash_env::parser::Config;
41use yash_env::system::{
42 Close, Dup, Errno, Fcntl, Fstat, Isatty, Mode, OfdAccess, Open, OpenFlag, Read, Signals, Write,
43};
44use yash_prompt::Prompter;
45use yash_syntax::input::InputObject;
46use yash_syntax::input::Memory;
47use yash_syntax::parser::lex::Lexer;
48use yash_syntax::source::Source as SyntaxSource;
49
50/// Error returned by [`prepare_input`]
51#[derive(Clone, Debug, Eq, Error, PartialEq)]
52#[error("cannot open script file '{path}': {errno}")]
53pub struct PrepareInputError<'a> {
54 /// Raw error value returned by the underlying system call.
55 pub errno: Errno,
56 /// Path of the script file that could not be opened.
57 pub path: &'a str,
58}
59
60/// Prepares the input for the shell syntax parser.
61///
62/// This function constructs a lexer from the given source with the
63/// following decorators applied to the input object:
64///
65/// - If the source is read with a file descriptor, the [`Echo`] decorator is
66/// applied to the input to implement the [`Verbose`] shell option.
67/// - If the [`Interactive`] option is enabled and the source is read with a
68/// file descriptor, the [`Prompter`] decorator is applied to the input to
69/// show the prompt.
70/// - If the [`Interactive`] option is enabled, the [`Reporter`] decorator is
71/// applied to the input to show changes in job status before prompting for
72/// the next command.
73/// - If the [`Interactive`] option is enabled and the source is read with a
74/// file descriptor, the [`IgnoreEof`] decorator is applied to the input to
75/// implement the [`IgnoreEof`](yash_env::option::IgnoreEof) shell option.
76///
77/// The `RefCell` passed as the first argument should be shared with (and only
78/// with) the [`read_eval_loop`](yash_semantics::read_eval_loop) function that
79/// consumes the input and executes the parsed commands.
80///
81/// [`Verbose`]: yash_env::option::Verbose
82pub fn prepare_input<'s, 'i, 'e, S>(
83 env: &'i RefCell<&mut Env<S>>,
84 source: &'s Source,
85) -> Result<Lexer<'i>, PrepareInputError<'e>>
86where
87 's: 'i + 'e,
88 S: Close + Dup + Fcntl + Fstat + Isatty + Open + Read + Signals + Write + 'static,
89{
90 fn lexer_with_input_and_source<'a>(
91 input: Box<dyn InputObject + 'a>,
92 source: SyntaxSource,
93 ) -> Lexer<'a> {
94 let mut config = Config::with_input(input);
95 config.source = Some(source.into());
96 config.into()
97 }
98
99 match source {
100 Source::Stdin => {
101 let system = env.borrow().system.clone();
102 if system.isatty(Fd::STDIN) || system.fd_is_pipe(Fd::STDIN) {
103 // It makes virtually no sense to make it blocking here
104 // since we will be doing non-blocking reads anyway,
105 // but POSIX requires us to do it.
106 // https://pubs.opengroup.org/onlinepubs/9799919799/utilities/sh.html#tag_20_110_06
107 _ = system.get_and_set_nonblocking(Fd::STDIN, false);
108 }
109
110 let input = prepare_fd_input(Fd::STDIN, env);
111 let source = SyntaxSource::Stdin;
112 Ok(lexer_with_input_and_source(input, source))
113 }
114
115 Source::File { path } => {
116 let system = env.borrow().system.clone();
117
118 let c_path = CString::new(path.as_str()).map_err(|_| PrepareInputError {
119 errno: Errno::EILSEQ,
120 path,
121 })?;
122 let fd = system
123 .open(
124 &c_path,
125 OfdAccess::ReadOnly,
126 OpenFlag::CloseOnExec.into(),
127 Mode::empty(),
128 )
129 .and_then(|fd| move_fd_internal(&system, fd))
130 .map_err(|errno| PrepareInputError { errno, path })?;
131
132 let input = prepare_fd_input(fd, env);
133 let path = path.to_owned();
134 let source = SyntaxSource::CommandFile { path };
135 Ok(lexer_with_input_and_source(input, source))
136 }
137
138 Source::String(command) => {
139 let basic_input = Memory::new(command);
140
141 let is_interactive = env.borrow().options.get(Interactive) == On;
142 let input: Box<dyn InputObject> = if is_interactive {
143 Box::new(Reporter::new(basic_input, env))
144 } else {
145 Box::new(basic_input)
146 };
147 let source = SyntaxSource::CommandString;
148 Ok(lexer_with_input_and_source(input, source))
149 }
150 }
151}
152
153/// Creates an input object from a file descriptor.
154///
155/// This function creates an [`FdReader`] object from the given file descriptor
156/// and wraps it with the [`Echo`] decorator. If the [`Interactive`] option is
157/// enabled, the [`Prompter`], [`Reporter`], and [`IgnoreEof`] decorators are
158/// applied to the input object.
159fn prepare_fd_input<'i, S>(fd: Fd, ref_env: &'i RefCell<&mut Env<S>>) -> Box<dyn InputObject + 'i>
160where
161 S: Fcntl + Isatty + Read + Signals + Write + 'static,
162{
163 let env = ref_env.borrow();
164 let system = env.system.clone();
165
166 let basic_input = Echo::new(FdReader::new(fd, system), ref_env);
167
168 if env.options.get(Interactive) == Off {
169 Box::new(basic_input)
170 } else {
171 // The order of these decorators is important. The prompt should be shown after
172 // the job status is reported, and both should be shown again if an EOF is ignored.
173 let prompter = Prompter::new(basic_input, ref_env);
174 let reporter = Reporter::new(prompter, ref_env);
175 let message =
176 "# Type `exit` to leave the shell when the ignore-eof option is on.\n".to_string();
177 Box::new(IgnoreEof::new(reporter, fd, ref_env, message))
178 }
179}