@@ -7,7 +7,6 @@ use std::{
77} ; 
88
99fn  main ( )  -> Result < ( ) ,  std:: io:: Error >  { 
10-     // Command line arguments using clap 
1110    let  matches = Command :: new ( "safe-ascii" ) 
1211        . version ( "1.1.0" ) 
1312        . about ( "A tool for sanitising ASCII files to printable characters." ) 
@@ -20,7 +19,7 @@ fn main() -> Result<(), std::io::Error> {
2019                . possible_values ( & [ "mnemonic" ,  "escape" ,  "suppress" ] ) 
2120                . long_help ( 
2221                    "mnemonic: abbreviation e.g. (NUL), (SP), (NL) 
23- escape: \\ x sequence, e.g. \\ x00 \\ x20, \\ x0a 
22+ escape: \\ x sequence, e.g. \\ x00,  \\ x20, \\ x0a 
2423suppress: don't print non-printable characters" , 
2524                ) 
2625                . takes_value ( true ) 
@@ -76,25 +75,23 @@ suppress: don't print non-printable characters",
7675    if  let  Some ( values)  = matches. values_of ( "files" )  { 
7776        // files 
7877        for  filename in  values { 
79-             // stdin 
8078            if  filename == "-"  { 
81-                 process_file ( io:: stdin ( ) ,  & mapping,  & mut  truncate) ?; 
82-                 continue ; 
83-             } 
84- 
85-             let  file = File :: open ( filename) ; 
86-             match  file { 
87-                 Ok ( file)  => { 
88-                     let  reader = BufReader :: new ( file) ; 
89-                     process_file ( reader,  & mapping,  & mut  truncate) ?; 
90-                 } 
91-                 Err ( err)  => { 
92-                     eprintln ! ( 
93-                         "{}: {}: {}" , 
94-                         env:: args( ) . next( ) . expect( "Cannot obtain executable name" ) , 
95-                         filename, 
96-                         err
97-                     ) 
79+                 try_process_file ( io:: stdin ( ) ,  & mapping,  & mut  truncate) ?; 
80+             }  else  { 
81+                 let  file = File :: open ( filename) ; 
82+                 match  file { 
83+                     Ok ( file)  => { 
84+                         let  buf_reader = BufReader :: new ( file) ; 
85+                         try_process_file ( buf_reader,  & mapping,  & mut  truncate) ?
86+                     } 
87+                     Err ( err)  => { 
88+                         eprintln ! ( 
89+                             "{}: {}: {}" , 
90+                             env:: args( ) . next( ) . expect( "Cannot obtain executable name" ) , 
91+                             filename, 
92+                             err
93+                         ) ; 
94+                     } 
9895                } 
9996            } 
10097
@@ -104,23 +101,37 @@ suppress: don't print non-printable characters",
104101            } 
105102        } 
106103    }  else  { 
107-         process_file ( io:: stdin ( ) ,  & mapping,  & mut  truncate) ?; 
104+         try_process_file ( std :: io:: stdin ( ) ,  & mapping,  & mut  truncate) ?
108105    } 
109106
110107    Ok ( ( ) ) 
111108} 
112109
110+ fn  try_process_file < R :  io:: Read > ( 
111+     reader :  R , 
112+     mapping :  & AsciiMapping , 
113+     truncate :  & mut  i128 , 
114+ )  -> Result < ( ) ,  std:: io:: Error >  { 
115+     if  let  Err ( e)  = process_file ( reader,  mapping,  truncate)  { 
116+         if  e. kind ( )  == std:: io:: ErrorKind :: BrokenPipe  { 
117+             std:: process:: exit ( 141 ) ; 
118+         } 
119+         Err ( e) ?
120+     } ; 
121+     Ok ( ( ) ) 
122+ } 
123+ 
113124// Process files with Read trait 
114125fn  process_file < R :  io:: Read > ( 
115-     f :  R , 
126+     reader :  R , 
116127    mapping :  & AsciiMapping , 
117128    truncate :  & mut  i128 , 
118129)  -> Result < ( ) ,  std:: io:: Error >  { 
119130    let  stdout = io:: stdout ( ) ; 
120131    let  mut  handle = stdout. lock ( ) ; 
121132
122-     for  b  in  f . bytes ( )  { 
123-         match  b  { 
133+     for  byte  in  reader . bytes ( )  { 
134+         match  byte  { 
124135            Ok ( c)  => { 
125136                if  ( 33 ..=126 ) . contains ( & c)  { 
126137                    handle. write_all ( & [ c] ) ?; 
0 commit comments