From e86e3fb82d7383234c9c64d213b92c84d5c41bb5 Mon Sep 17 00:00:00 2001 From: fox0 <15684995+fox0@users.noreply.github.com> Date: Sat, 26 Oct 2024 18:53:16 +0700 Subject: [PATCH 1/6] misc: Use compile-time macro env! --- misc/Cargo.toml | 4 +++- misc/test.rs | 12 +++++------- misc/tests/false/mod.rs | 2 +- misc/tests/test/mod.rs | 2 +- misc/tests/true/mod.rs | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/misc/Cargo.toml b/misc/Cargo.toml index 933758dd..5c01e72b 100644 --- a/misc/Cargo.toml +++ b/misc/Cargo.toml @@ -8,11 +8,13 @@ edition.workspace = true rust-version.workspace = true [dependencies] -plib = { path = "../plib" } clap.workspace = true libc.workspace = true gettext-rs.workspace = true +[dev-dependencies] +plib = { path = "../plib" } + [lints] workspace = true diff --git a/misc/test.rs b/misc/test.rs index 88c22c77..1e91ded8 100644 --- a/misc/test.rs +++ b/misc/test.rs @@ -11,13 +11,11 @@ // - fix and test unary ops // -use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; -use std::os::unix::fs::FileTypeExt; -use std::os::unix::fs::MetadataExt; -use std::os::unix::fs::PermissionsExt; +use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt}; use std::path::Path; +use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; + /// Unary operators #[allow(clippy::upper_case_acronyms)] #[derive(PartialEq)] @@ -281,8 +279,8 @@ fn eval_binary(s1: &str, op_str: &str, s2: &str) -> bool { fn main() -> Result<(), Box> { setlocale(LocaleCategory::LcAll, ""); - textdomain(PROJECT_NAME)?; - bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; + textdomain(env!("PROJECT_NAME"))?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; let mut args: Vec = std::env::args().collect(); diff --git a/misc/tests/false/mod.rs b/misc/tests/false/mod.rs index e94d0f1e..694ded2f 100644 --- a/misc/tests/false/mod.rs +++ b/misc/tests/false/mod.rs @@ -7,7 +7,7 @@ // SPDX-License-Identifier: MIT // -use plib::{run_test, TestPlan}; +use plib::testing::{run_test, TestPlan}; fn truefalse_test(cmd: &str, expected_exit_code: i32) { run_test(TestPlan { diff --git a/misc/tests/test/mod.rs b/misc/tests/test/mod.rs index cf802d1d..3006e1d2 100644 --- a/misc/tests/test/mod.rs +++ b/misc/tests/test/mod.rs @@ -8,7 +8,7 @@ // SPDX-License-Identifier: MIT // -use plib::{run_test, TestPlan}; +use plib::testing::{run_test, TestPlan}; fn test_test(args: &[&str], expected_code: i32) { let str_args: Vec = args.iter().map(|s| String::from(*s)).collect(); diff --git a/misc/tests/true/mod.rs b/misc/tests/true/mod.rs index 44ddabcd..9d6972bb 100644 --- a/misc/tests/true/mod.rs +++ b/misc/tests/true/mod.rs @@ -7,7 +7,7 @@ // SPDX-License-Identifier: MIT // -use plib::{run_test, TestPlan}; +use plib::testing::{run_test, TestPlan}; fn truefalse_test(cmd: &str, expected_exit_code: i32) { run_test(TestPlan { From a0228ab3add21f6d6e984ac6bbdde12cf30aede8 Mon Sep 17 00:00:00 2001 From: fox0 <15684995+fox0@users.noreply.github.com> Date: Sat, 26 Oct 2024 18:59:23 +0700 Subject: [PATCH 2/6] make: Use compile-time macro env! --- make/Cargo.toml | 4 +++- make/src/main.rs | 28 +++++++++++++--------------- make/tests/integration.rs | 3 ++- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/make/Cargo.toml b/make/Cargo.toml index 3b3d5368..a6bacbe7 100644 --- a/make/Cargo.toml +++ b/make/Cargo.toml @@ -7,13 +7,15 @@ license = "MIT" repository = "https://github.com/rustcoreutils/posixutils-rs.git" [dependencies] -plib = { path = "../plib" } clap.workspace = true libc.workspace = true gettext-rs.workspace = true const_format = "0.2" rowan = "0.15" +[dev-dependencies] +plib = { path = "../plib" } + [[bin]] name = "make" path = "src/main.rs" diff --git a/make/src/main.rs b/make/src/main.rs index 7b81e71a..36ae05d1 100644 --- a/make/src/main.rs +++ b/make/src/main.rs @@ -7,27 +7,24 @@ // SPDX-License-Identifier: MIT // +use core::str::FromStr; +use std::collections::{BTreeMap, BTreeSet}; +use std::ffi::OsString; +use std::io::Read; +use std::path::{Path, PathBuf}; +use std::sync::atomic::Ordering::Relaxed; +use std::{env, fs, io, process}; + use clap::Parser; use const_format::formatcp; -use core::str::FromStr; -use gettextrs::{bind_textdomain_codeset, gettext, textdomain}; -use plib::PROJECT_NAME; +use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; + use posixutils_make::{ config::Config, error_code::ErrorCode::{self, *}, parser::{preprocessor::ENV_MACROS, Makefile}, Make, }; -use std::sync::atomic::Ordering::Relaxed; -use std::{ - collections::{BTreeMap, BTreeSet}, - env, - ffi::OsString, - fs, - io::{self, Read}, - path::{Path, PathBuf}, - process, -}; const MAKEFILE_NAME: [&str; 2] = ["makefile", "Makefile"]; const MAKEFILE_PATH: [&str; 2] = [ @@ -111,8 +108,9 @@ struct Args { } fn main() -> Result<(), Box> { - textdomain(PROJECT_NAME)?; - bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; + setlocale(LocaleCategory::LcAll, ""); + textdomain(env!("PROJECT_NAME"))?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; let Args { directory, diff --git a/make/tests/integration.rs b/make/tests/integration.rs index 0598f95c..9a583df0 100644 --- a/make/tests/integration.rs +++ b/make/tests/integration.rs @@ -12,7 +12,8 @@ use std::fs::{remove_file, File}; use std::io::Write; use std::process::{Child, Command, Stdio}; -use plib::{run_test, run_test_base, TestPlan}; +use plib::testing::{run_test, run_test_base, TestPlan}; + use posixutils_make::error_code::ErrorCode; pub fn run_test_not_comparing_error_message(plan: TestPlan) { From bceb39cc598b51de9a6e69b0640b9d8594cb2b6a Mon Sep 17 00:00:00 2001 From: fox0 <15684995+fox0@users.noreply.github.com> Date: Sat, 26 Oct 2024 19:01:46 +0700 Subject: [PATCH 3/6] fs/make: Use compile-time macro env! --- Cargo.lock | 1 - fs/Cargo.toml | 1 - fs/df.rs | 10 ++--- make/src/main.rs | 104 +++++++++++++++++++++++------------------------ 4 files changed, 56 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f906fb4d..e7b4889e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1379,7 +1379,6 @@ dependencies = [ "clap", "gettext-rs", "libc", - "plib", ] [[package]] diff --git a/fs/Cargo.toml b/fs/Cargo.toml index b7e79efd..82944aa8 100644 --- a/fs/Cargo.toml +++ b/fs/Cargo.toml @@ -8,7 +8,6 @@ edition.workspace = true rust-version.workspace = true [dependencies] -plib = { path = "../plib" } clap.workspace = true gettext-rs.workspace = true libc.workspace = true diff --git a/fs/df.rs b/fs/df.rs index ef23260d..6b37eb2c 100644 --- a/fs/df.rs +++ b/fs/df.rs @@ -15,7 +15,6 @@ use crate::mntent::MountTable; use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; #[cfg(target_os = "macos")] use std::ffi::CStr; use std::{cmp, ffi::CString, fmt::Display, io}; @@ -364,12 +363,11 @@ fn mask_fs_by_file(info: &mut MountList, filename: &str) -> io::Result<()> { } fn main() -> Result<(), Box> { - // parse command line arguments - let args = Args::parse(); - setlocale(LocaleCategory::LcAll, ""); - textdomain(PROJECT_NAME)?; - bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; + textdomain(env!("PROJECT_NAME"))?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; + + let args = Args::parse(); let mut info = read_mount_info()?; diff --git a/make/src/main.rs b/make/src/main.rs index 36ae05d1..b14e770b 100644 --- a/make/src/main.rs +++ b/make/src/main.rs @@ -107,6 +107,58 @@ struct Args { targets: Vec, } +fn print_rules(rules: &BTreeMap>) { + print!("{:?}", rules); +} + +/// Parse the makefile at the given path, or the first default makefile found. +/// If no makefile is found, print an error message and exit. +fn parse_makefile(path: Option>) -> Result { + let path = path.as_ref().map(|p| p.as_ref()); + + let path = match path { + Some(path) => path, + None => { + let mut makefile = None; + for m in MAKEFILE_PATH.iter() { + let path = Path::new(m); + if path.exists() { + makefile = Some(path); + break; + } + } + if let Some(makefile) = makefile { + makefile + } else { + return Err(NoMakefile); + } + } + }; + + let contents = if path == Path::new("-") { + read_stdin()? + } else { + match fs::read_to_string(path) { + Ok(contents) => contents, + Err(err) => { + return Err(IoError(err.kind())); + } + } + }; + + match Makefile::from_str(&contents) { + Ok(makefile) => Ok(makefile), + Err(err) => Err(ErrorCode::ParserError { constraint: err }), + } +} + +/// Reads the makefile from `stdin` until EOF (Ctrl + D) +fn read_stdin() -> Result { + let mut buffer = String::new(); + io::stdin().read_to_string(&mut buffer)?; + Ok(buffer) +} + fn main() -> Result<(), Box> { setlocale(LocaleCategory::LcAll, ""); textdomain(env!("PROJECT_NAME"))?; @@ -229,55 +281,3 @@ fn main() -> Result<(), Box> { } process::exit(status_code); } - -fn print_rules(rules: &BTreeMap>) { - print!("{:?}", rules); -} - -/// Parse the makefile at the given path, or the first default makefile found. -/// If no makefile is found, print an error message and exit. -fn parse_makefile(path: Option>) -> Result { - let path = path.as_ref().map(|p| p.as_ref()); - - let path = match path { - Some(path) => path, - None => { - let mut makefile = None; - for m in MAKEFILE_PATH.iter() { - let path = Path::new(m); - if path.exists() { - makefile = Some(path); - break; - } - } - if let Some(makefile) = makefile { - makefile - } else { - return Err(NoMakefile); - } - } - }; - - let contents = if path == Path::new("-") { - read_stdin()? - } else { - match fs::read_to_string(path) { - Ok(contents) => contents, - Err(err) => { - return Err(IoError(err.kind())); - } - } - }; - - match Makefile::from_str(&contents) { - Ok(makefile) => Ok(makefile), - Err(err) => Err(ErrorCode::ParserError { constraint: err }), - } -} - -/// Reads the makefile from `stdin` until EOF (Ctrl + D) -fn read_stdin() -> Result { - let mut buffer = String::new(); - io::stdin().read_to_string(&mut buffer)?; - Ok(buffer) -} From 48a2c0116b4227c05c803bbf5a61b1cc7eba37ef Mon Sep 17 00:00:00 2001 From: fox0 <15684995+fox0@users.noreply.github.com> Date: Sat, 26 Oct 2024 19:05:28 +0700 Subject: [PATCH 4/6] datetime: Use compile-time macro env! --- datetime/Cargo.toml | 4 +++- datetime/cal.rs | 10 ++++------ datetime/date.rs | 10 ++++------ datetime/sleep.rs | 13 +++++-------- datetime/tests/time/mod.rs | 2 +- datetime/time.rs | 8 +++----- 6 files changed, 20 insertions(+), 27 deletions(-) diff --git a/datetime/Cargo.toml b/datetime/Cargo.toml index 6c85caf0..601b5ba9 100644 --- a/datetime/Cargo.toml +++ b/datetime/Cargo.toml @@ -8,12 +8,14 @@ edition.workspace = true rust-version.workspace = true [dependencies] -plib = { path = "../plib" } clap.workspace = true gettext-rs.workspace = true chrono.workspace = true libc.workspace = true +[dev-dependencies] +plib = { path = "../plib" } + [lints] workspace = true diff --git a/datetime/cal.rs b/datetime/cal.rs index b075909a..22b99292 100644 --- a/datetime/cal.rs +++ b/datetime/cal.rs @@ -14,7 +14,6 @@ use chrono::Datelike; use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; #[derive(Parser)] #[command(version, about = gettext("cal - print a calendar"))] @@ -94,12 +93,11 @@ fn print_year(year: u32) { } fn main() -> Result<(), Box> { - // parse command line arguments - let mut args = Args::parse(); - setlocale(LocaleCategory::LcAll, ""); - textdomain(PROJECT_NAME)?; - bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; + textdomain(env!("PROJECT_NAME"))?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; + + let mut args = Args::parse(); // If no arguments are provided, display the current month if args.month.is_none() && args.year.is_none() { diff --git a/datetime/date.rs b/datetime/date.rs index bbad511d..f7de2181 100644 --- a/datetime/date.rs +++ b/datetime/date.rs @@ -14,7 +14,6 @@ use chrono::{DateTime, Datelike, Local, LocalResult, TimeZone, Utc}; use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; const DEF_TIMESTR: &str = "%a %b %e %H:%M:%S %Z %Y"; @@ -146,12 +145,11 @@ fn set_time(utc: bool, timestr: &str) -> Result<(), &'static str> { } fn main() -> Result<(), Box> { - // parse command line arguments - let args = Args::parse(); - setlocale(LocaleCategory::LcAll, ""); - textdomain(PROJECT_NAME)?; - bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; + textdomain(env!("PROJECT_NAME"))?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; + + let args = Args::parse(); match &args.timestr { None => show_time(args.utc, DEF_TIMESTR), diff --git a/datetime/sleep.rs b/datetime/sleep.rs index 9206a5b1..a1669fa5 100644 --- a/datetime/sleep.rs +++ b/datetime/sleep.rs @@ -9,8 +9,6 @@ use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use libc::{signal, SIGALRM, SIG_IGN}; -use plib::PROJECT_NAME; use std::{thread, time}; #[derive(Parser)] @@ -24,16 +22,15 @@ struct Args { } fn main() -> Result<(), Box> { - // parse command line arguments - let args = Args::parse(); - setlocale(LocaleCategory::LcAll, ""); - textdomain(PROJECT_NAME)?; - bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; + textdomain(env!("PROJECT_NAME"))?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; + + let args = Args::parse(); unsafe { // Ignore the SIGALRM signal - signal(SIGALRM, SIG_IGN); + libc::signal(libc::SIGALRM, libc::SIG_IGN); } thread::sleep(time::Duration::from_secs(args.seconds)); diff --git a/datetime/tests/time/mod.rs b/datetime/tests/time/mod.rs index b480893f..7d4b0e4b 100644 --- a/datetime/tests/time/mod.rs +++ b/datetime/tests/time/mod.rs @@ -12,7 +12,7 @@ use std::{ process::{Command, Output, Stdio}, }; -use plib::TestPlan; +use plib::testing::TestPlan; fn run_test_base(cmd: &str, args: &Vec, stdin_data: &[u8]) -> Output { let relpath = if cfg!(debug_assertions) { diff --git a/datetime/time.rs b/datetime/time.rs index 6bd62c7f..ce293464 100644 --- a/datetime/time.rs +++ b/datetime/time.rs @@ -12,11 +12,9 @@ use std::process::{Command, Stdio}; use std::time::Instant; use clap::Parser; - use gettextrs::{ bind_textdomain_codeset, bindtextdomain, gettext, setlocale, textdomain, LocaleCategory, }; -use plib::PROJECT_NAME; #[derive(Parser)] #[command( @@ -134,9 +132,9 @@ impl Status { fn main() -> Result<(), Box> { setlocale(LocaleCategory::LcAll, ""); - textdomain(PROJECT_NAME)?; - bindtextdomain(PROJECT_NAME, "locale")?; - bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; + textdomain(env!("PROJECT_NAME"))?; + bindtextdomain(env!("PROJECT_NAME"), "locale")?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; let args = Args::parse(); From 1a6247c6594ab4b9aad9dec926e04e8f3057ffcc Mon Sep 17 00:00:00 2001 From: fox0 <15684995+fox0@users.noreply.github.com> Date: Sat, 26 Oct 2024 19:16:22 +0700 Subject: [PATCH 5/6] file: Use compile-time macro env! --- file/cat.rs | 21 +++++++++++---------- file/cmp.rs | 20 ++++++++++---------- file/dd.rs | 8 ++++---- file/file.rs | 23 ++++++++++------------- file/find.rs | 11 ++++++----- file/od.rs | 16 +++++++++------- file/split.rs | 23 ++++++++++++----------- file/tee.rs | 21 ++++++++++----------- 8 files changed, 72 insertions(+), 71 deletions(-) diff --git a/file/cat.rs b/file/cat.rs index 9ed22378..105916c9 100644 --- a/file/cat.rs +++ b/file/cat.rs @@ -12,12 +12,14 @@ // - Questionable behavior: if write_all() produces Err, the program will // continue to the next file, rather than stopping. -use clap::Parser; -use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; use std::io::{self, Read, Write}; use std::path::PathBuf; +use clap::Parser; +use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; +use plib::io::input_stream; +use plib::BUFSZ; + #[derive(Parser)] #[command(version, about = gettext("cat - concatenate and print files"))] struct Args { @@ -34,8 +36,8 @@ struct Args { } fn cat_file(pathname: &PathBuf) -> io::Result<()> { - let mut file = plib::io::input_stream(pathname, true)?; - let mut buffer = [0; plib::BUFSZ]; + let mut file = input_stream(pathname, true)?; + let mut buffer = [0; BUFSZ]; loop { let n_read = file.read(&mut buffer[..])?; @@ -50,12 +52,11 @@ fn cat_file(pathname: &PathBuf) -> io::Result<()> { } fn main() -> Result<(), Box> { - // parse command line arguments - let mut args = Args::parse(); - setlocale(LocaleCategory::LcAll, ""); - textdomain(PROJECT_NAME)?; - bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; + textdomain(env!("PROJECT_NAME"))?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; + + let mut args = Args::parse(); // if no file args, read from stdin if args.files.is_empty() { diff --git a/file/cmp.rs b/file/cmp.rs index 17557fdc..1de6b6c1 100644 --- a/file/cmp.rs +++ b/file/cmp.rs @@ -7,13 +7,14 @@ // SPDX-License-Identifier: MIT // -use clap::Parser; -use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; use std::io::{self, ErrorKind, Read}; use std::path::PathBuf; use std::process::ExitCode; +use clap::Parser; +use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; +use plib::io::input_reader; + #[derive(Parser)] #[command(version, about = gettext("cmp - compare two files"))] struct Args { @@ -76,8 +77,8 @@ fn cmp_main(args: &Args) -> io::Result { return Ok(0); } - let mut reader1 = plib::io::input_reader(&args.file1, true)?; - let mut reader2 = plib::io::input_reader(&args.file2, true)?; + let mut reader1 = input_reader(&args.file1, true)?; + let mut reader2 = input_reader(&args.file2, true)?; let mut lines: u64 = 1; let mut bytes: u64 = 0; @@ -135,12 +136,11 @@ fn cmp_main(args: &Args) -> io::Result { } fn main() -> ExitCode { - let args = Args::parse(); - - // Initialize translation system setlocale(LocaleCategory::LcAll, ""); - textdomain(PROJECT_NAME).unwrap(); - bind_textdomain_codeset(PROJECT_NAME, "UTF-8").unwrap(); + textdomain(env!("PROJECT_NAME")).unwrap(); + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8").unwrap(); + + let mut args = Args::parse(); match cmp_main(&args) { Ok(x) => ExitCode::from(x), diff --git a/file/dd.rs b/file/dd.rs index 8f5797ac..fd3bd0b2 100644 --- a/file/dd.rs +++ b/file/dd.rs @@ -7,11 +7,11 @@ // SPDX-License-Identifier: MIT // -use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; use std::fs; use std::io::{self, Read, Write}; +use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; + const DEF_BLOCK_SIZE: usize = 512; const CONV_ASCII_IBM: [u8; 256] = [ @@ -383,8 +383,8 @@ fn parse_cmdline(args: &[String]) -> Result> fn main() -> Result<(), Box> { setlocale(LocaleCategory::LcAll, ""); - textdomain(PROJECT_NAME)?; - bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; + textdomain(env!("PROJECT_NAME"))?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; let args: Vec = std::env::args().skip(1).collect(); let config = parse_cmdline(&args)?; diff --git a/file/file.rs b/file/file.rs index b9a7a3b0..dae907fd 100644 --- a/file/file.rs +++ b/file/file.rs @@ -9,17 +9,15 @@ mod magic; -use crate::magic::{get_type_from_magic_file_dbs, DEFAULT_MAGIC_FILE}; +use std::fs::read_link; +use std::os::unix::fs::FileTypeExt; +use std::path::PathBuf; +use std::{fs, io}; use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; -use std::{ - fs::{self, read_link}, - io, - os::unix::fs::FileTypeExt, - path::PathBuf, -}; + +use crate::magic::{get_type_from_magic_file_dbs, DEFAULT_MAGIC_FILE}; #[derive(Parser)] #[command( @@ -192,12 +190,11 @@ fn analyze_file(mut path: String, args: &Args, magic_files: &Vec) { } fn main() -> Result<(), Box> { - let args = Args::parse(); - - // Initialize translation system setlocale(LocaleCategory::LcAll, ""); - textdomain(PROJECT_NAME).unwrap(); - bind_textdomain_codeset(PROJECT_NAME, "UTF-8").unwrap(); + textdomain(env!("PROJECT_NAME"))?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; + + let args = Args::parse(); let magic_files = get_magic_files(&args); diff --git a/file/find.rs b/file/find.rs index 7449df8b..8a829b2c 100644 --- a/file/find.rs +++ b/file/find.rs @@ -7,13 +7,13 @@ // SPDX-License-Identifier: MIT // -use gettextrs::{bind_textdomain_codeset, textdomain}; -use plib::PROJECT_NAME; -use regex::Regex; use std::collections::HashSet; use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt}; use std::path::PathBuf; use std::{env, fs}; + +use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory}; +use regex::Regex; use walkdir::{DirEntry, WalkDir}; #[derive(Clone)] @@ -484,8 +484,9 @@ fn find(args: Vec) -> Result<(), String> { } fn main() -> Result<(), Box> { - textdomain(PROJECT_NAME)?; - bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; + setlocale(LocaleCategory::LcAll, ""); + textdomain(env!("PROJECT_NAME"))?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; let args: Vec = env::args().collect(); diff --git a/file/od.rs b/file/od.rs index aa97cf68..a579ccf1 100644 --- a/file/od.rs +++ b/file/od.rs @@ -6,11 +6,7 @@ // file in the root directory of this project. // SPDX-License-Identifier: MIT // -// -use crate::io::ErrorKind; -use clap::Parser; -use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; + use std::fs::File; use std::io::{self, BufReader, Error, Read, Seek, SeekFrom}; use std::num::ParseIntError; @@ -18,6 +14,11 @@ use std::path::PathBuf; use std::slice::Chunks; use std::str::FromStr; +use clap::Parser; +use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; + +use crate::io::ErrorKind; + #[derive(Parser)] #[command(version, about = gettext("od - dump files in octal and other formats"))] struct Args { @@ -1138,10 +1139,11 @@ fn od(args: &Args) -> Result<(), Box> { fn main() -> Result<(), Box> { setlocale(LocaleCategory::LcAll, ""); - textdomain(PROJECT_NAME)?; - bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; + textdomain(env!("PROJECT_NAME"))?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; let mut args = Args::parse(); + args.validate_args()?; let mut exit_code = 0; diff --git a/file/split.rs b/file/split.rs index b1a0288a..656af498 100644 --- a/file/split.rs +++ b/file/split.rs @@ -7,14 +7,16 @@ // SPDX-License-Identifier: MIT // -use clap::Parser; -use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; use std::cmp; use std::fs::{File, OpenOptions}; use std::io::{self, BufRead, Error, ErrorKind, Read, Write}; use std::path::PathBuf; +use clap::Parser; +use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; +use plib::io::{input_reader, input_stream}; +use plib::BUFSZ; + #[derive(Parser)] #[command(version, about = gettext("split - split a file into pieces"))] struct Args { @@ -213,8 +215,8 @@ fn split_by_bytes(args: &Args, bytesplit: String) -> io::Result<()> { }; // open file, or stdin - let mut file = plib::io::input_stream(&args.file, false)?; - let mut raw_buffer = [0; plib::BUFSZ]; + let mut file = input_stream(&args.file, false)?; + let mut raw_buffer = [0; BUFSZ]; let mut state = OutputState::new(&args.prefix, boundary, args.suffix_len); loop { @@ -237,7 +239,7 @@ fn split_by_lines(args: &Args, linesplit: u64) -> io::Result<()> { assert!(linesplit > 0); // open file, or stdin - let mut reader = plib::io::input_reader(&args.file, false)?; + let mut reader = input_reader(&args.file, false)?; let mut state = OutputState::new(&args.prefix, linesplit, args.suffix_len); loop { @@ -258,12 +260,11 @@ fn split_by_lines(args: &Args, linesplit: u64) -> io::Result<()> { } fn main() -> Result<(), Box> { - // parse command line arguments - let mut args = Args::parse(); - setlocale(LocaleCategory::LcAll, ""); - textdomain(PROJECT_NAME)?; - bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; + textdomain(env!("PROJECT_NAME"))?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; + + let mut args = Args::parse(); if args.lines.is_none() && args.bytes.is_none() { args.lines = Some(1000); diff --git a/file/tee.rs b/file/tee.rs index 019fb58b..4213b31e 100644 --- a/file/tee.rs +++ b/file/tee.rs @@ -7,13 +7,13 @@ // SPDX-License-Identifier: MIT // -use clap::Parser; -use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use libc::{signal, SIGINT, SIG_IGN}; -use plib::PROJECT_NAME; use std::fs::{File, OpenOptions}; use std::io::{self, Read, Write}; +use clap::Parser; +use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; +use plib::BUFSZ; + #[derive(Parser)] #[command(version, about = gettext("tee - duplicate standard input"))] struct Args { @@ -65,7 +65,7 @@ fn open_outputs(args: &Args, info: &mut TeeInfo) -> io::Result<()> { } fn tee_stdin(info: &mut TeeInfo) -> io::Result<()> { - let mut buffer = [0; plib::BUFSZ]; + let mut buffer = [0; BUFSZ]; loop { let n_read_res = io::stdin().read(&mut buffer[..]); @@ -93,16 +93,15 @@ fn tee_stdin(info: &mut TeeInfo) -> io::Result<()> { } fn main() -> Result<(), Box> { - // parse command line arguments - let args = Args::parse(); - setlocale(LocaleCategory::LcAll, ""); - textdomain(PROJECT_NAME)?; - bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; + textdomain(env!("PROJECT_NAME"))?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; + + let args = Args::parse(); if args.ignore { unsafe { - signal(SIGINT, SIG_IGN); + libc::signal(libc::SIGINT, libc::SIG_IGN); } } From 55346ceac3979aa0e196013ec299ff8595e65c0b Mon Sep 17 00:00:00 2001 From: fox0 <15684995+fox0@users.noreply.github.com> Date: Sat, 26 Oct 2024 19:20:22 +0700 Subject: [PATCH 6/6] plib: remove PROJECT_NAME --- file/cmp.rs | 2 +- file/tests/cmp/mod.rs | 2 +- file/tests/dd/mod.rs | 2 +- file/tests/file/mod.rs | 2 +- file/tests/find/mod.rs | 2 +- file/tests/od/mod.rs | 2 +- plib/src/lib.rs | 4 ---- 7 files changed, 6 insertions(+), 10 deletions(-) diff --git a/file/cmp.rs b/file/cmp.rs index 1de6b6c1..d1638776 100644 --- a/file/cmp.rs +++ b/file/cmp.rs @@ -140,7 +140,7 @@ fn main() -> ExitCode { textdomain(env!("PROJECT_NAME")).unwrap(); bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8").unwrap(); - let mut args = Args::parse(); + let args = Args::parse(); match cmp_main(&args) { Ok(x) => ExitCode::from(x), diff --git a/file/tests/cmp/mod.rs b/file/tests/cmp/mod.rs index b20c7757..badee5a3 100644 --- a/file/tests/cmp/mod.rs +++ b/file/tests/cmp/mod.rs @@ -7,7 +7,7 @@ // SPDX-License-Identifier: MIT // -use plib::{run_test, TestPlan}; +use plib::testing::{run_test, TestPlan}; fn run_test_helper( args: &[&str], diff --git a/file/tests/dd/mod.rs b/file/tests/dd/mod.rs index 4c9298bf..ff801e0c 100644 --- a/file/tests/dd/mod.rs +++ b/file/tests/dd/mod.rs @@ -2,7 +2,7 @@ use std::fs::File; use std::io::Read; use std::path::PathBuf; -use plib::{run_test_u8, TestPlanU8}; +use plib::testing::{run_test_u8, TestPlanU8}; fn get_test_file_path(filename: &str) -> PathBuf { let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); diff --git a/file/tests/file/mod.rs b/file/tests/file/mod.rs index aa0e5cfd..5a9f7ba7 100644 --- a/file/tests/file/mod.rs +++ b/file/tests/file/mod.rs @@ -9,7 +9,7 @@ use std::{env, path::PathBuf}; -use plib::{run_test, TestPlan}; +use plib::testing::{run_test, TestPlan}; fn file_test(args: &[&str], expected_output: &str, expected_error: &str) { let str_args: Vec = args.iter().map(|s| String::from(*s)).collect(); diff --git a/file/tests/find/mod.rs b/file/tests/find/mod.rs index 8901ffa9..6d413860 100644 --- a/file/tests/find/mod.rs +++ b/file/tests/find/mod.rs @@ -1,7 +1,7 @@ use std::fs::{remove_file, File}; use std::io::Write; -use plib::{run_test, TestPlan}; +use plib::testing::{run_test, TestPlan}; fn run_test_find( args: &[&str], diff --git a/file/tests/od/mod.rs b/file/tests/od/mod.rs index a5b1cd07..cdfdf689 100644 --- a/file/tests/od/mod.rs +++ b/file/tests/od/mod.rs @@ -7,7 +7,7 @@ // SPDX-License-Identifier: MIT // -use plib::{run_test, TestPlan}; +use plib::testing::{run_test, TestPlan}; fn od_test(args: &[&str], test_data: &str, expected_output: &str) { let str_args: Vec = args.iter().map(|s| String::from(*s)).collect(); diff --git a/plib/src/lib.rs b/plib/src/lib.rs index 81f71675..de06edd6 100644 --- a/plib/src/lib.rs +++ b/plib/src/lib.rs @@ -18,8 +18,4 @@ pub mod sccsfile; pub mod testing; pub mod utmpx; -pub const PROJECT_NAME: &str = "posixutils-rs"; - pub const BUFSZ: usize = 8 * 1024; - -pub use testing::*;