From 0548e74cb3227716cf445f27bd64b8c0b4d0f981 Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Mon, 26 Feb 2024 23:14:13 -0700 Subject: 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, 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. --- Cargo.lock | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'Cargo.lock') diff --git a/Cargo.lock b/Cargo.lock index 5e36732..7654911 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,7 +6,9 @@ version = 3 name = "dwarvish" version = "0.0.0" dependencies = [ + "libc", "signal-hook", + "thiserror", ] [[package]] @@ -15,6 +17,24 @@ version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +[[package]] +name = "proc-macro2" +version = "1.0.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +dependencies = [ + "proc-macro2", +] + [[package]] name = "signal-hook" version = "0.3.17" @@ -33,3 +53,40 @@ checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" dependencies = [ "libc", ] + +[[package]] +name = "syn" +version = "2.0.51" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ab617d94515e94ae53b8406c628598680aa0c9587474ecbe58188f7b345d66c" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e45bcbe8ed29775f228095caf2cd67af7a4ccf756ebff23a306bf3e8b47b24b" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" -- cgit v1.2.3