@@ -20,7 +20,7 @@ use std::os::unix::io::RawFd;
2020use std:: time:: Duration ;
2121use std:: net:: { ToSocketAddrs } ;
2222use socket2:: { Socket , Domain } ;
23- use super :: { UtilSetup , ArgsIter } ;
23+ use super :: { UtilSetup , UtilRead , ArgsIter } ;
2424use super :: { MesaError } ;
2525
2626
@@ -48,6 +48,8 @@ struct NcOptions {
4848 zflag : bool ,
4949 timeout : Option < Duration > ,
5050 unix_dg_tmp_socket : String ,
51+ stdin_fd : i32 ,
52+ stderr :
5153}
5254
5355fn mesaerr_result < T > ( err_msg : & str ) -> Result < T , MesaError > {
@@ -68,6 +70,10 @@ fn build_ports(ports: &str) -> Result<Vec<u16>, MesaError>{
6870 Ok ( vec ! ( port_list) )
6971}
7072
73+ fn warn < S : UtilSetup > ( setup : & mut S , msg : & str ) {
74+ let _ = write ! ( setup. error( ) , "{}" , msg) ;
75+ }
76+
7177fn warn ( msg : & str ) {
7278 eprint ! ( "{}" , msg) ;
7379}
@@ -96,7 +102,7 @@ impl NcOptions {
96102 let zflag = matches. is_present ( "z" ) ;
97103 let kflag = matches. is_present ( "k" ) ;
98104
99- /* Cruft to make sure options are clean, and used properly. */
105+ // Cruft to make sure options are clean, and used properly.
100106 let positionals: Vec < & str > = matches. values_of ( "positionals" ) . unwrap ( ) . collect ( ) ;
101107
102108 let family = if matches. is_present ( "U" ) {
@@ -155,7 +161,7 @@ impl NcOptions {
155161
156162 let mut unix_dg_tmp_socket = String :: new ( ) ;
157163
158- /* Get name of temporary socket for unix datagram client */
164+ // Get name of temporary socket for unix datagram client
159165 if family == AF_UNIX && uflag && !lflag {
160166 unix_dg_tmp_socket = if s_addr. is_some ( ) {
161167 s_addr. clone ( ) . unwrap ( )
@@ -183,6 +189,7 @@ impl NcOptions {
183189 timeout : timeout,
184190 unix_dg_tmp_socket : unix_dg_tmp_socket,
185191 zflag : zflag,
192+ stdin_fd : 0
186193 } ;
187194
188195 return Ok ( ret) ;
@@ -231,7 +238,7 @@ impl <'a> NcCore<'a> {
231238
232239 poll : Poll :: new ( ) ?,
233240 net_interest : Ready :: readable ( ) ,
234- event_stdin : EventedFd ( & 0 ) ,
241+ event_stdin : EventedFd ( & opts . stdin_fd ) ,
235242 event_net : EventedFd ( net_fd) ,
236243 event_stdout : EventedFd ( & 1 ) ,
237244 stdinbuf : [ 0 ; BUFSIZE ] ,
@@ -267,20 +274,20 @@ impl <'a> NcCore<'a> {
267274 let mut last_ready_end = -1 ;
268275
269276 loop {
270- /* both inputs are gone, buffers are empty, we are done */
277+ // both inputs are gone, buffers are empty, we are done
271278 if self . stdin_gone ( ) && self . netin_gone ( ) &&
272279 self . stdinbuf_empty ( ) && self . netinbuf_empty ( ) {
273280 // TODO: self.sock.shutdown(std::net::Shutdown::Both)?;
274281 return Ok ( ( ) ) ;
275282 }
276283
277- /* both outputs are gone, we can't continue */
284+ // both outputs are gone, we can't continue
278285 if self . stdout_gone ( ) && self . netout_gone ( ) {
279286 // TODO: self.sock.shutdown(std::net::Shutdown::Both)?;
280287 return Ok ( ( ) ) ;
281288 }
282289
283- /* listen and net in gone, queues empty, done */
290+ // listen and net in gone, queues empty, done
284291 if self . opts . lflag && self . netin_gone ( ) &&
285292 self . stdinbuf_empty ( ) && self . netinbuf_empty ( ) {
286293 // TODO: self.sock.shutdown(std::net::Shutdown::Both)?;
@@ -298,7 +305,7 @@ impl <'a> NcCore<'a> {
298305 return mesaerr_result ( "polling error" ) ;
299306 }
300307
301- /* timeout happened */
308+ // timeout happened
302309 if events. is_empty ( ) {
303310 return Ok ( ( ) ) ;
304311 }
@@ -888,7 +895,7 @@ fn nonunix_client(opts: &NcOptions) -> Result<(), MesaError> {
888895 Ok ( ( ) )
889896}
890897
891- pub fn execute < S , T > ( _setup : & mut S , args : T ) -> Result < ( ) , MesaError >
898+ pub fn execute < S , T > ( setup : & mut S , args : T ) -> Result < ( ) , MesaError >
892899where
893900 S : UtilSetup ,
894901 T : ArgsIter ,
@@ -936,7 +943,18 @@ where
936943 let matches = app. get_matches_from_safe ( args) ?;
937944
938945 debug_info ( & format ! ( "matches = {:?}" , matches) ) ;
939- let opts = NcOptions :: parse ( matches, & help_msg) ?;
946+ let mut opts = NcOptions :: parse ( matches, & help_msg) ?;
947+
948+ // adjust stdin_fd for UtilSetup
949+ // invalid fd is treated as dflag
950+ let stdin_fd = match setup. input ( ) . raw_fd ( ) {
951+ Some ( fd) => fd,
952+ _ => {
953+ opts. dflag = true ;
954+ 0
955+ }
956+ } ;
957+ opts. stdin_fd = stdin_fd;
940958
941959 if opts. lflag {
942960 return server ( & opts) ;
0 commit comments