Skip to content
Open
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
1 change: 1 addition & 0 deletions src/uu/od/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ od-error-invalid-endian = Invalid argument --endian={$endian}
od-error-invalid-inputs = Invalid inputs: {$msg}
od-error-too-large = value is too large
od-error-radix-invalid = invalid output address radix '{$radix}'; it must be one character from [doxn]
od-error-no-type-with-strings = no type may be specified when dumping strings
Comment thread
Logic769 marked this conversation as resolved.
Resolved
od-error-radix-empty = Radix cannot be empty, and must be one of [o, d, x, n]
od-error-invalid-width = invalid width {$width}; using {$min} instead
od-error-missing-format-spec = missing format specification after '--format' / '-t'
Expand Down
1 change: 1 addition & 0 deletions src/uu/od/locales/fr-FR.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ od-error-invalid-endian = Argument invalide --endian={$endian}
od-error-invalid-inputs = Entrées invalides : {$msg}
od-error-too-large = la valeur est trop grande
od-error-radix-invalid = base d'adresse de sortie invalide '{$radix}' ; elle doit être un caractère parmi [doxn]
od-error-no-type-with-strings = aucun type ne peut être spécifié lors de l'affichage des chaînes
od-error-radix-empty = Radix ne peut pas être vide, et doit être l'un de [o, d, x, n]
od-error-invalid-width = largeur invalide {$width} ; utilisation de {$min} à la place
od-error-missing-format-spec = spécification de format manquante après '--format' / '-t'
Expand Down
14 changes: 13 additions & 1 deletion src/uu/od/src/od.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use crate::parse_nrofbytes::{parse_number_of_bytes, parse_width};
use crate::partial_reader::PartialReader;
use crate::peek_reader::{PeekRead, PeekReader};
use crate::prn_char::format_ascii_dump;
use crate::prn_int::FORMAT_ITEM_OCT16;
use clap::ArgAction;
use clap::{Arg, ArgMatches, Command, parser::ValueSource};
use std::ffi::OsString;
Expand Down Expand Up @@ -138,7 +139,11 @@ impl OdOptions {
}
};

let formats = parse_format_flags(args).map_err(|e| USimpleError::new(1, e))?;
let mut formats = parse_format_flags(args).map_err(|e| USimpleError::new(1, e))?;
let has_type_option = !formats.is_empty();
if formats.is_empty() {
formats.push(ParsedFormatterItemInfo::new(FORMAT_ITEM_OCT16, false)); // 2 byte octal is the default
}

let mut line_bytes = if let (Some(s), Some(ValueSource::CommandLine)) = (
matches.get_one::<String>(options::WIDTH),
Expand Down Expand Up @@ -195,6 +200,13 @@ impl OdOptions {
let read_bytes = parse_bytes_option(matches, args, diag_args, options::READ_BYTES, 'N')?;

let strings = parse_bytes_option(matches, args, diag_args, options::STRINGS, 'S')?;
if strings.is_some() && has_type_option {
return Err(USimpleError::new(
1,
translate!("od-error-no-type-with-strings"),
));
}

let string_min_length = match strings {
None => None,
Some(n) => Some(usize::try_from(n).map_err(|_| {
Expand Down
19 changes: 9 additions & 10 deletions src/uu/od/src/parse_formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,6 @@ pub fn parse_format_flags(args: &[String]) -> Result<Vec<ParsedFormatterItemInfo
return Err(translate!("od-error-missing-format-spec"));
}

if formats.is_empty() {
formats.push(ParsedFormatterItemInfo::new(FORMAT_ITEM_OCT16, false)); // 2 byte octal is the default
}

Ok(formats)
}

Expand Down Expand Up @@ -365,13 +361,16 @@ fn parse_type_string(params: &str) -> Result<Vec<ParsedFormatterItemInfo>, Strin
#[cfg(test)]
pub fn parse_format_flags_str(args_str: &[&'static str]) -> Result<Vec<FormatterItemInfo>, String> {
let args: Vec<String> = args_str.iter().map(|s| (*s).to_string()).collect();
parse_format_flags(&args).map(|v| {
let mut formats = parse_format_flags(&args)?;
if formats.is_empty() {
formats.push(ParsedFormatterItemInfo::new(FORMAT_ITEM_OCT16, false)); // 2 byte octal is the default
}
Ok(formats
.into_iter()
// tests using this function assume add_ascii_dump is not set
v.into_iter()
.inspect(|f| assert!(!f.add_ascii_dump))
.map(|f| f.formatter_item_info)
.collect()
})
.inspect(|f| assert!(!f.add_ascii_dump))
.map(|f| f.formatter_item_info)
.collect())
}

#[test]
Expand Down
16 changes: 16 additions & 0 deletions tests/by-util/test_od.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1792,3 +1792,19 @@ fn test_od_zero_byte_type_size() {
.succeeds()
.stdout_is("0000000 64636261\n0000004\n");
}

#[test]
fn test_strings_with_type_option_is_error() {
for args in [
&["--strings=1", "-t", "x1", "a.txt"][..],
&["--strings=1", "--format=x1", "a.txt"][..],
&["-S1", "-x", "a.txt"][..],
] {
let (at, mut ucmd) = at_and_ucmd!();
at.write("a.txt", "a\n");
ucmd.args(args)
.fails()
.code_is(1)
.stderr_contains("no type may be specified when dumping strings");
}
}
Loading