Skip to content

Commit 769b33f

Browse files
authored
Merge pull request #367 from fox0/eenv
Use compile-time macro env!
2 parents 87b53d4 + 55346ce commit 769b33f

31 files changed

+182
-195
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

datetime/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ edition.workspace = true
88
rust-version.workspace = true
99

1010
[dependencies]
11-
plib = { path = "../plib" }
1211
clap.workspace = true
1312
gettext-rs.workspace = true
1413
chrono.workspace = true
1514
libc.workspace = true
1615

16+
[dev-dependencies]
17+
plib = { path = "../plib" }
18+
1719
[lints]
1820
workspace = true
1921

datetime/cal.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use chrono::Datelike;
1515
use clap::Parser;
1616
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
17-
use plib::PROJECT_NAME;
1817

1918
#[derive(Parser)]
2019
#[command(version, about = gettext("cal - print a calendar"))]
@@ -94,12 +93,11 @@ fn print_year(year: u32) {
9493
}
9594

9695
fn main() -> Result<(), Box<dyn std::error::Error>> {
97-
// parse command line arguments
98-
let mut args = Args::parse();
99-
10096
setlocale(LocaleCategory::LcAll, "");
101-
textdomain(PROJECT_NAME)?;
102-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
97+
textdomain(env!("PROJECT_NAME"))?;
98+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
99+
100+
let mut args = Args::parse();
103101

104102
// If no arguments are provided, display the current month
105103
if args.month.is_none() && args.year.is_none() {

datetime/date.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use chrono::{DateTime, Datelike, Local, LocalResult, TimeZone, Utc};
1515
use clap::Parser;
1616
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
17-
use plib::PROJECT_NAME;
1817

1918
const DEF_TIMESTR: &str = "%a %b %e %H:%M:%S %Z %Y";
2019

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

148147
fn main() -> Result<(), Box<dyn std::error::Error>> {
149-
// parse command line arguments
150-
let args = Args::parse();
151-
152148
setlocale(LocaleCategory::LcAll, "");
153-
textdomain(PROJECT_NAME)?;
154-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
149+
textdomain(env!("PROJECT_NAME"))?;
150+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
151+
152+
let args = Args::parse();
155153

156154
match &args.timestr {
157155
None => show_time(args.utc, DEF_TIMESTR),

datetime/sleep.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
use clap::Parser;
1111
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
12-
use libc::{signal, SIGALRM, SIG_IGN};
13-
use plib::PROJECT_NAME;
1412
use std::{thread, time};
1513

1614
#[derive(Parser)]
@@ -24,16 +22,15 @@ struct Args {
2422
}
2523

2624
fn main() -> Result<(), Box<dyn std::error::Error>> {
27-
// parse command line arguments
28-
let args = Args::parse();
29-
3025
setlocale(LocaleCategory::LcAll, "");
31-
textdomain(PROJECT_NAME)?;
32-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
26+
textdomain(env!("PROJECT_NAME"))?;
27+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
28+
29+
let args = Args::parse();
3330

3431
unsafe {
3532
// Ignore the SIGALRM signal
36-
signal(SIGALRM, SIG_IGN);
33+
libc::signal(libc::SIGALRM, libc::SIG_IGN);
3734
}
3835

3936
thread::sleep(time::Duration::from_secs(args.seconds));

datetime/tests/time/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::{
1212
process::{Command, Output, Stdio},
1313
};
1414

15-
use plib::TestPlan;
15+
use plib::testing::TestPlan;
1616

1717
fn run_test_base(cmd: &str, args: &Vec<String>, stdin_data: &[u8]) -> Output {
1818
let relpath = if cfg!(debug_assertions) {

datetime/time.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ use std::process::{Command, Stdio};
1212
use std::time::Instant;
1313

1414
use clap::Parser;
15-
1615
use gettextrs::{
1716
bind_textdomain_codeset, bindtextdomain, gettext, setlocale, textdomain, LocaleCategory,
1817
};
19-
use plib::PROJECT_NAME;
2018

2119
#[derive(Parser)]
2220
#[command(
@@ -134,9 +132,9 @@ impl Status {
134132

135133
fn main() -> Result<(), Box<dyn std::error::Error>> {
136134
setlocale(LocaleCategory::LcAll, "");
137-
textdomain(PROJECT_NAME)?;
138-
bindtextdomain(PROJECT_NAME, "locale")?;
139-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
135+
textdomain(env!("PROJECT_NAME"))?;
136+
bindtextdomain(env!("PROJECT_NAME"), "locale")?;
137+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
140138

141139
let args = Args::parse();
142140

file/cat.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
// - Questionable behavior: if write_all() produces Err, the program will
1313
// continue to the next file, rather than stopping.
1414

15-
use clap::Parser;
16-
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
17-
use plib::PROJECT_NAME;
1815
use std::io::{self, Read, Write};
1916
use std::path::PathBuf;
2017

18+
use clap::Parser;
19+
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
20+
use plib::io::input_stream;
21+
use plib::BUFSZ;
22+
2123
#[derive(Parser)]
2224
#[command(version, about = gettext("cat - concatenate and print files"))]
2325
struct Args {
@@ -34,8 +36,8 @@ struct Args {
3436
}
3537

3638
fn cat_file(pathname: &PathBuf) -> io::Result<()> {
37-
let mut file = plib::io::input_stream(pathname, true)?;
38-
let mut buffer = [0; plib::BUFSZ];
39+
let mut file = input_stream(pathname, true)?;
40+
let mut buffer = [0; BUFSZ];
3941

4042
loop {
4143
let n_read = file.read(&mut buffer[..])?;
@@ -50,12 +52,11 @@ fn cat_file(pathname: &PathBuf) -> io::Result<()> {
5052
}
5153

5254
fn main() -> Result<(), Box<dyn std::error::Error>> {
53-
// parse command line arguments
54-
let mut args = Args::parse();
55-
5655
setlocale(LocaleCategory::LcAll, "");
57-
textdomain(PROJECT_NAME)?;
58-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
56+
textdomain(env!("PROJECT_NAME"))?;
57+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
58+
59+
let mut args = Args::parse();
5960

6061
// if no file args, read from stdin
6162
if args.files.is_empty() {

file/cmp.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
// SPDX-License-Identifier: MIT
88
//
99

10-
use clap::Parser;
11-
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
12-
use plib::PROJECT_NAME;
1310
use std::io::{self, ErrorKind, Read};
1411
use std::path::PathBuf;
1512
use std::process::ExitCode;
1613

14+
use clap::Parser;
15+
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
16+
use plib::io::input_reader;
17+
1718
#[derive(Parser)]
1819
#[command(version, about = gettext("cmp - compare two files"))]
1920
struct Args {
@@ -76,8 +77,8 @@ fn cmp_main(args: &Args) -> io::Result<u8> {
7677
return Ok(0);
7778
}
7879

79-
let mut reader1 = plib::io::input_reader(&args.file1, true)?;
80-
let mut reader2 = plib::io::input_reader(&args.file2, true)?;
80+
let mut reader1 = input_reader(&args.file1, true)?;
81+
let mut reader2 = input_reader(&args.file2, true)?;
8182

8283
let mut lines: u64 = 1;
8384
let mut bytes: u64 = 0;
@@ -135,12 +136,11 @@ fn cmp_main(args: &Args) -> io::Result<u8> {
135136
}
136137

137138
fn main() -> ExitCode {
138-
let args = Args::parse();
139-
140-
// Initialize translation system
141139
setlocale(LocaleCategory::LcAll, "");
142-
textdomain(PROJECT_NAME).unwrap();
143-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8").unwrap();
140+
textdomain(env!("PROJECT_NAME")).unwrap();
141+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8").unwrap();
142+
143+
let args = Args::parse();
144144

145145
match cmp_main(&args) {
146146
Ok(x) => ExitCode::from(x),

file/dd.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
// SPDX-License-Identifier: MIT
88
//
99

10-
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
11-
use plib::PROJECT_NAME;
1210
use std::fs;
1311
use std::io::{self, Read, Write};
1412

13+
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
14+
1515
const DEF_BLOCK_SIZE: usize = 512;
1616

1717
const CONV_ASCII_IBM: [u8; 256] = [
@@ -383,8 +383,8 @@ fn parse_cmdline(args: &[String]) -> Result<Config, Box<dyn std::error::Error>>
383383

384384
fn main() -> Result<(), Box<dyn std::error::Error>> {
385385
setlocale(LocaleCategory::LcAll, "");
386-
textdomain(PROJECT_NAME)?;
387-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
386+
textdomain(env!("PROJECT_NAME"))?;
387+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
388388

389389
let args: Vec<String> = std::env::args().skip(1).collect();
390390
let config = parse_cmdline(&args)?;

file/file.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@
99

1010
mod magic;
1111

12-
use crate::magic::{get_type_from_magic_file_dbs, DEFAULT_MAGIC_FILE};
12+
use std::fs::read_link;
13+
use std::os::unix::fs::FileTypeExt;
14+
use std::path::PathBuf;
15+
use std::{fs, io};
1316

1417
use clap::Parser;
1518
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
16-
use plib::PROJECT_NAME;
17-
use std::{
18-
fs::{self, read_link},
19-
io,
20-
os::unix::fs::FileTypeExt,
21-
path::PathBuf,
22-
};
19+
20+
use crate::magic::{get_type_from_magic_file_dbs, DEFAULT_MAGIC_FILE};
2321

2422
#[derive(Parser)]
2523
#[command(
@@ -192,12 +190,11 @@ fn analyze_file(mut path: String, args: &Args, magic_files: &Vec<PathBuf>) {
192190
}
193191

194192
fn main() -> Result<(), Box<dyn std::error::Error>> {
195-
let args = Args::parse();
196-
197-
// Initialize translation system
198193
setlocale(LocaleCategory::LcAll, "");
199-
textdomain(PROJECT_NAME).unwrap();
200-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8").unwrap();
194+
textdomain(env!("PROJECT_NAME"))?;
195+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
196+
197+
let args = Args::parse();
201198

202199
let magic_files = get_magic_files(&args);
203200

file/find.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
// SPDX-License-Identifier: MIT
88
//
99

10-
use gettextrs::{bind_textdomain_codeset, textdomain};
11-
use plib::PROJECT_NAME;
12-
use regex::Regex;
1310
use std::collections::HashSet;
1411
use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt};
1512
use std::path::PathBuf;
1613
use std::{env, fs};
14+
15+
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
16+
use regex::Regex;
1717
use walkdir::{DirEntry, WalkDir};
1818

1919
#[derive(Clone)]
@@ -484,8 +484,9 @@ fn find(args: Vec<String>) -> Result<(), String> {
484484
}
485485

486486
fn main() -> Result<(), Box<dyn std::error::Error>> {
487-
textdomain(PROJECT_NAME)?;
488-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
487+
setlocale(LocaleCategory::LcAll, "");
488+
textdomain(env!("PROJECT_NAME"))?;
489+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
489490

490491
let args: Vec<String> = env::args().collect();
491492

file/od.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
// file in the root directory of this project.
77
// SPDX-License-Identifier: MIT
88
//
9-
//
10-
use crate::io::ErrorKind;
11-
use clap::Parser;
12-
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
13-
use plib::PROJECT_NAME;
9+
1410
use std::fs::File;
1511
use std::io::{self, BufReader, Error, Read, Seek, SeekFrom};
1612
use std::num::ParseIntError;
1713
use std::path::PathBuf;
1814
use std::slice::Chunks;
1915
use std::str::FromStr;
2016

17+
use clap::Parser;
18+
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
19+
20+
use crate::io::ErrorKind;
21+
2122
#[derive(Parser)]
2223
#[command(version, about = gettext("od - dump files in octal and other formats"))]
2324
struct Args {
@@ -1138,10 +1139,11 @@ fn od(args: &Args) -> Result<(), Box<dyn std::error::Error>> {
11381139

11391140
fn main() -> Result<(), Box<dyn std::error::Error>> {
11401141
setlocale(LocaleCategory::LcAll, "");
1141-
textdomain(PROJECT_NAME)?;
1142-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
1142+
textdomain(env!("PROJECT_NAME"))?;
1143+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
11431144

11441145
let mut args = Args::parse();
1146+
11451147
args.validate_args()?;
11461148
let mut exit_code = 0;
11471149

0 commit comments

Comments
 (0)