From c4cd1e2c165c4f34ebf67fa9350f8732b2aeca13 Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Tue, 4 Jun 2024 16:25:32 -0600 Subject: Updated the way built-in commands are called/used Previously, built-in commands were fairly primitive, merely outputting STDOUT and STDERR with the print! macros. However, we need them to behave like normal programs, that is: - Acknowledge their verse's meter (forking, piping, etc.), - Ability to capture STDOUT and STDERR (>, 2>), - and Affect the currently running environment. For these reasons, the anthology was reworked, and now contains the Anthology struct, which mimics both std::process::{Child, Command}. The AnthologyStdin helper struct was also created, for built-ins to take input on STDIN, though no built-in is currently using it. Each built-ins' incant functions were updated to return a std::process::Output. It contains output from STDOUT, output from STDERR, and the exit code of the "process". A fix was also implemented for aliases, where the STDOUT and STDERR vectors were not being copied to the newly constructed verse. --- src/poem/anthology/export.rs | 47 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) (limited to 'src/poem/anthology/export.rs') diff --git a/src/poem/anthology/export.rs b/src/poem/anthology/export.rs index aa5a894..35a24ae 100644 --- a/src/poem/anthology/export.rs +++ b/src/poem/anthology/export.rs @@ -1,6 +1,7 @@ -use crate::poem::Verse; use std::collections::BTreeMap; use std::env; +use std::os::unix::process::ExitStatusExt; +use std::process::{ExitStatus, Output}; /// export /// @@ -15,8 +16,11 @@ use std::env; /// ```sh /// export FOO=BAR /// ``` -pub fn incant(verse: &Verse) -> i32 { - match verse.clause() { +pub fn incant(clause: &Option>, uout: bool) -> Output { + let status = 0; + let mut out: Vec = Vec::new(); + let err: Vec = Vec::new(); + match clause { Some(clause) => { for stanza in clause { let (key, val) = match stanza.split_once("=") { @@ -27,14 +31,25 @@ pub fn incant(verse: &Verse) -> i32 { } } None => { + let mut lines = Vec::new(); let sorted: BTreeMap<_, _> = env::vars().into_iter().collect(); for (key, val) in sorted { - println!("{}={}", key, val); + lines.push(format!("{}={}", key, val)); + } + + if uout { + out.append(&mut format!("{}\n", lines.join("\n")).as_bytes().to_vec()); + } else { + println!("{}", lines.join("\n")); } } } - 0 + Output { + status: ExitStatus::from_raw(status), + stdout: out, + stderr: err, + } } /// unset @@ -47,17 +62,29 @@ pub fn incant(verse: &Verse) -> i32 { /// ```sh /// unset FOO /// ``` -pub fn unincant(verse: &Verse) -> i32 { - match verse.clause() { +pub fn unincant(clause: &Option>, uerr: bool) -> Output { + let mut status = 0; + let out: Vec = Vec::new(); + let mut err: Vec = Vec::new(); + match clause { Some(clause) => { for stanza in clause { env::remove_var(stanza); } } None => { - eprintln!("unset: not enough arguments"); - return 1; + status = 1; + if uerr { + err.append(&mut "unset: not enough arguments\n".as_bytes().to_vec()); + } else { + eprintln!("unset: not enough arguments"); + } } } - 0 + + Output { + status: ExitStatus::from_raw(status), + stdout: out, + stderr: err, + } } -- cgit v1.2.3