Skip to content

Commit

Permalink
Merge branch 'csplit_suppressed_matched' of github.com:Felle33/coreut…
Browse files Browse the repository at this point in the history
…ils into csplit_suppressed_matched
  • Loading branch information
Felle33 committed Jan 8, 2025
2 parents 2796cc1 + c1b9509 commit c1901fc
Show file tree
Hide file tree
Showing 25 changed files with 396 additions and 492 deletions.
33 changes: 17 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,16 @@ impl Options {
};
let update_mode = update_control::determine_update_mode(matches);

if backup_mode != BackupMode::NoBackup
&& matches
.get_one::<String>(update_control::arguments::OPT_UPDATE)
.map_or(false, |v| v == "none" || v == "none-fail")
{
return Err(Error::InvalidArgument(
"--backup is mutually exclusive with -n or --update=none-fail".to_string(),
));
}

let backup_suffix = backup_control::determine_backup_suffix(matches);

let overwrite = OverwriteMode::from_matches(matches);
Expand Down
23 changes: 10 additions & 13 deletions src/uu/csplit/src/csplit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use clap::{crate_version, Arg, ArgAction, ArgMatches, Command};
use regex::Regex;
use uucore::display::Quotable;
use uucore::error::{FromIo, UResult};
use uucore::{crash_if_err, format_usage, help_about, help_section, help_usage};
use uucore::{format_usage, help_about, help_section, help_usage};

mod csplit_error;
mod patterns;
Expand Down Expand Up @@ -51,26 +51,23 @@ pub struct CsplitOptions {
}

impl CsplitOptions {
fn new(matches: &ArgMatches) -> Self {
fn new(matches: &ArgMatches) -> Result<Self, CsplitError> {
let keep_files = matches.get_flag(options::KEEP_FILES);
let quiet = matches.get_flag(options::QUIET);
let elide_empty_files = matches.get_flag(options::ELIDE_EMPTY_FILES);
let suppress_matched = matches.get_flag(options::SUPPRESS_MATCHED);

Self {
split_name: crash_if_err!(
1,
SplitName::new(
matches.get_one::<String>(options::PREFIX).cloned(),
matches.get_one::<String>(options::SUFFIX_FORMAT).cloned(),
matches.get_one::<String>(options::DIGITS).cloned()
)
),
Ok(Self {
split_name: SplitName::new(
matches.get_one::<String>(options::PREFIX).cloned(),
matches.get_one::<String>(options::SUFFIX_FORMAT).cloned(),
matches.get_one::<String>(options::DIGITS).cloned(),
)?,
keep_files,
quiet,
elide_empty_files,
suppress_matched,
}
})
}
}

Expand Down Expand Up @@ -578,7 +575,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
.unwrap()
.map(|s| s.to_string())
.collect();
let options = CsplitOptions::new(&matches);
let options = CsplitOptions::new(&matches)?;
if file_name == "-" {
let stdin = io::stdin();
Ok(csplit(&options, &patterns, stdin.lock())?)
Expand Down
56 changes: 29 additions & 27 deletions src/uu/echo/src/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
// file that was distributed with this source code.

use clap::builder::ValueParser;
use clap::parser::ValuesRef;
use clap::{crate_version, Arg, ArgAction, Command};
use clap::{crate_version, Arg, ArgAction, ArgMatches, Command};
use std::env;
use std::ffi::{OsStr, OsString};
use std::io::{self, StdoutLock, Write};
use std::iter::Peekable;
Expand Down Expand Up @@ -272,35 +272,37 @@ fn handle_double_hyphens(args: impl uucore::Args) -> impl uucore::Args {
result.into_iter()
}

fn collect_args(matches: &ArgMatches) -> Vec<OsString> {
matches
.get_many::<OsString>(options::STRING)
.map_or_else(Vec::new, |values| values.cloned().collect())
}

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().get_matches_from(handle_double_hyphens(args));
let is_posixly_correct = env::var("POSIXLY_CORRECT").is_ok();

// TODO
// "If the POSIXLY_CORRECT environment variable is set, then when echo’s first argument is not -n it outputs option-like arguments instead of treating them as options."
// https://www.gnu.org/software/coreutils/manual/html_node/echo-invocation.html
let (args, trailing_newline, escaped) = if is_posixly_correct {
let mut args_iter = args.skip(1).peekable();

let trailing_newline = !matches.get_flag(options::NO_NEWLINE);
let escaped = matches.get_flag(options::ENABLE_BACKSLASH_ESCAPE);
if args_iter.peek() == Some(&OsString::from("-n")) {
let matches = uu_app().get_matches_from(handle_double_hyphens(args_iter));
let args = collect_args(&matches);
(args, false, true)
} else {
let args: Vec<_> = args_iter.collect();
(args, true, true)
}
} else {
let matches = uu_app().get_matches_from(handle_double_hyphens(args.into_iter()));
let trailing_newline = !matches.get_flag(options::NO_NEWLINE);
let escaped = matches.get_flag(options::ENABLE_BACKSLASH_ESCAPE);
let args = collect_args(&matches);
(args, trailing_newline, escaped)
};

let mut stdout_lock = io::stdout().lock();

match matches.get_many::<OsString>(options::STRING) {
Some(arguments_after_options) => {
execute(
&mut stdout_lock,
trailing_newline,
escaped,
arguments_after_options,
)?;
}
None => {
// No strings to print, so just handle newline setting
if trailing_newline {
stdout_lock.write_all(b"\n")?;
}
}
}
execute(&mut stdout_lock, args, trailing_newline, escaped)?;

Ok(())
}
Expand Down Expand Up @@ -348,11 +350,11 @@ pub fn uu_app() -> Command {

fn execute(
stdout_lock: &mut StdoutLock,
arguments_after_options: Vec<OsString>,
trailing_newline: bool,
escaped: bool,
arguments_after_options: ValuesRef<'_, OsString>,
) -> UResult<()> {
for (i, input) in arguments_after_options.enumerate() {
for (i, input) in arguments_after_options.into_iter().enumerate() {
let Some(bytes) = bytes_from_os_string(input.as_os_str()) else {
return Err(USimpleError::new(
1,
Expand Down
23 changes: 13 additions & 10 deletions src/uu/env/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,16 +539,19 @@ impl EnvAppData {
}
return Err(exit.code().unwrap().into());
}
Err(ref err)
if (err.kind() == io::ErrorKind::NotFound)
|| (err.kind() == io::ErrorKind::InvalidInput) =>
{
return Err(self.make_error_no_such_file_or_dir(prog.deref()));
}
Err(e) => {
uucore::show_error!("unknown error: {:?}", e);
return Err(126.into());
}
Err(ref err) => match err.kind() {
io::ErrorKind::NotFound | io::ErrorKind::InvalidInput => {
return Err(self.make_error_no_such_file_or_dir(prog.deref()));
}
io::ErrorKind::PermissionDenied => {
uucore::show_error!("{}: Permission denied", prog.quote());
return Err(126.into());
}
_ => {
uucore::show_error!("unknown error: {:?}", err);
return Err(126.into());
}
},
Ok(_) => (),
}
Ok(())
Expand Down
1 change: 1 addition & 0 deletions src/uu/head/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ path = "src/head.rs"
[dependencies]
clap = { workspace = true }
memchr = { workspace = true }
thiserror = { workspace = true }
uucore = { workspace = true, features = ["ringbuffer", "lines", "fs"] }

[[bin]]
Expand Down
Loading

0 comments on commit c1901fc

Please sign in to comment.