yash-semantics 0.16.0

Yash shell language semantics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// This file is part of yash, an extended POSIX shell.
// Copyright (C) 2022 WATANABE Yuki
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! Helper items for printing expansion results
//!
//! When the `xtrace` [shell option](yash_env::option) is on, the shell traces
//! words expanded during command execution. For each command executed, the
//! shell prints to the standard error a line containing the following:
//!
//! - An expansion of `$PS4`
//! - Expanded command words (assignments, command name, and arguments)
//! - Expanded redirections, possibly followed by here-document contents
//!
//! The trace of a command is printed at a time even though many separate steps
//! perform expansions during the execution of the command. [`XTrace`] is a
//! collection of string buffers that accumulates the results of expansions
//! until they are printed to the standard error.
//!
//! It is no use to collect the expansions when the `xtrace` option is off, so
//! you should create an `XTrace` only if the option is on.
//! [`XTrace::from_options`] is a convenient method to do so.

use crate::expansion::expand_text;
use crate::{Handle as _, Runtime};
use std::fmt::Write;
use std::ops::{Deref, DerefMut};
use yash_env::Env;
use yash_env::option::OptionSet;
use yash_env::option::State;
use yash_env::semantics::Field;
use yash_env::variable::PS4;
use yash_quote::quoted;
use yash_syntax::syntax::Text;

async fn expand_ps4<S: Runtime + 'static>(env: &mut Env<S>) -> String {
    let value = env.variables.get_scalar(PS4).unwrap_or_default().to_owned();

    let text = match value.parse::<Text>() {
        Ok(text) => text,
        Err(error) => {
            _ = error.handle(env).await;
            return value;
        }
    };

    match expand_text(env, &text).await {
        Ok((expansion, _exit_status)) => expansion,
        Err(error) => {
            _ = error.handle(env).await;
            value
        }
    }
}

/// Flag to indicate whether `$PS4` is being expanded
///
/// This is used in [`XTrace::finish`] to prevent (possibly infinite) recursion
/// when `$PS4` contains a command substitution that causes `XTrace::finish` to
/// be called again.
///
/// This flag is stored in [`Env::any`].
#[derive(Clone, Debug, Default, Eq, PartialEq)]
struct ExpandingPs4(bool);

/// Guard that sets the [`ExpandingPs4`] flag to true while it is alive
/// and resets it to false when dropped
#[derive(Debug)]
struct ExpandingGuard<'a, S>(&'a mut Env<S>);

impl<S> Deref for ExpandingGuard<'_, S> {
    type Target = Env<S>;
    fn deref(&self) -> &Self::Target {
        self.0
    }
}

impl<S> DerefMut for ExpandingGuard<'_, S> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.0
    }
}

impl<'a, S> ExpandingGuard<'a, S> {
    /// Creates a new guard that sets the [`ExpandingPs4`] flag to true.
    ///
    /// If the flag is already true, this function returns `None`.
    #[must_use]
    fn new(env: &'a mut Env<S>) -> Option<Self> {
        let expanding_ps4 = env.any.get_or_insert_with(Box::<ExpandingPs4>::default);
        if expanding_ps4.0 {
            None
        } else {
            expanding_ps4.0 = true;
            Some(Self(env))
        }
    }
}

impl<S> Drop for ExpandingGuard<'_, S> {
    fn drop(&mut self) {
        if let Some(expanding_ps4) = self.any.get_mut::<ExpandingPs4>() {
            expanding_ps4.0 = false;
        }
    }
}

/// Collection of temporary string buffers that accumulate expanded strings
///
/// See the [module documentation](self) for details.
///
/// An `XTrace` contains four string buffers that accumulate each of the
/// following:
///
/// - Command words (command name and arguments)
/// - Assignments
/// - Redirections
/// - Here-document contents
///
/// The [`finish`](Self::finish) function creates the final string to be
/// printed.
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
pub struct XTrace {
    words: String,
    assigns: String,
    redirs: String,
    here_doc_contents: String,
}

impl XTrace {
    /// Creates a new trace buffer.
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a new trace buffer if the `xtrace` option is on.
    ///
    /// If the option is off, this function returns `None`.
    #[must_use]
    pub fn from_options(options: &OptionSet) -> Option<Self> {
        match options.get(yash_env::option::Option::XTrace) {
            State::On => Some(Self::new()),
            State::Off => None,
        }
    }

    /// Returns a reference to the words buffer.
    ///
    /// The words buffer is for tracing command words.
    /// When writing to the buffer, the content should end with a space.
    #[inline]
    #[must_use]
    pub fn words(&mut self) -> &mut (impl Write + use<>) {
        &mut self.words
    }

    /// Returns a reference to the assignments buffer.
    ///
    /// The assignments buffer is for tracing assignments.
    /// When writing to the buffer, the content should end with a space.
    #[inline]
    #[must_use]
    pub fn assigns(&mut self) -> &mut (impl Write + use<>) {
        &mut self.assigns
    }

    /// Returns a reference to the redirections buffer.
    ///
    /// The redirections buffer is for tracing redirections.
    /// When writing to the buffer, the content should end with a space.
    ///
    /// You should not write the contents of here-documents to this buffer.
    /// See also [`here_doc_contents`](Self::here_doc_contents).
    #[inline]
    #[must_use]
    pub fn redirs(&mut self) -> &mut (impl Write + use<>) {
        &mut self.redirs
    }

    /// Returns a reference to the here-document contents buffer.
    ///
    /// You should write the contents of here-documents you wrote to the
    /// [redirections buffer](Self::redirs()).
    #[inline]
    #[must_use]
    pub fn here_doc_contents(&mut self) -> &mut (impl Write + use<>) {
        &mut self.here_doc_contents
    }

    /// Clears the buffer contents.
    pub fn clear(&mut self) {
        self.words.clear();
        self.assigns.clear();
        self.redirs.clear();
        self.here_doc_contents.clear();
    }

    /// Returns whether all the buffers are empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.words.is_empty()
            && self.assigns.is_empty()
            && self.redirs.is_empty()
            && self.here_doc_contents.is_empty()
    }

    /// Constructs the final trace to be printed to stderr.
    ///
    /// If all the buffers are empty, the result is empty. Otherwise, the result
    /// is the concatenation of the following:
    ///
    /// - The expansion of `$PS4`
    /// - The concatenation of the `assigns`, `words`, and `redirs` buffers with
    ///   trailing spaces trimmed and a newline appended
    /// - The `here_doc_contents` buffer
    ///
    /// If `$PS4` fails to expand, this function prints an error message and
    /// uses the variable value intact.
    ///
    /// If this function is called while `$PS4` is being expanded inside this
    /// function, the expansion of `$PS4` is skipped and an empty string is
    /// returned. This prevents infinite recursion when `$PS4` contains a
    /// command substitution that causes `XTrace::finish` to be called again.
    pub async fn finish<S: Runtime + 'static>(&self, env: &mut Env<S>) -> String {
        let len = self.assigns.len()
            + self.words.len()
            + self.redirs.len()
            + self.here_doc_contents.len();
        if len == 0 {
            return String::new();
        }

        // Expand $PS4 while preventing infinite recursion
        let Some(mut env) = ExpandingGuard::new(env) else {
            return String::new();
        };
        // TODO Support $YASH_PS4 and $YASH_PS4S
        let ps4 = expand_ps4(&mut env).await;
        drop(env);

        // Construct the final string
        let ps4_len = ps4.len();
        let mut result = ps4;
        result.reserve_exact(len);
        result += &self.assigns;
        result += &self.words;
        result += &self.redirs;
        result.truncate(ps4_len + result[ps4_len..].trim_end_matches(' ').len());
        result.push('\n');
        result += &self.here_doc_contents;
        result
    }
}

/// Convenience function for tracing fields.
///
/// This function writes the field values to the words buffer of the `XTrace`.
pub fn trace_fields(xtrace: Option<&mut XTrace>, fields: &[Field]) {
    if let Some(xtrace) = xtrace {
        for field in fields {
            write!(xtrace.words(), "{} ", quoted(&field.value)).unwrap();
        }
    }
}

/// Convenience function for calling [`XTrace::finish`] on an optional `XTrace`.
pub async fn finish<S: Runtime + 'static>(env: &mut Env<S>, xtrace: Option<XTrace>) -> String {
    if let Some(xtrace) = xtrace {
        xtrace.finish(env).await
    } else {
        String::new()
    }
}

/// Convenience function for [finish]ing and
/// [print](yash_env::system::concurrency::WriteAll::print_error)ing an
/// (optional) `XTrace`.
pub async fn print<S, X>(env: &mut Env<S>, xtrace: X)
where
    S: Runtime + 'static,
    X: Into<Option<XTrace>>,
{
    async fn inner<S: Runtime + 'static>(env: &mut Env<S>, xtrace: Option<XTrace>) {
        let s = finish(env, xtrace).await;
        env.system.print_error(&s).await;
    }
    inner(env, xtrace.into()).await
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tests::echo_builtin;
    use futures_util::FutureExt as _;
    use std::rc::Rc;
    use yash_env::system::Concurrent;
    use yash_env::system::r#virtual::VirtualSystem;
    use yash_env::test_helper::in_virtual_system;
    use yash_env::variable::Scope::Global;

    #[test]
    fn tracing_some_fields() {
        let mut xtrace = XTrace::new();
        let fields = Field::dummies(["one", "two", "'three'"]);
        trace_fields(Some(&mut xtrace), &fields);
        assert_eq!(xtrace.words, r#"one two "'three'" "#);
        assert_eq!(xtrace.assigns, "");
        assert_eq!(xtrace.redirs, "");
        assert_eq!(xtrace.here_doc_contents, "");
    }

    fn fixture() -> Env<Rc<Concurrent<VirtualSystem>>> {
        let mut env = Env::new_virtual();
        env.variables
            .get_or_new(PS4, Global)
            .assign("+${X=x}+ ", None)
            .unwrap();
        env
    }

    #[test]
    fn empty_finish() {
        let mut env = fixture();
        let result = XTrace::new().finish(&mut env).now_or_never().unwrap();
        assert_eq!(result, "");

        // $PS4 should not have been expanded
        assert_eq!(env.variables.get("X"), None);
    }

    #[test]
    fn finish_with_assigns() {
        let mut env = fixture();

        let mut xtrace = XTrace::new();
        xtrace.assigns.push_str("VAR=VALUE FOO=BAR ");
        let result = xtrace.finish(&mut env).now_or_never().unwrap();
        assert_eq!(result, "+x+ VAR=VALUE FOO=BAR\n");

        // $PS4 should have been expanded
        assert_ne!(env.variables.get("X"), None);
    }

    #[test]
    fn finish_with_words() {
        let mut env = fixture();

        let mut xtrace = XTrace::new();
        xtrace.words.push_str("abc '~' foo ");
        let result = xtrace.finish(&mut env).now_or_never().unwrap();
        assert_eq!(result, "+x+ abc '~' foo\n");

        // $PS4 should have been expanded
        assert_ne!(env.variables.get("X"), None);
    }

    #[test]
    fn finish_with_redirs() {
        let mut env = fixture();

        let mut xtrace = XTrace::new();
        xtrace.redirs.push_str("0< /dev/null 1> foo/bar ");
        let result = xtrace.finish(&mut env).now_or_never().unwrap();
        assert_eq!(result, "+x+ 0< /dev/null 1> foo/bar\n");
    }

    #[test]
    fn finish_with_assigns_and_words() {
        let mut env = fixture();

        let mut xtrace = XTrace::new();
        xtrace.assigns.push_str("VAR=VALUE ");
        xtrace.words.push_str("echo argument ");
        let result = xtrace.finish(&mut env).now_or_never().unwrap();
        assert_eq!(result, "+x+ VAR=VALUE echo argument\n");

        let mut xtrace = XTrace::new();
        xtrace.assigns.push(' ');
        xtrace.words.push(' ');
        let result = xtrace.finish(&mut env).now_or_never().unwrap();
        assert_eq!(result, "+x+ \n");
    }

    #[test]
    fn finish_with_words_and_redirs() {
        let mut env = fixture();

        let mut xtrace = XTrace::new();
        xtrace.words.push_str("echo argument ");
        xtrace.redirs.push_str("2> errors ");
        let result = xtrace.finish(&mut env).now_or_never().unwrap();
        assert_eq!(result, "+x+ echo argument 2> errors\n");

        let mut xtrace = XTrace::new();
        xtrace.words.push(' ');
        xtrace.redirs.push(' ');
        let result = xtrace.finish(&mut env).now_or_never().unwrap();
        assert_eq!(result, "+x+ \n");
    }

    #[test]
    fn finish_with_here_doc_contents() {
        let mut env = fixture();

        let mut xtrace = XTrace::new();
        xtrace.here_doc_contents.push_str("EOF\n");
        let result = xtrace.finish(&mut env).now_or_never().unwrap();
        assert_eq!(result, "+x+ \nEOF\n");
    }

    #[test]
    fn finish_with_redirs_and_here_doc_contents() {
        let mut env = fixture();

        let mut xtrace = XTrace::new();
        xtrace.redirs.push_str("0<< END ");
        xtrace.here_doc_contents.push_str(" X \nEND\n");
        let result = xtrace.finish(&mut env).now_or_never().unwrap();
        assert_eq!(result, "+x+ 0<< END\n X \nEND\n");
    }

    #[test]
    fn finish_prevents_recursion() {
        in_virtual_system(|mut env, _state| async move {
            env.builtins.insert("echo", echo_builtin());
            env.variables
                .get_or_new(PS4, Global)
                .assign("$(echo recursive) ", None)
                .unwrap();

            let mut xtrace = XTrace::new();
            xtrace.words.push_str("foo bar ");
            let result = xtrace.finish(&mut env).await;
            assert_eq!(result, "recursive foo bar\n");
        })
    }
}