From 6f6fdbf033858cc8322a9281fba6017b3eb10963 Mon Sep 17 00:00:00 2001 From: fox0 <15684995+fox0@users.noreply.github.com> Date: Wed, 16 Oct 2024 19:33:14 +0700 Subject: [PATCH 1/4] awk: Use compile-time macro env! --- awk/Cargo.toml | 4 +++- awk/src/main.rs | 8 ++++---- awk/tests/integration.rs | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/awk/Cargo.toml b/awk/Cargo.toml index 4b4eeba1c..7a204133a 100644 --- a/awk/Cargo.toml +++ b/awk/Cargo.toml @@ -7,7 +7,6 @@ edition.workspace = true rust-version.workspace = true [dependencies] -plib = { path = "../plib" } gettext-rs.workspace = true clap.workspace = true libc.workspace = true @@ -17,6 +16,9 @@ lazy_static = "1.4" lexical = { version = "6.1", features = ["format"] } rand = {version = "0.8", default-features = false, features = ["small_rng"] } +[dev-dependencies] +plib = { path = "../plib" } + [lints] workspace = true diff --git a/awk/src/main.rs b/awk/src/main.rs index ef1813340..5118ffdf0 100644 --- a/awk/src/main.rs +++ b/awk/src/main.rs @@ -11,8 +11,7 @@ use crate::compiler::compile_program; use crate::interpreter::interpret; use clap::Parser; use compiler::SourceFile; -use gettextrs::{bind_textdomain_codeset, gettext, textdomain}; -use plib::PROJECT_NAME; +use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; use std::error::Error; use std::fmt::Display; use std::io::Read; @@ -56,8 +55,9 @@ fn exit_if_error(r: Result) -> T { } 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 = Args::parse(); diff --git a/awk/tests/integration.rs b/awk/tests/integration.rs index 0c4f5bf32..831356c88 100644 --- a/awk/tests/integration.rs +++ b/awk/tests/integration.rs @@ -1,4 +1,4 @@ -use plib::{run_test, run_test_with_checker, TestPlan}; +use plib::testing::{run_test, run_test_with_checker, TestPlan}; fn test_awk(args: Vec, expected_output: &str) { run_test(TestPlan { From e4474489519c36328d26e95698699a248a4e1f48 Mon Sep 17 00:00:00 2001 From: fox0 <15684995+fox0@users.noreply.github.com> Date: Wed, 16 Oct 2024 19:37:07 +0700 Subject: [PATCH 2/4] calc: Use compile-time macro env! --- calc/Cargo.toml | 4 +++- calc/bc.rs | 6 +++--- calc/expr.rs | 6 ++---- calc/tests/bc/mod.rs | 2 +- calc/tests/expr/mod.rs | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/calc/Cargo.toml b/calc/Cargo.toml index a204a80ed..a568378df 100644 --- a/calc/Cargo.toml +++ b/calc/Cargo.toml @@ -7,7 +7,6 @@ edition.workspace = true rust-version.workspace = true [dependencies] -plib = { path = "../plib" } gettext-rs.workspace = true regex.workspace = true clap.workspace = true @@ -17,6 +16,9 @@ lazy_static = "1.4" bigdecimal = "0.4" rustyline = { version = "14.0", default-features = false } +[dev-dependencies] +plib = { path = "../plib" } + [lints] workspace = true diff --git a/calc/bc.rs b/calc/bc.rs index ef0fe5c04..46dc73cab 100644 --- a/calc/bc.rs +++ b/calc/bc.rs @@ -16,7 +16,6 @@ use bc_util::{ use clap::Parser; use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; use rustyline::{error::ReadlineError, DefaultEditor, Result}; mod bc_util; @@ -45,10 +44,11 @@ fn print_output_or_error(result: ExecutionResult) { fn main() -> Result<()> { 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 interpreter = Interpreter::default(); if args.define_math_functions { diff --git a/calc/expr.rs b/calc/expr.rs index e99efd9ed..618d7739b 100644 --- a/calc/expr.rs +++ b/calc/expr.rs @@ -8,7 +8,6 @@ // use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; use regex::Regex; #[derive(Clone, Debug, PartialEq)] @@ -370,10 +369,9 @@ fn eval_expression(tokens: &[Token]) -> Result { } fn main() -> Result<(), Box> { - // initialize translations 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")?; // tokenize and evaluate the expression let arg_tokens = tokenize(); diff --git a/calc/tests/bc/mod.rs b/calc/tests/bc/mod.rs index b2a166e62..b9bf58afc 100644 --- a/calc/tests/bc/mod.rs +++ b/calc/tests/bc/mod.rs @@ -7,7 +7,7 @@ // SPDX-License-Identifier: MIT // -use plib::{run_test, TestPlan}; +use plib::testing::{run_test, TestPlan}; fn test_bc(program: &str, expected_output: &str) { run_test(TestPlan { diff --git a/calc/tests/expr/mod.rs b/calc/tests/expr/mod.rs index ee7eaee59..8cb428c21 100644 --- a/calc/tests/expr/mod.rs +++ b/calc/tests/expr/mod.rs @@ -7,7 +7,7 @@ // SPDX-License-Identifier: MIT // -use plib::{run_test, TestPlan}; +use plib::testing::{run_test, TestPlan}; fn expr_test(args: &[&str], expected_output: &str) { let str_args: Vec = args.iter().map(|s| String::from(*s)).collect(); From 88df556362753e4bdf53ea5f5fc4d798e91d42ed Mon Sep 17 00:00:00 2001 From: fox0 <15684995+fox0@users.noreply.github.com> Date: Wed, 16 Oct 2024 20:15:23 +0700 Subject: [PATCH 3/4] dev: Use compile-time macro env! --- dev/Cargo.toml | 4 +++- dev/nm.rs | 10 ++++------ dev/strings.rs | 6 +++--- dev/strip.rs | 15 +++++++-------- dev/tests/dev-tests.rs | 2 +- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/dev/Cargo.toml b/dev/Cargo.toml index 7ac48cb08..faebcd676 100644 --- a/dev/Cargo.toml +++ b/dev/Cargo.toml @@ -8,13 +8,15 @@ edition.workspace = true rust-version.workspace = true [dependencies] -plib = { path = "../plib" } clap.workspace = true gettext-rs.workspace = true object = { version = "0.35", features = ["read", "build", "elf"]} chrono.workspace = true ar = "0.9" +[dev-dependencies] +plib = { path = "../plib" } + [lints] workspace = true diff --git a/dev/nm.rs b/dev/nm.rs index ebae7ad8d..2cb23893c 100644 --- a/dev/nm.rs +++ b/dev/nm.rs @@ -18,7 +18,6 @@ use object::{ use clap::{Parser, ValueEnum}; use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; use std::collections::HashMap; use std::fs; @@ -143,12 +142,11 @@ fn show_object_file(args: &Args) -> Result<(), Box> { } 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_object_file(&args)?; diff --git a/dev/strings.rs b/dev/strings.rs index 7666cb325..f46164f92 100644 --- a/dev/strings.rs +++ b/dev/strings.rs @@ -12,7 +12,6 @@ use std::ffi::OsString; use clap::{Parser, ValueEnum}; use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory}; use object::{Object, ObjectSection}; -use plib::PROJECT_NAME; #[derive(Clone, Copy, ValueEnum)] enum OffsetFormat { @@ -188,10 +187,11 @@ where fn main() -> StringsResult { 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 CharacterSet::from_env() { CharacterSet::Utf8 => { for file in args.input_files { diff --git a/dev/strip.rs b/dev/strip.rs index e6eaa471b..f3b210c7c 100644 --- a/dev/strip.rs +++ b/dev/strip.rs @@ -7,11 +7,6 @@ // SPDX-License-Identifier: MIT // -use std::{ - ffi::{OsStr, OsString}, - io::Read, -}; - use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; use object::{ @@ -19,7 +14,10 @@ use object::{ build::elf::{Builder, Section, SectionData}, elf, }; -use plib::PROJECT_NAME; +use std::{ + ffi::{OsStr, OsString}, + io::Read, +}; #[derive(Parser)] #[command(version, about = gettext("strip - remove unnecessary information from strippable files"))] @@ -145,10 +143,11 @@ fn strip_file(file: &OsStr) { 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 = Args::parse(); + for file in args.input_files { strip_file(&file); } diff --git a/dev/tests/dev-tests.rs b/dev/tests/dev-tests.rs index 89485119a..835d3f302 100644 --- a/dev/tests/dev-tests.rs +++ b/dev/tests/dev-tests.rs @@ -1,5 +1,5 @@ use object::{Object, ObjectSection, ObjectSymbol}; -use plib::{run_test, run_test_with_checker, TestPlan}; +use plib::testing::{run_test, run_test_with_checker, TestPlan}; use std::fs; fn ar_compare_test( From be574289c78a1cecc434c59f52b2add87867eda4 Mon Sep 17 00:00:00 2001 From: fox0 <15684995+fox0@users.noreply.github.com> Date: Wed, 16 Oct 2024 20:20:48 +0700 Subject: [PATCH 4/4] i18n: Use compile-time macro env! --- i18n/gencat.rs | 12 ++++++------ i18n/iconv.rs | 12 ++++++------ i18n/tests/gencat/mod.rs | 2 +- i18n/tests/iconv/mod.rs | 2 +- plib/src/io.rs | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/i18n/gencat.rs b/i18n/gencat.rs index 65fd0c105..5021ce90d 100644 --- a/i18n/gencat.rs +++ b/i18n/gencat.rs @@ -1,7 +1,7 @@ use byteorder::{BigEndian, ByteOrder, LittleEndian, NativeEndian, WriteBytesExt}; use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory}; -use plib::PROJECT_NAME; +use plib::io::input_stream; use std::{ cell::RefCell, collections::BTreeMap, @@ -295,7 +295,7 @@ impl MessageCatalog { input_path: &PathBuf, catfile_catalog: Option, ) -> Result> { - let mut file = plib::io::input_stream(input_path, true)?; + let mut file = input_stream(input_path, true)?; let mut input = String::new(); file.read_to_string(&mut input)?; @@ -774,11 +774,11 @@ impl MessageCatalog { } 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/i18n/iconv.rs b/i18n/iconv.rs index 5636ed28b..4ddae8d83 100644 --- a/i18n/iconv.rs +++ b/i18n/iconv.rs @@ -15,7 +15,7 @@ use iconv_lib::{ utf_32::{self, UTF32Variant}, utf_8, }; -use plib::PROJECT_NAME; +use plib::io::input_stream; use std::{ collections::HashMap, env, @@ -480,11 +480,11 @@ fn charmap_conversion( } 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(); if args.list_codesets { list_encodings(); @@ -517,7 +517,7 @@ fn main() -> Result<(), Box> { let inputs: Vec> = match args.files { Some(files) => files .into_iter() - .map(|file| plib::io::input_stream(&file, true)) + .map(|file| input_stream(&file, true)) .collect::, _>>()?, None => vec![Box::new(io::stdin().lock())], }; diff --git a/i18n/tests/gencat/mod.rs b/i18n/tests/gencat/mod.rs index a1f57b7cc..5c4fbf0ae 100644 --- a/i18n/tests/gencat/mod.rs +++ b/i18n/tests/gencat/mod.rs @@ -7,7 +7,7 @@ // SPDX-License-Identifier: MIT // -use plib::{run_test_u8, TestPlanU8}; +use plib::testing::{run_test_u8, TestPlanU8}; use std::env; use std::path::PathBuf; use std::{fs::File, io::Read}; diff --git a/i18n/tests/iconv/mod.rs b/i18n/tests/iconv/mod.rs index 2c3342dc3..47b63ca70 100644 --- a/i18n/tests/iconv/mod.rs +++ b/i18n/tests/iconv/mod.rs @@ -8,7 +8,7 @@ // #![allow(non_snake_case)] -use plib::{run_test_u8, TestPlanU8}; +use plib::testing::{run_test_u8, TestPlanU8}; use std::env; use std::path::PathBuf; use std::{fs::File, io::Read}; diff --git a/plib/src/io.rs b/plib/src/io.rs index 969d4336f..253763f4f 100644 --- a/plib/src/io.rs +++ b/plib/src/io.rs @@ -11,8 +11,8 @@ use std::fs; use std::io::{self, Read}; use std::path::PathBuf; +/// open file, or stdin pub fn input_stream(pathname: &PathBuf, dashed_stdin: bool) -> io::Result> { - // open file, or stdin let path_str = pathname.as_os_str(); let file: Box = if (dashed_stdin && path_str == "-") || (!dashed_stdin && path_str.is_empty()) {