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/ptx/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ ptx-error-dumb-format = There is no dumb format with GNU extensions disabled
ptx-error-not-implemented = { $feature } not implemented yet
ptx-error-write-failed = write failed
ptx-error-extra-operand = extra operand { $operand }
ptx-invalid-regular-expression = invalid regular expression: { $error }
18 changes: 12 additions & 6 deletions src/uu/ptx/src/ptx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use clap::{Arg, ArgAction, Command};
use regex::Regex;
use thiserror::Error;
use uucore::display::Quotable;
use uucore::error::{FromIo, UError, UResult, UUsageError};
use uucore::error::{FromIo, UError, UResult, USimpleError, UUsageError};
use uucore::format_usage;
use uucore::translate;

Expand Down Expand Up @@ -301,9 +301,15 @@ fn read_input(input_files: &[OsString]) -> std::io::Result<FileMap> {
}

/// Go through every lines in the input files and record each match occurrence as a `WordRef`.
fn create_word_set(config: &Config, filter: &WordFilter, file_map: &FileMap) -> BTreeSet<WordRef> {
let reg = Regex::new(&filter.word_regex).unwrap();
let ref_reg = Regex::new(&config.context_regex).unwrap();
fn create_word_set(
config: &Config,
filter: &WordFilter,
file_map: &FileMap,
) -> UResult<BTreeSet<WordRef>> {
let reg = Regex::new(&filter.word_regex)
.map_err(|e| USimpleError::new(1, translate!("ptx-invalid-regular-expression", "error" => e)))?;
let ref_reg = Regex::new(&config.context_regex)
.map_err(|e| USimpleError::new(1, translate!("ptx-invalid-regular-expression", "error" => e)))?;
let mut word_set: BTreeSet<WordRef> = BTreeSet::new();
for (file, lines) in file_map {
let mut count: usize = 0;
Expand Down Expand Up @@ -342,7 +348,7 @@ fn create_word_set(config: &Config, filter: &WordFilter, file_map: &FileMap) ->
count += 1;
}
}
word_set
Ok(word_set)
}

fn get_reference(config: &Config, word_ref: &WordRef, line: &str, context_reg: &Regex) -> String {
Expand Down Expand Up @@ -884,7 +890,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

let word_filter = WordFilter::new(&matches, &config)?;
let file_map = read_input(&input_files).map_err_context(String::new)?;
let word_set = create_word_set(&config, &word_filter, &file_map);
let word_set = create_word_set(&config, &word_filter, &file_map)?;
write_traditional_output(&mut config, &file_map, &word_set, &output_file)
}

Expand Down
16 changes: 16 additions & 0 deletions tests/by-util/test_ptx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,19 @@ fn test_unicode_truncation_alignment() {
.succeeds()
.stdout_only(" / bar\n föö/\n");
}

#[test]
fn test_invalid_regex_word_trailing_backslash() {
new_ucmd!()
.args(&["-W", "bar\\"])
.fails_with_code(1)
.stderr_contains("ptx: invalid regular expression");
}

#[test]
fn test_invalid_regex_word_unclosed_group() {
new_ucmd!()
.args(&["-W", "(wrong"])
.fails_with_code(1)
.stderr_contains("ptx: invalid regular expression");
}
Loading