Skip to content

Commit e99af00

Browse files
committed
process: Use compile-time macro env!
1 parent 8a06a6c commit e99af00

File tree

8 files changed

+110
-115
lines changed

8 files changed

+110
-115
lines changed

process/env.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
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::collections::HashMap;
1411
use std::env;
1512
use std::io;
1613
use std::os::unix::process::CommandExt;
1714
use std::process::{Command, Stdio};
1815

16+
use clap::Parser;
17+
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
18+
1919
#[derive(Parser)]
2020
#[command(version, about = gettext("env - set the environment for command invocation"))]
2121
struct Args {
@@ -92,12 +92,11 @@ fn exec_util(envs: HashMap<String, String>, util_args: Vec<String>) -> io::Resul
9292
}
9393

9494
fn main() -> Result<(), Box<dyn std::error::Error>> {
95-
// parse command line arguments
96-
let args = Args::parse();
97-
9895
setlocale(LocaleCategory::LcAll, "");
99-
textdomain(PROJECT_NAME)?;
100-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
96+
textdomain(env!("PROJECT_NAME"))?;
97+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
98+
99+
let args = Args::parse();
101100

102101
let (envs, util_args) = separate_ops(&args.operands);
103102
let new_env = merge_env(&envs, args.ignore_env);

process/fuser.rs

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,18 @@
77
// SPDX-License-Identifier: MIT
88
//
99

10-
use clap::{CommandFactory, Parser};
11-
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
12-
use plib::PROJECT_NAME;
10+
use std::collections::BTreeMap;
11+
use std::ffi::CStr;
1312
use std::fs::{metadata, Metadata};
1413
use std::io::{self, Write};
14+
use std::os::unix::fs::MetadataExt;
15+
use std::path::{Path, PathBuf};
1516
use std::sync::mpsc;
1617
use std::thread;
1718
use std::time::Duration;
18-
use std::{
19-
collections::BTreeMap,
20-
ffi::CStr,
21-
os::unix::fs::MetadataExt,
22-
path::{Path, PathBuf},
23-
};
19+
20+
use clap::{CommandFactory, Parser};
21+
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
2422

2523
const NAME_FIELD: usize = 20;
2624

@@ -1358,39 +1356,6 @@ struct Args {
13581356
/// A pathname on which the file or file system is to be reported.
13591357
file: Vec<PathBuf>,
13601358
}
1361-
fn main() -> Result<(), Box<dyn std::error::Error>> {
1362-
setlocale(LocaleCategory::LcAll, "");
1363-
textdomain(PROJECT_NAME)?;
1364-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
1365-
1366-
let Args {
1367-
mount, user, file, ..
1368-
} = Args::try_parse().unwrap_or_else(|err| match err.kind() {
1369-
clap::error::ErrorKind::DisplayHelp | clap::error::ErrorKind::DisplayVersion => {
1370-
print!("{err}");
1371-
std::process::exit(1);
1372-
}
1373-
_ => {
1374-
let mut stdout = std::io::stdout();
1375-
let mut cmd = Args::command();
1376-
eprintln!("No process specification given");
1377-
cmd.write_help(&mut stdout).unwrap();
1378-
std::process::exit(1);
1379-
}
1380-
});
1381-
1382-
#[cfg(target_os = "linux")]
1383-
let mut names = linux::get_matched_procs(file, mount)?;
1384-
1385-
#[cfg(target_os = "macos")]
1386-
let mut names = macos::get_matched_procs(file, mount)?;
1387-
1388-
for name in names.iter_mut() {
1389-
print_matches(name, user)?;
1390-
}
1391-
1392-
std::process::exit(0);
1393-
}
13941359

13951360
/// Prints process matches for a given `Names` object to `stderr` and `stdout`.
13961361
///
@@ -1515,3 +1480,37 @@ fn timeout(path: &str, seconds: u32) -> Result<Metadata, io::Error> {
15151480
}
15161481
}
15171482
}
1483+
1484+
fn main() -> Result<(), Box<dyn std::error::Error>> {
1485+
setlocale(LocaleCategory::LcAll, "");
1486+
textdomain(env!("PROJECT_NAME"))?;
1487+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
1488+
1489+
let Args {
1490+
mount, user, file, ..
1491+
} = Args::try_parse().unwrap_or_else(|err| match err.kind() {
1492+
clap::error::ErrorKind::DisplayHelp | clap::error::ErrorKind::DisplayVersion => {
1493+
print!("{err}");
1494+
std::process::exit(1);
1495+
}
1496+
_ => {
1497+
let mut stdout = std::io::stdout();
1498+
let mut cmd = Args::command();
1499+
eprintln!("No process specification given");
1500+
cmd.write_help(&mut stdout).unwrap();
1501+
std::process::exit(1);
1502+
}
1503+
});
1504+
1505+
#[cfg(target_os = "linux")]
1506+
let mut names = linux::get_matched_procs(file, mount)?;
1507+
1508+
#[cfg(target_os = "macos")]
1509+
let mut names = macos::get_matched_procs(file, mount)?;
1510+
1511+
for name in names.iter_mut() {
1512+
print_matches(name, user)?;
1513+
}
1514+
1515+
std::process::exit(0);
1516+
}

process/nice.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
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;
1411
use std::os::unix::process::CommandExt;
1512
use std::process::{Command, Stdio};
1613

14+
use clap::Parser;
15+
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
16+
1717
#[derive(Parser)]
1818
#[command(
1919
version,
@@ -51,12 +51,11 @@ fn exec_util(util: &str, util_args: Vec<String>) -> io::Result<()> {
5151
}
5252

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

6160
let res = unsafe { libc::nice(args.niceval) };
6261
if res < 0 {

process/nohup.rs

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,52 @@
77
// SPDX-License-Identifier: MIT
88
//
99

10-
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
11-
use libc::signal;
12-
use libc::{dup, dup2, SIGHUP, SIG_IGN};
13-
use plib::PROJECT_NAME;
1410
use std::env;
1511
use std::fs::{File, OpenOptions};
1612
use std::io::{self, IsTerminal};
1713
use std::os::unix::io::AsRawFd;
1814
use std::process::{self, Command};
1915

16+
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
17+
use libc::{dup, dup2, signal, SIGHUP, SIG_IGN};
18+
19+
enum NohupDir {
20+
Current,
21+
Home,
22+
}
23+
24+
fn get_nohup_out_file() -> Result<(File, NohupDir), io::Error> {
25+
// Attempting to open or create a nohup.out file in the current directory
26+
match OpenOptions::new()
27+
.create(true)
28+
.append(true)
29+
.open("nohup.out")
30+
{
31+
Ok(file) => Ok((file, NohupDir::Current)),
32+
Err(_) => {
33+
// If unsuccessful, attempt to create a nohup.out file in the home directory
34+
if let Some(home_dir) = dirs::home_dir() {
35+
let mut home_nohup_path = home_dir;
36+
home_nohup_path.push("nohup.out");
37+
let file = OpenOptions::new()
38+
.create(true)
39+
.append(true)
40+
.open(home_nohup_path)?;
41+
Ok((file, NohupDir::Home))
42+
} else {
43+
Err(io::Error::new(
44+
io::ErrorKind::NotFound,
45+
"Home directory not found",
46+
))
47+
}
48+
}
49+
}
50+
}
51+
2052
fn main() -> Result<(), Box<dyn std::error::Error>> {
2153
setlocale(LocaleCategory::LcAll, "");
22-
textdomain(PROJECT_NAME)?;
23-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
54+
textdomain(env!("PROJECT_NAME"))?;
55+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
2456

2557
unsafe {
2658
// Ignore the SIGHUP signal
@@ -111,36 +143,3 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
111143
}
112144
}
113145
}
114-
115-
enum NohupDir {
116-
Current,
117-
Home,
118-
}
119-
120-
fn get_nohup_out_file() -> Result<(File, NohupDir), io::Error> {
121-
// Attempting to open or create a nohup.out file in the current directory
122-
match OpenOptions::new()
123-
.create(true)
124-
.append(true)
125-
.open("nohup.out")
126-
{
127-
Ok(file) => Ok((file, NohupDir::Current)),
128-
Err(_) => {
129-
// If unsuccessful, attempt to create a nohup.out file in the home directory
130-
if let Some(home_dir) = dirs::home_dir() {
131-
let mut home_nohup_path = home_dir;
132-
home_nohup_path.push("nohup.out");
133-
let file = OpenOptions::new()
134-
.create(true)
135-
.append(true)
136-
.open(home_nohup_path)?;
137-
Ok((file, NohupDir::Home))
138-
} else {
139-
Err(io::Error::new(
140-
io::ErrorKind::NotFound,
141-
"Home directory not found",
142-
))
143-
}
144-
}
145-
}
146-
}

process/renice.rs

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

10+
use std::ffi::CString;
11+
1012
use clap::Parser;
1113
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
1214
use libc::{getpwnam, passwd};
1315
use plib::priority::{getpriority, setpriority};
14-
use plib::PROJECT_NAME;
15-
use std::ffi::CString;
1616

1717
const PRIO_MIN: i32 = -20;
1818
const PRIO_MAX: i32 = 20;
@@ -101,12 +101,11 @@ fn parse_id(which: u32, input: &str) -> Result<u32, &'static str> {
101101
}
102102

103103
fn main() -> Result<(), Box<dyn std::error::Error>> {
104-
// parse command line arguments
105-
let args = Args::parse();
106-
107104
setlocale(LocaleCategory::LcAll, "");
108-
textdomain(PROJECT_NAME)?;
109-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
105+
textdomain(env!("PROJECT_NAME"))?;
106+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
107+
108+
let args = Args::parse();
110109

111110
// which class of priority to modify
112111
let which: u32 = {

process/tests/fuser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use plib::{run_test_with_checker, TestPlan};
1+
use plib::testing::{run_test_with_checker, TestPlan};
22
use std::process::Output;
33

44
mod basic;

process/tests/xargs/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// SPDX-License-Identifier: MIT
88
//
99

10-
use plib::{run_test, TestPlan};
10+
use plib::testing::{run_test, TestPlan};
1111

1212
fn xargs_test(test_data: &str, expected_output: &str, args: Vec<&str>) {
1313
run_test(TestPlan {

process/xargs.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
// - write tests
1515
//
1616

17-
use clap::Parser;
18-
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
19-
use plib::PROJECT_NAME;
2017
use std::io::{self, Read};
2118
use std::process::{Command, Stdio};
2219

20+
use clap::Parser;
21+
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
22+
use plib::BUFSZ;
23+
2324
const ARG_MAX: i32 = 131072; // arbitrary. todo: discover actual value
2425
const MAX_ARGS_BYTES: usize = ARG_MAX as usize - 2048;
2526

@@ -304,7 +305,7 @@ impl ParseState {
304305
fn read_and_spawn(args: &Args) -> io::Result<()> {
305306
let mut state = ParseState::new(args);
306307

307-
let mut buffer = [0; plib::BUFSZ];
308+
let mut buffer = [0; BUFSZ];
308309

309310
// read stdin until EOF
310311
loop {
@@ -346,12 +347,11 @@ fn read_and_spawn(args: &Args) -> io::Result<()> {
346347
}
347348

348349
fn main() -> Result<(), Box<dyn std::error::Error>> {
349-
// parse command line arguments
350-
let args = Args::parse();
351-
352350
setlocale(LocaleCategory::LcAll, "");
353-
textdomain(PROJECT_NAME)?;
354-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
351+
textdomain(env!("PROJECT_NAME"))?;
352+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
353+
354+
let args = Args::parse();
355355

356356
read_and_spawn(&args)?;
357357

0 commit comments

Comments
 (0)