mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
echo: write using locked stdout
This commit is contained in:
parent
23f6dbf2d3
commit
42794e9f11
2 changed files with 51 additions and 14 deletions
|
@ -13,6 +13,7 @@
|
|||
#[macro_use]
|
||||
extern crate uucore;
|
||||
|
||||
use std::io::{self, Write};
|
||||
use std::iter::Peekable;
|
||||
use std::str::Chars;
|
||||
|
||||
|
@ -36,7 +37,12 @@ const HELP: &str = r#"
|
|||
\\xHH byte with hexadecimal value HH (1 to 2 digits)
|
||||
"#;
|
||||
|
||||
fn parse_code(input: &mut Peekable<Chars>, base: u32, max_digits: u32, bits_per_digit: u32) -> Option<char> {
|
||||
fn parse_code(
|
||||
input: &mut Peekable<Chars>,
|
||||
base: u32,
|
||||
max_digits: u32,
|
||||
bits_per_digit: u32,
|
||||
) -> Option<char> {
|
||||
let mut ret = 0x80000000;
|
||||
for _ in 0..max_digits {
|
||||
match input.peek().and_then(|c| c.to_digit(base)) {
|
||||
|
@ -48,9 +54,15 @@ fn parse_code(input: &mut Peekable<Chars>, base: u32, max_digits: u32, bits_per_
|
|||
std::char::from_u32(ret)
|
||||
}
|
||||
|
||||
fn print_escaped(input: &str, should_stop: &mut bool) {
|
||||
fn print_escaped(input: &str, mut output: impl Write) -> io::Result<bool> {
|
||||
let mut should_stop = false;
|
||||
|
||||
let mut buffer = ['\\'; 2];
|
||||
|
||||
let mut iter = input.chars().peekable();
|
||||
while let Some(mut c) = iter.next() {
|
||||
let mut start = 1;
|
||||
|
||||
if c == '\\' {
|
||||
if let Some(next) = iter.next() {
|
||||
c = match next {
|
||||
|
@ -58,7 +70,7 @@ fn print_escaped(input: &str, should_stop: &mut bool) {
|
|||
'a' => '\x07',
|
||||
'b' => '\x08',
|
||||
'c' => {
|
||||
*should_stop = true;
|
||||
should_stop = true;
|
||||
break
|
||||
},
|
||||
'e' => '\x1b',
|
||||
|
@ -68,22 +80,30 @@ fn print_escaped(input: &str, should_stop: &mut bool) {
|
|||
't' => '\t',
|
||||
'v' => '\x0b',
|
||||
'x' => parse_code(&mut iter, 16, 2, 4).unwrap_or_else(|| {
|
||||
print!("\\");
|
||||
start = 0;
|
||||
next
|
||||
}),
|
||||
'0' => parse_code(&mut iter, 8, 3, 3).unwrap_or_else(|| {
|
||||
print!("\\");
|
||||
start = 0;
|
||||
next
|
||||
}),
|
||||
_ => {
|
||||
print!("\\");
|
||||
start = 0;
|
||||
next
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
print!("{}", c);
|
||||
|
||||
buffer[1] = c;
|
||||
|
||||
// because printing char slices is apparently not available in the standard library
|
||||
for ch in &buffer[start..] {
|
||||
write!(output, "{}", ch)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(should_stop)
|
||||
}
|
||||
|
||||
pub fn uumain(args: Vec<String>) -> i32 {
|
||||
|
@ -96,24 +116,36 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
|||
let no_newline = matches.opt_present("n");
|
||||
let escaped = matches.opt_present("e");
|
||||
|
||||
for (i, input) in matches.free.iter().enumerate() {
|
||||
match execute(no_newline, escaped, matches.free) {
|
||||
Ok(_) => 0,
|
||||
Err(f) => {
|
||||
show_error!("{}", f);
|
||||
1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn execute(no_newline: bool, escaped: bool, free: Vec<String>) -> io::Result<()> {
|
||||
let stdout = io::stdout();
|
||||
let mut output = stdout.lock();
|
||||
|
||||
for (i, input) in free.iter().enumerate() {
|
||||
if i > 0 {
|
||||
print!(" ");
|
||||
write!(output, " ")?;
|
||||
}
|
||||
if escaped {
|
||||
let mut should_stop = false;
|
||||
print_escaped(&input, &mut should_stop);
|
||||
let should_stop = print_escaped(&input, &mut output)?;
|
||||
if should_stop {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
print!("{}", input);
|
||||
write!(output, "{}", input)?;
|
||||
}
|
||||
}
|
||||
|
||||
if !no_newline {
|
||||
println!();
|
||||
writeln!(output)?;
|
||||
}
|
||||
|
||||
0
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -63,6 +63,11 @@ fn test_escape_one_slash() {
|
|||
new_ucmd!().args(&["-e", "foo\\ bar"]).succeeds().stdout_only("foo\\ bar");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_escape_one_slash_multi() {
|
||||
new_ucmd!().args(&["-e", "foo\\", "bar"]).succeeds().stdout_only("foo\\ bar");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_escape_newline() {
|
||||
new_ucmd!().args(&["-e", "\\na"]).succeeds().stdout_only("\na\n");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue