@@ -11,24 +11,14 @@ extern crate libc;
11
11
extern crate log;
12
12
extern crate ref_slice;
13
13
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 ;
28
14
29
15
use clap:: { Arg , App , ArgMatches } ;
30
16
use log:: { LogRecord , LogLevelFilter } ;
31
17
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} ;
32
22
33
23
use errors:: * ;
34
24
@@ -79,9 +69,16 @@ fn main() {
79
69
fn run ( ) -> Result < ( ) > {
80
70
let matches = App :: new ( "itmdump" )
81
71
. 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 ) )
85
82
. arg ( Arg :: with_name ( "port" )
86
83
. long ( "stimulus" )
87
84
. short ( "s" )
@@ -150,43 +147,16 @@ fn run() -> Result<()> {
150
147
}
151
148
}
152
149
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
+ } )
192
162
}
0 commit comments