diff --git a/Cargo.toml b/Cargo.toml index 5b8e9da12b..73e4022c86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -62,4 +62,4 @@ matches = "^0.1.6" byteorder = "^1.2.1" libc = "^0.2.1" getopts = "^0.2" - +isatty = "0.1" diff --git a/src/bin/client-demo.rs b/src/bin/client-demo.rs index bc059a3347..962141720b 100644 --- a/src/bin/client-demo.rs +++ b/src/bin/client-demo.rs @@ -1,16 +1,18 @@ extern crate getopts; +extern crate isatty; extern crate rayon; extern crate serde_json; extern crate solana; use getopts::Options; +use isatty::stdin_isatty; use rayon::prelude::*; use solana::accountant_stub::AccountantStub; use solana::mint::Mint; use solana::signature::{KeyPair, KeyPairUtil}; use solana::transaction::Transaction; use std::env; -use std::io::stdin; +use std::io::{stdin, Read}; use std::net::UdpSocket; use std::process::exit; use std::thread::sleep; @@ -58,7 +60,20 @@ fn main() { if matches.opt_present("t") { threads = matches.opt_str("t").unwrap().parse().expect("integer"); } - let mint: Mint = serde_json::from_reader(stdin()).unwrap_or_else(|e| { + + if stdin_isatty() { + eprintln!("nothing found on stdin, expected a json file"); + exit(1); + } + + let mut buffer = String::new(); + let num_bytes = stdin().read_to_string(&mut buffer).unwrap(); + if num_bytes == 0 { + eprintln!("empty file on stdin, expected a json file"); + exit(1); + } + + let mint: Mint = serde_json::from_str(&buffer).unwrap_or_else(|e| { eprintln!("failed to parse json: {}", e); exit(1); }); diff --git a/src/bin/genesis-demo.rs b/src/bin/genesis-demo.rs index ff983d9a10..340f09c6cf 100644 --- a/src/bin/genesis-demo.rs +++ b/src/bin/genesis-demo.rs @@ -1,13 +1,15 @@ +extern crate isatty; extern crate serde_json; extern crate solana; +use isatty::stdin_isatty; use solana::entry::create_entry; use solana::event::Event; use solana::hash::Hash; use solana::mint::Mint; use solana::signature::{KeyPair, KeyPairUtil, PublicKey}; use solana::transaction::Transaction; -use std::io::stdin; +use std::io::{stdin, Read}; use std::process::exit; fn transfer(from: &KeyPair, (to, tokens): (PublicKey, i64), last_id: Hash) -> Event { @@ -15,7 +17,19 @@ fn transfer(from: &KeyPair, (to, tokens): (PublicKey, i64), last_id: Hash) -> Ev } fn main() { - let mint: Mint = serde_json::from_reader(stdin()).unwrap_or_else(|e| { + if stdin_isatty() { + eprintln!("nothing found on stdin, expected a json file"); + exit(1); + } + + let mut buffer = String::new(); + let num_bytes = stdin().read_to_string(&mut buffer).unwrap(); + if num_bytes == 0 { + eprintln!("empty file on stdin, expected a json file"); + exit(1); + } + + let mint: Mint = serde_json::from_str(&buffer).unwrap_or_else(|e| { eprintln!("failed to parse json: {}", e); exit(1); }); diff --git a/src/bin/genesis.rs b/src/bin/genesis.rs index 10a1437d73..07d7dc89fc 100644 --- a/src/bin/genesis.rs +++ b/src/bin/genesis.rs @@ -1,14 +1,28 @@ //! A command-line executable for generating the chain's genesis block. +extern crate isatty; extern crate serde_json; extern crate solana; +use isatty::stdin_isatty; use solana::mint::Mint; -use std::io::stdin; +use std::io::{stdin, Read}; use std::process::exit; fn main() { - let mint: Mint = serde_json::from_reader(stdin()).unwrap_or_else(|e| { + if stdin_isatty() { + eprintln!("nothing found on stdin, expected a json file"); + exit(1); + } + + let mut buffer = String::new(); + let num_bytes = stdin().read_to_string(&mut buffer).unwrap(); + if num_bytes == 0 { + eprintln!("empty file on stdin, expected a json file"); + exit(1); + } + + let mint: Mint = serde_json::from_str(&buffer).unwrap_or_else(|e| { eprintln!("failed to parse json: {}", e); exit(1); }); diff --git a/src/bin/mint.rs b/src/bin/mint.rs index 9dab5bc76f..73a67fb129 100644 --- a/src/bin/mint.rs +++ b/src/bin/mint.rs @@ -1,16 +1,25 @@ +extern crate isatty; extern crate serde_json; extern crate solana; +use isatty::stdin_isatty; use solana::mint::Mint; use std::io; use std::process::exit; fn main() { let mut input_text = String::new(); + if stdin_isatty() { + eprintln!("nothing found on stdin, expected a token number"); + exit(1); + } + io::stdin().read_line(&mut input_text).unwrap(); let trimmed = input_text.trim(); - let tokens = trimmed.parse::().unwrap(); - + let tokens = trimmed.parse::().unwrap_or_else(|e| { + eprintln!("{}", e); + exit(1); + }); let mint = Mint::new(tokens); let serialized = serde_json::to_string(&mint).unwrap_or_else(|e| { eprintln!("failed to serialize: {}", e); diff --git a/src/bin/testnode.rs b/src/bin/testnode.rs index 4d692f3f3f..3fa995a718 100644 --- a/src/bin/testnode.rs +++ b/src/bin/testnode.rs @@ -1,16 +1,18 @@ extern crate env_logger; extern crate getopts; +extern crate isatty; extern crate serde_json; extern crate solana; use getopts::Options; +use isatty::stdin_isatty; use solana::accountant::Accountant; use solana::accountant_skel::AccountantSkel; use solana::entry::Entry; use solana::event::Event; use solana::historian::Historian; use std::env; -use std::io::{self, stdout, BufRead}; +use std::io::{stdin, stdout, Read}; use std::process::exit; use std::sync::atomic::AtomicBool; use std::sync::{Arc, Mutex}; @@ -47,9 +49,21 @@ fn main() { port = matches.opt_str("p").unwrap().parse().expect("port"); } let addr = format!("0.0.0.0:{}", port); - let stdin = io::stdin(); - let mut entries = stdin.lock().lines().map(|line| { - serde_json::from_str(&line.unwrap()).unwrap_or_else(|e| { + + if stdin_isatty() { + eprintln!("nothing found on stdin, expected a log file"); + exit(1); + } + + let mut buffer = String::new(); + let num_bytes = stdin().read_to_string(&mut buffer).unwrap(); + if num_bytes == 0 { + eprintln!("empty file on stdin, expected a log file"); + exit(1); + } + + let mut entries = buffer.lines().map(|line| { + serde_json::from_str(&line).unwrap_or_else(|e| { eprintln!("failed to parse json: {}", e); exit(1); }) diff --git a/src/lib.rs b/src/lib.rs index 7cb82ff533..80a0f60779 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,8 @@ pub mod accountant_skel; pub mod accountant_stub; pub mod ecdsa; pub mod entry; +#[cfg(feature = "erasure")] +pub mod erasure; pub mod event; pub mod hash; pub mod historian; @@ -17,8 +19,6 @@ pub mod signature; pub mod streamer; pub mod subscribers; pub mod transaction; -#[cfg(feature = "erasure")] -pub mod erasure; extern crate bincode; extern crate byteorder; extern crate chrono;