| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
use clap::Command; |
| 7 |
use coreutils::validation::{self, exit}; |
| 8 |
use itertools::Itertools as _; |
| 9 |
use std::cmp; |
| 10 |
use std::ffi::OsString; |
| 11 |
use std::io::{self, Write}; |
| 12 |
use uucore::{Args, error::strip_errno}; |
| 13 |
|
| 14 |
const VERSION: &str = env!("CARGO_PKG_VERSION"); |
| 15 |
|
| 16 |
include!(concat!(env!("OUT_DIR"), "/uutils_map.rs")); |
| 17 |
|
| 18 |
fn usage<T>(utils: &UtilityMap<T>, name: &str) { |
| 19 |
let display_list = utils.keys().copied().join(", "); |
| 20 |
let width = cmp::min(textwrap::termwidth(), 100) - 8; // (opinion/heuristic) max 100 chars wide with 4 character side indentions |
| 21 |
let indent_list = textwrap::indent(&textwrap::fill(&display_list, width), " "); |
| 22 |
#[cfg(feature = "feat_common_core")] |
| 23 |
let common_core_string = " |
| 24 |
Functions: |
| 25 |
'<uutils>' [arguments...] |
| 26 |
|
| 27 |
"; |
| 28 |
#[cfg(not(feature = "feat_common_core"))] |
| 29 |
let common_core_string = ""; |
| 30 |
let s = format!( |
| 31 |
"{name} {VERSION} (multi-call binary) |
| 32 |
|
| 33 |
Usage: {name} [function [arguments...]] |
| 34 |
{name} --list |
| 35 |
|
| 36 |
{common_core_string}Options: |
| 37 |
--list lists all defined functions, one per row |
| 38 |
|
| 39 |
Currently defined functions: |
| 40 |
|
| 41 |
{indent_list}" |
| 42 |
); |
| 43 |
if let Err(e) = writeln!(io::stdout(), "{s}") |
| 44 |
&& e.kind() != io::ErrorKind::BrokenPipe |
| 45 |
{ |
| 46 |
let _ = writeln!(io::stderr(), "coreutils: {}", strip_errno(&e)); |
| 47 |
exit(1); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
#[allow(clippy::cognitive_complexity)] |
| 52 |
fn main() { |
| 53 |
let utils = util_map(); |
| 54 |
let mut args = uucore::args_os(); |
| 55 |
|
| 56 |
let binary = validation::binary_path(&mut args); |
| 57 |
let binary_as_util = validation::name(&binary).unwrap_or_else(|| { |
| 58 |
usage(&utils, "<unknown binary name>"); |
| 59 |
exit(0); |
| 60 |
}); |
| 61 |
|
| 62 |
// binary name ends with util name? |
| 63 |
let is_coreutils = binary_as_util.ends_with("utils"); |
| 64 |
let matched_util = utils |
| 65 |
.keys() |
| 66 |
.filter(|&&u| binary_as_util.ends_with(u) && !is_coreutils) |
| 67 |
.max_by_key(|u| u.len()); //Prefer stty more than tty. *utils is not ls |
| 68 |
|
| 69 |
let util_name = if let Some(&util) = matched_util { |
| 70 |
Some(OsString::from(util)) |
| 71 |
} else if is_coreutils || binary_as_util.ends_with("box") { |
| 72 |
// todo: Remove support of "*box" from binary |
| 73 |
uucore::set_utility_is_second_arg(); |
| 74 |
args.next() |
| 75 |
} else { |
| 76 |
validation::not_found(&OsString::from(binary_as_util)); |
| 77 |
}; |
| 78 |
|
| 79 |
// 0th argument equals util name? |
| 80 |
if let Some(util_os) = util_name { |
| 81 |
let Some(util) = util_os.to_str() else { |
| 82 |
validation::not_found(&util_os) |
| 83 |
}; |
| 84 |
|
| 85 |
match util { |
| 86 |
"--list" => { |
| 87 |
// we should fail with additional args https://github.com/uutils/coreutils/issues/11383#issuecomment-4082564058 |
| 88 |
if args.next().is_some() { |
| 89 |
let _ = writeln!(io::stderr(), "coreutils: invalid argument"); |
| 90 |
exit(1); |
| 91 |
} |
| 92 |
let mut out = io::stdout().lock(); |
| 93 |
for util in utils.keys() { |
| 94 |
if let Err(e) = writeln!(out, "{util}") |
| 95 |
&& e.kind() != io::ErrorKind::BrokenPipe |
| 96 |
{ |
| 97 |
let _ = writeln!(io::stderr(), "coreutils: {}", strip_errno(&e)); |
| 98 |
exit(1); |
| 99 |
} |
| 100 |
} |
| 101 |
exit(0); |
| 102 |
} |
| 103 |
"--version" | "-V" => { |
| 104 |
if let Err(e) = writeln!(io::stdout(), "coreutils {VERSION} (multi-call binary)") |
| 105 |
&& e.kind() != io::ErrorKind::BrokenPipe |
| 106 |
{ |
| 107 |
let _ = writeln!(io::stderr(), "coreutils: {}", strip_errno(&e)); |
| 108 |
exit(1); |
| 109 |
} |
| 110 |
exit(0); |
| 111 |
} |
| 112 |
// Not a special command: fallthrough to calling a util |
| 113 |
_ => {} |
| 114 |
} |
| 115 |
|
| 116 |
if let Some(&(uumain, _)) = utils.get(util) { |
| 117 |
// TODO: plug the deactivation of the translation |
| 118 |
// and load the English strings directly at compilation time in the |
| 119 |
// binary to avoid the load of the flt |
| 120 |
// Could be something like: |
| 121 |
// #[cfg(not(feature = "only_english"))] |
| 122 |
validation::setup_localization_or_exit(util); |
| 123 |
exit(uumain(vec![util_os].into_iter().chain(args))); |
| 124 |
} |
| 125 |
// GNU coreutils --help string shows help for coreutils |
| 126 |
if util == "--help" || util == "-h" { |
| 127 |
usage(&utils, binary_as_util); |
| 128 |
exit(0); |
| 129 |
} else if util.starts_with('-') { |
| 130 |
// Argument looks like an option but wasn't recognized |
| 131 |
validation::unrecognized_option(binary_as_util, &util_os); |
| 132 |
} |
| 133 |
validation::not_found(&util_os); |
| 134 |
} |
| 135 |
// GNU just fails, but busybox tests needs usage |
| 136 |
// todo: patch the test suite instead |
| 137 |
if binary_as_util.ends_with("box") { |
| 138 |
usage(&utils, binary_as_util); |
| 139 |
} else { |
| 140 |
let _ = writeln!(io::stderr(), "coreutils: missing argument"); |
| 141 |
} |
| 142 |
exit(1); |
| 143 |
} |