Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 31 additions & 25 deletions src/uu/expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use std::str::from_utf8;
use thiserror::Error;
use unicode_width::UnicodeWidthChar;
use uucore::display::Quotable;
use uucore::error::{FromIo, UError, UResult, set_exit_code};
use uucore::error::{FromIo, UError, UResult, USimpleError, set_exit_code};
use uucore::translate;
use uucore::{format_usage, show_error};
use uucore::{format_usage, show};

pub mod options {
pub static TABS: &str = "tabs";
Expand Down Expand Up @@ -296,6 +296,12 @@ fn open(path: &OsString) -> UResult<BufReader<Box<dyn Read + 'static>>> {
Ok(BufReader::new(Box::new(stdin()) as Box<dyn Read>))
} else {
let path_ref = Path::new(path);
if path_ref.is_dir() {
return Err(USimpleError::new(
1,
translate!("expand-error-is-directory", "file" => path.maybe_quote()),
));
}
file_buf = File::open(path_ref).map_err_context(|| path.maybe_quote().to_string())?;
Ok(BufReader::new(Box::new(file_buf) as Box<dyn Read>))
}
Expand Down Expand Up @@ -474,34 +480,34 @@ fn expand_line(
Ok(())
}

fn expand_file(
file: &OsString,
output: &mut BufWriter<std::io::Stdout>,
options: &Options,
) -> UResult<()> {
let mut buf = Vec::new();
let mut input = open(file)?;
let ts = options.tabstops.as_ref();
loop {
match input.read_until(b'\n', &mut buf) {
Ok(0) => break,
Ok(_) => {
expand_line(&mut buf, output, ts, options)
.map_err_context(|| translate!("expand-error-failed-to-write-output"))?;
}
Err(e) => return Err(e.map_err_context(|| file.maybe_quote().to_string())),
}
}
Ok(())
}

fn expand(options: &Options) -> UResult<()> {
let mut output = BufWriter::new(stdout());
let ts = options.tabstops.as_ref();
let mut buf = Vec::new();

for file in &options.files {
if Path::new(file).is_dir() {
show_error!(
"{}",
translate!("expand-error-is-directory", "file" => file.maybe_quote())
);
if let Err(e) = expand_file(file, &mut output, options) {
show!(e);
set_exit_code(1);
continue;
}
match open(file) {
Ok(mut fh) => {
while match fh.read_until(b'\n', &mut buf) {
Ok(s) => s > 0,
Err(_) => buf.is_empty(),
} {
expand_line(&mut buf, &mut output, ts, options)
.map_err_context(|| translate!("expand-error-failed-to-write-output"))?;
}
}
Err(e) => {
show_error!("{e}");
set_exit_code(1);
}
}
}
// Flush once at the end
Expand Down
38 changes: 23 additions & 15 deletions src/uu/unexpand/src/unexpand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::path::Path;
use std::str::from_utf8;
use thiserror::Error;
use uucore::display::Quotable;
use uucore::error::{FromIo, UError, UResult, USimpleError};
use uucore::error::{FromIo, UError, UResult, USimpleError, set_exit_code};
use uucore::translate;
use uucore::{format_usage, show};

Expand Down Expand Up @@ -566,10 +566,28 @@ fn unexpand_line(
Ok(())
}

fn unexpand_file(
file: &OsString,
output: &mut BufWriter<Stdout>,
options: &Options,
lastcol: usize,
tab_config: &TabConfig,
) -> UResult<()> {
let mut buf = Vec::new();
let mut input = open(file)?;
loop {
match input.read_until(b'\n', &mut buf) {
Ok(0) => break,
Ok(_) => unexpand_line(&mut buf, output, options, lastcol, tab_config)?,
Err(e) => return Err(e.map_err_context(|| file.maybe_quote().to_string())),
}
}
Ok(())
}

fn unexpand(options: &Options) -> UResult<()> {
let mut output = BufWriter::new(stdout());
let tab_config = &options.tab_config;
let mut buf = Vec::new();
let lastcol = if tab_config.tabstops.len() > 1
&& tab_config.increment_size.is_none()
&& tab_config.extend_size.is_none()
Expand All @@ -580,19 +598,9 @@ fn unexpand(options: &Options) -> UResult<()> {
};

for file in &options.files {
let mut fh = match open(file) {
Ok(reader) => reader,
Err(err) => {
show!(err);
continue;
}
};

while match fh.read_until(b'\n', &mut buf) {
Ok(s) => s > 0,
Err(_) => !buf.is_empty(),
} {
unexpand_line(&mut buf, &mut output, options, lastcol, tab_config)?;
if let Err(e) = unexpand_file(file, &mut output, options, lastcol, tab_config) {
show!(e);
set_exit_code(1);
}
}
output.flush()?;
Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,15 @@ fn test_nonexisting_file() {
.stdout_contains_line("// !note: file contains significant whitespace");
}

#[test]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
fn test_read_error() {
new_ucmd!()
.arg("/proc/self/mem")
.fails()
.stderr_contains("expand: /proc/self/mem: Input/output error");
}

#[test]
#[cfg(target_os = "linux")]
fn test_expand_non_utf8_paths() {
Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_unexpand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,15 @@ fn test_one_nonexisting_file() {
.stderr_contains("asdf.txt: No such file or directory");
}

#[test]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
fn test_read_error() {
new_ucmd!()
.arg("/proc/self/mem")
.fails()
.stderr_contains("unexpand: /proc/self/mem: Input/output error");
}

#[test]
#[cfg(target_os = "linux")]
fn test_non_utf8_filename() {
Expand Down
Loading