Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit 2f46b4a

Browse files
author
Alex Helfet
committed
Read from a file (or pipe) or stdin.
1 parent 50f92a1 commit 2f46b4a

File tree

1 file changed

+26
-56
lines changed

1 file changed

+26
-56
lines changed

src/bin/itmdump.rs

Lines changed: 26 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,14 @@ extern crate libc;
1111
extern crate log;
1212
extern crate ref_slice;
1313

14-
use std::io::{Read, Write};
15-
use std::path::PathBuf;
16-
use std::time::Duration;
17-
use std::{env, fs, io, process, thread};
18-
19-
#[cfg(not(unix))]
20-
use std::fs::OpenOptions;
21-
22-
#[cfg(unix)]
23-
use std::ffi::CString;
24-
#[cfg(unix)]
25-
use std::fs::File;
26-
#[cfg(unix)]
27-
use std::os::unix::ffi::OsStringExt;
2814

2915
use clap::{Arg, App, ArgMatches};
3016
use log::{LogRecord, LogLevelFilter};
3117
use ref_slice::ref_slice_mut;
18+
use std::fs::File;
19+
use std::io::{Read, Write};
20+
use std::time::Duration;
21+
use std::{env, io, process, thread};
3222

3323
use errors::*;
3424

@@ -79,9 +69,16 @@ fn main() {
7969
fn run() -> Result<()> {
8070
let matches = App::new("itmdump")
8171
.version(include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt")))
82-
.arg(Arg::with_name("PATH")
83-
.help("Named pipe to use")
84-
.required(true))
72+
.about("\n\
73+
Reads data from an ARM CPU ITM and decodes it. \n\
74+
\n\
75+
Input is from an existing file (or named pipe) at a \
76+
supplied path, or else from standard input.")
77+
.arg(Arg::with_name("file")
78+
.long("file")
79+
.short("f")
80+
.help("Path to file (or named pipe) to read from")
81+
.takes_value(true))
8582
.arg(Arg::with_name("port")
8683
.long("stimulus")
8784
.short("s")
@@ -150,43 +147,16 @@ fn run() -> Result<()> {
150147
}
151148
}
152149

153-
fn open_read(matches: &ArgMatches) -> Result<impl io::Read> {
154-
let pipe = PathBuf::from(matches.value_of("PATH").unwrap());
155-
let pipe_ = pipe.display();
156-
157-
if pipe.exists() {
158-
try!(fs::remove_file(&pipe)
159-
.chain_err(|| format!("couldn't remove {}", pipe_)));
160-
}
161-
162-
Ok(
163-
if cfg!(unix) {
164-
// Use a named pipe.
165-
let cpipe =
166-
try!(CString::new(pipe.clone().into_os_string().into_vec())
167-
.chain_err(|| {
168-
format!("error converting {} to a C string", pipe_)
169-
}));
170-
171-
match unsafe { libc::mkfifo(cpipe.as_ptr(), 0o644) } {
172-
0 => {}
173-
e => {
174-
try!(Err(io::Error::from_raw_os_error(e)).chain_err(|| {
175-
format!("couldn't create a named pipe in {}", pipe_)
176-
}))
177-
}
178-
}
179-
180-
try!(File::open(&pipe)
181-
.chain_err(|| format!("couldn't open {}", pipe_)))
182-
} else {
183-
// Not unix.
184-
try!(OpenOptions::new()
185-
.create(true)
186-
.read(true)
187-
.write(true)
188-
.open(&pipe)
189-
.chain_err(|| format!("couldn't open {}", pipe_)))
190-
}
191-
)
150+
fn open_read<'a>(matches: &ArgMatches) -> Result<impl io::Read + 'a> {
151+
let path = matches.value_of("file");
152+
Ok(match path {
153+
Some(path) => {
154+
let f =
155+
File::open(path)
156+
.chain_err(|| format!("Couldn't open source file '{}'",
157+
path))?;
158+
Box::new(f) as Box<io::Read + 'static>
159+
},
160+
None => Box::new(io::stdin()) as Box<io::Read + 'static>,
161+
})
192162
}

0 commit comments

Comments
 (0)