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
use super::*;
use crate::{syscalls::*, WasiTtyState};

/// ### `tty_set()`
/// Updates the properties of the rect
#[instrument(level = "trace", skip_all, ret)]
pub fn tty_set<M: MemorySize>(
    mut ctx: FunctionEnvMut<'_, WasiEnv>,
    tty_state: WasmPtr<Tty, M>,
) -> Result<Errno, WasiError> {
    let env = ctx.data();

    let memory = unsafe { env.memory_view(&ctx) };
    let state = wasi_try_mem_ok!(tty_state.read(&memory));
    let echo = state.echo;
    let line_buffered = state.line_buffered;
    let line_feeds = true;
    debug!(
        %echo,
        %line_buffered,
        %line_feeds
    );

    let state = crate::os::tty::WasiTtyState {
        cols: state.cols,
        rows: state.rows,
        width: state.width,
        height: state.height,
        stdin_tty: state.stdin_tty,
        stdout_tty: state.stdout_tty,
        stderr_tty: state.stderr_tty,
        echo,
        line_buffered,
        line_feeds,
    };

    wasi_try_ok!({
        #[allow(clippy::redundant_clone)]
        tty_set_internal(&mut ctx, state.clone())
    });
    let env = ctx.data();

    #[cfg(feature = "journal")]
    if env.enable_journal {
        JournalEffector::save_tty_set(&mut ctx, state).map_err(|err| {
            tracing::error!("failed to save path symbolic link event - {}", err);
            WasiError::Exit(ExitCode::from(Errno::Fault))
        })?;
    }

    Ok(Errno::Success)
}

pub fn tty_set_internal(
    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
    state: WasiTtyState,
) -> Result<(), Errno> {
    let env = ctx.data();
    let bridge = if let Some(t) = env.runtime.tty() {
        t
    } else {
        return Err(Errno::Notsup);
    };
    bridge.tty_set(state);

    Ok(())
}