diff options
author | Rory Dudley | 2024-02-26 23:14:13 -0700 |
---|---|---|
committer | Rory Dudley | 2024-02-26 23:14:13 -0700 |
commit | 0548e74cb3227716cf445f27bd64b8c0b4d0f981 (patch) | |
tree | 569003df69a1a81cbaa859b2bbd7cfb178020d1c /src/recite/ps.rs | |
parent | b6fc81066cfcc29d4519191eae5ba19581ad2774 (diff) | |
download | dwarvish-0548e74cb3227716cf445f27bd64b8c0b4d0f981.tar.gz |
Cleanup recite(), custom errors, fixed forking
First off, moved the giant match statements out of recite(), and into
macros in src/recite/ps.rs. There still needs to be two, since any verse
using the 'couplet' meter will need to redirect its STDOUT. Now the
recite() function returns a Result<(), Mishap>, which can be invoked
when calling the incant_ functions.
Custom errors were added in the form of 'Mishap''s. They are intended to
be returned from the incant_ functions, in the event that something goes
wrong with the Command::spawn() or Child::wait(). They each take a
String, which should be the verb or stanza that was entered by the user.
The incant_ functions separate the functionality of each type of meter
from the recite() function. They return a Result<i32, Mishap>, where
i32 is the exit code of the program that ran, and Mishap is a possible
error.
Before, the shell was cheating at forking a process to the background.
It would actually spawn a thread to wait for that process to finish.
Now, the program simply registers a handler for SIGCHLD, and uses libc's
waitpid() function to reap the child process, and print some output to
the user, indicating that it's finished.
Notes
Notes:
This was a huge patch which did some desperately needed cleanup of the
recite() function. Moving forward, will need to add more documentation,
and will probably scrap the custom errors, since this implementation is
a little half-baked. It's worth looking into in the future, but we can
probably live with io::Error's for the time being.
Fixing forking was a pretty big deal, though. In Linux, and other
u**x-like operating systems, parent processes need to reap their child
processes, otherwise they become zombies. Previously, the dwvsh did this
by spawning a separate thread to wait for child processes that were
forked to the background. Now, we are registering a handle for SIGCHLD,
which is a signal that gets sent to the parent when one of their
children finishes, or is killed. Using waitpid(2), we can determine
which process ended, and do something about it. In the case of a
processes that was forked into the background, when it finished,
waitpid(2) will return its PID. For foreground processes, it returns -1.
Diffstat (limited to 'src/recite/ps.rs')
-rw-r--r-- | src/recite/ps.rs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/recite/ps.rs b/src/recite/ps.rs new file mode 100644 index 0000000..3557505 --- /dev/null +++ b/src/recite/ps.rs @@ -0,0 +1,71 @@ +#[macro_export] +macro_rules! task { + ($verse:expr, $out:expr) => { + if $verse.couplet { + let mut child = Command::new($verse.verb()) + .args($verse.clause()) + .stdin(Stdio::piped()) + .spawn() + .map_err(|e| match e.kind() { + io::ErrorKind::PermissionDenied => Mishap::PermissionDenied($verse.verb()), + _ => Mishap::Else($verse.verb()), + })?; + + let stdin = child + .stdin + .as_mut() + .ok_or(Mishap::BrokenPipe($verse.verb()))?; + stdin + .write_all(&$out.as_bytes()) + .map_err(|_| Mishap::BrokenPipe($verse.verb()))?; + $out.clear(); + + child + } else { + Command::new($verse.verb()) + .args($verse.clause()) + .spawn() + .map_err(|e| match e.kind() { + io::ErrorKind::PermissionDenied => Mishap::PermissionDenied($verse.verb()), + _ => Mishap::Else($verse.verb()), + })? + } + }; +} + +#[macro_export()] +macro_rules! ctask { + ($verse:expr, $out:expr) => { + if $verse.couplet { + let mut child = Command::new($verse.verb()) + .args($verse.clause()) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .map_err(|e| match e.kind() { + io::ErrorKind::PermissionDenied => Mishap::PermissionDenied($verse.verb()), + _ => Mishap::Else($verse.verb()), + })?; + + let stdin = child + .stdin + .as_mut() + .ok_or(Mishap::BrokenPipe($verse.verb()))?; + stdin + .write_all(&$out.as_bytes()) + .map_err(|_| Mishap::BrokenPipe($verse.verb()))?; + $out.clear(); + + child + } else { + Command::new($verse.verb()) + .args($verse.clause()) + .stdout(Stdio::piped()) + .spawn() + .map_err(|e| match e.kind() { + io::ErrorKind::PermissionDenied => Mishap::PermissionDenied($verse.verb()), + _ => Mishap::Else($verse.verb()), + })? + } + }; +} |