-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcoreutils.rs
More file actions
152 lines (137 loc) · 5.37 KB
/
coreutils.rs
File metadata and controls
152 lines (137 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use clap::Command;
use coreutils::validation;
use itertools::Itertools as _;
use std::cmp;
use std::ffi::OsString;
use std::io::{self, Write};
use std::process;
use uucore::Args;
const VERSION: &str = env!("CARGO_PKG_VERSION");
include!(concat!(env!("OUT_DIR"), "/uutils_map.rs"));
#[cfg(unix)]
uucore::init_startup_state_capture!();
fn usage<T>(utils: &UtilityMap<T>, name: &str) {
println!("{name} {VERSION} (multi-call binary)\n");
println!("Usage: {name} [function [arguments...]]");
println!(" {name} --list");
println!();
#[cfg(feature = "feat_common_core")]
{
println!("Functions:");
println!(" '<uutils>' [arguments...]");
println!();
}
println!("Options:");
println!(" --list lists all defined functions, one per row\n");
println!("Currently defined functions:\n");
let display_list = utils.keys().copied().join(", ");
let width = cmp::min(textwrap::termwidth(), 100) - 4 * 2; // (opinion/heuristic) max 100 chars wide with 4 character side indentions
println!(
"{}",
textwrap::indent(&textwrap::fill(&display_list, width), " ")
);
}
#[allow(clippy::cognitive_complexity)]
fn main() {
#[cfg(unix)]
if !uucore::signals::sigpipe_was_ignored() {
let _ = uucore::signals::enable_pipe_errors();
}
uucore::panic::mute_sigpipe_panic();
let utils = util_map();
let mut args = uucore::args_os();
let binary = validation::binary_path(&mut args);
let binary_as_util = validation::name(&binary).unwrap_or_else(|| {
usage(&utils, "<unknown binary name>");
process::exit(0);
});
// binary name ends with util name?
let is_coreutils = binary_as_util.ends_with("utils");
let matched_util = utils
.keys()
.filter(|&&u| binary_as_util.ends_with(u) && !is_coreutils)
.max_by_key(|u| u.len()); //Prefer stty more than tty. *utils is not ls
let util_name = if let Some(&util) = matched_util {
Some(OsString::from(util))
} else if is_coreutils || binary_as_util.ends_with("box") {
// todo: Remove support of "*box" from binary
uucore::set_utility_is_second_arg();
args.next()
} else {
validation::not_found(&OsString::from(binary_as_util));
};
// 0th argument equals util name?
if let Some(util_os) = util_name {
let Some(util) = util_os.to_str() else {
validation::not_found(&util_os)
};
match util {
"--list" => {
// If --help is also present, show usage instead of list
if args.any(|arg| arg == "--help" || arg == "-h") {
usage(&utils, binary_as_util);
process::exit(0);
}
let utils: Vec<_> = utils.keys().collect();
for util in utils {
println!("{util}");
}
process::exit(0);
}
"--version" | "-V" => {
println!("{binary_as_util} {VERSION} (multi-call binary)");
process::exit(0);
}
// Not a special command: fallthrough to calling a util
_ => {}
}
match utils.get(util) {
Some(&(uumain, _)) => {
// TODO: plug the deactivation of the translation
// and load the English strings directly at compilation time in the
// binary to avoid the load of the flt
// Could be something like:
// #[cfg(not(feature = "only_english"))]
validation::setup_localization_or_exit(util);
process::exit(uumain(vec![util_os].into_iter().chain(args)));
}
None => {
if util == "--help" || util == "-h" {
// see if they want help on a specific util
if let Some(util_os) = args.next() {
let Some(util) = util_os.to_str() else {
validation::not_found(&util_os)
};
match utils.get(util) {
Some(&(uumain, _)) => {
let code = uumain(
vec![util_os, OsString::from("--help")]
.into_iter()
.chain(args),
);
io::stdout().flush().expect("could not flush stdout");
process::exit(code);
}
None => validation::not_found(&util_os),
}
}
usage(&utils, binary_as_util);
process::exit(0);
} else if util.starts_with('-') {
// Argument looks like an option but wasn't recognized
validation::unrecognized_option(binary_as_util, &util_os);
} else {
validation::not_found(&util_os);
}
}
}
} else {
// no arguments provided
usage(&utils, binary_as_util);
process::exit(0);
}
}