yosh 0.1.1

A POSIX-compliant shell implemented in Rust
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
use std::ffi::CString;

use nix::unistd::{execvp, fork, ForkResult};

use crate::builtin::{classify_builtin, exec_regular_builtin, BuiltinKind};
use crate::builtin::special::exec_special_builtin;
use crate::env::jobs;
use crate::error::{ShellError, RuntimeErrorKind};
use crate::expand::expand_words;
use crate::parser::ast::{Assignment, SimpleCommand};
use crate::signal;

use super::command::wait_child;
use super::redirect::RedirectState;
use super::Executor;

impl Executor {
    /// Execute a simple command (assignments, builtins, or external programs).
    pub(crate) fn exec_simple_command(&mut self, cmd: &SimpleCommand) -> Result<i32, ShellError> {
        // Expand all words
        let expanded = match expand_words(&mut self.env, &cmd.words) {
            Ok(words) => words,
            Err(e) => {
                self.env.exec.last_exit_status = 1;
                return Err(e);
            }
        };

        // Check if expansion triggered a flow control signal (e.g., nounset error)
        if self.env.exec.flow_control.is_some() {
            self.env.exec.last_exit_status = 1;
            return Ok(1);
        }

        // Assignment-only command (no words)
        if expanded.is_empty() {
            // Track the exit status from any command substitutions in the assignments.
            // POSIX: the exit status of an assignment-only command is the exit status
            // of the last command substitution performed during expansion.
            let mut last_cmd_sub_status = 0i32;
            for assignment in &cmd.assignments {
                // Reset before each expansion so we can capture per-assignment status
                self.env.exec.last_exit_status = 0;
                let value = match assignment.value.as_ref() {
                    Some(w) => match crate::expand::expand_word_to_string(&mut self.env, w) {
                        Ok(v) => v,
                        Err(e) => {
                            self.env.exec.last_exit_status = 1;
                            return Err(e);
                        }
                    },
                    None => String::new(),
                };
                // Capture the status set by any command substitution during expansion
                last_cmd_sub_status = self.env.exec.last_exit_status;
                if let Err(e) = self.env.vars.set_with_options(&assignment.name, value, self.env.mode.options.allexport) {
                    self.env.exec.last_exit_status = 1;
                    return Err(ShellError::runtime(RuntimeErrorKind::ReadonlyVariable, format!("{}", e)));
                }
            }
            self.env.exec.last_exit_status = last_cmd_sub_status;
            return Ok(last_cmd_sub_status);
        }

        if self.env.mode.options.xtrace && !expanded.is_empty() {
            eprintln!("+ {}", expanded.join(" "));
        }

        let mut expanded_iter = expanded.into_iter();
        let command_name = expanded_iter.next().unwrap();
        let args: Vec<String> = expanded_iter.collect();

        // Build command string for hooks
        let cmd_str_for_hooks = std::iter::once(command_name.as_str())
            .chain(args.iter().map(|s| s.as_str()))
            .collect::<Vec<_>>()
            .join(" ");
        self.plugins.call_pre_exec(&mut self.env, &cmd_str_for_hooks);

        // Check for function call (before builtins, matching POSIX lookup order)
        if let Some(func_def) = self.env.functions.get(&command_name).cloned() {
            let saved = self.apply_temp_assignments(&cmd.assignments).map_err(|e| {
                self.env.exec.last_exit_status = 1;
                e
            })?;
            let mut redirect_state = RedirectState::new();
            if let Err(e) = redirect_state.apply(&cmd.redirects, &mut self.env, true) {
                self.restore_assignments(saved);
                self.env.exec.last_exit_status = 1;
                return Err(ShellError::runtime(RuntimeErrorKind::RedirectFailed, e));
            }
            let status = self.exec_function_call(&func_def, &args);
            redirect_state.restore();
            self.restore_assignments(saved);
            self.plugins.call_post_exec(&mut self.env, &cmd_str_for_hooks, status);
            self.env.exec.last_exit_status = status;
            return Ok(status);
        }

        // wait needs Executor access (bg_jobs + signal processing)
        if command_name == "wait" {
            let saved = self.apply_temp_assignments(&cmd.assignments).map_err(|e| {
                self.env.exec.last_exit_status = 1;
                e
            })?;
            let mut redirect_state = RedirectState::new();
            if let Err(e) = redirect_state.apply(&cmd.redirects, &mut self.env, true) {
                self.restore_assignments(saved);
                self.env.exec.last_exit_status = 1;
                return Err(ShellError::runtime(RuntimeErrorKind::RedirectFailed, e));
            }
            let status = self.builtin_wait(&args).unwrap_or_else(|e| { eprintln!("{}", e); e.exit_code() });
            redirect_state.restore();
            self.restore_assignments(saved);
            self.plugins.call_post_exec(&mut self.env, &cmd_str_for_hooks, status);
            self.env.exec.last_exit_status = status;
            return Ok(status);
        }

        // fg/bg/jobs need Executor access for job table + terminal control
        if command_name == "fg" || command_name == "bg" || command_name == "jobs" {
            let saved = self.apply_temp_assignments(&cmd.assignments).map_err(|e| {
                self.env.exec.last_exit_status = 1;
                e
            })?;
            let mut redirect_state = RedirectState::new();
            if let Err(e) = redirect_state.apply(&cmd.redirects, &mut self.env, true) {
                self.restore_assignments(saved);
                self.env.exec.last_exit_status = 1;
                return Err(ShellError::runtime(RuntimeErrorKind::RedirectFailed, e));
            }
            let status = match command_name.as_str() {
                "fg" => self.builtin_fg(&args),
                "bg" => self.builtin_bg(&args),
                "jobs" => self.builtin_jobs(&args),
                _ => unreachable!(),
            }.unwrap_or_else(|e| { eprintln!("{}", e); e.exit_code() });
            redirect_state.restore();
            self.restore_assignments(saved);
            self.plugins.call_post_exec(&mut self.env, &cmd_str_for_hooks, status);
            self.env.exec.last_exit_status = status;
            return Ok(status);
        }

        match classify_builtin(&command_name) {
            BuiltinKind::Special => {
                // Special builtins: prefix assignments persist in current env
                for assignment in &cmd.assignments {
                    let value = match assignment.value.as_ref() {
                        Some(w) => match crate::expand::expand_word_to_string(&mut self.env, w) {
                            Ok(v) => v,
                            Err(e) => {
                                self.env.exec.last_exit_status = 1;
                                return Err(e);
                            }
                        },
                        None => String::new(),
                    };
                    if let Err(e) = self.env.vars.set_with_options(&assignment.name, value, self.env.mode.options.allexport) {
                        self.env.exec.last_exit_status = 1;
                        return Err(ShellError::runtime(RuntimeErrorKind::ReadonlyVariable, format!("{}", e)));
                    }
                }
                // exec with no args: redirects persist (don't save/restore)
                if command_name == "exec" && args.is_empty() {
                    let mut redirect_state = RedirectState::new();
                    if let Err(e) = redirect_state.apply(&cmd.redirects, &mut self.env, false) {
                        self.env.exec.last_exit_status = 1;
                        return Err(ShellError::runtime(RuntimeErrorKind::RedirectFailed, e));
                    }
                    self.env.exec.last_exit_status = 0;
                    return Ok(0);
                }

                let mut redirect_state = RedirectState::new();
                if let Err(e) = redirect_state.apply(&cmd.redirects, &mut self.env, true) {
                    self.env.exec.last_exit_status = 1;
                    return Err(ShellError::runtime(RuntimeErrorKind::RedirectFailed, e));
                }
                let status = exec_special_builtin(&command_name, &args, self);
                redirect_state.restore();
                self.plugins.call_post_exec(&mut self.env, &cmd_str_for_hooks, status);
                self.env.exec.last_exit_status = status;
                Ok(status)
            }
            BuiltinKind::Regular => {
                // Regular builtins: prefix assignments are temporary
                let saved = self.apply_temp_assignments(&cmd.assignments).map_err(|e| {
                    self.env.exec.last_exit_status = 1;
                    e
                })?;
                let mut redirect_state = RedirectState::new();
                if let Err(e) = redirect_state.apply(&cmd.redirects, &mut self.env, true) {
                    self.restore_assignments(saved);
                    self.env.exec.last_exit_status = 1;
                    return Err(ShellError::runtime(RuntimeErrorKind::RedirectFailed, e));
                }
                let old_pwd = if command_name == "cd" {
                    self.env.vars.get("PWD").map(|s| s.to_string())
                } else {
                    None
                };
                let status = exec_regular_builtin(&command_name, &args, &mut self.env);
                redirect_state.restore();
                self.restore_assignments(saved);
                self.plugins.call_post_exec(&mut self.env, &cmd_str_for_hooks, status);

                // on_cd hook: fire if cd succeeded
                if command_name == "cd" && status == 0 {
                    let old = old_pwd.unwrap_or_default();
                    let new_dir = self.env.vars.get("PWD").unwrap_or("").to_string();
                    self.plugins.call_on_cd(&mut self.env, &old, &new_dir);
                }

                self.env.exec.last_exit_status = status;
                Ok(status)
            }
            BuiltinKind::NotBuiltin => {
                // Check plugin commands before external
                if let Some(status) = self.plugins.exec_command(&mut self.env, &command_name, &args) {
                    self.plugins.call_post_exec(&mut self.env, &cmd_str_for_hooks, status);
                    self.env.exec.last_exit_status = status;
                    return Ok(status);
                }

                let env_vars = self.build_env_vars(&cmd.assignments).map_err(|e| {
                    self.env.exec.last_exit_status = 1;
                    e
                })?;
                let status = self.exec_external_with_redirects(
                    &command_name, &args, &env_vars, &cmd.redirects,
                );
                self.plugins.call_post_exec(&mut self.env, &cmd_str_for_hooks, status);
                self.env.exec.last_exit_status = status;
                Ok(status)
            }
        }
    }

    /// Merge exported shell variables with command-specific assignments.
    pub(crate) fn build_env_vars(&mut self, assignments: &[Assignment]) -> crate::error::Result<Vec<(String, String)>> {
        let mut vars = self.env.vars.environ().to_vec();
        for assign in assignments {
            let value = match assign.value.as_ref() {
                Some(w) => crate::expand::expand_word_to_string(&mut self.env, w)?,
                None => String::new(),
            };
            // Replace existing or push new
            if let Some(entry) = vars.iter_mut().find(|(k, _)| k == &assign.name) {
                entry.1 = value;
            } else {
                vars.push((assign.name.clone(), value));
            }
        }
        Ok(vars)
    }

    /// Fork, apply redirects in child, exec the command, wait in parent.
    pub(crate) fn exec_external_with_redirects(
        &mut self,
        cmd: &str,
        args: &[String],
        env_vars: &[(String, String)],
        redirects: &[crate::parser::ast::Redirect],
    ) -> i32 {
        // Build argv CStrings
        let c_cmd = match CString::new(cmd) {
            Ok(s) => s,
            Err(_) => {
                eprintln!("yosh: {}: invalid command name", cmd);
                return 127;
            }
        };
        let mut c_args: Vec<CString> = Vec::with_capacity(args.len() + 1);
        c_args.push(c_cmd.clone());
        for a in args {
            match CString::new(a.as_str()) {
                Ok(s) => c_args.push(s),
                Err(_) => {
                    eprintln!("yosh: {}: invalid argument", a);
                    return 1;
                }
            }
        }

        let monitor = self.env.mode.options.monitor;
        let shell_pgid = self.env.process.shell_pgid;
        let ignored = self.env.traps.ignored_signals();

        match unsafe { fork() } {
            Err(e) => {
                eprintln!("yosh: fork: {}", e);
                1
            }
            Ok(ForkResult::Child) => {
                // In monitor mode: put child in its own process group so Ctrl+Z
                // (SIGTSTP) stops only the foreground job, not the shell.
                if monitor {
                    let my_pid = nix::unistd::getpid();
                    nix::unistd::setpgid(my_pid, my_pid).ok();
                    signal::setup_foreground_child_signals(&ignored);
                } else {
                    signal::reset_child_signals(&ignored);
                }

                // Apply redirects (no need to save, we're in the child)
                let mut redir_state = RedirectState::new();
                if let Err(e) = redir_state.apply(redirects, &mut self.env, false) {
                    eprintln!("yosh: {}", e);
                    std::process::exit(1);
                }

                // Set environment variables using libc::setenv directly.
                // SAFETY: single-threaded child after fork. We must NOT use
                // std::env::set_var here because it acquires Rust's internal
                // ENV_LOCK (RwLock). If another thread in the parent held
                // that lock at fork() time, the child inherits the locked
                // state and deadlocks — the lock holder thread does not exist
                // in the child.
                for (k, v) in env_vars {
                    if let (Ok(c_key), Ok(c_val)) = (
                        CString::new(k.as_str()),
                        CString::new(v.as_str()),
                    ) {
                        unsafe { libc::setenv(c_key.as_ptr(), c_val.as_ptr(), 1) };
                    }
                }

                let err = execvp(&c_cmd, &c_args).unwrap_err();
                use nix::errno::Errno;
                let exit_code = match err {
                    Errno::ENOENT => {
                        eprintln!("yosh: {}: command not found", cmd);
                        127
                    }
                    Errno::EACCES => {
                        eprintln!("yosh: {}: permission denied", cmd);
                        126
                    }
                    _ => {
                        eprintln!("yosh: {}: {}", cmd, err);
                        127
                    }
                };
                std::process::exit(exit_code);
            }
            Ok(ForkResult::Parent { child }) => {
                if monitor {
                    // Ensure child is in its own process group (race-free: both
                    // parent and child call setpgid).
                    nix::unistd::setpgid(child, child).ok();

                    let full_cmd = std::iter::once(cmd)
                        .chain(args.iter().map(|s| s.as_str()))
                        .collect::<Vec<_>>()
                        .join(" ");
                    let job_id = self.env.process.jobs.add_job(
                        child,
                        vec![child],
                        full_cmd,
                        true,
                    );

                    // Hand terminal to the child's process group.
                    jobs::give_terminal(child).ok();

                    let result = self.wait_for_foreground_job(job_id);

                    // Take terminal back for the shell.
                    jobs::take_terminal(shell_pgid).ok();

                    result.last_status
                } else {
                    wait_child(child).unwrap_or(1)
                }
            }
        }
    }

    /// Apply prefix assignments temporarily, returning saved values for later restoration.
    pub(crate) fn apply_temp_assignments(
        &mut self,
        assignments: &[Assignment],
    ) -> crate::error::Result<Vec<(String, Option<String>)>> {
        let mut saved = Vec::new();
        for assignment in assignments {
            let old_val = self.env.vars.get(&assignment.name).map(|s| s.to_string());
            saved.push((assignment.name.clone(), old_val));
            let value = match assignment.value.as_ref() {
                Some(w) => crate::expand::expand_word_to_string(&mut self.env, w)?,
                None => String::new(),
            };
            let _ = self.env.vars.set(&assignment.name, value);
        }
        Ok(saved)
    }

    /// Restore variables to their pre-assignment values.
    pub(crate) fn restore_assignments(&mut self, saved: Vec<(String, Option<String>)>) {
        for (name, old_val) in saved {
            match old_val {
                Some(val) => {
                    let _ = self.env.vars.set(&name, val);
                }
                None => {
                    let _ = self.env.vars.unset(&name);
                }
            }
        }
    }
}