From 046ff62af6f67296d74323f180d39e3bc1d8883e Mon Sep 17 00:00:00 2001 From: Wim Hueskes Date: Thu, 6 Oct 2016 23:35:16 +0200 Subject: [PATCH] more: fix build on windows --- src/more/Cargo.toml | 4 +++- src/more/more.rs | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/more/Cargo.toml b/src/more/Cargo.toml index 88e88d39c..bac1ac0e0 100644 --- a/src/more/Cargo.toml +++ b/src/more/Cargo.toml @@ -10,9 +10,11 @@ path = "more.rs" [dependencies] getopts = "*" libc = "*" -nix = "*" uucore = { path="../uucore" } +[target."cfg(unix)".dependencies] +nix = "*" + [[bin]] name = "more" path = "main.rs" diff --git a/src/more/more.rs b/src/more/more.rs index 094894cbf..292d77def 100644 --- a/src/more/more.rs +++ b/src/more/more.rs @@ -18,7 +18,9 @@ use getopts::Options; use std::io::{stdout, Write, Read}; use std::fs::File; +#[cfg(unix)] extern crate nix; +#[cfg(unix)] use nix::sys::termios; #[derive(Clone, Eq, PartialEq)] @@ -74,17 +76,39 @@ fn help(usage: &str) { println!("{}", msg); } -fn more(matches: getopts::Matches) { - let files = matches.free; - let mut f = File::open(files.first().unwrap()).unwrap(); - let mut buffer = [0; 1024]; - +#[cfg(unix)] +fn setup_term() -> termios::Termios { let mut term = termios::tcgetattr(0).unwrap(); // Unset canonical mode, so we get characters immediately term.c_lflag.remove(termios::ICANON); // Disable local echo term.c_lflag.remove(termios::ECHO); termios::tcsetattr(0, termios::TCSADRAIN, &term).unwrap(); + term +} + +#[cfg(windows)] +fn setup_term() -> usize { + 0 +} + +#[cfg(unix)] +fn reset_term(term: &mut termios::Termios) { + term.c_lflag.insert(termios::ICANON); + term.c_lflag.insert(termios::ECHO); + termios::tcsetattr(0, termios::TCSADRAIN, &term).unwrap(); +} + +#[cfg(windows)] +fn reset_term(_: &mut usize) { +} + +fn more(matches: getopts::Matches) { + let files = matches.free; + let mut f = File::open(files.first().unwrap()).unwrap(); + let mut buffer = [0; 1024]; + + let mut term = setup_term(); let mut end = false; while let Ok(sz) = f.read(&mut buffer) { @@ -104,8 +128,6 @@ fn more(matches: getopts::Matches) { if end { break;} } - term.c_lflag.insert(termios::ICANON); - term.c_lflag.insert(termios::ECHO); - termios::tcsetattr(0, termios::TCSADRAIN, &term).unwrap(); + reset_term(&mut term); println!(""); }