From fa5d1944637ff2d3b5970233e56ee821b447e76f Mon Sep 17 00:00:00 2001 From: fox0 <15684995+fox0@users.noreply.github.com> Date: Fri, 25 Oct 2024 20:07:07 +0700 Subject: [PATCH 1/6] sys: Use compile-time macro env! --- sys/getconf.rs | 16 +++++++--------- sys/ipcrm.rs | 17 ++++++++--------- sys/ipcs.rs | 7 ++----- sys/ps.rs | 15 ++++++++++++--- sys/uname.rs | 10 ++++------ sys/who.rs | 27 ++++++++++++++------------- 6 files changed, 47 insertions(+), 45 deletions(-) diff --git a/sys/getconf.rs b/sys/getconf.rs index 5cddd7f8..1251d02b 100644 --- a/sys/getconf.rs +++ b/sys/getconf.rs @@ -12,12 +12,12 @@ // - Proper -v specification support. is it even necessary? // +use std::collections::HashMap; +use std::ffi::CString; + use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; use libc::{pathconf, sysconf}; -use plib::PROJECT_NAME; -use std::collections::HashMap; -use std::ffi::CString; #[derive(Parser)] #[command(version, about = gettext("getconf - get configuration values"))] @@ -324,13 +324,11 @@ fn load_pathconf_mapping() -> HashMap<&'static str, libc::c_int> { } fn main() -> Result<(), Box> { - // Parse command line arguments - let args = Args::parse(); - - // Set locale and text domain for localization 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 let Some(pathname) = args.pathname { let pathconf_mappings = load_pathconf_mapping(); diff --git a/sys/ipcrm.rs b/sys/ipcrm.rs index be844583..a402c3e6 100644 --- a/sys/ipcrm.rs +++ b/sys/ipcrm.rs @@ -7,15 +7,15 @@ // SPDX-License-Identifier: MIT // +use std::ffi::{c_int, c_ushort}; +use std::io::{self, Error, ErrorKind}; +use std::ptr; + use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; #[cfg(not(target_os = "macos"))] use libc::{msgctl, msgget, msqid_ds}; use libc::{semctl, semget, shmctl, shmget, shmid_ds}; -use plib::PROJECT_NAME; -use std::ffi::{c_int, c_ushort}; -use std::io::{self, Error, ErrorKind}; -use std::ptr; #[derive(Parser)] #[command( @@ -197,12 +197,11 @@ fn remove_ipcs(args: &Args) -> 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 exit_code = 0; diff --git a/sys/ipcs.rs b/sys/ipcs.rs index d5208cbd..8bf76de9 100644 --- a/sys/ipcs.rs +++ b/sys/ipcs.rs @@ -10,7 +10,6 @@ use chrono::Local; use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; /// ipcs - report XSI interprocess communication facilities status #[derive(Parser)] @@ -260,12 +259,10 @@ fn display_ipc_status(args: &Args) { } fn main() -> Result<(), Box> { - // Set locale and text domain for localization 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")?; - // Parse command line arguments let mut args = Args::parse(); // Validate arguments and determine what to display diff --git a/sys/ps.rs b/sys/ps.rs index 64455210..a3fd2253 100644 --- a/sys/ps.rs +++ b/sys/ps.rs @@ -13,9 +13,11 @@ mod psmacos; #[cfg(target_os = "linux")] mod pslinux; -use clap::Parser; use std::collections::HashMap; +use clap::Parser; +use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory}; + #[cfg(target_os = "macos")] mod platform { pub use crate::psmacos::*; @@ -98,8 +100,13 @@ fn posix_field_map() -> HashMap<&'static str, &'static str> { ]) } -fn main() { +fn main() -> Result<(), Box> { + setlocale(LocaleCategory::LcAll, ""); + textdomain(env!("PROJECT_NAME"))?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; + let mut args = Args::parse(); + if args.all2 { args.all = true; } @@ -108,7 +115,7 @@ fn main() { Ok(processes) => processes, Err(e) => { eprintln!("Error: {}", e); - return; + return Ok(()); } }; @@ -170,4 +177,6 @@ fn main() { } println!(); } + + Ok(()) } diff --git a/sys/uname.rs b/sys/uname.rs index 33fe0550..e842d3aa 100644 --- a/sys/uname.rs +++ b/sys/uname.rs @@ -9,7 +9,6 @@ use clap::Parser; use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; /// uname - return system name #[derive(Parser)] @@ -63,7 +62,10 @@ fn print_info(args: &Args, info: uname::Info) { } fn main() -> Result<(), Box> { - // parse command line arguments + setlocale(LocaleCategory::LcAll, ""); + textdomain(env!("PROJECT_NAME"))?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; + let mut args = Args::parse(); if args.all { @@ -76,10 +78,6 @@ fn main() -> Result<(), Box> { args.system = true; } - setlocale(LocaleCategory::LcAll, ""); - textdomain(PROJECT_NAME)?; - bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; - let mut exit_code = 0; match uname::uname() { diff --git a/sys/who.rs b/sys/who.rs index 68ed546d..37b22a4c 100644 --- a/sys/who.rs +++ b/sys/who.rs @@ -11,10 +11,12 @@ // - implement -T, -u options // +use std::path::PathBuf; + use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::{platform, PROJECT_NAME}; -use std::path::PathBuf; +use plib::utmpx::Utmpx; +use plib::{curuser, platform, utmpx}; /// who - display who is on the system #[derive(Parser)] @@ -86,7 +88,7 @@ fn fmt_timestamp(ts: libc::time_t) -> String { dt.format("%b %e %H:%M").to_string() } -fn print_fmt_short(entry: &plib::utmpx::Utmpx, line: &str) { +fn print_fmt_short(entry: &Utmpx, line: &str) { println!( "{:<16} {:<12} {}", entry.user, @@ -95,7 +97,7 @@ fn print_fmt_short(entry: &plib::utmpx::Utmpx, line: &str) { ); } -fn print_fmt_term(entry: &plib::utmpx::Utmpx, line: &str) { +fn print_fmt_term(entry: &Utmpx, line: &str) { let term_state = '?'; println!( "{:<16} {} {:<12} {}", @@ -107,7 +109,7 @@ fn print_fmt_term(entry: &plib::utmpx::Utmpx, line: &str) { } fn current_terminal() -> String { - let s = plib::curuser::tty(); + let s = curuser::tty(); if let Some(st) = s.strip_prefix("/dev/") { st.to_owned() } else { @@ -115,7 +117,7 @@ fn current_terminal() -> String { } } -fn print_entry(args: &Args, entry: &plib::utmpx::Utmpx) { +fn print_entry(args: &Args, entry: &Utmpx) { // Skip if current_terminal option is set and this entry is not for the current terminal if args.current_terminal { let current_tty = current_terminal(); @@ -161,7 +163,7 @@ fn show_utmpx_entries(args: &Args) { ); } - let entries = plib::utmpx::load(); + let entries = utmpx::load(); for entry in &entries { print_entry(args, entry); } @@ -169,7 +171,7 @@ fn show_utmpx_entries(args: &Args) { fn show_utmpx_summary() { let mut count = 0; - let entries = plib::utmpx::load(); + let entries = utmpx::load(); for entry in &entries { if !entry.user.is_empty() { println!("{}", entry.user); @@ -181,7 +183,10 @@ fn show_utmpx_summary() { } fn main() -> Result<(), Box> { - // manual CLI parse for special "who am i" case + setlocale(LocaleCategory::LcAll, ""); + textdomain(env!("PROJECT_NAME"))?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; + let args: Vec = std::env::args().skip(1).collect(); let am_i = args.len() == 2 && args[0] == "am" && (args[1] == "i" || args[1] == "I"); @@ -207,10 +212,6 @@ fn main() -> Result<(), Box> { args.userproc = true; } - setlocale(LocaleCategory::LcAll, ""); - textdomain(PROJECT_NAME)?; - bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; - let mut exit_code = 0; if args.file.is_some() { From 4d2a359b6e962e7cc7feb6702689b4535c9f6a6f Mon Sep 17 00:00:00 2001 From: fox0 <15684995+fox0@users.noreply.github.com> Date: Fri, 25 Oct 2024 20:09:44 +0700 Subject: [PATCH 2/6] screen: Use compile-time macro env! --- Cargo.lock | 1 - screen/Cargo.toml | 2 -- screen/stty.rs | 15 +++++++-------- screen/tabs.rs | 13 ++++++------- screen/tput.rs | 13 ++++++------- 5 files changed, 19 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9389edf9..8bea94fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1442,7 +1442,6 @@ dependencies = [ "clap", "gettext-rs", "libc", - "plib", "terminfo", "termios", ] diff --git a/screen/Cargo.toml b/screen/Cargo.toml index 2c0d7774..42027c8d 100644 --- a/screen/Cargo.toml +++ b/screen/Cargo.toml @@ -8,7 +8,6 @@ edition.workspace = true rust-version.workspace = true [dependencies] -plib = { path = "../plib" } clap.workspace = true gettext-rs.workspace = true terminfo = "0.8" @@ -29,4 +28,3 @@ path = "./tabs.rs" [[bin]] name = "tput" path = "./tput.rs" - diff --git a/screen/stty.rs b/screen/stty.rs index b98da5aa..9ad8ad69 100644 --- a/screen/stty.rs +++ b/screen/stty.rs @@ -12,12 +12,12 @@ mod osdata; +use std::collections::HashMap; +use std::io::{self, Error, ErrorKind}; + use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; use osdata::{ParamType, PARG, PNEG}; -use plib::PROJECT_NAME; -use std::collections::HashMap; -use std::io::{self, Error, ErrorKind}; use termios::{ cc_t, cfgetispeed, cfgetospeed, cfsetispeed, cfsetospeed, speed_t, tcflag_t, tcsetattr, Termios, TCSANOW, @@ -552,12 +552,11 @@ fn stty_set(ti: Termios, args: &Args) -> 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(); // load termio settings let ti = Termios::from_fd(libc::STDIN_FILENO)?; diff --git a/screen/tabs.rs b/screen/tabs.rs index 22da6c3a..5f4123af 100644 --- a/screen/tabs.rs +++ b/screen/tabs.rs @@ -12,10 +12,10 @@ // - gettext("Specify repetitive tab stops separated by ({}) columns") // +use std::io::{self, Error, ErrorKind, Write}; + use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; -use std::io::{self, Error, ErrorKind, Write}; use terminfo::{capability as cap, Database}; // arbitrarily chosen. todo: search if POSIX-ly correct. @@ -210,12 +210,11 @@ fn set_hw_tabs(info: &Database, tabstops: &Vec) -> 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 info = match args.term { None => Database::from_env().unwrap(), diff --git a/screen/tput.rs b/screen/tput.rs index 06b49b44..0e31ed4e 100644 --- a/screen/tput.rs +++ b/screen/tput.rs @@ -11,10 +11,10 @@ // - read init-file and reset-file data from filesystem // +use std::io; + use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; -use std::io; use terminfo::{capability as cap, Database}; #[derive(Parser)] @@ -72,12 +72,11 @@ fn tput_clear(info: Database) -> terminfo::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 info = match args.term { None => Database::from_env().unwrap(), From 8a06a6c92c336319a93b8c9333aa8a1117b1b314 Mon Sep 17 00:00:00 2001 From: fox0 <15684995+fox0@users.noreply.github.com> Date: Fri, 25 Oct 2024 20:11:27 +0700 Subject: [PATCH 3/6] sccs: Use compile-time macro env! --- sccs/Cargo.toml | 5 +++-- sccs/tests/what/mod.rs | 2 +- sccs/what.rs | 14 +++++++------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/sccs/Cargo.toml b/sccs/Cargo.toml index d6ec3d32..113a3601 100644 --- a/sccs/Cargo.toml +++ b/sccs/Cargo.toml @@ -8,14 +8,15 @@ edition.workspace = true rust-version.workspace = true [dependencies] -plib = { path = "../plib" } clap.workspace = true gettext-rs.workspace = true +[dev-dependencies] +plib = { path = "../plib" } + [lints] workspace = true [[bin]] name = "what" path = "./what.rs" - diff --git a/sccs/tests/what/mod.rs b/sccs/tests/what/mod.rs index 62e81c1b..ac07b259 100644 --- a/sccs/tests/what/mod.rs +++ b/sccs/tests/what/mod.rs @@ -7,7 +7,7 @@ // SPDX-License-Identifier: MIT // -use plib::{run_test, TestPlan}; +use plib::testing::{run_test, TestPlan}; use std::path::PathBuf; fn test_file_path(file_name: &str) -> PathBuf { diff --git a/sccs/what.rs b/sccs/what.rs index 42f0e3ff..51dc2871 100644 --- a/sccs/what.rs +++ b/sccs/what.rs @@ -7,13 +7,13 @@ // SPDX-License-Identifier: MIT // -use clap::Parser; -use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; use std::fs::File; use std::io::{self, BufRead, BufReader}; use std::path::{Path, PathBuf}; +use clap::Parser; +use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; + #[derive(Parser)] #[command(version, about = gettext("what - identify SCCS files"))] struct Args { @@ -51,11 +51,11 @@ fn process_file(reader: R, single: bool) -> io::Result<()> { } fn main() -> io::Result<()> { - 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(); for file in &args.files { let path = Path::new(file); From e99af000b0b9775622c6529a6fa899ed119467fc Mon Sep 17 00:00:00 2001 From: fox0 <15684995+fox0@users.noreply.github.com> Date: Fri, 25 Oct 2024 20:36:06 +0700 Subject: [PATCH 4/6] process: Use compile-time macro env! --- process/env.rs | 15 ++++--- process/fuser.rs | 83 +++++++++++++++++++------------------- process/nice.rs | 15 ++++--- process/nohup.rs | 77 +++++++++++++++++------------------ process/renice.rs | 13 +++--- process/tests/fuser/mod.rs | 2 +- process/tests/xargs/mod.rs | 2 +- process/xargs.rs | 18 ++++----- 8 files changed, 110 insertions(+), 115 deletions(-) diff --git a/process/env.rs b/process/env.rs index 6b492db6..2f4f3324 100644 --- a/process/env.rs +++ b/process/env.rs @@ -7,15 +7,15 @@ // SPDX-License-Identifier: MIT // -use clap::Parser; -use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; use std::collections::HashMap; use std::env; use std::io; use std::os::unix::process::CommandExt; use std::process::{Command, Stdio}; +use clap::Parser; +use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; + #[derive(Parser)] #[command(version, about = gettext("env - set the environment for command invocation"))] struct Args { @@ -92,12 +92,11 @@ fn exec_util(envs: HashMap, util_args: Vec) -> io::Resul } 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 (envs, util_args) = separate_ops(&args.operands); let new_env = merge_env(&envs, args.ignore_env); diff --git a/process/fuser.rs b/process/fuser.rs index f42f461b..b1b0f25b 100644 --- a/process/fuser.rs +++ b/process/fuser.rs @@ -7,20 +7,18 @@ // SPDX-License-Identifier: MIT // -use clap::{CommandFactory, Parser}; -use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; +use std::collections::BTreeMap; +use std::ffi::CStr; use std::fs::{metadata, Metadata}; use std::io::{self, Write}; +use std::os::unix::fs::MetadataExt; +use std::path::{Path, PathBuf}; use std::sync::mpsc; use std::thread; use std::time::Duration; -use std::{ - collections::BTreeMap, - ffi::CStr, - os::unix::fs::MetadataExt, - path::{Path, PathBuf}, -}; + +use clap::{CommandFactory, Parser}; +use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory}; const NAME_FIELD: usize = 20; @@ -1358,39 +1356,6 @@ struct Args { /// A pathname on which the file or file system is to be reported. file: Vec, } -fn main() -> Result<(), Box> { - setlocale(LocaleCategory::LcAll, ""); - textdomain(PROJECT_NAME)?; - bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; - - let Args { - mount, user, file, .. - } = Args::try_parse().unwrap_or_else(|err| match err.kind() { - clap::error::ErrorKind::DisplayHelp | clap::error::ErrorKind::DisplayVersion => { - print!("{err}"); - std::process::exit(1); - } - _ => { - let mut stdout = std::io::stdout(); - let mut cmd = Args::command(); - eprintln!("No process specification given"); - cmd.write_help(&mut stdout).unwrap(); - std::process::exit(1); - } - }); - - #[cfg(target_os = "linux")] - let mut names = linux::get_matched_procs(file, mount)?; - - #[cfg(target_os = "macos")] - let mut names = macos::get_matched_procs(file, mount)?; - - for name in names.iter_mut() { - print_matches(name, user)?; - } - - std::process::exit(0); -} /// Prints process matches for a given `Names` object to `stderr` and `stdout`. /// @@ -1515,3 +1480,37 @@ fn timeout(path: &str, seconds: u32) -> Result { } } } + +fn main() -> Result<(), Box> { + setlocale(LocaleCategory::LcAll, ""); + textdomain(env!("PROJECT_NAME"))?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; + + let Args { + mount, user, file, .. + } = Args::try_parse().unwrap_or_else(|err| match err.kind() { + clap::error::ErrorKind::DisplayHelp | clap::error::ErrorKind::DisplayVersion => { + print!("{err}"); + std::process::exit(1); + } + _ => { + let mut stdout = std::io::stdout(); + let mut cmd = Args::command(); + eprintln!("No process specification given"); + cmd.write_help(&mut stdout).unwrap(); + std::process::exit(1); + } + }); + + #[cfg(target_os = "linux")] + let mut names = linux::get_matched_procs(file, mount)?; + + #[cfg(target_os = "macos")] + let mut names = macos::get_matched_procs(file, mount)?; + + for name in names.iter_mut() { + print_matches(name, user)?; + } + + std::process::exit(0); +} diff --git a/process/nice.rs b/process/nice.rs index a9906aff..323dc97c 100644 --- a/process/nice.rs +++ b/process/nice.rs @@ -7,13 +7,13 @@ // SPDX-License-Identifier: MIT // -use clap::Parser; -use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; use std::io; use std::os::unix::process::CommandExt; use std::process::{Command, Stdio}; +use clap::Parser; +use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; + #[derive(Parser)] #[command( version, @@ -51,12 +51,11 @@ fn exec_util(util: &str, util_args: Vec) -> 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 res = unsafe { libc::nice(args.niceval) }; if res < 0 { diff --git a/process/nohup.rs b/process/nohup.rs index 83d685c2..8e835b84 100644 --- a/process/nohup.rs +++ b/process/nohup.rs @@ -7,20 +7,52 @@ // SPDX-License-Identifier: MIT // -use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory}; -use libc::signal; -use libc::{dup, dup2, SIGHUP, SIG_IGN}; -use plib::PROJECT_NAME; use std::env; use std::fs::{File, OpenOptions}; use std::io::{self, IsTerminal}; use std::os::unix::io::AsRawFd; use std::process::{self, Command}; +use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory}; +use libc::{dup, dup2, signal, SIGHUP, SIG_IGN}; + +enum NohupDir { + Current, + Home, +} + +fn get_nohup_out_file() -> Result<(File, NohupDir), io::Error> { + // Attempting to open or create a nohup.out file in the current directory + match OpenOptions::new() + .create(true) + .append(true) + .open("nohup.out") + { + Ok(file) => Ok((file, NohupDir::Current)), + Err(_) => { + // If unsuccessful, attempt to create a nohup.out file in the home directory + if let Some(home_dir) = dirs::home_dir() { + let mut home_nohup_path = home_dir; + home_nohup_path.push("nohup.out"); + let file = OpenOptions::new() + .create(true) + .append(true) + .open(home_nohup_path)?; + Ok((file, NohupDir::Home)) + } else { + Err(io::Error::new( + io::ErrorKind::NotFound, + "Home directory not found", + )) + } + } + } +} + 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")?; unsafe { // Ignore the SIGHUP signal @@ -111,36 +143,3 @@ fn main() -> Result<(), Box> { } } } - -enum NohupDir { - Current, - Home, -} - -fn get_nohup_out_file() -> Result<(File, NohupDir), io::Error> { - // Attempting to open or create a nohup.out file in the current directory - match OpenOptions::new() - .create(true) - .append(true) - .open("nohup.out") - { - Ok(file) => Ok((file, NohupDir::Current)), - Err(_) => { - // If unsuccessful, attempt to create a nohup.out file in the home directory - if let Some(home_dir) = dirs::home_dir() { - let mut home_nohup_path = home_dir; - home_nohup_path.push("nohup.out"); - let file = OpenOptions::new() - .create(true) - .append(true) - .open(home_nohup_path)?; - Ok((file, NohupDir::Home)) - } else { - Err(io::Error::new( - io::ErrorKind::NotFound, - "Home directory not found", - )) - } - } - } -} diff --git a/process/renice.rs b/process/renice.rs index 2ac55152..d7b434ef 100644 --- a/process/renice.rs +++ b/process/renice.rs @@ -7,12 +7,12 @@ // SPDX-License-Identifier: MIT // +use std::ffi::CString; + use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; use libc::{getpwnam, passwd}; use plib::priority::{getpriority, setpriority}; -use plib::PROJECT_NAME; -use std::ffi::CString; const PRIO_MIN: i32 = -20; const PRIO_MAX: i32 = 20; @@ -101,12 +101,11 @@ fn parse_id(which: u32, input: &str) -> 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(); // which class of priority to modify let which: u32 = { diff --git a/process/tests/fuser/mod.rs b/process/tests/fuser/mod.rs index 6c231bb2..85b6d82a 100644 --- a/process/tests/fuser/mod.rs +++ b/process/tests/fuser/mod.rs @@ -1,4 +1,4 @@ -use plib::{run_test_with_checker, TestPlan}; +use plib::testing::{run_test_with_checker, TestPlan}; use std::process::Output; mod basic; diff --git a/process/tests/xargs/mod.rs b/process/tests/xargs/mod.rs index 78cbc8bb..15ad9221 100644 --- a/process/tests/xargs/mod.rs +++ b/process/tests/xargs/mod.rs @@ -7,7 +7,7 @@ // SPDX-License-Identifier: MIT // -use plib::{run_test, TestPlan}; +use plib::testing::{run_test, TestPlan}; fn xargs_test(test_data: &str, expected_output: &str, args: Vec<&str>) { run_test(TestPlan { diff --git a/process/xargs.rs b/process/xargs.rs index 8c7497a6..1c4959e2 100644 --- a/process/xargs.rs +++ b/process/xargs.rs @@ -14,12 +14,13 @@ // - write tests // -use clap::Parser; -use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; use std::io::{self, Read}; use std::process::{Command, Stdio}; +use clap::Parser; +use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; +use plib::BUFSZ; + const ARG_MAX: i32 = 131072; // arbitrary. todo: discover actual value const MAX_ARGS_BYTES: usize = ARG_MAX as usize - 2048; @@ -304,7 +305,7 @@ impl ParseState { fn read_and_spawn(args: &Args) -> io::Result<()> { let mut state = ParseState::new(args); - let mut buffer = [0; plib::BUFSZ]; + let mut buffer = [0; BUFSZ]; // read stdin until EOF loop { @@ -346,12 +347,11 @@ fn read_and_spawn(args: &Args) -> 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(); read_and_spawn(&args)?; From 3ad61911df04b08bb21e465a292ca23d6603bda4 Mon Sep 17 00:00:00 2001 From: fox0 <15684995+fox0@users.noreply.github.com> Date: Fri, 25 Oct 2024 20:37:25 +0700 Subject: [PATCH 5/6] process: Split signal into mod --- process/kill.rs | 124 +++++----------------------------------- process/signal.rs | 138 +++++++++++++++++++++++++++++++++++++++++++++ process/timeout.rs | 135 ++++++-------------------------------------- 3 files changed, 169 insertions(+), 228 deletions(-) create mode 100644 process/signal.rs diff --git a/process/kill.rs b/process/kill.rs index 8a865038..a8d6ac2e 100644 --- a/process/kill.rs +++ b/process/kill.rs @@ -7,103 +7,14 @@ // SPDX-License-Identifier: MIT // -use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; - -#[cfg(target_os = "macos")] -const SIGLIST: [(&str, u32); 31] = [ - ("HUP", 1), - ("INT", 2), - ("QUIT", 3), - ("ILL", 4), - ("TRAP", 5), - ("ABRT", 6), - ("EMT", 7), - ("FPE", 8), - ("KILL", 9), - ("BUS", 10), - ("SEGV", 11), - ("SYS", 12), - ("PIPE", 13), - ("ALRM", 14), - ("TERM", 15), - ("URG", 16), - ("STOP", 17), - ("TSTP", 18), - ("CONT", 19), - ("CHLD", 20), - ("TTIN", 21), - ("TTOU", 22), - ("IO", 23), - ("XCPU", 24), - ("XFSZ", 25), - ("VTALRM", 26), - ("PROF", 27), - ("WINCH", 28), - ("INFO", 29), - ("USR1", 30), - ("USR2", 31), -]; - -#[cfg(target_os = "linux")] -const SIGLIST: [(&str, u32); 32] = [ - ("HUP", 1), - ("INT", 2), - ("QUIT", 3), - ("ILL", 4), - ("TRAP", 5), - ("ABRT", 6), - ("IOT", 6), - ("BUS", 7), - ("FPE", 8), - ("KILL", 9), - ("USR1", 10), - ("SEGV", 11), - ("USR2", 12), - ("PIPE", 13), - ("ALRM", 14), - ("TERM", 15), - ("STKFLT", 16), - ("CHLD", 17), - ("CONT", 18), - ("STOP", 19), - ("TSTP", 20), - ("TTIN", 21), - ("TTOU", 22), - ("URG", 23), - ("XCPU", 24), - ("XFSZ", 25), - ("VTALRM", 26), - ("PROF", 27), - ("WINCH", 28), - ("IO", 29), - ("PWR", 30), - ("SYS", 31), -]; - -fn siglist_get(name: &str) -> Option { - for (signame, signo) in SIGLIST.iter() { - if *signame == name { - return Some(*signo); - } - } +mod signal; - None -} +use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory}; -fn lookup_signum(signame: &str) -> Result { - if signame == "0" { - Ok(0) - } else { - match siglist_get(signame.to_uppercase().as_str()) { - Some(sig_no) => Ok(sig_no), - None => Err("Unknown signal name"), - } - } -} +use crate::signal::{list_signals, lookup_signum}; enum ConfigMode { - Signal(u32), + Signal(i32), List, } @@ -114,7 +25,7 @@ struct Config { fn parse_cmdline() -> Result { let mut pids = Vec::new(); - let mut mode = ConfigMode::Signal(libc::SIGTERM as u32); + let mut mode = ConfigMode::Signal(libc::SIGTERM); let mut in_args = true; let mut in_s_arg = false; for arg in std::env::args().skip(1) { @@ -130,7 +41,7 @@ fn parse_cmdline() -> Result { } else if arg == "--" { in_args = false; } else if let Some(st) = arg.strip_prefix("-") { - let sig_no = match st.parse::() { + let sig_no: i32 = match st.parse() { Ok(signo) => signo, Err(_) => lookup_signum(st)?, }; @@ -157,19 +68,7 @@ fn parse_cmdline() -> Result { Ok(Config { mode, pids }) } -fn list_signals() -> u32 { - let mut output = String::new(); - for (name, _) in SIGLIST.iter() { - output.push_str(name); - output.push(' '); - } - - println!("{}", output); - - 0 -} - -fn send_signal(prog_cfg: &Config, sig_no: u32) -> u32 { +fn send_signal(prog_cfg: &Config, sig_no: i32) -> u32 { let mut exit_code = 0; for pid in &prog_cfg.pids { @@ -186,13 +85,16 @@ fn send_signal(prog_cfg: &Config, sig_no: u32) -> u32 { 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 prog_cfg = parse_cmdline()?; let exit_code = match prog_cfg.mode { - ConfigMode::List => list_signals(), + ConfigMode::List => { + list_signals(); + 0 + } ConfigMode::Signal(sig_no) => send_signal(&prog_cfg, sig_no), }; diff --git a/process/signal.rs b/process/signal.rs new file mode 100644 index 00000000..9d08ce7a --- /dev/null +++ b/process/signal.rs @@ -0,0 +1,138 @@ +// +// Copyright (c) 2024 Jeff Garzik +// +// This file is part of the posixutils-rs project covered under +// the MIT License. For the full license text, please see the LICENSE +// file in the root directory of this project. +// SPDX-License-Identifier: MIT +// + +// `kill` and `timeout` bins +#![allow(dead_code)] + +pub fn list_signals() { + let mut output = String::new(); + for (name, _) in SIGLIST.iter() { + output.push_str(name); + output.push(' '); + } + + println!("{}", output); +} + +/// Parses [str] into [Signal]. +/// +/// # Arguments +/// +/// * `s` - [str] that represents the signal name. +/// +/// # Returns +/// +/// Returns the parsed [Signal] value. +/// +/// # Errors +/// +/// Returns an [String] error if passed invalid signal name. +pub fn parse_signal(s: &str) -> Result { + let normalized = s.trim().to_uppercase(); + let normalized = normalized.strip_prefix("SIG").unwrap_or(&normalized); + + for (name, num) in SIGLIST.iter() { + if name == &normalized { + return Ok(*num); + } + } + Err(format!("invalid signal name '{s}'")) +} + +pub fn lookup_signum(signame: &str) -> Result { + if signame == "0" { + Ok(0) + } else { + match siglist_get(signame.to_uppercase().as_str()) { + Some(sig_no) => Ok(sig_no), + None => Err("Unknown signal name"), + } + } +} + +fn siglist_get(name: &str) -> Option { + for (signame, signo) in SIGLIST.iter() { + if *signame == name { + return Some(*signo); + } + } + + None +} + +#[cfg(target_os = "macos")] +const SIGLIST: [(&str, i32); 31] = [ + ("HUP", 1), + ("INT", 2), + ("QUIT", 3), + ("ILL", 4), + ("TRAP", 5), + ("ABRT", 6), + ("EMT", 7), + ("FPE", 8), + ("KILL", 9), + ("BUS", 10), + ("SEGV", 11), + ("SYS", 12), + ("PIPE", 13), + ("ALRM", 14), + ("TERM", 15), + ("URG", 16), + ("STOP", 17), + ("TSTP", 18), + ("CONT", 19), + ("CHLD", 20), + ("TTIN", 21), + ("TTOU", 22), + ("IO", 23), + ("XCPU", 24), + ("XFSZ", 25), + ("VTALRM", 26), + ("PROF", 27), + ("WINCH", 28), + ("INFO", 29), + ("USR1", 30), + ("USR2", 31), +]; + +#[cfg(target_os = "linux")] +const SIGLIST: [(&str, i32); 32] = [ + ("HUP", 1), + ("INT", 2), + ("QUIT", 3), + ("ILL", 4), + ("TRAP", 5), + ("ABRT", 6), + ("IOT", 6), + ("BUS", 7), + ("FPE", 8), + ("KILL", 9), + ("USR1", 10), + ("SEGV", 11), + ("USR2", 12), + ("PIPE", 13), + ("ALRM", 14), + ("TERM", 15), + ("STKFLT", 16), + ("CHLD", 17), + ("CONT", 18), + ("STOP", 19), + ("TSTP", 20), + ("TTIN", 21), + ("TTOU", 22), + ("URG", 23), + ("XCPU", 24), + ("XFSZ", 25), + ("VTALRM", 26), + ("PROF", 27), + ("WINCH", 28), + ("IO", 29), + ("PWR", 30), + ("SYS", 31), +]; diff --git a/process/timeout.rs b/process/timeout.rs index e06ffea2..cad8a29a 100644 --- a/process/timeout.rs +++ b/process/timeout.rs @@ -7,94 +7,21 @@ // SPDX-License-Identifier: MIT // +mod signal; + +use std::error::Error; +use std::os::unix::fs::PermissionsExt; +use std::os::unix::process::{CommandExt, ExitStatusExt}; +use std::path::Path; +use std::process::{Command, ExitStatus}; +use std::sync::atomic::{AtomicBool, AtomicI32, Ordering}; +use std::sync::Mutex; +use std::time::Duration; + use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; -use std::{ - error::Error, - os::unix::{ - fs::PermissionsExt, - process::{CommandExt, ExitStatusExt}, - }, - path::Path, - process::{Command, ExitStatus}, - sync::{ - atomic::{AtomicBool, AtomicI32, Ordering}, - Mutex, - }, - time::Duration, -}; - -#[cfg(target_os = "macos")] -const SIGLIST: [(&str, i32); 31] = [ - ("HUP", 1), - ("INT", 2), - ("QUIT", 3), - ("ILL", 4), - ("TRAP", 5), - ("ABRT", 6), - ("EMT", 7), - ("FPE", 8), - ("KILL", 9), - ("BUS", 10), - ("SEGV", 11), - ("SYS", 12), - ("PIPE", 13), - ("ALRM", 14), - ("TERM", 15), - ("URG", 16), - ("STOP", 17), - ("TSTP", 18), - ("CONT", 19), - ("CHLD", 20), - ("TTIN", 21), - ("TTOU", 22), - ("IO", 23), - ("XCPU", 24), - ("XFSZ", 25), - ("VTALRM", 26), - ("PROF", 27), - ("WINCH", 28), - ("INFO", 29), - ("USR1", 30), - ("USR2", 31), -]; - -#[cfg(target_os = "linux")] -const SIGLIST: [(&str, i32); 32] = [ - ("HUP", 1), - ("INT", 2), - ("QUIT", 3), - ("ILL", 4), - ("TRAP", 5), - ("ABRT", 6), - ("IOT", 6), - ("BUS", 7), - ("FPE", 8), - ("KILL", 9), - ("USR1", 10), - ("SEGV", 11), - ("USR2", 12), - ("PIPE", 13), - ("ALRM", 14), - ("TERM", 15), - ("STKFLT", 16), - ("CHLD", 17), - ("CONT", 18), - ("STOP", 19), - ("TSTP", 20), - ("TTIN", 21), - ("TTOU", 22), - ("URG", 23), - ("XCPU", 24), - ("XFSZ", 25), - ("VTALRM", 26), - ("PROF", 27), - ("WINCH", 28), - ("IO", 29), - ("PWR", 30), - ("SYS", 31), -]; + +use crate::signal::parse_signal; static FOREGROUND: AtomicBool = AtomicBool::new(false); static FIRST_SIGNAL: AtomicI32 = AtomicI32::new(libc::SIGTERM); @@ -161,31 +88,6 @@ fn parse_duration(s: &str) -> Result { Ok(Duration::from_secs_f64(value * multiplier)) } -/// Parses [str] into [Signal]. -/// -/// # Arguments -/// -/// * `s` - [str] that represents the signal name. -/// -/// # Returns -/// -/// Returns the parsed [Signal] value. -/// -/// # Errors -/// -/// Returns an [String] error if passed invalid signal name. -fn parse_signal(s: &str) -> Result { - let normalized = s.trim().to_uppercase(); - let normalized = normalized.strip_prefix("SIG").unwrap_or(&normalized); - - for (name, num) in SIGLIST.iter() { - if name == &normalized { - return Ok(*num); - } - } - Err(format!("invalid signal name '{s}'")) -} - /// Starts the timeout after which [libc::SIGALRM] will be send. /// /// # Arguments @@ -548,7 +450,7 @@ fn timeout(args: Args) -> i32 { } 128 + signal } else { - eprintln!("timeout: unknown status from commnad: {status}"); + eprintln!("timeout: unknown status from command: {status}"); return 125; }; @@ -566,7 +468,10 @@ fn timeout(args: Args) -> i32 { /// 126 - The utility specified by utility was found but could not be executed. /// 127 - The utility specified by utility could not be found. fn main() -> Result<(), Box> { - // Parse command line arguments + setlocale(LocaleCategory::LcAll, ""); + textdomain(env!("PROJECT_NAME"))?; + bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?; + let args = Args::try_parse().unwrap_or_else(|err| match err.kind() { clap::error::ErrorKind::DisplayHelp | clap::error::ErrorKind::DisplayVersion => { print!("{err}"); @@ -582,10 +487,6 @@ fn main() -> Result<(), Box> { } }); - setlocale(LocaleCategory::LcAll, ""); - textdomain(PROJECT_NAME)?; - bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; - let exit_code = timeout(args); std::process::exit(exit_code); } From a2521970a0f1964286819432e3f9daa9e3b5cc66 Mon Sep 17 00:00:00 2001 From: fox0 <15684995+fox0@users.noreply.github.com> Date: Fri, 25 Oct 2024 20:40:35 +0700 Subject: [PATCH 6/6] pathnames: Use compile-time macro env! --- pathnames/Cargo.toml | 4 +++- pathnames/basename.rs | 13 ++++++------- pathnames/dirname.rs | 15 +++++++-------- pathnames/pathchk.rs | 15 +++++++-------- pathnames/realpath.rs | 12 ++++++------ pathnames/tests/basename/mod.rs | 2 +- pathnames/tests/dirname/mod.rs | 2 +- pathnames/tests/realpath/mod.rs | 2 +- 8 files changed, 32 insertions(+), 33 deletions(-) diff --git a/pathnames/Cargo.toml b/pathnames/Cargo.toml index 53aca747..38b7e6f6 100644 --- a/pathnames/Cargo.toml +++ b/pathnames/Cargo.toml @@ -8,11 +8,13 @@ edition.workspace = true rust-version.workspace = true [dependencies] -plib = { path = "../plib" } clap.workspace = true gettext-rs.workspace = true libc.workspace = true +[dev-dependencies] +plib = { path = "../plib" } + [lints] workspace = true diff --git a/pathnames/basename.rs b/pathnames/basename.rs index c2074cb1..4765f6a0 100644 --- a/pathnames/basename.rs +++ b/pathnames/basename.rs @@ -7,10 +7,10 @@ // SPDX-License-Identifier: MIT // +use std::path::Path; + use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; -use std::path::Path; #[derive(Parser)] #[command( @@ -52,12 +52,11 @@ fn show_basename(args: &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(); show_basename(&args); diff --git a/pathnames/dirname.rs b/pathnames/dirname.rs index e3ac3fe3..ce6ec746 100644 --- a/pathnames/dirname.rs +++ b/pathnames/dirname.rs @@ -7,12 +7,12 @@ // SPDX-License-Identifier: MIT // -use clap::Parser; -use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; use std::ffi::OsString; use std::path::PathBuf; +use clap::Parser; +use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; + #[derive(Parser)] #[command( version, @@ -40,12 +40,11 @@ fn show_dirname(args: &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(); show_dirname(&args); diff --git a/pathnames/pathchk.rs b/pathnames/pathchk.rs index 54bbcd2e..8a4361e5 100644 --- a/pathnames/pathchk.rs +++ b/pathnames/pathchk.rs @@ -7,12 +7,12 @@ // SPDX-License-Identifier: MIT // -use clap::Parser; -use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; use std::ffi::CString; use std::path::{Component, Path}; +use clap::Parser; +use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; + const _POSIX_PATH_MAX: usize = 255; const _POSIX_NAME_MAX: usize = 14; @@ -142,12 +142,11 @@ fn check_path(args: &Args, pathname: &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(); let mut exit_code = 0; diff --git a/pathnames/realpath.rs b/pathnames/realpath.rs index 658d2430..16d1822d 100644 --- a/pathnames/realpath.rs +++ b/pathnames/realpath.rs @@ -7,10 +7,10 @@ // SPDX-License-Identifier: MIT // +use std::path::{Component, Path, PathBuf}; + use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; -use std::path::{Component, Path, PathBuf}; /// realpath -- return resolved canonical path #[derive(Parser)] @@ -63,11 +63,11 @@ fn normalize>(path: P) -> std::io::Result { } fn main() -> Result<(), Box> { - 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 exit_code = 0; diff --git a/pathnames/tests/basename/mod.rs b/pathnames/tests/basename/mod.rs index 429118d1..6425fd03 100644 --- a/pathnames/tests/basename/mod.rs +++ b/pathnames/tests/basename/mod.rs @@ -7,7 +7,7 @@ // SPDX-License-Identifier: MIT // -use plib::{run_test, TestPlan}; +use plib::testing::{run_test, TestPlan}; #[test] fn basename_basic() { diff --git a/pathnames/tests/dirname/mod.rs b/pathnames/tests/dirname/mod.rs index 82dcb4fe..1722e0af 100644 --- a/pathnames/tests/dirname/mod.rs +++ b/pathnames/tests/dirname/mod.rs @@ -7,7 +7,7 @@ // SPDX-License-Identifier: MIT // -use plib::{run_test, TestPlan}; +use plib::testing::{run_test, TestPlan}; #[test] fn dirname_basic() { diff --git a/pathnames/tests/realpath/mod.rs b/pathnames/tests/realpath/mod.rs index 8bf1482d..db215c40 100644 --- a/pathnames/tests/realpath/mod.rs +++ b/pathnames/tests/realpath/mod.rs @@ -7,7 +7,7 @@ // SPDX-License-Identifier: MIT // -use plib::{run_test, TestPlan}; +use plib::testing::{run_test, TestPlan}; fn realpath_test(args: &[&str], stdout: &str, stderr: &str, expected_code: i32) { let str_args: Vec = args.iter().map(|s| String::from(*s)).collect();