From aac1d40f64295519713feb7c3f4dedcc141e78b7 Mon Sep 17 00:00:00 2001 From: Knight Date: Sat, 21 May 2016 18:06:09 +0800 Subject: [PATCH 1/2] dircolors: bypass the missing SHELL on windows --- src/dircolors/dircolors.rs | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/dircolors/dircolors.rs b/src/dircolors/dircolors.rs index f6224a36a..440153127 100644 --- a/src/dircolors/dircolors.rs +++ b/src/dircolors/dircolors.rs @@ -35,21 +35,14 @@ enum OutputFmt { Unknown, } -macro_rules! disp_err { - ($($args:tt)+) => ({ - pipe_write!(&mut ::std::io::stderr(), "{}: ", NAME); - pipe_writeln!(&mut ::std::io::stderr(), $($args)+); - pipe_writeln!(&mut ::std::io::stderr(), "Try '{} --help' for more information.", NAME); - }) -} - fn guess_syntax() -> OutputFmt { + use std::path; match env::var("SHELL") { Ok(s) => { if s.is_empty() { return OutputFmt::Unknown; } - if let Some(last) = s.rsplit('/').next() { + if let Some(last) = s.rsplit(path::MAIN_SEPARATOR).next() { if last == "csh" || last == "tcsh" { OutputFmt::CShell } else { @@ -79,7 +72,10 @@ pub fn uumain(args: Vec) -> i32 { let matches = match opts.parse(&args[1..]) { Ok(m) => m, - Err(f) => crash!(1, "Invalid options\n{}", f), + Err(f) => { + disp_err!("{}", f); + return 1; + } }; if matches.opt_present("help") { @@ -175,13 +171,14 @@ For details on the format of these files, run 'dircolors --print-database'.", } trait StrUtils { + /// Remove comments and trim whitespaces fn purify(&self) -> &Self; + /// Like split_whitespace() but only produce 2 components fn split_two(&self) -> (&str, &str); fn fnmatch(&self, pattern: &str) -> bool; } impl StrUtils for str { - /// Remove comments and trim whitespaces fn purify(&self) -> &Self { let mut line = self; for (n, c) in self.chars().enumerate() { @@ -204,7 +201,6 @@ impl StrUtils for str { line.trim() } - /// Like split_whitespace() but only produce 2 components fn split_two(&self) -> (&str, &str) { if let Some(b) = self.find(char::is_whitespace) { let key = &self[..b]; @@ -342,7 +338,7 @@ fn parse(lines: T, fmt: OutputFmt, fp: &str) -> Result #[test] fn test_shell_syntax() { use std::env; - let last = env!("SHELL"); + let last = env::var("SHELL"); env::set_var("SHELL", "/path/csh"); assert_eq!(OutputFmt::CShell, guess_syntax()); env::set_var("SHELL", "csh"); @@ -351,8 +347,6 @@ fn test_shell_syntax() { assert_eq!(OutputFmt::Shell, guess_syntax()); env::set_var("SHELL", "bash"); assert_eq!(OutputFmt::Shell, guess_syntax()); - env::set_var("SHELL", "bash"); - assert_eq!(OutputFmt::Shell, guess_syntax()); env::set_var("SHELL", "/asd/bar"); assert_eq!(OutputFmt::Shell, guess_syntax()); env::set_var("SHELL", "foo"); @@ -362,7 +356,9 @@ fn test_shell_syntax() { env::remove_var("SHELL"); assert_eq!(OutputFmt::Unknown, guess_syntax()); - env::set_var("SHELL", last); + if let Ok(s) = last { + env::set_var("SHELL", s); + } } #[test] From a196bebd1b10ec8de3154944b053ed976864a353 Mon Sep 17 00:00:00 2001 From: Knight Date: Sat, 21 May 2016 18:19:13 +0800 Subject: [PATCH 2/2] uucore: add disp_err macro --- src/uucore/macros.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/uucore/macros.rs b/src/uucore/macros.rs index 8789b7e6c..21691dc5c 100644 --- a/src/uucore/macros.rs +++ b/src/uucore/macros.rs @@ -43,6 +43,15 @@ macro_rules! show_info( }) ); +#[macro_export] +macro_rules! disp_err( + ($($args:tt)+) => ({ + pipe_write!(&mut ::std::io::stderr(), "{}: ", executable!()); + pipe_writeln!(&mut ::std::io::stderr(), $($args)+); + pipe_writeln!(&mut ::std::io::stderr(), "Try '{} --help' for more information.", executable!()); + }) +); + #[macro_export] macro_rules! eprint( ($($args:tt)+) => (pipe_write!(&mut ::std::io::stderr(), $($args)+))