Skip to main content

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