@@ -20,7 +20,7 @@ use std::os::unix::io::RawFd;
20
20
use std:: time:: Duration ;
21
21
use std:: net:: { ToSocketAddrs } ;
22
22
use socket2:: { Socket , Domain } ;
23
- use super :: { UtilSetup , ArgsIter } ;
23
+ use super :: { UtilSetup , UtilRead , ArgsIter } ;
24
24
use super :: { MesaError } ;
25
25
26
26
@@ -48,6 +48,8 @@ struct NcOptions {
48
48
zflag : bool ,
49
49
timeout : Option < Duration > ,
50
50
unix_dg_tmp_socket : String ,
51
+ stdin_fd : i32 ,
52
+ stderr :
51
53
}
52
54
53
55
fn mesaerr_result < T > ( err_msg : & str ) -> Result < T , MesaError > {
@@ -68,6 +70,10 @@ fn build_ports(ports: &str) -> Result<Vec<u16>, MesaError>{
68
70
Ok ( vec ! ( port_list) )
69
71
}
70
72
73
+ fn warn < S : UtilSetup > ( setup : & mut S , msg : & str ) {
74
+ let _ = write ! ( setup. error( ) , "{}" , msg) ;
75
+ }
76
+
71
77
fn warn ( msg : & str ) {
72
78
eprint ! ( "{}" , msg) ;
73
79
}
@@ -96,7 +102,7 @@ impl NcOptions {
96
102
let zflag = matches. is_present ( "z" ) ;
97
103
let kflag = matches. is_present ( "k" ) ;
98
104
99
- /* Cruft to make sure options are clean, and used properly. */
105
+ // Cruft to make sure options are clean, and used properly.
100
106
let positionals: Vec < & str > = matches. values_of ( "positionals" ) . unwrap ( ) . collect ( ) ;
101
107
102
108
let family = if matches. is_present ( "U" ) {
@@ -155,7 +161,7 @@ impl NcOptions {
155
161
156
162
let mut unix_dg_tmp_socket = String :: new ( ) ;
157
163
158
- /* Get name of temporary socket for unix datagram client */
164
+ // Get name of temporary socket for unix datagram client
159
165
if family == AF_UNIX && uflag && !lflag {
160
166
unix_dg_tmp_socket = if s_addr. is_some ( ) {
161
167
s_addr. clone ( ) . unwrap ( )
@@ -183,6 +189,7 @@ impl NcOptions {
183
189
timeout : timeout,
184
190
unix_dg_tmp_socket : unix_dg_tmp_socket,
185
191
zflag : zflag,
192
+ stdin_fd : 0
186
193
} ;
187
194
188
195
return Ok ( ret) ;
@@ -231,7 +238,7 @@ impl <'a> NcCore<'a> {
231
238
232
239
poll : Poll :: new ( ) ?,
233
240
net_interest : Ready :: readable ( ) ,
234
- event_stdin : EventedFd ( & 0 ) ,
241
+ event_stdin : EventedFd ( & opts . stdin_fd ) ,
235
242
event_net : EventedFd ( net_fd) ,
236
243
event_stdout : EventedFd ( & 1 ) ,
237
244
stdinbuf : [ 0 ; BUFSIZE ] ,
@@ -267,20 +274,20 @@ impl <'a> NcCore<'a> {
267
274
let mut last_ready_end = -1 ;
268
275
269
276
loop {
270
- /* both inputs are gone, buffers are empty, we are done */
277
+ // both inputs are gone, buffers are empty, we are done
271
278
if self . stdin_gone ( ) && self . netin_gone ( ) &&
272
279
self . stdinbuf_empty ( ) && self . netinbuf_empty ( ) {
273
280
// TODO: self.sock.shutdown(std::net::Shutdown::Both)?;
274
281
return Ok ( ( ) ) ;
275
282
}
276
283
277
- /* both outputs are gone, we can't continue */
284
+ // both outputs are gone, we can't continue
278
285
if self . stdout_gone ( ) && self . netout_gone ( ) {
279
286
// TODO: self.sock.shutdown(std::net::Shutdown::Both)?;
280
287
return Ok ( ( ) ) ;
281
288
}
282
289
283
- /* listen and net in gone, queues empty, done */
290
+ // listen and net in gone, queues empty, done
284
291
if self . opts . lflag && self . netin_gone ( ) &&
285
292
self . stdinbuf_empty ( ) && self . netinbuf_empty ( ) {
286
293
// TODO: self.sock.shutdown(std::net::Shutdown::Both)?;
@@ -298,7 +305,7 @@ impl <'a> NcCore<'a> {
298
305
return mesaerr_result ( "polling error" ) ;
299
306
}
300
307
301
- /* timeout happened */
308
+ // timeout happened
302
309
if events. is_empty ( ) {
303
310
return Ok ( ( ) ) ;
304
311
}
@@ -888,7 +895,7 @@ fn nonunix_client(opts: &NcOptions) -> Result<(), MesaError> {
888
895
Ok ( ( ) )
889
896
}
890
897
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 >
892
899
where
893
900
S : UtilSetup ,
894
901
T : ArgsIter ,
@@ -936,7 +943,18 @@ where
936
943
let matches = app. get_matches_from_safe ( args) ?;
937
944
938
945
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;
940
958
941
959
if opts. lflag {
942
960
return server ( & opts) ;
0 commit comments