Skip to content

Use compile-time macro env! #367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion datetime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 4 additions & 6 deletions datetime/cal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down Expand Up @@ -94,12 +93,11 @@ fn print_year(year: u32) {
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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() {
Expand Down
10 changes: 4 additions & 6 deletions datetime/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -146,12 +145,11 @@ fn set_time(utc: bool, timestr: &str) -> Result<(), &'static str> {
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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),
Expand Down
13 changes: 5 additions & 8 deletions datetime/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -24,16 +22,15 @@ struct Args {
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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));
Expand Down
2 changes: 1 addition & 1 deletion datetime/tests/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>, stdin_data: &[u8]) -> Output {
let relpath = if cfg!(debug_assertions) {
Expand Down
8 changes: 3 additions & 5 deletions datetime/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -134,9 +132,9 @@ impl Status {

fn main() -> Result<(), Box<dyn std::error::Error>> {
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();

Expand Down
21 changes: 11 additions & 10 deletions file/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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[..])?;
Expand All @@ -50,12 +52,11 @@ fn cat_file(pathname: &PathBuf) -> io::Result<()> {
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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() {
Expand Down
20 changes: 10 additions & 10 deletions file/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -76,8 +77,8 @@ fn cmp_main(args: &Args) -> io::Result<u8> {
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;
Expand Down Expand Up @@ -135,12 +136,11 @@ fn cmp_main(args: &Args) -> io::Result<u8> {
}

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 args = Args::parse();

match cmp_main(&args) {
Ok(x) => ExitCode::from(x),
Expand Down
8 changes: 4 additions & 4 deletions file/dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [
Expand Down Expand Up @@ -383,8 +383,8 @@ fn parse_cmdline(args: &[String]) -> Result<Config, Box<dyn std::error::Error>>

fn main() -> Result<(), Box<dyn std::error::Error>> {
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<String> = std::env::args().skip(1).collect();
let config = parse_cmdline(&args)?;
Expand Down
23 changes: 10 additions & 13 deletions file/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -192,12 +190,11 @@ fn analyze_file(mut path: String, args: &Args, magic_files: &Vec<PathBuf>) {
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
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);

Expand Down
11 changes: 6 additions & 5 deletions file/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -484,8 +484,9 @@ fn find(args: Vec<String>) -> Result<(), String> {
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
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<String> = env::args().collect();

Expand Down
16 changes: 9 additions & 7 deletions file/od.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
// 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;
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 {
Expand Down Expand Up @@ -1138,10 +1139,11 @@ fn od(args: &Args) -> Result<(), Box<dyn std::error::Error>> {

fn main() -> Result<(), Box<dyn std::error::Error>> {
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;

Expand Down
Loading