mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 19:17:43 +00:00
Merge pull request #7059 from cakebaker/echo_posixly_correct
echo: add support for `POSIXLY_CORRECT`
This commit is contained in:
commit
c201f38212
2 changed files with 81 additions and 27 deletions
|
@ -4,8 +4,8 @@
|
||||||
// file that was distributed with this source code.
|
// file that was distributed with this source code.
|
||||||
|
|
||||||
use clap::builder::ValueParser;
|
use clap::builder::ValueParser;
|
||||||
use clap::parser::ValuesRef;
|
use clap::{crate_version, Arg, ArgAction, ArgMatches, Command};
|
||||||
use clap::{crate_version, Arg, ArgAction, Command};
|
use std::env;
|
||||||
use std::ffi::{OsStr, OsString};
|
use std::ffi::{OsStr, OsString};
|
||||||
use std::io::{self, StdoutLock, Write};
|
use std::io::{self, StdoutLock, Write};
|
||||||
use std::iter::Peekable;
|
use std::iter::Peekable;
|
||||||
|
@ -272,35 +272,37 @@ fn handle_double_hyphens(args: impl uucore::Args) -> impl uucore::Args {
|
||||||
result.into_iter()
|
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]
|
#[uucore::main]
|
||||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
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
|
let (args, trailing_newline, escaped) = if is_posixly_correct {
|
||||||
// "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."
|
let mut args_iter = args.skip(1).peekable();
|
||||||
// https://www.gnu.org/software/coreutils/manual/html_node/echo-invocation.html
|
|
||||||
|
|
||||||
let trailing_newline = !matches.get_flag(options::NO_NEWLINE);
|
if args_iter.peek() == Some(&OsString::from("-n")) {
|
||||||
let escaped = matches.get_flag(options::ENABLE_BACKSLASH_ESCAPE);
|
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();
|
let mut stdout_lock = io::stdout().lock();
|
||||||
|
execute(&mut stdout_lock, args, trailing_newline, escaped)?;
|
||||||
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")?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -348,11 +350,11 @@ pub fn uu_app() -> Command {
|
||||||
|
|
||||||
fn execute(
|
fn execute(
|
||||||
stdout_lock: &mut StdoutLock,
|
stdout_lock: &mut StdoutLock,
|
||||||
|
arguments_after_options: Vec<OsString>,
|
||||||
trailing_newline: bool,
|
trailing_newline: bool,
|
||||||
escaped: bool,
|
escaped: bool,
|
||||||
arguments_after_options: ValuesRef<'_, OsString>,
|
|
||||||
) -> UResult<()> {
|
) -> 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 {
|
let Some(bytes) = bytes_from_os_string(input.as_os_str()) else {
|
||||||
return Err(USimpleError::new(
|
return Err(USimpleError::new(
|
||||||
1,
|
1,
|
||||||
|
|
|
@ -390,3 +390,55 @@ fn slash_eight_off_by_one() {
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only(r"\8");
|
.stdout_only(r"\8");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod posixly_correct {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ignore_options() {
|
||||||
|
for arg in ["--help", "--version", "-E -n 'foo'", "-nE 'foo'"] {
|
||||||
|
new_ucmd!()
|
||||||
|
.env("POSIXLY_CORRECT", "1")
|
||||||
|
.arg(arg)
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only(format!("{arg}\n"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn process_n_option() {
|
||||||
|
new_ucmd!()
|
||||||
|
.env("POSIXLY_CORRECT", "1")
|
||||||
|
.args(&["-n", "foo"])
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("foo");
|
||||||
|
|
||||||
|
// ignore -E & process escapes
|
||||||
|
new_ucmd!()
|
||||||
|
.env("POSIXLY_CORRECT", "1")
|
||||||
|
.args(&["-n", "-E", "foo\\cbar"])
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn process_escapes() {
|
||||||
|
new_ucmd!()
|
||||||
|
.env("POSIXLY_CORRECT", "1")
|
||||||
|
.arg("foo\\n")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("foo\n\n");
|
||||||
|
|
||||||
|
new_ucmd!()
|
||||||
|
.env("POSIXLY_CORRECT", "1")
|
||||||
|
.arg("foo\\tbar")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("foo\tbar\n");
|
||||||
|
|
||||||
|
new_ucmd!()
|
||||||
|
.env("POSIXLY_CORRECT", "1")
|
||||||
|
.arg("foo\\ctbar")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("foo");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue