Skip to content

Commit 7bffd7d

Browse files
committed
ptx: fixes
1 parent a64fce8 commit 7bffd7d

File tree

3 files changed

+70
-32
lines changed

3 files changed

+70
-32
lines changed

src/uu/ptx/src/ptx.rs

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ use std::fs::File;
1515
use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write};
1616
use std::num::ParseIntError;
1717
use uucore::display::Quotable;
18-
use uucore::error::{FromIo, UError, UResult};
18+
use uucore::error::{FromIo, UError, UResult, UUsageError};
1919
use uucore::{format_usage, help_about, help_usage};
2020

2121
const USAGE: &str = help_usage!("ptx.md");
2222
const ABOUT: &str = help_about!("ptx.md");
2323

24-
const REGEX_CHARCLASS: &str = "^-]\\";
25-
2624
#[derive(Debug)]
2725
enum OutFormat {
2826
Dumb,
@@ -161,11 +159,7 @@ impl WordFilter {
161159
break_set
162160
.unwrap()
163161
.into_iter()
164-
.map(|c| if REGEX_CHARCLASS.contains(c) {
165-
format!("\\{c}")
166-
} else {
167-
c.to_string()
168-
})
162+
.map(|c| regex::escape(&c.to_string()))
169163
.collect::<String>()
170164
)
171165
} else if config.gnu_ext {
@@ -260,10 +254,17 @@ fn get_config(matches: &clap::ArgMatches) -> UResult<Config> {
260254
.parse()
261255
.map_err(PtxError::ParseError)?;
262256
}
263-
if matches.get_flag(options::FORMAT_ROFF) {
257+
if let Some(format) = matches.get_one::<String>(options::FORMAT) {
258+
config.format = match format.as_str() {
259+
"roff" => OutFormat::Roff,
260+
"tex" => OutFormat::Tex,
261+
_ => panic!("should be caught by clap"),
262+
};
263+
}
264+
if matches.get_flag(options::format::ROFF) {
264265
config.format = OutFormat::Roff;
265266
}
266-
if matches.get_flag(options::FORMAT_TEX) {
267+
if matches.get_flag(options::format::TEX) {
267268
config.format = OutFormat::Tex;
268269
}
269270
Ok(config)
@@ -344,7 +345,7 @@ fn create_word_set(config: &Config, filter: &WordFilter, file_map: &FileMap) ->
344345
continue;
345346
}
346347
if config.ignore_case {
347-
word = word.to_lowercase();
348+
word = word.to_uppercase();
348349
}
349350
word_set.insert(WordRef {
350351
word,
@@ -693,15 +694,19 @@ fn write_traditional_output(
693694
}
694695

695696
mod options {
697+
pub mod format {
698+
pub static ROFF: &str = "roff";
699+
pub static TEX: &str = "tex";
700+
}
701+
696702
pub static FILE: &str = "file";
697703
pub static AUTO_REFERENCE: &str = "auto-reference";
698704
pub static TRADITIONAL: &str = "traditional";
699705
pub static FLAG_TRUNCATION: &str = "flag-truncation";
700706
pub static MACRO_NAME: &str = "macro-name";
701-
pub static FORMAT_ROFF: &str = "format=roff";
707+
pub static FORMAT: &str = "format";
702708
pub static RIGHT_SIDE_REFS: &str = "right-side-refs";
703709
pub static SENTENCE_REGEXP: &str = "sentence-regexp";
704-
pub static FORMAT_TEX: &str = "format=tex";
705710
pub static WORD_REGEXP: &str = "word-regexp";
706711
pub static BREAK_FILE: &str = "break-file";
707712
pub static IGNORE_CASE: &str = "ignore-case";
@@ -715,21 +720,34 @@ mod options {
715720
#[uucore::main]
716721
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
717722
let matches = uu_app().try_get_matches_from(args)?;
723+
let config = get_config(&matches)?;
718724

719-
let mut input_files: Vec<String> = match &matches.get_many::<String>(options::FILE) {
720-
Some(v) => v.clone().cloned().collect(),
721-
None => vec!["-".to_string()],
722-
};
725+
let input_files;
726+
let output_file;
727+
728+
let mut files = matches
729+
.get_many::<String>(options::FILE)
730+
.into_iter()
731+
.flatten()
732+
.cloned();
733+
734+
if !config.gnu_ext {
735+
input_files = vec![files.next().unwrap_or("-".to_string())];
736+
output_file = files.next().unwrap_or("-".to_string());
737+
if let Some(file) = files.next() {
738+
return Err(UUsageError::new(
739+
1,
740+
format!("extra operand {}", file.quote()),
741+
));
742+
}
743+
} else {
744+
input_files = files.collect::<Vec<_>>();
745+
output_file = "-".to_string();
746+
}
723747

724-
let config = get_config(&matches)?;
725748
let word_filter = WordFilter::new(&matches, &config)?;
726749
let file_map = read_input(&input_files, &config).map_err_context(String::new)?;
727750
let word_set = create_word_set(&config, &word_filter, &file_map);
728-
let output_file = if !config.gnu_ext && input_files.len() == 2 {
729-
input_files.pop().unwrap()
730-
} else {
731-
"-".to_string()
732-
};
733751
write_traditional_output(&config, &file_map, &word_set, &output_file)
734752
}
735753

@@ -774,10 +792,24 @@ pub fn uu_app() -> Command {
774792
.value_name("STRING"),
775793
)
776794
.arg(
777-
Arg::new(options::FORMAT_ROFF)
795+
Arg::new(options::FORMAT)
796+
.long(options::FORMAT)
797+
.hide(true)
798+
.value_parser(["roff", "tex"])
799+
.overrides_with_all([options::FORMAT, options::format::ROFF, options::format::TEX]),
800+
)
801+
.arg(
802+
Arg::new(options::format::ROFF)
778803
.short('O')
779-
.long(options::FORMAT_ROFF)
780804
.help("generate output as roff directives")
805+
.overrides_with_all([options::FORMAT, options::format::ROFF, options::format::TEX])
806+
.action(ArgAction::SetTrue),
807+
)
808+
.arg(
809+
Arg::new(options::format::TEX)
810+
.short('T')
811+
.help("generate output as TeX directives")
812+
.overrides_with_all([options::FORMAT, options::format::ROFF, options::format::TEX])
781813
.action(ArgAction::SetTrue),
782814
)
783815
.arg(
@@ -794,13 +826,6 @@ pub fn uu_app() -> Command {
794826
.help("for end of lines or end of sentences")
795827
.value_name("REGEXP"),
796828
)
797-
.arg(
798-
Arg::new(options::FORMAT_TEX)
799-
.short('T')
800-
.long(options::FORMAT_TEX)
801-
.help("generate output as TeX directives")
802-
.action(ArgAction::SetTrue),
803-
)
804829
.arg(
805830
Arg::new(options::WORD_REGEXP)
806831
.short('W')

tests/by-util/test_ptx.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,12 @@ fn gnu_ext_disabled_empty_word_regexp_ignores_break_file() {
112112
.succeeds()
113113
.stdout_only_fixture("gnu_ext_disabled_rightward_no_ref.expected");
114114
}
115+
116+
#[test]
117+
fn test_ignore_case() {
118+
new_ucmd!()
119+
.args(&["-G", "-f"])
120+
.pipe_in("a b c _")
121+
.succeeds()
122+
.stdout_only_fixture("ignore_case.expected");
123+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.xx "" "" "a b c _" ""
2+
.xx "" "a" "b c _" ""
3+
.xx "" "a b" "c _" ""
4+
.xx "" "a b c" "_" ""

0 commit comments

Comments
 (0)