diff options
author | Rory Dudley | 2024-05-19 18:50:06 -0600 |
---|---|---|
committer | Rory Dudley | 2024-05-19 18:50:06 -0600 |
commit | 4b1b8061e79b42128df4f06fd1e439549bf9696b (patch) | |
tree | 80db43cf7295937751d61435fb4e60118b8a3ea9 /src/poem/recite/ps.rs | |
parent | 8756d3e7512c1416cc15a688c62b8f51f030b192 (diff) | |
download | dwarvish-4b1b8061e79b42128df4f06fd1e439549bf9696b.tar.gz |
Handle STDERR, in addition to STDOUT
This patch overhauls the reading and reciting of verses, such that the
redirection of STDERR (in addition to STDOUT, which was already a
feature), is now possible.
Removed the 'stdout' argument from recite(), since it is no longer
needed with how incantations function.
A verse's couplet indicator is now a u8, instead of a bool, with certain
values corresponding to types of couplets, for instance:
ls | grep Ca | lolcat
^ ^ ^
| | 2: right side of a couplet
| 3: both sides of a couplet
1: left side of a couplet
Incantions are no longer hanlded in rune.rs, and the task macros have
been removed. Now, a verse incants itself, matching on its own meter to
determine how to handle the next verse.
The following runes were added to help with handling STDERR:
Write2 -> 2>
WriteAll -> &>
Addendum2 -> 2>>
AddendumAll -> &>>
The 'io' field in verse was changed from an Option<Rune>, to an array of
Runes, since a single verse might have multiple IO operations.
The following fields were added to Verse, to assist with handling
STDERR:
ip -> List of filenames to read into STDIN
op -> List of filenames to send STDOUT to
ep -> List of filenames to send STDERR to
Keep track of channels when reading a poem. Channels are relating to IO
operations. If channel is None, words get pushed to the verse's primary
stanza (i.e. the verb or the clause). If a channel is selected, words
are pushed to one of the aforementioned new fields in Verse.
Read -> ip
Write/Addedum -> op
Write2/Addedum2 -> ep
WriteAll/AddendumAll -> op and ep
Notes
Notes:
This commit also added tests for the new Runes.
Diffstat (limited to 'src/poem/recite/ps.rs')
-rw-r--r-- | src/poem/recite/ps.rs | 134 |
1 files changed, 0 insertions, 134 deletions
diff --git a/src/poem/recite/ps.rs b/src/poem/recite/ps.rs deleted file mode 100644 index 5700ae8..0000000 --- a/src/poem/recite/ps.rs +++ /dev/null @@ -1,134 +0,0 @@ -/// Fork into a process from a Verse -/// -/// Figures out whether or not the given Verse is a couplet. If it is, fork -/// into a process, and pipe the contents of out `out` into STDIN. If not, then -/// simply fork into the process. -/// -/// # Arguments -/// * `$verse: &Verse` - The verse to fork into -/// * `$out: &mut String` - If the $verse is a couplet, the contents of STDOUT from the last verse -#[macro_export] -macro_rules! task { - ($verse:expr, $out:expr) => { - if $verse.couplet { - let mut child = Command::new($verse.verb()) - .args($verse.clause().unwrap_or(vec![])) - .stdin(Stdio::piped()) - .spawn()?; - - let stdin = child.stdin.as_mut().ok_or(io::ErrorKind::BrokenPipe)?; - stdin.write_all(&$out)?; - $out.clear(); - - child - } else { - Command::new($verse.verb()) - .args($verse.clause().unwrap_or(vec![])) - .spawn()? - } - }; -} - -/// Fork into a process from a Verse, and capture STDOUT -/// -/// Figures out whether or not the given Verse is a couplet. If it is, fork -/// into a process, and pipe the contents of out `out` into STDIN. If not, then -/// simply fork into the process. Additionally, this function will capture -/// STDOUT of the process specified by the Verse, and store it in `out`. -/// -/// # Arguments -/// * `$verse: &Verse` - The verse to fork into -/// * `$out: &mut String` - If the $verse is a couplet, the contents of STDOUT from the last verse -#[macro_export] -macro_rules! ctask { - ($verse:expr, $out:expr) => { - if $verse.couplet { - let mut child = Command::new($verse.verb()) - .args($verse.clause().unwrap_or(vec![])) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn()?; - - let stdin = child.stdin.as_mut().ok_or(io::ErrorKind::BrokenPipe)?; - stdin.write_all(&$out)?; - $out.clear(); - - child - } else { - Command::new($verse.verb()) - .args($verse.clause().unwrap_or(vec![])) - .stdout(Stdio::piped()) - .spawn()? - } - }; -} - -/// Fork into a background process from a Verse -/// -/// Figures out whether or not the given Verse is a couplet. If it is, fork -/// into a backgournd process, and pipe the contents of out `out` into STDIN. -/// If not, then simply fork into the background process. -/// -/// # Arguments -/// * `$verse: &Verse` - The verse to fork into -/// * `$out: &mut String` - If the $verse is a couplet, the contents of STDOUT from the last verse -#[macro_export] -macro_rules! btask { - ($verse:expr, $out:expr) => { - if $verse.couplet { - let mut child = Command::new($verse.verb()) - .args($verse.clause().unwrap_or(vec![])) - .stdin(Stdio::piped()) - .process_group(0) - .spawn()?; - - let stdin = child.stdin.as_mut().ok_or(io::ErrorKind::BrokenPipe)?; - stdin.write_all(&$out)?; - $out.clear(); - - child - } else { - Command::new($verse.verb()) - .args($verse.clause().unwrap_or(vec![])) - .process_group(0) - .spawn()? - } - }; -} - -/// Fork into a background process from a Verse, and capture STDOUT -/// -/// Figures out whether or not the given Verse is a couplet. If it is, fork -/// into a backgournd process, and pipe the contents of out `out` into STDIN. -/// If not, then simply fork into the background process. This captures the -/// output of STDOUT, in order to redirect it to a file when the program -/// finishes running. -/// -/// # Arguments -/// * `$verse: &Verse` - The verse to fork into -/// * `$out: &mut String` - If the $verse is a couplet, the contents of STDOUT from the last verse -#[macro_export] -macro_rules! iobtask { - ($verse:expr, $out:expr) => { - if $verse.couplet { - let mut child = Command::new($verse.verb()) - .args($verse.clause().unwrap_or(vec![])) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .process_group(0) - .spawn()?; - - let stdin = child.stdin.as_mut().ok_or(io::ErrorKind::BrokenPipe)?; - stdin.write_all(&$out)?; - $out.clear(); - - child - } else { - Command::new($verse.verb()) - .args($verse.clause().unwrap_or(vec![])) - .stdout(Stdio::piped()) - .process_group(0) - .spawn()? - } - }; -} |