Skip to content

text: Use compile-time macro env! #360

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 1 commit into from
Oct 25, 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
18 changes: 9 additions & 9 deletions text/asa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
// - add tests
//

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
use plib::PROJECT_NAME;
use std::io::{self, BufRead};
use std::path::PathBuf;

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
use plib::io::input_reader;

/// asa - interpret carriage-control characters
#[derive(Parser)]
#[command(version, about)]
Expand Down Expand Up @@ -69,7 +70,7 @@ impl AsaState {
}

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

Expand Down Expand Up @@ -127,12 +128,11 @@ fn asa_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 files, read from stdin
if args.files.is_empty() {
Expand Down
10 changes: 4 additions & 6 deletions text/comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
use plib::PROJECT_NAME;
use std::fs;
use std::io::{self, BufRead, Write};
use std::path::PathBuf;
Expand Down Expand Up @@ -146,12 +145,11 @@ fn args_mask(args: &Args) -> u32 {
}

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();

let mask = args_mask(&args);

Expand Down
10 changes: 4 additions & 6 deletions text/csplit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

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

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();

let ctx = parse_operands(&args)?;

Expand Down
10 changes: 4 additions & 6 deletions text/cut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::io::{self, BufRead, Error, ErrorKind, Read};

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
use plib::PROJECT_NAME;
use std::path::PathBuf;

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

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();

let mut exit_code = 0;

Expand Down
10 changes: 4 additions & 6 deletions text/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use diff_util::{
functions::check_existance,
};
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
use plib::PROJECT_NAME;

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

fn check_difference(args: Args) -> io::Result<DiffExitStatus> {
setlocale(LocaleCategory::LcAll, "");
textdomain(PROJECT_NAME)?;
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;

let path1 = PathBuf::from(&args.file1);
let path2 = PathBuf::from(&args.file2);

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

fn main() -> DiffExitStatus {
// parse command line arguments
setlocale(LocaleCategory::LcAll, "");
textdomain(env!("PROJECT_NAME")).unwrap();
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8").unwrap();

let args = Args::parse();

let result = check_difference(args);
Expand Down
21 changes: 11 additions & 10 deletions text/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
// SPDX-License-Identifier: MIT
//

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
use plib::PROJECT_NAME;
use std::io::{self, BufWriter, Read, Write};
use std::path::PathBuf;

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
use plib::io::input_stream;
use plib::BUFSZ;

/// expand - convert tabs to spaces
#[derive(Parser)]
#[command(version, about)]
Expand Down Expand Up @@ -66,9 +68,9 @@ fn space_out(column: &mut usize, writer: &mut BufWriter<dyn Write>) -> io::Resul

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

let mut raw_buffer = [0; plib::BUFSZ];
let mut raw_buffer = [0; BUFSZ];
let mut writer = BufWriter::new(io::stdout());
let mut column: usize = 1;
let mut cur_stop = 0;
Expand Down Expand Up @@ -130,12 +132,11 @@ fn expand_file(tablist: &TabList, 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();

let tablist = {
if let Some(ref tablist) = args.tablist {
Expand Down
21 changes: 11 additions & 10 deletions text/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
// SPDX-License-Identifier: MIT
//

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, 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, setlocale, textdomain, LocaleCategory};
use plib::io::input_stream;
use plib::BUFSZ;

const TABSTOP: usize = 8;

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

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

let mut raw_buffer = [0; plib::BUFSZ];
let mut raw_buffer = [0; BUFSZ];
let mut state = OutputState::new(args);

loop {
Expand Down Expand Up @@ -165,12 +167,11 @@ fn fold_file(args: &Args, 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 files, read from stdin
if args.files.is_empty() {
Expand Down
10 changes: 5 additions & 5 deletions text/grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
//

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, textdomain};
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
use libc::{regcomp, regex_t, regexec, regfree, REG_EXTENDED, REG_ICASE, REG_NOMATCH};
use plib::PROJECT_NAME;
use std::{
ffi::CString,
fs::File,
Expand Down Expand Up @@ -488,9 +487,10 @@ impl GrepModel {
// 1 - No lines were selected.
// >1 - An error occurred.
fn main() -> Result<(), Box<dyn std::error::Error>> {
textdomain(PROJECT_NAME)?;
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
// Parse command line arguments
setlocale(LocaleCategory::LcAll, "");
textdomain(env!("PROJECT_NAME"))?;
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;

let mut args = Args::parse();

let exit_code = args
Expand Down
21 changes: 11 additions & 10 deletions text/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
// SPDX-License-Identifier: MIT
//

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
use plib::PROJECT_NAME;
use std::error::Error;
use std::io::{self, Read, StdoutLock, Write};
use std::path::PathBuf;

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
use plib::io::input_stream;
use plib::BUFSZ;

const N_C_GROUP: &str = "N_C_GROUP";

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

// print file header
if want_header {
Expand All @@ -60,7 +62,7 @@ fn head_file(
}

// open file, or stdin
let mut file = plib::io::input_stream(pathname, false)?;
let mut file = input_stream(pathname, false)?;

let mut raw_buffer = [0_u8; BUFFER_SIZE];

Expand Down Expand Up @@ -141,7 +143,10 @@ fn head_file(
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
// parse command line arguments
setlocale(LocaleCategory::LcAll, "");
textdomain(env!("PROJECT_NAME"))?;
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;

let mut args = Args::parse();

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

setlocale(LocaleCategory::LcAll, "");
textdomain(PROJECT_NAME)?;
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;

let files = &mut args.files;

// if no files, read from stdin
Expand Down
9 changes: 4 additions & 5 deletions text/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
use plib::PROJECT_NAME;
use std::collections::HashMap;
use std::fs::File;
use std::io::{self, BufRead, BufReader};
Expand Down Expand Up @@ -185,11 +184,11 @@ fn join(args: Args) -> Result<(), Box<dyn std::error::Error>> {
}

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

Expand Down
10 changes: 4 additions & 6 deletions text/nl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use clap::{Parser, ValueEnum};
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
use plib::PROJECT_NAME;
use regex::Regex;
use std::fs;
use std::io::{self, BufRead, Read};
Expand Down Expand Up @@ -310,6 +309,10 @@ fn nl_main(args: &Args) -> io::Result<()> {
}

fn main() -> ExitCode {
setlocale(LocaleCategory::LcAll, "");
textdomain(env!("PROJECT_NAME")).unwrap();
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8").unwrap();

let mut args = Args::parse();

match args.section_delimiter.len() {
Expand All @@ -323,11 +326,6 @@ fn main() -> ExitCode {
}
}

// Initialize translation system
setlocale(LocaleCategory::LcAll, "");
textdomain(PROJECT_NAME).unwrap();
bind_textdomain_codeset(PROJECT_NAME, "UTF-8").unwrap();

match nl_main(&args) {
Ok(_) => ExitCode::from(0),
Err(_) => ExitCode::from(1),
Expand Down
Loading