mxsh 0.1.0

Embeddable POSIX-style shell parser and runtime
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
use std::env;

use super::*;
use crate::embed::{OutcomeCaptureHandle, ShellDiagnostic, TraceEvent};

const GETOPTS_CURSOR_VAR: &str = "__MXSH_GETOPTS_CURSOR";
const GETOPTS_INDEX_VAR: &str = "__MXSH_GETOPTS_INDEX";

/// Branch control signals for break/continue/return.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub(super) enum BranchControl {
    None,
    Break(u32),
    Continue(u32),
    Return,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Variable {
    pub value: String,
    pub attrib: u32,
}

#[derive(Debug, Clone)]
pub(super) enum JobState {
    Running,
    Stopped(i32),
    Done(i32),
}

#[derive(Debug, Clone)]
pub(super) struct JobInfo {
    pub(super) job_id: u32,
    pub(super) display_pid: Option<u32>,
    pub(super) state: JobState,
}

#[derive(Debug, Clone)]
pub(super) struct ShellJob {
    pub(super) job_id: u32,
    pub(super) handle: sys::ProcessHandle,
    pub(super) display_pid: Option<u32>,
    pub(super) state: JobState,
    pub(super) command_id: String,
    pub(super) finish_emitted: bool,
}

#[derive(Debug, Clone)]
pub struct ShellState {
    pub exit_code: i32,
    pub options: u32,
    pub frame: Vec<String>,
    pub interactive: bool,
    pub last_status: i32,
    pub(super) vars: HashMap<String, Variable>,
    pub cwd: PathBuf,
    /// Shell functions: name -> command body.
    pub(super) functions: HashMap<String, AstCommand>,
    /// Aliases: name -> replacement text.
    pub(super) aliases: Arc<HashMap<String, String>>,
    /// Branch control for break/continue/return.
    pub(super) branch: BranchControl,
    /// Loop depth (for break/continue validation).
    pub(super) loop_depth: u32,
    /// Function-call depth (for return validation).
    pub(super) function_depth: u32,
    /// PID of the most recent asynchronous list.
    pub(super) last_bg_pid: Option<u32>,
    /// Trap actions by signal number (0 is EXIT).
    pub(super) traps: HashMap<i32, String>,
    /// Background jobs already notified via `set -o notify`.
    pub(super) notified_jobs: HashSet<u32>,
    /// Monotonic job ID generator.
    pub(super) next_job_id: u32,
    /// Background jobs and their latest observed state.
    pub(super) jobs: Vec<ShellJob>,
    /// Ensure we only print the unsupported vi-editor warning once.
    pub(super) warned_vi_unsupported: bool,
    /// Whether this shell instance should install OS signal handlers.
    pub(crate) manage_signals: bool,
    /// Previous handlers saved while monitor mode is active.
    pub(super) monitor_signals: MonitorSignalState,
    /// Original signal handlers replaced by `trap`.
    pub(super) trap_signals: TrapSignalState,
    /// Whether monitor-mode signal ignores are currently installed.
    pub(super) monitor_mode_active: bool,
    /// Non-fatal expansion failure pending propagation to the enclosing command.
    pub(super) expansion_error: Option<i32>,
    /// Source label for diagnostics (typically current script path).
    pub(super) current_source: Option<String>,
    /// Program command ID currently being executed for event correlation.
    pub(super) active_command_id: Option<String>,
    pub stdin_fd: sys::FileDescriptor,
    pub stdout_fd: sys::FileDescriptor,
    pub stderr_fd: sys::FileDescriptor,
    pub(super) raw_fds: HashMap<i32, sys::FileDescriptor>,
    pub(super) named_fds: HashMap<String, i32>,
    pub(crate) outcome_captures: Vec<OutcomeCaptureHandle>,
    pub(crate) definition: Arc<crate::embed::ShellDefinition>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(super) struct BackgroundStateSnapshot {
    pub(super) exit_code: i32,
    pub(super) options: u32,
    pub(super) frame: Vec<String>,
    pub(super) interactive: bool,
    pub(super) last_status: i32,
    pub(super) vars: HashMap<String, Variable>,
    pub(super) cwd: PathBuf,
    pub(super) functions: HashMap<String, AstCommand>,
    pub(super) aliases: HashMap<String, String>,
    pub(super) branch: BranchControl,
    pub(super) loop_depth: u32,
    pub(super) function_depth: u32,
    pub(super) last_bg_pid: Option<u32>,
    pub(super) traps: HashMap<i32, String>,
    pub(super) warned_vi_unsupported: bool,
    pub(super) expansion_error: Option<i32>,
    pub(super) current_source: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(super) struct BackgroundMachinePayload {
    pub(super) state: BackgroundStateSnapshot,
    pub(super) and_or_list: AndOrList,
    pub(super) command_id: String,
}

impl Default for ShellState {
    fn default() -> Self {
        Self::new()
    }
}

impl ShellState {
    pub fn new() -> Self {
        Self {
            exit_code: -1,
            options: 0,
            frame: Vec::new(),
            interactive: false,
            last_status: 0,
            vars: HashMap::new(),
            cwd: env::current_dir().unwrap_or_else(|_| PathBuf::from("/")),
            functions: HashMap::new(),
            aliases: Arc::new(HashMap::new()),
            branch: BranchControl::None,
            loop_depth: 0,
            function_depth: 0,
            last_bg_pid: None,
            traps: HashMap::new(),
            notified_jobs: HashSet::new(),
            next_job_id: 1,
            jobs: Vec::new(),
            warned_vi_unsupported: false,
            manage_signals: false,
            monitor_signals: MonitorSignalState::default(),
            trap_signals: TrapSignalState::default(),
            monitor_mode_active: false,
            expansion_error: None,
            current_source: None,
            active_command_id: None,
            stdin_fd: sys::FileDescriptor::STDIN,
            stdout_fd: sys::FileDescriptor::STDOUT,
            stderr_fd: sys::FileDescriptor::STDERR,
            raw_fds: HashMap::new(),
            named_fds: HashMap::new(),
            outcome_captures: Vec::new(),
            definition: Arc::new(crate::embed::ShellDefinition::default()),
        }
    }

    pub(crate) fn fork_session(&self) -> Self {
        self.clone()
    }

    pub(crate) fn detached_session(&self) -> Self {
        let mut detached = self.clone();
        detached.active_command_id = None;
        detached.outcome_captures.clear();
        detached
    }

    pub fn last_background_pid(&self) -> Option<u32> {
        self.last_bg_pid
    }

    pub(crate) fn shell_name(&self) -> &str {
        self.definition.identity.name()
    }

    pub(super) fn active_command_id(&self) -> Option<&str> {
        self.active_command_id.as_deref()
    }

    pub(super) fn set_active_command_id(&mut self, command_id: Option<String>) {
        self.active_command_id = command_id;
    }

    pub(crate) fn has_outcome_capture(&self) -> bool {
        !self.outcome_captures.is_empty()
    }

    pub(crate) fn push_outcome_capture(&mut self, capture: OutcomeCaptureHandle) {
        self.outcome_captures.push(capture);
    }

    pub(crate) fn pop_outcome_capture(&mut self) -> Option<OutcomeCaptureHandle> {
        self.outcome_captures.pop()
    }

    pub(crate) fn record_diagnostic(&self, diagnostic: ShellDiagnostic) {
        for capture in &self.outcome_captures {
            capture
                .lock()
                .unwrap_or_else(|poisoned| poisoned.into_inner())
                .diagnostics
                .push(diagnostic.clone());
        }
    }

    pub(crate) fn record_trace_event(&self, event: TraceEvent) {
        for capture in &self.outcome_captures {
            capture
                .lock()
                .unwrap_or_else(|poisoned| poisoned.into_inner())
                .trace
                .push(event.clone());
        }
    }

    pub(crate) fn prefixed_message(&self, message: impl AsRef<str>) -> String {
        format!("{}: {}", self.shell_name(), message.as_ref())
    }

    pub(super) fn background_snapshot(&self) -> BackgroundStateSnapshot {
        BackgroundStateSnapshot {
            exit_code: self.exit_code,
            options: self.options,
            frame: self.frame.clone(),
            interactive: self.interactive,
            last_status: self.last_status,
            vars: self.vars.clone(),
            cwd: self.cwd.clone(),
            functions: self.functions.clone(),
            aliases: self.aliases.as_ref().clone(),
            branch: self.branch,
            loop_depth: self.loop_depth,
            function_depth: self.function_depth,
            last_bg_pid: self.last_bg_pid,
            traps: self.traps.clone(),
            warned_vi_unsupported: self.warned_vi_unsupported,
            expansion_error: self.expansion_error,
            current_source: self.current_source.clone(),
        }
    }

    #[cfg(any(
        feature = "frontend",
        all(test, feature = "test-support", feature = "unix-runtime")
    ))]
    pub(super) fn apply_background_snapshot(&mut self, snapshot: BackgroundStateSnapshot) {
        self.exit_code = snapshot.exit_code;
        self.options = snapshot.options;
        self.frame = snapshot.frame;
        self.interactive = snapshot.interactive;
        self.last_status = snapshot.last_status;
        self.vars = snapshot.vars;
        self.cwd = snapshot.cwd;
        self.functions = snapshot.functions;
        self.aliases = Arc::new(snapshot.aliases);
        self.branch = snapshot.branch;
        self.loop_depth = snapshot.loop_depth;
        self.function_depth = snapshot.function_depth;
        self.last_bg_pid = snapshot.last_bg_pid;
        self.traps = snapshot.traps;
        self.warned_vi_unsupported = snapshot.warned_vi_unsupported;
        self.expansion_error = snapshot.expansion_error;
        self.current_source = snapshot.current_source;
        self.active_command_id = None;
        self.outcome_captures.clear();
        self.notified_jobs.clear();
        self.next_job_id = 1;
        self.jobs.clear();
        self.manage_signals = true;
        self.monitor_signals = MonitorSignalState::default();
        self.trap_signals = TrapSignalState::default();
        self.monitor_mode_active = false;
    }

    /// Set a variable, respecting readonly.
    pub fn env_set(&mut self, key: &str, value: String, attrib: u32) -> bool {
        if self
            .vars
            .get(key)
            .is_some_and(|var| (var.attrib & VAR_READONLY) != 0)
        {
            return false;
        }
        self.vars
            .insert(key.to_string(), Variable { value, attrib });
        self.reset_getopts_state_for(key);
        true
    }

    /// Set a variable unconditionally (bypasses readonly).
    pub(crate) fn env_set_internal(&mut self, key: &str, value: String, attrib: u32) {
        self.vars
            .insert(key.to_string(), Variable { value, attrib });
        self.reset_getopts_state_for(key);
    }

    /// Unset a variable, respecting readonly.
    pub fn env_unset(&mut self, key: &str) {
        if self
            .vars
            .get(key)
            .is_some_and(|var| (var.attrib & VAR_READONLY) != 0)
        {
            return;
        }
        self.vars.remove(key);
        self.reset_getopts_state_for(key);
    }

    /// Get a variable value.
    pub fn env_get(&self, key: &str) -> Option<&str> {
        self.vars.get(key).map(|v| v.value.as_str())
    }

    fn reset_getopts_state_for(&mut self, key: &str) {
        if key == "OPTIND" {
            self.vars.remove(GETOPTS_CURSOR_VAR);
            self.vars.remove(GETOPTS_INDEX_VAR);
        }
    }

    /// Collect all exported variables.
    pub fn exported_env(&self) -> Vec<(String, String)> {
        self.vars
            .iter()
            .filter(|(_, v)| (v.attrib & VAR_EXPORT) != 0)
            .map(|(k, v)| (k.clone(), v.value.clone()))
            .collect()
    }

    pub(crate) fn inherited_fds(&self) -> Vec<sys::PassedFileDescriptor> {
        let mut inherited: Vec<_> = self
            .raw_fds
            .iter()
            .filter_map(|(&child_fd, &parent_fd)| {
                parent_fd.is_valid().then_some(sys::PassedFileDescriptor {
                    parent_fd,
                    child_fd: sys::FileDescriptor::new(child_fd),
                })
            })
            .collect();
        inherited.sort_by_key(|passed| passed.child_fd.as_i32());
        inherited
    }

    pub(crate) fn inherited_fd_entries(&self) -> Vec<(i32, sys::FileDescriptor)> {
        let mut inherited: Vec<_> = self
            .raw_fds
            .iter()
            .map(|(&child, &parent)| (child, parent))
            .collect();
        inherited.sort_by_key(|(child, _)| *child);
        inherited
    }

    pub(crate) fn named_inherited_fd_entries(&self) -> Vec<crate::embed::NamedFileDescriptor> {
        let mut inherited: Vec<_> = self
            .named_fds
            .iter()
            .filter_map(|(name, &child)| {
                self.raw_fds.get(&child).copied().map(|parent| {
                    crate::embed::NamedFileDescriptor::new(name.clone(), child, parent)
                })
            })
            .collect();
        inherited.sort_by(|left, right| left.name.cmp(&right.name));
        inherited
    }

    pub(crate) fn named_inherited_fd(
        &self,
        name: &str,
    ) -> Option<crate::embed::NamedFileDescriptor> {
        let child_fd = *self.named_fds.get(name)?;
        let parent_fd = *self.raw_fds.get(&child_fd)?;
        Some(crate::embed::NamedFileDescriptor::new(
            name, child_fd, parent_fd,
        ))
    }

    pub(crate) fn set_inherited_fd(
        &mut self,
        child_fd: i32,
        parent_fd: sys::FileDescriptor,
    ) -> Option<sys::FileDescriptor> {
        self.raw_fds.insert(child_fd, parent_fd)
    }

    pub(crate) fn set_named_inherited_fd(
        &mut self,
        named_fd: crate::embed::NamedFileDescriptor,
    ) -> Option<crate::embed::NamedFileDescriptor> {
        let crate::embed::NamedFileDescriptor {
            name,
            child_fd,
            parent_fd,
        } = named_fd;
        let previous = self.named_inherited_fd(&name);
        self.named_fds.insert(name, child_fd);
        let _ = self.set_inherited_fd(child_fd, parent_fd);
        if let Some(ref previous) = previous
            && previous.child_fd != child_fd
            && !self
                .named_fds
                .values()
                .any(|&named_child| named_child == previous.child_fd)
        {
            let _ = self.raw_fds.remove(&previous.child_fd);
        }
        previous
    }

    pub(crate) fn aliases(&self) -> Vec<(String, String)> {
        let mut aliases: Vec<_> = self
            .aliases
            .iter()
            .map(|(name, value)| (name.clone(), value.clone()))
            .collect();
        aliases.sort_by(|left, right| left.0.cmp(&right.0));
        aliases
    }

    pub(crate) fn alias(&self, name: &str) -> Option<&str> {
        self.aliases.get(name).map(String::as_str)
    }

    pub(crate) fn set_alias(&mut self, name: impl Into<String>, value: impl Into<String>) {
        Arc::make_mut(&mut self.aliases).insert(name.into(), value.into());
    }

    pub(crate) fn functions(&self) -> Vec<(String, AstCommand)> {
        let mut functions: Vec<_> = self
            .functions
            .iter()
            .map(|(name, body)| (name.clone(), body.clone()))
            .collect();
        functions.sort_by(|left, right| left.0.cmp(&right.0));
        functions
    }

    pub(crate) fn function(&self, name: &str) -> Option<&AstCommand> {
        self.functions.get(name)
    }

    pub(crate) fn set_function(&mut self, name: impl Into<String>, body: AstCommand) {
        self.functions.insert(name.into(), body);
    }

    pub(crate) fn argv0(&self) -> Option<&str> {
        self.frame.first().map(String::as_str)
    }

    pub(crate) fn positional_parameters(&self) -> &[String] {
        self.frame.get(1..).unwrap_or(&[])
    }

    pub(crate) fn initialize_defaults(&mut self) {
        self.env_set("IFS", " \t\n".to_string(), 0);
        self.env_set("PPID", sys::parent_pid().to_string(), 0);
        let pwd = self
            .env_get("PWD")
            .map(ToString::to_string)
            .unwrap_or_else(|| self.cwd.display().to_string());
        self.env_set_internal("PWD", pwd, VAR_EXPORT);
        self.env_set("OPTIND", "1".to_string(), 0);
    }

    /// Import environment from the process, set default variables.
    pub fn populate_env(&mut self) {
        for (k, v) in env::vars() {
            self.env_set(&k, v, VAR_EXPORT);
        }
        self.initialize_defaults();
    }

    /// Check if an option is set.
    pub(super) fn has_option(&self, opt: u32) -> bool {
        (self.options & opt) != 0
    }

    pub(super) fn aliases_snapshot(&self) -> Arc<HashMap<String, String>> {
        Arc::clone(&self.aliases)
    }

    pub(crate) fn set_last_status(&mut self, status: i32) {
        self.last_status = status;
    }

    pub(crate) fn set_exit_code(&mut self, code: i32) {
        self.exit_code = code;
    }

    pub(super) fn set_exit_code_if_unset(&mut self, code: i32) {
        if self.exit_code < 0 {
            self.exit_code = code;
        }
    }

    pub(super) fn set_last_background_pid(&mut self, pid: Option<u32>) {
        self.last_bg_pid = pid;
    }

    pub(super) fn set_current_source(&mut self, source: Option<String>) {
        self.current_source = source;
    }

    pub(super) fn record_expansion_error(&mut self, status: i32) {
        if self.expansion_error.is_none() {
            self.expansion_error = Some(status);
        }
    }

    pub(super) fn take_expansion_error(&mut self) -> Option<i32> {
        self.expansion_error.take()
    }
}