From 71490f7421d6eaebcd13380b00fac8f83797616a Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 11 Apr 2023 18:12:56 -0400 Subject: [PATCH 1/5] basic argument parsing --- Cargo.lock | 230 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/main.rs | 36 +++++++- 3 files changed, 265 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5cd0555..cab7774 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,12 +2,54 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "arg" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "568bbed0b9810cd4b43c8560ff3c5d2318b29d78fc314818d156bbed7075d394" +dependencies = [ + "arg-derive", +] + +[[package]] +name = "arg-derive" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "849efc162c06e51ce911bf4fe702f62a08f3b6ebbbfc5178e86c6bae449c2c60" +dependencies = [ + "quote", + "syn", + "tabwriter", +] + [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "bindgen" +version = "0.63.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36d860121800b2a9a94f9b5604b332d5cffb234ce17609ea479d723dbc9d3885" +dependencies = [ + "bitflags", + "cexpr", + "clang-sys", + "lazy_static", + "lazycell", + "log", + "peeking_take_while", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex", + "syn", + "which", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -18,8 +60,19 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" name = "breed" version = "0.1.0" dependencies = [ + "arg", "crossterm", "ropey", + "yacexits", +] + +[[package]] +name = "cexpr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" +dependencies = [ + "nom", ] [[package]] @@ -28,6 +81,17 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clang-sys" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c688fc74432808e3eb684cae8830a86be1d66a2bd58e1f248ed0960a590baf6f" +dependencies = [ + "glob", + "libc", + "libloading", +] + [[package]] name = "crossterm" version = "0.26.1" @@ -53,12 +117,46 @@ dependencies = [ "winapi", ] +[[package]] +name = "either" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" + +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "lazycell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" + [[package]] name = "libc" version = "0.2.141" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" +[[package]] +name = "libloading" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f" +dependencies = [ + "cfg-if", + "winapi", +] + [[package]] name = "lock_api" version = "0.4.9" @@ -78,6 +176,18 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "mio" version = "0.8.6" @@ -90,6 +200,22 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "once_cell" +version = "1.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" + [[package]] name = "parking_lot" version = "0.12.1" @@ -113,6 +239,30 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "peeking_take_while" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" + +[[package]] +name = "proc-macro2" +version = "1.0.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" +dependencies = [ + "proc-macro2", +] + [[package]] name = "redox_syscall" version = "0.2.16" @@ -122,6 +272,21 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b1f693b24f6ac912f4893ef08244d70b6067480d2f1a46e950c9691e6749d1d" +dependencies = [ + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + [[package]] name = "ropey" version = "1.6.0" @@ -132,12 +297,24 @@ dependencies = [ "str_indices", ] +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + [[package]] name = "scopeguard" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "shlex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" + [[package]] name = "signal-hook" version = "0.3.15" @@ -180,12 +357,55 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f026164926842ec52deb1938fae44f83dfdb82d0a5b0270c5bd5935ab74d6dd" +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tabwriter" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36205cfc997faadcc4b0b87aaef3fbedafe20d38d4959a7ca6ff803564051111" +dependencies = [ + "unicode-width", +] + +[[package]] +name = "unicode-ident" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" + +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "which" +version = "4.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2441c784c52b289a054b7201fc93253e288f094e2f4be9058343127c4226a269" +dependencies = [ + "either", + "libc", + "once_cell", +] + [[package]] name = "winapi" version = "0.3.9" @@ -273,3 +493,13 @@ name = "windows_x86_64_msvc" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" + +[[package]] +name = "yacexits" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53fe740dd05c1bbc919431e842e6c1bea30195e0518ae99cae35b7f0730ddc18" +dependencies = [ + "bindgen", + "libc", +] diff --git a/Cargo.toml b/Cargo.toml index f8a2bcf..9dc3429 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,5 +5,7 @@ edition = "2021" license = "AGPL-3.0-or-later" [dependencies] +arg = "0.4.1" crossterm = "0.26" ropey = "1.6" +yacexits = "0.1.5" diff --git a/src/main.rs b/src/main.rs index 3c15c53..65ce734 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ /* * Copyright (c) 2023 Marceline Cramer + * Copyright (c) 2023 Emma Tebibyte * SPDX-License-Identifier: AGPL-3.0-or-later * * This program is free software: you can redistribute it and/or modify it under @@ -16,7 +17,16 @@ * along with this program. If not, see https://www.gnu.org/licenses/. */ -use std::io::{stdout, Stdout, Write}; +use std::{ + env::args, + io::{ + Read, + stdin, + stdout, + Stdout, + Write, + }, +}; use crossterm::{ cursor, @@ -26,6 +36,7 @@ use crossterm::{ Result }; use ropey::Rope; +use yacexits::{ exit, EX_DATAERR, EX_UNAVAILABLE }; struct Buffer { pub text: Rope, @@ -341,8 +352,27 @@ fn screen_main(stdout: &mut Stdout, mut state: State) -> Result<()> { } fn main() -> Result<()> { - let text = include_str!("main.rs"); - let state = State::from_str(text)?; + let argv = args().collect::>(); + let text: String; + + if !argv.get(1).is_some() { + text = "".to_string(); + } else { + let mut buf = Vec::new(); + std::fs::File::open(argv[1].clone()).unwrap_or_else(|_| { + eprintln!("{}: {}: No such file.", argv[0], argv[1]); + exit(EX_UNAVAILABLE); + }).read_to_end(&mut buf).unwrap(); + + text = String::from_utf8(buf).unwrap_or_else(|_| { + eprintln!( + "{}: {}: File contents are invalid UTF-8.", argv[0], argv[1] + ); + exit(EX_DATAERR); + }); + } + + let state = State::from_str(&text)?; let mut stdout = stdout(); terminal::enable_raw_mode()?; stdout.execute(terminal::EnterAlternateScreen)?; From 1e69ae5e986daf9f12243b72980f3299217bf108 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 11 Apr 2023 18:26:02 -0400 Subject: [PATCH 2/5] better argument parsing --- src/main.rs | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 65ce734..a97ac07 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,13 +19,14 @@ use std::{ env::args, + fs::File, io::{ Read, - stdin, stdout, Stdout, Write, }, + os::fd::FromRawFd, }; use crossterm::{ @@ -353,24 +354,34 @@ fn screen_main(stdout: &mut Stdout, mut state: State) -> Result<()> { fn main() -> Result<()> { let argv = args().collect::>(); + let mut buf = Vec::new(); let text: String; - if !argv.get(1).is_some() { - text = "".to_string(); - } else { - let mut buf = Vec::new(); - std::fs::File::open(argv[1].clone()).unwrap_or_else(|_| { - eprintln!("{}: {}: No such file.", argv[0], argv[1]); - exit(EX_UNAVAILABLE); - }).read_to_end(&mut buf).unwrap(); + text = match argv.get(1) { + Some(path) => { + match path.as_str() { + "-" => unsafe { File::from_raw_fd(0) }, + _ => { + std::fs::File::open(path).unwrap_or_else(|_| { + eprintln!( + "{}: {}: No such file or directory.", + argv[0], + argv[1] + ); + exit(EX_UNAVAILABLE); + }) + }, + }.read_to_end(&mut buf).unwrap(); - text = String::from_utf8(buf).unwrap_or_else(|_| { - eprintln!( - "{}: {}: File contents are invalid UTF-8.", argv[0], argv[1] - ); - exit(EX_DATAERR); - }); - } + String::from_utf8(buf).unwrap_or_else(|_| { + eprintln!( + "{}: {}: File contents are invalid UTF-8.", argv[0], argv[1] + ); + exit(EX_DATAERR); + }) + }, + None => "".to_string(), + }; let state = State::from_str(&text)?; let mut stdout = stdout(); From 1ef228faa2aa9d4f842f893789650b2b1a7e3e45 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 11 Apr 2023 18:49:11 -0400 Subject: [PATCH 3/5] EVEN BETTER argument parsing --- src/main.rs | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index a97ac07..9b4ef62 100644 --- a/src/main.rs +++ b/src/main.rs @@ -355,33 +355,27 @@ fn screen_main(stdout: &mut Stdout, mut state: State) -> Result<()> { fn main() -> Result<()> { let argv = args().collect::>(); let mut buf = Vec::new(); - let text: String; - text = match argv.get(1) { + match argv.get(1).map(|s| s.as_str()) { + Some("-") | None => unsafe { File::from_raw_fd(0) }, Some(path) => { - match path.as_str() { - "-" => unsafe { File::from_raw_fd(0) }, - _ => { - std::fs::File::open(path).unwrap_or_else(|_| { - eprintln!( - "{}: {}: No such file or directory.", - argv[0], - argv[1] - ); - exit(EX_UNAVAILABLE); - }) - }, - }.read_to_end(&mut buf).unwrap(); - - String::from_utf8(buf).unwrap_or_else(|_| { + std::fs::File::open(path).unwrap_or_else(|_| { eprintln!( - "{}: {}: File contents are invalid UTF-8.", argv[0], argv[1] + "{}: {}: No such file or directory.", + argv[0], + argv[1] ); - exit(EX_DATAERR); + exit(EX_UNAVAILABLE); }) }, - None => "".to_string(), - }; + }.read_to_end(&mut buf).unwrap(); + + let text = String::from_utf8(buf).unwrap_or_else(|_| { + eprintln!( + "{}: {}: File contents are invalid UTF-8.", argv[0], argv[1] + ); + exit(EX_DATAERR); + }); let state = State::from_str(&text)?; let mut stdout = stdout(); From 6bde2a1eec5768342ec4d5eb56a516c01c3f1c10 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 11 Apr 2023 18:51:33 -0400 Subject: [PATCH 4/5] Changed wording --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 9b4ef62..e50950f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -372,7 +372,7 @@ fn main() -> Result<()> { let text = String::from_utf8(buf).unwrap_or_else(|_| { eprintln!( - "{}: {}: File contents are invalid UTF-8.", argv[0], argv[1] + "{}: {}: File contents are not valid UTF-8.", argv[0], argv[1] ); exit(EX_DATAERR); }); From 31d9cdf266988f40ebec8a9e29164c8685823d83 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 11 Apr 2023 18:54:33 -0400 Subject: [PATCH 5/5] Added comment explaining stdin as a file descriptor --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index e50950f..0ef1d36 100644 --- a/src/main.rs +++ b/src/main.rs @@ -357,7 +357,7 @@ fn main() -> Result<()> { let mut buf = Vec::new(); match argv.get(1).map(|s| s.as_str()) { - Some("-") | None => unsafe { File::from_raw_fd(0) }, + Some("-") | None => unsafe { File::from_raw_fd(0) }, // stdin as a file Some(path) => { std::fs::File::open(path).unwrap_or_else(|_| { eprintln!(