Skip to content

Commit 8fe9286

Browse files
authored
Merge pull request #360 from fox0/ennv
text: Use compile-time macro env!
2 parents db2f8f2 + bc51be6 commit 8fe9286

File tree

37 files changed

+163
-173
lines changed

37 files changed

+163
-173
lines changed

text/asa.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
// - add tests
1212
//
1313

14-
use clap::Parser;
15-
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
16-
use plib::PROJECT_NAME;
1714
use std::io::{self, BufRead};
1815
use std::path::PathBuf;
1916

17+
use clap::Parser;
18+
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
19+
use plib::io::input_reader;
20+
2021
/// asa - interpret carriage-control characters
2122
#[derive(Parser)]
2223
#[command(version, about)]
@@ -69,7 +70,7 @@ impl AsaState {
6970
}
7071

7172
fn asa_file(pathname: &PathBuf) -> io::Result<()> {
72-
let mut reader = plib::io::input_reader(pathname, false)?;
73+
let mut reader = input_reader(pathname, false)?;
7374
let mut line_no: usize = 0;
7475
let mut state = AsaState::default();
7576

@@ -127,12 +128,11 @@ fn asa_file(pathname: &PathBuf) -> io::Result<()> {
127128
}
128129

129130
fn main() -> Result<(), Box<dyn std::error::Error>> {
130-
// parse command line arguments
131-
let mut args = Args::parse();
132-
133131
setlocale(LocaleCategory::LcAll, "");
134-
textdomain(PROJECT_NAME)?;
135-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
132+
textdomain(env!("PROJECT_NAME"))?;
133+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
134+
135+
let mut args = Args::parse();
136136

137137
// if no files, read from stdin
138138
if args.files.is_empty() {

text/comm.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use clap::Parser;
1111
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
12-
use plib::PROJECT_NAME;
1312
use std::fs;
1413
use std::io::{self, BufRead, Write};
1514
use std::path::PathBuf;
@@ -146,12 +145,11 @@ fn args_mask(args: &Args) -> u32 {
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
let mask = args_mask(&args);
157155

text/csplit.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
use clap::Parser;
1414
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
15-
use plib::PROJECT_NAME;
1615
use regex::Regex;
1716
use std::fs::{self, File, OpenOptions};
1817
use std::io::{self, BufRead, Error, ErrorKind, Read, Write};
@@ -593,12 +592,11 @@ fn parse_operands(args: &Args) -> io::Result<SplitOps> {
593592
}
594593

595594
fn main() -> Result<(), Box<dyn std::error::Error>> {
596-
// parse command line arguments
597-
let args = Args::parse();
598-
599595
setlocale(LocaleCategory::LcAll, "");
600-
textdomain(PROJECT_NAME)?;
601-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
596+
textdomain(env!("PROJECT_NAME"))?;
597+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
598+
599+
let args = Args::parse();
602600

603601
let ctx = parse_operands(&args)?;
604602

text/cut.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use std::io::{self, BufRead, Error, ErrorKind, Read};
1111

1212
use clap::Parser;
1313
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
14-
use plib::PROJECT_NAME;
1514
use std::path::PathBuf;
1615

1716
/// Cut - cut out selected fields of each line of a file
@@ -434,12 +433,11 @@ fn read_range(line: &str) -> Result<Vec<(i32, i32)>, String> {
434433
}
435434

436435
fn main() -> Result<(), Box<dyn std::error::Error>> {
437-
// parse command line arguments
438-
let args = Args::parse();
439-
440436
setlocale(LocaleCategory::LcAll, "");
441-
textdomain(PROJECT_NAME)?;
442-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
437+
textdomain(env!("PROJECT_NAME"))?;
438+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
439+
440+
let args = Args::parse();
443441

444442
let mut exit_code = 0;
445443

text/diff.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use diff_util::{
2525
functions::check_existance,
2626
};
2727
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
28-
use plib::PROJECT_NAME;
2928

3029
/// diff - compare two files
3130
#[derive(Parser, Clone)]
@@ -104,10 +103,6 @@ impl From<&Args> for OutputFormat {
104103
}
105104

106105
fn check_difference(args: Args) -> io::Result<DiffExitStatus> {
107-
setlocale(LocaleCategory::LcAll, "");
108-
textdomain(PROJECT_NAME)?;
109-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
110-
111106
let path1 = PathBuf::from(&args.file1);
112107
let path2 = PathBuf::from(&args.file2);
113108

@@ -145,7 +140,10 @@ fn check_difference(args: Args) -> io::Result<DiffExitStatus> {
145140
}
146141

147142
fn main() -> DiffExitStatus {
148-
// parse command line arguments
143+
setlocale(LocaleCategory::LcAll, "");
144+
textdomain(env!("PROJECT_NAME")).unwrap();
145+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8").unwrap();
146+
149147
let args = Args::parse();
150148

151149
let result = check_difference(args);

text/expand.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
// SPDX-License-Identifier: MIT
88
//
99

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

13+
use clap::Parser;
14+
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
15+
use plib::io::input_stream;
16+
use plib::BUFSZ;
17+
1618
/// expand - convert tabs to spaces
1719
#[derive(Parser)]
1820
#[command(version, about)]
@@ -66,9 +68,9 @@ fn space_out(column: &mut usize, writer: &mut BufWriter<dyn Write>) -> io::Resul
6668

6769
fn expand_file(tablist: &TabList, pathname: &PathBuf) -> io::Result<()> {
6870
// open file, or stdin
69-
let mut file = plib::io::input_stream(pathname, false)?;
71+
let mut file = input_stream(pathname, false)?;
7072

71-
let mut raw_buffer = [0; plib::BUFSZ];
73+
let mut raw_buffer = [0; BUFSZ];
7274
let mut writer = BufWriter::new(io::stdout());
7375
let mut column: usize = 1;
7476
let mut cur_stop = 0;
@@ -130,12 +132,11 @@ fn expand_file(tablist: &TabList, pathname: &PathBuf) -> io::Result<()> {
130132
}
131133

132134
fn main() -> Result<(), Box<dyn std::error::Error>> {
133-
// parse command line arguments
134-
let mut args = Args::parse();
135-
136135
setlocale(LocaleCategory::LcAll, "");
137-
textdomain(PROJECT_NAME)?;
138-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
136+
textdomain(env!("PROJECT_NAME"))?;
137+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
138+
139+
let mut args = Args::parse();
139140

140141
let tablist = {
141142
if let Some(ref tablist) = args.tablist {

text/fold.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
// SPDX-License-Identifier: MIT
88
//
99

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

13+
use clap::Parser;
14+
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
15+
use plib::io::input_stream;
16+
use plib::BUFSZ;
17+
1618
const TABSTOP: usize = 8;
1719

1820
/// fold - filter for folding lines
@@ -100,9 +102,9 @@ fn find_last_blank(v: &[u8]) -> Option<usize> {
100102

101103
fn fold_file(args: &Args, pathname: &PathBuf) -> io::Result<()> {
102104
// open file, or stdin
103-
let mut file = plib::io::input_stream(pathname, false)?;
105+
let mut file = input_stream(pathname, false)?;
104106

105-
let mut raw_buffer = [0; plib::BUFSZ];
107+
let mut raw_buffer = [0; BUFSZ];
106108
let mut state = OutputState::new(args);
107109

108110
loop {
@@ -165,12 +167,11 @@ fn fold_file(args: &Args, pathname: &PathBuf) -> io::Result<()> {
165167
}
166168

167169
fn main() -> Result<(), Box<dyn std::error::Error>> {
168-
// parse command line arguments
169-
let mut args = Args::parse();
170-
171170
setlocale(LocaleCategory::LcAll, "");
172-
textdomain(PROJECT_NAME)?;
173-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
171+
textdomain(env!("PROJECT_NAME"))?;
172+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
173+
174+
let mut args = Args::parse();
174175

175176
// if no files, read from stdin
176177
if args.files.is_empty() {

text/grep.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
//
99

1010
use clap::Parser;
11-
use gettextrs::{bind_textdomain_codeset, textdomain};
11+
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
1212
use libc::{regcomp, regex_t, regexec, regfree, REG_EXTENDED, REG_ICASE, REG_NOMATCH};
13-
use plib::PROJECT_NAME;
1413
use std::{
1514
ffi::CString,
1615
fs::File,
@@ -488,9 +487,10 @@ impl GrepModel {
488487
// 1 - No lines were selected.
489488
// >1 - An error occurred.
490489
fn main() -> Result<(), Box<dyn std::error::Error>> {
491-
textdomain(PROJECT_NAME)?;
492-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
493-
// Parse command line arguments
490+
setlocale(LocaleCategory::LcAll, "");
491+
textdomain(env!("PROJECT_NAME"))?;
492+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
493+
494494
let mut args = Args::parse();
495495

496496
let exit_code = args

text/head.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
// SPDX-License-Identifier: MIT
88
//
99

10-
use clap::Parser;
11-
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
12-
use plib::PROJECT_NAME;
1310
use std::error::Error;
1411
use std::io::{self, Read, StdoutLock, Write};
1512
use std::path::PathBuf;
1613

14+
use clap::Parser;
15+
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
16+
use plib::io::input_stream;
17+
use plib::BUFSZ;
18+
1719
const N_C_GROUP: &str = "N_C_GROUP";
1820

1921
/// head - copy the first part of files.
@@ -48,7 +50,7 @@ fn head_file(
4850
want_header: bool,
4951
stdout_lock: &mut StdoutLock,
5052
) -> Result<(), Box<dyn Error>> {
51-
const BUFFER_SIZE: usize = plib::BUFSZ;
53+
const BUFFER_SIZE: usize = BUFSZ;
5254

5355
// print file header
5456
if want_header {
@@ -60,7 +62,7 @@ fn head_file(
6062
}
6163

6264
// open file, or stdin
63-
let mut file = plib::io::input_stream(pathname, false)?;
65+
let mut file = input_stream(pathname, false)?;
6466

6567
let mut raw_buffer = [0_u8; BUFFER_SIZE];
6668

@@ -141,7 +143,10 @@ fn head_file(
141143
}
142144

143145
fn main() -> Result<(), Box<dyn std::error::Error>> {
144-
// parse command line arguments
146+
setlocale(LocaleCategory::LcAll, "");
147+
textdomain(env!("PROJECT_NAME"))?;
148+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
149+
145150
let mut args = Args::parse();
146151

147152
// bsdutils (FreeBSD) enforces n > 0 (and c > 0)
@@ -178,10 +183,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
178183
}
179184
};
180185

181-
setlocale(LocaleCategory::LcAll, "");
182-
textdomain(PROJECT_NAME)?;
183-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
184-
185186
let files = &mut args.files;
186187

187188
// if no files, read from stdin

text/join.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use clap::Parser;
1111
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
12-
use plib::PROJECT_NAME;
1312
use std::collections::HashMap;
1413
use std::fs::File;
1514
use std::io::{self, BufRead, BufReader};
@@ -185,11 +184,11 @@ fn join(args: Args) -> Result<(), Box<dyn std::error::Error>> {
185184
}
186185

187186
fn main() -> Result<(), Box<dyn std::error::Error>> {
188-
let args = Args::parse();
189-
190187
setlocale(LocaleCategory::LcAll, "");
191-
textdomain(PROJECT_NAME)?;
192-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
188+
textdomain(env!("PROJECT_NAME"))?;
189+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
190+
191+
let args = Args::parse();
193192

194193
let mut exit_code = 0;
195194

text/nl.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use clap::{Parser, ValueEnum};
1111
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
12-
use plib::PROJECT_NAME;
1312
use regex::Regex;
1413
use std::fs;
1514
use std::io::{self, BufRead, Read};
@@ -310,6 +309,10 @@ fn nl_main(args: &Args) -> io::Result<()> {
310309
}
311310

312311
fn main() -> ExitCode {
312+
setlocale(LocaleCategory::LcAll, "");
313+
textdomain(env!("PROJECT_NAME")).unwrap();
314+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8").unwrap();
315+
313316
let mut args = Args::parse();
314317

315318
match args.section_delimiter.len() {
@@ -323,11 +326,6 @@ fn main() -> ExitCode {
323326
}
324327
}
325328

326-
// Initialize translation system
327-
setlocale(LocaleCategory::LcAll, "");
328-
textdomain(PROJECT_NAME).unwrap();
329-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8").unwrap();
330-
331329
match nl_main(&args) {
332330
Ok(_) => ExitCode::from(0),
333331
Err(_) => ExitCode::from(1),

0 commit comments

Comments
 (0)